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