Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2017 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 "h4_protocol.h" |
| 18 | |
| 19 | #define LOG_TAG "android.hardware.bluetooth-hci-h4" |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 20 | |
Peng Qi | 92afd74 | 2017-06-19 18:01:07 +0800 | [diff] [blame] | 21 | #include <errno.h> |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 22 | #include <fcntl.h> |
Peng Qi | 92afd74 | 2017-06-19 18:01:07 +0800 | [diff] [blame] | 23 | #include <log/log.h> |
| 24 | #include <sys/uio.h> |
Jiyong Park | cb198f7 | 2017-07-10 13:27:05 +0900 | [diff] [blame] | 25 | #include <unistd.h> |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | namespace bluetooth { |
| 30 | namespace hci { |
| 31 | |
| 32 | size_t H4Protocol::Send(uint8_t type, const uint8_t* data, size_t length) { |
Sukesh Srikakula | 1e2b234 | 2021-10-15 17:17:48 -0700 | [diff] [blame] | 33 | struct iovec iov_array[] = {{&type, sizeof(type)}, |
| 34 | {const_cast<uint8_t*>(data), length}}; |
| 35 | struct iovec* iov = iov_array; |
| 36 | int iovcnt = sizeof(iov_array) / sizeof(iov_array[0]); |
| 37 | size_t total_bytes = 0; |
| 38 | for (int i = 0; i < iovcnt; i++) { |
| 39 | total_bytes += iov_array[i].iov_len; |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 40 | } |
Sukesh Srikakula | 1e2b234 | 2021-10-15 17:17:48 -0700 | [diff] [blame] | 41 | size_t bytes_written = 0; |
| 42 | size_t remaining_bytes = total_bytes; |
| 43 | |
| 44 | while (remaining_bytes > 0) { |
| 45 | ssize_t ret = TEMP_FAILURE_RETRY(writev(uart_fd_, iov, iovcnt)); |
| 46 | if (ret == -1) { |
| 47 | if (errno == EAGAIN) continue; |
| 48 | ALOGE("%s error writing to UART (%s)", __func__, strerror(errno)); |
| 49 | break; |
| 50 | } else if (ret == 0) { |
| 51 | // Nothing written |
| 52 | ALOGE("%s zero bytes written - something went wrong...", __func__); |
| 53 | break; |
| 54 | } else if (ret == remaining_bytes) { |
| 55 | // Everything written |
| 56 | bytes_written += ret; |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | bytes_written += ret; |
| 61 | remaining_bytes -= ret; |
| 62 | ALOGW("%s: %d/%d bytes written - retrying remaining %d bytes", __func__, |
| 63 | static_cast<int>(bytes_written), static_cast<int>(total_bytes), |
| 64 | static_cast<int>(remaining_bytes)); |
| 65 | |
| 66 | // Remove iovs which are written from the list |
| 67 | while (ret >= iov->iov_len) { |
| 68 | ret -= iov->iov_len; |
| 69 | ++iov; |
| 70 | --iovcnt; |
| 71 | } |
| 72 | // Adjust the iov to point to the remaining data which needs to be written |
| 73 | if (ret) { |
| 74 | iov->iov_base = static_cast<uint8_t*>(iov->iov_base) + ret; |
| 75 | iov->iov_len -= ret; |
| 76 | } |
| 77 | } |
| 78 | return bytes_written; |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void H4Protocol::OnPacketReady() { |
| 82 | switch (hci_packet_type_) { |
| 83 | case HCI_PACKET_TYPE_EVENT: |
| 84 | event_cb_(hci_packetizer_.GetPacket()); |
| 85 | break; |
| 86 | case HCI_PACKET_TYPE_ACL_DATA: |
| 87 | acl_cb_(hci_packetizer_.GetPacket()); |
| 88 | break; |
| 89 | case HCI_PACKET_TYPE_SCO_DATA: |
| 90 | sco_cb_(hci_packetizer_.GetPacket()); |
| 91 | break; |
Jakub Pawlowski | 13b4d31 | 2019-11-05 12:27:29 +0100 | [diff] [blame] | 92 | case HCI_PACKET_TYPE_ISO_DATA: |
| 93 | iso_cb_(hci_packetizer_.GetPacket()); |
| 94 | break; |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 95 | default: |
| 96 | LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__, |
| 97 | static_cast<int>(hci_packet_type_)); |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 98 | } |
| 99 | // Get ready for the next type byte. |
| 100 | hci_packet_type_ = HCI_PACKET_TYPE_UNKNOWN; |
| 101 | } |
| 102 | |
| 103 | void H4Protocol::OnDataReady(int fd) { |
| 104 | if (hci_packet_type_ == HCI_PACKET_TYPE_UNKNOWN) { |
| 105 | uint8_t buffer[1] = {0}; |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 106 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1)); |
| 107 | if (bytes_read != 1) { |
| 108 | if (bytes_read == 0) { |
Myles Watson | 0d63f8a | 2018-01-08 16:49:13 -0800 | [diff] [blame] | 109 | // This is only expected if the UART got closed when shutting down. |
| 110 | ALOGE("%s: Unexpected EOF reading the packet type!", __func__); |
| 111 | sleep(5); // Expect to be shut down within 5 seconds. |
| 112 | return; |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 113 | } else if (bytes_read < 0) { |
| 114 | LOG_ALWAYS_FATAL("%s: Read packet type error: %s", __func__, |
| 115 | strerror(errno)); |
| 116 | } else { |
| 117 | LOG_ALWAYS_FATAL("%s: More bytes read than expected (%u)!", __func__, |
| 118 | static_cast<unsigned int>(bytes_read)); |
| 119 | } |
| 120 | } |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 121 | hci_packet_type_ = static_cast<HciPacketType>(buffer[0]); |
Myles Watson | 521621d | 2017-08-11 16:47:28 -0700 | [diff] [blame] | 122 | if (hci_packet_type_ != HCI_PACKET_TYPE_ACL_DATA && |
| 123 | hci_packet_type_ != HCI_PACKET_TYPE_SCO_DATA && |
Jakub Pawlowski | db779b4 | 2020-11-23 14:07:54 +0100 | [diff] [blame] | 124 | hci_packet_type_ != HCI_PACKET_TYPE_ISO_DATA && |
Myles Watson | 521621d | 2017-08-11 16:47:28 -0700 | [diff] [blame] | 125 | hci_packet_type_ != HCI_PACKET_TYPE_EVENT) { |
| 126 | LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__, |
| 127 | static_cast<int>(hci_packet_type_)); |
| 128 | } |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 129 | } else { |
| 130 | hci_packetizer_.OnDataReady(fd, hci_packet_type_); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } // namespace hci |
| 135 | } // namespace bluetooth |
| 136 | } // namespace hardware |
| 137 | } // namespace android |