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 
21 #include <cstdint>
22 #include <memory>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include "test_channel_transport.h"
28 #include "test_model.h"
29 
30 namespace rootcanal {
31 
32 class TestCommandHandler {
33  public:
34   // Sets all of the methods to be used as callbacks in the HciHandler.
35   TestCommandHandler(TestModel& test_model);
36 
37   ~TestCommandHandler() = default;
38 
39   // Dispatches the action corresponding to the command specified by |name|.
40   void HandleCommand(const std::string& name,
41                      const std::vector<std::string>& args);
42 
43   // Dispatches the action corresponding to the command specified by |name|.
44   void RegisterSendResponse(std::function<void(const std::string&)> callback);
45 
46   // Commands:
47 
48   // Add a device
49   void AddDevice(const std::vector<std::string>& args);
50 
51   // Add a remote device
52   void AddRemote(const std::vector<std::string>& args);
53 
54   // Remove devices by index
55   void RemoveDevice(const std::vector<std::string>& args);
56 
57   // Add phy
58   void AddPhy(const std::vector<std::string>& args);
59 
60   // Remove phy by name
61   void RemovePhy(const std::vector<std::string>& args);
62 
63   // Add device to phy
64   void AddDeviceToPhy(const std::vector<std::string>& args);
65 
66   // Remove device from phy
67   void RemoveDeviceFromPhy(const std::vector<std::string>& args);
68 
69   // List the devices that the test knows about
70   void List(const std::vector<std::string>& args);
71 
72   // Change the device's MAC address
73   void SetDeviceAddress(const std::vector<std::string>& args);
74 
75   // Change the device's configuration
76   void SetDeviceConfiguration(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,
96                      std::function<void(const std::vector<std::string>&)>>
97       active_commands_;
98 
99   std::function<void(const std::string&)> send_response_;
100 
101   TestCommandHandler(const TestCommandHandler& cmdPckt) = delete;
102   TestCommandHandler& operator=(const TestCommandHandler& cmdPckt) = delete;
103 };
104 
105 }  // namespace rootcanal
106