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