blob: 1d15dd6f11cf49d8b7fcfe4299386f8e15f80d6c [file] [log] [blame]
Andre Eisenbach89ba5282016-10-13 15:45:02 -07001//
2// Copyright 2016 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 "vendor_interface.h"
18
19#define LOG_TAG "android.hardware.bluetooth@1.0-impl"
Myles Watson6a7d6222016-10-13 15:45:02 -070020#include <cutils/properties.h>
Andre Eisenbach89ba5282016-10-13 15:45:02 -070021#include <utils/Log.h>
22
23#include <dlfcn.h>
Myles Watson6a7d6222016-10-13 15:45:02 -070024#include <fcntl.h>
25
26#include "bluetooth_address.h"
Zach Johnson917efb12017-02-26 23:46:05 -080027#include "h4_protocol.h"
28#include "mct_protocol.h"
Andre Eisenbach89ba5282016-10-13 15:45:02 -070029
Ayushi Khopkara37d3df2021-06-24 20:24:03 +053030#ifdef BT_FUZZER
31static const char* VENDOR_LIBRARY_NAME = "libbt-vendor-fuzz.so";
32#else
Andre Eisenbach89ba5282016-10-13 15:45:02 -070033static const char* VENDOR_LIBRARY_NAME = "libbt-vendor.so";
Ayushi Khopkara37d3df2021-06-24 20:24:03 +053034#endif
Andre Eisenbach89ba5282016-10-13 15:45:02 -070035static const char* VENDOR_LIBRARY_SYMBOL_NAME =
36 "BLUETOOTH_VENDOR_LIB_INTERFACE";
37
38static const int INVALID_FD = -1;
39
40namespace {
41
Andre Eisenbach9041d972017-01-17 18:23:12 -080042using android::hardware::hidl_vec;
Jack Hecaeab052018-10-23 18:13:51 -070043using android::hardware::bluetooth::V1_0::implementation::VendorInterface;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070044
Myles Watson4e2e8ec2017-02-01 10:46:16 -080045struct {
46 tINT_CMD_CBACK cb;
47 uint16_t opcode;
48} internal_command;
Myles Watsona7d33b32017-01-24 09:09:58 -080049
Myles Watsonbeb13b42017-01-26 10:47:27 -080050// True when LPM is not enabled yet or wake is not asserted.
51bool lpm_wake_deasserted;
52uint32_t lpm_timeout_ms;
53bool recent_activity_flag;
54
Andre Eisenbach89ba5282016-10-13 15:45:02 -070055VendorInterface* g_vendor_interface = nullptr;
Guillaume Baileya2b4a182023-07-03 12:01:08 +000056std::mutex wakeup_mutex_;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070057
Andre Eisenbach9041d972017-01-17 18:23:12 -080058HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070059 size_t packet_size = data.size() + sizeof(HC_BT_HDR);
60 HC_BT_HDR* packet = reinterpret_cast<HC_BT_HDR*>(new uint8_t[packet_size]);
61 packet->offset = 0;
62 packet->len = data.size();
63 packet->layer_specific = 0;
64 packet->event = event;
Myles Watson6a7d6222016-10-13 15:45:02 -070065 // TODO(eisenbach): Avoid copy here; if BT_HDR->data can be ensured to
Andre Eisenbach89ba5282016-10-13 15:45:02 -070066 // be the only way the data is accessed, a pointer could be passed here...
67 memcpy(packet->data, data.data(), data.size());
68 return packet;
69}
70
Myles Watsona7d33b32017-01-24 09:09:58 -080071bool internal_command_event_match(const hidl_vec<uint8_t>& packet) {
72 uint8_t event_code = packet[0];
73 if (event_code != HCI_COMMAND_COMPLETE_EVENT) {
74 ALOGE("%s: Unhandled event type %02X", __func__, event_code);
75 return false;
76 }
77
78 size_t opcode_offset = HCI_EVENT_PREAMBLE_SIZE + 1; // Skip num packets.
79
80 uint16_t opcode = packet[opcode_offset] | (packet[opcode_offset + 1] << 8);
81
Myles Watson4e2e8ec2017-02-01 10:46:16 -080082 ALOGV("%s internal_command.opcode = %04X opcode = %04x", __func__,
83 internal_command.opcode, opcode);
84 return opcode == internal_command.opcode;
Myles Watsona7d33b32017-01-24 09:09:58 -080085}
86
Andre Eisenbach89ba5282016-10-13 15:45:02 -070087uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
Myles Watsona7d33b32017-01-24 09:09:58 -080088 ALOGV("%s opcode: 0x%04x, ptr: %p, cb: %p", __func__, opcode, buffer,
89 callback);
Myles Watson4e2e8ec2017-02-01 10:46:16 -080090 internal_command.cb = callback;
91 internal_command.opcode = opcode;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070092 uint8_t type = HCI_PACKET_TYPE_COMMAND;
Andre Eisenbach9041d972017-01-17 18:23:12 -080093 HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
Myles Watsondf765ea2017-01-30 09:07:37 -080094 VendorInterface::get()->Send(type, bt_hdr->data, bt_hdr->len);
Myles Watson4e2e8ec2017-02-01 10:46:16 -080095 delete[] reinterpret_cast<uint8_t*>(buffer);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070096 return true;
97}
98
99void firmware_config_cb(bt_vendor_op_result_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800100 ALOGV("%s result: %d", __func__, result);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700101 VendorInterface::get()->OnFirmwareConfigured(result);
102}
103
104void sco_config_cb(bt_vendor_op_result_t result) {
105 ALOGD("%s result: %d", __func__, result);
106}
107
108void low_power_mode_cb(bt_vendor_op_result_t result) {
109 ALOGD("%s result: %d", __func__, result);
110}
111
112void sco_audiostate_cb(bt_vendor_op_result_t result) {
113 ALOGD("%s result: %d", __func__, result);
114}
115
116void* buffer_alloc_cb(int size) {
117 void* p = new uint8_t[size];
118 ALOGV("%s pts: %p, size: %d", __func__, p, size);
119 return p;
120}
121
122void buffer_free_cb(void* buffer) {
123 ALOGV("%s ptr: %p", __func__, buffer);
124 delete[] reinterpret_cast<uint8_t*>(buffer);
125}
126
127void epilog_cb(bt_vendor_op_result_t result) {
128 ALOGD("%s result: %d", __func__, result);
129}
130
131void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op,
132 uint8_t av_handle) {
133 ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle);
134}
135
136const bt_vendor_callbacks_t lib_callbacks = {
137 sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
138 low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
139 buffer_free_cb, transmit_cb, epilog_cb,
140 a2dp_offload_cb};
141
142} // namespace
143
144namespace android {
145namespace hardware {
146namespace bluetooth {
147namespace V1_0 {
148namespace implementation {
149
Andre Eisenbach9041d972017-01-17 18:23:12 -0800150class FirmwareStartupTimer {
151 public:
152 FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {}
153
154 ~FirmwareStartupTimer() {
155 std::chrono::duration<double> duration =
156 std::chrono::steady_clock::now() - start_time_;
157 double s = duration.count();
158 if (s == 0) return;
Myles Watson8ffcbc72017-01-20 10:09:38 -0800159 ALOGI("Firmware configured in %.3fs", s);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800160 }
161
162 private:
163 std::chrono::steady_clock::time_point start_time_;
164};
165
166bool VendorInterface::Initialize(
167 InitializeCompleteCallback initialize_complete_cb,
Zach Johnson917efb12017-02-26 23:46:05 -0800168 PacketReadCallback event_cb, PacketReadCallback acl_cb,
Jakub Pawlowski13b4d312019-11-05 12:27:29 +0100169 PacketReadCallback sco_cb, PacketReadCallback iso_cb) {
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000170 if (g_vendor_interface) {
171 ALOGE("%s: No previous Shutdown()?", __func__);
172 return false;
173 }
174 g_vendor_interface = new VendorInterface();
Zach Johnson917efb12017-02-26 23:46:05 -0800175 return g_vendor_interface->Open(initialize_complete_cb, event_cb, acl_cb,
Jakub Pawlowski13b4d312019-11-05 12:27:29 +0100176 sco_cb, iso_cb);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700177}
178
179void VendorInterface::Shutdown() {
Myles Watson3e272a72017-06-26 13:09:11 -0700180 LOG_ALWAYS_FATAL_IF(!g_vendor_interface, "%s: No Vendor interface!",
181 __func__);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700182 g_vendor_interface->Close();
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000183 delete g_vendor_interface;
184 g_vendor_interface = nullptr;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700185}
186
187VendorInterface* VendorInterface::get() { return g_vendor_interface; }
188
Andre Eisenbach9041d972017-01-17 18:23:12 -0800189bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
Zach Johnson917efb12017-02-26 23:46:05 -0800190 PacketReadCallback event_cb,
191 PacketReadCallback acl_cb,
Jakub Pawlowski13b4d312019-11-05 12:27:29 +0100192 PacketReadCallback sco_cb,
193 PacketReadCallback iso_cb) {
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000194 initialize_complete_cb_ = initialize_complete_cb;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700195
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000196 // Initialize vendor interface
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700197
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000198 lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
199 if (!lib_handle_) {
200 ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
201 dlerror());
202 return false;
203 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700204
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000205 lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
206 dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
207 if (!lib_interface_) {
208 ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
209 VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
210 return false;
211 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700212
Myles Watson6a7d6222016-10-13 15:45:02 -0700213 // Get the local BD address
214
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000215 uint8_t local_bda[BluetoothAddress::kBytes];
216 if (!BluetoothAddress::get_local_address(local_bda)) {
217 LOG_ALWAYS_FATAL("%s: No Bluetooth Address!", __func__);
218 }
219 int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
220 if (status) {
221 ALOGE("%s unable to initialize vendor library: %d", __func__, status);
222 return false;
223 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700224
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000225 ALOGD("%s vendor library loaded", __func__);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700226
Myles Watsonc0aee0c2017-03-13 12:35:25 -0700227 // Power on the controller
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700228
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000229 int power_state = BT_VND_PWR_ON;
230 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700231
Zach Johnson917efb12017-02-26 23:46:05 -0800232 // Get the UART socket(s)
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700233
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000234 int fd_list[CH_MAX] = {0};
235 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700236
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000237 if (fd_count < 1 || fd_count > CH_MAX - 1) {
238 ALOGE("%s: fd_count %d is invalid!", __func__, fd_count);
239 return false;
240 }
241
242 for (int i = 0; i < fd_count; i++) {
243 if (fd_list[i] == INVALID_FD) {
244 ALOGE("%s: fd %d is invalid!", __func__, fd_list[i]);
Zach Johnson917efb12017-02-26 23:46:05 -0800245 return false;
246 }
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000247 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700248
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000249 event_cb_ = event_cb;
250 PacketReadCallback intercept_events = [this](const hidl_vec<uint8_t>& event) {
251 HandleIncomingEvent(event);
252 };
Zach Johnson917efb12017-02-26 23:46:05 -0800253
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000254 if (fd_count == 1) {
255 hci::H4Protocol* h4_hci =
256 new hci::H4Protocol(fd_list[0], intercept_events, acl_cb, sco_cb, iso_cb);
257 fd_watcher_.WatchFdForNonBlockingReads(
258 fd_list[0], [h4_hci](int fd) { h4_hci->OnDataReady(fd); });
259 hci_ = h4_hci;
260 } else {
261 hci::MctProtocol* mct_hci =
262 new hci::MctProtocol(fd_list, intercept_events, acl_cb);
263 fd_watcher_.WatchFdForNonBlockingReads(
264 fd_list[CH_EVT], [mct_hci](int fd) { mct_hci->OnEventDataReady(fd); });
265 fd_watcher_.WatchFdForNonBlockingReads(
266 fd_list[CH_ACL_IN], [mct_hci](int fd) { mct_hci->OnAclDataReady(fd); });
267 hci_ = mct_hci;
268 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700269
Myles Watsonbeb13b42017-01-26 10:47:27 -0800270 // Initially, the power management is off.
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000271 lpm_wake_deasserted = true;
Myles Watsonbeb13b42017-01-26 10:47:27 -0800272
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700273 // Start configuring the firmware
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000274 firmware_startup_timer_ = new FirmwareStartupTimer();
275 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700276
277 return true;
278}
279
280void VendorInterface::Close() {
Andre Eisenbach8a9efb62017-03-17 20:28:09 +0000281 // These callbacks may send HCI events (vendor-dependent), so make sure to
282 // StopWatching the file descriptor after this.
283 if (lib_interface_ != nullptr) {
284 bt_vendor_lpm_mode_t mode = BT_VND_LPM_DISABLE;
285 lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
286 }
287
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000288 fd_watcher_.StopWatchingFileDescriptors();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700289
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000290 if (hci_ != nullptr) {
291 delete hci_;
292 hci_ = nullptr;
293 }
Zach Johnson917efb12017-02-26 23:46:05 -0800294
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000295 if (lib_interface_ != nullptr) {
296 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
Myles Watson66a4ca32017-03-10 09:10:51 -0800297
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000298 int power_state = BT_VND_PWR_OFF;
299 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
Myles Watson9eee8302017-06-08 08:38:58 -0700300
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000301 lib_interface_->cleanup();
302 lib_interface_ = nullptr;
303 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700304
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000305 if (lib_handle_ != nullptr) {
306 dlclose(lib_handle_);
307 lib_handle_ = nullptr;
308 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700309
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000310 if (firmware_startup_timer_ != nullptr) {
311 delete firmware_startup_timer_;
312 firmware_startup_timer_ = nullptr;
313 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700314}
315
Myles Watsondf765ea2017-01-30 09:07:37 -0800316size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) {
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000317 std::unique_lock<std::mutex> lock(wakeup_mutex_);
318 recent_activity_flag = true;
Myles Watsonbeb13b42017-01-26 10:47:27 -0800319
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000320 if (lpm_wake_deasserted == true) {
321 // Restart the timer.
322 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
Myles Watsonbeb13b42017-01-26 10:47:27 -0800323 [this]() { OnTimeout(); });
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000324 // Assert wake.
325 lpm_wake_deasserted = false;
326 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_ASSERT;
327 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
328 ALOGV("%s: Sent wake before (%02x)", __func__, data[0] | (data[1] << 8));
329 }
Myles Watsonbeb13b42017-01-26 10:47:27 -0800330
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000331 return hci_->Send(type, data, length);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700332}
333
334void VendorInterface::OnFirmwareConfigured(uint8_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800335 ALOGD("%s result: %d", __func__, result);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800336
337 if (firmware_startup_timer_ != nullptr) {
338 delete firmware_startup_timer_;
339 firmware_startup_timer_ = nullptr;
340 }
341
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000342 if (initialize_complete_cb_ != nullptr) {
343 initialize_complete_cb_(result == 0);
344 initialize_complete_cb_ = nullptr;
Andre Eisenbach9041d972017-01-17 18:23:12 -0800345 }
Myles Watsonbeb13b42017-01-26 10:47:27 -0800346
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000347 lib_interface_->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &lpm_timeout_ms);
348 ALOGI("%s: lpm_timeout_ms %d", __func__, lpm_timeout_ms);
349
350 bt_vendor_lpm_mode_t mode = BT_VND_LPM_ENABLE;
351 lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
352
353 ALOGD("%s Calling StartLowPowerWatchdog()", __func__);
354 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
355 [this]() { OnTimeout(); });
Myles Watsonbeb13b42017-01-26 10:47:27 -0800356}
357
358void VendorInterface::OnTimeout() {
359 ALOGV("%s", __func__);
Guillaume Baileya2b4a182023-07-03 12:01:08 +0000360 std::unique_lock<std::mutex> lock(wakeup_mutex_);
Myles Watsonbeb13b42017-01-26 10:47:27 -0800361 if (recent_activity_flag == false) {
362 lpm_wake_deasserted = true;
363 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_DEASSERT;
364 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
365 fd_watcher_.ConfigureTimeout(std::chrono::seconds(0), []() {
366 ALOGE("Zero timeout! Should never happen.");
367 });
368 }
369 recent_activity_flag = false;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700370}
371
Zach Johnson917efb12017-02-26 23:46:05 -0800372void VendorInterface::HandleIncomingEvent(const hidl_vec<uint8_t>& hci_packet) {
373 if (internal_command.cb != nullptr &&
374 internal_command_event_match(hci_packet)) {
375 HC_BT_HDR* bt_hdr = WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700376
Zach Johnson917efb12017-02-26 23:46:05 -0800377 // The callbacks can send new commands, so don't zero after calling.
378 tINT_CMD_CBACK saved_cb = internal_command.cb;
379 internal_command.cb = nullptr;
380 saved_cb(bt_hdr);
381 } else {
382 event_cb_(hci_packet);
383 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700384}
385
386} // namespace implementation
387} // namespace V1_0
388} // namespace bluetooth
389} // namespace hardware
390} // namespace android