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

Line 
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
13#include <cstdlib>
14
15#include "vtcross/common.h"
16#include "vtcross/debug.h"
17#include "vtcross/libvtcross.h"
18
19
20uint32_t
21ConnectToRemoteComponent()
22{
23    // TODO why is this hardcoded like this??
24       return ClientSocket("localhost", "40000");
25}
26
27// TODO the following three functions all do exactly the same thing.  Why not
28// simply combine them?
29uint32_t
30GetNumObservables()
31{
32    char buffer[256];
33
34    uint32_t socketfd = ConnectToRemoteComponent();
35    SendMessage(socketfd, "get_number_observables");
36   
37    memset(buffer, 0, 256);
38    ReadMessage(socketfd, buffer);
39    uint32_t numObservables = atoi(buffer);
40   
41    return numObservables;
42}
43
44uint32_t
45GetNumUtilities()
46{
47    char buffer[256];
48
49    uint32_t socketfd = ConnectToRemoteComponent();
50    SendMessage(socketfd, "get_number_utilities");
51   
52    memset(buffer, 0, 256);
53    ReadMessage(socketfd, buffer);
54    uint32_t numUtilities = atoi(buffer);
55   
56    return numUtilities;
57}
58
59uint32_t
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
77GetNumParameters()
78{
79    char buffer[256];
80
81    uint32_t socketfd = ConnectToRemoteComponent();
82    SendMessage(socketfd, "get_number_parameters");
83   
84    memset(buffer, 0, 256);
85    ReadMessage(socketfd, buffer);
86    uint32_t numParameters = atoi(buffer);
87   
88    return numParameters;
89}
90// end previous TODO
91
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?
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.
101 */
102Parameter*
103GetOptimalParameters(Observable *radioObservables, uint32_t numObservables,
104        Parameter *currentParameters, uint32_t numCurrentParameters)
105{
106    char var[50];
107    char counter[55];
108    char buffer[256];
109
110    uint32_t socketFD = ConnectToRemoteComponent();
111    SendMessage(socketFD, "request_optimization");
112
113    /* Get number of observables to send.  This information needs to be
114     * sent to the Cognitive Radio Shell also.   
115     */
116
117    /* Send Observables */
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);
123        SendMessage(socketFD, var);   
124    }
125
126    /* Send Parameters */
127    memset(counter, 0, 55);
128    sprintf(counter, "%i", numCurrentParameters);
129    SendMessage(socketFD, counter);
130    for(size_t i = 0; i < numCurrentParameters; i++) {
131        SendMessage(socketFD,currentParameters[i].name.c_str());
132        sprintf(var,"%f",currentParameters[i].value);
133        SendMessage(socketFD,var);   
134    }
135
136    /* Receive Set of Parameters */
137    memset(buffer, 0, 256);
138    ReadMessage(socketFD, buffer);
139    uint32_t numParameters = atoi(buffer);
140    Parameter *pList = new Parameter[numParameters];
141   
142    for(ssize_t i = 0; i < numParameters; i++) {
143        memset(buffer, 0, 256);
144        ReadMessage(socketFD, buffer);
145        pList[i].name = std::string(buffer);
146       
147        memset(buffer, 0, 256);
148        ReadMessage(socketFD, buffer);
149        pList[i].value = atof(buffer);
150    }   
151
152    return pList;
153}
154
155bool
156UpdateParameterPerformance(Parameter *p, uint32_t numParameters, Observable *o,
157        uint32_t numObservables)
158{
159    char counter[55];
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);
169   
170    for(size_t i = 0; i < numParameters; i++) {
171        SendMessage(socketFD, p[i].name.c_str());
172        sprintf(var, "%f", p[i].value);
173        SendMessage(socketFD, var);   
174    }
175   
176    // Send Observables
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);
182        SendMessage(socketFD, var);   
183    }
184}
Note: See TracBrowser for help on using the browser.