blob: 41359200b4bee5fda2d44c9f9ca97a317ef880a3 [file] [log] [blame]
Myles Watsone4501e52022-09-30 06:20:50 -07001/*
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
22namespace android::hardware::bluetooth::hci {
23
24namespace {
25
26const size_t header_size_for_type[] = {0,
27 kCommandHeaderSize,
28 kAclHeaderSize,
29 kScoHeaderSize,
30 kEventHeaderSize,
31 kIsoHeaderSize};
32const size_t packet_length_offset_for_type[] = {0,
33 kCommandLengthOffset,
34 kAclLengthOffset,
35 kScoLengthOffset,
36 kEventLengthOffset,
37 kIsoLengthOffset};
38
39size_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
50const std::vector<uint8_t>& HciPacketizer::GetPacket() const { return packet_; }
51
52bool HciPacketizer::OnDataReady(PacketType packet_type,
53 const std::vector<uint8_t>& buffer,
Henri Chataing28453032023-04-19 18:14:03 +000054 size_t* offset) {
Myles Watsone4501e52022-09-30 06:20:50 -070055 bool packet_completed = false;
Henri Chataing28453032023-04-19 18:14:03 +000056 size_t bytes_available = buffer.size() - *offset;
57
Myles Watsone4501e52022-09-30 06:20:50 -070058 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 }
Henri Chataing28453032023-04-19 18:14:03 +000066
Myles Watsone4501e52022-09-30 06:20:50 -070067 size_t bytes_to_copy = std::min(bytes_remaining_, bytes_available);
Henri Chataing28453032023-04-19 18:14:03 +000068 packet_.insert(packet_.end(), buffer.begin() + *offset,
69 buffer.begin() + *offset + bytes_to_copy);
Myles Watsone4501e52022-09-30 06:20:50 -070070 bytes_remaining_ -= bytes_to_copy;
71 bytes_available -= bytes_to_copy;
Henri Chataing28453032023-04-19 18:14:03 +000072 *offset += bytes_to_copy;
73
Myles Watsone4501e52022-09-30 06:20:50 -070074 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) {
Henri Chataing28453032023-04-19 18:14:03 +000079 packet_completed = OnDataReady(packet_type, buffer, offset);
Myles Watsone4501e52022-09-30 06:20:50 -070080 }
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);
Henri Chataing28453032023-04-19 18:14:03 +000090 packet_.insert(packet_.end(), buffer.begin() + *offset,
91 buffer.begin() + *offset + bytes_to_copy);
Myles Watsone4501e52022-09-30 06:20:50 -070092 bytes_remaining_ -= bytes_to_copy;
Henri Chataing28453032023-04-19 18:14:03 +000093 *offset += bytes_to_copy;
Myles Watsone4501e52022-09-30 06:20:50 -070094 if (bytes_remaining_ == 0) {
95 state_ = HCI_HEADER;
96 packet_completed = true;
97 }
98 break;
99 }
100 }
Henri Chataing28453032023-04-19 18:14:03 +0000101
Myles Watsone4501e52022-09-30 06:20:50 -0700102 return packet_completed;
103}
104
105} // namespace android::hardware::bluetooth::hci