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