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 #pragma once 18 19 #include <functional> 20 #include <vector> 21 22 #include "hci_protocol.h" 23 24 namespace test_vendor_lib { 25 26 using HciPacketReadyCallback = std::function<void(void)>; 27 using ClientDisconnectCallback = std::function<void()>; 28 29 enum class PacketType : uint8_t { 30 UNKNOWN = 0, 31 COMMAND = 1, 32 ACL = 2, 33 SCO = 3, 34 EVENT = 4, 35 ISO = 5, 36 }; 37 38 class H4Packetizer : public HciProtocol { 39 public: 40 H4Packetizer(int fd, PacketReadCallback command_cb, 41 PacketReadCallback event_cb, PacketReadCallback acl_cb, 42 PacketReadCallback sco_cb, PacketReadCallback iso_cb, 43 ClientDisconnectCallback disconnect_cb); 44 45 size_t Send(uint8_t type, const uint8_t* data, size_t length) override; 46 47 void OnPacketReady(); 48 49 void OnDataReady(int fd); 50 51 // 2 bytes for opcode, 1 byte for parameter length (Volume 2, Part E, 5.4.1) 52 static constexpr size_t COMMAND_PREAMBLE_SIZE = 3; 53 static constexpr size_t COMMAND_LENGTH_OFFSET = 2; 54 55 // 2 bytes for handle, 2 bytes for data length (Volume 2, Part E, 5.4.2) 56 static constexpr size_t ACL_PREAMBLE_SIZE = 4; 57 static constexpr size_t ACL_LENGTH_OFFSET = 2; 58 59 // 2 bytes for handle, 1 byte for data length (Volume 2, Part E, 5.4.3) 60 static constexpr size_t SCO_PREAMBLE_SIZE = 3; 61 static constexpr size_t SCO_LENGTH_OFFSET = 2; 62 63 // 1 byte for event code, 1 byte for parameter length (Volume 2, Part 64 // E, 5.4.4) 65 static constexpr size_t EVENT_PREAMBLE_SIZE = 2; 66 static constexpr size_t EVENT_LENGTH_OFFSET = 1; 67 68 // 2 bytes for handle and flags, 12 bits for length (Volume 2, Part E, 5.4.5) 69 static constexpr size_t ISO_PREAMBLE_SIZE = 4; 70 static constexpr size_t ISO_LENGTH_OFFSET = 2; 71 72 private: 73 int uart_fd_; 74 75 PacketReadCallback command_cb_; 76 PacketReadCallback event_cb_; 77 PacketReadCallback acl_cb_; 78 PacketReadCallback sco_cb_; 79 PacketReadCallback iso_cb_; 80 81 ClientDisconnectCallback disconnect_cb_; 82 bool disconnected_{false}; 83 84 size_t HciGetPacketLengthForType(PacketType type, 85 const uint8_t* preamble) const; 86 87 PacketType hci_packet_type_{PacketType::UNKNOWN}; 88 89 enum State { HCI_TYPE, HCI_PREAMBLE, HCI_PAYLOAD }; 90 State state_{HCI_TYPE}; 91 uint8_t packet_type_{}; 92 std::vector<uint8_t> packet_; 93 size_t bytes_read_{0}; 94 }; 95 96 } // namespace test_vendor_lib 97