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 #pragma once
18 
19 #include <unistd.h>
20 #include <cstdint>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "model/devices/device.h"
27 #include "phy_layer_factory.h"
28 #include "test_channel_transport.h"
29 #include "test_model.h"
30 
31 namespace test_vendor_lib {
32 
33 class TestCommandHandler {
34  public:
35   // Sets all of the methods to be used as callbacks in the HciHandler.
36   TestCommandHandler(TestModel& test_model);
37 
38   ~TestCommandHandler() = default;
39 
40   // Dispatches the action corresponding to the command specified by |name|.
41   void HandleCommand(const std::string& name, const std::vector<std::string>& args);
42 
43   // Dispatches the action from a file
44   void FromFile(const std::string& file_name);
45 
46   // Dispatches the action corresponding to the command specified by |name|.
47   void RegisterSendResponse(const std::function<void(const std::string&)> callback);
48 
49   // Commands:
50 
51   // Add a device
52   void Add(const std::vector<std::string>& args);
53 
54   // Add a remote device
55   void AddRemote(const std::vector<std::string>& args);
56 
57   // Remove devices by index
58   void Del(const std::vector<std::string>& args);
59 
60   // Add phy
61   void AddPhy(const std::vector<std::string>& args);
62 
63   // Remove phy by name
64   void DelPhy(const std::vector<std::string>& args);
65 
66   // Add device to phy
67   void AddDeviceToPhy(const std::vector<std::string>& args);
68 
69   // Remove device from phy
70   void DelDeviceFromPhy(const std::vector<std::string>& args);
71 
72   // List the devices that the test knows about
73   void List(const std::vector<std::string>& args);
74 
75   // Change the device's MAC address
76   void SetDeviceAddress(const std::vector<std::string>& args);
77 
78   // Timer management functions
79   void SetTimerPeriod(const std::vector<std::string>& args);
80 
81   void StartTimer(const std::vector<std::string>& args);
82 
83   void StopTimer(const std::vector<std::string>& args);
84 
85   void Reset(const std::vector<std::string>& args);
86 
87   // For manual testing
88   void AddDefaults();
89 
90  private:
91   TestModel& model_;
92 
93   std::string response_string_;
94 
95   std::unordered_map<std::string, std::function<void(const std::vector<std::string>&)>> active_commands_;
96 
97   std::function<void(const std::string&)> send_response_;
98 
99   TestCommandHandler(const TestCommandHandler& cmdPckt) = delete;
100   TestCommandHandler& operator=(const TestCommandHandler& cmdPckt) = delete;
101 };
102 
103 }  // namespace test_vendor_lib
104