Myles Watson | 274a381 | 2017-02-23 06:29:08 -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 "hci_packetizer.h" |
| 18 | |
| 19 | #define LOG_TAG "android.hardware.bluetooth.hci_packetizer" |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 20 | |
| 21 | #include <dlfcn.h> |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 22 | #include <errno.h> |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 23 | #include <fcntl.h> |
Jiyong Park | cb198f7 | 2017-07-10 13:27:05 +0900 | [diff] [blame] | 24 | #include <unistd.h> |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 25 | #include <utils/Log.h> |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | |
Jakub Pawlowski | db779b4 | 2020-11-23 14:07:54 +0100 | [diff] [blame] | 29 | const size_t preamble_size_for_type[] = {0, |
| 30 | HCI_COMMAND_PREAMBLE_SIZE, |
| 31 | HCI_ACL_PREAMBLE_SIZE, |
| 32 | HCI_SCO_PREAMBLE_SIZE, |
| 33 | HCI_EVENT_PREAMBLE_SIZE, |
| 34 | HCI_ISO_PREAMBLE_SIZE}; |
| 35 | const size_t packet_length_offset_for_type[] = {0, |
| 36 | HCI_LENGTH_OFFSET_CMD, |
| 37 | HCI_LENGTH_OFFSET_ACL, |
| 38 | HCI_LENGTH_OFFSET_SCO, |
| 39 | HCI_LENGTH_OFFSET_EVT, |
| 40 | HCI_LENGTH_OFFSET_ISO}; |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 41 | |
| 42 | size_t HciGetPacketLengthForType(HciPacketType type, const uint8_t* preamble) { |
| 43 | size_t offset = packet_length_offset_for_type[type]; |
Jakub Pawlowski | db779b4 | 2020-11-23 14:07:54 +0100 | [diff] [blame] | 44 | if (type == HCI_PACKET_TYPE_ACL_DATA) { |
| 45 | return (((preamble[offset + 1]) << 8) | preamble[offset]); |
| 46 | } else if (type == HCI_PACKET_TYPE_ISO_DATA) { |
| 47 | return ((((preamble[offset + 1]) & 0x3f) << 8) | preamble[offset]); |
| 48 | } |
| 49 | return preamble[offset]; |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | } // namespace |
| 53 | |
| 54 | namespace android { |
| 55 | namespace hardware { |
| 56 | namespace bluetooth { |
| 57 | namespace hci { |
| 58 | |
Jack He | caeab05 | 2018-10-23 18:13:51 -0700 | [diff] [blame] | 59 | const hidl_vec<uint8_t>& HciPacketizer::GetPacket() const { return packet_; } |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 60 | |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 61 | void HciPacketizer::OnDataReady(int fd, HciPacketType packet_type) { |
| 62 | switch (state_) { |
| 63 | case HCI_PREAMBLE: { |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 64 | ssize_t bytes_read = TEMP_FAILURE_RETRY( |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 65 | read(fd, preamble_ + bytes_read_, |
| 66 | preamble_size_for_type[packet_type] - bytes_read_)); |
Myles Watson | 0d63f8a | 2018-01-08 16:49:13 -0800 | [diff] [blame] | 67 | if (bytes_read == 0) { |
| 68 | // This is only expected if the UART got closed when shutting down. |
| 69 | ALOGE("%s: Unexpected EOF reading the header!", __func__); |
| 70 | sleep(5); // Expect to be shut down within 5 seconds. |
| 71 | return; |
| 72 | } |
| 73 | if (bytes_read < 0) { |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 74 | LOG_ALWAYS_FATAL("%s: Read header error: %s", __func__, |
| 75 | strerror(errno)); |
| 76 | } |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 77 | bytes_read_ += bytes_read; |
| 78 | if (bytes_read_ == preamble_size_for_type[packet_type]) { |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 79 | size_t packet_length = |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 80 | HciGetPacketLengthForType(packet_type, preamble_); |
| 81 | packet_.resize(preamble_size_for_type[packet_type] + packet_length); |
| 82 | memcpy(packet_.data(), preamble_, preamble_size_for_type[packet_type]); |
| 83 | bytes_remaining_ = packet_length; |
| 84 | state_ = HCI_PAYLOAD; |
| 85 | bytes_read_ = 0; |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 86 | } |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | case HCI_PAYLOAD: { |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 91 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read( |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 92 | fd, |
| 93 | packet_.data() + preamble_size_for_type[packet_type] + bytes_read_, |
| 94 | bytes_remaining_)); |
Myles Watson | 0d63f8a | 2018-01-08 16:49:13 -0800 | [diff] [blame] | 95 | if (bytes_read == 0) { |
| 96 | // This is only expected if the UART got closed when shutting down. |
| 97 | ALOGE("%s: Unexpected EOF reading the payload!", __func__); |
| 98 | sleep(5); // Expect to be shut down within 5 seconds. |
| 99 | return; |
| 100 | } |
| 101 | if (bytes_read < 0) { |
Myles Watson | 3e272a7 | 2017-06-26 13:09:11 -0700 | [diff] [blame] | 102 | LOG_ALWAYS_FATAL("%s: Read payload error: %s", __func__, |
| 103 | strerror(errno)); |
| 104 | } |
Zach Johnson | 917efb1 | 2017-02-26 23:46:05 -0800 | [diff] [blame] | 105 | bytes_remaining_ -= bytes_read; |
| 106 | bytes_read_ += bytes_read; |
| 107 | if (bytes_remaining_ == 0) { |
| 108 | packet_ready_cb_(); |
| 109 | state_ = HCI_PREAMBLE; |
| 110 | bytes_read_ = 0; |
Myles Watson | 274a381 | 2017-02-23 06:29:08 -0800 | [diff] [blame] | 111 | } |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } // namespace hci |
| 118 | } // namespace bluetooth |
| 119 | } // namespace hardware |
| 120 | } // namespace android |