blob: a6507dd0d8ded87480f3686dc885a33da9fff498 [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"
Zach Johnson917efb12017-02-26 23:46:05 -080030#include "h4_protocol.h"
31#include "mct_protocol.h"
Andre Eisenbach89ba5282016-10-13 15:45:02 -070032
33static const char* VENDOR_LIBRARY_NAME = "libbt-vendor.so";
34static const char* VENDOR_LIBRARY_SYMBOL_NAME =
35 "BLUETOOTH_VENDOR_LIB_INTERFACE";
36
37static const int INVALID_FD = -1;
38
39namespace {
40
41using android::hardware::bluetooth::V1_0::implementation::VendorInterface;
Andre Eisenbach9041d972017-01-17 18:23:12 -080042using android::hardware::hidl_vec;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070043
Myles Watson4e2e8ec2017-02-01 10:46:16 -080044struct {
45 tINT_CMD_CBACK cb;
46 uint16_t opcode;
47} internal_command;
Myles Watsona7d33b32017-01-24 09:09:58 -080048
Myles Watsonbeb13b42017-01-26 10:47:27 -080049// True when LPM is not enabled yet or wake is not asserted.
50bool lpm_wake_deasserted;
51uint32_t lpm_timeout_ms;
52bool recent_activity_flag;
53
Andre Eisenbach89ba5282016-10-13 15:45:02 -070054VendorInterface* g_vendor_interface = nullptr;
55
Andre Eisenbach9041d972017-01-17 18:23:12 -080056HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070057 size_t packet_size = data.size() + sizeof(HC_BT_HDR);
58 HC_BT_HDR* packet = reinterpret_cast<HC_BT_HDR*>(new uint8_t[packet_size]);
59 packet->offset = 0;
60 packet->len = data.size();
61 packet->layer_specific = 0;
62 packet->event = event;
Myles Watson6a7d6222016-10-13 15:45:02 -070063 // TODO(eisenbach): Avoid copy here; if BT_HDR->data can be ensured to
Andre Eisenbach89ba5282016-10-13 15:45:02 -070064 // be the only way the data is accessed, a pointer could be passed here...
65 memcpy(packet->data, data.data(), data.size());
66 return packet;
67}
68
Myles Watsona7d33b32017-01-24 09:09:58 -080069bool internal_command_event_match(const hidl_vec<uint8_t>& packet) {
70 uint8_t event_code = packet[0];
71 if (event_code != HCI_COMMAND_COMPLETE_EVENT) {
72 ALOGE("%s: Unhandled event type %02X", __func__, event_code);
73 return false;
74 }
75
76 size_t opcode_offset = HCI_EVENT_PREAMBLE_SIZE + 1; // Skip num packets.
77
78 uint16_t opcode = packet[opcode_offset] | (packet[opcode_offset + 1] << 8);
79
Myles Watson4e2e8ec2017-02-01 10:46:16 -080080 ALOGV("%s internal_command.opcode = %04X opcode = %04x", __func__,
81 internal_command.opcode, opcode);
82 return opcode == internal_command.opcode;
Myles Watsona7d33b32017-01-24 09:09:58 -080083}
84
Andre Eisenbach89ba5282016-10-13 15:45:02 -070085uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
Myles Watsona7d33b32017-01-24 09:09:58 -080086 ALOGV("%s opcode: 0x%04x, ptr: %p, cb: %p", __func__, opcode, buffer,
87 callback);
Myles Watson4e2e8ec2017-02-01 10:46:16 -080088 internal_command.cb = callback;
89 internal_command.opcode = opcode;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070090 uint8_t type = HCI_PACKET_TYPE_COMMAND;
Andre Eisenbach9041d972017-01-17 18:23:12 -080091 HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
Myles Watsondf765ea2017-01-30 09:07:37 -080092 VendorInterface::get()->Send(type, bt_hdr->data, bt_hdr->len);
Myles Watson4e2e8ec2017-02-01 10:46:16 -080093 delete[] reinterpret_cast<uint8_t*>(buffer);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070094 return true;
95}
96
97void firmware_config_cb(bt_vendor_op_result_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -080098 ALOGV("%s result: %d", __func__, result);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070099 VendorInterface::get()->OnFirmwareConfigured(result);
100}
101
102void sco_config_cb(bt_vendor_op_result_t result) {
103 ALOGD("%s result: %d", __func__, result);
104}
105
106void low_power_mode_cb(bt_vendor_op_result_t result) {
107 ALOGD("%s result: %d", __func__, result);
108}
109
110void sco_audiostate_cb(bt_vendor_op_result_t result) {
111 ALOGD("%s result: %d", __func__, result);
112}
113
114void* buffer_alloc_cb(int size) {
115 void* p = new uint8_t[size];
116 ALOGV("%s pts: %p, size: %d", __func__, p, size);
117 return p;
118}
119
120void buffer_free_cb(void* buffer) {
121 ALOGV("%s ptr: %p", __func__, buffer);
122 delete[] reinterpret_cast<uint8_t*>(buffer);
123}
124
125void epilog_cb(bt_vendor_op_result_t result) {
126 ALOGD("%s result: %d", __func__, result);
127}
128
129void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op,
130 uint8_t av_handle) {
131 ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle);
132}
133
134const bt_vendor_callbacks_t lib_callbacks = {
135 sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
136 low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
137 buffer_free_cb, transmit_cb, epilog_cb,
138 a2dp_offload_cb};
139
140} // namespace
141
142namespace android {
143namespace hardware {
144namespace bluetooth {
145namespace V1_0 {
146namespace implementation {
147
Andre Eisenbach9041d972017-01-17 18:23:12 -0800148class FirmwareStartupTimer {
149 public:
150 FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {}
151
152 ~FirmwareStartupTimer() {
153 std::chrono::duration<double> duration =
154 std::chrono::steady_clock::now() - start_time_;
155 double s = duration.count();
156 if (s == 0) return;
Myles Watson8ffcbc72017-01-20 10:09:38 -0800157 ALOGI("Firmware configured in %.3fs", s);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800158 }
159
160 private:
161 std::chrono::steady_clock::time_point start_time_;
162};
163
164bool VendorInterface::Initialize(
165 InitializeCompleteCallback initialize_complete_cb,
Zach Johnson917efb12017-02-26 23:46:05 -0800166 PacketReadCallback event_cb, PacketReadCallback acl_cb,
167 PacketReadCallback sco_cb) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700168 assert(!g_vendor_interface);
169 g_vendor_interface = new VendorInterface();
Zach Johnson917efb12017-02-26 23:46:05 -0800170 return g_vendor_interface->Open(initialize_complete_cb, event_cb, acl_cb,
171 sco_cb);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700172}
173
174void VendorInterface::Shutdown() {
175 CHECK(g_vendor_interface);
176 g_vendor_interface->Close();
177 delete g_vendor_interface;
178 g_vendor_interface = nullptr;
179}
180
181VendorInterface* VendorInterface::get() { return g_vendor_interface; }
182
Andre Eisenbach9041d972017-01-17 18:23:12 -0800183bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
Zach Johnson917efb12017-02-26 23:46:05 -0800184 PacketReadCallback event_cb,
185 PacketReadCallback acl_cb,
186 PacketReadCallback sco_cb) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800187 initialize_complete_cb_ = initialize_complete_cb;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700188
189 // Initialize vendor interface
190
191 lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
192 if (!lib_handle_) {
193 ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
194 dlerror());
195 return false;
196 }
197
198 lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
199 dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
200 if (!lib_interface_) {
201 ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
202 VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
203 return false;
204 }
205
Myles Watson6a7d6222016-10-13 15:45:02 -0700206 // Get the local BD address
207
208 uint8_t local_bda[BluetoothAddress::kBytes];
209 CHECK(BluetoothAddress::get_local_address(local_bda));
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700210 int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
211 if (status) {
212 ALOGE("%s unable to initialize vendor library: %d", __func__, status);
213 return false;
214 }
215
216 ALOGD("%s vendor library loaded", __func__);
217
218 // Power cycle chip
219
220 int power_state = BT_VND_PWR_OFF;
221 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
222 power_state = BT_VND_PWR_ON;
223 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
224
Zach Johnson917efb12017-02-26 23:46:05 -0800225 // Get the UART socket(s)
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700226
227 int fd_list[CH_MAX] = {0};
228 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
229
Zach Johnson917efb12017-02-26 23:46:05 -0800230 for (int i = 0; i < fd_count; i++) {
231 if (fd_list[i] == INVALID_FD) {
232 ALOGE("%s: fd %d is invalid!", __func__, fd_list[i]);
233 return false;
234 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700235 }
236
Zach Johnson917efb12017-02-26 23:46:05 -0800237 event_cb_ = event_cb;
238 PacketReadCallback intercept_events = [this](const hidl_vec<uint8_t>& event) {
239 HandleIncomingEvent(event);
240 };
241
242 if (fd_count == 1) {
243 hci::H4Protocol* h4_hci =
244 new hci::H4Protocol(fd_list[0], intercept_events, acl_cb, sco_cb);
245 fd_watcher_.WatchFdForNonBlockingReads(
246 fd_list[0], [h4_hci](int fd) { h4_hci->OnDataReady(fd); });
247 hci_ = h4_hci;
248 } else {
249 hci::MctProtocol* mct_hci =
250 new hci::MctProtocol(fd_list, intercept_events, acl_cb);
251 fd_watcher_.WatchFdForNonBlockingReads(
252 fd_list[CH_EVT], [mct_hci](int fd) { mct_hci->OnEventDataReady(fd); });
253 if (fd_count >= CH_ACL_IN)
254 fd_watcher_.WatchFdForNonBlockingReads(
255 fd_list[CH_ACL_IN],
256 [mct_hci](int fd) { mct_hci->OnAclDataReady(fd); });
257 hci_ = mct_hci;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700258 }
259
Myles Watsonbeb13b42017-01-26 10:47:27 -0800260 // Initially, the power management is off.
Andre Eisenbachf60aeb42017-02-07 20:28:32 -0800261 lpm_wake_deasserted = true;
Myles Watsonbeb13b42017-01-26 10:47:27 -0800262
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700263 // Start configuring the firmware
Andre Eisenbach9041d972017-01-17 18:23:12 -0800264 firmware_startup_timer_ = new FirmwareStartupTimer();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700265 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
266
267 return true;
268}
269
270void VendorInterface::Close() {
Myles Watson9ef1f712017-03-09 10:39:31 -0800271 // These callbacks may send HCI events (vendor-dependent), so make sure to
272 // StopWatching the file descriptor after this.
273 if (lib_interface_ != nullptr) {
274 bt_vendor_lpm_mode_t mode = BT_VND_LPM_DISABLE;
275 lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
Myles Watson9ef1f712017-03-09 10:39:31 -0800276 }
277
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800278 fd_watcher_.StopWatchingFileDescriptors();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700279
Zach Johnson917efb12017-02-26 23:46:05 -0800280 if (hci_ != nullptr) {
281 delete hci_;
282 hci_ = nullptr;
283 }
284
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700285 if (lib_interface_ != nullptr) {
286 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
Myles Watson66a4ca32017-03-10 09:10:51 -0800287
288 int power_state = BT_VND_PWR_OFF;
289 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700290 }
291
292 if (lib_handle_ != nullptr) {
293 dlclose(lib_handle_);
294 lib_handle_ = nullptr;
295 }
296
Andre Eisenbach9041d972017-01-17 18:23:12 -0800297 if (firmware_startup_timer_ != nullptr) {
298 delete firmware_startup_timer_;
299 firmware_startup_timer_ = nullptr;
300 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700301}
302
Myles Watsondf765ea2017-01-30 09:07:37 -0800303size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) {
Myles Watsonbeb13b42017-01-26 10:47:27 -0800304 recent_activity_flag = true;
305
306 if (lpm_wake_deasserted == true) {
307 // Restart the timer.
308 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
309 [this]() { OnTimeout(); });
310 // Assert wake.
311 lpm_wake_deasserted = false;
312 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_ASSERT;
313 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
314 ALOGV("%s: Sent wake before (%02x)", __func__, data[0] | (data[1] << 8));
315 }
316
Zach Johnson917efb12017-02-26 23:46:05 -0800317 return hci_->Send(type, data, length);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700318}
319
320void VendorInterface::OnFirmwareConfigured(uint8_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800321 ALOGD("%s result: %d", __func__, result);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800322
323 if (firmware_startup_timer_ != nullptr) {
324 delete firmware_startup_timer_;
325 firmware_startup_timer_ = nullptr;
326 }
327
328 if (initialize_complete_cb_ != nullptr) {
329 initialize_complete_cb_(result == 0);
330 initialize_complete_cb_ = nullptr;
331 }
Myles Watsonbeb13b42017-01-26 10:47:27 -0800332
333 lib_interface_->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &lpm_timeout_ms);
334 ALOGI("%s: lpm_timeout_ms %d", __func__, lpm_timeout_ms);
335
336 bt_vendor_lpm_mode_t mode = BT_VND_LPM_ENABLE;
337 lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
338
339 ALOGD("%s Calling StartLowPowerWatchdog()", __func__);
340 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
341 [this]() { OnTimeout(); });
Myles Watsonbeb13b42017-01-26 10:47:27 -0800342}
343
344void VendorInterface::OnTimeout() {
345 ALOGV("%s", __func__);
346 if (recent_activity_flag == false) {
347 lpm_wake_deasserted = true;
348 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_DEASSERT;
349 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
350 fd_watcher_.ConfigureTimeout(std::chrono::seconds(0), []() {
351 ALOGE("Zero timeout! Should never happen.");
352 });
353 }
354 recent_activity_flag = false;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700355}
356
Zach Johnson917efb12017-02-26 23:46:05 -0800357void VendorInterface::HandleIncomingEvent(const hidl_vec<uint8_t>& hci_packet) {
358 if (internal_command.cb != nullptr &&
359 internal_command_event_match(hci_packet)) {
360 HC_BT_HDR* bt_hdr = WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700361
Zach Johnson917efb12017-02-26 23:46:05 -0800362 // The callbacks can send new commands, so don't zero after calling.
363 tINT_CMD_CBACK saved_cb = internal_command.cb;
364 internal_command.cb = nullptr;
365 saved_cb(bt_hdr);
366 } else {
367 event_cb_(hci_packet);
368 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700369}
370
371} // namespace implementation
372} // namespace V1_0
373} // namespace bluetooth
374} // namespace hardware
375} // namespace android