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 | uint32_t socket; |
---|
24 | socket = ClientSocket("localhost", "40000"); |
---|
25 | |
---|
26 | return socket; |
---|
27 | } |
---|
28 | |
---|
29 | uint32_t |
---|
30 | GetNumObservables() |
---|
31 | { |
---|
32 | uint32_t socketfd, numObservables; |
---|
33 | char buffer[256]; |
---|
34 | |
---|
35 | socketfd = ConnectToRemoteComponent(); |
---|
36 | SendMessage(socketfd,"get_number_observables"); |
---|
37 | |
---|
38 | memset(buffer, 0, 256); |
---|
39 | ReadMessage(socketfd, buffer); |
---|
40 | numObservables = atoi(buffer); |
---|
41 | |
---|
42 | return numObservables; |
---|
43 | } |
---|
44 | |
---|
45 | uint32_t |
---|
46 | GetNumUtilities() |
---|
47 | { |
---|
48 | uint32_t socketfd, numUtilities; |
---|
49 | char buffer[256]; |
---|
50 | |
---|
51 | socketfd = ConnectToRemoteComponent(); |
---|
52 | SendMessage(socketfd,"get_number_utilities"); |
---|
53 | |
---|
54 | memset(buffer, 0, 256); |
---|
55 | ReadMessage(socketfd, buffer); |
---|
56 | numUtilities = atoi(buffer); |
---|
57 | |
---|
58 | return numUtilities; |
---|
59 | } |
---|
60 | |
---|
61 | uint32_t |
---|
62 | GetNumParameters() |
---|
63 | { |
---|
64 | uint32_t socketfd, numParameters; |
---|
65 | char buffer[256]; |
---|
66 | |
---|
67 | socketfd = ConnectToRemoteComponent(); |
---|
68 | SendMessage(socketfd,"get_number_parameters"); |
---|
69 | |
---|
70 | memset(buffer, 0, 256); |
---|
71 | ReadMessage(socketfd, buffer); |
---|
72 | numParameters = atoi(buffer); |
---|
73 | |
---|
74 | return numParameters; |
---|
75 | } |
---|
76 | |
---|
77 | /* Given a certain set of observables, ask the radio to find the optimum radio |
---|
78 | * parameters and return them. |
---|
79 | * |
---|
80 | * TODO I'm a little confused about this function... why would anyone need to |
---|
81 | * use this? Shouldn't this be internal to the radio operation? |
---|
82 | * |
---|
83 | * TODO this function is returning a pointer to allocated memory, which is fine, |
---|
84 | * but we need to document this and make sure the caller is deallocating the |
---|
85 | * memory when it is done using it. |
---|
86 | */ |
---|
87 | Parameter* GetOptimalParameters(Observable *radioObservables, uint32_t numObservables, |
---|
88 | Parameter *currentParameters, uint32_t numCurrentParameters) { |
---|
89 | uint32_t i,socketfd,numParameters; |
---|
90 | char var[50]; |
---|
91 | char counter[55]; |
---|
92 | char buffer[256]; |
---|
93 | |
---|
94 | uint32_t socketFD = ConnectToRemoteComponent(); |
---|
95 | SendMessage(socketFD, "request_optimization"); |
---|
96 | |
---|
97 | /* Get number of observables to send. This information needs to be |
---|
98 | * sent to the Cognitive Radio Shell also. |
---|
99 | */ |
---|
100 | |
---|
101 | // Send Observables |
---|
102 | sprintf(counter, "%i", numObservables); |
---|
103 | SendMessage(socketFD, counter); |
---|
104 | for(size_t i = 0; i < numObservables; i++) { |
---|
105 | SendMessage(socketFD, radioObservables[i].name.c_str()); |
---|
106 | sprintf(var, "%f", radioObservables[i].value); |
---|
107 | SendMessage(socketFD, var); |
---|
108 | } |
---|
109 | |
---|
110 | // Send Parameters |
---|
111 | memset(counter, 0, 55); |
---|
112 | sprintf(counter, "%i", numCurrentParameters); |
---|
113 | SendMessage(socketFD, counter); |
---|
114 | |
---|
115 | for(i = 0; i < numCurrentParameters; i++) { |
---|
116 | SendMessage(socketFD,currentParameters[i].name.c_str()); |
---|
117 | sprintf(var,"%f",currentParameters[i].value); |
---|
118 | SendMessage(socketFD,var); |
---|
119 | } |
---|
120 | |
---|
121 | /* Receive Set of Parameters */ |
---|
122 | memset(buffer, 0, 256); |
---|
123 | ReadMessage(socketFD, buffer); |
---|
124 | numParameters = atoi(buffer); |
---|
125 | Parameter *pList = new Parameter[numParameters]; |
---|
126 | |
---|
127 | for(ssize_t i = 0; i < numParameters; i++) { |
---|
128 | memset(buffer, 0, 256); |
---|
129 | ReadMessage(socketFD, buffer); |
---|
130 | pList[i].name = std::string(buffer); |
---|
131 | |
---|
132 | memset(buffer, 0, 256); |
---|
133 | ReadMessage(socketFD, buffer); |
---|
134 | pList[i].value = atof(buffer); |
---|
135 | } |
---|
136 | |
---|
137 | return pList; |
---|
138 | } |
---|
139 | |
---|
140 | bool |
---|
141 | UpdateParameterPerformance(Parameter *p, uint32_t numParameters, Observable *o, |
---|
142 | uint32_t numObservables) |
---|
143 | { |
---|
144 | char counter[55]; |
---|
145 | char var[50]; |
---|
146 | |
---|
147 | uint32_t socketFD = ConnectToRemoteComponent(); |
---|
148 | SendMessage(socketFD, "update_performance"); |
---|
149 | |
---|
150 | // Send Parameters |
---|
151 | memset(counter, 0, 55); |
---|
152 | sprintf(counter, "%i", numParameters); |
---|
153 | SendMessage(socketFD, counter); |
---|
154 | |
---|
155 | for(size_t i = 0; i < numParameters; i++) { |
---|
156 | SendMessage(socketFD,p[i].name.c_str()); |
---|
157 | sprintf(var,"%f",p[i].value); |
---|
158 | SendMessage(socketFD,var); |
---|
159 | } |
---|
160 | |
---|
161 | // Send Observables |
---|
162 | sprintf(counter, "%i", numObservables); |
---|
163 | SendMessage(socketFD, counter); |
---|
164 | for(size_t i = 0; i < numObservables; i++) { |
---|
165 | SendMessage(socketFD, o[i].name.c_str()); |
---|
166 | sprintf(var, "%f", o[i].value); |
---|
167 | SendMessage(socketFD, var); |
---|
168 | } |
---|
169 | } |
---|