xref: /aosp_15_r20/hardware/interfaces/bluetooth/aidl/vts/VtsHalBluetoothTargetTest.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1 /*
2  * Copyright (C) 2023 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 <VtsCoreUtil.h>
18 #include <aidl/Gtest.h>
19 #include <aidl/Vintf.h>
20 #include <aidl/android/hardware/bluetooth/BnBluetoothHciCallbacks.h>
21 #include <aidl/android/hardware/bluetooth/IBluetoothHci.h>
22 #include <aidl/android/hardware/bluetooth/IBluetoothHciCallbacks.h>
23 #include <aidl/android/hardware/bluetooth/Status.h>
24 #include <android-base/properties.h>
25 #include <android/binder_auto_utils.h>
26 #include <android/binder_manager.h>
27 #include <android/binder_process.h>
28 #include <binder/IServiceManager.h>
29 
30 #include <chrono>
31 #include <condition_variable>
32 #include <future>
33 #include <queue>
34 #include <thread>
35 #include <utility>
36 #include <vector>
37 
38 // TODO: Remove custom logging defines from PDL packets.
39 #undef LOG_INFO
40 #undef LOG_DEBUG
41 #undef LOG_TAG
42 #define LOG_TAG "VtsHalBluetooth"
43 #include "hci/hci_packets.h"
44 #include "packet/raw_builder.h"
45 
46 using aidl::android::hardware::bluetooth::IBluetoothHci;
47 using aidl::android::hardware::bluetooth::IBluetoothHciCallbacks;
48 using aidl::android::hardware::bluetooth::Status;
49 using ndk::ScopedAStatus;
50 using ndk::SpAIBinder;
51 
52 using ::bluetooth::hci::CommandBuilder;
53 using ::bluetooth::hci::CommandCompleteView;
54 using ::bluetooth::hci::CommandView;
55 using ::bluetooth::hci::ErrorCode;
56 using ::bluetooth::hci::EventView;
57 using ::bluetooth::hci::LeReadLocalSupportedFeaturesBuilder;
58 using ::bluetooth::hci::LeReadLocalSupportedFeaturesCompleteView;
59 using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsBuilder;
60 using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsCompleteView;
61 using ::bluetooth::hci::LeReadResolvingListSizeBuilder;
62 using ::bluetooth::hci::LeReadResolvingListSizeCompleteView;
63 using ::bluetooth::hci::LLFeaturesBits;
64 using ::bluetooth::hci::OpCode;
65 using ::bluetooth::hci::OpCodeText;
66 using ::bluetooth::hci::PacketView;
67 using ::bluetooth::hci::ReadLocalVersionInformationBuilder;
68 using ::bluetooth::hci::ReadLocalVersionInformationCompleteView;
69 
70 static constexpr uint8_t kMinLeAdvSetForBt5 = 10;
71 static constexpr uint8_t kMinLeAdvSetForBt5ForTv = 10;
72 static constexpr uint8_t kMinLeResolvingListForBt5 = 8;
73 
74 static constexpr size_t kNumHciCommandsBandwidth = 100;
75 static constexpr size_t kNumAclPacketsBandwidth = 100;
76 static constexpr std::chrono::milliseconds kWaitForInitTimeout(2000);
77 static constexpr std::chrono::milliseconds kWaitForHciEventTimeout(2000);
78 static constexpr std::chrono::milliseconds kWaitForAclDataTimeout(1000);
79 static constexpr std::chrono::milliseconds kInterfaceCloseDelayMs(200);
80 
81 // To discard Qualcomm ACL debugging
82 static constexpr uint16_t kAclHandleQcaDebugMessage = 0xedc;
83 
get_vsr_api_level()84 static int get_vsr_api_level() {
85   int vendor_api_level =
86       ::android::base::GetIntProperty("ro.vendor.api_level", -1);
87   if (vendor_api_level != -1) {
88     return vendor_api_level;
89   }
90 
91   // Android S and older devices do not define ro.vendor.api_level
92   vendor_api_level = ::android::base::GetIntProperty("ro.board.api_level", -1);
93   if (vendor_api_level == -1) {
94     vendor_api_level =
95         ::android::base::GetIntProperty("ro.board.first_api_level", -1);
96   }
97 
98   int product_api_level =
99       ::android::base::GetIntProperty("ro.product.first_api_level", -1);
100   if (product_api_level == -1) {
101     product_api_level =
102         ::android::base::GetIntProperty("ro.build.version.sdk", -1);
103     EXPECT_NE(product_api_level, -1) << "Could not find ro.build.version.sdk";
104   }
105 
106   // VSR API level is the minimum of vendor_api_level and product_api_level.
107   if (vendor_api_level == -1 || vendor_api_level > product_api_level) {
108     return product_api_level;
109   }
110   return vendor_api_level;
111 }
112 
isTv()113 static bool isTv() {
114   return testing::deviceSupportsFeature("android.software.leanback") ||
115          testing::deviceSupportsFeature("android.hardware.type.television");
116 }
117 
isHandheld()118 static bool isHandheld() {
119   return testing::deviceSupportsFeature("android.hardware.type.handheld");
120 }
121 
122 class ThroughputLogger {
123  public:
ThroughputLogger(std::string task)124   explicit ThroughputLogger(std::string task)
125       : total_bytes_(0),
126         task_(std::move(task)),
127         start_time_(std::chrono::steady_clock::now()) {}
128 
~ThroughputLogger()129   ~ThroughputLogger() {
130     if (total_bytes_ == 0) {
131       return;
132     }
133     std::chrono::duration<double> duration =
134         std::chrono::steady_clock::now() - start_time_;
135     double s = duration.count();
136     if (s == 0) {
137       return;
138     }
139     double rate_kb = (static_cast<double>(total_bytes_) / s) / 1024;
140     ALOGD("%s %.1f KB/s (%zu bytes in %.3fs)", task_.c_str(), rate_kb,
141           total_bytes_, s);
142   }
143 
setTotalBytes(size_t total_bytes)144   void setTotalBytes(size_t total_bytes) { total_bytes_ = total_bytes; }
145 
146  private:
147   size_t total_bytes_;
148   std::string task_;
149   std::chrono::steady_clock::time_point start_time_;
150 };
151 
152 // The main test class for Bluetooth HAL.
153 class BluetoothAidlTest : public ::testing::TestWithParam<std::string> {
154   std::chrono::time_point<std::chrono::system_clock>
155       time_after_initialize_complete;
156 
157  public:
SetUp()158   void SetUp() override {
159     // currently test passthrough mode only
160     hci = IBluetoothHci::fromBinder(
161         SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
162     ASSERT_NE(hci, nullptr);
163     ALOGI("%s: getService() for bluetooth hci is %s", __func__,
164           hci->isRemote() ? "remote" : "local");
165 
166     // Lambda function
167     auto on_binder_death = [](void* /*cookie*/) { FAIL(); };
168 
169     bluetooth_hci_death_recipient =
170         AIBinder_DeathRecipient_new(on_binder_death);
171     ASSERT_NE(bluetooth_hci_death_recipient, nullptr);
172     ASSERT_EQ(STATUS_OK,
173               AIBinder_linkToDeath(hci->asBinder().get(),
174                                    bluetooth_hci_death_recipient, nullptr));
175 
176     hci_cb = ndk::SharedRefBase::make<BluetoothHciCallbacks>(*this);
177     ASSERT_NE(hci_cb, nullptr);
178 
179     max_acl_data_packet_length = 0;
180     max_sco_data_packet_length = 0;
181     max_acl_data_packets = 0;
182     max_sco_data_packets = 0;
183 
184     event_cb_count = 0;
185     acl_cb_count = 0;
186     sco_cb_count = 0;
187     std::chrono::time_point<std::chrono::system_clock>
188         timeout_after_initialize =
189             std::chrono::system_clock::now() + kWaitForInitTimeout;
190 
191     ASSERT_TRUE(hci->initialize(hci_cb).isOk());
192     auto future = initialized_promise.get_future();
193     auto timeout_status = future.wait_for(kWaitForInitTimeout);
194     ASSERT_EQ(timeout_status, std::future_status::ready);
195     ASSERT_TRUE(future.get());
196     ASSERT_GE(timeout_after_initialize, time_after_initialize_complete);
197   }
198 
TearDown()199   void TearDown() override {
200     ALOGI("TearDown");
201     // Should not be checked in production code
202     ASSERT_TRUE(hci->close().isOk());
203     std::this_thread::sleep_for(kInterfaceCloseDelayMs);
204     handle_no_ops();
205     discard_qca_debugging();
206     EXPECT_EQ(static_cast<size_t>(0), event_queue.size());
207     EXPECT_EQ(static_cast<size_t>(0), sco_queue.size());
208     EXPECT_EQ(static_cast<size_t>(0), acl_queue.size());
209     EXPECT_EQ(static_cast<size_t>(0), iso_queue.size());
210   }
211 
212   void setBufferSizes();
213   void setSynchronousFlowControlEnable();
214 
215   // Functions called from within tests in loopback mode
216   void sendAndCheckHci(int num_packets);
217   void sendAndCheckAcl(int num_packets, size_t size, uint16_t handle);
218 
219   // Helper functions to try to get a handle on verbosity
220   void enterLoopbackMode();
221   void handle_no_ops();
222   void discard_qca_debugging();
223   void wait_for_event(bool timeout_is_error);
224   void wait_for_command_complete_event(OpCode opCode,
225                                        std::vector<uint8_t>& complete_event);
226   // Wait until a command complete is received.
227   // Command complete will be consumed after this method
228   void wait_and_validate_command_complete_event(OpCode opCode);
229   int wait_for_completed_packets_event(uint16_t handle);
230   void send_and_wait_for_cmd_complete(std::unique_ptr<CommandBuilder> cmd,
231                                       std::vector<uint8_t>& cmd_complete);
232   void reassemble_sco_loopback_pkt(std::vector<uint8_t>& scoPackets,
233     size_t size);
234 
235   // A simple test implementation of BluetoothHciCallbacks.
236   class BluetoothHciCallbacks
237       : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
238     BluetoothAidlTest& parent_;
239 
240    public:
BluetoothHciCallbacks(BluetoothAidlTest & parent)241     explicit BluetoothHciCallbacks(BluetoothAidlTest& parent)
242         : parent_(parent){};
243 
244     ~BluetoothHciCallbacks() override = default;
245 
initializationComplete(Status status)246     ndk::ScopedAStatus initializationComplete(Status status) override {
247       if (status == Status::SUCCESS) {
248         parent_.time_after_initialize_complete =
249             std::chrono::system_clock::now();
250       }
251       parent_.initialized_promise.set_value(status == Status::SUCCESS);
252       ALOGV("%s (status = %d)", __func__, static_cast<int>(status));
253       return ScopedAStatus::ok();
254     };
255 
hciEventReceived(const std::vector<uint8_t> & event)256     ndk::ScopedAStatus hciEventReceived(
257         const std::vector<uint8_t>& event) override {
258       parent_.event_cb_count++;
259       parent_.event_queue.push(event);
260       ALOGI("Event received (length = %d)", static_cast<int>(event.size()));
261       return ScopedAStatus::ok();
262     };
263 
aclDataReceived(const std::vector<uint8_t> & data)264     ndk::ScopedAStatus aclDataReceived(
265         const std::vector<uint8_t>& data) override {
266       parent_.acl_cb_count++;
267       parent_.acl_queue.push(data);
268       return ScopedAStatus::ok();
269     };
270 
scoDataReceived(const std::vector<uint8_t> & data)271     ndk::ScopedAStatus scoDataReceived(
272         const std::vector<uint8_t>& data) override {
273       parent_.sco_cb_count++;
274       parent_.sco_queue.push(data);
275       return ScopedAStatus::ok();
276     };
277 
isoDataReceived(const std::vector<uint8_t> & data)278     ndk::ScopedAStatus isoDataReceived(
279         const std::vector<uint8_t>& data) override {
280       parent_.iso_cb_count++;
281       parent_.iso_queue.push(data);
282       return ScopedAStatus::ok();
283     };
284   };
285 
286   template <class T>
287   class WaitQueue {
288    public:
289     WaitQueue() = default;
290     ;
291 
292     virtual ~WaitQueue() = default;
293 
empty() const294     bool empty() const {
295       std::lock_guard<std::mutex> lock(m_);
296       return q_.empty();
297     };
298 
size() const299     size_t size() const {
300       std::lock_guard<std::mutex> lock(m_);
301       return q_.size();
302     };
303 
push(const T & v)304     void push(const T& v) {
305       std::lock_guard<std::mutex> lock(m_);
306       q_.push(v);
307       ready_.notify_one();
308     };
309 
pop(T & v)310     bool pop(T& v) {
311       std::lock_guard<std::mutex> lock(m_);
312       if (q_.empty()) {
313         return false;
314       }
315       v = std::move(q_.front());
316       q_.pop();
317       return true;
318     };
319 
pop()320     void pop() {
321       std::lock_guard<std::mutex> lock(m_);
322       if (q_.empty()) {
323         return;
324       }
325       q_.pop();
326     };
327 
front(T & v)328     bool front(T& v) {
329       std::lock_guard<std::mutex> lock(m_);
330       if (q_.empty()) {
331         return false;
332       }
333       v = q_.front();
334       return true;
335     };
336 
wait()337     void wait() {
338       std::unique_lock<std::mutex> lock(m_);
339       while (q_.empty()) {
340         ready_.wait(lock);
341       }
342     };
343 
waitWithTimeout(std::chrono::milliseconds timeout)344     bool waitWithTimeout(std::chrono::milliseconds timeout) {
345       std::unique_lock<std::mutex> lock(m_);
346       while (q_.empty()) {
347         if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
348           return false;
349         }
350       }
351       return true;
352     };
353 
tryPopWithTimeout(T & v,std::chrono::milliseconds timeout)354     bool tryPopWithTimeout(T& v, std::chrono::milliseconds timeout) {
355       std::unique_lock<std::mutex> lock(m_);
356       while (q_.empty()) {
357         if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
358           return false;
359         }
360       }
361       v = std::move(q_.front());
362       q_.pop();
363       return true;
364     };
365 
366    private:
367     mutable std::mutex m_;
368     std::queue<T> q_;
369     std::condition_variable_any ready_;
370   };
371 
372   std::shared_ptr<IBluetoothHci> hci;
373   std::shared_ptr<BluetoothHciCallbacks> hci_cb;
374   AIBinder_DeathRecipient* bluetooth_hci_death_recipient{};
375   WaitQueue<std::vector<uint8_t>> event_queue;
376   WaitQueue<std::vector<uint8_t>> acl_queue;
377   WaitQueue<std::vector<uint8_t>> sco_queue;
378   WaitQueue<std::vector<uint8_t>> iso_queue;
379 
380   std::promise<bool> initialized_promise;
381   int event_cb_count{};
382   int sco_cb_count{};
383   int acl_cb_count{};
384   int iso_cb_count{};
385 
386   int max_acl_data_packet_length{};
387   int max_sco_data_packet_length{};
388   int max_acl_data_packets{};
389   int max_sco_data_packets{};
390 
391   std::vector<uint16_t> sco_connection_handles;
392   std::vector<uint16_t> acl_connection_handles;
393 };
394 
395 // Discard NO-OPs from the event queue.
handle_no_ops()396 void BluetoothAidlTest::handle_no_ops() {
397   while (!event_queue.empty()) {
398     std::vector<uint8_t> event;
399     event_queue.front(event);
400     auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
401         ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
402             std::make_shared<std::vector<uint8_t>>(event))));
403     auto status_view = ::bluetooth::hci::CommandCompleteView::Create(
404         ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
405             std::make_shared<std::vector<uint8_t>>(event))));
406     bool is_complete_no_op =
407         complete_view.IsValid() &&
408         complete_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
409     bool is_status_no_op =
410         status_view.IsValid() &&
411         status_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
412     if (is_complete_no_op || is_status_no_op) {
413       event_queue.pop();
414     } else {
415       break;
416     }
417   }
418 }
419 
420 // Discard Qualcomm ACL debugging
discard_qca_debugging()421 void BluetoothAidlTest::discard_qca_debugging() {
422   while (!acl_queue.empty()) {
423     std::vector<uint8_t> acl_packet;
424     acl_queue.front(acl_packet);
425     auto acl_view =
426         ::bluetooth::hci::AclView::Create(::bluetooth::hci::PacketView<true>(
427             std::make_shared<std::vector<uint8_t>>(acl_packet)));
428     EXPECT_TRUE(acl_view.IsValid());
429     if (acl_view.GetHandle() == kAclHandleQcaDebugMessage) {
430       acl_queue.pop();
431     } else {
432       break;
433     }
434   }
435 }
436 
437 // Receive an event, discarding NO-OPs.
wait_for_event(bool timeout_is_error=true)438 void BluetoothAidlTest::wait_for_event(bool timeout_is_error = true) {
439   // Wait until we get something that's not a no-op.
440   while (true) {
441     bool event_ready = event_queue.waitWithTimeout(kWaitForHciEventTimeout);
442     ASSERT_TRUE(event_ready || !timeout_is_error);
443     if (event_queue.empty()) {
444       // waitWithTimeout timed out
445       return;
446     }
447     handle_no_ops();
448     if (!event_queue.empty()) {
449       // There's an event in the queue that's not a no-op.
450       return;
451     }
452   }
453 }
454 
wait_for_command_complete_event(OpCode opCode,std::vector<uint8_t> & complete_event)455 void BluetoothAidlTest::wait_for_command_complete_event(
456     OpCode opCode, std::vector<uint8_t>& complete_event) {
457   ASSERT_NO_FATAL_FAILURE(wait_for_event());
458   ASSERT_FALSE(event_queue.empty());
459   ASSERT_TRUE(event_queue.pop(complete_event));
460   auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
461       ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
462           std::make_shared<std::vector<uint8_t>>(complete_event))));
463   ASSERT_TRUE(complete_view.IsValid());
464   ASSERT_EQ(complete_view.GetCommandOpCode(), opCode);
465   ASSERT_EQ(complete_view.GetPayload()[0],
466             static_cast<uint8_t>(::bluetooth::hci::ErrorCode::SUCCESS));
467 }
468 
wait_and_validate_command_complete_event(::bluetooth::hci::OpCode opCode)469 void BluetoothAidlTest::wait_and_validate_command_complete_event(
470     ::bluetooth::hci::OpCode opCode) {
471   std::vector<uint8_t> complete_event;
472   ASSERT_NO_FATAL_FAILURE(
473       wait_for_command_complete_event(opCode, complete_event));
474 }
475 
476 // Send the command to read the controller's buffer sizes.
setBufferSizes()477 void BluetoothAidlTest::setBufferSizes() {
478   std::vector<uint8_t> cmd;
479   ::bluetooth::packet::BitInserter bi{cmd};
480   ::bluetooth::hci::ReadBufferSizeBuilder::Create()->Serialize(bi);
481   hci->sendHciCommand(cmd);
482 
483   ASSERT_NO_FATAL_FAILURE(wait_for_event());
484   std::vector<uint8_t> event;
485   ASSERT_TRUE(event_queue.pop(event));
486   auto complete_view = ::bluetooth::hci::ReadBufferSizeCompleteView::Create(
487       ::bluetooth::hci::CommandCompleteView::Create(
488           ::bluetooth::hci::EventView::Create(
489               ::bluetooth::hci::PacketView<true>(
490                   std::make_shared<std::vector<uint8_t>>(event)))));
491 
492   ASSERT_TRUE(complete_view.IsValid());
493   ASSERT_EQ(complete_view.GetStatus(), ::bluetooth::hci::ErrorCode::SUCCESS);
494   max_acl_data_packet_length = complete_view.GetAclDataPacketLength();
495   max_sco_data_packet_length = complete_view.GetSynchronousDataPacketLength();
496   max_acl_data_packets = complete_view.GetTotalNumAclDataPackets();
497   max_sco_data_packets = complete_view.GetTotalNumSynchronousDataPackets();
498 
499   ALOGD("%s: ACL max %d num %d SCO max %d num %d", __func__,
500         static_cast<int>(max_acl_data_packet_length),
501         static_cast<int>(max_acl_data_packets),
502         static_cast<int>(max_sco_data_packet_length),
503         static_cast<int>(max_sco_data_packets));
504 }
505 
506 // Enable flow control packets for SCO
setSynchronousFlowControlEnable()507 void BluetoothAidlTest::setSynchronousFlowControlEnable() {
508   std::vector<uint8_t> cmd;
509   ::bluetooth::packet::BitInserter bi{cmd};
510   ::bluetooth::hci::WriteSynchronousFlowControlEnableBuilder::Create(
511       ::bluetooth::hci::Enable::ENABLED)
512       ->Serialize(bi);
513   hci->sendHciCommand(cmd);
514 
515   wait_and_validate_command_complete_event(
516       ::bluetooth::hci::OpCode::WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE);
517 }
518 
519 // Send an HCI command (in Loopback mode) and check the response.
sendAndCheckHci(int num_packets)520 void BluetoothAidlTest::sendAndCheckHci(int num_packets) {
521   ThroughputLogger logger{__func__};
522   size_t command_size = 0;
523   char new_name[] = "John Jacob Jingleheimer Schmidt ___________________";
524   size_t new_name_length = strlen(new_name);
525   for (int n = 0; n < num_packets; n++) {
526     // The name to set is new_name
527     std::array<uint8_t, 248> name_array{};
528     for (size_t i = 0; i < new_name_length; i++) {
529       name_array[i] = new_name[i];
530     }
531     // And the packet number
532     char number[11] = "0000000000";
533     snprintf(number, sizeof(number), "%010d", static_cast<int>(n));
534     for (size_t i = new_name_length; i < new_name_length + sizeof(number) - 1;
535          i++) {
536       name_array[new_name_length + i] = number[i];
537     }
538     std::vector<uint8_t> write_name;
539     ::bluetooth::packet::BitInserter bi{write_name};
540     ::bluetooth::hci::WriteLocalNameBuilder::Create(name_array)->Serialize(bi);
541     hci->sendHciCommand(write_name);
542 
543     // Check the loopback of the HCI packet
544     ASSERT_NO_FATAL_FAILURE(wait_for_event());
545 
546     std::vector<uint8_t> event;
547     ASSERT_TRUE(event_queue.pop(event));
548     auto event_view = ::bluetooth::hci::LoopbackCommandView::Create(
549         ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
550             std::make_shared<std::vector<uint8_t>>(event))));
551     ASSERT_TRUE(event_view.IsValid());
552     std::vector<uint8_t> looped_back_command{event_view.GetPayload().begin(),
553                                              event_view.GetPayload().end()};
554     ASSERT_EQ(looped_back_command, write_name);
555 
556     if (n == num_packets - 1) {
557       command_size = write_name.size();
558     }
559   }
560   logger.setTotalBytes(command_size * num_packets * 2);
561 }
562 
563 // Send an ACL data packet (in Loopback mode) and check the response.
sendAndCheckAcl(int num_packets,size_t size,uint16_t handle)564 void BluetoothAidlTest::sendAndCheckAcl(int num_packets, size_t size,
565                                         uint16_t handle) {
566   ThroughputLogger logger{__func__};
567   for (int n = 0; n < num_packets; n++) {
568     // Send an ACL packet with counting data
569     auto payload = std::make_unique<::bluetooth::packet::RawBuilder>();
570     for (size_t i = 0; i < size; i++) {
571       payload->AddOctets1(static_cast<uint8_t>(i + n));
572     }
573     std::vector<uint8_t> acl_packet;
574     ::bluetooth::packet::BitInserter bi{acl_packet};
575     ::bluetooth::hci::AclBuilder::Create(
576         handle,
577         ::bluetooth::hci::PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE,
578         ::bluetooth::hci::BroadcastFlag::POINT_TO_POINT, std::move(payload))
579         ->Serialize(bi);
580     hci->sendAclData(acl_packet);
581 
582     std::vector<uint8_t> acl_loopback;
583     // Check the loopback of the ACL packet
584     ASSERT_TRUE(
585         acl_queue.tryPopWithTimeout(acl_loopback, kWaitForAclDataTimeout));
586 
587     ASSERT_EQ(acl_packet, acl_loopback);
588   }
589   logger.setTotalBytes(num_packets * size * 2);
590 }
591 
592 // Return the number of completed packets reported by the controller.
wait_for_completed_packets_event(uint16_t handle)593 int BluetoothAidlTest::wait_for_completed_packets_event(uint16_t handle) {
594   int packets_processed = 0;
595   while (true) {
596     // There should be at least one event.
597     wait_for_event(packets_processed == 0);
598     if (event_queue.empty()) {
599       if (packets_processed == 0) {
600         ALOGW("%s: waitForBluetoothCallback timed out.", __func__);
601       }
602       return packets_processed;
603     }
604     std::vector<uint8_t> event;
605     EXPECT_TRUE(event_queue.pop(event));
606     auto event_view = ::bluetooth::hci::NumberOfCompletedPacketsView::Create(
607         ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
608             std::make_shared<std::vector<uint8_t>>(event))));
609     if (!event_view.IsValid()) {
610       ADD_FAILURE();
611       return packets_processed;
612     }
613     auto completed_packets = event_view.GetCompletedPackets();
614     for (const auto& entry : completed_packets) {
615       EXPECT_EQ(handle, entry.connection_handle_);
616       packets_processed += entry.host_num_of_completed_packets_;
617     }
618   }
619   return packets_processed;
620 }
621 
622 // Send local loopback command and initialize SCO and ACL handles.
enterLoopbackMode()623 void BluetoothAidlTest::enterLoopbackMode() {
624   std::vector<uint8_t> cmd;
625   ::bluetooth::packet::BitInserter bi{cmd};
626   ::bluetooth::hci::WriteLoopbackModeBuilder::Create(
627       bluetooth::hci::LoopbackMode::ENABLE_LOCAL)
628       ->Serialize(bi);
629   hci->sendHciCommand(cmd);
630 
631   // Receive connection complete events with data channels
632   int connection_event_count = 0;
633   bool command_complete_received = false;
634   while (true) {
635     wait_for_event(false);
636     if (event_queue.empty()) {
637       // Fail if there was no event received or no connections completed.
638       ASSERT_TRUE(command_complete_received);
639       ASSERT_LT(0, connection_event_count);
640       return;
641     }
642     std::vector<uint8_t> event;
643     ASSERT_TRUE(event_queue.pop(event));
644     auto event_view =
645         ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
646             std::make_shared<std::vector<uint8_t>>(event)));
647     ASSERT_TRUE(event_view.IsValid());
648 
649     if (event_view.GetEventCode() ==
650         ::bluetooth::hci::EventCode::CONNECTION_COMPLETE) {
651       auto complete_view =
652           ::bluetooth::hci::ConnectionCompleteView::Create(event_view);
653       ASSERT_TRUE(complete_view.IsValid());
654       switch (complete_view.GetLinkType()) {
655         case ::bluetooth::hci::LinkType::ACL:
656           acl_connection_handles.push_back(complete_view.GetConnectionHandle());
657           break;
658         case ::bluetooth::hci::LinkType::SCO:
659           sco_connection_handles.push_back(complete_view.GetConnectionHandle());
660           break;
661         default:
662           ASSERT_EQ(complete_view.GetLinkType(),
663                     ::bluetooth::hci::LinkType::ACL);
664       }
665       connection_event_count++;
666     } else {
667       auto command_complete_view =
668           ::bluetooth::hci::WriteLoopbackModeCompleteView::Create(
669               ::bluetooth::hci::CommandCompleteView::Create(event_view));
670       ASSERT_TRUE(command_complete_view.IsValid());
671       ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
672                 command_complete_view.GetStatus());
673       command_complete_received = true;
674     }
675   }
676 }
677 
send_and_wait_for_cmd_complete(std::unique_ptr<CommandBuilder> cmd,std::vector<uint8_t> & cmd_complete)678 void BluetoothAidlTest::send_and_wait_for_cmd_complete(
679     std::unique_ptr<CommandBuilder> cmd, std::vector<uint8_t>& cmd_complete) {
680   std::vector<uint8_t> cmd_bytes = cmd->SerializeToBytes();
681   hci->sendHciCommand(cmd_bytes);
682 
683   auto view = CommandView::Create(
684       PacketView<true>(std::make_shared<std::vector<uint8_t>>(cmd_bytes)));
685   ASSERT_TRUE(view.IsValid());
686   ALOGI("Waiting for %s[0x%x]", OpCodeText(view.GetOpCode()).c_str(),
687         static_cast<int>(view.GetOpCode()));
688   ASSERT_NO_FATAL_FAILURE(
689       wait_for_command_complete_event(view.GetOpCode(), cmd_complete));
690 }
691 
692 // Empty test: Initialize()/Close() are called in SetUp()/TearDown().
TEST_P(BluetoothAidlTest,InitializeAndClose)693 TEST_P(BluetoothAidlTest, InitializeAndClose) {}
694 
695 // Send an HCI Reset with sendHciCommand and wait for a command complete event.
TEST_P(BluetoothAidlTest,HciReset)696 TEST_P(BluetoothAidlTest, HciReset) {
697   std::vector<uint8_t> reset;
698   ::bluetooth::packet::BitInserter bi{reset};
699   ::bluetooth::hci::ResetBuilder::Create()->Serialize(bi);
700   hci->sendHciCommand(reset);
701 
702   wait_and_validate_command_complete_event(::bluetooth::hci::OpCode::RESET);
703 }
704 
705 // Read and check the HCI version of the controller.
TEST_P(BluetoothAidlTest,HciVersionTest)706 TEST_P(BluetoothAidlTest, HciVersionTest) {
707   std::vector<uint8_t> cmd;
708   ::bluetooth::packet::BitInserter bi{cmd};
709   ::bluetooth::hci::ReadLocalVersionInformationBuilder::Create()->Serialize(bi);
710   hci->sendHciCommand(cmd);
711 
712   ASSERT_NO_FATAL_FAILURE(wait_for_event());
713 
714   std::vector<uint8_t> event;
715   ASSERT_TRUE(event_queue.pop(event));
716   auto complete_view =
717       ::bluetooth::hci::ReadLocalVersionInformationCompleteView::Create(
718           ::bluetooth::hci::CommandCompleteView::Create(
719               ::bluetooth::hci::EventView::Create(
720                   ::bluetooth::hci::PacketView<true>(
721                       std::make_shared<std::vector<uint8_t>>(event)))));
722   ASSERT_TRUE(complete_view.IsValid());
723   ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, complete_view.GetStatus());
724   auto version = complete_view.GetLocalVersionInformation();
725   ASSERT_LE(::bluetooth::hci::HciVersion::V_3_0, version.hci_version_);
726   ASSERT_LE(::bluetooth::hci::LmpVersion::V_3_0, version.lmp_version_);
727 }
728 
729 // Send an unknown HCI command and wait for the error message.
TEST_P(BluetoothAidlTest,HciUnknownCommand)730 TEST_P(BluetoothAidlTest, HciUnknownCommand) {
731   std::vector<uint8_t> cmd;
732   ::bluetooth::packet::BitInserter bi{cmd};
733   ::bluetooth::hci::CommandBuilder::Create(
734       static_cast<::bluetooth::hci::OpCode>(0x3cff),
735       std::make_unique<::bluetooth::packet::RawBuilder>())
736       ->Serialize(bi);
737   hci->sendHciCommand(cmd);
738 
739   ASSERT_NO_FATAL_FAILURE(wait_for_event());
740 
741   std::vector<uint8_t> event;
742   ASSERT_TRUE(event_queue.pop(event));
743   auto event_view =
744       ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
745           std::make_shared<std::vector<uint8_t>>(event)));
746   ASSERT_TRUE(event_view.IsValid());
747 
748   switch (event_view.GetEventCode()) {
749     case ::bluetooth::hci::EventCode::COMMAND_COMPLETE: {
750       auto command_complete =
751           ::bluetooth::hci::CommandCompleteView::Create(event_view);
752       ASSERT_TRUE(command_complete.IsValid());
753       ASSERT_EQ(command_complete.GetPayload()[0],
754                 static_cast<uint8_t>(
755                     ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND));
756     } break;
757     case ::bluetooth::hci::EventCode::COMMAND_STATUS: {
758       auto command_status =
759           ::bluetooth::hci::CommandStatusView::Create(event_view);
760       ASSERT_TRUE(command_status.IsValid());
761       ASSERT_EQ(command_status.GetStatus(),
762                 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND);
763     } break;
764     default:
765       ADD_FAILURE();
766   }
767 }
768 
769 // Enter loopback mode, but don't send any packets.
TEST_P(BluetoothAidlTest,WriteLoopbackMode)770 TEST_P(BluetoothAidlTest, WriteLoopbackMode) { enterLoopbackMode(); }
771 
772 // Enter loopback mode and send a single command.
TEST_P(BluetoothAidlTest,LoopbackModeSingleCommand)773 TEST_P(BluetoothAidlTest, LoopbackModeSingleCommand) {
774   setBufferSizes();
775 
776   enterLoopbackMode();
777 
778   sendAndCheckHci(1);
779 }
780 
781 // Enter loopback mode and send a single ACL packet.
TEST_P(BluetoothAidlTest,LoopbackModeSingleAcl)782 TEST_P(BluetoothAidlTest, LoopbackModeSingleAcl) {
783   setBufferSizes();
784 
785   enterLoopbackMode();
786 
787   if (!acl_connection_handles.empty()) {
788     ASSERT_LT(0, max_acl_data_packet_length);
789     sendAndCheckAcl(1, max_acl_data_packet_length - 1,
790                     acl_connection_handles[0]);
791     int acl_packets_sent = 1;
792     int completed_packets =
793         wait_for_completed_packets_event(acl_connection_handles[0]);
794     if (acl_packets_sent != completed_packets) {
795       ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
796             acl_packets_sent, completed_packets);
797     }
798   }
799   ASSERT_GE(acl_cb_count, 1);
800 }
801 
802 // Enter loopback mode and send command packets for bandwidth measurements.
TEST_P(BluetoothAidlTest,LoopbackModeCommandBandwidth)803 TEST_P(BluetoothAidlTest, LoopbackModeCommandBandwidth) {
804   setBufferSizes();
805 
806   enterLoopbackMode();
807 
808   sendAndCheckHci(kNumHciCommandsBandwidth);
809 }
810 
811 // Enter loopback mode and send packets for ACL bandwidth measurements.
TEST_P(BluetoothAidlTest,LoopbackModeAclBandwidth)812 TEST_P(BluetoothAidlTest, LoopbackModeAclBandwidth) {
813   setBufferSizes();
814 
815   enterLoopbackMode();
816 
817   if (!acl_connection_handles.empty()) {
818     ASSERT_LT(0, max_acl_data_packet_length);
819     sendAndCheckAcl(kNumAclPacketsBandwidth, max_acl_data_packet_length - 1,
820                     acl_connection_handles[0]);
821     int acl_packets_sent = kNumAclPacketsBandwidth;
822     int completed_packets =
823         wait_for_completed_packets_event(acl_connection_handles[0]);
824     if (acl_packets_sent != completed_packets) {
825       ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
826             acl_packets_sent, completed_packets);
827     }
828   }
829 }
830 
831 // Set all bits in the event mask
TEST_P(BluetoothAidlTest,SetEventMask)832 TEST_P(BluetoothAidlTest, SetEventMask) {
833   std::vector<uint8_t> cmd;
834   ::bluetooth::packet::BitInserter bi{cmd};
835   uint64_t full_mask = UINT64_MAX;
836   ::bluetooth::hci::SetEventMaskBuilder::Create(full_mask)->Serialize(bi);
837   hci->sendHciCommand(cmd);
838   wait_and_validate_command_complete_event(
839       ::bluetooth::hci::OpCode::SET_EVENT_MASK);
840 }
841 
842 // Set all bits in the LE event mask
TEST_P(BluetoothAidlTest,SetLeEventMask)843 TEST_P(BluetoothAidlTest, SetLeEventMask) {
844   std::vector<uint8_t> cmd;
845   ::bluetooth::packet::BitInserter bi{cmd};
846   uint64_t full_mask = UINT64_MAX;
847   ::bluetooth::hci::LeSetEventMaskBuilder::Create(full_mask)->Serialize(bi);
848   hci->sendHciCommand(cmd);
849   wait_and_validate_command_complete_event(
850       ::bluetooth::hci::OpCode::LE_SET_EVENT_MASK);
851 }
852 
853 // Call initialize twice, second one should fail.
TEST_P(BluetoothAidlTest,CallInitializeTwice)854 TEST_P(BluetoothAidlTest, CallInitializeTwice) {
855   class SecondCb
856       : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
857    public:
858     ndk::ScopedAStatus initializationComplete(Status status) override {
859       EXPECT_EQ(status, Status::ALREADY_INITIALIZED);
860       init_promise.set_value();
861       return ScopedAStatus::ok();
862     };
863 
864     ndk::ScopedAStatus hciEventReceived(
865         const std::vector<uint8_t>& /*event*/) override {
866       ADD_FAILURE();
867       return ScopedAStatus::ok();
868     };
869 
870     ndk::ScopedAStatus aclDataReceived(
871         const std::vector<uint8_t>& /*data*/) override {
872       ADD_FAILURE();
873       return ScopedAStatus::ok();
874     };
875 
876     ndk::ScopedAStatus scoDataReceived(
877         const std::vector<uint8_t>& /*data*/) override {
878       ADD_FAILURE();
879       return ScopedAStatus::ok();
880     };
881 
882     ndk::ScopedAStatus isoDataReceived(
883         const std::vector<uint8_t>& /*data*/) override {
884       ADD_FAILURE();
885       return ScopedAStatus::ok();
886     };
887     std::promise<void> init_promise;
888   };
889 
890   std::shared_ptr<SecondCb> second_cb = ndk::SharedRefBase::make<SecondCb>();
891   ASSERT_NE(second_cb, nullptr);
892 
893   auto future = second_cb->init_promise.get_future();
894   ASSERT_TRUE(hci->initialize(second_cb).isOk());
895   auto status = future.wait_for(std::chrono::seconds(1));
896   ASSERT_EQ(status, std::future_status::ready);
897 }
898 
899 // @VsrTest = 5.3.14-001
900 // @VsrTest = 5.3.14-002
901 // @VsrTest = 5.3.14-004
TEST_P(BluetoothAidlTest,Vsr_Bluetooth5Requirements)902 TEST_P(BluetoothAidlTest, Vsr_Bluetooth5Requirements) {
903   int api_level = get_vsr_api_level();
904   if (api_level < __ANDROID_API_U__) {
905     GTEST_SKIP() << "API level is lower than 34";
906     return;
907   }
908 
909   std::vector<uint8_t> version_event;
910   send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
911                                  version_event);
912   auto version_view = ReadLocalVersionInformationCompleteView::Create(
913       CommandCompleteView::Create(EventView::Create(PacketView<true>(
914           std::make_shared<std::vector<uint8_t>>(version_event)))));
915   ASSERT_TRUE(version_view.IsValid());
916   ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
917   auto version = version_view.GetLocalVersionInformation();
918 
919   if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) {
920     GTEST_SKIP() << "Bluetooth version is lower than 5.0";
921     return;
922   }
923 
924   // When HCI version is 5.0, LMP version must also be at least 5.0
925   ASSERT_GE(static_cast<int>(version.lmp_version_),
926             static_cast<int>(version.hci_version_));
927 
928   std::vector<uint8_t> le_features_event;
929   send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(),
930                                  le_features_event);
931   auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create(
932       CommandCompleteView::Create(EventView::Create(PacketView<true>(
933           std::make_shared<std::vector<uint8_t>>(le_features_event)))));
934   ASSERT_TRUE(le_features_view.IsValid());
935   ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus());
936 
937   // CHIPSETs that set ro.board.api_level to 34 and report 5.0 or higher for
938   // the Bluetooth version through the IBluetoothHci HAL:
939   //
940   // [VSR-5.3.14-001] Must return TRUE for
941   //   - LE 2M PHY
942   //   - LE Coded PHY
943   //   - LE Advertising Extension
944   //   - LE Periodic Advertising
945   //   - LE Link Layer Privacy
946   auto le_features = le_features_view.GetLeFeatures();
947   ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LL_PRIVACY));
948   ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LE_2M_PHY));
949   ASSERT_TRUE(le_features &
950               static_cast<uint64_t>(LLFeaturesBits::LE_CODED_PHY));
951   ASSERT_TRUE(le_features &
952               static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING));
953   ASSERT_TRUE(le_features &
954               static_cast<uint64_t>(LLFeaturesBits::LE_PERIODIC_ADVERTISING));
955 
956   std::vector<uint8_t> num_adv_set_event;
957   send_and_wait_for_cmd_complete(
958       LeReadNumberOfSupportedAdvertisingSetsBuilder::Create(),
959       num_adv_set_event);
960   auto num_adv_set_view =
961       LeReadNumberOfSupportedAdvertisingSetsCompleteView::Create(
962           CommandCompleteView::Create(EventView::Create(PacketView<true>(
963               std::make_shared<std::vector<uint8_t>>(num_adv_set_event)))));
964   ASSERT_TRUE(num_adv_set_view.IsValid());
965   ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, num_adv_set_view.GetStatus());
966   auto num_adv_set = num_adv_set_view.GetNumberSupportedAdvertisingSets();
967 
968   // CHIPSETs that set ro.board.api_level to 34 and report 5.0 or higher for
969   // the Bluetooth version through the IBluetoothHci HAL:
970   //
971   // [VSR-5.3.14-002] MUST support at least 10 advertising sets.
972   if (isTv() && get_vsr_api_level() == __ANDROID_API_U__) {
973     ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5ForTv);
974   } else {
975     ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5);
976   }
977 
978   std::vector<uint8_t> num_resolving_list_event;
979   send_and_wait_for_cmd_complete(LeReadResolvingListSizeBuilder::Create(),
980                                  num_resolving_list_event);
981   auto num_resolving_list_view = LeReadResolvingListSizeCompleteView::Create(
982       CommandCompleteView::Create(EventView::Create(PacketView<true>(
983           std::make_shared<std::vector<uint8_t>>(num_resolving_list_event)))));
984   ASSERT_TRUE(num_resolving_list_view.IsValid());
985   ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
986             num_resolving_list_view.GetStatus());
987   auto num_resolving_list = num_resolving_list_view.GetResolvingListSize();
988 
989   // CHIPSETs that set ro.board.api_level to 34 and report 5.0 or higher for
990   // the Bluetooth version through the IBluetoothHci HAL:
991   //
992   // [VSR-5.3.14-004] MUST support a resolving list size of at least 8 entries.
993   ASSERT_GE(num_resolving_list, kMinLeResolvingListForBt5);
994 }
995 
996 /**
997  * VSR-5.3.14-007 MUST support Bluetooth 4.2 and Bluetooth LE Data Length Extension.
998  * VSR-5.3.14-008 MUST support Bluetooth Low Energy (BLE).
999  */
1000 // @VsrTest = 5.3.14-007
1001 // @VsrTest = 5.3.14-008
TEST_P(BluetoothAidlTest,Vsr_Bluetooth4_2Requirements)1002 TEST_P(BluetoothAidlTest, Vsr_Bluetooth4_2Requirements) {
1003   // test only applies to handheld devices
1004   if (!isHandheld()) {
1005     return;
1006   }
1007 
1008   std::vector<uint8_t> version_event;
1009   send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
1010                                  version_event);
1011   auto version_view = ReadLocalVersionInformationCompleteView::Create(
1012       CommandCompleteView::Create(EventView::Create(PacketView<true>(
1013           std::make_shared<std::vector<uint8_t>>(version_event)))));
1014   ASSERT_TRUE(version_view.IsValid());
1015   ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
1016   auto version = version_view.GetLocalVersionInformation();
1017   // Starting with Android 15, Fails when HCI version is lower than 4.2.
1018   ASSERT_GE(static_cast<int>(version.hci_version_),
1019     static_cast<int>(::bluetooth::hci::HciVersion::V_4_2));
1020   ASSERT_GE(static_cast<int>(version.lmp_version_),
1021     static_cast<int>(::bluetooth::hci::LmpVersion::V_4_2));
1022 
1023   std::vector<uint8_t> le_features_event;
1024   send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(),
1025                                  le_features_event);
1026   auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create(
1027       CommandCompleteView::Create(EventView::Create(PacketView<true>(
1028           std::make_shared<std::vector<uint8_t>>(le_features_event)))));
1029   ASSERT_TRUE(le_features_view.IsValid());
1030   ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus());
1031   auto le_features = le_features_view.GetLeFeatures();
1032   ASSERT_TRUE(le_features &
1033               static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING));
1034 
1035 }
1036 
1037 /**
1038  * VSR-5.3.14-012 MUST support at least eight LE concurrent connections with
1039  *                three in peripheral role.
1040  */
1041 // @VsrTest = 5.3.14-012
TEST_P(BluetoothAidlTest,Vsr_BlE_Connection_Requirement)1042 TEST_P(BluetoothAidlTest, Vsr_BlE_Connection_Requirement) {
1043   std::vector<uint8_t> version_event;
1044   send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
1045                                  version_event);
1046   auto version_view = ReadLocalVersionInformationCompleteView::Create(
1047       CommandCompleteView::Create(EventView::Create(PacketView<true>(
1048           std::make_shared<std::vector<uint8_t>>(version_event)))));
1049   ASSERT_TRUE(version_view.IsValid());
1050   ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
1051   auto version = version_view.GetLocalVersionInformation();
1052   if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) {
1053     // This test does not apply to controllers below 5.0
1054     return;
1055   };
1056 
1057   int max_connections = ::android::base::GetIntProperty(
1058       "bluetooth.core.le.max_number_of_concurrent_connections", -1);
1059   if (max_connections == -1) {
1060     // With the property not set the default minimum of 8 will be used
1061     ALOGI("Max number of LE concurrent connections isn't set");
1062     return;
1063   }
1064   ALOGI("Max number of LE concurrent connections = %d", max_connections);
1065   ASSERT_GE(max_connections, 8);
1066 }
1067 
1068 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothAidlTest);
1069 INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothAidlTest,
1070                          testing::ValuesIn(android::getAidlHalInstanceNames(
1071                              IBluetoothHci::descriptor)),
1072                          android::PrintInstanceNameToString);
1073 
main(int argc,char ** argv)1074 int main(int argc, char** argv) {
1075   ABinderProcess_startThreadPool();
1076   ::testing::InitGoogleTest(&argc, argv);
1077   int status = RUN_ALL_TESTS();
1078   ALOGI("Test result = %d", status);
1079   return status;
1080 }
1081