blob: 57ea1a3ee8ba752adbb708197e88f558a46a15df [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
Myles Watsonc0aee0c2017-03-13 12:35:25 -0700218 // Power on the controller
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700219
Myles Watsonc0aee0c2017-03-13 12:35:25 -0700220 int power_state = BT_VND_PWR_ON;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700221 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
222
Zach Johnson917efb12017-02-26 23:46:05 -0800223 // Get the UART socket(s)
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700224
225 int fd_list[CH_MAX] = {0};
226 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
227
Zach Johnson917efb12017-02-26 23:46:05 -0800228 for (int i = 0; i < fd_count; i++) {
229 if (fd_list[i] == INVALID_FD) {
230 ALOGE("%s: fd %d is invalid!", __func__, fd_list[i]);
231 return false;
232 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700233 }
234
Zach Johnson917efb12017-02-26 23:46:05 -0800235 event_cb_ = event_cb;
236 PacketReadCallback intercept_events = [this](const hidl_vec<uint8_t>& event) {
237 HandleIncomingEvent(event);
238 };
239
240 if (fd_count == 1) {
241 hci::H4Protocol* h4_hci =
242 new hci::H4Protocol(fd_list[0], intercept_events, acl_cb, sco_cb);
243 fd_watcher_.WatchFdForNonBlockingReads(
244 fd_list[0], [h4_hci](int fd) { h4_hci->OnDataReady(fd); });
245 hci_ = h4_hci;
246 } else {
247 hci::MctProtocol* mct_hci =
248 new hci::MctProtocol(fd_list, intercept_events, acl_cb);
249 fd_watcher_.WatchFdForNonBlockingReads(
250 fd_list[CH_EVT], [mct_hci](int fd) { mct_hci->OnEventDataReady(fd); });
251 if (fd_count >= CH_ACL_IN)
252 fd_watcher_.WatchFdForNonBlockingReads(
253 fd_list[CH_ACL_IN],
254 [mct_hci](int fd) { mct_hci->OnAclDataReady(fd); });
255 hci_ = mct_hci;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700256 }
257
Myles Watsonbeb13b42017-01-26 10:47:27 -0800258 // Initially, the power management is off.
Andre Eisenbachf60aeb42017-02-07 20:28:32 -0800259 lpm_wake_deasserted = true;
Myles Watsonbeb13b42017-01-26 10:47:27 -0800260
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700261 // Start configuring the firmware
Andre Eisenbach9041d972017-01-17 18:23:12 -0800262 firmware_startup_timer_ = new FirmwareStartupTimer();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700263 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
264
265 return true;
266}
267
268void VendorInterface::Close() {
Myles Watson9ef1f712017-03-09 10:39:31 -0800269 // These callbacks may send HCI events (vendor-dependent), so make sure to
270 // StopWatching the file descriptor after this.
271 if (lib_interface_ != nullptr) {
272 bt_vendor_lpm_mode_t mode = BT_VND_LPM_DISABLE;
273 lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
Myles Watson9ef1f712017-03-09 10:39:31 -0800274 }
275
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800276 fd_watcher_.StopWatchingFileDescriptors();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700277
Zach Johnson917efb12017-02-26 23:46:05 -0800278 if (hci_ != nullptr) {
279 delete hci_;
280 hci_ = nullptr;
281 }
282
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700283 if (lib_interface_ != nullptr) {
284 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
Myles Watson66a4ca32017-03-10 09:10:51 -0800285
286 int power_state = BT_VND_PWR_OFF;
287 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700288 }
289
290 if (lib_handle_ != nullptr) {
291 dlclose(lib_handle_);
292 lib_handle_ = nullptr;
293 }
294
Andre Eisenbach9041d972017-01-17 18:23:12 -0800295 if (firmware_startup_timer_ != nullptr) {
296 delete firmware_startup_timer_;
297 firmware_startup_timer_ = nullptr;
298 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700299}
300
Myles Watsondf765ea2017-01-30 09:07:37 -0800301size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) {
Myles Watsonbeb13b42017-01-26 10:47:27 -0800302 recent_activity_flag = true;
303
304 if (lpm_wake_deasserted == true) {
305 // Restart the timer.
306 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
307 [this]() { OnTimeout(); });
308 // Assert wake.
309 lpm_wake_deasserted = false;
310 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_ASSERT;
311 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
312 ALOGV("%s: Sent wake before (%02x)", __func__, data[0] | (data[1] << 8));
313 }
314
Zach Johnson917efb12017-02-26 23:46:05 -0800315 return hci_->Send(type, data, length);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700316}
317
318void VendorInterface::OnFirmwareConfigured(uint8_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800319 ALOGD("%s result: %d", __func__, result);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800320
321 if (firmware_startup_timer_ != nullptr) {
322 delete firmware_startup_timer_;
323 firmware_startup_timer_ = nullptr;
324 }
325
326 if (initialize_complete_cb_ != nullptr) {
327 initialize_complete_cb_(result == 0);
328 initialize_complete_cb_ = nullptr;
329 }
Myles Watsonbeb13b42017-01-26 10:47:27 -0800330
331 lib_interface_->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &lpm_timeout_ms);
332 ALOGI("%s: lpm_timeout_ms %d", __func__, lpm_timeout_ms);
333
334 bt_vendor_lpm_mode_t mode = BT_VND_LPM_ENABLE;
335 lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
336
337 ALOGD("%s Calling StartLowPowerWatchdog()", __func__);
338 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
339 [this]() { OnTimeout(); });
Myles Watsonbeb13b42017-01-26 10:47:27 -0800340}
341
342void VendorInterface::OnTimeout() {
343 ALOGV("%s", __func__);
344 if (recent_activity_flag == false) {
345 lpm_wake_deasserted = true;
346 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_DEASSERT;
347 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
348 fd_watcher_.ConfigureTimeout(std::chrono::seconds(0), []() {
349 ALOGE("Zero timeout! Should never happen.");
350 });
351 }
352 recent_activity_flag = false;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700353}
354
Zach Johnson917efb12017-02-26 23:46:05 -0800355void VendorInterface::HandleIncomingEvent(const hidl_vec<uint8_t>& hci_packet) {
356 if (internal_command.cb != nullptr &&
357 internal_command_event_match(hci_packet)) {
358 HC_BT_HDR* bt_hdr = WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700359
Zach Johnson917efb12017-02-26 23:46:05 -0800360 // The callbacks can send new commands, so don't zero after calling.
361 tINT_CMD_CBACK saved_cb = internal_command.cb;
362 internal_command.cb = nullptr;
363 saved_cb(bt_hdr);
364 } else {
365 event_cb_(hci_packet);
366 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700367}
368
369} // namespace implementation
370} // namespace V1_0
371} // namespace bluetooth
372} // namespace hardware
373} // namespace android