PcapPlusPlus  Next
BluetoothHciLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 
5 #include <iterator>
6 #include <algorithm>
7 #include <vector>
8 
17 
20 namespace pcpp
21 {
26  class BluetoothHciLayer : public Layer
27  {
28  public:
31  enum class BluetoothHciPacketType : uint8_t
32  {
34  Unknown = 0x00,
36  Command = 0x01,
38  AclData = 0x02,
40  ScoData = 0x03,
42  Event = 0x04,
44  IsoData = 0x05
45  };
46 
50  enum class BluetoothHciDirection : uint32_t
51  {
53  HostToController = 0,
55  ControllerToHost = 1,
57  Unknown = 0xffffffff
58  };
59 
60  ~BluetoothHciLayer() override = default;
61 
71  static BluetoothHciLayer* parseLayer(uint8_t* data, size_t dataLen, Packet* packet, bool hasDirectionHeader);
72 
74  bool hasDirectionHeader() const
75  {
76  return m_HasDirectionHeader;
77  }
78 
83 
87  {
88  return packetTypeFromIndicator(m_Data[getDirectionHeaderLen()]);
89  }
90 
91  // implement abstract methods
92 
95  void parseNextLayer() override
96  {}
97 
99  void computeCalculateFields() override
100  {}
101 
103  {
104  return OsiModelDataLinkLayer;
105  }
106 
113  static bool isDataValid(const uint8_t* data, size_t dataLen, bool hasDirectionHeader);
114 
115  protected:
118  static constexpr size_t directionHeaderSize = 4;
119 
125  BluetoothHciLayer(uint8_t* data, size_t dataLen, Packet* packet, bool hasDirectionHeader)
126  : Layer(data, dataLen, nullptr, packet, BluetoothHCI), m_HasDirectionHeader(hasDirectionHeader)
127  {}
128 
131  explicit BluetoothHciLayer(bool hasDirectionHeader) : m_HasDirectionHeader(hasDirectionHeader)
132  {
133  m_Protocol = BluetoothHCI;
134  }
135 
137  size_t getDirectionHeaderLen() const
138  {
139  return m_HasDirectionHeader ? directionHeaderSize : 0;
140  }
141 
142  private:
143 #pragma pack(push, 1)
144  struct bluetooth_hci_direction_header
145  {
146  uint32_t direction;
147  };
148 #pragma pack(pop)
149  static_assert(sizeof(bluetooth_hci_direction_header) == directionHeaderSize,
150  "bluetooth_hci_direction_header size does not match directionHeaderSize");
151 
152  static BluetoothHciPacketType packetTypeFromIndicator(uint8_t packetIndicator);
153 
154  bool m_HasDirectionHeader;
155  };
156 
160  {
161  friend BluetoothHciLayer* BluetoothHciLayer::parseLayer(uint8_t* data, size_t dataLen, Packet* packet,
162  bool hasDirectionHeader);
163 
164  public:
165  ~BluetoothHciEventLayer() override = default;
166 
172  explicit BluetoothHciEventLayer(uint8_t eventCode, const std::vector<uint8_t>& parameters = {});
173 
181  BluetoothHciEventLayer(BluetoothHciDirection direction, uint8_t eventCode,
182  const std::vector<uint8_t>& parameters = {});
183 
186  uint8_t getEventCode() const
187  {
188  return getEventHeader()->eventCode;
189  }
190 
193  void setEventCode(uint8_t eventCode)
194  {
195  getEventHeader()->eventCode = eventCode;
196  }
197 
201  uint8_t getParameterTotalLength() const;
202 
205  uint8_t* getParameters() const
206  {
207  return m_Data + getDirectionHeaderLen() + sizeof(uint8_t) + sizeof(bluetooth_hci_event_header);
208  }
209 
214  void setParameters(const std::vector<uint8_t>& parameters);
215 
216  // implement abstract methods
217 
220  size_t getHeaderLen() const override
221  {
222  return m_DataLen;
223  }
224 
225  std::string toString() const override;
226 
232  static bool isDataValid(const uint8_t* data, size_t dataLen, bool hasDirectionHeader);
233 
237  {
239  static std::vector<FieldDescriptor> all()
240  {
241  auto result = Layer::SerializedFields::all();
242  std::initializer_list<FieldDescriptor> extra{ Direction, EventCode, ParameterTotalLength };
243  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
244  return result;
245  }
246 
249 
252 
255  };
256 
257  protected:
258  void serializeLayer(ObjectScope& serializer) const override;
259 
260  private:
261 #pragma pack(push, 1)
262  struct bluetooth_hci_event_header
263  {
264  uint8_t eventCode;
265  uint8_t parameterTotalLength;
266  };
267 #pragma pack(pop)
268  static_assert(sizeof(bluetooth_hci_event_header) == 2, "bluetooth_hci_event_header size is not 2 bytes");
269 
276  BluetoothHciEventLayer(uint8_t* data, size_t dataLen, Packet* packet, bool hasDirectionHeader)
277  : BluetoothHciLayer(data, dataLen, packet, hasDirectionHeader)
278  {}
279 
284  void initLayer(uint8_t eventCode, const std::vector<uint8_t>& parameters);
285 
286  bluetooth_hci_event_header* getEventHeader() const
287  {
288  return reinterpret_cast<bluetooth_hci_event_header*>(m_Data + getDirectionHeaderLen() + sizeof(uint8_t));
289  }
290  };
291 
292 } // namespace pcpp
Definition: BluetoothHciLayer.h:160
void setEventCode(uint8_t eventCode)
Definition: BluetoothHciLayer.h:193
static bool isDataValid(const uint8_t *data, size_t dataLen, bool hasDirectionHeader)
uint8_t getEventCode() const
Definition: BluetoothHciLayer.h:186
size_t getHeaderLen() const override
Definition: BluetoothHciLayer.h:220
void setParameters(const std::vector< uint8_t > &parameters)
BluetoothHciEventLayer(BluetoothHciDirection direction, uint8_t eventCode, const std::vector< uint8_t > &parameters={})
uint8_t * getParameters() const
Definition: BluetoothHciLayer.h:205
std::string toString() const override
uint8_t getParameterTotalLength() const
void serializeLayer(ObjectScope &serializer) const override
BluetoothHciEventLayer(uint8_t eventCode, const std::vector< uint8_t > &parameters={})
Definition: BluetoothHciLayer.h:27
static bool isDataValid(const uint8_t *data, size_t dataLen, bool hasDirectionHeader)
BluetoothHciDirection
Definition: BluetoothHciLayer.h:51
@ HostToController
Sent from the host to the controller.
@ ControllerToHost
Received by the host from the controller.
@ Unknown
The capture has no direction pseudo-header (LINKTYPE_BLUETOOTH_HCI_H4)
static BluetoothHciLayer * parseLayer(uint8_t *data, size_t dataLen, Packet *packet, bool hasDirectionHeader)
OsiModelLayer getOsiModelLayer() const override
Definition: BluetoothHciLayer.h:102
static constexpr size_t directionHeaderSize
Definition: BluetoothHciLayer.h:118
BluetoothHciDirection getDirection() const
void computeCalculateFields() override
Does nothing for this layer.
Definition: BluetoothHciLayer.h:99
BluetoothHciPacketType getPacketType() const
Definition: BluetoothHciLayer.h:86
bool hasDirectionHeader() const
Definition: BluetoothHciLayer.h:74
size_t getDirectionHeaderLen() const
Definition: BluetoothHciLayer.h:137
BluetoothHciPacketType
Definition: BluetoothHciLayer.h:32
@ Unknown
Not a recognized HCI packet type. 0x00 is not a valid H4 packet indicator value.
@ Event
An event sent from the controller to the host.
@ Command
A command sent from the host to the controller.
void parseNextLayer() override
Definition: BluetoothHciLayer.h:95
BluetoothHciLayer(uint8_t *data, size_t dataLen, Packet *packet, bool hasDirectionHeader)
Definition: BluetoothHciLayer.h:125
BluetoothHciLayer(bool hasDirectionHeader)
Definition: BluetoothHciLayer.h:131
Definition: Layer.h:117
Definition: Serializers.h:230
Definition: Packet.h:49
const ProtocolType BluetoothHCI
Bluetooth HCI Protocol.
Definition: ProtocolType.h:276
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:430
@ OsiModelDataLinkLayer
Data link layer (layer 2)
Definition: ProtocolType.h:434
Definition: BluetoothHciLayer.h:237
static const FieldDescriptor EventCode
HCI event code.
Definition: BluetoothHciLayer.h:251
static std::vector< FieldDescriptor > all()
Definition: BluetoothHciLayer.h:239
static const FieldDescriptor Direction
Packet direction, as "HostToController", "ControllerToHost", or "Unknown".
Definition: BluetoothHciLayer.h:248
static const FieldDescriptor ParameterTotalLength
Length of the parameter bytes.
Definition: BluetoothHciLayer.h:254
Definition: Serializers.h:18
Definition: Layer.h:212
static std::vector< FieldDescriptor > all()
Definition: Layer.h:214