[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] | 20 | uint32_t |
---|
| 21 | ConnectToRemoteComponent() |
---|
| 22 | { |
---|
| 23 | uint32_t socket; |
---|
| 24 | socket = ClientSocket("localhost", "40000"); |
---|
| 25 | |
---|
| 26 | return socket; |
---|
| 27 | } |
---|
| 28 | |
---|
[228] | 29 | uint32_t |
---|
| 30 | GetNumParameters() |
---|
| 31 | { |
---|
| 32 | uint32_t socketfd, numParameters; |
---|
| 33 | char buffer[256]; |
---|
[227] | 34 | |
---|
[228] | 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 | |
---|
[222] | 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? |
---|
[227] | 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. |
---|
[222] | 54 | */ |
---|
[228] | 55 | Parameter* GetOptimalParameters(Observable *radioObservables, uint32_t numObservables, |
---|
| 56 | Parameter *currentParameters, uint32_t numCurrentParameters) { |
---|
| 57 | uint32_t i,socketfd,numParameters; |
---|
[222] | 58 | char var[50]; |
---|
| 59 | char counter[55]; |
---|
| 60 | char buffer[256]; |
---|
| 61 | |
---|
[227] | 62 | uint32_t socketFD = ConnectToRemoteComponent(); |
---|
| 63 | SendMessage(socketFD, "request_optimization"); |
---|
[222] | 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 |
---|
[227] | 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); |
---|
[222] | 76 | } |
---|
| 77 | |
---|
[229] | 78 | // Send Parameters |
---|
[228] | 79 | memset(counter, 0, 55); |
---|
[229] | 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()); |
---|
[228] | 85 | sprintf(var,"%f",currentParameters[i].value); |
---|
[229] | 86 | SendMessage(socketFD,var); |
---|
[228] | 87 | } |
---|
[229] | 88 | |
---|
[222] | 89 | /* Receive Set of Parameters */ |
---|
| 90 | memset(buffer, 0, 256); |
---|
[227] | 91 | ReadMessage(socketFD, buffer); |
---|
[229] | 92 | numParameters = atoi(buffer); |
---|
[227] | 93 | Parameter *pList = new Parameter[numParameters]; |
---|
[222] | 94 | |
---|
[227] | 95 | for(ssize_t i = 0; i < numParameters; i++) { |
---|
[222] | 96 | memset(buffer, 0, 256); |
---|
[227] | 97 | ReadMessage(socketFD, buffer); |
---|
[222] | 98 | pList[i].name = std::string(buffer); |
---|
| 99 | |
---|
| 100 | memset(buffer, 0, 256); |
---|
[227] | 101 | ReadMessage(socketFD, buffer); |
---|
[222] | 102 | pList[i].value = atof(buffer); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | return pList; |
---|
| 106 | } |
---|
[227] | 107 | |
---|