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