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

Revision 299, 6.5 KB (checked in by trnewman, 15 years ago)

Added python swig interface

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
72    return 1;
73}
74
75uint32_t
76GetNumParameters()
77{
78    char buffer[256];
79
80    uint32_t socketfd = ConnectToRemoteComponent();
81    SendMessage(socketfd, "get_number_parameters");
82   
83    memset(buffer, 0, 256);
84    ReadMessage(socketfd, buffer);
85    uint32_t numParameters = atoi(buffer);
86   
87    return numParameters;
88}
89// end previous TODO
90
91/* Given a certain set of observables, ask the radio to find the optimum radio
92 * parameters and return them.
93 *
94 * TODO I'm a little confused about this function... why would anyone need to
95 * use this?  Shouldn't this be internal to the radio operation?
96 *
97 * TODO this function is returning a pointer to allocated memory, which is fine,
98 * but we need to document this and make sure the caller is deallocating the
99 * memory when it is done using it.
100 */
101Parameter*
102GetOptimalParameters(Observable *radioObservables, uint32_t numObservables,
103        Parameter *currentParameters, uint32_t numCurrentParameters)
104{
105    char var[50];
106    char counter[55];
107    char buffer[256];
108
109    uint32_t socketFD = ConnectToRemoteComponent();
110    SendMessage(socketFD, "request_optimization");
111
112    /* Get number of observables to send.  This information needs to be
113     * sent to the Cognitive Radio Shell also.   
114     */
115
116    /* Send Observables */
117    sprintf(counter, "%i", numObservables);
118    SendMessage(socketFD, counter);
119    for(size_t i = 0; i < numObservables; i++) {
120        SendMessage(socketFD, radioObservables[i].name.c_str());
121        sprintf(var, "%f", radioObservables[i].value);
122        SendMessage(socketFD, var);   
123    }
124
125    /* Send Parameters */
126    memset(counter, 0, 55);
127    sprintf(counter, "%i", numCurrentParameters);
128    SendMessage(socketFD, counter);
129    for(size_t i = 0; i < numCurrentParameters; i++) {
130        SendMessage(socketFD,currentParameters[i].name.c_str());
131        sprintf(var,"%f",currentParameters[i].value);
132        SendMessage(socketFD,var);   
133    }
134
135    /* Receive Set of Parameters */
136    memset(buffer, 0, 256);
137    ReadMessage(socketFD, buffer);
138    uint32_t numParameters = atoi(buffer);
139    Parameter *pList = new Parameter[numParameters];
140   
141    for(ssize_t i = 0; i < numParameters; i++) {
142        memset(buffer, 0, 256);
143        ReadMessage(socketFD, buffer);
144        pList[i].name = std::string(buffer);
145       
146        memset(buffer, 0, 256);
147        ReadMessage(socketFD, buffer);
148        pList[i].value = atof(buffer);
149    }   
150
151    return pList;
152}
153
154bool
155UpdateParameterPerformance(Parameter *p, uint32_t numParameters, Observable *o,
156        uint32_t numObservables)
157{
158    char counter[55];
159    char var[50];
160
161    uint32_t socketFD = ConnectToRemoteComponent();
162    SendMessage(socketFD, "update_performance");
163   
164    // Send Parameters
165    memset(counter, 0, 55);
166    sprintf(counter, "%i", numParameters);
167    SendMessage(socketFD, counter);
168   
169    for(size_t i = 0; i < numParameters; i++) {
170        SendMessage(socketFD, p[i].name.c_str());
171        sprintf(var, "%f", p[i].value);
172        SendMessage(socketFD, var);   
173    }
174   
175    // Send Observables
176    sprintf(counter, "%i", numObservables);
177    SendMessage(socketFD, counter);
178    for(size_t i = 0; i < numObservables; i++) {
179        SendMessage(socketFD, o[i].name.c_str());
180        sprintf(var, "%f", o[i].value);
181        SendMessage(socketFD, var);   
182    }
183}
184
185bool ActivateComponent(uint32_t id) {
186    return 1;
187}
188bool DeactivateComponent(uint32_t id) {
189    return 1;
190}
191bool DisconnectComponent(uint32_t id) {
192    return 1;
193}
194
195Component* GetComponentInformation(uint32_t id) {
196
197}
198
199/* View components currently connected to the radio by id.
200 *
201 * TODO Should there be another way to list components? If you have 10 cognitive
202 * engines, how are you going to know which is which just by id?
203 */
204uint32_t* GetConnectedCognitiveEngines() {
205}
206uint32_t* GetConnectedPolicyEngines(){
207}
208uint32_t* GetConnectedManagementServiceLayers(){
209}
210uint32_t* GetConnectedComponents(){
211}
212
213
214/* View data from the current status of the radio.
215 *
216 * This function allows client code to capture radio properties at any certain
217 * instant.  Note, however, that these properties could be changing at very
218 * rapid rates. There is no guarantee that the return results from these
219 * functions will still be valid by the time the client code receives them.
220 */
221Observable* GetRadioObservables() {
222}
223Parameter* GetRadioParameters(){
224}
225Utility* GetRadioUtilities(){
226}
227
228
229/* Parses VTCROSS XML configuration file and uses it to configure the radio.
230 *
231 * This function *must* be called when the radio first starts up, and may be
232 * called at any point after that to reconfigure the radio.
233 */
234bool ParseRadioConfiguration(){
235}
236
237
238/* Lists current radio configuration options loaded from the configuration XML
239 * file.
240 *
241 * TODO How are we listing these?  Are we simply returning them to stdout?
242 * Logging them? Returning strings?  Need to figure this out...
243 */
244void ListCurrentRadioConfiguration(){
245}
246
247/* Shut down the radio.
248 *
249 * This function will deactivate and disconnect all radio components before
250 * finally shutting down the shell and stopping radio operations.
251 */
252bool Shutdown(){
253}
254
Note: See TracBrowser for help on using the browser.