• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "hci/controller.h"
18 
19 #include <algorithm>
20 #include <chrono>
21 #include <future>
22 #include <map>
23 
24 #include <gtest/gtest.h>
25 
26 #include "common/bind.h"
27 #include "common/callback.h"
28 #include "common/init_flags.h"
29 #include "hci/address.h"
30 #include "hci/hci_layer.h"
31 #include "os/thread.h"
32 #include "packet/raw_builder.h"
33 
34 namespace bluetooth {
35 namespace hci {
36 namespace {
37 
38 using common::BidiQueue;
39 using common::BidiQueueEnd;
40 using packet::kLittleEndian;
41 using packet::PacketView;
42 using packet::RawBuilder;
43 
44 constexpr uint16_t kHandle1 = 0x123;
45 constexpr uint16_t kCredits1 = 0x78;
46 constexpr uint16_t kHandle2 = 0x456;
47 constexpr uint16_t kCredits2 = 0x9a;
48 uint16_t feature_spec_version = 55;
49 
GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet)50 PacketView<kLittleEndian> GetPacketView(std::unique_ptr<packet::BasePacketBuilder> packet) {
51   auto bytes = std::make_shared<std::vector<uint8_t>>();
52   BitInserter i(*bytes);
53   bytes->reserve(packet->size());
54   packet->Serialize(i);
55   return packet::PacketView<packet::kLittleEndian>(bytes);
56 }
57 
58 class TestHciLayer : public HciLayer {
59  public:
EnqueueCommand(std::unique_ptr<CommandBuilder> command,common::ContextualOnceCallback<void (CommandCompleteView)> on_complete)60   void EnqueueCommand(
61       std::unique_ptr<CommandBuilder> command,
62       common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) override {
63     GetHandler()->Post(common::BindOnce(&TestHciLayer::HandleCommand, common::Unretained(this), std::move(command),
64                                         std::move(on_complete)));
65   }
66 
EnqueueCommand(std::unique_ptr<CommandBuilder> command,common::ContextualOnceCallback<void (CommandStatusView)> on_status)67   void EnqueueCommand(
68       std::unique_ptr<CommandBuilder> command,
69       common::ContextualOnceCallback<void(CommandStatusView)> on_status) override {
70     EXPECT_TRUE(false) << "Controller properties should not generate Command Status";
71   }
72 
HandleCommand(std::unique_ptr<CommandBuilder> command_builder,common::ContextualOnceCallback<void (CommandCompleteView)> on_complete)73   void HandleCommand(
74       std::unique_ptr<CommandBuilder> command_builder,
75       common::ContextualOnceCallback<void(CommandCompleteView)> on_complete) {
76     auto packet_view = GetPacketView(std::move(command_builder));
77     CommandView command = CommandView::Create(packet_view);
78     ASSERT_TRUE(command.IsValid());
79 
80     uint8_t num_packets = 1;
81     std::unique_ptr<packet::BasePacketBuilder> event_builder;
82     switch (command.GetOpCode()) {
83       case (OpCode::READ_LOCAL_NAME): {
84         std::array<uint8_t, 248> local_name = {'D', 'U', 'T', '\0'};
85         event_builder = ReadLocalNameCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, local_name);
86       } break;
87       case (OpCode::READ_LOCAL_VERSION_INFORMATION): {
88         LocalVersionInformation local_version_information;
89         local_version_information.hci_version_ = HciVersion::V_5_0;
90         local_version_information.hci_revision_ = 0x1234;
91         local_version_information.lmp_version_ = LmpVersion::V_4_2;
92         local_version_information.manufacturer_name_ = 0xBAD;
93         local_version_information.lmp_subversion_ = 0x5678;
94         event_builder = ReadLocalVersionInformationCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS,
95                                                                            local_version_information);
96       } break;
97       case (OpCode::READ_LOCAL_SUPPORTED_COMMANDS): {
98         std::array<uint8_t, 64> supported_commands;
99         for (int i = 0; i < 37; i++) {
100           supported_commands[i] = 0xff;
101         }
102         for (int i = 37; i < 64; i++) {
103           supported_commands[i] = 0x00;
104         }
105         event_builder =
106             ReadLocalSupportedCommandsCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, supported_commands);
107       } break;
108       case (OpCode::READ_LOCAL_EXTENDED_FEATURES): {
109         ReadLocalExtendedFeaturesView read_command = ReadLocalExtendedFeaturesView::Create(command);
110         ASSERT_TRUE(read_command.IsValid());
111         uint8_t page_bumber = read_command.GetPageNumber();
112         uint64_t lmp_features = 0x012345678abcdef;
113         lmp_features += page_bumber;
114         event_builder = ReadLocalExtendedFeaturesCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, page_bumber,
115                                                                          0x02, lmp_features);
116       } break;
117       case (OpCode::READ_BUFFER_SIZE): {
118         event_builder = ReadBufferSizeCompleteBuilder::Create(
119             num_packets, ErrorCode::SUCCESS, acl_data_packet_length, synchronous_data_packet_length,
120             total_num_acl_data_packets, total_num_synchronous_data_packets);
121       } break;
122       case (OpCode::READ_BD_ADDR): {
123         event_builder = ReadBdAddrCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, Address::kAny);
124       } break;
125       case (OpCode::LE_READ_BUFFER_SIZE_V1): {
126         LeBufferSize le_buffer_size;
127         le_buffer_size.le_data_packet_length_ = 0x16;
128         le_buffer_size.total_num_le_packets_ = 0x08;
129         event_builder = LeReadBufferSizeV1CompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, le_buffer_size);
130       } break;
131       case (OpCode::LE_READ_LOCAL_SUPPORTED_FEATURES): {
132         event_builder =
133             LeReadLocalSupportedFeaturesCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, 0x001f123456789abc);
134       } break;
135       case (OpCode::LE_READ_SUPPORTED_STATES): {
136         event_builder =
137             LeReadSupportedStatesCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, 0x001f123456789abe);
138       } break;
139       case (OpCode::LE_READ_MAXIMUM_DATA_LENGTH): {
140         LeMaximumDataLength le_maximum_data_length;
141         le_maximum_data_length.supported_max_tx_octets_ = 0x12;
142         le_maximum_data_length.supported_max_tx_time_ = 0x34;
143         le_maximum_data_length.supported_max_rx_octets_ = 0x56;
144         le_maximum_data_length.supported_max_rx_time_ = 0x78;
145         event_builder =
146             LeReadMaximumDataLengthCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, le_maximum_data_length);
147       } break;
148       case (OpCode::LE_READ_MAXIMUM_ADVERTISING_DATA_LENGTH): {
149         event_builder =
150             LeReadMaximumAdvertisingDataLengthCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, 0x0672);
151       } break;
152       case (OpCode::LE_READ_NUMBER_OF_SUPPORTED_ADVERTISING_SETS): {
153         event_builder =
154             LeReadNumberOfSupportedAdvertisingSetsCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS, 0xF0);
155       } break;
156       case (OpCode::LE_GET_VENDOR_CAPABILITIES): {
157         BaseVendorCapabilities base_vendor_capabilities;
158         base_vendor_capabilities.max_advt_instances_ = 0x10;
159         base_vendor_capabilities.offloaded_resolution_of_private_address_ = 0x01;
160         base_vendor_capabilities.total_scan_results_storage_ = 0x2800;
161         base_vendor_capabilities.max_irk_list_sz_ = 0x20;
162         base_vendor_capabilities.filtering_support_ = 0x01;
163         base_vendor_capabilities.max_filter_ = 0x10;
164         base_vendor_capabilities.activity_energy_info_support_ = 0x01;
165 
166         auto payload = std::make_unique<RawBuilder>();
167         if (feature_spec_version > 55) {
168           std::vector<uint8_t> payload_bytes = {0x20, 0x00, 0x01, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00};
169           payload->AddOctets2(feature_spec_version);
170           payload->AddOctets(payload_bytes);
171         }
172         event_builder = LeGetVendorCapabilitiesCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS,
173                                                                        base_vendor_capabilities, std::move(payload));
174       } break;
175       case (OpCode::SET_EVENT_MASK): {
176         auto view = SetEventMaskView::Create(command);
177         ASSERT_TRUE(view.IsValid());
178         event_mask = view.GetEventMask();
179         event_builder = SetEventMaskCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS);
180       } break;
181       case (OpCode::LE_SET_EVENT_MASK): {
182         auto view = LeSetEventMaskView::Create(command);
183         ASSERT_TRUE(view.IsValid());
184         le_event_mask = view.GetLeEventMask();
185         event_builder = LeSetEventMaskCompleteBuilder::Create(num_packets, ErrorCode::SUCCESS);
186       } break;
187 
188       case (OpCode::RESET):
189       case (OpCode::SET_EVENT_FILTER):
190       case (OpCode::HOST_BUFFER_SIZE):
191         command_queue_.push(command);
192         not_empty_.notify_all();
193         return;
194       default:
195         LOG_INFO("Dropping unhandled packet");
196         return;
197     }
198     auto packet = GetPacketView(std::move(event_builder));
199     EventView event = EventView::Create(packet);
200     ASSERT_TRUE(event.IsValid());
201     CommandCompleteView command_complete = CommandCompleteView::Create(event);
202     ASSERT_TRUE(command_complete.IsValid());
203     on_complete.Invoke(std::move(command_complete));
204   }
205 
RegisterEventHandler(EventCode event_code,common::ContextualCallback<void (EventView)> event_handler)206   void RegisterEventHandler(EventCode event_code, common::ContextualCallback<void(EventView)> event_handler) override {
207     EXPECT_EQ(event_code, EventCode::NUMBER_OF_COMPLETED_PACKETS) << "Only NUMBER_OF_COMPLETED_PACKETS is needed";
208     number_of_completed_packets_callback_ = event_handler;
209   }
210 
UnregisterEventHandler(EventCode event_code)211   void UnregisterEventHandler(EventCode event_code) override {
212     EXPECT_EQ(event_code, EventCode::NUMBER_OF_COMPLETED_PACKETS) << "Only NUMBER_OF_COMPLETED_PACKETS is needed";
213     number_of_completed_packets_callback_ = {};
214   }
215 
IncomingCredit()216   void IncomingCredit() {
217     std::vector<CompletedPackets> completed_packets;
218     CompletedPackets cp;
219     cp.host_num_of_completed_packets_ = kCredits1;
220     cp.connection_handle_ = kHandle1;
221     completed_packets.push_back(cp);
222     cp.host_num_of_completed_packets_ = kCredits2;
223     cp.connection_handle_ = kHandle2;
224     completed_packets.push_back(cp);
225     auto event_builder = NumberOfCompletedPacketsBuilder::Create(completed_packets);
226     auto packet = GetPacketView(std::move(event_builder));
227     EventView event = EventView::Create(packet);
228     ASSERT_TRUE(event.IsValid());
229     number_of_completed_packets_callback_.Invoke(event);
230   }
231 
GetCommand(OpCode op_code)232   CommandView GetCommand(OpCode op_code) {
233     std::unique_lock<std::mutex> lock(mutex_);
234     std::chrono::milliseconds time = std::chrono::milliseconds(3000);
235 
236     // wait for command
237     while (command_queue_.size() == 0) {
238       if (not_empty_.wait_for(lock, time) == std::cv_status::timeout) {
239         break;
240       }
241     }
242     EXPECT_TRUE(command_queue_.size() > 0);
243     if (command_queue_.empty()) {
244       return CommandView::Create(PacketView<kLittleEndian>(std::make_shared<std::vector<uint8_t>>()));
245     }
246     CommandView command = command_queue_.front();
247     EXPECT_EQ(command.GetOpCode(), op_code);
248     command_queue_.pop();
249     return command;
250   }
251 
ListDependencies(ModuleList * list)252   void ListDependencies(ModuleList* list) override {}
Start()253   void Start() override {}
Stop()254   void Stop() override {}
255 
256   constexpr static uint16_t acl_data_packet_length = 1024;
257   constexpr static uint8_t synchronous_data_packet_length = 60;
258   constexpr static uint16_t total_num_acl_data_packets = 10;
259   constexpr static uint16_t total_num_synchronous_data_packets = 12;
260   uint64_t event_mask = 0;
261   uint64_t le_event_mask = 0;
262 
263  private:
264   common::ContextualCallback<void(EventView)> number_of_completed_packets_callback_;
265   std::queue<CommandView> command_queue_;
266   mutable std::mutex mutex_;
267   std::condition_variable not_empty_;
268 };
269 
270 class ControllerTest : public ::testing::Test {
271  protected:
SetUp()272   void SetUp() override {
273     bluetooth::common::InitFlags::SetAllForTesting();
274     test_hci_layer_ = new TestHciLayer;
275     fake_registry_.InjectTestModule(&HciLayer::Factory, test_hci_layer_);
276     client_handler_ = fake_registry_.GetTestModuleHandler(&HciLayer::Factory);
277     fake_registry_.Start<Controller>(&thread_);
278     controller_ = static_cast<Controller*>(fake_registry_.GetModuleUnderTest(&Controller::Factory));
279   }
280 
TearDown()281   void TearDown() override {
282     fake_registry_.StopAll();
283   }
284 
285   TestModuleRegistry fake_registry_;
286   TestHciLayer* test_hci_layer_ = nullptr;
287   os::Thread& thread_ = fake_registry_.GetTestThread();
288   Controller* controller_ = nullptr;
289   os::Handler* client_handler_ = nullptr;
290 };
291 
TEST_F(ControllerTest,startup_teardown)292 TEST_F(ControllerTest, startup_teardown) {}
293 
TEST_F(ControllerTest,read_controller_info)294 TEST_F(ControllerTest, read_controller_info) {
295   ASSERT_EQ(controller_->GetAclPacketLength(), test_hci_layer_->acl_data_packet_length);
296   ASSERT_EQ(controller_->GetNumAclPacketBuffers(), test_hci_layer_->total_num_acl_data_packets);
297   ASSERT_EQ(controller_->GetScoPacketLength(), test_hci_layer_->synchronous_data_packet_length);
298   ASSERT_EQ(controller_->GetNumScoPacketBuffers(), test_hci_layer_->total_num_synchronous_data_packets);
299   ASSERT_EQ(controller_->GetMacAddress(), Address::kAny);
300   LocalVersionInformation local_version_information = controller_->GetLocalVersionInformation();
301   ASSERT_EQ(local_version_information.hci_version_, HciVersion::V_5_0);
302   ASSERT_EQ(local_version_information.hci_revision_, 0x1234);
303   ASSERT_EQ(local_version_information.lmp_version_, LmpVersion::V_4_2);
304   ASSERT_EQ(local_version_information.manufacturer_name_, 0xBAD);
305   ASSERT_EQ(local_version_information.lmp_subversion_, 0x5678);
306   ASSERT_EQ(controller_->GetLeBufferSize().le_data_packet_length_, 0x16);
307   ASSERT_EQ(controller_->GetLeBufferSize().total_num_le_packets_, 0x08);
308   ASSERT_EQ(controller_->GetLeSupportedStates(), 0x001f123456789abe);
309   ASSERT_EQ(controller_->GetLeMaximumDataLength().supported_max_tx_octets_, 0x12);
310   ASSERT_EQ(controller_->GetLeMaximumDataLength().supported_max_tx_time_, 0x34);
311   ASSERT_EQ(controller_->GetLeMaximumDataLength().supported_max_rx_octets_, 0x56);
312   ASSERT_EQ(controller_->GetLeMaximumDataLength().supported_max_rx_time_, 0x78);
313   ASSERT_EQ(controller_->GetLeMaximumAdvertisingDataLength(), 0x0672);
314   ASSERT_EQ(controller_->GetLeNumberOfSupportedAdverisingSets(), 0xF0);
315 }
316 
TEST_F(ControllerTest,read_write_local_name)317 TEST_F(ControllerTest, read_write_local_name) {
318   ASSERT_EQ(controller_->GetLocalName(), "DUT");
319   controller_->WriteLocalName("New name");
320   ASSERT_EQ(controller_->GetLocalName(), "New name");
321 }
322 
TEST_F(ControllerTest,send_set_event_mask_command)323 TEST_F(ControllerTest, send_set_event_mask_command) {
324   uint64_t new_event_mask = test_hci_layer_->event_mask - 1;
325   controller_->SetEventMask(new_event_mask);
326   // Send another command to make sure it was applied
327   controller_->Reset();
328   auto packet = test_hci_layer_->GetCommand(OpCode::RESET);
329   ASSERT_EQ(new_event_mask, test_hci_layer_->event_mask);
330 }
331 
TEST_F(ControllerTest,send_reset_command)332 TEST_F(ControllerTest, send_reset_command) {
333   controller_->Reset();
334   auto packet = test_hci_layer_->GetCommand(OpCode::RESET);
335   auto command = ResetView::Create(packet);
336   ASSERT_TRUE(command.IsValid());
337 }
338 
TEST_F(ControllerTest,send_set_event_filter_command)339 TEST_F(ControllerTest, send_set_event_filter_command) {
340   controller_->SetEventFilterInquiryResultAllDevices();
341   auto packet = test_hci_layer_->GetCommand(OpCode::SET_EVENT_FILTER);
342   auto set_event_filter_view1 = SetEventFilterView::Create(packet);
343   auto set_event_filter_inquiry_result_view1 = SetEventFilterInquiryResultView::Create(set_event_filter_view1);
344   auto command1 = SetEventFilterInquiryResultAllDevicesView::Create(set_event_filter_inquiry_result_view1);
345   ASSERT_TRUE(command1.IsValid());
346 
347   ClassOfDevice class_of_device({0xab, 0xcd, 0xef});
348   ClassOfDevice class_of_device_mask({0x12, 0x34, 0x56});
349   controller_->SetEventFilterInquiryResultClassOfDevice(class_of_device, class_of_device_mask);
350   packet = test_hci_layer_->GetCommand(OpCode::SET_EVENT_FILTER);
351   auto set_event_filter_view2 = SetEventFilterView::Create(packet);
352   auto set_event_filter_inquiry_result_view2 = SetEventFilterInquiryResultView::Create(set_event_filter_view2);
353   auto command2 = SetEventFilterInquiryResultClassOfDeviceView::Create(set_event_filter_inquiry_result_view2);
354   ASSERT_TRUE(command2.IsValid());
355   ASSERT_EQ(command2.GetClassOfDevice(), class_of_device);
356 
357   Address bdaddr({0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc});
358   controller_->SetEventFilterConnectionSetupAddress(bdaddr, AutoAcceptFlag::AUTO_ACCEPT_ON_ROLE_SWITCH_ENABLED);
359   packet = test_hci_layer_->GetCommand(OpCode::SET_EVENT_FILTER);
360   auto set_event_filter_view3 = SetEventFilterView::Create(packet);
361   auto set_event_filter_connection_setup_view = SetEventFilterConnectionSetupView::Create(set_event_filter_view3);
362   auto command3 = SetEventFilterConnectionSetupAddressView::Create(set_event_filter_connection_setup_view);
363   ASSERT_TRUE(command3.IsValid());
364   ASSERT_EQ(command3.GetAddress(), bdaddr);
365 }
366 
TEST_F(ControllerTest,send_host_buffer_size_command)367 TEST_F(ControllerTest, send_host_buffer_size_command) {
368   controller_->HostBufferSize(0xFF00, 0xF1, 0xFF02, 0xFF03);
369   auto packet = test_hci_layer_->GetCommand(OpCode::HOST_BUFFER_SIZE);
370   auto command = HostBufferSizeView::Create(packet);
371   ASSERT_TRUE(command.IsValid());
372   ASSERT_EQ(command.GetHostAclDataPacketLength(), 0xFF00);
373   ASSERT_EQ(command.GetHostSynchronousDataPacketLength(), 0xF1);
374   ASSERT_EQ(command.GetHostTotalNumAclDataPackets(), 0xFF02);
375   ASSERT_EQ(command.GetHostTotalNumSynchronousDataPackets(), 0xFF03);
376 }
377 
TEST_F(ControllerTest,send_le_set_event_mask_command)378 TEST_F(ControllerTest, send_le_set_event_mask_command) {
379   uint64_t new_le_event_mask = test_hci_layer_->event_mask - 1;
380   controller_->LeSetEventMask(new_le_event_mask);
381   // Send another command to make sure it was applied
382   controller_->Reset();
383   auto packet = test_hci_layer_->GetCommand(OpCode::RESET);
384   ASSERT_EQ(new_le_event_mask, test_hci_layer_->le_event_mask);
385 }
386 
TEST_F(ControllerTest,is_supported_test)387 TEST_F(ControllerTest, is_supported_test) {
388   ASSERT_TRUE(controller_->IsSupported(OpCode::INQUIRY));
389   ASSERT_TRUE(controller_->IsSupported(OpCode::REJECT_CONNECTION_REQUEST));
390   ASSERT_TRUE(controller_->IsSupported(OpCode::ACCEPT_CONNECTION_REQUEST));
391   ASSERT_FALSE(controller_->IsSupported(OpCode::LE_REMOVE_ADVERTISING_SET));
392   ASSERT_FALSE(controller_->IsSupported(OpCode::LE_CLEAR_ADVERTISING_SETS));
393   ASSERT_FALSE(controller_->IsSupported(OpCode::LE_SET_PERIODIC_ADVERTISING_PARAM));
394 }
395 
TEST_F(ControllerTest,feature_spec_version_055_test)396 TEST_F(ControllerTest, feature_spec_version_055_test) {
397   EXPECT_EQ(controller_->GetVendorCapabilities().version_supported_, 55);
398   EXPECT_TRUE(controller_->IsSupported(OpCode::LE_MULTI_ADVT));
399   EXPECT_FALSE(controller_->IsSupported(OpCode::CONTROLLER_DEBUG_INFO));
400   EXPECT_FALSE(controller_->IsSupported(OpCode::CONTROLLER_A2DP_OPCODE));
401   feature_spec_version = 95;
402 }
403 
TEST_F(ControllerTest,feature_spec_version_095_test)404 TEST_F(ControllerTest, feature_spec_version_095_test) {
405   EXPECT_EQ(controller_->GetVendorCapabilities().version_supported_, 95);
406   EXPECT_TRUE(controller_->IsSupported(OpCode::LE_MULTI_ADVT));
407   EXPECT_FALSE(controller_->IsSupported(OpCode::CONTROLLER_DEBUG_INFO));
408   EXPECT_FALSE(controller_->IsSupported(OpCode::CONTROLLER_A2DP_OPCODE));
409   feature_spec_version = 96;
410 }
411 
TEST_F(ControllerTest,feature_spec_version_096_test)412 TEST_F(ControllerTest, feature_spec_version_096_test) {
413   EXPECT_EQ(controller_->GetVendorCapabilities().version_supported_, 96);
414   EXPECT_TRUE(controller_->IsSupported(OpCode::LE_MULTI_ADVT));
415   EXPECT_FALSE(controller_->IsSupported(OpCode::CONTROLLER_DEBUG_INFO));
416   EXPECT_FALSE(controller_->IsSupported(OpCode::CONTROLLER_A2DP_OPCODE));
417   feature_spec_version = 98;
418 }
419 
TEST_F(ControllerTest,feature_spec_version_098_test)420 TEST_F(ControllerTest, feature_spec_version_098_test) {
421   EXPECT_EQ(controller_->GetVendorCapabilities().version_supported_, 98);
422   EXPECT_TRUE(controller_->IsSupported(OpCode::LE_MULTI_ADVT));
423   EXPECT_FALSE(controller_->IsSupported(OpCode::CONTROLLER_DEBUG_INFO));
424   EXPECT_TRUE(controller_->IsSupported(OpCode::CONTROLLER_A2DP_OPCODE));
425 }
426 
427 std::promise<void> credits1_set;
428 std::promise<void> credits2_set;
429 
CheckReceivedCredits(uint16_t handle,uint16_t credits)430 void CheckReceivedCredits(uint16_t handle, uint16_t credits) {
431   switch (handle) {
432     case (kHandle1):
433       ASSERT_EQ(kCredits1, credits);
434       credits1_set.set_value();
435       break;
436     case (kHandle2):
437       ASSERT_EQ(kCredits2, credits);
438       credits2_set.set_value();
439       break;
440     default:
441       ASSERT_LOG(false, "Unknown handle 0x%0hx with 0x%0hx credits", handle, credits);
442   }
443 }
444 
TEST_F(ControllerTest,aclCreditCallbacksTest)445 TEST_F(ControllerTest, aclCreditCallbacksTest) {
446   controller_->RegisterCompletedAclPacketsCallback(client_handler_->Bind(&CheckReceivedCredits));
447 
448   test_hci_layer_->IncomingCredit();
449 
450   credits1_set.get_future().wait();
451   credits2_set.get_future().wait();
452 }
453 
TEST_F(ControllerTest,aclCreditCallbackListenerUnregistered)454 TEST_F(ControllerTest, aclCreditCallbackListenerUnregistered) {
455   os::Thread thread("test_thread", os::Thread::Priority::NORMAL);
456   os::Handler handler(&thread);
457   controller_->RegisterCompletedAclPacketsCallback(handler.Bind(&CheckReceivedCredits));
458 
459   handler.Clear();
460   handler.WaitUntilStopped(std::chrono::milliseconds(100));
461   controller_->UnregisterCompletedAclPacketsCallback();
462 
463   test_hci_layer_->IncomingCredit();
464 }
465 }  // namespace
466 }  // namespace hci
467 }  // namespace bluetooth
468