blob: 278b66c77870d021d64e57dd89266108521382d1 [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
Andre Eisenbach89ba5282016-10-13 15:45:02 -070047VendorInterface* g_vendor_interface = nullptr;
48
49const size_t preamble_size_for_type[] = {
50 0, HCI_COMMAND_PREAMBLE_SIZE, HCI_ACL_PREAMBLE_SIZE, HCI_SCO_PREAMBLE_SIZE,
51 HCI_EVENT_PREAMBLE_SIZE};
52const size_t packet_length_offset_for_type[] = {
53 0, HCI_LENGTH_OFFSET_CMD, HCI_LENGTH_OFFSET_ACL, HCI_LENGTH_OFFSET_SCO,
54 HCI_LENGTH_OFFSET_EVT};
55
Myles Watson71390182017-01-24 13:34:59 -080056size_t HciGetPacketLengthForType(HciPacketType type, const uint8_t* preamble) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070057 size_t offset = packet_length_offset_for_type[type];
Myles Watson71390182017-01-24 13:34:59 -080058 if (type != HCI_PACKET_TYPE_ACL_DATA) return preamble[offset];
59 return (((preamble[offset + 1]) << 8) | preamble[offset]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070060}
61
Andre Eisenbach9041d972017-01-17 18:23:12 -080062HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070063 size_t packet_size = data.size() + sizeof(HC_BT_HDR);
64 HC_BT_HDR* packet = reinterpret_cast<HC_BT_HDR*>(new uint8_t[packet_size]);
65 packet->offset = 0;
66 packet->len = data.size();
67 packet->layer_specific = 0;
68 packet->event = event;
Myles Watson6a7d6222016-10-13 15:45:02 -070069 // TODO(eisenbach): Avoid copy here; if BT_HDR->data can be ensured to
Andre Eisenbach89ba5282016-10-13 15:45:02 -070070 // be the only way the data is accessed, a pointer could be passed here...
71 memcpy(packet->data, data.data(), data.size());
72 return packet;
73}
74
Myles Watsondf765ea2017-01-30 09:07:37 -080075size_t write_safely(int fd, const uint8_t* data, size_t length) {
76 size_t transmitted_length = 0;
77 while (length > 0) {
78 ssize_t ret =
79 TEMP_FAILURE_RETRY(write(fd, data + transmitted_length, length));
80
81 if (ret == -1) {
82 if (errno == EAGAIN) continue;
83 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
84 break;
85
86 } else if (ret == 0) {
87 // Nothing written :(
88 ALOGE("%s zero bytes written - something went wrong...", __func__);
89 break;
90 }
91
92 transmitted_length += ret;
93 length -= ret;
94 }
95
96 return transmitted_length;
97}
98
Myles Watsona7d33b32017-01-24 09:09:58 -080099bool internal_command_event_match(const hidl_vec<uint8_t>& packet) {
100 uint8_t event_code = packet[0];
101 if (event_code != HCI_COMMAND_COMPLETE_EVENT) {
102 ALOGE("%s: Unhandled event type %02X", __func__, event_code);
103 return false;
104 }
105
106 size_t opcode_offset = HCI_EVENT_PREAMBLE_SIZE + 1; // Skip num packets.
107
108 uint16_t opcode = packet[opcode_offset] | (packet[opcode_offset + 1] << 8);
109
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800110 ALOGV("%s internal_command.opcode = %04X opcode = %04x", __func__,
111 internal_command.opcode, opcode);
112 return opcode == internal_command.opcode;
Myles Watsona7d33b32017-01-24 09:09:58 -0800113}
114
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700115uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
Myles Watsona7d33b32017-01-24 09:09:58 -0800116 ALOGV("%s opcode: 0x%04x, ptr: %p, cb: %p", __func__, opcode, buffer,
117 callback);
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800118 internal_command.cb = callback;
119 internal_command.opcode = opcode;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700120 uint8_t type = HCI_PACKET_TYPE_COMMAND;
Andre Eisenbach9041d972017-01-17 18:23:12 -0800121 HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
Myles Watsondf765ea2017-01-30 09:07:37 -0800122 VendorInterface::get()->Send(type, bt_hdr->data, bt_hdr->len);
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800123 delete[] reinterpret_cast<uint8_t*>(buffer);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700124 return true;
125}
126
127void firmware_config_cb(bt_vendor_op_result_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800128 ALOGV("%s result: %d", __func__, result);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700129 VendorInterface::get()->OnFirmwareConfigured(result);
130}
131
132void sco_config_cb(bt_vendor_op_result_t result) {
133 ALOGD("%s result: %d", __func__, result);
134}
135
136void low_power_mode_cb(bt_vendor_op_result_t result) {
137 ALOGD("%s result: %d", __func__, result);
138}
139
140void sco_audiostate_cb(bt_vendor_op_result_t result) {
141 ALOGD("%s result: %d", __func__, result);
142}
143
144void* buffer_alloc_cb(int size) {
145 void* p = new uint8_t[size];
146 ALOGV("%s pts: %p, size: %d", __func__, p, size);
147 return p;
148}
149
150void buffer_free_cb(void* buffer) {
151 ALOGV("%s ptr: %p", __func__, buffer);
152 delete[] reinterpret_cast<uint8_t*>(buffer);
153}
154
155void epilog_cb(bt_vendor_op_result_t result) {
156 ALOGD("%s result: %d", __func__, result);
157}
158
159void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op,
160 uint8_t av_handle) {
161 ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle);
162}
163
164const bt_vendor_callbacks_t lib_callbacks = {
165 sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
166 low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
167 buffer_free_cb, transmit_cb, epilog_cb,
168 a2dp_offload_cb};
169
170} // namespace
171
172namespace android {
173namespace hardware {
174namespace bluetooth {
175namespace V1_0 {
176namespace implementation {
177
Andre Eisenbach9041d972017-01-17 18:23:12 -0800178class FirmwareStartupTimer {
179 public:
180 FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {}
181
182 ~FirmwareStartupTimer() {
183 std::chrono::duration<double> duration =
184 std::chrono::steady_clock::now() - start_time_;
185 double s = duration.count();
186 if (s == 0) return;
Myles Watson8ffcbc72017-01-20 10:09:38 -0800187 ALOGI("Firmware configured in %.3fs", s);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800188 }
189
190 private:
191 std::chrono::steady_clock::time_point start_time_;
192};
193
194bool VendorInterface::Initialize(
195 InitializeCompleteCallback initialize_complete_cb,
196 PacketReadCallback packet_read_cb) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700197 assert(!g_vendor_interface);
198 g_vendor_interface = new VendorInterface();
Andre Eisenbach9041d972017-01-17 18:23:12 -0800199 return g_vendor_interface->Open(initialize_complete_cb, packet_read_cb);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700200}
201
202void VendorInterface::Shutdown() {
203 CHECK(g_vendor_interface);
204 g_vendor_interface->Close();
205 delete g_vendor_interface;
206 g_vendor_interface = nullptr;
207}
208
209VendorInterface* VendorInterface::get() { return g_vendor_interface; }
210
Andre Eisenbach9041d972017-01-17 18:23:12 -0800211bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
212 PacketReadCallback packet_read_cb) {
213 initialize_complete_cb_ = initialize_complete_cb;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700214 packet_read_cb_ = packet_read_cb;
215
216 // Initialize vendor interface
217
218 lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
219 if (!lib_handle_) {
220 ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
221 dlerror());
222 return false;
223 }
224
225 lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
226 dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
227 if (!lib_interface_) {
228 ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
229 VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
230 return false;
231 }
232
Myles Watson6a7d6222016-10-13 15:45:02 -0700233 // Get the local BD address
234
235 uint8_t local_bda[BluetoothAddress::kBytes];
236 CHECK(BluetoothAddress::get_local_address(local_bda));
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700237 int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
238 if (status) {
239 ALOGE("%s unable to initialize vendor library: %d", __func__, status);
240 return false;
241 }
242
243 ALOGD("%s vendor library loaded", __func__);
244
245 // Power cycle chip
246
247 int power_state = BT_VND_PWR_OFF;
248 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
249 power_state = BT_VND_PWR_ON;
250 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
251
252 // Get the UART socket
253
254 int fd_list[CH_MAX] = {0};
255 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
256
257 if (fd_count != 1) {
258 ALOGE("%s fd count %d != 1; we can't handle this currently...", __func__,
259 fd_count);
260 return false;
261 }
262
263 uart_fd_ = fd_list[0];
264 if (uart_fd_ == INVALID_FD) {
265 ALOGE("%s unable to determine UART fd", __func__);
266 return false;
267 }
268
Myles Watson8ffcbc72017-01-20 10:09:38 -0800269 ALOGI("%s UART fd: %d", __func__, uart_fd_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700270
271 fd_watcher_.WatchFdForNonBlockingReads(uart_fd_,
272 [this](int fd) { OnDataReady(fd); });
273
274 // Start configuring the firmware
Andre Eisenbach9041d972017-01-17 18:23:12 -0800275 firmware_startup_timer_ = new FirmwareStartupTimer();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700276 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
277
278 return true;
279}
280
281void VendorInterface::Close() {
282 fd_watcher_.StopWatchingFileDescriptor();
283
284 if (lib_interface_ != nullptr) {
285 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
286 uart_fd_ = INVALID_FD;
287 int power_state = BT_VND_PWR_OFF;
288 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
289 }
290
291 if (lib_handle_ != nullptr) {
292 dlclose(lib_handle_);
293 lib_handle_ = nullptr;
294 }
295
Andre Eisenbach9041d972017-01-17 18:23:12 -0800296 if (firmware_startup_timer_ != nullptr) {
297 delete firmware_startup_timer_;
298 firmware_startup_timer_ = nullptr;
299 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700300}
301
Myles Watsondf765ea2017-01-30 09:07:37 -0800302size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700303 if (uart_fd_ == INVALID_FD) return 0;
304
Myles Watsondf765ea2017-01-30 09:07:37 -0800305 int rv = write_safely(uart_fd_, &type, sizeof(type));
306 if (rv == sizeof(type))
307 rv = write_safely(uart_fd_, data, length);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700308
Myles Watsondf765ea2017-01-30 09:07:37 -0800309 return rv;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700310}
311
312void VendorInterface::OnFirmwareConfigured(uint8_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800313 ALOGD("%s result: %d", __func__, result);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800314
315 if (firmware_startup_timer_ != nullptr) {
316 delete firmware_startup_timer_;
317 firmware_startup_timer_ = nullptr;
318 }
319
320 if (initialize_complete_cb_ != nullptr) {
321 initialize_complete_cb_(result == 0);
322 initialize_complete_cb_ = nullptr;
323 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700324}
325
326void VendorInterface::OnDataReady(int fd) {
327 switch (hci_parser_state_) {
328 case HCI_IDLE: {
329 uint8_t buffer[1] = {0};
330 size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
331 CHECK(bytes_read == 1);
332 hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
333 // TODO(eisenbach): Check for workaround(s)
334 CHECK(hci_packet_type_ >= HCI_PACKET_TYPE_ACL_DATA &&
335 hci_packet_type_ <= HCI_PACKET_TYPE_EVENT)
Myles Watson8ffcbc72017-01-20 10:09:38 -0800336 << "buffer[0] = " << static_cast<unsigned int>(buffer[0]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700337 hci_parser_state_ = HCI_TYPE_READY;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700338 hci_packet_bytes_remaining_ = preamble_size_for_type[hci_packet_type_];
339 hci_packet_bytes_read_ = 0;
340 break;
341 }
342
343 case HCI_TYPE_READY: {
344 size_t bytes_read = TEMP_FAILURE_RETRY(
Myles Watson71390182017-01-24 13:34:59 -0800345 read(fd, hci_packet_preamble_ + hci_packet_bytes_read_,
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700346 hci_packet_bytes_remaining_));
347 CHECK(bytes_read > 0);
348 hci_packet_bytes_remaining_ -= bytes_read;
349 hci_packet_bytes_read_ += bytes_read;
350 if (hci_packet_bytes_remaining_ == 0) {
351 size_t packet_length =
Myles Watson71390182017-01-24 13:34:59 -0800352 HciGetPacketLengthForType(hci_packet_type_, hci_packet_preamble_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700353 hci_packet_.resize(preamble_size_for_type[hci_packet_type_] +
354 packet_length);
Myles Watson71390182017-01-24 13:34:59 -0800355 memcpy(hci_packet_.data(), hci_packet_preamble_,
356 preamble_size_for_type[hci_packet_type_]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700357 hci_packet_bytes_remaining_ = packet_length;
358 hci_parser_state_ = HCI_PAYLOAD;
359 hci_packet_bytes_read_ = 0;
360 }
361 break;
362 }
363
364 case HCI_PAYLOAD: {
365 size_t bytes_read = TEMP_FAILURE_RETRY(
366 read(fd,
367 hci_packet_.data() + preamble_size_for_type[hci_packet_type_] +
368 hci_packet_bytes_read_,
369 hci_packet_bytes_remaining_));
370 hci_packet_bytes_remaining_ -= bytes_read;
371 hci_packet_bytes_read_ += bytes_read;
372 if (hci_packet_bytes_remaining_ == 0) {
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800373 if (internal_command.cb != nullptr &&
Myles Watsona7d33b32017-01-24 09:09:58 -0800374 hci_packet_type_ == HCI_PACKET_TYPE_EVENT &&
375 internal_command_event_match(hci_packet_)) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800376 HC_BT_HDR* bt_hdr =
377 WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet_);
Myles Watsona7d33b32017-01-24 09:09:58 -0800378
379 // The callbacks can send new commands, so don't zero after calling.
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800380 tINT_CMD_CBACK saved_cb = internal_command.cb;
381 internal_command.cb = nullptr;
Myles Watsona7d33b32017-01-24 09:09:58 -0800382 saved_cb(bt_hdr);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700383 } else {
Myles Watsona7d33b32017-01-24 09:09:58 -0800384 packet_read_cb_(hci_packet_type_, hci_packet_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700385 }
386 hci_parser_state_ = HCI_IDLE;
387 }
388 break;
389 }
390 }
391}
392
393} // namespace implementation
394} // namespace V1_0
395} // namespace bluetooth
396} // namespace hardware
397} // namespace android