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 <android/hardware/bluetooth/1.1/IBluetoothHci.h> 20 #include <hidl/MQDescriptor.h> 21 22 #include "hci_packetizer.h" 23 #include "model/controller/dual_mode_controller.h" 24 #include "model/setup/async_manager.h" 25 #include "model/setup/test_channel_transport.h" 26 #include "model/setup/test_command_handler.h" 27 #include "model/setup/test_model.h" 28 #include "net/posix/posix_async_socket_connector.h" 29 #include "net/posix/posix_async_socket_server.h" 30 31 namespace android { 32 namespace hardware { 33 namespace bluetooth { 34 namespace V1_1 { 35 namespace sim { 36 37 class BluetoothDeathRecipient; 38 39 using android::net::AsyncDataChannel; 40 using android::net::AsyncDataChannelConnector; 41 using android::net::AsyncDataChannelServer; 42 using android::net::ConnectCallback; 43 44 using rootcanal::Device; 45 using rootcanal::Phy; 46 47 class BluetoothHci : public IBluetoothHci { 48 public: 49 BluetoothHci(); 50 51 ::android::hardware::Return<void> initialize( 52 const sp<V1_0::IBluetoothHciCallbacks>& cb) override; 53 ::android::hardware::Return<void> initialize_1_1( 54 const sp<V1_1::IBluetoothHciCallbacks>& cb) override; 55 56 ::android::hardware::Return<void> sendHciCommand( 57 const ::android::hardware::hidl_vec<uint8_t>& packet) override; 58 59 ::android::hardware::Return<void> sendAclData( 60 const ::android::hardware::hidl_vec<uint8_t>& packet) override; 61 62 ::android::hardware::Return<void> sendScoData( 63 const ::android::hardware::hidl_vec<uint8_t>& packet) override; 64 65 ::android::hardware::Return<void> sendIsoData( 66 const ::android::hardware::hidl_vec<uint8_t>& packet) override; 67 68 ::android::hardware::Return<void> close() override; 69 70 static void OnPacketReady(); 71 72 static BluetoothHci* get(); 73 74 private: 75 ::android::hardware::Return<void> initialize_impl( 76 const sp<V1_0::IBluetoothHciCallbacks>& cb, 77 const sp<V1_1::IBluetoothHciCallbacks>& cb_1_1); 78 79 sp<BluetoothDeathRecipient> death_recipient_; 80 81 std::function<void(sp<BluetoothDeathRecipient>&)> unlink_cb_; 82 83 void HandleIncomingPacket(); 84 85 std::shared_ptr<AsyncDataChannelServer> test_socket_server_; 86 std::shared_ptr<AsyncDataChannelServer> hci_socket_server_; 87 std::shared_ptr<AsyncDataChannelServer> link_socket_server_; 88 std::shared_ptr<AsyncDataChannelConnector> connector_; 89 rootcanal::AsyncManager async_manager_; 90 91 void SetUpTestChannel(); 92 void SetUpHciServer(ConnectCallback on_connect); 93 void SetUpLinkLayerServer(ConnectCallback on_connect); 94 std::shared_ptr<Device> ConnectToRemoteServer(const std::string& server, 95 int port, Phy::Type phy_type); 96 97 std::shared_ptr<rootcanal::DualModeController> controller_; 98 99 rootcanal::TestChannelTransport test_channel_transport_; 100 rootcanal::TestChannelTransport remote_hci_transport_; 101 rootcanal::TestChannelTransport remote_link_layer_transport_; 102 103 rootcanal::AsyncUserId user_id_ = async_manager_.GetNextUserId(); 104 rootcanal::TestModel test_model_{ 105 [this]() { return async_manager_.GetNextUserId(); }, 106 [this](rootcanal::AsyncUserId user_id, std::chrono::milliseconds delay, 107 const rootcanal::TaskCallback& task) { 108 return async_manager_.ExecAsync(user_id, delay, task); 109 }, 110 111 [this](rootcanal::AsyncUserId user_id, std::chrono::milliseconds delay, 112 std::chrono::milliseconds period, 113 const rootcanal::TaskCallback& task) { 114 return async_manager_.ExecAsyncPeriodically(user_id, delay, period, 115 task); 116 }, 117 118 [this](rootcanal::AsyncUserId user) { 119 async_manager_.CancelAsyncTasksFromUser(user); 120 }, 121 122 [this](rootcanal::AsyncTaskId task) { 123 async_manager_.CancelAsyncTask(task); 124 }, 125 126 [this](const std::string& server, int port, Phy::Type phy_type) { 127 return ConnectToRemoteServer(server, port, phy_type); 128 }}; 129 rootcanal::TestCommandHandler test_channel_{test_model_}; 130 }; 131 132 extern "C" IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* name); 133 134 } // namespace sim 135 } // namespace V1_1 136 } // namespace bluetooth 137 } // namespace hardware 138 } // namespace android 139