Myles Watson | 90ee15b | 2017-02-09 16:47:33 -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_hikey.h" |
| 18 | |
| 19 | #include <sys/ioctl.h> |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include <android-base/logging.h> |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | const size_t preamble_size_for_type[] = { |
| 27 | 0, HCI_COMMAND_PREAMBLE_SIZE, HCI_ACL_PREAMBLE_SIZE, HCI_SCO_PREAMBLE_SIZE, |
| 28 | HCI_EVENT_PREAMBLE_SIZE}; |
| 29 | const size_t packet_length_offset_for_type[] = { |
| 30 | 0, HCI_LENGTH_OFFSET_CMD, HCI_LENGTH_OFFSET_ACL, HCI_LENGTH_OFFSET_SCO, |
| 31 | HCI_LENGTH_OFFSET_EVT}; |
| 32 | |
| 33 | size_t HciGetPacketLengthForType(HciPacketType type, const uint8_t* preamble) { |
| 34 | size_t offset = packet_length_offset_for_type[type]; |
| 35 | if (type != HCI_PACKET_TYPE_ACL_DATA) return preamble[offset]; |
| 36 | return (((preamble[offset + 1]) << 8) | preamble[offset]); |
| 37 | } |
| 38 | |
| 39 | } // namespace |
| 40 | |
| 41 | namespace android { |
| 42 | namespace hardware { |
| 43 | namespace bluetooth { |
| 44 | namespace V1_0 { |
| 45 | namespace hikey { |
| 46 | |
| 47 | void HciPacketizerHikey::OnDataReadyHikey(int fd) { |
| 48 | int tty_bytes = 0; |
| 49 | if (ioctl(fd, FIONREAD, &tty_bytes) == -1) |
| 50 | ALOGE("%s:FIONREAD %s", __func__, strerror(errno)); |
| 51 | ALOGV("%s:tty_bytes = %d", __func__, tty_bytes); |
| 52 | uint8_t* tmp_buffer = new uint8_t[tty_bytes]; |
| 53 | size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, tmp_buffer, tty_bytes)); |
| 54 | CHECK(static_cast<int>(bytes_read) == tty_bytes); |
| 55 | |
| 56 | size_t index = 0; |
| 57 | while (index < bytes_read - 1) { |
| 58 | switch (hci_parser_state_) { |
| 59 | case HCI_IDLE: { |
| 60 | hci_packet_type_ = static_cast<HciPacketType>(tmp_buffer[index++]); |
| 61 | CHECK(hci_packet_type_ >= HCI_PACKET_TYPE_ACL_DATA && |
| 62 | hci_packet_type_ <= HCI_PACKET_TYPE_EVENT) |
| 63 | << "hci_packet_type_ = " |
| 64 | << static_cast<unsigned int>(hci_packet_type_); |
| 65 | hci_parser_state_ = HCI_TYPE_READY; |
| 66 | hci_packet_bytes_remaining_ = preamble_size_for_type[hci_packet_type_]; |
| 67 | hci_packet_bytes_read_ = 0; |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | case HCI_TYPE_READY: { |
| 72 | size_t bytes_to_copy = (bytes_read - index < hci_packet_bytes_remaining_ |
| 73 | ? bytes_read - index |
| 74 | : hci_packet_bytes_remaining_); |
| 75 | memcpy(hci_packet_preamble_ + hci_packet_bytes_read_, |
| 76 | &tmp_buffer[index], bytes_to_copy); |
| 77 | hci_packet_bytes_remaining_ -= bytes_to_copy; |
| 78 | hci_packet_bytes_read_ += bytes_to_copy; |
| 79 | index += bytes_to_copy; |
| 80 | ALOGV("%s:TYPE index = %d", __func__, static_cast<int>(index)); |
| 81 | if (hci_packet_bytes_remaining_ == 0) { |
| 82 | size_t packet_length = |
| 83 | HciGetPacketLengthForType(hci_packet_type_, hci_packet_preamble_); |
| 84 | hci_packet_.resize(preamble_size_for_type[hci_packet_type_] + |
| 85 | packet_length); |
| 86 | memcpy(hci_packet_.data(), hci_packet_preamble_, |
| 87 | preamble_size_for_type[hci_packet_type_]); |
| 88 | hci_packet_bytes_remaining_ = packet_length; |
| 89 | hci_parser_state_ = HCI_PAYLOAD; |
| 90 | hci_packet_bytes_read_ = 0; |
| 91 | } |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | case HCI_PAYLOAD: { |
| 96 | size_t bytes_to_copy = (bytes_read - index < hci_packet_bytes_remaining_ |
| 97 | ? bytes_read - index |
| 98 | : hci_packet_bytes_remaining_); |
| 99 | memcpy(hci_packet_.data() + preamble_size_for_type[hci_packet_type_] + |
| 100 | hci_packet_bytes_read_, |
| 101 | &tmp_buffer[index], bytes_to_copy); |
| 102 | hci_packet_bytes_remaining_ -= bytes_to_copy; |
| 103 | hci_packet_bytes_read_ += bytes_to_copy; |
| 104 | index += bytes_to_copy; |
| 105 | ALOGV("%s:PAYLOAD index = %d", __func__, static_cast<int>(index)); |
| 106 | if (hci_packet_bytes_remaining_ == 0) { |
| 107 | hci_packet_ready_cb_(); |
| 108 | hci_parser_state_ = HCI_IDLE; |
| 109 | } |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | delete[] tmp_buffer; |
| 115 | } |
| 116 | |
| 117 | } // namespace hikey |
| 118 | } // namespace V1_0 |
| 119 | } // namespace bluetooth |
| 120 | } // namespace hardware |
| 121 | } // namespace android |