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 "hci/le_advertising_manager.h"
18
19 #include <bluetooth/log.h>
20 #include <com_android_bluetooth_flags.h>
21 #include <flag_macros.h>
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24
25 #include <algorithm>
26 #include <chrono>
27 #include <future>
28 #include <map>
29
30 #include "common/bind.h"
31 #include "hardware/ble_advertiser.h"
32 #include "hci/acl_manager.h"
33 #include "hci/address.h"
34 #include "hci/controller.h"
35 #include "hci/hci_layer_fake.h"
36 #include "os/thread.h"
37 #include "packet/raw_builder.h"
38
39 #define TEST_BT com::android::bluetooth::flags
40
41 namespace bluetooth {
42 namespace hci {
43 namespace {
44
45 using namespace std::literals;
46 using namespace std::literals::chrono_literals;
47
48 using packet::RawBuilder;
49
50 using testing::_;
51 using testing::InSequence;
52 using testing::SaveArg;
53
54 class TestController : public Controller {
55 public:
IsSupported(OpCode op_code) const56 bool IsSupported(OpCode op_code) const override {
57 return supported_opcodes_.count(op_code) == 1;
58 }
59
AddSupported(OpCode op_code)60 void AddSupported(OpCode op_code) {
61 supported_opcodes_.insert(op_code);
62 }
63
GetLeNumberOfSupportedAdverisingSets() const64 uint8_t GetLeNumberOfSupportedAdverisingSets() const override {
65 return num_advertisers_;
66 }
67
GetLeMaximumAdvertisingDataLength() const68 uint16_t GetLeMaximumAdvertisingDataLength() const override {
69 return 0x0672;
70 }
71
SupportsBlePeriodicAdvertising() const72 bool SupportsBlePeriodicAdvertising() const override {
73 return true;
74 }
75
SupportsBleExtendedAdvertising() const76 bool SupportsBleExtendedAdvertising() const override {
77 return support_ble_extended_advertising_;
78 }
79
SetBleExtendedAdvertisingSupport(bool support)80 void SetBleExtendedAdvertisingSupport(bool support) {
81 support_ble_extended_advertising_ = support;
82 }
83
GetVendorCapabilities() const84 VendorCapabilities GetVendorCapabilities() const override {
85 return vendor_capabilities_;
86 }
87
88 uint8_t num_advertisers_{0};
89 VendorCapabilities vendor_capabilities_;
90
91 protected:
Start()92 void Start() override {}
Stop()93 void Stop() override {}
ListDependencies(ModuleList *) const94 void ListDependencies(ModuleList* /* list */) const {}
95
96 private:
97 std::set<OpCode> supported_opcodes_{};
98 bool support_ble_extended_advertising_ = false;
99 };
100
101 class TestLeAddressManager : public LeAddressManager {
102 public:
TestLeAddressManager(common::Callback<void (std::unique_ptr<CommandBuilder>)> enqueue_command,os::Handler * handler,Address public_address,uint8_t accept_list_size,uint8_t resolving_list_size)103 TestLeAddressManager(
104 common::Callback<void(std::unique_ptr<CommandBuilder>)> enqueue_command,
105 os::Handler* handler,
106 Address public_address,
107 uint8_t accept_list_size,
108 uint8_t resolving_list_size)
109 : LeAddressManager(
110 enqueue_command, handler, public_address, accept_list_size, resolving_list_size) {
111 address_policy_ = AddressPolicy::USE_STATIC_ADDRESS;
112 minimum_rotation_time_ = 0ms;
113 maximum_rotation_time_ = 100ms;
114 }
115
Register(LeAddressManagerCallback * callback)116 AddressPolicy Register(LeAddressManagerCallback* callback) override {
117 client_ = callback;
118 test_client_state_ = RESUMED;
119 return AddressPolicy::USE_STATIC_ADDRESS;
120 }
121
Unregister(LeAddressManagerCallback *)122 void Unregister(LeAddressManagerCallback* /* callback */) override {
123 if (!ignore_unregister_for_testing) {
124 client_ = nullptr;
125 }
126 test_client_state_ = UNREGISTERED;
127 }
128
AckPause(LeAddressManagerCallback *)129 void AckPause(LeAddressManagerCallback* /* callback */) override {
130 test_client_state_ = PAUSED;
131 }
132
AckResume(LeAddressManagerCallback *)133 void AckResume(LeAddressManagerCallback* /* callback */) override {
134 test_client_state_ = RESUMED;
135 }
136
SetAddressPolicy(AddressPolicy address_policy)137 void SetAddressPolicy(AddressPolicy address_policy) {
138 address_policy_ = address_policy;
139 }
140
141 LeAddressManagerCallback* client_;
142 bool ignore_unregister_for_testing = false;
143 enum TestClientState {
144 UNREGISTERED,
145 PAUSED,
146 RESUMED,
147 };
148 TestClientState test_client_state_ = UNREGISTERED;
149 };
150
151 class TestAclManager : public AclManager {
152 public:
GetLeAddressManager()153 LeAddressManager* GetLeAddressManager() override {
154 return test_le_address_manager_;
155 }
156
SetAddressPolicy(LeAddressManager::AddressPolicy address_policy)157 void SetAddressPolicy(LeAddressManager::AddressPolicy address_policy) {
158 test_le_address_manager_->SetAddressPolicy(address_policy);
159 }
160
161 protected:
Start()162 void Start() override {
163 thread_ = new os::Thread("thread", os::Thread::Priority::NORMAL);
164 handler_ = new os::Handler(thread_);
165 Address address({0x01, 0x02, 0x03, 0x04, 0x05, 0x06});
166 test_le_address_manager_ = new TestLeAddressManager(
167 common::Bind(&TestAclManager::enqueue_command, common::Unretained(this)), handler_, address, 0x3F, 0x3F);
168 }
169
Stop()170 void Stop() override {
171 delete test_le_address_manager_;
172 handler_->Clear();
173 delete handler_;
174 delete thread_;
175 }
176
ListDependencies(ModuleList *) const177 void ListDependencies(ModuleList* /* list */) const {}
178
SetRandomAddress(Address)179 void SetRandomAddress(Address /* address */) {}
180
enqueue_command(std::unique_ptr<CommandBuilder>)181 void enqueue_command(std::unique_ptr<CommandBuilder> /* command_packet */){};
182
183 os::Thread* thread_;
184 os::Handler* handler_;
185 TestLeAddressManager* test_le_address_manager_;
186 };
187
188 class LeAdvertisingManagerTest : public ::testing::Test {
189 protected:
SetUp()190 void SetUp() override {
191 test_hci_layer_ = new HciLayerFake; // Ownership is transferred to registry
192 test_controller_ = new TestController;
193 test_acl_manager_ = new TestAclManager;
194 test_controller_->AddSupported(param_opcode_);
195 fake_registry_.InjectTestModule(&HciLayer::Factory, test_hci_layer_);
196 fake_registry_.InjectTestModule(&Controller::Factory, test_controller_);
197 fake_registry_.InjectTestModule(&AclManager::Factory, test_acl_manager_);
198 client_handler_ = fake_registry_.GetTestModuleHandler(&HciLayer::Factory);
199 ASSERT_NE(client_handler_, nullptr);
200 test_controller_->num_advertisers_ = num_instances_;
201 test_controller_->vendor_capabilities_.max_advt_instances_ = num_instances_;
202 test_controller_->SetBleExtendedAdvertisingSupport(support_ble_extended_advertising_);
203 le_advertising_manager_ = fake_registry_.Start<LeAdvertisingManager>(&thread_);
204 le_advertising_manager_->RegisterAdvertisingCallback(&mock_advertising_callback_);
205 }
206
TearDown()207 void TearDown() override {
208 sync_client_handler();
209 fake_registry_.SynchronizeModuleHandler(&LeAdvertisingManager::Factory, std::chrono::milliseconds(20));
210 fake_registry_.StopAll();
211 }
212
213 TestModuleRegistry fake_registry_;
214 HciLayerFake* test_hci_layer_ = nullptr;
215 TestController* test_controller_ = nullptr;
216 TestAclManager* test_acl_manager_ = nullptr;
217 os::Thread& thread_ = fake_registry_.GetTestThread();
218 LeAdvertisingManager* le_advertising_manager_ = nullptr;
219 os::Handler* client_handler_ = nullptr;
220 OpCode param_opcode_{OpCode::LE_SET_ADVERTISING_PARAMETERS};
221 uint8_t num_instances_ = 8;
222 bool support_ble_extended_advertising_ = false;
223
224 const common::Callback<void(Address, AddressType)> scan_callback =
225 common::Bind(&LeAdvertisingManagerTest::on_scan, common::Unretained(this));
226 const common::Callback<void(ErrorCode, uint8_t, uint8_t)> set_terminated_callback =
227 common::Bind(&LeAdvertisingManagerTest::on_set_terminated, common::Unretained(this));
228
on_scan(Address,AddressType)229 void on_scan(Address /* address */, AddressType /* address_type */) {}
230
on_set_terminated(ErrorCode,uint8_t,uint8_t)231 void on_set_terminated(ErrorCode /* error_code */, uint8_t, uint8_t) {}
232
sync_client_handler()233 void sync_client_handler() {
234 log::assert_that(
235 thread_.GetReactor()->WaitForIdle(2s),
236 "assert failed: thread_.GetReactor()->WaitForIdle(2s)");
237 }
238
239 class MockAdvertisingCallback : public AdvertisingCallback {
240 public:
241 MOCK_METHOD4(
242 OnAdvertisingSetStarted, void(int reg_id, uint8_t advertiser_id, int8_t tx_power, AdvertisingStatus status));
243 MOCK_METHOD3(OnAdvertisingEnabled, void(uint8_t advertiser_id, bool enable, uint8_t status));
244 MOCK_METHOD2(OnAdvertisingDataSet, void(uint8_t advertiser_id, uint8_t status));
245 MOCK_METHOD2(OnScanResponseDataSet, void(uint8_t advertiser_id, uint8_t status));
246 MOCK_METHOD3(OnAdvertisingParametersUpdated, void(uint8_t advertiser_id, int8_t tx_power, uint8_t status));
247 MOCK_METHOD2(OnPeriodicAdvertisingParametersUpdated, void(uint8_t advertiser_id, uint8_t status));
248 MOCK_METHOD2(OnPeriodicAdvertisingDataSet, void(uint8_t advertiser_id, uint8_t status));
249 MOCK_METHOD3(OnPeriodicAdvertisingEnabled, void(uint8_t advertiser_id, bool enable, uint8_t status));
250 MOCK_METHOD3(OnOwnAddressRead, void(uint8_t advertiser_id, uint8_t address_type, Address address));
251 } mock_advertising_callback_;
252 };
253
254 class LeAdvertisingAPITest : public LeAdvertisingManagerTest {
255 protected:
SetUp()256 void SetUp() override {
257 LeAdvertisingManagerTest::SetUp();
258
259 // start advertising set
260 AdvertisingConfig advertising_config{};
261 advertising_config.advertising_type = AdvertisingType::ADV_IND;
262 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
263 std::vector<GapData> gap_data{};
264 GapData data_item{};
265 data_item.data_type_ = GapDataType::FLAGS;
266 data_item.data_ = {0x34};
267 gap_data.push_back(data_item);
268 data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
269 data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
270 gap_data.push_back(data_item);
271 advertising_config.advertisement = gap_data;
272 advertising_config.scan_response = gap_data;
273 advertising_config.channel_map = 1;
274
275 EXPECT_CALL(
276 mock_advertising_callback_,
277 OnAdvertisingSetStarted(0x00, _, 0x00, AdvertisingCallback::AdvertisingStatus::SUCCESS))
278 .WillOnce(SaveArg<1>(&advertiser_id_));
279
280 le_advertising_manager_->ExtendedCreateAdvertiser(
281 kAdvertiserClientIdJni,
282 0x00,
283 advertising_config,
284 scan_callback,
285 set_terminated_callback,
286 0,
287 0,
288 client_handler_);
289
290 std::vector<OpCode> adv_opcodes = {
291 OpCode::LE_READ_ADVERTISING_PHYSICAL_CHANNEL_TX_POWER,
292 OpCode::LE_SET_ADVERTISING_PARAMETERS,
293 OpCode::LE_SET_SCAN_RESPONSE_DATA,
294 OpCode::LE_SET_ADVERTISING_DATA,
295 OpCode::LE_SET_ADVERTISING_ENABLE,
296 };
297
298 std::vector<uint8_t> success_vector{static_cast<uint8_t>(ErrorCode::SUCCESS)};
299 for (size_t i = 0; i < adv_opcodes.size(); i++) {
300 ASSERT_EQ(adv_opcodes[i], test_hci_layer_->GetCommand().GetOpCode());
301 if (adv_opcodes[i] == OpCode::LE_READ_ADVERTISING_PHYSICAL_CHANNEL_TX_POWER) {
302 test_hci_layer_->IncomingEvent(
303 LeReadAdvertisingPhysicalChannelTxPowerCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS, 0x00));
304 } else {
305 test_hci_layer_->IncomingEvent(
306 CommandCompleteBuilder::Create(uint8_t{1}, adv_opcodes[i], std::make_unique<RawBuilder>(success_vector)));
307 }
308 }
309
310 sync_client_handler();
311 ASSERT_NE(LeAdvertisingManager::kInvalidId, advertiser_id_);
312 }
313
314 AdvertiserId advertiser_id_;
315 };
316
317 class LeAndroidHciAdvertisingManagerTest : public LeAdvertisingManagerTest {
318 protected:
SetUp()319 void SetUp() override {
320 param_opcode_ = OpCode::LE_MULTI_ADVT;
321 LeAdvertisingManagerTest::SetUp();
322 test_acl_manager_->SetAddressPolicy(LeAddressManager::AddressPolicy::USE_RESOLVABLE_ADDRESS);
323 }
324 };
325
326 class LeAndroidHciAdvertisingAPITest : public LeAndroidHciAdvertisingManagerTest {
327 protected:
SetUp()328 void SetUp() override {
329 LeAndroidHciAdvertisingManagerTest::SetUp();
330
331 AdvertisingConfig advertising_config{};
332 advertising_config.advertising_type = AdvertisingType::ADV_IND;
333 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
334 std::vector<GapData> gap_data{};
335 GapData data_item{};
336 data_item.data_type_ = GapDataType::FLAGS;
337 data_item.data_ = {0x34};
338 gap_data.push_back(data_item);
339 data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
340 data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
341 gap_data.push_back(data_item);
342 advertising_config.advertisement = gap_data;
343 advertising_config.scan_response = gap_data;
344 advertising_config.channel_map = 1;
345
346 EXPECT_CALL(
347 mock_advertising_callback_,
348 OnAdvertisingSetStarted(0x00, _, 0x00, AdvertisingCallback::AdvertisingStatus::SUCCESS))
349 .WillOnce(SaveArg<1>(&advertiser_id_));
350
351 le_advertising_manager_->ExtendedCreateAdvertiser(
352 kAdvertiserClientIdJni,
353 0x00,
354 advertising_config,
355 scan_callback,
356 set_terminated_callback,
357 0,
358 0,
359 client_handler_);
360
361 std::vector<SubOcf> sub_ocf = {
362 SubOcf::SET_PARAM,
363 SubOcf::SET_SCAN_RESP,
364 SubOcf::SET_DATA,
365 SubOcf::SET_ENABLE,
366 };
367 for (size_t i = 0; i < sub_ocf.size(); i++) {
368 auto packet = test_hci_layer_->GetCommand();
369 auto sub_packet = LeMultiAdvtView::Create(LeAdvertisingCommandView::Create(packet));
370 ASSERT_TRUE(sub_packet.IsValid());
371 ASSERT_EQ(sub_packet.GetSubCmd(), sub_ocf[i]);
372 test_hci_layer_->IncomingEvent(LeMultiAdvtCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS, sub_ocf[i]));
373 }
374
375 sync_client_handler();
376 ASSERT_NE(LeAdvertisingManager::kInvalidId, advertiser_id_);
377 }
378
379 AdvertiserId advertiser_id_;
380 };
381
382 class LeAndroidHciAdvertisingAPIPublicAddressTest : public LeAndroidHciAdvertisingManagerTest {
383 protected:
SetUp()384 void SetUp() override {
385 LeAndroidHciAdvertisingManagerTest::SetUp();
386
387 AdvertisingConfig advertising_config{};
388 advertising_config.advertising_type = AdvertisingType::ADV_IND;
389 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
390 std::vector<GapData> gap_data{};
391 GapData data_item{};
392 data_item.data_type_ = GapDataType::FLAGS;
393 data_item.data_ = {0x34};
394 gap_data.push_back(data_item);
395 data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
396 data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
397 gap_data.push_back(data_item);
398 advertising_config.advertisement = gap_data;
399 advertising_config.scan_response = gap_data;
400 advertising_config.channel_map = 1;
401
402 test_acl_manager_->SetAddressPolicy(LeAddressManager::AddressPolicy::USE_PUBLIC_ADDRESS);
403
404 EXPECT_CALL(
405 mock_advertising_callback_,
406 OnAdvertisingSetStarted(0x00, _, 0x00, AdvertisingCallback::AdvertisingStatus::SUCCESS))
407 .WillOnce(SaveArg<1>(&advertiser_id_));
408
409 le_advertising_manager_->ExtendedCreateAdvertiser(
410 kAdvertiserClientIdJni,
411 0x00,
412 advertising_config,
413 scan_callback,
414 set_terminated_callback,
415 0,
416 0,
417 client_handler_);
418
419 std::vector<SubOcf> sub_ocf = {
420 SubOcf::SET_PARAM,
421 SubOcf::SET_SCAN_RESP,
422 SubOcf::SET_DATA,
423 SubOcf::SET_ENABLE,
424 };
425 for (size_t i = 0; i < sub_ocf.size(); i++) {
426 auto packet = test_hci_layer_->GetCommand();
427 auto sub_packet = LeMultiAdvtView::Create(LeAdvertisingCommandView::Create(packet));
428 ASSERT_TRUE(sub_packet.IsValid());
429 ASSERT_EQ(sub_packet.GetSubCmd(), sub_ocf[i]);
430 test_hci_layer_->IncomingEvent(LeMultiAdvtCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS, sub_ocf[i]));
431 }
432
433 sync_client_handler();
434 ASSERT_NE(LeAdvertisingManager::kInvalidId, advertiser_id_);
435 }
436
437 AdvertiserId advertiser_id_;
438 };
439
440 class LeExtendedAdvertisingManagerTest : public LeAdvertisingManagerTest {
441 protected:
SetUp()442 void SetUp() override {
443 support_ble_extended_advertising_ = true;
444 param_opcode_ = OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS;
445 LeAdvertisingManagerTest::SetUp();
446 }
447 };
448
449 class LeExtendedAdvertisingAPITest : public LeExtendedAdvertisingManagerTest {
450 protected:
SetUp()451 void SetUp() override {
452 LeExtendedAdvertisingManagerTest::SetUp();
453
454 // start advertising set
455 AdvertisingConfig advertising_config{};
456 advertising_config.advertising_type = AdvertisingType::ADV_IND;
457 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
458 std::vector<GapData> gap_data{};
459 GapData data_item{};
460 data_item.data_type_ = GapDataType::FLAGS;
461 data_item.data_ = {0x34};
462 gap_data.push_back(data_item);
463 data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
464 data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
465 gap_data.push_back(data_item);
466 advertising_config.advertisement = gap_data;
467 advertising_config.scan_response = gap_data;
468 advertising_config.channel_map = 1;
469 advertising_config.sid = 0x01;
470
471 EXPECT_CALL(
472 mock_advertising_callback_,
473 OnAdvertisingSetStarted(0x00, _, -23, AdvertisingCallback::AdvertisingStatus::SUCCESS))
474 .WillOnce(SaveArg<1>(&advertiser_id_));
475
476 le_advertising_manager_->ExtendedCreateAdvertiser(
477 kAdvertiserClientIdJni,
478 0x00,
479 advertising_config,
480 scan_callback,
481 set_terminated_callback,
482 0,
483 0,
484 client_handler_);
485
486 std::vector<OpCode> adv_opcodes = {
487 OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS,
488 OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA,
489 OpCode::LE_SET_EXTENDED_ADVERTISING_DATA,
490 OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE,
491 };
492 std::vector<uint8_t> success_vector{static_cast<uint8_t>(ErrorCode::SUCCESS)};
493 for (size_t i = 0; i < adv_opcodes.size(); i++) {
494 ASSERT_EQ(adv_opcodes[i], test_hci_layer_->GetCommand().GetOpCode());
495 if (adv_opcodes[i] == OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS) {
496 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingParametersCompleteBuilder::Create(
497 uint8_t{1}, ErrorCode::SUCCESS, static_cast<uint8_t>(-23)));
498 } else {
499 test_hci_layer_->IncomingEvent(
500 CommandCompleteBuilder::Create(uint8_t{1}, adv_opcodes[i], std::make_unique<RawBuilder>(success_vector)));
501 }
502 }
503
504 sync_client_handler();
505 ASSERT_NE(LeAdvertisingManager::kInvalidId, advertiser_id_);
506 }
507
508 AdvertiserId advertiser_id_;
509 };
510
TEST_F(LeAdvertisingManagerTest,startup_teardown)511 TEST_F(LeAdvertisingManagerTest, startup_teardown) {}
512
TEST_F(LeAndroidHciAdvertisingManagerTest,startup_teardown)513 TEST_F(LeAndroidHciAdvertisingManagerTest, startup_teardown) {}
514
TEST_F(LeExtendedAdvertisingManagerTest,startup_teardown)515 TEST_F(LeExtendedAdvertisingManagerTest, startup_teardown) {}
516
TEST_F(LeAdvertisingManagerTest,create_advertiser_test)517 TEST_F(LeAdvertisingManagerTest, create_advertiser_test) {
518 AdvertisingConfig advertising_config{};
519 advertising_config.advertising_type = AdvertisingType::ADV_IND;
520 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
521 std::vector<GapData> gap_data{};
522 GapData data_item{};
523 data_item.data_type_ = GapDataType::FLAGS;
524 data_item.data_ = {0x34};
525 gap_data.push_back(data_item);
526 data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
527 data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
528 gap_data.push_back(data_item);
529 advertising_config.advertisement = gap_data;
530 advertising_config.scan_response = gap_data;
531 advertising_config.channel_map = 1;
532
533 le_advertising_manager_->ExtendedCreateAdvertiser(
534 kAdvertiserClientIdJni,
535 0x00,
536 advertising_config,
537 scan_callback,
538 set_terminated_callback,
539 0,
540 0,
541 client_handler_);
542 std::vector<OpCode> adv_opcodes = {
543 OpCode::LE_READ_ADVERTISING_PHYSICAL_CHANNEL_TX_POWER,
544 OpCode::LE_SET_ADVERTISING_PARAMETERS,
545 OpCode::LE_SET_SCAN_RESPONSE_DATA,
546 OpCode::LE_SET_ADVERTISING_DATA,
547 OpCode::LE_SET_ADVERTISING_ENABLE,
548 };
549
550 AdvertiserId id;
551 EXPECT_CALL(
552 mock_advertising_callback_,
553 OnAdvertisingSetStarted(0x00, _, 0x00, AdvertisingCallback::AdvertisingStatus::SUCCESS))
554 .WillOnce(SaveArg<1>(&id));
555
556 std::vector<uint8_t> success_vector{static_cast<uint8_t>(ErrorCode::SUCCESS)};
557 for (size_t i = 0; i < adv_opcodes.size(); i++) {
558 ASSERT_EQ(adv_opcodes[i], test_hci_layer_->GetCommand().GetOpCode());
559 if (adv_opcodes[i] == OpCode::LE_READ_ADVERTISING_PHYSICAL_CHANNEL_TX_POWER) {
560 test_hci_layer_->IncomingEvent(
561 LeReadAdvertisingPhysicalChannelTxPowerCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS, 0x00));
562 } else {
563 test_hci_layer_->IncomingEvent(
564 CommandCompleteBuilder::Create(uint8_t{1}, adv_opcodes[i], std::make_unique<RawBuilder>(success_vector)));
565 }
566 }
567 sync_client_handler();
568
569 // Disable the advertiser
570 ASSERT_NE(LeAdvertisingManager::kInvalidId, id);
571 le_advertising_manager_->RemoveAdvertiser(id);
572 ASSERT_EQ(OpCode::LE_SET_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
573 }
574
TEST_F(LeAndroidHciAdvertisingManagerTest,create_advertiser_test)575 TEST_F(LeAndroidHciAdvertisingManagerTest, create_advertiser_test) {
576 AdvertisingConfig advertising_config{};
577 advertising_config.advertising_type = AdvertisingType::ADV_IND;
578 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
579 std::vector<GapData> gap_data{};
580 GapData data_item{};
581 data_item.data_type_ = GapDataType::FLAGS;
582 data_item.data_ = {0x34};
583 gap_data.push_back(data_item);
584 data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
585 data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
586 gap_data.push_back(data_item);
587 advertising_config.advertisement = gap_data;
588 advertising_config.scan_response = gap_data;
589 advertising_config.channel_map = 1;
590
591 AdvertiserId id;
592 EXPECT_CALL(
593 mock_advertising_callback_,
594 OnAdvertisingSetStarted(0, _, 0, AdvertisingCallback::AdvertisingStatus::SUCCESS))
595 .WillOnce(SaveArg<1>(&id));
596
597 le_advertising_manager_->ExtendedCreateAdvertiser(
598 kAdvertiserClientIdJni,
599 0x00,
600 advertising_config,
601 scan_callback,
602 set_terminated_callback,
603 0,
604 0,
605 client_handler_);
606
607 std::vector<SubOcf> sub_ocf = {
608 SubOcf::SET_PARAM,
609 SubOcf::SET_SCAN_RESP,
610 SubOcf::SET_DATA,
611 SubOcf::SET_ENABLE,
612 };
613 for (size_t i = 0; i < sub_ocf.size(); i++) {
614 auto packet = test_hci_layer_->GetCommand();
615 auto sub_packet = LeMultiAdvtView::Create(LeAdvertisingCommandView::Create(packet));
616 ASSERT_TRUE(sub_packet.IsValid());
617 ASSERT_EQ(sub_packet.GetSubCmd(), sub_ocf[i]);
618 test_hci_layer_->IncomingEvent(LeMultiAdvtCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS, sub_ocf[i]));
619 }
620 sync_client_handler();
621
622 // Disable the advertiser
623 ASSERT_NE(LeAdvertisingManager::kInvalidId, id);
624 le_advertising_manager_->RemoveAdvertiser(id);
625 ASSERT_EQ(OpCode::LE_MULTI_ADVT, test_hci_layer_->GetCommand().GetOpCode());
626 test_hci_layer_->IncomingEvent(LeMultiAdvtSetEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
627 }
628
TEST_F(LeAndroidHciAdvertisingManagerTest,create_advertiser_with_rpa_test)629 TEST_F(LeAndroidHciAdvertisingManagerTest, create_advertiser_with_rpa_test) {
630 AdvertisingConfig advertising_config{};
631 advertising_config.advertising_type = AdvertisingType::ADV_IND;
632 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::RESOLVABLE_RANDOM;
633 advertising_config.channel_map = 1;
634
635 AdvertiserId id;
636 EXPECT_CALL(
637 mock_advertising_callback_,
638 OnAdvertisingSetStarted(0, _, 0, AdvertisingCallback::AdvertisingStatus::SUCCESS))
639 .WillOnce(SaveArg<1>(&id));
640
641 le_advertising_manager_->ExtendedCreateAdvertiser(
642 kAdvertiserClientIdJni,
643 0x00,
644 advertising_config,
645 scan_callback,
646 set_terminated_callback,
647 0,
648 0,
649 client_handler_);
650 std::vector<SubOcf> sub_ocf = {
651 SubOcf::SET_PARAM,
652 SubOcf::SET_SCAN_RESP,
653 SubOcf::SET_DATA,
654 SubOcf::SET_RANDOM_ADDR,
655 SubOcf::SET_ENABLE,
656 };
657
658 for (size_t i = 0; i < sub_ocf.size(); i++) {
659 auto packet = test_hci_layer_->GetCommand();
660 auto sub_packet = LeMultiAdvtView::Create(LeAdvertisingCommandView::Create(packet));
661 ASSERT_TRUE(sub_packet.IsValid());
662 ASSERT_EQ(sub_packet.GetSubCmd(), sub_ocf[i]);
663 test_hci_layer_->IncomingEvent(
664 LeMultiAdvtCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS, sub_ocf[i]));
665 }
666
667 sync_client_handler();
668 ASSERT_NE(LeAdvertisingManager::kInvalidId, id);
669 }
670
TEST_F(LeExtendedAdvertisingManagerTest,create_advertiser_test)671 TEST_F(LeExtendedAdvertisingManagerTest, create_advertiser_test) {
672 AdvertisingConfig advertising_config{};
673 advertising_config.advertising_type = AdvertisingType::ADV_IND;
674 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
675 std::vector<GapData> gap_data{};
676 GapData data_item{};
677 data_item.data_type_ = GapDataType::FLAGS;
678 data_item.data_ = {0x34};
679 gap_data.push_back(data_item);
680 data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
681 data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
682 gap_data.push_back(data_item);
683 advertising_config.advertisement = gap_data;
684 advertising_config.scan_response = gap_data;
685 advertising_config.channel_map = 1;
686 advertising_config.sid = 0x01;
687
688 AdvertiserId id;
689 EXPECT_CALL(
690 mock_advertising_callback_,
691 OnAdvertisingSetStarted(0, _, -23, AdvertisingCallback::AdvertisingStatus::SUCCESS))
692 .WillOnce(SaveArg<1>(&id));
693
694 le_advertising_manager_->ExtendedCreateAdvertiser(
695 kAdvertiserClientIdJni,
696 0x00,
697 advertising_config,
698 scan_callback,
699 set_terminated_callback,
700 0,
701 0,
702 client_handler_);
703
704 std::vector<OpCode> adv_opcodes = {
705 OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS,
706 OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA,
707 OpCode::LE_SET_EXTENDED_ADVERTISING_DATA,
708 OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE,
709 };
710 std::vector<uint8_t> success_vector{static_cast<uint8_t>(ErrorCode::SUCCESS)};
711 for (size_t i = 0; i < adv_opcodes.size(); i++) {
712 ASSERT_EQ(adv_opcodes[i], test_hci_layer_->GetCommand().GetOpCode());
713 if (adv_opcodes[i] == OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS) {
714 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingParametersCompleteBuilder::Create(
715 uint8_t{1}, ErrorCode::SUCCESS, static_cast<uint8_t>(-23)));
716 } else {
717 test_hci_layer_->IncomingEvent(
718 CommandCompleteBuilder::Create(uint8_t{1}, adv_opcodes[i], std::make_unique<RawBuilder>(success_vector)));
719 }
720 }
721 sync_client_handler();
722
723 // Remove the advertiser
724 ASSERT_NE(LeAdvertisingManager::kInvalidId, id);
725 le_advertising_manager_->RemoveAdvertiser(id);
726 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
727 ASSERT_EQ(OpCode::LE_REMOVE_ADVERTISING_SET, test_hci_layer_->GetCommand().GetOpCode());
728 }
729
TEST_F(LeExtendedAdvertisingManagerTest,create_periodic_advertiser_test)730 TEST_F(LeExtendedAdvertisingManagerTest, create_periodic_advertiser_test) {
731 AdvertisingConfig advertising_config{};
732 advertising_config.advertising_type = AdvertisingType::ADV_IND;
733 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
734 std::vector<GapData> gap_data{};
735 GapData data_item{};
736 data_item.data_type_ = GapDataType::FLAGS;
737 data_item.data_ = {0x34};
738 gap_data.push_back(data_item);
739 data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
740 data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
741 gap_data.push_back(data_item);
742 advertising_config.advertisement = gap_data;
743 advertising_config.scan_response = gap_data;
744 advertising_config.channel_map = 1;
745 advertising_config.sid = 0x01;
746 advertising_config.periodic_advertising_parameters.enable = true;
747
748 AdvertiserId id;
749 EXPECT_CALL(
750 mock_advertising_callback_,
751 OnAdvertisingSetStarted(0, _, -23, AdvertisingCallback::AdvertisingStatus::SUCCESS))
752 .WillOnce(SaveArg<1>(&id));
753
754 le_advertising_manager_->ExtendedCreateAdvertiser(
755 kAdvertiserClientIdJni,
756 0x00,
757 advertising_config,
758 scan_callback,
759 set_terminated_callback,
760 0,
761 0,
762 client_handler_);
763
764 std::vector<OpCode> adv_opcodes = {
765 OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS,
766 OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA,
767 OpCode::LE_SET_EXTENDED_ADVERTISING_DATA,
768 OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE,
769 };
770 std::vector<uint8_t> success_vector{static_cast<uint8_t>(ErrorCode::SUCCESS)};
771 for (size_t i = 0; i < adv_opcodes.size(); i++) {
772 ASSERT_EQ(adv_opcodes[i], test_hci_layer_->GetCommand().GetOpCode());
773 if (adv_opcodes[i] == OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS) {
774 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingParametersCompleteBuilder::Create(
775 uint8_t{1}, ErrorCode::SUCCESS, static_cast<uint8_t>(-23)));
776 } else {
777 test_hci_layer_->IncomingEvent(CommandCompleteBuilder::Create(
778 uint8_t{1}, adv_opcodes[i], std::make_unique<RawBuilder>(success_vector)));
779 }
780 }
781 sync_client_handler();
782
783 // Remove the advertiser
784 ASSERT_NE(LeAdvertisingManager::kInvalidId, id);
785 le_advertising_manager_->RemoveAdvertiser(id);
786 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
787 ASSERT_EQ(OpCode::LE_SET_PERIODIC_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
788 ASSERT_EQ(OpCode::LE_REMOVE_ADVERTISING_SET, test_hci_layer_->GetCommand().GetOpCode());
789 }
790
TEST_F_WITH_FLAGS(LeExtendedAdvertisingManagerTest,create_advertiser_valid_max_251_ad_data_length_test,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,divide_long_single_gap_data)))791 TEST_F_WITH_FLAGS(
792 LeExtendedAdvertisingManagerTest,
793 create_advertiser_valid_max_251_ad_data_length_test,
794 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, divide_long_single_gap_data))) {
795 AdvertisingConfig advertising_config{};
796 advertising_config.advertising_type = AdvertisingType::ADV_IND;
797 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
798 std::vector<GapData> gap_data{};
799 // data length 251
800 GapData data_item{};
801 data_item.data_type_ = GapDataType::MANUFACTURER_SPECIFIC_DATA;
802 uint8_t service_data[249];
803 std::copy_n(service_data, 249, std::back_inserter(data_item.data_));
804 gap_data.push_back(data_item);
805 advertising_config.advertisement = gap_data;
806 advertising_config.scan_response = gap_data;
807 advertising_config.channel_map = 1;
808 advertising_config.sid = 0x01;
809
810 AdvertiserId id;
811 EXPECT_CALL(
812 mock_advertising_callback_,
813 OnAdvertisingSetStarted(0, _, -23, AdvertisingCallback::AdvertisingStatus::SUCCESS))
814 .WillOnce(SaveArg<1>(&id));
815
816 le_advertising_manager_->ExtendedCreateAdvertiser(
817 kAdvertiserClientIdJni,
818 0x00,
819 advertising_config,
820 scan_callback,
821 set_terminated_callback,
822 0,
823 0,
824 client_handler_);
825
826 std::vector<OpCode> adv_opcodes = {
827 OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS,
828 OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA,
829 OpCode::LE_SET_EXTENDED_ADVERTISING_DATA,
830 OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE,
831 };
832 std::vector<uint8_t> success_vector{static_cast<uint8_t>(ErrorCode::SUCCESS)};
833 for (size_t i = 0; i < adv_opcodes.size(); i++) {
834 ASSERT_EQ(adv_opcodes[i], test_hci_layer_->GetCommand().GetOpCode());
835 if (adv_opcodes[i] == OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS) {
836 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingParametersCompleteBuilder::Create(
837 uint8_t{1}, ErrorCode::SUCCESS, static_cast<uint8_t>(-23)));
838 } else {
839 test_hci_layer_->IncomingEvent(CommandCompleteBuilder::Create(
840 uint8_t{1}, adv_opcodes[i], std::make_unique<RawBuilder>(success_vector)));
841 }
842 }
843 sync_client_handler();
844
845 // Remove the advertiser
846 ASSERT_NE(LeAdvertisingManager::kInvalidId, id);
847 le_advertising_manager_->RemoveAdvertiser(id);
848 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
849 ASSERT_EQ(OpCode::LE_REMOVE_ADVERTISING_SET, test_hci_layer_->GetCommand().GetOpCode());
850 }
851
TEST_F_WITH_FLAGS(LeExtendedAdvertisingManagerTest,create_advertiser_valid_max_252_ad_data_length_fragments_test,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,divide_long_single_gap_data)))852 TEST_F_WITH_FLAGS(
853 LeExtendedAdvertisingManagerTest,
854 create_advertiser_valid_max_252_ad_data_length_fragments_test,
855 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, divide_long_single_gap_data))) {
856 AdvertisingConfig advertising_config{};
857 advertising_config.advertising_type = AdvertisingType::ADV_IND;
858 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
859 std::vector<GapData> gap_data{};
860 // set data 252 bytes, this should pass and be fragmented into 2 packets
861 GapData data_item{};
862 data_item.data_type_ = GapDataType::MANUFACTURER_SPECIFIC_DATA;
863 uint8_t service_data[250];
864 std::copy_n(service_data, 250, std::back_inserter(data_item.data_));
865 gap_data.push_back(data_item);
866 advertising_config.advertisement = gap_data;
867 advertising_config.scan_response = gap_data;
868 advertising_config.channel_map = 1;
869 advertising_config.sid = 0x01;
870
871 AdvertiserId id;
872 EXPECT_CALL(
873 mock_advertising_callback_,
874 OnAdvertisingSetStarted(0, _, -23, AdvertisingCallback::AdvertisingStatus::SUCCESS))
875 .WillOnce(SaveArg<1>(&id));
876
877 le_advertising_manager_->ExtendedCreateAdvertiser(
878 kAdvertiserClientIdJni,
879 0x00,
880 advertising_config,
881 scan_callback,
882 set_terminated_callback,
883 0,
884 0,
885 client_handler_);
886
887 std::vector<OpCode> adv_opcodes = {
888 OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS,
889 OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA, // 1st fragment
890 OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA, // 2nd fragment
891 OpCode::LE_SET_EXTENDED_ADVERTISING_DATA, // 1st fragment
892 OpCode::LE_SET_EXTENDED_ADVERTISING_DATA, // 2nd fragment
893 OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE,
894 };
895 std::vector<uint8_t> success_vector{static_cast<uint8_t>(ErrorCode::SUCCESS)};
896 for (size_t i = 0; i < adv_opcodes.size(); i++) {
897 ASSERT_EQ(adv_opcodes[i], test_hci_layer_->GetCommand().GetOpCode());
898 if (adv_opcodes[i] == OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS) {
899 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingParametersCompleteBuilder::Create(
900 uint8_t{1}, ErrorCode::SUCCESS, static_cast<uint8_t>(-23)));
901 } else if (adv_opcodes[i] == OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA) {
902 test_hci_layer_->IncomingEvent(
903 LeSetExtendedScanResponseDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
904 } else if (adv_opcodes[i] == OpCode::LE_SET_EXTENDED_ADVERTISING_DATA) {
905 test_hci_layer_->IncomingEvent(
906 LeSetExtendedAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
907 } else {
908 test_hci_layer_->IncomingEvent(CommandCompleteBuilder::Create(
909 uint8_t{1}, adv_opcodes[i], std::make_unique<RawBuilder>(success_vector)));
910 }
911 }
912 sync_client_handler();
913
914 // Remove the advertiser
915 ASSERT_NE(LeAdvertisingManager::kInvalidId, id);
916 le_advertising_manager_->RemoveAdvertiser(id);
917 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
918 ASSERT_EQ(OpCode::LE_REMOVE_ADVERTISING_SET, test_hci_layer_->GetCommand().GetOpCode());
919 }
920
TEST_F_WITH_FLAGS(LeExtendedAdvertisingManagerTest,create_advertiser_test_invalid_256_ad_data_length_test,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,divide_long_single_gap_data)))921 TEST_F_WITH_FLAGS(
922 LeExtendedAdvertisingManagerTest,
923 create_advertiser_test_invalid_256_ad_data_length_test,
924 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, divide_long_single_gap_data))) {
925 AdvertisingConfig advertising_config{};
926 advertising_config.advertising_type = AdvertisingType::ADV_IND;
927 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
928 std::vector<GapData> gap_data{};
929
930 // set data 256 bytes, this should fail with error
931 GapData data_item{};
932 data_item.data_type_ = GapDataType::MANUFACTURER_SPECIFIC_DATA;
933 uint8_t service_data[254];
934 std::copy_n(service_data, 254, std::back_inserter(data_item.data_));
935 gap_data.push_back(data_item);
936
937 advertising_config.advertisement = gap_data;
938 advertising_config.scan_response = gap_data;
939 advertising_config.channel_map = 1;
940 advertising_config.sid = 0x01;
941
942 AdvertiserId id;
943 EXPECT_CALL(
944 mock_advertising_callback_,
945 OnAdvertisingSetStarted(0, _, _, AdvertisingCallback::AdvertisingStatus::DATA_TOO_LARGE))
946 .WillOnce(SaveArg<1>(&id));
947
948 le_advertising_manager_->ExtendedCreateAdvertiser(
949 kAdvertiserClientIdJni,
950 0x00,
951 advertising_config,
952 scan_callback,
953 set_terminated_callback,
954 0,
955 0,
956 client_handler_);
957
958 sync_client_handler();
959 }
960
TEST_F(LeExtendedAdvertisingManagerTest,ignore_on_pause_on_resume_after_unregistered)961 TEST_F(LeExtendedAdvertisingManagerTest, ignore_on_pause_on_resume_after_unregistered) {
962 TestLeAddressManager* test_le_address_manager = (TestLeAddressManager*)test_acl_manager_->GetLeAddressManager();
963 test_le_address_manager->ignore_unregister_for_testing = true;
964
965 // Register LeAddressManager vai ExtendedCreateAdvertiser
966 AdvertisingConfig advertising_config{};
967 advertising_config.advertising_type = AdvertisingType::ADV_IND;
968 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
969 std::vector<GapData> gap_data{};
970 GapData data_item{};
971 data_item.data_type_ = GapDataType::FLAGS;
972 data_item.data_ = {0x34};
973 gap_data.push_back(data_item);
974 data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
975 data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
976 gap_data.push_back(data_item);
977 advertising_config.advertisement = gap_data;
978 advertising_config.scan_response = gap_data;
979 advertising_config.channel_map = 1;
980 advertising_config.sid = 0x01;
981
982 AdvertiserId id;
983 EXPECT_CALL(
984 mock_advertising_callback_,
985 OnAdvertisingSetStarted(0, _, -23, AdvertisingCallback::AdvertisingStatus::SUCCESS))
986 .WillOnce(SaveArg<1>(&id));
987
988 le_advertising_manager_->ExtendedCreateAdvertiser(
989 kAdvertiserClientIdJni,
990 0x00,
991 advertising_config,
992 scan_callback,
993 set_terminated_callback,
994 0,
995 0,
996 client_handler_);
997
998 std::vector<OpCode> adv_opcodes = {
999 OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS,
1000 OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA,
1001 OpCode::LE_SET_EXTENDED_ADVERTISING_DATA,
1002 OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE,
1003 };
1004 std::vector<uint8_t> success_vector{static_cast<uint8_t>(ErrorCode::SUCCESS)};
1005 for (size_t i = 0; i < adv_opcodes.size(); i++) {
1006 ASSERT_EQ(adv_opcodes[i], test_hci_layer_->GetCommand().GetOpCode());
1007 if (adv_opcodes[i] == OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS) {
1008 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingParametersCompleteBuilder::Create(
1009 uint8_t{1}, ErrorCode::SUCCESS, static_cast<uint8_t>(-23)));
1010 } else {
1011 test_hci_layer_->IncomingEvent(
1012 CommandCompleteBuilder::Create(uint8_t{1}, adv_opcodes[i], std::make_unique<RawBuilder>(success_vector)));
1013 }
1014 }
1015 sync_client_handler();
1016
1017 // Unregister LeAddressManager vai RemoveAdvertiser
1018 ASSERT_NE(LeAdvertisingManager::kInvalidId, id);
1019 le_advertising_manager_->RemoveAdvertiser(id);
1020 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
1021 ASSERT_EQ(OpCode::LE_REMOVE_ADVERTISING_SET, test_hci_layer_->GetCommand().GetOpCode());
1022 sync_client_handler();
1023
1024 // Unregistered client should ignore OnPause/OnResume
1025 ASSERT_NE(test_le_address_manager->client_, nullptr);
1026 ASSERT_EQ(test_le_address_manager->test_client_state_, TestLeAddressManager::TestClientState::UNREGISTERED);
1027 test_le_address_manager->client_->OnPause();
1028 ASSERT_EQ(test_le_address_manager->test_client_state_, TestLeAddressManager::TestClientState::UNREGISTERED);
1029 test_le_address_manager->client_->OnResume();
1030 ASSERT_EQ(test_le_address_manager->test_client_state_, TestLeAddressManager::TestClientState::UNREGISTERED);
1031 }
1032
TEST_F(LeAdvertisingAPITest,startup_teardown)1033 TEST_F(LeAdvertisingAPITest, startup_teardown) {}
1034
TEST_F(LeAndroidHciAdvertisingAPITest,startup_teardown)1035 TEST_F(LeAndroidHciAdvertisingAPITest, startup_teardown) {}
1036
TEST_F(LeAndroidHciAdvertisingAPIPublicAddressTest,startup_teardown)1037 TEST_F(LeAndroidHciAdvertisingAPIPublicAddressTest, startup_teardown) {}
1038
TEST_F(LeExtendedAdvertisingAPITest,startup_teardown)1039 TEST_F(LeExtendedAdvertisingAPITest, startup_teardown) {}
1040
TEST_F(LeAdvertisingAPITest,set_parameter)1041 TEST_F(LeAdvertisingAPITest, set_parameter) {
1042 AdvertisingConfig advertising_config{};
1043 advertising_config.advertising_type = AdvertisingType::ADV_IND;
1044 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
1045 std::vector<GapData> gap_data{};
1046 GapData data_item{};
1047 data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
1048 data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
1049 gap_data.push_back(data_item);
1050 advertising_config.advertisement = gap_data;
1051 advertising_config.channel_map = 1;
1052 le_advertising_manager_->SetParameters(advertiser_id_, advertising_config);
1053 ASSERT_EQ(OpCode::LE_SET_ADVERTISING_PARAMETERS, test_hci_layer_->GetCommand().GetOpCode());
1054 EXPECT_CALL(
1055 mock_advertising_callback_,
1056 OnAdvertisingParametersUpdated(advertiser_id_, 0x00, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1057 test_hci_layer_->IncomingEvent(LeSetAdvertisingParametersCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1058 }
1059
TEST_F(LeAndroidHciAdvertisingAPITest,set_parameter)1060 TEST_F(LeAndroidHciAdvertisingAPITest, set_parameter) {
1061 AdvertisingConfig advertising_config{};
1062 advertising_config.advertising_type = AdvertisingType::ADV_IND;
1063 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
1064 std::vector<GapData> gap_data{};
1065 GapData data_item{};
1066 data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
1067 data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
1068 gap_data.push_back(data_item);
1069 advertising_config.advertisement = gap_data;
1070 advertising_config.channel_map = 1;
1071 le_advertising_manager_->SetParameters(advertiser_id_, advertising_config);
1072 auto packet = test_hci_layer_->GetCommand();
1073 auto sub_packet = LeMultiAdvtView::Create(LeAdvertisingCommandView::Create(packet));
1074 ASSERT_TRUE(sub_packet.IsValid());
1075 ASSERT_EQ(sub_packet.GetSubCmd(), SubOcf::SET_PARAM);
1076 EXPECT_CALL(
1077 mock_advertising_callback_,
1078 OnAdvertisingParametersUpdated(advertiser_id_, 0x00, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1079 test_hci_layer_->IncomingEvent(LeMultiAdvtCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS, SubOcf::SET_PARAM));
1080 }
1081
TEST_F(LeExtendedAdvertisingAPITest,set_parameter)1082 TEST_F(LeExtendedAdvertisingAPITest, set_parameter) {
1083 AdvertisingConfig advertising_config{};
1084 advertising_config.advertising_type = AdvertisingType::ADV_IND;
1085 advertising_config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
1086 std::vector<GapData> gap_data{};
1087 GapData data_item{};
1088 data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
1089 data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
1090 gap_data.push_back(data_item);
1091 advertising_config.advertisement = gap_data;
1092 advertising_config.channel_map = 1;
1093 advertising_config.sid = 0x01;
1094 advertising_config.tx_power = 0x08;
1095 le_advertising_manager_->SetParameters(advertiser_id_, advertising_config);
1096 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS, test_hci_layer_->GetCommand().GetOpCode());
1097 EXPECT_CALL(
1098 mock_advertising_callback_,
1099 OnAdvertisingParametersUpdated(advertiser_id_, 0x08, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1100 test_hci_layer_->IncomingEvent(
1101 LeSetExtendedAdvertisingParametersCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS, 0x08));
1102 }
1103
TEST_F(LeAdvertisingAPITest,set_data_test)1104 TEST_F(LeAdvertisingAPITest, set_data_test) {
1105 // Set advertising data
1106 std::vector<GapData> advertising_data{};
1107 GapData data_item{};
1108 data_item.data_type_ = GapDataType::TX_POWER_LEVEL;
1109 data_item.data_ = {0x00};
1110 advertising_data.push_back(data_item);
1111 le_advertising_manager_->SetData(advertiser_id_, false, advertising_data);
1112 ASSERT_EQ(OpCode::LE_SET_ADVERTISING_DATA, test_hci_layer_->GetCommand().GetOpCode());
1113 EXPECT_CALL(
1114 mock_advertising_callback_,
1115 OnAdvertisingDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1116 test_hci_layer_->IncomingEvent(LeSetAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1117
1118 // Set scan response data
1119 std::vector<GapData> response_data{};
1120 GapData data_item2{};
1121 data_item2.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
1122 data_item2.data_ = {'t', 'e', 's', 't', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
1123 response_data.push_back(data_item2);
1124 le_advertising_manager_->SetData(advertiser_id_, true, response_data);
1125 ASSERT_EQ(OpCode::LE_SET_SCAN_RESPONSE_DATA, test_hci_layer_->GetCommand().GetOpCode());
1126 EXPECT_CALL(
1127 mock_advertising_callback_,
1128 OnScanResponseDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1129 test_hci_layer_->IncomingEvent(LeSetScanResponseDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1130 }
1131
TEST_F(LeExtendedAdvertisingAPITest,set_data_test)1132 TEST_F(LeExtendedAdvertisingAPITest, set_data_test) {
1133 // Set advertising data
1134 std::vector<GapData> advertising_data{};
1135 GapData data_item{};
1136 data_item.data_type_ = GapDataType::TX_POWER_LEVEL;
1137 data_item.data_ = {0x00};
1138 advertising_data.push_back(data_item);
1139 le_advertising_manager_->SetData(advertiser_id_, false, advertising_data);
1140 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_DATA, test_hci_layer_->GetCommand().GetOpCode());
1141 EXPECT_CALL(
1142 mock_advertising_callback_,
1143 OnAdvertisingDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1144 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1145
1146 // Set scan response data
1147 std::vector<GapData> response_data{};
1148 GapData data_item2{};
1149 data_item2.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
1150 data_item2.data_ = {'t', 'e', 's', 't', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
1151 response_data.push_back(data_item2);
1152 le_advertising_manager_->SetData(advertiser_id_, true, response_data);
1153 ASSERT_EQ(OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA, test_hci_layer_->GetCommand().GetOpCode());
1154 EXPECT_CALL(
1155 mock_advertising_callback_,
1156 OnScanResponseDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1157 test_hci_layer_->IncomingEvent(LeSetExtendedScanResponseDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1158 }
1159
TEST_F(LeAndroidHciAdvertisingAPITest,set_data_test)1160 TEST_F(LeAndroidHciAdvertisingAPITest, set_data_test) {
1161 // Set advertising data
1162 std::vector<GapData> advertising_data{};
1163 GapData data_item{};
1164 data_item.data_type_ = GapDataType::TX_POWER_LEVEL;
1165 data_item.data_ = {0x00};
1166 advertising_data.push_back(data_item);
1167 le_advertising_manager_->SetData(advertiser_id_, false, advertising_data);
1168 auto packet = test_hci_layer_->GetCommand();
1169 auto sub_packet = LeMultiAdvtView::Create(LeAdvertisingCommandView::Create(packet));
1170 ASSERT_TRUE(sub_packet.IsValid());
1171 ASSERT_EQ(sub_packet.GetSubCmd(), SubOcf::SET_DATA);
1172 EXPECT_CALL(
1173 mock_advertising_callback_,
1174 OnAdvertisingDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1175 test_hci_layer_->IncomingEvent(LeMultiAdvtCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS, SubOcf::SET_DATA));
1176
1177 // Set scan response data
1178 std::vector<GapData> response_data{};
1179 GapData data_item2{};
1180 data_item2.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
1181 data_item2.data_ = {'t', 'e', 's', 't', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
1182 response_data.push_back(data_item2);
1183 le_advertising_manager_->SetData(advertiser_id_, true, response_data);
1184 packet = test_hci_layer_->GetCommand();
1185 sub_packet = LeMultiAdvtView::Create(LeAdvertisingCommandView::Create(packet));
1186 ASSERT_TRUE(sub_packet.IsValid());
1187 ASSERT_EQ(sub_packet.GetSubCmd(), SubOcf::SET_SCAN_RESP);
1188 EXPECT_CALL(
1189 mock_advertising_callback_,
1190 OnScanResponseDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1191 test_hci_layer_->IncomingEvent(
1192 LeMultiAdvtCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS, SubOcf::SET_SCAN_RESP));
1193 }
1194
TEST_F(LeExtendedAdvertisingAPITest,set_data_fragments_test)1195 TEST_F(LeExtendedAdvertisingAPITest, set_data_fragments_test) {
1196 // Set advertising data
1197 std::vector<GapData> advertising_data{};
1198 for (uint8_t i = 0; i < 3; i++) {
1199 GapData data_item{};
1200 data_item.data_.push_back(0xda);
1201 data_item.data_type_ = GapDataType::SERVICE_DATA_128_BIT_UUIDS;
1202 uint8_t uuid[16] = {0xf0, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00, i};
1203 std::copy_n(uuid, 16, std::back_inserter(data_item.data_));
1204 uint8_t service_data[200];
1205 std::copy_n(service_data, 200, std::back_inserter(data_item.data_));
1206 advertising_data.push_back(data_item);
1207 }
1208 le_advertising_manager_->SetData(advertiser_id_, false, advertising_data);
1209
1210 // First fragment
1211 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_DATA, test_hci_layer_->GetCommand().GetOpCode());
1212 // Intermediate fragment
1213 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_DATA, test_hci_layer_->GetCommand().GetOpCode());
1214 // Last fragment
1215 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_DATA, test_hci_layer_->GetCommand().GetOpCode());
1216
1217 EXPECT_CALL(
1218 mock_advertising_callback_,
1219 OnAdvertisingDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1220 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1221 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1222 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1223 }
1224
TEST_F(LeExtendedAdvertisingAPITest,set_scan_response_fragments_test)1225 TEST_F(LeExtendedAdvertisingAPITest, set_scan_response_fragments_test) {
1226 // Set advertising data
1227 std::vector<GapData> advertising_data{};
1228 for (uint8_t i = 0; i < 3; i++) {
1229 GapData data_item{};
1230 data_item.data_.push_back(0xfa);
1231 data_item.data_type_ = GapDataType::SERVICE_DATA_128_BIT_UUIDS;
1232 uint8_t uuid[16] = {0xf0, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00, i};
1233 std::copy_n(uuid, 16, std::back_inserter(data_item.data_));
1234 uint8_t service_data[232];
1235 std::copy_n(service_data, 232, std::back_inserter(data_item.data_));
1236 advertising_data.push_back(data_item);
1237 }
1238 le_advertising_manager_->SetData(advertiser_id_, true, advertising_data);
1239
1240 // First fragment
1241 ASSERT_EQ(OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA, test_hci_layer_->GetCommand().GetOpCode());
1242 // Intermediate fragment
1243 ASSERT_EQ(OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA, test_hci_layer_->GetCommand().GetOpCode());
1244 // Last fragment
1245 ASSERT_EQ(OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA, test_hci_layer_->GetCommand().GetOpCode());
1246
1247 EXPECT_CALL(
1248 mock_advertising_callback_,
1249 OnScanResponseDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1250 test_hci_layer_->IncomingEvent(LeSetExtendedScanResponseDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1251 test_hci_layer_->IncomingEvent(LeSetExtendedScanResponseDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1252 test_hci_layer_->IncomingEvent(LeSetExtendedScanResponseDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1253 }
1254
TEST_F(LeExtendedAdvertisingAPITest,set_data_with_invalid_length)1255 TEST_F(LeExtendedAdvertisingAPITest, set_data_with_invalid_length) {
1256 // Set advertising data with data that greater than le_maximum_advertising_data_length_
1257 std::vector<GapData> advertising_data{};
1258 for (uint8_t i = 0; i < 10; i++) {
1259 GapData data_item{};
1260 data_item.data_.push_back(0xfb);
1261 data_item.data_type_ = GapDataType::SERVICE_DATA_128_BIT_UUIDS;
1262 uint8_t uuid[16] = {0xf0, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00, i};
1263 std::copy_n(uuid, 16, std::back_inserter(data_item.data_));
1264 uint8_t service_data[200];
1265 std::copy_n(service_data, 200, std::back_inserter(data_item.data_));
1266 advertising_data.push_back(data_item);
1267 }
1268
1269 EXPECT_CALL(
1270 mock_advertising_callback_,
1271 OnAdvertisingDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::DATA_TOO_LARGE));
1272 le_advertising_manager_->SetData(advertiser_id_, false, advertising_data);
1273
1274 EXPECT_CALL(
1275 mock_advertising_callback_,
1276 OnScanResponseDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::DATA_TOO_LARGE));
1277 le_advertising_manager_->SetData(advertiser_id_, true, advertising_data);
1278
1279 sync_client_handler();
1280 }
1281
TEST_F_WITH_FLAGS(LeExtendedAdvertisingAPITest,set_data_valid_max_251_ad_data_length_test,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,divide_long_single_gap_data)))1282 TEST_F_WITH_FLAGS(
1283 LeExtendedAdvertisingAPITest,
1284 set_data_valid_max_251_ad_data_length_test,
1285 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, divide_long_single_gap_data))) {
1286 // Set advertising data
1287 std::vector<GapData> advertising_data{};
1288 // set data 251 bytes
1289 GapData data_item{};
1290 data_item.data_type_ = GapDataType::MANUFACTURER_SPECIFIC_DATA;
1291 uint8_t service_data[249];
1292 std::copy_n(service_data, 249, std::back_inserter(data_item.data_));
1293 advertising_data.push_back(data_item);
1294
1295 le_advertising_manager_->SetData(advertiser_id_, false, advertising_data);
1296 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_DATA, test_hci_layer_->GetCommand().GetOpCode());
1297 EXPECT_CALL(
1298 mock_advertising_callback_,
1299 OnAdvertisingDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1300 test_hci_layer_->IncomingEvent(
1301 LeSetExtendedAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1302
1303 // Set scan response data
1304 std::vector<GapData> response_data{};
1305 // set data 251 bytes
1306 GapData data_item2{};
1307 data_item2.data_type_ = GapDataType::MANUFACTURER_SPECIFIC_DATA;
1308 std::copy_n(service_data, 249, std::back_inserter(data_item2.data_));
1309 response_data.push_back(data_item2);
1310
1311 le_advertising_manager_->SetData(advertiser_id_, true, response_data);
1312 ASSERT_EQ(OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA, test_hci_layer_->GetCommand().GetOpCode());
1313 EXPECT_CALL(
1314 mock_advertising_callback_,
1315 OnScanResponseDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1316 test_hci_layer_->IncomingEvent(
1317 LeSetExtendedScanResponseDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1318 }
1319
TEST_F_WITH_FLAGS(LeExtendedAdvertisingAPITest,set_data_valid_252_ad_data_length_fragments_test,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,divide_long_single_gap_data)))1320 TEST_F_WITH_FLAGS(
1321 LeExtendedAdvertisingAPITest,
1322 set_data_valid_252_ad_data_length_fragments_test,
1323 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, divide_long_single_gap_data))) {
1324 // Set advertising data
1325 std::vector<GapData> advertising_data{};
1326 // set data 252 bytes
1327 GapData data_item{};
1328 data_item.data_type_ = GapDataType::MANUFACTURER_SPECIFIC_DATA;
1329 uint8_t service_data[250];
1330 std::copy_n(service_data, 250, std::back_inserter(data_item.data_));
1331 advertising_data.push_back(data_item);
1332
1333 le_advertising_manager_->SetData(advertiser_id_, false, advertising_data);
1334
1335 // First fragment
1336 auto command = test_hci_layer_->GetCommand();
1337 ASSERT_EQ(command.GetOpCode(), OpCode::LE_SET_EXTENDED_ADVERTISING_DATA);
1338 ASSERT_EQ(
1339 1ul /* Advertising_Handle */ + 1ul /* Operation */ + 1ul /* Fragment_Preference */
1340 + 1ul /*Advertising_Data_Length*/ +
1341 251ul /* Max data length 251 includes AD length and type */,
1342 command.GetPayload().size());
1343 // Second fragment
1344 auto command2 = test_hci_layer_->GetCommand();
1345 ASSERT_EQ(command2.GetOpCode(), OpCode::LE_SET_EXTENDED_ADVERTISING_DATA);
1346 ASSERT_EQ(
1347 1ul /* Advertising_Handle */ + 1ul /* Operation */ + 1ul /* Fragment_Preference */
1348 + 1ul /*Advertising_Data_Length*/ + 1ul /* Remaining 1 byte data */,
1349 command2.GetPayload().size());
1350
1351 EXPECT_CALL(
1352 mock_advertising_callback_,
1353 OnAdvertisingDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1354 test_hci_layer_->IncomingEvent(
1355 LeSetExtendedAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1356 test_hci_layer_->IncomingEvent(
1357 LeSetExtendedAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1358
1359 // Set scan response data
1360 std::vector<GapData> response_data{};
1361 // set data 252 bytes, this should pass and be fragmented into 2 packets
1362 GapData data_item2{};
1363 data_item2.data_type_ = GapDataType::MANUFACTURER_SPECIFIC_DATA;
1364 std::copy_n(service_data, 250, std::back_inserter(data_item2.data_));
1365 response_data.push_back(data_item2);
1366
1367 le_advertising_manager_->SetData(advertiser_id_, true, response_data);
1368
1369 // First fragment
1370 command = test_hci_layer_->GetCommand();
1371 ASSERT_EQ(command.GetOpCode(), OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA);
1372 ASSERT_EQ(
1373 1ul /* Advertising_Handle */ + 1ul /* Operation */ + 1ul /* Fragment_Preference */
1374 + 1ul /*Advertising_Data_Length*/ +
1375 251ul /* Max data length 251 includes AD length and type */,
1376 command.GetPayload().size());
1377 // Second fragment
1378 command2 = test_hci_layer_->GetCommand();
1379 ASSERT_EQ(command2.GetOpCode(), OpCode::LE_SET_EXTENDED_SCAN_RESPONSE_DATA);
1380 ASSERT_EQ(
1381 1ul /* Advertising_Handle */ + 1ul /* Operation */ + 1ul /* Fragment_Preference */
1382 + 1ul /*Advertising_Data_Length*/ + 1ul /* Remaining 1 byte data */,
1383 command2.GetPayload().size());
1384
1385 EXPECT_CALL(
1386 mock_advertising_callback_,
1387 OnScanResponseDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1388 test_hci_layer_->IncomingEvent(
1389 LeSetExtendedScanResponseDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1390 test_hci_layer_->IncomingEvent(
1391 LeSetExtendedScanResponseDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1392 }
1393
TEST_F_WITH_FLAGS(LeExtendedAdvertisingAPITest,set_data_with_invalid_256_ad_data_length_test,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,divide_long_single_gap_data)))1394 TEST_F_WITH_FLAGS(
1395 LeExtendedAdvertisingAPITest,
1396 set_data_with_invalid_256_ad_data_length_test,
1397 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, divide_long_single_gap_data))) {
1398 // Set advertising data with data that greater than le_maximum_advertising_data_length_
1399 std::vector<GapData> advertising_data{};
1400
1401 // set data 256 bytes, this should fail with error
1402 GapData data_item{};
1403 data_item.data_type_ = GapDataType::MANUFACTURER_SPECIFIC_DATA;
1404 uint8_t service_data[254];
1405 std::copy_n(service_data, 254, std::back_inserter(data_item.data_));
1406 advertising_data.push_back(data_item);
1407
1408 EXPECT_CALL(
1409 mock_advertising_callback_,
1410 OnAdvertisingDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::INTERNAL_ERROR));
1411 le_advertising_manager_->SetData(advertiser_id_, false, advertising_data);
1412
1413 EXPECT_CALL(
1414 mock_advertising_callback_,
1415 OnScanResponseDataSet(
1416 advertiser_id_, AdvertisingCallback::AdvertisingStatus::INTERNAL_ERROR));
1417 le_advertising_manager_->SetData(advertiser_id_, true, advertising_data);
1418
1419 sync_client_handler();
1420 }
1421
TEST_F(LeAdvertisingAPITest,disable_enable_advertiser_test)1422 TEST_F(LeAdvertisingAPITest, disable_enable_advertiser_test) {
1423 // disable advertiser
1424 le_advertising_manager_->EnableAdvertiser(advertiser_id_, false, 0x00, 0x00);
1425 ASSERT_EQ(OpCode::LE_SET_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
1426 EXPECT_CALL(
1427 mock_advertising_callback_,
1428 OnAdvertisingEnabled(advertiser_id_, false, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1429 test_hci_layer_->IncomingEvent(LeSetAdvertisingEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1430 sync_client_handler();
1431
1432 // enable advertiser
1433 le_advertising_manager_->EnableAdvertiser(advertiser_id_, true, 0x00, 0x00);
1434 ASSERT_EQ(OpCode::LE_SET_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
1435 EXPECT_CALL(
1436 mock_advertising_callback_,
1437 OnAdvertisingEnabled(advertiser_id_, true, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1438 test_hci_layer_->IncomingEvent(LeSetAdvertisingEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1439 }
1440
TEST_F(LeAndroidHciAdvertisingAPITest,disable_enable_advertiser_test)1441 TEST_F(LeAndroidHciAdvertisingAPITest, disable_enable_advertiser_test) {
1442 // disable advertiser
1443 le_advertising_manager_->EnableAdvertiser(advertiser_id_, false, 0x00, 0x00);
1444 auto packet = test_hci_layer_->GetCommand();
1445 auto sub_packet = LeMultiAdvtView::Create(LeAdvertisingCommandView::Create(packet));
1446 ASSERT_TRUE(sub_packet.IsValid());
1447 ASSERT_EQ(sub_packet.GetSubCmd(), SubOcf::SET_ENABLE);
1448 EXPECT_CALL(
1449 mock_advertising_callback_,
1450 OnAdvertisingEnabled(advertiser_id_, false, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1451 test_hci_layer_->IncomingEvent(
1452 LeMultiAdvtCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS, SubOcf::SET_ENABLE));
1453 sync_client_handler();
1454
1455 // enable advertiser
1456 le_advertising_manager_->EnableAdvertiser(advertiser_id_, true, 0x00, 0x00);
1457 packet = test_hci_layer_->GetCommand();
1458 sub_packet = LeMultiAdvtView::Create(LeAdvertisingCommandView::Create(packet));
1459 ASSERT_TRUE(sub_packet.IsValid());
1460 ASSERT_EQ(sub_packet.GetSubCmd(), SubOcf::SET_ENABLE);
1461 EXPECT_CALL(
1462 mock_advertising_callback_,
1463 OnAdvertisingEnabled(advertiser_id_, true, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1464 test_hci_layer_->IncomingEvent(
1465 LeMultiAdvtCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS, SubOcf::SET_ENABLE));
1466 }
1467
TEST_F(LeExtendedAdvertisingAPITest,disable_enable_advertiser_test)1468 TEST_F(LeExtendedAdvertisingAPITest, disable_enable_advertiser_test) {
1469 // disable advertiser
1470 le_advertising_manager_->EnableAdvertiser(advertiser_id_, false, 0x00, 0x00);
1471 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
1472 EXPECT_CALL(
1473 mock_advertising_callback_,
1474 OnAdvertisingEnabled(advertiser_id_, false, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1475 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1476 sync_client_handler();
1477
1478 // enable advertiser
1479 le_advertising_manager_->EnableAdvertiser(advertiser_id_, true, 0x00, 0x00);
1480 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
1481 EXPECT_CALL(
1482 mock_advertising_callback_,
1483 OnAdvertisingEnabled(advertiser_id_, true, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1484 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1485 }
1486
TEST_F(LeExtendedAdvertisingAPITest,disable_after_enable)1487 TEST_F(LeExtendedAdvertisingAPITest, disable_after_enable) {
1488 // we expect Started -> Enable(false) -> Enable(true) -> Enable(false)
1489
1490 // setup already arranges everything and starts the advertiser
1491
1492 // expect
1493 InSequence s;
1494 EXPECT_CALL(mock_advertising_callback_, OnAdvertisingEnabled(_, false, _));
1495 EXPECT_CALL(mock_advertising_callback_, OnAdvertisingEnabled(_, true, _));
1496 EXPECT_CALL(mock_advertising_callback_, OnAdvertisingEnabled(_, false, _));
1497 EXPECT_CALL(mock_advertising_callback_, OnAdvertisingEnabled(_, true, _));
1498
1499 // act
1500
1501 // disable
1502 le_advertising_manager_->EnableAdvertiser(advertiser_id_, false, 0x00, 0x00);
1503 test_hci_layer_->GetCommand();
1504 test_hci_layer_->IncomingEvent(
1505 LeSetExtendedAdvertisingEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1506
1507 // enable
1508 le_advertising_manager_->EnableAdvertiser(advertiser_id_, true, 0x00, 0x00);
1509 test_hci_layer_->GetCommand();
1510 test_hci_layer_->IncomingEvent(
1511 LeSetExtendedAdvertisingEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1512
1513 // disable
1514 le_advertising_manager_->EnableAdvertiser(advertiser_id_, false, 0x00, 0x00);
1515 test_hci_layer_->GetCommand();
1516 test_hci_layer_->IncomingEvent(
1517 LeSetExtendedAdvertisingEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1518
1519 // enable
1520 le_advertising_manager_->EnableAdvertiser(advertiser_id_, true, 0x00, 0x00);
1521 test_hci_layer_->GetCommand();
1522 test_hci_layer_->IncomingEvent(
1523 LeSetExtendedAdvertisingEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1524
1525 sync_client_handler();
1526 }
1527
TEST_F(LeExtendedAdvertisingAPITest,set_periodic_parameter)1528 TEST_F(LeExtendedAdvertisingAPITest, set_periodic_parameter) {
1529 PeriodicAdvertisingParameters advertising_config{};
1530 advertising_config.max_interval = 0x1000;
1531 advertising_config.min_interval = 0x0006;
1532 le_advertising_manager_->SetPeriodicParameters(advertiser_id_, advertising_config);
1533 ASSERT_EQ(
1534 OpCode::LE_SET_PERIODIC_ADVERTISING_PARAMETERS, test_hci_layer_->GetCommand().GetOpCode());
1535 EXPECT_CALL(
1536 mock_advertising_callback_,
1537 OnPeriodicAdvertisingParametersUpdated(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1538 test_hci_layer_->IncomingEvent(
1539 LeSetPeriodicAdvertisingParametersCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1540 sync_client_handler();
1541 }
1542
TEST_F(LeExtendedAdvertisingAPITest,set_periodic_data_test)1543 TEST_F(LeExtendedAdvertisingAPITest, set_periodic_data_test) {
1544 // Set advertising data
1545 std::vector<GapData> advertising_data{};
1546 GapData data_item{};
1547 data_item.data_type_ = GapDataType::TX_POWER_LEVEL;
1548 data_item.data_ = {0x00};
1549 advertising_data.push_back(data_item);
1550 le_advertising_manager_->SetPeriodicData(advertiser_id_, advertising_data);
1551 ASSERT_EQ(OpCode::LE_SET_PERIODIC_ADVERTISING_DATA, test_hci_layer_->GetCommand().GetOpCode());
1552 EXPECT_CALL(
1553 mock_advertising_callback_,
1554 OnPeriodicAdvertisingDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1555 test_hci_layer_->IncomingEvent(LeSetPeriodicAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1556 sync_client_handler();
1557 }
1558
TEST_F(LeExtendedAdvertisingAPITest,set_periodic_data_fragments_test)1559 TEST_F(LeExtendedAdvertisingAPITest, set_periodic_data_fragments_test) {
1560 // Set advertising data
1561 std::vector<GapData> advertising_data{};
1562 for (uint8_t i = 0; i < 3; i++) {
1563 GapData data_item{};
1564 data_item.data_.push_back(0xfa);
1565 data_item.data_type_ = GapDataType::SERVICE_DATA_128_BIT_UUIDS;
1566 uint8_t uuid[16] = {0xf0, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00, i};
1567 std::copy_n(uuid, 16, std::back_inserter(data_item.data_));
1568 uint8_t service_data[232];
1569 std::copy_n(service_data, 232, std::back_inserter(data_item.data_));
1570 advertising_data.push_back(data_item);
1571 }
1572 le_advertising_manager_->SetPeriodicData(advertiser_id_, advertising_data);
1573
1574 // First fragment
1575 ASSERT_EQ(OpCode::LE_SET_PERIODIC_ADVERTISING_DATA, test_hci_layer_->GetCommand().GetOpCode());
1576 // Intermediate fragment
1577 ASSERT_EQ(OpCode::LE_SET_PERIODIC_ADVERTISING_DATA, test_hci_layer_->GetCommand().GetOpCode());
1578 // Last fragment
1579 ASSERT_EQ(OpCode::LE_SET_PERIODIC_ADVERTISING_DATA, test_hci_layer_->GetCommand().GetOpCode());
1580
1581 EXPECT_CALL(
1582 mock_advertising_callback_,
1583 OnPeriodicAdvertisingDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1584 test_hci_layer_->IncomingEvent(LeSetPeriodicAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1585 test_hci_layer_->IncomingEvent(LeSetPeriodicAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1586 test_hci_layer_->IncomingEvent(LeSetPeriodicAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1587 }
1588
TEST_F_WITH_FLAGS(LeExtendedAdvertisingAPITest,set_periodic_data_valid_max_252_ad_data_length_test,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,divide_long_single_gap_data)))1589 TEST_F_WITH_FLAGS(
1590 LeExtendedAdvertisingAPITest,
1591 set_periodic_data_valid_max_252_ad_data_length_test,
1592 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, divide_long_single_gap_data))) {
1593 // Set advertising data
1594 std::vector<GapData> advertising_data{};
1595
1596 // set data 252 bytes, this should pass and be sent in 1 packet
1597 GapData data_item{};
1598 data_item.data_type_ = GapDataType::MANUFACTURER_SPECIFIC_DATA;
1599 uint8_t service_data[250];
1600 std::copy_n(service_data, 250, std::back_inserter(data_item.data_));
1601 advertising_data.push_back(data_item);
1602
1603 le_advertising_manager_->SetPeriodicData(advertiser_id_, advertising_data);
1604
1605 ASSERT_EQ(OpCode::LE_SET_PERIODIC_ADVERTISING_DATA, test_hci_layer_->GetCommand().GetOpCode());
1606 EXPECT_CALL(
1607 mock_advertising_callback_,
1608 OnPeriodicAdvertisingDataSet(
1609 advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1610 test_hci_layer_->IncomingEvent(
1611 LeSetPeriodicAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1612 sync_client_handler();
1613 }
1614
TEST_F_WITH_FLAGS(LeExtendedAdvertisingAPITest,set_periodic_data_valid_253_ad_data_length_fragments_test,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,divide_long_single_gap_data)))1615 TEST_F_WITH_FLAGS(
1616 LeExtendedAdvertisingAPITest,
1617 set_periodic_data_valid_253_ad_data_length_fragments_test,
1618 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, divide_long_single_gap_data))) {
1619 // Set advertising data
1620 std::vector<GapData> advertising_data{};
1621
1622 // set data 253 bytes, this should pass and be fragmented into 2 packets
1623 GapData data_item{};
1624 data_item.data_type_ = GapDataType::MANUFACTURER_SPECIFIC_DATA;
1625 uint8_t service_data[251];
1626 std::copy_n(service_data, 251, std::back_inserter(data_item.data_));
1627 advertising_data.push_back(data_item);
1628
1629 le_advertising_manager_->SetPeriodicData(advertiser_id_, advertising_data);
1630
1631 // First fragment
1632 auto command = test_hci_layer_->GetCommand();
1633 ASSERT_EQ(command.GetOpCode(), OpCode::LE_SET_PERIODIC_ADVERTISING_DATA);
1634 ASSERT_EQ(
1635 1ul /* Advertising_Handle */ + 1ul /* Operation */ + 1ul /*Advertising_Data_Length*/
1636 + 252ul /* Max data length 252 includes AD length and type */,
1637 command.GetPayload().size());
1638 // Second fragment
1639 auto command2 = test_hci_layer_->GetCommand();
1640 ASSERT_EQ(command2.GetOpCode(), OpCode::LE_SET_PERIODIC_ADVERTISING_DATA);
1641 ASSERT_EQ(
1642 1ul /* Advertising_Handle */ + 1ul /* Operation */ + 1ul /*Advertising_Data_Length*/
1643 + 1ul /* Remaining 1 byte data */,
1644 command2.GetPayload().size());
1645
1646 EXPECT_CALL(
1647 mock_advertising_callback_,
1648 OnPeriodicAdvertisingDataSet(
1649 advertiser_id_, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1650 test_hci_layer_->IncomingEvent(
1651 LeSetPeriodicAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1652 test_hci_layer_->IncomingEvent(
1653 LeSetPeriodicAdvertisingDataCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1654 }
1655
TEST_F_WITH_FLAGS(LeExtendedAdvertisingAPITest,set_periodic_data_invalid_256_ad_data_length_test,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,divide_long_single_gap_data)))1656 TEST_F_WITH_FLAGS(
1657 LeExtendedAdvertisingAPITest,
1658 set_periodic_data_invalid_256_ad_data_length_test,
1659 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, divide_long_single_gap_data))) {
1660 // Set advertising data
1661 std::vector<GapData> advertising_data{};
1662
1663 // set data 256 bytes, this should fail with error
1664 GapData data_item{};
1665 data_item.data_type_ = GapDataType::MANUFACTURER_SPECIFIC_DATA;
1666 uint8_t service_data[254];
1667 std::copy_n(service_data, 254, std::back_inserter(data_item.data_));
1668 advertising_data.push_back(data_item);
1669
1670 EXPECT_CALL(
1671 mock_advertising_callback_,
1672 OnPeriodicAdvertisingDataSet(
1673 advertiser_id_, AdvertisingCallback::AdvertisingStatus::INTERNAL_ERROR));
1674 le_advertising_manager_->SetPeriodicData(advertiser_id_, advertising_data);
1675
1676 sync_client_handler();
1677 }
1678
TEST_F(LeExtendedAdvertisingAPITest,set_perodic_data_with_invalid_length)1679 TEST_F(LeExtendedAdvertisingAPITest, set_perodic_data_with_invalid_length) {
1680 // Set advertising data with data that greater than le_maximum_advertising_data_length_
1681 std::vector<GapData> advertising_data{};
1682 for (uint8_t i = 0; i < 10; i++) {
1683 GapData data_item{};
1684 data_item.data_.push_back(0xfb);
1685 data_item.data_type_ = GapDataType::SERVICE_DATA_128_BIT_UUIDS;
1686 uint8_t uuid[16] = {0xf0, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x10, 0x00, i};
1687 std::copy_n(uuid, 16, std::back_inserter(data_item.data_));
1688 uint8_t service_data[200];
1689 std::copy_n(service_data, 200, std::back_inserter(data_item.data_));
1690 advertising_data.push_back(data_item);
1691 }
1692
1693 EXPECT_CALL(
1694 mock_advertising_callback_,
1695 OnPeriodicAdvertisingDataSet(advertiser_id_, AdvertisingCallback::AdvertisingStatus::DATA_TOO_LARGE));
1696 le_advertising_manager_->SetPeriodicData(advertiser_id_, advertising_data);
1697
1698 sync_client_handler();
1699 }
1700
TEST_F(LeExtendedAdvertisingAPITest,disable_enable_periodic_advertiser_test)1701 TEST_F(LeExtendedAdvertisingAPITest, disable_enable_periodic_advertiser_test) {
1702 // disable advertiser
1703 le_advertising_manager_->EnablePeriodicAdvertising(advertiser_id_, false, false);
1704 ASSERT_EQ(OpCode::LE_SET_PERIODIC_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
1705 EXPECT_CALL(
1706 mock_advertising_callback_,
1707 OnPeriodicAdvertisingEnabled(advertiser_id_, false, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1708 test_hci_layer_->IncomingEvent(LeSetPeriodicAdvertisingEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1709 sync_client_handler();
1710
1711 // enable advertiser
1712 le_advertising_manager_->EnablePeriodicAdvertising(advertiser_id_, true, false);
1713 ASSERT_EQ(OpCode::LE_SET_PERIODIC_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
1714 EXPECT_CALL(
1715 mock_advertising_callback_,
1716 OnPeriodicAdvertisingEnabled(advertiser_id_, true, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1717 test_hci_layer_->IncomingEvent(LeSetPeriodicAdvertisingEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1718 sync_client_handler();
1719 }
1720
TEST_F(LeExtendedAdvertisingAPITest,trigger_advertiser_callbacks_if_started_while_paused)1721 TEST_F(LeExtendedAdvertisingAPITest, trigger_advertiser_callbacks_if_started_while_paused) {
1722 // arrange
1723 auto test_le_address_manager = (TestLeAddressManager*)test_acl_manager_->GetLeAddressManager();
1724 auto id_promise = std::promise<uint8_t>{};
1725 auto id_future = id_promise.get_future();
1726 le_advertising_manager_->RegisterAdvertiser(client_handler_->BindOnce(
1727 [](std::promise<uint8_t> promise, uint8_t id, uint8_t /* _status */) {
1728 promise.set_value(id);
1729 },
1730 std::move(id_promise)));
1731 sync_client_handler();
1732 auto set_id = id_future.get();
1733
1734 auto status_promise = std::promise<ErrorCode>{};
1735 auto status_future = status_promise.get_future();
1736
1737 test_le_address_manager->client_->OnPause();
1738
1739 test_hci_layer_->GetCommand();
1740 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingEnableCompleteBuilder::Create(1, ErrorCode::SUCCESS));
1741 sync_client_handler();
1742
1743 // act
1744 le_advertising_manager_->StartAdvertising(
1745 set_id,
1746 {},
1747 0,
1748 base::BindOnce(
1749 [](std::promise<ErrorCode> promise, uint8_t status) {
1750 promise.set_value((ErrorCode)status);
1751 },
1752 std::move(status_promise)),
1753 base::Bind([](uint8_t /* _status */) {}),
1754 base::Bind([](Address /* _address */, AddressType /* _address_type */) {}),
1755 base::Bind([](ErrorCode /* _status */, uint8_t /* _unused_1 */, uint8_t /* _unused_2 */) {}),
1756 client_handler_);
1757
1758 test_hci_layer_->GetCommand();
1759 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingParametersCompleteBuilder::Create(1, ErrorCode::SUCCESS, 0));
1760
1761 test_hci_layer_->GetCommand();
1762 test_hci_layer_->IncomingEvent(LeSetExtendedScanResponseDataCompleteBuilder::Create(1, ErrorCode::SUCCESS));
1763
1764 test_hci_layer_->GetCommand();
1765 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingDataCompleteBuilder::Create(1, ErrorCode::SUCCESS));
1766
1767 EXPECT_EQ(status_future.wait_for(std::chrono::milliseconds(100)), std::future_status::timeout);
1768
1769 test_le_address_manager->client_->OnResume();
1770
1771 test_hci_layer_->GetCommand();
1772 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingEnableCompleteBuilder::Create(1, ErrorCode::SUCCESS));
1773
1774 // assert
1775 EXPECT_EQ(status_future.get(), ErrorCode::SUCCESS);
1776
1777 sync_client_handler();
1778 }
1779
TEST_F(LeExtendedAdvertisingAPITest,duration_maxevents_restored_on_resume)1780 TEST_F(LeExtendedAdvertisingAPITest, duration_maxevents_restored_on_resume) {
1781 // arrange
1782 auto test_le_address_manager = (TestLeAddressManager*)test_acl_manager_->GetLeAddressManager();
1783 uint16_t duration = 1000;
1784 uint8_t max_extended_advertising_events = 100;
1785
1786 // enable advertiser
1787 le_advertising_manager_->EnableAdvertiser(
1788 advertiser_id_, true, duration, max_extended_advertising_events);
1789 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
1790 EXPECT_CALL(
1791 mock_advertising_callback_,
1792 OnAdvertisingEnabled(advertiser_id_, true, AdvertisingCallback::AdvertisingStatus::SUCCESS));
1793 test_hci_layer_->IncomingEvent(
1794 LeSetExtendedAdvertisingEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
1795
1796 test_le_address_manager->client_->OnPause();
1797 // verify advertising is disabled onPause
1798 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE, test_hci_layer_->GetCommand().GetOpCode());
1799 test_hci_layer_->IncomingEvent(
1800 LeSetExtendedAdvertisingEnableCompleteBuilder::Create(1, ErrorCode::SUCCESS));
1801 sync_client_handler();
1802
1803 test_le_address_manager->client_->OnResume();
1804 // verify advertising is reenabled onResume with correct parameters
1805 auto command = test_hci_layer_->GetCommand();
1806 ASSERT_EQ(OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE, command.GetOpCode());
1807 auto enable_command_view =
1808 LeSetExtendedAdvertisingEnableView::Create(LeAdvertisingCommandView::Create(command));
1809 ASSERT_TRUE(enable_command_view.IsValid());
1810 ASSERT_EQ(bluetooth::hci::Enable::ENABLED, enable_command_view.GetEnable());
1811 auto enabled_sets = enable_command_view.GetEnabledSets();
1812 ASSERT_EQ(static_cast<uint8_t>(1), enabled_sets.size());
1813 ASSERT_EQ(duration, enabled_sets[0].duration_);
1814 ASSERT_EQ(max_extended_advertising_events, enabled_sets[0].max_extended_advertising_events_);
1815 test_hci_layer_->IncomingEvent(
1816 LeSetExtendedAdvertisingEnableCompleteBuilder::Create(1, ErrorCode::SUCCESS));
1817
1818 sync_client_handler();
1819 }
1820
TEST_F(LeExtendedAdvertisingAPITest,no_callbacks_on_pause)1821 TEST_F(LeExtendedAdvertisingAPITest, no_callbacks_on_pause) {
1822 // arrange
1823 auto test_le_address_manager = (TestLeAddressManager*)test_acl_manager_->GetLeAddressManager();
1824
1825 // expect
1826 EXPECT_CALL(mock_advertising_callback_, OnAdvertisingEnabled(_, _, _)).Times(0);
1827
1828 // act
1829 log::info("pause");
1830 test_le_address_manager->client_->OnPause();
1831 test_hci_layer_->GetCommand();
1832 test_hci_layer_->IncomingEvent(
1833 LeSetExtendedAdvertisingEnableCompleteBuilder::Create(1, ErrorCode::SUCCESS));
1834
1835 sync_client_handler();
1836 }
1837
TEST_F(LeExtendedAdvertisingAPITest,no_callbacks_on_resume)1838 TEST_F(LeExtendedAdvertisingAPITest, no_callbacks_on_resume) {
1839 // arrange
1840 auto test_le_address_manager = (TestLeAddressManager*)test_acl_manager_->GetLeAddressManager();
1841 test_le_address_manager->client_->OnPause();
1842 test_hci_layer_->GetCommand();
1843 test_hci_layer_->IncomingEvent(
1844 LeSetExtendedAdvertisingEnableCompleteBuilder::Create(1, ErrorCode::SUCCESS));
1845 sync_client_handler();
1846
1847 // expect
1848 EXPECT_CALL(mock_advertising_callback_, OnAdvertisingEnabled(_, _, _)).Times(0);
1849
1850 // act
1851 test_le_address_manager->client_->OnResume();
1852 test_hci_layer_->GetCommand();
1853 test_hci_layer_->IncomingEvent(
1854 LeSetExtendedAdvertisingEnableCompleteBuilder::Create(1, ErrorCode::SUCCESS));
1855
1856 sync_client_handler();
1857 }
1858
TEST_F(LeExtendedAdvertisingManagerTest,use_rpa)1859 TEST_F(LeExtendedAdvertisingManagerTest, use_rpa) {
1860 // arrange: use RANDOM address policy
1861 test_acl_manager_->SetAddressPolicy(LeAddressManager::AddressPolicy::USE_RESOLVABLE_ADDRESS);
1862
1863 // act: start advertising set with RPA
1864 AdvertisingConfig config{};
1865 config.requested_advertiser_address_type = AdvertiserAddressType::RESOLVABLE_RANDOM;
1866 config.channel_map = 1;
1867
1868 le_advertising_manager_->ExtendedCreateAdvertiser(
1869 kAdvertiserClientIdJni,
1870 0x00,
1871 config,
1872 scan_callback,
1873 set_terminated_callback,
1874 0,
1875 0,
1876 client_handler_);
1877 auto command = LeAdvertisingCommandView::Create(test_hci_layer_->GetCommand());
1878
1879 // assert
1880 ASSERT_TRUE(command.IsValid());
1881 EXPECT_EQ(command.GetOpCode(), OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS);
1882
1883 auto set_parameters_command =
1884 LeSetExtendedAdvertisingParametersView::Create(LeAdvertisingCommandView::Create(command));
1885 ASSERT_TRUE(set_parameters_command.IsValid());
1886 EXPECT_EQ(set_parameters_command.GetOwnAddressType(), OwnAddressType::RANDOM_DEVICE_ADDRESS);
1887 }
1888
TEST_F(LeExtendedAdvertisingManagerTest,use_non_resolvable_address)1889 TEST_F(LeExtendedAdvertisingManagerTest, use_non_resolvable_address) {
1890 test_acl_manager_->SetAddressPolicy(LeAddressManager::AddressPolicy::USE_RESOLVABLE_ADDRESS);
1891
1892 // start advertising set with NRPA
1893 AdvertisingConfig config{};
1894 config.requested_advertiser_address_type = AdvertiserAddressType::NONRESOLVABLE_RANDOM;
1895 config.channel_map = 1;
1896
1897 le_advertising_manager_->ExtendedCreateAdvertiser(
1898 kAdvertiserClientIdJni,
1899 0x00,
1900 config,
1901 scan_callback,
1902 set_terminated_callback,
1903 0,
1904 0,
1905 client_handler_);
1906
1907 ASSERT_EQ(
1908 test_hci_layer_->GetCommand().GetOpCode(), OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS);
1909 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingParametersCompleteBuilder::Create(
1910 uint8_t{1}, ErrorCode::SUCCESS, static_cast<uint8_t>(-23)));
1911
1912 auto command = LeAdvertisingCommandView::Create(test_hci_layer_->GetCommand());
1913 ASSERT_TRUE(command.IsValid());
1914 ASSERT_EQ(command.GetOpCode(), OpCode::LE_SET_ADVERTISING_SET_RANDOM_ADDRESS);
1915
1916 auto set_address_command =
1917 LeSetAdvertisingSetRandomAddressView::Create(LeAdvertisingCommandView::Create(command));
1918 ASSERT_TRUE(set_address_command.IsValid());
1919 EXPECT_EQ(set_address_command.GetOpCode(), OpCode::LE_SET_ADVERTISING_SET_RANDOM_ADDRESS);
1920
1921 // checking that it is an NRPA (first two bits = 0b00)
1922 Address address = set_address_command.GetRandomAddress();
1923 EXPECT_EQ(address.data()[5] >> 6, 0b00);
1924 }
1925
TEST_F(LeExtendedAdvertisingManagerTest,use_public_address_type_if_public_address_policy)1926 TEST_F(LeExtendedAdvertisingManagerTest, use_public_address_type_if_public_address_policy) {
1927 // arrange: use PUBLIC address policy
1928 test_acl_manager_->SetAddressPolicy(LeAddressManager::AddressPolicy::USE_PUBLIC_ADDRESS);
1929
1930 // act: start advertising set with RPA
1931 AdvertisingConfig config{};
1932 config.requested_advertiser_address_type = AdvertiserAddressType::RESOLVABLE_RANDOM;
1933 config.channel_map = 1;
1934
1935 le_advertising_manager_->ExtendedCreateAdvertiser(
1936 kAdvertiserClientIdJni,
1937 0x00,
1938 config,
1939 scan_callback,
1940 set_terminated_callback,
1941 0,
1942 0,
1943 client_handler_);
1944 auto command = LeAdvertisingCommandView::Create(test_hci_layer_->GetCommand());
1945
1946 // assert
1947 ASSERT_TRUE(command.IsValid());
1948 EXPECT_EQ(command.GetOpCode(), OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS);
1949
1950 auto set_parameters_command =
1951 LeSetExtendedAdvertisingParametersView::Create(LeAdvertisingCommandView::Create(command));
1952 ASSERT_TRUE(set_parameters_command.IsValid());
1953 EXPECT_EQ(set_parameters_command.GetOwnAddressType(), OwnAddressType::PUBLIC_DEVICE_ADDRESS);
1954 }
1955
TEST_F_WITH_FLAGS(LeExtendedAdvertisingManagerTest,use_nrpa_if_public_address_policy_non_connectable,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,nrpa_non_connectable_adv)))1956 TEST_F_WITH_FLAGS(
1957 LeExtendedAdvertisingManagerTest,
1958 use_nrpa_if_public_address_policy_non_connectable,
1959 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, nrpa_non_connectable_adv))) {
1960 // arrange: use PUBLIC address policy
1961 test_acl_manager_->SetAddressPolicy(LeAddressManager::AddressPolicy::USE_PUBLIC_ADDRESS);
1962
1963 // act: start non-connectable advertising set with RPA
1964 AdvertisingConfig config{};
1965 config.requested_advertiser_address_type = AdvertiserAddressType::RESOLVABLE_RANDOM;
1966 config.channel_map = 1;
1967 config.connectable = false;
1968
1969 le_advertising_manager_->ExtendedCreateAdvertiser(
1970 kAdvertiserClientIdJni,
1971 0x00,
1972 config,
1973 scan_callback,
1974 set_terminated_callback,
1975 0,
1976 0,
1977 client_handler_);
1978 ASSERT_EQ(
1979 test_hci_layer_->GetCommand().GetOpCode(), OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS);
1980 test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingParametersCompleteBuilder::Create(
1981 uint8_t{1}, ErrorCode::SUCCESS, static_cast<uint8_t>(-23)));
1982
1983 auto command = LeAdvertisingCommandView::Create(test_hci_layer_->GetCommand());
1984 ASSERT_TRUE(command.IsValid());
1985 ASSERT_EQ(command.GetOpCode(), OpCode::LE_SET_ADVERTISING_SET_RANDOM_ADDRESS);
1986
1987 auto set_address_command =
1988 LeSetAdvertisingSetRandomAddressView::Create(LeAdvertisingCommandView::Create(command));
1989 ASSERT_TRUE(set_address_command.IsValid());
1990 EXPECT_EQ(set_address_command.GetOpCode(), OpCode::LE_SET_ADVERTISING_SET_RANDOM_ADDRESS);
1991
1992 // checking that it is an NRPA (first two bits = 0b00)
1993 Address address = set_address_command.GetRandomAddress();
1994 EXPECT_EQ(address.data()[5] >> 6, 0b00);
1995 }
1996
TEST_F_WITH_FLAGS(LeExtendedAdvertisingManagerTest,use_public_if_requested_with_public_address_policy_non_connectable,REQUIRES_FLAGS_ENABLED (ACONFIG_FLAG (TEST_BT,nrpa_non_connectable_adv)))1997 TEST_F_WITH_FLAGS(
1998 LeExtendedAdvertisingManagerTest,
1999 use_public_if_requested_with_public_address_policy_non_connectable,
2000 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(TEST_BT, nrpa_non_connectable_adv))) {
2001 // arrange: use PUBLIC address policy
2002 test_acl_manager_->SetAddressPolicy(LeAddressManager::AddressPolicy::USE_PUBLIC_ADDRESS);
2003
2004 // act: start non-connectable advertising set with PUBLIC
2005 AdvertisingConfig config{};
2006 config.requested_advertiser_address_type = AdvertiserAddressType::PUBLIC;
2007 config.channel_map = 1;
2008 config.connectable = false;
2009
2010 le_advertising_manager_->ExtendedCreateAdvertiser(
2011 kAdvertiserClientIdJni,
2012 0x00,
2013 config,
2014 scan_callback,
2015 set_terminated_callback,
2016 0,
2017 0,
2018 client_handler_);
2019 auto command = LeAdvertisingCommandView::Create(test_hci_layer_->GetCommand());
2020
2021 // assert
2022 ASSERT_TRUE(command.IsValid());
2023 EXPECT_EQ(command.GetOpCode(), OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS);
2024
2025 auto set_parameters_command =
2026 LeSetExtendedAdvertisingParametersView::Create(LeAdvertisingCommandView::Create(command));
2027 ASSERT_TRUE(set_parameters_command.IsValid());
2028 EXPECT_EQ(set_parameters_command.GetOwnAddressType(), OwnAddressType::PUBLIC_DEVICE_ADDRESS);
2029 }
2030
2031 } // namespace
2032 } // namespace hci
2033 } // namespace bluetooth
2034