• 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 #include <cstdint>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "async_manager.h"
27 #include "model/devices/device.h"
28 #include "phy_layer_factory.h"
29 #include "test_channel_transport.h"
30 
31 namespace test_vendor_lib {
32 
33 class TestModel {
34  public:
35   TestModel(std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)> evtScheduler,
36             std::function<AsyncTaskId(std::chrono::milliseconds, std::chrono::milliseconds, const TaskCallback&)>
37                 periodicEvtScheduler,
38             std::function<void(AsyncTaskId)> cancel, std::function<int(const std::string&, int)> connect_to_remote);
39   ~TestModel() = default;
40 
41   // Commands:
42 
43   // Add a device, return its index
44   size_t Add(std::shared_ptr<Device> device);
45 
46   // Remove devices by index
47   void Del(size_t device_index);
48 
49   // Add phy, return its index
50   size_t AddPhy(std::shared_ptr<PhyLayerFactory> phy);
51 
52   // Remove phy by index
53   void DelPhy(size_t phy_index);
54 
55   // Add device to phy
56   void AddDeviceToPhy(size_t device_index, size_t phy_index);
57 
58   // Remove device from phy
59   void DelDeviceFromPhy(size_t device_index, size_t phy_index);
60 
61   // Handle incoming remote connections
62   void AddLinkLayerConnection(int socket_fd, Phy::Type phy_type);
63   void IncomingLinkLayerConnection(int socket_fd);
64   void IncomingHciConnection(int socket_fd);
65 
66   // Connect to a remote device
67   void AddRemote(const std::string& server, int port, Phy::Type phy_type);
68 
69   // Let devices know about the passage of time
70   void TimerTick();
71   void StartTimer();
72   void StopTimer();
73   void SetTimerPeriod(std::chrono::milliseconds new_period);
74 
75   // List the devices that the test knows about
76   const std::string& List();
77 
78   // Clear all devices and phys.
79   void Reset();
80 
81  private:
82   std::vector<std::shared_ptr<PhyLayerFactory>> phys_;
83   std::vector<std::shared_ptr<Device>> devices_;
84   std::string list_string_;
85 
86   // Callbacks to schedule tasks.
87   std::function<AsyncTaskId(std::chrono::milliseconds, const TaskCallback&)> schedule_task_;
88   std::function<AsyncTaskId(std::chrono::milliseconds, std::chrono::milliseconds, const TaskCallback&)>
89       schedule_periodic_task_;
90   std::function<void(AsyncTaskId)> cancel_task_;
91   std::function<int(const std::string&, int)> connect_to_remote_;
92 
93   AsyncTaskId timer_tick_task_{kInvalidTaskId};
94   std::chrono::milliseconds timer_period_;
95 
96   TestModel(TestModel& model) = delete;
97   TestModel& operator=(const TestModel& model) = delete;
98 
99   std::vector<std::shared_ptr<Device>> example_devices_;
100 };
101 
102 }  // namespace test_vendor_lib
103