1 /*
2 * Copyright (C) 2022 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_periodic_sync_manager.h"
18
19 #include <com_android_bluetooth_flags.h>
20 #include <flag_macros.h>
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <log/log.h>
24
25 #include "hci/le_scanning_callback.h"
26 #include "hci/le_scanning_interface.h"
27 #include "hci/le_scanning_manager_mock.h"
28 #include "os/handler.h"
29
30 #define TEST_BT com::android::bluetooth::flags
31
32 using namespace std::chrono_literals;
33
34 using testing::_;
35
36 namespace bluetooth {
37 namespace hci {
38 namespace {
39
GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet)40 PacketView<kLittleEndian> GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet) {
41 auto bytes = std::make_shared<std::vector<uint8_t>>();
42 BitInserter i(*bytes);
43 bytes->reserve(packet->size());
44 packet->Serialize(i);
45 return packet::PacketView<packet::kLittleEndian>(bytes);
46 }
47
48 class TestLeScanningInterface : public LeScanningInterface {
49 public:
EnqueueCommand(std::unique_ptr<LeScanningCommandBuilder> command,common::ContextualOnceCallback<void (CommandCompleteView)> on_complete)50 void EnqueueCommand(
51 std::unique_ptr<LeScanningCommandBuilder> command,
52 common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override {
53 std::lock_guard<std::mutex> lock(mutex_);
54 command_queue_.push(std::move(command));
55 command_complete_callbacks.push_back(std::move(on_complete));
56 if (command_promise_ != nullptr) {
57 std::promise<void>* prom = command_promise_.release();
58 prom->set_value();
59 delete prom;
60 }
61 }
62
EnqueueCommand(std::unique_ptr<LeScanningCommandBuilder> command,common::ContextualOnceCallback<void (CommandStatusView)> on_status)63 void EnqueueCommand(std::unique_ptr<LeScanningCommandBuilder> command,
64 common::ContextualOnceCallback<void(CommandStatusView)> on_status) override {
65 command_queue_.push(std::move(command));
66 command_status_callbacks.push_back(std::move(on_status));
67 if (command_promise_ != nullptr) {
68 std::promise<void>* prom = command_promise_.release();
69 prom->set_value();
70 delete prom;
71 }
72 }
73
EnqueueCommand(std::unique_ptr<LeScanningCommandBuilder>,common::ContextualOnceCallback<void (CommandStatusOrCompleteView)>)74 void EnqueueCommand(std::unique_ptr<LeScanningCommandBuilder> /* command */,
75 common::ContextualOnceCallback<void(
76 CommandStatusOrCompleteView)> /* on_status_or_complete */) override {
77 FAIL();
78 }
79
SetCommandFuture()80 void SetCommandFuture() {
81 ASSERT_EQ(command_promise_, nullptr) << "Promises, Promises, ... Only one at a time.";
82 command_promise_ = std::make_unique<std::promise<void>>();
83 command_future_ = std::make_unique<std::future<void>>(command_promise_->get_future());
84 }
85
GetLastCommand()86 CommandView GetLastCommand() {
87 if (command_queue_.empty()) {
88 return CommandView::Create(
89 PacketView<kLittleEndian>(std::make_shared<std::vector<uint8_t>>()));
90 }
91 auto last = std::move(command_queue_.front());
92 command_queue_.pop();
93 return CommandView::Create(GetPacketView(std::move(last)));
94 }
95
GetCommand(OpCode op_code)96 CommandView GetCommand(OpCode op_code) {
97 if (!command_queue_.empty()) {
98 std::lock_guard<std::mutex> lock(mutex_);
99 if (command_future_ != nullptr) {
100 command_future_.reset();
101 command_promise_.reset();
102 }
103 } else if (command_future_ != nullptr) {
104 auto result = command_future_->wait_for(std::chrono::milliseconds(1000));
105 EXPECT_NE(std::future_status::timeout, result);
106 }
107 std::lock_guard<std::mutex> lock(mutex_);
108 log::assert_that(!command_queue_.empty(), "Expecting command {} but command queue was empty",
109 OpCodeText(op_code));
110 CommandView command_packet_view = GetLastCommand();
111 EXPECT_TRUE(command_packet_view.IsValid());
112 EXPECT_EQ(command_packet_view.GetOpCode(), op_code);
113 return command_packet_view;
114 }
115
CommandCompleteCallback(std::unique_ptr<EventBuilder> event_builder)116 void CommandCompleteCallback(std::unique_ptr<EventBuilder> event_builder) {
117 auto event = EventView::Create(GetPacketView(std::move(event_builder)));
118 CommandCompleteView complete_view = CommandCompleteView::Create(event);
119 ASSERT_TRUE(complete_view.IsValid());
120 ASSERT_NE((uint16_t)command_complete_callbacks.size(), 0);
121 std::move(command_complete_callbacks.front())(complete_view);
122 command_complete_callbacks.pop_front();
123 }
124
CommandStatusCallback(std::unique_ptr<EventBuilder> event_builder)125 void CommandStatusCallback(std::unique_ptr<EventBuilder> event_builder) {
126 auto event = EventView::Create(GetPacketView(std::move(event_builder)));
127 CommandStatusView status_view = CommandStatusView::Create(event);
128 ASSERT_TRUE(status_view.IsValid());
129 ASSERT_NE((uint16_t)command_status_callbacks.size(), 0);
130 std::move(command_status_callbacks.front())(status_view);
131 command_status_callbacks.pop_front();
132 }
133
134 private:
135 std::list<common::ContextualOnceCallback<void(CommandCompleteView)>> command_complete_callbacks;
136 std::list<common::ContextualOnceCallback<void(CommandStatusView)>> command_status_callbacks;
137 std::queue<std::unique_ptr<CommandBuilder>> command_queue_;
138 std::unique_ptr<std::promise<void>> command_promise_;
139 std::unique_ptr<std::future<void>> command_future_;
140 mutable std::mutex mutex_;
141 };
142
143 class PeriodicSyncManagerTest : public ::testing::Test {
144 protected:
SetUp()145 void SetUp() override {
146 __android_log_set_minimum_priority(ANDROID_LOG_VERBOSE);
147 thread_ = new os::Thread("thread", os::Thread::Priority::NORMAL);
148 handler_ = new os::Handler(thread_);
149 test_le_scanning_interface_ = new TestLeScanningInterface();
150 periodic_sync_manager_ = new PeriodicSyncManager(&mock_callbacks_);
151 periodic_sync_manager_->Init(test_le_scanning_interface_, handler_);
152 }
153
TearDown()154 void TearDown() override {
155 delete periodic_sync_manager_;
156 periodic_sync_manager_ = nullptr;
157 delete test_le_scanning_interface_;
158 test_le_scanning_interface_ = nullptr;
159 handler_->Clear();
160 delete handler_;
161 handler_ = nullptr;
162 delete thread_;
163 thread_ = nullptr;
164 }
165
sync_handler()166 void sync_handler() {
167 log::assert_that(thread_ != nullptr, "assert failed: thread_ != nullptr");
168 log::assert_that(thread_->GetReactor()->WaitForIdle(2s),
169 "assert failed: thread_->GetReactor()->WaitForIdle(2s)");
170 }
171
172 class MockCallbacks : public bluetooth::hci::ScanningCallback {
173 public:
174 MOCK_METHOD(void, OnScannerRegistered,
175 (const bluetooth::hci::Uuid app_uuid, ScannerId scanner_id, ScanningStatus status),
176 (override));
177 MOCK_METHOD(void, OnSetScannerParameterComplete, (ScannerId scanner_id, ScanningStatus status),
178 (override));
179 MOCK_METHOD(void, OnScanResult,
180 (uint16_t event_type, uint8_t address_type, Address address, uint8_t primary_phy,
181 uint8_t secondary_phy, uint8_t advertising_sid, int8_t tx_power, int8_t rssi,
182 uint16_t periodic_advertising_interval, std::vector<uint8_t> advertising_data),
183 (override));
184 MOCK_METHOD(void, OnTrackAdvFoundLost,
185 (bluetooth::hci::AdvertisingFilterOnFoundOnLostInfo on_found_on_lost_info),
186 (override));
187 MOCK_METHOD(void, OnBatchScanReports,
188 (int client_if, int status, int report_format, int num_records,
189 std::vector<uint8_t> data),
190 (override));
191 MOCK_METHOD(void, OnBatchScanThresholdCrossed, (int client_if), (override));
192 MOCK_METHOD(void, OnTimeout, (), (override));
193 MOCK_METHOD(void, OnFilterEnable, (Enable enable, uint8_t status), (override));
194 MOCK_METHOD(void, OnFilterParamSetup,
195 (uint8_t available_spaces, ApcfAction action, uint8_t status), (override));
196 MOCK_METHOD(void, OnFilterConfigCallback,
197 (ApcfFilterType filter_type, uint8_t available_spaces, ApcfAction action,
198 uint8_t status),
199 (override));
200 MOCK_METHOD(void, OnPeriodicSyncStarted,
201 (int, uint8_t, uint16_t, uint8_t, AddressWithType, uint8_t, uint16_t));
202 MOCK_METHOD(void, OnPeriodicSyncReport,
203 (uint16_t, int8_t, int8_t, uint8_t, std::vector<uint8_t>));
204 MOCK_METHOD(void, OnPeriodicSyncLost, (uint16_t));
205 MOCK_METHOD(void, OnPeriodicSyncTransferred, (int, uint8_t, Address));
206 MOCK_METHOD(void, OnBigInfoReport, (uint16_t, bool));
207 } mock_callbacks_;
208
209 os::Thread* thread_;
210 os::Handler* handler_;
211 TestLeScanningInterface* test_le_scanning_interface_;
212 PeriodicSyncManager* periodic_sync_manager_ = nullptr;
213 };
214
TEST_F(PeriodicSyncManagerTest,startup_teardown)215 TEST_F(PeriodicSyncManagerTest, startup_teardown) {}
216
TEST_F(PeriodicSyncManagerTest,start_sync_test)217 TEST_F(PeriodicSyncManagerTest, start_sync_test) {
218 Address address;
219 Address::FromString("00:11:22:33:44:55", address);
220 int request_id = 0x01;
221 uint8_t advertiser_sid = 0x02;
222 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
223 uint16_t sync_handle = 0x03;
224 PeriodicSyncStates request{
225 .request_id = request_id,
226 .advertiser_sid = advertiser_sid,
227 .address_with_type = address_with_type,
228 .sync_handle = sync_handle,
229 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
230 };
231 uint16_t skip = 0x04;
232 uint16_t sync_timeout = 0x0A;
233 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
234 periodic_sync_manager_->StartSync(request, skip, sync_timeout);
235 auto packet =
236 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
237 auto packet_view =
238 LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
239 ASSERT_TRUE(packet_view.IsValid());
240 ASSERT_EQ(advertiser_sid, packet_view.GetAdvertisingSid());
241 ASSERT_EQ(AdvertisingAddressType::PUBLIC_DEVICE_OR_IDENTITY_ADDRESS,
242 packet_view.GetAdvertiserAddressType());
243 ASSERT_EQ(address, packet_view.GetAdvertiserAddress());
244 ASSERT_EQ(skip, packet_view.GetSkip());
245 ASSERT_EQ(sync_timeout, packet_view.GetSyncTimeout());
246 sync_handler();
247 }
248
TEST_F(PeriodicSyncManagerTest,handle_advertising_sync_established_test)249 TEST_F(PeriodicSyncManagerTest, handle_advertising_sync_established_test) {
250 uint16_t sync_handle = 0x12;
251 uint8_t advertiser_sid = 0x02;
252 // start scan
253 Address address;
254 Address::FromString("00:11:22:33:44:55", address);
255 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
256 PeriodicSyncStates request{
257 .request_id = 0x01,
258 .advertiser_sid = advertiser_sid,
259 .address_with_type = address_with_type,
260 .sync_handle = sync_handle,
261 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
262 };
263 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
264 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
265 auto packet =
266 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
267 auto temp_view =
268 LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
269 ASSERT_TRUE(temp_view.IsValid());
270
271 // Get command status
272 test_le_scanning_interface_->CommandStatusCallback(
273 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
274
275 EXPECT_CALL(mock_callbacks_, OnPeriodicSyncStarted);
276
277 // Get LePeriodicAdvertisingSyncEstablished
278 auto builder = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
279 ErrorCode::SUCCESS, sync_handle, advertiser_sid, address_with_type.GetAddressType(),
280 address_with_type.GetAddress(), SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
281 auto event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
282 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
283 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
284 sync_handler();
285 }
286
TEST_F(PeriodicSyncManagerTest,handle_advertising_sync_established_with_public_identity_address_test)287 TEST_F(PeriodicSyncManagerTest,
288 handle_advertising_sync_established_with_public_identity_address_test) {
289 uint16_t sync_handle = 0x12;
290 uint8_t advertiser_sid = 0x02;
291 // start scan
292 Address address;
293 Address::FromString("00:11:22:33:44:55", address);
294 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
295 PeriodicSyncStates request{
296 .request_id = 0x01,
297 .advertiser_sid = advertiser_sid,
298 .address_with_type = address_with_type,
299 .sync_handle = sync_handle,
300 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
301 };
302 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
303 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
304 auto packet =
305 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
306 auto temp_view =
307 LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
308 ASSERT_TRUE(temp_view.IsValid());
309
310 // Get command status
311 test_le_scanning_interface_->CommandStatusCallback(
312 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
313
314 EXPECT_CALL(mock_callbacks_, OnPeriodicSyncStarted);
315
316 // Get LePeriodicAdvertisingSyncEstablished with AddressType::PUBLIC_IDENTITY_ADDRESS
317 auto builder = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
318 ErrorCode::SUCCESS, sync_handle, advertiser_sid, AddressType::PUBLIC_IDENTITY_ADDRESS,
319 address_with_type.GetAddress(), SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
320 auto event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
321 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
322 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
323 sync_handler();
324 }
325
TEST_F(PeriodicSyncManagerTest,stop_sync_test)326 TEST_F(PeriodicSyncManagerTest, stop_sync_test) {
327 uint16_t sync_handle = 0x12;
328 uint8_t advertiser_sid = 0x02;
329 // start scan
330 Address address;
331 Address::FromString("00:11:22:33:44:55", address);
332 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
333 PeriodicSyncStates request{
334 .request_id = 0x01,
335 .advertiser_sid = advertiser_sid,
336 .address_with_type = address_with_type,
337 .sync_handle = sync_handle,
338 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
339 };
340 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
341 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
342 auto packet =
343 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
344 auto temp_view =
345 LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
346 ASSERT_TRUE(temp_view.IsValid());
347
348 // Get command status
349 test_le_scanning_interface_->CommandStatusCallback(
350 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
351
352 EXPECT_CALL(mock_callbacks_, OnPeriodicSyncStarted);
353
354 // Get LePeriodicAdvertisingSyncEstablished
355 auto builder = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
356 ErrorCode::SUCCESS, sync_handle, advertiser_sid, address_with_type.GetAddressType(),
357 address_with_type.GetAddress(), SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
358 auto event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
359 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
360 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
361
362 // StopSync
363 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
364 periodic_sync_manager_->StopSync(sync_handle);
365 packet = test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_TERMINATE_SYNC);
366 auto packet_view =
367 LePeriodicAdvertisingTerminateSyncView::Create(LeScanningCommandView::Create(packet));
368 ASSERT_TRUE(packet_view.IsValid());
369 ASSERT_EQ(sync_handle, packet_view.GetSyncHandle());
370 sync_handler();
371 }
372
TEST_F(PeriodicSyncManagerTest,cancel_create_sync_test)373 TEST_F(PeriodicSyncManagerTest, cancel_create_sync_test) {
374 uint16_t sync_handle = 0x12;
375 uint8_t advertiser_sid = 0x02;
376 // start scan
377 Address address;
378 Address::FromString("00:11:22:33:44:55", address);
379 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
380 PeriodicSyncStates request{
381 .request_id = 0x01,
382 .advertiser_sid = advertiser_sid,
383 .address_with_type = address_with_type,
384 .sync_handle = sync_handle,
385 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
386 };
387 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
388 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
389 auto packet =
390 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
391 auto temp_view =
392 LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
393 ASSERT_TRUE(temp_view.IsValid());
394
395 // Get command status
396 test_le_scanning_interface_->CommandStatusCallback(
397 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
398
399 // Cancel crate sync
400 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
401 periodic_sync_manager_->CancelCreateSync(advertiser_sid, address_with_type.GetAddress());
402 packet = test_le_scanning_interface_->GetCommand(
403 OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC_CANCEL);
404 auto packet_view =
405 LePeriodicAdvertisingCreateSyncCancelView::Create(LeScanningCommandView::Create(packet));
406 ASSERT_TRUE(packet_view.IsValid());
407 sync_handler();
408 }
409
TEST_F(PeriodicSyncManagerTest,transfer_sync_test)410 TEST_F(PeriodicSyncManagerTest, transfer_sync_test) {
411 Address address;
412 Address::FromString("00:11:22:33:44:55", address);
413 uint16_t service_data = 0x10;
414 uint16_t sync_handle = 0x11;
415 uint16_t connection_handle = 0x12;
416 int pa_source = 0x01;
417 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
418 periodic_sync_manager_->TransferSync(address, service_data, sync_handle, pa_source,
419 connection_handle);
420 auto packet =
421 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_SYNC_TRANSFER);
422 auto packet_view =
423 LePeriodicAdvertisingSyncTransferView::Create(LeScanningCommandView::Create(packet));
424 ASSERT_TRUE(packet_view.IsValid());
425 ASSERT_EQ(connection_handle, packet_view.GetConnectionHandle());
426 ASSERT_EQ(service_data, packet_view.GetServiceData());
427 ASSERT_EQ(sync_handle, packet_view.GetSyncHandle());
428
429 EXPECT_CALL(mock_callbacks_, OnPeriodicSyncTransferred);
430
431 // Get command complete
432 test_le_scanning_interface_->CommandCompleteCallback(
433 LePeriodicAdvertisingSyncTransferCompleteBuilder::Create(0x00, ErrorCode::SUCCESS,
434 connection_handle));
435
436 sync_handler();
437 }
438
TEST_F(PeriodicSyncManagerTest,sync_set_info_test)439 TEST_F(PeriodicSyncManagerTest, sync_set_info_test) {
440 Address address;
441 Address::FromString("00:11:22:33:44:55", address);
442 uint16_t service_data = 0x10;
443 uint16_t advertising_handle = 0x11;
444 uint16_t connection_handle = 0x12;
445 int pa_source = 0x01;
446 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
447 periodic_sync_manager_->SyncSetInfo(address, service_data, advertising_handle, pa_source,
448 connection_handle);
449 auto packet = test_le_scanning_interface_->GetCommand(
450 OpCode::LE_PERIODIC_ADVERTISING_SET_INFO_TRANSFER);
451 auto packet_view =
452 LePeriodicAdvertisingSetInfoTransferView::Create(LeScanningCommandView::Create(packet));
453 ASSERT_TRUE(packet_view.IsValid());
454 ASSERT_EQ(connection_handle, packet_view.GetConnectionHandle());
455 ASSERT_EQ(service_data, packet_view.GetServiceData());
456 ASSERT_EQ(advertising_handle, packet_view.GetAdvertisingHandle());
457
458 EXPECT_CALL(mock_callbacks_, OnPeriodicSyncTransferred);
459
460 // Get command complete
461 test_le_scanning_interface_->CommandCompleteCallback(
462 LePeriodicAdvertisingSetInfoTransferCompleteBuilder::Create(0x00, ErrorCode::SUCCESS,
463 connection_handle));
464
465 sync_handler();
466 }
467
TEST_F(PeriodicSyncManagerTest,sync_tx_parameters_test)468 TEST_F(PeriodicSyncManagerTest, sync_tx_parameters_test) {
469 Address address;
470 Address::FromString("00:11:22:33:44:55", address);
471 uint8_t mode = 0x00;
472 uint16_t skip = 0x11;
473 uint16_t timeout = 0x12;
474 int reg_id = 0x01;
475 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
476 periodic_sync_manager_->SyncTxParameters(address, mode, skip, timeout, reg_id);
477 auto packet = test_le_scanning_interface_->GetCommand(
478 OpCode::LE_SET_DEFAULT_PERIODIC_ADVERTISING_SYNC_TRANSFER_PARAMETERS);
479 auto packet_view = LeSetDefaultPeriodicAdvertisingSyncTransferParametersView::Create(
480 LeScanningCommandView::Create(packet));
481
482 ASSERT_TRUE(packet_view.IsValid());
483 ASSERT_EQ(mode, (uint8_t)packet_view.GetMode());
484 ASSERT_EQ(skip, packet_view.GetSkip());
485 ASSERT_EQ(timeout, packet_view.GetSyncTimeout());
486
487 sync_handler();
488 }
489
TEST_F(PeriodicSyncManagerTest,handle_sync_lost_test)490 TEST_F(PeriodicSyncManagerTest, handle_sync_lost_test) {
491 uint16_t sync_handle = 0x12;
492 uint8_t advertiser_sid = 0x02;
493 // start scan
494 Address address;
495 Address::FromString("00:11:22:33:44:55", address);
496 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
497 PeriodicSyncStates request{
498 .request_id = 0x01,
499 .advertiser_sid = advertiser_sid,
500 .address_with_type = address_with_type,
501 .sync_handle = sync_handle,
502 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
503 };
504 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
505 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
506 auto packet =
507 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
508 auto temp_view =
509 LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
510 ASSERT_TRUE(temp_view.IsValid());
511
512 // Get command status
513 test_le_scanning_interface_->CommandStatusCallback(
514 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
515
516 EXPECT_CALL(mock_callbacks_, OnPeriodicSyncStarted);
517
518 // Get LePeriodicAdvertisingSyncEstablished
519 auto builder = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
520 ErrorCode::SUCCESS, sync_handle, advertiser_sid, address_with_type.GetAddressType(),
521 address_with_type.GetAddress(), SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
522 auto event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
523 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
524 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
525
526 EXPECT_CALL(mock_callbacks_, OnPeriodicSyncLost);
527
528 // Get LePeriodicAdvertisingSyncLost
529 auto builder2 = LePeriodicAdvertisingSyncLostBuilder::Create(sync_handle);
530
531 auto event_view2 = LePeriodicAdvertisingSyncLostView::Create(
532 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder2)))));
533 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncLost(event_view2);
534
535 sync_handler();
536 }
537
TEST_F(PeriodicSyncManagerTest,handle_advertising_sync_established_after_error_test)538 TEST_F(PeriodicSyncManagerTest, handle_advertising_sync_established_after_error_test) {
539 uint16_t sync_handle = 0x12;
540 uint8_t advertiser_sid = 0x02;
541 // start scan
542 Address address;
543 Address::FromString("00:11:22:33:44:55", address);
544 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
545
546 // First request which will finish with error
547 int request_id_1 = 0x01;
548 PeriodicSyncStates request{
549 .request_id = request_id_1,
550 .advertiser_sid = advertiser_sid,
551 .address_with_type = address_with_type,
552 .sync_handle = sync_handle,
553 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
554 };
555 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
556 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
557 auto packet =
558 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
559 auto temp_view =
560 LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
561 ASSERT_TRUE(temp_view.IsValid());
562
563 // Get command status
564 test_le_scanning_interface_->CommandStatusCallback(
565 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
566
567 EXPECT_CALL(
568 mock_callbacks_,
569 OnPeriodicSyncStarted(request_id_1,
570 static_cast<uint8_t>(ErrorCode::CONNECTION_FAILED_ESTABLISHMENT), _,
571 _, _, _, _))
572 .Times(1);
573
574 // Get LePeriodicAdvertisingSyncEstablished
575 auto builder = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
576 ErrorCode::CONNECTION_FAILED_ESTABLISHMENT, sync_handle, advertiser_sid,
577 address_with_type.GetAddressType(), address_with_type.GetAddress(),
578 SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
579 auto event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
580 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
581 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
582
583 // Second request with the same data but different id
584 int request_id_2 = 0x02;
585 request.request_id = request_id_2;
586 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
587 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
588 packet = test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
589 temp_view = LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
590 ASSERT_TRUE(temp_view.IsValid());
591
592 // Get command status
593 test_le_scanning_interface_->CommandStatusCallback(
594 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
595
596 EXPECT_CALL(mock_callbacks_,
597 OnPeriodicSyncStarted(request_id_2, static_cast<uint8_t>(ErrorCode::SUCCESS), _, _, _,
598 _, _))
599 .Times(1);
600
601 // Get LePeriodicAdvertisingSyncEstablished
602 auto builder2 = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
603 ErrorCode::SUCCESS, sync_handle, advertiser_sid, address_with_type.GetAddressType(),
604 address_with_type.GetAddress(), SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
605 event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
606 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder2)))));
607 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
608
609 sync_handler();
610 }
611
TEST_F(PeriodicSyncManagerTest,handle_advertising_sync_established_after_create_command_error_test)612 TEST_F(PeriodicSyncManagerTest,
613 handle_advertising_sync_established_after_create_command_error_test) {
614 uint16_t sync_handle = 0x12;
615 Address address;
616 Address::FromString("00:11:22:33:44:55", address);
617 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
618
619 // First request which will finish with error
620 int request_id_1 = 0x01;
621 uint8_t advertiser_sid_1 = 0x02;
622 PeriodicSyncStates request{
623 .request_id = request_id_1,
624 .advertiser_sid = advertiser_sid_1,
625 .address_with_type = address_with_type,
626 .sync_handle = sync_handle,
627 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
628 };
629 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
630 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
631 auto packet =
632 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
633 auto temp_view =
634 LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
635 ASSERT_TRUE(temp_view.IsValid());
636
637 EXPECT_CALL(mock_callbacks_,
638 OnPeriodicSyncStarted(request_id_1,
639 static_cast<uint8_t>(ErrorCode::MEMORY_CAPACITY_EXCEEDED), _,
640 advertiser_sid_1, _, _, _))
641 .Times(1);
642
643 // Get command status
644 test_le_scanning_interface_->CommandStatusCallback(
645 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::MEMORY_CAPACITY_EXCEEDED,
646 0x00));
647
648 // Second request
649 int request_id_2 = 0x02;
650 uint8_t advertiser_sid_2 = 0x03;
651 request.request_id = request_id_2;
652 request.advertiser_sid = advertiser_sid_2;
653 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
654 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
655 packet = test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
656 temp_view = LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
657 ASSERT_TRUE(temp_view.IsValid());
658
659 // Get command status
660 test_le_scanning_interface_->CommandStatusCallback(
661 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
662
663 EXPECT_CALL(mock_callbacks_,
664 OnPeriodicSyncStarted(request_id_2, static_cast<uint8_t>(ErrorCode::SUCCESS), _,
665 advertiser_sid_2, _, _, _))
666 .Times(1);
667
668 // Get LePeriodicAdvertisingSyncEstablished
669 auto builder = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
670 ErrorCode::SUCCESS, sync_handle, advertiser_sid_2, address_with_type.GetAddressType(),
671 address_with_type.GetAddress(), SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
672 auto event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
673 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
674 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
675
676 sync_handler();
677 }
678
TEST_F(PeriodicSyncManagerTest,handle_advertising_sync_established_after_cancel_command_error_test)679 TEST_F(PeriodicSyncManagerTest,
680 handle_advertising_sync_established_after_cancel_command_error_test) {
681 uint16_t sync_handle = 0x12;
682 Address address;
683 Address::FromString("00:11:22:33:44:55", address);
684 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
685
686 // First request which will finish with timeout error
687 uint8_t advertiser_sid_1 = 0x02;
688 int request_id_1 = 0x01;
689 PeriodicSyncStates request{
690 .request_id = request_id_1,
691 .advertiser_sid = advertiser_sid_1,
692 .address_with_type = address_with_type,
693 .sync_handle = sync_handle,
694 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
695 };
696 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
697 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
698 auto packet =
699 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
700 auto temp_view =
701 LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
702 ASSERT_TRUE(temp_view.IsValid());
703
704 // Get command status
705 test_le_scanning_interface_->CommandStatusCallback(
706 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
707
708 EXPECT_CALL(
709 mock_callbacks_,
710 OnPeriodicSyncStarted(request_id_1, static_cast<uint8_t>(ErrorCode::ADVERTISING_TIMEOUT),
711 _, advertiser_sid_1, _, _, _))
712 .Times(1);
713
714 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
715 periodic_sync_manager_->OnStartSyncTimeout();
716 packet = test_le_scanning_interface_->GetCommand(
717 OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC_CANCEL);
718 auto temp_view2 =
719 LePeriodicAdvertisingCreateSyncCancelView::Create(LeScanningCommandView::Create(packet));
720 ASSERT_TRUE(temp_view2.IsValid());
721
722 // Get command status
723 test_le_scanning_interface_->CommandCompleteCallback(
724 LePeriodicAdvertisingCreateSyncCancelCompleteBuilder::Create(
725 0x00, ErrorCode::COMMAND_DISALLOWED));
726
727 // Second request
728 int request_id_2 = 0x02;
729 uint8_t advertiser_sid_2 = 0x03;
730 request.request_id = request_id_2;
731 request.advertiser_sid = advertiser_sid_2;
732 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
733 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
734 packet = test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
735 temp_view = LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
736 ASSERT_TRUE(temp_view.IsValid());
737
738 // Get command status
739 test_le_scanning_interface_->CommandStatusCallback(
740 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
741
742 EXPECT_CALL(mock_callbacks_,
743 OnPeriodicSyncStarted(request_id_2, static_cast<uint8_t>(ErrorCode::SUCCESS), _,
744 advertiser_sid_2, _, _, _))
745 .Times(1);
746
747 // Get LePeriodicAdvertisingSyncEstablished
748 auto builder2 = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
749 ErrorCode::SUCCESS, sync_handle, advertiser_sid_2, address_with_type.GetAddressType(),
750 address_with_type.GetAddress(), SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
751 auto event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
752 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder2)))));
753 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
754
755 sync_handler();
756 }
757
TEST_F(PeriodicSyncManagerTest,onStartSyncTimeout_callWithoutPendingRequestsAndPeriodicSyncs)758 TEST_F(PeriodicSyncManagerTest, onStartSyncTimeout_callWithoutPendingRequestsAndPeriodicSyncs) {
759 periodic_sync_manager_->OnStartSyncTimeout();
760 sync_handler();
761 }
762
TEST_F(PeriodicSyncManagerTest,onStartSyncTimeout_callWithoutPeriodicSyncs)763 TEST_F(PeriodicSyncManagerTest, onStartSyncTimeout_callWithoutPeriodicSyncs) {
764 uint16_t sync_handle = 0x12;
765 Address address;
766 Address::FromString("00:11:22:33:44:55", address);
767 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
768
769 int request_id_1 = 0x01;
770 uint8_t advertiser_sid_1 = 0x02;
771 PeriodicSyncStates request{
772 .request_id = request_id_1,
773 .advertiser_sid = advertiser_sid_1,
774 .address_with_type = address_with_type,
775 .sync_handle = sync_handle,
776 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
777 };
778 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
779
780 // First timeout to erase periodic_syncs_
781 periodic_sync_manager_->OnStartSyncTimeout();
782 // Second to actual check
783 periodic_sync_manager_->OnStartSyncTimeout();
784 sync_handler();
785 }
786
TEST_F(PeriodicSyncManagerTest,handlePeriodicAdvertisingCreateSyncStatus_callWithoutPeriodicSyncs)787 TEST_F(PeriodicSyncManagerTest,
788 handlePeriodicAdvertisingCreateSyncStatus_callWithoutPeriodicSyncs) {
789 uint16_t sync_handle = 0x12;
790 Address address;
791 Address::FromString("00:11:22:33:44:55", address);
792 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
793
794 int request_id_1 = 0x01;
795 uint8_t advertiser_sid_1 = 0x02;
796 PeriodicSyncStates request{
797 .request_id = request_id_1,
798 .advertiser_sid = advertiser_sid_1,
799 .address_with_type = address_with_type,
800 .sync_handle = sync_handle,
801 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
802 };
803 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
804 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
805
806 // Timeout to erase periodic_syncs_
807 periodic_sync_manager_->OnStartSyncTimeout();
808
809 auto packet =
810 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
811 LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
812 test_le_scanning_interface_->CommandStatusCallback(
813 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::MEMORY_CAPACITY_EXCEEDED,
814 0x00));
815 sync_handler();
816 }
817
TEST_F(PeriodicSyncManagerTest,handleLePeriodicAdvertisingReport_callWithoutPeriodicSyncs)818 TEST_F(PeriodicSyncManagerTest, handleLePeriodicAdvertisingReport_callWithoutPeriodicSyncs) {
819 uint16_t sync_handle = 0x12;
820 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
821
822 // Get LePeriodicAdvertisingReport
823 std::vector<uint8_t> data = {0x01, 0x02, 0x03};
824 auto builder = LePeriodicAdvertisingReportBuilder::Create(sync_handle, 0x1a, 0x1a,
825 CteType::AOA_CONSTANT_TONE_EXTENSION,
826 DataStatus::COMPLETE, data);
827 auto event_view = LePeriodicAdvertisingReportView::Create(
828 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
829 periodic_sync_manager_->HandleLePeriodicAdvertisingReport(event_view);
830
831 // Check sync termination
832 auto packet =
833 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_TERMINATE_SYNC);
834 auto packet_view =
835 LePeriodicAdvertisingTerminateSyncView::Create(LeScanningCommandView::Create(packet));
836 ASSERT_TRUE(packet_view.IsValid());
837 ASSERT_EQ(sync_handle, packet_view.GetSyncHandle());
838 sync_handler();
839 }
840
TEST_F(PeriodicSyncManagerTest,handleLePeriodicAdvertisingSyncLost_callWithoutPeriodicSyncs)841 TEST_F(PeriodicSyncManagerTest, handleLePeriodicAdvertisingSyncLost_callWithoutPeriodicSyncs) {
842 uint16_t sync_handle = 0x12;
843 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
844
845 // Get LePeriodicAdvertisingSyncLost
846 auto builder = LePeriodicAdvertisingSyncLostBuilder::Create(sync_handle);
847
848 auto event_view = LePeriodicAdvertisingSyncLostView::Create(
849 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
850 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncLost(event_view);
851
852 // Check sync termination
853 auto packet =
854 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_TERMINATE_SYNC);
855 auto packet_view =
856 LePeriodicAdvertisingTerminateSyncView::Create(LeScanningCommandView::Create(packet));
857 ASSERT_TRUE(packet_view.IsValid());
858 ASSERT_EQ(sync_handle, packet_view.GetSyncHandle());
859 sync_handler();
860 }
861
TEST_F(PeriodicSyncManagerTest,handleLeBigInfoAdvertisingReport_callWithoutPeriodicSyncs)862 TEST_F(PeriodicSyncManagerTest, handleLeBigInfoAdvertisingReport_callWithoutPeriodicSyncs) {
863 uint16_t sync_handle = 0x12;
864 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
865
866 // Get LeBigInfoAdvertisingReport
867 auto builder = LeBigInfoAdvertisingReportBuilder::Create(
868 sync_handle, 2, 9, 24, 3, 1, 2, 100, 10000, 100, static_cast<SecondaryPhyType>(2),
869 static_cast<Enable>(0), static_cast<Enable>(1));
870
871 auto event_view = LeBigInfoAdvertisingReportView::Create(
872 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
873 periodic_sync_manager_->HandleLeBigInfoAdvertisingReport(event_view);
874
875 // Check sync termination
876 auto packet =
877 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_TERMINATE_SYNC);
878 auto packet_view =
879 LePeriodicAdvertisingTerminateSyncView::Create(LeScanningCommandView::Create(packet));
880 ASSERT_TRUE(packet_view.IsValid());
881 ASSERT_EQ(sync_handle, packet_view.GetSyncHandle());
882 sync_handler();
883 }
884
TEST_F(PeriodicSyncManagerTest,syncEstablished_pendingCheckToCorrectTheOrder)885 TEST_F(PeriodicSyncManagerTest, syncEstablished_pendingCheckToCorrectTheOrder) {
886 uint16_t sync_handle = 0x12;
887 uint8_t advertiser_sid = 0x02;
888 Address address;
889 Address::FromString("00:11:22:33:44:55", address);
890 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
891
892 // start scan
893 int request_id_1 = 0x01;
894 PeriodicSyncStates request{
895 .request_id = request_id_1,
896 .advertiser_sid = advertiser_sid,
897 .address_with_type = address_with_type,
898 .sync_handle = sync_handle,
899 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
900 };
901 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
902
903 EXPECT_CALL(
904 mock_callbacks_,
905 OnPeriodicSyncStarted(request_id_1, static_cast<uint8_t>(ErrorCode::ADVERTISING_TIMEOUT),
906 _, _, _, _, _))
907 .Times(1);
908
909 // First timeout
910 periodic_sync_manager_->OnStartSyncTimeout();
911
912 // Second request with the same data but different id
913 int request_id_2 = 0x02;
914 request.request_id = request_id_2;
915 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
916
917 // Get LePeriodicAdvertisingSyncEstablished for the first request
918 auto builder = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
919 ErrorCode::OPERATION_CANCELLED_BY_HOST, sync_handle, advertiser_sid,
920 address_with_type.GetAddressType(), address_with_type.GetAddress(),
921 SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
922 auto event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
923 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
924 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
925
926 EXPECT_CALL(
927 mock_callbacks_,
928 OnPeriodicSyncStarted(request_id_2, static_cast<uint8_t>(ErrorCode::ADVERTISING_TIMEOUT),
929 _, _, _, _, _))
930 .Times(1);
931
932 // Second timeout
933 periodic_sync_manager_->OnStartSyncTimeout();
934
935 // Get LePeriodicAdvertisingSyncEstablished for the second request
936 auto builder2 = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
937 ErrorCode::OPERATION_CANCELLED_BY_HOST, sync_handle, advertiser_sid,
938 address_with_type.GetAddressType(), address_with_type.GetAddress(),
939 SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
940 event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
941 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder2)))));
942 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
943 sync_handler();
944 }
945
TEST_F(PeriodicSyncManagerTest,handle_periodic_advertising_report_test)946 TEST_F(PeriodicSyncManagerTest, handle_periodic_advertising_report_test) {
947 uint16_t sync_handle = 0x12;
948 uint8_t advertiser_sid = 0x02;
949 // start scan
950 Address address;
951 Address::FromString("00:11:22:33:44:55", address);
952 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
953 PeriodicSyncStates request{
954 .request_id = 0x01,
955 .advertiser_sid = advertiser_sid,
956 .address_with_type = address_with_type,
957 .sync_handle = sync_handle,
958 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
959 };
960 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
961 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
962 auto packet =
963 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
964 auto temp_view =
965 LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
966 ASSERT_TRUE(temp_view.IsValid());
967
968 // Get command status
969 test_le_scanning_interface_->CommandStatusCallback(
970 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
971
972 EXPECT_CALL(mock_callbacks_, OnPeriodicSyncStarted);
973
974 // Get LePeriodicAdvertisingSyncEstablished
975 auto builder = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
976 ErrorCode::SUCCESS, sync_handle, advertiser_sid, address_with_type.GetAddressType(),
977 address_with_type.GetAddress(), SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
978 auto event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
979 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
980 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
981
982 EXPECT_CALL(mock_callbacks_, OnPeriodicSyncReport);
983
984 // Get LePeriodicAdvertisingReport
985 std::vector<uint8_t> data = {0x01, 0x02, 0x03};
986 auto builder2 = LePeriodicAdvertisingReportBuilder::Create(sync_handle, 0x1a, 0x1a,
987 CteType::AOA_CONSTANT_TONE_EXTENSION,
988 DataStatus::COMPLETE, data);
989
990 auto event_view2 = LePeriodicAdvertisingReportView::Create(
991 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder2)))));
992 periodic_sync_manager_->HandleLePeriodicAdvertisingReport(event_view2);
993
994 sync_handler();
995 }
996
TEST_F(PeriodicSyncManagerTest,handle_biginfo_advertising_report_test)997 TEST_F(PeriodicSyncManagerTest, handle_biginfo_advertising_report_test) {
998 uint16_t sync_handle = 0x12;
999 uint8_t advertiser_sid = 0x02;
1000 // start scan
1001 Address address;
1002 Address::FromString("00:11:22:33:44:55", address);
1003 AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
1004 PeriodicSyncStates request{
1005 .request_id = 0x01,
1006 .advertiser_sid = advertiser_sid,
1007 .address_with_type = address_with_type,
1008 .sync_handle = sync_handle,
1009 .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
1010 };
1011 ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
1012 periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
1013 auto packet =
1014 test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
1015 auto temp_view =
1016 LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
1017 ASSERT_TRUE(temp_view.IsValid());
1018
1019 // Get command status
1020 test_le_scanning_interface_->CommandStatusCallback(
1021 LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
1022
1023 EXPECT_CALL(mock_callbacks_, OnPeriodicSyncStarted);
1024
1025 // Get LePeriodicAdvertisingSyncEstablished
1026 auto builder = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
1027 ErrorCode::SUCCESS, sync_handle, advertiser_sid, address_with_type.GetAddressType(),
1028 address_with_type.GetAddress(), SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
1029 auto event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
1030 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
1031 periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
1032
1033 EXPECT_CALL(mock_callbacks_, OnBigInfoReport);
1034
1035 // Get LeBigInfoAdvertisingReport
1036 auto builder2 = LeBigInfoAdvertisingReportBuilder::Create(
1037 sync_handle, 2, 9, 24, 3, 1, 2, 100, 10000, 100, static_cast<SecondaryPhyType>(2),
1038 static_cast<Enable>(0), static_cast<Enable>(1));
1039
1040 auto event_view2 = LeBigInfoAdvertisingReportView::Create(
1041 LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder2)))));
1042 periodic_sync_manager_->HandleLeBigInfoAdvertisingReport(event_view2);
1043
1044 sync_handler();
1045 }
1046
1047 } // namespace
1048 } // namespace hci
1049 } // namespace bluetooth
1050