1 // 2 // Copyright 2017 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 <chrono> 20 #include <functional> 21 #include <future> 22 #include <memory> 23 #include <string> 24 #include <vector> 25 26 #include "model/controller/controller_properties.h" 27 #include "model/setup/async_manager.h" 28 #include "model/setup/test_channel_transport.h" 29 #include "model/setup/test_command_handler.h" 30 #include "model/setup/test_model.h" 31 #include "net/async_data_channel_server.h" 32 33 namespace android::net { 34 class AsyncDataChannel; 35 class AsyncDataChannelConnector; 36 } // namespace android::net 37 38 namespace rootcanal { 39 40 using android::net::AsyncDataChannel; 41 using android::net::AsyncDataChannelConnector; 42 using android::net::AsyncDataChannelServer; 43 using android::net::ConnectCallback; 44 45 using rootcanal::AsyncManager; 46 using rootcanal::Device; 47 using rootcanal::Phy; 48 49 class TestEnvironment { 50 public: 51 TestEnvironment( 52 std::function<std::shared_ptr<AsyncDataChannelServer>(AsyncManager*, int)> 53 open_server, 54 std::function<std::shared_ptr<AsyncDataChannelConnector>(AsyncManager*)> 55 open_connector, 56 int test_port, int hci_port, int link_port, int link_ble_port, 57 std::string const& config_str, 58 bool enable_hci_sniffer = false, bool enable_baseband_sniffer = false, 59 bool enable_pcap_filter = false, bool disable_address_reuse = false); 60 61 void initialize(std::promise<void> barrier); 62 void close(); 63 64 private: 65 rootcanal::AsyncManager async_manager_; 66 rootcanal::TestChannelTransport test_channel_transport_; 67 std::shared_ptr<AsyncDataChannelServer> test_socket_server_; 68 std::vector<std::shared_ptr<AsyncDataChannelServer>> hci_socket_servers_; 69 std::shared_ptr<AsyncDataChannelServer> link_socket_server_; 70 std::shared_ptr<AsyncDataChannelServer> link_ble_socket_server_; 71 std::shared_ptr<AsyncDataChannelConnector> connector_; 72 bool enable_hci_sniffer_; 73 bool enable_baseband_sniffer_; 74 bool enable_pcap_filter_; 75 bool test_channel_open_{false}; 76 std::promise<void> barrier_; 77 rootcanal::AsyncUserId socket_user_id_{}; 78 79 void SetUpTestChannel(); 80 void SetUpHciServer( 81 std::function<std::shared_ptr<AsyncDataChannelServer>(AsyncManager*, int)> 82 open_server, 83 int tcp_port, rootcanal::ControllerProperties properties); 84 void SetUpLinkLayerServer(); 85 void SetUpLinkBleLayerServer(); 86 87 std::shared_ptr<Device> ConnectToRemoteServer(const std::string& server, 88 int port, Phy::Type phy_type); 89 90 rootcanal::TestModel test_model_{ 91 [this]() { return async_manager_.GetNextUserId(); }, 92 [this](rootcanal::AsyncUserId user_id, std::chrono::milliseconds delay, 93 const rootcanal::TaskCallback& task) { 94 return async_manager_.ExecAsync(user_id, delay, task); 95 }, 96 97 [this](rootcanal::AsyncUserId user_id, std::chrono::milliseconds delay, 98 std::chrono::milliseconds period, 99 const rootcanal::TaskCallback& task) { 100 return async_manager_.ExecAsyncPeriodically(user_id, delay, period, 101 task); 102 }, 103 104 [this](rootcanal::AsyncUserId user) { 105 async_manager_.CancelAsyncTasksFromUser(user); 106 }, 107 108 [this](rootcanal::AsyncTaskId task) { 109 async_manager_.CancelAsyncTask(task); 110 }, 111 112 [this](const std::string& server, int port, Phy::Type phy_type) { 113 return ConnectToRemoteServer(server, port, phy_type); 114 }}; 115 116 rootcanal::TestCommandHandler test_channel_{test_model_}; 117 }; 118 119 } // namespace rootcanal 120