blob: 2c55d1ce1bddb075f536b8de58700499a144b2a4 [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
Myles Watsonbeb13b42017-01-26 10:47:27 -080047// True when LPM is not enabled yet or wake is not asserted.
48bool lpm_wake_deasserted;
49uint32_t lpm_timeout_ms;
50bool recent_activity_flag;
51
Andre Eisenbach89ba5282016-10-13 15:45:02 -070052VendorInterface* g_vendor_interface = nullptr;
53
Andre Eisenbach9041d972017-01-17 18:23:12 -080054HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070055 size_t packet_size = data.size() + sizeof(HC_BT_HDR);
56 HC_BT_HDR* packet = reinterpret_cast<HC_BT_HDR*>(new uint8_t[packet_size]);
57 packet->offset = 0;
58 packet->len = data.size();
59 packet->layer_specific = 0;
60 packet->event = event;
Myles Watson6a7d6222016-10-13 15:45:02 -070061 // TODO(eisenbach): Avoid copy here; if BT_HDR->data can be ensured to
Andre Eisenbach89ba5282016-10-13 15:45:02 -070062 // be the only way the data is accessed, a pointer could be passed here...
63 memcpy(packet->data, data.data(), data.size());
64 return packet;
65}
66
Myles Watsondf765ea2017-01-30 09:07:37 -080067size_t write_safely(int fd, const uint8_t* data, size_t length) {
68 size_t transmitted_length = 0;
69 while (length > 0) {
70 ssize_t ret =
71 TEMP_FAILURE_RETRY(write(fd, data + transmitted_length, length));
72
73 if (ret == -1) {
74 if (errno == EAGAIN) continue;
75 ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
76 break;
77
78 } else if (ret == 0) {
79 // Nothing written :(
80 ALOGE("%s zero bytes written - something went wrong...", __func__);
81 break;
82 }
83
84 transmitted_length += ret;
85 length -= ret;
86 }
87
88 return transmitted_length;
89}
90
Myles Watsona7d33b32017-01-24 09:09:58 -080091bool internal_command_event_match(const hidl_vec<uint8_t>& packet) {
92 uint8_t event_code = packet[0];
93 if (event_code != HCI_COMMAND_COMPLETE_EVENT) {
94 ALOGE("%s: Unhandled event type %02X", __func__, event_code);
95 return false;
96 }
97
98 size_t opcode_offset = HCI_EVENT_PREAMBLE_SIZE + 1; // Skip num packets.
99
100 uint16_t opcode = packet[opcode_offset] | (packet[opcode_offset + 1] << 8);
101
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800102 ALOGV("%s internal_command.opcode = %04X opcode = %04x", __func__,
103 internal_command.opcode, opcode);
104 return opcode == internal_command.opcode;
Myles Watsona7d33b32017-01-24 09:09:58 -0800105}
106
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700107uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
Myles Watsona7d33b32017-01-24 09:09:58 -0800108 ALOGV("%s opcode: 0x%04x, ptr: %p, cb: %p", __func__, opcode, buffer,
109 callback);
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800110 internal_command.cb = callback;
111 internal_command.opcode = opcode;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700112 uint8_t type = HCI_PACKET_TYPE_COMMAND;
Andre Eisenbach9041d972017-01-17 18:23:12 -0800113 HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
Myles Watsondf765ea2017-01-30 09:07:37 -0800114 VendorInterface::get()->Send(type, bt_hdr->data, bt_hdr->len);
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800115 delete[] reinterpret_cast<uint8_t*>(buffer);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700116 return true;
117}
118
119void firmware_config_cb(bt_vendor_op_result_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800120 ALOGV("%s result: %d", __func__, result);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700121 VendorInterface::get()->OnFirmwareConfigured(result);
122}
123
124void sco_config_cb(bt_vendor_op_result_t result) {
125 ALOGD("%s result: %d", __func__, result);
126}
127
128void low_power_mode_cb(bt_vendor_op_result_t result) {
129 ALOGD("%s result: %d", __func__, result);
130}
131
132void sco_audiostate_cb(bt_vendor_op_result_t result) {
133 ALOGD("%s result: %d", __func__, result);
134}
135
136void* buffer_alloc_cb(int size) {
137 void* p = new uint8_t[size];
138 ALOGV("%s pts: %p, size: %d", __func__, p, size);
139 return p;
140}
141
142void buffer_free_cb(void* buffer) {
143 ALOGV("%s ptr: %p", __func__, buffer);
144 delete[] reinterpret_cast<uint8_t*>(buffer);
145}
146
147void epilog_cb(bt_vendor_op_result_t result) {
148 ALOGD("%s result: %d", __func__, result);
149}
150
151void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op,
152 uint8_t av_handle) {
153 ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle);
154}
155
156const bt_vendor_callbacks_t lib_callbacks = {
157 sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
158 low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
159 buffer_free_cb, transmit_cb, epilog_cb,
160 a2dp_offload_cb};
161
162} // namespace
163
164namespace android {
165namespace hardware {
166namespace bluetooth {
167namespace V1_0 {
168namespace implementation {
169
Andre Eisenbach9041d972017-01-17 18:23:12 -0800170class FirmwareStartupTimer {
171 public:
172 FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {}
173
174 ~FirmwareStartupTimer() {
175 std::chrono::duration<double> duration =
176 std::chrono::steady_clock::now() - start_time_;
177 double s = duration.count();
178 if (s == 0) return;
Myles Watson8ffcbc72017-01-20 10:09:38 -0800179 ALOGI("Firmware configured in %.3fs", s);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800180 }
181
182 private:
183 std::chrono::steady_clock::time_point start_time_;
184};
185
186bool VendorInterface::Initialize(
187 InitializeCompleteCallback initialize_complete_cb,
188 PacketReadCallback packet_read_cb) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700189 assert(!g_vendor_interface);
190 g_vendor_interface = new VendorInterface();
Andre Eisenbach9041d972017-01-17 18:23:12 -0800191 return g_vendor_interface->Open(initialize_complete_cb, packet_read_cb);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700192}
193
194void VendorInterface::Shutdown() {
195 CHECK(g_vendor_interface);
196 g_vendor_interface->Close();
197 delete g_vendor_interface;
198 g_vendor_interface = nullptr;
199}
200
201VendorInterface* VendorInterface::get() { return g_vendor_interface; }
202
Andre Eisenbach9041d972017-01-17 18:23:12 -0800203bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
204 PacketReadCallback packet_read_cb) {
205 initialize_complete_cb_ = initialize_complete_cb;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700206 packet_read_cb_ = packet_read_cb;
207
208 // Initialize vendor interface
209
210 lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
211 if (!lib_handle_) {
212 ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
213 dlerror());
214 return false;
215 }
216
217 lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
218 dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
219 if (!lib_interface_) {
220 ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
221 VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
222 return false;
223 }
224
Myles Watson6a7d6222016-10-13 15:45:02 -0700225 // Get the local BD address
226
227 uint8_t local_bda[BluetoothAddress::kBytes];
228 CHECK(BluetoothAddress::get_local_address(local_bda));
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700229 int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
230 if (status) {
231 ALOGE("%s unable to initialize vendor library: %d", __func__, status);
232 return false;
233 }
234
235 ALOGD("%s vendor library loaded", __func__);
236
237 // Power cycle chip
238
239 int power_state = BT_VND_PWR_OFF;
240 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
241 power_state = BT_VND_PWR_ON;
242 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
243
244 // Get the UART socket
245
246 int fd_list[CH_MAX] = {0};
247 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
248
249 if (fd_count != 1) {
250 ALOGE("%s fd count %d != 1; we can't handle this currently...", __func__,
251 fd_count);
252 return false;
253 }
254
255 uart_fd_ = fd_list[0];
256 if (uart_fd_ == INVALID_FD) {
257 ALOGE("%s unable to determine UART fd", __func__);
258 return false;
259 }
260
Myles Watson8ffcbc72017-01-20 10:09:38 -0800261 ALOGI("%s UART fd: %d", __func__, uart_fd_);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700262
263 fd_watcher_.WatchFdForNonBlockingReads(uart_fd_,
Myles Watson274a3812017-02-23 06:29:08 -0800264 [this](int fd) { hci_packetizer_.OnDataReady(fd); });
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700265
Myles Watsonbeb13b42017-01-26 10:47:27 -0800266 // Initially, the power management is off.
Andre Eisenbachf60aeb42017-02-07 20:28:32 -0800267 lpm_wake_deasserted = true;
Myles Watsonbeb13b42017-01-26 10:47:27 -0800268
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700269 // Start configuring the firmware
Andre Eisenbach9041d972017-01-17 18:23:12 -0800270 firmware_startup_timer_ = new FirmwareStartupTimer();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700271 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
272
273 return true;
274}
275
276void VendorInterface::Close() {
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800277 fd_watcher_.StopWatchingFileDescriptors();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700278
279 if (lib_interface_ != nullptr) {
Andre Eisenbachf60aeb42017-02-07 20:28:32 -0800280 bt_vendor_lpm_mode_t mode = BT_VND_LPM_DISABLE;
281 lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
282
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700283 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
284 uart_fd_ = INVALID_FD;
285 int power_state = BT_VND_PWR_OFF;
286 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
287 }
288
289 if (lib_handle_ != nullptr) {
290 dlclose(lib_handle_);
291 lib_handle_ = nullptr;
292 }
293
Andre Eisenbach9041d972017-01-17 18:23:12 -0800294 if (firmware_startup_timer_ != nullptr) {
295 delete firmware_startup_timer_;
296 firmware_startup_timer_ = nullptr;
297 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700298}
299
Myles Watsondf765ea2017-01-30 09:07:37 -0800300size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700301 if (uart_fd_ == INVALID_FD) return 0;
302
Myles Watsonbeb13b42017-01-26 10:47:27 -0800303 recent_activity_flag = true;
304
305 if (lpm_wake_deasserted == true) {
306 // Restart the timer.
307 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
308 [this]() { OnTimeout(); });
309 // Assert wake.
310 lpm_wake_deasserted = false;
311 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_ASSERT;
312 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
313 ALOGV("%s: Sent wake before (%02x)", __func__, data[0] | (data[1] << 8));
314 }
315
Myles Watsondf765ea2017-01-30 09:07:37 -0800316 int rv = write_safely(uart_fd_, &type, sizeof(type));
317 if (rv == sizeof(type))
318 rv = write_safely(uart_fd_, data, length);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700319
Myles Watsondf765ea2017-01-30 09:07:37 -0800320 return rv;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700321}
322
323void VendorInterface::OnFirmwareConfigured(uint8_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800324 ALOGD("%s result: %d", __func__, result);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800325
326 if (firmware_startup_timer_ != nullptr) {
327 delete firmware_startup_timer_;
328 firmware_startup_timer_ = nullptr;
329 }
330
331 if (initialize_complete_cb_ != nullptr) {
332 initialize_complete_cb_(result == 0);
333 initialize_complete_cb_ = nullptr;
334 }
Myles Watsonbeb13b42017-01-26 10:47:27 -0800335
336 lib_interface_->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &lpm_timeout_ms);
337 ALOGI("%s: lpm_timeout_ms %d", __func__, lpm_timeout_ms);
338
339 bt_vendor_lpm_mode_t mode = BT_VND_LPM_ENABLE;
340 lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
341
342 ALOGD("%s Calling StartLowPowerWatchdog()", __func__);
343 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
344 [this]() { OnTimeout(); });
Myles Watsonbeb13b42017-01-26 10:47:27 -0800345}
346
347void VendorInterface::OnTimeout() {
348 ALOGV("%s", __func__);
349 if (recent_activity_flag == false) {
350 lpm_wake_deasserted = true;
351 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_DEASSERT;
352 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
353 fd_watcher_.ConfigureTimeout(std::chrono::seconds(0), []() {
354 ALOGE("Zero timeout! Should never happen.");
355 });
356 }
357 recent_activity_flag = false;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700358}
359
Myles Watson274a3812017-02-23 06:29:08 -0800360void VendorInterface::OnPacketReady() {
361 VendorInterface::get()->HandleIncomingPacket();
362}
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700363
Myles Watson274a3812017-02-23 06:29:08 -0800364void VendorInterface::HandleIncomingPacket() {
365 HciPacketType hci_packet_type = hci_packetizer_.GetPacketType();
366 hidl_vec<uint8_t> hci_packet = hci_packetizer_.GetPacket();
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800367 if (internal_command.cb != nullptr &&
Myles Watson274a3812017-02-23 06:29:08 -0800368 hci_packet_type == HCI_PACKET_TYPE_EVENT &&
369 internal_command_event_match(hci_packet)) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800370 HC_BT_HDR* bt_hdr =
Myles Watson274a3812017-02-23 06:29:08 -0800371 WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet);
Myles Watsona7d33b32017-01-24 09:09:58 -0800372
373 // The callbacks can send new commands, so don't zero after calling.
Myles Watson4e2e8ec2017-02-01 10:46:16 -0800374 tINT_CMD_CBACK saved_cb = internal_command.cb;
375 internal_command.cb = nullptr;
Myles Watsona7d33b32017-01-24 09:09:58 -0800376 saved_cb(bt_hdr);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700377 } else {
Myles Watson274a3812017-02-23 06:29:08 -0800378 packet_read_cb_(hci_packet_type, hci_packet);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700379 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700380}
381
382} // namespace implementation
383} // namespace V1_0
384} // namespace bluetooth
385} // namespace hardware
386} // namespace android