[412] | 1 | /* |
---|
| 2 | Copyright 2009 Virginia Polytechnic Institute and State University |
---|
[161] | 3 | |
---|
[412] | 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 5 | you may not use this file except in compliance with the License. |
---|
| 6 | You may obtain a copy of the License at |
---|
| 7 | |
---|
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 9 | |
---|
| 10 | Unless required by applicable law or agreed to in writing, software |
---|
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 13 | See the License for the specific language governing permissions and |
---|
| 14 | limitations under the License. |
---|
| 15 | */ |
---|
| 16 | |
---|
[161] | 17 | /* Implementation file for the VCROSS Cognitive Radio public API defined in |
---|
| 18 | * include/libvtcross.h. |
---|
| 19 | * |
---|
| 20 | * MORE INFO HERE |
---|
| 21 | */ |
---|
| 22 | |
---|
[226] | 23 | #include <cstdlib> |
---|
| 24 | |
---|
[161] | 25 | #include "vtcross/common.h" |
---|
[222] | 26 | #include "vtcross/debug.h" |
---|
[226] | 27 | #include "vtcross/libvtcross.h" |
---|
[161] | 28 | |
---|
[227] | 29 | |
---|
[222] | 30 | uint32_t |
---|
| 31 | ConnectToRemoteComponent() |
---|
| 32 | { |
---|
[231] | 33 | // TODO why is this hardcoded like this?? |
---|
[232] | 34 | return ClientSocket("localhost", "40000"); |
---|
[222] | 35 | } |
---|
| 36 | |
---|
[231] | 37 | // TODO the following three functions all do exactly the same thing. Why not |
---|
| 38 | // simply combine them? |
---|
[228] | 39 | uint32_t |
---|
[230] | 40 | GetNumObservables() |
---|
| 41 | { |
---|
| 42 | char buffer[256]; |
---|
| 43 | |
---|
[231] | 44 | uint32_t socketfd = ConnectToRemoteComponent(); |
---|
| 45 | SendMessage(socketfd, "get_number_observables"); |
---|
[230] | 46 | |
---|
| 47 | memset(buffer, 0, 256); |
---|
| 48 | ReadMessage(socketfd, buffer); |
---|
[231] | 49 | uint32_t numObservables = atoi(buffer); |
---|
[232] | 50 | |
---|
| 51 | return numObservables; |
---|
[230] | 52 | } |
---|
| 53 | |
---|
| 54 | uint32_t |
---|
| 55 | GetNumUtilities() |
---|
| 56 | { |
---|
| 57 | char buffer[256]; |
---|
| 58 | |
---|
[231] | 59 | uint32_t socketfd = ConnectToRemoteComponent(); |
---|
| 60 | SendMessage(socketfd, "get_number_utilities"); |
---|
[230] | 61 | |
---|
| 62 | memset(buffer, 0, 256); |
---|
| 63 | ReadMessage(socketfd, buffer); |
---|
[231] | 64 | uint32_t numUtilities = atoi(buffer); |
---|
[232] | 65 | |
---|
| 66 | return numUtilities; |
---|
[230] | 67 | } |
---|
| 68 | |
---|
| 69 | uint32_t |
---|
[280] | 70 | SetActiveMission(char * activeMission) |
---|
| 71 | { |
---|
| 72 | char buffer[256]; |
---|
| 73 | |
---|
| 74 | uint32_t socketfd = ConnectToRemoteComponent(); |
---|
| 75 | SendMessage(socketfd, "set_active_mission"); |
---|
| 76 | |
---|
| 77 | SendMessage(socketfd, activeMission); |
---|
| 78 | |
---|
[285] | 79 | //memset(buffer, 0, 256); |
---|
| 80 | //ReadMessage(socketfd, buffer); |
---|
[280] | 81 | |
---|
[285] | 82 | return 1; |
---|
[280] | 83 | } |
---|
| 84 | |
---|
| 85 | uint32_t |
---|
[228] | 86 | GetNumParameters() |
---|
| 87 | { |
---|
| 88 | char buffer[256]; |
---|
[227] | 89 | |
---|
[231] | 90 | uint32_t socketfd = ConnectToRemoteComponent(); |
---|
| 91 | SendMessage(socketfd, "get_number_parameters"); |
---|
[228] | 92 | |
---|
| 93 | memset(buffer, 0, 256); |
---|
| 94 | ReadMessage(socketfd, buffer); |
---|
[231] | 95 | uint32_t numParameters = atoi(buffer); |
---|
[232] | 96 | |
---|
| 97 | return numParameters; |
---|
[228] | 98 | } |
---|
[231] | 99 | // end previous TODO |
---|
[228] | 100 | |
---|
[222] | 101 | /* Given a certain set of observables, ask the radio to find the optimum radio |
---|
| 102 | * parameters and return them. |
---|
| 103 | * |
---|
| 104 | * TODO I'm a little confused about this function... why would anyone need to |
---|
| 105 | * use this? Shouldn't this be internal to the radio operation? |
---|
[227] | 106 | * |
---|
| 107 | * TODO this function is returning a pointer to allocated memory, which is fine, |
---|
| 108 | * but we need to document this and make sure the caller is deallocating the |
---|
| 109 | * memory when it is done using it. |
---|
[222] | 110 | */ |
---|
[231] | 111 | Parameter* |
---|
| 112 | GetOptimalParameters(Observable *radioObservables, uint32_t numObservables, |
---|
[232] | 113 | Parameter *currentParameters, uint32_t numCurrentParameters) |
---|
[231] | 114 | { |
---|
[222] | 115 | char var[50]; |
---|
| 116 | char counter[55]; |
---|
[232] | 117 | char buffer[256]; |
---|
[222] | 118 | |
---|
[227] | 119 | uint32_t socketFD = ConnectToRemoteComponent(); |
---|
| 120 | SendMessage(socketFD, "request_optimization"); |
---|
[222] | 121 | |
---|
| 122 | /* Get number of observables to send. This information needs to be |
---|
| 123 | * sent to the Cognitive Radio Shell also. |
---|
| 124 | */ |
---|
| 125 | |
---|
[231] | 126 | /* Send Observables */ |
---|
[227] | 127 | sprintf(counter, "%i", numObservables); |
---|
| 128 | SendMessage(socketFD, counter); |
---|
| 129 | for(size_t i = 0; i < numObservables; i++) { |
---|
| 130 | SendMessage(socketFD, radioObservables[i].name.c_str()); |
---|
| 131 | sprintf(var, "%f", radioObservables[i].value); |
---|
[232] | 132 | SendMessage(socketFD, var); |
---|
[222] | 133 | } |
---|
| 134 | |
---|
[231] | 135 | /* Send Parameters */ |
---|
[228] | 136 | memset(counter, 0, 55); |
---|
[229] | 137 | sprintf(counter, "%i", numCurrentParameters); |
---|
| 138 | SendMessage(socketFD, counter); |
---|
[232] | 139 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
[229] | 140 | SendMessage(socketFD,currentParameters[i].name.c_str()); |
---|
[228] | 141 | sprintf(var,"%f",currentParameters[i].value); |
---|
[232] | 142 | SendMessage(socketFD,var); |
---|
[228] | 143 | } |
---|
[229] | 144 | |
---|
[222] | 145 | /* Receive Set of Parameters */ |
---|
| 146 | memset(buffer, 0, 256); |
---|
[227] | 147 | ReadMessage(socketFD, buffer); |
---|
[231] | 148 | uint32_t numParameters = atoi(buffer); |
---|
[227] | 149 | Parameter *pList = new Parameter[numParameters]; |
---|
[222] | 150 | |
---|
[227] | 151 | for(ssize_t i = 0; i < numParameters; i++) { |
---|
[222] | 152 | memset(buffer, 0, 256); |
---|
[227] | 153 | ReadMessage(socketFD, buffer); |
---|
[222] | 154 | pList[i].name = std::string(buffer); |
---|
| 155 | |
---|
[232] | 156 | memset(buffer, 0, 256); |
---|
[227] | 157 | ReadMessage(socketFD, buffer); |
---|
[222] | 158 | pList[i].value = atof(buffer); |
---|
| 159 | } |
---|
| 160 | |
---|
[389] | 161 | close(socketFD); |
---|
[222] | 162 | return pList; |
---|
| 163 | } |
---|
[227] | 164 | |
---|
[230] | 165 | bool |
---|
| 166 | UpdateParameterPerformance(Parameter *p, uint32_t numParameters, Observable *o, |
---|
[232] | 167 | uint32_t numObservables) |
---|
[230] | 168 | { |
---|
[232] | 169 | char counter[55]; |
---|
[230] | 170 | char var[50]; |
---|
| 171 | |
---|
| 172 | uint32_t socketFD = ConnectToRemoteComponent(); |
---|
| 173 | SendMessage(socketFD, "update_performance"); |
---|
| 174 | |
---|
| 175 | // Send Parameters |
---|
| 176 | memset(counter, 0, 55); |
---|
| 177 | sprintf(counter, "%i", numParameters); |
---|
| 178 | SendMessage(socketFD, counter); |
---|
[232] | 179 | |
---|
| 180 | for(size_t i = 0; i < numParameters; i++) { |
---|
[231] | 181 | SendMessage(socketFD, p[i].name.c_str()); |
---|
| 182 | sprintf(var, "%f", p[i].value); |
---|
[232] | 183 | SendMessage(socketFD, var); |
---|
[230] | 184 | } |
---|
| 185 | |
---|
[232] | 186 | // Send Observables |
---|
[230] | 187 | sprintf(counter, "%i", numObservables); |
---|
| 188 | SendMessage(socketFD, counter); |
---|
| 189 | for(size_t i = 0; i < numObservables; i++) { |
---|
| 190 | SendMessage(socketFD, o[i].name.c_str()); |
---|
| 191 | sprintf(var, "%f", o[i].value); |
---|
[232] | 192 | SendMessage(socketFD, var); |
---|
[230] | 193 | } |
---|
| 194 | } |
---|
[299] | 195 | |
---|
| 196 | bool ActivateComponent(uint32_t id) { |
---|
| 197 | return 1; |
---|
| 198 | } |
---|
| 199 | bool DeactivateComponent(uint32_t id) { |
---|
| 200 | return 1; |
---|
| 201 | } |
---|
| 202 | bool DisconnectComponent(uint32_t id) { |
---|
| 203 | return 1; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | Component* GetComponentInformation(uint32_t id) { |
---|
| 207 | |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /* View components currently connected to the radio by id. |
---|
| 211 | * |
---|
| 212 | * TODO Should there be another way to list components? If you have 10 cognitive |
---|
| 213 | * engines, how are you going to know which is which just by id? |
---|
| 214 | */ |
---|
| 215 | uint32_t* GetConnectedCognitiveEngines() { |
---|
| 216 | } |
---|
| 217 | uint32_t* GetConnectedPolicyEngines(){ |
---|
| 218 | } |
---|
| 219 | uint32_t* GetConnectedManagementServiceLayers(){ |
---|
| 220 | } |
---|
| 221 | uint32_t* GetConnectedComponents(){ |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | /* View data from the current status of the radio. |
---|
| 226 | * |
---|
| 227 | * This function allows client code to capture radio properties at any certain |
---|
| 228 | * instant. Note, however, that these properties could be changing at very |
---|
| 229 | * rapid rates. There is no guarantee that the return results from these |
---|
| 230 | * functions will still be valid by the time the client code receives them. |
---|
| 231 | */ |
---|
| 232 | Observable* GetRadioObservables() { |
---|
| 233 | } |
---|
| 234 | Parameter* GetRadioParameters(){ |
---|
| 235 | } |
---|
| 236 | Utility* GetRadioUtilities(){ |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | |
---|
| 240 | /* Parses VTCROSS XML configuration file and uses it to configure the radio. |
---|
| 241 | * |
---|
| 242 | * This function *must* be called when the radio first starts up, and may be |
---|
| 243 | * called at any point after that to reconfigure the radio. |
---|
| 244 | */ |
---|
| 245 | bool ParseRadioConfiguration(){ |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | /* Lists current radio configuration options loaded from the configuration XML |
---|
| 250 | * file. |
---|
| 251 | * |
---|
| 252 | * TODO How are we listing these? Are we simply returning them to stdout? |
---|
| 253 | * Logging them? Returning strings? Need to figure this out... |
---|
| 254 | */ |
---|
| 255 | void ListCurrentRadioConfiguration(){ |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | /* Shut down the radio. |
---|
| 259 | * |
---|
| 260 | * This function will deactivate and disconnect all radio components before |
---|
| 261 | * finally shutting down the shell and stopping radio operations. |
---|
| 262 | */ |
---|
| 263 | bool Shutdown(){ |
---|
| 264 | } |
---|
| 265 | |
---|