1 /*
2 * Copyright 2022 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 "hci_packetizer.h"
18
19 #define LOG_TAG "android.hardware.bluetooth.hci-packetizer"
20 #include "log/log.h"
21
22 namespace android::hardware::bluetooth::hci {
23
24 namespace {
25
26 const size_t header_size_for_type[] = {0,
27 kCommandHeaderSize,
28 kAclHeaderSize,
29 kScoHeaderSize,
30 kEventHeaderSize,
31 kIsoHeaderSize};
32 const size_t packet_length_offset_for_type[] = {0,
33 kCommandLengthOffset,
34 kAclLengthOffset,
35 kScoLengthOffset,
36 kEventLengthOffset,
37 kIsoLengthOffset};
38
HciGetPacketLengthForType(PacketType type,const std::vector<uint8_t> & header)39 size_t HciGetPacketLengthForType(PacketType type,
40 const std::vector<uint8_t>& header) {
41 size_t offset = packet_length_offset_for_type[static_cast<uint8_t>(type)];
42 if (type != PacketType::ACL_DATA && type != PacketType::ISO_DATA) {
43 return header[offset];
44 }
45 return (((header[offset + 1]) << 8) | header[offset]);
46 }
47
48 } // namespace
49
GetPacket() const50 const std::vector<uint8_t>& HciPacketizer::GetPacket() const { return packet_; }
51
OnDataReady(PacketType packet_type,const std::vector<uint8_t> & buffer,size_t * offset)52 bool HciPacketizer::OnDataReady(PacketType packet_type,
53 const std::vector<uint8_t>& buffer,
54 size_t* offset) {
55 bool packet_completed = false;
56 size_t bytes_available = buffer.size() - *offset;
57
58 switch (state_) {
59 case HCI_HEADER: {
60 size_t header_size =
61 header_size_for_type[static_cast<size_t>(packet_type)];
62 if (bytes_remaining_ == 0) {
63 bytes_remaining_ = header_size;
64 packet_.clear();
65 }
66
67 size_t bytes_to_copy = std::min(bytes_remaining_, bytes_available);
68 packet_.insert(packet_.end(), buffer.begin() + *offset,
69 buffer.begin() + *offset + bytes_to_copy);
70 bytes_remaining_ -= bytes_to_copy;
71 bytes_available -= bytes_to_copy;
72 *offset += bytes_to_copy;
73
74 if (bytes_remaining_ == 0) {
75 bytes_remaining_ = HciGetPacketLengthForType(packet_type, packet_);
76 if (bytes_remaining_ > 0) {
77 state_ = HCI_PAYLOAD;
78 if (bytes_available > 0) {
79 packet_completed = OnDataReady(packet_type, buffer, offset);
80 }
81 } else {
82 packet_completed = true;
83 }
84 }
85 break;
86 }
87
88 case HCI_PAYLOAD: {
89 size_t bytes_to_copy = std::min(bytes_remaining_, bytes_available);
90 packet_.insert(packet_.end(), buffer.begin() + *offset,
91 buffer.begin() + *offset + bytes_to_copy);
92 bytes_remaining_ -= bytes_to_copy;
93 *offset += bytes_to_copy;
94 if (bytes_remaining_ == 0) {
95 state_ = HCI_HEADER;
96 packet_completed = true;
97 }
98 break;
99 }
100 }
101
102 return packet_completed;
103 }
104
105 } // namespace android::hardware::bluetooth::hci
106