• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   ASSERT_EQ(advertising_data, view.GetAdvertisingData());
279 }
280 
281 std::vector<uint8_t> le_set_extended_advertising_parameters_set_0{
282     0x36, 0x20, 0x19, 0x00, 0x13, 0x00, 0x90, 0x01, 0x00, 0xc2, 0x01, 0x00, 0x07, 0x01,
283     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf9, 0x01, 0x00, 0x01, 0x01, 0x00,
284 };
TEST(HciPacketsTest,testLeSetExtendedAdvertisingParametersLegacySet0)285 TEST(HciPacketsTest, testLeSetExtendedAdvertisingParametersLegacySet0) {
286   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
287       std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_parameters_set_0);
288   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
289   auto view = LeSetExtendedAdvertisingParametersLegacyView::Create(
290       LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
291   ASSERT_TRUE(view.IsValid());
292   ASSERT_EQ(0, view.GetAdvertisingHandle());
293   ASSERT_EQ(400ul, view.GetPrimaryAdvertisingIntervalMin());
294   ASSERT_EQ(450ul, view.GetPrimaryAdvertisingIntervalMax());
295   ASSERT_EQ(0x7, view.GetPrimaryAdvertisingChannelMap());
296   ASSERT_EQ(OwnAddressType::RANDOM_DEVICE_ADDRESS, view.GetOwnAddressType());
297   ASSERT_EQ(PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, view.GetPeerAddressType());
298   ASSERT_EQ(Address::kEmpty, view.GetPeerAddress());
299   ASSERT_EQ(AdvertisingFilterPolicy::ALL_DEVICES, view.GetAdvertisingFilterPolicy());
300   ASSERT_EQ(1, view.GetAdvertisingSid());
301   ASSERT_EQ(Enable::DISABLED, view.GetScanRequestNotificationEnable());
302 }
303 
304 std::vector<uint8_t> le_set_extended_advertising_parameters_set_1{
305     0x36, 0x20, 0x19, 0x01, 0x13, 0x00, 0x90, 0x01, 0x00, 0xc2, 0x01, 0x00, 0x07, 0x01,
306     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf9, 0x01, 0x00, 0x01, 0x01, 0x00,
307 };
TEST(HciPacketsTest,testLeSetExtendedAdvertisingParametersSet1)308 TEST(HciPacketsTest, testLeSetExtendedAdvertisingParametersSet1) {
309   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
310       std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_parameters_set_1);
311   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
312   auto view = LeSetExtendedAdvertisingParametersLegacyView::Create(
313       LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
314   ASSERT_TRUE(view.IsValid());
315   ASSERT_EQ(1, view.GetAdvertisingHandle());
316   ASSERT_EQ(400ul, view.GetPrimaryAdvertisingIntervalMin());
317   ASSERT_EQ(450ul, view.GetPrimaryAdvertisingIntervalMax());
318   ASSERT_EQ(0x7, view.GetPrimaryAdvertisingChannelMap());
319   ASSERT_EQ(OwnAddressType::RANDOM_DEVICE_ADDRESS, view.GetOwnAddressType());
320   ASSERT_EQ(PeerAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS, view.GetPeerAddressType());
321   ASSERT_EQ(Address::kEmpty, view.GetPeerAddress());
322   ASSERT_EQ(AdvertisingFilterPolicy::ALL_DEVICES, view.GetAdvertisingFilterPolicy());
323   ASSERT_EQ(1, view.GetAdvertisingSid());
324   ASSERT_EQ(Enable::DISABLED, view.GetScanRequestNotificationEnable());
325 }
326 
327 std::vector<uint8_t> le_set_extended_advertising_parameters_complete{0x0e, 0x05, 0x01, 0x36, 0x20, 0x00, 0xf5};
TEST(HciPacketsTest,testLeSetExtendedAdvertisingParametersComplete)328 TEST(HciPacketsTest, testLeSetExtendedAdvertisingParametersComplete) {
329   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
330       std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_parameters_complete);
331   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
332   auto view = LeSetExtendedAdvertisingParametersCompleteView::Create(
333       CommandCompleteView::Create(EventView::Create(packet_bytes_view)));
334   ASSERT_TRUE(view.IsValid());
335   ASSERT_EQ(static_cast<uint8_t>(-11), view.GetSelectedTxPower());
336 }
337 
338 std::vector<uint8_t> le_remove_advertising_set_1{
339     0x3c,
340     0x20,
341     0x01,
342     0x01,
343 };
TEST(HciPacketsTest,testLeRemoveAdvertisingSet1)344 TEST(HciPacketsTest, testLeRemoveAdvertisingSet1) {
345   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
346       std::make_shared<std::vector<uint8_t>>(le_remove_advertising_set_1);
347   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
348   auto view =
349       LeRemoveAdvertisingSetView::Create(LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
350   ASSERT_TRUE(view.IsValid());
351   ASSERT_EQ(1, view.GetAdvertisingHandle());
352 }
353 
354 std::vector<uint8_t> le_set_extended_advertising_disable_1{
355     0x39, 0x20, 0x06, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00,
356 };
TEST(HciPacketsTest,testLeSetExtendedAdvertisingDisable1)357 TEST(HciPacketsTest, testLeSetExtendedAdvertisingDisable1) {
358   std::shared_ptr<std::vector<uint8_t>> packet_bytes =
359       std::make_shared<std::vector<uint8_t>>(le_set_extended_advertising_disable_1);
360   PacketView<kLittleEndian> packet_bytes_view(packet_bytes);
361   auto view = LeSetExtendedAdvertisingDisableView::Create(
362       LeAdvertisingCommandView::Create(CommandView::Create(packet_bytes_view)));
363   ASSERT_TRUE(view.IsValid());
364   auto disabled_set = view.GetDisabledSets();
365   ASSERT_EQ(1ul, disabled_set.size());
366   ASSERT_EQ(1, disabled_set[0].advertising_handle_);
367 }
368 
TEST(HciPacketsTest,testLeSetAdvertisingDataBuilderLength)369 TEST(HciPacketsTest, testLeSetAdvertisingDataBuilderLength) {
370   GapData gap_data;
371   gap_data.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
372   gap_data.data_ = std::vector<uint8_t>({'A', ' ', 'g', 'o', 'o', 'd', ' ', 'n', 'a', 'm', 'e'});
373   auto builder = LeSetAdvertisingDataBuilder::Create({gap_data});
374   ASSERT_EQ(2ul /*opcode*/ + 1ul /* parameter size */ + 1ul /* data_length */ + 31ul /* data */, builder->size());
375 
376   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
377   packet_bytes->reserve(builder->size());
378   BitInserter bit_inserter(*packet_bytes);
379   builder->Serialize(bit_inserter);
380   auto command_view = LeAdvertisingCommandView::Create(CommandView::Create(PacketView<kLittleEndian>(packet_bytes)));
381   ASSERT_TRUE(command_view.IsValid());
382   ASSERT_EQ(1ul /* data_length */ + 31ul /* data */, command_view.GetPayload().size());
383   auto view = LeSetAdvertisingDataView::Create(command_view);
384   ASSERT_TRUE(view.IsValid());
385 }
386 
TEST(HciPacketsTest,testLeSetScanResponseDataBuilderLength)387 TEST(HciPacketsTest, testLeSetScanResponseDataBuilderLength) {
388   GapData gap_data;
389   gap_data.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
390   gap_data.data_ = std::vector<uint8_t>({'A', ' ', 'g', 'o', 'o', 'd', ' ', 'n', 'a', 'm', 'e'});
391   auto builder = LeSetScanResponseDataBuilder::Create({gap_data});
392   ASSERT_EQ(2ul /*opcode*/ + 1ul /* parameter size */ + 1ul /*data_length */ + 31ul /* data */, builder->size());
393 
394   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
395   packet_bytes->reserve(builder->size());
396   BitInserter bit_inserter(*packet_bytes);
397   builder->Serialize(bit_inserter);
398   auto command_view = LeAdvertisingCommandView::Create(CommandView::Create(PacketView<kLittleEndian>(packet_bytes)));
399   ASSERT_TRUE(command_view.IsValid());
400   ASSERT_EQ(1ul /* data_length */ + 31ul /* data */, command_view.GetPayload().size());
401   auto view = LeSetScanResponseDataView::Create(command_view);
402   ASSERT_TRUE(view.IsValid());
403 }
404 
TEST(HciPacketsTest,testLeMultiAdvSetAdvertisingDataBuilderLength)405 TEST(HciPacketsTest, testLeMultiAdvSetAdvertisingDataBuilderLength) {
406   GapData gap_data;
407   gap_data.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
408   gap_data.data_ = std::vector<uint8_t>({'A', ' ', 'g', 'o', 'o', 'd', ' ', 'n', 'a', 'm', 'e'});
409   uint8_t set = 3;
410   auto builder = LeMultiAdvtSetDataBuilder::Create({gap_data}, set);
411 
412   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
413   packet_bytes->reserve(builder->size());
414   BitInserter bit_inserter(*packet_bytes);
415   builder->Serialize(bit_inserter);
416   auto command_view = LeMultiAdvtSetDataView::Create(LeMultiAdvtView::Create(
417       LeAdvertisingCommandView::Create(CommandView::Create(PacketView<kLittleEndian>(packet_bytes)))));
418   ASSERT_TRUE(command_view.IsValid());
419   auto view = LeMultiAdvtSetDataView::Create(command_view);
420   ASSERT_TRUE(view.IsValid());
421   ASSERT_TRUE(view.GetAdvertisingData().size() > 0ul);
422   ASSERT_EQ(view.GetAdvertisingData()[0].data_, gap_data.data_);
423   ASSERT_EQ(view.GetAdvertisingInstance(), 3);
424 }
425 
TEST(HciPacketsTest,testLeMultiAdvSetScanResponseDataBuilderLength)426 TEST(HciPacketsTest, testLeMultiAdvSetScanResponseDataBuilderLength) {
427   GapData gap_data;
428   gap_data.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
429   gap_data.data_ = std::vector<uint8_t>({'A', ' ', 'g', 'o', 'o', 'd', ' ', 'n', 'a', 'm', 'e'});
430   uint8_t set = 3;
431   auto builder = LeMultiAdvtSetScanRespBuilder::Create({gap_data}, set);
432 
433   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
434   packet_bytes->reserve(builder->size());
435   BitInserter bit_inserter(*packet_bytes);
436   builder->Serialize(bit_inserter);
437   auto command_view = LeMultiAdvtSetScanRespView::Create(LeMultiAdvtView::Create(
438       LeAdvertisingCommandView::Create(CommandView::Create(PacketView<kLittleEndian>(packet_bytes)))));
439   ASSERT_TRUE(command_view.IsValid());
440   auto view = LeMultiAdvtSetScanRespView::Create(command_view);
441   ASSERT_TRUE(view.IsValid());
442   ASSERT_EQ(view.GetAdvertisingData()[0].data_, gap_data.data_);
443   ASSERT_EQ(view.GetAdvertisingInstance(), 3);
444 }
445 
TEST(HciPacketsTest,testMsftReadSupportedFeatures)446 TEST(HciPacketsTest, testMsftReadSupportedFeatures) {
447   // MSFT opcode is not defined in PDL.
448   auto msft_opcode = static_cast<OpCode>(0xfc01);
449 
450   auto builder = MsftReadSupportedFeaturesBuilder::Create(msft_opcode);
451 
452   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
453   packet_bytes->reserve(builder->size());
454   BitInserter bit_inserter(*packet_bytes);
455   builder->Serialize(bit_inserter);
456 
457   std::vector<uint8_t> expected_bytes{
458       0x01,  // Vendor command opcode and MSFT base code.
459       0xfc,
460       0x01,  // Packet length
461       0x00,  // Subcommand Opcode for Read Supported Features
462   };
463   ASSERT_EQ(expected_bytes, *packet_bytes);
464 }
465 
TEST(HciPacketsTest,testMsftLeMonitorAdvUuid)466 TEST(HciPacketsTest, testMsftLeMonitorAdvUuid) {
467   // MSFT opcode is not defined in PDL.
468   auto msft_opcode = static_cast<OpCode>(0xfc01);
469 
470   auto builder = MsftLeMonitorAdvConditionUuid2Builder::Create(
471       msft_opcode,
472       0x10 /* RSSI threshold high */,
473       0x11 /* RSSI threshold low */,
474       0x12 /* RSSI threshold low timeout */,
475       0x13 /* RSSI sampling period */,
476       std::array<uint8_t, 2>{0x71, 0x72} /* 16-bit UUID */);
477 
478   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
479   packet_bytes->reserve(builder->size());
480   BitInserter bit_inserter(*packet_bytes);
481   builder->Serialize(bit_inserter);
482 
483   std::vector<uint8_t> expected_bytes{
484       0x01,  // Vendor command opcode and MSFT base code.
485       0xfc,
486       0x09,  // Packet length
487       0x03,  // Subcommand Opcode for LE Monitor Adv
488       0x10,  // RSSI threshold high
489       0x11,  // RSSI threshold low
490       0x12,  // RSSI threshold low timeout
491       0x13,  // RSSI sampling period
492       0x02,  // Condition type = UUID
493       0x01,  // UUID type = 16-bit UUID
494       0x71,  // UUID content
495       0x72,
496   };
497   ASSERT_EQ(expected_bytes, *packet_bytes);
498 }
499 
TEST(HciPacketsTest,testMsftLeMonitorAdvPatternsEmpty)500 TEST(HciPacketsTest, testMsftLeMonitorAdvPatternsEmpty) {
501   // MSFT opcode is not defined in PDL.
502   auto msft_opcode = static_cast<OpCode>(0xfc01);
503 
504   std::vector<MsftLeMonitorAdvConditionPattern> patterns;
505 
506   auto builder = MsftLeMonitorAdvConditionPatternsBuilder::Create(
507       msft_opcode,
508       0x10 /* RSSI threshold high */,
509       0x11 /* RSSI threshold low */,
510       0x12 /* RSSI threshold low timeout */,
511       0x13 /* RSSI sampling period */,
512       patterns);
513 
514   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
515   packet_bytes->reserve(builder->size());
516   BitInserter bit_inserter(*packet_bytes);
517   builder->Serialize(bit_inserter);
518 
519   std::vector<uint8_t> expected_bytes{
520       0x01,  // Vendor command opcode and MSFT base code.
521       0xfc,
522       0x07,  // Packet length
523       0x03,  // Subcommand Opcode for LE Monitor Adv
524       0x10,  // RSSI threshold high
525       0x11,  // RSSI threshold low
526       0x12,  // RSSI threshold low timeout
527       0x13,  // RSSI sampling period
528       0x01,  // Condition type = Patterns
529       0x00,  // Number of patterns
530   };
531   ASSERT_EQ(expected_bytes, *packet_bytes);
532 }
533 
TEST(HciPacketsTest,testMsftLeMonitorAdvPatterns)534 TEST(HciPacketsTest, testMsftLeMonitorAdvPatterns) {
535   // MSFT opcode is not defined in PDL.
536   auto msft_opcode = static_cast<OpCode>(0xfc01);
537 
538   MsftLeMonitorAdvConditionPattern pattern1;
539   pattern1.ad_type_ = 0x03;
540   pattern1.start_of_pattern_ = 0x00;
541   pattern1.pattern_ = {1, 2, 3};
542 
543   MsftLeMonitorAdvConditionPattern pattern2;
544   pattern2.ad_type_ = 0x0f;
545   pattern2.start_of_pattern_ = 0x10;
546   pattern2.pattern_ = {0xa1, 0xa2};
547 
548   std::vector<MsftLeMonitorAdvConditionPattern> patterns{pattern1, pattern2};
549 
550   auto builder = MsftLeMonitorAdvConditionPatternsBuilder::Create(
551       msft_opcode,
552       0x10 /* RSSI threshold high */,
553       0x11 /* RSSI threshold low */,
554       0x12 /* RSSI threshold low timeout */,
555       0x13 /* RSSI sampling period */,
556       patterns);
557 
558   auto packet_bytes = std::make_shared<std::vector<uint8_t>>();
559   packet_bytes->reserve(builder->size());
560   BitInserter bit_inserter(*packet_bytes);
561   builder->Serialize(bit_inserter);
562 
563   std::vector<uint8_t> expected_bytes{
564       0x01,  // Vendor command opcode and MSFT base code.
565       0xfc,
566       0x12,  // Packet length
567       0x03,  // Subcommand Opcode for LE Monitor Adv
568       0x10,  // RSSI threshold high
569       0x11,  // RSSI threshold low
570       0x12,  // RSSI threshold low timeout
571       0x13,  // RSSI sampling period
572       0x01,  // Condition type = Patterns
573       0x02,  // Number of patterns
574       // Pattern 1
575       0x05,  // Length
576       0x03,  // AD Type
577       0x00,  // Start of pattern
578       0x01,  // Pattern
579       0x02,
580       0x03,
581       // Pattern 2
582       0x04,  // Length
583       0x0f,  // AD Type
584       0x10,  // Start of pattern
585       0xa1,  // Pattern
586       0xa2,
587   };
588   ASSERT_EQ(expected_bytes, *packet_bytes);
589 }
590 
591 std::vector<uint8_t> msft_read_supported_features_complete{
592     0x0e,  // command complete event code
593     0x10,  // event size
594     0x01,  // num_hci_command_packets
595     0x1e,
596     0xfc,  // vendor specific MSFT opcode assigned by Intel
597     0x00,  // status
598     0x00,  // MSFT subcommand opcode
599     0x7f,
600     0x00,
601     0x00,
602     0x00,
603     0x00,
604     0x00,
605     0x00,
606     0x00,  // supported features
607     0x02,  // MSFT event prefix length
608     0x87,
609     0x80,  // prefix: MSFT event prefix provided by Intel
610 };
TEST(HciPacketsTest,testMsftReadSupportedFeaturesComplete)611 TEST(HciPacketsTest, testMsftReadSupportedFeaturesComplete) {
612   PacketView<kLittleEndian> packet_bytes_view(
613       std::make_shared<std::vector<uint8_t>>(msft_read_supported_features_complete));
614   auto view = MsftReadSupportedFeaturesCommandCompleteView::Create(
615       MsftCommandCompleteView::Create(CommandCompleteView::Create(EventView::Create(packet_bytes_view))));
616 
617   ASSERT_TRUE(view.IsValid());
618   ASSERT_EQ(ErrorCode::SUCCESS, view.GetStatus());
619   ASSERT_EQ((uint8_t)0x00, (uint8_t)view.GetSubcommandOpcode());
620   ASSERT_EQ((uint64_t)0x000000000000007f, view.GetSupportedFeatures());
621   ASSERT_EQ(2ul, view.GetPrefix().size());
622 
623   uint16_t prefix = 0;
624   for (auto p : view.GetPrefix()) prefix = (prefix << 8) + p;
625   ASSERT_EQ((uint16_t)0x8780, prefix);
626 }
627 
628 }  // namespace hci
629 }  // namespace bluetooth
630