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

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

Added python swig interface

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
[285]69    //memset(buffer, 0, 256);
70    //ReadMessage(socketfd, buffer);
[280]71
[285]72    return 1;
[280]73}
74
75uint32_t
[228]76GetNumParameters()
77{
78    char buffer[256];
[227]79
[231]80    uint32_t socketfd = ConnectToRemoteComponent();
81    SendMessage(socketfd, "get_number_parameters");
[228]82   
83    memset(buffer, 0, 256);
84    ReadMessage(socketfd, buffer);
[231]85    uint32_t numParameters = atoi(buffer);
[232]86   
87    return numParameters;
[228]88}
[231]89// end previous TODO
[228]90
[222]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?
[227]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.
[222]100 */
[231]101Parameter*
102GetOptimalParameters(Observable *radioObservables, uint32_t numObservables,
[232]103        Parameter *currentParameters, uint32_t numCurrentParameters)
[231]104{
[222]105    char var[50];
106    char counter[55];
[232]107    char buffer[256];
[222]108
[227]109    uint32_t socketFD = ConnectToRemoteComponent();
110    SendMessage(socketFD, "request_optimization");
[222]111
112    /* Get number of observables to send.  This information needs to be
113     * sent to the Cognitive Radio Shell also.   
114     */
115
[231]116    /* Send Observables */
[227]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);
[232]122        SendMessage(socketFD, var);   
[222]123    }
124
[231]125    /* Send Parameters */
[228]126    memset(counter, 0, 55);
[229]127    sprintf(counter, "%i", numCurrentParameters);
128    SendMessage(socketFD, counter);
[232]129    for(size_t i = 0; i < numCurrentParameters; i++) {
[229]130        SendMessage(socketFD,currentParameters[i].name.c_str());
[228]131        sprintf(var,"%f",currentParameters[i].value);
[232]132        SendMessage(socketFD,var);   
[228]133    }
[229]134
[222]135    /* Receive Set of Parameters */
136    memset(buffer, 0, 256);
[227]137    ReadMessage(socketFD, buffer);
[231]138    uint32_t numParameters = atoi(buffer);
[227]139    Parameter *pList = new Parameter[numParameters];
[222]140   
[227]141    for(ssize_t i = 0; i < numParameters; i++) {
[222]142        memset(buffer, 0, 256);
[227]143        ReadMessage(socketFD, buffer);
[222]144        pList[i].name = std::string(buffer);
145       
[232]146        memset(buffer, 0, 256);
[227]147        ReadMessage(socketFD, buffer);
[222]148        pList[i].value = atof(buffer);
149    }   
150
151    return pList;
152}
[227]153
[230]154bool
155UpdateParameterPerformance(Parameter *p, uint32_t numParameters, Observable *o,
[232]156        uint32_t numObservables)
[230]157{
[232]158    char counter[55];
[230]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);
[232]168   
169    for(size_t i = 0; i < numParameters; i++) {
[231]170        SendMessage(socketFD, p[i].name.c_str());
171        sprintf(var, "%f", p[i].value);
[232]172        SendMessage(socketFD, var);   
[230]173    }
174   
[232]175    // Send Observables
[230]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);
[232]181        SendMessage(socketFD, var);   
[230]182    }
183}
[299]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.