1 /*
2 * Copyright 2018 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 #include "packets/hci/event_packet_builder.h"
18
19 #include <base/logging.h>
20 #include "hci.h"
21 #include "packets/hci/le_meta_event_builder.h"
22
23 using std::vector;
24 using test_vendor_lib::hci::EventCode;
25 using test_vendor_lib::hci::OpCode;
26 using test_vendor_lib::hci::Status;
27
28 namespace test_vendor_lib {
29 namespace packets {
30
EventPacketBuilder(EventCode event_code)31 EventPacketBuilder::EventPacketBuilder(EventCode event_code)
32 : event_code_(event_code), payload_(std::make_unique<RawBuilder>()) {}
33
EventPacketBuilder(EventCode event_code,std::unique_ptr<RawBuilder> payload)34 EventPacketBuilder::EventPacketBuilder(EventCode event_code, std::unique_ptr<RawBuilder> payload)
35 : event_code_(event_code), payload_(std::move(payload)) {}
36
37 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.1
CreateInquiryCompleteEvent(hci::Status status)38 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateInquiryCompleteEvent(hci::Status status) {
39 std::unique_ptr<EventPacketBuilder> evt_ptr =
40 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::INQUIRY_COMPLETE));
41 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
42
43 return evt_ptr;
44 }
45
46 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.14
CreateCommandCompleteEvent(hci::OpCode command_opcode,const vector<uint8_t> & event_return_parameters)47 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteEvent(
48 hci::OpCode command_opcode, const vector<uint8_t>& event_return_parameters) {
49 std::unique_ptr<EventPacketBuilder> evt_ptr =
50 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::COMMAND_COMPLETE));
51
52 CHECK(evt_ptr->AddPayloadOctets1(1)); // num_hci_command_packets
53 CHECK(evt_ptr->AddPayloadOctets2(static_cast<uint16_t>(command_opcode)));
54 CHECK(evt_ptr->AddPayloadOctets(event_return_parameters));
55
56 return evt_ptr;
57 }
58
CreateCommandCompleteOnlyStatusEvent(hci::OpCode command_opcode,hci::Status status)59 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(hci::OpCode command_opcode,
60 hci::Status status) {
61 std::unique_ptr<EventPacketBuilder> evt_ptr =
62 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::COMMAND_COMPLETE));
63
64 CHECK(evt_ptr->AddPayloadOctets1(1)); // num_hci_command_packets
65 CHECK(evt_ptr->AddPayloadOctets2(static_cast<uint16_t>(command_opcode)));
66 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
67
68 return evt_ptr;
69 }
70
CreateCommandCompleteStatusAndAddressEvent(hci::OpCode command_opcode,hci::Status status,const Address & address)71 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteStatusAndAddressEvent(
72 hci::OpCode command_opcode, hci::Status status, const Address& address) {
73 std::unique_ptr<EventPacketBuilder> evt_ptr =
74 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::COMMAND_COMPLETE));
75
76 CHECK(evt_ptr->AddPayloadOctets1(1)); // num_hci_command_packets
77 CHECK(evt_ptr->AddPayloadOctets2(static_cast<uint16_t>(command_opcode)));
78 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
79 CHECK(evt_ptr->AddPayloadAddress(address));
80
81 return evt_ptr;
82 }
83
CreateCommandCompleteUnknownOpCodeEvent(uint16_t command_opcode)84 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteUnknownOpCodeEvent(
85 uint16_t command_opcode) {
86 std::unique_ptr<EventPacketBuilder> evt_ptr =
87 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::COMMAND_COMPLETE));
88
89 CHECK(evt_ptr->AddPayloadOctets1(1)); // num_hci_command_packets
90 CHECK(evt_ptr->AddPayloadOctets2(static_cast<uint16_t>(command_opcode)));
91 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(Status::UNKNOWN_COMMAND)));
92
93 return evt_ptr;
94 }
95
96 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.15
CreateCommandStatusEvent(hci::Status status,hci::OpCode command_opcode)97 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandStatusEvent(hci::Status status,
98 hci::OpCode command_opcode) {
99 std::unique_ptr<EventPacketBuilder> evt_ptr =
100 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::COMMAND_STATUS));
101
102 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
103 CHECK(evt_ptr->AddPayloadOctets1(1)); // num_hci_command_packets
104 CHECK(evt_ptr->AddPayloadOctets2(static_cast<uint16_t>(command_opcode)));
105
106 return evt_ptr;
107 }
108
109 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.19
CreateNumberOfCompletedPacketsEvent(uint16_t handle,uint16_t num_completed_packets)110 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateNumberOfCompletedPacketsEvent(
111 uint16_t handle, uint16_t num_completed_packets) {
112 std::unique_ptr<RawBuilder> payload = std::make_unique<CountedBuilder>();
113 std::unique_ptr<EventPacketBuilder> evt_ptr = std::unique_ptr<EventPacketBuilder>(
114 new EventPacketBuilder(EventCode::NUMBER_OF_COMPLETED_PACKETS, std::move(payload)));
115
116 evt_ptr->AddCompletedPackets(handle, num_completed_packets);
117
118 return evt_ptr;
119 }
120
AddCompletedPackets(uint16_t handle,uint16_t num_completed_packets)121 void EventPacketBuilder::AddCompletedPackets(uint16_t handle, uint16_t num_completed_packets) {
122 CHECK(event_code_ == EventCode::NUMBER_OF_COMPLETED_PACKETS);
123
124 std::unique_ptr<RawBuilder> handle_pair = std::make_unique<RawBuilder>();
125 CHECK(handle_pair->AddOctets2(handle));
126 CHECK(handle_pair->AddOctets2(num_completed_packets));
127 AddBuilder(std::move(handle_pair));
128 }
129
130 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.3.10
CreateCommandCompleteDeleteStoredLinkKey(hci::Status status,uint16_t num_keys_deleted)131 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteDeleteStoredLinkKey(
132 hci::Status status, uint16_t num_keys_deleted) {
133 std::unique_ptr<EventPacketBuilder> evt_ptr =
134 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::DELETE_STORED_LINK_KEY, status);
135
136 CHECK(evt_ptr->AddPayloadOctets2(num_keys_deleted));
137
138 return evt_ptr;
139 }
140
141 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.3.12
CreateCommandCompleteReadLocalName(hci::Status status,const std::vector<uint8_t> & local_name)142 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteReadLocalName(
143 hci::Status status, const std::vector<uint8_t>& local_name) {
144 std::unique_ptr<EventPacketBuilder> evt_ptr =
145 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::READ_LOCAL_NAME, status);
146
147 size_t len = local_name.size();
148 if (len > 247) {
149 len = 247;
150 }
151 CHECK(evt_ptr->AddPayloadOctets(len, local_name));
152 CHECK(evt_ptr->AddPayloadOctets1(0)); // Null terminated
153 for (size_t i = 0; i < 248 - len - 1; i++) CHECK(evt_ptr->AddPayloadOctets1(0xFF));
154
155 return evt_ptr;
156 }
157
158 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.3.23
CreateCommandCompleteReadAuthenticationEnable(hci::Status status,uint8_t enable)159 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteReadAuthenticationEnable(
160 hci::Status status, uint8_t enable) {
161 std::unique_ptr<EventPacketBuilder> evt_ptr =
162 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::READ_LOCAL_NAME, status);
163 CHECK(evt_ptr->AddPayloadOctets1(enable));
164 return evt_ptr;
165 }
166
167 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.1
CreateCommandCompleteReadLocalVersionInformation(hci::Status status,uint8_t hci_version,uint16_t hci_revision,uint8_t lmp_pal_version,uint16_t manufacturer_name,uint16_t lmp_pal_subversion)168 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteReadLocalVersionInformation(
169 hci::Status status, uint8_t hci_version, uint16_t hci_revision, uint8_t lmp_pal_version, uint16_t manufacturer_name,
170 uint16_t lmp_pal_subversion) {
171 std::unique_ptr<EventPacketBuilder> evt_ptr =
172 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::READ_LOCAL_VERSION_INFORMATION, status);
173
174 CHECK(evt_ptr->AddPayloadOctets1(hci_version));
175 CHECK(evt_ptr->AddPayloadOctets2(hci_revision));
176 CHECK(evt_ptr->AddPayloadOctets1(lmp_pal_version));
177 CHECK(evt_ptr->AddPayloadOctets2(manufacturer_name));
178 CHECK(evt_ptr->AddPayloadOctets2(lmp_pal_subversion));
179
180 return evt_ptr;
181 }
182
CreateReadRemoteVersionInformationEvent(hci::Status status,uint16_t connection_handle,uint8_t lmp_pal_version,uint16_t manufacturer_name,uint16_t lmp_pal_subversion)183 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateReadRemoteVersionInformationEvent(
184 hci::Status status, uint16_t connection_handle, uint8_t lmp_pal_version, uint16_t manufacturer_name,
185 uint16_t lmp_pal_subversion) {
186 std::unique_ptr<EventPacketBuilder> evt_ptr =
187 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::READ_REMOTE_VERSION_INFORMATION_COMPLETE));
188
189 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
190 CHECK(evt_ptr->AddPayloadOctets2(connection_handle));
191 CHECK(evt_ptr->AddPayloadOctets1(lmp_pal_version));
192 CHECK(evt_ptr->AddPayloadOctets2(manufacturer_name));
193 CHECK(evt_ptr->AddPayloadOctets2(lmp_pal_subversion));
194
195 return evt_ptr;
196 }
197 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.2
CreateCommandCompleteReadLocalSupportedCommands(hci::Status status,const vector<uint8_t> & supported_commands)198 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteReadLocalSupportedCommands(
199 hci::Status status, const vector<uint8_t>& supported_commands) {
200 std::unique_ptr<EventPacketBuilder> evt_ptr =
201 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::READ_LOCAL_SUPPORTED_COMMANDS, status);
202
203 CHECK(evt_ptr->AddPayloadOctets(64, supported_commands));
204
205 return evt_ptr;
206 }
207
208 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.4
CreateCommandCompleteReadLocalExtendedFeatures(hci::Status status,uint8_t page_number,uint8_t maximum_page_number,uint64_t extended_lmp_features)209 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteReadLocalExtendedFeatures(
210 hci::Status status, uint8_t page_number, uint8_t maximum_page_number, uint64_t extended_lmp_features) {
211 std::unique_ptr<EventPacketBuilder> evt_ptr =
212 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::READ_LOCAL_EXTENDED_FEATURES, status);
213
214 CHECK(evt_ptr->AddPayloadOctets1(page_number));
215 CHECK(evt_ptr->AddPayloadOctets1(maximum_page_number));
216 CHECK(evt_ptr->AddPayloadOctets8(extended_lmp_features));
217
218 return evt_ptr;
219 }
220
CreateReadRemoteExtendedFeaturesEvent(hci::Status status,uint16_t handle,uint8_t page_number,uint8_t maximum_page_number,uint64_t extended_lmp_features)221 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateReadRemoteExtendedFeaturesEvent(
222 hci::Status status, uint16_t handle, uint8_t page_number, uint8_t maximum_page_number,
223 uint64_t extended_lmp_features) {
224 std::unique_ptr<EventPacketBuilder> evt_ptr =
225 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::READ_REMOTE_EXTENDED_FEATURES_COMPLETE));
226
227 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
228 CHECK(evt_ptr->AddPayloadOctets2(handle));
229 CHECK(evt_ptr->AddPayloadOctets1(page_number));
230 CHECK(evt_ptr->AddPayloadOctets1(maximum_page_number));
231 CHECK(evt_ptr->AddPayloadOctets8(extended_lmp_features));
232
233 return evt_ptr;
234 }
235
236 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.5
CreateCommandCompleteReadBufferSize(hci::Status status,uint16_t hc_acl_data_packet_length,uint8_t hc_synchronous_data_packet_length,uint16_t hc_total_num_acl_data_packets,uint16_t hc_total_synchronous_data_packets)237 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteReadBufferSize(
238 hci::Status status, uint16_t hc_acl_data_packet_length, uint8_t hc_synchronous_data_packet_length,
239 uint16_t hc_total_num_acl_data_packets, uint16_t hc_total_synchronous_data_packets) {
240 std::unique_ptr<EventPacketBuilder> evt_ptr =
241 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::READ_BUFFER_SIZE, status);
242
243 CHECK(evt_ptr->AddPayloadOctets2(hc_acl_data_packet_length));
244 CHECK(evt_ptr->AddPayloadOctets1(hc_synchronous_data_packet_length));
245 CHECK(evt_ptr->AddPayloadOctets2(hc_total_num_acl_data_packets));
246 CHECK(evt_ptr->AddPayloadOctets2(hc_total_synchronous_data_packets));
247
248 return evt_ptr;
249 }
250
251 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.6
CreateCommandCompleteReadBdAddr(hci::Status status,const Address & address)252 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteReadBdAddr(hci::Status status,
253 const Address& address) {
254 std::unique_ptr<EventPacketBuilder> evt_ptr =
255 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::READ_BD_ADDR, status);
256
257 CHECK(evt_ptr->AddPayloadAddress(address));
258
259 return evt_ptr;
260 }
261
262 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.4.8
CreateCommandCompleteReadLocalSupportedCodecs(hci::Status status,const vector<uint8_t> & supported_codecs,const vector<uint32_t> & vendor_specific_codecs)263 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteReadLocalSupportedCodecs(
264 hci::Status status, const vector<uint8_t>& supported_codecs, const vector<uint32_t>& vendor_specific_codecs) {
265 std::unique_ptr<EventPacketBuilder> evt_ptr =
266 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::READ_LOCAL_SUPPORTED_CODECS, status);
267
268 CHECK(evt_ptr->AddPayloadOctets1(supported_codecs.size()));
269 CHECK(evt_ptr->AddPayloadOctets(supported_codecs));
270 CHECK(evt_ptr->AddPayloadOctets1(vendor_specific_codecs.size()));
271 for (size_t i = 0; i < vendor_specific_codecs.size(); i++)
272 CHECK(evt_ptr->AddPayloadOctets4(vendor_specific_codecs[i]));
273
274 return evt_ptr;
275 }
276
277 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.6.1
CreateCommandCompleteReadLoopbackMode(hci::Status status,hci::LoopbackMode mode)278 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteReadLoopbackMode(hci::Status status,
279 hci::LoopbackMode mode) {
280 std::unique_ptr<EventPacketBuilder> evt_ptr =
281 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::READ_LOOPBACK_MODE, status);
282 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(mode)));
283
284 return evt_ptr;
285 }
286
287 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.2
CreateInquiryResultEvent()288 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateInquiryResultEvent() {
289 std::unique_ptr<RawBuilder> payload = std::unique_ptr<RawBuilder>(new CountedBuilder());
290 std::unique_ptr<EventPacketBuilder> evt_ptr(new EventPacketBuilder(EventCode::INQUIRY_RESULT, std::move(payload)));
291
292 return evt_ptr;
293 }
294
AddInquiryResult(const Address & address,uint8_t page_scan_repetition_mode,ClassOfDevice class_of_device,uint16_t clock_offset)295 bool EventPacketBuilder::AddInquiryResult(const Address& address, uint8_t page_scan_repetition_mode,
296 ClassOfDevice class_of_device, uint16_t clock_offset) {
297 CHECK(event_code_ == EventCode::INQUIRY_RESULT);
298
299 if (!CanAddPayloadOctets(14)) return false;
300
301 std::unique_ptr<RawBuilder> result = std::make_unique<RawBuilder>();
302
303 CHECK(result->AddAddress(address));
304 CHECK(result->AddOctets1(page_scan_repetition_mode));
305 CHECK(result->AddOctets2(0)); // Reserved
306 CHECK(result->AddOctets1(class_of_device.cod[0]));
307 CHECK(result->AddOctets1(class_of_device.cod[1]));
308 CHECK(result->AddOctets1(class_of_device.cod[2]));
309 CHECK(!(clock_offset & 0x8000));
310 CHECK(result->AddOctets2(clock_offset));
311 AddBuilder(std::move(result));
312 return true;
313 }
314
315 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.3
CreateConnectionCompleteEvent(hci::Status status,uint16_t handle,const Address & address,hci::LinkType link_type,bool encryption_enabled)316 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateConnectionCompleteEvent(
317 hci::Status status, uint16_t handle, const Address& address, hci::LinkType link_type, bool encryption_enabled) {
318 std::unique_ptr<EventPacketBuilder> evt_ptr =
319 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::CONNECTION_COMPLETE));
320
321 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
322 CHECK((handle & 0xf000) == 0); // Handles are 12-bit values.
323 CHECK(evt_ptr->AddPayloadOctets2(handle));
324 CHECK(evt_ptr->AddPayloadAddress(address));
325 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(link_type)));
326 CHECK(evt_ptr->AddPayloadOctets1(encryption_enabled ? 1 : 0));
327
328 return evt_ptr;
329 }
330
331 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.4
CreateConnectionRequestEvent(const Address & address,ClassOfDevice class_of_device,hci::LinkType link_type)332 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateConnectionRequestEvent(const Address& address,
333 ClassOfDevice class_of_device,
334 hci::LinkType link_type) {
335 std::unique_ptr<EventPacketBuilder> evt_ptr =
336 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::CONNECTION_REQUEST));
337
338 CHECK(evt_ptr->AddPayloadAddress(address));
339 CHECK(evt_ptr->AddPayloadOctets1(class_of_device.cod[0]));
340 CHECK(evt_ptr->AddPayloadOctets1(class_of_device.cod[1]));
341 CHECK(evt_ptr->AddPayloadOctets1(class_of_device.cod[2]));
342 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(link_type)));
343
344 return evt_ptr;
345 }
346
347 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.5
CreateDisconnectionCompleteEvent(hci::Status status,uint16_t handle,uint8_t reason)348 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateDisconnectionCompleteEvent(hci::Status status,
349 uint16_t handle,
350 uint8_t reason) {
351 std::unique_ptr<EventPacketBuilder> evt_ptr =
352 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::DISCONNECTION_COMPLETE));
353
354 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
355 CHECK((handle & 0xf000) == 0); // Handles are 12-bit values.
356 CHECK(evt_ptr->AddPayloadOctets2(handle));
357 CHECK(evt_ptr->AddPayloadOctets1(reason));
358
359 return evt_ptr;
360 }
361
362 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.6
CreateAuthenticationCompleteEvent(hci::Status status,uint16_t handle)363 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateAuthenticationCompleteEvent(hci::Status status,
364 uint16_t handle) {
365 std::unique_ptr<EventPacketBuilder> evt_ptr =
366 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::AUTHENTICATION_COMPLETE));
367 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
368 CHECK((handle & 0xf000) == 0); // Handles are 12-bit values.
369 CHECK(evt_ptr->AddPayloadOctets2(handle));
370 return evt_ptr;
371 }
372
373 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.7
CreateRemoteNameRequestCompleteEvent(hci::Status status,const Address & address,const std::string & remote_name)374 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateRemoteNameRequestCompleteEvent(
375 hci::Status status, const Address& address, const std::string& remote_name) {
376 std::unique_ptr<EventPacketBuilder> evt_ptr =
377 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::REMOTE_NAME_REQUEST_COMPLETE));
378
379 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
380 CHECK(evt_ptr->AddPayloadAddress(address));
381 for (size_t i = 0; i < remote_name.length(); i++) CHECK(evt_ptr->AddPayloadOctets1(remote_name[i]));
382 CHECK(evt_ptr->AddPayloadOctets1(0)); // Null terminated
383 for (size_t i = 0; i < 248 - remote_name.length() - 1; i++) CHECK(evt_ptr->AddPayloadOctets1(0xFF));
384
385 return evt_ptr;
386 }
387
388 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.23
CreateLinkKeyRequestEvent(const Address & remote)389 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateLinkKeyRequestEvent(const Address& remote) {
390 std::unique_ptr<EventPacketBuilder> evt_ptr =
391 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::LINK_KEY_REQUEST));
392 CHECK(evt_ptr->AddPayloadAddress(remote));
393 return evt_ptr;
394 }
395
396 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.24
CreateLinkKeyNotificationEvent(const Address & remote,const std::vector<uint8_t> & key,uint8_t key_type)397 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateLinkKeyNotificationEvent(const Address& remote,
398 const std::vector<uint8_t>& key,
399 uint8_t key_type) {
400 std::unique_ptr<EventPacketBuilder> evt_ptr =
401 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::LINK_KEY_NOTIFICATION));
402 CHECK(evt_ptr->AddPayloadAddress(remote));
403 CHECK(key.size() == 16);
404 CHECK(evt_ptr->AddPayloadOctets(key));
405 CHECK(evt_ptr->AddPayloadOctets1(key_type));
406 return evt_ptr;
407 }
408
409 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.25
CreateLoopbackCommandEvent(hci::OpCode opcode,PacketView<true> payload)410 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateLoopbackCommandEvent(hci::OpCode opcode,
411 PacketView<true> payload) {
412 std::unique_ptr<EventPacketBuilder> evt_ptr =
413 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::LOOPBACK_COMMAND));
414 CHECK(evt_ptr->AddPayloadOctets2(static_cast<uint16_t>(opcode)));
415 for (const auto& payload_byte : payload) // Fill the packet.
416 evt_ptr->AddPayloadOctets1(payload_byte);
417 return evt_ptr;
418 }
419
420 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.28
CreateReadClockOffsetEvent(hci::Status status,uint16_t handle,uint16_t offset)421 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateReadClockOffsetEvent(hci::Status status, uint16_t handle,
422 uint16_t offset) {
423 std::unique_ptr<EventPacketBuilder> evt_ptr =
424 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::READ_CLOCK_OFFSET_COMPLETE));
425 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
426 CHECK(evt_ptr->AddPayloadOctets2(handle));
427 CHECK(evt_ptr->AddPayloadOctets2(offset));
428 return evt_ptr;
429 }
430
431 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.29
CreateConnectionPacketTypeChangedEvent(hci::Status status,uint16_t handle,uint16_t packet_type)432 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateConnectionPacketTypeChangedEvent(hci::Status status,
433 uint16_t handle,
434 uint16_t packet_type) {
435 std::unique_ptr<EventPacketBuilder> evt_ptr =
436 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::CONNECTION_PACKET_TYPE_CHANGE));
437 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
438 CHECK(evt_ptr->AddPayloadOctets2(handle));
439 CHECK(evt_ptr->AddPayloadOctets2(packet_type));
440 return evt_ptr;
441 }
442
443 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.37
CreateSniffSubratingEvent(const hci::Status status,uint16_t handle)444 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateSniffSubratingEvent(const hci::Status status,
445 uint16_t handle) {
446 std::unique_ptr<EventPacketBuilder> evt_ptr =
447 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::SNIFF_SUBRATING, status);
448
449 CHECK(evt_ptr->AddPayloadOctets2(handle));
450 return evt_ptr;
451 }
452
453 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.38
CreateExtendedInquiryResultEvent(const Address & address,uint8_t page_scan_repetition_mode,ClassOfDevice class_of_device,uint16_t clock_offset,uint8_t rssi,const vector<uint8_t> & extended_inquiry_response)454 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateExtendedInquiryResultEvent(
455 const Address& address, uint8_t page_scan_repetition_mode, ClassOfDevice class_of_device, uint16_t clock_offset,
456 uint8_t rssi, const vector<uint8_t>& extended_inquiry_response) {
457 std::unique_ptr<EventPacketBuilder> evt_ptr =
458 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::EXTENDED_INQUIRY_RESULT));
459
460 CHECK(evt_ptr->AddPayloadOctets1(1)); // Always contains a single response
461
462 CHECK(evt_ptr->AddPayloadAddress(address));
463 CHECK(evt_ptr->AddPayloadOctets1(page_scan_repetition_mode));
464 CHECK(evt_ptr->AddPayloadOctets1(0)); // Reserved
465 CHECK(evt_ptr->AddPayloadOctets1(class_of_device.cod[0]));
466 CHECK(evt_ptr->AddPayloadOctets1(class_of_device.cod[1]));
467 CHECK(evt_ptr->AddPayloadOctets1(class_of_device.cod[2]));
468 CHECK(!(clock_offset & 0x8000));
469 CHECK(evt_ptr->AddPayloadOctets2(clock_offset));
470 CHECK(evt_ptr->AddPayloadOctets1(rssi));
471 CHECK(evt_ptr->AddPayloadOctets(extended_inquiry_response));
472 evt_ptr->AddPayloadOctets1(0x00); // End marker
473 while (evt_ptr->AddPayloadOctets1(0x00))
474 ; // Fill packet
475 return evt_ptr;
476 }
477
478 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.40
CreateIoCapabilityRequestEvent(const Address & peer)479 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateIoCapabilityRequestEvent(const Address& peer) {
480 std::unique_ptr<EventPacketBuilder> evt_ptr =
481 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::IO_CAPABILITY_REQUEST));
482
483 CHECK(evt_ptr->AddPayloadAddress(peer));
484 return evt_ptr;
485 } // namespace packets
486
487 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.41
CreateIoCapabilityResponseEvent(const Address & peer,uint8_t io_capability,bool oob_data_present,uint8_t authentication_requirements)488 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateIoCapabilityResponseEvent(
489 const Address& peer, uint8_t io_capability, bool oob_data_present, uint8_t authentication_requirements) {
490 std::unique_ptr<EventPacketBuilder> evt_ptr =
491 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::IO_CAPABILITY_RESPONSE));
492
493 CHECK(evt_ptr->AddPayloadAddress(peer));
494 CHECK(evt_ptr->AddPayloadOctets1(io_capability));
495 CHECK(evt_ptr->AddPayloadOctets1(oob_data_present));
496 CHECK(evt_ptr->AddPayloadOctets1(authentication_requirements));
497 return evt_ptr;
498 } // namespace test_vendor_lib
499
500 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.42
CreateUserConfirmationRequestEvent(const Address & peer,uint32_t numeric_value)501 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateUserConfirmationRequestEvent(const Address& peer,
502 uint32_t numeric_value) {
503 std::unique_ptr<EventPacketBuilder> evt_ptr =
504 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::USER_CONFIRMATION_REQUEST));
505
506 CHECK(evt_ptr->AddPayloadAddress(peer));
507 CHECK(evt_ptr->AddPayloadOctets4(numeric_value));
508 return evt_ptr;
509 }
510
511 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.43
CreateUserPasskeyRequestEvent(const Address & peer)512 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateUserPasskeyRequestEvent(const Address& peer) {
513 std::unique_ptr<EventPacketBuilder> evt_ptr =
514 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::USER_PASSKEY_REQUEST));
515
516 CHECK(evt_ptr->AddPayloadAddress(peer));
517 return evt_ptr;
518 }
519
520 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.44
CreateRemoteOobDataRequestEvent(const Address & peer)521 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateRemoteOobDataRequestEvent(const Address& peer) {
522 std::unique_ptr<EventPacketBuilder> evt_ptr =
523 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::REMOTE_OOB_DATA_REQUEST));
524
525 CHECK(evt_ptr->AddPayloadAddress(peer));
526 return evt_ptr;
527 }
528
529 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.45
CreateSimplePairingCompleteEvent(hci::Status status,const Address & peer)530 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateSimplePairingCompleteEvent(hci::Status status,
531 const Address& peer) {
532 std::unique_ptr<EventPacketBuilder> evt_ptr =
533 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::SIMPLE_PAIRING_COMPLETE));
534
535 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
536 CHECK(evt_ptr->AddPayloadAddress(peer));
537 return evt_ptr;
538 }
539
540 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.48
CreateUserPasskeyNotificationEvent(const Address & peer,uint32_t passkey)541 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateUserPasskeyNotificationEvent(const Address& peer,
542 uint32_t passkey) {
543 std::unique_ptr<EventPacketBuilder> evt_ptr =
544 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::USER_PASSKEY_NOTIFICATION));
545
546 CHECK(evt_ptr->AddPayloadAddress(peer));
547 CHECK(evt_ptr->AddPayloadOctets4(passkey));
548 return evt_ptr;
549 }
550
551 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.49
CreateKeypressNotificationEvent(const Address & peer,uint8_t notification_type)552 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateKeypressNotificationEvent(const Address& peer,
553 uint8_t notification_type) {
554 std::unique_ptr<EventPacketBuilder> evt_ptr =
555 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::KEYPRESS_NOTIFICATION));
556
557 CHECK(evt_ptr->AddPayloadAddress(peer));
558 CHECK(evt_ptr->AddPayloadOctets1(notification_type));
559 return evt_ptr;
560 }
561
562 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.65.1
CreateLeConnectionCompleteEvent(hci::Status status,uint16_t handle,uint8_t role,uint8_t peer_address_type,const Address & peer,uint16_t interval,uint16_t latency,uint16_t supervision_timeout)563 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateLeConnectionCompleteEvent(
564 hci::Status status, uint16_t handle, uint8_t role, uint8_t peer_address_type, const Address& peer,
565 uint16_t interval, uint16_t latency, uint16_t supervision_timeout) {
566 std::unique_ptr<RawBuilder> meta_evt = LeMetaEventBuilder::CreateLeConnectionCompleteEvent(
567 status, handle, role, peer_address_type, peer, interval, latency, supervision_timeout);
568
569 return std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::LE_META_EVENT, std::move(meta_evt)));
570 }
571
CreateLeEnhancedConnectionCompleteEvent(hci::Status status,uint16_t handle,uint8_t role,uint8_t peer_address_type,const Address & peer,const Address & local_private_address,const Address & peer_private_address,uint16_t interval,uint16_t latency,uint16_t supervision_timeout)572 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateLeEnhancedConnectionCompleteEvent(
573 hci::Status status, uint16_t handle, uint8_t role, uint8_t peer_address_type, const Address& peer,
574 const Address& local_private_address, const Address& peer_private_address, uint16_t interval, uint16_t latency,
575 uint16_t supervision_timeout) {
576 std::unique_ptr<RawBuilder> meta_evt = LeMetaEventBuilder::CreateLeEnhancedConnectionCompleteEvent(
577 status, handle, role, peer_address_type, peer, local_private_address, peer_private_address, interval, latency,
578 supervision_timeout);
579
580 return std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::LE_META_EVENT, std::move(meta_evt)));
581 }
582
CreateLeConnectionUpdateCompleteEvent(hci::Status status,uint16_t handle,uint16_t interval,uint16_t latency,uint16_t supervision_timeout)583 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateLeConnectionUpdateCompleteEvent(
584 hci::Status status, uint16_t handle, uint16_t interval, uint16_t latency, uint16_t supervision_timeout) {
585 std::unique_ptr<RawBuilder> meta_evt =
586 LeMetaEventBuilder::CreateLeConnectionUpdateCompleteEvent(status, handle, interval, latency, supervision_timeout);
587
588 return std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::LE_META_EVENT, std::move(meta_evt)));
589 }
590
591 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.65.2
CreateLeAdvertisingReportEvent()592 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateLeAdvertisingReportEvent() {
593 std::unique_ptr<RawBuilder> meta_evt = LeMetaEventBuilder::CreateLeAdvertisingReportEvent();
594
595 return std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::LE_META_EVENT, std::move(meta_evt)));
596 }
597
AddLeAdvertisingReport(LeAdvertisement::AdvertisementType event_type,LeAdvertisement::AddressType addr_type,const Address & addr,const vector<uint8_t> & data,uint8_t rssi)598 bool EventPacketBuilder::AddLeAdvertisingReport(LeAdvertisement::AdvertisementType event_type,
599 LeAdvertisement::AddressType addr_type, const Address& addr,
600 const vector<uint8_t>& data, uint8_t rssi) {
601 CHECK(event_code_ == EventCode::LE_META_EVENT);
602
603 // Upcast the payload to add the next report.
604 LeMetaEventBuilder* meta_ptr = static_cast<LeMetaEventBuilder*>(payload_.get());
605 return meta_ptr->AddLeAdvertisingReport(event_type, addr_type, addr, data, rssi);
606 }
607
608 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.7.65.4
CreateLeRemoteUsedFeaturesEvent(hci::Status status,uint16_t handle,uint64_t features)609 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateLeRemoteUsedFeaturesEvent(hci::Status status,
610 uint16_t handle,
611 uint64_t features) {
612 std::unique_ptr<RawBuilder> meta_evt = LeMetaEventBuilder::CreateLeRemoteUsedFeaturesEvent(status, handle, features);
613 return std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::LE_META_EVENT, std::move(meta_evt)));
614 }
615
CreateRemoteSupportedFeaturesEvent(hci::Status status,uint16_t handle,uint64_t features)616 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateRemoteSupportedFeaturesEvent(hci::Status status,
617 uint16_t handle,
618 uint64_t features) {
619 std::unique_ptr<EventPacketBuilder> evt_ptr =
620 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::READ_REMOTE_SUPPORTED_FEATURES_COMPLETE));
621
622 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
623 CHECK(evt_ptr->AddPayloadOctets2(handle));
624 CHECK(evt_ptr->AddPayloadOctets8(features));
625
626 return evt_ptr;
627 }
628
CreateCommandCompleteLinkKeyRequestReply(hci::Status status,Address address)629 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteLinkKeyRequestReply(hci::Status status,
630 Address address) {
631 std::unique_ptr<EventPacketBuilder> evt_ptr =
632 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::LINK_KEY_REQUEST_REPLY, status);
633
634 CHECK(evt_ptr->AddPayloadAddress(address));
635
636 return evt_ptr;
637 }
638
CreateCommandCompleteLinkKeyRequestNegativeReply(hci::Status status,Address address)639 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteLinkKeyRequestNegativeReply(
640 hci::Status status, Address address) {
641 std::unique_ptr<EventPacketBuilder> evt_ptr =
642 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::LINK_KEY_REQUEST_NEGATIVE_REPLY, status);
643
644 CHECK(evt_ptr->AddPayloadAddress(address));
645
646 return evt_ptr;
647 }
648
CreateCommandCompleteWriteLinkPolicySettings(hci::Status status,uint16_t handle)649 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteWriteLinkPolicySettings(hci::Status status,
650 uint16_t handle) {
651 std::unique_ptr<EventPacketBuilder> evt_ptr =
652 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::WRITE_LINK_POLICY_SETTINGS, status);
653
654 CHECK(evt_ptr->AddPayloadOctets2(handle));
655
656 return evt_ptr;
657 }
658
CreateCommandCompleteWriteLinkSupervisionTimeout(hci::Status status,uint16_t handle)659 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteWriteLinkSupervisionTimeout(
660 hci::Status status, uint16_t handle) {
661 std::unique_ptr<EventPacketBuilder> evt_ptr =
662 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::WRITE_LINK_SUPERVISION_TIMEOUT, status);
663
664 CHECK(evt_ptr->AddPayloadOctets2(handle));
665
666 return evt_ptr;
667 }
668
669 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.2
CreateCommandCompleteLeReadBufferSize(hci::Status status,uint16_t hc_le_data_packet_length,uint8_t hc_total_num_le_data_packets)670 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteLeReadBufferSize(
671 hci::Status status, uint16_t hc_le_data_packet_length, uint8_t hc_total_num_le_data_packets) {
672 std::unique_ptr<EventPacketBuilder> evt_ptr =
673 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::LE_READ_BUFFER_SIZE, status);
674
675 CHECK(evt_ptr->AddPayloadOctets2(hc_le_data_packet_length));
676 CHECK(evt_ptr->AddPayloadOctets1(hc_total_num_le_data_packets));
677
678 return evt_ptr;
679 }
680
681 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.3
CreateCommandCompleteLeReadLocalSupportedFeatures(hci::Status status,uint64_t le_features)682 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteLeReadLocalSupportedFeatures(
683 hci::Status status, uint64_t le_features) {
684 std::unique_ptr<EventPacketBuilder> evt_ptr =
685 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::LE_READ_LOCAL_SUPPORTED_FEATURES, status);
686
687 CHECK(evt_ptr->AddPayloadOctets8(le_features));
688
689 return evt_ptr;
690 }
691
692 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.14
CreateCommandCompleteLeReadWhiteListSize(hci::Status status,uint8_t white_list_size)693 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteLeReadWhiteListSize(
694 hci::Status status, uint8_t white_list_size) {
695 std::unique_ptr<EventPacketBuilder> evt_ptr =
696 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::LE_READ_WHITE_LIST_SIZE, status);
697
698 CHECK(evt_ptr->AddPayloadOctets8(white_list_size));
699
700 return evt_ptr;
701 }
702
703 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.23
CreateCommandCompleteLeRand(hci::Status status,uint64_t random_val)704 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteLeRand(hci::Status status,
705 uint64_t random_val) {
706 std::unique_ptr<EventPacketBuilder> evt_ptr =
707 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::LE_RAND, status);
708
709 CHECK(evt_ptr->AddPayloadOctets8(random_val));
710
711 return evt_ptr;
712 }
713
714 // Bluetooth Core Specification Version 4.2, Volume 2, Part E, Section 7.8.27
CreateCommandCompleteLeReadSupportedStates(hci::Status status,uint64_t le_states)715 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteLeReadSupportedStates(hci::Status status,
716 uint64_t le_states) {
717 std::unique_ptr<EventPacketBuilder> evt_ptr =
718 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::LE_READ_SUPPORTED_STATES, status);
719
720 CHECK(evt_ptr->AddPayloadOctets8(le_states));
721
722 return evt_ptr;
723 }
724
725 // Vendor-specific commands
726
CreateCommandCompleteLeGetVendorCapabilities(hci::Status status,const vector<uint8_t> & vendor_cap)727 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateCommandCompleteLeGetVendorCapabilities(
728 hci::Status status, const vector<uint8_t>& vendor_cap) {
729 std::unique_ptr<EventPacketBuilder> evt_ptr =
730 EventPacketBuilder::CreateCommandCompleteOnlyStatusEvent(OpCode::LE_GET_VENDOR_CAPABILITIES, status);
731
732 CHECK(evt_ptr->AddPayloadOctets(vendor_cap));
733
734 return evt_ptr;
735 }
736
CreateEncryptionChange(hci::Status status,uint16_t handle,uint8_t encryption_enable)737 std::unique_ptr<EventPacketBuilder> EventPacketBuilder::CreateEncryptionChange(hci::Status status, uint16_t handle,
738 uint8_t encryption_enable) {
739 std::unique_ptr<EventPacketBuilder> evt_ptr =
740 std::unique_ptr<EventPacketBuilder>(new EventPacketBuilder(EventCode::ENCRYPTION_CHANGE));
741
742 CHECK(evt_ptr->AddPayloadOctets1(static_cast<uint8_t>(status)));
743 CHECK(evt_ptr->AddPayloadOctets2(handle));
744 CHECK(evt_ptr->AddPayloadOctets1(encryption_enable));
745
746 return evt_ptr;
747 }
748
size() const749 size_t EventPacketBuilder::size() const {
750 size_t header_size = 2; // Event code and payload size
751 return header_size + payload_->size();
752 }
753
Serialize(std::back_insert_iterator<std::vector<uint8_t>> it) const754 void EventPacketBuilder::Serialize(std::back_insert_iterator<std::vector<uint8_t>> it) const {
755 insert(static_cast<uint8_t>(event_code_), it);
756 uint8_t payload_size = size() - 2; // Event code and payload size
757 CHECK(size() - 2 == static_cast<size_t>(payload_size)) << "Payload too large for an event: " << size();
758 insert(payload_size, it);
759 payload_->Serialize(it);
760 }
761
CanAddPayloadOctets(size_t octets)762 bool EventPacketBuilder::CanAddPayloadOctets(size_t octets) {
763 return payload_->CanAddOctets(octets);
764 }
765
AddPayloadOctets(size_t octets,const std::vector<uint8_t> & bytes)766 bool EventPacketBuilder::AddPayloadOctets(size_t octets, const std::vector<uint8_t>& bytes) {
767 return payload_->AddOctets(octets, bytes);
768 }
769
AddPayloadOctets(const std::vector<uint8_t> & bytes)770 bool EventPacketBuilder::AddPayloadOctets(const std::vector<uint8_t>& bytes) {
771 return payload_->AddOctets(bytes);
772 }
773
AddPayloadOctets1(uint8_t value)774 bool EventPacketBuilder::AddPayloadOctets1(uint8_t value) {
775 return payload_->AddOctets1(value);
776 }
777
AddPayloadOctets2(uint16_t value)778 bool EventPacketBuilder::AddPayloadOctets2(uint16_t value) {
779 return payload_->AddOctets2(value);
780 }
781
AddPayloadOctets3(uint32_t value)782 bool EventPacketBuilder::AddPayloadOctets3(uint32_t value) {
783 return payload_->AddOctets3(value);
784 }
785
AddPayloadOctets4(uint32_t value)786 bool EventPacketBuilder::AddPayloadOctets4(uint32_t value) {
787 return payload_->AddOctets4(value);
788 }
789
AddPayloadOctets6(uint64_t value)790 bool EventPacketBuilder::AddPayloadOctets6(uint64_t value) {
791 return payload_->AddOctets6(value);
792 }
793
AddPayloadOctets8(uint64_t value)794 bool EventPacketBuilder::AddPayloadOctets8(uint64_t value) {
795 return payload_->AddOctets8(value);
796 }
797
AddPayloadAddress(Address address)798 bool EventPacketBuilder::AddPayloadAddress(Address address) {
799 return payload_->AddAddress(address);
800 }
801
AddBuilder(std::unique_ptr<BasePacketBuilder> builder)802 bool EventPacketBuilder::AddBuilder(std::unique_ptr<BasePacketBuilder> builder) {
803 // Upcast the payload to add the next builder.
804 CountedBuilder* temp_ptr = static_cast<CountedBuilder*>(payload_.get());
805 temp_ptr->Add(std::move(builder));
806 return true;
807 }
808
809 } // namespace packets
810 } // namespace test_vendor_lib
811