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/types.h>
18 #include <android/hardware/bluetooth/1.1/IBluetoothHci.h>
19 #include <android/hardware/bluetooth/1.1/IBluetoothHciCallbacks.h>
20 #include <stdlib.h>
21
22 #include <future>
23 #include <vector>
24
25 #include "btaa/activity_attribution.h"
26 #include "common/init_flags.h"
27 #include "common/stop_watch.h"
28 #include "common/strings.h"
29 #include "hal/hci_hal.h"
30 #include "hal/snoop_logger.h"
31 #include "os/alarm.h"
32 #include "os/log.h"
33
34 using ::android::hardware::hidl_vec;
35 using ::android::hardware::Return;
36 using ::android::hardware::Void;
37 using ::android::hardware::bluetooth::V1_1::IBluetoothHci;
38 using ::android::hardware::bluetooth::V1_1::IBluetoothHciCallbacks;
39 using HidlStatus = ::android::hardware::bluetooth::V1_0::Status;
40 using IBluetoothHci_1_0 = ::android::hardware::bluetooth::V1_0::IBluetoothHci;
41 using bluetooth::common::BindOnce;
42
43 namespace bluetooth {
44 namespace hal {
45 namespace {
46
47 class HciDeathRecipient : public ::android::hardware::hidl_death_recipient {
48 public:
serviceDied(uint64_t,const android::wp<::android::hidl::base::V1_0::IBase> &)49 virtual void serviceDied(uint64_t /*cookie*/, const android::wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
50 LOG_ERROR("Bluetooth HAL service died!");
51 common::StopWatch::DumpStopWatchLog();
52 abort();
53 }
54 };
55
56 android::sp<HciDeathRecipient> hci_death_recipient_ = new HciDeathRecipient();
57
58 template <class VecType>
GetTimerText(const char * func_name,VecType vec)59 std::string GetTimerText(const char* func_name, VecType vec) {
60 return common::StringFormat(
61 "%s: len %zu, 1st 5 bytes '%s'",
62 func_name,
63 vec.size(),
64 common::ToHexString(vec.begin(), std::min(vec.end(), vec.begin() + 5)).c_str());
65 }
66
67 class InternalHciCallbacks : public IBluetoothHciCallbacks {
68 public:
InternalHciCallbacks(activity_attribution::ActivityAttribution * btaa_logger_,SnoopLogger * btsnoop_logger)69 InternalHciCallbacks(activity_attribution::ActivityAttribution* btaa_logger_, SnoopLogger* btsnoop_logger)
70 : btaa_logger_(btaa_logger_), btsnoop_logger_(btsnoop_logger) {
71 init_promise_ = new std::promise<void>();
72 }
73
SetCallback(HciHalCallbacks * callback)74 void SetCallback(HciHalCallbacks* callback) {
75 ASSERT(callback_ == nullptr && callback != nullptr);
76 callback_ = callback;
77 }
78
ResetCallback()79 void ResetCallback() {
80 callback_ = nullptr;
81 }
82
GetInitPromise()83 std::promise<void>* GetInitPromise() {
84 return init_promise_;
85 }
86
initializationComplete(HidlStatus status)87 Return<void> initializationComplete(HidlStatus status) {
88 common::StopWatch stop_watch(__func__);
89 ASSERT(status == HidlStatus::SUCCESS);
90 init_promise_->set_value();
91 return Void();
92 }
93
hciEventReceived(const hidl_vec<uint8_t> & event)94 Return<void> hciEventReceived(const hidl_vec<uint8_t>& event) override {
95 common::StopWatch stop_watch(GetTimerText(__func__, event));
96 std::vector<uint8_t> received_hci_packet(event.begin(), event.end());
97 btsnoop_logger_->Capture(received_hci_packet, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::EVT);
98 if (common::init_flags::btaa_hci_is_enabled()) {
99 btaa_logger_->Capture(received_hci_packet, SnoopLogger::PacketType::EVT);
100 }
101 if (callback_ != nullptr) {
102 callback_->hciEventReceived(std::move(received_hci_packet));
103 }
104 return Void();
105 }
106
aclDataReceived(const hidl_vec<uint8_t> & data)107 Return<void> aclDataReceived(const hidl_vec<uint8_t>& data) override {
108 common::StopWatch stop_watch(GetTimerText(__func__, data));
109 std::vector<uint8_t> received_hci_packet(data.begin(), data.end());
110 btsnoop_logger_->Capture(received_hci_packet, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::ACL);
111 if (common::init_flags::btaa_hci_is_enabled()) {
112 btaa_logger_->Capture(received_hci_packet, SnoopLogger::PacketType::ACL);
113 }
114 if (callback_ != nullptr) {
115 callback_->aclDataReceived(std::move(received_hci_packet));
116 }
117 return Void();
118 }
119
scoDataReceived(const hidl_vec<uint8_t> & data)120 Return<void> scoDataReceived(const hidl_vec<uint8_t>& data) override {
121 common::StopWatch stop_watch(GetTimerText(__func__, data));
122 std::vector<uint8_t> received_hci_packet(data.begin(), data.end());
123 btsnoop_logger_->Capture(received_hci_packet, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::SCO);
124 if (common::init_flags::btaa_hci_is_enabled()) {
125 btaa_logger_->Capture(received_hci_packet, SnoopLogger::PacketType::SCO);
126 }
127 if (callback_ != nullptr) {
128 callback_->scoDataReceived(std::move(received_hci_packet));
129 }
130 return Void();
131 }
132
isoDataReceived(const hidl_vec<uint8_t> & data)133 Return<void> isoDataReceived(const hidl_vec<uint8_t>& data) override {
134 common::StopWatch stop_watch(GetTimerText(__func__, data));
135 std::vector<uint8_t> received_hci_packet(data.begin(), data.end());
136 btsnoop_logger_->Capture(received_hci_packet, SnoopLogger::Direction::INCOMING, SnoopLogger::PacketType::ISO);
137 if (callback_ != nullptr) {
138 callback_->isoDataReceived(std::move(received_hci_packet));
139 }
140 return Void();
141 }
142
143 private:
144 std::promise<void>* init_promise_ = nullptr;
145 HciHalCallbacks* callback_ = nullptr;
146 activity_attribution::ActivityAttribution* btaa_logger_ = nullptr;
147 SnoopLogger* btsnoop_logger_ = nullptr;
148 };
149
150 } // namespace
151
152 class HciHalHidl : public HciHal {
153 public:
registerIncomingPacketCallback(HciHalCallbacks * callback)154 void registerIncomingPacketCallback(HciHalCallbacks* callback) override {
155 callbacks_->SetCallback(callback);
156 }
157
unregisterIncomingPacketCallback()158 void unregisterIncomingPacketCallback() override {
159 callbacks_->ResetCallback();
160 }
161
sendHciCommand(HciPacket command)162 void sendHciCommand(HciPacket command) override {
163 btsnoop_logger_->Capture(command, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::CMD);
164 if (common::init_flags::btaa_hci_is_enabled()) {
165 btaa_logger_->Capture(command, SnoopLogger::PacketType::CMD);
166 }
167 bt_hci_->sendHciCommand(command);
168 }
169
sendAclData(HciPacket packet)170 void sendAclData(HciPacket packet) override {
171 btsnoop_logger_->Capture(packet, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ACL);
172 if (common::init_flags::btaa_hci_is_enabled()) {
173 btaa_logger_->Capture(packet, SnoopLogger::PacketType::ACL);
174 }
175 bt_hci_->sendAclData(packet);
176 }
177
sendScoData(HciPacket packet)178 void sendScoData(HciPacket packet) override {
179 btsnoop_logger_->Capture(packet, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::SCO);
180 if (common::init_flags::btaa_hci_is_enabled()) {
181 btaa_logger_->Capture(packet, SnoopLogger::PacketType::SCO);
182 }
183 bt_hci_->sendScoData(packet);
184 }
185
sendIsoData(HciPacket packet)186 void sendIsoData(HciPacket packet) override {
187 if (bt_hci_1_1_ == nullptr) {
188 LOG_ERROR("ISO is not supported in HAL v1.0");
189 return;
190 }
191
192 btsnoop_logger_->Capture(packet, SnoopLogger::Direction::OUTGOING, SnoopLogger::PacketType::ISO);
193 bt_hci_1_1_->sendIsoData(packet);
194 }
195
196 protected:
ListDependencies(ModuleList * list) const197 void ListDependencies(ModuleList* list) const {
198 list->add<SnoopLogger>();
199 if (common::init_flags::btaa_hci_is_enabled()) {
200 list->add<activity_attribution::ActivityAttribution>();
201 }
202 }
203
Start()204 void Start() override {
205 if (common::init_flags::btaa_hci_is_enabled()) {
206 btaa_logger_ = GetDependency<activity_attribution::ActivityAttribution>();
207 }
208 btsnoop_logger_ = GetDependency<SnoopLogger>();
209
210 auto get_service_alarm = new os::Alarm(GetHandler());
211 get_service_alarm->Schedule(
212 BindOnce([] {
213 LOG_ALWAYS_FATAL("Unable to get a Bluetooth service after 500ms, start the HAL before starting Bluetooth");
214 }),
215 std::chrono::milliseconds(500));
216
217 bt_hci_1_1_ = IBluetoothHci::getService();
218
219 if (bt_hci_1_1_ != nullptr) {
220 bt_hci_ = bt_hci_1_1_;
221 } else {
222 bt_hci_ = IBluetoothHci_1_0::getService();
223 }
224
225 get_service_alarm->Cancel();
226 delete get_service_alarm;
227
228 ASSERT(bt_hci_ != nullptr);
229 auto death_link = bt_hci_->linkToDeath(hci_death_recipient_, 0);
230 ASSERT_LOG(death_link.isOk(), "Unable to set the death recipient for the Bluetooth HAL");
231 callbacks_ = new InternalHciCallbacks(btaa_logger_, btsnoop_logger_);
232
233 if (bt_hci_1_1_ != nullptr) {
234 bt_hci_1_1_->initialize_1_1(callbacks_);
235 } else {
236 bt_hci_->initialize(callbacks_);
237 }
238
239 // Don't timeout here, time out at a higher layer
240 callbacks_->GetInitPromise()->get_future().wait();
241 }
242
Stop()243 void Stop() override {
244 ASSERT(bt_hci_ != nullptr);
245 auto death_unlink = bt_hci_->unlinkToDeath(hci_death_recipient_);
246 if (!death_unlink.isOk()) {
247 LOG_ERROR("Error unlinking death recipient from the Bluetooth HAL");
248 }
249 auto close_status = bt_hci_->close();
250 if (!close_status.isOk()) {
251 LOG_ERROR("Error calling close on the Bluetooth HAL");
252 }
253 callbacks_->ResetCallback();
254 bt_hci_ = nullptr;
255 bt_hci_1_1_ = nullptr;
256 }
257
ToString() const258 std::string ToString() const override {
259 return std::string("HciHalHidl");
260 }
261
262 private:
263 android::sp<InternalHciCallbacks> callbacks_;
264 android::sp<IBluetoothHci_1_0> bt_hci_;
265 android::sp<IBluetoothHci> bt_hci_1_1_;
266 activity_attribution::ActivityAttribution* btaa_logger_;
267 SnoopLogger* btsnoop_logger_;
268 };
269
__anon43993d740302() 270 const ModuleFactory HciHal::Factory = ModuleFactory([]() { return new HciHalHidl(); });
271
272 } // namespace hal
273 } // namespace bluetooth
274