Myles Watson | 761dc49 | 2017-03-03 13:50:49 -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" |
| 20 | #include <android-base/logging.h> |
| 21 | #include <assert.h> |
| 22 | #include <fcntl.h> |
| 23 | |
| 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace bluetooth { |
| 27 | namespace hci { |
| 28 | |
| 29 | size_t H4Protocol::Send(uint8_t type, const uint8_t* data, size_t length) { |
| 30 | int rv = WriteSafely(uart_fd_, &type, sizeof(type)); |
| 31 | if (rv == sizeof(type)) { |
| 32 | rv = WriteSafely(uart_fd_, data, length); |
| 33 | } |
| 34 | return rv; |
| 35 | } |
| 36 | |
| 37 | void H4Protocol::OnPacketReady() { |
| 38 | switch (hci_packet_type_) { |
| 39 | case HCI_PACKET_TYPE_EVENT: |
| 40 | event_cb_(hci_packetizer_.GetPacket()); |
| 41 | break; |
| 42 | case HCI_PACKET_TYPE_ACL_DATA: |
| 43 | acl_cb_(hci_packetizer_.GetPacket()); |
| 44 | break; |
| 45 | case HCI_PACKET_TYPE_SCO_DATA: |
| 46 | sco_cb_(hci_packetizer_.GetPacket()); |
| 47 | break; |
| 48 | default: { |
| 49 | bool bad_packet_type = true; |
| 50 | CHECK(!bad_packet_type); |
| 51 | } |
| 52 | } |
| 53 | // Get ready for the next type byte. |
| 54 | hci_packet_type_ = HCI_PACKET_TYPE_UNKNOWN; |
| 55 | } |
| 56 | |
| 57 | void H4Protocol::OnDataReady(int fd) { |
| 58 | if (hci_packet_type_ == HCI_PACKET_TYPE_UNKNOWN) { |
| 59 | uint8_t buffer[1] = {0}; |
| 60 | size_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, 1)); |
| 61 | CHECK(bytes_read == 1); |
| 62 | hci_packet_type_ = static_cast<HciPacketType>(buffer[0]); |
| 63 | } else { |
| 64 | hci_packetizer_.OnDataReady(fd, hci_packet_type_); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | } // namespace hci |
| 69 | } // namespace bluetooth |
| 70 | } // namespace hardware |
| 71 | } // namespace android |