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 | |
---|
60 | Parameter* |
---|
61 | GetOptimalParameters(Observable *radioObservables, uint32_t numObservables, Parameter *currentParameters, uint32_t numCurrentParameters, Utility *radioUtilities, uint32_t numUtilities) |
---|
62 | { |
---|
63 | char var[50]; |
---|
64 | char counter[55]; |
---|
65 | char buffer[256]; |
---|
66 | |
---|
67 | uint32_t socketFD = ConnectToShell(); |
---|
68 | SendMessage(socketFD, "request_optimization"); |
---|
69 | |
---|
70 | /* Get number of observables to send. This information needs to be |
---|
71 | * sent to the Cognitive Radio Shell also. |
---|
72 | */ |
---|
73 | |
---|
74 | /* Send Observables */ |
---|
75 | sprintf(counter, "%i", numObservables); |
---|
76 | SendMessage(socketFD, counter); |
---|
77 | for(size_t i = 0; i < numObservables; i++) { |
---|
78 | SendMessage(socketFD, radioObservables[i].name.c_str()); |
---|
79 | sprintf(var, "%f", radioObservables[i].value); |
---|
80 | SendMessage(socketFD, var); |
---|
81 | } |
---|
82 | |
---|
83 | /* Send Parameters */ |
---|
84 | memset(counter, 0, 55); |
---|
85 | sprintf(counter, "%i", numCurrentParameters); |
---|
86 | SendMessage(socketFD, counter); |
---|
87 | for(size_t i = 0; i < numCurrentParameters; i++) { |
---|
88 | SendMessage(socketFD,currentParameters[i].name.c_str()); |
---|
89 | sprintf(var,"%f",currentParameters[i].value); |
---|
90 | SendMessage(socketFD,var); |
---|
91 | } |
---|
92 | |
---|
93 | /* Send Utilities */ |
---|
94 | memset(counter, 0, 55); |
---|
95 | sprintf(counter, "%i", numUtilities); |
---|
96 | SendMessage(socketFD, counter); |
---|
97 | for(size_t i = 0; i < numUtilities; i++) { |
---|
98 | SendMessage(socketFD,radioUtilities[i].name.c_str()); |
---|
99 | sprintf(var,"%f",radioUtilities[i].value); |
---|
100 | SendMessage(socketFD,var); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | /* Receive Set of Parameters */ |
---|
105 | memset(buffer, 0, 256); |
---|
106 | ReadMessage(socketFD, buffer); |
---|
107 | uint32_t numParameters = atoi(buffer); |
---|
108 | Parameter *pList = new Parameter[numParameters]; |
---|
109 | |
---|
110 | for(ssize_t i = 0; i < numParameters; i++) { |
---|
111 | memset(buffer, 0, 256); |
---|
112 | ReadMessage(socketFD, buffer); |
---|
113 | pList[i].name = std::string(buffer); |
---|
114 | |
---|
115 | memset(buffer, 0, 256); |
---|
116 | ReadMessage(socketFD, buffer); |
---|
117 | pList[i].value = atof(buffer); |
---|
118 | } |
---|
119 | |
---|
120 | close(socketFD); |
---|
121 | return pList; |
---|
122 | } |
---|
123 | |
---|
124 | bool |
---|
125 | UpdateParameterPerformance(Parameter *p, uint32_t numParameters, Observable *o, uint32_t numObservables, Utility *u, uint32_t numUtilities) |
---|
126 | { |
---|
127 | char counter[55]; |
---|
128 | char var[50]; |
---|
129 | |
---|
130 | uint32_t socketFD = ConnectToShell(); |
---|
131 | SendMessage(socketFD, "update_performance"); |
---|
132 | |
---|
133 | // Send Parameters |
---|
134 | memset(counter, 0, 55); |
---|
135 | sprintf(counter, "%i", numParameters); |
---|
136 | SendMessage(socketFD, counter); |
---|
137 | |
---|
138 | for(size_t i = 0; i < numParameters; i++) { |
---|
139 | SendMessage(socketFD, p[i].name.c_str()); |
---|
140 | sprintf(var, "%f", p[i].value); |
---|
141 | SendMessage(socketFD, var); |
---|
142 | } |
---|
143 | |
---|
144 | // Send Observables |
---|
145 | memset(counter, 0, 55); |
---|
146 | sprintf(counter, "%i", numObservables); |
---|
147 | SendMessage(socketFD, counter); |
---|
148 | for(size_t i = 0; i < numObservables; i++) { |
---|
149 | SendMessage(socketFD, o[i].name.c_str()); |
---|
150 | sprintf(var, "%f", o[i].value); |
---|
151 | SendMessage(socketFD, var); |
---|
152 | } |
---|
153 | |
---|
154 | // Send Utilities |
---|
155 | memset(counter, 0, 55); |
---|
156 | sprintf(counter, "%i", numUtilities); |
---|
157 | SendMessage(socketFD, counter); |
---|
158 | for(size_t i = 0; i < numUtilities; i++) { |
---|
159 | SendMessage(socketFD, u[i].name.c_str()); |
---|
160 | sprintf(var, "%f", u[i].value); |
---|
161 | SendMessage(socketFD, var); |
---|
162 | } |
---|
163 | |
---|
164 | } |
---|
165 | |
---|
166 | uint32_t GetNum(string type) |
---|
167 | { |
---|
168 | |
---|
169 | char buffer[256]; |
---|
170 | uint32_t socketfd = ConnectToShell(); |
---|
171 | |
---|
172 | if (type == "parameters") |
---|
173 | SendMessage(socketfd, "get_number_parameters"); |
---|
174 | else if (type == "utilities") |
---|
175 | SendMessage(socketfd, "get_number_utilities"); |
---|
176 | else if (type == "observables") |
---|
177 | SendMessage(socketfd, "get_number_observables"); |
---|
178 | else |
---|
179 | LOG("Type not included, either 'parameters' or 'utilities' or 'observables'"); |
---|
180 | |
---|
181 | memset(buffer, 0, 256); |
---|
182 | ReadMessage(socketfd, buffer); |
---|
183 | uint32_t number = atoi(buffer); |
---|
184 | return number; |
---|
185 | |
---|
186 | |
---|
187 | } |
---|