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