Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 1 | // |
| 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 Albert | 3d0263f | 2017-01-26 15:33:15 -0800 | [diff] [blame] | 19 | #include <assert.h> |
| 20 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 21 | #define LOG_TAG "android.hardware.bluetooth@1.0-impl" |
| 22 | #include <android-base/logging.h> |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 23 | #include <cutils/properties.h> |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | |
| 26 | #include <dlfcn.h> |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | |
| 29 | #include "bluetooth_address.h" |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 30 | |
| 31 | static const char* VENDOR_LIBRARY_NAME = "libbt-vendor.so"; |
| 32 | static const char* VENDOR_LIBRARY_SYMBOL_NAME = |
| 33 | "BLUETOOTH_VENDOR_LIB_INTERFACE"; |
| 34 | |
| 35 | static const int INVALID_FD = -1; |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | using android::hardware::bluetooth::V1_0::implementation::VendorInterface; |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 40 | using android::hardware::hidl_vec; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 41 | |
Myles Watson | 4e2e8ec | 2017-02-01 10:46:16 -0800 | [diff] [blame] | 42 | struct { |
| 43 | tINT_CMD_CBACK cb; |
| 44 | uint16_t opcode; |
| 45 | } internal_command; |
Myles Watson | a7d33b3 | 2017-01-24 09:09:58 -0800 | [diff] [blame] | 46 | |
Myles Watson | beb13b4 | 2017-01-26 10:47:27 -0800 | [diff] [blame] | 47 | // True when LPM is not enabled yet or wake is not asserted. |
| 48 | bool lpm_wake_deasserted; |
| 49 | uint32_t lpm_timeout_ms; |
| 50 | bool recent_activity_flag; |
| 51 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 52 | VendorInterface* g_vendor_interface = nullptr; |
| 53 | |
| 54 | const size_t preamble_size_for_type[] = { |
| 55 | 0, HCI_COMMAND_PREAMBLE_SIZE, HCI_ACL_PREAMBLE_SIZE, HCI_SCO_PREAMBLE_SIZE, |
| 56 | HCI_EVENT_PREAMBLE_SIZE}; |
| 57 | const size_t packet_length_offset_for_type[] = { |
| 58 | 0, HCI_LENGTH_OFFSET_CMD, HCI_LENGTH_OFFSET_ACL, HCI_LENGTH_OFFSET_SCO, |
| 59 | HCI_LENGTH_OFFSET_EVT}; |
| 60 | |
Myles Watson | 7139018 | 2017-01-24 13:34:59 -0800 | [diff] [blame] | 61 | size_t HciGetPacketLengthForType(HciPacketType type, const uint8_t* preamble) { |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 62 | size_t offset = packet_length_offset_for_type[type]; |
Myles Watson | 7139018 | 2017-01-24 13:34:59 -0800 | [diff] [blame] | 63 | if (type != HCI_PACKET_TYPE_ACL_DATA) return preamble[offset]; |
| 64 | return (((preamble[offset + 1]) << 8) | preamble[offset]); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 67 | HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) { |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 68 | size_t packet_size = data.size() + sizeof(HC_BT_HDR); |
| 69 | HC_BT_HDR* packet = reinterpret_cast<HC_BT_HDR*>(new uint8_t[packet_size]); |
| 70 | packet->offset = 0; |
| 71 | packet->len = data.size(); |
| 72 | packet->layer_specific = 0; |
| 73 | packet->event = event; |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 74 | // TODO(eisenbach): Avoid copy here; if BT_HDR->data can be ensured to |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 75 | // be the only way the data is accessed, a pointer could be passed here... |
| 76 | memcpy(packet->data, data.data(), data.size()); |
| 77 | return packet; |
| 78 | } |
| 79 | |
Myles Watson | df765ea | 2017-01-30 09:07:37 -0800 | [diff] [blame] | 80 | size_t write_safely(int fd, const uint8_t* data, size_t length) { |
| 81 | size_t transmitted_length = 0; |
| 82 | while (length > 0) { |
| 83 | ssize_t ret = |
| 84 | TEMP_FAILURE_RETRY(write(fd, data + transmitted_length, length)); |
| 85 | |
| 86 | if (ret == -1) { |
| 87 | if (errno == EAGAIN) continue; |
| 88 | ALOGE("%s error writing to UART (%s)", __func__, strerror(errno)); |
| 89 | break; |
| 90 | |
| 91 | } else if (ret == 0) { |
| 92 | // Nothing written :( |
| 93 | ALOGE("%s zero bytes written - something went wrong...", __func__); |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | transmitted_length += ret; |
| 98 | length -= ret; |
| 99 | } |
| 100 | |
| 101 | return transmitted_length; |
| 102 | } |
| 103 | |
Myles Watson | a7d33b3 | 2017-01-24 09:09:58 -0800 | [diff] [blame] | 104 | bool internal_command_event_match(const hidl_vec<uint8_t>& packet) { |
| 105 | uint8_t event_code = packet[0]; |
| 106 | if (event_code != HCI_COMMAND_COMPLETE_EVENT) { |
| 107 | ALOGE("%s: Unhandled event type %02X", __func__, event_code); |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | size_t opcode_offset = HCI_EVENT_PREAMBLE_SIZE + 1; // Skip num packets. |
| 112 | |
| 113 | uint16_t opcode = packet[opcode_offset] | (packet[opcode_offset + 1] << 8); |
| 114 | |
Myles Watson | 4e2e8ec | 2017-02-01 10:46:16 -0800 | [diff] [blame] | 115 | ALOGV("%s internal_command.opcode = %04X opcode = %04x", __func__, |
| 116 | internal_command.opcode, opcode); |
| 117 | return opcode == internal_command.opcode; |
Myles Watson | a7d33b3 | 2017-01-24 09:09:58 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 120 | uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) { |
Myles Watson | a7d33b3 | 2017-01-24 09:09:58 -0800 | [diff] [blame] | 121 | ALOGV("%s opcode: 0x%04x, ptr: %p, cb: %p", __func__, opcode, buffer, |
| 122 | callback); |
Myles Watson | 4e2e8ec | 2017-02-01 10:46:16 -0800 | [diff] [blame] | 123 | internal_command.cb = callback; |
| 124 | internal_command.opcode = opcode; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 125 | uint8_t type = HCI_PACKET_TYPE_COMMAND; |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 126 | HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer); |
Myles Watson | df765ea | 2017-01-30 09:07:37 -0800 | [diff] [blame] | 127 | VendorInterface::get()->Send(type, bt_hdr->data, bt_hdr->len); |
Myles Watson | 4e2e8ec | 2017-02-01 10:46:16 -0800 | [diff] [blame] | 128 | delete[] reinterpret_cast<uint8_t*>(buffer); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | |
| 132 | void firmware_config_cb(bt_vendor_op_result_t result) { |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 133 | ALOGV("%s result: %d", __func__, result); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 134 | VendorInterface::get()->OnFirmwareConfigured(result); |
| 135 | } |
| 136 | |
| 137 | void sco_config_cb(bt_vendor_op_result_t result) { |
| 138 | ALOGD("%s result: %d", __func__, result); |
| 139 | } |
| 140 | |
| 141 | void low_power_mode_cb(bt_vendor_op_result_t result) { |
| 142 | ALOGD("%s result: %d", __func__, result); |
| 143 | } |
| 144 | |
| 145 | void sco_audiostate_cb(bt_vendor_op_result_t result) { |
| 146 | ALOGD("%s result: %d", __func__, result); |
| 147 | } |
| 148 | |
| 149 | void* buffer_alloc_cb(int size) { |
| 150 | void* p = new uint8_t[size]; |
| 151 | ALOGV("%s pts: %p, size: %d", __func__, p, size); |
| 152 | return p; |
| 153 | } |
| 154 | |
| 155 | void buffer_free_cb(void* buffer) { |
| 156 | ALOGV("%s ptr: %p", __func__, buffer); |
| 157 | delete[] reinterpret_cast<uint8_t*>(buffer); |
| 158 | } |
| 159 | |
| 160 | void epilog_cb(bt_vendor_op_result_t result) { |
| 161 | ALOGD("%s result: %d", __func__, result); |
| 162 | } |
| 163 | |
| 164 | void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op, |
| 165 | uint8_t av_handle) { |
| 166 | ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle); |
| 167 | } |
| 168 | |
| 169 | const bt_vendor_callbacks_t lib_callbacks = { |
| 170 | sizeof(lib_callbacks), firmware_config_cb, sco_config_cb, |
| 171 | low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb, |
| 172 | buffer_free_cb, transmit_cb, epilog_cb, |
| 173 | a2dp_offload_cb}; |
| 174 | |
| 175 | } // namespace |
| 176 | |
| 177 | namespace android { |
| 178 | namespace hardware { |
| 179 | namespace bluetooth { |
| 180 | namespace V1_0 { |
| 181 | namespace implementation { |
| 182 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 183 | class FirmwareStartupTimer { |
| 184 | public: |
| 185 | FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {} |
| 186 | |
| 187 | ~FirmwareStartupTimer() { |
| 188 | std::chrono::duration<double> duration = |
| 189 | std::chrono::steady_clock::now() - start_time_; |
| 190 | double s = duration.count(); |
| 191 | if (s == 0) return; |
Myles Watson | 8ffcbc7 | 2017-01-20 10:09:38 -0800 | [diff] [blame] | 192 | ALOGI("Firmware configured in %.3fs", s); |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | private: |
| 196 | std::chrono::steady_clock::time_point start_time_; |
| 197 | }; |
| 198 | |
| 199 | bool VendorInterface::Initialize( |
| 200 | InitializeCompleteCallback initialize_complete_cb, |
| 201 | PacketReadCallback packet_read_cb) { |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 202 | assert(!g_vendor_interface); |
| 203 | g_vendor_interface = new VendorInterface(); |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 204 | return g_vendor_interface->Open(initialize_complete_cb, packet_read_cb); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void VendorInterface::Shutdown() { |
| 208 | CHECK(g_vendor_interface); |
| 209 | g_vendor_interface->Close(); |
| 210 | delete g_vendor_interface; |
| 211 | g_vendor_interface = nullptr; |
| 212 | } |
| 213 | |
| 214 | VendorInterface* VendorInterface::get() { return g_vendor_interface; } |
| 215 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 216 | bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb, |
| 217 | PacketReadCallback packet_read_cb) { |
| 218 | initialize_complete_cb_ = initialize_complete_cb; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 219 | packet_read_cb_ = packet_read_cb; |
| 220 | |
| 221 | // Initialize vendor interface |
| 222 | |
| 223 | lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW); |
| 224 | if (!lib_handle_) { |
| 225 | ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME, |
| 226 | dlerror()); |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>( |
| 231 | dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME)); |
| 232 | if (!lib_interface_) { |
| 233 | ALOGE("%s unable to find symbol %s in %s (%s)", __func__, |
| 234 | VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror()); |
| 235 | return false; |
| 236 | } |
| 237 | |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 238 | // Get the local BD address |
| 239 | |
| 240 | uint8_t local_bda[BluetoothAddress::kBytes]; |
| 241 | CHECK(BluetoothAddress::get_local_address(local_bda)); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 242 | int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda); |
| 243 | if (status) { |
| 244 | ALOGE("%s unable to initialize vendor library: %d", __func__, status); |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | ALOGD("%s vendor library loaded", __func__); |
| 249 | |
| 250 | // Power cycle chip |
| 251 | |
| 252 | int power_state = BT_VND_PWR_OFF; |
| 253 | lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state); |
| 254 | power_state = BT_VND_PWR_ON; |
| 255 | lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state); |
| 256 | |
| 257 | // Get the UART socket |
| 258 | |
| 259 | int fd_list[CH_MAX] = {0}; |
| 260 | int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list); |
| 261 | |
| 262 | if (fd_count != 1) { |
| 263 | ALOGE("%s fd count %d != 1; we can't handle this currently...", __func__, |
| 264 | fd_count); |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | uart_fd_ = fd_list[0]; |
| 269 | if (uart_fd_ == INVALID_FD) { |
| 270 | ALOGE("%s unable to determine UART fd", __func__); |
| 271 | return false; |
| 272 | } |
| 273 | |
Myles Watson | 8ffcbc7 | 2017-01-20 10:09:38 -0800 | [diff] [blame] | 274 | ALOGI("%s UART fd: %d", __func__, uart_fd_); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 275 | |
| 276 | fd_watcher_.WatchFdForNonBlockingReads(uart_fd_, |
| 277 | [this](int fd) { OnDataReady(fd); }); |
| 278 | |
Myles Watson | beb13b4 | 2017-01-26 10:47:27 -0800 | [diff] [blame] | 279 | // Initially, the power management is off. |
Andre Eisenbach | f60aeb4 | 2017-02-07 20:28:32 -0800 | [diff] [blame^] | 280 | lpm_wake_deasserted = true; |
Myles Watson | beb13b4 | 2017-01-26 10:47:27 -0800 | [diff] [blame] | 281 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 282 | // Start configuring the firmware |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 283 | firmware_startup_timer_ = new FirmwareStartupTimer(); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 284 | lib_interface_->op(BT_VND_OP_FW_CFG, nullptr); |
| 285 | |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | void VendorInterface::Close() { |
| 290 | fd_watcher_.StopWatchingFileDescriptor(); |
| 291 | |
| 292 | if (lib_interface_ != nullptr) { |
Andre Eisenbach | f60aeb4 | 2017-02-07 20:28:32 -0800 | [diff] [blame^] | 293 | bt_vendor_lpm_mode_t mode = BT_VND_LPM_DISABLE; |
| 294 | lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode); |
| 295 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 296 | lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr); |
| 297 | uart_fd_ = INVALID_FD; |
| 298 | int power_state = BT_VND_PWR_OFF; |
| 299 | lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state); |
| 300 | } |
| 301 | |
| 302 | if (lib_handle_ != nullptr) { |
| 303 | dlclose(lib_handle_); |
| 304 | lib_handle_ = nullptr; |
| 305 | } |
| 306 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 307 | if (firmware_startup_timer_ != nullptr) { |
| 308 | delete firmware_startup_timer_; |
| 309 | firmware_startup_timer_ = nullptr; |
| 310 | } |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Myles Watson | df765ea | 2017-01-30 09:07:37 -0800 | [diff] [blame] | 313 | size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) { |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 314 | if (uart_fd_ == INVALID_FD) return 0; |
| 315 | |
Myles Watson | beb13b4 | 2017-01-26 10:47:27 -0800 | [diff] [blame] | 316 | recent_activity_flag = true; |
| 317 | |
| 318 | if (lpm_wake_deasserted == true) { |
| 319 | // Restart the timer. |
| 320 | fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms), |
| 321 | [this]() { OnTimeout(); }); |
| 322 | // Assert wake. |
| 323 | lpm_wake_deasserted = false; |
| 324 | bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_ASSERT; |
| 325 | lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState); |
| 326 | ALOGV("%s: Sent wake before (%02x)", __func__, data[0] | (data[1] << 8)); |
| 327 | } |
| 328 | |
Myles Watson | df765ea | 2017-01-30 09:07:37 -0800 | [diff] [blame] | 329 | int rv = write_safely(uart_fd_, &type, sizeof(type)); |
| 330 | if (rv == sizeof(type)) |
| 331 | rv = write_safely(uart_fd_, data, length); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 332 | |
Myles Watson | df765ea | 2017-01-30 09:07:37 -0800 | [diff] [blame] | 333 | return rv; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void VendorInterface::OnFirmwareConfigured(uint8_t result) { |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 337 | ALOGD("%s result: %d", __func__, result); |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 338 | |
| 339 | if (firmware_startup_timer_ != nullptr) { |
| 340 | delete firmware_startup_timer_; |
| 341 | firmware_startup_timer_ = nullptr; |
| 342 | } |
| 343 | |
| 344 | if (initialize_complete_cb_ != nullptr) { |
| 345 | initialize_complete_cb_(result == 0); |
| 346 | initialize_complete_cb_ = nullptr; |
| 347 | } |
Myles Watson | beb13b4 | 2017-01-26 10:47:27 -0800 | [diff] [blame] | 348 | |
| 349 | lib_interface_->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &lpm_timeout_ms); |
| 350 | ALOGI("%s: lpm_timeout_ms %d", __func__, lpm_timeout_ms); |
| 351 | |
| 352 | bt_vendor_lpm_mode_t mode = BT_VND_LPM_ENABLE; |
| 353 | lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode); |
| 354 | |
| 355 | ALOGD("%s Calling StartLowPowerWatchdog()", __func__); |
| 356 | fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms), |
| 357 | [this]() { OnTimeout(); }); |
Myles Watson | beb13b4 | 2017-01-26 10:47:27 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void VendorInterface::OnTimeout() { |
| 361 | ALOGV("%s", __func__); |
| 362 | if (recent_activity_flag == false) { |
| 363 | lpm_wake_deasserted = true; |
| 364 | bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_DEASSERT; |
| 365 | lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState); |
| 366 | fd_watcher_.ConfigureTimeout(std::chrono::seconds(0), []() { |
| 367 | ALOGE("Zero timeout! Should never happen."); |
| 368 | }); |
| 369 | } |
| 370 | recent_activity_flag = false; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void VendorInterface::OnDataReady(int fd) { |
| 374 | switch (hci_parser_state_) { |
| 375 | case HCI_IDLE: { |
| 376 | uint8_t buffer[1] = {0}; |
| 377 | size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1)); |
| 378 | CHECK(bytes_read == 1); |
| 379 | hci_packet_type_ = static_cast<HciPacketType>(buffer[0]); |
| 380 | // TODO(eisenbach): Check for workaround(s) |
| 381 | CHECK(hci_packet_type_ >= HCI_PACKET_TYPE_ACL_DATA && |
| 382 | hci_packet_type_ <= HCI_PACKET_TYPE_EVENT) |
Myles Watson | 8ffcbc7 | 2017-01-20 10:09:38 -0800 | [diff] [blame] | 383 | << "buffer[0] = " << static_cast<unsigned int>(buffer[0]); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 384 | hci_parser_state_ = HCI_TYPE_READY; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 385 | hci_packet_bytes_remaining_ = preamble_size_for_type[hci_packet_type_]; |
| 386 | hci_packet_bytes_read_ = 0; |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | case HCI_TYPE_READY: { |
| 391 | size_t bytes_read = TEMP_FAILURE_RETRY( |
Myles Watson | 7139018 | 2017-01-24 13:34:59 -0800 | [diff] [blame] | 392 | read(fd, hci_packet_preamble_ + hci_packet_bytes_read_, |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 393 | hci_packet_bytes_remaining_)); |
| 394 | CHECK(bytes_read > 0); |
| 395 | hci_packet_bytes_remaining_ -= bytes_read; |
| 396 | hci_packet_bytes_read_ += bytes_read; |
| 397 | if (hci_packet_bytes_remaining_ == 0) { |
| 398 | size_t packet_length = |
Myles Watson | 7139018 | 2017-01-24 13:34:59 -0800 | [diff] [blame] | 399 | HciGetPacketLengthForType(hci_packet_type_, hci_packet_preamble_); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 400 | hci_packet_.resize(preamble_size_for_type[hci_packet_type_] + |
| 401 | packet_length); |
Myles Watson | 7139018 | 2017-01-24 13:34:59 -0800 | [diff] [blame] | 402 | memcpy(hci_packet_.data(), hci_packet_preamble_, |
| 403 | preamble_size_for_type[hci_packet_type_]); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 404 | hci_packet_bytes_remaining_ = packet_length; |
| 405 | hci_parser_state_ = HCI_PAYLOAD; |
| 406 | hci_packet_bytes_read_ = 0; |
| 407 | } |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | case HCI_PAYLOAD: { |
| 412 | size_t bytes_read = TEMP_FAILURE_RETRY( |
| 413 | read(fd, |
| 414 | hci_packet_.data() + preamble_size_for_type[hci_packet_type_] + |
| 415 | hci_packet_bytes_read_, |
| 416 | hci_packet_bytes_remaining_)); |
| 417 | hci_packet_bytes_remaining_ -= bytes_read; |
| 418 | hci_packet_bytes_read_ += bytes_read; |
| 419 | if (hci_packet_bytes_remaining_ == 0) { |
Myles Watson | 4e2e8ec | 2017-02-01 10:46:16 -0800 | [diff] [blame] | 420 | if (internal_command.cb != nullptr && |
Myles Watson | a7d33b3 | 2017-01-24 09:09:58 -0800 | [diff] [blame] | 421 | hci_packet_type_ == HCI_PACKET_TYPE_EVENT && |
| 422 | internal_command_event_match(hci_packet_)) { |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 423 | HC_BT_HDR* bt_hdr = |
| 424 | WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet_); |
Myles Watson | a7d33b3 | 2017-01-24 09:09:58 -0800 | [diff] [blame] | 425 | |
| 426 | // The callbacks can send new commands, so don't zero after calling. |
Myles Watson | 4e2e8ec | 2017-02-01 10:46:16 -0800 | [diff] [blame] | 427 | tINT_CMD_CBACK saved_cb = internal_command.cb; |
| 428 | internal_command.cb = nullptr; |
Myles Watson | a7d33b3 | 2017-01-24 09:09:58 -0800 | [diff] [blame] | 429 | saved_cb(bt_hdr); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 430 | } else { |
Myles Watson | a7d33b3 | 2017-01-24 09:09:58 -0800 | [diff] [blame] | 431 | packet_read_cb_(hci_packet_type_, hci_packet_); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 432 | } |
| 433 | hci_parser_state_ = HCI_IDLE; |
| 434 | } |
| 435 | break; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | } // namespace implementation |
| 441 | } // namespace V1_0 |
| 442 | } // namespace bluetooth |
| 443 | } // namespace hardware |
| 444 | } // namespace android |