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( 36 std::function<AsyncUserId()> getNextUserId, 37 std::function<AsyncTaskId(AsyncUserId, std::chrono::milliseconds, 38 const TaskCallback&)> 39 evtScheduler, 40 std::function<AsyncTaskId(AsyncUserId, std::chrono::milliseconds, 41 std::chrono::milliseconds, const TaskCallback&)> 42 periodicEvtScheduler, 43 std::function<void(AsyncUserId)> cancel_user_tasks, 44 std::function<void(AsyncTaskId)> cancel, 45 std::function<int(const std::string&, int)> connect_to_remote); 46 ~TestModel() = default; 47 48 TestModel(TestModel& model) = delete; 49 TestModel& operator=(const TestModel& model) = delete; 50 51 // Commands: 52 53 // Add a device, return its index 54 size_t Add(std::shared_ptr<Device> device); 55 56 // Remove devices by index 57 void Del(size_t device_index); 58 59 // Add phy, return its index 60 size_t AddPhy(Phy::Type phy_type); 61 62 // Remove phy by index 63 void DelPhy(size_t phy_index); 64 65 // Add device to phy 66 void AddDeviceToPhy(size_t device_index, size_t phy_index); 67 68 // Remove device from phy 69 void DelDeviceFromPhy(size_t device_index, size_t phy_index); 70 71 // Handle incoming remote connections 72 void AddLinkLayerConnection(int socket_fd, Phy::Type phy_type); 73 void IncomingLinkLayerConnection(int socket_fd); 74 void IncomingHciConnection(int socket_fd); 75 76 // Handle closed remote connections 77 void OnHciConnectionClosed(int socket_fd, size_t index, AsyncUserId user_id); 78 79 // Connect to a remote device 80 void AddRemote(const std::string& server, int port, Phy::Type phy_type); 81 82 // Set the device's Bluetooth address 83 void SetDeviceAddress(size_t device_index, Address device_address); 84 85 // Let devices know about the passage of time 86 void TimerTick(); 87 void StartTimer(); 88 void StopTimer(); 89 void SetTimerPeriod(std::chrono::milliseconds new_period); 90 91 // List the devices that the test knows about 92 const std::string& List(); 93 94 // Clear all devices and phys. 95 void Reset(); 96 97 private: 98 std::vector<PhyLayerFactory> phys_; 99 std::vector<std::shared_ptr<Device>> devices_; 100 std::string list_string_; 101 102 // Callbacks to schedule tasks. 103 std::function<AsyncUserId()> get_user_id_; 104 std::function<AsyncTaskId(AsyncUserId, std::chrono::milliseconds, 105 const TaskCallback&)> 106 schedule_task_; 107 std::function<AsyncTaskId(AsyncUserId, std::chrono::milliseconds, 108 std::chrono::milliseconds, const TaskCallback&)> 109 schedule_periodic_task_; 110 std::function<void(AsyncTaskId)> cancel_task_; 111 std::function<void(AsyncUserId)> cancel_tasks_from_user_; 112 std::function<int(const std::string&, int)> connect_to_remote_; 113 114 AsyncUserId model_user_id_; 115 AsyncTaskId timer_tick_task_{kInvalidTaskId}; 116 std::chrono::milliseconds timer_period_{}; 117 118 std::vector<std::shared_ptr<Device>> example_devices_; 119 }; 120 121 } // namespace test_vendor_lib 122