root/vtcross/trunk/src/libvtcross/libvtcross.cpp @ 469

Revision 412, 7.0 KB (checked in by trnewman, 15 years ago)

Adding Apache license information.

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