Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [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 | #define LOG_TAG "android.hardware.bluetooth@1.0.hikey" |
| 18 | |
| 19 | #include "bluetooth_hci.h" |
| 20 | |
Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Myles Watson | 761dc49 | 2017-03-03 13:50:49 -0800 | [diff] [blame] | 22 | #include <sys/ioctl.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <unistd.h> |
| 25 | #include <utils/Log.h> |
Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | namespace bluetooth { |
| 30 | namespace V1_0 { |
| 31 | namespace hikey { |
| 32 | |
Myles Watson | 761dc49 | 2017-03-03 13:50:49 -0800 | [diff] [blame] | 33 | using android::hardware::hidl_vec; |
| 34 | |
Myles Watson | 7cc9c40 | 2017-03-03 14:58:47 -0800 | [diff] [blame] | 35 | BluetoothHci::BluetoothHci() |
| 36 | : deathRecipient(new BluetoothDeathRecipient(this)) {} |
| 37 | |
Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [diff] [blame] | 38 | Return<void> BluetoothHci::initialize( |
| 39 | const ::android::sp<IBluetoothHciCallbacks>& cb) { |
| 40 | ALOGI("BluetoothHci::initialize()"); |
| 41 | |
Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [diff] [blame] | 42 | hci_tty_fd_ = open("/dev/hci_tty", O_RDWR); |
| 43 | if (hci_tty_fd_ < 0) { |
Myles Watson | 761dc49 | 2017-03-03 13:50:49 -0800 | [diff] [blame] | 44 | ALOGE("%s: Can't open hci_tty (%s)", __func__, strerror(errno)); |
| 45 | cb->initializationComplete(Status::INITIALIZATION_ERROR); |
| 46 | return Void(); |
Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Myles Watson | 761dc49 | 2017-03-03 13:50:49 -0800 | [diff] [blame] | 49 | event_cb_ = cb; |
Myles Watson | 7cc9c40 | 2017-03-03 14:58:47 -0800 | [diff] [blame] | 50 | event_cb_->linkToDeath(deathRecipient, 0); |
Myles Watson | 761dc49 | 2017-03-03 13:50:49 -0800 | [diff] [blame] | 51 | |
| 52 | hci_ = new hci::H4Protocol( |
| 53 | hci_tty_fd_, |
| 54 | [cb](const hidl_vec<uint8_t>& packet) { cb->hciEventReceived(packet); }, |
| 55 | [cb](const hidl_vec<uint8_t>& packet) { cb->aclDataReceived(packet); }, |
| 56 | [cb](const hidl_vec<uint8_t>& packet) { cb->scoDataReceived(packet); }); |
| 57 | |
| 58 | // Use a socket pair to enforce the TI FIONREAD requirement. |
| 59 | int sockfd[2]; |
| 60 | socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd); |
| 61 | int shim_fd = sockfd[0]; |
| 62 | int for_hci = sockfd[1]; |
| 63 | |
| 64 | fd_watcher_.WatchFdForNonBlockingReads(hci_tty_fd_, [this, shim_fd](int fd) { |
| 65 | int tty_bytes = 0; |
| 66 | if (TEMP_FAILURE_RETRY(ioctl(fd, FIONREAD, &tty_bytes))) |
| 67 | ALOGE("%s:FIONREAD %s", __func__, strerror(errno)); |
| 68 | ALOGV("%s:tty_bytes = %d", __func__, tty_bytes); |
| 69 | |
| 70 | uint8_t* tmp_buffer = new uint8_t[tty_bytes]; |
| 71 | size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, tmp_buffer, tty_bytes)); |
| 72 | CHECK(static_cast<int>(bytes_read) == tty_bytes); |
| 73 | size_t bytes_written = |
| 74 | TEMP_FAILURE_RETRY(write(shim_fd, tmp_buffer, tty_bytes)); |
| 75 | CHECK(static_cast<int>(bytes_written) == tty_bytes); |
| 76 | delete[] tmp_buffer; |
| 77 | }); |
Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [diff] [blame] | 78 | |
| 79 | fd_watcher_.WatchFdForNonBlockingReads( |
Myles Watson | 761dc49 | 2017-03-03 13:50:49 -0800 | [diff] [blame] | 80 | for_hci, [this](int fd) { hci_->OnDataReady(fd); }); |
Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [diff] [blame] | 81 | |
Myles Watson | 761dc49 | 2017-03-03 13:50:49 -0800 | [diff] [blame] | 82 | cb->initializationComplete(Status::SUCCESS); |
Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [diff] [blame] | 83 | return Void(); |
| 84 | } |
| 85 | |
| 86 | Return<void> BluetoothHci::close() { |
Myles Watson | 761dc49 | 2017-03-03 13:50:49 -0800 | [diff] [blame] | 87 | ALOGI("BluetoothHci::close()"); |
| 88 | |
| 89 | if (hci_tty_fd_ >= 0) { |
| 90 | fd_watcher_.StopWatchingFileDescriptors(); |
| 91 | ::close(hci_tty_fd_); |
| 92 | hci_tty_fd_ = -1; |
| 93 | } |
| 94 | |
Myles Watson | 7cc9c40 | 2017-03-03 14:58:47 -0800 | [diff] [blame] | 95 | event_cb_->unlinkToDeath(deathRecipient); |
| 96 | |
Myles Watson | 761dc49 | 2017-03-03 13:50:49 -0800 | [diff] [blame] | 97 | if (hci_ != nullptr) { |
| 98 | delete hci_; |
| 99 | hci_ = nullptr; |
| 100 | } |
| 101 | |
Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [diff] [blame] | 102 | return Void(); |
| 103 | } |
| 104 | |
| 105 | Return<void> BluetoothHci::sendHciCommand(const hidl_vec<uint8_t>& packet) { |
Myles Watson | 761dc49 | 2017-03-03 13:50:49 -0800 | [diff] [blame] | 106 | hci_->Send(HCI_PACKET_TYPE_COMMAND, packet.data(), packet.size()); |
Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [diff] [blame] | 107 | return Void(); |
| 108 | } |
| 109 | |
| 110 | Return<void> BluetoothHci::sendAclData(const hidl_vec<uint8_t>& packet) { |
Myles Watson | 761dc49 | 2017-03-03 13:50:49 -0800 | [diff] [blame] | 111 | hci_->Send(HCI_PACKET_TYPE_ACL_DATA, packet.data(), packet.size()); |
Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [diff] [blame] | 112 | return Void(); |
| 113 | } |
| 114 | |
| 115 | Return<void> BluetoothHci::sendScoData(const hidl_vec<uint8_t>& packet) { |
Myles Watson | 761dc49 | 2017-03-03 13:50:49 -0800 | [diff] [blame] | 116 | hci_->Send(HCI_PACKET_TYPE_SCO_DATA, packet.data(), packet.size()); |
Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [diff] [blame] | 117 | return Void(); |
| 118 | } |
| 119 | |
Myles Watson | 90ee15b | 2017-02-09 16:47:33 -0800 | [diff] [blame] | 120 | } // namespace hikey |
| 121 | } // namespace V1_0 |
| 122 | } // namespace bluetooth |
| 123 | } // namespace hardware |
| 124 | } // namespace android |