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