blob: 51931e7ec66bf393c6b616bd8a70f9eb181b70bd [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);
Henri Chataing99062652024-12-18 00:08:12 +0000400
401 auto event_view =
Myles Watson51b8bae2023-01-27 14:22:24 -0800402 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
Henri Chataing99062652024-12-18 00:08:12 +0000403 std::make_shared<std::vector<uint8_t>>(event)));
404 if (!event_view.IsValid()) {
405 break;
406 }
407
408 auto status_view = ::bluetooth::hci::CommandStatusView::Create(event_view);
409 auto complete_view =
410 ::bluetooth::hci::CommandCompleteView::Create(event_view);
411
Myles Watson51b8bae2023-01-27 14:22:24 -0800412 bool is_complete_no_op =
413 complete_view.IsValid() &&
414 complete_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
415 bool is_status_no_op =
416 status_view.IsValid() &&
417 status_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
418 if (is_complete_no_op || is_status_no_op) {
419 event_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000420 } else {
421 break;
422 }
423 }
Myles Watsone1708c82023-01-18 17:07:58 -0800424}
425
426// Discard Qualcomm ACL debugging
427void BluetoothAidlTest::discard_qca_debugging() {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000428 while (!acl_queue.empty()) {
429 std::vector<uint8_t> acl_packet;
430 acl_queue.front(acl_packet);
Myles Watson51b8bae2023-01-27 14:22:24 -0800431 auto acl_view =
432 ::bluetooth::hci::AclView::Create(::bluetooth::hci::PacketView<true>(
433 std::make_shared<std::vector<uint8_t>>(acl_packet)));
434 EXPECT_TRUE(acl_view.IsValid());
435 if (acl_view.GetHandle() == kAclHandleQcaDebugMessage) {
436 acl_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000437 } else {
438 break;
439 }
440 }
441}
442
443// Receive an event, discarding NO-OPs.
444void BluetoothAidlTest::wait_for_event(bool timeout_is_error = true) {
Myles Watsone1708c82023-01-18 17:07:58 -0800445 // Wait until we get something that's not a no-op.
446 while (true) {
447 bool event_ready = event_queue.waitWithTimeout(kWaitForHciEventTimeout);
448 ASSERT_TRUE(event_ready || !timeout_is_error);
449 if (event_queue.empty()) {
450 // waitWithTimeout timed out
451 return;
452 }
453 handle_no_ops();
454 if (!event_queue.empty()) {
455 // There's an event in the queue that's not a no-op.
456 return;
457 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000458 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000459}
460
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000461void BluetoothAidlTest::wait_for_command_complete_event(
Jack He2d2f3c22023-03-27 03:04:52 -0700462 OpCode opCode, std::vector<uint8_t>& complete_event) {
Myles Watsone1708c82023-01-18 17:07:58 -0800463 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Myles Watsone1708c82023-01-18 17:07:58 -0800464 ASSERT_FALSE(event_queue.empty());
Jack He2d2f3c22023-03-27 03:04:52 -0700465 ASSERT_TRUE(event_queue.pop(complete_event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800466 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
467 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
Jack He2d2f3c22023-03-27 03:04:52 -0700468 std::make_shared<std::vector<uint8_t>>(complete_event))));
Myles Watson51b8bae2023-01-27 14:22:24 -0800469 ASSERT_TRUE(complete_view.IsValid());
470 ASSERT_EQ(complete_view.GetCommandOpCode(), opCode);
471 ASSERT_EQ(complete_view.GetPayload()[0],
472 static_cast<uint8_t>(::bluetooth::hci::ErrorCode::SUCCESS));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000473}
474
Jack He2d2f3c22023-03-27 03:04:52 -0700475void BluetoothAidlTest::wait_and_validate_command_complete_event(
476 ::bluetooth::hci::OpCode opCode) {
477 std::vector<uint8_t> complete_event;
478 ASSERT_NO_FATAL_FAILURE(
479 wait_for_command_complete_event(opCode, complete_event));
480}
481
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000482// Send the command to read the controller's buffer sizes.
483void BluetoothAidlTest::setBufferSizes() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800484 std::vector<uint8_t> cmd;
485 ::bluetooth::packet::BitInserter bi{cmd};
486 ::bluetooth::hci::ReadBufferSizeBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000487 hci->sendHciCommand(cmd);
488
Myles Watsone1708c82023-01-18 17:07:58 -0800489 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000490 std::vector<uint8_t> event;
491 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800492 auto complete_view = ::bluetooth::hci::ReadBufferSizeCompleteView::Create(
493 ::bluetooth::hci::CommandCompleteView::Create(
494 ::bluetooth::hci::EventView::Create(
495 ::bluetooth::hci::PacketView<true>(
496 std::make_shared<std::vector<uint8_t>>(event)))));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000497
Myles Watson51b8bae2023-01-27 14:22:24 -0800498 ASSERT_TRUE(complete_view.IsValid());
499 ASSERT_EQ(complete_view.GetStatus(), ::bluetooth::hci::ErrorCode::SUCCESS);
500 max_acl_data_packet_length = complete_view.GetAclDataPacketLength();
501 max_sco_data_packet_length = complete_view.GetSynchronousDataPacketLength();
502 max_acl_data_packets = complete_view.GetTotalNumAclDataPackets();
503 max_sco_data_packets = complete_view.GetTotalNumSynchronousDataPackets();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000504
505 ALOGD("%s: ACL max %d num %d SCO max %d num %d", __func__,
506 static_cast<int>(max_acl_data_packet_length),
507 static_cast<int>(max_acl_data_packets),
508 static_cast<int>(max_sco_data_packet_length),
509 static_cast<int>(max_sco_data_packets));
510}
511
Myles Watsone1708c82023-01-18 17:07:58 -0800512// Enable flow control packets for SCO
513void BluetoothAidlTest::setSynchronousFlowControlEnable() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800514 std::vector<uint8_t> cmd;
515 ::bluetooth::packet::BitInserter bi{cmd};
516 ::bluetooth::hci::WriteSynchronousFlowControlEnableBuilder::Create(
517 ::bluetooth::hci::Enable::ENABLED)
518 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800519 hci->sendHciCommand(cmd);
520
Jack He2d2f3c22023-03-27 03:04:52 -0700521 wait_and_validate_command_complete_event(
Myles Watson51b8bae2023-01-27 14:22:24 -0800522 ::bluetooth::hci::OpCode::WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE);
Myles Watsone1708c82023-01-18 17:07:58 -0800523}
524
525// Send an HCI command (in Loopback mode) and check the response.
526void BluetoothAidlTest::sendAndCheckHci(int num_packets) {
Jack Hedbceaca2023-03-27 18:06:38 -0700527 ThroughputLogger logger{__func__};
528 size_t command_size = 0;
Myles Watson51b8bae2023-01-27 14:22:24 -0800529 char new_name[] = "John Jacob Jingleheimer Schmidt ___________________";
530 size_t new_name_length = strlen(new_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800531 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800532 // The name to set is new_name
Jack Hedbceaca2023-03-27 18:06:38 -0700533 std::array<uint8_t, 248> name_array{};
Myles Watsone1708c82023-01-18 17:07:58 -0800534 for (size_t i = 0; i < new_name_length; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800535 name_array[i] = new_name[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800536 }
537 // And the packet number
Myles Watson51b8bae2023-01-27 14:22:24 -0800538 char number[11] = "0000000000";
539 snprintf(number, sizeof(number), "%010d", static_cast<int>(n));
540 for (size_t i = new_name_length; i < new_name_length + sizeof(number) - 1;
541 i++) {
542 name_array[new_name_length + i] = number[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800543 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800544 std::vector<uint8_t> write_name;
545 ::bluetooth::packet::BitInserter bi{write_name};
546 ::bluetooth::hci::WriteLocalNameBuilder::Create(name_array)->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800547 hci->sendHciCommand(write_name);
548
549 // Check the loopback of the HCI packet
550 ASSERT_NO_FATAL_FAILURE(wait_for_event());
551
552 std::vector<uint8_t> event;
553 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800554 auto event_view = ::bluetooth::hci::LoopbackCommandView::Create(
555 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
556 std::make_shared<std::vector<uint8_t>>(event))));
557 ASSERT_TRUE(event_view.IsValid());
558 std::vector<uint8_t> looped_back_command{event_view.GetPayload().begin(),
559 event_view.GetPayload().end()};
560 ASSERT_EQ(looped_back_command, write_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800561
562 if (n == num_packets - 1) {
563 command_size = write_name.size();
564 }
Myles Watsone1708c82023-01-18 17:07:58 -0800565 }
566 logger.setTotalBytes(command_size * num_packets * 2);
567}
568
Myles Watsone1708c82023-01-18 17:07:58 -0800569// Send an ACL data packet (in Loopback mode) and check the response.
570void BluetoothAidlTest::sendAndCheckAcl(int num_packets, size_t size,
571 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700572 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800573 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800574 // Send an ACL packet with counting data
575 auto payload = std::make_unique<::bluetooth::packet::RawBuilder>();
Myles Watsone1708c82023-01-18 17:07:58 -0800576 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800577 payload->AddOctets1(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800578 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800579 std::vector<uint8_t> acl_packet;
580 ::bluetooth::packet::BitInserter bi{acl_packet};
581 ::bluetooth::hci::AclBuilder::Create(
582 handle,
583 ::bluetooth::hci::PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE,
584 ::bluetooth::hci::BroadcastFlag::POINT_TO_POINT, std::move(payload))
585 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800586 hci->sendAclData(acl_packet);
587
588 std::vector<uint8_t> acl_loopback;
589 // Check the loopback of the ACL packet
590 ASSERT_TRUE(
591 acl_queue.tryPopWithTimeout(acl_loopback, kWaitForAclDataTimeout));
592
Myles Watson51b8bae2023-01-27 14:22:24 -0800593 ASSERT_EQ(acl_packet, acl_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800594 }
595 logger.setTotalBytes(num_packets * size * 2);
596}
597
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000598// Return the number of completed packets reported by the controller.
599int BluetoothAidlTest::wait_for_completed_packets_event(uint16_t handle) {
600 int packets_processed = 0;
Myles Watson65b47f52023-01-26 12:59:06 -0800601 while (true) {
602 // There should be at least one event.
603 wait_for_event(packets_processed == 0);
604 if (event_queue.empty()) {
605 if (packets_processed == 0) {
606 ALOGW("%s: waitForBluetoothCallback timed out.", __func__);
607 }
608 return packets_processed;
609 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000610 std::vector<uint8_t> event;
611 EXPECT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800612 auto event_view = ::bluetooth::hci::NumberOfCompletedPacketsView::Create(
613 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
614 std::make_shared<std::vector<uint8_t>>(event))));
615 if (!event_view.IsValid()) {
616 ADD_FAILURE();
617 return packets_processed;
618 }
619 auto completed_packets = event_view.GetCompletedPackets();
Jack Hedbceaca2023-03-27 18:06:38 -0700620 for (const auto& entry : completed_packets) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800621 EXPECT_EQ(handle, entry.connection_handle_);
622 packets_processed += entry.host_num_of_completed_packets_;
623 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000624 }
625 return packets_processed;
626}
627
Myles Watsone1708c82023-01-18 17:07:58 -0800628// Send local loopback command and initialize SCO and ACL handles.
629void BluetoothAidlTest::enterLoopbackMode() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800630 std::vector<uint8_t> cmd;
631 ::bluetooth::packet::BitInserter bi{cmd};
632 ::bluetooth::hci::WriteLoopbackModeBuilder::Create(
633 bluetooth::hci::LoopbackMode::ENABLE_LOCAL)
634 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800635 hci->sendHciCommand(cmd);
636
637 // Receive connection complete events with data channels
638 int connection_event_count = 0;
639 bool command_complete_received = false;
640 while (true) {
641 wait_for_event(false);
642 if (event_queue.empty()) {
643 // Fail if there was no event received or no connections completed.
644 ASSERT_TRUE(command_complete_received);
645 ASSERT_LT(0, connection_event_count);
646 return;
647 }
648 std::vector<uint8_t> event;
649 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800650 auto event_view =
651 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
652 std::make_shared<std::vector<uint8_t>>(event)));
653 ASSERT_TRUE(event_view.IsValid());
Myles Watsone1708c82023-01-18 17:07:58 -0800654
Myles Watson51b8bae2023-01-27 14:22:24 -0800655 if (event_view.GetEventCode() ==
656 ::bluetooth::hci::EventCode::CONNECTION_COMPLETE) {
657 auto complete_view =
658 ::bluetooth::hci::ConnectionCompleteView::Create(event_view);
659 ASSERT_TRUE(complete_view.IsValid());
660 switch (complete_view.GetLinkType()) {
661 case ::bluetooth::hci::LinkType::ACL:
662 acl_connection_handles.push_back(complete_view.GetConnectionHandle());
663 break;
664 case ::bluetooth::hci::LinkType::SCO:
665 sco_connection_handles.push_back(complete_view.GetConnectionHandle());
666 break;
667 default:
668 ASSERT_EQ(complete_view.GetLinkType(),
669 ::bluetooth::hci::LinkType::ACL);
Myles Watsone1708c82023-01-18 17:07:58 -0800670 }
Myles Watsone1708c82023-01-18 17:07:58 -0800671 connection_event_count++;
672 } else {
Myles Watson51b8bae2023-01-27 14:22:24 -0800673 auto command_complete_view =
674 ::bluetooth::hci::WriteLoopbackModeCompleteView::Create(
675 ::bluetooth::hci::CommandCompleteView::Create(event_view));
676 ASSERT_TRUE(command_complete_view.IsValid());
677 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
678 command_complete_view.GetStatus());
Myles Watsone1708c82023-01-18 17:07:58 -0800679 command_complete_received = true;
680 }
681 }
682}
683
Jack He2d2f3c22023-03-27 03:04:52 -0700684void BluetoothAidlTest::send_and_wait_for_cmd_complete(
685 std::unique_ptr<CommandBuilder> cmd, std::vector<uint8_t>& cmd_complete) {
686 std::vector<uint8_t> cmd_bytes = cmd->SerializeToBytes();
687 hci->sendHciCommand(cmd_bytes);
688
689 auto view = CommandView::Create(
690 PacketView<true>(std::make_shared<std::vector<uint8_t>>(cmd_bytes)));
691 ASSERT_TRUE(view.IsValid());
692 ALOGI("Waiting for %s[0x%x]", OpCodeText(view.GetOpCode()).c_str(),
693 static_cast<int>(view.GetOpCode()));
694 ASSERT_NO_FATAL_FAILURE(
695 wait_for_command_complete_event(view.GetOpCode(), cmd_complete));
696}
697
Myles Watsone1708c82023-01-18 17:07:58 -0800698// Empty test: Initialize()/Close() are called in SetUp()/TearDown().
699TEST_P(BluetoothAidlTest, InitializeAndClose) {}
700
701// Send an HCI Reset with sendHciCommand and wait for a command complete event.
Myles Watson65b47f52023-01-26 12:59:06 -0800702TEST_P(BluetoothAidlTest, HciReset) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800703 std::vector<uint8_t> reset;
704 ::bluetooth::packet::BitInserter bi{reset};
705 ::bluetooth::hci::ResetBuilder::Create()->Serialize(bi);
Myles Watson65b47f52023-01-26 12:59:06 -0800706 hci->sendHciCommand(reset);
707
Jack He2d2f3c22023-03-27 03:04:52 -0700708 wait_and_validate_command_complete_event(::bluetooth::hci::OpCode::RESET);
Myles Watson65b47f52023-01-26 12:59:06 -0800709}
Myles Watsone1708c82023-01-18 17:07:58 -0800710
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000711// Read and check the HCI version of the controller.
712TEST_P(BluetoothAidlTest, HciVersionTest) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800713 std::vector<uint8_t> cmd;
714 ::bluetooth::packet::BitInserter bi{cmd};
715 ::bluetooth::hci::ReadLocalVersionInformationBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000716 hci->sendHciCommand(cmd);
717
Myles Watsone1708c82023-01-18 17:07:58 -0800718 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000719
720 std::vector<uint8_t> event;
721 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800722 auto complete_view =
723 ::bluetooth::hci::ReadLocalVersionInformationCompleteView::Create(
724 ::bluetooth::hci::CommandCompleteView::Create(
725 ::bluetooth::hci::EventView::Create(
726 ::bluetooth::hci::PacketView<true>(
727 std::make_shared<std::vector<uint8_t>>(event)))));
728 ASSERT_TRUE(complete_view.IsValid());
729 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, complete_view.GetStatus());
730 auto version = complete_view.GetLocalVersionInformation();
731 ASSERT_LE(::bluetooth::hci::HciVersion::V_3_0, version.hci_version_);
732 ASSERT_LE(::bluetooth::hci::LmpVersion::V_3_0, version.lmp_version_);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000733}
734
735// Send an unknown HCI command and wait for the error message.
736TEST_P(BluetoothAidlTest, HciUnknownCommand) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800737 std::vector<uint8_t> cmd;
738 ::bluetooth::packet::BitInserter bi{cmd};
739 ::bluetooth::hci::CommandBuilder::Create(
740 static_cast<::bluetooth::hci::OpCode>(0x3cff),
741 std::make_unique<::bluetooth::packet::RawBuilder>())
742 ->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000743 hci->sendHciCommand(cmd);
744
Myles Watsone1708c82023-01-18 17:07:58 -0800745 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000746
747 std::vector<uint8_t> event;
748 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800749 auto event_view =
750 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
751 std::make_shared<std::vector<uint8_t>>(event)));
752 ASSERT_TRUE(event_view.IsValid());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000753
Myles Watson51b8bae2023-01-27 14:22:24 -0800754 switch (event_view.GetEventCode()) {
755 case ::bluetooth::hci::EventCode::COMMAND_COMPLETE: {
756 auto command_complete =
757 ::bluetooth::hci::CommandCompleteView::Create(event_view);
758 ASSERT_TRUE(command_complete.IsValid());
759 ASSERT_EQ(command_complete.GetPayload()[0],
760 static_cast<uint8_t>(
761 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND));
762 } break;
763 case ::bluetooth::hci::EventCode::COMMAND_STATUS: {
764 auto command_status =
765 ::bluetooth::hci::CommandStatusView::Create(event_view);
766 ASSERT_TRUE(command_status.IsValid());
767 ASSERT_EQ(command_status.GetStatus(),
768 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND);
769 } break;
770 default:
771 ADD_FAILURE();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000772 }
773}
774
Myles Watsone1708c82023-01-18 17:07:58 -0800775// Enter loopback mode, but don't send any packets.
Myles Watson65b47f52023-01-26 12:59:06 -0800776TEST_P(BluetoothAidlTest, WriteLoopbackMode) { enterLoopbackMode(); }
Myles Watsone1708c82023-01-18 17:07:58 -0800777
778// Enter loopback mode and send a single command.
779TEST_P(BluetoothAidlTest, LoopbackModeSingleCommand) {
Myles Watsone1708c82023-01-18 17:07:58 -0800780 setBufferSizes();
781
782 enterLoopbackMode();
783
784 sendAndCheckHci(1);
785}
786
Myles Watsone1708c82023-01-18 17:07:58 -0800787// Enter loopback mode and send a single ACL packet.
788TEST_P(BluetoothAidlTest, LoopbackModeSingleAcl) {
Myles Watsone1708c82023-01-18 17:07:58 -0800789 setBufferSizes();
790
791 enterLoopbackMode();
792
793 if (!acl_connection_handles.empty()) {
794 ASSERT_LT(0, max_acl_data_packet_length);
795 sendAndCheckAcl(1, max_acl_data_packet_length - 1,
796 acl_connection_handles[0]);
797 int acl_packets_sent = 1;
798 int completed_packets =
799 wait_for_completed_packets_event(acl_connection_handles[0]);
800 if (acl_packets_sent != completed_packets) {
801 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
802 acl_packets_sent, completed_packets);
803 }
804 }
805 ASSERT_GE(acl_cb_count, 1);
806}
807
808// Enter loopback mode and send command packets for bandwidth measurements.
809TEST_P(BluetoothAidlTest, LoopbackModeCommandBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800810 setBufferSizes();
811
812 enterLoopbackMode();
813
814 sendAndCheckHci(kNumHciCommandsBandwidth);
815}
816
Myles Watsone1708c82023-01-18 17:07:58 -0800817// Enter loopback mode and send packets for ACL bandwidth measurements.
818TEST_P(BluetoothAidlTest, LoopbackModeAclBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800819 setBufferSizes();
820
821 enterLoopbackMode();
822
823 if (!acl_connection_handles.empty()) {
824 ASSERT_LT(0, max_acl_data_packet_length);
825 sendAndCheckAcl(kNumAclPacketsBandwidth, max_acl_data_packet_length - 1,
826 acl_connection_handles[0]);
827 int acl_packets_sent = kNumAclPacketsBandwidth;
828 int completed_packets =
829 wait_for_completed_packets_event(acl_connection_handles[0]);
830 if (acl_packets_sent != completed_packets) {
831 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
832 acl_packets_sent, completed_packets);
833 }
834 }
835}
836
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000837// Set all bits in the event mask
838TEST_P(BluetoothAidlTest, SetEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800839 std::vector<uint8_t> cmd;
840 ::bluetooth::packet::BitInserter bi{cmd};
841 uint64_t full_mask = UINT64_MAX;
842 ::bluetooth::hci::SetEventMaskBuilder::Create(full_mask)->Serialize(bi);
843 hci->sendHciCommand(cmd);
Jack He2d2f3c22023-03-27 03:04:52 -0700844 wait_and_validate_command_complete_event(
845 ::bluetooth::hci::OpCode::SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000846}
847
848// Set all bits in the LE event mask
849TEST_P(BluetoothAidlTest, SetLeEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800850 std::vector<uint8_t> cmd;
851 ::bluetooth::packet::BitInserter bi{cmd};
852 uint64_t full_mask = UINT64_MAX;
853 ::bluetooth::hci::LeSetEventMaskBuilder::Create(full_mask)->Serialize(bi);
854 hci->sendHciCommand(cmd);
Jack He2d2f3c22023-03-27 03:04:52 -0700855 wait_and_validate_command_complete_event(
856 ::bluetooth::hci::OpCode::LE_SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000857}
858
Myles Watson0daa1642023-01-26 15:58:28 -0800859// Call initialize twice, second one should fail.
860TEST_P(BluetoothAidlTest, CallInitializeTwice) {
861 class SecondCb
862 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
863 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700864 ndk::ScopedAStatus initializationComplete(Status status) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800865 EXPECT_EQ(status, Status::ALREADY_INITIALIZED);
866 init_promise.set_value();
867 return ScopedAStatus::ok();
868 };
869
Jack Hedbceaca2023-03-27 18:06:38 -0700870 ndk::ScopedAStatus hciEventReceived(
871 const std::vector<uint8_t>& /*event*/) 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 aclDataReceived(
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 scoDataReceived(
883 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800884 ADD_FAILURE();
885 return ScopedAStatus::ok();
886 };
887
Jack Hedbceaca2023-03-27 18:06:38 -0700888 ndk::ScopedAStatus isoDataReceived(
889 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800890 ADD_FAILURE();
891 return ScopedAStatus::ok();
892 };
893 std::promise<void> init_promise;
894 };
895
896 std::shared_ptr<SecondCb> second_cb = ndk::SharedRefBase::make<SecondCb>();
897 ASSERT_NE(second_cb, nullptr);
898
899 auto future = second_cb->init_promise.get_future();
900 ASSERT_TRUE(hci->initialize(second_cb).isOk());
901 auto status = future.wait_for(std::chrono::seconds(1));
902 ASSERT_EQ(status, std::future_status::ready);
903}
904
Henri Chataingf506e2d2024-09-18 21:51:39 +0000905// @VsrTest = 5.3.14-001
906// @VsrTest = 5.3.14-002
907// @VsrTest = 5.3.14-004
Myles Watson5bfb3a72023-05-10 14:06:32 -0700908TEST_P(BluetoothAidlTest, Vsr_Bluetooth5Requirements) {
Henri Chataingf506e2d2024-09-18 21:51:39 +0000909 int api_level = get_vsr_api_level();
910 if (api_level < __ANDROID_API_U__) {
911 GTEST_SKIP() << "API level is lower than 34";
912 return;
913 }
914
Jack He2d2f3c22023-03-27 03:04:52 -0700915 std::vector<uint8_t> version_event;
916 send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
917 version_event);
918 auto version_view = ReadLocalVersionInformationCompleteView::Create(
919 CommandCompleteView::Create(EventView::Create(PacketView<true>(
920 std::make_shared<std::vector<uint8_t>>(version_event)))));
921 ASSERT_TRUE(version_view.IsValid());
922 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
923 auto version = version_view.GetLocalVersionInformation();
Henri Chataingf506e2d2024-09-18 21:51:39 +0000924
Jack He2d2f3c22023-03-27 03:04:52 -0700925 if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) {
Henri Chataingf506e2d2024-09-18 21:51:39 +0000926 GTEST_SKIP() << "Bluetooth version is lower than 5.0";
Jack He2d2f3c22023-03-27 03:04:52 -0700927 return;
Henri Chataingf506e2d2024-09-18 21:51:39 +0000928 }
929
Jack He2d2f3c22023-03-27 03:04:52 -0700930 // When HCI version is 5.0, LMP version must also be at least 5.0
931 ASSERT_GE(static_cast<int>(version.lmp_version_),
932 static_cast<int>(version.hci_version_));
933
934 std::vector<uint8_t> le_features_event;
935 send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(),
936 le_features_event);
937 auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create(
938 CommandCompleteView::Create(EventView::Create(PacketView<true>(
939 std::make_shared<std::vector<uint8_t>>(le_features_event)))));
940 ASSERT_TRUE(le_features_view.IsValid());
941 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus());
Henri Chataingf506e2d2024-09-18 21:51:39 +0000942
943 // CHIPSETs that set ro.board.api_level to 34 and report 5.0 or higher for
944 // the Bluetooth version through the IBluetoothHci HAL:
945 //
946 // [VSR-5.3.14-001] Must return TRUE for
947 // - LE 2M PHY
948 // - LE Coded PHY
949 // - LE Advertising Extension
950 // - LE Periodic Advertising
951 // - LE Link Layer Privacy
Jack He2d2f3c22023-03-27 03:04:52 -0700952 auto le_features = le_features_view.GetLeFeatures();
953 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LL_PRIVACY));
954 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LE_2M_PHY));
955 ASSERT_TRUE(le_features &
956 static_cast<uint64_t>(LLFeaturesBits::LE_CODED_PHY));
957 ASSERT_TRUE(le_features &
958 static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING));
Henri Chataingf506e2d2024-09-18 21:51:39 +0000959 ASSERT_TRUE(le_features &
960 static_cast<uint64_t>(LLFeaturesBits::LE_PERIODIC_ADVERTISING));
Jack He2d2f3c22023-03-27 03:04:52 -0700961
962 std::vector<uint8_t> num_adv_set_event;
963 send_and_wait_for_cmd_complete(
964 LeReadNumberOfSupportedAdvertisingSetsBuilder::Create(),
965 num_adv_set_event);
966 auto num_adv_set_view =
967 LeReadNumberOfSupportedAdvertisingSetsCompleteView::Create(
968 CommandCompleteView::Create(EventView::Create(PacketView<true>(
969 std::make_shared<std::vector<uint8_t>>(num_adv_set_event)))));
970 ASSERT_TRUE(num_adv_set_view.IsValid());
971 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, num_adv_set_view.GetStatus());
972 auto num_adv_set = num_adv_set_view.GetNumberSupportedAdvertisingSets();
Treehugger Robot5e0277f2023-05-12 02:44:22 +0000973
Henri Chataingf506e2d2024-09-18 21:51:39 +0000974 // CHIPSETs that set ro.board.api_level to 34 and report 5.0 or higher for
975 // the Bluetooth version through the IBluetoothHci HAL:
976 //
977 // [VSR-5.3.14-002] MUST support at least 10 advertising sets.
Treehugger Robot5e0277f2023-05-12 02:44:22 +0000978 if (isTv() && get_vsr_api_level() == __ANDROID_API_U__) {
Myles Watson864c9042024-07-24 14:39:05 -0700979 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5ForTv);
Treehugger Robot5e0277f2023-05-12 02:44:22 +0000980 } else {
981 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5);
982 }
Jack He2d2f3c22023-03-27 03:04:52 -0700983
984 std::vector<uint8_t> num_resolving_list_event;
985 send_and_wait_for_cmd_complete(LeReadResolvingListSizeBuilder::Create(),
986 num_resolving_list_event);
987 auto num_resolving_list_view = LeReadResolvingListSizeCompleteView::Create(
988 CommandCompleteView::Create(EventView::Create(PacketView<true>(
989 std::make_shared<std::vector<uint8_t>>(num_resolving_list_event)))));
990 ASSERT_TRUE(num_resolving_list_view.IsValid());
991 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
992 num_resolving_list_view.GetStatus());
993 auto num_resolving_list = num_resolving_list_view.GetResolvingListSize();
Henri Chataingf506e2d2024-09-18 21:51:39 +0000994
995 // CHIPSETs that set ro.board.api_level to 34 and report 5.0 or higher for
996 // the Bluetooth version through the IBluetoothHci HAL:
997 //
998 // [VSR-5.3.14-004] MUST support a resolving list size of at least 8 entries.
Jack He2d2f3c22023-03-27 03:04:52 -0700999 ASSERT_GE(num_resolving_list, kMinLeResolvingListForBt5);
1000}
1001
Doug Ferraz83b22672024-03-22 19:54:26 -04001002/**
Doug Ferraz83b22672024-03-22 19:54:26 -04001003 * VSR-5.3.14-007 MUST support Bluetooth 4.2 and Bluetooth LE Data Length Extension.
1004 * VSR-5.3.14-008 MUST support Bluetooth Low Energy (BLE).
1005 */
Doug Ferrazeff7b912024-03-25 20:04:19 -04001006// @VsrTest = 5.3.14-007
1007// @VsrTest = 5.3.14-008
Doug Ferraz83b22672024-03-22 19:54:26 -04001008TEST_P(BluetoothAidlTest, Vsr_Bluetooth4_2Requirements) {
1009 // test only applies to handheld devices
1010 if (!isHandheld()) {
1011 return;
1012 }
1013
1014 std::vector<uint8_t> version_event;
1015 send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
1016 version_event);
1017 auto version_view = ReadLocalVersionInformationCompleteView::Create(
1018 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1019 std::make_shared<std::vector<uint8_t>>(version_event)))));
1020 ASSERT_TRUE(version_view.IsValid());
1021 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
1022 auto version = version_view.GetLocalVersionInformation();
1023 // Starting with Android 15, Fails when HCI version is lower than 4.2.
1024 ASSERT_GE(static_cast<int>(version.hci_version_),
1025 static_cast<int>(::bluetooth::hci::HciVersion::V_4_2));
1026 ASSERT_GE(static_cast<int>(version.lmp_version_),
1027 static_cast<int>(::bluetooth::hci::LmpVersion::V_4_2));
1028
1029 std::vector<uint8_t> le_features_event;
1030 send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(),
1031 le_features_event);
1032 auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create(
1033 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1034 std::make_shared<std::vector<uint8_t>>(le_features_event)))));
1035 ASSERT_TRUE(le_features_view.IsValid());
1036 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus());
1037 auto le_features = le_features_view.GetLeFeatures();
1038 ASSERT_TRUE(le_features &
1039 static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING));
1040
1041}
1042
Omair Kamil84a95be2024-04-22 13:29:19 -07001043/**
1044 * VSR-5.3.14-012 MUST support at least eight LE concurrent connections with
1045 * three in peripheral role.
1046 */
1047// @VsrTest = 5.3.14-012
1048TEST_P(BluetoothAidlTest, Vsr_BlE_Connection_Requirement) {
1049 std::vector<uint8_t> version_event;
1050 send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
1051 version_event);
1052 auto version_view = ReadLocalVersionInformationCompleteView::Create(
1053 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1054 std::make_shared<std::vector<uint8_t>>(version_event)))));
1055 ASSERT_TRUE(version_view.IsValid());
1056 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
1057 auto version = version_view.GetLocalVersionInformation();
1058 if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) {
1059 // This test does not apply to controllers below 5.0
1060 return;
1061 };
1062
1063 int max_connections = ::android::base::GetIntProperty(
1064 "bluetooth.core.le.max_number_of_concurrent_connections", -1);
1065 if (max_connections == -1) {
1066 // With the property not set the default minimum of 8 will be used
1067 ALOGI("Max number of LE concurrent connections isn't set");
1068 return;
1069 }
1070 ALOGI("Max number of LE concurrent connections = %d", max_connections);
1071 ASSERT_GE(max_connections, 8);
1072}
1073
Dylan Tian8a6e09c2022-08-16 07:29:28 +00001074GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothAidlTest);
1075INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothAidlTest,
1076 testing::ValuesIn(android::getAidlHalInstanceNames(
1077 IBluetoothHci::descriptor)),
1078 android::PrintInstanceNameToString);
1079
1080int main(int argc, char** argv) {
1081 ABinderProcess_startThreadPool();
1082 ::testing::InitGoogleTest(&argc, argv);
1083 int status = RUN_ALL_TESTS();
1084 ALOGI("Test result = %d", status);
1085 return status;
1086}