1 /*
2  * Copyright 2019 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 <gtest/gtest.h>
18 
19 #include <memory>
20 
21 #define PACKET_TESTING  // Instantiate the tests in the packet files
22 #include "hci/hci_packets.h"
23 #include "packet/bit_inserter.h"
24 #include "packet/raw_builder.h"
25 
26 using bluetooth::packet::BitInserter;
27 using std::vector;
28 
29 namespace bluetooth {
30 namespace hci {
31 
32 std::vector<uint8_t> pixel_3_xl_write_extended_inquiry_response{
33         0x52, 0x0c, 0xf1, 0x01, 0x0b, 0x09, 0x50, 0x69, 0x78, 0x65, 0x6c, 0x20, 0x33, 0x20, 0x58,
34         0x4c, 0x19, 0x03, 0x05, 0x11, 0x0a, 0x11, 0x0c, 0x11, 0x0e, 0x11, 0x12, 0x11, 0x15, 0x11,
35         0x16, 0x11, 0x1f, 0x11, 0x2d, 0x11, 0x2f, 0x11, 0x00, 0x12, 0x32, 0x11, 0x01, 0x05, 0x81,
36         0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49         0x00, 0x00, 0x00, 0x00};
50 
51 std::vector<uint8_t> pixel_3_xl_write_extended_inquiry_response_no_uuids{
52         0x52, 0x0c, 0xf1, 0x01, 0x0b, 0x09, 0x50, 0x69, 0x78, 0x65, 0x6c, 0x20, 0x33, 0x20, 0x58,
53         0x4c, 0x01, 0x03, 0x01, 0x05, 0x81, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68         0x00, 0x00, 0x00, 0x00};
69 
70 std::vector<uint8_t> pixel_3_xl_write_extended_inquiry_response_no_uuids_just_eir{
71         pixel_3_xl_write_extended_inquiry_response_no_uuids.begin() +
72                 4,  // skip command, size, and fec_required
73         pixel_3_xl_write_extended_inquiry_response_no_uuids.end()};
74 
TEST(HciPacketsTest,testWriteExtendedInquiryResponse)75 TEST(HciPacketsTest, testWriteExtendedInquiryResponse) {
76   std::shared_ptr<std::vector<uint8_t>> view_bytes =
77           std::make_shared<std::vector<uint8_t>>(pixel_3_xl_write_extended_inquiry_response);
78 
79   PacketView<kLittleEndian> packet_bytes_view(view_bytes);
80   auto view = WriteExtendedInquiryResponseView::Create(CommandView::Create(packet_bytes_view));
81   ASSERT_TRUE(view.IsValid());
82   auto gap_data = view.GetExtendedInquiryResponse();
83   ASSERT_GE(gap_data.size(), 4ul);
84   ASSERT_EQ(gap_data[0].data_type_, GapDataType::COMPLETE_LOCAL_NAME);
85   ASSERT_EQ(gap_data[0].data_.size(), 10ul);
86   ASSERT_EQ(gap_data[1].data_type_, GapDataType::COMPLETE_LIST_16_BIT_UUIDS);
87   ASSERT_EQ(gap_data[1].data_.size(), 24ul);
88   ASSERT_EQ(gap_data[2].data_type_, GapDataType::COMPLETE_LIST_32_BIT_UUIDS);
89   ASSERT_EQ(gap_data[2].data_.size(), 0ul);
90   ASSERT_EQ(gap_data[3].data_type_, GapDataType::COMPLETE_LIST_128_BIT_UUIDS);
91   ASSERT_EQ(gap_data[3].data_.size(), 128ul);
92 
93   std::vector<GapData> no_padding{gap_data.begin(), gap_data.begin() + 4};
94   auto builder = WriteExtendedInquiryResponseBuilder::Create(view.GetFecRequired(), no_padding);
95 
96   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
97   BitInserter it(*packet_bytes);
98   builder->Serialize(it);
99 
100   EXPECT_EQ(packet_bytes->size(), view_bytes->size());
101   for (size_t i = 0; i < view_bytes->size(); i++) {
102     ASSERT_EQ(packet_bytes->at(i), view_bytes->at(i));
103   }
104 }
105 
106 //  TODO: Revisit reflection tests for EIR
107 // DEFINE_AND_INSTANTIATE_WriteExtendedInquiryResponseReflectionTest(pixel_3_xl_write_extended_inquiry_response,
108 // pixel_3_xl_write_extended_inquiry_response_no_uuids);
109 
110 std::vector<uint8_t> le_set_scan_parameters{
111         0x0b, 0x20, 0x07, 0x01, 0x12, 0x00, 0x12, 0x00, 0x01, 0x00,
112 };
TEST(HciPacketsTest,testLeSetScanParameters)113 TEST(HciPacketsTest, testLeSetScanParameters) {
114   PacketView<kLittleEndian> packet_bytes_view(
115           std::make_shared<std::vector<uint8_t>>(le_set_scan_parameters));
116   auto view = LeSetScanParametersView::Create(
117           LeScanningCommandView::Create(CommandView::Create(packet_bytes_view)));
118 
119   ASSERT_TRUE(view.IsValid());
120   ASSERT_EQ(LeScanType::ACTIVE, view.GetLeScanType());
121   ASSERT_EQ(0x12, view.GetLeScanInterval());
122   ASSERT_EQ(0x12, view.GetLeScanWindow());
123   ASSERT_EQ(OwnAddressType::RANDOM_DEVICE_ADDRESS, view.GetOwnAddressType());
124   ASSERT_EQ(LeScanningFilterPolicy::ACCEPT_ALL, view.GetScanningFilterPolicy());
125 }
126 
127 std::vector<uint8_t> le_set_scan_enable{
128         0x0c, 0x20, 0x02, 0x01, 0x00,
129 };
TEST(HciPacketsTest,testLeSetScanEnable)130 TEST(HciPacketsTest, testLeSetScanEnable) {
131   PacketView<kLittleEndian> packet_bytes_view(
132           std::make_shared<std::vector<uint8_t>>(le_set_scan_enable));
133   auto view = LeSetScanEnableView::Create(
134           LeScanningCommandView::Create(CommandView::Create(packet_bytes_view)));
135 
136   ASSERT_TRUE(view.IsValid());
137   ASSERT_EQ(Enable::ENABLED, view.GetLeScanEnable());
138   ASSERT_EQ(Enable::DISABLED, view.GetFilterDuplicates());
139 }
140 
141 std::vector<uint8_t> le_get_vendor_capabilities{
142         0x53,
143         0xfd,
144         0x00,
145 };
TEST(HciPacketsTest,testLeGetVendorCapabilities)146 TEST(HciPacketsTest, testLeGetVendorCapabilities) {
147   PacketView<kLittleEndian> packet_bytes_view(
148           std::make_shared<std::vector<uint8_t>>(le_get_vendor_capabilities));
149   auto view = LeGetVendorCapabilitiesView::Create(
150           VendorCommandView::Create(CommandView::Create(packet_bytes_view)));
151 
152   ASSERT_TRUE(view.IsValid());
153 }
154 
155 std::vector<uint8_t> le_get_vendor_capabilities_complete{
156         0x0e, 0x0c, 0x01, 0x53, 0xfd, 0x00, 0x05, 0x01, 0x00, 0x04, 0x80, 0x01, 0x10, 0x01,
157 };
TEST(HciPacketsTest,testLeGetVendorCapabilitiesComplete)158 TEST(HciPacketsTest, testLeGetVendorCapabilitiesComplete) {
159   PacketView<kLittleEndian> packet_bytes_view(
160           std::make_shared<std::vector<uint8_t>>(le_get_vendor_capabilities_complete));
161   auto view = LeGetVendorCapabilitiesCompleteView::Create(
162           CommandCompleteView::Create(EventView::Create(packet_bytes_view)));
163 
164   ASSERT_TRUE(view.IsValid());
165   auto base_capabilities = view.GetBaseVendorCapabilities();
166   ASSERT_EQ(5, base_capabilities.max_advt_instances_);
167   ASSERT_EQ(1, base_capabilities.offloaded_resolution_of_private_address_);
168   ASSERT_EQ(1024, base_capabilities.total_scan_results_storage_);
169   ASSERT_EQ(128, base_capabilities.max_irk_list_sz_);
170   ASSERT_EQ(1, base_capabilities.filtering_support_);
171   ASSERT_EQ(16, base_capabilities.max_filter_);
172   ASSERT_EQ(1, base_capabilities.activity_energy_info_support_);
173 }
174 
175 std::vector<uint8_t> le_set_extended_scan_parameters{
176         0x41, 0x20, 0x08, 0x01, 0x00, 0x01, 0x01, 0x12, 0x00, 0x12, 0x00,
177 };
178 
TEST(HciPacketsTest,testLeSetExtendedScanParameters)179 TEST(HciPacketsTest, testLeSetExtendedScanParameters) {
180   PacketView<kLittleEndian> packet_bytes_view(
181           std::make_shared<std::vector<uint8_t>>(le_set_extended_scan_parameters));
182   auto view = LeSetExtendedScanParametersView::Create(
183           LeScanningCommandView::Create(CommandView::Create(packet_bytes_view)));
184 
185   ASSERT_TRUE(view.IsValid());
186   ASSERT_EQ(1, view.GetScanningPhys());
187   auto params = view.GetParameters();
188   ASSERT_EQ(1ul, params.size());
189   ASSERT_EQ(LeScanType::ACTIVE, params[0].le_scan_type_);
190   ASSERT_EQ(18, params[0].le_scan_interval_);
191   ASSERT_EQ(18, params[0].le_scan_window_);
192 }
193 
194 std::vector<uint8_t> le_set_extended_scan_parameters_6553{
195         0x41, 0x20, 0x08, 0x01, 0x00, 0x01, 0x01, 0x99, 0x19, 0x99, 0x19,
196 };
197 
TEST(HciPacketsTest,testLeSetExtendedScanParameters_6553)198 TEST(HciPacketsTest, testLeSetExtendedScanParameters_6553) {
199   PacketView<kLittleEndian> packet_bytes_view(
200           std::make_shared<std::vector<uint8_t>>(le_set_extended_scan_parameters_6553));
201   auto view = LeSetExtendedScanParametersView::Create(
202           LeScanningCommandView::Create(CommandView::Create(packet_bytes_view)));
203 
204   ASSERT_TRUE(view.IsValid());
205   ASSERT_EQ(1, view.GetScanningPhys());
206   auto params = view.GetParameters();
207   ASSERT_EQ(1ul, params.size());
208   ASSERT_EQ(LeScanType::ACTIVE, params[0].le_scan_type_);
209   ASSERT_EQ(6553, params[0].le_scan_interval_);
210   ASSERT_EQ(6553, params[0].le_scan_window_);
211 }
212 
213 std::vector<uint8_t> le_set_extended_scan_enable{
214         0x42, 0x20, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
215 };
216 
TEST(HciPacketsTest,testLeSetExtendedScanEnable)217 TEST(HciPacketsTest, testLeSetExtendedScanEnable) {
218   PacketView<kLittleEndian> packet_bytes_view(
219           std::make_shared<std::vector<uint8_t>>(le_set_extended_scan_enable));
220   auto view = LeSetExtendedScanEnableView::Create(
221           LeScanningCommandView::Create(CommandView::Create(packet_bytes_view)));
222 
223   ASSERT_TRUE(view.IsValid());
224   ASSERT_EQ(FilterDuplicates::DISABLED, view.GetFilterDuplicates());
225   ASSERT_EQ(Enable::ENABLED, view.GetEnable());
226   ASSERT_EQ(0, view.GetDuration());
227   ASSERT_EQ(0, view.GetPeriod());
228 }
229 
230 std::vector<uint8_t> le_set_extended_scan_enable_disable{
231         0x42, 0x20, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
232 };
233 
TEST(HciPacketsTest,testLeSetExtendedScanEnableDisable)234 TEST(HciPacketsTest, testLeSetExtendedScanEnableDisable) {
235   PacketView<kLittleEndian> packet_bytes_view(
236           std::make_shared<std::vector<uint8_t>>(le_set_extended_scan_enable_disable));
237   auto view = LeSetExtendedScanEnableView::Create(
238           LeScanningCommandView::Create(CommandView::Create(packet_bytes_view)));
239 
240   ASSERT_TRUE(view.IsValid());
241   ASSERT_EQ(FilterDuplicates::ENABLED, view.GetFilterDuplicates());
242   ASSERT_EQ(Enable::DISABLED, view.GetEnable());
243   ASSERT_EQ(0, view.GetDuration());
244   ASSERT_EQ(0, view.GetPeriod());
245 }
246 
247 std::vector<uint8_t> le_extended_create_connection = {
248         0x43, 0x20, 0x2a, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x08,
249         0x30, 0x00, 0x18, 0x00, 0x28, 0x00, 0x00, 0x00, 0xf4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
250         0x08, 0x30, 0x00, 0x18, 0x00, 0x28, 0x00, 0x00, 0x00, 0xf4, 0x01, 0x00, 0x00, 0x00, 0x00};
251 
TEST(HciPacketsTest,testLeExtendedCreateConnection)252 TEST(HciPacketsTest, testLeExtendedCreateConnection) {
253   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
254           std::make_shared<std::vector<uint8_t>>(le_extended_create_connection);
255   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
256   auto view = LeExtendedCreateConnectionView::Create(LeConnectionManagementCommandView::Create(
257           AclCommandView::Create(CommandView::Create(packet_bytes_view))));
258   ASSERT_TRUE(view.IsValid());
259 }
260 
261 std::vector<uint8_t> le_set_extended_advertising_random_address = {
262         0x35, 0x20, 0x07, 0x00, 0x77, 0x58, 0xeb, 0xd3, 0x1c, 0x6e,
263 };
264 
TEST(HciPacketsTest,testLeSetAdvertisingSetRandomAddress)265 TEST(HciPacketsTest, testLeSetAdvertisingSetRandomAddress) {
266   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
267           std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_random_address);
268   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
269   auto view = LeSetAdvertisingSetRandomAddressView::Create(
270           LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
271   ASSERT_TRUE(view.IsValid());
272   uint8_t random_address_bytes[] = {0x77, 0x58, 0xeb, 0xd3, 0x1c, 0x6e};
273   ASSERT_EQ(0, view.GetAdvertisingHandle());
274   ASSERT_EQ(Address(random_address_bytes), view.GetRandomAddress());
275 }
276 
277 std::vector<uint8_t> le_set_extended_advertising_data{
278         0x37, 0x20, 0x12, 0x00, 0x03, 0x01, 0x0e, 0x02, 0x01, 0x02, 0x0a,
279         0x09, 0x50, 0x69, 0x78, 0x65, 0x6c, 0x20, 0x33, 0x20, 0x58,
280 };
TEST(HciPacketsTest,testLeSetExtendedAdvertisingData)281 TEST(HciPacketsTest, testLeSetExtendedAdvertisingData) {
282   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
283           std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_data);
284   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
285   auto view = LeSetExtendedAdvertisingDataRawView::Create(
286           LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
287   ASSERT_TRUE(view.IsValid());
288   ASSERT_EQ(0, view.GetAdvertisingHandle());
289   ASSERT_EQ(Operation::COMPLETE_ADVERTISEMENT, view.GetOperation());
290   ASSERT_EQ(FragmentPreference::CONTROLLER_SHOULD_NOT, view.GetFragmentPreference());
291   std::vector<uint8_t> advertising_data{
292           0x02, 0x01, 0x02, 0x0a, 0x09, 0x50, 0x69, 0x78, 0x65, 0x6c, 0x20, 0x33, 0x20, 0x58,
293   };
294   auto payload = view.GetPayload();
295   std::vector<uint8_t> payload_data(payload.begin(), payload.end());
296   ASSERT_EQ(advertising_data, payload_data);
297 }
298 
299 std::vector<uint8_t> le_set_extended_advertising_parameters_set_0{
300         0x36, 0x20, 0x19, 0x00, 0x13, 0x00, 0x90, 0x01, 0x00, 0xc2, 0x01, 0x00, 0x07, 0x01,
301         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf9, 0x01, 0x00, 0x01, 0x01, 0x00,
302 };
TEST(HciPacketsTest,testLeSetExtendedAdvertisingParametersLegacySet0)303 TEST(HciPacketsTest, testLeSetExtendedAdvertisingParametersLegacySet0) {
304   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
305           std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_parameters_set_0);
306   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
307   auto view = LeSetExtendedAdvertisingParametersLegacyView::Create(
308           LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
309   ASSERT_TRUE(view.IsValid());
310   ASSERT_EQ(0, view.GetAdvertisingHandle());
311   ASSERT_EQ(400ul, view.GetPrimaryAdvertisingIntervalMin());
312   ASSERT_EQ(450ul, view.GetPrimaryAdvertisingIntervalMax());
313   ASSERT_EQ(0x7, view.GetPrimaryAdvertisingChannelMap());
314   ASSERT_EQ(OwnAddressType::RANDOM_DEVICE_ADDRESS, view.GetOwnAddressType());
315   ASSERT_EQ(PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, view.GetPeerAddressType());
316   ASSERT_EQ(Address::kEmpty, view.GetPeerAddress());
317   ASSERT_EQ(AdvertisingFilterPolicy::ALL_DEVICES, view.GetAdvertisingFilterPolicy());
318   ASSERT_EQ(1, view.GetAdvertisingSid());
319   ASSERT_EQ(Enable::DISABLED, view.GetScanRequestNotificationEnable());
320 }
321 
322 std::vector<uint8_t> le_set_extended_advertising_parameters_set_1{
323         0x36, 0x20, 0x19, 0x01, 0x13, 0x00, 0x90, 0x01, 0x00, 0xc2, 0x01, 0x00, 0x07, 0x01,
324         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf9, 0x01, 0x00, 0x01, 0x01, 0x00,
325 };
TEST(HciPacketsTest,testLeSetExtendedAdvertisingParametersSet1)326 TEST(HciPacketsTest, testLeSetExtendedAdvertisingParametersSet1) {
327   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
328           std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_parameters_set_1);
329   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
330   auto view = LeSetExtendedAdvertisingParametersLegacyView::Create(
331           LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
332   ASSERT_TRUE(view.IsValid());
333   ASSERT_EQ(1, view.GetAdvertisingHandle());
334   ASSERT_EQ(400ul, view.GetPrimaryAdvertisingIntervalMin());
335   ASSERT_EQ(450ul, view.GetPrimaryAdvertisingIntervalMax());
336   ASSERT_EQ(0x7, view.GetPrimaryAdvertisingChannelMap());
337   ASSERT_EQ(OwnAddressType::RANDOM_DEVICE_ADDRESS, view.GetOwnAddressType());
338   ASSERT_EQ(PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, view.GetPeerAddressType());
339   ASSERT_EQ(Address::kEmpty, view.GetPeerAddress());
340   ASSERT_EQ(AdvertisingFilterPolicy::ALL_DEVICES, view.GetAdvertisingFilterPolicy());
341   ASSERT_EQ(1, view.GetAdvertisingSid());
342   ASSERT_EQ(Enable::DISABLED, view.GetScanRequestNotificationEnable());
343 }
344 
345 std::vector<uint8_t> le_set_extended_advertising_parameters_complete{0x0e, 0x05, 0x01, 0x36,
346                                                                      0x20, 0x00, 0xf5};
TEST(HciPacketsTest,testLeSetExtendedAdvertisingParametersComplete)347 TEST(HciPacketsTest, testLeSetExtendedAdvertisingParametersComplete) {
348   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
349           std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_parameters_complete);
350   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
351   auto view = LeSetExtendedAdvertisingParametersCompleteView::Create(
352           CommandCompleteView::Create(EventView::Create(packet_bytes_view)));
353   ASSERT_TRUE(view.IsValid());
354   ASSERT_EQ(static_cast<uint8_t>(-11), view.GetSelectedTxPower());
355 }
356 
357 std::vector<uint8_t> le_remove_advertising_set_1{
358         0x3c,
359         0x20,
360         0x01,
361         0x01,
362 };
TEST(HciPacketsTest,testLeRemoveAdvertisingSet1)363 TEST(HciPacketsTest, testLeRemoveAdvertisingSet1) {
364   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
365           std::make_shared<std::vector<uint8_t>>(le_remove_advertising_set_1);
366   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
367   auto view = LeRemoveAdvertisingSetView::Create(
368           LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
369   ASSERT_TRUE(view.IsValid());
370   ASSERT_EQ(1, view.GetAdvertisingHandle());
371 }
372 
373 std::vector<uint8_t> le_set_extended_advertising_disable_1{
374         0x39, 0x20, 0x06, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00,
375 };
TEST(HciPacketsTest,testLeSetExtendedAdvertisingDisable1)376 TEST(HciPacketsTest, testLeSetExtendedAdvertisingDisable1) {
377   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
378           std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_disable_1);
379   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
380   auto view = LeSetExtendedAdvertisingDisableView::Create(
381           LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
382   ASSERT_TRUE(view.IsValid());
383   auto disabled_set = view.GetDisabledSets();
384   ASSERT_EQ(1ul, disabled_set.size());
385   ASSERT_EQ(1, disabled_set[0].advertising_handle_);
386 }
387 
TEST(HciPacketsTest,testLeSetAdvertisingDataBuilderLength)388 TEST(HciPacketsTest, testLeSetAdvertisingDataBuilderLength) {
389   GapData gap_data;
390   gap_data.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
391   gap_data.data_ = std::vector<uint8_t>({'A', ' ', 'g', 'o', 'o', 'd', ' ', 'n', 'a', 'm', 'e'});
392   auto builder = LeSetAdvertisingDataBuilder::Create({gap_data});
393   ASSERT_EQ(2ul /*opcode*/ + 1ul /* parameter size */ + 1ul /* data_length */ + 31ul /* data */,
394             builder->size());
395 
396   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
397   packet_bytes->reserve(builder->size());
398   BitInserter bit_inserter(*packet_bytes);
399   builder->Serialize(bit_inserter);
400   auto command_view = LeAdvertisingCommandView::Create(
401           CommandView::Create(PacketView<kLittleEndian>(packet_bytes)));
402   ASSERT_TRUE(command_view.IsValid());
403   ASSERT_EQ(1ul /* data_length */ + 31ul /* data */, command_view.GetPayload().size());
404   auto view = LeSetAdvertisingDataView::Create(command_view);
405   ASSERT_TRUE(view.IsValid());
406 }
407 
TEST(HciPacketsTest,testLeSetScanResponseDataBuilderLength)408 TEST(HciPacketsTest, testLeSetScanResponseDataBuilderLength) {
409   GapData gap_data;
410   gap_data.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
411   gap_data.data_ = std::vector<uint8_t>({'A', ' ', 'g', 'o', 'o', 'd', ' ', 'n', 'a', 'm', 'e'});
412   auto builder = LeSetScanResponseDataBuilder::Create({gap_data});
413   ASSERT_EQ(2ul /*opcode*/ + 1ul /* parameter size */ + 1ul /*data_length */ + 31ul /* data */,
414             builder->size());
415 
416   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
417   packet_bytes->reserve(builder->size());
418   BitInserter bit_inserter(*packet_bytes);
419   builder->Serialize(bit_inserter);
420   auto command_view = LeAdvertisingCommandView::Create(
421           CommandView::Create(PacketView<kLittleEndian>(packet_bytes)));
422   ASSERT_TRUE(command_view.IsValid());
423   ASSERT_EQ(1ul /* data_length */ + 31ul /* data */, command_view.GetPayload().size());
424   auto view = LeSetScanResponseDataView::Create(command_view);
425   ASSERT_TRUE(view.IsValid());
426 }
427 
TEST(HciPacketsTest,testLeMultiAdvSetAdvertisingDataBuilderLength)428 TEST(HciPacketsTest, testLeMultiAdvSetAdvertisingDataBuilderLength) {
429   GapData gap_data;
430   gap_data.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
431   gap_data.data_ = std::vector<uint8_t>({'A', ' ', 'g', 'o', 'o', 'd', ' ', 'n', 'a', 'm', 'e'});
432   uint8_t set = 3;
433   auto builder = LeMultiAdvtSetDataBuilder::Create({gap_data}, set);
434 
435   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
436   packet_bytes->reserve(builder->size());
437   BitInserter bit_inserter(*packet_bytes);
438   builder->Serialize(bit_inserter);
439   auto command_view =
440           LeMultiAdvtSetDataView::Create(LeMultiAdvtView::Create(LeAdvertisingCommandView::Create(
441                   CommandView::Create(PacketView<kLittleEndian>(packet_bytes)))));
442   ASSERT_TRUE(command_view.IsValid());
443   auto view = LeMultiAdvtSetDataView::Create(command_view);
444   ASSERT_TRUE(view.IsValid());
445   ASSERT_GT(view.GetAdvertisingData().size(), 0ul);
446   ASSERT_EQ(view.GetAdvertisingData()[0].data_, gap_data.data_);
447   ASSERT_EQ(view.GetAdvertisingInstance(), 3);
448 }
449 
TEST(HciPacketsTest,testLeMultiAdvSetScanResponseDataBuilderLength)450 TEST(HciPacketsTest, testLeMultiAdvSetScanResponseDataBuilderLength) {
451   GapData gap_data;
452   gap_data.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
453   gap_data.data_ = std::vector<uint8_t>({'A', ' ', 'g', 'o', 'o', 'd', ' ', 'n', 'a', 'm', 'e'});
454   uint8_t set = 3;
455   auto builder = LeMultiAdvtSetScanRespBuilder::Create({gap_data}, set);
456 
457   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
458   packet_bytes->reserve(builder->size());
459   BitInserter bit_inserter(*packet_bytes);
460   builder->Serialize(bit_inserter);
461   auto command_view = LeMultiAdvtSetScanRespView::Create(
462           LeMultiAdvtView::Create(LeAdvertisingCommandView::Create(
463                   CommandView::Create(PacketView<kLittleEndian>(packet_bytes)))));
464   ASSERT_TRUE(command_view.IsValid());
465   auto view = LeMultiAdvtSetScanRespView::Create(command_view);
466   ASSERT_TRUE(view.IsValid());
467   ASSERT_EQ(view.GetAdvertisingData()[0].data_, gap_data.data_);
468   ASSERT_EQ(view.GetAdvertisingInstance(), 3);
469 }
470 
TEST(HciPacketsTest,testMsftReadSupportedFeatures)471 TEST(HciPacketsTest, testMsftReadSupportedFeatures) {
472   // MSFT opcode is not defined in PDL.
473   auto msft_opcode = static_cast<OpCode>(0xfc01);
474 
475   auto builder = MsftReadSupportedFeaturesBuilder::Create(msft_opcode);
476 
477   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
478   packet_bytes->reserve(builder->size());
479   BitInserter bit_inserter(*packet_bytes);
480   builder->Serialize(bit_inserter);
481 
482   std::vector<uint8_t> expected_bytes{
483           0x01,  // Vendor command opcode and MSFT base code.
484           0xfc,
485           0x01,  // Packet length
486           0x00,  // Subcommand Opcode for Read Supported Features
487   };
488   ASSERT_EQ(expected_bytes, *packet_bytes);
489 }
490 
TEST(HciPacketsTest,testMsftLeMonitorAdvUuid)491 TEST(HciPacketsTest, testMsftLeMonitorAdvUuid) {
492   // MSFT opcode is not defined in PDL.
493   auto msft_opcode = static_cast<OpCode>(0xfc01);
494 
495   auto builder = MsftLeMonitorAdvConditionUuid2Builder::Create(
496           msft_opcode, 0x10 /* RSSI threshold high */, 0x11 /* RSSI threshold low */,
497           0x12 /* RSSI threshold low timeout */, 0x13 /* RSSI sampling period */,
498           std::array<uint8_t, 2>{0x71, 0x72} /* 16-bit UUID */);
499 
500   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
501   packet_bytes->reserve(builder->size());
502   BitInserter bit_inserter(*packet_bytes);
503   builder->Serialize(bit_inserter);
504 
505   std::vector<uint8_t> expected_bytes{
506           0x01,  // Vendor command opcode and MSFT base code.
507           0xfc,
508           0x09,  // Packet length
509           0x03,  // Subcommand Opcode for LE Monitor Adv
510           0x10,  // RSSI threshold high
511           0x11,  // RSSI threshold low
512           0x12,  // RSSI threshold low timeout
513           0x13,  // RSSI sampling period
514           0x02,  // Condition type = UUID
515           0x01,  // UUID type = 16-bit UUID
516           0x71,  // UUID content
517           0x72,
518   };
519   ASSERT_EQ(expected_bytes, *packet_bytes);
520 }
521 
TEST(HciPacketsTest,testMsftLeMonitorAdvPatternsEmpty)522 TEST(HciPacketsTest, testMsftLeMonitorAdvPatternsEmpty) {
523   // MSFT opcode is not defined in PDL.
524   auto msft_opcode = static_cast<OpCode>(0xfc01);
525 
526   std::vector<MsftLeMonitorAdvConditionPattern> patterns;
527 
528   auto builder = MsftLeMonitorAdvConditionPatternsBuilder::Create(
529           msft_opcode, 0x10 /* RSSI threshold high */, 0x11 /* RSSI threshold low */,
530           0x12 /* RSSI threshold low timeout */, 0x13 /* RSSI sampling period */, patterns);
531 
532   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
533   packet_bytes->reserve(builder->size());
534   BitInserter bit_inserter(*packet_bytes);
535   builder->Serialize(bit_inserter);
536 
537   std::vector<uint8_t> expected_bytes{
538           0x01,  // Vendor command opcode and MSFT base code.
539           0xfc,
540           0x07,  // Packet length
541           0x03,  // Subcommand Opcode for LE Monitor Adv
542           0x10,  // RSSI threshold high
543           0x11,  // RSSI threshold low
544           0x12,  // RSSI threshold low timeout
545           0x13,  // RSSI sampling period
546           0x01,  // Condition type = Patterns
547           0x00,  // Number of patterns
548   };
549   ASSERT_EQ(expected_bytes, *packet_bytes);
550 }
551 
TEST(HciPacketsTest,testMsftLeMonitorAdvPatterns)552 TEST(HciPacketsTest, testMsftLeMonitorAdvPatterns) {
553   // MSFT opcode is not defined in PDL.
554   auto msft_opcode = static_cast<OpCode>(0xfc01);
555 
556   MsftLeMonitorAdvConditionPattern pattern1;
557   pattern1.ad_type_ = 0x03;
558   pattern1.start_of_pattern_ = 0x00;
559   pattern1.pattern_ = {1, 2, 3};
560 
561   MsftLeMonitorAdvConditionPattern pattern2;
562   pattern2.ad_type_ = 0x0f;
563   pattern2.start_of_pattern_ = 0x10;
564   pattern2.pattern_ = {0xa1, 0xa2};
565 
566   std::vector<MsftLeMonitorAdvConditionPattern> patterns{pattern1, pattern2};
567 
568   auto builder = MsftLeMonitorAdvConditionPatternsBuilder::Create(
569           msft_opcode, 0x10 /* RSSI threshold high */, 0x11 /* RSSI threshold low */,
570           0x12 /* RSSI threshold low timeout */, 0x13 /* RSSI sampling period */, patterns);
571 
572   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
573   packet_bytes->reserve(builder->size());
574   BitInserter bit_inserter(*packet_bytes);
575   builder->Serialize(bit_inserter);
576 
577   std::vector<uint8_t> expected_bytes{
578           0x01,  // Vendor command opcode and MSFT base code.
579           0xfc,
580           0x12,  // Packet length
581           0x03,  // Subcommand Opcode for LE Monitor Adv
582           0x10,  // RSSI threshold high
583           0x11,  // RSSI threshold low
584           0x12,  // RSSI threshold low timeout
585           0x13,  // RSSI sampling period
586           0x01,  // Condition type = Patterns
587           0x02,  // Number of patterns
588           // Pattern 1
589           0x05,  // Length
590           0x03,  // AD Type
591           0x00,  // Start of pattern
592           0x01,  // Pattern
593           0x02,
594           0x03,
595           // Pattern 2
596           0x04,  // Length
597           0x0f,  // AD Type
598           0x10,  // Start of pattern
599           0xa1,  // Pattern
600           0xa2,
601   };
602   ASSERT_EQ(expected_bytes, *packet_bytes);
603 }
604 
605 std::vector<uint8_t> msft_read_supported_features_complete{
606         0x0e,  // command complete event code
607         0x10,  // event size
608         0x01,  // num_hci_command_packets
609         0x1e,
610         0xfc,  // vendor specific MSFT opcode assigned by Intel
611         0x00,  // status
612         0x00,  // MSFT subcommand opcode
613         0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
614         0x00,  // supported features
615         0x02,  // MSFT event prefix length
616         0x87,
617         0x80,  // prefix: MSFT event prefix provided by Intel
618 };
TEST(HciPacketsTest,testMsftReadSupportedFeaturesComplete)619 TEST(HciPacketsTest, testMsftReadSupportedFeaturesComplete) {
620   PacketView<kLittleEndian> packet_bytes_view(
621           std::make_shared<std::vector<uint8_t>>(msft_read_supported_features_complete));
622   auto view = MsftReadSupportedFeaturesCommandCompleteView::Create(MsftCommandCompleteView::Create(
623           CommandCompleteView::Create(EventView::Create(packet_bytes_view))));
624 
625   ASSERT_TRUE(view.IsValid());
626   ASSERT_EQ(ErrorCode::SUCCESS, view.GetStatus());
627   ASSERT_EQ((uint8_t)0x00, (uint8_t)view.GetSubcommandOpcode());
628   ASSERT_EQ((uint64_t)0x000000000000007f, view.GetSupportedFeatures());
629   ASSERT_EQ(2ul, view.GetPrefix().size());
630 
631   uint16_t prefix = 0;
632   for (auto p : view.GetPrefix()) {
633     prefix = (prefix << 8) + p;
634   }
635   ASSERT_EQ((uint16_t)0x8780, prefix);
636 }
637 
638 }  // namespace hci
639 }  // namespace bluetooth
640