blob: 4d900585963318c59f1a2e6304f0ff5767e05e49 [file] [log] [blame]
Dylan Tian8a6e09c2022-08-16 07:29:28 +00001/*
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
Treehugger Robot5e0277f2023-05-12 02:44:22 +000017#include <VtsCoreUtil.h>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000018#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>
Treehugger Robot5e0277f2023-05-12 02:44:22 +000024#include <android-base/properties.h>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000025#include <android/binder_auto_utils.h>
26#include <android/binder_manager.h>
27#include <android/binder_process.h>
28#include <binder/IServiceManager.h>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000029
Dylan Tian8a6e09c2022-08-16 07:29:28 +000030#include <chrono>
31#include <condition_variable>
32#include <future>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000033#include <queue>
34#include <thread>
Jack Hedbceaca2023-03-27 18:06:38 -070035#include <utility>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000036#include <vector>
37
Myles Watson51b8bae2023-01-27 14:22:24 -080038// 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
Dylan Tian8a6e09c2022-08-16 07:29:28 +000046using aidl::android::hardware::bluetooth::IBluetoothHci;
47using aidl::android::hardware::bluetooth::IBluetoothHciCallbacks;
48using aidl::android::hardware::bluetooth::Status;
49using ndk::ScopedAStatus;
50using ndk::SpAIBinder;
51
Jack He2d2f3c22023-03-27 03:04:52 -070052using ::bluetooth::hci::CommandBuilder;
53using ::bluetooth::hci::CommandCompleteView;
54using ::bluetooth::hci::CommandView;
55using ::bluetooth::hci::ErrorCode;
56using ::bluetooth::hci::EventView;
57using ::bluetooth::hci::LeReadLocalSupportedFeaturesBuilder;
58using ::bluetooth::hci::LeReadLocalSupportedFeaturesCompleteView;
59using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsBuilder;
60using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsCompleteView;
61using ::bluetooth::hci::LeReadResolvingListSizeBuilder;
62using ::bluetooth::hci::LeReadResolvingListSizeCompleteView;
63using ::bluetooth::hci::LLFeaturesBits;
64using ::bluetooth::hci::OpCode;
65using ::bluetooth::hci::OpCodeText;
66using ::bluetooth::hci::PacketView;
67using ::bluetooth::hci::ReadLocalVersionInformationBuilder;
68using ::bluetooth::hci::ReadLocalVersionInformationCompleteView;
69
Myles Watson864c9042024-07-24 14:39:05 -070070static constexpr uint8_t kMinLeAdvSetForBt5 = 10;
71static constexpr uint8_t kMinLeAdvSetForBt5ForTv = 10;
Jack He2d2f3c22023-03-27 03:04:52 -070072static constexpr uint8_t kMinLeResolvingListForBt5 = 8;
73
Myles Watsone1708c82023-01-18 17:07:58 -080074static constexpr size_t kNumHciCommandsBandwidth = 100;
Myles Watsone1708c82023-01-18 17:07:58 -080075static constexpr size_t kNumAclPacketsBandwidth = 100;
William Escandeac0f34f2024-04-11 09:06:31 -070076static constexpr std::chrono::milliseconds kWaitForInitTimeout(2000);
Dylan Tian8a6e09c2022-08-16 07:29:28 +000077static constexpr std::chrono::milliseconds kWaitForHciEventTimeout(2000);
Myles Watsone1708c82023-01-18 17:07:58 -080078static constexpr std::chrono::milliseconds kWaitForAclDataTimeout(1000);
Dylan Tian8a6e09c2022-08-16 07:29:28 +000079static constexpr std::chrono::milliseconds kInterfaceCloseDelayMs(200);
80
Dylan Tian8a6e09c2022-08-16 07:29:28 +000081// To discard Qualcomm ACL debugging
82static constexpr uint16_t kAclHandleQcaDebugMessage = 0xedc;
83
Treehugger Robot5e0277f2023-05-12 02:44:22 +000084static 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
113static bool isTv() {
114 return testing::deviceSupportsFeature("android.software.leanback") ||
115 testing::deviceSupportsFeature("android.hardware.type.television");
116}
117
Doug Ferraz83b22672024-03-22 19:54:26 -0400118static bool isHandheld() {
119 return testing::deviceSupportsFeature("android.hardware.type.handheld");
120}
121
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000122class ThroughputLogger {
123 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700124 explicit ThroughputLogger(std::string task)
Myles Watsone1708c82023-01-18 17:07:58 -0800125 : total_bytes_(0),
Jack Hedbceaca2023-03-27 18:06:38 -0700126 task_(std::move(task)),
Myles Watsone1708c82023-01-18 17:07:58 -0800127 start_time_(std::chrono::steady_clock::now()) {}
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000128
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
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.
153class BluetoothAidlTest : public ::testing::TestWithParam<std::string> {
William Escande1a139902024-02-27 15:57:33 -0800154 std::chrono::time_point<std::chrono::system_clock>
155 time_after_initialize_complete;
156
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000157 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700158 void SetUp() override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000159 // 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(),
Jack Hedbceaca2023-03-27 18:06:38 -0700174 bluetooth_hci_death_recipient, nullptr));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000175
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;
William Escande1a139902024-02-27 15:57:33 -0800187 std::chrono::time_point<std::chrono::system_clock>
188 timeout_after_initialize =
189 std::chrono::system_clock::now() + kWaitForInitTimeout;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000190
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());
William Escande1a139902024-02-27 15:57:33 -0800196 ASSERT_GE(timeout_after_initialize, time_after_initialize_complete);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000197 }
198
Jack Hedbceaca2023-03-27 18:06:38 -0700199 void TearDown() override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000200 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();
Myles Watsone1708c82023-01-18 17:07:58 -0800205 discard_qca_debugging();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000206 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();
Myles Watsone1708c82023-01-18 17:07:58 -0800213 void setSynchronousFlowControlEnable();
214
215 // Functions called from within tests in loopback mode
216 void sendAndCheckHci(int num_packets);
Myles Watsone1708c82023-01-18 17:07:58 -0800217 void sendAndCheckAcl(int num_packets, size_t size, uint16_t handle);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000218
219 // Helper functions to try to get a handle on verbosity
Myles Watsone1708c82023-01-18 17:07:58 -0800220 void enterLoopbackMode();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000221 void handle_no_ops();
Myles Watsone1708c82023-01-18 17:07:58 -0800222 void discard_qca_debugging();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000223 void wait_for_event(bool timeout_is_error);
Jack He2d2f3c22023-03-27 03:04:52 -0700224 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);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000229 int wait_for_completed_packets_event(uint16_t handle);
Jack He2d2f3c22023-03-27 03:04:52 -0700230 void send_and_wait_for_cmd_complete(std::unique_ptr<CommandBuilder> cmd,
231 std::vector<uint8_t>& cmd_complete);
xiaoshun.xu4e85c092023-07-11 17:30:04 +0800232 void reassemble_sco_loopback_pkt(std::vector<uint8_t>& scoPackets,
233 size_t size);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000234
235 // A simple test implementation of BluetoothHciCallbacks.
236 class BluetoothHciCallbacks
237 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
238 BluetoothAidlTest& parent_;
239
240 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700241 explicit BluetoothHciCallbacks(BluetoothAidlTest& parent)
242 : parent_(parent){};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000243
Jack Hedbceaca2023-03-27 18:06:38 -0700244 ~BluetoothHciCallbacks() override = default;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000245
Jack Hedbceaca2023-03-27 18:06:38 -0700246 ndk::ScopedAStatus initializationComplete(Status status) override {
William Escande1a139902024-02-27 15:57:33 -0800247 if (status == Status::SUCCESS) {
248 parent_.time_after_initialize_complete =
249 std::chrono::system_clock::now();
250 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000251 parent_.initialized_promise.set_value(status == Status::SUCCESS);
252 ALOGV("%s (status = %d)", __func__, static_cast<int>(status));
253 return ScopedAStatus::ok();
254 };
255
Jack Hedbceaca2023-03-27 18:06:38 -0700256 ndk::ScopedAStatus hciEventReceived(
257 const std::vector<uint8_t>& event) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000258 parent_.event_cb_count++;
259 parent_.event_queue.push(event);
Jack He2d2f3c22023-03-27 03:04:52 -0700260 ALOGI("Event received (length = %d)", static_cast<int>(event.size()));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000261 return ScopedAStatus::ok();
262 };
263
Jack Hedbceaca2023-03-27 18:06:38 -0700264 ndk::ScopedAStatus aclDataReceived(
265 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000266 parent_.acl_cb_count++;
267 parent_.acl_queue.push(data);
268 return ScopedAStatus::ok();
269 };
270
Jack Hedbceaca2023-03-27 18:06:38 -0700271 ndk::ScopedAStatus scoDataReceived(
272 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000273 parent_.sco_cb_count++;
274 parent_.sco_queue.push(data);
275 return ScopedAStatus::ok();
276 };
277
Jack Hedbceaca2023-03-27 18:06:38 -0700278 ndk::ScopedAStatus isoDataReceived(
279 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000280 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:
Jack Hedbceaca2023-03-27 18:06:38 -0700289 WaitQueue() = default;
290 ;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000291
292 virtual ~WaitQueue() = default;
293
294 bool empty() const {
295 std::lock_guard<std::mutex> lock(m_);
296 return q_.empty();
297 };
298
299 size_t size() const {
300 std::lock_guard<std::mutex> lock(m_);
301 return q_.size();
302 };
303
304 void push(const T& v) {
305 std::lock_guard<std::mutex> lock(m_);
306 q_.push(v);
307 ready_.notify_one();
308 };
309
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
Myles Watson51b8bae2023-01-27 14:22:24 -0800320 void pop() {
321 std::lock_guard<std::mutex> lock(m_);
322 if (q_.empty()) {
323 return;
324 }
325 q_.pop();
326 };
327
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000328 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
337 void wait() {
338 std::unique_lock<std::mutex> lock(m_);
339 while (q_.empty()) {
340 ready_.wait(lock);
341 }
342 };
343
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
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;
Jack Hedbceaca2023-03-27 18:06:38 -0700374 AIBinder_DeathRecipient* bluetooth_hci_death_recipient{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000375 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;
Jack Hedbceaca2023-03-27 18:06:38 -0700381 int event_cb_count{};
382 int sco_cb_count{};
383 int acl_cb_count{};
384 int iso_cb_count{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000385
Jack Hedbceaca2023-03-27 18:06:38 -0700386 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{};
Myles Watsone1708c82023-01-18 17:07:58 -0800390
391 std::vector<uint16_t> sco_connection_handles;
392 std::vector<uint16_t> acl_connection_handles;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000393};
394
395// Discard NO-OPs from the event queue.
396void BluetoothAidlTest::handle_no_ops() {
397 while (!event_queue.empty()) {
398 std::vector<uint8_t> event;
399 event_queue.front(event);
Myles Watson51b8bae2023-01-27 14:22:24 -0800400 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();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000414 } else {
415 break;
416 }
417 }
Myles Watsone1708c82023-01-18 17:07:58 -0800418}
419
420// Discard Qualcomm ACL debugging
421void BluetoothAidlTest::discard_qca_debugging() {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000422 while (!acl_queue.empty()) {
423 std::vector<uint8_t> acl_packet;
424 acl_queue.front(acl_packet);
Myles Watson51b8bae2023-01-27 14:22:24 -0800425 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();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000431 } else {
432 break;
433 }
434 }
435}
436
437// Receive an event, discarding NO-OPs.
438void BluetoothAidlTest::wait_for_event(bool timeout_is_error = true) {
Myles Watsone1708c82023-01-18 17:07:58 -0800439 // 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 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000452 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000453}
454
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000455void BluetoothAidlTest::wait_for_command_complete_event(
Jack He2d2f3c22023-03-27 03:04:52 -0700456 OpCode opCode, std::vector<uint8_t>& complete_event) {
Myles Watsone1708c82023-01-18 17:07:58 -0800457 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Myles Watsone1708c82023-01-18 17:07:58 -0800458 ASSERT_FALSE(event_queue.empty());
Jack He2d2f3c22023-03-27 03:04:52 -0700459 ASSERT_TRUE(event_queue.pop(complete_event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800460 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
461 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
Jack He2d2f3c22023-03-27 03:04:52 -0700462 std::make_shared<std::vector<uint8_t>>(complete_event))));
Myles Watson51b8bae2023-01-27 14:22:24 -0800463 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));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000467}
468
Jack He2d2f3c22023-03-27 03:04:52 -0700469void 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
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000476// Send the command to read the controller's buffer sizes.
477void BluetoothAidlTest::setBufferSizes() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800478 std::vector<uint8_t> cmd;
479 ::bluetooth::packet::BitInserter bi{cmd};
480 ::bluetooth::hci::ReadBufferSizeBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000481 hci->sendHciCommand(cmd);
482
Myles Watsone1708c82023-01-18 17:07:58 -0800483 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000484 std::vector<uint8_t> event;
485 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800486 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)))));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000491
Myles Watson51b8bae2023-01-27 14:22:24 -0800492 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();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000498
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
Myles Watsone1708c82023-01-18 17:07:58 -0800506// Enable flow control packets for SCO
507void BluetoothAidlTest::setSynchronousFlowControlEnable() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800508 std::vector<uint8_t> cmd;
509 ::bluetooth::packet::BitInserter bi{cmd};
510 ::bluetooth::hci::WriteSynchronousFlowControlEnableBuilder::Create(
511 ::bluetooth::hci::Enable::ENABLED)
512 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800513 hci->sendHciCommand(cmd);
514
Jack He2d2f3c22023-03-27 03:04:52 -0700515 wait_and_validate_command_complete_event(
Myles Watson51b8bae2023-01-27 14:22:24 -0800516 ::bluetooth::hci::OpCode::WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE);
Myles Watsone1708c82023-01-18 17:07:58 -0800517}
518
519// Send an HCI command (in Loopback mode) and check the response.
520void BluetoothAidlTest::sendAndCheckHci(int num_packets) {
Jack Hedbceaca2023-03-27 18:06:38 -0700521 ThroughputLogger logger{__func__};
522 size_t command_size = 0;
Myles Watson51b8bae2023-01-27 14:22:24 -0800523 char new_name[] = "John Jacob Jingleheimer Schmidt ___________________";
524 size_t new_name_length = strlen(new_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800525 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800526 // The name to set is new_name
Jack Hedbceaca2023-03-27 18:06:38 -0700527 std::array<uint8_t, 248> name_array{};
Myles Watsone1708c82023-01-18 17:07:58 -0800528 for (size_t i = 0; i < new_name_length; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800529 name_array[i] = new_name[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800530 }
531 // And the packet number
Myles Watson51b8bae2023-01-27 14:22:24 -0800532 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];
Myles Watsone1708c82023-01-18 17:07:58 -0800537 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800538 std::vector<uint8_t> write_name;
539 ::bluetooth::packet::BitInserter bi{write_name};
540 ::bluetooth::hci::WriteLocalNameBuilder::Create(name_array)->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800541 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));
Myles Watson51b8bae2023-01-27 14:22:24 -0800548 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);
Myles Watsone1708c82023-01-18 17:07:58 -0800555
556 if (n == num_packets - 1) {
557 command_size = write_name.size();
558 }
Myles Watsone1708c82023-01-18 17:07:58 -0800559 }
560 logger.setTotalBytes(command_size * num_packets * 2);
561}
562
Myles Watsone1708c82023-01-18 17:07:58 -0800563// Send an ACL data packet (in Loopback mode) and check the response.
564void BluetoothAidlTest::sendAndCheckAcl(int num_packets, size_t size,
565 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700566 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800567 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800568 // Send an ACL packet with counting data
569 auto payload = std::make_unique<::bluetooth::packet::RawBuilder>();
Myles Watsone1708c82023-01-18 17:07:58 -0800570 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800571 payload->AddOctets1(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800572 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800573 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);
Myles Watsone1708c82023-01-18 17:07:58 -0800580 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
Myles Watson51b8bae2023-01-27 14:22:24 -0800587 ASSERT_EQ(acl_packet, acl_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800588 }
589 logger.setTotalBytes(num_packets * size * 2);
590}
591
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000592// Return the number of completed packets reported by the controller.
593int BluetoothAidlTest::wait_for_completed_packets_event(uint16_t handle) {
594 int packets_processed = 0;
Myles Watson65b47f52023-01-26 12:59:06 -0800595 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 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000604 std::vector<uint8_t> event;
605 EXPECT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800606 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();
Jack Hedbceaca2023-03-27 18:06:38 -0700614 for (const auto& entry : completed_packets) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800615 EXPECT_EQ(handle, entry.connection_handle_);
616 packets_processed += entry.host_num_of_completed_packets_;
617 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000618 }
619 return packets_processed;
620}
621
Myles Watsone1708c82023-01-18 17:07:58 -0800622// Send local loopback command and initialize SCO and ACL handles.
623void BluetoothAidlTest::enterLoopbackMode() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800624 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);
Myles Watsone1708c82023-01-18 17:07:58 -0800629 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));
Myles Watson51b8bae2023-01-27 14:22:24 -0800644 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());
Myles Watsone1708c82023-01-18 17:07:58 -0800648
Myles Watson51b8bae2023-01-27 14:22:24 -0800649 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);
Myles Watsone1708c82023-01-18 17:07:58 -0800664 }
Myles Watsone1708c82023-01-18 17:07:58 -0800665 connection_event_count++;
666 } else {
Myles Watson51b8bae2023-01-27 14:22:24 -0800667 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());
Myles Watsone1708c82023-01-18 17:07:58 -0800673 command_complete_received = true;
674 }
675 }
676}
677
Jack He2d2f3c22023-03-27 03:04:52 -0700678void 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
Myles Watsone1708c82023-01-18 17:07:58 -0800692// Empty test: Initialize()/Close() are called in SetUp()/TearDown().
693TEST_P(BluetoothAidlTest, InitializeAndClose) {}
694
695// Send an HCI Reset with sendHciCommand and wait for a command complete event.
Myles Watson65b47f52023-01-26 12:59:06 -0800696TEST_P(BluetoothAidlTest, HciReset) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800697 std::vector<uint8_t> reset;
698 ::bluetooth::packet::BitInserter bi{reset};
699 ::bluetooth::hci::ResetBuilder::Create()->Serialize(bi);
Myles Watson65b47f52023-01-26 12:59:06 -0800700 hci->sendHciCommand(reset);
701
Jack He2d2f3c22023-03-27 03:04:52 -0700702 wait_and_validate_command_complete_event(::bluetooth::hci::OpCode::RESET);
Myles Watson65b47f52023-01-26 12:59:06 -0800703}
Myles Watsone1708c82023-01-18 17:07:58 -0800704
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000705// Read and check the HCI version of the controller.
706TEST_P(BluetoothAidlTest, HciVersionTest) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800707 std::vector<uint8_t> cmd;
708 ::bluetooth::packet::BitInserter bi{cmd};
709 ::bluetooth::hci::ReadLocalVersionInformationBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000710 hci->sendHciCommand(cmd);
711
Myles Watsone1708c82023-01-18 17:07:58 -0800712 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000713
714 std::vector<uint8_t> event;
715 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800716 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_);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000727}
728
729// Send an unknown HCI command and wait for the error message.
730TEST_P(BluetoothAidlTest, HciUnknownCommand) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800731 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);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000737 hci->sendHciCommand(cmd);
738
Myles Watsone1708c82023-01-18 17:07:58 -0800739 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000740
741 std::vector<uint8_t> event;
742 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800743 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());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000747
Myles Watson51b8bae2023-01-27 14:22:24 -0800748 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();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000766 }
767}
768
Myles Watsone1708c82023-01-18 17:07:58 -0800769// Enter loopback mode, but don't send any packets.
Myles Watson65b47f52023-01-26 12:59:06 -0800770TEST_P(BluetoothAidlTest, WriteLoopbackMode) { enterLoopbackMode(); }
Myles Watsone1708c82023-01-18 17:07:58 -0800771
772// Enter loopback mode and send a single command.
773TEST_P(BluetoothAidlTest, LoopbackModeSingleCommand) {
Myles Watsone1708c82023-01-18 17:07:58 -0800774 setBufferSizes();
775
776 enterLoopbackMode();
777
778 sendAndCheckHci(1);
779}
780
Myles Watsone1708c82023-01-18 17:07:58 -0800781// Enter loopback mode and send a single ACL packet.
782TEST_P(BluetoothAidlTest, LoopbackModeSingleAcl) {
Myles Watsone1708c82023-01-18 17:07:58 -0800783 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.
803TEST_P(BluetoothAidlTest, LoopbackModeCommandBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800804 setBufferSizes();
805
806 enterLoopbackMode();
807
808 sendAndCheckHci(kNumHciCommandsBandwidth);
809}
810
Myles Watsone1708c82023-01-18 17:07:58 -0800811// Enter loopback mode and send packets for ACL bandwidth measurements.
812TEST_P(BluetoothAidlTest, LoopbackModeAclBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800813 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
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000831// Set all bits in the event mask
832TEST_P(BluetoothAidlTest, SetEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800833 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);
Jack He2d2f3c22023-03-27 03:04:52 -0700838 wait_and_validate_command_complete_event(
839 ::bluetooth::hci::OpCode::SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000840}
841
842// Set all bits in the LE event mask
843TEST_P(BluetoothAidlTest, SetLeEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800844 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);
Jack He2d2f3c22023-03-27 03:04:52 -0700849 wait_and_validate_command_complete_event(
850 ::bluetooth::hci::OpCode::LE_SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000851}
852
Myles Watson0daa1642023-01-26 15:58:28 -0800853// Call initialize twice, second one should fail.
854TEST_P(BluetoothAidlTest, CallInitializeTwice) {
855 class SecondCb
856 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
857 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700858 ndk::ScopedAStatus initializationComplete(Status status) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800859 EXPECT_EQ(status, Status::ALREADY_INITIALIZED);
860 init_promise.set_value();
861 return ScopedAStatus::ok();
862 };
863
Jack Hedbceaca2023-03-27 18:06:38 -0700864 ndk::ScopedAStatus hciEventReceived(
865 const std::vector<uint8_t>& /*event*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800866 ADD_FAILURE();
867 return ScopedAStatus::ok();
868 };
869
Jack Hedbceaca2023-03-27 18:06:38 -0700870 ndk::ScopedAStatus aclDataReceived(
871 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800872 ADD_FAILURE();
873 return ScopedAStatus::ok();
874 };
875
Jack Hedbceaca2023-03-27 18:06:38 -0700876 ndk::ScopedAStatus scoDataReceived(
877 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800878 ADD_FAILURE();
879 return ScopedAStatus::ok();
880 };
881
Jack Hedbceaca2023-03-27 18:06:38 -0700882 ndk::ScopedAStatus isoDataReceived(
883 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800884 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
Henri Chataingf506e2d2024-09-18 21:51:39 +0000899// @VsrTest = 5.3.14-001
900// @VsrTest = 5.3.14-002
901// @VsrTest = 5.3.14-004
Myles Watson5bfb3a72023-05-10 14:06:32 -0700902TEST_P(BluetoothAidlTest, Vsr_Bluetooth5Requirements) {
Henri Chataingf506e2d2024-09-18 21:51:39 +0000903 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
Jack He2d2f3c22023-03-27 03:04:52 -0700909 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();
Henri Chataingf506e2d2024-09-18 21:51:39 +0000918
Jack He2d2f3c22023-03-27 03:04:52 -0700919 if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) {
Henri Chataingf506e2d2024-09-18 21:51:39 +0000920 GTEST_SKIP() << "Bluetooth version is lower than 5.0";
Jack He2d2f3c22023-03-27 03:04:52 -0700921 return;
Henri Chataingf506e2d2024-09-18 21:51:39 +0000922 }
923
Jack He2d2f3c22023-03-27 03:04:52 -0700924 // 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());
Henri Chataingf506e2d2024-09-18 21:51:39 +0000936
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
Jack He2d2f3c22023-03-27 03:04:52 -0700946 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));
Henri Chataingf506e2d2024-09-18 21:51:39 +0000953 ASSERT_TRUE(le_features &
954 static_cast<uint64_t>(LLFeaturesBits::LE_PERIODIC_ADVERTISING));
Jack He2d2f3c22023-03-27 03:04:52 -0700955
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();
Treehugger Robot5e0277f2023-05-12 02:44:22 +0000967
Henri Chataingf506e2d2024-09-18 21:51:39 +0000968 // 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.
Treehugger Robot5e0277f2023-05-12 02:44:22 +0000972 if (isTv() && get_vsr_api_level() == __ANDROID_API_U__) {
Myles Watson864c9042024-07-24 14:39:05 -0700973 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5ForTv);
Treehugger Robot5e0277f2023-05-12 02:44:22 +0000974 } else {
975 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5);
976 }
Jack He2d2f3c22023-03-27 03:04:52 -0700977
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();
Henri Chataingf506e2d2024-09-18 21:51:39 +0000988
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.
Jack He2d2f3c22023-03-27 03:04:52 -0700993 ASSERT_GE(num_resolving_list, kMinLeResolvingListForBt5);
994}
995
Doug Ferraz83b22672024-03-22 19:54:26 -0400996/**
Doug Ferraz83b22672024-03-22 19:54:26 -0400997 * 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 */
Doug Ferrazeff7b912024-03-25 20:04:19 -04001000// @VsrTest = 5.3.14-007
1001// @VsrTest = 5.3.14-008
Doug Ferraz83b22672024-03-22 19:54:26 -04001002TEST_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
Omair Kamil84a95be2024-04-22 13:29:19 -07001037/**
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
1042TEST_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
Dylan Tian8a6e09c2022-08-16 07:29:28 +00001068GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothAidlTest);
1069INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothAidlTest,
1070 testing::ValuesIn(android::getAidlHalInstanceNames(
1071 IBluetoothHci::descriptor)),
1072 android::PrintInstanceNameToString);
1073
1074int 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}