2 // Copyright 2017 The Android Open Source Project
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "include/hci.h"
24 namespace test_vendor_lib {
27 using HciPacketReadyCallback = std::function<void(void)>;
31 HciPacketizer(HciPacketReadyCallback packet_cb) : packet_ready_cb_(packet_cb){};
32 void OnDataReady(int fd, hci::PacketType packet_type);
33 const std::vector<uint8_t>& GetPacket() const;
36 // 2 bytes for opcode, 1 byte for parameter length (Volume 2, Part E, 5.4.1)
37 static constexpr size_t COMMAND_PREAMBLE_SIZE = 3;
38 static constexpr size_t COMMAND_LENGTH_OFFSET = 2;
40 // 2 bytes for handle, 2 bytes for data length (Volume 2, Part E, 5.4.2)
41 static constexpr size_t ACL_PREAMBLE_SIZE = 4;
42 static constexpr size_t ACL_LENGTH_OFFSET = 2;
44 // 2 bytes for handle, 1 byte for data length (Volume 2, Part E, 5.4.3)
45 static constexpr size_t SCO_PREAMBLE_SIZE = 3;
46 static constexpr size_t SCO_LENGTH_OFFSET = 2;
48 // 1 byte for event code, 1 byte for parameter length (Volume 2, Part
50 static constexpr size_t EVENT_PREAMBLE_SIZE = 2;
51 static constexpr size_t EVENT_LENGTH_OFFSET = 1;
53 static constexpr size_t PREAMBLE_SIZE_MAX = ACL_PREAMBLE_SIZE;
55 size_t HciGetPacketLengthForType(hci::PacketType type, const uint8_t* preamble);
58 enum State { HCI_PREAMBLE, HCI_PAYLOAD };
59 State state_{HCI_PREAMBLE};
60 uint8_t preamble_[PREAMBLE_SIZE_MAX];
61 std::vector<uint8_t> packet_;
62 size_t bytes_remaining_{0};
63 size_t bytes_read_{0};
64 HciPacketReadyCallback packet_ready_cb_;
68 } // namespace test_vendor_lib