blob: 24eb4d08711dafab98d2060348f061f8442b794d [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;
Dylan Tian8a6e09c2022-08-16 07:29:28 +000078static constexpr std::chrono::milliseconds kWaitForInitTimeout(2000);
79static 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> {
153 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700154 void SetUp() override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000155 // currently test passthrough mode only
156 hci = IBluetoothHci::fromBinder(
157 SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
158 ASSERT_NE(hci, nullptr);
159 ALOGI("%s: getService() for bluetooth hci is %s", __func__,
160 hci->isRemote() ? "remote" : "local");
161
162 // Lambda function
163 auto on_binder_death = [](void* /*cookie*/) { FAIL(); };
164
165 bluetooth_hci_death_recipient =
166 AIBinder_DeathRecipient_new(on_binder_death);
167 ASSERT_NE(bluetooth_hci_death_recipient, nullptr);
168 ASSERT_EQ(STATUS_OK,
169 AIBinder_linkToDeath(hci->asBinder().get(),
Jack Hedbceaca2023-03-27 18:06:38 -0700170 bluetooth_hci_death_recipient, nullptr));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000171
172 hci_cb = ndk::SharedRefBase::make<BluetoothHciCallbacks>(*this);
173 ASSERT_NE(hci_cb, nullptr);
174
175 max_acl_data_packet_length = 0;
176 max_sco_data_packet_length = 0;
177 max_acl_data_packets = 0;
178 max_sco_data_packets = 0;
179
180 event_cb_count = 0;
181 acl_cb_count = 0;
182 sco_cb_count = 0;
183
184 ASSERT_TRUE(hci->initialize(hci_cb).isOk());
185 auto future = initialized_promise.get_future();
186 auto timeout_status = future.wait_for(kWaitForInitTimeout);
187 ASSERT_EQ(timeout_status, std::future_status::ready);
188 ASSERT_TRUE(future.get());
189 }
190
Jack Hedbceaca2023-03-27 18:06:38 -0700191 void TearDown() override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000192 ALOGI("TearDown");
193 // Should not be checked in production code
194 ASSERT_TRUE(hci->close().isOk());
195 std::this_thread::sleep_for(kInterfaceCloseDelayMs);
196 handle_no_ops();
Myles Watsone1708c82023-01-18 17:07:58 -0800197 discard_qca_debugging();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000198 EXPECT_EQ(static_cast<size_t>(0), event_queue.size());
199 EXPECT_EQ(static_cast<size_t>(0), sco_queue.size());
200 EXPECT_EQ(static_cast<size_t>(0), acl_queue.size());
201 EXPECT_EQ(static_cast<size_t>(0), iso_queue.size());
202 }
203
204 void setBufferSizes();
Myles Watsone1708c82023-01-18 17:07:58 -0800205 void setSynchronousFlowControlEnable();
206
207 // Functions called from within tests in loopback mode
208 void sendAndCheckHci(int num_packets);
209 void sendAndCheckSco(int num_packets, size_t size, uint16_t handle);
210 void sendAndCheckAcl(int num_packets, size_t size, uint16_t handle);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000211
212 // Helper functions to try to get a handle on verbosity
Myles Watsone1708c82023-01-18 17:07:58 -0800213 void enterLoopbackMode();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000214 void handle_no_ops();
Myles Watsone1708c82023-01-18 17:07:58 -0800215 void discard_qca_debugging();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000216 void wait_for_event(bool timeout_is_error);
Jack He2d2f3c22023-03-27 03:04:52 -0700217 void wait_for_command_complete_event(OpCode opCode,
218 std::vector<uint8_t>& complete_event);
219 // Wait until a command complete is received.
220 // Command complete will be consumed after this method
221 void wait_and_validate_command_complete_event(OpCode opCode);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000222 int wait_for_completed_packets_event(uint16_t handle);
Jack He2d2f3c22023-03-27 03:04:52 -0700223 void send_and_wait_for_cmd_complete(std::unique_ptr<CommandBuilder> cmd,
224 std::vector<uint8_t>& cmd_complete);
xiaoshun.xu4e85c092023-07-11 17:30:04 +0800225 void reassemble_sco_loopback_pkt(std::vector<uint8_t>& scoPackets,
226 size_t size);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000227
228 // A simple test implementation of BluetoothHciCallbacks.
229 class BluetoothHciCallbacks
230 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
231 BluetoothAidlTest& parent_;
232
233 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700234 explicit BluetoothHciCallbacks(BluetoothAidlTest& parent)
235 : parent_(parent){};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000236
Jack Hedbceaca2023-03-27 18:06:38 -0700237 ~BluetoothHciCallbacks() override = default;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000238
Jack Hedbceaca2023-03-27 18:06:38 -0700239 ndk::ScopedAStatus initializationComplete(Status status) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000240 parent_.initialized_promise.set_value(status == Status::SUCCESS);
241 ALOGV("%s (status = %d)", __func__, static_cast<int>(status));
242 return ScopedAStatus::ok();
243 };
244
Jack Hedbceaca2023-03-27 18:06:38 -0700245 ndk::ScopedAStatus hciEventReceived(
246 const std::vector<uint8_t>& event) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000247 parent_.event_cb_count++;
248 parent_.event_queue.push(event);
Jack He2d2f3c22023-03-27 03:04:52 -0700249 ALOGI("Event received (length = %d)", static_cast<int>(event.size()));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000250 return ScopedAStatus::ok();
251 };
252
Jack Hedbceaca2023-03-27 18:06:38 -0700253 ndk::ScopedAStatus aclDataReceived(
254 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000255 parent_.acl_cb_count++;
256 parent_.acl_queue.push(data);
257 return ScopedAStatus::ok();
258 };
259
Jack Hedbceaca2023-03-27 18:06:38 -0700260 ndk::ScopedAStatus scoDataReceived(
261 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000262 parent_.sco_cb_count++;
263 parent_.sco_queue.push(data);
264 return ScopedAStatus::ok();
265 };
266
Jack Hedbceaca2023-03-27 18:06:38 -0700267 ndk::ScopedAStatus isoDataReceived(
268 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000269 parent_.iso_cb_count++;
270 parent_.iso_queue.push(data);
271 return ScopedAStatus::ok();
272 };
273 };
274
275 template <class T>
276 class WaitQueue {
277 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700278 WaitQueue() = default;
279 ;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000280
281 virtual ~WaitQueue() = default;
282
283 bool empty() const {
284 std::lock_guard<std::mutex> lock(m_);
285 return q_.empty();
286 };
287
288 size_t size() const {
289 std::lock_guard<std::mutex> lock(m_);
290 return q_.size();
291 };
292
293 void push(const T& v) {
294 std::lock_guard<std::mutex> lock(m_);
295 q_.push(v);
296 ready_.notify_one();
297 };
298
299 bool pop(T& v) {
300 std::lock_guard<std::mutex> lock(m_);
301 if (q_.empty()) {
302 return false;
303 }
304 v = std::move(q_.front());
305 q_.pop();
306 return true;
307 };
308
Myles Watson51b8bae2023-01-27 14:22:24 -0800309 void pop() {
310 std::lock_guard<std::mutex> lock(m_);
311 if (q_.empty()) {
312 return;
313 }
314 q_.pop();
315 };
316
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000317 bool front(T& v) {
318 std::lock_guard<std::mutex> lock(m_);
319 if (q_.empty()) {
320 return false;
321 }
322 v = q_.front();
323 return true;
324 };
325
326 void wait() {
327 std::unique_lock<std::mutex> lock(m_);
328 while (q_.empty()) {
329 ready_.wait(lock);
330 }
331 };
332
333 bool waitWithTimeout(std::chrono::milliseconds timeout) {
334 std::unique_lock<std::mutex> lock(m_);
335 while (q_.empty()) {
336 if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
337 return false;
338 }
339 }
340 return true;
341 };
342
343 bool tryPopWithTimeout(T& v, std::chrono::milliseconds timeout) {
344 std::unique_lock<std::mutex> lock(m_);
345 while (q_.empty()) {
346 if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
347 return false;
348 }
349 }
350 v = std::move(q_.front());
351 q_.pop();
352 return true;
353 };
354
355 private:
356 mutable std::mutex m_;
357 std::queue<T> q_;
358 std::condition_variable_any ready_;
359 };
360
361 std::shared_ptr<IBluetoothHci> hci;
362 std::shared_ptr<BluetoothHciCallbacks> hci_cb;
Jack Hedbceaca2023-03-27 18:06:38 -0700363 AIBinder_DeathRecipient* bluetooth_hci_death_recipient{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000364 WaitQueue<std::vector<uint8_t>> event_queue;
365 WaitQueue<std::vector<uint8_t>> acl_queue;
366 WaitQueue<std::vector<uint8_t>> sco_queue;
367 WaitQueue<std::vector<uint8_t>> iso_queue;
368
369 std::promise<bool> initialized_promise;
Jack Hedbceaca2023-03-27 18:06:38 -0700370 int event_cb_count{};
371 int sco_cb_count{};
372 int acl_cb_count{};
373 int iso_cb_count{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000374
Jack Hedbceaca2023-03-27 18:06:38 -0700375 int max_acl_data_packet_length{};
376 int max_sco_data_packet_length{};
377 int max_acl_data_packets{};
378 int max_sco_data_packets{};
Myles Watsone1708c82023-01-18 17:07:58 -0800379
380 std::vector<uint16_t> sco_connection_handles;
381 std::vector<uint16_t> acl_connection_handles;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000382};
383
384// Discard NO-OPs from the event queue.
385void BluetoothAidlTest::handle_no_ops() {
386 while (!event_queue.empty()) {
387 std::vector<uint8_t> event;
388 event_queue.front(event);
Myles Watson51b8bae2023-01-27 14:22:24 -0800389 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
390 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
391 std::make_shared<std::vector<uint8_t>>(event))));
392 auto status_view = ::bluetooth::hci::CommandCompleteView::Create(
393 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
394 std::make_shared<std::vector<uint8_t>>(event))));
395 bool is_complete_no_op =
396 complete_view.IsValid() &&
397 complete_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
398 bool is_status_no_op =
399 status_view.IsValid() &&
400 status_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
401 if (is_complete_no_op || is_status_no_op) {
402 event_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000403 } else {
404 break;
405 }
406 }
Myles Watsone1708c82023-01-18 17:07:58 -0800407}
408
409// Discard Qualcomm ACL debugging
410void BluetoothAidlTest::discard_qca_debugging() {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000411 while (!acl_queue.empty()) {
412 std::vector<uint8_t> acl_packet;
413 acl_queue.front(acl_packet);
Myles Watson51b8bae2023-01-27 14:22:24 -0800414 auto acl_view =
415 ::bluetooth::hci::AclView::Create(::bluetooth::hci::PacketView<true>(
416 std::make_shared<std::vector<uint8_t>>(acl_packet)));
417 EXPECT_TRUE(acl_view.IsValid());
418 if (acl_view.GetHandle() == kAclHandleQcaDebugMessage) {
419 acl_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000420 } else {
421 break;
422 }
423 }
424}
425
426// Receive an event, discarding NO-OPs.
427void BluetoothAidlTest::wait_for_event(bool timeout_is_error = true) {
Myles Watsone1708c82023-01-18 17:07:58 -0800428 // Wait until we get something that's not a no-op.
429 while (true) {
430 bool event_ready = event_queue.waitWithTimeout(kWaitForHciEventTimeout);
431 ASSERT_TRUE(event_ready || !timeout_is_error);
432 if (event_queue.empty()) {
433 // waitWithTimeout timed out
434 return;
435 }
436 handle_no_ops();
437 if (!event_queue.empty()) {
438 // There's an event in the queue that's not a no-op.
439 return;
440 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000441 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000442}
443
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000444void BluetoothAidlTest::wait_for_command_complete_event(
Jack He2d2f3c22023-03-27 03:04:52 -0700445 OpCode opCode, std::vector<uint8_t>& complete_event) {
Myles Watsone1708c82023-01-18 17:07:58 -0800446 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Myles Watsone1708c82023-01-18 17:07:58 -0800447 ASSERT_FALSE(event_queue.empty());
Jack He2d2f3c22023-03-27 03:04:52 -0700448 ASSERT_TRUE(event_queue.pop(complete_event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800449 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
450 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
Jack He2d2f3c22023-03-27 03:04:52 -0700451 std::make_shared<std::vector<uint8_t>>(complete_event))));
Myles Watson51b8bae2023-01-27 14:22:24 -0800452 ASSERT_TRUE(complete_view.IsValid());
453 ASSERT_EQ(complete_view.GetCommandOpCode(), opCode);
454 ASSERT_EQ(complete_view.GetPayload()[0],
455 static_cast<uint8_t>(::bluetooth::hci::ErrorCode::SUCCESS));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000456}
457
Jack He2d2f3c22023-03-27 03:04:52 -0700458void BluetoothAidlTest::wait_and_validate_command_complete_event(
459 ::bluetooth::hci::OpCode opCode) {
460 std::vector<uint8_t> complete_event;
461 ASSERT_NO_FATAL_FAILURE(
462 wait_for_command_complete_event(opCode, complete_event));
463}
464
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000465// Send the command to read the controller's buffer sizes.
466void BluetoothAidlTest::setBufferSizes() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800467 std::vector<uint8_t> cmd;
468 ::bluetooth::packet::BitInserter bi{cmd};
469 ::bluetooth::hci::ReadBufferSizeBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000470 hci->sendHciCommand(cmd);
471
Myles Watsone1708c82023-01-18 17:07:58 -0800472 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000473 std::vector<uint8_t> event;
474 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800475 auto complete_view = ::bluetooth::hci::ReadBufferSizeCompleteView::Create(
476 ::bluetooth::hci::CommandCompleteView::Create(
477 ::bluetooth::hci::EventView::Create(
478 ::bluetooth::hci::PacketView<true>(
479 std::make_shared<std::vector<uint8_t>>(event)))));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000480
Myles Watson51b8bae2023-01-27 14:22:24 -0800481 ASSERT_TRUE(complete_view.IsValid());
482 ASSERT_EQ(complete_view.GetStatus(), ::bluetooth::hci::ErrorCode::SUCCESS);
483 max_acl_data_packet_length = complete_view.GetAclDataPacketLength();
484 max_sco_data_packet_length = complete_view.GetSynchronousDataPacketLength();
485 max_acl_data_packets = complete_view.GetTotalNumAclDataPackets();
486 max_sco_data_packets = complete_view.GetTotalNumSynchronousDataPackets();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000487
488 ALOGD("%s: ACL max %d num %d SCO max %d num %d", __func__,
489 static_cast<int>(max_acl_data_packet_length),
490 static_cast<int>(max_acl_data_packets),
491 static_cast<int>(max_sco_data_packet_length),
492 static_cast<int>(max_sco_data_packets));
493}
494
Myles Watsone1708c82023-01-18 17:07:58 -0800495// Enable flow control packets for SCO
496void BluetoothAidlTest::setSynchronousFlowControlEnable() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800497 std::vector<uint8_t> cmd;
498 ::bluetooth::packet::BitInserter bi{cmd};
499 ::bluetooth::hci::WriteSynchronousFlowControlEnableBuilder::Create(
500 ::bluetooth::hci::Enable::ENABLED)
501 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800502 hci->sendHciCommand(cmd);
503
Jack He2d2f3c22023-03-27 03:04:52 -0700504 wait_and_validate_command_complete_event(
Myles Watson51b8bae2023-01-27 14:22:24 -0800505 ::bluetooth::hci::OpCode::WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE);
Myles Watsone1708c82023-01-18 17:07:58 -0800506}
507
508// Send an HCI command (in Loopback mode) and check the response.
509void BluetoothAidlTest::sendAndCheckHci(int num_packets) {
Jack Hedbceaca2023-03-27 18:06:38 -0700510 ThroughputLogger logger{__func__};
511 size_t command_size = 0;
Myles Watson51b8bae2023-01-27 14:22:24 -0800512 char new_name[] = "John Jacob Jingleheimer Schmidt ___________________";
513 size_t new_name_length = strlen(new_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800514 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800515 // The name to set is new_name
Jack Hedbceaca2023-03-27 18:06:38 -0700516 std::array<uint8_t, 248> name_array{};
Myles Watsone1708c82023-01-18 17:07:58 -0800517 for (size_t i = 0; i < new_name_length; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800518 name_array[i] = new_name[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800519 }
520 // And the packet number
Myles Watson51b8bae2023-01-27 14:22:24 -0800521 char number[11] = "0000000000";
522 snprintf(number, sizeof(number), "%010d", static_cast<int>(n));
523 for (size_t i = new_name_length; i < new_name_length + sizeof(number) - 1;
524 i++) {
525 name_array[new_name_length + i] = number[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800526 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800527 std::vector<uint8_t> write_name;
528 ::bluetooth::packet::BitInserter bi{write_name};
529 ::bluetooth::hci::WriteLocalNameBuilder::Create(name_array)->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800530 hci->sendHciCommand(write_name);
531
532 // Check the loopback of the HCI packet
533 ASSERT_NO_FATAL_FAILURE(wait_for_event());
534
535 std::vector<uint8_t> event;
536 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800537 auto event_view = ::bluetooth::hci::LoopbackCommandView::Create(
538 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
539 std::make_shared<std::vector<uint8_t>>(event))));
540 ASSERT_TRUE(event_view.IsValid());
541 std::vector<uint8_t> looped_back_command{event_view.GetPayload().begin(),
542 event_view.GetPayload().end()};
543 ASSERT_EQ(looped_back_command, write_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800544
545 if (n == num_packets - 1) {
546 command_size = write_name.size();
547 }
Myles Watsone1708c82023-01-18 17:07:58 -0800548 }
549 logger.setTotalBytes(command_size * num_packets * 2);
550}
551
552// Send a SCO data packet (in Loopback mode) and check the response.
553void BluetoothAidlTest::sendAndCheckSco(int num_packets, size_t size,
554 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700555 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800556 for (int n = 0; n < num_packets; n++) {
557 // Send a SCO packet
558 std::vector<uint8_t> sco_packet;
Myles Watson51b8bae2023-01-27 14:22:24 -0800559 std::vector<uint8_t> payload;
Myles Watsone1708c82023-01-18 17:07:58 -0800560 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800561 payload.push_back(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800562 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800563 ::bluetooth::packet::BitInserter bi{sco_packet};
564 ::bluetooth::hci::ScoBuilder::Create(
565 handle, ::bluetooth::hci::PacketStatusFlag::CORRECTLY_RECEIVED, payload)
566 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800567 hci->sendScoData(sco_packet);
568
569 // Check the loopback of the SCO packet
570 std::vector<uint8_t> sco_loopback;
571 ASSERT_TRUE(
572 sco_queue.tryPopWithTimeout(sco_loopback, kWaitForScoDataTimeout));
573
xiaoshun.xu4e85c092023-07-11 17:30:04 +0800574 if (sco_loopback.size() < size) {
575 // The packets may have been split for USB. Reassemble before checking.
576 reassemble_sco_loopback_pkt(sco_loopback, size);
577 }
578
Myles Watson51b8bae2023-01-27 14:22:24 -0800579 ASSERT_EQ(sco_packet, sco_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800580 }
581 logger.setTotalBytes(num_packets * size * 2);
582}
583
584// Send an ACL data packet (in Loopback mode) and check the response.
585void BluetoothAidlTest::sendAndCheckAcl(int num_packets, size_t size,
586 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700587 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800588 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800589 // Send an ACL packet with counting data
590 auto payload = std::make_unique<::bluetooth::packet::RawBuilder>();
Myles Watsone1708c82023-01-18 17:07:58 -0800591 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800592 payload->AddOctets1(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800593 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800594 std::vector<uint8_t> acl_packet;
595 ::bluetooth::packet::BitInserter bi{acl_packet};
596 ::bluetooth::hci::AclBuilder::Create(
597 handle,
598 ::bluetooth::hci::PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE,
599 ::bluetooth::hci::BroadcastFlag::POINT_TO_POINT, std::move(payload))
600 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800601 hci->sendAclData(acl_packet);
602
603 std::vector<uint8_t> acl_loopback;
604 // Check the loopback of the ACL packet
605 ASSERT_TRUE(
606 acl_queue.tryPopWithTimeout(acl_loopback, kWaitForAclDataTimeout));
607
Myles Watson51b8bae2023-01-27 14:22:24 -0800608 ASSERT_EQ(acl_packet, acl_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800609 }
610 logger.setTotalBytes(num_packets * size * 2);
611}
612
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000613// Return the number of completed packets reported by the controller.
614int BluetoothAidlTest::wait_for_completed_packets_event(uint16_t handle) {
615 int packets_processed = 0;
Myles Watson65b47f52023-01-26 12:59:06 -0800616 while (true) {
617 // There should be at least one event.
618 wait_for_event(packets_processed == 0);
619 if (event_queue.empty()) {
620 if (packets_processed == 0) {
621 ALOGW("%s: waitForBluetoothCallback timed out.", __func__);
622 }
623 return packets_processed;
624 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000625 std::vector<uint8_t> event;
626 EXPECT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800627 auto event_view = ::bluetooth::hci::NumberOfCompletedPacketsView::Create(
628 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
629 std::make_shared<std::vector<uint8_t>>(event))));
630 if (!event_view.IsValid()) {
631 ADD_FAILURE();
632 return packets_processed;
633 }
634 auto completed_packets = event_view.GetCompletedPackets();
Jack Hedbceaca2023-03-27 18:06:38 -0700635 for (const auto& entry : completed_packets) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800636 EXPECT_EQ(handle, entry.connection_handle_);
637 packets_processed += entry.host_num_of_completed_packets_;
638 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000639 }
640 return packets_processed;
641}
642
Myles Watsone1708c82023-01-18 17:07:58 -0800643// Send local loopback command and initialize SCO and ACL handles.
644void BluetoothAidlTest::enterLoopbackMode() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800645 std::vector<uint8_t> cmd;
646 ::bluetooth::packet::BitInserter bi{cmd};
647 ::bluetooth::hci::WriteLoopbackModeBuilder::Create(
648 bluetooth::hci::LoopbackMode::ENABLE_LOCAL)
649 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800650 hci->sendHciCommand(cmd);
651
652 // Receive connection complete events with data channels
653 int connection_event_count = 0;
654 bool command_complete_received = false;
655 while (true) {
656 wait_for_event(false);
657 if (event_queue.empty()) {
658 // Fail if there was no event received or no connections completed.
659 ASSERT_TRUE(command_complete_received);
660 ASSERT_LT(0, connection_event_count);
661 return;
662 }
663 std::vector<uint8_t> event;
664 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800665 auto event_view =
666 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
667 std::make_shared<std::vector<uint8_t>>(event)));
668 ASSERT_TRUE(event_view.IsValid());
Myles Watsone1708c82023-01-18 17:07:58 -0800669
Myles Watson51b8bae2023-01-27 14:22:24 -0800670 if (event_view.GetEventCode() ==
671 ::bluetooth::hci::EventCode::CONNECTION_COMPLETE) {
672 auto complete_view =
673 ::bluetooth::hci::ConnectionCompleteView::Create(event_view);
674 ASSERT_TRUE(complete_view.IsValid());
675 switch (complete_view.GetLinkType()) {
676 case ::bluetooth::hci::LinkType::ACL:
677 acl_connection_handles.push_back(complete_view.GetConnectionHandle());
678 break;
679 case ::bluetooth::hci::LinkType::SCO:
680 sco_connection_handles.push_back(complete_view.GetConnectionHandle());
681 break;
682 default:
683 ASSERT_EQ(complete_view.GetLinkType(),
684 ::bluetooth::hci::LinkType::ACL);
Myles Watsone1708c82023-01-18 17:07:58 -0800685 }
Myles Watsone1708c82023-01-18 17:07:58 -0800686 connection_event_count++;
687 } else {
Myles Watson51b8bae2023-01-27 14:22:24 -0800688 auto command_complete_view =
689 ::bluetooth::hci::WriteLoopbackModeCompleteView::Create(
690 ::bluetooth::hci::CommandCompleteView::Create(event_view));
691 ASSERT_TRUE(command_complete_view.IsValid());
692 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
693 command_complete_view.GetStatus());
Myles Watsone1708c82023-01-18 17:07:58 -0800694 command_complete_received = true;
695 }
696 }
697}
698
Jack He2d2f3c22023-03-27 03:04:52 -0700699void BluetoothAidlTest::send_and_wait_for_cmd_complete(
700 std::unique_ptr<CommandBuilder> cmd, std::vector<uint8_t>& cmd_complete) {
701 std::vector<uint8_t> cmd_bytes = cmd->SerializeToBytes();
702 hci->sendHciCommand(cmd_bytes);
703
704 auto view = CommandView::Create(
705 PacketView<true>(std::make_shared<std::vector<uint8_t>>(cmd_bytes)));
706 ASSERT_TRUE(view.IsValid());
707 ALOGI("Waiting for %s[0x%x]", OpCodeText(view.GetOpCode()).c_str(),
708 static_cast<int>(view.GetOpCode()));
709 ASSERT_NO_FATAL_FAILURE(
710 wait_for_command_complete_event(view.GetOpCode(), cmd_complete));
711}
712
xiaoshun.xu4e85c092023-07-11 17:30:04 +0800713// Handle the loopback packet.
714void BluetoothAidlTest::reassemble_sco_loopback_pkt(std::vector<uint8_t>& scoPackets,
715 size_t size) {
716 std::vector<uint8_t> sco_packet_whole;
717 sco_packet_whole.assign(scoPackets.begin(), scoPackets.end());
718 while (size + 3 > sco_packet_whole.size()) {
719 std::vector<uint8_t> sco_packets;
720 ASSERT_TRUE(
721 sco_queue.tryPopWithTimeout(sco_packets, kWaitForScoDataTimeout));
722 sco_packet_whole.insert(sco_packet_whole.end(), sco_packets.begin() + 3,
723 sco_packets.end());
724 }
725 scoPackets.assign(sco_packet_whole.begin(), sco_packet_whole.end());
726 scoPackets[2] = size;
727}
728
Myles Watsone1708c82023-01-18 17:07:58 -0800729// Empty test: Initialize()/Close() are called in SetUp()/TearDown().
730TEST_P(BluetoothAidlTest, InitializeAndClose) {}
731
732// Send an HCI Reset with sendHciCommand and wait for a command complete event.
Myles Watson65b47f52023-01-26 12:59:06 -0800733TEST_P(BluetoothAidlTest, HciReset) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800734 std::vector<uint8_t> reset;
735 ::bluetooth::packet::BitInserter bi{reset};
736 ::bluetooth::hci::ResetBuilder::Create()->Serialize(bi);
Myles Watson65b47f52023-01-26 12:59:06 -0800737 hci->sendHciCommand(reset);
738
Jack He2d2f3c22023-03-27 03:04:52 -0700739 wait_and_validate_command_complete_event(::bluetooth::hci::OpCode::RESET);
Myles Watson65b47f52023-01-26 12:59:06 -0800740}
Myles Watsone1708c82023-01-18 17:07:58 -0800741
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000742// Read and check the HCI version of the controller.
743TEST_P(BluetoothAidlTest, HciVersionTest) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800744 std::vector<uint8_t> cmd;
745 ::bluetooth::packet::BitInserter bi{cmd};
746 ::bluetooth::hci::ReadLocalVersionInformationBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000747 hci->sendHciCommand(cmd);
748
Myles Watsone1708c82023-01-18 17:07:58 -0800749 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000750
751 std::vector<uint8_t> event;
752 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800753 auto complete_view =
754 ::bluetooth::hci::ReadLocalVersionInformationCompleteView::Create(
755 ::bluetooth::hci::CommandCompleteView::Create(
756 ::bluetooth::hci::EventView::Create(
757 ::bluetooth::hci::PacketView<true>(
758 std::make_shared<std::vector<uint8_t>>(event)))));
759 ASSERT_TRUE(complete_view.IsValid());
760 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, complete_view.GetStatus());
761 auto version = complete_view.GetLocalVersionInformation();
762 ASSERT_LE(::bluetooth::hci::HciVersion::V_3_0, version.hci_version_);
763 ASSERT_LE(::bluetooth::hci::LmpVersion::V_3_0, version.lmp_version_);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000764}
765
766// Send an unknown HCI command and wait for the error message.
767TEST_P(BluetoothAidlTest, HciUnknownCommand) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800768 std::vector<uint8_t> cmd;
769 ::bluetooth::packet::BitInserter bi{cmd};
770 ::bluetooth::hci::CommandBuilder::Create(
771 static_cast<::bluetooth::hci::OpCode>(0x3cff),
772 std::make_unique<::bluetooth::packet::RawBuilder>())
773 ->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000774 hci->sendHciCommand(cmd);
775
Myles Watsone1708c82023-01-18 17:07:58 -0800776 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000777
778 std::vector<uint8_t> event;
779 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800780 auto event_view =
781 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
782 std::make_shared<std::vector<uint8_t>>(event)));
783 ASSERT_TRUE(event_view.IsValid());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000784
Myles Watson51b8bae2023-01-27 14:22:24 -0800785 switch (event_view.GetEventCode()) {
786 case ::bluetooth::hci::EventCode::COMMAND_COMPLETE: {
787 auto command_complete =
788 ::bluetooth::hci::CommandCompleteView::Create(event_view);
789 ASSERT_TRUE(command_complete.IsValid());
790 ASSERT_EQ(command_complete.GetPayload()[0],
791 static_cast<uint8_t>(
792 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND));
793 } break;
794 case ::bluetooth::hci::EventCode::COMMAND_STATUS: {
795 auto command_status =
796 ::bluetooth::hci::CommandStatusView::Create(event_view);
797 ASSERT_TRUE(command_status.IsValid());
798 ASSERT_EQ(command_status.GetStatus(),
799 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND);
800 } break;
801 default:
802 ADD_FAILURE();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000803 }
804}
805
Myles Watsone1708c82023-01-18 17:07:58 -0800806// Enter loopback mode, but don't send any packets.
Myles Watson65b47f52023-01-26 12:59:06 -0800807TEST_P(BluetoothAidlTest, WriteLoopbackMode) { enterLoopbackMode(); }
Myles Watsone1708c82023-01-18 17:07:58 -0800808
809// Enter loopback mode and send a single command.
810TEST_P(BluetoothAidlTest, LoopbackModeSingleCommand) {
Myles Watsone1708c82023-01-18 17:07:58 -0800811 setBufferSizes();
812
813 enterLoopbackMode();
814
815 sendAndCheckHci(1);
816}
817
818// Enter loopback mode and send a single SCO packet.
819TEST_P(BluetoothAidlTest, LoopbackModeSingleSco) {
Myles Watsone1708c82023-01-18 17:07:58 -0800820 setBufferSizes();
821 setSynchronousFlowControlEnable();
822
823 enterLoopbackMode();
824
825 if (!sco_connection_handles.empty()) {
826 ASSERT_LT(0, max_sco_data_packet_length);
827 sendAndCheckSco(1, max_sco_data_packet_length, sco_connection_handles[0]);
828 int sco_packets_sent = 1;
829 int completed_packets =
830 wait_for_completed_packets_event(sco_connection_handles[0]);
831 if (sco_packets_sent != completed_packets) {
832 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
833 sco_packets_sent, completed_packets);
834 }
835 }
836}
837
838// Enter loopback mode and send a single ACL packet.
839TEST_P(BluetoothAidlTest, LoopbackModeSingleAcl) {
Myles Watsone1708c82023-01-18 17:07:58 -0800840 setBufferSizes();
841
842 enterLoopbackMode();
843
844 if (!acl_connection_handles.empty()) {
845 ASSERT_LT(0, max_acl_data_packet_length);
846 sendAndCheckAcl(1, max_acl_data_packet_length - 1,
847 acl_connection_handles[0]);
848 int acl_packets_sent = 1;
849 int completed_packets =
850 wait_for_completed_packets_event(acl_connection_handles[0]);
851 if (acl_packets_sent != completed_packets) {
852 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
853 acl_packets_sent, completed_packets);
854 }
855 }
856 ASSERT_GE(acl_cb_count, 1);
857}
858
859// Enter loopback mode and send command packets for bandwidth measurements.
860TEST_P(BluetoothAidlTest, LoopbackModeCommandBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800861 setBufferSizes();
862
863 enterLoopbackMode();
864
865 sendAndCheckHci(kNumHciCommandsBandwidth);
866}
867
868// Enter loopback mode and send SCO packets for bandwidth measurements.
869TEST_P(BluetoothAidlTest, LoopbackModeScoBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800870 setBufferSizes();
871 setSynchronousFlowControlEnable();
872
873 enterLoopbackMode();
874
875 if (!sco_connection_handles.empty()) {
876 ASSERT_LT(0, max_sco_data_packet_length);
877 sendAndCheckSco(kNumScoPacketsBandwidth, max_sco_data_packet_length,
878 sco_connection_handles[0]);
879 int sco_packets_sent = kNumScoPacketsBandwidth;
880 int completed_packets =
881 wait_for_completed_packets_event(sco_connection_handles[0]);
882 if (sco_packets_sent != completed_packets) {
883 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
884 sco_packets_sent, completed_packets);
885 }
886 }
887}
888
889// Enter loopback mode and send packets for ACL bandwidth measurements.
890TEST_P(BluetoothAidlTest, LoopbackModeAclBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800891 setBufferSizes();
892
893 enterLoopbackMode();
894
895 if (!acl_connection_handles.empty()) {
896 ASSERT_LT(0, max_acl_data_packet_length);
897 sendAndCheckAcl(kNumAclPacketsBandwidth, max_acl_data_packet_length - 1,
898 acl_connection_handles[0]);
899 int acl_packets_sent = kNumAclPacketsBandwidth;
900 int completed_packets =
901 wait_for_completed_packets_event(acl_connection_handles[0]);
902 if (acl_packets_sent != completed_packets) {
903 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
904 acl_packets_sent, completed_packets);
905 }
906 }
907}
908
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000909// Set all bits in the event mask
910TEST_P(BluetoothAidlTest, SetEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800911 std::vector<uint8_t> cmd;
912 ::bluetooth::packet::BitInserter bi{cmd};
913 uint64_t full_mask = UINT64_MAX;
914 ::bluetooth::hci::SetEventMaskBuilder::Create(full_mask)->Serialize(bi);
915 hci->sendHciCommand(cmd);
Jack He2d2f3c22023-03-27 03:04:52 -0700916 wait_and_validate_command_complete_event(
917 ::bluetooth::hci::OpCode::SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000918}
919
920// Set all bits in the LE event mask
921TEST_P(BluetoothAidlTest, SetLeEventMask) {
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::LeSetEventMaskBuilder::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::LE_SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000929}
930
Myles Watson0daa1642023-01-26 15:58:28 -0800931// Call initialize twice, second one should fail.
932TEST_P(BluetoothAidlTest, CallInitializeTwice) {
933 class SecondCb
934 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
935 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700936 ndk::ScopedAStatus initializationComplete(Status status) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800937 EXPECT_EQ(status, Status::ALREADY_INITIALIZED);
938 init_promise.set_value();
939 return ScopedAStatus::ok();
940 };
941
Jack Hedbceaca2023-03-27 18:06:38 -0700942 ndk::ScopedAStatus hciEventReceived(
943 const std::vector<uint8_t>& /*event*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800944 ADD_FAILURE();
945 return ScopedAStatus::ok();
946 };
947
Jack Hedbceaca2023-03-27 18:06:38 -0700948 ndk::ScopedAStatus aclDataReceived(
949 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800950 ADD_FAILURE();
951 return ScopedAStatus::ok();
952 };
953
Jack Hedbceaca2023-03-27 18:06:38 -0700954 ndk::ScopedAStatus scoDataReceived(
955 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800956 ADD_FAILURE();
957 return ScopedAStatus::ok();
958 };
959
Jack Hedbceaca2023-03-27 18:06:38 -0700960 ndk::ScopedAStatus isoDataReceived(
961 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800962 ADD_FAILURE();
963 return ScopedAStatus::ok();
964 };
965 std::promise<void> init_promise;
966 };
967
968 std::shared_ptr<SecondCb> second_cb = ndk::SharedRefBase::make<SecondCb>();
969 ASSERT_NE(second_cb, nullptr);
970
971 auto future = second_cb->init_promise.get_future();
972 ASSERT_TRUE(hci->initialize(second_cb).isOk());
973 auto status = future.wait_for(std::chrono::seconds(1));
974 ASSERT_EQ(status, std::future_status::ready);
975}
976
Myles Watson5bfb3a72023-05-10 14:06:32 -0700977TEST_P(BluetoothAidlTest, Vsr_Bluetooth5Requirements) {
Jack He2d2f3c22023-03-27 03:04:52 -0700978 std::vector<uint8_t> version_event;
979 send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
980 version_event);
981 auto version_view = ReadLocalVersionInformationCompleteView::Create(
982 CommandCompleteView::Create(EventView::Create(PacketView<true>(
983 std::make_shared<std::vector<uint8_t>>(version_event)))));
984 ASSERT_TRUE(version_view.IsValid());
985 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
986 auto version = version_view.GetLocalVersionInformation();
987 if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) {
988 // This test does not apply to controllers below 5.0
989 return;
990 };
991 // When HCI version is 5.0, LMP version must also be at least 5.0
992 ASSERT_GE(static_cast<int>(version.lmp_version_),
993 static_cast<int>(version.hci_version_));
994
995 std::vector<uint8_t> le_features_event;
996 send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(),
997 le_features_event);
998 auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create(
999 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1000 std::make_shared<std::vector<uint8_t>>(le_features_event)))));
1001 ASSERT_TRUE(le_features_view.IsValid());
1002 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus());
1003 auto le_features = le_features_view.GetLeFeatures();
1004 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LL_PRIVACY));
1005 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LE_2M_PHY));
1006 ASSERT_TRUE(le_features &
1007 static_cast<uint64_t>(LLFeaturesBits::LE_CODED_PHY));
1008 ASSERT_TRUE(le_features &
1009 static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING));
1010
1011 std::vector<uint8_t> num_adv_set_event;
1012 send_and_wait_for_cmd_complete(
1013 LeReadNumberOfSupportedAdvertisingSetsBuilder::Create(),
1014 num_adv_set_event);
1015 auto num_adv_set_view =
1016 LeReadNumberOfSupportedAdvertisingSetsCompleteView::Create(
1017 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1018 std::make_shared<std::vector<uint8_t>>(num_adv_set_event)))));
1019 ASSERT_TRUE(num_adv_set_view.IsValid());
1020 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, num_adv_set_view.GetStatus());
1021 auto num_adv_set = num_adv_set_view.GetNumberSupportedAdvertisingSets();
Treehugger Robot5e0277f2023-05-12 02:44:22 +00001022
1023 if (isTv() && get_vsr_api_level() == __ANDROID_API_U__) {
1024 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5FoTv);
1025 } else {
1026 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5);
1027 }
Jack He2d2f3c22023-03-27 03:04:52 -07001028
1029 std::vector<uint8_t> num_resolving_list_event;
1030 send_and_wait_for_cmd_complete(LeReadResolvingListSizeBuilder::Create(),
1031 num_resolving_list_event);
1032 auto num_resolving_list_view = LeReadResolvingListSizeCompleteView::Create(
1033 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1034 std::make_shared<std::vector<uint8_t>>(num_resolving_list_event)))));
1035 ASSERT_TRUE(num_resolving_list_view.IsValid());
1036 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
1037 num_resolving_list_view.GetStatus());
1038 auto num_resolving_list = num_resolving_list_view.GetResolvingListSize();
1039 ASSERT_GE(num_resolving_list, kMinLeResolvingListForBt5);
1040}
1041
Dylan Tian8a6e09c2022-08-16 07:29:28 +00001042GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothAidlTest);
1043INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothAidlTest,
1044 testing::ValuesIn(android::getAidlHalInstanceNames(
1045 IBluetoothHci::descriptor)),
1046 android::PrintInstanceNameToString);
1047
1048int main(int argc, char** argv) {
1049 ABinderProcess_startThreadPool();
1050 ::testing::InitGoogleTest(&argc, argv);
1051 int status = RUN_ALL_TESTS();
1052 ALOGI("Test result = %d", status);
1053 return status;
1054}