blob: 2e0f5d6ded94ab3b7dc1ee424d29925d85d7fa36 [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 Watsona7d33b32017-01-24 09:09:58 -080073bool internal_command_event_match(const hidl_vec<uint8_t>& packet) {
74 uint8_t event_code = packet[0];
75 if (event_code != HCI_COMMAND_COMPLETE_EVENT) {
76 ALOGE("%s: Unhandled event type %02X", __func__, event_code);
77 return false;
78 }
79
80 size_t opcode_offset = HCI_EVENT_PREAMBLE_SIZE + 1; // Skip num packets.
81
82 uint16_t opcode = packet[opcode_offset] | (packet[opcode_offset + 1] << 8);
83
84 ALOGV("%s internal_command_opcode = %04X opcode = %04x", __func__,
85 internal_command_opcode, opcode);
86 return opcode == internal_command_opcode;
87}
88
Andre Eisenbach89ba5282016-10-13 15:45:02 -070089uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
Myles Watsona7d33b32017-01-24 09:09:58 -080090 ALOGV("%s opcode: 0x%04x, ptr: %p, cb: %p", __func__, opcode, buffer,
91 callback);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070092 internal_command_cb = callback;
Myles Watsona7d33b32017-01-24 09:09:58 -080093 internal_command_opcode = opcode;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070094 uint8_t type = HCI_PACKET_TYPE_COMMAND;
Andre Eisenbach9041d972017-01-17 18:23:12 -080095 VendorInterface::get()->Send(&type, 1);
96 HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
97 VendorInterface::get()->Send(bt_hdr->data, bt_hdr->len);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070098 return true;
99}
100
101void firmware_config_cb(bt_vendor_op_result_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800102 ALOGV("%s result: %d", __func__, result);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700103 VendorInterface::get()->OnFirmwareConfigured(result);
104}
105
106void sco_config_cb(bt_vendor_op_result_t result) {
107 ALOGD("%s result: %d", __func__, result);
108}
109
110void low_power_mode_cb(bt_vendor_op_result_t result) {
111 ALOGD("%s result: %d", __func__, result);
112}
113
114void sco_audiostate_cb(bt_vendor_op_result_t result) {
115 ALOGD("%s result: %d", __func__, result);
116}
117
118void* buffer_alloc_cb(int size) {
119 void* p = new uint8_t[size];
120 ALOGV("%s pts: %p, size: %d", __func__, p, size);
121 return p;
122}
123
124void buffer_free_cb(void* buffer) {
125 ALOGV("%s ptr: %p", __func__, buffer);
126 delete[] reinterpret_cast<uint8_t*>(buffer);
127}
128
129void epilog_cb(bt_vendor_op_result_t result) {
130 ALOGD("%s result: %d", __func__, result);
131}
132
133void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op,
134 uint8_t av_handle) {
135 ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle);
136}
137
138const bt_vendor_callbacks_t lib_callbacks = {
139 sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
140 low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
141 buffer_free_cb, transmit_cb, epilog_cb,
142 a2dp_offload_cb};
143
144} // namespace
145
146namespace android {
147namespace hardware {
148namespace bluetooth {
149namespace V1_0 {
150namespace implementation {
151
Andre Eisenbach9041d972017-01-17 18:23:12 -0800152class FirmwareStartupTimer {
153 public:
154 FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {}
155
156 ~FirmwareStartupTimer() {
157 std::chrono::duration<double> duration =
158 std::chrono::steady_clock::now() - start_time_;
159 double s = duration.count();
160 if (s == 0) return;
Myles Watson8ffcbc72017-01-20 10:09:38 -0800161 ALOGI("Firmware configured in %.3fs", s);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800162 }
163
164 private:
165 std::chrono::steady_clock::time_point start_time_;
166};
167
168bool VendorInterface::Initialize(
169 InitializeCompleteCallback initialize_complete_cb,
170 PacketReadCallback packet_read_cb) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700171 assert(!g_vendor_interface);
172 g_vendor_interface = new VendorInterface();
Andre Eisenbach9041d972017-01-17 18:23:12 -0800173 return g_vendor_interface->Open(initialize_complete_cb, packet_read_cb);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700174}
175
176void VendorInterface::Shutdown() {
177 CHECK(g_vendor_interface);
178 g_vendor_interface->Close();
179 delete g_vendor_interface;
180 g_vendor_interface = nullptr;
181}
182
183VendorInterface* VendorInterface::get() { return g_vendor_interface; }
184
Andre Eisenbach9041d972017-01-17 18:23:12 -0800185bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
186 PacketReadCallback packet_read_cb) {
187 initialize_complete_cb_ = initialize_complete_cb;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700188 packet_read_cb_ = packet_read_cb;
189
190 // Initialize vendor interface
191
192 lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
193 if (!lib_handle_) {
194 ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
195 dlerror());
196 return false;
197 }
198
199 lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
200 dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
201 if (!lib_interface_) {
202 ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
203 VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
204 return false;
205 }
206
Myles Watson6a7d6222016-10-13 15:45:02 -0700207 // Get the local BD address
208
209 uint8_t local_bda[BluetoothAddress::kBytes];
210 CHECK(BluetoothAddress::get_local_address(local_bda));
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700211 int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
212 if (status) {
213 ALOGE("%s unable to initialize vendor library: %d", __func__, status);
214 return false;
215 }
216
217 ALOGD("%s vendor library loaded", __func__);
218
219 // Power cycle chip
220
221 int power_state = BT_VND_PWR_OFF;
222 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
223 power_state = BT_VND_PWR_ON;
224 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
225
226 // Get the UART socket
227
228 int fd_list[CH_MAX] = {0};
229 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
230
231 if (fd_count != 1) {
232 ALOGE("%s fd count %d != 1; we can't handle this currently...", __func__,
233 fd_count);
234 return false;
235 }
236
237 uart_fd_ = fd_list[0];
238 if (uart_fd_ == INVALID_FD) {
239 ALOGE("%s unable to determine UART fd", __func__);
240 return false;
241 }
242
Myles Watson8ffcbc72017-01-20 10:09:38 -0800243 ALOGI("%s UART fd: %d", __func__, uart_fd_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700244
245 fd_watcher_.WatchFdForNonBlockingReads(uart_fd_,
246 [this](int fd) { OnDataReady(fd); });
247
248 // Start configuring the firmware
Andre Eisenbach9041d972017-01-17 18:23:12 -0800249 firmware_startup_timer_ = new FirmwareStartupTimer();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700250 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
251
252 return true;
253}
254
255void VendorInterface::Close() {
256 fd_watcher_.StopWatchingFileDescriptor();
257
258 if (lib_interface_ != nullptr) {
259 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
260 uart_fd_ = INVALID_FD;
261 int power_state = BT_VND_PWR_OFF;
262 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
263 }
264
265 if (lib_handle_ != nullptr) {
266 dlclose(lib_handle_);
267 lib_handle_ = nullptr;
268 }
269
Andre Eisenbach9041d972017-01-17 18:23:12 -0800270 if (firmware_startup_timer_ != nullptr) {
271 delete firmware_startup_timer_;
272 firmware_startup_timer_ = nullptr;
273 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700274}
275
276size_t VendorInterface::Send(const uint8_t* data, size_t length) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700277 if (uart_fd_ == INVALID_FD) return 0;
278
279 size_t transmitted_length = 0;
280 while (length > 0) {
281 ssize_t ret =
282 TEMP_FAILURE_RETRY(write(uart_fd_, data + transmitted_length, length));
283
284 if (ret == -1) {
285 if (errno == EAGAIN) continue;
286 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
287 break;
288
289 } else if (ret == 0) {
290 // Nothing written :(
291 ALOGE("%s zero bytes written - something went wrong...", __func__);
292 break;
293 }
294
295 transmitted_length += ret;
296 length -= ret;
297 }
298
299 return transmitted_length;
300}
301
302void VendorInterface::OnFirmwareConfigured(uint8_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800303 ALOGD("%s result: %d", __func__, result);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800304
305 if (firmware_startup_timer_ != nullptr) {
306 delete firmware_startup_timer_;
307 firmware_startup_timer_ = nullptr;
308 }
309
310 if (initialize_complete_cb_ != nullptr) {
311 initialize_complete_cb_(result == 0);
312 initialize_complete_cb_ = nullptr;
313 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700314}
315
316void VendorInterface::OnDataReady(int fd) {
317 switch (hci_parser_state_) {
318 case HCI_IDLE: {
319 uint8_t buffer[1] = {0};
320 size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
321 CHECK(bytes_read == 1);
322 hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
323 // TODO(eisenbach): Check for workaround(s)
324 CHECK(hci_packet_type_ >= HCI_PACKET_TYPE_ACL_DATA &&
325 hci_packet_type_ <= HCI_PACKET_TYPE_EVENT)
Myles Watson8ffcbc72017-01-20 10:09:38 -0800326 << "buffer[0] = " << static_cast<unsigned int>(buffer[0]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700327 hci_parser_state_ = HCI_TYPE_READY;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700328 hci_packet_bytes_remaining_ = preamble_size_for_type[hci_packet_type_];
329 hci_packet_bytes_read_ = 0;
330 break;
331 }
332
333 case HCI_TYPE_READY: {
334 size_t bytes_read = TEMP_FAILURE_RETRY(
Myles Watson71390182017-01-24 13:34:59 -0800335 read(fd, hci_packet_preamble_ + hci_packet_bytes_read_,
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700336 hci_packet_bytes_remaining_));
337 CHECK(bytes_read > 0);
338 hci_packet_bytes_remaining_ -= bytes_read;
339 hci_packet_bytes_read_ += bytes_read;
340 if (hci_packet_bytes_remaining_ == 0) {
341 size_t packet_length =
Myles Watson71390182017-01-24 13:34:59 -0800342 HciGetPacketLengthForType(hci_packet_type_, hci_packet_preamble_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700343 hci_packet_.resize(preamble_size_for_type[hci_packet_type_] +
344 packet_length);
Myles Watson71390182017-01-24 13:34:59 -0800345 memcpy(hci_packet_.data(), hci_packet_preamble_,
346 preamble_size_for_type[hci_packet_type_]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700347 hci_packet_bytes_remaining_ = packet_length;
348 hci_parser_state_ = HCI_PAYLOAD;
349 hci_packet_bytes_read_ = 0;
350 }
351 break;
352 }
353
354 case HCI_PAYLOAD: {
355 size_t bytes_read = TEMP_FAILURE_RETRY(
356 read(fd,
357 hci_packet_.data() + preamble_size_for_type[hci_packet_type_] +
358 hci_packet_bytes_read_,
359 hci_packet_bytes_remaining_));
360 hci_packet_bytes_remaining_ -= bytes_read;
361 hci_packet_bytes_read_ += bytes_read;
362 if (hci_packet_bytes_remaining_ == 0) {
Myles Watsona7d33b32017-01-24 09:09:58 -0800363 if (internal_command_cb != nullptr &&
364 hci_packet_type_ == HCI_PACKET_TYPE_EVENT &&
365 internal_command_event_match(hci_packet_)) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800366 HC_BT_HDR* bt_hdr =
367 WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet_);
Myles Watsona7d33b32017-01-24 09:09:58 -0800368
369 // The callbacks can send new commands, so don't zero after calling.
370 tINT_CMD_CBACK saved_cb = internal_command_cb;
371 internal_command_cb = nullptr;
372 saved_cb(bt_hdr);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700373 } else {
Myles Watsona7d33b32017-01-24 09:09:58 -0800374 packet_read_cb_(hci_packet_type_, hci_packet_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700375 }
376 hci_parser_state_ = HCI_IDLE;
377 }
378 break;
379 }
380 }
381}
382
383} // namespace implementation
384} // namespace V1_0
385} // namespace bluetooth
386} // namespace hardware
387} // namespace android