blob: 395e4cc8cfc349c026eab1a388e266ed7a61cd75 [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 Escande1a139902024-02-27 15:57:33 -080078static constexpr std::chrono::milliseconds kWaitForInitTimeout(1000);
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
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000121class ThroughputLogger {
122 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700123 explicit ThroughputLogger(std::string task)
Myles Watsone1708c82023-01-18 17:07:58 -0800124 : total_bytes_(0),
Jack Hedbceaca2023-03-27 18:06:38 -0700125 task_(std::move(task)),
Myles Watsone1708c82023-01-18 17:07:58 -0800126 start_time_(std::chrono::steady_clock::now()) {}
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000127
128 ~ThroughputLogger() {
129 if (total_bytes_ == 0) {
130 return;
131 }
132 std::chrono::duration<double> duration =
133 std::chrono::steady_clock::now() - start_time_;
134 double s = duration.count();
135 if (s == 0) {
136 return;
137 }
138 double rate_kb = (static_cast<double>(total_bytes_) / s) / 1024;
139 ALOGD("%s %.1f KB/s (%zu bytes in %.3fs)", task_.c_str(), rate_kb,
140 total_bytes_, s);
141 }
142
143 void setTotalBytes(size_t total_bytes) { total_bytes_ = total_bytes; }
144
145 private:
146 size_t total_bytes_;
147 std::string task_;
148 std::chrono::steady_clock::time_point start_time_;
149};
150
151// The main test class for Bluetooth HAL.
152class BluetoothAidlTest : public ::testing::TestWithParam<std::string> {
William Escande1a139902024-02-27 15:57:33 -0800153 std::chrono::time_point<std::chrono::system_clock>
154 time_after_initialize_complete;
155
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000156 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700157 void SetUp() override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000158 // currently test passthrough mode only
159 hci = IBluetoothHci::fromBinder(
160 SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
161 ASSERT_NE(hci, nullptr);
162 ALOGI("%s: getService() for bluetooth hci is %s", __func__,
163 hci->isRemote() ? "remote" : "local");
164
165 // Lambda function
166 auto on_binder_death = [](void* /*cookie*/) { FAIL(); };
167
168 bluetooth_hci_death_recipient =
169 AIBinder_DeathRecipient_new(on_binder_death);
170 ASSERT_NE(bluetooth_hci_death_recipient, nullptr);
171 ASSERT_EQ(STATUS_OK,
172 AIBinder_linkToDeath(hci->asBinder().get(),
Jack Hedbceaca2023-03-27 18:06:38 -0700173 bluetooth_hci_death_recipient, nullptr));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000174
175 hci_cb = ndk::SharedRefBase::make<BluetoothHciCallbacks>(*this);
176 ASSERT_NE(hci_cb, nullptr);
177
178 max_acl_data_packet_length = 0;
179 max_sco_data_packet_length = 0;
180 max_acl_data_packets = 0;
181 max_sco_data_packets = 0;
182
183 event_cb_count = 0;
184 acl_cb_count = 0;
185 sco_cb_count = 0;
William Escande1a139902024-02-27 15:57:33 -0800186 std::chrono::time_point<std::chrono::system_clock>
187 timeout_after_initialize =
188 std::chrono::system_clock::now() + kWaitForInitTimeout;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000189
190 ASSERT_TRUE(hci->initialize(hci_cb).isOk());
191 auto future = initialized_promise.get_future();
192 auto timeout_status = future.wait_for(kWaitForInitTimeout);
193 ASSERT_EQ(timeout_status, std::future_status::ready);
194 ASSERT_TRUE(future.get());
William Escande1a139902024-02-27 15:57:33 -0800195 ASSERT_GE(timeout_after_initialize, time_after_initialize_complete);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000196 }
197
Jack Hedbceaca2023-03-27 18:06:38 -0700198 void TearDown() override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000199 ALOGI("TearDown");
200 // Should not be checked in production code
201 ASSERT_TRUE(hci->close().isOk());
202 std::this_thread::sleep_for(kInterfaceCloseDelayMs);
203 handle_no_ops();
Myles Watsone1708c82023-01-18 17:07:58 -0800204 discard_qca_debugging();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000205 EXPECT_EQ(static_cast<size_t>(0), event_queue.size());
206 EXPECT_EQ(static_cast<size_t>(0), sco_queue.size());
207 EXPECT_EQ(static_cast<size_t>(0), acl_queue.size());
208 EXPECT_EQ(static_cast<size_t>(0), iso_queue.size());
209 }
210
211 void setBufferSizes();
Myles Watsone1708c82023-01-18 17:07:58 -0800212 void setSynchronousFlowControlEnable();
213
214 // Functions called from within tests in loopback mode
215 void sendAndCheckHci(int num_packets);
216 void sendAndCheckSco(int num_packets, size_t size, uint16_t handle);
217 void sendAndCheckAcl(int num_packets, size_t size, uint16_t handle);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000218
219 // Helper functions to try to get a handle on verbosity
Myles Watsone1708c82023-01-18 17:07:58 -0800220 void enterLoopbackMode();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000221 void handle_no_ops();
Myles Watsone1708c82023-01-18 17:07:58 -0800222 void discard_qca_debugging();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000223 void wait_for_event(bool timeout_is_error);
Jack He2d2f3c22023-03-27 03:04:52 -0700224 void wait_for_command_complete_event(OpCode opCode,
225 std::vector<uint8_t>& complete_event);
226 // Wait until a command complete is received.
227 // Command complete will be consumed after this method
228 void wait_and_validate_command_complete_event(OpCode opCode);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000229 int wait_for_completed_packets_event(uint16_t handle);
Jack He2d2f3c22023-03-27 03:04:52 -0700230 void send_and_wait_for_cmd_complete(std::unique_ptr<CommandBuilder> cmd,
231 std::vector<uint8_t>& cmd_complete);
xiaoshun.xu4e85c092023-07-11 17:30:04 +0800232 void reassemble_sco_loopback_pkt(std::vector<uint8_t>& scoPackets,
233 size_t size);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000234
235 // A simple test implementation of BluetoothHciCallbacks.
236 class BluetoothHciCallbacks
237 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
238 BluetoothAidlTest& parent_;
239
240 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700241 explicit BluetoothHciCallbacks(BluetoothAidlTest& parent)
242 : parent_(parent){};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000243
Jack Hedbceaca2023-03-27 18:06:38 -0700244 ~BluetoothHciCallbacks() override = default;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000245
Jack Hedbceaca2023-03-27 18:06:38 -0700246 ndk::ScopedAStatus initializationComplete(Status status) override {
William Escande1a139902024-02-27 15:57:33 -0800247 if (status == Status::SUCCESS) {
248 parent_.time_after_initialize_complete =
249 std::chrono::system_clock::now();
250 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000251 parent_.initialized_promise.set_value(status == Status::SUCCESS);
252 ALOGV("%s (status = %d)", __func__, static_cast<int>(status));
253 return ScopedAStatus::ok();
254 };
255
Jack Hedbceaca2023-03-27 18:06:38 -0700256 ndk::ScopedAStatus hciEventReceived(
257 const std::vector<uint8_t>& event) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000258 parent_.event_cb_count++;
259 parent_.event_queue.push(event);
Jack He2d2f3c22023-03-27 03:04:52 -0700260 ALOGI("Event received (length = %d)", static_cast<int>(event.size()));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000261 return ScopedAStatus::ok();
262 };
263
Jack Hedbceaca2023-03-27 18:06:38 -0700264 ndk::ScopedAStatus aclDataReceived(
265 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000266 parent_.acl_cb_count++;
267 parent_.acl_queue.push(data);
268 return ScopedAStatus::ok();
269 };
270
Jack Hedbceaca2023-03-27 18:06:38 -0700271 ndk::ScopedAStatus scoDataReceived(
272 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000273 parent_.sco_cb_count++;
274 parent_.sco_queue.push(data);
275 return ScopedAStatus::ok();
276 };
277
Jack Hedbceaca2023-03-27 18:06:38 -0700278 ndk::ScopedAStatus isoDataReceived(
279 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000280 parent_.iso_cb_count++;
281 parent_.iso_queue.push(data);
282 return ScopedAStatus::ok();
283 };
284 };
285
286 template <class T>
287 class WaitQueue {
288 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700289 WaitQueue() = default;
290 ;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000291
292 virtual ~WaitQueue() = default;
293
294 bool empty() const {
295 std::lock_guard<std::mutex> lock(m_);
296 return q_.empty();
297 };
298
299 size_t size() const {
300 std::lock_guard<std::mutex> lock(m_);
301 return q_.size();
302 };
303
304 void push(const T& v) {
305 std::lock_guard<std::mutex> lock(m_);
306 q_.push(v);
307 ready_.notify_one();
308 };
309
310 bool pop(T& v) {
311 std::lock_guard<std::mutex> lock(m_);
312 if (q_.empty()) {
313 return false;
314 }
315 v = std::move(q_.front());
316 q_.pop();
317 return true;
318 };
319
Myles Watson51b8bae2023-01-27 14:22:24 -0800320 void pop() {
321 std::lock_guard<std::mutex> lock(m_);
322 if (q_.empty()) {
323 return;
324 }
325 q_.pop();
326 };
327
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000328 bool front(T& v) {
329 std::lock_guard<std::mutex> lock(m_);
330 if (q_.empty()) {
331 return false;
332 }
333 v = q_.front();
334 return true;
335 };
336
337 void wait() {
338 std::unique_lock<std::mutex> lock(m_);
339 while (q_.empty()) {
340 ready_.wait(lock);
341 }
342 };
343
344 bool waitWithTimeout(std::chrono::milliseconds timeout) {
345 std::unique_lock<std::mutex> lock(m_);
346 while (q_.empty()) {
347 if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
348 return false;
349 }
350 }
351 return true;
352 };
353
354 bool tryPopWithTimeout(T& v, std::chrono::milliseconds timeout) {
355 std::unique_lock<std::mutex> lock(m_);
356 while (q_.empty()) {
357 if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
358 return false;
359 }
360 }
361 v = std::move(q_.front());
362 q_.pop();
363 return true;
364 };
365
366 private:
367 mutable std::mutex m_;
368 std::queue<T> q_;
369 std::condition_variable_any ready_;
370 };
371
372 std::shared_ptr<IBluetoothHci> hci;
373 std::shared_ptr<BluetoothHciCallbacks> hci_cb;
Jack Hedbceaca2023-03-27 18:06:38 -0700374 AIBinder_DeathRecipient* bluetooth_hci_death_recipient{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000375 WaitQueue<std::vector<uint8_t>> event_queue;
376 WaitQueue<std::vector<uint8_t>> acl_queue;
377 WaitQueue<std::vector<uint8_t>> sco_queue;
378 WaitQueue<std::vector<uint8_t>> iso_queue;
379
380 std::promise<bool> initialized_promise;
Jack Hedbceaca2023-03-27 18:06:38 -0700381 int event_cb_count{};
382 int sco_cb_count{};
383 int acl_cb_count{};
384 int iso_cb_count{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000385
Jack Hedbceaca2023-03-27 18:06:38 -0700386 int max_acl_data_packet_length{};
387 int max_sco_data_packet_length{};
388 int max_acl_data_packets{};
389 int max_sco_data_packets{};
Myles Watsone1708c82023-01-18 17:07:58 -0800390
391 std::vector<uint16_t> sco_connection_handles;
392 std::vector<uint16_t> acl_connection_handles;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000393};
394
395// Discard NO-OPs from the event queue.
396void BluetoothAidlTest::handle_no_ops() {
397 while (!event_queue.empty()) {
398 std::vector<uint8_t> event;
399 event_queue.front(event);
Myles Watson51b8bae2023-01-27 14:22:24 -0800400 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
401 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
402 std::make_shared<std::vector<uint8_t>>(event))));
403 auto status_view = ::bluetooth::hci::CommandCompleteView::Create(
404 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
405 std::make_shared<std::vector<uint8_t>>(event))));
406 bool is_complete_no_op =
407 complete_view.IsValid() &&
408 complete_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
409 bool is_status_no_op =
410 status_view.IsValid() &&
411 status_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
412 if (is_complete_no_op || is_status_no_op) {
413 event_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000414 } else {
415 break;
416 }
417 }
Myles Watsone1708c82023-01-18 17:07:58 -0800418}
419
420// Discard Qualcomm ACL debugging
421void BluetoothAidlTest::discard_qca_debugging() {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000422 while (!acl_queue.empty()) {
423 std::vector<uint8_t> acl_packet;
424 acl_queue.front(acl_packet);
Myles Watson51b8bae2023-01-27 14:22:24 -0800425 auto acl_view =
426 ::bluetooth::hci::AclView::Create(::bluetooth::hci::PacketView<true>(
427 std::make_shared<std::vector<uint8_t>>(acl_packet)));
428 EXPECT_TRUE(acl_view.IsValid());
429 if (acl_view.GetHandle() == kAclHandleQcaDebugMessage) {
430 acl_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000431 } else {
432 break;
433 }
434 }
435}
436
437// Receive an event, discarding NO-OPs.
438void BluetoothAidlTest::wait_for_event(bool timeout_is_error = true) {
Myles Watsone1708c82023-01-18 17:07:58 -0800439 // Wait until we get something that's not a no-op.
440 while (true) {
441 bool event_ready = event_queue.waitWithTimeout(kWaitForHciEventTimeout);
442 ASSERT_TRUE(event_ready || !timeout_is_error);
443 if (event_queue.empty()) {
444 // waitWithTimeout timed out
445 return;
446 }
447 handle_no_ops();
448 if (!event_queue.empty()) {
449 // There's an event in the queue that's not a no-op.
450 return;
451 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000452 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000453}
454
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000455void BluetoothAidlTest::wait_for_command_complete_event(
Jack He2d2f3c22023-03-27 03:04:52 -0700456 OpCode opCode, std::vector<uint8_t>& complete_event) {
Myles Watsone1708c82023-01-18 17:07:58 -0800457 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Myles Watsone1708c82023-01-18 17:07:58 -0800458 ASSERT_FALSE(event_queue.empty());
Jack He2d2f3c22023-03-27 03:04:52 -0700459 ASSERT_TRUE(event_queue.pop(complete_event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800460 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
461 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
Jack He2d2f3c22023-03-27 03:04:52 -0700462 std::make_shared<std::vector<uint8_t>>(complete_event))));
Myles Watson51b8bae2023-01-27 14:22:24 -0800463 ASSERT_TRUE(complete_view.IsValid());
464 ASSERT_EQ(complete_view.GetCommandOpCode(), opCode);
465 ASSERT_EQ(complete_view.GetPayload()[0],
466 static_cast<uint8_t>(::bluetooth::hci::ErrorCode::SUCCESS));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000467}
468
Jack He2d2f3c22023-03-27 03:04:52 -0700469void BluetoothAidlTest::wait_and_validate_command_complete_event(
470 ::bluetooth::hci::OpCode opCode) {
471 std::vector<uint8_t> complete_event;
472 ASSERT_NO_FATAL_FAILURE(
473 wait_for_command_complete_event(opCode, complete_event));
474}
475
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000476// Send the command to read the controller's buffer sizes.
477void BluetoothAidlTest::setBufferSizes() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800478 std::vector<uint8_t> cmd;
479 ::bluetooth::packet::BitInserter bi{cmd};
480 ::bluetooth::hci::ReadBufferSizeBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000481 hci->sendHciCommand(cmd);
482
Myles Watsone1708c82023-01-18 17:07:58 -0800483 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000484 std::vector<uint8_t> event;
485 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800486 auto complete_view = ::bluetooth::hci::ReadBufferSizeCompleteView::Create(
487 ::bluetooth::hci::CommandCompleteView::Create(
488 ::bluetooth::hci::EventView::Create(
489 ::bluetooth::hci::PacketView<true>(
490 std::make_shared<std::vector<uint8_t>>(event)))));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000491
Myles Watson51b8bae2023-01-27 14:22:24 -0800492 ASSERT_TRUE(complete_view.IsValid());
493 ASSERT_EQ(complete_view.GetStatus(), ::bluetooth::hci::ErrorCode::SUCCESS);
494 max_acl_data_packet_length = complete_view.GetAclDataPacketLength();
495 max_sco_data_packet_length = complete_view.GetSynchronousDataPacketLength();
496 max_acl_data_packets = complete_view.GetTotalNumAclDataPackets();
497 max_sco_data_packets = complete_view.GetTotalNumSynchronousDataPackets();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000498
499 ALOGD("%s: ACL max %d num %d SCO max %d num %d", __func__,
500 static_cast<int>(max_acl_data_packet_length),
501 static_cast<int>(max_acl_data_packets),
502 static_cast<int>(max_sco_data_packet_length),
503 static_cast<int>(max_sco_data_packets));
504}
505
Myles Watsone1708c82023-01-18 17:07:58 -0800506// Enable flow control packets for SCO
507void BluetoothAidlTest::setSynchronousFlowControlEnable() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800508 std::vector<uint8_t> cmd;
509 ::bluetooth::packet::BitInserter bi{cmd};
510 ::bluetooth::hci::WriteSynchronousFlowControlEnableBuilder::Create(
511 ::bluetooth::hci::Enable::ENABLED)
512 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800513 hci->sendHciCommand(cmd);
514
Jack He2d2f3c22023-03-27 03:04:52 -0700515 wait_and_validate_command_complete_event(
Myles Watson51b8bae2023-01-27 14:22:24 -0800516 ::bluetooth::hci::OpCode::WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE);
Myles Watsone1708c82023-01-18 17:07:58 -0800517}
518
519// Send an HCI command (in Loopback mode) and check the response.
520void BluetoothAidlTest::sendAndCheckHci(int num_packets) {
Jack Hedbceaca2023-03-27 18:06:38 -0700521 ThroughputLogger logger{__func__};
522 size_t command_size = 0;
Myles Watson51b8bae2023-01-27 14:22:24 -0800523 char new_name[] = "John Jacob Jingleheimer Schmidt ___________________";
524 size_t new_name_length = strlen(new_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800525 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800526 // The name to set is new_name
Jack Hedbceaca2023-03-27 18:06:38 -0700527 std::array<uint8_t, 248> name_array{};
Myles Watsone1708c82023-01-18 17:07:58 -0800528 for (size_t i = 0; i < new_name_length; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800529 name_array[i] = new_name[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800530 }
531 // And the packet number
Myles Watson51b8bae2023-01-27 14:22:24 -0800532 char number[11] = "0000000000";
533 snprintf(number, sizeof(number), "%010d", static_cast<int>(n));
534 for (size_t i = new_name_length; i < new_name_length + sizeof(number) - 1;
535 i++) {
536 name_array[new_name_length + i] = number[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800537 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800538 std::vector<uint8_t> write_name;
539 ::bluetooth::packet::BitInserter bi{write_name};
540 ::bluetooth::hci::WriteLocalNameBuilder::Create(name_array)->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800541 hci->sendHciCommand(write_name);
542
543 // Check the loopback of the HCI packet
544 ASSERT_NO_FATAL_FAILURE(wait_for_event());
545
546 std::vector<uint8_t> event;
547 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800548 auto event_view = ::bluetooth::hci::LoopbackCommandView::Create(
549 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
550 std::make_shared<std::vector<uint8_t>>(event))));
551 ASSERT_TRUE(event_view.IsValid());
552 std::vector<uint8_t> looped_back_command{event_view.GetPayload().begin(),
553 event_view.GetPayload().end()};
554 ASSERT_EQ(looped_back_command, write_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800555
556 if (n == num_packets - 1) {
557 command_size = write_name.size();
558 }
Myles Watsone1708c82023-01-18 17:07:58 -0800559 }
560 logger.setTotalBytes(command_size * num_packets * 2);
561}
562
563// Send a SCO data packet (in Loopback mode) and check the response.
564void BluetoothAidlTest::sendAndCheckSco(int num_packets, size_t size,
565 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700566 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800567 for (int n = 0; n < num_packets; n++) {
568 // Send a SCO packet
569 std::vector<uint8_t> sco_packet;
Myles Watson51b8bae2023-01-27 14:22:24 -0800570 std::vector<uint8_t> payload;
Myles Watsone1708c82023-01-18 17:07:58 -0800571 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800572 payload.push_back(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800573 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800574 ::bluetooth::packet::BitInserter bi{sco_packet};
575 ::bluetooth::hci::ScoBuilder::Create(
576 handle, ::bluetooth::hci::PacketStatusFlag::CORRECTLY_RECEIVED, payload)
577 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800578 hci->sendScoData(sco_packet);
579
580 // Check the loopback of the SCO packet
581 std::vector<uint8_t> sco_loopback;
582 ASSERT_TRUE(
583 sco_queue.tryPopWithTimeout(sco_loopback, kWaitForScoDataTimeout));
584
xiaoshun.xu4e85c092023-07-11 17:30:04 +0800585 if (sco_loopback.size() < size) {
586 // The packets may have been split for USB. Reassemble before checking.
587 reassemble_sco_loopback_pkt(sco_loopback, size);
588 }
589
Myles Watson51b8bae2023-01-27 14:22:24 -0800590 ASSERT_EQ(sco_packet, sco_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800591 }
592 logger.setTotalBytes(num_packets * size * 2);
593}
594
595// Send an ACL data packet (in Loopback mode) and check the response.
596void BluetoothAidlTest::sendAndCheckAcl(int num_packets, size_t size,
597 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700598 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800599 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800600 // Send an ACL packet with counting data
601 auto payload = std::make_unique<::bluetooth::packet::RawBuilder>();
Myles Watsone1708c82023-01-18 17:07:58 -0800602 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800603 payload->AddOctets1(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800604 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800605 std::vector<uint8_t> acl_packet;
606 ::bluetooth::packet::BitInserter bi{acl_packet};
607 ::bluetooth::hci::AclBuilder::Create(
608 handle,
609 ::bluetooth::hci::PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE,
610 ::bluetooth::hci::BroadcastFlag::POINT_TO_POINT, std::move(payload))
611 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800612 hci->sendAclData(acl_packet);
613
614 std::vector<uint8_t> acl_loopback;
615 // Check the loopback of the ACL packet
616 ASSERT_TRUE(
617 acl_queue.tryPopWithTimeout(acl_loopback, kWaitForAclDataTimeout));
618
Myles Watson51b8bae2023-01-27 14:22:24 -0800619 ASSERT_EQ(acl_packet, acl_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800620 }
621 logger.setTotalBytes(num_packets * size * 2);
622}
623
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000624// Return the number of completed packets reported by the controller.
625int BluetoothAidlTest::wait_for_completed_packets_event(uint16_t handle) {
626 int packets_processed = 0;
Myles Watson65b47f52023-01-26 12:59:06 -0800627 while (true) {
628 // There should be at least one event.
629 wait_for_event(packets_processed == 0);
630 if (event_queue.empty()) {
631 if (packets_processed == 0) {
632 ALOGW("%s: waitForBluetoothCallback timed out.", __func__);
633 }
634 return packets_processed;
635 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000636 std::vector<uint8_t> event;
637 EXPECT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800638 auto event_view = ::bluetooth::hci::NumberOfCompletedPacketsView::Create(
639 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
640 std::make_shared<std::vector<uint8_t>>(event))));
641 if (!event_view.IsValid()) {
642 ADD_FAILURE();
643 return packets_processed;
644 }
645 auto completed_packets = event_view.GetCompletedPackets();
Jack Hedbceaca2023-03-27 18:06:38 -0700646 for (const auto& entry : completed_packets) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800647 EXPECT_EQ(handle, entry.connection_handle_);
648 packets_processed += entry.host_num_of_completed_packets_;
649 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000650 }
651 return packets_processed;
652}
653
Myles Watsone1708c82023-01-18 17:07:58 -0800654// Send local loopback command and initialize SCO and ACL handles.
655void BluetoothAidlTest::enterLoopbackMode() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800656 std::vector<uint8_t> cmd;
657 ::bluetooth::packet::BitInserter bi{cmd};
658 ::bluetooth::hci::WriteLoopbackModeBuilder::Create(
659 bluetooth::hci::LoopbackMode::ENABLE_LOCAL)
660 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800661 hci->sendHciCommand(cmd);
662
663 // Receive connection complete events with data channels
664 int connection_event_count = 0;
665 bool command_complete_received = false;
666 while (true) {
667 wait_for_event(false);
668 if (event_queue.empty()) {
669 // Fail if there was no event received or no connections completed.
670 ASSERT_TRUE(command_complete_received);
671 ASSERT_LT(0, connection_event_count);
672 return;
673 }
674 std::vector<uint8_t> event;
675 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800676 auto event_view =
677 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
678 std::make_shared<std::vector<uint8_t>>(event)));
679 ASSERT_TRUE(event_view.IsValid());
Myles Watsone1708c82023-01-18 17:07:58 -0800680
Myles Watson51b8bae2023-01-27 14:22:24 -0800681 if (event_view.GetEventCode() ==
682 ::bluetooth::hci::EventCode::CONNECTION_COMPLETE) {
683 auto complete_view =
684 ::bluetooth::hci::ConnectionCompleteView::Create(event_view);
685 ASSERT_TRUE(complete_view.IsValid());
686 switch (complete_view.GetLinkType()) {
687 case ::bluetooth::hci::LinkType::ACL:
688 acl_connection_handles.push_back(complete_view.GetConnectionHandle());
689 break;
690 case ::bluetooth::hci::LinkType::SCO:
691 sco_connection_handles.push_back(complete_view.GetConnectionHandle());
692 break;
693 default:
694 ASSERT_EQ(complete_view.GetLinkType(),
695 ::bluetooth::hci::LinkType::ACL);
Myles Watsone1708c82023-01-18 17:07:58 -0800696 }
Myles Watsone1708c82023-01-18 17:07:58 -0800697 connection_event_count++;
698 } else {
Myles Watson51b8bae2023-01-27 14:22:24 -0800699 auto command_complete_view =
700 ::bluetooth::hci::WriteLoopbackModeCompleteView::Create(
701 ::bluetooth::hci::CommandCompleteView::Create(event_view));
702 ASSERT_TRUE(command_complete_view.IsValid());
703 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
704 command_complete_view.GetStatus());
Myles Watsone1708c82023-01-18 17:07:58 -0800705 command_complete_received = true;
706 }
707 }
708}
709
Jack He2d2f3c22023-03-27 03:04:52 -0700710void BluetoothAidlTest::send_and_wait_for_cmd_complete(
711 std::unique_ptr<CommandBuilder> cmd, std::vector<uint8_t>& cmd_complete) {
712 std::vector<uint8_t> cmd_bytes = cmd->SerializeToBytes();
713 hci->sendHciCommand(cmd_bytes);
714
715 auto view = CommandView::Create(
716 PacketView<true>(std::make_shared<std::vector<uint8_t>>(cmd_bytes)));
717 ASSERT_TRUE(view.IsValid());
718 ALOGI("Waiting for %s[0x%x]", OpCodeText(view.GetOpCode()).c_str(),
719 static_cast<int>(view.GetOpCode()));
720 ASSERT_NO_FATAL_FAILURE(
721 wait_for_command_complete_event(view.GetOpCode(), cmd_complete));
722}
723
xiaoshun.xu4e85c092023-07-11 17:30:04 +0800724// Handle the loopback packet.
725void BluetoothAidlTest::reassemble_sco_loopback_pkt(std::vector<uint8_t>& scoPackets,
726 size_t size) {
727 std::vector<uint8_t> sco_packet_whole;
728 sco_packet_whole.assign(scoPackets.begin(), scoPackets.end());
729 while (size + 3 > sco_packet_whole.size()) {
730 std::vector<uint8_t> sco_packets;
731 ASSERT_TRUE(
732 sco_queue.tryPopWithTimeout(sco_packets, kWaitForScoDataTimeout));
733 sco_packet_whole.insert(sco_packet_whole.end(), sco_packets.begin() + 3,
734 sco_packets.end());
735 }
736 scoPackets.assign(sco_packet_whole.begin(), sco_packet_whole.end());
737 scoPackets[2] = size;
738}
739
Myles Watsone1708c82023-01-18 17:07:58 -0800740// Empty test: Initialize()/Close() are called in SetUp()/TearDown().
741TEST_P(BluetoothAidlTest, InitializeAndClose) {}
742
743// Send an HCI Reset with sendHciCommand and wait for a command complete event.
Myles Watson65b47f52023-01-26 12:59:06 -0800744TEST_P(BluetoothAidlTest, HciReset) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800745 std::vector<uint8_t> reset;
746 ::bluetooth::packet::BitInserter bi{reset};
747 ::bluetooth::hci::ResetBuilder::Create()->Serialize(bi);
Myles Watson65b47f52023-01-26 12:59:06 -0800748 hci->sendHciCommand(reset);
749
Jack He2d2f3c22023-03-27 03:04:52 -0700750 wait_and_validate_command_complete_event(::bluetooth::hci::OpCode::RESET);
Myles Watson65b47f52023-01-26 12:59:06 -0800751}
Myles Watsone1708c82023-01-18 17:07:58 -0800752
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000753// Read and check the HCI version of the controller.
754TEST_P(BluetoothAidlTest, HciVersionTest) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800755 std::vector<uint8_t> cmd;
756 ::bluetooth::packet::BitInserter bi{cmd};
757 ::bluetooth::hci::ReadLocalVersionInformationBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000758 hci->sendHciCommand(cmd);
759
Myles Watsone1708c82023-01-18 17:07:58 -0800760 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000761
762 std::vector<uint8_t> event;
763 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800764 auto complete_view =
765 ::bluetooth::hci::ReadLocalVersionInformationCompleteView::Create(
766 ::bluetooth::hci::CommandCompleteView::Create(
767 ::bluetooth::hci::EventView::Create(
768 ::bluetooth::hci::PacketView<true>(
769 std::make_shared<std::vector<uint8_t>>(event)))));
770 ASSERT_TRUE(complete_view.IsValid());
771 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, complete_view.GetStatus());
772 auto version = complete_view.GetLocalVersionInformation();
773 ASSERT_LE(::bluetooth::hci::HciVersion::V_3_0, version.hci_version_);
774 ASSERT_LE(::bluetooth::hci::LmpVersion::V_3_0, version.lmp_version_);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000775}
776
777// Send an unknown HCI command and wait for the error message.
778TEST_P(BluetoothAidlTest, HciUnknownCommand) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800779 std::vector<uint8_t> cmd;
780 ::bluetooth::packet::BitInserter bi{cmd};
781 ::bluetooth::hci::CommandBuilder::Create(
782 static_cast<::bluetooth::hci::OpCode>(0x3cff),
783 std::make_unique<::bluetooth::packet::RawBuilder>())
784 ->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000785 hci->sendHciCommand(cmd);
786
Myles Watsone1708c82023-01-18 17:07:58 -0800787 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000788
789 std::vector<uint8_t> event;
790 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800791 auto event_view =
792 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
793 std::make_shared<std::vector<uint8_t>>(event)));
794 ASSERT_TRUE(event_view.IsValid());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000795
Myles Watson51b8bae2023-01-27 14:22:24 -0800796 switch (event_view.GetEventCode()) {
797 case ::bluetooth::hci::EventCode::COMMAND_COMPLETE: {
798 auto command_complete =
799 ::bluetooth::hci::CommandCompleteView::Create(event_view);
800 ASSERT_TRUE(command_complete.IsValid());
801 ASSERT_EQ(command_complete.GetPayload()[0],
802 static_cast<uint8_t>(
803 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND));
804 } break;
805 case ::bluetooth::hci::EventCode::COMMAND_STATUS: {
806 auto command_status =
807 ::bluetooth::hci::CommandStatusView::Create(event_view);
808 ASSERT_TRUE(command_status.IsValid());
809 ASSERT_EQ(command_status.GetStatus(),
810 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND);
811 } break;
812 default:
813 ADD_FAILURE();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000814 }
815}
816
Myles Watsone1708c82023-01-18 17:07:58 -0800817// Enter loopback mode, but don't send any packets.
Myles Watson65b47f52023-01-26 12:59:06 -0800818TEST_P(BluetoothAidlTest, WriteLoopbackMode) { enterLoopbackMode(); }
Myles Watsone1708c82023-01-18 17:07:58 -0800819
820// Enter loopback mode and send a single command.
821TEST_P(BluetoothAidlTest, LoopbackModeSingleCommand) {
Myles Watsone1708c82023-01-18 17:07:58 -0800822 setBufferSizes();
823
824 enterLoopbackMode();
825
826 sendAndCheckHci(1);
827}
828
829// Enter loopback mode and send a single SCO packet.
830TEST_P(BluetoothAidlTest, LoopbackModeSingleSco) {
Myles Watsone1708c82023-01-18 17:07:58 -0800831 setBufferSizes();
832 setSynchronousFlowControlEnable();
833
834 enterLoopbackMode();
835
836 if (!sco_connection_handles.empty()) {
837 ASSERT_LT(0, max_sco_data_packet_length);
838 sendAndCheckSco(1, max_sco_data_packet_length, sco_connection_handles[0]);
839 int sco_packets_sent = 1;
840 int completed_packets =
841 wait_for_completed_packets_event(sco_connection_handles[0]);
842 if (sco_packets_sent != completed_packets) {
843 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
844 sco_packets_sent, completed_packets);
845 }
846 }
847}
848
849// Enter loopback mode and send a single ACL packet.
850TEST_P(BluetoothAidlTest, LoopbackModeSingleAcl) {
Myles Watsone1708c82023-01-18 17:07:58 -0800851 setBufferSizes();
852
853 enterLoopbackMode();
854
855 if (!acl_connection_handles.empty()) {
856 ASSERT_LT(0, max_acl_data_packet_length);
857 sendAndCheckAcl(1, max_acl_data_packet_length - 1,
858 acl_connection_handles[0]);
859 int acl_packets_sent = 1;
860 int completed_packets =
861 wait_for_completed_packets_event(acl_connection_handles[0]);
862 if (acl_packets_sent != completed_packets) {
863 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
864 acl_packets_sent, completed_packets);
865 }
866 }
867 ASSERT_GE(acl_cb_count, 1);
868}
869
870// Enter loopback mode and send command packets for bandwidth measurements.
871TEST_P(BluetoothAidlTest, LoopbackModeCommandBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800872 setBufferSizes();
873
874 enterLoopbackMode();
875
876 sendAndCheckHci(kNumHciCommandsBandwidth);
877}
878
879// Enter loopback mode and send SCO packets for bandwidth measurements.
880TEST_P(BluetoothAidlTest, LoopbackModeScoBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800881 setBufferSizes();
882 setSynchronousFlowControlEnable();
883
884 enterLoopbackMode();
885
886 if (!sco_connection_handles.empty()) {
887 ASSERT_LT(0, max_sco_data_packet_length);
888 sendAndCheckSco(kNumScoPacketsBandwidth, max_sco_data_packet_length,
889 sco_connection_handles[0]);
890 int sco_packets_sent = kNumScoPacketsBandwidth;
891 int completed_packets =
892 wait_for_completed_packets_event(sco_connection_handles[0]);
893 if (sco_packets_sent != completed_packets) {
894 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
895 sco_packets_sent, completed_packets);
896 }
897 }
898}
899
900// Enter loopback mode and send packets for ACL bandwidth measurements.
901TEST_P(BluetoothAidlTest, LoopbackModeAclBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800902 setBufferSizes();
903
904 enterLoopbackMode();
905
906 if (!acl_connection_handles.empty()) {
907 ASSERT_LT(0, max_acl_data_packet_length);
908 sendAndCheckAcl(kNumAclPacketsBandwidth, max_acl_data_packet_length - 1,
909 acl_connection_handles[0]);
910 int acl_packets_sent = kNumAclPacketsBandwidth;
911 int completed_packets =
912 wait_for_completed_packets_event(acl_connection_handles[0]);
913 if (acl_packets_sent != completed_packets) {
914 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
915 acl_packets_sent, completed_packets);
916 }
917 }
918}
919
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000920// Set all bits in the event mask
921TEST_P(BluetoothAidlTest, SetEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800922 std::vector<uint8_t> cmd;
923 ::bluetooth::packet::BitInserter bi{cmd};
924 uint64_t full_mask = UINT64_MAX;
925 ::bluetooth::hci::SetEventMaskBuilder::Create(full_mask)->Serialize(bi);
926 hci->sendHciCommand(cmd);
Jack He2d2f3c22023-03-27 03:04:52 -0700927 wait_and_validate_command_complete_event(
928 ::bluetooth::hci::OpCode::SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000929}
930
931// Set all bits in the LE event mask
932TEST_P(BluetoothAidlTest, SetLeEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800933 std::vector<uint8_t> cmd;
934 ::bluetooth::packet::BitInserter bi{cmd};
935 uint64_t full_mask = UINT64_MAX;
936 ::bluetooth::hci::LeSetEventMaskBuilder::Create(full_mask)->Serialize(bi);
937 hci->sendHciCommand(cmd);
Jack He2d2f3c22023-03-27 03:04:52 -0700938 wait_and_validate_command_complete_event(
939 ::bluetooth::hci::OpCode::LE_SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000940}
941
Myles Watson0daa1642023-01-26 15:58:28 -0800942// Call initialize twice, second one should fail.
943TEST_P(BluetoothAidlTest, CallInitializeTwice) {
944 class SecondCb
945 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
946 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700947 ndk::ScopedAStatus initializationComplete(Status status) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800948 EXPECT_EQ(status, Status::ALREADY_INITIALIZED);
949 init_promise.set_value();
950 return ScopedAStatus::ok();
951 };
952
Jack Hedbceaca2023-03-27 18:06:38 -0700953 ndk::ScopedAStatus hciEventReceived(
954 const std::vector<uint8_t>& /*event*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800955 ADD_FAILURE();
956 return ScopedAStatus::ok();
957 };
958
Jack Hedbceaca2023-03-27 18:06:38 -0700959 ndk::ScopedAStatus aclDataReceived(
960 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800961 ADD_FAILURE();
962 return ScopedAStatus::ok();
963 };
964
Jack Hedbceaca2023-03-27 18:06:38 -0700965 ndk::ScopedAStatus scoDataReceived(
966 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800967 ADD_FAILURE();
968 return ScopedAStatus::ok();
969 };
970
Jack Hedbceaca2023-03-27 18:06:38 -0700971 ndk::ScopedAStatus isoDataReceived(
972 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800973 ADD_FAILURE();
974 return ScopedAStatus::ok();
975 };
976 std::promise<void> init_promise;
977 };
978
979 std::shared_ptr<SecondCb> second_cb = ndk::SharedRefBase::make<SecondCb>();
980 ASSERT_NE(second_cb, nullptr);
981
982 auto future = second_cb->init_promise.get_future();
983 ASSERT_TRUE(hci->initialize(second_cb).isOk());
984 auto status = future.wait_for(std::chrono::seconds(1));
985 ASSERT_EQ(status, std::future_status::ready);
986}
987
Myles Watson5bfb3a72023-05-10 14:06:32 -0700988TEST_P(BluetoothAidlTest, Vsr_Bluetooth5Requirements) {
Jack He2d2f3c22023-03-27 03:04:52 -0700989 std::vector<uint8_t> version_event;
990 send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
991 version_event);
992 auto version_view = ReadLocalVersionInformationCompleteView::Create(
993 CommandCompleteView::Create(EventView::Create(PacketView<true>(
994 std::make_shared<std::vector<uint8_t>>(version_event)))));
995 ASSERT_TRUE(version_view.IsValid());
996 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
997 auto version = version_view.GetLocalVersionInformation();
998 if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) {
999 // This test does not apply to controllers below 5.0
1000 return;
1001 };
1002 // When HCI version is 5.0, LMP version must also be at least 5.0
1003 ASSERT_GE(static_cast<int>(version.lmp_version_),
1004 static_cast<int>(version.hci_version_));
1005
1006 std::vector<uint8_t> le_features_event;
1007 send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(),
1008 le_features_event);
1009 auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create(
1010 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1011 std::make_shared<std::vector<uint8_t>>(le_features_event)))));
1012 ASSERT_TRUE(le_features_view.IsValid());
1013 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus());
1014 auto le_features = le_features_view.GetLeFeatures();
1015 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LL_PRIVACY));
1016 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LE_2M_PHY));
1017 ASSERT_TRUE(le_features &
1018 static_cast<uint64_t>(LLFeaturesBits::LE_CODED_PHY));
1019 ASSERT_TRUE(le_features &
1020 static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING));
1021
1022 std::vector<uint8_t> num_adv_set_event;
1023 send_and_wait_for_cmd_complete(
1024 LeReadNumberOfSupportedAdvertisingSetsBuilder::Create(),
1025 num_adv_set_event);
1026 auto num_adv_set_view =
1027 LeReadNumberOfSupportedAdvertisingSetsCompleteView::Create(
1028 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1029 std::make_shared<std::vector<uint8_t>>(num_adv_set_event)))));
1030 ASSERT_TRUE(num_adv_set_view.IsValid());
1031 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, num_adv_set_view.GetStatus());
1032 auto num_adv_set = num_adv_set_view.GetNumberSupportedAdvertisingSets();
Treehugger Robot5e0277f2023-05-12 02:44:22 +00001033
1034 if (isTv() && get_vsr_api_level() == __ANDROID_API_U__) {
1035 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5FoTv);
1036 } else {
1037 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5);
1038 }
Jack He2d2f3c22023-03-27 03:04:52 -07001039
1040 std::vector<uint8_t> num_resolving_list_event;
1041 send_and_wait_for_cmd_complete(LeReadResolvingListSizeBuilder::Create(),
1042 num_resolving_list_event);
1043 auto num_resolving_list_view = LeReadResolvingListSizeCompleteView::Create(
1044 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1045 std::make_shared<std::vector<uint8_t>>(num_resolving_list_event)))));
1046 ASSERT_TRUE(num_resolving_list_view.IsValid());
1047 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
1048 num_resolving_list_view.GetStatus());
1049 auto num_resolving_list = num_resolving_list_view.GetResolvingListSize();
1050 ASSERT_GE(num_resolving_list, kMinLeResolvingListForBt5);
1051}
1052
Dylan Tian8a6e09c2022-08-16 07:29:28 +00001053GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothAidlTest);
1054INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothAidlTest,
1055 testing::ValuesIn(android::getAidlHalInstanceNames(
1056 IBluetoothHci::descriptor)),
1057 android::PrintInstanceNameToString);
1058
1059int main(int argc, char** argv) {
1060 ABinderProcess_startThreadPool();
1061 ::testing::InitGoogleTest(&argc, argv);
1062 int status = RUN_ALL_TESTS();
1063 ALOGI("Test result = %d", status);
1064 return status;
1065}