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 <stddef.h> // for size_t 20 21 #include <chrono> // for milliseconds 22 #include <functional> // for function 23 #include <memory> // for shared_ptr 24 #include <string> // for string 25 #include <vector> // for vector 26 27 #include "hci/address.h" // for Address 28 #include "model/devices/hci_device.h" // for HciDevice 29 #include "model/setup/async_manager.h" // for AsyncUserId, AsyncTaskId 30 #include "phy.h" // for Phy, Phy::Type 31 #include "phy_layer_factory.h" // for PhyLayerFactory 32 33 namespace rootcanal { 34 class Device; 35 36 using ::bluetooth::hci::Address; 37 38 class TestModel { 39 public: 40 TestModel( 41 std::function<AsyncUserId()> getNextUserId, 42 std::function<AsyncTaskId(AsyncUserId, std::chrono::milliseconds, 43 const TaskCallback&)> 44 evtScheduler, 45 std::function<AsyncTaskId(AsyncUserId, std::chrono::milliseconds, 46 std::chrono::milliseconds, const TaskCallback&)> 47 periodicEvtScheduler, 48 std::function<void(AsyncUserId)> cancel_user_tasks, 49 std::function<void(AsyncTaskId)> cancel, 50 std::function<std::shared_ptr<Device>(const std::string&, int, Phy::Type)> 51 connect_to_remote, 52 std::array<uint8_t, 5> bluetooth_address_prefix = {0xda, 0x4c, 0x10, 0xde, 53 0x17}); 54 virtual ~TestModel(); 55 56 TestModel(TestModel& model) = delete; 57 TestModel& operator=(const TestModel& model) = delete; 58 59 // Commands: 60 61 // Add a device, return its index 62 size_t Add(std::shared_ptr<Device> device); 63 64 // Remove devices by index 65 void Del(size_t device_index); 66 67 // Add phy, return its index 68 size_t AddPhy(Phy::Type phy_type); 69 70 // Allow derived classes to use custom phy layer 71 virtual std::unique_ptr<PhyLayerFactory> CreatePhy(Phy::Type phy_type, size_t phy_index); 72 73 // Remove phy by index 74 void DelPhy(size_t phy_index); 75 76 // Add device to phy 77 void AddDeviceToPhy(size_t device_index, size_t phy_index); 78 79 // Remove device from phy 80 void DelDeviceFromPhy(size_t device_index, size_t phy_index); 81 82 // Handle incoming remote connections 83 void AddLinkLayerConnection(std::shared_ptr<Device> dev, Phy::Type phy_type); 84 // Add an HCI device, return its index 85 size_t AddHciConnection(std::shared_ptr<HciDevice> dev); 86 87 // Handle closed remote connections (both hci & link layer) 88 void OnConnectionClosed(size_t index, AsyncUserId user_id); 89 90 // Connect to a remote device 91 void AddRemote(const std::string& server, int port, Phy::Type phy_type); 92 93 // Set the device's Bluetooth address 94 void SetDeviceAddress(size_t device_index, Address device_address); 95 96 // Let devices know about the passage of time 97 void TimerTick(); 98 void StartTimer(); 99 void StopTimer(); 100 void SetTimerPeriod(std::chrono::milliseconds new_period); 101 102 // List the devices that the test knows about 103 const std::string& List(); 104 105 // Clear all devices and phys. 106 void Reset(); 107 108 private: 109 std::vector<std::unique_ptr<PhyLayerFactory>> phys_; 110 std::vector<std::shared_ptr<Device>> devices_; 111 std::string list_string_; 112 113 // Prefix used to generate public device addresses for hosts 114 // connecting over TCP. 115 std::array<uint8_t, 5> bluetooth_address_prefix_; 116 117 // Callbacks to schedule tasks. 118 std::function<AsyncUserId()> get_user_id_; 119 std::function<AsyncTaskId(AsyncUserId, std::chrono::milliseconds, 120 const TaskCallback&)> 121 schedule_task_; 122 std::function<AsyncTaskId(AsyncUserId, std::chrono::milliseconds, 123 std::chrono::milliseconds, const TaskCallback&)> 124 schedule_periodic_task_; 125 std::function<void(AsyncTaskId)> cancel_task_; 126 std::function<void(AsyncUserId)> cancel_tasks_from_user_; 127 std::function<std::shared_ptr<Device>(const std::string&, int, Phy::Type)> 128 connect_to_remote_; 129 130 AsyncUserId model_user_id_; 131 AsyncTaskId timer_tick_task_{kInvalidTaskId}; 132 std::chrono::milliseconds timer_period_{}; 133 }; 134 135 } // namespace rootcanal 136