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