• 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,
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   // Timer management functions
76   void SetTimerPeriod(const std::vector<std::string>& args);
77 
78   void StartTimer(const std::vector<std::string>& args);
79 
80   void StopTimer(const std::vector<std::string>& args);
81 
82   void Reset(const std::vector<std::string>& args);
83 
84   // For manual testing
85   void AddDefaults();
86 
87  private:
88   TestModel& model_;
89 
90   std::string response_string_;
91 
92   std::unordered_map<std::string,
93                      std::function<void(const std::vector<std::string>&)>>
94       active_commands_;
95 
96   std::function<void(const std::string&)> send_response_;
97 
98   TestCommandHandler(const TestCommandHandler& cmdPckt) = delete;
99   TestCommandHandler& operator=(const TestCommandHandler& cmdPckt) = delete;
100 };
101 
102 }  // namespace rootcanal
103