blob: e5222a764d4c5f4420b7d182ad4ff34360cc2701 [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);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000225
226 // A simple test implementation of BluetoothHciCallbacks.
227 class BluetoothHciCallbacks
228 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
229 BluetoothAidlTest& parent_;
230
231 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700232 explicit BluetoothHciCallbacks(BluetoothAidlTest& parent)
233 : parent_(parent){};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000234
Jack Hedbceaca2023-03-27 18:06:38 -0700235 ~BluetoothHciCallbacks() override = default;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000236
Jack Hedbceaca2023-03-27 18:06:38 -0700237 ndk::ScopedAStatus initializationComplete(Status status) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000238 parent_.initialized_promise.set_value(status == Status::SUCCESS);
239 ALOGV("%s (status = %d)", __func__, static_cast<int>(status));
240 return ScopedAStatus::ok();
241 };
242
Jack Hedbceaca2023-03-27 18:06:38 -0700243 ndk::ScopedAStatus hciEventReceived(
244 const std::vector<uint8_t>& event) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000245 parent_.event_cb_count++;
246 parent_.event_queue.push(event);
Jack He2d2f3c22023-03-27 03:04:52 -0700247 ALOGI("Event received (length = %d)", static_cast<int>(event.size()));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000248 return ScopedAStatus::ok();
249 };
250
Jack Hedbceaca2023-03-27 18:06:38 -0700251 ndk::ScopedAStatus aclDataReceived(
252 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000253 parent_.acl_cb_count++;
254 parent_.acl_queue.push(data);
255 return ScopedAStatus::ok();
256 };
257
Jack Hedbceaca2023-03-27 18:06:38 -0700258 ndk::ScopedAStatus scoDataReceived(
259 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000260 parent_.sco_cb_count++;
261 parent_.sco_queue.push(data);
262 return ScopedAStatus::ok();
263 };
264
Jack Hedbceaca2023-03-27 18:06:38 -0700265 ndk::ScopedAStatus isoDataReceived(
266 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000267 parent_.iso_cb_count++;
268 parent_.iso_queue.push(data);
269 return ScopedAStatus::ok();
270 };
271 };
272
273 template <class T>
274 class WaitQueue {
275 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700276 WaitQueue() = default;
277 ;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000278
279 virtual ~WaitQueue() = default;
280
281 bool empty() const {
282 std::lock_guard<std::mutex> lock(m_);
283 return q_.empty();
284 };
285
286 size_t size() const {
287 std::lock_guard<std::mutex> lock(m_);
288 return q_.size();
289 };
290
291 void push(const T& v) {
292 std::lock_guard<std::mutex> lock(m_);
293 q_.push(v);
294 ready_.notify_one();
295 };
296
297 bool pop(T& v) {
298 std::lock_guard<std::mutex> lock(m_);
299 if (q_.empty()) {
300 return false;
301 }
302 v = std::move(q_.front());
303 q_.pop();
304 return true;
305 };
306
Myles Watson51b8bae2023-01-27 14:22:24 -0800307 void pop() {
308 std::lock_guard<std::mutex> lock(m_);
309 if (q_.empty()) {
310 return;
311 }
312 q_.pop();
313 };
314
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000315 bool front(T& v) {
316 std::lock_guard<std::mutex> lock(m_);
317 if (q_.empty()) {
318 return false;
319 }
320 v = q_.front();
321 return true;
322 };
323
324 void wait() {
325 std::unique_lock<std::mutex> lock(m_);
326 while (q_.empty()) {
327 ready_.wait(lock);
328 }
329 };
330
331 bool waitWithTimeout(std::chrono::milliseconds timeout) {
332 std::unique_lock<std::mutex> lock(m_);
333 while (q_.empty()) {
334 if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
335 return false;
336 }
337 }
338 return true;
339 };
340
341 bool tryPopWithTimeout(T& v, std::chrono::milliseconds timeout) {
342 std::unique_lock<std::mutex> lock(m_);
343 while (q_.empty()) {
344 if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
345 return false;
346 }
347 }
348 v = std::move(q_.front());
349 q_.pop();
350 return true;
351 };
352
353 private:
354 mutable std::mutex m_;
355 std::queue<T> q_;
356 std::condition_variable_any ready_;
357 };
358
359 std::shared_ptr<IBluetoothHci> hci;
360 std::shared_ptr<BluetoothHciCallbacks> hci_cb;
Jack Hedbceaca2023-03-27 18:06:38 -0700361 AIBinder_DeathRecipient* bluetooth_hci_death_recipient{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000362 WaitQueue<std::vector<uint8_t>> event_queue;
363 WaitQueue<std::vector<uint8_t>> acl_queue;
364 WaitQueue<std::vector<uint8_t>> sco_queue;
365 WaitQueue<std::vector<uint8_t>> iso_queue;
366
367 std::promise<bool> initialized_promise;
Jack Hedbceaca2023-03-27 18:06:38 -0700368 int event_cb_count{};
369 int sco_cb_count{};
370 int acl_cb_count{};
371 int iso_cb_count{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000372
Jack Hedbceaca2023-03-27 18:06:38 -0700373 int max_acl_data_packet_length{};
374 int max_sco_data_packet_length{};
375 int max_acl_data_packets{};
376 int max_sco_data_packets{};
Myles Watsone1708c82023-01-18 17:07:58 -0800377
378 std::vector<uint16_t> sco_connection_handles;
379 std::vector<uint16_t> acl_connection_handles;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000380};
381
382// Discard NO-OPs from the event queue.
383void BluetoothAidlTest::handle_no_ops() {
384 while (!event_queue.empty()) {
385 std::vector<uint8_t> event;
386 event_queue.front(event);
Myles Watson51b8bae2023-01-27 14:22:24 -0800387 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
388 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
389 std::make_shared<std::vector<uint8_t>>(event))));
390 auto status_view = ::bluetooth::hci::CommandCompleteView::Create(
391 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
392 std::make_shared<std::vector<uint8_t>>(event))));
393 bool is_complete_no_op =
394 complete_view.IsValid() &&
395 complete_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
396 bool is_status_no_op =
397 status_view.IsValid() &&
398 status_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
399 if (is_complete_no_op || is_status_no_op) {
400 event_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000401 } else {
402 break;
403 }
404 }
Myles Watsone1708c82023-01-18 17:07:58 -0800405}
406
407// Discard Qualcomm ACL debugging
408void BluetoothAidlTest::discard_qca_debugging() {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000409 while (!acl_queue.empty()) {
410 std::vector<uint8_t> acl_packet;
411 acl_queue.front(acl_packet);
Myles Watson51b8bae2023-01-27 14:22:24 -0800412 auto acl_view =
413 ::bluetooth::hci::AclView::Create(::bluetooth::hci::PacketView<true>(
414 std::make_shared<std::vector<uint8_t>>(acl_packet)));
415 EXPECT_TRUE(acl_view.IsValid());
416 if (acl_view.GetHandle() == kAclHandleQcaDebugMessage) {
417 acl_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000418 } else {
419 break;
420 }
421 }
422}
423
424// Receive an event, discarding NO-OPs.
425void BluetoothAidlTest::wait_for_event(bool timeout_is_error = true) {
Myles Watsone1708c82023-01-18 17:07:58 -0800426 // Wait until we get something that's not a no-op.
427 while (true) {
428 bool event_ready = event_queue.waitWithTimeout(kWaitForHciEventTimeout);
429 ASSERT_TRUE(event_ready || !timeout_is_error);
430 if (event_queue.empty()) {
431 // waitWithTimeout timed out
432 return;
433 }
434 handle_no_ops();
435 if (!event_queue.empty()) {
436 // There's an event in the queue that's not a no-op.
437 return;
438 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000439 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000440}
441
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000442void BluetoothAidlTest::wait_for_command_complete_event(
Jack He2d2f3c22023-03-27 03:04:52 -0700443 OpCode opCode, std::vector<uint8_t>& complete_event) {
Myles Watsone1708c82023-01-18 17:07:58 -0800444 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Myles Watsone1708c82023-01-18 17:07:58 -0800445 ASSERT_FALSE(event_queue.empty());
Jack He2d2f3c22023-03-27 03:04:52 -0700446 ASSERT_TRUE(event_queue.pop(complete_event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800447 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
448 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
Jack He2d2f3c22023-03-27 03:04:52 -0700449 std::make_shared<std::vector<uint8_t>>(complete_event))));
Myles Watson51b8bae2023-01-27 14:22:24 -0800450 ASSERT_TRUE(complete_view.IsValid());
451 ASSERT_EQ(complete_view.GetCommandOpCode(), opCode);
452 ASSERT_EQ(complete_view.GetPayload()[0],
453 static_cast<uint8_t>(::bluetooth::hci::ErrorCode::SUCCESS));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000454}
455
Jack He2d2f3c22023-03-27 03:04:52 -0700456void BluetoothAidlTest::wait_and_validate_command_complete_event(
457 ::bluetooth::hci::OpCode opCode) {
458 std::vector<uint8_t> complete_event;
459 ASSERT_NO_FATAL_FAILURE(
460 wait_for_command_complete_event(opCode, complete_event));
461}
462
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000463// Send the command to read the controller's buffer sizes.
464void BluetoothAidlTest::setBufferSizes() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800465 std::vector<uint8_t> cmd;
466 ::bluetooth::packet::BitInserter bi{cmd};
467 ::bluetooth::hci::ReadBufferSizeBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000468 hci->sendHciCommand(cmd);
469
Myles Watsone1708c82023-01-18 17:07:58 -0800470 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000471 std::vector<uint8_t> event;
472 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800473 auto complete_view = ::bluetooth::hci::ReadBufferSizeCompleteView::Create(
474 ::bluetooth::hci::CommandCompleteView::Create(
475 ::bluetooth::hci::EventView::Create(
476 ::bluetooth::hci::PacketView<true>(
477 std::make_shared<std::vector<uint8_t>>(event)))));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000478
Myles Watson51b8bae2023-01-27 14:22:24 -0800479 ASSERT_TRUE(complete_view.IsValid());
480 ASSERT_EQ(complete_view.GetStatus(), ::bluetooth::hci::ErrorCode::SUCCESS);
481 max_acl_data_packet_length = complete_view.GetAclDataPacketLength();
482 max_sco_data_packet_length = complete_view.GetSynchronousDataPacketLength();
483 max_acl_data_packets = complete_view.GetTotalNumAclDataPackets();
484 max_sco_data_packets = complete_view.GetTotalNumSynchronousDataPackets();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000485
486 ALOGD("%s: ACL max %d num %d SCO max %d num %d", __func__,
487 static_cast<int>(max_acl_data_packet_length),
488 static_cast<int>(max_acl_data_packets),
489 static_cast<int>(max_sco_data_packet_length),
490 static_cast<int>(max_sco_data_packets));
491}
492
Myles Watsone1708c82023-01-18 17:07:58 -0800493// Enable flow control packets for SCO
494void BluetoothAidlTest::setSynchronousFlowControlEnable() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800495 std::vector<uint8_t> cmd;
496 ::bluetooth::packet::BitInserter bi{cmd};
497 ::bluetooth::hci::WriteSynchronousFlowControlEnableBuilder::Create(
498 ::bluetooth::hci::Enable::ENABLED)
499 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800500 hci->sendHciCommand(cmd);
501
Jack He2d2f3c22023-03-27 03:04:52 -0700502 wait_and_validate_command_complete_event(
Myles Watson51b8bae2023-01-27 14:22:24 -0800503 ::bluetooth::hci::OpCode::WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE);
Myles Watsone1708c82023-01-18 17:07:58 -0800504}
505
506// Send an HCI command (in Loopback mode) and check the response.
507void BluetoothAidlTest::sendAndCheckHci(int num_packets) {
Jack Hedbceaca2023-03-27 18:06:38 -0700508 ThroughputLogger logger{__func__};
509 size_t command_size = 0;
Myles Watson51b8bae2023-01-27 14:22:24 -0800510 char new_name[] = "John Jacob Jingleheimer Schmidt ___________________";
511 size_t new_name_length = strlen(new_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800512 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800513 // The name to set is new_name
Jack Hedbceaca2023-03-27 18:06:38 -0700514 std::array<uint8_t, 248> name_array{};
Myles Watsone1708c82023-01-18 17:07:58 -0800515 for (size_t i = 0; i < new_name_length; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800516 name_array[i] = new_name[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800517 }
518 // And the packet number
Myles Watson51b8bae2023-01-27 14:22:24 -0800519 char number[11] = "0000000000";
520 snprintf(number, sizeof(number), "%010d", static_cast<int>(n));
521 for (size_t i = new_name_length; i < new_name_length + sizeof(number) - 1;
522 i++) {
523 name_array[new_name_length + i] = number[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800524 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800525 std::vector<uint8_t> write_name;
526 ::bluetooth::packet::BitInserter bi{write_name};
527 ::bluetooth::hci::WriteLocalNameBuilder::Create(name_array)->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800528 hci->sendHciCommand(write_name);
529
530 // Check the loopback of the HCI packet
531 ASSERT_NO_FATAL_FAILURE(wait_for_event());
532
533 std::vector<uint8_t> event;
534 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800535 auto event_view = ::bluetooth::hci::LoopbackCommandView::Create(
536 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
537 std::make_shared<std::vector<uint8_t>>(event))));
538 ASSERT_TRUE(event_view.IsValid());
539 std::vector<uint8_t> looped_back_command{event_view.GetPayload().begin(),
540 event_view.GetPayload().end()};
541 ASSERT_EQ(looped_back_command, write_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800542
543 if (n == num_packets - 1) {
544 command_size = write_name.size();
545 }
Myles Watsone1708c82023-01-18 17:07:58 -0800546 }
547 logger.setTotalBytes(command_size * num_packets * 2);
548}
549
550// Send a SCO data packet (in Loopback mode) and check the response.
551void BluetoothAidlTest::sendAndCheckSco(int num_packets, size_t size,
552 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700553 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800554 for (int n = 0; n < num_packets; n++) {
555 // Send a SCO packet
556 std::vector<uint8_t> sco_packet;
Myles Watson51b8bae2023-01-27 14:22:24 -0800557 std::vector<uint8_t> payload;
Myles Watsone1708c82023-01-18 17:07:58 -0800558 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800559 payload.push_back(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800560 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800561 ::bluetooth::packet::BitInserter bi{sco_packet};
562 ::bluetooth::hci::ScoBuilder::Create(
563 handle, ::bluetooth::hci::PacketStatusFlag::CORRECTLY_RECEIVED, payload)
564 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800565 hci->sendScoData(sco_packet);
566
567 // Check the loopback of the SCO packet
568 std::vector<uint8_t> sco_loopback;
569 ASSERT_TRUE(
570 sco_queue.tryPopWithTimeout(sco_loopback, kWaitForScoDataTimeout));
571
Myles Watson51b8bae2023-01-27 14:22:24 -0800572 ASSERT_EQ(sco_packet, sco_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800573 }
574 logger.setTotalBytes(num_packets * size * 2);
575}
576
577// Send an ACL data packet (in Loopback mode) and check the response.
578void BluetoothAidlTest::sendAndCheckAcl(int num_packets, size_t size,
579 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700580 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800581 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800582 // Send an ACL packet with counting data
583 auto payload = std::make_unique<::bluetooth::packet::RawBuilder>();
Myles Watsone1708c82023-01-18 17:07:58 -0800584 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800585 payload->AddOctets1(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800586 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800587 std::vector<uint8_t> acl_packet;
588 ::bluetooth::packet::BitInserter bi{acl_packet};
589 ::bluetooth::hci::AclBuilder::Create(
590 handle,
591 ::bluetooth::hci::PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE,
592 ::bluetooth::hci::BroadcastFlag::POINT_TO_POINT, std::move(payload))
593 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800594 hci->sendAclData(acl_packet);
595
596 std::vector<uint8_t> acl_loopback;
597 // Check the loopback of the ACL packet
598 ASSERT_TRUE(
599 acl_queue.tryPopWithTimeout(acl_loopback, kWaitForAclDataTimeout));
600
Myles Watson51b8bae2023-01-27 14:22:24 -0800601 ASSERT_EQ(acl_packet, acl_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800602 }
603 logger.setTotalBytes(num_packets * size * 2);
604}
605
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000606// Return the number of completed packets reported by the controller.
607int BluetoothAidlTest::wait_for_completed_packets_event(uint16_t handle) {
608 int packets_processed = 0;
Myles Watson65b47f52023-01-26 12:59:06 -0800609 while (true) {
610 // There should be at least one event.
611 wait_for_event(packets_processed == 0);
612 if (event_queue.empty()) {
613 if (packets_processed == 0) {
614 ALOGW("%s: waitForBluetoothCallback timed out.", __func__);
615 }
616 return packets_processed;
617 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000618 std::vector<uint8_t> event;
619 EXPECT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800620 auto event_view = ::bluetooth::hci::NumberOfCompletedPacketsView::Create(
621 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
622 std::make_shared<std::vector<uint8_t>>(event))));
623 if (!event_view.IsValid()) {
624 ADD_FAILURE();
625 return packets_processed;
626 }
627 auto completed_packets = event_view.GetCompletedPackets();
Jack Hedbceaca2023-03-27 18:06:38 -0700628 for (const auto& entry : completed_packets) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800629 EXPECT_EQ(handle, entry.connection_handle_);
630 packets_processed += entry.host_num_of_completed_packets_;
631 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000632 }
633 return packets_processed;
634}
635
Myles Watsone1708c82023-01-18 17:07:58 -0800636// Send local loopback command and initialize SCO and ACL handles.
637void BluetoothAidlTest::enterLoopbackMode() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800638 std::vector<uint8_t> cmd;
639 ::bluetooth::packet::BitInserter bi{cmd};
640 ::bluetooth::hci::WriteLoopbackModeBuilder::Create(
641 bluetooth::hci::LoopbackMode::ENABLE_LOCAL)
642 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800643 hci->sendHciCommand(cmd);
644
645 // Receive connection complete events with data channels
646 int connection_event_count = 0;
647 bool command_complete_received = false;
648 while (true) {
649 wait_for_event(false);
650 if (event_queue.empty()) {
651 // Fail if there was no event received or no connections completed.
652 ASSERT_TRUE(command_complete_received);
653 ASSERT_LT(0, connection_event_count);
654 return;
655 }
656 std::vector<uint8_t> event;
657 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800658 auto event_view =
659 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
660 std::make_shared<std::vector<uint8_t>>(event)));
661 ASSERT_TRUE(event_view.IsValid());
Myles Watsone1708c82023-01-18 17:07:58 -0800662
Myles Watson51b8bae2023-01-27 14:22:24 -0800663 if (event_view.GetEventCode() ==
664 ::bluetooth::hci::EventCode::CONNECTION_COMPLETE) {
665 auto complete_view =
666 ::bluetooth::hci::ConnectionCompleteView::Create(event_view);
667 ASSERT_TRUE(complete_view.IsValid());
668 switch (complete_view.GetLinkType()) {
669 case ::bluetooth::hci::LinkType::ACL:
670 acl_connection_handles.push_back(complete_view.GetConnectionHandle());
671 break;
672 case ::bluetooth::hci::LinkType::SCO:
673 sco_connection_handles.push_back(complete_view.GetConnectionHandle());
674 break;
675 default:
676 ASSERT_EQ(complete_view.GetLinkType(),
677 ::bluetooth::hci::LinkType::ACL);
Myles Watsone1708c82023-01-18 17:07:58 -0800678 }
Myles Watsone1708c82023-01-18 17:07:58 -0800679 connection_event_count++;
680 } else {
Myles Watson51b8bae2023-01-27 14:22:24 -0800681 auto command_complete_view =
682 ::bluetooth::hci::WriteLoopbackModeCompleteView::Create(
683 ::bluetooth::hci::CommandCompleteView::Create(event_view));
684 ASSERT_TRUE(command_complete_view.IsValid());
685 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
686 command_complete_view.GetStatus());
Myles Watsone1708c82023-01-18 17:07:58 -0800687 command_complete_received = true;
688 }
689 }
690}
691
Jack He2d2f3c22023-03-27 03:04:52 -0700692void BluetoothAidlTest::send_and_wait_for_cmd_complete(
693 std::unique_ptr<CommandBuilder> cmd, std::vector<uint8_t>& cmd_complete) {
694 std::vector<uint8_t> cmd_bytes = cmd->SerializeToBytes();
695 hci->sendHciCommand(cmd_bytes);
696
697 auto view = CommandView::Create(
698 PacketView<true>(std::make_shared<std::vector<uint8_t>>(cmd_bytes)));
699 ASSERT_TRUE(view.IsValid());
700 ALOGI("Waiting for %s[0x%x]", OpCodeText(view.GetOpCode()).c_str(),
701 static_cast<int>(view.GetOpCode()));
702 ASSERT_NO_FATAL_FAILURE(
703 wait_for_command_complete_event(view.GetOpCode(), cmd_complete));
704}
705
Myles Watsone1708c82023-01-18 17:07:58 -0800706// Empty test: Initialize()/Close() are called in SetUp()/TearDown().
707TEST_P(BluetoothAidlTest, InitializeAndClose) {}
708
709// Send an HCI Reset with sendHciCommand and wait for a command complete event.
Myles Watson65b47f52023-01-26 12:59:06 -0800710TEST_P(BluetoothAidlTest, HciReset) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800711 std::vector<uint8_t> reset;
712 ::bluetooth::packet::BitInserter bi{reset};
713 ::bluetooth::hci::ResetBuilder::Create()->Serialize(bi);
Myles Watson65b47f52023-01-26 12:59:06 -0800714 hci->sendHciCommand(reset);
715
Jack He2d2f3c22023-03-27 03:04:52 -0700716 wait_and_validate_command_complete_event(::bluetooth::hci::OpCode::RESET);
Myles Watson65b47f52023-01-26 12:59:06 -0800717}
Myles Watsone1708c82023-01-18 17:07:58 -0800718
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000719// Read and check the HCI version of the controller.
720TEST_P(BluetoothAidlTest, HciVersionTest) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800721 std::vector<uint8_t> cmd;
722 ::bluetooth::packet::BitInserter bi{cmd};
723 ::bluetooth::hci::ReadLocalVersionInformationBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000724 hci->sendHciCommand(cmd);
725
Myles Watsone1708c82023-01-18 17:07:58 -0800726 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000727
728 std::vector<uint8_t> event;
729 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800730 auto complete_view =
731 ::bluetooth::hci::ReadLocalVersionInformationCompleteView::Create(
732 ::bluetooth::hci::CommandCompleteView::Create(
733 ::bluetooth::hci::EventView::Create(
734 ::bluetooth::hci::PacketView<true>(
735 std::make_shared<std::vector<uint8_t>>(event)))));
736 ASSERT_TRUE(complete_view.IsValid());
737 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, complete_view.GetStatus());
738 auto version = complete_view.GetLocalVersionInformation();
739 ASSERT_LE(::bluetooth::hci::HciVersion::V_3_0, version.hci_version_);
740 ASSERT_LE(::bluetooth::hci::LmpVersion::V_3_0, version.lmp_version_);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000741}
742
743// Send an unknown HCI command and wait for the error message.
744TEST_P(BluetoothAidlTest, HciUnknownCommand) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800745 std::vector<uint8_t> cmd;
746 ::bluetooth::packet::BitInserter bi{cmd};
747 ::bluetooth::hci::CommandBuilder::Create(
748 static_cast<::bluetooth::hci::OpCode>(0x3cff),
749 std::make_unique<::bluetooth::packet::RawBuilder>())
750 ->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000751 hci->sendHciCommand(cmd);
752
Myles Watsone1708c82023-01-18 17:07:58 -0800753 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000754
755 std::vector<uint8_t> event;
756 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800757 auto event_view =
758 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
759 std::make_shared<std::vector<uint8_t>>(event)));
760 ASSERT_TRUE(event_view.IsValid());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000761
Myles Watson51b8bae2023-01-27 14:22:24 -0800762 switch (event_view.GetEventCode()) {
763 case ::bluetooth::hci::EventCode::COMMAND_COMPLETE: {
764 auto command_complete =
765 ::bluetooth::hci::CommandCompleteView::Create(event_view);
766 ASSERT_TRUE(command_complete.IsValid());
767 ASSERT_EQ(command_complete.GetPayload()[0],
768 static_cast<uint8_t>(
769 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND));
770 } break;
771 case ::bluetooth::hci::EventCode::COMMAND_STATUS: {
772 auto command_status =
773 ::bluetooth::hci::CommandStatusView::Create(event_view);
774 ASSERT_TRUE(command_status.IsValid());
775 ASSERT_EQ(command_status.GetStatus(),
776 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND);
777 } break;
778 default:
779 ADD_FAILURE();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000780 }
781}
782
Myles Watsone1708c82023-01-18 17:07:58 -0800783// Enter loopback mode, but don't send any packets.
Myles Watson65b47f52023-01-26 12:59:06 -0800784TEST_P(BluetoothAidlTest, WriteLoopbackMode) { enterLoopbackMode(); }
Myles Watsone1708c82023-01-18 17:07:58 -0800785
786// Enter loopback mode and send a single command.
787TEST_P(BluetoothAidlTest, LoopbackModeSingleCommand) {
Myles Watsone1708c82023-01-18 17:07:58 -0800788 setBufferSizes();
789
790 enterLoopbackMode();
791
792 sendAndCheckHci(1);
793}
794
795// Enter loopback mode and send a single SCO packet.
796TEST_P(BluetoothAidlTest, LoopbackModeSingleSco) {
Myles Watsone1708c82023-01-18 17:07:58 -0800797 setBufferSizes();
798 setSynchronousFlowControlEnable();
799
800 enterLoopbackMode();
801
802 if (!sco_connection_handles.empty()) {
803 ASSERT_LT(0, max_sco_data_packet_length);
804 sendAndCheckSco(1, max_sco_data_packet_length, sco_connection_handles[0]);
805 int sco_packets_sent = 1;
806 int completed_packets =
807 wait_for_completed_packets_event(sco_connection_handles[0]);
808 if (sco_packets_sent != completed_packets) {
809 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
810 sco_packets_sent, completed_packets);
811 }
812 }
813}
814
815// Enter loopback mode and send a single ACL packet.
816TEST_P(BluetoothAidlTest, LoopbackModeSingleAcl) {
Myles Watsone1708c82023-01-18 17:07:58 -0800817 setBufferSizes();
818
819 enterLoopbackMode();
820
821 if (!acl_connection_handles.empty()) {
822 ASSERT_LT(0, max_acl_data_packet_length);
823 sendAndCheckAcl(1, max_acl_data_packet_length - 1,
824 acl_connection_handles[0]);
825 int acl_packets_sent = 1;
826 int completed_packets =
827 wait_for_completed_packets_event(acl_connection_handles[0]);
828 if (acl_packets_sent != completed_packets) {
829 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
830 acl_packets_sent, completed_packets);
831 }
832 }
833 ASSERT_GE(acl_cb_count, 1);
834}
835
836// Enter loopback mode and send command packets for bandwidth measurements.
837TEST_P(BluetoothAidlTest, LoopbackModeCommandBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800838 setBufferSizes();
839
840 enterLoopbackMode();
841
842 sendAndCheckHci(kNumHciCommandsBandwidth);
843}
844
845// Enter loopback mode and send SCO packets for bandwidth measurements.
846TEST_P(BluetoothAidlTest, LoopbackModeScoBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800847 setBufferSizes();
848 setSynchronousFlowControlEnable();
849
850 enterLoopbackMode();
851
852 if (!sco_connection_handles.empty()) {
853 ASSERT_LT(0, max_sco_data_packet_length);
854 sendAndCheckSco(kNumScoPacketsBandwidth, max_sco_data_packet_length,
855 sco_connection_handles[0]);
856 int sco_packets_sent = kNumScoPacketsBandwidth;
857 int completed_packets =
858 wait_for_completed_packets_event(sco_connection_handles[0]);
859 if (sco_packets_sent != completed_packets) {
860 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
861 sco_packets_sent, completed_packets);
862 }
863 }
864}
865
866// Enter loopback mode and send packets for ACL bandwidth measurements.
867TEST_P(BluetoothAidlTest, LoopbackModeAclBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800868 setBufferSizes();
869
870 enterLoopbackMode();
871
872 if (!acl_connection_handles.empty()) {
873 ASSERT_LT(0, max_acl_data_packet_length);
874 sendAndCheckAcl(kNumAclPacketsBandwidth, max_acl_data_packet_length - 1,
875 acl_connection_handles[0]);
876 int acl_packets_sent = kNumAclPacketsBandwidth;
877 int completed_packets =
878 wait_for_completed_packets_event(acl_connection_handles[0]);
879 if (acl_packets_sent != completed_packets) {
880 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
881 acl_packets_sent, completed_packets);
882 }
883 }
884}
885
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000886// Set all bits in the event mask
887TEST_P(BluetoothAidlTest, SetEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800888 std::vector<uint8_t> cmd;
889 ::bluetooth::packet::BitInserter bi{cmd};
890 uint64_t full_mask = UINT64_MAX;
891 ::bluetooth::hci::SetEventMaskBuilder::Create(full_mask)->Serialize(bi);
892 hci->sendHciCommand(cmd);
Jack He2d2f3c22023-03-27 03:04:52 -0700893 wait_and_validate_command_complete_event(
894 ::bluetooth::hci::OpCode::SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000895}
896
897// Set all bits in the LE event mask
898TEST_P(BluetoothAidlTest, SetLeEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800899 std::vector<uint8_t> cmd;
900 ::bluetooth::packet::BitInserter bi{cmd};
901 uint64_t full_mask = UINT64_MAX;
902 ::bluetooth::hci::LeSetEventMaskBuilder::Create(full_mask)->Serialize(bi);
903 hci->sendHciCommand(cmd);
Jack He2d2f3c22023-03-27 03:04:52 -0700904 wait_and_validate_command_complete_event(
905 ::bluetooth::hci::OpCode::LE_SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000906}
907
Myles Watson0daa1642023-01-26 15:58:28 -0800908// Call initialize twice, second one should fail.
909TEST_P(BluetoothAidlTest, CallInitializeTwice) {
910 class SecondCb
911 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
912 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700913 ndk::ScopedAStatus initializationComplete(Status status) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800914 EXPECT_EQ(status, Status::ALREADY_INITIALIZED);
915 init_promise.set_value();
916 return ScopedAStatus::ok();
917 };
918
Jack Hedbceaca2023-03-27 18:06:38 -0700919 ndk::ScopedAStatus hciEventReceived(
920 const std::vector<uint8_t>& /*event*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800921 ADD_FAILURE();
922 return ScopedAStatus::ok();
923 };
924
Jack Hedbceaca2023-03-27 18:06:38 -0700925 ndk::ScopedAStatus aclDataReceived(
926 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800927 ADD_FAILURE();
928 return ScopedAStatus::ok();
929 };
930
Jack Hedbceaca2023-03-27 18:06:38 -0700931 ndk::ScopedAStatus scoDataReceived(
932 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800933 ADD_FAILURE();
934 return ScopedAStatus::ok();
935 };
936
Jack Hedbceaca2023-03-27 18:06:38 -0700937 ndk::ScopedAStatus isoDataReceived(
938 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800939 ADD_FAILURE();
940 return ScopedAStatus::ok();
941 };
942 std::promise<void> init_promise;
943 };
944
945 std::shared_ptr<SecondCb> second_cb = ndk::SharedRefBase::make<SecondCb>();
946 ASSERT_NE(second_cb, nullptr);
947
948 auto future = second_cb->init_promise.get_future();
949 ASSERT_TRUE(hci->initialize(second_cb).isOk());
950 auto status = future.wait_for(std::chrono::seconds(1));
951 ASSERT_EQ(status, std::future_status::ready);
952}
953
Myles Watson5bfb3a72023-05-10 14:06:32 -0700954TEST_P(BluetoothAidlTest, Vsr_Bluetooth5Requirements) {
Jack He2d2f3c22023-03-27 03:04:52 -0700955 std::vector<uint8_t> version_event;
956 send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
957 version_event);
958 auto version_view = ReadLocalVersionInformationCompleteView::Create(
959 CommandCompleteView::Create(EventView::Create(PacketView<true>(
960 std::make_shared<std::vector<uint8_t>>(version_event)))));
961 ASSERT_TRUE(version_view.IsValid());
962 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
963 auto version = version_view.GetLocalVersionInformation();
964 if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) {
965 // This test does not apply to controllers below 5.0
966 return;
967 };
968 // When HCI version is 5.0, LMP version must also be at least 5.0
969 ASSERT_GE(static_cast<int>(version.lmp_version_),
970 static_cast<int>(version.hci_version_));
971
972 std::vector<uint8_t> le_features_event;
973 send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(),
974 le_features_event);
975 auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create(
976 CommandCompleteView::Create(EventView::Create(PacketView<true>(
977 std::make_shared<std::vector<uint8_t>>(le_features_event)))));
978 ASSERT_TRUE(le_features_view.IsValid());
979 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus());
980 auto le_features = le_features_view.GetLeFeatures();
981 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LL_PRIVACY));
982 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LE_2M_PHY));
983 ASSERT_TRUE(le_features &
984 static_cast<uint64_t>(LLFeaturesBits::LE_CODED_PHY));
985 ASSERT_TRUE(le_features &
986 static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING));
987
988 std::vector<uint8_t> num_adv_set_event;
989 send_and_wait_for_cmd_complete(
990 LeReadNumberOfSupportedAdvertisingSetsBuilder::Create(),
991 num_adv_set_event);
992 auto num_adv_set_view =
993 LeReadNumberOfSupportedAdvertisingSetsCompleteView::Create(
994 CommandCompleteView::Create(EventView::Create(PacketView<true>(
995 std::make_shared<std::vector<uint8_t>>(num_adv_set_event)))));
996 ASSERT_TRUE(num_adv_set_view.IsValid());
997 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, num_adv_set_view.GetStatus());
998 auto num_adv_set = num_adv_set_view.GetNumberSupportedAdvertisingSets();
Treehugger Robot5e0277f2023-05-12 02:44:22 +0000999
1000 if (isTv() && get_vsr_api_level() == __ANDROID_API_U__) {
1001 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5FoTv);
1002 } else {
1003 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5);
1004 }
Jack He2d2f3c22023-03-27 03:04:52 -07001005
1006 std::vector<uint8_t> num_resolving_list_event;
1007 send_and_wait_for_cmd_complete(LeReadResolvingListSizeBuilder::Create(),
1008 num_resolving_list_event);
1009 auto num_resolving_list_view = LeReadResolvingListSizeCompleteView::Create(
1010 CommandCompleteView::Create(EventView::Create(PacketView<true>(
1011 std::make_shared<std::vector<uint8_t>>(num_resolving_list_event)))));
1012 ASSERT_TRUE(num_resolving_list_view.IsValid());
1013 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
1014 num_resolving_list_view.GetStatus());
1015 auto num_resolving_list = num_resolving_list_view.GetResolvingListSize();
1016 ASSERT_GE(num_resolving_list, kMinLeResolvingListForBt5);
1017}
1018
Dylan Tian8a6e09c2022-08-16 07:29:28 +00001019GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothAidlTest);
1020INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothAidlTest,
1021 testing::ValuesIn(android::getAidlHalInstanceNames(
1022 IBluetoothHci::descriptor)),
1023 android::PrintInstanceNameToString);
1024
1025int main(int argc, char** argv) {
1026 ABinderProcess_startThreadPool();
1027 ::testing::InitGoogleTest(&argc, argv);
1028 int status = RUN_ALL_TESTS();
1029 ALOGI("Test result = %d", status);
1030 return status;
1031}