1 | /* |
---|
2 | Copyright 2009 Virginia Polytechnic Institute and State University |
---|
3 | |
---|
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 | |
---|
17 | /* Implementation file for the VCROSS Cognitive Radio public API defined in |
---|
18 | * include/libvtcross.h. |
---|
19 | * |
---|
20 | * MORE INFO HERE |
---|
21 | */ |
---|
22 | |
---|
23 | #include <cstdlib> |
---|
24 | #include <string> |
---|
25 | |
---|
26 | #include "vtcross/common.h" |
---|
27 | #include "vtcross/debug.h" |
---|
28 | #include "vtcross/libvtcross.h" |
---|
29 | |
---|
30 | using namespace std; |
---|
31 | |
---|
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 | |
---|
40 | uint32_t |
---|
41 | ConnectToShell() |
---|
42 | { |
---|
43 | return ClientSocket(shellHostname.c_str(), shellPort.c_str()); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | void |
---|
48 | SetCrossShellLocation(string hostname, string port) |
---|
49 | { |
---|
50 | shellHostname = hostname; |
---|
51 | shellPort = port; |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | // TODO the following three functions all do exactly the same thing. Why not |
---|
56 | // simply combine them? |
---|
57 | uint32_t |
---|
58 | GetNumObservables() |
---|
59 | { |
---|
60 | char buffer[256]; |
---|
61 | |
---|
62 | uint32_t socketfd = ConnectToShell(); |
---|
63 | SendMessage(socketfd, "get_number_observables"); |
---|
64 | |
---|
65 | memset(buffer, 0, 256); |
---|
66 | ReadMessage(socketfd, buffer); |
---|
67 | uint32_t numObservables = atoi(buffer); |
---|
68 | |
---|
69 | return numObservables; |
---|
70 | } |
---|
71 | |
---|
72 | uint32_t |
---|
73 | GetNumUtilities() |
---|
74 | { |
---|
75 | char buffer[256]; |
---|
76 | |
---|
77 | uint32_t socketfd = ConnectToShell(); |
---|
78 | SendMessage(socketfd, "get_number_utilities"); |
---|
79 | |
---|
80 | memset(buffer, 0, 256); |
---|
81 | ReadMessage(socketfd, buffer); |
---|
82 | uint32_t numUtilities = atoi(buffer); |
---|
83 | |
---|
84 | return numUtilities; |
---|
85 | } |
---|
86 | |
---|
87 | uint32_t |
---|
88 | SetActiveMission(char * activeMission) |
---|
89 | { |
---|
90 | char buffer[256]; |
---|
91 | |
---|
92 | uint32_t socketfd = ConnectToShell(); |
---|
93 | SendMessage(socketfd, "set_active_mission"); |
---|
94 | |
---|
95 | SendMessage(socketfd, activeMission); |
---|
96 | |
---|
97 | //memset(buffer, 0, 256); |
---|
98 | //ReadMessage(socketfd, buffer); |
---|
99 | |
---|
100 | return 1; |
---|
101 | } |
---|
102 | |
---|
103 | uint32_t |
---|
104 | GetNumParameters() |
---|
105 | { |
---|
106 | char buffer[256]; |
---|
107 | |
---|
108 | uint32_t socketfd = ConnectToShell(); |
---|
109 | SendMessage(socketfd, "get_number_parameters"); |
---|
110 | |
---|
111 | memset(buffer, 0, 256); |
---|
112 | ReadMessage(socketfd, buffer); |
---|
113 | uint32_t numParameters = atoi(buffer); |
---|
114 | |
---|
115 | return numParameters; |
---|
116 | } |
---|
117 | // end previous TODO |
---|
118 | |
---|
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? |
---|
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. |
---|
128 | */ |
---|
129 | Parameter* |
---|
130 | GetOptimalParameters(Observable *radioObservables, uint32_t numObservables, |
---|
131 | Parameter *currentParameters, uint32_t numCurrentParameters) |
---|
132 | { |
---|
133 | char var[50]; |
---|
134 | char counter[55]; |
---|
135 | char buffer[256]; |
---|
136 | |
---|
137 | uint32_t socketFD = ConnectToShell(); |
---|
138 | SendMessage(socketFD, "request_optimization"); |
---|
139 | |
---|
140 | /* Get number of observables to send. This information needs to be |
---|
141 | * sent to the Cognitive Radio Shell also. |
---|
142 | */ |
---|
143 | |
---|
144 | /* Send Observables */ |
---|
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); |
---|
150 | SendMessage(socketFD, var); |
---|
151 | } |
---|
152 | |
---|
153 | /* Send Parameters */ |
---|
154 | memset(counter, 0, 55); |
---|
155 | sprintf(counter, "%i", numCurrentParameters); |
---|
156 | SendMessage(socketFD, counter); |
---|
157 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
158 | SendMessage(socketFD,currentParameters[i].name.c_str()); |
---|
159 | sprintf(var,"%f",currentParameters[i].value); |
---|
160 | SendMessage(socketFD,var); |
---|
161 | } |
---|
162 | |
---|
163 | /* Receive Set of Parameters */ |
---|
164 | memset(buffer, 0, 256); |
---|
165 | ReadMessage(socketFD, buffer); |
---|
166 | uint32_t numParameters = atoi(buffer); |
---|
167 | Parameter *pList = new Parameter[numParameters]; |
---|
168 | |
---|
169 | for(ssize_t i = 0; i < numParameters; i++) { |
---|
170 | memset(buffer, 0, 256); |
---|
171 | ReadMessage(socketFD, buffer); |
---|
172 | pList[i].name = std::string(buffer); |
---|
173 | |
---|
174 | memset(buffer, 0, 256); |
---|
175 | ReadMessage(socketFD, buffer); |
---|
176 | pList[i].value = atof(buffer); |
---|
177 | } |
---|
178 | |
---|
179 | close(socketFD); |
---|
180 | return pList; |
---|
181 | } |
---|
182 | |
---|
183 | bool |
---|
184 | UpdateParameterPerformance(Parameter *p, uint32_t numParameters, Observable *o, |
---|
185 | uint32_t numObservables) |
---|
186 | { |
---|
187 | char counter[55]; |
---|
188 | char var[50]; |
---|
189 | |
---|
190 | uint32_t socketFD = ConnectToShell(); |
---|
191 | SendMessage(socketFD, "update_performance"); |
---|
192 | |
---|
193 | // Send Parameters |
---|
194 | memset(counter, 0, 55); |
---|
195 | sprintf(counter, "%i", numParameters); |
---|
196 | SendMessage(socketFD, counter); |
---|
197 | |
---|
198 | for(size_t i = 0; i < numParameters; i++) { |
---|
199 | SendMessage(socketFD, p[i].name.c_str()); |
---|
200 | sprintf(var, "%f", p[i].value); |
---|
201 | SendMessage(socketFD, var); |
---|
202 | } |
---|
203 | |
---|
204 | // Send Observables |
---|
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); |
---|
210 | SendMessage(socketFD, var); |
---|
211 | } |
---|
212 | } |
---|
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 | |
---|