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 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 54 | 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] | 55 | 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 Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 61 | // 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] | 62 | // 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 Watson | df765ea | 2017-01-30 09:07:37 -0800 | [diff] [blame] | 67 | size_t write_safely(int fd, const uint8_t* data, size_t length) { |
| 68 | size_t transmitted_length = 0; |
| 69 | while (length > 0) { |
| 70 | ssize_t ret = |
| 71 | TEMP_FAILURE_RETRY(write(fd, data + transmitted_length, length)); |
| 72 | |
| 73 | if (ret == -1) { |
| 74 | if (errno == EAGAIN) continue; |
| 75 | ALOGE("%s error writing to UART (%s)", __func__, strerror(errno)); |
| 76 | break; |
| 77 | |
| 78 | } else if (ret == 0) { |
| 79 | // Nothing written :( |
| 80 | ALOGE("%s zero bytes written - something went wrong...", __func__); |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | transmitted_length += ret; |
| 85 | length -= ret; |
| 86 | } |
| 87 | |
| 88 | return transmitted_length; |
| 89 | } |
| 90 | |
Myles Watson | a7d33b3 | 2017-01-24 09:09:58 -0800 | [diff] [blame] | 91 | bool internal_command_event_match(const hidl_vec<uint8_t>& packet) { |
| 92 | uint8_t event_code = packet[0]; |
| 93 | if (event_code != HCI_COMMAND_COMPLETE_EVENT) { |
| 94 | ALOGE("%s: Unhandled event type %02X", __func__, event_code); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | size_t opcode_offset = HCI_EVENT_PREAMBLE_SIZE + 1; // Skip num packets. |
| 99 | |
| 100 | uint16_t opcode = packet[opcode_offset] | (packet[opcode_offset + 1] << 8); |
| 101 | |
Myles Watson | 4e2e8ec | 2017-02-01 10:46:16 -0800 | [diff] [blame] | 102 | ALOGV("%s internal_command.opcode = %04X opcode = %04x", __func__, |
| 103 | internal_command.opcode, opcode); |
| 104 | return opcode == internal_command.opcode; |
Myles Watson | a7d33b3 | 2017-01-24 09:09:58 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 107 | 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] | 108 | ALOGV("%s opcode: 0x%04x, ptr: %p, cb: %p", __func__, opcode, buffer, |
| 109 | callback); |
Myles Watson | 4e2e8ec | 2017-02-01 10:46:16 -0800 | [diff] [blame] | 110 | internal_command.cb = callback; |
| 111 | internal_command.opcode = opcode; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 112 | uint8_t type = HCI_PACKET_TYPE_COMMAND; |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 113 | HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer); |
Myles Watson | df765ea | 2017-01-30 09:07:37 -0800 | [diff] [blame] | 114 | VendorInterface::get()->Send(type, bt_hdr->data, bt_hdr->len); |
Myles Watson | 4e2e8ec | 2017-02-01 10:46:16 -0800 | [diff] [blame] | 115 | delete[] reinterpret_cast<uint8_t*>(buffer); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 116 | return true; |
| 117 | } |
| 118 | |
| 119 | void firmware_config_cb(bt_vendor_op_result_t result) { |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 120 | ALOGV("%s result: %d", __func__, result); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 121 | VendorInterface::get()->OnFirmwareConfigured(result); |
| 122 | } |
| 123 | |
| 124 | void sco_config_cb(bt_vendor_op_result_t result) { |
| 125 | ALOGD("%s result: %d", __func__, result); |
| 126 | } |
| 127 | |
| 128 | void low_power_mode_cb(bt_vendor_op_result_t result) { |
| 129 | ALOGD("%s result: %d", __func__, result); |
| 130 | } |
| 131 | |
| 132 | void sco_audiostate_cb(bt_vendor_op_result_t result) { |
| 133 | ALOGD("%s result: %d", __func__, result); |
| 134 | } |
| 135 | |
| 136 | void* buffer_alloc_cb(int size) { |
| 137 | void* p = new uint8_t[size]; |
| 138 | ALOGV("%s pts: %p, size: %d", __func__, p, size); |
| 139 | return p; |
| 140 | } |
| 141 | |
| 142 | void buffer_free_cb(void* buffer) { |
| 143 | ALOGV("%s ptr: %p", __func__, buffer); |
| 144 | delete[] reinterpret_cast<uint8_t*>(buffer); |
| 145 | } |
| 146 | |
| 147 | void epilog_cb(bt_vendor_op_result_t result) { |
| 148 | ALOGD("%s result: %d", __func__, result); |
| 149 | } |
| 150 | |
| 151 | void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op, |
| 152 | uint8_t av_handle) { |
| 153 | ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle); |
| 154 | } |
| 155 | |
| 156 | const bt_vendor_callbacks_t lib_callbacks = { |
| 157 | sizeof(lib_callbacks), firmware_config_cb, sco_config_cb, |
| 158 | low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb, |
| 159 | buffer_free_cb, transmit_cb, epilog_cb, |
| 160 | a2dp_offload_cb}; |
| 161 | |
| 162 | } // namespace |
| 163 | |
| 164 | namespace android { |
| 165 | namespace hardware { |
| 166 | namespace bluetooth { |
| 167 | namespace V1_0 { |
| 168 | namespace implementation { |
| 169 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 170 | class FirmwareStartupTimer { |
| 171 | public: |
| 172 | FirmwareStartupTimer() : start_time_(std::chrono::steady_clock::now()) {} |
| 173 | |
| 174 | ~FirmwareStartupTimer() { |
| 175 | std::chrono::duration<double> duration = |
| 176 | std::chrono::steady_clock::now() - start_time_; |
| 177 | double s = duration.count(); |
| 178 | if (s == 0) return; |
Myles Watson | 8ffcbc7 | 2017-01-20 10:09:38 -0800 | [diff] [blame] | 179 | ALOGI("Firmware configured in %.3fs", s); |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | private: |
| 183 | std::chrono::steady_clock::time_point start_time_; |
| 184 | }; |
| 185 | |
| 186 | bool VendorInterface::Initialize( |
| 187 | InitializeCompleteCallback initialize_complete_cb, |
| 188 | PacketReadCallback packet_read_cb) { |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 189 | assert(!g_vendor_interface); |
| 190 | g_vendor_interface = new VendorInterface(); |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 191 | return g_vendor_interface->Open(initialize_complete_cb, packet_read_cb); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void VendorInterface::Shutdown() { |
| 195 | CHECK(g_vendor_interface); |
| 196 | g_vendor_interface->Close(); |
| 197 | delete g_vendor_interface; |
| 198 | g_vendor_interface = nullptr; |
| 199 | } |
| 200 | |
| 201 | VendorInterface* VendorInterface::get() { return g_vendor_interface; } |
| 202 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 203 | bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb, |
| 204 | PacketReadCallback packet_read_cb) { |
| 205 | initialize_complete_cb_ = initialize_complete_cb; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 206 | packet_read_cb_ = packet_read_cb; |
| 207 | |
| 208 | // Initialize vendor interface |
| 209 | |
| 210 | lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW); |
| 211 | if (!lib_handle_) { |
| 212 | ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME, |
| 213 | dlerror()); |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>( |
| 218 | dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME)); |
| 219 | if (!lib_interface_) { |
| 220 | ALOGE("%s unable to find symbol %s in %s (%s)", __func__, |
| 221 | VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror()); |
| 222 | return false; |
| 223 | } |
| 224 | |
Myles Watson | 6a7d622 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 225 | // Get the local BD address |
| 226 | |
| 227 | uint8_t local_bda[BluetoothAddress::kBytes]; |
| 228 | CHECK(BluetoothAddress::get_local_address(local_bda)); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 229 | int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda); |
| 230 | if (status) { |
| 231 | ALOGE("%s unable to initialize vendor library: %d", __func__, status); |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | ALOGD("%s vendor library loaded", __func__); |
| 236 | |
| 237 | // Power cycle chip |
| 238 | |
| 239 | int power_state = BT_VND_PWR_OFF; |
| 240 | lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state); |
| 241 | power_state = BT_VND_PWR_ON; |
| 242 | lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state); |
| 243 | |
| 244 | // Get the UART socket |
| 245 | |
| 246 | int fd_list[CH_MAX] = {0}; |
| 247 | int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list); |
| 248 | |
| 249 | if (fd_count != 1) { |
| 250 | ALOGE("%s fd count %d != 1; we can't handle this currently...", __func__, |
| 251 | fd_count); |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | uart_fd_ = fd_list[0]; |
| 256 | if (uart_fd_ == INVALID_FD) { |
| 257 | ALOGE("%s unable to determine UART fd", __func__); |
| 258 | return false; |
| 259 | } |
| 260 | |
Myles Watson | 8ffcbc7 | 2017-01-20 10:09:38 -0800 | [diff] [blame] | 261 | ALOGI("%s UART fd: %d", __func__, uart_fd_); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 262 | |
| 263 | fd_watcher_.WatchFdForNonBlockingReads(uart_fd_, |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 264 | [this](int fd) { hci_packetizer_.OnDataReady(fd); }); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 265 | |
Myles Watson | beb13b4 | 2017-01-26 10:47:27 -0800 | [diff] [blame] | 266 | // Initially, the power management is off. |
Andre Eisenbach | f60aeb4 | 2017-02-07 20:28:32 -0800 | [diff] [blame] | 267 | lpm_wake_deasserted = true; |
Myles Watson | beb13b4 | 2017-01-26 10:47:27 -0800 | [diff] [blame] | 268 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 269 | // Start configuring the firmware |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 270 | firmware_startup_timer_ = new FirmwareStartupTimer(); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 271 | lib_interface_->op(BT_VND_OP_FW_CFG, nullptr); |
| 272 | |
| 273 | return true; |
| 274 | } |
| 275 | |
| 276 | void VendorInterface::Close() { |
Myles Watson | f3a3cb7 | 2017-03-02 09:26:53 -0800 | [diff] [blame^] | 277 | fd_watcher_.StopWatchingFileDescriptors(); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 278 | |
| 279 | if (lib_interface_ != nullptr) { |
Andre Eisenbach | f60aeb4 | 2017-02-07 20:28:32 -0800 | [diff] [blame] | 280 | bt_vendor_lpm_mode_t mode = BT_VND_LPM_DISABLE; |
| 281 | lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode); |
| 282 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 283 | lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr); |
| 284 | uart_fd_ = INVALID_FD; |
| 285 | int power_state = BT_VND_PWR_OFF; |
| 286 | lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state); |
| 287 | } |
| 288 | |
| 289 | if (lib_handle_ != nullptr) { |
| 290 | dlclose(lib_handle_); |
| 291 | lib_handle_ = nullptr; |
| 292 | } |
| 293 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 294 | if (firmware_startup_timer_ != nullptr) { |
| 295 | delete firmware_startup_timer_; |
| 296 | firmware_startup_timer_ = nullptr; |
| 297 | } |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Myles Watson | df765ea | 2017-01-30 09:07:37 -0800 | [diff] [blame] | 300 | 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] | 301 | if (uart_fd_ == INVALID_FD) return 0; |
| 302 | |
Myles Watson | beb13b4 | 2017-01-26 10:47:27 -0800 | [diff] [blame] | 303 | recent_activity_flag = true; |
| 304 | |
| 305 | if (lpm_wake_deasserted == true) { |
| 306 | // Restart the timer. |
| 307 | fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms), |
| 308 | [this]() { OnTimeout(); }); |
| 309 | // Assert wake. |
| 310 | lpm_wake_deasserted = false; |
| 311 | bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_ASSERT; |
| 312 | lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState); |
| 313 | ALOGV("%s: Sent wake before (%02x)", __func__, data[0] | (data[1] << 8)); |
| 314 | } |
| 315 | |
Myles Watson | df765ea | 2017-01-30 09:07:37 -0800 | [diff] [blame] | 316 | int rv = write_safely(uart_fd_, &type, sizeof(type)); |
| 317 | if (rv == sizeof(type)) |
| 318 | rv = write_safely(uart_fd_, data, length); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 319 | |
Myles Watson | df765ea | 2017-01-30 09:07:37 -0800 | [diff] [blame] | 320 | return rv; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void VendorInterface::OnFirmwareConfigured(uint8_t result) { |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 324 | ALOGD("%s result: %d", __func__, result); |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 325 | |
| 326 | if (firmware_startup_timer_ != nullptr) { |
| 327 | delete firmware_startup_timer_; |
| 328 | firmware_startup_timer_ = nullptr; |
| 329 | } |
| 330 | |
| 331 | if (initialize_complete_cb_ != nullptr) { |
| 332 | initialize_complete_cb_(result == 0); |
| 333 | initialize_complete_cb_ = nullptr; |
| 334 | } |
Myles Watson | beb13b4 | 2017-01-26 10:47:27 -0800 | [diff] [blame] | 335 | |
| 336 | lib_interface_->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &lpm_timeout_ms); |
| 337 | ALOGI("%s: lpm_timeout_ms %d", __func__, lpm_timeout_ms); |
| 338 | |
| 339 | bt_vendor_lpm_mode_t mode = BT_VND_LPM_ENABLE; |
| 340 | lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode); |
| 341 | |
| 342 | ALOGD("%s Calling StartLowPowerWatchdog()", __func__); |
| 343 | fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms), |
| 344 | [this]() { OnTimeout(); }); |
Myles Watson | beb13b4 | 2017-01-26 10:47:27 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void VendorInterface::OnTimeout() { |
| 348 | ALOGV("%s", __func__); |
| 349 | if (recent_activity_flag == false) { |
| 350 | lpm_wake_deasserted = true; |
| 351 | bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_DEASSERT; |
| 352 | lib_interface_->op(BT_VND_OP_LPM_WAKE_SET_STATE, &wakeState); |
| 353 | fd_watcher_.ConfigureTimeout(std::chrono::seconds(0), []() { |
| 354 | ALOGE("Zero timeout! Should never happen."); |
| 355 | }); |
| 356 | } |
| 357 | recent_activity_flag = false; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 360 | void VendorInterface::OnPacketReady() { |
| 361 | VendorInterface::get()->HandleIncomingPacket(); |
| 362 | } |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 363 | |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 364 | void VendorInterface::HandleIncomingPacket() { |
| 365 | HciPacketType hci_packet_type = hci_packetizer_.GetPacketType(); |
| 366 | hidl_vec<uint8_t> hci_packet = hci_packetizer_.GetPacket(); |
Myles Watson | 4e2e8ec | 2017-02-01 10:46:16 -0800 | [diff] [blame] | 367 | if (internal_command.cb != nullptr && |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 368 | hci_packet_type == HCI_PACKET_TYPE_EVENT && |
| 369 | internal_command_event_match(hci_packet)) { |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 370 | HC_BT_HDR* bt_hdr = |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 371 | WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet); |
Myles Watson | a7d33b3 | 2017-01-24 09:09:58 -0800 | [diff] [blame] | 372 | |
| 373 | // The callbacks can send new commands, so don't zero after calling. |
Myles Watson | 4e2e8ec | 2017-02-01 10:46:16 -0800 | [diff] [blame] | 374 | tINT_CMD_CBACK saved_cb = internal_command.cb; |
| 375 | internal_command.cb = nullptr; |
Myles Watson | a7d33b3 | 2017-01-24 09:09:58 -0800 | [diff] [blame] | 376 | saved_cb(bt_hdr); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 377 | } else { |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 378 | packet_read_cb_(hci_packet_type, hci_packet); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 379 | } |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | } // namespace implementation |
| 383 | } // namespace V1_0 |
| 384 | } // namespace bluetooth |
| 385 | } // namespace hardware |
| 386 | } // namespace android |