root/vtcross/trunk/src/libvtcross/libvtcross.cpp @ 280

Revision 280, 4.7 KB (checked in by trnewman, 15 years ago)

added set active mission functionality to library

RevLine 
[161]1/* Virginia Tech Cognitive Radio Open Source Systems
2 * Virginia Tech, 2009
3 *
4 * LICENSE INFORMATION GOES HERE
5 */
6
7/* Implementation file for the VCROSS Cognitive Radio public API defined in
8 * include/libvtcross.h.
9 *
10 * MORE INFO HERE
11 */
12
[226]13#include <cstdlib>
14
[161]15#include "vtcross/common.h"
[222]16#include "vtcross/debug.h"
[226]17#include "vtcross/libvtcross.h"
[161]18
[227]19
[222]20uint32_t
21ConnectToRemoteComponent()
22{
[231]23    // TODO why is this hardcoded like this??
[232]24       return ClientSocket("localhost", "40000");
[222]25}
26
[231]27// TODO the following three functions all do exactly the same thing.  Why not
28// simply combine them?
[228]29uint32_t
[230]30GetNumObservables()
31{
32    char buffer[256];
33
[231]34    uint32_t socketfd = ConnectToRemoteComponent();
35    SendMessage(socketfd, "get_number_observables");
[230]36   
37    memset(buffer, 0, 256);
38    ReadMessage(socketfd, buffer);
[231]39    uint32_t numObservables = atoi(buffer);
[232]40   
41    return numObservables;
[230]42}
43
44uint32_t
45GetNumUtilities()
46{
47    char buffer[256];
48
[231]49    uint32_t socketfd = ConnectToRemoteComponent();
50    SendMessage(socketfd, "get_number_utilities");
[230]51   
52    memset(buffer, 0, 256);
53    ReadMessage(socketfd, buffer);
[231]54    uint32_t numUtilities = atoi(buffer);
[232]55   
56    return numUtilities;
[230]57}
58
59uint32_t
[280]60SetActiveMission(char * activeMission)
61{
62    char buffer[256];
63   
64    uint32_t socketfd = ConnectToRemoteComponent();
65    SendMessage(socketfd, "set_active_mission");
66
67    SendMessage(socketfd, activeMission);
68
69    memset(buffer, 0, 256);
70    ReadMessage(socketfd, buffer);
71    uint32_t ret = atoi(buffer);
72
73    return ret;
74}
75
76uint32_t
[228]77GetNumParameters()
78{
79    char buffer[256];
[227]80
[231]81    uint32_t socketfd = ConnectToRemoteComponent();
82    SendMessage(socketfd, "get_number_parameters");
[228]83   
84    memset(buffer, 0, 256);
85    ReadMessage(socketfd, buffer);
[231]86    uint32_t numParameters = atoi(buffer);
[232]87   
88    return numParameters;
[228]89}
[231]90// end previous TODO
[228]91
[222]92/* Given a certain set of observables, ask the radio to find the optimum radio
93 * parameters and return them.
94 *
95 * TODO I'm a little confused about this function... why would anyone need to
96 * use this?  Shouldn't this be internal to the radio operation?
[227]97 *
98 * TODO this function is returning a pointer to allocated memory, which is fine,
99 * but we need to document this and make sure the caller is deallocating the
100 * memory when it is done using it.
[222]101 */
[231]102Parameter*
103GetOptimalParameters(Observable *radioObservables, uint32_t numObservables,
[232]104        Parameter *currentParameters, uint32_t numCurrentParameters)
[231]105{
[222]106    char var[50];
107    char counter[55];
[232]108    char buffer[256];
[222]109
[227]110    uint32_t socketFD = ConnectToRemoteComponent();
111    SendMessage(socketFD, "request_optimization");
[222]112
113    /* Get number of observables to send.  This information needs to be
114     * sent to the Cognitive Radio Shell also.   
115     */
116
[231]117    /* Send Observables */
[227]118    sprintf(counter, "%i", numObservables);
119    SendMessage(socketFD, counter);
120    for(size_t i = 0; i < numObservables; i++) {
121        SendMessage(socketFD, radioObservables[i].name.c_str());
122        sprintf(var, "%f", radioObservables[i].value);
[232]123        SendMessage(socketFD, var);   
[222]124    }
125
[231]126    /* Send Parameters */
[228]127    memset(counter, 0, 55);
[229]128    sprintf(counter, "%i", numCurrentParameters);
129    SendMessage(socketFD, counter);
[232]130    for(size_t i = 0; i < numCurrentParameters; i++) {
[229]131        SendMessage(socketFD,currentParameters[i].name.c_str());
[228]132        sprintf(var,"%f",currentParameters[i].value);
[232]133        SendMessage(socketFD,var);   
[228]134    }
[229]135
[222]136    /* Receive Set of Parameters */
137    memset(buffer, 0, 256);
[227]138    ReadMessage(socketFD, buffer);
[231]139    uint32_t numParameters = atoi(buffer);
[227]140    Parameter *pList = new Parameter[numParameters];
[222]141   
[227]142    for(ssize_t i = 0; i < numParameters; i++) {
[222]143        memset(buffer, 0, 256);
[227]144        ReadMessage(socketFD, buffer);
[222]145        pList[i].name = std::string(buffer);
146       
[232]147        memset(buffer, 0, 256);
[227]148        ReadMessage(socketFD, buffer);
[222]149        pList[i].value = atof(buffer);
150    }   
151
152    return pList;
153}
[227]154
[230]155bool
156UpdateParameterPerformance(Parameter *p, uint32_t numParameters, Observable *o,
[232]157        uint32_t numObservables)
[230]158{
[232]159    char counter[55];
[230]160    char var[50];
161
162    uint32_t socketFD = ConnectToRemoteComponent();
163    SendMessage(socketFD, "update_performance");
164   
165    // Send Parameters
166    memset(counter, 0, 55);
167    sprintf(counter, "%i", numParameters);
168    SendMessage(socketFD, counter);
[232]169   
170    for(size_t i = 0; i < numParameters; i++) {
[231]171        SendMessage(socketFD, p[i].name.c_str());
172        sprintf(var, "%f", p[i].value);
[232]173        SendMessage(socketFD, var);   
[230]174    }
175   
[232]176    // Send Observables
[230]177    sprintf(counter, "%i", numObservables);
178    SendMessage(socketFD, counter);
179    for(size_t i = 0; i < numObservables; i++) {
180        SendMessage(socketFD, o[i].name.c_str());
181        sprintf(var, "%f", o[i].value);
[232]182        SendMessage(socketFD, var);   
[230]183    }
184}
Note: See TracBrowser for help on using the browser.