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