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