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(const sp<V1_0::IBluetoothHciCallbacks>& cb) override; 52 ::android::hardware::Return<void> initialize_1_1( 53 const sp<V1_1::IBluetoothHciCallbacks>& cb) override; 54 55 ::android::hardware::Return<void> sendHciCommand( 56 const ::android::hardware::hidl_vec<uint8_t>& packet) override; 57 58 ::android::hardware::Return<void> sendAclData( 59 const ::android::hardware::hidl_vec<uint8_t>& packet) override; 60 61 ::android::hardware::Return<void> sendScoData( 62 const ::android::hardware::hidl_vec<uint8_t>& packet) override; 63 64 ::android::hardware::Return<void> sendIsoData( 65 const ::android::hardware::hidl_vec<uint8_t>& packet) override; 66 67 ::android::hardware::Return<void> close() override; 68 69 static void OnPacketReady(); 70 71 static BluetoothHci* get(); 72 73 private: 74 ::android::hardware::Return<void> initialize_impl(const sp<V1_0::IBluetoothHciCallbacks>& cb, 75 const sp<V1_1::IBluetoothHciCallbacks>& cb_1_1); 76 77 sp<BluetoothDeathRecipient> death_recipient_; 78 79 std::function<void(sp<BluetoothDeathRecipient>&)> unlink_cb_; 80 81 void HandleIncomingPacket(); 82 83 std::shared_ptr<AsyncDataChannelServer> test_socket_server_; 84 std::shared_ptr<AsyncDataChannelServer> hci_socket_server_; 85 std::shared_ptr<AsyncDataChannelServer> link_socket_server_; 86 std::shared_ptr<AsyncDataChannelConnector> connector_; 87 rootcanal::AsyncManager async_manager_; 88 89 void SetUpTestChannel(); 90 void SetUpHciServer(ConnectCallback on_connect); 91 void SetUpLinkLayerServer(ConnectCallback on_connect); 92 std::shared_ptr<Device> ConnectToRemoteServer(const std::string& server, int port, 93 Phy::Type phy_type); 94 95 std::shared_ptr<rootcanal::DualModeController> controller_; 96 97 rootcanal::TestChannelTransport test_channel_transport_; 98 rootcanal::TestChannelTransport remote_hci_transport_; 99 rootcanal::TestChannelTransport remote_link_layer_transport_; 100 101 rootcanal::AsyncUserId user_id_ = async_manager_.GetNextUserId(); 102 rootcanal::TestModel test_model_{ 103 [this]() { return async_manager_.GetNextUserId(); }, 104 [this](rootcanal::AsyncUserId user_id, std::chrono::milliseconds delay, 105 const rootcanal::TaskCallback& task) { 106 return async_manager_.ExecAsync(user_id, delay, task); 107 }, 108 109 [this](rootcanal::AsyncUserId user_id, std::chrono::milliseconds delay, 110 std::chrono::milliseconds period, const rootcanal::TaskCallback& task) { 111 return async_manager_.ExecAsyncPeriodically(user_id, delay, period, task); 112 }, 113 114 [this](rootcanal::AsyncUserId user) { async_manager_.CancelAsyncTasksFromUser(user); }, 115 116 [this](rootcanal::AsyncTaskId task) { async_manager_.CancelAsyncTask(task); }, 117 118 [this](const std::string& server, int port, Phy::Type phy_type) { 119 return ConnectToRemoteServer(server, port, phy_type); 120 }}; 121 rootcanal::TestCommandHandler test_channel_{test_model_}; 122 }; 123 124 extern "C" IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* name); 125 126 } // namespace sim 127 } // namespace V1_1 128 } // namespace bluetooth 129 } // namespace hardware 130 } // namespace android 131