1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "hci/le_advertising_manager.h"
18 
19 #include <algorithm>
20 #include <chrono>
21 #include <future>
22 #include <map>
23 
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26 
27 #include "common/bind.h"
28 #include "hci/acl_manager.h"
29 #include "hci/address.h"
30 #include "hci/controller.h"
31 #include "hci/hci_layer.h"
32 #include "os/thread.h"
33 #include "packet/raw_builder.h"
34 
35 namespace bluetooth {
36 namespace hci {
37 namespace {
38 
39 using packet::kLittleEndian;
40 using packet::PacketView;
41 using packet::RawBuilder;
42 
GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet)43 PacketView<kLittleEndian> GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet) {
44   auto bytes = std::make_shared<std::vector<uint8_t>>();
45   BitInserter i(*bytes);
46   bytes->reserve(packet->size());
47   packet->Serialize(i);
48   return packet::PacketView<packet::kLittleEndian>(bytes);
49 }
50 
51 class TestController : public Controller {
52  public:
IsSupported(OpCode op_code) const53   bool IsSupported(OpCode op_code) const override {
54     return supported_opcodes_.count(op_code) == 1;
55   }
56 
AddSupported(OpCode op_code)57   void AddSupported(OpCode op_code) {
58     supported_opcodes_.insert(op_code);
59   }
60 
GetControllerLeNumberOfSupportedAdverisingSets() const61   uint8_t GetControllerLeNumberOfSupportedAdverisingSets() const override {
62     return num_advertisers;
63   }
64 
65   uint8_t num_advertisers{0};
66 
67  protected:
Start()68   void Start() override {}
Stop()69   void Stop() override {}
ListDependencies(ModuleList * list)70   void ListDependencies(ModuleList* list) override {}
71 
72  private:
73   std::set<OpCode> supported_opcodes_{};
74 };
75 
76 class TestHciLayer : public HciLayer {
77  public:
EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,common::ContextualOnceCallback<void (CommandStatusView)> on_status)78   void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,
79                       common::ContextualOnceCallback<void(CommandStatusView)> on_status) override {
80     auto packet_view = CommandPacketView::Create(GetPacketView(std::move(command)));
81     ASSERT_TRUE(packet_view.IsValid());
82     command_queue_.push_back(packet_view);
83     command_status_callbacks.push_back(std::move(on_status));
84     if (command_promise_ != nullptr &&
85         (command_op_code_ == OpCode::NONE || command_op_code_ == packet_view.GetOpCode())) {
86       if (command_op_code_ == OpCode::LE_MULTI_ADVT && command_sub_ocf_ != SubOcf::SET_ENABLE) {
87         return;
88       }
89       command_promise_->set_value(command_queue_.size());
90       command_promise_.reset();
91     }
92   }
93 
EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,common::ContextualOnceCallback<void (CommandCompleteView)> on_complete)94   void EnqueueCommand(std::unique_ptr<CommandPacketBuilder> command,
95                       common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override {
96     auto packet_view = CommandPacketView::Create(GetPacketView(std::move(command)));
97     ASSERT_TRUE(packet_view.IsValid());
98     command_queue_.push_back(packet_view);
99     command_complete_callbacks.push_back(std::move(on_complete));
100     if (command_promise_ != nullptr &&
101         (command_op_code_ == OpCode::NONE || command_op_code_ == packet_view.GetOpCode())) {
102       if (command_op_code_ == OpCode::LE_MULTI_ADVT) {
103         auto sub_view = LeMultiAdvtView::Create(LeAdvertisingCommandView::Create(packet_view));
104         ASSERT_TRUE(sub_view.IsValid());
105         if (sub_view.GetSubCmd() != command_sub_ocf_) {
106           return;
107         }
108       }
109       command_promise_->set_value(command_queue_.size());
110       command_promise_.reset();
111     }
112   }
113 
GetCommandFuture(OpCode op_code=OpCode::NONE)114   std::future<size_t> GetCommandFuture(OpCode op_code = OpCode::NONE) {
115     ASSERT_LOG(command_promise_ == nullptr, "Promises promises ... Only one at a time");
116     command_op_code_ = op_code;
117     command_promise_ = std::make_unique<std::promise<size_t>>();
118     return command_promise_->get_future();
119   }
120 
GetSubCommandFuture(SubOcf sub_ocf)121   std::future<size_t> GetSubCommandFuture(SubOcf sub_ocf) {
122     ASSERT_LOG(command_promise_ == nullptr, "Promises promises ... Only one at a time");
123     command_op_code_ = OpCode::LE_MULTI_ADVT;
124     command_sub_ocf_ = sub_ocf;
125     command_promise_ = std::make_unique<std::promise<size_t>>();
126     return command_promise_->get_future();
127   }
128 
GetCommandPacket(OpCode op_code)129   ConnectionManagementCommandView GetCommandPacket(OpCode op_code) {
130     if (command_queue_.empty()) {
131       return ConnectionManagementCommandView::Create(
132           CommandPacketView::Create(PacketView<kLittleEndian>(std::make_shared<std::vector<uint8_t>>())));
133     }
134     CommandPacketView command_packet_view = CommandPacketView::Create(command_queue_.front());
135     command_queue_.pop_front();
136     ConnectionManagementCommandView command = ConnectionManagementCommandView::Create(command_packet_view);
137     EXPECT_TRUE(command.IsValid());
138     EXPECT_EQ(command.GetOpCode(), op_code);
139 
140     return command;
141   }
142 
RegisterEventHandler(EventCode event_code,common::ContextualCallback<void (EventPacketView)> event_handler)143   void RegisterEventHandler(EventCode event_code,
144                             common::ContextualCallback<void(EventPacketView)> event_handler) override {
145     registered_events_[event_code] = event_handler;
146   }
147 
RegisterLeEventHandler(SubeventCode subevent_code,common::ContextualCallback<void (LeMetaEventView)> event_handler)148   void RegisterLeEventHandler(SubeventCode subevent_code,
149                               common::ContextualCallback<void(LeMetaEventView)> event_handler) override {
150     registered_le_events_[subevent_code] = event_handler;
151   }
152 
IncomingEvent(std::unique_ptr<EventPacketBuilder> event_builder)153   void IncomingEvent(std::unique_ptr<EventPacketBuilder> event_builder) {
154     auto packet = GetPacketView(std::move(event_builder));
155     EventPacketView event = EventPacketView::Create(packet);
156     ASSERT_TRUE(event.IsValid());
157     EventCode event_code = event.GetEventCode();
158     ASSERT_NE(registered_events_.find(event_code), registered_events_.end()) << EventCodeText(event_code);
159     registered_events_[event_code].Invoke(event);
160   }
161 
IncomingLeMetaEvent(std::unique_ptr<LeMetaEventBuilder> event_builder)162   void IncomingLeMetaEvent(std::unique_ptr<LeMetaEventBuilder> event_builder) {
163     auto packet = GetPacketView(std::move(event_builder));
164     EventPacketView event = EventPacketView::Create(packet);
165     LeMetaEventView meta_event_view = LeMetaEventView::Create(event);
166     ASSERT_TRUE(meta_event_view.IsValid());
167     SubeventCode subevent_code = meta_event_view.GetSubeventCode();
168     ASSERT_NE(registered_le_events_.find(subevent_code), registered_le_events_.end())
169         << SubeventCodeText(subevent_code);
170     registered_le_events_[subevent_code].Invoke(meta_event_view);
171   }
172 
CommandCompleteCallback(EventPacketView event)173   void CommandCompleteCallback(EventPacketView event) {
174     CommandCompleteView complete_view = CommandCompleteView::Create(event);
175     ASSERT_TRUE(complete_view.IsValid());
176     std::move(command_complete_callbacks.front()).Invoke(complete_view);
177     command_complete_callbacks.pop_front();
178   }
179 
CommandStatusCallback(EventPacketView event)180   void CommandStatusCallback(EventPacketView event) {
181     CommandStatusView status_view = CommandStatusView::Create(event);
182     ASSERT_TRUE(status_view.IsValid());
183     std::move(command_status_callbacks.front()).Invoke(status_view);
184     command_status_callbacks.pop_front();
185   }
186 
ListDependencies(ModuleList * list)187   void ListDependencies(ModuleList* list) override {}
Start()188   void Start() override {
189     RegisterEventHandler(EventCode::COMMAND_COMPLETE,
190                          GetHandler()->BindOn(this, &TestHciLayer::CommandCompleteCallback));
191     RegisterEventHandler(EventCode::COMMAND_STATUS, GetHandler()->BindOn(this, &TestHciLayer::CommandStatusCallback));
192   }
Stop()193   void Stop() override {}
194 
195  private:
196   std::map<EventCode, common::ContextualCallback<void(EventPacketView)>> registered_events_;
197   std::map<SubeventCode, common::ContextualCallback<void(LeMetaEventView)>> registered_le_events_;
198   std::list<common::ContextualOnceCallback<void(CommandCompleteView)>> command_complete_callbacks;
199   std::list<common::ContextualOnceCallback<void(CommandStatusView)>> command_status_callbacks;
200 
201   std::list<CommandPacketView> command_queue_;
202   mutable std::mutex mutex_;
203   std::unique_ptr<std::promise<size_t>> command_promise_{};
204   OpCode command_op_code_;
205   SubOcf command_sub_ocf_;
206 };
207 
208 class TestLeAddressManager : public LeAddressManager {
209  public:
TestLeAddressManager(common::Callback<void (std::unique_ptr<CommandPacketBuilder>)> enqueue_command,os::Handler * handler,Address public_address,uint8_t connect_list_size,uint8_t resolving_list_size)210   TestLeAddressManager(
211       common::Callback<void(std::unique_ptr<CommandPacketBuilder>)> enqueue_command,
212       os::Handler* handler,
213       Address public_address,
214       uint8_t connect_list_size,
215       uint8_t resolving_list_size)
216       : LeAddressManager(enqueue_command, handler, public_address, connect_list_size, resolving_list_size) {}
217 
Register(LeAddressManagerCallback * callback)218   AddressPolicy Register(LeAddressManagerCallback* callback) override {
219     return AddressPolicy::USE_STATIC_ADDRESS;
220   }
221 
Unregister(LeAddressManagerCallback * callback)222   void Unregister(LeAddressManagerCallback* callback) override {}
223 
GetAnotherAddress()224   AddressWithType GetAnotherAddress() override {
225     hci::Address address;
226     Address::FromString("05:04:03:02:01:00", address);
227     auto random_address = AddressWithType(address, AddressType::RANDOM_DEVICE_ADDRESS);
228     return random_address;
229   }
230 };
231 
232 class TestAclManager : public AclManager {
233  public:
GetLeAddressManager()234   LeAddressManager* GetLeAddressManager() override {
235     return test_le_address_manager_;
236   }
237 
238  protected:
Start()239   void Start() override {
240     thread_ = new os::Thread("thread", os::Thread::Priority::NORMAL);
241     handler_ = new os::Handler(thread_);
242     Address address({0x01, 0x02, 0x03, 0x04, 0x05, 0x06});
243     test_le_address_manager_ = new TestLeAddressManager(
244         common::Bind(&TestAclManager::enqueue_command, common::Unretained(this)), handler_, address, 0x3F, 0x3F);
245   }
246 
Stop()247   void Stop() override {
248     delete test_le_address_manager_;
249     handler_->Clear();
250     delete handler_;
251     delete thread_;
252   }
253 
ListDependencies(ModuleList * list)254   void ListDependencies(ModuleList* list) override {}
255 
SetRandomAddress(Address address)256   void SetRandomAddress(Address address) {}
257 
enqueue_command(std::unique_ptr<CommandPacketBuilder> command_packet)258   void enqueue_command(std::unique_ptr<CommandPacketBuilder> command_packet){};
259 
260   os::Thread* thread_;
261   os::Handler* handler_;
262   TestLeAddressManager* test_le_address_manager_;
263 };
264 
265 class LeAdvertisingManagerTest : public ::testing::Test {
266  protected:
SetUp()267   void SetUp() override {
268     test_hci_layer_ = new TestHciLayer;  // Ownership is transferred to registry
269     test_controller_ = new TestController;
270     test_acl_manager_ = new TestAclManager;
271     test_controller_->AddSupported(param_opcode_);
272     fake_registry_.InjectTestModule(&HciLayer::Factory, test_hci_layer_);
273     fake_registry_.InjectTestModule(&Controller::Factory, test_controller_);
274     fake_registry_.InjectTestModule(&AclManager::Factory, test_acl_manager_);
275     client_handler_ = fake_registry_.GetTestModuleHandler(&HciLayer::Factory);
276     ASSERT_NE(client_handler_, nullptr);
277     test_controller_->num_advertisers = 1;
278     le_advertising_manager_ = fake_registry_.Start<LeAdvertisingManager>(&thread_);
279   }
280 
TearDown()281   void TearDown() override {
282     fake_registry_.SynchronizeModuleHandler(&LeAdvertisingManager::Factory, std::chrono::milliseconds(20));
283     fake_registry_.StopAll();
284   }
285 
286   TestModuleRegistry fake_registry_;
287   TestHciLayer* test_hci_layer_ = nullptr;
288   TestController* test_controller_ = nullptr;
289   TestAclManager* test_acl_manager_ = nullptr;
290   os::Thread& thread_ = fake_registry_.GetTestThread();
291   LeAdvertisingManager* le_advertising_manager_ = nullptr;
292   os::Handler* client_handler_ = nullptr;
293 
294   const common::Callback<void(Address, AddressType)> scan_callback =
295       common::Bind(&LeAdvertisingManagerTest::on_scan, common::Unretained(this));
296   const common::Callback<void(ErrorCode, uint8_t, uint8_t)> set_terminated_callback =
297       common::Bind(&LeAdvertisingManagerTest::on_set_terminated, common::Unretained(this));
298 
GetOnScanPromise()299   std::future<Address> GetOnScanPromise() {
300     ASSERT_LOG(address_promise_ == nullptr, "Promises promises ... Only one at a time");
301     address_promise_ = std::make_unique<std::promise<Address>>();
302     return address_promise_->get_future();
303   }
on_scan(Address address,AddressType address_type)304   void on_scan(Address address, AddressType address_type) {
305     if (address_promise_ == nullptr) {
306       return;
307     }
308     address_promise_->set_value(address);
309     address_promise_.reset();
310   }
311 
GetSetTerminatedPromise()312   std::future<ErrorCode> GetSetTerminatedPromise() {
313     ASSERT_LOG(set_terminated_promise_ == nullptr, "Promises promises ... Only one at a time");
314     set_terminated_promise_ = std::make_unique<std::promise<ErrorCode>>();
315     return set_terminated_promise_->get_future();
316   }
on_set_terminated(ErrorCode error_code,uint8_t,uint8_t)317   void on_set_terminated(ErrorCode error_code, uint8_t, uint8_t) {
318     if (set_terminated_promise_ != nullptr) {
319       return;
320     }
321     set_terminated_promise_->set_value(error_code);
322     set_terminated_promise_.reset();
323   }
324 
325   std::unique_ptr<std::promise<Address>> address_promise_{};
326   std::unique_ptr<std::promise<ErrorCode>> set_terminated_promise_{};
327 
328   OpCode param_opcode_{OpCode::LE_SET_ADVERTISING_PARAMETERS};
329 };
330 
331 class LeAndroidHciAdvertisingManagerTest : public LeAdvertisingManagerTest {
332  protected:
SetUp()333   void SetUp() override {
334     param_opcode_ = OpCode::LE_MULTI_ADVT;
335     LeAdvertisingManagerTest::SetUp();
336     test_controller_->num_advertisers = 3;
337   }
338 };
339 
340 class LeExtendedAdvertisingManagerTest : public LeAdvertisingManagerTest {
341  protected:
SetUp()342   void SetUp() override {
343     param_opcode_ = OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS;
344     LeAdvertisingManagerTest::SetUp();
345     test_controller_->num_advertisers = 5;
346   }
347 };
348 
TEST_F(LeAdvertisingManagerTest,startup_teardown)349 TEST_F(LeAdvertisingManagerTest, startup_teardown) {}
350 
TEST_F(LeAndroidHciAdvertisingManagerTest,startup_teardown)351 TEST_F(LeAndroidHciAdvertisingManagerTest, startup_teardown) {}
352 
TEST_F(LeExtendedAdvertisingManagerTest,startup_teardown)353 TEST_F(LeExtendedAdvertisingManagerTest, startup_teardown) {}
354 
TEST_F(LeAdvertisingManagerTest,create_advertiser_test)355 TEST_F(LeAdvertisingManagerTest, create_advertiser_test) {
356   AdvertisingConfig advertising_config{};
357   advertising_config.event_type = AdvertisingType::ADV_IND;
358   advertising_config.address_type = AddressType::PUBLIC_DEVICE_ADDRESS;
359   std::vector<GapData> gap_data{};
360   GapData data_item{};
361   data_item.data_type_ = GapDataType::FLAGS;
362   data_item.data_ = {0x34};
363   gap_data.push_back(data_item);
364   data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
365   data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
366   gap_data.push_back(data_item);
367   advertising_config.advertisement = gap_data;
368   advertising_config.scan_response = gap_data;
369 
370   auto last_command_future = test_hci_layer_->GetCommandFuture(OpCode::LE_SET_ADVERTISING_ENABLE);
371   auto id = le_advertising_manager_->CreateAdvertiser(advertising_config, scan_callback, set_terminated_callback,
372                                                       client_handler_);
373   ASSERT_NE(LeAdvertisingManager::kInvalidId, id);
374   std::vector<OpCode> adv_opcodes = {
375       OpCode::LE_SET_ADVERTISING_PARAMETERS,
376       OpCode::LE_SET_SCAN_RESPONSE_DATA,
377       OpCode::LE_SET_ADVERTISING_DATA,
378       OpCode::LE_SET_ADVERTISING_ENABLE,
379   };
380   std::vector<uint8_t> success_vector{static_cast<uint8_t>(ErrorCode::SUCCESS)};
381   auto result = last_command_future.wait_for(std::chrono::duration(std::chrono::milliseconds(100)));
382   ASSERT_EQ(std::future_status::ready, result);
383   for (size_t i = 0; i < adv_opcodes.size(); i++) {
384     auto packet_view = test_hci_layer_->GetCommandPacket(adv_opcodes[i]);
385     CommandPacketView command_packet_view = CommandPacketView::Create(packet_view);
386     ConnectionManagementCommandView command = ConnectionManagementCommandView::Create(command_packet_view);
387     test_hci_layer_->IncomingEvent(
388         CommandCompleteBuilder::Create(uint8_t{1}, adv_opcodes[i], std::make_unique<RawBuilder>(success_vector)));
389   }
390   // Disable the advertiser
391   last_command_future = test_hci_layer_->GetCommandFuture(OpCode::LE_SET_ADVERTISING_ENABLE);
392   le_advertising_manager_->RemoveAdvertiser(id);
393   result = last_command_future.wait_for(std::chrono::duration(std::chrono::milliseconds(100)));
394   ASSERT_EQ(std::future_status::ready, result);
395   test_hci_layer_->IncomingEvent(LeSetAdvertisingEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
396 }
397 
TEST_F(LeAndroidHciAdvertisingManagerTest,create_advertiser_test)398 TEST_F(LeAndroidHciAdvertisingManagerTest, create_advertiser_test) {
399   AdvertisingConfig advertising_config{};
400   advertising_config.event_type = AdvertisingType::ADV_IND;
401   advertising_config.address_type = AddressType::PUBLIC_DEVICE_ADDRESS;
402   std::vector<GapData> gap_data{};
403   GapData data_item{};
404   data_item.data_type_ = GapDataType::FLAGS;
405   data_item.data_ = {0x34};
406   gap_data.push_back(data_item);
407   data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
408   data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
409   gap_data.push_back(data_item);
410   advertising_config.advertisement = gap_data;
411   advertising_config.scan_response = gap_data;
412 
413   auto next_command_future = test_hci_layer_->GetSubCommandFuture(SubOcf::SET_ENABLE);
414   auto id = le_advertising_manager_->CreateAdvertiser(advertising_config, scan_callback, set_terminated_callback,
415                                                       client_handler_);
416   ASSERT_NE(LeAdvertisingManager::kInvalidId, id);
417   std::vector<SubOcf> sub_ocf = {
418       SubOcf::SET_PARAM, SubOcf::SET_DATA, SubOcf::SET_SCAN_RESP, SubOcf::SET_RANDOM_ADDR, SubOcf::SET_ENABLE,
419   };
420   auto result = next_command_future.wait_for(std::chrono::duration(std::chrono::milliseconds(100)));
421   ASSERT_EQ(std::future_status::ready, result);
422   size_t num_commands = next_command_future.get();
423   for (size_t i = 0; i < sub_ocf.size(); i++) {
424     auto packet = test_hci_layer_->GetCommandPacket(OpCode::LE_MULTI_ADVT);
425     auto sub_packet = LeMultiAdvtView::Create(LeAdvertisingCommandView::Create(packet));
426     ASSERT_TRUE(sub_packet.IsValid());
427     test_hci_layer_->IncomingEvent(LeMultiAdvtCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS, sub_ocf[i]));
428     num_commands -= 1;
429   }
430   ASSERT_EQ(0, num_commands);
431   // Disable the advertiser
432   next_command_future = test_hci_layer_->GetSubCommandFuture(SubOcf::SET_ENABLE);
433   le_advertising_manager_->RemoveAdvertiser(id);
434   result = next_command_future.wait_for(std::chrono::duration(std::chrono::milliseconds(100)));
435   ASSERT_EQ(std::future_status::ready, result);
436   test_hci_layer_->IncomingEvent(LeMultiAdvtSetEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
437 }
438 
TEST_F(LeExtendedAdvertisingManagerTest,create_advertiser_test)439 TEST_F(LeExtendedAdvertisingManagerTest, create_advertiser_test) {
440   ExtendedAdvertisingConfig advertising_config{};
441   advertising_config.event_type = AdvertisingType::ADV_IND;
442   advertising_config.address_type = AddressType::PUBLIC_DEVICE_ADDRESS;
443   std::vector<GapData> gap_data{};
444   GapData data_item{};
445   data_item.data_type_ = GapDataType::FLAGS;
446   data_item.data_ = {0x34};
447   gap_data.push_back(data_item);
448   data_item.data_type_ = GapDataType::COMPLETE_LOCAL_NAME;
449   data_item.data_ = {'r', 'a', 'n', 'd', 'o', 'm', ' ', 'd', 'e', 'v', 'i', 'c', 'e'};
450   gap_data.push_back(data_item);
451   advertising_config.advertisement = gap_data;
452   advertising_config.scan_response = gap_data;
453   advertising_config.channel_map = 1;
454   advertising_config.sid = 0x01;
455 
456   auto last_command_future = test_hci_layer_->GetCommandFuture(OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE);
457   auto id = le_advertising_manager_->ExtendedCreateAdvertiser(advertising_config, scan_callback,
458                                                               set_terminated_callback, client_handler_);
459   ASSERT_NE(LeAdvertisingManager::kInvalidId, id);
460   std::vector<OpCode> adv_opcodes = {
461       OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS,    OpCode::LE_SET_EXTENDED_ADVERTISING_RANDOM_ADDRESS,
462       OpCode::LE_SET_EXTENDED_ADVERTISING_SCAN_RESPONSE, OpCode::LE_SET_EXTENDED_ADVERTISING_DATA,
463       OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE,
464   };
465   auto result = last_command_future.wait_for(std::chrono::duration(std::chrono::milliseconds(100)));
466   std::vector<uint8_t> success_vector{static_cast<uint8_t>(ErrorCode::SUCCESS)};
467   ASSERT_EQ(std::future_status::ready, result);
468   for (size_t i = 0; i < adv_opcodes.size(); i++) {
469     auto packet_view = test_hci_layer_->GetCommandPacket(adv_opcodes[i]);
470     CommandPacketView command_packet_view = CommandPacketView::Create(packet_view);
471     ConnectionManagementCommandView command = ConnectionManagementCommandView::Create(command_packet_view);
472     if (adv_opcodes[i] == OpCode::LE_SET_EXTENDED_ADVERTISING_PARAMETERS) {
473       test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingParametersCompleteBuilder::Create(
474           uint8_t{1}, ErrorCode::SUCCESS, static_cast<uint8_t>(-23)));
475     } else {
476       test_hci_layer_->IncomingEvent(
477           CommandCompleteBuilder::Create(uint8_t{1}, adv_opcodes[i], std::make_unique<RawBuilder>(success_vector)));
478     }
479   }
480   // Disable the advertiser
481   last_command_future = test_hci_layer_->GetCommandFuture(OpCode::LE_SET_EXTENDED_ADVERTISING_ENABLE);
482   le_advertising_manager_->RemoveAdvertiser(id);
483   result = last_command_future.wait_for(std::chrono::duration(std::chrono::milliseconds(100)));
484   ASSERT_EQ(std::future_status::ready, result);
485   test_hci_layer_->IncomingEvent(LeSetExtendedAdvertisingEnableCompleteBuilder::Create(uint8_t{1}, ErrorCode::SUCCESS));
486 }
487 }  // namespace
488 }  // namespace hci
489 }  // namespace bluetooth
490