blob: dcb5fac53c2578b5869a83dd5509cd74fb2090d3 [file] [log] [blame]
Dylan Tian8a6e09c2022-08-16 07:29:28 +00001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Treehugger Robot5e0277f2023-05-12 02:44:22 +000017#include <VtsCoreUtil.h>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000018#include <aidl/Gtest.h>
19#include <aidl/Vintf.h>
20#include <aidl/android/hardware/bluetooth/BnBluetoothHciCallbacks.h>
21#include <aidl/android/hardware/bluetooth/IBluetoothHci.h>
22#include <aidl/android/hardware/bluetooth/IBluetoothHciCallbacks.h>
23#include <aidl/android/hardware/bluetooth/Status.h>
Treehugger Robot5e0277f2023-05-12 02:44:22 +000024#include <android-base/properties.h>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000025#include <android/binder_auto_utils.h>
26#include <android/binder_manager.h>
27#include <android/binder_process.h>
28#include <binder/IServiceManager.h>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000029
Dylan Tian8a6e09c2022-08-16 07:29:28 +000030#include <chrono>
31#include <condition_variable>
32#include <future>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000033#include <queue>
34#include <thread>
Jack Hedbceaca2023-03-27 18:06:38 -070035#include <utility>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000036#include <vector>
37
Myles Watson51b8bae2023-01-27 14:22:24 -080038// TODO: Remove custom logging defines from PDL packets.
39#undef LOG_INFO
40#undef LOG_DEBUG
41#undef LOG_TAG
42#define LOG_TAG "VtsHalBluetooth"
43#include "hci/hci_packets.h"
44#include "packet/raw_builder.h"
45
Dylan Tian8a6e09c2022-08-16 07:29:28 +000046using aidl::android::hardware::bluetooth::IBluetoothHci;
47using aidl::android::hardware::bluetooth::IBluetoothHciCallbacks;
48using aidl::android::hardware::bluetooth::Status;
49using ndk::ScopedAStatus;
50using ndk::SpAIBinder;
51
Jack He2d2f3c22023-03-27 03:04:52 -070052using ::bluetooth::hci::CommandBuilder;
53using ::bluetooth::hci::CommandCompleteView;
54using ::bluetooth::hci::CommandView;
55using ::bluetooth::hci::ErrorCode;
56using ::bluetooth::hci::EventView;
57using ::bluetooth::hci::LeReadLocalSupportedFeaturesBuilder;
58using ::bluetooth::hci::LeReadLocalSupportedFeaturesCompleteView;
59using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsBuilder;
60using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsCompleteView;
61using ::bluetooth::hci::LeReadResolvingListSizeBuilder;
62using ::bluetooth::hci::LeReadResolvingListSizeCompleteView;
63using ::bluetooth::hci::LLFeaturesBits;
64using ::bluetooth::hci::OpCode;
65using ::bluetooth::hci::OpCodeText;
66using ::bluetooth::hci::PacketView;
67using ::bluetooth::hci::ReadLocalVersionInformationBuilder;
68using ::bluetooth::hci::ReadLocalVersionInformationCompleteView;
69
Myles Watson864c9042024-07-24 14:39:05 -070070static constexpr uint8_t kMinLeAdvSetForBt5 = 10;
71static constexpr uint8_t kMinLeAdvSetForBt5ForTv = 10;
Jack He2d2f3c22023-03-27 03:04:52 -070072static constexpr uint8_t kMinLeResolvingListForBt5 = 8;
73
Myles Watsone1708c82023-01-18 17:07:58 -080074static constexpr size_t kNumHciCommandsBandwidth = 100;
75static constexpr size_t kNumScoPacketsBandwidth = 100;
76static constexpr size_t kNumAclPacketsBandwidth = 100;
William Escandeac0f34f2024-04-11 09:06:31 -070077static constexpr std::chrono::milliseconds kWaitForInitTimeout(2000);
Dylan Tian8a6e09c2022-08-16 07:29:28 +000078static constexpr std::chrono::milliseconds kWaitForHciEventTimeout(2000);
Myles Watsone1708c82023-01-18 17:07:58 -080079static constexpr std::chrono::milliseconds kWaitForScoDataTimeout(1000);
80static constexpr std::chrono::milliseconds kWaitForAclDataTimeout(1000);
Dylan Tian8a6e09c2022-08-16 07:29:28 +000081static constexpr std::chrono::milliseconds kInterfaceCloseDelayMs(200);
82
Dylan Tian8a6e09c2022-08-16 07:29:28 +000083// To discard Qualcomm ACL debugging
84static constexpr uint16_t kAclHandleQcaDebugMessage = 0xedc;
85
Treehugger Robot5e0277f2023-05-12 02:44:22 +000086static int get_vsr_api_level() {
87 int vendor_api_level =
88 ::android::base::GetIntProperty("ro.vendor.api_level", -1);
89 if (vendor_api_level != -1) {
90 return vendor_api_level;
91 }
92
93 // Android S and older devices do not define ro.vendor.api_level
94 vendor_api_level = ::android::base::GetIntProperty("ro.board.api_level", -1);
95 if (vendor_api_level == -1) {
96 vendor_api_level =
97 ::android::base::GetIntProperty("ro.board.first_api_level", -1);
98 }
99
100 int product_api_level =
101 ::android::base::GetIntProperty("ro.product.first_api_level", -1);
102 if (product_api_level == -1) {
103 product_api_level =
104 ::android::base::GetIntProperty("ro.build.version.sdk", -1);
105 EXPECT_NE(product_api_level, -1) << "Could not find ro.build.version.sdk";
106 }
107
108 // VSR API level is the minimum of vendor_api_level and product_api_level.
109 if (vendor_api_level == -1 || vendor_api_level > product_api_level) {
110 return product_api_level;
111 }
112 return vendor_api_level;
113}
114
115static bool isTv() {
116 return testing::deviceSupportsFeature("android.software.leanback") ||
117 testing::deviceSupportsFeature("android.hardware.type.television");
118}
119
Doug Ferraz83b22672024-03-22 19:54:26 -0400120static bool isHandheld() {
121 return testing::deviceSupportsFeature("android.hardware.type.handheld");
122}
123
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000124class ThroughputLogger {
125 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700126 explicit ThroughputLogger(std::string task)
Myles Watsone1708c82023-01-18 17:07:58 -0800127 : total_bytes_(0),
Jack Hedbceaca2023-03-27 18:06:38 -0700128 task_(std::move(task)),
Myles Watsone1708c82023-01-18 17:07:58 -0800129 start_time_(std::chrono::steady_clock::now()) {}
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000130
131 ~ThroughputLogger() {
132 if (total_bytes_ == 0) {
133 return;
134 }
135 std::chrono::duration<double> duration =
136 std::chrono::steady_clock::now() - start_time_;
137 double s = duration.count();
138 if (s == 0) {
139 return;
140 }
141 double rate_kb = (static_cast<double>(total_bytes_) / s) / 1024;
142 ALOGD("%s %.1f KB/s (%zu bytes in %.3fs)", task_.c_str(), rate_kb,
143 total_bytes_, s);
144 }
145
146 void setTotalBytes(size_t total_bytes) { total_bytes_ = total_bytes; }
147
148 private:
149 size_t total_bytes_;
150 std::string task_;
151 std::chrono::steady_clock::time_point start_time_;
152};
153
154// The main test class for Bluetooth HAL.
155class BluetoothAidlTest : public ::testing::TestWithParam<std::string> {
William Escande1a139902024-02-27 15:57:33 -0800156 std::chrono::time_point<std::chrono::system_clock>
157 time_after_initialize_complete;
158
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000159 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700160 void SetUp() override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000161 // currently test passthrough mode only
162 hci = IBluetoothHci::fromBinder(
163 SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
164 ASSERT_NE(hci, nullptr);
165 ALOGI("%s: getService() for bluetooth hci is %s", __func__,
166 hci->isRemote() ? "remote" : "local");
167
168 // Lambda function
169 auto on_binder_death = [](void* /*cookie*/) { FAIL(); };
170
171 bluetooth_hci_death_recipient =
172 AIBinder_DeathRecipient_new(on_binder_death);
173 ASSERT_NE(bluetooth_hci_death_recipient, nullptr);
174 ASSERT_EQ(STATUS_OK,
175 AIBinder_linkToDeath(hci->asBinder().get(),
Jack Hedbceaca2023-03-27 18:06:38 -0700176 bluetooth_hci_death_recipient, nullptr));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000177
178 hci_cb = ndk::SharedRefBase::make<BluetoothHciCallbacks>(*this);
179 ASSERT_NE(hci_cb, nullptr);
180
181 max_acl_data_packet_length = 0;
182 max_sco_data_packet_length = 0;
183 max_acl_data_packets = 0;
184 max_sco_data_packets = 0;
185
186 event_cb_count = 0;
187 acl_cb_count = 0;
188 sco_cb_count = 0;
William Escande1a139902024-02-27 15:57:33 -0800189 std::chrono::time_point<std::chrono::system_clock>
190 timeout_after_initialize =
191 std::chrono::system_clock::now() + kWaitForInitTimeout;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000192
193 ASSERT_TRUE(hci->initialize(hci_cb).isOk());
194 auto future = initialized_promise.get_future();
195 auto timeout_status = future.wait_for(kWaitForInitTimeout);
196 ASSERT_EQ(timeout_status, std::future_status::ready);
197 ASSERT_TRUE(future.get());
William Escande1a139902024-02-27 15:57:33 -0800198 ASSERT_GE(timeout_after_initialize, time_after_initialize_complete);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000199 }
200
Jack Hedbceaca2023-03-27 18:06:38 -0700201 void TearDown() override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000202 ALOGI("TearDown");
203 // Should not be checked in production code
204 ASSERT_TRUE(hci->close().isOk());
205 std::this_thread::sleep_for(kInterfaceCloseDelayMs);
206 handle_no_ops();
Myles Watsone1708c82023-01-18 17:07:58 -0800207 discard_qca_debugging();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000208 EXPECT_EQ(static_cast<size_t>(0), event_queue.size());
209 EXPECT_EQ(static_cast<size_t>(0), sco_queue.size());
210 EXPECT_EQ(static_cast<size_t>(0), acl_queue.size());
211 EXPECT_EQ(static_cast<size_t>(0), iso_queue.size());
212 }
213
214 void setBufferSizes();
Myles Watsone1708c82023-01-18 17:07:58 -0800215 void setSynchronousFlowControlEnable();
216
217 // Functions called from within tests in loopback mode
218 void sendAndCheckHci(int num_packets);
219 void sendAndCheckSco(int num_packets, size_t size, uint16_t handle);
220 void sendAndCheckAcl(int num_packets, size_t size, uint16_t handle);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000221
222 // Helper functions to try to get a handle on verbosity
Myles Watsone1708c82023-01-18 17:07:58 -0800223 void enterLoopbackMode();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000224 void handle_no_ops();
Myles Watsone1708c82023-01-18 17:07:58 -0800225 void discard_qca_debugging();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000226 void wait_for_event(bool timeout_is_error);
Jack He2d2f3c22023-03-27 03:04:52 -0700227 void wait_for_command_complete_event(OpCode opCode,
228 std::vector<uint8_t>& complete_event);
229 // Wait until a command complete is received.
230 // Command complete will be consumed after this method
231 void wait_and_validate_command_complete_event(OpCode opCode);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000232 int wait_for_completed_packets_event(uint16_t handle);
Jack He2d2f3c22023-03-27 03:04:52 -0700233 void send_and_wait_for_cmd_complete(std::unique_ptr<CommandBuilder> cmd,
234 std::vector<uint8_t>& cmd_complete);
xiaoshun.xu4e85c092023-07-11 17:30:04 +0800235 void reassemble_sco_loopback_pkt(std::vector<uint8_t>& scoPackets,
236 size_t size);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000237
238 // A simple test implementation of BluetoothHciCallbacks.
239 class BluetoothHciCallbacks
240 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
241 BluetoothAidlTest& parent_;
242
243 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700244 explicit BluetoothHciCallbacks(BluetoothAidlTest& parent)
245 : parent_(parent){};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000246
Jack Hedbceaca2023-03-27 18:06:38 -0700247 ~BluetoothHciCallbacks() override = default;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000248
Jack Hedbceaca2023-03-27 18:06:38 -0700249 ndk::ScopedAStatus initializationComplete(Status status) override {
William Escande1a139902024-02-27 15:57:33 -0800250 if (status == Status::SUCCESS) {
251 parent_.time_after_initialize_complete =
252 std::chrono::system_clock::now();
253 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000254 parent_.initialized_promise.set_value(status == Status::SUCCESS);
255 ALOGV("%s (status = %d)", __func__, static_cast<int>(status));
256 return ScopedAStatus::ok();
257 };
258
Jack Hedbceaca2023-03-27 18:06:38 -0700259 ndk::ScopedAStatus hciEventReceived(
260 const std::vector<uint8_t>& event) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000261 parent_.event_cb_count++;
262 parent_.event_queue.push(event);
Jack He2d2f3c22023-03-27 03:04:52 -0700263 ALOGI("Event received (length = %d)", static_cast<int>(event.size()));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000264 return ScopedAStatus::ok();
265 };
266
Jack Hedbceaca2023-03-27 18:06:38 -0700267 ndk::ScopedAStatus aclDataReceived(
268 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000269 parent_.acl_cb_count++;
270 parent_.acl_queue.push(data);
271 return ScopedAStatus::ok();
272 };
273
Jack Hedbceaca2023-03-27 18:06:38 -0700274 ndk::ScopedAStatus scoDataReceived(
275 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000276 parent_.sco_cb_count++;
277 parent_.sco_queue.push(data);
278 return ScopedAStatus::ok();
279 };
280
Jack Hedbceaca2023-03-27 18:06:38 -0700281 ndk::ScopedAStatus isoDataReceived(
282 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000283 parent_.iso_cb_count++;
284 parent_.iso_queue.push(data);
285 return ScopedAStatus::ok();
286 };
287 };
288
289 template <class T>
290 class WaitQueue {
291 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700292 WaitQueue() = default;
293 ;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000294
295 virtual ~WaitQueue() = default;
296
297 bool empty() const {
298 std::lock_guard<std::mutex> lock(m_);
299 return q_.empty();
300 };
301
302 size_t size() const {
303 std::lock_guard<std::mutex> lock(m_);
304 return q_.size();
305 };
306
307 void push(const T& v) {
308 std::lock_guard<std::mutex> lock(m_);
309 q_.push(v);
310 ready_.notify_one();
311 };
312
313 bool pop(T& v) {
314 std::lock_guard<std::mutex> lock(m_);
315 if (q_.empty()) {
316 return false;
317 }
318 v = std::move(q_.front());
319 q_.pop();
320 return true;
321 };
322
Myles Watson51b8bae2023-01-27 14:22:24 -0800323 void pop() {
324 std::lock_guard<std::mutex> lock(m_);
325 if (q_.empty()) {
326 return;
327 }
328 q_.pop();
329 };
330
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000331 bool front(T& v) {
332 std::lock_guard<std::mutex> lock(m_);
333 if (q_.empty()) {
334 return false;
335 }
336 v = q_.front();
337 return true;
338 };
339
340 void wait() {
341 std::unique_lock<std::mutex> lock(m_);
342 while (q_.empty()) {
343 ready_.wait(lock);
344 }
345 };
346
347 bool waitWithTimeout(std::chrono::milliseconds timeout) {
348 std::unique_lock<std::mutex> lock(m_);
349 while (q_.empty()) {
350 if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
351 return false;
352 }
353 }
354 return true;
355 };
356
357 bool tryPopWithTimeout(T& v, std::chrono::milliseconds timeout) {
358 std::unique_lock<std::mutex> lock(m_);
359 while (q_.empty()) {
360 if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
361 return false;
362 }
363 }
364 v = std::move(q_.front());
365 q_.pop();
366 return true;
367 };
368
369 private:
370 mutable std::mutex m_;
371 std::queue<T> q_;
372 std::condition_variable_any ready_;
373 };
374
375 std::shared_ptr<IBluetoothHci> hci;
376 std::shared_ptr<BluetoothHciCallbacks> hci_cb;
Jack Hedbceaca2023-03-27 18:06:38 -0700377 AIBinder_DeathRecipient* bluetooth_hci_death_recipient{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000378 WaitQueue<std::vector<uint8_t>> event_queue;
379 WaitQueue<std::vector<uint8_t>> acl_queue;
380 WaitQueue<std::vector<uint8_t>> sco_queue;
381 WaitQueue<std::vector<uint8_t>> iso_queue;
382
383 std::promise<bool> initialized_promise;
Jack Hedbceaca2023-03-27 18:06:38 -0700384 int event_cb_count{};
385 int sco_cb_count{};
386 int acl_cb_count{};
387 int iso_cb_count{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000388
Jack Hedbceaca2023-03-27 18:06:38 -0700389 int max_acl_data_packet_length{};
390 int max_sco_data_packet_length{};
391 int max_acl_data_packets{};
392 int max_sco_data_packets{};
Myles Watsone1708c82023-01-18 17:07:58 -0800393
394 std::vector<uint16_t> sco_connection_handles;
395 std::vector<uint16_t> acl_connection_handles;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000396};
397
398// Discard NO-OPs from the event queue.
399void BluetoothAidlTest::handle_no_ops() {
400 while (!event_queue.empty()) {
401 std::vector<uint8_t> event;
402 event_queue.front(event);
Myles Watson51b8bae2023-01-27 14:22:24 -0800403 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
404 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
405 std::make_shared<std::vector<uint8_t>>(event))));
406 auto status_view = ::bluetooth::hci::CommandCompleteView::Create(
407 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
408 std::make_shared<std::vector<uint8_t>>(event))));
409 bool is_complete_no_op =
410 complete_view.IsValid() &&
411 complete_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
412 bool is_status_no_op =
413 status_view.IsValid() &&
414 status_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
415 if (is_complete_no_op || is_status_no_op) {
416 event_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000417 } else {
418 break;
419 }
420 }
Myles Watsone1708c82023-01-18 17:07:58 -0800421}
422
423// Discard Qualcomm ACL debugging
424void BluetoothAidlTest::discard_qca_debugging() {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000425 while (!acl_queue.empty()) {
426 std::vector<uint8_t> acl_packet;
427 acl_queue.front(acl_packet);
Myles Watson51b8bae2023-01-27 14:22:24 -0800428 auto acl_view =
429 ::bluetooth::hci::AclView::Create(::bluetooth::hci::PacketView<true>(
430 std::make_shared<std::vector<uint8_t>>(acl_packet)));
431 EXPECT_TRUE(acl_view.IsValid());
432 if (acl_view.GetHandle() == kAclHandleQcaDebugMessage) {
433 acl_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000434 } else {
435 break;
436 }
437 }
438}
439
440// Receive an event, discarding NO-OPs.
441void BluetoothAidlTest::wait_for_event(bool timeout_is_error = true) {
Myles Watsone1708c82023-01-18 17:07:58 -0800442 // Wait until we get something that's not a no-op.
443 while (true) {
444 bool event_ready = event_queue.waitWithTimeout(kWaitForHciEventTimeout);
445 ASSERT_TRUE(event_ready || !timeout_is_error);
446 if (event_queue.empty()) {
447 // waitWithTimeout timed out
448 return;
449 }
450 handle_no_ops();
451 if (!event_queue.empty()) {
452 // There's an event in the queue that's not a no-op.
453 return;
454 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000455 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000456}
457
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000458void BluetoothAidlTest::wait_for_command_complete_event(
Jack He2d2f3c22023-03-27 03:04:52 -0700459 OpCode opCode, std::vector<uint8_t>& complete_event) {
Myles Watsone1708c82023-01-18 17:07:58 -0800460 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Myles Watsone1708c82023-01-18 17:07:58 -0800461 ASSERT_FALSE(event_queue.empty());
Jack He2d2f3c22023-03-27 03:04:52 -0700462 ASSERT_TRUE(event_queue.pop(complete_event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800463 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
464 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
Jack He2d2f3c22023-03-27 03:04:52 -0700465 std::make_shared<std::vector<uint8_t>>(complete_event))));
Myles Watson51b8bae2023-01-27 14:22:24 -0800466 ASSERT_TRUE(complete_view.IsValid());
467 ASSERT_EQ(complete_view.GetCommandOpCode(), opCode);
468 ASSERT_EQ(complete_view.GetPayload()[0],
469 static_cast<uint8_t>(::bluetooth::hci::ErrorCode::SUCCESS));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000470}
471
Jack He2d2f3c22023-03-27 03:04:52 -0700472void BluetoothAidlTest::wait_and_validate_command_complete_event(
473 ::bluetooth::hci::OpCode opCode) {
474 std::vector<uint8_t> complete_event;
475 ASSERT_NO_FATAL_FAILURE(
476 wait_for_command_complete_event(opCode, complete_event));
477}
478
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000479// Send the command to read the controller's buffer sizes.
480void BluetoothAidlTest::setBufferSizes() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800481 std::vector<uint8_t> cmd;
482 ::bluetooth::packet::BitInserter bi{cmd};
483 ::bluetooth::hci::ReadBufferSizeBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000484 hci->sendHciCommand(cmd);
485
Myles Watsone1708c82023-01-18 17:07:58 -0800486 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000487 std::vector<uint8_t> event;
488 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800489 auto complete_view = ::bluetooth::hci::ReadBufferSizeCompleteView::Create(
490 ::bluetooth::hci::CommandCompleteView::Create(
491 ::bluetooth::hci::EventView::Create(
492 ::bluetooth::hci::PacketView<true>(
493 std::make_shared<std::vector<uint8_t>>(event)))));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000494
Myles Watson51b8bae2023-01-27 14:22:24 -0800495 ASSERT_TRUE(complete_view.IsValid());
496 ASSERT_EQ(complete_view.GetStatus(), ::bluetooth::hci::ErrorCode::SUCCESS);
497 max_acl_data_packet_length = complete_view.GetAclDataPacketLength();
498 max_sco_data_packet_length = complete_view.GetSynchronousDataPacketLength();
499 max_acl_data_packets = complete_view.GetTotalNumAclDataPackets();
500 max_sco_data_packets = complete_view.GetTotalNumSynchronousDataPackets();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000501
502 ALOGD("%s: ACL max %d num %d SCO max %d num %d", __func__,
503 static_cast<int>(max_acl_data_packet_length),
504 static_cast<int>(max_acl_data_packets),
505 static_cast<int>(max_sco_data_packet_length),
506 static_cast<int>(max_sco_data_packets));
507}
508
Myles Watsone1708c82023-01-18 17:07:58 -0800509// Enable flow control packets for SCO
510void BluetoothAidlTest::setSynchronousFlowControlEnable() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800511 std::vector<uint8_t> cmd;
512 ::bluetooth::packet::BitInserter bi{cmd};
513 ::bluetooth::hci::WriteSynchronousFlowControlEnableBuilder::Create(
514 ::bluetooth::hci::Enable::ENABLED)
515 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800516 hci->sendHciCommand(cmd);
517
Jack He2d2f3c22023-03-27 03:04:52 -0700518 wait_and_validate_command_complete_event(
Myles Watson51b8bae2023-01-27 14:22:24 -0800519 ::bluetooth::hci::OpCode::WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE);
Myles Watsone1708c82023-01-18 17:07:58 -0800520}
521
522// Send an HCI command (in Loopback mode) and check the response.
523void BluetoothAidlTest::sendAndCheckHci(int num_packets) {
Jack Hedbceaca2023-03-27 18:06:38 -0700524 ThroughputLogger logger{__func__};
525 size_t command_size = 0;
Myles Watson51b8bae2023-01-27 14:22:24 -0800526 char new_name[] = "John Jacob Jingleheimer Schmidt ___________________";
527 size_t new_name_length = strlen(new_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800528 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800529 // The name to set is new_name
Jack Hedbceaca2023-03-27 18:06:38 -0700530 std::array<uint8_t, 248> name_array{};
Myles Watsone1708c82023-01-18 17:07:58 -0800531 for (size_t i = 0; i < new_name_length; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800532 name_array[i] = new_name[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800533 }
534 // And the packet number
Myles Watson51b8bae2023-01-27 14:22:24 -0800535 char number[11] = "0000000000";
536 snprintf(number, sizeof(number), "%010d", static_cast<int>(n));
537 for (size_t i = new_name_length; i < new_name_length + sizeof(number) - 1;
538 i++) {
539 name_array[new_name_length + i] = number[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800540 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800541 std::vector<uint8_t> write_name;
542 ::bluetooth::packet::BitInserter bi{write_name};
543 ::bluetooth::hci::WriteLocalNameBuilder::Create(name_array)->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800544 hci->sendHciCommand(write_name);
545
546 // Check the loopback of the HCI packet
547 ASSERT_NO_FATAL_FAILURE(wait_for_event());
548
549 std::vector<uint8_t> event;
550 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800551 auto event_view = ::bluetooth::hci::LoopbackCommandView::Create(
552 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
553 std::make_shared<std::vector<uint8_t>>(event))));
554 ASSERT_TRUE(event_view.IsValid());
555 std::vector<uint8_t> looped_back_command{event_view.GetPayload().begin(),
556 event_view.GetPayload().end()};
557 ASSERT_EQ(looped_back_command, write_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800558
559 if (n == num_packets - 1) {
560 command_size = write_name.size();
561 }
Myles Watsone1708c82023-01-18 17:07:58 -0800562 }
563 logger.setTotalBytes(command_size * num_packets * 2);
564}
565
566// Send a SCO data packet (in Loopback mode) and check the response.
567void BluetoothAidlTest::sendAndCheckSco(int num_packets, size_t size,
568 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700569 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800570 for (int n = 0; n < num_packets; n++) {
571 // Send a SCO packet
572 std::vector<uint8_t> sco_packet;
Myles Watson51b8bae2023-01-27 14:22:24 -0800573 std::vector<uint8_t> payload;
Myles Watsone1708c82023-01-18 17:07:58 -0800574 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800575 payload.push_back(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800576 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800577 ::bluetooth::packet::BitInserter bi{sco_packet};
578 ::bluetooth::hci::ScoBuilder::Create(
579 handle, ::bluetooth::hci::PacketStatusFlag::CORRECTLY_RECEIVED, payload)
580 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800581 hci->sendScoData(sco_packet);
582
583 // Check the loopback of the SCO packet
584 std::vector<uint8_t> sco_loopback;
585 ASSERT_TRUE(
586 sco_queue.tryPopWithTimeout(sco_loopback, kWaitForScoDataTimeout));
587
xiaoshun.xu4e85c092023-07-11 17:30:04 +0800588 if (sco_loopback.size() < size) {
589 // The packets may have been split for USB. Reassemble before checking.
590 reassemble_sco_loopback_pkt(sco_loopback, size);
591 }
592
Myles Watson51b8bae2023-01-27 14:22:24 -0800593 ASSERT_EQ(sco_packet, sco_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800594 }
595 logger.setTotalBytes(num_packets * size * 2);
596}
597
598// Send an ACL data packet (in Loopback mode) and check the response.
599void BluetoothAidlTest::sendAndCheckAcl(int num_packets, size_t size,
600 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700601 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800602 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800603 // Send an ACL packet with counting data
604 auto payload = std::make_unique<::bluetooth::packet::RawBuilder>();
Myles Watsone1708c82023-01-18 17:07:58 -0800605 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800606 payload->AddOctets1(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800607 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800608 std::vector<uint8_t> acl_packet;
609 ::bluetooth::packet::BitInserter bi{acl_packet};
610 ::bluetooth::hci::AclBuilder::Create(
611 handle,
612 ::bluetooth::hci::PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE,
613 ::bluetooth::hci::BroadcastFlag::POINT_TO_POINT, std::move(payload))
614 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800615 hci->sendAclData(acl_packet);
616
617 std::vector<uint8_t> acl_loopback;
618 // Check the loopback of the ACL packet
619 ASSERT_TRUE(
620 acl_queue.tryPopWithTimeout(acl_loopback, kWaitForAclDataTimeout));
621
Myles Watson51b8bae2023-01-27 14:22:24 -0800622 ASSERT_EQ(acl_packet, acl_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800623 }
624 logger.setTotalBytes(num_packets * size * 2);
625}
626
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000627// Return the number of completed packets reported by the controller.
628int BluetoothAidlTest::wait_for_completed_packets_event(uint16_t handle) {
629 int packets_processed = 0;
Myles Watson65b47f52023-01-26 12:59:06 -0800630 while (true) {
631 // There should be at least one event.
632 wait_for_event(packets_processed == 0);
633 if (event_queue.empty()) {
634 if (packets_processed == 0) {
635 ALOGW("%s: waitForBluetoothCallback timed out.", __func__);
636 }
637 return packets_processed;
638 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000639 std::vector<uint8_t> event;
640 EXPECT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800641 auto event_view = ::bluetooth::hci::NumberOfCompletedPacketsView::Create(
642 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
643 std::make_shared<std::vector<uint8_t>>(event))));
644 if (!event_view.IsValid()) {
645 ADD_FAILURE();
646 return packets_processed;
647 }
648 auto completed_packets = event_view.GetCompletedPackets();
Jack Hedbceaca2023-03-27 18:06:38 -0700649 for (const auto& entry : completed_packets) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800650 EXPECT_EQ(handle, entry.connection_handle_);
651 packets_processed += entry.host_num_of_completed_packets_;
652 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000653 }
654 return packets_processed;
655}
656
Myles Watsone1708c82023-01-18 17:07:58 -0800657// Send local loopback command and initialize SCO and ACL handles.
658void BluetoothAidlTest::enterLoopbackMode() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800659 std::vector<uint8_t> cmd;
660 ::bluetooth::packet::BitInserter bi{cmd};
661 ::bluetooth::hci::WriteLoopbackModeBuilder::Create(
662 bluetooth::hci::LoopbackMode::ENABLE_LOCAL)
663 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800664 hci->sendHciCommand(cmd);
665
666 // Receive connection complete events with data channels
667 int connection_event_count = 0;
668 bool command_complete_received = false;
669 while (true) {
670 wait_for_event(false);
671 if (event_queue.empty()) {
672 // Fail if there was no event received or no connections completed.
673 ASSERT_TRUE(command_complete_received);
674 ASSERT_LT(0, connection_event_count);
675 return;
676 }
677 std::vector<uint8_t> event;
678 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800679 auto event_view =
680 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
681 std::make_shared<std::vector<uint8_t>>(event)));
682 ASSERT_TRUE(event_view.IsValid());
Myles Watsone1708c82023-01-18 17:07:58 -0800683
Myles Watson51b8bae2023-01-27 14:22:24 -0800684 if (event_view.GetEventCode() ==
685 ::bluetooth::hci::EventCode::CONNECTION_COMPLETE) {
686 auto complete_view =
687 ::bluetooth::hci::ConnectionCompleteView::Create(event_view);
688 ASSERT_TRUE(complete_view.IsValid());
689 switch (complete_view.GetLinkType()) {
690 case ::bluetooth::hci::LinkType::ACL:
691 acl_connection_handles.push_back(complete_view.GetConnectionHandle());
692 break;
693 case ::bluetooth::hci::LinkType::SCO:
694 sco_connection_handles.push_back(complete_view.GetConnectionHandle());
695 break;
696 default:
697 ASSERT_EQ(complete_view.GetLinkType(),
698 ::bluetooth::hci::LinkType::ACL);
Myles Watsone1708c82023-01-18 17:07:58 -0800699 }
Myles Watsone1708c82023-01-18 17:07:58 -0800700 connection_event_count++;
701 } else {
Myles Watson51b8bae2023-01-27 14:22:24 -0800702 auto command_complete_view =
703 ::bluetooth::hci::WriteLoopbackModeCompleteView::Create(
704 ::bluetooth::hci::CommandCompleteView::Create(event_view));
705 ASSERT_TRUE(command_complete_view.IsValid());
706 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
707 command_complete_view.GetStatus());
Myles Watsone1708c82023-01-18 17:07:58 -0800708 command_complete_received = true;
709 }
710 }
711}
712
Jack He2d2f3c22023-03-27 03:04:52 -0700713void BluetoothAidlTest::send_and_wait_for_cmd_complete(
714 std::unique_ptr<CommandBuilder> cmd, std::vector<uint8_t>& cmd_complete) {
715 std::vector<uint8_t> cmd_bytes = cmd->SerializeToBytes();
716 hci->sendHciCommand(cmd_bytes);
717
718 auto view = CommandView::Create(
719 PacketView<true>(std::make_shared<std::vector<uint8_t>>(cmd_bytes)));
720 ASSERT_TRUE(view.IsValid());
721 ALOGI("Waiting for %s[0x%x]", OpCodeText(view.GetOpCode()).c_str(),
722 static_cast<int>(view.GetOpCode()));
723 ASSERT_NO_FATAL_FAILURE(
724 wait_for_command_complete_event(view.GetOpCode(), cmd_complete));
725}
726
xiaoshun.xu4e85c092023-07-11 17:30:04 +0800727// Handle the loopback packet.
728void BluetoothAidlTest::reassemble_sco_loopback_pkt(std::vector<uint8_t>& scoPackets,
729 size_t size) {
730 std::vector<uint8_t> sco_packet_whole;
731 sco_packet_whole.assign(scoPackets.begin(), scoPackets.end());
732 while (size + 3 > sco_packet_whole.size()) {
733 std::vector<uint8_t> sco_packets;
734 ASSERT_TRUE(
735 sco_queue.tryPopWithTimeout(sco_packets, kWaitForScoDataTimeout));
736 sco_packet_whole.insert(sco_packet_whole.end(), sco_packets.begin() + 3,
737 sco_packets.end());
738 }
739 scoPackets.assign(sco_packet_whole.begin(), sco_packet_whole.end());
740 scoPackets[2] = size;
741}
742
Myles Watsone1708c82023-01-18 17:07:58 -0800743// Empty test: Initialize()/Close() are called in SetUp()/TearDown().
744TEST_P(BluetoothAidlTest, InitializeAndClose) {}
745
746// Send an HCI Reset with sendHciCommand and wait for a command complete event.
Myles Watson65b47f52023-01-26 12:59:06 -0800747TEST_P(BluetoothAidlTest, HciReset) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800748 std::vector<uint8_t> reset;
749 ::bluetooth::packet::BitInserter bi{reset};
750 ::bluetooth::hci::ResetBuilder::Create()->Serialize(bi);
Myles Watson65b47f52023-01-26 12:59:06 -0800751 hci->sendHciCommand(reset);
752
Jack He2d2f3c22023-03-27 03:04:52 -0700753 wait_and_validate_command_complete_event(::bluetooth::hci::OpCode::RESET);
Myles Watson65b47f52023-01-26 12:59:06 -0800754}
Myles Watsone1708c82023-01-18 17:07:58 -0800755
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000756// Read and check the HCI version of the controller.
757TEST_P(BluetoothAidlTest, HciVersionTest) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800758 std::vector<uint8_t> cmd;
759 ::bluetooth::packet::BitInserter bi{cmd};
760 ::bluetooth::hci::ReadLocalVersionInformationBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000761 hci->sendHciCommand(cmd);
762
Myles Watsone1708c82023-01-18 17:07:58 -0800763 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000764
765 std::vector<uint8_t> event;
766 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800767 auto complete_view =
768 ::bluetooth::hci::ReadLocalVersionInformationCompleteView::Create(
769 ::bluetooth::hci::CommandCompleteView::Create(
770 ::bluetooth::hci::EventView::Create(
771 ::bluetooth::hci::PacketView<true>(
772 std::make_shared<std::vector<uint8_t>>(event)))));
773 ASSERT_TRUE(complete_view.IsValid());
774 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, complete_view.GetStatus());
775 auto version = complete_view.GetLocalVersionInformation();
776 ASSERT_LE(::bluetooth::hci::HciVersion::V_3_0, version.hci_version_);
777 ASSERT_LE(::bluetooth::hci::LmpVersion::V_3_0, version.lmp_version_);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000778}
779
780// Send an unknown HCI command and wait for the error message.
781TEST_P(BluetoothAidlTest, HciUnknownCommand) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800782 std::vector<uint8_t> cmd;
783 ::bluetooth::packet::BitInserter bi{cmd};
784 ::bluetooth::hci::CommandBuilder::Create(
785 static_cast<::bluetooth::hci::OpCode>(0x3cff),
786 std::make_unique<::bluetooth::packet::RawBuilder>())
787 ->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000788 hci->sendHciCommand(cmd);
789
Myles Watsone1708c82023-01-18 17:07:58 -0800790 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000791
792 std::vector<uint8_t> event;
793 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800794 auto event_view =
795 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
796 std::make_shared<std::vector<uint8_t>>(event)));
797 ASSERT_TRUE(event_view.IsValid());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000798
Myles Watson51b8bae2023-01-27 14:22:24 -0800799 switch (event_view.GetEventCode()) {
800 case ::bluetooth::hci::EventCode::COMMAND_COMPLETE: {
801 auto command_complete =
802 ::bluetooth::hci::CommandCompleteView::Create(event_view);
803 ASSERT_TRUE(command_complete.IsValid());
804 ASSERT_EQ(command_complete.GetPayload()[0],
805 static_cast<uint8_t>(
806 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND));
807 } break;
808 case ::bluetooth::hci::EventCode::COMMAND_STATUS: {
809 auto command_status =
810 ::bluetooth::hci::CommandStatusView::Create(event_view);
811 ASSERT_TRUE(command_status.IsValid());
812 ASSERT_EQ(command_status.GetStatus(),
813 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND);
814 } break;
815 default:
816 ADD_FAILURE();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000817 }
818}
819
Myles Watsone1708c82023-01-18 17:07:58 -0800820// Enter loopback mode, but don't send any packets.
Myles Watson65b47f52023-01-26 12:59:06 -0800821TEST_P(BluetoothAidlTest, WriteLoopbackMode) { enterLoopbackMode(); }
Myles Watsone1708c82023-01-18 17:07:58 -0800822
823// Enter loopback mode and send a single command.
824TEST_P(BluetoothAidlTest, LoopbackModeSingleCommand) {
Myles Watsone1708c82023-01-18 17:07:58 -0800825 setBufferSizes();
826
827 enterLoopbackMode();
828
829 sendAndCheckHci(1);
830}
831
832// Enter loopback mode and send a single SCO packet.
833TEST_P(BluetoothAidlTest, LoopbackModeSingleSco) {
Myles Watsone1708c82023-01-18 17:07:58 -0800834 setBufferSizes();
835 setSynchronousFlowControlEnable();
836
837 enterLoopbackMode();
838
839 if (!sco_connection_handles.empty()) {
840 ASSERT_LT(0, max_sco_data_packet_length);
841 sendAndCheckSco(1, max_sco_data_packet_length, sco_connection_handles[0]);
842 int sco_packets_sent = 1;
843 int completed_packets =
844 wait_for_completed_packets_event(sco_connection_handles[0]);
845 if (sco_packets_sent != completed_packets) {
846 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
847 sco_packets_sent, completed_packets);
848 }
849 }
850}
851
852// Enter loopback mode and send a single ACL packet.
853TEST_P(BluetoothAidlTest, LoopbackModeSingleAcl) {
Myles Watsone1708c82023-01-18 17:07:58 -0800854 setBufferSizes();
855
856 enterLoopbackMode();
857
858 if (!acl_connection_handles.empty()) {
859 ASSERT_LT(0, max_acl_data_packet_length);
860 sendAndCheckAcl(1, max_acl_data_packet_length - 1,
861 acl_connection_handles[0]);
862 int acl_packets_sent = 1;
863 int completed_packets =
864 wait_for_completed_packets_event(acl_connection_handles[0]);
865 if (acl_packets_sent != completed_packets) {
866 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
867 acl_packets_sent, completed_packets);
868 }
869 }
870 ASSERT_GE(acl_cb_count, 1);
871}
872
873// Enter loopback mode and send command packets for bandwidth measurements.
874TEST_P(BluetoothAidlTest, LoopbackModeCommandBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800875 setBufferSizes();
876
877 enterLoopbackMode();
878
879 sendAndCheckHci(kNumHciCommandsBandwidth);
880}
881
882// Enter loopback mode and send SCO packets for bandwidth measurements.
883TEST_P(BluetoothAidlTest, LoopbackModeScoBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800884 setBufferSizes();
885 setSynchronousFlowControlEnable();
886
887 enterLoopbackMode();
888
889 if (!sco_connection_handles.empty()) {
890 ASSERT_LT(0, max_sco_data_packet_length);
891 sendAndCheckSco(kNumScoPacketsBandwidth, max_sco_data_packet_length,
892 sco_connection_handles[0]);
893 int sco_packets_sent = kNumScoPacketsBandwidth;
894 int completed_packets =
895 wait_for_completed_packets_event(sco_connection_handles[0]);
896 if (sco_packets_sent != completed_packets) {
897 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
898 sco_packets_sent, completed_packets);
899 }
900 }
901}
902
903// Enter loopback mode and send packets for ACL bandwidth measurements.
904TEST_P(BluetoothAidlTest, LoopbackModeAclBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800905 setBufferSizes();
906
907 enterLoopbackMode();
908
909 if (!acl_connection_handles.empty()) {
910 ASSERT_LT(0, max_acl_data_packet_length);
911 sendAndCheckAcl(kNumAclPacketsBandwidth, max_acl_data_packet_length - 1,
912 acl_connection_handles[0]);
913 int acl_packets_sent = kNumAclPacketsBandwidth;
914 int completed_packets =
915 wait_for_completed_packets_event(acl_connection_handles[0]);
916 if (acl_packets_sent != completed_packets) {
917 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
918 acl_packets_sent, completed_packets);
919 }
920 }
921}
922
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000923// Set all bits in the event mask
924TEST_P(BluetoothAidlTest, SetEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800925 std::vector<uint8_t> cmd;
926 ::bluetooth::packet::BitInserter bi{cmd};
927 uint64_t full_mask = UINT64_MAX;
928 ::bluetooth::hci::SetEventMaskBuilder::Create(full_mask)->Serialize(bi);
929 hci->sendHciCommand(cmd);
Jack He2d2f3c22023-03-27 03:04:52 -0700930 wait_and_validate_command_complete_event(
931 ::bluetooth::hci::OpCode::SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000932}
933
934// Set all bits in the LE event mask
935TEST_P(BluetoothAidlTest, SetLeEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800936 std::vector<uint8_t> cmd;
937 ::bluetooth::packet::BitInserter bi{cmd};
938 uint64_t full_mask = UINT64_MAX;
939 ::bluetooth::hci::LeSetEventMaskBuilder::Create(full_mask)->Serialize(bi);
940 hci->sendHciCommand(cmd);
Jack He2d2f3c22023-03-27 03:04:52 -0700941 wait_and_validate_command_complete_event(
942 ::bluetooth::hci::OpCode::LE_SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000943}
944
Myles Watson0daa1642023-01-26 15:58:28 -0800945// Call initialize twice, second one should fail.
946TEST_P(BluetoothAidlTest, CallInitializeTwice) {
947 class SecondCb
948 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
949 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700950 ndk::ScopedAStatus initializationComplete(Status status) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800951 EXPECT_EQ(status, Status::ALREADY_INITIALIZED);
952 init_promise.set_value();
953 return ScopedAStatus::ok();
954 };
955
Jack Hedbceaca2023-03-27 18:06:38 -0700956 ndk::ScopedAStatus hciEventReceived(
957 const std::vector<uint8_t>& /*event*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800958 ADD_FAILURE();
959 return ScopedAStatus::ok();
960 };
961
Jack Hedbceaca2023-03-27 18:06:38 -0700962 ndk::ScopedAStatus aclDataReceived(
963 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800964 ADD_FAILURE();
965 return ScopedAStatus::ok();
966 };
967
Jack Hedbceaca2023-03-27 18:06:38 -0700968 ndk::ScopedAStatus scoDataReceived(
969 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800970 ADD_FAILURE();
971 return ScopedAStatus::ok();
972 };
973
Jack Hedbceaca2023-03-27 18:06:38 -0700974 ndk::ScopedAStatus isoDataReceived(
975 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800976 ADD_FAILURE();
977 return ScopedAStatus::ok();
978 };
979 std::promise<void> init_promise;
980 };
981
982 std::shared_ptr<SecondCb> second_cb = ndk::SharedRefBase::make<SecondCb>();
983 ASSERT_NE(second_cb, nullptr);
984
985 auto future = second_cb->init_promise.get_future();
986 ASSERT_TRUE(hci->initialize(second_cb).isOk());
987 auto status = future.wait_for(std::chrono::seconds(1));
988 ASSERT_EQ(status, std::future_status::ready);
989}
990
Myles Watson5bfb3a72023-05-10 14:06:32 -0700991TEST_P(BluetoothAidlTest, Vsr_Bluetooth5Requirements) {
Jack He2d2f3c22023-03-27 03:04:52 -0700992 std::vector<uint8_t> version_event;
993 send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
994 version_event);
995 auto version_view = ReadLocalVersionInformationCompleteView::Create(
996 CommandCompleteView::Create(EventView::Create(PacketView<true>(
997 std::make_shared<std::vector<uint8_t>>(version_event)))));
998 ASSERT_TRUE(version_view.IsValid());
999 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
1000 auto version = version_view.GetLocalVersionInformation();
1001 if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) {
1002 // This test does not apply to controllers below 5.0
1003 return;
1004 };
1005 // When HCI version is 5.0, LMP version must also be at least 5.0
1006 ASSERT_GE(static_cast<int>(version.lmp_version_),
1007 static_cast<int>(version.hci_version_));
1008
1009 std::vector<uint8_t> le_features_event;
1010 send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(),
1011 le_features_event);
1012 auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create(
1013 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1014 std::make_shared<std::vector<uint8_t>>(le_features_event)))));
1015 ASSERT_TRUE(le_features_view.IsValid());
1016 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus());
1017 auto le_features = le_features_view.GetLeFeatures();
1018 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LL_PRIVACY));
1019 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LE_2M_PHY));
1020 ASSERT_TRUE(le_features &
1021 static_cast<uint64_t>(LLFeaturesBits::LE_CODED_PHY));
1022 ASSERT_TRUE(le_features &
1023 static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING));
1024
1025 std::vector<uint8_t> num_adv_set_event;
1026 send_and_wait_for_cmd_complete(
1027 LeReadNumberOfSupportedAdvertisingSetsBuilder::Create(),
1028 num_adv_set_event);
1029 auto num_adv_set_view =
1030 LeReadNumberOfSupportedAdvertisingSetsCompleteView::Create(
1031 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1032 std::make_shared<std::vector<uint8_t>>(num_adv_set_event)))));
1033 ASSERT_TRUE(num_adv_set_view.IsValid());
1034 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, num_adv_set_view.GetStatus());
1035 auto num_adv_set = num_adv_set_view.GetNumberSupportedAdvertisingSets();
Treehugger Robot5e0277f2023-05-12 02:44:22 +00001036
1037 if (isTv() && get_vsr_api_level() == __ANDROID_API_U__) {
Myles Watson864c9042024-07-24 14:39:05 -07001038 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5ForTv);
Treehugger Robot5e0277f2023-05-12 02:44:22 +00001039 } else {
1040 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5);
1041 }
Jack He2d2f3c22023-03-27 03:04:52 -07001042
1043 std::vector<uint8_t> num_resolving_list_event;
1044 send_and_wait_for_cmd_complete(LeReadResolvingListSizeBuilder::Create(),
1045 num_resolving_list_event);
1046 auto num_resolving_list_view = LeReadResolvingListSizeCompleteView::Create(
1047 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1048 std::make_shared<std::vector<uint8_t>>(num_resolving_list_event)))));
1049 ASSERT_TRUE(num_resolving_list_view.IsValid());
1050 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
1051 num_resolving_list_view.GetStatus());
1052 auto num_resolving_list = num_resolving_list_view.GetResolvingListSize();
1053 ASSERT_GE(num_resolving_list, kMinLeResolvingListForBt5);
1054}
1055
Doug Ferraz83b22672024-03-22 19:54:26 -04001056/**
Doug Ferraz83b22672024-03-22 19:54:26 -04001057 * VSR-5.3.14-007 MUST support Bluetooth 4.2 and Bluetooth LE Data Length Extension.
1058 * VSR-5.3.14-008 MUST support Bluetooth Low Energy (BLE).
1059 */
Doug Ferrazeff7b912024-03-25 20:04:19 -04001060// @VsrTest = 5.3.14-007
1061// @VsrTest = 5.3.14-008
Doug Ferraz83b22672024-03-22 19:54:26 -04001062TEST_P(BluetoothAidlTest, Vsr_Bluetooth4_2Requirements) {
1063 // test only applies to handheld devices
1064 if (!isHandheld()) {
1065 return;
1066 }
1067
1068 std::vector<uint8_t> version_event;
1069 send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
1070 version_event);
1071 auto version_view = ReadLocalVersionInformationCompleteView::Create(
1072 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1073 std::make_shared<std::vector<uint8_t>>(version_event)))));
1074 ASSERT_TRUE(version_view.IsValid());
1075 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
1076 auto version = version_view.GetLocalVersionInformation();
1077 // Starting with Android 15, Fails when HCI version is lower than 4.2.
1078 ASSERT_GE(static_cast<int>(version.hci_version_),
1079 static_cast<int>(::bluetooth::hci::HciVersion::V_4_2));
1080 ASSERT_GE(static_cast<int>(version.lmp_version_),
1081 static_cast<int>(::bluetooth::hci::LmpVersion::V_4_2));
1082
1083 std::vector<uint8_t> le_features_event;
1084 send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(),
1085 le_features_event);
1086 auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create(
1087 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1088 std::make_shared<std::vector<uint8_t>>(le_features_event)))));
1089 ASSERT_TRUE(le_features_view.IsValid());
1090 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus());
1091 auto le_features = le_features_view.GetLeFeatures();
1092 ASSERT_TRUE(le_features &
1093 static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING));
1094
1095}
1096
Omair Kamil84a95be2024-04-22 13:29:19 -07001097/**
1098 * VSR-5.3.14-012 MUST support at least eight LE concurrent connections with
1099 * three in peripheral role.
1100 */
1101// @VsrTest = 5.3.14-012
1102TEST_P(BluetoothAidlTest, Vsr_BlE_Connection_Requirement) {
1103 std::vector<uint8_t> version_event;
1104 send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
1105 version_event);
1106 auto version_view = ReadLocalVersionInformationCompleteView::Create(
1107 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1108 std::make_shared<std::vector<uint8_t>>(version_event)))));
1109 ASSERT_TRUE(version_view.IsValid());
1110 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
1111 auto version = version_view.GetLocalVersionInformation();
1112 if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) {
1113 // This test does not apply to controllers below 5.0
1114 return;
1115 };
1116
1117 int max_connections = ::android::base::GetIntProperty(
1118 "bluetooth.core.le.max_number_of_concurrent_connections", -1);
1119 if (max_connections == -1) {
1120 // With the property not set the default minimum of 8 will be used
1121 ALOGI("Max number of LE concurrent connections isn't set");
1122 return;
1123 }
1124 ALOGI("Max number of LE concurrent connections = %d", max_connections);
1125 ASSERT_GE(max_connections, 8);
1126}
1127
Dylan Tian8a6e09c2022-08-16 07:29:28 +00001128GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothAidlTest);
1129INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothAidlTest,
1130 testing::ValuesIn(android::getAidlHalInstanceNames(
1131 IBluetoothHci::descriptor)),
1132 android::PrintInstanceNameToString);
1133
1134int main(int argc, char** argv) {
1135 ABinderProcess_startThreadPool();
1136 ::testing::InitGoogleTest(&argc, argv);
1137 int status = RUN_ALL_TESTS();
1138 ALOGI("Test result = %d", status);
1139 return status;
1140}