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> |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace bluetooth { |
| 29 | namespace hci { |
| 30 | |
| 31 | size_t H4Protocol::Send(uint8_t type, const uint8_t* data, size_t length) { |
Peng Qi | 92afd74 | 2017-06-19 18:01:07 +0800 | [diff] [blame] | 32 | struct iovec iov[] = {{&type, sizeof(type)}, |
| 33 | {const_cast<uint8_t*>(data), length}}; |
| 34 | ssize_t ret = 0; |
| 35 | do { |
| 36 | ret = TEMP_FAILURE_RETRY(writev(uart_fd_, iov, sizeof(iov) / sizeof(iov[0]))); |
| 37 | } while (-1 == ret && EAGAIN == errno); |
| 38 | |
| 39 | if (ret == -1) { |
| 40 | ALOGE("%s error writing to UART (%s)", __func__, strerror(errno)); |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 41 | } else if (ret < static_cast<ssize_t>(length + 1)) { |
| 42 | ALOGE("%s: %d / %d bytes written - something went wrong...", __func__, |
| 43 | static_cast<int>(ret), static_cast<int>(length + 1)); |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 44 | } |
Peng Qi | 92afd74 | 2017-06-19 18:01:07 +0800 | [diff] [blame] | 45 | return ret; |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void H4Protocol::OnPacketReady() { |
| 49 | switch (hci_packet_type_) { |
| 50 | case HCI_PACKET_TYPE_EVENT: |
| 51 | event_cb_(hci_packetizer_.GetPacket()); |
| 52 | break; |
| 53 | case HCI_PACKET_TYPE_ACL_DATA: |
| 54 | acl_cb_(hci_packetizer_.GetPacket()); |
| 55 | break; |
| 56 | case HCI_PACKET_TYPE_SCO_DATA: |
| 57 | sco_cb_(hci_packetizer_.GetPacket()); |
| 58 | break; |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 59 | default: |
| 60 | LOG_ALWAYS_FATAL("%s: Unimplemented packet type %d", __func__, |
| 61 | static_cast<int>(hci_packet_type_)); |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 62 | } |
| 63 | // Get ready for the next type byte. |
| 64 | hci_packet_type_ = HCI_PACKET_TYPE_UNKNOWN; |
| 65 | } |
| 66 | |
| 67 | void H4Protocol::OnDataReady(int fd) { |
| 68 | if (hci_packet_type_ == HCI_PACKET_TYPE_UNKNOWN) { |
| 69 | uint8_t buffer[1] = {0}; |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 70 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1)); |
| 71 | if (bytes_read != 1) { |
| 72 | if (bytes_read == 0) { |
| 73 | LOG_ALWAYS_FATAL("%s: Unexpected EOF reading the packet type!", |
| 74 | __func__); |
| 75 | } else if (bytes_read < 0) { |
| 76 | LOG_ALWAYS_FATAL("%s: Read packet type error: %s", __func__, |
| 77 | strerror(errno)); |
| 78 | } else { |
| 79 | LOG_ALWAYS_FATAL("%s: More bytes read than expected (%u)!", __func__, |
| 80 | static_cast<unsigned int>(bytes_read)); |
| 81 | } |
| 82 | } |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 83 | hci_packet_type_ = static_cast<HciPacketType>(buffer[0]); |
| 84 | } else { |
| 85 | hci_packetizer_.OnDataReady(fd, hci_packet_type_); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | } // namespace hci |
| 90 | } // namespace bluetooth |
| 91 | } // namespace hardware |
| 92 | } // namespace android |