blob: aaf436f4aaa36683b934b177a371efed7c5debb5 [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
30#include <atomic>
31#include <chrono>
32#include <condition_variable>
33#include <future>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000034#include <queue>
35#include <thread>
Jack Hedbceaca2023-03-27 18:06:38 -070036#include <utility>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000037#include <vector>
38
Myles Watson51b8bae2023-01-27 14:22:24 -080039// TODO: Remove custom logging defines from PDL packets.
40#undef LOG_INFO
41#undef LOG_DEBUG
42#undef LOG_TAG
43#define LOG_TAG "VtsHalBluetooth"
44#include "hci/hci_packets.h"
45#include "packet/raw_builder.h"
46
Dylan Tian8a6e09c2022-08-16 07:29:28 +000047using aidl::android::hardware::bluetooth::IBluetoothHci;
48using aidl::android::hardware::bluetooth::IBluetoothHciCallbacks;
49using aidl::android::hardware::bluetooth::Status;
50using ndk::ScopedAStatus;
51using ndk::SpAIBinder;
52
Jack He2d2f3c22023-03-27 03:04:52 -070053using ::bluetooth::hci::CommandBuilder;
54using ::bluetooth::hci::CommandCompleteView;
55using ::bluetooth::hci::CommandView;
56using ::bluetooth::hci::ErrorCode;
57using ::bluetooth::hci::EventView;
58using ::bluetooth::hci::LeReadLocalSupportedFeaturesBuilder;
59using ::bluetooth::hci::LeReadLocalSupportedFeaturesCompleteView;
60using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsBuilder;
61using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsCompleteView;
62using ::bluetooth::hci::LeReadResolvingListSizeBuilder;
63using ::bluetooth::hci::LeReadResolvingListSizeCompleteView;
64using ::bluetooth::hci::LLFeaturesBits;
65using ::bluetooth::hci::OpCode;
66using ::bluetooth::hci::OpCodeText;
67using ::bluetooth::hci::PacketView;
68using ::bluetooth::hci::ReadLocalVersionInformationBuilder;
69using ::bluetooth::hci::ReadLocalVersionInformationCompleteView;
70
71static constexpr uint8_t kMinLeAdvSetForBt5 = 16;
Treehugger Robot5e0277f2023-05-12 02:44:22 +000072static constexpr uint8_t kMinLeAdvSetForBt5FoTv = 10;
Jack He2d2f3c22023-03-27 03:04:52 -070073static constexpr uint8_t kMinLeResolvingListForBt5 = 8;
74
Myles Watsone1708c82023-01-18 17:07:58 -080075static constexpr size_t kNumHciCommandsBandwidth = 100;
76static constexpr size_t kNumScoPacketsBandwidth = 100;
77static constexpr size_t kNumAclPacketsBandwidth = 100;
William Escandeac0f34f2024-04-11 09:06:31 -070078static constexpr std::chrono::milliseconds kWaitForInitTimeout(2000);
Dylan Tian8a6e09c2022-08-16 07:29:28 +000079static constexpr std::chrono::milliseconds kWaitForHciEventTimeout(2000);
Myles Watsone1708c82023-01-18 17:07:58 -080080static constexpr std::chrono::milliseconds kWaitForScoDataTimeout(1000);
81static constexpr std::chrono::milliseconds kWaitForAclDataTimeout(1000);
Dylan Tian8a6e09c2022-08-16 07:29:28 +000082static constexpr std::chrono::milliseconds kInterfaceCloseDelayMs(200);
83
Dylan Tian8a6e09c2022-08-16 07:29:28 +000084// To discard Qualcomm ACL debugging
85static constexpr uint16_t kAclHandleQcaDebugMessage = 0xedc;
86
Treehugger Robot5e0277f2023-05-12 02:44:22 +000087static int get_vsr_api_level() {
88 int vendor_api_level =
89 ::android::base::GetIntProperty("ro.vendor.api_level", -1);
90 if (vendor_api_level != -1) {
91 return vendor_api_level;
92 }
93
94 // Android S and older devices do not define ro.vendor.api_level
95 vendor_api_level = ::android::base::GetIntProperty("ro.board.api_level", -1);
96 if (vendor_api_level == -1) {
97 vendor_api_level =
98 ::android::base::GetIntProperty("ro.board.first_api_level", -1);
99 }
100
101 int product_api_level =
102 ::android::base::GetIntProperty("ro.product.first_api_level", -1);
103 if (product_api_level == -1) {
104 product_api_level =
105 ::android::base::GetIntProperty("ro.build.version.sdk", -1);
106 EXPECT_NE(product_api_level, -1) << "Could not find ro.build.version.sdk";
107 }
108
109 // VSR API level is the minimum of vendor_api_level and product_api_level.
110 if (vendor_api_level == -1 || vendor_api_level > product_api_level) {
111 return product_api_level;
112 }
113 return vendor_api_level;
114}
115
116static bool isTv() {
117 return testing::deviceSupportsFeature("android.software.leanback") ||
118 testing::deviceSupportsFeature("android.hardware.type.television");
119}
120
Doug Ferraz83b22672024-03-22 19:54:26 -0400121static bool isHandheld() {
122 return testing::deviceSupportsFeature("android.hardware.type.handheld");
123}
124
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000125class ThroughputLogger {
126 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700127 explicit ThroughputLogger(std::string task)
Myles Watsone1708c82023-01-18 17:07:58 -0800128 : total_bytes_(0),
Jack Hedbceaca2023-03-27 18:06:38 -0700129 task_(std::move(task)),
Myles Watsone1708c82023-01-18 17:07:58 -0800130 start_time_(std::chrono::steady_clock::now()) {}
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000131
132 ~ThroughputLogger() {
133 if (total_bytes_ == 0) {
134 return;
135 }
136 std::chrono::duration<double> duration =
137 std::chrono::steady_clock::now() - start_time_;
138 double s = duration.count();
139 if (s == 0) {
140 return;
141 }
142 double rate_kb = (static_cast<double>(total_bytes_) / s) / 1024;
143 ALOGD("%s %.1f KB/s (%zu bytes in %.3fs)", task_.c_str(), rate_kb,
144 total_bytes_, s);
145 }
146
147 void setTotalBytes(size_t total_bytes) { total_bytes_ = total_bytes; }
148
149 private:
150 size_t total_bytes_;
151 std::string task_;
152 std::chrono::steady_clock::time_point start_time_;
153};
154
155// The main test class for Bluetooth HAL.
156class BluetoothAidlTest : public ::testing::TestWithParam<std::string> {
William Escande1a139902024-02-27 15:57:33 -0800157 std::chrono::time_point<std::chrono::system_clock>
158 time_after_initialize_complete;
159
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000160 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700161 void SetUp() override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000162 // currently test passthrough mode only
163 hci = IBluetoothHci::fromBinder(
164 SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
165 ASSERT_NE(hci, nullptr);
166 ALOGI("%s: getService() for bluetooth hci is %s", __func__,
167 hci->isRemote() ? "remote" : "local");
168
169 // Lambda function
170 auto on_binder_death = [](void* /*cookie*/) { FAIL(); };
171
172 bluetooth_hci_death_recipient =
173 AIBinder_DeathRecipient_new(on_binder_death);
174 ASSERT_NE(bluetooth_hci_death_recipient, nullptr);
175 ASSERT_EQ(STATUS_OK,
176 AIBinder_linkToDeath(hci->asBinder().get(),
Jack Hedbceaca2023-03-27 18:06:38 -0700177 bluetooth_hci_death_recipient, nullptr));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000178
179 hci_cb = ndk::SharedRefBase::make<BluetoothHciCallbacks>(*this);
180 ASSERT_NE(hci_cb, nullptr);
181
182 max_acl_data_packet_length = 0;
183 max_sco_data_packet_length = 0;
184 max_acl_data_packets = 0;
185 max_sco_data_packets = 0;
186
187 event_cb_count = 0;
188 acl_cb_count = 0;
189 sco_cb_count = 0;
William Escande1a139902024-02-27 15:57:33 -0800190 std::chrono::time_point<std::chrono::system_clock>
191 timeout_after_initialize =
192 std::chrono::system_clock::now() + kWaitForInitTimeout;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000193
194 ASSERT_TRUE(hci->initialize(hci_cb).isOk());
195 auto future = initialized_promise.get_future();
196 auto timeout_status = future.wait_for(kWaitForInitTimeout);
197 ASSERT_EQ(timeout_status, std::future_status::ready);
198 ASSERT_TRUE(future.get());
William Escande1a139902024-02-27 15:57:33 -0800199 ASSERT_GE(timeout_after_initialize, time_after_initialize_complete);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000200 }
201
Jack Hedbceaca2023-03-27 18:06:38 -0700202 void TearDown() override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000203 ALOGI("TearDown");
204 // Should not be checked in production code
205 ASSERT_TRUE(hci->close().isOk());
206 std::this_thread::sleep_for(kInterfaceCloseDelayMs);
207 handle_no_ops();
Myles Watsone1708c82023-01-18 17:07:58 -0800208 discard_qca_debugging();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000209 EXPECT_EQ(static_cast<size_t>(0), event_queue.size());
210 EXPECT_EQ(static_cast<size_t>(0), sco_queue.size());
211 EXPECT_EQ(static_cast<size_t>(0), acl_queue.size());
212 EXPECT_EQ(static_cast<size_t>(0), iso_queue.size());
213 }
214
215 void setBufferSizes();
Myles Watsone1708c82023-01-18 17:07:58 -0800216 void setSynchronousFlowControlEnable();
217
218 // Functions called from within tests in loopback mode
219 void sendAndCheckHci(int num_packets);
220 void sendAndCheckSco(int num_packets, size_t size, uint16_t handle);
221 void sendAndCheckAcl(int num_packets, size_t size, uint16_t handle);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000222
223 // Helper functions to try to get a handle on verbosity
Myles Watsone1708c82023-01-18 17:07:58 -0800224 void enterLoopbackMode();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000225 void handle_no_ops();
Myles Watsone1708c82023-01-18 17:07:58 -0800226 void discard_qca_debugging();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000227 void wait_for_event(bool timeout_is_error);
Jack He2d2f3c22023-03-27 03:04:52 -0700228 void wait_for_command_complete_event(OpCode opCode,
229 std::vector<uint8_t>& complete_event);
230 // Wait until a command complete is received.
231 // Command complete will be consumed after this method
232 void wait_and_validate_command_complete_event(OpCode opCode);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000233 int wait_for_completed_packets_event(uint16_t handle);
Jack He2d2f3c22023-03-27 03:04:52 -0700234 void send_and_wait_for_cmd_complete(std::unique_ptr<CommandBuilder> cmd,
235 std::vector<uint8_t>& cmd_complete);
xiaoshun.xu4e85c092023-07-11 17:30:04 +0800236 void reassemble_sco_loopback_pkt(std::vector<uint8_t>& scoPackets,
237 size_t size);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000238
239 // A simple test implementation of BluetoothHciCallbacks.
240 class BluetoothHciCallbacks
241 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
242 BluetoothAidlTest& parent_;
243
244 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700245 explicit BluetoothHciCallbacks(BluetoothAidlTest& parent)
246 : parent_(parent){};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000247
Jack Hedbceaca2023-03-27 18:06:38 -0700248 ~BluetoothHciCallbacks() override = default;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000249
Jack Hedbceaca2023-03-27 18:06:38 -0700250 ndk::ScopedAStatus initializationComplete(Status status) override {
William Escande1a139902024-02-27 15:57:33 -0800251 if (status == Status::SUCCESS) {
252 parent_.time_after_initialize_complete =
253 std::chrono::system_clock::now();
254 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000255 parent_.initialized_promise.set_value(status == Status::SUCCESS);
256 ALOGV("%s (status = %d)", __func__, static_cast<int>(status));
257 return ScopedAStatus::ok();
258 };
259
Jack Hedbceaca2023-03-27 18:06:38 -0700260 ndk::ScopedAStatus hciEventReceived(
261 const std::vector<uint8_t>& event) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000262 parent_.event_cb_count++;
263 parent_.event_queue.push(event);
Jack He2d2f3c22023-03-27 03:04:52 -0700264 ALOGI("Event received (length = %d)", static_cast<int>(event.size()));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000265 return ScopedAStatus::ok();
266 };
267
Jack Hedbceaca2023-03-27 18:06:38 -0700268 ndk::ScopedAStatus aclDataReceived(
269 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000270 parent_.acl_cb_count++;
271 parent_.acl_queue.push(data);
272 return ScopedAStatus::ok();
273 };
274
Jack Hedbceaca2023-03-27 18:06:38 -0700275 ndk::ScopedAStatus scoDataReceived(
276 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000277 parent_.sco_cb_count++;
278 parent_.sco_queue.push(data);
279 return ScopedAStatus::ok();
280 };
281
Jack Hedbceaca2023-03-27 18:06:38 -0700282 ndk::ScopedAStatus isoDataReceived(
283 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000284 parent_.iso_cb_count++;
285 parent_.iso_queue.push(data);
286 return ScopedAStatus::ok();
287 };
288 };
289
290 template <class T>
291 class WaitQueue {
292 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700293 WaitQueue() = default;
294 ;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000295
296 virtual ~WaitQueue() = default;
297
298 bool empty() const {
299 std::lock_guard<std::mutex> lock(m_);
300 return q_.empty();
301 };
302
303 size_t size() const {
304 std::lock_guard<std::mutex> lock(m_);
305 return q_.size();
306 };
307
308 void push(const T& v) {
309 std::lock_guard<std::mutex> lock(m_);
310 q_.push(v);
311 ready_.notify_one();
312 };
313
314 bool pop(T& v) {
315 std::lock_guard<std::mutex> lock(m_);
316 if (q_.empty()) {
317 return false;
318 }
319 v = std::move(q_.front());
320 q_.pop();
321 return true;
322 };
323
Myles Watson51b8bae2023-01-27 14:22:24 -0800324 void pop() {
325 std::lock_guard<std::mutex> lock(m_);
326 if (q_.empty()) {
327 return;
328 }
329 q_.pop();
330 };
331
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000332 bool front(T& v) {
333 std::lock_guard<std::mutex> lock(m_);
334 if (q_.empty()) {
335 return false;
336 }
337 v = q_.front();
338 return true;
339 };
340
341 void wait() {
342 std::unique_lock<std::mutex> lock(m_);
343 while (q_.empty()) {
344 ready_.wait(lock);
345 }
346 };
347
348 bool waitWithTimeout(std::chrono::milliseconds timeout) {
349 std::unique_lock<std::mutex> lock(m_);
350 while (q_.empty()) {
351 if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
352 return false;
353 }
354 }
355 return true;
356 };
357
358 bool tryPopWithTimeout(T& v, std::chrono::milliseconds timeout) {
359 std::unique_lock<std::mutex> lock(m_);
360 while (q_.empty()) {
361 if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
362 return false;
363 }
364 }
365 v = std::move(q_.front());
366 q_.pop();
367 return true;
368 };
369
370 private:
371 mutable std::mutex m_;
372 std::queue<T> q_;
373 std::condition_variable_any ready_;
374 };
375
376 std::shared_ptr<IBluetoothHci> hci;
377 std::shared_ptr<BluetoothHciCallbacks> hci_cb;
Jack Hedbceaca2023-03-27 18:06:38 -0700378 AIBinder_DeathRecipient* bluetooth_hci_death_recipient{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000379 WaitQueue<std::vector<uint8_t>> event_queue;
380 WaitQueue<std::vector<uint8_t>> acl_queue;
381 WaitQueue<std::vector<uint8_t>> sco_queue;
382 WaitQueue<std::vector<uint8_t>> iso_queue;
383
384 std::promise<bool> initialized_promise;
Jack Hedbceaca2023-03-27 18:06:38 -0700385 int event_cb_count{};
386 int sco_cb_count{};
387 int acl_cb_count{};
388 int iso_cb_count{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000389
Jack Hedbceaca2023-03-27 18:06:38 -0700390 int max_acl_data_packet_length{};
391 int max_sco_data_packet_length{};
392 int max_acl_data_packets{};
393 int max_sco_data_packets{};
Myles Watsone1708c82023-01-18 17:07:58 -0800394
395 std::vector<uint16_t> sco_connection_handles;
396 std::vector<uint16_t> acl_connection_handles;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000397};
398
399// Discard NO-OPs from the event queue.
400void BluetoothAidlTest::handle_no_ops() {
401 while (!event_queue.empty()) {
402 std::vector<uint8_t> event;
403 event_queue.front(event);
Myles Watson51b8bae2023-01-27 14:22:24 -0800404 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
405 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
406 std::make_shared<std::vector<uint8_t>>(event))));
407 auto status_view = ::bluetooth::hci::CommandCompleteView::Create(
408 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
409 std::make_shared<std::vector<uint8_t>>(event))));
410 bool is_complete_no_op =
411 complete_view.IsValid() &&
412 complete_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
413 bool is_status_no_op =
414 status_view.IsValid() &&
415 status_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
416 if (is_complete_no_op || is_status_no_op) {
417 event_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000418 } else {
419 break;
420 }
421 }
Myles Watsone1708c82023-01-18 17:07:58 -0800422}
423
424// Discard Qualcomm ACL debugging
425void BluetoothAidlTest::discard_qca_debugging() {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000426 while (!acl_queue.empty()) {
427 std::vector<uint8_t> acl_packet;
428 acl_queue.front(acl_packet);
Myles Watson51b8bae2023-01-27 14:22:24 -0800429 auto acl_view =
430 ::bluetooth::hci::AclView::Create(::bluetooth::hci::PacketView<true>(
431 std::make_shared<std::vector<uint8_t>>(acl_packet)));
432 EXPECT_TRUE(acl_view.IsValid());
433 if (acl_view.GetHandle() == kAclHandleQcaDebugMessage) {
434 acl_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000435 } else {
436 break;
437 }
438 }
439}
440
441// Receive an event, discarding NO-OPs.
442void BluetoothAidlTest::wait_for_event(bool timeout_is_error = true) {
Myles Watsone1708c82023-01-18 17:07:58 -0800443 // Wait until we get something that's not a no-op.
444 while (true) {
445 bool event_ready = event_queue.waitWithTimeout(kWaitForHciEventTimeout);
446 ASSERT_TRUE(event_ready || !timeout_is_error);
447 if (event_queue.empty()) {
448 // waitWithTimeout timed out
449 return;
450 }
451 handle_no_ops();
452 if (!event_queue.empty()) {
453 // There's an event in the queue that's not a no-op.
454 return;
455 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000456 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000457}
458
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000459void BluetoothAidlTest::wait_for_command_complete_event(
Jack He2d2f3c22023-03-27 03:04:52 -0700460 OpCode opCode, std::vector<uint8_t>& complete_event) {
Myles Watsone1708c82023-01-18 17:07:58 -0800461 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Myles Watsone1708c82023-01-18 17:07:58 -0800462 ASSERT_FALSE(event_queue.empty());
Jack He2d2f3c22023-03-27 03:04:52 -0700463 ASSERT_TRUE(event_queue.pop(complete_event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800464 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
465 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
Jack He2d2f3c22023-03-27 03:04:52 -0700466 std::make_shared<std::vector<uint8_t>>(complete_event))));
Myles Watson51b8bae2023-01-27 14:22:24 -0800467 ASSERT_TRUE(complete_view.IsValid());
468 ASSERT_EQ(complete_view.GetCommandOpCode(), opCode);
469 ASSERT_EQ(complete_view.GetPayload()[0],
470 static_cast<uint8_t>(::bluetooth::hci::ErrorCode::SUCCESS));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000471}
472
Jack He2d2f3c22023-03-27 03:04:52 -0700473void BluetoothAidlTest::wait_and_validate_command_complete_event(
474 ::bluetooth::hci::OpCode opCode) {
475 std::vector<uint8_t> complete_event;
476 ASSERT_NO_FATAL_FAILURE(
477 wait_for_command_complete_event(opCode, complete_event));
478}
479
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000480// Send the command to read the controller's buffer sizes.
481void BluetoothAidlTest::setBufferSizes() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800482 std::vector<uint8_t> cmd;
483 ::bluetooth::packet::BitInserter bi{cmd};
484 ::bluetooth::hci::ReadBufferSizeBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000485 hci->sendHciCommand(cmd);
486
Myles Watsone1708c82023-01-18 17:07:58 -0800487 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000488 std::vector<uint8_t> event;
489 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800490 auto complete_view = ::bluetooth::hci::ReadBufferSizeCompleteView::Create(
491 ::bluetooth::hci::CommandCompleteView::Create(
492 ::bluetooth::hci::EventView::Create(
493 ::bluetooth::hci::PacketView<true>(
494 std::make_shared<std::vector<uint8_t>>(event)))));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000495
Myles Watson51b8bae2023-01-27 14:22:24 -0800496 ASSERT_TRUE(complete_view.IsValid());
497 ASSERT_EQ(complete_view.GetStatus(), ::bluetooth::hci::ErrorCode::SUCCESS);
498 max_acl_data_packet_length = complete_view.GetAclDataPacketLength();
499 max_sco_data_packet_length = complete_view.GetSynchronousDataPacketLength();
500 max_acl_data_packets = complete_view.GetTotalNumAclDataPackets();
501 max_sco_data_packets = complete_view.GetTotalNumSynchronousDataPackets();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000502
503 ALOGD("%s: ACL max %d num %d SCO max %d num %d", __func__,
504 static_cast<int>(max_acl_data_packet_length),
505 static_cast<int>(max_acl_data_packets),
506 static_cast<int>(max_sco_data_packet_length),
507 static_cast<int>(max_sco_data_packets));
508}
509
Myles Watsone1708c82023-01-18 17:07:58 -0800510// Enable flow control packets for SCO
511void BluetoothAidlTest::setSynchronousFlowControlEnable() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800512 std::vector<uint8_t> cmd;
513 ::bluetooth::packet::BitInserter bi{cmd};
514 ::bluetooth::hci::WriteSynchronousFlowControlEnableBuilder::Create(
515 ::bluetooth::hci::Enable::ENABLED)
516 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800517 hci->sendHciCommand(cmd);
518
Jack He2d2f3c22023-03-27 03:04:52 -0700519 wait_and_validate_command_complete_event(
Myles Watson51b8bae2023-01-27 14:22:24 -0800520 ::bluetooth::hci::OpCode::WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE);
Myles Watsone1708c82023-01-18 17:07:58 -0800521}
522
523// Send an HCI command (in Loopback mode) and check the response.
524void BluetoothAidlTest::sendAndCheckHci(int num_packets) {
Jack Hedbceaca2023-03-27 18:06:38 -0700525 ThroughputLogger logger{__func__};
526 size_t command_size = 0;
Myles Watson51b8bae2023-01-27 14:22:24 -0800527 char new_name[] = "John Jacob Jingleheimer Schmidt ___________________";
528 size_t new_name_length = strlen(new_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800529 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800530 // The name to set is new_name
Jack Hedbceaca2023-03-27 18:06:38 -0700531 std::array<uint8_t, 248> name_array{};
Myles Watsone1708c82023-01-18 17:07:58 -0800532 for (size_t i = 0; i < new_name_length; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800533 name_array[i] = new_name[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800534 }
535 // And the packet number
Myles Watson51b8bae2023-01-27 14:22:24 -0800536 char number[11] = "0000000000";
537 snprintf(number, sizeof(number), "%010d", static_cast<int>(n));
538 for (size_t i = new_name_length; i < new_name_length + sizeof(number) - 1;
539 i++) {
540 name_array[new_name_length + i] = number[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800541 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800542 std::vector<uint8_t> write_name;
543 ::bluetooth::packet::BitInserter bi{write_name};
544 ::bluetooth::hci::WriteLocalNameBuilder::Create(name_array)->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800545 hci->sendHciCommand(write_name);
546
547 // Check the loopback of the HCI packet
548 ASSERT_NO_FATAL_FAILURE(wait_for_event());
549
550 std::vector<uint8_t> event;
551 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800552 auto event_view = ::bluetooth::hci::LoopbackCommandView::Create(
553 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
554 std::make_shared<std::vector<uint8_t>>(event))));
555 ASSERT_TRUE(event_view.IsValid());
556 std::vector<uint8_t> looped_back_command{event_view.GetPayload().begin(),
557 event_view.GetPayload().end()};
558 ASSERT_EQ(looped_back_command, write_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800559
560 if (n == num_packets - 1) {
561 command_size = write_name.size();
562 }
Myles Watsone1708c82023-01-18 17:07:58 -0800563 }
564 logger.setTotalBytes(command_size * num_packets * 2);
565}
566
567// Send a SCO data packet (in Loopback mode) and check the response.
568void BluetoothAidlTest::sendAndCheckSco(int num_packets, size_t size,
569 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700570 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800571 for (int n = 0; n < num_packets; n++) {
572 // Send a SCO packet
573 std::vector<uint8_t> sco_packet;
Myles Watson51b8bae2023-01-27 14:22:24 -0800574 std::vector<uint8_t> payload;
Myles Watsone1708c82023-01-18 17:07:58 -0800575 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800576 payload.push_back(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800577 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800578 ::bluetooth::packet::BitInserter bi{sco_packet};
579 ::bluetooth::hci::ScoBuilder::Create(
580 handle, ::bluetooth::hci::PacketStatusFlag::CORRECTLY_RECEIVED, payload)
581 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800582 hci->sendScoData(sco_packet);
583
584 // Check the loopback of the SCO packet
585 std::vector<uint8_t> sco_loopback;
586 ASSERT_TRUE(
587 sco_queue.tryPopWithTimeout(sco_loopback, kWaitForScoDataTimeout));
588
xiaoshun.xu4e85c092023-07-11 17:30:04 +0800589 if (sco_loopback.size() < size) {
590 // The packets may have been split for USB. Reassemble before checking.
591 reassemble_sco_loopback_pkt(sco_loopback, size);
592 }
593
Myles Watson51b8bae2023-01-27 14:22:24 -0800594 ASSERT_EQ(sco_packet, sco_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800595 }
596 logger.setTotalBytes(num_packets * size * 2);
597}
598
599// Send an ACL data packet (in Loopback mode) and check the response.
600void BluetoothAidlTest::sendAndCheckAcl(int num_packets, size_t size,
601 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700602 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800603 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800604 // Send an ACL packet with counting data
605 auto payload = std::make_unique<::bluetooth::packet::RawBuilder>();
Myles Watsone1708c82023-01-18 17:07:58 -0800606 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800607 payload->AddOctets1(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800608 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800609 std::vector<uint8_t> acl_packet;
610 ::bluetooth::packet::BitInserter bi{acl_packet};
611 ::bluetooth::hci::AclBuilder::Create(
612 handle,
613 ::bluetooth::hci::PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE,
614 ::bluetooth::hci::BroadcastFlag::POINT_TO_POINT, std::move(payload))
615 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800616 hci->sendAclData(acl_packet);
617
618 std::vector<uint8_t> acl_loopback;
619 // Check the loopback of the ACL packet
620 ASSERT_TRUE(
621 acl_queue.tryPopWithTimeout(acl_loopback, kWaitForAclDataTimeout));
622
Myles Watson51b8bae2023-01-27 14:22:24 -0800623 ASSERT_EQ(acl_packet, acl_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800624 }
625 logger.setTotalBytes(num_packets * size * 2);
626}
627
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000628// Return the number of completed packets reported by the controller.
629int BluetoothAidlTest::wait_for_completed_packets_event(uint16_t handle) {
630 int packets_processed = 0;
Myles Watson65b47f52023-01-26 12:59:06 -0800631 while (true) {
632 // There should be at least one event.
633 wait_for_event(packets_processed == 0);
634 if (event_queue.empty()) {
635 if (packets_processed == 0) {
636 ALOGW("%s: waitForBluetoothCallback timed out.", __func__);
637 }
638 return packets_processed;
639 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000640 std::vector<uint8_t> event;
641 EXPECT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800642 auto event_view = ::bluetooth::hci::NumberOfCompletedPacketsView::Create(
643 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
644 std::make_shared<std::vector<uint8_t>>(event))));
645 if (!event_view.IsValid()) {
646 ADD_FAILURE();
647 return packets_processed;
648 }
649 auto completed_packets = event_view.GetCompletedPackets();
Jack Hedbceaca2023-03-27 18:06:38 -0700650 for (const auto& entry : completed_packets) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800651 EXPECT_EQ(handle, entry.connection_handle_);
652 packets_processed += entry.host_num_of_completed_packets_;
653 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000654 }
655 return packets_processed;
656}
657
Myles Watsone1708c82023-01-18 17:07:58 -0800658// Send local loopback command and initialize SCO and ACL handles.
659void BluetoothAidlTest::enterLoopbackMode() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800660 std::vector<uint8_t> cmd;
661 ::bluetooth::packet::BitInserter bi{cmd};
662 ::bluetooth::hci::WriteLoopbackModeBuilder::Create(
663 bluetooth::hci::LoopbackMode::ENABLE_LOCAL)
664 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800665 hci->sendHciCommand(cmd);
666
667 // Receive connection complete events with data channels
668 int connection_event_count = 0;
669 bool command_complete_received = false;
670 while (true) {
671 wait_for_event(false);
672 if (event_queue.empty()) {
673 // Fail if there was no event received or no connections completed.
674 ASSERT_TRUE(command_complete_received);
675 ASSERT_LT(0, connection_event_count);
676 return;
677 }
678 std::vector<uint8_t> event;
679 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800680 auto event_view =
681 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
682 std::make_shared<std::vector<uint8_t>>(event)));
683 ASSERT_TRUE(event_view.IsValid());
Myles Watsone1708c82023-01-18 17:07:58 -0800684
Myles Watson51b8bae2023-01-27 14:22:24 -0800685 if (event_view.GetEventCode() ==
686 ::bluetooth::hci::EventCode::CONNECTION_COMPLETE) {
687 auto complete_view =
688 ::bluetooth::hci::ConnectionCompleteView::Create(event_view);
689 ASSERT_TRUE(complete_view.IsValid());
690 switch (complete_view.GetLinkType()) {
691 case ::bluetooth::hci::LinkType::ACL:
692 acl_connection_handles.push_back(complete_view.GetConnectionHandle());
693 break;
694 case ::bluetooth::hci::LinkType::SCO:
695 sco_connection_handles.push_back(complete_view.GetConnectionHandle());
696 break;
697 default:
698 ASSERT_EQ(complete_view.GetLinkType(),
699 ::bluetooth::hci::LinkType::ACL);
Myles Watsone1708c82023-01-18 17:07:58 -0800700 }
Myles Watsone1708c82023-01-18 17:07:58 -0800701 connection_event_count++;
702 } else {
Myles Watson51b8bae2023-01-27 14:22:24 -0800703 auto command_complete_view =
704 ::bluetooth::hci::WriteLoopbackModeCompleteView::Create(
705 ::bluetooth::hci::CommandCompleteView::Create(event_view));
706 ASSERT_TRUE(command_complete_view.IsValid());
707 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
708 command_complete_view.GetStatus());
Myles Watsone1708c82023-01-18 17:07:58 -0800709 command_complete_received = true;
710 }
711 }
712}
713
Jack He2d2f3c22023-03-27 03:04:52 -0700714void BluetoothAidlTest::send_and_wait_for_cmd_complete(
715 std::unique_ptr<CommandBuilder> cmd, std::vector<uint8_t>& cmd_complete) {
716 std::vector<uint8_t> cmd_bytes = cmd->SerializeToBytes();
717 hci->sendHciCommand(cmd_bytes);
718
719 auto view = CommandView::Create(
720 PacketView<true>(std::make_shared<std::vector<uint8_t>>(cmd_bytes)));
721 ASSERT_TRUE(view.IsValid());
722 ALOGI("Waiting for %s[0x%x]", OpCodeText(view.GetOpCode()).c_str(),
723 static_cast<int>(view.GetOpCode()));
724 ASSERT_NO_FATAL_FAILURE(
725 wait_for_command_complete_event(view.GetOpCode(), cmd_complete));
726}
727
xiaoshun.xu4e85c092023-07-11 17:30:04 +0800728// Handle the loopback packet.
729void BluetoothAidlTest::reassemble_sco_loopback_pkt(std::vector<uint8_t>& scoPackets,
730 size_t size) {
731 std::vector<uint8_t> sco_packet_whole;
732 sco_packet_whole.assign(scoPackets.begin(), scoPackets.end());
733 while (size + 3 > sco_packet_whole.size()) {
734 std::vector<uint8_t> sco_packets;
735 ASSERT_TRUE(
736 sco_queue.tryPopWithTimeout(sco_packets, kWaitForScoDataTimeout));
737 sco_packet_whole.insert(sco_packet_whole.end(), sco_packets.begin() + 3,
738 sco_packets.end());
739 }
740 scoPackets.assign(sco_packet_whole.begin(), sco_packet_whole.end());
741 scoPackets[2] = size;
742}
743
Myles Watsone1708c82023-01-18 17:07:58 -0800744// Empty test: Initialize()/Close() are called in SetUp()/TearDown().
745TEST_P(BluetoothAidlTest, InitializeAndClose) {}
746
747// Send an HCI Reset with sendHciCommand and wait for a command complete event.
Myles Watson65b47f52023-01-26 12:59:06 -0800748TEST_P(BluetoothAidlTest, HciReset) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800749 std::vector<uint8_t> reset;
750 ::bluetooth::packet::BitInserter bi{reset};
751 ::bluetooth::hci::ResetBuilder::Create()->Serialize(bi);
Myles Watson65b47f52023-01-26 12:59:06 -0800752 hci->sendHciCommand(reset);
753
Jack He2d2f3c22023-03-27 03:04:52 -0700754 wait_and_validate_command_complete_event(::bluetooth::hci::OpCode::RESET);
Myles Watson65b47f52023-01-26 12:59:06 -0800755}
Myles Watsone1708c82023-01-18 17:07:58 -0800756
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000757// Read and check the HCI version of the controller.
758TEST_P(BluetoothAidlTest, HciVersionTest) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800759 std::vector<uint8_t> cmd;
760 ::bluetooth::packet::BitInserter bi{cmd};
761 ::bluetooth::hci::ReadLocalVersionInformationBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000762 hci->sendHciCommand(cmd);
763
Myles Watsone1708c82023-01-18 17:07:58 -0800764 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000765
766 std::vector<uint8_t> event;
767 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800768 auto complete_view =
769 ::bluetooth::hci::ReadLocalVersionInformationCompleteView::Create(
770 ::bluetooth::hci::CommandCompleteView::Create(
771 ::bluetooth::hci::EventView::Create(
772 ::bluetooth::hci::PacketView<true>(
773 std::make_shared<std::vector<uint8_t>>(event)))));
774 ASSERT_TRUE(complete_view.IsValid());
775 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, complete_view.GetStatus());
776 auto version = complete_view.GetLocalVersionInformation();
777 ASSERT_LE(::bluetooth::hci::HciVersion::V_3_0, version.hci_version_);
778 ASSERT_LE(::bluetooth::hci::LmpVersion::V_3_0, version.lmp_version_);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000779}
780
781// Send an unknown HCI command and wait for the error message.
782TEST_P(BluetoothAidlTest, HciUnknownCommand) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800783 std::vector<uint8_t> cmd;
784 ::bluetooth::packet::BitInserter bi{cmd};
785 ::bluetooth::hci::CommandBuilder::Create(
786 static_cast<::bluetooth::hci::OpCode>(0x3cff),
787 std::make_unique<::bluetooth::packet::RawBuilder>())
788 ->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000789 hci->sendHciCommand(cmd);
790
Myles Watsone1708c82023-01-18 17:07:58 -0800791 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000792
793 std::vector<uint8_t> event;
794 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800795 auto event_view =
796 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
797 std::make_shared<std::vector<uint8_t>>(event)));
798 ASSERT_TRUE(event_view.IsValid());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000799
Myles Watson51b8bae2023-01-27 14:22:24 -0800800 switch (event_view.GetEventCode()) {
801 case ::bluetooth::hci::EventCode::COMMAND_COMPLETE: {
802 auto command_complete =
803 ::bluetooth::hci::CommandCompleteView::Create(event_view);
804 ASSERT_TRUE(command_complete.IsValid());
805 ASSERT_EQ(command_complete.GetPayload()[0],
806 static_cast<uint8_t>(
807 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND));
808 } break;
809 case ::bluetooth::hci::EventCode::COMMAND_STATUS: {
810 auto command_status =
811 ::bluetooth::hci::CommandStatusView::Create(event_view);
812 ASSERT_TRUE(command_status.IsValid());
813 ASSERT_EQ(command_status.GetStatus(),
814 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND);
815 } break;
816 default:
817 ADD_FAILURE();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000818 }
819}
820
Myles Watsone1708c82023-01-18 17:07:58 -0800821// Enter loopback mode, but don't send any packets.
Myles Watson65b47f52023-01-26 12:59:06 -0800822TEST_P(BluetoothAidlTest, WriteLoopbackMode) { enterLoopbackMode(); }
Myles Watsone1708c82023-01-18 17:07:58 -0800823
824// Enter loopback mode and send a single command.
825TEST_P(BluetoothAidlTest, LoopbackModeSingleCommand) {
Myles Watsone1708c82023-01-18 17:07:58 -0800826 setBufferSizes();
827
828 enterLoopbackMode();
829
830 sendAndCheckHci(1);
831}
832
833// Enter loopback mode and send a single SCO packet.
834TEST_P(BluetoothAidlTest, LoopbackModeSingleSco) {
Myles Watsone1708c82023-01-18 17:07:58 -0800835 setBufferSizes();
836 setSynchronousFlowControlEnable();
837
838 enterLoopbackMode();
839
840 if (!sco_connection_handles.empty()) {
841 ASSERT_LT(0, max_sco_data_packet_length);
842 sendAndCheckSco(1, max_sco_data_packet_length, sco_connection_handles[0]);
843 int sco_packets_sent = 1;
844 int completed_packets =
845 wait_for_completed_packets_event(sco_connection_handles[0]);
846 if (sco_packets_sent != completed_packets) {
847 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
848 sco_packets_sent, completed_packets);
849 }
850 }
851}
852
853// Enter loopback mode and send a single ACL packet.
854TEST_P(BluetoothAidlTest, LoopbackModeSingleAcl) {
Myles Watsone1708c82023-01-18 17:07:58 -0800855 setBufferSizes();
856
857 enterLoopbackMode();
858
859 if (!acl_connection_handles.empty()) {
860 ASSERT_LT(0, max_acl_data_packet_length);
861 sendAndCheckAcl(1, max_acl_data_packet_length - 1,
862 acl_connection_handles[0]);
863 int acl_packets_sent = 1;
864 int completed_packets =
865 wait_for_completed_packets_event(acl_connection_handles[0]);
866 if (acl_packets_sent != completed_packets) {
867 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
868 acl_packets_sent, completed_packets);
869 }
870 }
871 ASSERT_GE(acl_cb_count, 1);
872}
873
874// Enter loopback mode and send command packets for bandwidth measurements.
875TEST_P(BluetoothAidlTest, LoopbackModeCommandBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800876 setBufferSizes();
877
878 enterLoopbackMode();
879
880 sendAndCheckHci(kNumHciCommandsBandwidth);
881}
882
883// Enter loopback mode and send SCO packets for bandwidth measurements.
884TEST_P(BluetoothAidlTest, LoopbackModeScoBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800885 setBufferSizes();
886 setSynchronousFlowControlEnable();
887
888 enterLoopbackMode();
889
890 if (!sco_connection_handles.empty()) {
891 ASSERT_LT(0, max_sco_data_packet_length);
892 sendAndCheckSco(kNumScoPacketsBandwidth, max_sco_data_packet_length,
893 sco_connection_handles[0]);
894 int sco_packets_sent = kNumScoPacketsBandwidth;
895 int completed_packets =
896 wait_for_completed_packets_event(sco_connection_handles[0]);
897 if (sco_packets_sent != completed_packets) {
898 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
899 sco_packets_sent, completed_packets);
900 }
901 }
902}
903
904// Enter loopback mode and send packets for ACL bandwidth measurements.
905TEST_P(BluetoothAidlTest, LoopbackModeAclBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800906 setBufferSizes();
907
908 enterLoopbackMode();
909
910 if (!acl_connection_handles.empty()) {
911 ASSERT_LT(0, max_acl_data_packet_length);
912 sendAndCheckAcl(kNumAclPacketsBandwidth, max_acl_data_packet_length - 1,
913 acl_connection_handles[0]);
914 int acl_packets_sent = kNumAclPacketsBandwidth;
915 int completed_packets =
916 wait_for_completed_packets_event(acl_connection_handles[0]);
917 if (acl_packets_sent != completed_packets) {
918 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
919 acl_packets_sent, completed_packets);
920 }
921 }
922}
923
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000924// Set all bits in the event mask
925TEST_P(BluetoothAidlTest, SetEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800926 std::vector<uint8_t> cmd;
927 ::bluetooth::packet::BitInserter bi{cmd};
928 uint64_t full_mask = UINT64_MAX;
929 ::bluetooth::hci::SetEventMaskBuilder::Create(full_mask)->Serialize(bi);
930 hci->sendHciCommand(cmd);
Jack He2d2f3c22023-03-27 03:04:52 -0700931 wait_and_validate_command_complete_event(
932 ::bluetooth::hci::OpCode::SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000933}
934
935// Set all bits in the LE event mask
936TEST_P(BluetoothAidlTest, SetLeEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800937 std::vector<uint8_t> cmd;
938 ::bluetooth::packet::BitInserter bi{cmd};
939 uint64_t full_mask = UINT64_MAX;
940 ::bluetooth::hci::LeSetEventMaskBuilder::Create(full_mask)->Serialize(bi);
941 hci->sendHciCommand(cmd);
Jack He2d2f3c22023-03-27 03:04:52 -0700942 wait_and_validate_command_complete_event(
943 ::bluetooth::hci::OpCode::LE_SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000944}
945
Myles Watson0daa1642023-01-26 15:58:28 -0800946// Call initialize twice, second one should fail.
947TEST_P(BluetoothAidlTest, CallInitializeTwice) {
948 class SecondCb
949 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
950 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700951 ndk::ScopedAStatus initializationComplete(Status status) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800952 EXPECT_EQ(status, Status::ALREADY_INITIALIZED);
953 init_promise.set_value();
954 return ScopedAStatus::ok();
955 };
956
Jack Hedbceaca2023-03-27 18:06:38 -0700957 ndk::ScopedAStatus hciEventReceived(
958 const std::vector<uint8_t>& /*event*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800959 ADD_FAILURE();
960 return ScopedAStatus::ok();
961 };
962
Jack Hedbceaca2023-03-27 18:06:38 -0700963 ndk::ScopedAStatus aclDataReceived(
964 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800965 ADD_FAILURE();
966 return ScopedAStatus::ok();
967 };
968
Jack Hedbceaca2023-03-27 18:06:38 -0700969 ndk::ScopedAStatus scoDataReceived(
970 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800971 ADD_FAILURE();
972 return ScopedAStatus::ok();
973 };
974
Jack Hedbceaca2023-03-27 18:06:38 -0700975 ndk::ScopedAStatus isoDataReceived(
976 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800977 ADD_FAILURE();
978 return ScopedAStatus::ok();
979 };
980 std::promise<void> init_promise;
981 };
982
983 std::shared_ptr<SecondCb> second_cb = ndk::SharedRefBase::make<SecondCb>();
984 ASSERT_NE(second_cb, nullptr);
985
986 auto future = second_cb->init_promise.get_future();
987 ASSERT_TRUE(hci->initialize(second_cb).isOk());
988 auto status = future.wait_for(std::chrono::seconds(1));
989 ASSERT_EQ(status, std::future_status::ready);
990}
991
Myles Watson5bfb3a72023-05-10 14:06:32 -0700992TEST_P(BluetoothAidlTest, Vsr_Bluetooth5Requirements) {
Jack He2d2f3c22023-03-27 03:04:52 -0700993 std::vector<uint8_t> version_event;
994 send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
995 version_event);
996 auto version_view = ReadLocalVersionInformationCompleteView::Create(
997 CommandCompleteView::Create(EventView::Create(PacketView<true>(
998 std::make_shared<std::vector<uint8_t>>(version_event)))));
999 ASSERT_TRUE(version_view.IsValid());
1000 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
1001 auto version = version_view.GetLocalVersionInformation();
1002 if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) {
1003 // This test does not apply to controllers below 5.0
1004 return;
1005 };
1006 // When HCI version is 5.0, LMP version must also be at least 5.0
1007 ASSERT_GE(static_cast<int>(version.lmp_version_),
1008 static_cast<int>(version.hci_version_));
1009
1010 std::vector<uint8_t> le_features_event;
1011 send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(),
1012 le_features_event);
1013 auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create(
1014 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1015 std::make_shared<std::vector<uint8_t>>(le_features_event)))));
1016 ASSERT_TRUE(le_features_view.IsValid());
1017 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus());
1018 auto le_features = le_features_view.GetLeFeatures();
1019 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LL_PRIVACY));
1020 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LE_2M_PHY));
1021 ASSERT_TRUE(le_features &
1022 static_cast<uint64_t>(LLFeaturesBits::LE_CODED_PHY));
1023 ASSERT_TRUE(le_features &
1024 static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING));
1025
1026 std::vector<uint8_t> num_adv_set_event;
1027 send_and_wait_for_cmd_complete(
1028 LeReadNumberOfSupportedAdvertisingSetsBuilder::Create(),
1029 num_adv_set_event);
1030 auto num_adv_set_view =
1031 LeReadNumberOfSupportedAdvertisingSetsCompleteView::Create(
1032 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1033 std::make_shared<std::vector<uint8_t>>(num_adv_set_event)))));
1034 ASSERT_TRUE(num_adv_set_view.IsValid());
1035 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, num_adv_set_view.GetStatus());
1036 auto num_adv_set = num_adv_set_view.GetNumberSupportedAdvertisingSets();
Treehugger Robot5e0277f2023-05-12 02:44:22 +00001037
1038 if (isTv() && get_vsr_api_level() == __ANDROID_API_U__) {
1039 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5FoTv);
1040 } else {
1041 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5);
1042 }
Jack He2d2f3c22023-03-27 03:04:52 -07001043
1044 std::vector<uint8_t> num_resolving_list_event;
1045 send_and_wait_for_cmd_complete(LeReadResolvingListSizeBuilder::Create(),
1046 num_resolving_list_event);
1047 auto num_resolving_list_view = LeReadResolvingListSizeCompleteView::Create(
1048 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1049 std::make_shared<std::vector<uint8_t>>(num_resolving_list_event)))));
1050 ASSERT_TRUE(num_resolving_list_view.IsValid());
1051 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
1052 num_resolving_list_view.GetStatus());
1053 auto num_resolving_list = num_resolving_list_view.GetResolvingListSize();
1054 ASSERT_GE(num_resolving_list, kMinLeResolvingListForBt5);
1055}
1056
Doug Ferraz83b22672024-03-22 19:54:26 -04001057/**
Doug Ferraz83b22672024-03-22 19:54:26 -04001058 * VSR-5.3.14-007 MUST support Bluetooth 4.2 and Bluetooth LE Data Length Extension.
1059 * VSR-5.3.14-008 MUST support Bluetooth Low Energy (BLE).
1060 */
Doug Ferrazeff7b912024-03-25 20:04:19 -04001061// @VsrTest = 5.3.14-007
1062// @VsrTest = 5.3.14-008
Doug Ferraz83b22672024-03-22 19:54:26 -04001063TEST_P(BluetoothAidlTest, Vsr_Bluetooth4_2Requirements) {
1064 // test only applies to handheld devices
1065 if (!isHandheld()) {
1066 return;
1067 }
1068
1069 std::vector<uint8_t> version_event;
1070 send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
1071 version_event);
1072 auto version_view = ReadLocalVersionInformationCompleteView::Create(
1073 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1074 std::make_shared<std::vector<uint8_t>>(version_event)))));
1075 ASSERT_TRUE(version_view.IsValid());
1076 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
1077 auto version = version_view.GetLocalVersionInformation();
1078 // Starting with Android 15, Fails when HCI version is lower than 4.2.
1079 ASSERT_GE(static_cast<int>(version.hci_version_),
1080 static_cast<int>(::bluetooth::hci::HciVersion::V_4_2));
1081 ASSERT_GE(static_cast<int>(version.lmp_version_),
1082 static_cast<int>(::bluetooth::hci::LmpVersion::V_4_2));
1083
1084 std::vector<uint8_t> le_features_event;
1085 send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(),
1086 le_features_event);
1087 auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create(
1088 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1089 std::make_shared<std::vector<uint8_t>>(le_features_event)))));
1090 ASSERT_TRUE(le_features_view.IsValid());
1091 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus());
1092 auto le_features = le_features_view.GetLeFeatures();
1093 ASSERT_TRUE(le_features &
1094 static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING));
1095
1096}
1097
Omair Kamil84a95be2024-04-22 13:29:19 -07001098/**
1099 * VSR-5.3.14-012 MUST support at least eight LE concurrent connections with
1100 * three in peripheral role.
1101 */
1102// @VsrTest = 5.3.14-012
1103TEST_P(BluetoothAidlTest, Vsr_BlE_Connection_Requirement) {
1104 std::vector<uint8_t> version_event;
1105 send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
1106 version_event);
1107 auto version_view = ReadLocalVersionInformationCompleteView::Create(
1108 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1109 std::make_shared<std::vector<uint8_t>>(version_event)))));
1110 ASSERT_TRUE(version_view.IsValid());
1111 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
1112 auto version = version_view.GetLocalVersionInformation();
1113 if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) {
1114 // This test does not apply to controllers below 5.0
1115 return;
1116 };
1117
1118 int max_connections = ::android::base::GetIntProperty(
1119 "bluetooth.core.le.max_number_of_concurrent_connections", -1);
1120 if (max_connections == -1) {
1121 // With the property not set the default minimum of 8 will be used
1122 ALOGI("Max number of LE concurrent connections isn't set");
1123 return;
1124 }
1125 ALOGI("Max number of LE concurrent connections = %d", max_connections);
1126 ASSERT_GE(max_connections, 8);
1127}
1128
Dylan Tian8a6e09c2022-08-16 07:29:28 +00001129GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothAidlTest);
1130INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothAidlTest,
1131 testing::ValuesIn(android::getAidlHalInstanceNames(
1132 IBluetoothHci::descriptor)),
1133 android::PrintInstanceNameToString);
1134
1135int main(int argc, char** argv) {
1136 ABinderProcess_startThreadPool();
1137 ::testing::InitGoogleTest(&argc, argv);
1138 int status = RUN_ALL_TESTS();
1139 ALOGI("Test result = %d", status);
1140 return status;
1141}