[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 | { |
---|
[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] | 29 | uint32_t |
---|
[230] | 30 | GetNumObservables() |
---|
| 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 | |
---|
| 44 | uint32_t |
---|
| 45 | GetNumUtilities() |
---|
| 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 | |
---|
| 59 | uint32_t |
---|
[228] | 60 | GetNumParameters() |
---|
| 61 | { |
---|
| 62 | char buffer[256]; |
---|
[227] | 63 | |
---|
[231] | 64 | uint32_t socketfd = ConnectToRemoteComponent(); |
---|
| 65 | SendMessage(socketfd, "get_number_parameters"); |
---|
[228] | 66 | |
---|
| 67 | memset(buffer, 0, 256); |
---|
| 68 | ReadMessage(socketfd, buffer); |
---|
[231] | 69 | uint32_t numParameters = atoi(buffer); |
---|
[232] | 70 | |
---|
| 71 | return numParameters; |
---|
[228] | 72 | } |
---|
[231] | 73 | // end previous TODO |
---|
[228] | 74 | |
---|
[222] | 75 | /* Given a certain set of observables, ask the radio to find the optimum radio |
---|
| 76 | * parameters and return them. |
---|
| 77 | * |
---|
| 78 | * TODO I'm a little confused about this function... why would anyone need to |
---|
| 79 | * use this? Shouldn't this be internal to the radio operation? |
---|
[227] | 80 | * |
---|
| 81 | * TODO this function is returning a pointer to allocated memory, which is fine, |
---|
| 82 | * but we need to document this and make sure the caller is deallocating the |
---|
| 83 | * memory when it is done using it. |
---|
[222] | 84 | */ |
---|
[231] | 85 | Parameter* |
---|
| 86 | GetOptimalParameters(Observable *radioObservables, uint32_t numObservables, |
---|
[232] | 87 | Parameter *currentParameters, uint32_t numCurrentParameters) |
---|
[231] | 88 | { |
---|
[222] | 89 | char var[50]; |
---|
| 90 | char counter[55]; |
---|
[232] | 91 | char buffer[256]; |
---|
[222] | 92 | |
---|
[227] | 93 | uint32_t socketFD = ConnectToRemoteComponent(); |
---|
| 94 | SendMessage(socketFD, "request_optimization"); |
---|
[222] | 95 | |
---|
| 96 | /* Get number of observables to send. This information needs to be |
---|
| 97 | * sent to the Cognitive Radio Shell also. |
---|
| 98 | */ |
---|
| 99 | |
---|
[231] | 100 | /* Send Observables */ |
---|
[227] | 101 | sprintf(counter, "%i", numObservables); |
---|
| 102 | SendMessage(socketFD, counter); |
---|
| 103 | for(size_t i = 0; i < numObservables; i++) { |
---|
| 104 | SendMessage(socketFD, radioObservables[i].name.c_str()); |
---|
| 105 | sprintf(var, "%f", radioObservables[i].value); |
---|
[232] | 106 | SendMessage(socketFD, var); |
---|
[222] | 107 | } |
---|
| 108 | |
---|
[231] | 109 | /* Send Parameters */ |
---|
[228] | 110 | memset(counter, 0, 55); |
---|
[229] | 111 | sprintf(counter, "%i", numCurrentParameters); |
---|
| 112 | SendMessage(socketFD, counter); |
---|
[232] | 113 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
[229] | 114 | SendMessage(socketFD,currentParameters[i].name.c_str()); |
---|
[228] | 115 | sprintf(var,"%f",currentParameters[i].value); |
---|
[232] | 116 | SendMessage(socketFD,var); |
---|
[228] | 117 | } |
---|
[229] | 118 | |
---|
[222] | 119 | /* Receive Set of Parameters */ |
---|
| 120 | memset(buffer, 0, 256); |
---|
[227] | 121 | ReadMessage(socketFD, buffer); |
---|
[231] | 122 | uint32_t numParameters = atoi(buffer); |
---|
[227] | 123 | Parameter *pList = new Parameter[numParameters]; |
---|
[222] | 124 | |
---|
[227] | 125 | for(ssize_t i = 0; i < numParameters; i++) { |
---|
[222] | 126 | memset(buffer, 0, 256); |
---|
[227] | 127 | ReadMessage(socketFD, buffer); |
---|
[222] | 128 | pList[i].name = std::string(buffer); |
---|
| 129 | |
---|
[232] | 130 | memset(buffer, 0, 256); |
---|
[227] | 131 | ReadMessage(socketFD, buffer); |
---|
[222] | 132 | pList[i].value = atof(buffer); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | return pList; |
---|
| 136 | } |
---|
[227] | 137 | |
---|
[230] | 138 | bool |
---|
| 139 | UpdateParameterPerformance(Parameter *p, uint32_t numParameters, Observable *o, |
---|
[232] | 140 | uint32_t numObservables) |
---|
[230] | 141 | { |
---|
[232] | 142 | char counter[55]; |
---|
[230] | 143 | char var[50]; |
---|
| 144 | |
---|
| 145 | uint32_t socketFD = ConnectToRemoteComponent(); |
---|
| 146 | SendMessage(socketFD, "update_performance"); |
---|
| 147 | |
---|
| 148 | // Send Parameters |
---|
| 149 | memset(counter, 0, 55); |
---|
| 150 | sprintf(counter, "%i", numParameters); |
---|
| 151 | SendMessage(socketFD, counter); |
---|
[232] | 152 | |
---|
| 153 | for(size_t i = 0; i < numParameters; i++) { |
---|
[231] | 154 | SendMessage(socketFD, p[i].name.c_str()); |
---|
| 155 | sprintf(var, "%f", p[i].value); |
---|
[232] | 156 | SendMessage(socketFD, var); |
---|
[230] | 157 | } |
---|
| 158 | |
---|
[232] | 159 | // Send Observables |
---|
[230] | 160 | sprintf(counter, "%i", numObservables); |
---|
| 161 | SendMessage(socketFD, counter); |
---|
| 162 | for(size_t i = 0; i < numObservables; i++) { |
---|
| 163 | SendMessage(socketFD, o[i].name.c_str()); |
---|
| 164 | sprintf(var, "%f", o[i].value); |
---|
[232] | 165 | SendMessage(socketFD, var); |
---|
[230] | 166 | } |
---|
| 167 | } |
---|