/src/PcapPlusPlus/Packet++/src/BluetoothHciLayer.cpp
Line | Count | Source |
1 | | #include "BluetoothHciLayer.h" |
2 | | #include "Serializers.h" |
3 | | #include "SystemUtils.h" |
4 | | |
5 | | #include <algorithm> |
6 | | #include <cstring> |
7 | | #include <iomanip> |
8 | | #include <limits> |
9 | | #include <sstream> |
10 | | #include <stdexcept> |
11 | | |
12 | | namespace pcpp |
13 | | { |
14 | | // ~~~~~~~~~~~~~~~~~ |
15 | | // BluetoothHciLayer |
16 | | // ~~~~~~~~~~~~~~~~~ |
17 | | |
18 | | BluetoothHciLayer::BluetoothHciPacketType BluetoothHciLayer::packetTypeFromIndicator(uint8_t packetIndicator) |
19 | 0 | { |
20 | 0 | switch (packetIndicator) |
21 | 0 | { |
22 | 0 | case static_cast<uint8_t>(BluetoothHciPacketType::Command): |
23 | 0 | return BluetoothHciPacketType::Command; |
24 | 0 | case static_cast<uint8_t>(BluetoothHciPacketType::AclData): |
25 | 0 | return BluetoothHciPacketType::AclData; |
26 | 0 | case static_cast<uint8_t>(BluetoothHciPacketType::ScoData): |
27 | 0 | return BluetoothHciPacketType::ScoData; |
28 | 0 | case static_cast<uint8_t>(BluetoothHciPacketType::Event): |
29 | 0 | return BluetoothHciPacketType::Event; |
30 | 0 | case static_cast<uint8_t>(BluetoothHciPacketType::IsoData): |
31 | 0 | return BluetoothHciPacketType::IsoData; |
32 | 0 | default: |
33 | 0 | return BluetoothHciPacketType::Unknown; |
34 | 0 | } |
35 | 0 | } |
36 | | |
37 | | BluetoothHciLayer* BluetoothHciLayer::parseLayer(uint8_t* data, size_t dataLen, Packet* packet, |
38 | | bool hasDirectionHeader) |
39 | 0 | { |
40 | 0 | if (!isDataValid(data, dataLen, hasDirectionHeader)) |
41 | 0 | { |
42 | 0 | return nullptr; |
43 | 0 | } |
44 | | |
45 | 0 | size_t directionHeaderLen = hasDirectionHeader ? directionHeaderSize : 0; |
46 | 0 | switch (packetTypeFromIndicator(data[directionHeaderLen])) |
47 | 0 | { |
48 | 0 | case BluetoothHciPacketType::Event: |
49 | 0 | { |
50 | 0 | if (!BluetoothHciEventLayer::isDataValid(data, dataLen, hasDirectionHeader)) |
51 | 0 | { |
52 | 0 | return nullptr; |
53 | 0 | } |
54 | 0 | return new BluetoothHciEventLayer(data, dataLen, packet, hasDirectionHeader); |
55 | 0 | } |
56 | 0 | default: |
57 | 0 | { |
58 | | // Command, ACL data, SCO data and ISO data packets are not supported yet |
59 | 0 | return nullptr; |
60 | 0 | } |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | BluetoothHciLayer::BluetoothHciDirection BluetoothHciLayer::getDirection() const |
65 | 0 | { |
66 | 0 | if (!hasDirectionHeader()) |
67 | 0 | { |
68 | 0 | return BluetoothHciDirection::Unknown; |
69 | 0 | } |
70 | | |
71 | 0 | const auto* directionHeader = reinterpret_cast<const bluetooth_hci_direction_header*>(m_Data); |
72 | 0 | switch (netToHost32(directionHeader->direction)) |
73 | 0 | { |
74 | 0 | case static_cast<uint32_t>(BluetoothHciDirection::HostToController): |
75 | 0 | return BluetoothHciDirection::HostToController; |
76 | 0 | case static_cast<uint32_t>(BluetoothHciDirection::ControllerToHost): |
77 | 0 | return BluetoothHciDirection::ControllerToHost; |
78 | 0 | default: |
79 | 0 | return BluetoothHciDirection::Unknown; |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | | bool BluetoothHciLayer::isDataValid(const uint8_t* data, size_t dataLen, bool hasDirectionHeader) |
84 | 0 | { |
85 | 0 | if (data == nullptr) |
86 | 0 | { |
87 | 0 | return false; |
88 | 0 | } |
89 | | |
90 | 0 | size_t directionHeaderLen = hasDirectionHeader ? directionHeaderSize : 0; |
91 | 0 | return dataLen >= directionHeaderLen + sizeof(uint8_t); |
92 | 0 | } |
93 | | |
94 | | // ~~~~~~~~~~~~~~~~~~~~~~ |
95 | | // BluetoothHciEventLayer |
96 | | // ~~~~~~~~~~~~~~~~~~~~~~ |
97 | | |
98 | | BluetoothHciEventLayer::BluetoothHciEventLayer(uint8_t eventCode, const std::vector<uint8_t>& parameters) |
99 | 0 | : BluetoothHciLayer(false) |
100 | 0 | { |
101 | 0 | initLayer(eventCode, parameters); |
102 | 0 | } |
103 | | |
104 | | BluetoothHciEventLayer::BluetoothHciEventLayer(BluetoothHciDirection direction, uint8_t eventCode, |
105 | | const std::vector<uint8_t>& parameters) |
106 | 0 | : BluetoothHciLayer(true) |
107 | 0 | { |
108 | 0 | if (direction != BluetoothHciDirection::HostToController && |
109 | 0 | direction != BluetoothHciDirection::ControllerToHost) |
110 | 0 | { |
111 | 0 | throw std::invalid_argument("Direction must be HostToController or ControllerToHost"); |
112 | 0 | } |
113 | | |
114 | 0 | initLayer(eventCode, parameters); |
115 | 0 | uint32_t netDirection = hostToNet32(static_cast<uint32_t>(direction)); |
116 | 0 | std::memcpy(m_Data, &netDirection, directionHeaderSize); |
117 | 0 | } |
118 | | |
119 | | void BluetoothHciEventLayer::initLayer(uint8_t eventCode, const std::vector<uint8_t>& parameters) |
120 | 0 | { |
121 | 0 | if (parameters.size() > std::numeric_limits<uint8_t>::max()) |
122 | 0 | { |
123 | 0 | throw std::invalid_argument("Parameters length is greater than 255 bytes"); |
124 | 0 | } |
125 | | |
126 | 0 | size_t paramOffset = getDirectionHeaderLen() + sizeof(uint8_t) + sizeof(bluetooth_hci_event_header); |
127 | 0 | allocData(paramOffset + parameters.size()); |
128 | 0 | m_Data[getDirectionHeaderLen()] = static_cast<uint8_t>(BluetoothHciPacketType::Event); |
129 | 0 | auto* header = getEventHeader(); |
130 | 0 | header->eventCode = eventCode; |
131 | 0 | header->parameterTotalLength = static_cast<uint8_t>(parameters.size()); |
132 | 0 | if (!parameters.empty()) |
133 | 0 | { |
134 | 0 | std::memcpy(m_Data + paramOffset, parameters.data(), parameters.size()); |
135 | 0 | } |
136 | 0 | } |
137 | | |
138 | | uint8_t BluetoothHciEventLayer::getParameterTotalLength() const |
139 | 0 | { |
140 | | // Compute the available length as size_t so that it isn't truncated to 8 bits before the comparison |
141 | 0 | size_t availableLen = |
142 | 0 | m_DataLen - getDirectionHeaderLen() - sizeof(uint8_t) - sizeof(bluetooth_hci_event_header); |
143 | 0 | size_t declaredLen = getEventHeader()->parameterTotalLength; |
144 | 0 | return static_cast<uint8_t>(std::min(declaredLen, availableLen)); |
145 | 0 | } |
146 | | |
147 | | void BluetoothHciEventLayer::setParameters(const std::vector<uint8_t>& parameters) |
148 | 0 | { |
149 | 0 | if (parameters.size() > std::numeric_limits<uint8_t>::max()) |
150 | 0 | { |
151 | 0 | throw std::invalid_argument("Parameters length is greater than 255 bytes"); |
152 | 0 | } |
153 | | |
154 | 0 | size_t paramOffset = getDirectionHeaderLen() + sizeof(uint8_t) + sizeof(bluetooth_hci_event_header); |
155 | 0 | size_t currentParamLen = getParameterTotalLength(); |
156 | 0 | size_t newParamLen = parameters.size(); |
157 | |
|
158 | 0 | if (newParamLen > currentParamLen) |
159 | 0 | { |
160 | 0 | if (!extendLayer(static_cast<int>(paramOffset + currentParamLen), newParamLen - currentParamLen)) |
161 | 0 | { |
162 | 0 | throw std::runtime_error("Could not extend the Bluetooth HCI Event layer to fit the new parameters"); |
163 | 0 | } |
164 | 0 | } |
165 | 0 | else if (newParamLen < currentParamLen) |
166 | 0 | { |
167 | 0 | if (!shortenLayer(static_cast<int>(paramOffset + newParamLen), currentParamLen - newParamLen)) |
168 | 0 | { |
169 | 0 | throw std::runtime_error("Could not shorten the Bluetooth HCI Event layer to fit the new parameters"); |
170 | 0 | } |
171 | 0 | } |
172 | | |
173 | 0 | getEventHeader()->parameterTotalLength = static_cast<uint8_t>(newParamLen); |
174 | 0 | if (newParamLen > 0) |
175 | 0 | { |
176 | 0 | std::memcpy(m_Data + paramOffset, parameters.data(), newParamLen); |
177 | 0 | } |
178 | 0 | } |
179 | | |
180 | | std::string BluetoothHciEventLayer::toString() const |
181 | 0 | { |
182 | 0 | std::ostringstream stream; |
183 | 0 | stream << "Bluetooth HCI Event, Event Code: 0x" << std::hex << std::setw(2) << std::setfill('0') |
184 | 0 | << static_cast<int>(getEventCode()); |
185 | 0 | return stream.str(); |
186 | 0 | } |
187 | | |
188 | | bool BluetoothHciEventLayer::isDataValid(const uint8_t* data, size_t dataLen, bool hasDirectionHeader) |
189 | 0 | { |
190 | 0 | if (data == nullptr) |
191 | 0 | { |
192 | 0 | return false; |
193 | 0 | } |
194 | | |
195 | 0 | size_t directionHeaderLen = hasDirectionHeader ? directionHeaderSize : 0; |
196 | 0 | if (dataLen < directionHeaderLen + sizeof(uint8_t) + sizeof(bluetooth_hci_event_header)) |
197 | 0 | { |
198 | 0 | return false; |
199 | 0 | } |
200 | | |
201 | 0 | return data[directionHeaderLen] == static_cast<uint8_t>(BluetoothHciPacketType::Event); |
202 | 0 | } |
203 | | |
204 | | const FieldDescriptor BluetoothHciEventLayer::SerializedFields::Direction{ Layer::SerializedFields::MaxID + 1, |
205 | | "direction" }; |
206 | | const FieldDescriptor BluetoothHciEventLayer::SerializedFields::EventCode{ Layer::SerializedFields::MaxID + 2, |
207 | | "eventCode" }; |
208 | | const FieldDescriptor BluetoothHciEventLayer::SerializedFields::ParameterTotalLength{ |
209 | | Layer::SerializedFields::MaxID + 3, "parameterTotalLength" |
210 | | }; |
211 | | |
212 | | void BluetoothHciEventLayer::serializeLayer(ObjectScope& serializer) const |
213 | 0 | { |
214 | 0 | const char* directionStr; |
215 | 0 | switch (getDirection()) |
216 | 0 | { |
217 | 0 | case BluetoothHciDirection::HostToController: |
218 | 0 | directionStr = "HostToController"; |
219 | 0 | break; |
220 | 0 | case BluetoothHciDirection::ControllerToHost: |
221 | 0 | directionStr = "ControllerToHost"; |
222 | 0 | break; |
223 | 0 | default: |
224 | 0 | directionStr = "Unknown"; |
225 | 0 | break; |
226 | 0 | } |
227 | 0 | serializer.writeField(SerializedFields::Direction, directionStr); |
228 | 0 | serializer.writeField(SerializedFields::EventCode, getEventCode()); |
229 | 0 | serializer.writeField(SerializedFields::ParameterTotalLength, getParameterTotalLength()); |
230 | 0 | } |
231 | | |
232 | | } // namespace pcpp |