1 // 2 // Copyright 2021 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 <stddef.h> // for size_t 20 #include <cstdint> // for uint8_t, int32_t 21 #include <functional> // for function 22 #include <ostream> // for operator<<, ostream 23 #include <vector> // for vector 24 25 #include "model/devices/hci_protocol.h" // for PacketReadCallback 26 27 namespace test_vendor_lib { 28 29 using HciPacketReadyCallback = std::function<void(void)>; 30 using ClientDisconnectCallback = std::function<void()>; 31 32 enum class PacketType : uint8_t { 33 UNKNOWN = 0, 34 COMMAND = 1, 35 ACL = 2, 36 SCO = 3, 37 EVENT = 4, 38 ISO = 5, 39 }; 40 41 // An H4 Parser can parse H4 Packets and will invoke the proper callback 42 // once a packet has been parsed. 43 // 44 // You use it as follows: 45 // 46 // H4Parser h4(....); 47 // size_t nr_bytes = h4.BytesRequested(); 48 // std::vector fill_this_vector_with_at_most_nr_bytes(nr_bytes); 49 // h4.Consume(fill_this_vector_with_at_most_nr_bytes.data(), nr_bytes.size()); 50 // 51 // The parser will invoke the proper callbacks once a packet has been parsed. 52 // The parser keeps internal state and is not thread safe. 53 class H4Parser { 54 public: 55 enum State { HCI_TYPE, HCI_PREAMBLE, HCI_PAYLOAD }; 56 57 H4Parser(PacketReadCallback command_cb, PacketReadCallback event_cb, 58 PacketReadCallback acl_cb, PacketReadCallback sco_cb, 59 PacketReadCallback iso_cb); 60 61 // Consumes the given number of bytes, returns true on success. 62 bool Consume(uint8_t* buffer, int32_t bytes); 63 64 // The maximum number of bytes the parser can consume in the current state. 65 size_t BytesRequested(); 66 67 // Resets the parser to the empty, initial state. 68 void Reset(); 69 CurrentState()70 State CurrentState() { return state_; }; 71 72 private: 73 void OnPacketReady(); 74 75 // 2 bytes for opcode, 1 byte for parameter length (Volume 2, Part E, 5.4.1) 76 static constexpr size_t COMMAND_PREAMBLE_SIZE = 3; 77 static constexpr size_t COMMAND_LENGTH_OFFSET = 2; 78 79 // 2 bytes for handle, 2 bytes for data length (Volume 2, Part E, 5.4.2) 80 static constexpr size_t ACL_PREAMBLE_SIZE = 4; 81 static constexpr size_t ACL_LENGTH_OFFSET = 2; 82 83 // 2 bytes for handle, 1 byte for data length (Volume 2, Part E, 5.4.3) 84 static constexpr size_t SCO_PREAMBLE_SIZE = 3; 85 static constexpr size_t SCO_LENGTH_OFFSET = 2; 86 87 // 1 byte for event code, 1 byte for parameter length (Volume 2, Part 88 // E, 5.4.4) 89 static constexpr size_t EVENT_PREAMBLE_SIZE = 2; 90 static constexpr size_t EVENT_LENGTH_OFFSET = 1; 91 92 // 2 bytes for handle and flags, 12 bits for length (Volume 2, Part E, 5.4.5) 93 static constexpr size_t ISO_PREAMBLE_SIZE = 4; 94 static constexpr size_t ISO_LENGTH_OFFSET = 2; 95 96 PacketReadCallback command_cb_; 97 PacketReadCallback event_cb_; 98 PacketReadCallback acl_cb_; 99 PacketReadCallback sco_cb_; 100 PacketReadCallback iso_cb_; 101 102 size_t HciGetPacketLengthForType(PacketType type, 103 const uint8_t* preamble) const; 104 105 PacketType hci_packet_type_{PacketType::UNKNOWN}; 106 107 State state_{HCI_TYPE}; 108 uint8_t packet_type_{}; 109 std::vector<uint8_t> packet_; 110 size_t bytes_wanted_{0}; 111 }; 112 113 inline std::ostream& operator<<(std::ostream& os, 114 H4Parser::State const& state_) { 115 os << (state_ == H4Parser::State::HCI_TYPE 116 ? "HCI_TYPE" 117 : state_ == H4Parser::State::HCI_PREAMBLE ? "HCI_PREAMBLE" 118 : "HCI_PAYLOAD"); 119 return os; 120 } 121 122 } // namespace test_vendor_lib 123