blob: a291e149e134dd84d821e8f3cc96168d0e52ce90 [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"
20#include <android-base/logging.h>
Myles Watson6a7d6222016-10-13 15:45:02 -070021#include <cutils/properties.h>
Andre Eisenbach89ba5282016-10-13 15:45:02 -070022#include <utils/Log.h>
23
24#include <dlfcn.h>
Myles Watson6a7d6222016-10-13 15:45:02 -070025#include <fcntl.h>
26
27#include "bluetooth_address.h"
Zach Johnson917efb12017-02-26 23:46:05 -080028#include "h4_protocol.h"
29#include "mct_protocol.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 Watsona7d33b32017-01-24 09:09:58 -080067bool internal_command_event_match(const hidl_vec<uint8_t>& packet) {
68 uint8_t event_code = packet[0];
69 if (event_code != HCI_COMMAND_COMPLETE_EVENT) {
70 ALOGE("%s: Unhandled event type %02X", __func__, event_code);
71 return false;
72 }
73
74 size_t opcode_offset = HCI_EVENT_PREAMBLE_SIZE + 1; // Skip num packets.
75
76 uint16_t opcode = packet[opcode_offset] | (packet[opcode_offset + 1] << 8);
77
Myles Watson4e2e8ec2017-02-01 10:46:16 -080078 ALOGV("%s internal_command.opcode = %04X opcode = %04x", __func__,
79 internal_command.opcode, opcode);
80 return opcode == internal_command.opcode;
Myles Watsona7d33b32017-01-24 09:09:58 -080081}
82
Andre Eisenbach89ba5282016-10-13 15:45:02 -070083uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) {
Myles Watsona7d33b32017-01-24 09:09:58 -080084 ALOGV("%s opcode: 0x%04x, ptr: %p, cb: %p", __func__, opcode, buffer,
85 callback);
Myles Watson4e2e8ec2017-02-01 10:46:16 -080086 internal_command.cb = callback;
87 internal_command.opcode = opcode;
Andre Eisenbach89ba5282016-10-13 15:45:02 -070088 uint8_t type = HCI_PACKET_TYPE_COMMAND;
Andre Eisenbach9041d972017-01-17 18:23:12 -080089 HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer);
Myles Watsondf765ea2017-01-30 09:07:37 -080090 VendorInterface::get()->Send(type, bt_hdr->data, bt_hdr->len);
Myles Watson4e2e8ec2017-02-01 10:46:16 -080091 delete[] reinterpret_cast<uint8_t*>(buffer);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070092 return true;
93}
94
95void firmware_config_cb(bt_vendor_op_result_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -080096 ALOGV("%s result: %d", __func__, result);
Andre Eisenbach89ba5282016-10-13 15:45:02 -070097 VendorInterface::get()->OnFirmwareConfigured(result);
98}
99
100void sco_config_cb(bt_vendor_op_result_t result) {
101 ALOGD("%s result: %d", __func__, result);
102}
103
104void low_power_mode_cb(bt_vendor_op_result_t result) {
105 ALOGD("%s result: %d", __func__, result);
106}
107
108void sco_audiostate_cb(bt_vendor_op_result_t result) {
109 ALOGD("%s result: %d", __func__, result);
110}
111
112void* buffer_alloc_cb(int size) {
113 void* p = new uint8_t[size];
114 ALOGV("%s pts: %p, size: %d", __func__, p, size);
115 return p;
116}
117
118void buffer_free_cb(void* buffer) {
119 ALOGV("%s ptr: %p", __func__, buffer);
120 delete[] reinterpret_cast<uint8_t*>(buffer);
121}
122
123void epilog_cb(bt_vendor_op_result_t result) {
124 ALOGD("%s result: %d", __func__, result);
125}
126
127void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op,
128 uint8_t av_handle) {
129 ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle);
130}
131
132const bt_vendor_callbacks_t lib_callbacks = {
133 sizeof(lib_callbacks), firmware_config_cb, sco_config_cb,
134 low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb,
135 buffer_free_cb, transmit_cb, epilog_cb,
136 a2dp_offload_cb};
137
138} // namespace
139
140namespace android {
141namespace hardware {
142namespace bluetooth {
143namespace V1_0 {
144namespace implementation {
145
Andre Eisenbach9041d972017-01-17 18:23:12 -0800146class FirmwareStartupTimer {
147 public:
148 FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {}
149
150 ~FirmwareStartupTimer() {
151 std::chrono::duration<double> duration =
152 std::chrono::steady_clock::now() - start_time_;
153 double s = duration.count();
154 if (s == 0) return;
Myles Watson8ffcbc72017-01-20 10:09:38 -0800155 ALOGI("Firmware configured in %.3fs", s);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800156 }
157
158 private:
159 std::chrono::steady_clock::time_point start_time_;
160};
161
162bool VendorInterface::Initialize(
163 InitializeCompleteCallback initialize_complete_cb,
Zach Johnson917efb12017-02-26 23:46:05 -0800164 PacketReadCallback event_cb, PacketReadCallback acl_cb,
165 PacketReadCallback sco_cb) {
Myles Watson35617172017-02-23 15:50:54 -0800166 CHECK(!g_vendor_interface);
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() {
173 CHECK(g_vendor_interface);
174 g_vendor_interface->Close();
175 delete g_vendor_interface;
176 g_vendor_interface = nullptr;
177}
178
179VendorInterface* VendorInterface::get() { return g_vendor_interface; }
180
Andre Eisenbach9041d972017-01-17 18:23:12 -0800181bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
Zach Johnson917efb12017-02-26 23:46:05 -0800182 PacketReadCallback event_cb,
183 PacketReadCallback acl_cb,
184 PacketReadCallback sco_cb) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800185 initialize_complete_cb_ = initialize_complete_cb;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700186
187 // Initialize vendor interface
188
189 lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
190 if (!lib_handle_) {
191 ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME,
192 dlerror());
193 return false;
194 }
195
196 lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>(
197 dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME));
198 if (!lib_interface_) {
199 ALOGE("%s unable to find symbol %s in %s (%s)", __func__,
200 VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror());
201 return false;
202 }
203
Myles Watson6a7d6222016-10-13 15:45:02 -0700204 // Get the local BD address
205
206 uint8_t local_bda[BluetoothAddress::kBytes];
207 CHECK(BluetoothAddress::get_local_address(local_bda));
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700208 int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
209 if (status) {
210 ALOGE("%s unable to initialize vendor library: %d", __func__, status);
211 return false;
212 }
213
214 ALOGD("%s vendor library loaded", __func__);
215
Myles Watsonc0aee0c2017-03-13 12:35:25 -0700216 // Power on the controller
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700217
Myles Watsonc0aee0c2017-03-13 12:35:25 -0700218 int power_state = BT_VND_PWR_ON;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700219 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
220
Zach Johnson917efb12017-02-26 23:46:05 -0800221 // Get the UART socket(s)
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700222
223 int fd_list[CH_MAX] = {0};
224 int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list);
225
Zach Johnson917efb12017-02-26 23:46:05 -0800226 for (int i = 0; i < fd_count; i++) {
227 if (fd_list[i] == INVALID_FD) {
228 ALOGE("%s: fd %d is invalid!", __func__, fd_list[i]);
229 return false;
230 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700231 }
232
Zach Johnson917efb12017-02-26 23:46:05 -0800233 event_cb_ = event_cb;
234 PacketReadCallback intercept_events = [this](const hidl_vec<uint8_t>& event) {
235 HandleIncomingEvent(event);
236 };
237
238 if (fd_count == 1) {
239 hci::H4Protocol* h4_hci =
240 new hci::H4Protocol(fd_list[0], intercept_events, acl_cb, sco_cb);
241 fd_watcher_.WatchFdForNonBlockingReads(
242 fd_list[0], [h4_hci](int fd) { h4_hci->OnDataReady(fd); });
243 hci_ = h4_hci;
244 } else {
245 hci::MctProtocol* mct_hci =
246 new hci::MctProtocol(fd_list, intercept_events, acl_cb);
247 fd_watcher_.WatchFdForNonBlockingReads(
248 fd_list[CH_EVT], [mct_hci](int fd) { mct_hci->OnEventDataReady(fd); });
Zach Johnsonfed25ad2017-03-15 13:23:54 -0700249 fd_watcher_.WatchFdForNonBlockingReads(
250 fd_list[CH_ACL_IN],
251 [mct_hci](int fd) { mct_hci->OnAclDataReady(fd); });
Zach Johnson917efb12017-02-26 23:46:05 -0800252 hci_ = mct_hci;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700253 }
254
Myles Watsonbeb13b42017-01-26 10:47:27 -0800255 // Initially, the power management is off.
Andre Eisenbachf60aeb42017-02-07 20:28:32 -0800256 lpm_wake_deasserted = true;
Myles Watsonbeb13b42017-01-26 10:47:27 -0800257
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700258 // Start configuring the firmware
Andre Eisenbach9041d972017-01-17 18:23:12 -0800259 firmware_startup_timer_ = new FirmwareStartupTimer();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700260 lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
261
262 return true;
263}
264
265void VendorInterface::Close() {
Andre Eisenbach8a9efb62017-03-17 20:28:09 +0000266 // These callbacks may send HCI events (vendor-dependent), so make sure to
267 // StopWatching the file descriptor after this.
268 if (lib_interface_ != nullptr) {
269 bt_vendor_lpm_mode_t mode = BT_VND_LPM_DISABLE;
270 lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
271 }
272
Myles Watsonf3a3cb72017-03-02 09:26:53 -0800273 fd_watcher_.StopWatchingFileDescriptors();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700274
Zach Johnson917efb12017-02-26 23:46:05 -0800275 if (hci_ != nullptr) {
276 delete hci_;
277 hci_ = nullptr;
278 }
279
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700280 if (lib_interface_ != nullptr) {
281 lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr);
Myles Watson66a4ca32017-03-10 09:10:51 -0800282
283 int power_state = BT_VND_PWR_OFF;
284 lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
Myles Watson9eee8302017-06-08 08:38:58 -0700285
286 lib_interface_->cleanup();
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700287 }
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) {
Myles Watsonbeb13b42017-01-26 10:47:27 -0800301 recent_activity_flag = true;
302
303 if (lpm_wake_deasserted == true) {
304 // Restart the timer.
305 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
306 [this]() { OnTimeout(); });
307 // Assert wake.
308 lpm_wake_deasserted = false;
309 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_ASSERT;
310 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
311 ALOGV("%s: Sent wake before (%02x)", __func__, data[0] | (data[1] << 8));
312 }
313
Zach Johnson917efb12017-02-26 23:46:05 -0800314 return hci_->Send(type, data, length);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700315}
316
317void VendorInterface::OnFirmwareConfigured(uint8_t result) {
Andre Eisenbach9041d972017-01-17 18:23:12 -0800318 ALOGD("%s result: %d", __func__, result);
Andre Eisenbach9041d972017-01-17 18:23:12 -0800319
320 if (firmware_startup_timer_ != nullptr) {
321 delete firmware_startup_timer_;
322 firmware_startup_timer_ = nullptr;
323 }
324
325 if (initialize_complete_cb_ != nullptr) {
326 initialize_complete_cb_(result == 0);
327 initialize_complete_cb_ = nullptr;
328 }
Myles Watsonbeb13b42017-01-26 10:47:27 -0800329
330 lib_interface_->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &lpm_timeout_ms);
331 ALOGI("%s: lpm_timeout_ms %d", __func__, lpm_timeout_ms);
332
333 bt_vendor_lpm_mode_t mode = BT_VND_LPM_ENABLE;
334 lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
335
336 ALOGD("%s Calling StartLowPowerWatchdog()", __func__);
337 fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
338 [this]() { OnTimeout(); });
Myles Watsonbeb13b42017-01-26 10:47:27 -0800339}
340
341void VendorInterface::OnTimeout() {
342 ALOGV("%s", __func__);
343 if (recent_activity_flag == false) {
344 lpm_wake_deasserted = true;
345 bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_DEASSERT;
346 lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState);
347 fd_watcher_.ConfigureTimeout(std::chrono::seconds(0), []() {
348 ALOGE("Zero timeout! Should never happen.");
349 });
350 }
351 recent_activity_flag = false;
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700352}
353
Zach Johnson917efb12017-02-26 23:46:05 -0800354void VendorInterface::HandleIncomingEvent(const hidl_vec<uint8_t>& hci_packet) {
355 if (internal_command.cb != nullptr &&
356 internal_command_event_match(hci_packet)) {
357 HC_BT_HDR* bt_hdr = WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet);
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700358
Zach Johnson917efb12017-02-26 23:46:05 -0800359 // The callbacks can send new commands, so don't zero after calling.
360 tINT_CMD_CBACK saved_cb = internal_command.cb;
361 internal_command.cb = nullptr;
362 saved_cb(bt_hdr);
363 } else {
364 event_cb_(hci_packet);
365 }
Andre Eisenbach89ba5282016-10-13 15:45:02 -0700366}
367
368} // namespace implementation
369} // namespace V1_0
370} // namespace bluetooth
371} // namespace hardware
372} // namespace android