blob: 51a0addae2361a5835c69f4a7462f9aab5d222e3 [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
42tINT_CMD_CBACK internal_command_cb;
Myles Watsona7d33b32017-01-24 09:09:58 -080043uint16_t internal_command_opcode;
44
Andre Eisenbach89ba5282016-10-13 15:45:02 -070045VendorInterface* g_vendor_interface = nullptr;
46
47const size_t preamble_size_for_type[] = {
48 0, HCI_COMMAND_PREAMBLE_SIZE, HCI_ACL_PREAMBLE_SIZE, HCI_SCO_PREAMBLE_SIZE,
49 HCI_EVENT_PREAMBLE_SIZE};
50const size_t packet_length_offset_for_type[] = {
51 0, HCI_LENGTH_OFFSET_CMD, HCI_LENGTH_OFFSET_ACL, HCI_LENGTH_OFFSET_SCO,
52 HCI_LENGTH_OFFSET_EVT};
53
Myles Watson71390182017-01-24 13:34:59 -080054size_t HciGetPacketLengthForType(HciPacketType type, const uint8_t* preamble) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070055 size_t offset = packet_length_offset_for_type[type];
Myles Watson71390182017-01-24 13:34:59 -080056 if (type != HCI_PACKET_TYPE_ACL_DATA) return preamble[offset];
57 return (((preamble[offset + 1]) << 8) | preamble[offset]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070058}
59
Andre Eisenbach9041d972017-01-17 18:23:12 -080060HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070061 size_t packet_size = data.size() + sizeof(HC_BT_HDR);
62 HC_BT_HDR* packet = reinterpret_cast<HC_BT_HDR*>(new uint8_t[packet_size]);
63 packet->offset = 0;
64 packet->len = data.size();
65 packet->layer_specific = 0;
66 packet->event = event;
Myles Watson6a7d6222016-10-13 15:45:02 -070067 // TODO(eisenbach): Avoid copy here; if BT_HDR->data can be ensured to
Andre Eisenbach89ba5282016-10-13 15:45:02 -070068 // be the only way the data is accessed, a pointer could be passed here...
69 memcpy(packet->data, data.data(), data.size());
70 return packet;
71}
72
Myles Watsondf765ea2017-01-30 09:07:37 -080073size_t write_safely(int fd, const uint8_t* data, size_t length) {
74 size_t transmitted_length = 0;
75 while (length > 0) {
76 ssize_t ret =
77 TEMP_FAILURE_RETRY(write(fd, data + transmitted_length, length));
78
79 if (ret == -1) {
80 if (errno == EAGAIN) continue;
81 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
82 break;
83
84 } else if (ret == 0) {
85 // Nothing written :(
86 ALOGE("%s zero bytes written - something went wrong...", __func__);
87 break;
88 }
89
90 transmitted_length += ret;
91 length -= ret;
92 }
93
94 return transmitted_length;
95}
96
Myles Watsona7d33b32017-01-24 09:09:58 -080097bool internal_command_event_match(const hidl_vec<uint8_t>& packet) {
98 uint8_t event_code = packet[0];
99 if (event_code != HCI_COMMAND_COMPLETE_EVENT) {
100 ALOGE("%s: Unhandled event type %02X", __func__, event_code);
101 return false;
102 }
103
104 size_t opcode_offset = HCI_EVENT_PREAMBLE_SIZE + 1; // Skip num packets.
105
106 uint16_t opcode = packet[opcode_offset] | (packet[opcode_offset + 1] << 8);
107
108 ALOGV("%s internal_command_opcode = %04X opcode = %04x", __func__,
109 internal_command_opcode, opcode);
110 return opcode == internal_command_opcode;
111}
112
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700113uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
Myles Watsona7d33b32017-01-24 09:09:58 -0800114 ALOGV("%s opcode: 0x%04x, ptr: %p, cb: %p", __func__, opcode, buffer,
115 callback);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700116 internal_command_cb = callback;
Myles Watsona7d33b32017-01-24 09:09:58 -0800117 internal_command_opcode = opcode;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700118 uint8_t type = HCI_PACKET_TYPE_COMMAND;
Andre Eisenbach9041d972017-01-17 18:23:12 -0800119 HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
Myles Watsondf765ea2017-01-30 09:07:37 -0800120 VendorInterface::get()->Send(type, bt_hdr->data, bt_hdr->len);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700121 return true;
122}
123
124void firmware_config_cb(bt_vendor_op_result_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800125 ALOGV("%s result: %d", __func__, result);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700126 VendorInterface::get()->OnFirmwareConfigured(result);
127}
128
129void sco_config_cb(bt_vendor_op_result_t result) {
130 ALOGD("%s result: %d", __func__, result);
131}
132
133void low_power_mode_cb(bt_vendor_op_result_t result) {
134 ALOGD("%s result: %d", __func__, result);
135}
136
137void sco_audiostate_cb(bt_vendor_op_result_t result) {
138 ALOGD("%s result: %d", __func__, result);
139}
140
141void* buffer_alloc_cb(int size) {
142 void* p = new uint8_t[size];
143 ALOGV("%s pts: %p, size: %d", __func__, p, size);
144 return p;
145}
146
147void buffer_free_cb(void* buffer) {
148 ALOGV("%s ptr: %p", __func__, buffer);
149 delete[] reinterpret_cast<uint8_t*>(buffer);
150}
151
152void epilog_cb(bt_vendor_op_result_t result) {
153 ALOGD("%s result: %d", __func__, result);
154}
155
156void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op,
157 uint8_t av_handle) {
158 ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle);
159}
160
161const bt_vendor_callbacks_t lib_callbacks = {
162 sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
163 low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
164 buffer_free_cb, transmit_cb, epilog_cb,
165 a2dp_offload_cb};
166
167} // namespace
168
169namespace android {
170namespace hardware {
171namespace bluetooth {
172namespace V1_0 {
173namespace implementation {
174
Andre Eisenbach9041d972017-01-17 18:23:12 -0800175class FirmwareStartupTimer {
176 public:
177 FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {}
178
179 ~FirmwareStartupTimer() {
180 std::chrono::duration<double> duration =
181 std::chrono::steady_clock::now() - start_time_;
182 double s = duration.count();
183 if (s == 0) return;
Myles Watson8ffcbc72017-01-20 10:09:38 -0800184 ALOGI("Firmware configured in %.3fs", s);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800185 }
186
187 private:
188 std::chrono::steady_clock::time_point start_time_;
189};
190
191bool VendorInterface::Initialize(
192 InitializeCompleteCallback initialize_complete_cb,
193 PacketReadCallback packet_read_cb) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700194 assert(!g_vendor_interface);
195 g_vendor_interface = new VendorInterface();
Andre Eisenbach9041d972017-01-17 18:23:12 -0800196 return g_vendor_interface->Open(initialize_complete_cb, packet_read_cb);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700197}
198
199void VendorInterface::Shutdown() {
200 CHECK(g_vendor_interface);
201 g_vendor_interface->Close();
202 delete g_vendor_interface;
203 g_vendor_interface = nullptr;
204}
205
206VendorInterface* VendorInterface::get() { return g_vendor_interface; }
207
Andre Eisenbach9041d972017-01-17 18:23:12 -0800208bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
209 PacketReadCallback packet_read_cb) {
210 initialize_complete_cb_ = initialize_complete_cb;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700211 packet_read_cb_ = packet_read_cb;
212
213 // Initialize vendor interface
214
215 lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
216 if (!lib_handle_) {
217 ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
218 dlerror());
219 return false;
220 }
221
222 lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
223 dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
224 if (!lib_interface_) {
225 ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
226 VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
227 return false;
228 }
229
Myles Watson6a7d6222016-10-13 15:45:02 -0700230 // Get the local BD address
231
232 uint8_t local_bda[BluetoothAddress::kBytes];
233 CHECK(BluetoothAddress::get_local_address(local_bda));
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700234 int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
235 if (status) {
236 ALOGE("%s unable to initialize vendor library: %d", __func__, status);
237 return false;
238 }
239
240 ALOGD("%s vendor library loaded", __func__);
241
242 // Power cycle chip
243
244 int power_state = BT_VND_PWR_OFF;
245 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
246 power_state = BT_VND_PWR_ON;
247 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
248
249 // Get the UART socket
250
251 int fd_list[CH_MAX] = {0};
252 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
253
254 if (fd_count != 1) {
255 ALOGE("%s fd count %d != 1; we can't handle this currently...", __func__,
256 fd_count);
257 return false;
258 }
259
260 uart_fd_ = fd_list[0];
261 if (uart_fd_ == INVALID_FD) {
262 ALOGE("%s unable to determine UART fd", __func__);
263 return false;
264 }
265
Myles Watson8ffcbc72017-01-20 10:09:38 -0800266 ALOGI("%s UART fd: %d", __func__, uart_fd_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700267
268 fd_watcher_.WatchFdForNonBlockingReads(uart_fd_,
269 [this](int fd) { OnDataReady(fd); });
270
271 // Start configuring the firmware
Andre Eisenbach9041d972017-01-17 18:23:12 -0800272 firmware_startup_timer_ = new FirmwareStartupTimer();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700273 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
274
275 return true;
276}
277
278void VendorInterface::Close() {
279 fd_watcher_.StopWatchingFileDescriptor();
280
281 if (lib_interface_ != nullptr) {
282 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
283 uart_fd_ = INVALID_FD;
284 int power_state = BT_VND_PWR_OFF;
285 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
286 }
287
288 if (lib_handle_ != nullptr) {
289 dlclose(lib_handle_);
290 lib_handle_ = nullptr;
291 }
292
Andre Eisenbach9041d972017-01-17 18:23:12 -0800293 if (firmware_startup_timer_ != nullptr) {
294 delete firmware_startup_timer_;
295 firmware_startup_timer_ = nullptr;
296 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700297}
298
Myles Watsondf765ea2017-01-30 09:07:37 -0800299size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700300 if (uart_fd_ == INVALID_FD) return 0;
301
Myles Watsondf765ea2017-01-30 09:07:37 -0800302 int rv = write_safely(uart_fd_, &type, sizeof(type));
303 if (rv == sizeof(type))
304 rv = write_safely(uart_fd_, data, length);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700305
Myles Watsondf765ea2017-01-30 09:07:37 -0800306 return rv;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700307}
308
309void VendorInterface::OnFirmwareConfigured(uint8_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800310 ALOGD("%s result: %d", __func__, result);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800311
312 if (firmware_startup_timer_ != nullptr) {
313 delete firmware_startup_timer_;
314 firmware_startup_timer_ = nullptr;
315 }
316
317 if (initialize_complete_cb_ != nullptr) {
318 initialize_complete_cb_(result == 0);
319 initialize_complete_cb_ = nullptr;
320 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700321}
322
323void VendorInterface::OnDataReady(int fd) {
324 switch (hci_parser_state_) {
325 case HCI_IDLE: {
326 uint8_t buffer[1] = {0};
327 size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
328 CHECK(bytes_read == 1);
329 hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
330 // TODO(eisenbach): Check for workaround(s)
331 CHECK(hci_packet_type_ >= HCI_PACKET_TYPE_ACL_DATA &&
332 hci_packet_type_ <= HCI_PACKET_TYPE_EVENT)
Myles Watson8ffcbc72017-01-20 10:09:38 -0800333 << "buffer[0] = " << static_cast<unsigned int>(buffer[0]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700334 hci_parser_state_ = HCI_TYPE_READY;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700335 hci_packet_bytes_remaining_ = preamble_size_for_type[hci_packet_type_];
336 hci_packet_bytes_read_ = 0;
337 break;
338 }
339
340 case HCI_TYPE_READY: {
341 size_t bytes_read = TEMP_FAILURE_RETRY(
Myles Watson71390182017-01-24 13:34:59 -0800342 read(fd, hci_packet_preamble_ + hci_packet_bytes_read_,
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700343 hci_packet_bytes_remaining_));
344 CHECK(bytes_read > 0);
345 hci_packet_bytes_remaining_ -= bytes_read;
346 hci_packet_bytes_read_ += bytes_read;
347 if (hci_packet_bytes_remaining_ == 0) {
348 size_t packet_length =
Myles Watson71390182017-01-24 13:34:59 -0800349 HciGetPacketLengthForType(hci_packet_type_, hci_packet_preamble_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700350 hci_packet_.resize(preamble_size_for_type[hci_packet_type_] +
351 packet_length);
Myles Watson71390182017-01-24 13:34:59 -0800352 memcpy(hci_packet_.data(), hci_packet_preamble_,
353 preamble_size_for_type[hci_packet_type_]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700354 hci_packet_bytes_remaining_ = packet_length;
355 hci_parser_state_ = HCI_PAYLOAD;
356 hci_packet_bytes_read_ = 0;
357 }
358 break;
359 }
360
361 case HCI_PAYLOAD: {
362 size_t bytes_read = TEMP_FAILURE_RETRY(
363 read(fd,
364 hci_packet_.data() + preamble_size_for_type[hci_packet_type_] +
365 hci_packet_bytes_read_,
366 hci_packet_bytes_remaining_));
367 hci_packet_bytes_remaining_ -= bytes_read;
368 hci_packet_bytes_read_ += bytes_read;
369 if (hci_packet_bytes_remaining_ == 0) {
Myles Watsona7d33b32017-01-24 09:09:58 -0800370 if (internal_command_cb != nullptr &&
371 hci_packet_type_ == HCI_PACKET_TYPE_EVENT &&
372 internal_command_event_match(hci_packet_)) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800373 HC_BT_HDR* bt_hdr =
374 WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet_);
Myles Watsona7d33b32017-01-24 09:09:58 -0800375
376 // The callbacks can send new commands, so don't zero after calling.
377 tINT_CMD_CBACK saved_cb = internal_command_cb;
378 internal_command_cb = nullptr;
379 saved_cb(bt_hdr);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700380 } else {
Myles Watsona7d33b32017-01-24 09:09:58 -0800381 packet_read_cb_(hci_packet_type_, hci_packet_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700382 }
383 hci_parser_state_ = HCI_IDLE;
384 }
385 break;
386 }
387 }
388}
389
390} // namespace implementation
391} // namespace V1_0
392} // namespace bluetooth
393} // namespace hardware
394} // namespace android