blob: f2289828c67d826cc51d4a271a6aaa13765127b4 [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
Dan Albert3d0263f2017-01-26 15:33:15 -080019#include <assert.h>
20
Andre Eisenbach89ba5282016-10-13 15:45:02 -070021#define LOG_TAG "android.hardware.bluetooth@1.0-impl"
22#include <android-base/logging.h>
Myles Watson6a7d6222016-10-13 15:45:02 -070023#include <cutils/properties.h>
Andre Eisenbach89ba5282016-10-13 15:45:02 -070024#include <utils/Log.h>
25
26#include <dlfcn.h>
Myles Watson6a7d6222016-10-13 15:45:02 -070027#include <fcntl.h>
28
29#include "bluetooth_address.h"
Andre Eisenbach89ba5282016-10-13 15:45:02 -070030
31static const char* VENDOR_LIBRARY_NAME = "libbt-vendor.so";
32static const char* VENDOR_LIBRARY_SYMBOL_NAME =
33 "BLUETOOTH_VENDOR_LIB_INTERFACE";
34
35static const int INVALID_FD = -1;
36
37namespace {
38
39using android::hardware::bluetooth::V1_0::implementation::VendorInterface;
Andre Eisenbach9041d972017-01-17 18:23:12 -080040using android::hardware::hidl_vec;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070041
Myles Watson4e2e8ec2017-02-01 10:46:16 -080042struct {
43 tINT_CMD_CBACK cb;
44 uint16_t opcode;
45} internal_command;
Myles Watsona7d33b32017-01-24 09:09:58 -080046
Myles Watsonbeb13b42017-01-26 10:47:27 -080047// True when LPM is not enabled yet or wake is not asserted.
48bool lpm_wake_deasserted;
49uint32_t lpm_timeout_ms;
50bool recent_activity_flag;
51
Andre Eisenbach89ba5282016-10-13 15:45:02 -070052VendorInterface* g_vendor_interface = nullptr;
53
54const size_t preamble_size_for_type[] = {
55 0, HCI_COMMAND_PREAMBLE_SIZE, HCI_ACL_PREAMBLE_SIZE, HCI_SCO_PREAMBLE_SIZE,
56 HCI_EVENT_PREAMBLE_SIZE};
57const size_t packet_length_offset_for_type[] = {
58 0, HCI_LENGTH_OFFSET_CMD, HCI_LENGTH_OFFSET_ACL, HCI_LENGTH_OFFSET_SCO,
59 HCI_LENGTH_OFFSET_EVT};
60
Myles Watson71390182017-01-24 13:34:59 -080061size_t HciGetPacketLengthForType(HciPacketType type, const uint8_t* preamble) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070062 size_t offset = packet_length_offset_for_type[type];
Myles Watson71390182017-01-24 13:34:59 -080063 if (type != HCI_PACKET_TYPE_ACL_DATA) return preamble[offset];
64 return (((preamble[offset + 1]) << 8) | preamble[offset]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070065}
66
Andre Eisenbach9041d972017-01-17 18:23:12 -080067HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070068 size_t packet_size = data.size() + sizeof(HC_BT_HDR);
69 HC_BT_HDR* packet = reinterpret_cast<HC_BT_HDR*>(new uint8_t[packet_size]);
70 packet->offset = 0;
71 packet->len = data.size();
72 packet->layer_specific = 0;
73 packet->event = event;
Myles Watson6a7d6222016-10-13 15:45:02 -070074 // TODO(eisenbach): Avoid copy here; if BT_HDR->data can be ensured to
Andre Eisenbach89ba5282016-10-13 15:45:02 -070075 // be the only way the data is accessed, a pointer could be passed here...
76 memcpy(packet->data, data.data(), data.size());
77 return packet;
78}
79
Myles Watsondf765ea2017-01-30 09:07:37 -080080size_t write_safely(int fd, const uint8_t* data, size_t length) {
81 size_t transmitted_length = 0;
82 while (length > 0) {
83 ssize_t ret =
84 TEMP_FAILURE_RETRY(write(fd, data + transmitted_length, length));
85
86 if (ret == -1) {
87 if (errno == EAGAIN) continue;
88 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
89 break;
90
91 } else if (ret == 0) {
92 // Nothing written :(
93 ALOGE("%s zero bytes written - something went wrong...", __func__);
94 break;
95 }
96
97 transmitted_length += ret;
98 length -= ret;
99 }
100
101 return transmitted_length;
102}
103
Myles Watsona7d33b32017-01-24 09:09:58 -0800104bool internal_command_event_match(const hidl_vec<uint8_t>& packet) {
105 uint8_t event_code = packet[0];
106 if (event_code != HCI_COMMAND_COMPLETE_EVENT) {
107 ALOGE("%s: Unhandled event type %02X", __func__, event_code);
108 return false;
109 }
110
111 size_t opcode_offset = HCI_EVENT_PREAMBLE_SIZE + 1; // Skip num packets.
112
113 uint16_t opcode = packet[opcode_offset] | (packet[opcode_offset + 1] << 8);
114
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800115 ALOGV("%s internal_command.opcode = %04X opcode = %04x", __func__,
116 internal_command.opcode, opcode);
117 return opcode == internal_command.opcode;
Myles Watsona7d33b32017-01-24 09:09:58 -0800118}
119
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700120uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
Myles Watsona7d33b32017-01-24 09:09:58 -0800121 ALOGV("%s opcode: 0x%04x, ptr: %p, cb: %p", __func__, opcode, buffer,
122 callback);
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800123 internal_command.cb = callback;
124 internal_command.opcode = opcode;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700125 uint8_t type = HCI_PACKET_TYPE_COMMAND;
Andre Eisenbach9041d972017-01-17 18:23:12 -0800126 HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
Myles Watsondf765ea2017-01-30 09:07:37 -0800127 VendorInterface::get()->Send(type, bt_hdr->data, bt_hdr->len);
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800128 delete[] reinterpret_cast<uint8_t*>(buffer);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700129 return true;
130}
131
132void firmware_config_cb(bt_vendor_op_result_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800133 ALOGV("%s result: %d", __func__, result);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700134 VendorInterface::get()->OnFirmwareConfigured(result);
135}
136
137void sco_config_cb(bt_vendor_op_result_t result) {
138 ALOGD("%s result: %d", __func__, result);
139}
140
141void low_power_mode_cb(bt_vendor_op_result_t result) {
142 ALOGD("%s result: %d", __func__, result);
143}
144
145void sco_audiostate_cb(bt_vendor_op_result_t result) {
146 ALOGD("%s result: %d", __func__, result);
147}
148
149void* buffer_alloc_cb(int size) {
150 void* p = new uint8_t[size];
151 ALOGV("%s pts: %p, size: %d", __func__, p, size);
152 return p;
153}
154
155void buffer_free_cb(void* buffer) {
156 ALOGV("%s ptr: %p", __func__, buffer);
157 delete[] reinterpret_cast<uint8_t*>(buffer);
158}
159
160void epilog_cb(bt_vendor_op_result_t result) {
161 ALOGD("%s result: %d", __func__, result);
162}
163
164void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op,
165 uint8_t av_handle) {
166 ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle);
167}
168
169const bt_vendor_callbacks_t lib_callbacks = {
170 sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
171 low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
172 buffer_free_cb, transmit_cb, epilog_cb,
173 a2dp_offload_cb};
174
175} // namespace
176
177namespace android {
178namespace hardware {
179namespace bluetooth {
180namespace V1_0 {
181namespace implementation {
182
Andre Eisenbach9041d972017-01-17 18:23:12 -0800183class FirmwareStartupTimer {
184 public:
185 FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {}
186
187 ~FirmwareStartupTimer() {
188 std::chrono::duration<double> duration =
189 std::chrono::steady_clock::now() - start_time_;
190 double s = duration.count();
191 if (s == 0) return;
Myles Watson8ffcbc72017-01-20 10:09:38 -0800192 ALOGI("Firmware configured in %.3fs", s);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800193 }
194
195 private:
196 std::chrono::steady_clock::time_point start_time_;
197};
198
199bool VendorInterface::Initialize(
200 InitializeCompleteCallback initialize_complete_cb,
201 PacketReadCallback packet_read_cb) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700202 assert(!g_vendor_interface);
203 g_vendor_interface = new VendorInterface();
Andre Eisenbach9041d972017-01-17 18:23:12 -0800204 return g_vendor_interface->Open(initialize_complete_cb, packet_read_cb);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700205}
206
207void VendorInterface::Shutdown() {
208 CHECK(g_vendor_interface);
209 g_vendor_interface->Close();
210 delete g_vendor_interface;
211 g_vendor_interface = nullptr;
212}
213
214VendorInterface* VendorInterface::get() { return g_vendor_interface; }
215
Andre Eisenbach9041d972017-01-17 18:23:12 -0800216bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
217 PacketReadCallback packet_read_cb) {
218 initialize_complete_cb_ = initialize_complete_cb;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700219 packet_read_cb_ = packet_read_cb;
220
221 // Initialize vendor interface
222
223 lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
224 if (!lib_handle_) {
225 ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
226 dlerror());
227 return false;
228 }
229
230 lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
231 dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
232 if (!lib_interface_) {
233 ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
234 VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
235 return false;
236 }
237
Myles Watson6a7d6222016-10-13 15:45:02 -0700238 // Get the local BD address
239
240 uint8_t local_bda[BluetoothAddress::kBytes];
241 CHECK(BluetoothAddress::get_local_address(local_bda));
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700242 int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
243 if (status) {
244 ALOGE("%s unable to initialize vendor library: %d", __func__, status);
245 return false;
246 }
247
248 ALOGD("%s vendor library loaded", __func__);
249
250 // Power cycle chip
251
252 int power_state = BT_VND_PWR_OFF;
253 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
254 power_state = BT_VND_PWR_ON;
255 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
256
257 // Get the UART socket
258
259 int fd_list[CH_MAX] = {0};
260 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
261
262 if (fd_count != 1) {
263 ALOGE("%s fd count %d != 1; we can't handle this currently...", __func__,
264 fd_count);
265 return false;
266 }
267
268 uart_fd_ = fd_list[0];
269 if (uart_fd_ == INVALID_FD) {
270 ALOGE("%s unable to determine UART fd", __func__);
271 return false;
272 }
273
Myles Watson8ffcbc72017-01-20 10:09:38 -0800274 ALOGI("%s UART fd: %d", __func__, uart_fd_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700275
276 fd_watcher_.WatchFdForNonBlockingReads(uart_fd_,
277 [this](int fd) { OnDataReady(fd); });
278
Myles Watsonbeb13b42017-01-26 10:47:27 -0800279 // Initially, the power management is off.
280 lpm_wake_deasserted = false;
281
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700282 // Start configuring the firmware
Andre Eisenbach9041d972017-01-17 18:23:12 -0800283 firmware_startup_timer_ = new FirmwareStartupTimer();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700284 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
285
286 return true;
287}
288
289void VendorInterface::Close() {
290 fd_watcher_.StopWatchingFileDescriptor();
291
292 if (lib_interface_ != nullptr) {
293 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
294 uart_fd_ = INVALID_FD;
295 int power_state = BT_VND_PWR_OFF;
296 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
297 }
298
299 if (lib_handle_ != nullptr) {
300 dlclose(lib_handle_);
301 lib_handle_ = nullptr;
302 }
303
Andre Eisenbach9041d972017-01-17 18:23:12 -0800304 if (firmware_startup_timer_ != nullptr) {
305 delete firmware_startup_timer_;
306 firmware_startup_timer_ = nullptr;
307 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700308}
309
Myles Watsondf765ea2017-01-30 09:07:37 -0800310size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700311 if (uart_fd_ == INVALID_FD) return 0;
312
Myles Watsonbeb13b42017-01-26 10:47:27 -0800313 recent_activity_flag = true;
314
315 if (lpm_wake_deasserted == true) {
316 // Restart the timer.
317 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
318 [this]() { OnTimeout(); });
319 // Assert wake.
320 lpm_wake_deasserted = false;
321 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_ASSERT;
322 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
323 ALOGV("%s: Sent wake before (%02x)", __func__, data[0] | (data[1] << 8));
324 }
325
Myles Watsondf765ea2017-01-30 09:07:37 -0800326 int rv = write_safely(uart_fd_, &type, sizeof(type));
327 if (rv == sizeof(type))
328 rv = write_safely(uart_fd_, data, length);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700329
Myles Watsondf765ea2017-01-30 09:07:37 -0800330 return rv;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700331}
332
333void VendorInterface::OnFirmwareConfigured(uint8_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800334 ALOGD("%s result: %d", __func__, result);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800335
336 if (firmware_startup_timer_ != nullptr) {
337 delete firmware_startup_timer_;
338 firmware_startup_timer_ = nullptr;
339 }
340
341 if (initialize_complete_cb_ != nullptr) {
342 initialize_complete_cb_(result == 0);
343 initialize_complete_cb_ = nullptr;
344 }
Myles Watsonbeb13b42017-01-26 10:47:27 -0800345
346 lib_interface_->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &lpm_timeout_ms);
347 ALOGI("%s: lpm_timeout_ms %d", __func__, lpm_timeout_ms);
348
349 bt_vendor_lpm_mode_t mode = BT_VND_LPM_ENABLE;
350 lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
351
352 ALOGD("%s Calling StartLowPowerWatchdog()", __func__);
353 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
354 [this]() { OnTimeout(); });
355
356 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_ASSERT;
357 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
358}
359
360void VendorInterface::OnTimeout() {
361 ALOGV("%s", __func__);
362 if (recent_activity_flag == false) {
363 lpm_wake_deasserted = true;
364 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_DEASSERT;
365 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
366 fd_watcher_.ConfigureTimeout(std::chrono::seconds(0), []() {
367 ALOGE("Zero timeout! Should never happen.");
368 });
369 }
370 recent_activity_flag = false;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700371}
372
373void VendorInterface::OnDataReady(int fd) {
374 switch (hci_parser_state_) {
375 case HCI_IDLE: {
376 uint8_t buffer[1] = {0};
377 size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
378 CHECK(bytes_read == 1);
379 hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
380 // TODO(eisenbach): Check for workaround(s)
381 CHECK(hci_packet_type_ >= HCI_PACKET_TYPE_ACL_DATA &&
382 hci_packet_type_ <= HCI_PACKET_TYPE_EVENT)
Myles Watson8ffcbc72017-01-20 10:09:38 -0800383 << "buffer[0] = " << static_cast<unsigned int>(buffer[0]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700384 hci_parser_state_ = HCI_TYPE_READY;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700385 hci_packet_bytes_remaining_ = preamble_size_for_type[hci_packet_type_];
386 hci_packet_bytes_read_ = 0;
387 break;
388 }
389
390 case HCI_TYPE_READY: {
391 size_t bytes_read = TEMP_FAILURE_RETRY(
Myles Watson71390182017-01-24 13:34:59 -0800392 read(fd, hci_packet_preamble_ + hci_packet_bytes_read_,
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700393 hci_packet_bytes_remaining_));
394 CHECK(bytes_read > 0);
395 hci_packet_bytes_remaining_ -= bytes_read;
396 hci_packet_bytes_read_ += bytes_read;
397 if (hci_packet_bytes_remaining_ == 0) {
398 size_t packet_length =
Myles Watson71390182017-01-24 13:34:59 -0800399 HciGetPacketLengthForType(hci_packet_type_, hci_packet_preamble_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700400 hci_packet_.resize(preamble_size_for_type[hci_packet_type_] +
401 packet_length);
Myles Watson71390182017-01-24 13:34:59 -0800402 memcpy(hci_packet_.data(), hci_packet_preamble_,
403 preamble_size_for_type[hci_packet_type_]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700404 hci_packet_bytes_remaining_ = packet_length;
405 hci_parser_state_ = HCI_PAYLOAD;
406 hci_packet_bytes_read_ = 0;
407 }
408 break;
409 }
410
411 case HCI_PAYLOAD: {
412 size_t bytes_read = TEMP_FAILURE_RETRY(
413 read(fd,
414 hci_packet_.data() + preamble_size_for_type[hci_packet_type_] +
415 hci_packet_bytes_read_,
416 hci_packet_bytes_remaining_));
417 hci_packet_bytes_remaining_ -= bytes_read;
418 hci_packet_bytes_read_ += bytes_read;
419 if (hci_packet_bytes_remaining_ == 0) {
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800420 if (internal_command.cb != nullptr &&
Myles Watsona7d33b32017-01-24 09:09:58 -0800421 hci_packet_type_ == HCI_PACKET_TYPE_EVENT &&
422 internal_command_event_match(hci_packet_)) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800423 HC_BT_HDR* bt_hdr =
424 WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet_);
Myles Watsona7d33b32017-01-24 09:09:58 -0800425
426 // The callbacks can send new commands, so don't zero after calling.
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800427 tINT_CMD_CBACK saved_cb = internal_command.cb;
428 internal_command.cb = nullptr;
Myles Watsona7d33b32017-01-24 09:09:58 -0800429 saved_cb(bt_hdr);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700430 } else {
Myles Watsona7d33b32017-01-24 09:09:58 -0800431 packet_read_cb_(hci_packet_type_, hci_packet_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700432 }
433 hci_parser_state_ = HCI_IDLE;
434 }
435 break;
436 }
437 }
438}
439
440} // namespace implementation
441} // namespace V1_0
442} // namespace bluetooth
443} // namespace hardware
444} // namespace android