• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, const std::vector<std::string>& args);
41 
42   // Dispatches the action corresponding to the command specified by |name|.
43   void RegisterSendResponse(std::function<void(const std::string&)> callback);
44 
45   // Commands:
46 
47   // Add a device
48   void AddDevice(const std::vector<std::string>& args);
49 
50   // Add a remote device
51   void AddRemote(const std::vector<std::string>& args);
52 
53   // Remove devices by index
54   void RemoveDevice(const std::vector<std::string>& args);
55 
56   // Add phy
57   void AddPhy(const std::vector<std::string>& args);
58 
59   // Remove phy by name
60   void RemovePhy(const std::vector<std::string>& args);
61 
62   // Add device to phy
63   void AddDeviceToPhy(const std::vector<std::string>& args);
64 
65   // Remove device from phy
66   void RemoveDeviceFromPhy(const std::vector<std::string>& args);
67 
68   // List the devices that the test knows about
69   void List(const std::vector<std::string>& args);
70 
71   // Change the device's MAC address
72   void SetDeviceAddress(const std::vector<std::string>& args);
73 
74   // Change the device's configuration
75   void SetDeviceConfiguration(const std::vector<std::string>& args);
76 
77   // Timer management functions
78   void SetTimerPeriod(const std::vector<std::string>& args);
79 
80   void StartTimer(const std::vector<std::string>& args);
81 
82   void StopTimer(const std::vector<std::string>& args);
83 
84   void Reset(const std::vector<std::string>& args);
85 
86   // For manual testing
87   void AddDefaults();
88 
89 private:
90   TestModel& model_;
91 
92   std::string response_string_;
93 
94   std::unordered_map<std::string, std::function<void(const std::vector<std::string>&)>>
95           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 rootcanal
104