OSDN Git Service

RootCanal: New Directory Structure
[android-x86/system-bt.git] / vendor_libs / test_vendor_lib / model / devices / hci_packetizer.h
1 //
2 // Copyright 2017 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 #pragma once
18
19 #include <functional>
20 #include <vector>
21
22 #include "include/hci.h"
23
24 namespace test_vendor_lib {
25 namespace hci {
26
27 using HciPacketReadyCallback = std::function<void(void)>;
28
29 class HciPacketizer {
30  public:
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;
34
35  private:
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;
39
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;
43
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;
47
48   // 1 byte for event code, 1 byte for parameter length (Volume 2, Part
49   // E, 5.4.4)
50   static constexpr size_t EVENT_PREAMBLE_SIZE = 2;
51   static constexpr size_t EVENT_LENGTH_OFFSET = 1;
52
53   static constexpr size_t PREAMBLE_SIZE_MAX = ACL_PREAMBLE_SIZE;
54
55   size_t HciGetPacketLengthForType(hci::PacketType type, const uint8_t* preamble);
56
57  protected:
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_;
65 };
66
67 }  // namespace hci
68 }  // namespace test_vendor_lib