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 | return pList; |
---|
152 | } |
---|
153 | |
---|
154 | bool |
---|
155 | UpdateParameterPerformance(Parameter *p, uint32_t numParameters, Observable *o, |
---|
156 | uint32_t numObservables) |
---|
157 | { |
---|
158 | char counter[55]; |
---|
159 | char var[50]; |
---|
160 | |
---|
161 | uint32_t socketFD = ConnectToRemoteComponent(); |
---|
162 | SendMessage(socketFD, "update_performance"); |
---|
163 | |
---|
164 | // Send Parameters |
---|
165 | memset(counter, 0, 55); |
---|
166 | sprintf(counter, "%i", numParameters); |
---|
167 | SendMessage(socketFD, counter); |
---|
168 | |
---|
169 | for(size_t i = 0; i < numParameters; i++) { |
---|
170 | SendMessage(socketFD, p[i].name.c_str()); |
---|
171 | sprintf(var, "%f", p[i].value); |
---|
172 | SendMessage(socketFD, var); |
---|
173 | } |
---|
174 | |
---|
175 | // Send Observables |
---|
176 | sprintf(counter, "%i", numObservables); |
---|
177 | SendMessage(socketFD, counter); |
---|
178 | for(size_t i = 0; i < numObservables; i++) { |
---|
179 | SendMessage(socketFD, o[i].name.c_str()); |
---|
180 | sprintf(var, "%f", o[i].value); |
---|
181 | SendMessage(socketFD, var); |
---|
182 | } |
---|
183 | } |
---|