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 #include "model/hci/h4_parser.h" // for H4Parser, PacketType, H4Pars...
18
19 #include <array>
20 #include <cstddef> // for size_t
21 #include <cstdint> // for uint8_t, int32_t
22 #include <functional> // for function
23 #include <utility> // for move
24 #include <vector> // for vector
25
26 #include "log.h"
27
28 namespace rootcanal {
29
Reset()30 void H4Parser::Reset() {
31 state_ = HCI_TYPE;
32 packet_.clear();
33 bytes_wanted_ = 0;
34 packet_type_ = 0;
35 }
36
HciGetPacketLengthForType(PacketType type,const uint8_t * preamble)37 size_t H4Parser::HciGetPacketLengthForType(PacketType type,
38 const uint8_t* preamble) {
39 static const size_t
40 packet_length_offset[static_cast<size_t>(PacketType::ISO) + 1] = {
41 0,
42 H4Parser::COMMAND_LENGTH_OFFSET,
43 H4Parser::ACL_LENGTH_OFFSET,
44 H4Parser::SCO_LENGTH_OFFSET,
45 H4Parser::EVENT_LENGTH_OFFSET,
46 H4Parser::ISO_LENGTH_OFFSET,
47 };
48
49 size_t offset = packet_length_offset[static_cast<size_t>(type)];
50 size_t size = preamble[offset];
51 if (type == PacketType::ACL) {
52 size |= ((size_t)preamble[offset + 1]) << 8;
53 }
54 if (type == PacketType::ISO) {
55 size |= ((size_t)preamble[offset + 1] & 0x0fU) << 8;
56 }
57 return size;
58 }
59
H4Parser(PacketReadCallback command_cb,PacketReadCallback event_cb,PacketReadCallback acl_cb,PacketReadCallback sco_cb,PacketReadCallback iso_cb,bool enable_recovery_state)60 H4Parser::H4Parser(PacketReadCallback command_cb, PacketReadCallback event_cb,
61 PacketReadCallback acl_cb, PacketReadCallback sco_cb,
62 PacketReadCallback iso_cb, bool enable_recovery_state)
63 : command_cb_(std::move(command_cb)),
64 event_cb_(std::move(event_cb)),
65 acl_cb_(std::move(acl_cb)),
66 sco_cb_(std::move(sco_cb)),
67 iso_cb_(std::move(iso_cb)),
68 enable_recovery_state_(enable_recovery_state) {}
69
OnPacketReady()70 void H4Parser::OnPacketReady() {
71 switch (hci_packet_type_) {
72 case PacketType::COMMAND:
73 command_cb_(packet_);
74 break;
75 case PacketType::ACL:
76 acl_cb_(packet_);
77 break;
78 case PacketType::SCO:
79 sco_cb_(packet_);
80 break;
81 case PacketType::EVENT:
82 event_cb_(packet_);
83 break;
84 case PacketType::ISO:
85 iso_cb_(packet_);
86 break;
87 default:
88 FATAL("Unimplemented packet type {}", fmt::underlying(hci_packet_type_));
89 }
90 // Get ready for the next type byte.
91 hci_packet_type_ = PacketType::UNKNOWN;
92 }
93
BytesRequested()94 size_t H4Parser::BytesRequested() {
95 switch (state_) {
96 case HCI_TYPE:
97 case HCI_RECOVERY:
98 return 1;
99 case HCI_PREAMBLE:
100 case HCI_PAYLOAD:
101 return bytes_wanted_;
102 }
103 }
104
Consume(const uint8_t * buffer,int32_t bytes_read)105 bool H4Parser::Consume(const uint8_t* buffer, int32_t bytes_read) {
106 size_t bytes_to_read = BytesRequested();
107 if (bytes_read <= 0) {
108 INFO("remote disconnected, or unhandled error?");
109 return false;
110 }
111 if ((uint32_t)bytes_read > BytesRequested()) {
112 FATAL("More bytes read ({}) than expected ({})!", bytes_read,
113 bytes_to_read);
114 }
115
116 static const size_t preamble_size[static_cast<size_t>(PacketType::ISO) + 1] =
117 {
118 0,
119 H4Parser::COMMAND_PREAMBLE_SIZE,
120 H4Parser::ACL_PREAMBLE_SIZE,
121 H4Parser::SCO_PREAMBLE_SIZE,
122 H4Parser::EVENT_PREAMBLE_SIZE,
123 H4Parser::ISO_PREAMBLE_SIZE,
124 };
125 switch (state_) {
126 case HCI_TYPE:
127 // bytes_read >= 1
128 packet_type_ = *buffer;
129 packet_.clear();
130 break;
131
132 case HCI_RECOVERY: {
133 // Skip all received bytes until the HCI Reset command is received.
134 // The parser can end up in a bad state when the host is restarted.
135 const std::array<uint8_t, 4> reset_command{0x01, 0x03, 0x0c, 0x00};
136 size_t offset = packet_.size();
137 WARNING("Received byte in recovery state : 0x{:x}", *buffer);
138 packet_.push_back(*buffer);
139
140 // Last byte does not match expected byte in the sequence.
141 // Drop all the bytes and start over.
142 if (packet_[offset] != reset_command[offset]) {
143 packet_.clear();
144 // The mismatched byte can also be the first of the correct sequence.
145 if (*buffer == reset_command[0]) {
146 packet_.push_back(*buffer);
147 }
148 }
149
150 // Received full reset command.
151 if (packet_.size() == reset_command.size()) {
152 INFO("Received HCI Reset command, exiting recovery state");
153 // Pop the Idc from the received packet.
154 packet_.erase(packet_.begin());
155 bytes_wanted_ = 0;
156 }
157 break;
158 }
159
160 case HCI_PREAMBLE:
161 case HCI_PAYLOAD:
162 packet_.insert(packet_.end(), buffer, buffer + bytes_read);
163 bytes_wanted_ -= bytes_read;
164 break;
165 }
166
167 switch (state_) {
168 case HCI_TYPE:
169 hci_packet_type_ = static_cast<PacketType>(packet_type_);
170 if (hci_packet_type_ != PacketType::ACL &&
171 hci_packet_type_ != PacketType::SCO &&
172 hci_packet_type_ != PacketType::COMMAND &&
173 hci_packet_type_ != PacketType::EVENT &&
174 hci_packet_type_ != PacketType::ISO) {
175 if (!enable_recovery_state_) {
176 FATAL("Received invalid packet type 0x{:x}", packet_type_);
177 }
178 ERROR("Received invalid packet type 0x{:x}, entering recovery state",
179 packet_type_);
180 state_ = HCI_RECOVERY;
181 hci_packet_type_ = PacketType::COMMAND;
182 bytes_wanted_ = 1;
183 } else {
184 state_ = HCI_PREAMBLE;
185 bytes_wanted_ = preamble_size[static_cast<size_t>(hci_packet_type_)];
186 }
187 break;
188 case HCI_PREAMBLE:
189 if (bytes_wanted_ == 0) {
190 size_t payload_size =
191 HciGetPacketLengthForType(hci_packet_type_, packet_.data());
192 if (payload_size == 0) {
193 OnPacketReady();
194 state_ = HCI_TYPE;
195 } else {
196 bytes_wanted_ = payload_size;
197 state_ = HCI_PAYLOAD;
198 }
199 }
200 break;
201 case HCI_RECOVERY:
202 case HCI_PAYLOAD:
203 if (bytes_wanted_ == 0) {
204 OnPacketReady();
205 state_ = HCI_TYPE;
206 }
207 break;
208 }
209 return true;
210 }
211 } // namespace rootcanal
212