1 /*
2  * Copyright 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   uint8_t advertiser_sid_1 = 0x02;
770   int request_id_1 = 0x01;
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   ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
779   periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
780 
781   // First timeout to erase periodic_syncs_
782   periodic_sync_manager_->OnStartSyncTimeout();
783   // Second to actual check
784   periodic_sync_manager_->OnStartSyncTimeout();
785   sync_handler();
786 }
787 
TEST_F(PeriodicSyncManagerTest,handlePeriodicAdvertisingCreateSyncStatus_callWithoutPeriodicSyncs)788 TEST_F(PeriodicSyncManagerTest,
789        handlePeriodicAdvertisingCreateSyncStatus_callWithoutPeriodicSyncs) {
790   uint16_t sync_handle = 0x12;
791   Address address;
792   Address::FromString("00:11:22:33:44:55", address);
793   AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
794 
795   int request_id_1 = 0x01;
796   uint8_t advertiser_sid_1 = 0x02;
797   PeriodicSyncStates request{
798           .request_id = request_id_1,
799           .advertiser_sid = advertiser_sid_1,
800           .address_with_type = address_with_type,
801           .sync_handle = sync_handle,
802           .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
803   };
804   ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
805   periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
806 
807   // Timeout to erase periodic_syncs_
808   periodic_sync_manager_->OnStartSyncTimeout();
809 
810   auto packet =
811           test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
812   LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
813   test_le_scanning_interface_->CommandStatusCallback(
814           LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::MEMORY_CAPACITY_EXCEEDED,
815                                                                0x00));
816   sync_handler();
817 }
818 
TEST_F(PeriodicSyncManagerTest,syncEstablished_pendingCheckToCorrectTheOrder)819 TEST_F(PeriodicSyncManagerTest, syncEstablished_pendingCheckToCorrectTheOrder) {
820   uint16_t sync_handle = 0x12;
821   uint8_t advertiser_sid = 0x02;
822   Address address;
823   Address::FromString("00:11:22:33:44:55", address);
824   AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
825 
826   // start scan
827   int request_id_1 = 0x01;
828   PeriodicSyncStates request{
829           .request_id = request_id_1,
830           .advertiser_sid = advertiser_sid,
831           .address_with_type = address_with_type,
832           .sync_handle = sync_handle,
833           .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
834   };
835   periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
836 
837   EXPECT_CALL(
838           mock_callbacks_,
839           OnPeriodicSyncStarted(request_id_1, static_cast<uint8_t>(ErrorCode::ADVERTISING_TIMEOUT),
840                                 _, _, _, _, _))
841           .Times(1);
842 
843   // First timeout
844   periodic_sync_manager_->OnStartSyncTimeout();
845 
846   // Second request with the same data but different id
847   int request_id_2 = 0x02;
848   request.request_id = request_id_2;
849   periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
850 
851   // Get LePeriodicAdvertisingSyncEstablished for the first request
852   auto builder = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
853           ErrorCode::OPERATION_CANCELLED_BY_HOST, sync_handle, advertiser_sid,
854           address_with_type.GetAddressType(), address_with_type.GetAddress(),
855           SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
856   auto event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
857           LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
858   periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
859 
860   EXPECT_CALL(
861           mock_callbacks_,
862           OnPeriodicSyncStarted(request_id_2, static_cast<uint8_t>(ErrorCode::ADVERTISING_TIMEOUT),
863                                 _, _, _, _, _))
864           .Times(1);
865 
866   // Second timeout
867   periodic_sync_manager_->OnStartSyncTimeout();
868 
869   // Get LePeriodicAdvertisingSyncEstablished for the second request
870   auto builder2 = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
871           ErrorCode::OPERATION_CANCELLED_BY_HOST, sync_handle, advertiser_sid,
872           address_with_type.GetAddressType(), address_with_type.GetAddress(),
873           SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
874   event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
875           LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder2)))));
876   periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
877   sync_handler();
878 }
879 
TEST_F(PeriodicSyncManagerTest,handle_periodic_advertising_report_test)880 TEST_F(PeriodicSyncManagerTest, handle_periodic_advertising_report_test) {
881   uint16_t sync_handle = 0x12;
882   uint8_t advertiser_sid = 0x02;
883   // start scan
884   Address address;
885   Address::FromString("00:11:22:33:44:55", address);
886   AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
887   PeriodicSyncStates request{
888           .request_id = 0x01,
889           .advertiser_sid = advertiser_sid,
890           .address_with_type = address_with_type,
891           .sync_handle = sync_handle,
892           .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
893   };
894   ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
895   periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
896   auto packet =
897           test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
898   auto temp_view =
899           LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
900   ASSERT_TRUE(temp_view.IsValid());
901 
902   // Get command status
903   test_le_scanning_interface_->CommandStatusCallback(
904           LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
905 
906   EXPECT_CALL(mock_callbacks_, OnPeriodicSyncStarted);
907 
908   // Get LePeriodicAdvertisingSyncEstablished
909   auto builder = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
910           ErrorCode::SUCCESS, sync_handle, advertiser_sid, address_with_type.GetAddressType(),
911           address_with_type.GetAddress(), SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
912   auto event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
913           LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
914   periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
915 
916   EXPECT_CALL(mock_callbacks_, OnPeriodicSyncReport);
917 
918   // Get LePeriodicAdvertisingReport
919   std::vector<uint8_t> data = {0x01, 0x02, 0x03};
920   auto builder2 = LePeriodicAdvertisingReportBuilder::Create(sync_handle, 0x1a, 0x1a,
921                                                              CteType::AOA_CONSTANT_TONE_EXTENSION,
922                                                              DataStatus::COMPLETE, data);
923 
924   auto event_view2 = LePeriodicAdvertisingReportView::Create(
925           LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder2)))));
926   periodic_sync_manager_->HandleLePeriodicAdvertisingReport(event_view2);
927 
928   sync_handler();
929 }
930 
TEST_F(PeriodicSyncManagerTest,handle_biginfo_advertising_report_test)931 TEST_F(PeriodicSyncManagerTest, handle_biginfo_advertising_report_test) {
932   uint16_t sync_handle = 0x12;
933   uint8_t advertiser_sid = 0x02;
934   // start scan
935   Address address;
936   Address::FromString("00:11:22:33:44:55", address);
937   AddressWithType address_with_type = AddressWithType(address, AddressType::PUBLIC_DEVICE_ADDRESS);
938   PeriodicSyncStates request{
939           .request_id = 0x01,
940           .advertiser_sid = advertiser_sid,
941           .address_with_type = address_with_type,
942           .sync_handle = sync_handle,
943           .sync_state = PeriodicSyncState::PERIODIC_SYNC_STATE_IDLE,
944   };
945   ASSERT_NO_FATAL_FAILURE(test_le_scanning_interface_->SetCommandFuture());
946   periodic_sync_manager_->StartSync(request, 0x04, 0x0A);
947   auto packet =
948           test_le_scanning_interface_->GetCommand(OpCode::LE_PERIODIC_ADVERTISING_CREATE_SYNC);
949   auto temp_view =
950           LePeriodicAdvertisingCreateSyncView::Create(LeScanningCommandView::Create(packet));
951   ASSERT_TRUE(temp_view.IsValid());
952 
953   // Get command status
954   test_le_scanning_interface_->CommandStatusCallback(
955           LePeriodicAdvertisingCreateSyncStatusBuilder::Create(ErrorCode::SUCCESS, 0x00));
956 
957   EXPECT_CALL(mock_callbacks_, OnPeriodicSyncStarted);
958 
959   // Get LePeriodicAdvertisingSyncEstablished
960   auto builder = LePeriodicAdvertisingSyncEstablishedBuilder::Create(
961           ErrorCode::SUCCESS, sync_handle, advertiser_sid, address_with_type.GetAddressType(),
962           address_with_type.GetAddress(), SecondaryPhyType::LE_1M, 0xFF, ClockAccuracy::PPM_250);
963   auto event_view = LePeriodicAdvertisingSyncEstablishedView::Create(
964           LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder)))));
965   periodic_sync_manager_->HandleLePeriodicAdvertisingSyncEstablished(event_view);
966 
967   EXPECT_CALL(mock_callbacks_, OnBigInfoReport);
968 
969   // Get LeBigInfoAdvertisingReport
970   auto builder2 = LeBigInfoAdvertisingReportBuilder::Create(
971           sync_handle, 2, 9, 24, 3, 1, 2, 100, 10000, 100, static_cast<SecondaryPhyType>(2),
972           static_cast<Enable>(0), static_cast<Enable>(1));
973 
974   auto event_view2 = LeBigInfoAdvertisingReportView::Create(
975           LeMetaEventView::Create(EventView::Create(GetPacketView(std::move(builder2)))));
976   periodic_sync_manager_->HandleLeBigInfoAdvertisingReport(event_view2);
977 
978   sync_handler();
979 }
980 
981 }  // namespace
982 }  // namespace hci
983 }  // namespace bluetooth
984