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