1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
/*
* Copyright 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "test_command_handler.h"
#include <stdlib.h>
#include <fstream>
#include <memory>
#include <regex>
#include "device_boutique.h"
#include "os/log.h"
#include "osi/include/osi.h"
#include "phy.h"
using std::vector;
namespace rootcanal {
TestCommandHandler::TestCommandHandler(TestModel& test_model)
: model_(test_model) {
#define SET_HANDLER(command_name, method) \
active_commands_[command_name] = [this](const vector<std::string>& param) { \
method(param); \
};
SET_HANDLER("add", Add);
SET_HANDLER("add_remote", AddRemote);
SET_HANDLER("del", Del);
SET_HANDLER("add_phy", AddPhy);
SET_HANDLER("del_phy", DelPhy);
SET_HANDLER("add_device_to_phy", AddDeviceToPhy);
SET_HANDLER("del_device_from_phy", DelDeviceFromPhy);
SET_HANDLER("list", List);
SET_HANDLER("set_device_address", SetDeviceAddress);
SET_HANDLER("set_timer_period", SetTimerPeriod);
SET_HANDLER("start_timer", StartTimer);
SET_HANDLER("stop_timer", StopTimer);
SET_HANDLER("reset", Reset);
#undef SET_HANDLER
}
void TestCommandHandler::AddDefaults() {
// Add a phy for LE and one for BR/EDR
AddPhy({"LOW_ENERGY"});
AddPhy({"BR_EDR"});
// Add the controller to the Phys
AddDeviceToPhy({"1", "1"});
AddDeviceToPhy({"1", "2"});
// Add default test devices and add the devices to the phys
// Add({"beacon", "be:ac:10:00:00:01", "1000"});
// AddDeviceToPhy({"2", "1"});
// Add({"keyboard", "cc:1c:eb:0a:12:d1", "500"});
// AddDeviceToPhy({"3", "1"});
// Add({"classic", "c1:a5:51:c0:00:01", "22"});
// AddDeviceToPhy({"4", "2"});
// Add({"car_kit", "ca:12:1c:17:00:01", "238"});
// AddDeviceToPhy({"5", "2"});
// Add({"sniffer", "ca:12:1c:17:00:01"});
// AddDeviceToPhy({"6", "2"});
// Add({"sniffer", "3c:5a:b4:04:05:06"});
// AddDeviceToPhy({"7", "2"});
// Add({"remote_loopback_device", "10:0d:00:ba:c1:06"});
// AddDeviceToPhy({"8", "2"});
List({});
SetTimerPeriod({"10"});
StartTimer({});
}
void TestCommandHandler::HandleCommand(const std::string& name,
const vector<std::string>& args) {
if (active_commands_.count(name) == 0) {
response_string_ = "Unhandled command: " + name;
send_response_(response_string_);
return;
}
active_commands_[name](args);
}
void TestCommandHandler::FromFile(const std::string& file_name) {
if (file_name.size() == 0) {
return;
}
std::ifstream file(file_name.c_str());
const std::regex re("\\s+");
std::string line;
while (std::getline(file, line)) {
auto begin = std::sregex_token_iterator(line.begin(), line.end(), re, -1);
auto end = std::sregex_token_iterator();
auto params = std::vector<std::string>(std::next(begin), end);
HandleCommand(*begin, params);
}
if (file.fail()) {
LOG_ERROR("Error reading commands from file.");
return;
}
}
void TestCommandHandler::RegisterSendResponse(
const std::function<void(const std::string&)> callback) {
send_response_ = callback;
send_response_("RegisterSendResponse called");
}
void TestCommandHandler::Add(const vector<std::string>& args) {
if (args.size() < 1) {
response_string_ = "TestCommandHandler 'add' takes an argument";
send_response_(response_string_);
return;
}
std::shared_ptr<Device> new_dev = DeviceBoutique::Create(args);
if (new_dev == NULL) {
response_string_ = "TestCommandHandler 'add' " + args[0] + " failed!";
send_response_(response_string_);
LOG_WARN("%s", response_string_.c_str());
return;
}
LOG_INFO("Add %s", new_dev->ToString().c_str());
size_t dev_index = model_.Add(new_dev);
response_string_ =
std::to_string(dev_index) + std::string(":") + new_dev->ToString();
send_response_(response_string_);
}
void TestCommandHandler::AddRemote(const vector<std::string>& args) {
if (args.size() < 3) {
response_string_ =
"TestCommandHandler usage: add_remote host port phy_type";
send_response_(response_string_);
return;
}
size_t port = std::stoi(args[1]);
Phy::Type phy_type = Phy::Type::BR_EDR;
if ("LOW_ENERGY" == args[2]) {
phy_type = Phy::Type::LOW_ENERGY;
}
if (port == 0 || port > 0xffff || args[0].size() < 2) {
response_string_ = "TestCommandHandler bad arguments to 'add_remote': ";
response_string_ += args[0];
response_string_ += "@";
response_string_ += args[1];
send_response_(response_string_);
return;
}
model_.AddRemote(args[0], port, phy_type);
response_string_ = args[0] + std::string("@") + std::to_string(port);
send_response_(response_string_);
}
void TestCommandHandler::Del(const vector<std::string>& args) {
size_t dev_index = std::stoi(args[0]);
model_.Del(dev_index);
response_string_ = "TestCommandHandler 'del' called with device at index " +
std::to_string(dev_index);
send_response_(response_string_);
}
void TestCommandHandler::AddPhy(const vector<std::string>& args) {
if (args[0] == "LOW_ENERGY") {
model_.AddPhy(Phy::Type::LOW_ENERGY);
} else if (args[0] == "BR_EDR") {
model_.AddPhy(Phy::Type::BR_EDR);
} else {
response_string_ =
"TestCommandHandler 'add_phy' with unrecognized type " + args[0];
send_response_(response_string_);
}
}
void TestCommandHandler::DelPhy(const vector<std::string>& args) {
size_t phy_index = std::stoi(args[0]);
model_.DelPhy(phy_index);
response_string_ = "TestCommandHandler 'del_phy' called with phy at index " +
std::to_string(phy_index);
send_response_(response_string_);
}
void TestCommandHandler::AddDeviceToPhy(const vector<std::string>& args) {
if (args.size() != 2) {
response_string_ =
"TestCommandHandler 'add_device_to_phy' takes two arguments";
send_response_(response_string_);
return;
}
size_t dev_index = std::stoi(args[0]);
size_t phy_index = std::stoi(args[1]);
model_.AddDeviceToPhy(dev_index, phy_index);
response_string_ =
"TestCommandHandler 'add_device_to_phy' called with device " +
std::to_string(dev_index) + " and phy " + std::to_string(phy_index);
send_response_(response_string_);
return;
}
void TestCommandHandler::DelDeviceFromPhy(const vector<std::string>& args) {
if (args.size() != 2) {
response_string_ =
"TestCommandHandler 'del_device_from_phy' takes two arguments";
send_response_(response_string_);
return;
}
size_t dev_index = std::stoi(args[0]);
size_t phy_index = std::stoi(args[1]);
model_.DelDeviceFromPhy(dev_index, phy_index);
response_string_ =
"TestCommandHandler 'del_device_from_phy' called with device " +
std::to_string(dev_index) + " and phy " + std::to_string(phy_index);
send_response_(response_string_);
return;
}
void TestCommandHandler::List(const vector<std::string>& args) {
if (args.size() > 0) {
LOG_INFO("Unused args: arg[0] = %s", args[0].c_str());
return;
}
send_response_(model_.List());
}
void TestCommandHandler::SetDeviceAddress(const vector<std::string>& args) {
if (args.size() != 2) {
response_string_ =
"TestCommandHandler 'set_device_address' takes two arguments";
send_response_(response_string_);
return;
}
size_t device_id = std::stoi(args[0]);
Address device_address{};
Address::FromString(args[1], device_address);
model_.SetDeviceAddress(device_id, device_address);
response_string_ = "set_device_address " + args[0];
response_string_ += " ";
response_string_ += args[1];
send_response_(response_string_);
}
void TestCommandHandler::SetTimerPeriod(const vector<std::string>& args) {
if (args.size() != 1) {
LOG_INFO("SetTimerPeriod takes 1 argument");
}
size_t period = std::stoi(args[0]);
if (period != 0) {
response_string_ = "set timer period to ";
response_string_ += args[0];
model_.SetTimerPeriod(std::chrono::milliseconds(period));
} else {
response_string_ = "invalid timer period ";
response_string_ += args[0];
}
send_response_(response_string_);
}
void TestCommandHandler::StartTimer(const vector<std::string>& args) {
if (args.size() > 0) {
LOG_INFO("Unused args: arg[0] = %s", args[0].c_str());
}
model_.StartTimer();
response_string_ = "timer started";
send_response_(response_string_);
}
void TestCommandHandler::StopTimer(const vector<std::string>& args) {
if (args.size() > 0) {
LOG_INFO("Unused args: arg[0] = %s", args[0].c_str());
}
model_.StopTimer();
response_string_ = "timer stopped";
send_response_(response_string_);
}
void TestCommandHandler::Reset(const std::vector<std::string>& args) {
if (args.size() > 0) {
LOG_INFO("Unused args: arg[0] = %s", args[0].c_str());
}
model_.Reset();
response_string_ = "model reset";
send_response_(response_string_);
}
} // namespace rootcanal
|