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 | |
| 19 | #define LOG_TAG "android.hardware.bluetooth@1.0-impl" |
| 20 | #include <android-base/logging.h> |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | #include <dlfcn.h> |
| 24 | |
| 25 | static const char* VENDOR_LIBRARY_NAME = "libbt-vendor.so"; |
| 26 | static const char* VENDOR_LIBRARY_SYMBOL_NAME = |
| 27 | "BLUETOOTH_VENDOR_LIB_INTERFACE"; |
| 28 | |
| 29 | static const int INVALID_FD = -1; |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | using android::hardware::bluetooth::V1_0::implementation::VendorInterface; |
| 34 | |
| 35 | tINT_CMD_CBACK internal_command_cb; |
| 36 | VendorInterface* g_vendor_interface = nullptr; |
| 37 | |
| 38 | const size_t preamble_size_for_type[] = { |
| 39 | 0, HCI_COMMAND_PREAMBLE_SIZE, HCI_ACL_PREAMBLE_SIZE, HCI_SCO_PREAMBLE_SIZE, |
| 40 | HCI_EVENT_PREAMBLE_SIZE}; |
| 41 | const size_t packet_length_offset_for_type[] = { |
| 42 | 0, HCI_LENGTH_OFFSET_CMD, HCI_LENGTH_OFFSET_ACL, HCI_LENGTH_OFFSET_SCO, |
| 43 | HCI_LENGTH_OFFSET_EVT}; |
| 44 | |
| 45 | size_t HciGetPacketLengthForType( |
| 46 | HciPacketType type, const android::hardware::hidl_vec<uint8_t>& packet) { |
| 47 | size_t offset = packet_length_offset_for_type[type]; |
| 48 | if (type == HCI_PACKET_TYPE_ACL_DATA) { |
| 49 | return (((packet[offset + 1]) << 8) | packet[offset]); |
| 50 | } |
| 51 | return packet[offset]; |
| 52 | } |
| 53 | |
| 54 | HC_BT_HDR* WrapPacketAndCopy(uint16_t event, |
| 55 | const android::hardware::hidl_vec<uint8_t>& data) { |
| 56 | size_t packet_size = data.size() + sizeof(HC_BT_HDR); |
| 57 | HC_BT_HDR* packet = reinterpret_cast<HC_BT_HDR*>(new uint8_t[packet_size]); |
| 58 | packet->offset = 0; |
| 59 | packet->len = data.size(); |
| 60 | packet->layer_specific = 0; |
| 61 | packet->event = event; |
| 62 | // TODO(eisenbach): Avoid copy here; if BT_HDR->data can be enusred to |
| 63 | // be the only way the data is accessed, a pointer could be passed here... |
| 64 | memcpy(packet->data, data.data(), data.size()); |
| 65 | return packet; |
| 66 | } |
| 67 | |
| 68 | uint8_t transmit_cb(uint16_t opcode, void* buffer, tINT_CMD_CBACK callback) { |
| 69 | ALOGV("%s opcode: 0x%04x, ptr: %p", __func__, opcode, buffer); |
| 70 | HC_BT_HDR* bt_hdr = reinterpret_cast<HC_BT_HDR*>(buffer); |
| 71 | |
| 72 | internal_command_cb = callback; |
| 73 | uint8_t type = HCI_PACKET_TYPE_COMMAND; |
| 74 | VendorInterface::get()->SendPrivate(&type, 1); |
| 75 | VendorInterface::get()->SendPrivate(bt_hdr->data, bt_hdr->len); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | void firmware_config_cb(bt_vendor_op_result_t result) { |
| 80 | ALOGD("%s result: %d", __func__, result); |
| 81 | VendorInterface::get()->OnFirmwareConfigured(result); |
| 82 | } |
| 83 | |
| 84 | void sco_config_cb(bt_vendor_op_result_t result) { |
| 85 | ALOGD("%s result: %d", __func__, result); |
| 86 | } |
| 87 | |
| 88 | void low_power_mode_cb(bt_vendor_op_result_t result) { |
| 89 | ALOGD("%s result: %d", __func__, result); |
| 90 | } |
| 91 | |
| 92 | void sco_audiostate_cb(bt_vendor_op_result_t result) { |
| 93 | ALOGD("%s result: %d", __func__, result); |
| 94 | } |
| 95 | |
| 96 | void* buffer_alloc_cb(int size) { |
| 97 | void* p = new uint8_t[size]; |
| 98 | ALOGV("%s pts: %p, size: %d", __func__, p, size); |
| 99 | return p; |
| 100 | } |
| 101 | |
| 102 | void buffer_free_cb(void* buffer) { |
| 103 | ALOGV("%s ptr: %p", __func__, buffer); |
| 104 | delete[] reinterpret_cast<uint8_t*>(buffer); |
| 105 | } |
| 106 | |
| 107 | void epilog_cb(bt_vendor_op_result_t result) { |
| 108 | ALOGD("%s result: %d", __func__, result); |
| 109 | } |
| 110 | |
| 111 | void a2dp_offload_cb(bt_vendor_op_result_t result, bt_vendor_opcode_t op, |
| 112 | uint8_t av_handle) { |
| 113 | ALOGD("%s result: %d, op: %d, handle: %d", __func__, result, op, av_handle); |
| 114 | } |
| 115 | |
| 116 | const bt_vendor_callbacks_t lib_callbacks = { |
| 117 | sizeof(lib_callbacks), firmware_config_cb, sco_config_cb, |
| 118 | low_power_mode_cb, sco_audiostate_cb, buffer_alloc_cb, |
| 119 | buffer_free_cb, transmit_cb, epilog_cb, |
| 120 | a2dp_offload_cb}; |
| 121 | |
| 122 | } // namespace |
| 123 | |
| 124 | namespace android { |
| 125 | namespace hardware { |
| 126 | namespace bluetooth { |
| 127 | namespace V1_0 { |
| 128 | namespace implementation { |
| 129 | |
| 130 | bool VendorInterface::Initialize(PacketReadCallback packet_read_cb) { |
| 131 | assert(!g_vendor_interface); |
| 132 | g_vendor_interface = new VendorInterface(); |
| 133 | return g_vendor_interface->Open(packet_read_cb); |
| 134 | } |
| 135 | |
| 136 | void VendorInterface::Shutdown() { |
| 137 | CHECK(g_vendor_interface); |
| 138 | g_vendor_interface->Close(); |
| 139 | delete g_vendor_interface; |
| 140 | g_vendor_interface = nullptr; |
| 141 | } |
| 142 | |
| 143 | VendorInterface* VendorInterface::get() { return g_vendor_interface; } |
| 144 | |
| 145 | bool VendorInterface::Open(PacketReadCallback packet_read_cb) { |
| 146 | // TODO(eisenbach): P0 - get local BD address somehow |
| 147 | uint8_t local_bda[] = {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}; |
| 148 | |
| 149 | firmware_configured_ = false; |
| 150 | packet_read_cb_ = packet_read_cb; |
| 151 | |
| 152 | // Initialize vendor interface |
| 153 | |
| 154 | lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW); |
| 155 | if (!lib_handle_) { |
| 156 | ALOGE("%s unable to open %s (%s)", __func__, VENDOR_LIBRARY_NAME, |
| 157 | dlerror()); |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | lib_interface_ = reinterpret_cast<bt_vendor_interface_t*>( |
| 162 | dlsym(lib_handle_, VENDOR_LIBRARY_SYMBOL_NAME)); |
| 163 | if (!lib_interface_) { |
| 164 | ALOGE("%s unable to find symbol %s in %s (%s)", __func__, |
| 165 | VENDOR_LIBRARY_SYMBOL_NAME, VENDOR_LIBRARY_NAME, dlerror()); |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda); |
| 170 | if (status) { |
| 171 | ALOGE("%s unable to initialize vendor library: %d", __func__, status); |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | ALOGD("%s vendor library loaded", __func__); |
| 176 | |
| 177 | // Power cycle chip |
| 178 | |
| 179 | int power_state = BT_VND_PWR_OFF; |
| 180 | lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state); |
| 181 | power_state = BT_VND_PWR_ON; |
| 182 | lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state); |
| 183 | |
| 184 | // Get the UART socket |
| 185 | |
| 186 | int fd_list[CH_MAX] = {0}; |
| 187 | int fd_count = lib_interface_->op(BT_VND_OP_USERIAL_OPEN, &fd_list); |
| 188 | |
| 189 | if (fd_count != 1) { |
| 190 | ALOGE("%s fd count %d != 1; we can't handle this currently...", __func__, |
| 191 | fd_count); |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | uart_fd_ = fd_list[0]; |
| 196 | if (uart_fd_ == INVALID_FD) { |
| 197 | ALOGE("%s unable to determine UART fd", __func__); |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | ALOGD("%s UART fd: %d", __func__, uart_fd_); |
| 202 | |
| 203 | fd_watcher_.WatchFdForNonBlockingReads(uart_fd_, |
| 204 | [this](int fd) { OnDataReady(fd); }); |
| 205 | |
| 206 | // Start configuring the firmware |
| 207 | lib_interface_->op(BT_VND_OP_FW_CFG, nullptr); |
| 208 | |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | void VendorInterface::Close() { |
| 213 | fd_watcher_.StopWatchingFileDescriptor(); |
| 214 | |
| 215 | if (lib_interface_ != nullptr) { |
| 216 | lib_interface_->op(BT_VND_OP_USERIAL_CLOSE, nullptr); |
| 217 | uart_fd_ = INVALID_FD; |
| 218 | int power_state = BT_VND_PWR_OFF; |
| 219 | lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state); |
| 220 | } |
| 221 | |
| 222 | if (lib_handle_ != nullptr) { |
| 223 | dlclose(lib_handle_); |
| 224 | lib_handle_ = nullptr; |
| 225 | } |
| 226 | |
| 227 | firmware_configured_ = false; |
| 228 | } |
| 229 | |
| 230 | size_t VendorInterface::Send(const uint8_t* data, size_t length) { |
| 231 | if (firmware_configured_ && queued_data_.size() == 0) |
| 232 | return SendPrivate(data, length); |
| 233 | |
| 234 | if (!firmware_configured_) { |
| 235 | ALOGI("%s queueing command", __func__); |
| 236 | queued_data_.resize(queued_data_.size() + length); |
| 237 | uint8_t* append_ptr = &queued_data_[queued_data_.size() - length]; |
| 238 | memcpy(append_ptr, data, length); |
| 239 | return length; |
| 240 | } |
| 241 | |
| 242 | ALOGI("%s sending queued command", __func__); |
| 243 | SendPrivate(queued_data_.data(), queued_data_.size()); |
| 244 | queued_data_.resize(0); |
| 245 | |
| 246 | ALOGI("%s done sending queued command", __func__); |
| 247 | |
| 248 | return SendPrivate(data, length); |
| 249 | } |
| 250 | |
| 251 | size_t VendorInterface::SendPrivate(const uint8_t* data, size_t length) { |
| 252 | if (uart_fd_ == INVALID_FD) return 0; |
| 253 | |
| 254 | size_t transmitted_length = 0; |
| 255 | while (length > 0) { |
| 256 | ssize_t ret = |
| 257 | TEMP_FAILURE_RETRY(write(uart_fd_, data + transmitted_length, length)); |
| 258 | |
| 259 | if (ret == -1) { |
| 260 | if (errno == EAGAIN) continue; |
| 261 | ALOGE("%s error writing to UART (%s)", __func__, strerror(errno)); |
| 262 | break; |
| 263 | |
| 264 | } else if (ret == 0) { |
| 265 | // Nothing written :( |
| 266 | ALOGE("%s zero bytes written - something went wrong...", __func__); |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | transmitted_length += ret; |
| 271 | length -= ret; |
| 272 | } |
| 273 | |
| 274 | return transmitted_length; |
| 275 | } |
| 276 | |
| 277 | void VendorInterface::OnFirmwareConfigured(uint8_t result) { |
| 278 | ALOGI("%s: result = %d", __func__, result); |
| 279 | firmware_configured_ = true; |
| 280 | VendorInterface::get()->Send(NULL, 0); |
| 281 | } |
| 282 | |
| 283 | void VendorInterface::OnDataReady(int fd) { |
| 284 | switch (hci_parser_state_) { |
| 285 | case HCI_IDLE: { |
| 286 | uint8_t buffer[1] = {0}; |
| 287 | size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1)); |
| 288 | CHECK(bytes_read == 1); |
| 289 | hci_packet_type_ = static_cast<HciPacketType>(buffer[0]); |
| 290 | // TODO(eisenbach): Check for workaround(s) |
| 291 | CHECK(hci_packet_type_ >= HCI_PACKET_TYPE_ACL_DATA && |
| 292 | hci_packet_type_ <= HCI_PACKET_TYPE_EVENT) |
| 293 | << "buffer[0] = " << buffer[0]; |
| 294 | hci_parser_state_ = HCI_TYPE_READY; |
| 295 | hci_packet_.resize(HCI_PREAMBLE_SIZE_MAX); |
| 296 | hci_packet_bytes_remaining_ = preamble_size_for_type[hci_packet_type_]; |
| 297 | hci_packet_bytes_read_ = 0; |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | case HCI_TYPE_READY: { |
| 302 | size_t bytes_read = TEMP_FAILURE_RETRY( |
| 303 | read(fd, hci_packet_.data() + hci_packet_bytes_read_, |
| 304 | hci_packet_bytes_remaining_)); |
| 305 | CHECK(bytes_read > 0); |
| 306 | hci_packet_bytes_remaining_ -= bytes_read; |
| 307 | hci_packet_bytes_read_ += bytes_read; |
| 308 | if (hci_packet_bytes_remaining_ == 0) { |
| 309 | size_t packet_length = |
| 310 | HciGetPacketLengthForType(hci_packet_type_, hci_packet_); |
| 311 | hci_packet_.resize(preamble_size_for_type[hci_packet_type_] + |
| 312 | packet_length); |
| 313 | hci_packet_bytes_remaining_ = packet_length; |
| 314 | hci_parser_state_ = HCI_PAYLOAD; |
| 315 | hci_packet_bytes_read_ = 0; |
| 316 | } |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | case HCI_PAYLOAD: { |
| 321 | size_t bytes_read = TEMP_FAILURE_RETRY( |
| 322 | read(fd, |
| 323 | hci_packet_.data() + preamble_size_for_type[hci_packet_type_] + |
| 324 | hci_packet_bytes_read_, |
| 325 | hci_packet_bytes_remaining_)); |
| 326 | hci_packet_bytes_remaining_ -= bytes_read; |
| 327 | hci_packet_bytes_read_ += bytes_read; |
| 328 | if (hci_packet_bytes_remaining_ == 0) { |
| 329 | if (firmware_configured_) { |
| 330 | if (packet_read_cb_ != nullptr) { |
| 331 | packet_read_cb_(hci_packet_type_, hci_packet_); |
| 332 | } |
| 333 | } else { |
| 334 | if (internal_command_cb != nullptr) { |
| 335 | HC_BT_HDR* bt_hdr = |
| 336 | WrapPacketAndCopy(HCI_PACKET_TYPE_EVENT, hci_packet_); |
| 337 | internal_command_cb(bt_hdr); |
| 338 | } |
| 339 | } |
| 340 | hci_parser_state_ = HCI_IDLE; |
| 341 | } |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | } // namespace implementation |
| 348 | } // namespace V1_0 |
| 349 | } // namespace bluetooth |
| 350 | } // namespace hardware |
| 351 | } // namespace android |