aboutsummaryrefslogtreecommitdiff
path: root/tools/rootcanal/model/hci/h4_parser.h
blob: 1a8576067f36fe4953ec524f34779cbfedbd4e11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// Copyright 2021 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#pragma once

#include <stddef.h>  // for size_t

#include <cstdint>     // for uint8_t, int32_t
#include <functional>  // for function
#include <ostream>     // for operator<<, ostream
#include <vector>      // for vector

#include "model/hci/h4.h"            // for PacketType
#include "model/hci/hci_protocol.h"  // for PacketReadCallback

namespace rootcanal {

using HciPacketReadyCallback = std::function<void(void)>;
using ClientDisconnectCallback = std::function<void()>;

// An H4 Parser can parse H4 Packets and will invoke the proper callback
// once a packet has been parsed.
//
// You use it as follows:
//
// H4Parser h4(....);
// size_t nr_bytes = h4.BytesRequested();
// std::vector fill_this_vector_with_at_most_nr_bytes(nr_bytes);
// h4.Consume(fill_this_vector_with_at_most_nr_bytes.data(), nr_bytes.size());
//
// The parser will invoke the proper callbacks once a packet has been parsed.
// The parser keeps internal state and is not thread safe.
class H4Parser {
 public:
  enum State { HCI_TYPE, HCI_PREAMBLE, HCI_PAYLOAD, HCI_RECOVERY };

  H4Parser(PacketReadCallback command_cb, PacketReadCallback event_cb,
           PacketReadCallback acl_cb, PacketReadCallback sco_cb,
           PacketReadCallback iso_cb, bool enable_recovery_state = false);

  // Consumes the given number of bytes, returns true on success.
  bool Consume(const uint8_t* buffer, int32_t bytes);

  // The maximum number of bytes the parser can consume in the current state.
  size_t BytesRequested();

  // Resets the parser to the empty, initial state.
  void Reset();

  State CurrentState() { return state_; };

  void EnableRecovery() { enable_recovery_state_ = true; }
  void DisableRecovery() { enable_recovery_state_ = false; }

 private:
  void OnPacketReady();

  // 2 bytes for opcode, 1 byte for parameter length (Volume 2, Part E, 5.4.1)
  static constexpr size_t COMMAND_PREAMBLE_SIZE = 3;
  static constexpr size_t COMMAND_LENGTH_OFFSET = 2;
  // 2 bytes for handle, 2 bytes for data length (Volume 2, Part E, 5.4.2)
  static constexpr size_t ACL_PREAMBLE_SIZE = 4;
  static constexpr size_t ACL_LENGTH_OFFSET = 2;

  // 2 bytes for handle, 1 byte for data length (Volume 2, Part E, 5.4.3)
  static constexpr size_t SCO_PREAMBLE_SIZE = 3;
  static constexpr size_t SCO_LENGTH_OFFSET = 2;

  // 1 byte for event code, 1 byte for parameter length (Volume 2, Part
  // E, 5.4.4)
  static constexpr size_t EVENT_PREAMBLE_SIZE = 2;
  static constexpr size_t EVENT_LENGTH_OFFSET = 1;

  // 2 bytes for handle and flags, 12 bits for length (Volume 2, Part E, 5.4.5)
  static constexpr size_t ISO_PREAMBLE_SIZE = 4;
  static constexpr size_t ISO_LENGTH_OFFSET = 2;

  PacketReadCallback command_cb_;
  PacketReadCallback event_cb_;
  PacketReadCallback acl_cb_;
  PacketReadCallback sco_cb_;
  PacketReadCallback iso_cb_;

  size_t HciGetPacketLengthForType(PacketType type,
                                   const uint8_t* preamble) const;

  PacketType hci_packet_type_{PacketType::UNKNOWN};

  State state_{HCI_TYPE};
  uint8_t packet_type_{};
  std::vector<uint8_t> packet_;
  size_t bytes_wanted_{0};
  bool enable_recovery_state_{false};
};

inline std::ostream& operator<<(std::ostream& os,
                                H4Parser::State const& state_) {
  switch (state_) {
    case H4Parser::State::HCI_TYPE:
      os << "HCI_TYPE";
      break;
    case H4Parser::State::HCI_PREAMBLE:
      os << "HCI_PREAMBLE";
      break;
    case H4Parser::State::HCI_PAYLOAD:
      os << "HCI_PAYLOAD";
      break;
    case H4Parser::State::HCI_RECOVERY:
      os << "HCI_RECOVERY";
      break;
    default:
      os << "unknown state " << static_cast<int>(state_);
      break;
  }
  return os;
}

}  // namespace rootcanal