blob: c04a7632805dcae2bf04969b09b65947a951f6b3 [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
Andre Eisenbach9041d972017-01-17 18:23:12 -080052size_t HciGetPacketLengthForType(HciPacketType type,
53 const hidl_vec<uint8_t>& packet) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070054 size_t offset = packet_length_offset_for_type[type];
Andre Eisenbach9041d972017-01-17 18:23:12 -080055 if (type != HCI_PACKET_TYPE_ACL_DATA) return packet[offset];
56 return (((packet[offset + 1]) << 8) | packet[offset]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070057}
58
Andre Eisenbach9041d972017-01-17 18:23:12 -080059HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070060 size_t packet_size = data.size() + sizeof(HC_BT_HDR);
61 HC_BT_HDR* packet = reinterpret_cast<HC_BT_HDR*>(new uint8_t[packet_size]);
62 packet->offset = 0;
63 packet->len = data.size();
64 packet->layer_specific = 0;
65 packet->event = event;
Myles Watson6a7d6222016-10-13 15:45:02 -070066 // TODO(eisenbach): Avoid copy here; if BT_HDR->data can be ensured to
Andre Eisenbach89ba5282016-10-13 15:45:02 -070067 // be the only way the data is accessed, a pointer could be passed here...
68 memcpy(packet->data, data.data(), data.size());
69 return packet;
70}
71
72uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
73 ALOGV("%s opcode: 0x%04x, ptr: %p", __func__, opcode, buffer);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070074 internal_command_cb = callback;
75 uint8_t type = HCI_PACKET_TYPE_COMMAND;
Andre Eisenbach9041d972017-01-17 18:23:12 -080076 VendorInterface::get()->Send(&type, 1);
77 HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
78 VendorInterface::get()->Send(bt_hdr->data, bt_hdr->len);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070079 return true;
80}
81
82void firmware_config_cb(bt_vendor_op_result_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -080083 ALOGV("%s result: %d", __func__, result);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070084 VendorInterface::get()->OnFirmwareConfigured(result);
85}
86
87void sco_config_cb(bt_vendor_op_result_t result) {
88 ALOGD("%s result: %d", __func__, result);
89}
90
91void low_power_mode_cb(bt_vendor_op_result_t result) {
92 ALOGD("%s result: %d", __func__, result);
93}
94
95void sco_audiostate_cb(bt_vendor_op_result_t result) {
96 ALOGD("%s result: %d", __func__, result);
97}
98
99void* buffer_alloc_cb(int size) {
100 void* p = new uint8_t[size];
101 ALOGV("%s pts: %p, size: %d", __func__, p, size);
102 return p;
103}
104
105void buffer_free_cb(void* buffer) {
106 ALOGV("%s ptr: %p", __func__, buffer);
107 delete[] reinterpret_cast<uint8_t*>(buffer);
108}
109
110void epilog_cb(bt_vendor_op_result_t result) {
111 ALOGD("%s result: %d", __func__, result);
112}
113
114void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op,
115 uint8_t av_handle) {
116 ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle);
117}
118
119const bt_vendor_callbacks_t lib_callbacks = {
120 sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
121 low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
122 buffer_free_cb, transmit_cb, epilog_cb,
123 a2dp_offload_cb};
124
125} // namespace
126
127namespace android {
128namespace hardware {
129namespace bluetooth {
130namespace V1_0 {
131namespace implementation {
132
Andre Eisenbach9041d972017-01-17 18:23:12 -0800133class FirmwareStartupTimer {
134 public:
135 FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {}
136
137 ~FirmwareStartupTimer() {
138 std::chrono::duration<double> duration =
139 std::chrono::steady_clock::now() - start_time_;
140 double s = duration.count();
141 if (s == 0) return;
Myles Watson8ffcbc72017-01-20 10:09:38 -0800142 ALOGI("Firmware configured in %.3fs", s);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800143 }
144
145 private:
146 std::chrono::steady_clock::time_point start_time_;
147};
148
149bool VendorInterface::Initialize(
150 InitializeCompleteCallback initialize_complete_cb,
151 PacketReadCallback packet_read_cb) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700152 assert(!g_vendor_interface);
153 g_vendor_interface = new VendorInterface();
Andre Eisenbach9041d972017-01-17 18:23:12 -0800154 return g_vendor_interface->Open(initialize_complete_cb, packet_read_cb);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700155}
156
157void VendorInterface::Shutdown() {
158 CHECK(g_vendor_interface);
159 g_vendor_interface->Close();
160 delete g_vendor_interface;
161 g_vendor_interface = nullptr;
162}
163
164VendorInterface* VendorInterface::get() { return g_vendor_interface; }
165
Andre Eisenbach9041d972017-01-17 18:23:12 -0800166bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
167 PacketReadCallback packet_read_cb) {
168 initialize_complete_cb_ = initialize_complete_cb;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700169 packet_read_cb_ = packet_read_cb;
170
171 // Initialize vendor interface
172
173 lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
174 if (!lib_handle_) {
175 ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
176 dlerror());
177 return false;
178 }
179
180 lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
181 dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
182 if (!lib_interface_) {
183 ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
184 VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
185 return false;
186 }
187
Myles Watson6a7d6222016-10-13 15:45:02 -0700188 // Get the local BD address
189
190 uint8_t local_bda[BluetoothAddress::kBytes];
191 CHECK(BluetoothAddress::get_local_address(local_bda));
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700192 int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
193 if (status) {
194 ALOGE("%s unable to initialize vendor library: %d", __func__, status);
195 return false;
196 }
197
198 ALOGD("%s vendor library loaded", __func__);
199
200 // Power cycle chip
201
202 int power_state = BT_VND_PWR_OFF;
203 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
204 power_state = BT_VND_PWR_ON;
205 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
206
207 // Get the UART socket
208
209 int fd_list[CH_MAX] = {0};
210 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
211
212 if (fd_count != 1) {
213 ALOGE("%s fd count %d != 1; we can't handle this currently...", __func__,
214 fd_count);
215 return false;
216 }
217
218 uart_fd_ = fd_list[0];
219 if (uart_fd_ == INVALID_FD) {
220 ALOGE("%s unable to determine UART fd", __func__);
221 return false;
222 }
223
Myles Watson8ffcbc72017-01-20 10:09:38 -0800224 ALOGI("%s UART fd: %d", __func__, uart_fd_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700225
226 fd_watcher_.WatchFdForNonBlockingReads(uart_fd_,
227 [this](int fd) { OnDataReady(fd); });
228
229 // Start configuring the firmware
Andre Eisenbach9041d972017-01-17 18:23:12 -0800230 firmware_startup_timer_ = new FirmwareStartupTimer();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700231 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
232
233 return true;
234}
235
236void VendorInterface::Close() {
237 fd_watcher_.StopWatchingFileDescriptor();
238
239 if (lib_interface_ != nullptr) {
240 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
241 uart_fd_ = INVALID_FD;
242 int power_state = BT_VND_PWR_OFF;
243 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
244 }
245
246 if (lib_handle_ != nullptr) {
247 dlclose(lib_handle_);
248 lib_handle_ = nullptr;
249 }
250
Andre Eisenbach9041d972017-01-17 18:23:12 -0800251 if (firmware_startup_timer_ != nullptr) {
252 delete firmware_startup_timer_;
253 firmware_startup_timer_ = nullptr;
254 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700255}
256
257size_t VendorInterface::Send(const uint8_t* data, size_t length) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700258 if (uart_fd_ == INVALID_FD) return 0;
259
260 size_t transmitted_length = 0;
261 while (length > 0) {
262 ssize_t ret =
263 TEMP_FAILURE_RETRY(write(uart_fd_, data + transmitted_length, length));
264
265 if (ret == -1) {
266 if (errno == EAGAIN) continue;
267 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
268 break;
269
270 } else if (ret == 0) {
271 // Nothing written :(
272 ALOGE("%s zero bytes written - something went wrong...", __func__);
273 break;
274 }
275
276 transmitted_length += ret;
277 length -= ret;
278 }
279
280 return transmitted_length;
281}
282
283void VendorInterface::OnFirmwareConfigured(uint8_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800284 ALOGD("%s result: %d", __func__, result);
285 internal_command_cb = nullptr;
286
287 if (firmware_startup_timer_ != nullptr) {
288 delete firmware_startup_timer_;
289 firmware_startup_timer_ = nullptr;
290 }
291
292 if (initialize_complete_cb_ != nullptr) {
293 initialize_complete_cb_(result == 0);
294 initialize_complete_cb_ = nullptr;
295 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700296}
297
298void VendorInterface::OnDataReady(int fd) {
299 switch (hci_parser_state_) {
300 case HCI_IDLE: {
301 uint8_t buffer[1] = {0};
302 size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1));
303 CHECK(bytes_read == 1);
304 hci_packet_type_ = static_cast<HciPacketType>(buffer[0]);
305 // TODO(eisenbach): Check for workaround(s)
306 CHECK(hci_packet_type_ >= HCI_PACKET_TYPE_ACL_DATA &&
307 hci_packet_type_ <= HCI_PACKET_TYPE_EVENT)
Myles Watson8ffcbc72017-01-20 10:09:38 -0800308 << "buffer[0] = " << static_cast<unsigned int>(buffer[0]);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700309 hci_parser_state_ = HCI_TYPE_READY;
310 hci_packet_.resize(HCI_PREAMBLE_SIZE_MAX);
311 hci_packet_bytes_remaining_ = preamble_size_for_type[hci_packet_type_];
312 hci_packet_bytes_read_ = 0;
313 break;
314 }
315
316 case HCI_TYPE_READY: {
317 size_t bytes_read = TEMP_FAILURE_RETRY(
318 read(fd, hci_packet_.data() + hci_packet_bytes_read_,
319 hci_packet_bytes_remaining_));
320 CHECK(bytes_read > 0);
321 hci_packet_bytes_remaining_ -= bytes_read;
322 hci_packet_bytes_read_ += bytes_read;
323 if (hci_packet_bytes_remaining_ == 0) {
324 size_t packet_length =
325 HciGetPacketLengthForType(hci_packet_type_, hci_packet_);
326 hci_packet_.resize(preamble_size_for_type[hci_packet_type_] +
327 packet_length);
328 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