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