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

Revision 229, 2.8 KB (checked in by trnewman, 15 years ago)

Fixed typos

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        uint32_t socket;
24    socket = ClientSocket("localhost", "40000");
25   
26    return socket;
27}
28
29uint32_t
30GetNumParameters()
31{
32        uint32_t socketfd, numParameters;
33    char buffer[256];
34
35    socketfd = ConnectToRemoteComponent();
36    SendMessage(socketfd,"get_number_parameters");
37   
38    memset(buffer, 0, 256);
39    ReadMessage(socketfd, buffer);
40    numParameters = atoi(buffer);
41       
42        return numParameters;
43}
44
45/* Given a certain set of observables, ask the radio to find the optimum radio
46 * parameters and return them.
47 *
48 * TODO I'm a little confused about this function... why would anyone need to
49 * use this?  Shouldn't this be internal to the radio operation?
50 *
51 * TODO this function is returning a pointer to allocated memory, which is fine,
52 * but we need to document this and make sure the caller is deallocating the
53 * memory when it is done using it.
54 */
55Parameter* GetOptimalParameters(Observable *radioObservables, uint32_t numObservables,
56                Parameter *currentParameters, uint32_t numCurrentParameters) {
57    uint32_t i,socketfd,numParameters;
58    char var[50];
59    char counter[55];
60        char buffer[256];
61
62    uint32_t socketFD = ConnectToRemoteComponent();
63    SendMessage(socketFD, "request_optimization");
64
65    /* Get number of observables to send.  This information needs to be
66     * sent to the Cognitive Radio Shell also.   
67     */
68
69    // Send Observables
70    sprintf(counter, "%i", numObservables);
71    SendMessage(socketFD, counter);
72    for(size_t i = 0; i < numObservables; i++) {
73        SendMessage(socketFD, radioObservables[i].name.c_str());
74        sprintf(var, "%f", radioObservables[i].value);
75        SendMessage(socketFD, var);     
76    }
77
78    // Send Parameters
79    memset(counter, 0, 55);
80    sprintf(counter, "%i", numCurrentParameters);
81    SendMessage(socketFD, counter);
82       
83        for(i = 0; i < numCurrentParameters; i++) {
84        SendMessage(socketFD,currentParameters[i].name.c_str());
85        sprintf(var,"%f",currentParameters[i].value);
86        SendMessage(socketFD,var);     
87    }
88
89    /* Receive Set of Parameters */
90    memset(buffer, 0, 256);
91    ReadMessage(socketFD, buffer);
92    numParameters = atoi(buffer);
93    Parameter *pList = new Parameter[numParameters];
94   
95    for(ssize_t i = 0; i < numParameters; i++) {
96        memset(buffer, 0, 256);
97        ReadMessage(socketFD, buffer);
98        pList[i].name = std::string(buffer);
99       
100                memset(buffer, 0, 256);
101        ReadMessage(socketFD, buffer);
102        pList[i].value = atof(buffer);
103    }   
104
105    return pList;
106}
107
Note: See TracBrowser for help on using the browser.