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