blob: 15b6c74532461dc75590e026e6d7a8dc20b62c8e [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
19#define LOG_TAG "android.hardware.bluetooth@1.0-impl"
Myles Watson6a7d6222016-10-13 15:45:02 -070020#include <cutils/properties.h>
Andre Eisenbach89ba5282016-10-13 15:45:02 -070021#include <utils/Log.h>
22
23#include <dlfcn.h>
Myles Watson6a7d6222016-10-13 15:45:02 -070024#include <fcntl.h>
25
26#include "bluetooth_address.h"
Zach Johnson917efb12017-02-26 23:46:05 -080027#include "h4_protocol.h"
28#include "mct_protocol.h"
Andre Eisenbach89ba5282016-10-13 15:45:02 -070029
30static const char* VENDOR_LIBRARY_NAME = "libbt-vendor.so";
31static const char* VENDOR_LIBRARY_SYMBOL_NAME =
32 "BLUETOOTH_VENDOR_LIB_INTERFACE";
33
34static const int INVALID_FD = -1;
35
36namespace {
37
38using android::hardware::bluetooth::V1_0::implementation::VendorInterface;
Andre Eisenbach9041d972017-01-17 18:23:12 -080039using android::hardware::hidl_vec;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070040
Myles Watson4e2e8ec2017-02-01 10:46:16 -080041struct {
42 tINT_CMD_CBACK cb;
43 uint16_t opcode;
44} internal_command;
Myles Watsona7d33b32017-01-24 09:09:58 -080045
Myles Watsonbeb13b42017-01-26 10:47:27 -080046// True when LPM is not enabled yet or wake is not asserted.
47bool lpm_wake_deasserted;
48uint32_t lpm_timeout_ms;
49bool recent_activity_flag;
50
Andre Eisenbach89ba5282016-10-13 15:45:02 -070051VendorInterface* g_vendor_interface = nullptr;
52
Andre Eisenbach9041d972017-01-17 18:23:12 -080053HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
Andre Eisenbach89ba5282016-10-13 15:45:02 -070054 size_t packet_size = data.size() + sizeof(HC_BT_HDR);
55 HC_BT_HDR* packet = reinterpret_cast<HC_BT_HDR*>(new uint8_t[packet_size]);
56 packet->offset = 0;
57 packet->len = data.size();
58 packet->layer_specific = 0;
59 packet->event = event;
Myles Watson6a7d6222016-10-13 15:45:02 -070060 // TODO(eisenbach): Avoid copy here; if BT_HDR->data can be ensured to
Andre Eisenbach89ba5282016-10-13 15:45:02 -070061 // be the only way the data is accessed, a pointer could be passed here...
62 memcpy(packet->data, data.data(), data.size());
63 return packet;
64}
65
Myles Watsona7d33b32017-01-24 09:09:58 -080066bool internal_command_event_match(const hidl_vec<uint8_t>& packet) {
67 uint8_t event_code = packet[0];
68 if (event_code != HCI_COMMAND_COMPLETE_EVENT) {
69 ALOGE("%s: Unhandled event type %02X", __func__, event_code);
70 return false;
71 }
72
73 size_t opcode_offset = HCI_EVENT_PREAMBLE_SIZE + 1; // Skip num packets.
74
75 uint16_t opcode = packet[opcode_offset] | (packet[opcode_offset + 1] << 8);
76
Myles Watson4e2e8ec2017-02-01 10:46:16 -080077 ALOGV("%s internal_command.opcode = %04X opcode = %04x", __func__,
78 internal_command.opcode, opcode);
79 return opcode == internal_command.opcode;
Myles Watsona7d33b32017-01-24 09:09:58 -080080}
81
Andre Eisenbach89ba5282016-10-13 15:45:02 -070082uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
Myles Watsona7d33b32017-01-24 09:09:58 -080083 ALOGV("%s opcode: 0x%04x, ptr: %p, cb: %p", __func__, opcode, buffer,
84 callback);
Myles Watson4e2e8ec2017-02-01 10:46:16 -080085 internal_command.cb = callback;
86 internal_command.opcode = opcode;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070087 uint8_t type = HCI_PACKET_TYPE_COMMAND;
Andre Eisenbach9041d972017-01-17 18:23:12 -080088 HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
Myles Watsondf765ea2017-01-30 09:07:37 -080089 VendorInterface::get()->Send(type, bt_hdr->data, bt_hdr->len);
Myles Watson4e2e8ec2017-02-01 10:46:16 -080090 delete[] reinterpret_cast<uint8_t*>(buffer);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070091 return true;
92}
93
94void firmware_config_cb(bt_vendor_op_result_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -080095 ALOGV("%s result: %d", __func__, result);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070096 VendorInterface::get()->OnFirmwareConfigured(result);
97}
98
99void sco_config_cb(bt_vendor_op_result_t result) {
100 ALOGD("%s result: %d", __func__, result);
101}
102
103void low_power_mode_cb(bt_vendor_op_result_t result) {
104 ALOGD("%s result: %d", __func__, result);
105}
106
107void sco_audiostate_cb(bt_vendor_op_result_t result) {
108 ALOGD("%s result: %d", __func__, result);
109}
110
111void* buffer_alloc_cb(int size) {
112 void* p = new uint8_t[size];
113 ALOGV("%s pts: %p, size: %d", __func__, p, size);
114 return p;
115}
116
117void buffer_free_cb(void* buffer) {
118 ALOGV("%s ptr: %p", __func__, buffer);
119 delete[] reinterpret_cast<uint8_t*>(buffer);
120}
121
122void epilog_cb(bt_vendor_op_result_t result) {
123 ALOGD("%s result: %d", __func__, result);
124}
125
126void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op,
127 uint8_t av_handle) {
128 ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle);
129}
130
131const bt_vendor_callbacks_t lib_callbacks = {
132 sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
133 low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
134 buffer_free_cb, transmit_cb, epilog_cb,
135 a2dp_offload_cb};
136
137} // namespace
138
139namespace android {
140namespace hardware {
141namespace bluetooth {
142namespace V1_0 {
143namespace implementation {
144
Andre Eisenbach9041d972017-01-17 18:23:12 -0800145class FirmwareStartupTimer {
146 public:
147 FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {}
148
149 ~FirmwareStartupTimer() {
150 std::chrono::duration<double> duration =
151 std::chrono::steady_clock::now() - start_time_;
152 double s = duration.count();
153 if (s == 0) return;
Myles Watson8ffcbc72017-01-20 10:09:38 -0800154 ALOGI("Firmware configured in %.3fs", s);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800155 }
156
157 private:
158 std::chrono::steady_clock::time_point start_time_;
159};
160
161bool VendorInterface::Initialize(
162 InitializeCompleteCallback initialize_complete_cb,
Zach Johnson917efb12017-02-26 23:46:05 -0800163 PacketReadCallback event_cb, PacketReadCallback acl_cb,
164 PacketReadCallback sco_cb) {
Myles Watson3e272a72017-06-26 13:09:11 -0700165 LOG_ALWAYS_FATAL_IF(g_vendor_interface, "%s: No previous Shutdown()?",
166 __func__);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700167 g_vendor_interface = new VendorInterface();
Zach Johnson917efb12017-02-26 23:46:05 -0800168 return g_vendor_interface->Open(initialize_complete_cb, event_cb, acl_cb,
169 sco_cb);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700170}
171
172void VendorInterface::Shutdown() {
Myles Watson3e272a72017-06-26 13:09:11 -0700173 LOG_ALWAYS_FATAL_IF(!g_vendor_interface, "%s: No Vendor interface!",
174 __func__);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700175 g_vendor_interface->Close();
176 delete g_vendor_interface;
177 g_vendor_interface = nullptr;
178}
179
180VendorInterface* VendorInterface::get() { return g_vendor_interface; }
181
Andre Eisenbach9041d972017-01-17 18:23:12 -0800182bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
Zach Johnson917efb12017-02-26 23:46:05 -0800183 PacketReadCallback event_cb,
184 PacketReadCallback acl_cb,
185 PacketReadCallback sco_cb) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800186 initialize_complete_cb_ = initialize_complete_cb;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700187
188 // Initialize vendor interface
189
190 lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
191 if (!lib_handle_) {
192 ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
193 dlerror());
194 return false;
195 }
196
197 lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
198 dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
199 if (!lib_interface_) {
200 ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
201 VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
202 return false;
203 }
204
Myles Watson6a7d6222016-10-13 15:45:02 -0700205 // Get the local BD address
206
207 uint8_t local_bda[BluetoothAddress::kBytes];
Myles Watson3e272a72017-06-26 13:09:11 -0700208 if (!BluetoothAddress::get_local_address(local_bda)) {
209 LOG_ALWAYS_FATAL("%s: No Bluetooth Address!", __func__);
210 }
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
Myles Watsonc0aee0c2017-03-13 12:35:25 -0700219 // Power on the controller
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700220
Myles Watsonc0aee0c2017-03-13 12:35:25 -0700221 int power_state = BT_VND_PWR_ON;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700222 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
223
Zach Johnson917efb12017-02-26 23:46:05 -0800224 // Get the UART socket(s)
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700225
226 int fd_list[CH_MAX] = {0};
227 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
228
Myles Watsonec1eda62017-08-08 16:00:30 -0700229 if (fd_count < 1 || fd_count > CH_MAX - 1) {
230 ALOGE("%s: fd_count %d is invalid!", __func__, fd_count);
231 return false;
232 }
233
Zach Johnson917efb12017-02-26 23:46:05 -0800234 for (int i = 0; i < fd_count; i++) {
235 if (fd_list[i] == INVALID_FD) {
236 ALOGE("%s: fd %d is invalid!", __func__, fd_list[i]);
237 return false;
238 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700239 }
240
Zach Johnson917efb12017-02-26 23:46:05 -0800241 event_cb_ = event_cb;
242 PacketReadCallback intercept_events = [this](const hidl_vec<uint8_t>& event) {
243 HandleIncomingEvent(event);
244 };
245
246 if (fd_count == 1) {
247 hci::H4Protocol* h4_hci =
248 new hci::H4Protocol(fd_list[0], intercept_events, acl_cb, sco_cb);
249 fd_watcher_.WatchFdForNonBlockingReads(
250 fd_list[0], [h4_hci](int fd) { h4_hci->OnDataReady(fd); });
251 hci_ = h4_hci;
252 } else {
253 hci::MctProtocol* mct_hci =
254 new hci::MctProtocol(fd_list, intercept_events, acl_cb);
255 fd_watcher_.WatchFdForNonBlockingReads(
256 fd_list[CH_EVT], [mct_hci](int fd) { mct_hci->OnEventDataReady(fd); });
Zach Johnsonfed25ad2017-03-15 13:23:54 -0700257 fd_watcher_.WatchFdForNonBlockingReads(
258 fd_list[CH_ACL_IN],
259 [mct_hci](int fd) { mct_hci->OnAclDataReady(fd); });
Zach Johnson917efb12017-02-26 23:46:05 -0800260 hci_ = mct_hci;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700261 }
262
Myles Watsonbeb13b42017-01-26 10:47:27 -0800263 // Initially, the power management is off.
Andre Eisenbachf60aeb42017-02-07 20:28:32 -0800264 lpm_wake_deasserted = true;
Myles Watsonbeb13b42017-01-26 10:47:27 -0800265
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700266 // Start configuring the firmware
Andre Eisenbach9041d972017-01-17 18:23:12 -0800267 firmware_startup_timer_ = new FirmwareStartupTimer();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700268 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
269
270 return true;
271}
272
273void VendorInterface::Close() {
Andre Eisenbach8a9efb62017-03-17 20:28:09 +0000274 // These callbacks may send HCI events (vendor-dependent), so make sure to
275 // StopWatching the file descriptor after this.
276 if (lib_interface_ != nullptr) {
277 bt_vendor_lpm_mode_t mode = BT_VND_LPM_DISABLE;
278 lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
279 }
280
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800281 fd_watcher_.StopWatchingFileDescriptors();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700282
Zach Johnson917efb12017-02-26 23:46:05 -0800283 if (hci_ != nullptr) {
284 delete hci_;
285 hci_ = nullptr;
286 }
287
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700288 if (lib_interface_ != nullptr) {
289 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
Myles Watson66a4ca32017-03-10 09:10:51 -0800290
291 int power_state = BT_VND_PWR_OFF;
292 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
Myles Watson9eee8302017-06-08 08:38:58 -0700293
294 lib_interface_->cleanup();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700295 }
296
297 if (lib_handle_ != nullptr) {
298 dlclose(lib_handle_);
299 lib_handle_ = nullptr;
300 }
301
Andre Eisenbach9041d972017-01-17 18:23:12 -0800302 if (firmware_startup_timer_ != nullptr) {
303 delete firmware_startup_timer_;
304 firmware_startup_timer_ = nullptr;
305 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700306}
307
Myles Watsondf765ea2017-01-30 09:07:37 -0800308size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) {
Myles Watsonbeb13b42017-01-26 10:47:27 -0800309 recent_activity_flag = true;
310
311 if (lpm_wake_deasserted == true) {
312 // Restart the timer.
313 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
314 [this]() { OnTimeout(); });
315 // Assert wake.
316 lpm_wake_deasserted = false;
317 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_ASSERT;
318 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
319 ALOGV("%s: Sent wake before (%02x)", __func__, data[0] | (data[1] << 8));
320 }
321
Zach Johnson917efb12017-02-26 23:46:05 -0800322 return hci_->Send(type, data, length);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700323}
324
325void VendorInterface::OnFirmwareConfigured(uint8_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800326 ALOGD("%s result: %d", __func__, result);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800327
328 if (firmware_startup_timer_ != nullptr) {
329 delete firmware_startup_timer_;
330 firmware_startup_timer_ = nullptr;
331 }
332
333 if (initialize_complete_cb_ != nullptr) {
334 initialize_complete_cb_(result == 0);
335 initialize_complete_cb_ = nullptr;
336 }
Myles Watsonbeb13b42017-01-26 10:47:27 -0800337
338 lib_interface_->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &lpm_timeout_ms);
339 ALOGI("%s: lpm_timeout_ms %d", __func__, lpm_timeout_ms);
340
341 bt_vendor_lpm_mode_t mode = BT_VND_LPM_ENABLE;
342 lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
343
344 ALOGD("%s Calling StartLowPowerWatchdog()", __func__);
345 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
346 [this]() { OnTimeout(); });
Myles Watsonbeb13b42017-01-26 10:47:27 -0800347}
348
349void VendorInterface::OnTimeout() {
350 ALOGV("%s", __func__);
351 if (recent_activity_flag == false) {
352 lpm_wake_deasserted = true;
353 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_DEASSERT;
354 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
355 fd_watcher_.ConfigureTimeout(std::chrono::seconds(0), []() {
356 ALOGE("Zero timeout! Should never happen.");
357 });
358 }
359 recent_activity_flag = false;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700360}
361
Zach Johnson917efb12017-02-26 23:46:05 -0800362void VendorInterface::HandleIncomingEvent(const hidl_vec<uint8_t>& hci_packet) {
363 if (internal_command.cb != nullptr &&
364 internal_command_event_match(hci_packet)) {
365 HC_BT_HDR* bt_hdr = WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700366
Zach Johnson917efb12017-02-26 23:46:05 -0800367 // The callbacks can send new commands, so don't zero after calling.
368 tINT_CMD_CBACK saved_cb = internal_command.cb;
369 internal_command.cb = nullptr;
370 saved_cb(bt_hdr);
371 } else {
372 event_cb_(hci_packet);
373 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700374}
375
376} // namespace implementation
377} // namespace V1_0
378} // namespace bluetooth
379} // namespace hardware
380} // namespace android