1 /*
2 * Copyright 2018 The Android Open Source Project
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 #define LOG_TAG "test_command_handler"
18
19 #include "test_command_handler.h"
20 #include "device_boutique.h"
21 #include "phy.h"
22
23 #include <memory>
24
25 #include <stdlib.h>
26
27 #include <base/logging.h>
28 #include "base/files/file_util.h"
29 #include "base/json/json_reader.h"
30 #include "base/values.h"
31
32 #include "osi/include/log.h"
33 #include "osi/include/osi.h"
34
35 using std::vector;
36
37 namespace test_vendor_lib {
38
TestCommandHandler(TestModel & test_model)39 TestCommandHandler::TestCommandHandler(TestModel& test_model) : model_(test_model) {
40 #define SET_HANDLER(command_name, method) \
41 active_commands_[command_name] = [this](const vector<std::string>& param) { method(param); };
42 SET_HANDLER("add", Add);
43 SET_HANDLER("add_remote", AddRemote);
44 SET_HANDLER("del", Del);
45 SET_HANDLER("add_phy", AddPhy);
46 SET_HANDLER("del_phy", DelPhy);
47 SET_HANDLER("add_device_to_phy", AddDeviceToPhy);
48 SET_HANDLER("del_device_from_phy", DelDeviceFromPhy);
49 SET_HANDLER("list", List);
50 SET_HANDLER("set_timer_period", SetTimerPeriod);
51 SET_HANDLER("start_timer", StartTimer);
52 SET_HANDLER("stop_timer", StopTimer);
53 #undef SET_HANDLER
54 }
55
AddDefaults()56 void TestCommandHandler::AddDefaults() {
57 // Add a phy for LE and one for BR/EDR
58 AddPhy({"LOW_ENERGY"});
59 AddPhy({"BR_EDR"});
60
61 // Add the controller to the Phys
62 AddDeviceToPhy({"0", "0"});
63 AddDeviceToPhy({"0", "1"});
64
65 // Add default test devices and add the devices to the phys
66 // Add({"beacon", "be:ac:10:00:00:01", "1000"});
67 // AddDeviceToPhy({"1", "0"});
68
69 // Add({"keyboard", "cc:1c:eb:0a:12:d1", "500"});
70 // AddDeviceToPhy({"2", "0"});
71
72 // Add({"classic", "c1:a5:51:c0:00:01", "22"});
73 // AddDeviceToPhy({"3", "1"});
74
75 // Add({"car_kit", "ca:12:1c:17:00:01", "238"});
76 // AddDeviceToPhy({"4", "1"});
77
78 // Add({"sniffer", "ca:12:1c:17:00:01"});
79 // AddDeviceToPhy({"5", "1"});
80
81 // Add({"sniffer", "3c:5a:b4:04:05:06"});
82 // AddDeviceToPhy({"1", "1"});
83 // Add({"remote_loopback_device", "10:0d:00:ba:c1:06"});
84 // AddDeviceToPhy({"2", "1"});
85 List({});
86
87 SetTimerPeriod({"10"});
88 StartTimer({});
89 }
90
HandleCommand(const std::string & name,const vector<std::string> & args)91 void TestCommandHandler::HandleCommand(const std::string& name, const vector<std::string>& args) {
92 if (active_commands_.count(name) == 0) {
93 response_string_ = "Unhandled command: " + name;
94 send_response_(response_string_);
95 return;
96 }
97 active_commands_[name](args);
98 }
99
RegisterSendResponse(const std::function<void (const std::string &)> callback)100 void TestCommandHandler::RegisterSendResponse(const std::function<void(const std::string&)> callback) {
101 send_response_ = callback;
102 send_response_("RegisterSendResponse called");
103 }
104
Add(const vector<std::string> & args)105 void TestCommandHandler::Add(const vector<std::string>& args) {
106 if (args.size() < 1) {
107 response_string_ = "TestCommandHandler 'add' takes an argument";
108 send_response_(response_string_);
109 return;
110 }
111 std::shared_ptr<Device> new_dev = DeviceBoutique::Create(args);
112
113 if (new_dev == NULL) {
114 response_string_ = "TestCommandHandler 'add' " + args[0] + " failed!";
115 send_response_(response_string_);
116 LOG_WARN(LOG_TAG, "%s", response_string_.c_str());
117 return;
118 }
119
120 LOG_INFO(LOG_TAG, "Add %s", new_dev->ToString().c_str());
121 size_t dev_index = model_.Add(new_dev);
122 response_string_ = std::to_string(dev_index) + std::string(":") + new_dev->ToString();
123 send_response_(response_string_);
124 }
125
AddRemote(const vector<std::string> & args)126 void TestCommandHandler::AddRemote(const vector<std::string>& args) {
127 if (args.size() < 3) {
128 response_string_ = "TestCommandHandler usage: add_remote host port phy_type";
129 send_response_(response_string_);
130 return;
131 }
132
133 size_t port = std::stoi(args[1]);
134 Phy::Type phy_type = Phy::Type::BR_EDR;
135 if ("LOW_ENERGY" == args[2]) {
136 phy_type = Phy::Type::LOW_ENERGY;
137 }
138 if (port == 0 || port > 0xffff || args[0].size() < 2) {
139 response_string_ = "TestCommandHandler bad arguments to 'add_remote': ";
140 response_string_ += args[0];
141 response_string_ += "@";
142 response_string_ += args[1];
143 send_response_(response_string_);
144 return;
145 }
146
147 model_.AddRemote(args[0], port, phy_type);
148
149 response_string_ = args[0] + std::string("@") + std::to_string(port);
150 send_response_(response_string_);
151 }
152
Del(const vector<std::string> & args)153 void TestCommandHandler::Del(const vector<std::string>& args) {
154 size_t dev_index = std::stoi(args[0]);
155
156 model_.Del(dev_index);
157 response_string_ = "TestCommandHandler 'del' called with device at index " + std::to_string(dev_index);
158 send_response_(response_string_);
159 }
160
AddPhy(const vector<std::string> & args)161 void TestCommandHandler::AddPhy(const vector<std::string>& args) {
162 if (args[0] == "LOW_ENERGY") {
163 std::shared_ptr<PhyLayerFactory> new_phy = std::make_shared<PhyLayerFactory>(Phy::Type::LOW_ENERGY);
164 model_.AddPhy(new_phy);
165 } else if (args[0] == "BR_EDR") {
166 std::shared_ptr<PhyLayerFactory> new_phy = std::make_shared<PhyLayerFactory>(Phy::Type::BR_EDR);
167 model_.AddPhy(new_phy);
168 } else {
169 response_string_ = "TestCommandHandler 'add_phy' with unrecognized type " + args[0];
170 send_response_(response_string_);
171 }
172 }
173
DelPhy(const vector<std::string> & args)174 void TestCommandHandler::DelPhy(const vector<std::string>& args) {
175 size_t phy_index = std::stoi(args[0]);
176
177 model_.DelPhy(phy_index);
178 response_string_ = "TestCommandHandler 'del_phy' called with phy at index " + std::to_string(phy_index);
179 send_response_(response_string_);
180 }
181
AddDeviceToPhy(const vector<std::string> & args)182 void TestCommandHandler::AddDeviceToPhy(const vector<std::string>& args) {
183 if (args.size() != 2) {
184 response_string_ = "TestCommandHandler 'add_device_to_phy' takes two arguments";
185 send_response_(response_string_);
186 return;
187 }
188 size_t dev_index = std::stoi(args[0]);
189 size_t phy_index = std::stoi(args[1]);
190 model_.AddDeviceToPhy(dev_index, phy_index);
191 response_string_ = "TestCommandHandler 'add_device_to_phy' called with device " + std::to_string(dev_index) +
192 " and phy " + std::to_string(phy_index);
193 send_response_(response_string_);
194 return;
195 }
196
DelDeviceFromPhy(const vector<std::string> & args)197 void TestCommandHandler::DelDeviceFromPhy(const vector<std::string>& args) {
198 if (args.size() != 2) {
199 response_string_ = "TestCommandHandler 'del_device_from_phy' takes two arguments";
200 send_response_(response_string_);
201 return;
202 }
203 size_t dev_index = std::stoi(args[0]);
204 size_t phy_index = std::stoi(args[1]);
205 model_.DelDeviceFromPhy(dev_index, phy_index);
206 response_string_ = "TestCommandHandler 'del_device_from_phy' called with device " + std::to_string(dev_index) +
207 " and phy " + std::to_string(phy_index);
208 send_response_(response_string_);
209 return;
210 }
211
List(const vector<std::string> & args)212 void TestCommandHandler::List(const vector<std::string>& args) {
213 if (args.size() > 0) {
214 LOG_INFO(LOG_TAG, "Unused args: arg[0] = %s", args[0].c_str());
215 return;
216 }
217 send_response_(model_.List());
218 }
219
SetTimerPeriod(const vector<std::string> & args)220 void TestCommandHandler::SetTimerPeriod(const vector<std::string>& args) {
221 if (args.size() != 1) {
222 LOG_INFO(LOG_TAG, "SetTimerPeriod takes 1 argument");
223 }
224 size_t period = std::stoi(args[0]);
225 model_.SetTimerPeriod(std::chrono::milliseconds(period));
226 }
227
StartTimer(const vector<std::string> & args)228 void TestCommandHandler::StartTimer(const vector<std::string>& args) {
229 if (args.size() > 0) {
230 LOG_INFO(LOG_TAG, "Unused args: arg[0] = %s", args[0].c_str());
231 }
232 model_.StartTimer();
233 }
234
StopTimer(const vector<std::string> & args)235 void TestCommandHandler::StopTimer(const vector<std::string>& args) {
236 if (args.size() > 0) {
237 LOG_INFO(LOG_TAG, "Unused args: arg[0] = %s", args[0].c_str());
238 }
239 model_.StopTimer();
240 }
241
242 } // namespace test_vendor_lib
243