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 #include <memory>
19 
20 #define PACKET_TESTING  // Instantiate the tests in the packet files
21 #include "hci/hci_packets.h"
22 
23 #include "os/log.h"
24 #include "packet/bit_inserter.h"
25 #include "packet/raw_builder.h"
26 
27 using bluetooth::packet::BitInserter;
28 using bluetooth::packet::RawBuilder;
29 using std::vector;
30 
31 namespace bluetooth {
32 namespace hci {
33 
34 std::vector<uint8_t> pixel_3_xl_write_extended_inquiry_response{
35     0x52, 0x0c, 0xf1, 0x01, 0x0b, 0x09, 0x50, 0x69, 0x78, 0x65, 0x6c, 0x20, 0x33, 0x20, 0x58, 0x4c, 0x19, 0x03, 0x05,
36     0x11, 0x0a, 0x11, 0x0c, 0x11, 0x0e, 0x11, 0x12, 0x11, 0x15, 0x11, 0x16, 0x11, 0x1f, 0x11, 0x2d, 0x11, 0x2f, 0x11,
37     0x00, 0x12, 0x32, 0x11, 0x01, 0x05, 0x81, 0x07, 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, 0x00, 0x00, 0x00, 0x00,
39     0x00, 0x00, 0x00, 0x00, 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, 0x00, 0x00, 0x00, 0x00,
41     0x00, 0x00, 0x00, 0x00, 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, 0x00, 0x00, 0x00, 0x00,
43     0x00, 0x00, 0x00, 0x00, 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, 0x00, 0x00, 0x00, 0x00,
45     0x00, 0x00, 0x00, 0x00, 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, 0x00, 0x00, 0x00, 0x00,
47     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
48 
49 std::vector<uint8_t> pixel_3_xl_write_extended_inquiry_response_no_uuids{
50     0x52, 0x0c, 0xf1, 0x01, 0x0b, 0x09, 0x50, 0x69, 0x78, 0x65, 0x6c, 0x20, 0x33, 0x20, 0x58, 0x4c, 0x01, 0x03, 0x01,
51     0x05, 0x81, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54     0x00, 0x00, 0x00, 0x00, 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, 0x00, 0x00, 0x00, 0x00,
56     0x00, 0x00, 0x00, 0x00, 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, 0x00, 0x00, 0x00, 0x00,
58     0x00, 0x00, 0x00, 0x00, 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, 0x00, 0x00, 0x00, 0x00,
60     0x00, 0x00, 0x00, 0x00, 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, 0x00, 0x00, 0x00, 0x00,
62     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
63 
64 std::vector<uint8_t> pixel_3_xl_write_extended_inquiry_response_no_uuids_just_eir{
65     pixel_3_xl_write_extended_inquiry_response_no_uuids.begin() + 4,  // skip command, size, and fec_required
66     pixel_3_xl_write_extended_inquiry_response_no_uuids.end()};
67 
TEST(HciPacketsTest,testWriteExtendedInquiryResponse)68 TEST(HciPacketsTest, testWriteExtendedInquiryResponse) {
69   std::shared_ptr<std::vector<uint8_t>> view_bytes =
70       std::make_shared<std::vector<uint8_t>>(pixel_3_xl_write_extended_inquiry_response);
71 
72   PacketView<kLittleEndian> packet_bytes_view(view_bytes);
73   auto view = WriteExtendedInquiryResponseView::Create(CommandView::Create(packet_bytes_view));
74   ASSERT_TRUE(view.IsValid());
75   auto gap_data = view.GetExtendedInquiryResponse();
76   ASSERT_GE(gap_data.size(), 4);
77   ASSERT_EQ(gap_data[0].data_type_, GapDataType::COMPLETE_LOCAL_NAME);
78   ASSERT_EQ(gap_data[0].data_.size(), 10);
79   ASSERT_EQ(gap_data[1].data_type_, GapDataType::COMPLETE_LIST_16_BIT_UUIDS);
80   ASSERT_EQ(gap_data[1].data_.size(), 24);
81   ASSERT_EQ(gap_data[2].data_type_, GapDataType::COMPLETE_LIST_32_BIT_UUIDS);
82   ASSERT_EQ(gap_data[2].data_.size(), 0);
83   ASSERT_EQ(gap_data[3].data_type_, GapDataType::COMPLETE_LIST_128_BIT_UUIDS);
84   ASSERT_EQ(gap_data[3].data_.size(), 128);
85 
86   std::vector<GapData> no_padding{gap_data.begin(), gap_data.begin() + 4};
87   auto builder = WriteExtendedInquiryResponseBuilder::Create(view.GetFecRequired(), no_padding);
88 
89   std::shared_ptr<std::vector<uint8_t>> packet_bytes = std::make_shared<std::vector<uint8_t>>();
90   BitInserter it(*packet_bytes);
91   builder->Serialize(it);
92 
93   EXPECT_EQ(packet_bytes->size(), view_bytes->size());
94   for (size_t i = 0; i < view_bytes->size(); i++) {
95     ASSERT_EQ(packet_bytes->at(i), view_bytes->at(i));
96   }
97 }
98 
99 //  TODO: Revisit reflection tests for EIR
100 // DEFINE_AND_INSTANTIATE_WriteExtendedInquiryResponseReflectionTest(pixel_3_xl_write_extended_inquiry_response,
101 // pixel_3_xl_write_extended_inquiry_response_no_uuids);
102 
103 std::vector<uint8_t> le_set_scan_parameters{
104     0x0b, 0x20, 0x07, 0x01, 0x12, 0x00, 0x12, 0x00, 0x01, 0x00,
105 };
TEST(HciPacketsTest,testLeSetScanParameters)106 TEST(HciPacketsTest, testLeSetScanParameters) {
107   PacketView<kLittleEndian> packet_bytes_view(std::make_shared<std::vector<uint8_t>>(le_set_scan_parameters));
108   auto view = LeSetScanParametersView::Create(LeScanningCommandView::Create(CommandView::Create(packet_bytes_view)));
109 
110   ASSERT_TRUE(view.IsValid());
111   ASSERT_EQ(LeScanType::ACTIVE, view.GetLeScanType());
112   ASSERT_EQ(0x12, view.GetLeScanInterval());
113   ASSERT_EQ(0x12, view.GetLeScanWindow());
114   ASSERT_EQ(OwnAddressType::RANDOM_DEVICE_ADDRESS, view.GetOwnAddressType());
115   ASSERT_EQ(LeScanningFilterPolicy::ACCEPT_ALL, view.GetScanningFilterPolicy());
116 }
117 
118 std::vector<uint8_t> le_set_scan_enable{
119     0x0c, 0x20, 0x02, 0x01, 0x00,
120 };
TEST(HciPacketsTest,testLeSetScanEnable)121 TEST(HciPacketsTest, testLeSetScanEnable) {
122   PacketView<kLittleEndian> packet_bytes_view(std::make_shared<std::vector<uint8_t>>(le_set_scan_enable));
123   auto view = LeSetScanEnableView::Create(LeScanningCommandView::Create(CommandView::Create(packet_bytes_view)));
124 
125   ASSERT_TRUE(view.IsValid());
126   ASSERT_EQ(Enable::ENABLED, view.GetLeScanEnable());
127   ASSERT_EQ(Enable::DISABLED, view.GetFilterDuplicates());
128 }
129 
130 std::vector<uint8_t> le_get_vendor_capabilities{
131     0x53,
132     0xfd,
133     0x00,
134 };
TEST(HciPacketsTest,testLeGetVendorCapabilities)135 TEST(HciPacketsTest, testLeGetVendorCapabilities) {
136   PacketView<kLittleEndian> packet_bytes_view(std::make_shared<std::vector<uint8_t>>(le_get_vendor_capabilities));
137   auto view = LeGetVendorCapabilitiesView::Create(VendorCommandView::Create(CommandView::Create(packet_bytes_view)));
138 
139   ASSERT_TRUE(view.IsValid());
140 }
141 
142 std::vector<uint8_t> le_get_vendor_capabilities_complete{
143     0x0e, 0x0c, 0x01, 0x53, 0xfd, 0x00, 0x05, 0x01, 0x00, 0x04, 0x80, 0x01, 0x10, 0x01,
144 };
TEST(HciPacketsTest,testLeGetVendorCapabilitiesComplete)145 TEST(HciPacketsTest, testLeGetVendorCapabilitiesComplete) {
146   PacketView<kLittleEndian> packet_bytes_view(
147       std::make_shared<std::vector<uint8_t>>(le_get_vendor_capabilities_complete));
148   auto view =
149       LeGetVendorCapabilitiesCompleteView::Create(CommandCompleteView::Create(EventView::Create(packet_bytes_view)));
150 
151   ASSERT_TRUE(view.IsValid());
152   auto base_capabilities = view.GetBaseVendorCapabilities();
153   ASSERT_EQ(5, base_capabilities.max_advt_instances_);
154   ASSERT_EQ(1, base_capabilities.offloaded_resolution_of_private_address_);
155   ASSERT_EQ(1024, base_capabilities.total_scan_results_storage_);
156   ASSERT_EQ(128, base_capabilities.max_irk_list_sz_);
157   ASSERT_EQ(1, base_capabilities.filtering_support_);
158   ASSERT_EQ(16, base_capabilities.max_filter_);
159   ASSERT_EQ(1, base_capabilities.activity_energy_info_support_);
160 }
161 
162 std::vector<uint8_t> le_set_extended_scan_parameters{
163     0x41, 0x20, 0x08, 0x01, 0x00, 0x01, 0x01, 0x12, 0x00, 0x12, 0x00,
164 };
165 
TEST(HciPacketsTest,testLeSetExtendedScanParameters)166 TEST(HciPacketsTest, testLeSetExtendedScanParameters) {
167   PacketView<kLittleEndian> packet_bytes_view(std::make_shared<std::vector<uint8_t>>(le_set_extended_scan_parameters));
168   auto view =
169       LeSetExtendedScanParametersView::Create(LeScanningCommandView::Create(CommandView::Create(packet_bytes_view)));
170 
171   ASSERT_TRUE(view.IsValid());
172   ASSERT_EQ(1, view.GetScanningPhys());
173   auto params = view.GetParameters();
174   ASSERT_EQ(1, params.size());
175   ASSERT_EQ(LeScanType::ACTIVE, params[0].le_scan_type_);
176   ASSERT_EQ(18, params[0].le_scan_interval_);
177   ASSERT_EQ(18, params[0].le_scan_window_);
178 }
179 
180 std::vector<uint8_t> le_set_extended_scan_parameters_6553{
181     0x41, 0x20, 0x08, 0x01, 0x00, 0x01, 0x01, 0x99, 0x19, 0x99, 0x19,
182 };
183 
TEST(HciPacketsTest,testLeSetExtendedScanParameters_6553)184 TEST(HciPacketsTest, testLeSetExtendedScanParameters_6553) {
185   PacketView<kLittleEndian> packet_bytes_view(
186       std::make_shared<std::vector<uint8_t>>(le_set_extended_scan_parameters_6553));
187   auto view =
188       LeSetExtendedScanParametersView::Create(LeScanningCommandView::Create(CommandView::Create(packet_bytes_view)));
189 
190   ASSERT_TRUE(view.IsValid());
191   ASSERT_EQ(1, view.GetScanningPhys());
192   auto params = view.GetParameters();
193   ASSERT_EQ(1, params.size());
194   ASSERT_EQ(LeScanType::ACTIVE, params[0].le_scan_type_);
195   ASSERT_EQ(6553, params[0].le_scan_interval_);
196   ASSERT_EQ(6553, params[0].le_scan_window_);
197 }
198 
199 std::vector<uint8_t> le_set_extended_scan_enable{
200     0x42, 0x20, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
201 };
202 
TEST(HciPacketsTest,testLeSetExtendedScanEnable)203 TEST(HciPacketsTest, testLeSetExtendedScanEnable) {
204   PacketView<kLittleEndian> packet_bytes_view(std::make_shared<std::vector<uint8_t>>(le_set_extended_scan_enable));
205   auto view =
206       LeSetExtendedScanEnableView::Create(LeScanningCommandView::Create(CommandView::Create(packet_bytes_view)));
207 
208   ASSERT_TRUE(view.IsValid());
209   ASSERT_EQ(FilterDuplicates::DISABLED, view.GetFilterDuplicates());
210   ASSERT_EQ(Enable::ENABLED, view.GetEnable());
211   ASSERT_EQ(0, view.GetDuration());
212   ASSERT_EQ(0, view.GetPeriod());
213 }
214 
215 std::vector<uint8_t> le_set_extended_scan_enable_disable{
216     0x42, 0x20, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
217 };
218 
TEST(HciPacketsTest,testLeSetExtendedScanEnableDisable)219 TEST(HciPacketsTest, testLeSetExtendedScanEnableDisable) {
220   PacketView<kLittleEndian> packet_bytes_view(
221       std::make_shared<std::vector<uint8_t>>(le_set_extended_scan_enable_disable));
222   auto view =
223       LeSetExtendedScanEnableView::Create(LeScanningCommandView::Create(CommandView::Create(packet_bytes_view)));
224 
225   ASSERT_TRUE(view.IsValid());
226   ASSERT_EQ(FilterDuplicates::ENABLED, view.GetFilterDuplicates());
227   ASSERT_EQ(Enable::DISABLED, view.GetEnable());
228   ASSERT_EQ(0, view.GetDuration());
229   ASSERT_EQ(0, view.GetPeriod());
230 }
231 
232 std::vector<uint8_t> le_extended_create_connection = {
233     0x43, 0x20, 0x2a, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x08,
234     0x30, 0x00, 0x18, 0x00, 0x28, 0x00, 0x00, 0x00, 0xf4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
235     0x08, 0x30, 0x00, 0x18, 0x00, 0x28, 0x00, 0x00, 0x00, 0xf4, 0x01, 0x00, 0x00, 0x00, 0x00};
236 
TEST(HciPacketsTest,testLeExtendedCreateConnection)237 TEST(HciPacketsTest, testLeExtendedCreateConnection) {
238   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
239       std::make_shared<std::vector<uint8_t>>(le_extended_create_connection);
240   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
241   auto view = LeExtendedCreateConnectionView::Create(
242       LeConnectionManagementCommandView::Create(AclCommandView::Create(CommandView::Create(packet_bytes_view))));
243   ASSERT_TRUE(view.IsValid());
244 }
245 
246 std::vector<uint8_t> le_set_extended_advertising_random_address = {
247     0x35, 0x20, 0x07, 0x00, 0x77, 0x58, 0xeb, 0xd3, 0x1c, 0x6e,
248 };
249 
TEST(HciPacketsTest,testLeSetExtendedAdvertisingRandomAddress)250 TEST(HciPacketsTest, testLeSetExtendedAdvertisingRandomAddress) {
251   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
252       std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_random_address);
253   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
254   auto view = LeSetExtendedAdvertisingRandomAddressView::Create(
255       LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
256   ASSERT_TRUE(view.IsValid());
257   uint8_t random_address_bytes[] = {0x77, 0x58, 0xeb, 0xd3, 0x1c, 0x6e};
258   ASSERT_EQ(0, view.GetAdvertisingHandle());
259   ASSERT_EQ(Address(random_address_bytes), view.GetAdvertisingRandomAddress());
260 }
261 
262 std::vector<uint8_t> le_set_extended_advertising_data{
263     0x37, 0x20, 0x12, 0x00, 0x03, 0x01, 0x0e, 0x02, 0x01, 0x02, 0x0a,
264     0x09, 0x50, 0x69, 0x78, 0x65, 0x6c, 0x20, 0x33, 0x20, 0x58,
265 };
TEST(HciPacketsTest,testLeSetExtendedAdvertisingData)266 TEST(HciPacketsTest, testLeSetExtendedAdvertisingData) {
267   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
268       std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_data);
269   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
270   auto view = LeSetExtendedAdvertisingDataRawView::Create(
271       LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
272   ASSERT_TRUE(view.IsValid());
273   ASSERT_EQ(0, view.GetAdvertisingHandle());
274   ASSERT_EQ(Operation::COMPLETE_ADVERTISEMENT, view.GetOperation());
275   ASSERT_EQ(FragmentPreference::CONTROLLER_SHOULD_NOT, view.GetFragmentPreference());
276   std::vector<uint8_t> advertising_data{
277       0x02, 0x01, 0x02, 0x0a, 0x09, 0x50, 0x69, 0x78, 0x65, 0x6c, 0x20, 0x33, 0x20, 0x58,
278   };
279   ASSERT_EQ(advertising_data, view.GetAdvertisingData());
280 }
281 
282 std::vector<uint8_t> le_set_extended_advertising_parameters_set_0{
283     0x36, 0x20, 0x19, 0x00, 0x13, 0x00, 0x90, 0x01, 0x00, 0xc2, 0x01, 0x00, 0x07, 0x01,
284     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf9, 0x01, 0x00, 0x01, 0x01, 0x00,
285 };
TEST(HciPacketsTest,testLeSetExtendedAdvertisingParametersLegacySet0)286 TEST(HciPacketsTest, testLeSetExtendedAdvertisingParametersLegacySet0) {
287   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
288       std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_parameters_set_0);
289   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
290   auto view = LeSetExtendedAdvertisingLegacyParametersView::Create(
291       LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
292   ASSERT_TRUE(view.IsValid());
293   ASSERT_EQ(0, view.GetAdvertisingHandle());
294   ASSERT_EQ(400, view.GetPrimaryAdvertisingIntervalMin());
295   ASSERT_EQ(450, view.GetPrimaryAdvertisingIntervalMax());
296   ASSERT_EQ(0x7, view.GetPrimaryAdvertisingChannelMap());
297   ASSERT_EQ(OwnAddressType::RANDOM_DEVICE_ADDRESS, view.GetOwnAddressType());
298   ASSERT_EQ(PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, view.GetPeerAddressType());
299   ASSERT_EQ(Address::kEmpty, view.GetPeerAddress());
300   ASSERT_EQ(AdvertisingFilterPolicy::ALL_DEVICES, view.GetAdvertisingFilterPolicy());
301   ASSERT_EQ(1, view.GetAdvertisingSid());
302   ASSERT_EQ(Enable::DISABLED, view.GetScanRequestNotificationEnable());
303 }
304 
305 std::vector<uint8_t> le_set_extended_advertising_parameters_set_1{
306     0x36, 0x20, 0x19, 0x01, 0x13, 0x00, 0x90, 0x01, 0x00, 0xc2, 0x01, 0x00, 0x07, 0x01,
307     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf9, 0x01, 0x00, 0x01, 0x01, 0x00,
308 };
TEST(HciPacketsTest,testLeSetExtendedAdvertisingParametersSet1)309 TEST(HciPacketsTest, testLeSetExtendedAdvertisingParametersSet1) {
310   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
311       std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_parameters_set_1);
312   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
313   auto view = LeSetExtendedAdvertisingLegacyParametersView::Create(
314       LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
315   ASSERT_TRUE(view.IsValid());
316   ASSERT_EQ(1, view.GetAdvertisingHandle());
317   ASSERT_EQ(400, view.GetPrimaryAdvertisingIntervalMin());
318   ASSERT_EQ(450, view.GetPrimaryAdvertisingIntervalMax());
319   ASSERT_EQ(0x7, view.GetPrimaryAdvertisingChannelMap());
320   ASSERT_EQ(OwnAddressType::RANDOM_DEVICE_ADDRESS, view.GetOwnAddressType());
321   ASSERT_EQ(PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, view.GetPeerAddressType());
322   ASSERT_EQ(Address::kEmpty, view.GetPeerAddress());
323   ASSERT_EQ(AdvertisingFilterPolicy::ALL_DEVICES, view.GetAdvertisingFilterPolicy());
324   ASSERT_EQ(1, view.GetAdvertisingSid());
325   ASSERT_EQ(Enable::DISABLED, view.GetScanRequestNotificationEnable());
326 }
327 
328 std::vector<uint8_t> le_set_extended_advertising_parameters_complete{0x0e, 0x05, 0x01, 0x36, 0x20, 0x00, 0xf5};
TEST(HciPacketsTest,testLeSetExtendedAdvertisingParametersComplete)329 TEST(HciPacketsTest, testLeSetExtendedAdvertisingParametersComplete) {
330   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
331       std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_parameters_complete);
332   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
333   auto view = LeSetExtendedAdvertisingParametersCompleteView::Create(
334       CommandCompleteView::Create(EventView::Create(packet_bytes_view)));
335   ASSERT_TRUE(view.IsValid());
336   ASSERT_EQ(static_cast<uint8_t>(-11), view.GetSelectedTxPower());
337 }
338 
339 std::vector<uint8_t> le_remove_advertising_set_1{
340     0x3c,
341     0x20,
342     0x01,
343     0x01,
344 };
TEST(HciPacketsTest,testLeRemoveAdvertisingSet1)345 TEST(HciPacketsTest, testLeRemoveAdvertisingSet1) {
346   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
347       std::make_shared<std::vector<uint8_t>>(le_remove_advertising_set_1);
348   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
349   auto view =
350       LeRemoveAdvertisingSetView::Create(LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
351   ASSERT_TRUE(view.IsValid());
352   ASSERT_EQ(1, view.GetAdvertisingHandle());
353 }
354 
355 std::vector<uint8_t> le_set_extended_advertising_disable_1{
356     0x39, 0x20, 0x06, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00,
357 };
TEST(HciPacketsTest,testLeSetExtendedAdvertisingDisable1)358 TEST(HciPacketsTest, testLeSetExtendedAdvertisingDisable1) {
359   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
360       std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_disable_1);
361   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
362   auto view = LeSetExtendedAdvertisingDisableView::Create(
363       LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
364   ASSERT_TRUE(view.IsValid());
365   auto disabled_set = view.GetDisabledSets();
366   ASSERT_EQ(1, disabled_set.size());
367   ASSERT_EQ(1, disabled_set[0].advertising_handle_);
368 }
369 
TEST(HciPacketsTest,testLeSetAdvertisingDataBuilderLength)370 TEST(HciPacketsTest, testLeSetAdvertisingDataBuilderLength) {
371   GapData gap_data;
372   gap_data.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
373   gap_data.data_ = std::vector<uint8_t>({'A', ' ', 'g', 'o', 'o', 'd', ' ', 'n', 'a', 'm', 'e'});
374   auto builder = LeSetAdvertisingDataBuilder::Create({gap_data});
375   ASSERT_EQ(2 /*opcode*/ + 1 /* parameter size */ + 1 /* data_length */ + 31 /* data */, builder->size());
376 
377   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
378   packet_bytes->reserve(builder->size());
379   BitInserter bit_inserter(*packet_bytes);
380   builder->Serialize(bit_inserter);
381   auto command_view = LeAdvertisingCommandView::Create(CommandView::Create(PacketView<kLittleEndian>(packet_bytes)));
382   ASSERT_TRUE(command_view.IsValid());
383   ASSERT_EQ(1 /* data_length */ + 31 /* data */, command_view.GetPayload().size());
384   auto view = LeSetAdvertisingDataView::Create(command_view);
385   ASSERT_TRUE(view.IsValid());
386 }
387 
TEST(HciPacketsTest,testLeSetScanResponseDataBuilderLength)388 TEST(HciPacketsTest, testLeSetScanResponseDataBuilderLength) {
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 = LeSetScanResponseDataBuilder::Create({gap_data});
393   ASSERT_EQ(2 /*opcode*/ + 1 /* parameter size */ + 1 /*data_length */ + 31 /* data */, builder->size());
394 
395   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
396   packet_bytes->reserve(builder->size());
397   BitInserter bit_inserter(*packet_bytes);
398   builder->Serialize(bit_inserter);
399   auto command_view = LeAdvertisingCommandView::Create(CommandView::Create(PacketView<kLittleEndian>(packet_bytes)));
400   ASSERT_TRUE(command_view.IsValid());
401   ASSERT_EQ(1 /* data_length */ + 31 /* data */, command_view.GetPayload().size());
402   auto view = LeSetScanResponseDataView::Create(command_view);
403   ASSERT_TRUE(view.IsValid());
404 }
405 
TEST(HciPacketsTest,testLeMultiAdvSetAdvertisingDataBuilderLength)406 TEST(HciPacketsTest, testLeMultiAdvSetAdvertisingDataBuilderLength) {
407   GapData gap_data;
408   gap_data.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
409   gap_data.data_ = std::vector<uint8_t>({'A', ' ', 'g', 'o', 'o', 'd', ' ', 'n', 'a', 'm', 'e'});
410   uint8_t set = 3;
411   auto builder = LeMultiAdvtSetDataBuilder::Create({gap_data}, set);
412 
413   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
414   packet_bytes->reserve(builder->size());
415   BitInserter bit_inserter(*packet_bytes);
416   builder->Serialize(bit_inserter);
417   auto command_view = LeMultiAdvtSetDataView::Create(LeMultiAdvtView::Create(
418       LeAdvertisingCommandView::Create(CommandView::Create(PacketView<kLittleEndian>(packet_bytes)))));
419   ASSERT_TRUE(command_view.IsValid());
420   auto view = LeMultiAdvtSetDataView::Create(command_view);
421   ASSERT_TRUE(view.IsValid());
422   ASSERT_TRUE(view.GetAdvertisingData().size() > 0);
423   ASSERT_EQ(view.GetAdvertisingData()[0].data_, gap_data.data_);
424   ASSERT_EQ(view.GetAdvertisingInstance(), 3);
425 }
426 
TEST(HciPacketsTest,testLeMultiAdvSetScanResponseDataBuilderLength)427 TEST(HciPacketsTest, testLeMultiAdvSetScanResponseDataBuilderLength) {
428   GapData gap_data;
429   gap_data.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
430   gap_data.data_ = std::vector<uint8_t>({'A', ' ', 'g', 'o', 'o', 'd', ' ', 'n', 'a', 'm', 'e'});
431   uint8_t set = 3;
432   auto builder = LeMultiAdvtSetScanRespBuilder::Create({gap_data}, set);
433 
434   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
435   packet_bytes->reserve(builder->size());
436   BitInserter bit_inserter(*packet_bytes);
437   builder->Serialize(bit_inserter);
438   auto command_view = LeMultiAdvtSetScanRespView::Create(LeMultiAdvtView::Create(
439       LeAdvertisingCommandView::Create(CommandView::Create(PacketView<kLittleEndian>(packet_bytes)))));
440   ASSERT_TRUE(command_view.IsValid());
441   auto view = LeMultiAdvtSetScanRespView::Create(command_view);
442   ASSERT_TRUE(view.IsValid());
443   ASSERT_EQ(view.GetAdvertisingData()[0].data_, gap_data.data_);
444   ASSERT_EQ(view.GetAdvertisingInstance(), 3);
445 }
446 
447 }  // namespace hci
448 }  // namespace bluetooth
449