1 /*
2  * Copyright 2019 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 #include <android/hardware/bluetooth/1.0/IBluetoothHci.h>
18 #include <android/hardware/bluetooth/1.0/IBluetoothHciCallbacks.h>
19 #include <android/hardware/bluetooth/1.0/types.h>
20 #include <stdlib.h>
21 
22 #include <future>
23 #include <vector>
24 
25 #include "hal/hci_hal.h"
26 #include "hal/snoop_logger.h"
27 #include "os/log.h"
28 
29 using ::android::hardware::hidl_vec;
30 using ::android::hardware::Return;
31 using ::android::hardware::Void;
32 using ::android::hardware::bluetooth::V1_0::IBluetoothHci;
33 using ::android::hardware::bluetooth::V1_0::IBluetoothHciCallbacks;
34 using HidlStatus = ::android::hardware::bluetooth::V1_0::Status;
35 
36 namespace bluetooth {
37 namespace hal {
38 namespace {
39 
40 class HciDeathRecipient : public ::android::hardware::hidl_death_recipient {
41  public:
serviceDied(uint64_t,const android::wp<::android::hidl::base::V1_0::IBase> &)42   virtual void serviceDied(uint64_t /*cookie*/, const android::wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
43     LOG_ERROR("Bluetooth HAL service died!");
44     abort();
45   }
46 };
47 
48 android::sp<HciDeathRecipient> hci_death_recipient_ = new HciDeathRecipient();
49 
50 class InternalHciCallbacks : public IBluetoothHciCallbacks {
51  public:
InternalHciCallbacks(SnoopLogger * btsnoop_logger)52   InternalHciCallbacks(SnoopLogger* btsnoop_logger) : btsnoop_logger_(btsnoop_logger) {
53     init_promise_ = new std::promise<void>();
54   }
55 
SetCallback(HciHalCallbacks * callback)56   void SetCallback(HciHalCallbacks* callback) {
57     ASSERT(callback_ == nullptr && callback != nullptr);
58     callback_ = callback;
59   }
60 
ResetCallback()61   void ResetCallback() {
62     callback_ = nullptr;
63   }
64 
GetInitPromise()65   std::promise<void>* GetInitPromise() {
66     return init_promise_;
67   }
68 
initializationComplete(HidlStatus status)69   Return<void> initializationComplete(HidlStatus status) {
70     ASSERT(status == HidlStatus::SUCCESS);
71     init_promise_->set_value();
72     return Void();
73   }
74 
hciEventReceived(const hidl_vec<uint8_t> & event)75   Return<void> hciEventReceived(const hidl_vec<uint8_t>& event) {
76     std::vector<uint8_t> received_hci_packet(event.begin(), event.end());
77     btsnoop_logger_->capture(received_hci_packet, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::EVT);
78     if (callback_ != nullptr) {
79       callback_->hciEventReceived(std::move(received_hci_packet));
80     }
81     return Void();
82   }
83 
aclDataReceived(const hidl_vec<uint8_t> & data)84   Return<void> aclDataReceived(const hidl_vec<uint8_t>& data) {
85     std::vector<uint8_t> received_hci_packet(data.begin(), data.end());
86     btsnoop_logger_->capture(received_hci_packet, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::ACL);
87     if (callback_ != nullptr) {
88       callback_->aclDataReceived(std::move(received_hci_packet));
89     }
90     return Void();
91   }
92 
scoDataReceived(const hidl_vec<uint8_t> & data)93   Return<void> scoDataReceived(const hidl_vec<uint8_t>& data) {
94     std::vector<uint8_t> received_hci_packet(data.begin(), data.end());
95     btsnoop_logger_->capture(received_hci_packet, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::SCO);
96     if (callback_ != nullptr) {
97       callback_->scoDataReceived(std::move(received_hci_packet));
98     }
99     return Void();
100   }
101 
102  private:
103   std::promise<void>* init_promise_ = nullptr;
104   HciHalCallbacks* callback_ = nullptr;
105   SnoopLogger* btsnoop_logger_ = nullptr;
106 };
107 
108 }  // namespace
109 
110 const std::string SnoopLogger::DefaultFilePath = "/data/misc/bluetooth/logs/btsnoop_hci.log";
111 const bool SnoopLogger::AlwaysFlush = false;
112 
113 class HciHalHidl : public HciHal {
114  public:
registerIncomingPacketCallback(HciHalCallbacks * callback)115   void registerIncomingPacketCallback(HciHalCallbacks* callback) override {
116     callbacks_->SetCallback(callback);
117   }
118 
unregisterIncomingPacketCallback()119   void unregisterIncomingPacketCallback() override {
120     callbacks_->ResetCallback();
121   }
122 
sendHciCommand(HciPacket command)123   void sendHciCommand(HciPacket command) override {
124     btsnoop_logger_->capture(command, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::CMD);
125     bt_hci_->sendHciCommand(command);
126   }
127 
sendAclData(HciPacket packet)128   void sendAclData(HciPacket packet) override {
129     btsnoop_logger_->capture(packet, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
130     bt_hci_->sendAclData(packet);
131   }
132 
sendScoData(HciPacket packet)133   void sendScoData(HciPacket packet) override {
134     btsnoop_logger_->capture(packet, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::SCO);
135     bt_hci_->sendScoData(packet);
136   }
137 
138  protected:
ListDependencies(ModuleList * list)139   void ListDependencies(ModuleList* list) override {
140     list->add<SnoopLogger>();
141   }
142 
Start()143   void Start() override {
144     btsnoop_logger_ = GetDependency<SnoopLogger>();
145     bt_hci_ = IBluetoothHci::getService();
146     ASSERT(bt_hci_ != nullptr);
147     auto death_link = bt_hci_->linkToDeath(hci_death_recipient_, 0);
148     ASSERT_LOG(death_link.isOk(), "Unable to set the death recipient for the Bluetooth HAL");
149     // Block allows allocation of a variable that might be bypassed by goto.
150     {
151       callbacks_ = new InternalHciCallbacks(btsnoop_logger_);
152       bt_hci_->initialize(callbacks_);
153       // Don't timeout here, time out at a higher layer
154       callbacks_->GetInitPromise()->get_future().wait();
155     }
156   }
157 
Stop()158   void Stop() override {
159     ASSERT(bt_hci_ != nullptr);
160     auto death_unlink = bt_hci_->unlinkToDeath(hci_death_recipient_);
161     if (!death_unlink.isOk()) {
162       LOG_ERROR("Error unlinking death recipient from the Bluetooth HAL");
163     }
164     bt_hci_->close();
165     callbacks_->ResetCallback();
166     bt_hci_ = nullptr;
167   }
168 
169  private:
170   android::sp<InternalHciCallbacks> callbacks_;
171   android::sp<IBluetoothHci> bt_hci_;
172   SnoopLogger* btsnoop_logger_;
173 };
174 
__anone88e76320202() 175 const ModuleFactory HciHal::Factory = ModuleFactory([]() { return new HciHalHidl(); });
176 
177 }  // namespace hal
178 }  // namespace bluetooth
179