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