blob: 8573ee5478d57b74694dab20e360f512572eb3ac [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;
43VendorInterface* g_vendor_interface = nullptr;
44
45const size_t preamble_size_for_type[] = {
46 0, HCI_COMMAND_PREAMBLE_SIZE, HCI_ACL_PREAMBLE_SIZE, HCI_SCO_PREAMBLE_SIZE,
47 HCI_EVENT_PREAMBLE_SIZE};
48const size_t packet_length_offset_for_type[] = {
49 0, HCI_LENGTH_OFFSET_CMD, HCI_LENGTH_OFFSET_ACL, HCI_LENGTH_OFFSET_SCO,
50 HCI_LENGTH_OFFSET_EVT};
51
Myles Watson71390182017-01-24 13:34:59 -080052size_t HciGetPacketLengthForType(HciPacketType type, const uint8_t* preamble) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070053 size_t offset = packet_length_offset_for_type[type];
Myles Watson71390182017-01-24 13:34:59 -080054 if (type != HCI_PACKET_TYPE_ACL_DATA) return preamble[offset];
55 return (((preamble[offset + 1]) << 8) | preamble[offset]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070056}
57
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
71uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
72 ALOGV("%s opcode: 0x%04x, ptr: %p", __func__, opcode, buffer);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070073 internal_command_cb = callback;
74 uint8_t type = HCI_PACKET_TYPE_COMMAND;
Andre Eisenbach9041d972017-01-17 18:23:12 -080075 VendorInterface::get()->Send(&type, 1);
76 HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
77 VendorInterface::get()->Send(bt_hdr->data, bt_hdr->len);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070078 return true;
79}
80
81void firmware_config_cb(bt_vendor_op_result_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -080082 ALOGV("%s result: %d", __func__, result);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070083 VendorInterface::get()->OnFirmwareConfigured(result);
84}
85
86void sco_config_cb(bt_vendor_op_result_t result) {
87 ALOGD("%s result: %d", __func__, result);
88}
89
90void low_power_mode_cb(bt_vendor_op_result_t result) {
91 ALOGD("%s result: %d", __func__, result);
92}
93
94void sco_audiostate_cb(bt_vendor_op_result_t result) {
95 ALOGD("%s result: %d", __func__, result);
96}
97
98void* buffer_alloc_cb(int size) {
99 void* p = new uint8_t[size];
100 ALOGV("%s pts: %p, size: %d", __func__, p, size);
101 return p;
102}
103
104void buffer_free_cb(void* buffer) {
105 ALOGV("%s ptr: %p", __func__, buffer);
106 delete[] reinterpret_cast<uint8_t*>(buffer);
107}
108
109void epilog_cb(bt_vendor_op_result_t result) {
110 ALOGD("%s result: %d", __func__, result);
111}
112
113void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op,
114 uint8_t av_handle) {
115 ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle);
116}
117
118const bt_vendor_callbacks_t lib_callbacks = {
119 sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
120 low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
121 buffer_free_cb, transmit_cb, epilog_cb,
122 a2dp_offload_cb};
123
124} // namespace
125
126namespace android {
127namespace hardware {
128namespace bluetooth {
129namespace V1_0 {
130namespace implementation {
131
Andre Eisenbach9041d972017-01-17 18:23:12 -0800132class FirmwareStartupTimer {
133 public:
134 FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {}
135
136 ~FirmwareStartupTimer() {
137 std::chrono::duration<double> duration =
138 std::chrono::steady_clock::now() - start_time_;
139 double s = duration.count();
140 if (s == 0) return;
Myles Watson8ffcbc72017-01-20 10:09:38 -0800141 ALOGI("Firmware configured in %.3fs", s);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800142 }
143
144 private:
145 std::chrono::steady_clock::time_point start_time_;
146};
147
148bool VendorInterface::Initialize(
149 InitializeCompleteCallback initialize_complete_cb,
150 PacketReadCallback packet_read_cb) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700151 assert(!g_vendor_interface);
152 g_vendor_interface = new VendorInterface();
Andre Eisenbach9041d972017-01-17 18:23:12 -0800153 return g_vendor_interface->Open(initialize_complete_cb, packet_read_cb);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700154}
155
156void VendorInterface::Shutdown() {
157 CHECK(g_vendor_interface);
158 g_vendor_interface->Close();
159 delete g_vendor_interface;
160 g_vendor_interface = nullptr;
161}
162
163VendorInterface* VendorInterface::get() { return g_vendor_interface; }
164
Andre Eisenbach9041d972017-01-17 18:23:12 -0800165bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
166 PacketReadCallback packet_read_cb) {
167 initialize_complete_cb_ = initialize_complete_cb;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700168 packet_read_cb_ = packet_read_cb;
169
170 // Initialize vendor interface
171
172 lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
173 if (!lib_handle_) {
174 ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
175 dlerror());
176 return false;
177 }
178
179 lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
180 dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
181 if (!lib_interface_) {
182 ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
183 VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
184 return false;
185 }
186
Myles Watson6a7d6222016-10-13 15:45:02 -0700187 // Get the local BD address
188
189 uint8_t local_bda[BluetoothAddress::kBytes];
190 CHECK(BluetoothAddress::get_local_address(local_bda));
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700191 int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
192 if (status) {
193 ALOGE("%s unable to initialize vendor library: %d", __func__, status);
194 return false;
195 }
196
197 ALOGD("%s vendor library loaded", __func__);
198
199 // Power cycle chip
200
201 int power_state = BT_VND_PWR_OFF;
202 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
203 power_state = BT_VND_PWR_ON;
204 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
205
206 // Get the UART socket
207
208 int fd_list[CH_MAX] = {0};
209 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
210
211 if (fd_count != 1) {
212 ALOGE("%s fd count %d != 1; we can't handle this currently...", __func__,
213 fd_count);
214 return false;
215 }
216
217 uart_fd_ = fd_list[0];
218 if (uart_fd_ == INVALID_FD) {
219 ALOGE("%s unable to determine UART fd", __func__);
220 return false;
221 }
222
Myles Watson8ffcbc72017-01-20 10:09:38 -0800223 ALOGI("%s UART fd: %d", __func__, uart_fd_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700224
225 fd_watcher_.WatchFdForNonBlockingReads(uart_fd_,
226 [this](int fd) { OnDataReady(fd); });
227
228 // Start configuring the firmware
Andre Eisenbach9041d972017-01-17 18:23:12 -0800229 firmware_startup_timer_ = new FirmwareStartupTimer();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700230 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
231
232 return true;
233}
234
235void VendorInterface::Close() {
236 fd_watcher_.StopWatchingFileDescriptor();
237
238 if (lib_interface_ != nullptr) {
239 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
240 uart_fd_ = INVALID_FD;
241 int power_state = BT_VND_PWR_OFF;
242 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
243 }
244
245 if (lib_handle_ != nullptr) {
246 dlclose(lib_handle_);
247 lib_handle_ = nullptr;
248 }
249
Andre Eisenbach9041d972017-01-17 18:23:12 -0800250 if (firmware_startup_timer_ != nullptr) {
251 delete firmware_startup_timer_;
252 firmware_startup_timer_ = nullptr;
253 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700254}
255
256size_t VendorInterface::Send(const uint8_t* data, size_t length) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700257 if (uart_fd_ == INVALID_FD) return 0;
258
259 size_t transmitted_length = 0;
260 while (length > 0) {
261 ssize_t ret =
262 TEMP_FAILURE_RETRY(write(uart_fd_, data + transmitted_length, length));
263
264 if (ret == -1) {
265 if (errno == EAGAIN) continue;
266 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
267 break;
268
269 } else if (ret == 0) {
270 // Nothing written :(
271 ALOGE("%s zero bytes written - something went wrong...", __func__);
272 break;
273 }
274
275 transmitted_length += ret;
276 length -= ret;
277 }
278
279 return transmitted_length;
280}
281
282void VendorInterface::OnFirmwareConfigured(uint8_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800283 ALOGD("%s result: %d", __func__, result);
284 internal_command_cb = nullptr;
285
286 if (firmware_startup_timer_ != nullptr) {
287 delete firmware_startup_timer_;
288 firmware_startup_timer_ = nullptr;
289 }
290
291 if (initialize_complete_cb_ != nullptr) {
292 initialize_complete_cb_(result == 0);
293 initialize_complete_cb_ = nullptr;
294 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700295}
296
297void VendorInterface::OnDataReady(int fd) {
298 switch (hci_parser_state_) {
299 case HCI_IDLE: {
300 uint8_t buffer[1] = {0};
301 size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
302 CHECK(bytes_read == 1);
303 hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
304 // TODO(eisenbach): Check for workaround(s)
305 CHECK(hci_packet_type_ >= HCI_PACKET_TYPE_ACL_DATA &&
306 hci_packet_type_ <= HCI_PACKET_TYPE_EVENT)
Myles Watson8ffcbc72017-01-20 10:09:38 -0800307 << "buffer[0] = " << static_cast<unsigned int>(buffer[0]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700308 hci_parser_state_ = HCI_TYPE_READY;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700309 hci_packet_bytes_remaining_ = preamble_size_for_type[hci_packet_type_];
310 hci_packet_bytes_read_ = 0;
311 break;
312 }
313
314 case HCI_TYPE_READY: {
315 size_t bytes_read = TEMP_FAILURE_RETRY(
Myles Watson71390182017-01-24 13:34:59 -0800316 read(fd, hci_packet_preamble_ + hci_packet_bytes_read_,
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700317 hci_packet_bytes_remaining_));
318 CHECK(bytes_read > 0);
319 hci_packet_bytes_remaining_ -= bytes_read;
320 hci_packet_bytes_read_ += bytes_read;
321 if (hci_packet_bytes_remaining_ == 0) {
322 size_t packet_length =
Myles Watson71390182017-01-24 13:34:59 -0800323 HciGetPacketLengthForType(hci_packet_type_, hci_packet_preamble_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700324 hci_packet_.resize(preamble_size_for_type[hci_packet_type_] +
325 packet_length);
Myles Watson71390182017-01-24 13:34:59 -0800326 memcpy(hci_packet_.data(), hci_packet_preamble_,
327 preamble_size_for_type[hci_packet_type_]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700328 hci_packet_bytes_remaining_ = packet_length;
329 hci_parser_state_ = HCI_PAYLOAD;
330 hci_packet_bytes_read_ = 0;
331 }
332 break;
333 }
334
335 case HCI_PAYLOAD: {
336 size_t bytes_read = TEMP_FAILURE_RETRY(
337 read(fd,
338 hci_packet_.data() + preamble_size_for_type[hci_packet_type_] +
339 hci_packet_bytes_read_,
340 hci_packet_bytes_remaining_));
341 hci_packet_bytes_remaining_ -= bytes_read;
342 hci_packet_bytes_read_ += bytes_read;
343 if (hci_packet_bytes_remaining_ == 0) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800344 if (internal_command_cb != nullptr) {
345 HC_BT_HDR* bt_hdr =
346 WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet_);
347 internal_command_cb(bt_hdr);
348 } else if (packet_read_cb_ != nullptr &&
349 initialize_complete_cb_ == nullptr) {
350 packet_read_cb_(hci_packet_type_, hci_packet_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700351 } else {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800352 ALOGE(
353 "%s HCI_PAYLOAD received without packet_read_cb or pending init.",
354 __func__);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700355 }
356 hci_parser_state_ = HCI_IDLE;
357 }
358 break;
359 }
360 }
361}
362
363} // namespace implementation
364} // namespace V1_0
365} // namespace bluetooth
366} // namespace hardware
367} // namespace android