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