blob: 46366ef765e9bb74097b1593e81d3a15dbf746de [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
17#include <aidl/Gtest.h>
18#include <aidl/Vintf.h>
19#include <aidl/android/hardware/bluetooth/BnBluetoothHciCallbacks.h>
20#include <aidl/android/hardware/bluetooth/IBluetoothHci.h>
21#include <aidl/android/hardware/bluetooth/IBluetoothHciCallbacks.h>
22#include <aidl/android/hardware/bluetooth/Status.h>
23#include <android/binder_auto_utils.h>
24#include <android/binder_manager.h>
25#include <android/binder_process.h>
26#include <binder/IServiceManager.h>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000027
28#include <atomic>
29#include <chrono>
30#include <condition_variable>
31#include <future>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000032#include <queue>
33#include <thread>
Jack Hedbceaca2023-03-27 18:06:38 -070034#include <utility>
Dylan Tian8a6e09c2022-08-16 07:29:28 +000035#include <vector>
36
Myles Watson51b8bae2023-01-27 14:22:24 -080037// TODO: Remove custom logging defines from PDL packets.
38#undef LOG_INFO
39#undef LOG_DEBUG
40#undef LOG_TAG
41#define LOG_TAG "VtsHalBluetooth"
42#include "hci/hci_packets.h"
43#include "packet/raw_builder.h"
44
Dylan Tian8a6e09c2022-08-16 07:29:28 +000045using aidl::android::hardware::bluetooth::IBluetoothHci;
46using aidl::android::hardware::bluetooth::IBluetoothHciCallbacks;
47using aidl::android::hardware::bluetooth::Status;
48using ndk::ScopedAStatus;
49using ndk::SpAIBinder;
50
Jack He2d2f3c22023-03-27 03:04:52 -070051using ::bluetooth::hci::CommandBuilder;
52using ::bluetooth::hci::CommandCompleteView;
53using ::bluetooth::hci::CommandView;
54using ::bluetooth::hci::ErrorCode;
55using ::bluetooth::hci::EventView;
56using ::bluetooth::hci::LeReadLocalSupportedFeaturesBuilder;
57using ::bluetooth::hci::LeReadLocalSupportedFeaturesCompleteView;
58using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsBuilder;
59using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsCompleteView;
60using ::bluetooth::hci::LeReadResolvingListSizeBuilder;
61using ::bluetooth::hci::LeReadResolvingListSizeCompleteView;
62using ::bluetooth::hci::LLFeaturesBits;
63using ::bluetooth::hci::OpCode;
64using ::bluetooth::hci::OpCodeText;
65using ::bluetooth::hci::PacketView;
66using ::bluetooth::hci::ReadLocalVersionInformationBuilder;
67using ::bluetooth::hci::ReadLocalVersionInformationCompleteView;
68
69static constexpr uint8_t kMinLeAdvSetForBt5 = 16;
70static constexpr uint8_t kMinLeResolvingListForBt5 = 8;
71
Myles Watsone1708c82023-01-18 17:07:58 -080072static constexpr size_t kNumHciCommandsBandwidth = 100;
73static constexpr size_t kNumScoPacketsBandwidth = 100;
74static constexpr size_t kNumAclPacketsBandwidth = 100;
Dylan Tian8a6e09c2022-08-16 07:29:28 +000075static constexpr std::chrono::milliseconds kWaitForInitTimeout(2000);
76static constexpr std::chrono::milliseconds kWaitForHciEventTimeout(2000);
Myles Watsone1708c82023-01-18 17:07:58 -080077static constexpr std::chrono::milliseconds kWaitForScoDataTimeout(1000);
78static constexpr std::chrono::milliseconds kWaitForAclDataTimeout(1000);
Dylan Tian8a6e09c2022-08-16 07:29:28 +000079static constexpr std::chrono::milliseconds kInterfaceCloseDelayMs(200);
80
Dylan Tian8a6e09c2022-08-16 07:29:28 +000081// To discard Qualcomm ACL debugging
82static constexpr uint16_t kAclHandleQcaDebugMessage = 0xedc;
83
84class ThroughputLogger {
85 public:
Jack Hedbceaca2023-03-27 18:06:38 -070086 explicit ThroughputLogger(std::string task)
Myles Watsone1708c82023-01-18 17:07:58 -080087 : total_bytes_(0),
Jack Hedbceaca2023-03-27 18:06:38 -070088 task_(std::move(task)),
Myles Watsone1708c82023-01-18 17:07:58 -080089 start_time_(std::chrono::steady_clock::now()) {}
Dylan Tian8a6e09c2022-08-16 07:29:28 +000090
91 ~ThroughputLogger() {
92 if (total_bytes_ == 0) {
93 return;
94 }
95 std::chrono::duration<double> duration =
96 std::chrono::steady_clock::now() - start_time_;
97 double s = duration.count();
98 if (s == 0) {
99 return;
100 }
101 double rate_kb = (static_cast<double>(total_bytes_) / s) / 1024;
102 ALOGD("%s %.1f KB/s (%zu bytes in %.3fs)", task_.c_str(), rate_kb,
103 total_bytes_, s);
104 }
105
106 void setTotalBytes(size_t total_bytes) { total_bytes_ = total_bytes; }
107
108 private:
109 size_t total_bytes_;
110 std::string task_;
111 std::chrono::steady_clock::time_point start_time_;
112};
113
114// The main test class for Bluetooth HAL.
115class BluetoothAidlTest : public ::testing::TestWithParam<std::string> {
116 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700117 void SetUp() override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000118 // currently test passthrough mode only
119 hci = IBluetoothHci::fromBinder(
120 SpAIBinder(AServiceManager_waitForService(GetParam().c_str())));
121 ASSERT_NE(hci, nullptr);
122 ALOGI("%s: getService() for bluetooth hci is %s", __func__,
123 hci->isRemote() ? "remote" : "local");
124
125 // Lambda function
126 auto on_binder_death = [](void* /*cookie*/) { FAIL(); };
127
128 bluetooth_hci_death_recipient =
129 AIBinder_DeathRecipient_new(on_binder_death);
130 ASSERT_NE(bluetooth_hci_death_recipient, nullptr);
131 ASSERT_EQ(STATUS_OK,
132 AIBinder_linkToDeath(hci->asBinder().get(),
Jack Hedbceaca2023-03-27 18:06:38 -0700133 bluetooth_hci_death_recipient, nullptr));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000134
135 hci_cb = ndk::SharedRefBase::make<BluetoothHciCallbacks>(*this);
136 ASSERT_NE(hci_cb, nullptr);
137
138 max_acl_data_packet_length = 0;
139 max_sco_data_packet_length = 0;
140 max_acl_data_packets = 0;
141 max_sco_data_packets = 0;
142
143 event_cb_count = 0;
144 acl_cb_count = 0;
145 sco_cb_count = 0;
146
147 ASSERT_TRUE(hci->initialize(hci_cb).isOk());
148 auto future = initialized_promise.get_future();
149 auto timeout_status = future.wait_for(kWaitForInitTimeout);
150 ASSERT_EQ(timeout_status, std::future_status::ready);
151 ASSERT_TRUE(future.get());
152 }
153
Jack Hedbceaca2023-03-27 18:06:38 -0700154 void TearDown() override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000155 ALOGI("TearDown");
156 // Should not be checked in production code
157 ASSERT_TRUE(hci->close().isOk());
158 std::this_thread::sleep_for(kInterfaceCloseDelayMs);
159 handle_no_ops();
Myles Watsone1708c82023-01-18 17:07:58 -0800160 discard_qca_debugging();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000161 EXPECT_EQ(static_cast<size_t>(0), event_queue.size());
162 EXPECT_EQ(static_cast<size_t>(0), sco_queue.size());
163 EXPECT_EQ(static_cast<size_t>(0), acl_queue.size());
164 EXPECT_EQ(static_cast<size_t>(0), iso_queue.size());
165 }
166
167 void setBufferSizes();
Myles Watsone1708c82023-01-18 17:07:58 -0800168 void setSynchronousFlowControlEnable();
169
170 // Functions called from within tests in loopback mode
171 void sendAndCheckHci(int num_packets);
172 void sendAndCheckSco(int num_packets, size_t size, uint16_t handle);
173 void sendAndCheckAcl(int num_packets, size_t size, uint16_t handle);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000174
175 // Helper functions to try to get a handle on verbosity
Myles Watsone1708c82023-01-18 17:07:58 -0800176 void enterLoopbackMode();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000177 void handle_no_ops();
Myles Watsone1708c82023-01-18 17:07:58 -0800178 void discard_qca_debugging();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000179 void wait_for_event(bool timeout_is_error);
Jack He2d2f3c22023-03-27 03:04:52 -0700180 void wait_for_command_complete_event(OpCode opCode,
181 std::vector<uint8_t>& complete_event);
182 // Wait until a command complete is received.
183 // Command complete will be consumed after this method
184 void wait_and_validate_command_complete_event(OpCode opCode);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000185 int wait_for_completed_packets_event(uint16_t handle);
Jack He2d2f3c22023-03-27 03:04:52 -0700186 void send_and_wait_for_cmd_complete(std::unique_ptr<CommandBuilder> cmd,
187 std::vector<uint8_t>& cmd_complete);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000188
189 // A simple test implementation of BluetoothHciCallbacks.
190 class BluetoothHciCallbacks
191 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
192 BluetoothAidlTest& parent_;
193
194 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700195 explicit BluetoothHciCallbacks(BluetoothAidlTest& parent)
196 : parent_(parent){};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000197
Jack Hedbceaca2023-03-27 18:06:38 -0700198 ~BluetoothHciCallbacks() override = default;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000199
Jack Hedbceaca2023-03-27 18:06:38 -0700200 ndk::ScopedAStatus initializationComplete(Status status) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000201 parent_.initialized_promise.set_value(status == Status::SUCCESS);
202 ALOGV("%s (status = %d)", __func__, static_cast<int>(status));
203 return ScopedAStatus::ok();
204 };
205
Jack Hedbceaca2023-03-27 18:06:38 -0700206 ndk::ScopedAStatus hciEventReceived(
207 const std::vector<uint8_t>& event) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000208 parent_.event_cb_count++;
209 parent_.event_queue.push(event);
Jack He2d2f3c22023-03-27 03:04:52 -0700210 ALOGI("Event received (length = %d)", static_cast<int>(event.size()));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000211 return ScopedAStatus::ok();
212 };
213
Jack Hedbceaca2023-03-27 18:06:38 -0700214 ndk::ScopedAStatus aclDataReceived(
215 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000216 parent_.acl_cb_count++;
217 parent_.acl_queue.push(data);
218 return ScopedAStatus::ok();
219 };
220
Jack Hedbceaca2023-03-27 18:06:38 -0700221 ndk::ScopedAStatus scoDataReceived(
222 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000223 parent_.sco_cb_count++;
224 parent_.sco_queue.push(data);
225 return ScopedAStatus::ok();
226 };
227
Jack Hedbceaca2023-03-27 18:06:38 -0700228 ndk::ScopedAStatus isoDataReceived(
229 const std::vector<uint8_t>& data) override {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000230 parent_.iso_cb_count++;
231 parent_.iso_queue.push(data);
232 return ScopedAStatus::ok();
233 };
234 };
235
236 template <class T>
237 class WaitQueue {
238 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700239 WaitQueue() = default;
240 ;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000241
242 virtual ~WaitQueue() = default;
243
244 bool empty() const {
245 std::lock_guard<std::mutex> lock(m_);
246 return q_.empty();
247 };
248
249 size_t size() const {
250 std::lock_guard<std::mutex> lock(m_);
251 return q_.size();
252 };
253
254 void push(const T& v) {
255 std::lock_guard<std::mutex> lock(m_);
256 q_.push(v);
257 ready_.notify_one();
258 };
259
260 bool pop(T& v) {
261 std::lock_guard<std::mutex> lock(m_);
262 if (q_.empty()) {
263 return false;
264 }
265 v = std::move(q_.front());
266 q_.pop();
267 return true;
268 };
269
Myles Watson51b8bae2023-01-27 14:22:24 -0800270 void pop() {
271 std::lock_guard<std::mutex> lock(m_);
272 if (q_.empty()) {
273 return;
274 }
275 q_.pop();
276 };
277
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000278 bool front(T& v) {
279 std::lock_guard<std::mutex> lock(m_);
280 if (q_.empty()) {
281 return false;
282 }
283 v = q_.front();
284 return true;
285 };
286
287 void wait() {
288 std::unique_lock<std::mutex> lock(m_);
289 while (q_.empty()) {
290 ready_.wait(lock);
291 }
292 };
293
294 bool waitWithTimeout(std::chrono::milliseconds timeout) {
295 std::unique_lock<std::mutex> lock(m_);
296 while (q_.empty()) {
297 if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
298 return false;
299 }
300 }
301 return true;
302 };
303
304 bool tryPopWithTimeout(T& v, std::chrono::milliseconds timeout) {
305 std::unique_lock<std::mutex> lock(m_);
306 while (q_.empty()) {
307 if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) {
308 return false;
309 }
310 }
311 v = std::move(q_.front());
312 q_.pop();
313 return true;
314 };
315
316 private:
317 mutable std::mutex m_;
318 std::queue<T> q_;
319 std::condition_variable_any ready_;
320 };
321
322 std::shared_ptr<IBluetoothHci> hci;
323 std::shared_ptr<BluetoothHciCallbacks> hci_cb;
Jack Hedbceaca2023-03-27 18:06:38 -0700324 AIBinder_DeathRecipient* bluetooth_hci_death_recipient{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000325 WaitQueue<std::vector<uint8_t>> event_queue;
326 WaitQueue<std::vector<uint8_t>> acl_queue;
327 WaitQueue<std::vector<uint8_t>> sco_queue;
328 WaitQueue<std::vector<uint8_t>> iso_queue;
329
330 std::promise<bool> initialized_promise;
Jack Hedbceaca2023-03-27 18:06:38 -0700331 int event_cb_count{};
332 int sco_cb_count{};
333 int acl_cb_count{};
334 int iso_cb_count{};
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000335
Jack Hedbceaca2023-03-27 18:06:38 -0700336 int max_acl_data_packet_length{};
337 int max_sco_data_packet_length{};
338 int max_acl_data_packets{};
339 int max_sco_data_packets{};
Myles Watsone1708c82023-01-18 17:07:58 -0800340
341 std::vector<uint16_t> sco_connection_handles;
342 std::vector<uint16_t> acl_connection_handles;
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000343};
344
345// Discard NO-OPs from the event queue.
346void BluetoothAidlTest::handle_no_ops() {
347 while (!event_queue.empty()) {
348 std::vector<uint8_t> event;
349 event_queue.front(event);
Myles Watson51b8bae2023-01-27 14:22:24 -0800350 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
351 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
352 std::make_shared<std::vector<uint8_t>>(event))));
353 auto status_view = ::bluetooth::hci::CommandCompleteView::Create(
354 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
355 std::make_shared<std::vector<uint8_t>>(event))));
356 bool is_complete_no_op =
357 complete_view.IsValid() &&
358 complete_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
359 bool is_status_no_op =
360 status_view.IsValid() &&
361 status_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE;
362 if (is_complete_no_op || is_status_no_op) {
363 event_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000364 } else {
365 break;
366 }
367 }
Myles Watsone1708c82023-01-18 17:07:58 -0800368}
369
370// Discard Qualcomm ACL debugging
371void BluetoothAidlTest::discard_qca_debugging() {
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000372 while (!acl_queue.empty()) {
373 std::vector<uint8_t> acl_packet;
374 acl_queue.front(acl_packet);
Myles Watson51b8bae2023-01-27 14:22:24 -0800375 auto acl_view =
376 ::bluetooth::hci::AclView::Create(::bluetooth::hci::PacketView<true>(
377 std::make_shared<std::vector<uint8_t>>(acl_packet)));
378 EXPECT_TRUE(acl_view.IsValid());
379 if (acl_view.GetHandle() == kAclHandleQcaDebugMessage) {
380 acl_queue.pop();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000381 } else {
382 break;
383 }
384 }
385}
386
387// Receive an event, discarding NO-OPs.
388void BluetoothAidlTest::wait_for_event(bool timeout_is_error = true) {
Myles Watsone1708c82023-01-18 17:07:58 -0800389 // Wait until we get something that's not a no-op.
390 while (true) {
391 bool event_ready = event_queue.waitWithTimeout(kWaitForHciEventTimeout);
392 ASSERT_TRUE(event_ready || !timeout_is_error);
393 if (event_queue.empty()) {
394 // waitWithTimeout timed out
395 return;
396 }
397 handle_no_ops();
398 if (!event_queue.empty()) {
399 // There's an event in the queue that's not a no-op.
400 return;
401 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000402 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000403}
404
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000405void BluetoothAidlTest::wait_for_command_complete_event(
Jack He2d2f3c22023-03-27 03:04:52 -0700406 OpCode opCode, std::vector<uint8_t>& complete_event) {
Myles Watsone1708c82023-01-18 17:07:58 -0800407 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Myles Watsone1708c82023-01-18 17:07:58 -0800408 ASSERT_FALSE(event_queue.empty());
Jack He2d2f3c22023-03-27 03:04:52 -0700409 ASSERT_TRUE(event_queue.pop(complete_event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800410 auto complete_view = ::bluetooth::hci::CommandCompleteView::Create(
411 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
Jack He2d2f3c22023-03-27 03:04:52 -0700412 std::make_shared<std::vector<uint8_t>>(complete_event))));
Myles Watson51b8bae2023-01-27 14:22:24 -0800413 ASSERT_TRUE(complete_view.IsValid());
414 ASSERT_EQ(complete_view.GetCommandOpCode(), opCode);
415 ASSERT_EQ(complete_view.GetPayload()[0],
416 static_cast<uint8_t>(::bluetooth::hci::ErrorCode::SUCCESS));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000417}
418
Jack He2d2f3c22023-03-27 03:04:52 -0700419void BluetoothAidlTest::wait_and_validate_command_complete_event(
420 ::bluetooth::hci::OpCode opCode) {
421 std::vector<uint8_t> complete_event;
422 ASSERT_NO_FATAL_FAILURE(
423 wait_for_command_complete_event(opCode, complete_event));
424}
425
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000426// Send the command to read the controller's buffer sizes.
427void BluetoothAidlTest::setBufferSizes() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800428 std::vector<uint8_t> cmd;
429 ::bluetooth::packet::BitInserter bi{cmd};
430 ::bluetooth::hci::ReadBufferSizeBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000431 hci->sendHciCommand(cmd);
432
Myles Watsone1708c82023-01-18 17:07:58 -0800433 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000434 std::vector<uint8_t> event;
435 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800436 auto complete_view = ::bluetooth::hci::ReadBufferSizeCompleteView::Create(
437 ::bluetooth::hci::CommandCompleteView::Create(
438 ::bluetooth::hci::EventView::Create(
439 ::bluetooth::hci::PacketView<true>(
440 std::make_shared<std::vector<uint8_t>>(event)))));
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000441
Myles Watson51b8bae2023-01-27 14:22:24 -0800442 ASSERT_TRUE(complete_view.IsValid());
443 ASSERT_EQ(complete_view.GetStatus(), ::bluetooth::hci::ErrorCode::SUCCESS);
444 max_acl_data_packet_length = complete_view.GetAclDataPacketLength();
445 max_sco_data_packet_length = complete_view.GetSynchronousDataPacketLength();
446 max_acl_data_packets = complete_view.GetTotalNumAclDataPackets();
447 max_sco_data_packets = complete_view.GetTotalNumSynchronousDataPackets();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000448
449 ALOGD("%s: ACL max %d num %d SCO max %d num %d", __func__,
450 static_cast<int>(max_acl_data_packet_length),
451 static_cast<int>(max_acl_data_packets),
452 static_cast<int>(max_sco_data_packet_length),
453 static_cast<int>(max_sco_data_packets));
454}
455
Myles Watsone1708c82023-01-18 17:07:58 -0800456// Enable flow control packets for SCO
457void BluetoothAidlTest::setSynchronousFlowControlEnable() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800458 std::vector<uint8_t> cmd;
459 ::bluetooth::packet::BitInserter bi{cmd};
460 ::bluetooth::hci::WriteSynchronousFlowControlEnableBuilder::Create(
461 ::bluetooth::hci::Enable::ENABLED)
462 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800463 hci->sendHciCommand(cmd);
464
Jack He2d2f3c22023-03-27 03:04:52 -0700465 wait_and_validate_command_complete_event(
Myles Watson51b8bae2023-01-27 14:22:24 -0800466 ::bluetooth::hci::OpCode::WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE);
Myles Watsone1708c82023-01-18 17:07:58 -0800467}
468
469// Send an HCI command (in Loopback mode) and check the response.
470void BluetoothAidlTest::sendAndCheckHci(int num_packets) {
Jack Hedbceaca2023-03-27 18:06:38 -0700471 ThroughputLogger logger{__func__};
472 size_t command_size = 0;
Myles Watson51b8bae2023-01-27 14:22:24 -0800473 char new_name[] = "John Jacob Jingleheimer Schmidt ___________________";
474 size_t new_name_length = strlen(new_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800475 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800476 // The name to set is new_name
Jack Hedbceaca2023-03-27 18:06:38 -0700477 std::array<uint8_t, 248> name_array{};
Myles Watsone1708c82023-01-18 17:07:58 -0800478 for (size_t i = 0; i < new_name_length; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800479 name_array[i] = new_name[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800480 }
481 // And the packet number
Myles Watson51b8bae2023-01-27 14:22:24 -0800482 char number[11] = "0000000000";
483 snprintf(number, sizeof(number), "%010d", static_cast<int>(n));
484 for (size_t i = new_name_length; i < new_name_length + sizeof(number) - 1;
485 i++) {
486 name_array[new_name_length + i] = number[i];
Myles Watsone1708c82023-01-18 17:07:58 -0800487 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800488 std::vector<uint8_t> write_name;
489 ::bluetooth::packet::BitInserter bi{write_name};
490 ::bluetooth::hci::WriteLocalNameBuilder::Create(name_array)->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800491 hci->sendHciCommand(write_name);
492
493 // Check the loopback of the HCI packet
494 ASSERT_NO_FATAL_FAILURE(wait_for_event());
495
496 std::vector<uint8_t> event;
497 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800498 auto event_view = ::bluetooth::hci::LoopbackCommandView::Create(
499 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
500 std::make_shared<std::vector<uint8_t>>(event))));
501 ASSERT_TRUE(event_view.IsValid());
502 std::vector<uint8_t> looped_back_command{event_view.GetPayload().begin(),
503 event_view.GetPayload().end()};
504 ASSERT_EQ(looped_back_command, write_name);
Myles Watsone1708c82023-01-18 17:07:58 -0800505
506 if (n == num_packets - 1) {
507 command_size = write_name.size();
508 }
Myles Watsone1708c82023-01-18 17:07:58 -0800509 }
510 logger.setTotalBytes(command_size * num_packets * 2);
511}
512
513// Send a SCO data packet (in Loopback mode) and check the response.
514void BluetoothAidlTest::sendAndCheckSco(int num_packets, size_t size,
515 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700516 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800517 for (int n = 0; n < num_packets; n++) {
518 // Send a SCO packet
519 std::vector<uint8_t> sco_packet;
Myles Watson51b8bae2023-01-27 14:22:24 -0800520 std::vector<uint8_t> payload;
Myles Watsone1708c82023-01-18 17:07:58 -0800521 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800522 payload.push_back(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800523 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800524 ::bluetooth::packet::BitInserter bi{sco_packet};
525 ::bluetooth::hci::ScoBuilder::Create(
526 handle, ::bluetooth::hci::PacketStatusFlag::CORRECTLY_RECEIVED, payload)
527 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800528 hci->sendScoData(sco_packet);
529
530 // Check the loopback of the SCO packet
531 std::vector<uint8_t> sco_loopback;
532 ASSERT_TRUE(
533 sco_queue.tryPopWithTimeout(sco_loopback, kWaitForScoDataTimeout));
534
Myles Watson51b8bae2023-01-27 14:22:24 -0800535 ASSERT_EQ(sco_packet, sco_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800536 }
537 logger.setTotalBytes(num_packets * size * 2);
538}
539
540// Send an ACL data packet (in Loopback mode) and check the response.
541void BluetoothAidlTest::sendAndCheckAcl(int num_packets, size_t size,
542 uint16_t handle) {
Jack Hedbceaca2023-03-27 18:06:38 -0700543 ThroughputLogger logger{__func__};
Myles Watsone1708c82023-01-18 17:07:58 -0800544 for (int n = 0; n < num_packets; n++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800545 // Send an ACL packet with counting data
546 auto payload = std::make_unique<::bluetooth::packet::RawBuilder>();
Myles Watsone1708c82023-01-18 17:07:58 -0800547 for (size_t i = 0; i < size; i++) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800548 payload->AddOctets1(static_cast<uint8_t>(i + n));
Myles Watsone1708c82023-01-18 17:07:58 -0800549 }
Myles Watson51b8bae2023-01-27 14:22:24 -0800550 std::vector<uint8_t> acl_packet;
551 ::bluetooth::packet::BitInserter bi{acl_packet};
552 ::bluetooth::hci::AclBuilder::Create(
553 handle,
554 ::bluetooth::hci::PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE,
555 ::bluetooth::hci::BroadcastFlag::POINT_TO_POINT, std::move(payload))
556 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800557 hci->sendAclData(acl_packet);
558
559 std::vector<uint8_t> acl_loopback;
560 // Check the loopback of the ACL packet
561 ASSERT_TRUE(
562 acl_queue.tryPopWithTimeout(acl_loopback, kWaitForAclDataTimeout));
563
Myles Watson51b8bae2023-01-27 14:22:24 -0800564 ASSERT_EQ(acl_packet, acl_loopback);
Myles Watsone1708c82023-01-18 17:07:58 -0800565 }
566 logger.setTotalBytes(num_packets * size * 2);
567}
568
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000569// Return the number of completed packets reported by the controller.
570int BluetoothAidlTest::wait_for_completed_packets_event(uint16_t handle) {
571 int packets_processed = 0;
Myles Watson65b47f52023-01-26 12:59:06 -0800572 while (true) {
573 // There should be at least one event.
574 wait_for_event(packets_processed == 0);
575 if (event_queue.empty()) {
576 if (packets_processed == 0) {
577 ALOGW("%s: waitForBluetoothCallback timed out.", __func__);
578 }
579 return packets_processed;
580 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000581 std::vector<uint8_t> event;
582 EXPECT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800583 auto event_view = ::bluetooth::hci::NumberOfCompletedPacketsView::Create(
584 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
585 std::make_shared<std::vector<uint8_t>>(event))));
586 if (!event_view.IsValid()) {
587 ADD_FAILURE();
588 return packets_processed;
589 }
590 auto completed_packets = event_view.GetCompletedPackets();
Jack Hedbceaca2023-03-27 18:06:38 -0700591 for (const auto& entry : completed_packets) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800592 EXPECT_EQ(handle, entry.connection_handle_);
593 packets_processed += entry.host_num_of_completed_packets_;
594 }
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000595 }
596 return packets_processed;
597}
598
Myles Watsone1708c82023-01-18 17:07:58 -0800599// Send local loopback command and initialize SCO and ACL handles.
600void BluetoothAidlTest::enterLoopbackMode() {
Myles Watson51b8bae2023-01-27 14:22:24 -0800601 std::vector<uint8_t> cmd;
602 ::bluetooth::packet::BitInserter bi{cmd};
603 ::bluetooth::hci::WriteLoopbackModeBuilder::Create(
604 bluetooth::hci::LoopbackMode::ENABLE_LOCAL)
605 ->Serialize(bi);
Myles Watsone1708c82023-01-18 17:07:58 -0800606 hci->sendHciCommand(cmd);
607
608 // Receive connection complete events with data channels
609 int connection_event_count = 0;
610 bool command_complete_received = false;
611 while (true) {
612 wait_for_event(false);
613 if (event_queue.empty()) {
614 // Fail if there was no event received or no connections completed.
615 ASSERT_TRUE(command_complete_received);
616 ASSERT_LT(0, connection_event_count);
617 return;
618 }
619 std::vector<uint8_t> event;
620 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800621 auto event_view =
622 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
623 std::make_shared<std::vector<uint8_t>>(event)));
624 ASSERT_TRUE(event_view.IsValid());
Myles Watsone1708c82023-01-18 17:07:58 -0800625
Myles Watson51b8bae2023-01-27 14:22:24 -0800626 if (event_view.GetEventCode() ==
627 ::bluetooth::hci::EventCode::CONNECTION_COMPLETE) {
628 auto complete_view =
629 ::bluetooth::hci::ConnectionCompleteView::Create(event_view);
630 ASSERT_TRUE(complete_view.IsValid());
631 switch (complete_view.GetLinkType()) {
632 case ::bluetooth::hci::LinkType::ACL:
633 acl_connection_handles.push_back(complete_view.GetConnectionHandle());
634 break;
635 case ::bluetooth::hci::LinkType::SCO:
636 sco_connection_handles.push_back(complete_view.GetConnectionHandle());
637 break;
638 default:
639 ASSERT_EQ(complete_view.GetLinkType(),
640 ::bluetooth::hci::LinkType::ACL);
Myles Watsone1708c82023-01-18 17:07:58 -0800641 }
Myles Watsone1708c82023-01-18 17:07:58 -0800642 connection_event_count++;
643 } else {
Myles Watson51b8bae2023-01-27 14:22:24 -0800644 auto command_complete_view =
645 ::bluetooth::hci::WriteLoopbackModeCompleteView::Create(
646 ::bluetooth::hci::CommandCompleteView::Create(event_view));
647 ASSERT_TRUE(command_complete_view.IsValid());
648 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
649 command_complete_view.GetStatus());
Myles Watsone1708c82023-01-18 17:07:58 -0800650 command_complete_received = true;
651 }
652 }
653}
654
Jack He2d2f3c22023-03-27 03:04:52 -0700655void BluetoothAidlTest::send_and_wait_for_cmd_complete(
656 std::unique_ptr<CommandBuilder> cmd, std::vector<uint8_t>& cmd_complete) {
657 std::vector<uint8_t> cmd_bytes = cmd->SerializeToBytes();
658 hci->sendHciCommand(cmd_bytes);
659
660 auto view = CommandView::Create(
661 PacketView<true>(std::make_shared<std::vector<uint8_t>>(cmd_bytes)));
662 ASSERT_TRUE(view.IsValid());
663 ALOGI("Waiting for %s[0x%x]", OpCodeText(view.GetOpCode()).c_str(),
664 static_cast<int>(view.GetOpCode()));
665 ASSERT_NO_FATAL_FAILURE(
666 wait_for_command_complete_event(view.GetOpCode(), cmd_complete));
667}
668
Myles Watsone1708c82023-01-18 17:07:58 -0800669// Empty test: Initialize()/Close() are called in SetUp()/TearDown().
670TEST_P(BluetoothAidlTest, InitializeAndClose) {}
671
672// Send an HCI Reset with sendHciCommand and wait for a command complete event.
Myles Watson65b47f52023-01-26 12:59:06 -0800673TEST_P(BluetoothAidlTest, HciReset) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800674 std::vector<uint8_t> reset;
675 ::bluetooth::packet::BitInserter bi{reset};
676 ::bluetooth::hci::ResetBuilder::Create()->Serialize(bi);
Myles Watson65b47f52023-01-26 12:59:06 -0800677 hci->sendHciCommand(reset);
678
Jack He2d2f3c22023-03-27 03:04:52 -0700679 wait_and_validate_command_complete_event(::bluetooth::hci::OpCode::RESET);
Myles Watson65b47f52023-01-26 12:59:06 -0800680}
Myles Watsone1708c82023-01-18 17:07:58 -0800681
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000682// Read and check the HCI version of the controller.
683TEST_P(BluetoothAidlTest, HciVersionTest) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800684 std::vector<uint8_t> cmd;
685 ::bluetooth::packet::BitInserter bi{cmd};
686 ::bluetooth::hci::ReadLocalVersionInformationBuilder::Create()->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000687 hci->sendHciCommand(cmd);
688
Myles Watsone1708c82023-01-18 17:07:58 -0800689 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000690
691 std::vector<uint8_t> event;
692 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800693 auto complete_view =
694 ::bluetooth::hci::ReadLocalVersionInformationCompleteView::Create(
695 ::bluetooth::hci::CommandCompleteView::Create(
696 ::bluetooth::hci::EventView::Create(
697 ::bluetooth::hci::PacketView<true>(
698 std::make_shared<std::vector<uint8_t>>(event)))));
699 ASSERT_TRUE(complete_view.IsValid());
700 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, complete_view.GetStatus());
701 auto version = complete_view.GetLocalVersionInformation();
702 ASSERT_LE(::bluetooth::hci::HciVersion::V_3_0, version.hci_version_);
703 ASSERT_LE(::bluetooth::hci::LmpVersion::V_3_0, version.lmp_version_);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000704}
705
706// Send an unknown HCI command and wait for the error message.
707TEST_P(BluetoothAidlTest, HciUnknownCommand) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800708 std::vector<uint8_t> cmd;
709 ::bluetooth::packet::BitInserter bi{cmd};
710 ::bluetooth::hci::CommandBuilder::Create(
711 static_cast<::bluetooth::hci::OpCode>(0x3cff),
712 std::make_unique<::bluetooth::packet::RawBuilder>())
713 ->Serialize(bi);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000714 hci->sendHciCommand(cmd);
715
Myles Watsone1708c82023-01-18 17:07:58 -0800716 ASSERT_NO_FATAL_FAILURE(wait_for_event());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000717
718 std::vector<uint8_t> event;
719 ASSERT_TRUE(event_queue.pop(event));
Myles Watson51b8bae2023-01-27 14:22:24 -0800720 auto event_view =
721 ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>(
722 std::make_shared<std::vector<uint8_t>>(event)));
723 ASSERT_TRUE(event_view.IsValid());
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000724
Myles Watson51b8bae2023-01-27 14:22:24 -0800725 switch (event_view.GetEventCode()) {
726 case ::bluetooth::hci::EventCode::COMMAND_COMPLETE: {
727 auto command_complete =
728 ::bluetooth::hci::CommandCompleteView::Create(event_view);
729 ASSERT_TRUE(command_complete.IsValid());
730 ASSERT_EQ(command_complete.GetPayload()[0],
731 static_cast<uint8_t>(
732 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND));
733 } break;
734 case ::bluetooth::hci::EventCode::COMMAND_STATUS: {
735 auto command_status =
736 ::bluetooth::hci::CommandStatusView::Create(event_view);
737 ASSERT_TRUE(command_status.IsValid());
738 ASSERT_EQ(command_status.GetStatus(),
739 ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND);
740 } break;
741 default:
742 ADD_FAILURE();
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000743 }
744}
745
Myles Watsone1708c82023-01-18 17:07:58 -0800746// Enter loopback mode, but don't send any packets.
Myles Watson65b47f52023-01-26 12:59:06 -0800747TEST_P(BluetoothAidlTest, WriteLoopbackMode) { enterLoopbackMode(); }
Myles Watsone1708c82023-01-18 17:07:58 -0800748
749// Enter loopback mode and send a single command.
750TEST_P(BluetoothAidlTest, LoopbackModeSingleCommand) {
Myles Watsone1708c82023-01-18 17:07:58 -0800751 setBufferSizes();
752
753 enterLoopbackMode();
754
755 sendAndCheckHci(1);
756}
757
758// Enter loopback mode and send a single SCO packet.
759TEST_P(BluetoothAidlTest, LoopbackModeSingleSco) {
Myles Watsone1708c82023-01-18 17:07:58 -0800760 setBufferSizes();
761 setSynchronousFlowControlEnable();
762
763 enterLoopbackMode();
764
765 if (!sco_connection_handles.empty()) {
766 ASSERT_LT(0, max_sco_data_packet_length);
767 sendAndCheckSco(1, max_sco_data_packet_length, sco_connection_handles[0]);
768 int sco_packets_sent = 1;
769 int completed_packets =
770 wait_for_completed_packets_event(sco_connection_handles[0]);
771 if (sco_packets_sent != completed_packets) {
772 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
773 sco_packets_sent, completed_packets);
774 }
775 }
776}
777
778// Enter loopback mode and send a single ACL packet.
779TEST_P(BluetoothAidlTest, LoopbackModeSingleAcl) {
Myles Watsone1708c82023-01-18 17:07:58 -0800780 setBufferSizes();
781
782 enterLoopbackMode();
783
784 if (!acl_connection_handles.empty()) {
785 ASSERT_LT(0, max_acl_data_packet_length);
786 sendAndCheckAcl(1, max_acl_data_packet_length - 1,
787 acl_connection_handles[0]);
788 int acl_packets_sent = 1;
789 int completed_packets =
790 wait_for_completed_packets_event(acl_connection_handles[0]);
791 if (acl_packets_sent != completed_packets) {
792 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
793 acl_packets_sent, completed_packets);
794 }
795 }
796 ASSERT_GE(acl_cb_count, 1);
797}
798
799// Enter loopback mode and send command packets for bandwidth measurements.
800TEST_P(BluetoothAidlTest, LoopbackModeCommandBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800801 setBufferSizes();
802
803 enterLoopbackMode();
804
805 sendAndCheckHci(kNumHciCommandsBandwidth);
806}
807
808// Enter loopback mode and send SCO packets for bandwidth measurements.
809TEST_P(BluetoothAidlTest, LoopbackModeScoBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800810 setBufferSizes();
811 setSynchronousFlowControlEnable();
812
813 enterLoopbackMode();
814
815 if (!sco_connection_handles.empty()) {
816 ASSERT_LT(0, max_sco_data_packet_length);
817 sendAndCheckSco(kNumScoPacketsBandwidth, max_sco_data_packet_length,
818 sco_connection_handles[0]);
819 int sco_packets_sent = kNumScoPacketsBandwidth;
820 int completed_packets =
821 wait_for_completed_packets_event(sco_connection_handles[0]);
822 if (sco_packets_sent != completed_packets) {
823 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
824 sco_packets_sent, completed_packets);
825 }
826 }
827}
828
829// Enter loopback mode and send packets for ACL bandwidth measurements.
830TEST_P(BluetoothAidlTest, LoopbackModeAclBandwidth) {
Myles Watsone1708c82023-01-18 17:07:58 -0800831 setBufferSizes();
832
833 enterLoopbackMode();
834
835 if (!acl_connection_handles.empty()) {
836 ASSERT_LT(0, max_acl_data_packet_length);
837 sendAndCheckAcl(kNumAclPacketsBandwidth, max_acl_data_packet_length - 1,
838 acl_connection_handles[0]);
839 int acl_packets_sent = kNumAclPacketsBandwidth;
840 int completed_packets =
841 wait_for_completed_packets_event(acl_connection_handles[0]);
842 if (acl_packets_sent != completed_packets) {
843 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
844 acl_packets_sent, completed_packets);
845 }
846 }
847}
848
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000849// Set all bits in the event mask
850TEST_P(BluetoothAidlTest, SetEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800851 std::vector<uint8_t> cmd;
852 ::bluetooth::packet::BitInserter bi{cmd};
853 uint64_t full_mask = UINT64_MAX;
854 ::bluetooth::hci::SetEventMaskBuilder::Create(full_mask)->Serialize(bi);
855 hci->sendHciCommand(cmd);
Jack He2d2f3c22023-03-27 03:04:52 -0700856 wait_and_validate_command_complete_event(
857 ::bluetooth::hci::OpCode::SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000858}
859
860// Set all bits in the LE event mask
861TEST_P(BluetoothAidlTest, SetLeEventMask) {
Myles Watson51b8bae2023-01-27 14:22:24 -0800862 std::vector<uint8_t> cmd;
863 ::bluetooth::packet::BitInserter bi{cmd};
864 uint64_t full_mask = UINT64_MAX;
865 ::bluetooth::hci::LeSetEventMaskBuilder::Create(full_mask)->Serialize(bi);
866 hci->sendHciCommand(cmd);
Jack He2d2f3c22023-03-27 03:04:52 -0700867 wait_and_validate_command_complete_event(
868 ::bluetooth::hci::OpCode::LE_SET_EVENT_MASK);
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000869}
870
Myles Watson0daa1642023-01-26 15:58:28 -0800871// Call initialize twice, second one should fail.
872TEST_P(BluetoothAidlTest, CallInitializeTwice) {
873 class SecondCb
874 : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks {
875 public:
Jack Hedbceaca2023-03-27 18:06:38 -0700876 ndk::ScopedAStatus initializationComplete(Status status) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800877 EXPECT_EQ(status, Status::ALREADY_INITIALIZED);
878 init_promise.set_value();
879 return ScopedAStatus::ok();
880 };
881
Jack Hedbceaca2023-03-27 18:06:38 -0700882 ndk::ScopedAStatus hciEventReceived(
883 const std::vector<uint8_t>& /*event*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800884 ADD_FAILURE();
885 return ScopedAStatus::ok();
886 };
887
Jack Hedbceaca2023-03-27 18:06:38 -0700888 ndk::ScopedAStatus aclDataReceived(
889 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800890 ADD_FAILURE();
891 return ScopedAStatus::ok();
892 };
893
Jack Hedbceaca2023-03-27 18:06:38 -0700894 ndk::ScopedAStatus scoDataReceived(
895 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800896 ADD_FAILURE();
897 return ScopedAStatus::ok();
898 };
899
Jack Hedbceaca2023-03-27 18:06:38 -0700900 ndk::ScopedAStatus isoDataReceived(
901 const std::vector<uint8_t>& /*data*/) override {
Myles Watson0daa1642023-01-26 15:58:28 -0800902 ADD_FAILURE();
903 return ScopedAStatus::ok();
904 };
905 std::promise<void> init_promise;
906 };
907
908 std::shared_ptr<SecondCb> second_cb = ndk::SharedRefBase::make<SecondCb>();
909 ASSERT_NE(second_cb, nullptr);
910
911 auto future = second_cb->init_promise.get_future();
912 ASSERT_TRUE(hci->initialize(second_cb).isOk());
913 auto status = future.wait_for(std::chrono::seconds(1));
914 ASSERT_EQ(status, std::future_status::ready);
915}
916
Myles Watson5bfb3a72023-05-10 14:06:32 -0700917TEST_P(BluetoothAidlTest, Vsr_Bluetooth5Requirements) {
Jack He2d2f3c22023-03-27 03:04:52 -0700918 std::vector<uint8_t> version_event;
919 send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(),
920 version_event);
921 auto version_view = ReadLocalVersionInformationCompleteView::Create(
922 CommandCompleteView::Create(EventView::Create(PacketView<true>(
923 std::make_shared<std::vector<uint8_t>>(version_event)))));
924 ASSERT_TRUE(version_view.IsValid());
925 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus());
926 auto version = version_view.GetLocalVersionInformation();
927 if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) {
928 // This test does not apply to controllers below 5.0
929 return;
930 };
931 // When HCI version is 5.0, LMP version must also be at least 5.0
932 ASSERT_GE(static_cast<int>(version.lmp_version_),
933 static_cast<int>(version.hci_version_));
934
935 std::vector<uint8_t> le_features_event;
936 send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(),
937 le_features_event);
938 auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create(
939 CommandCompleteView::Create(EventView::Create(PacketView<true>(
940 std::make_shared<std::vector<uint8_t>>(le_features_event)))));
941 ASSERT_TRUE(le_features_view.IsValid());
942 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus());
943 auto le_features = le_features_view.GetLeFeatures();
944 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LL_PRIVACY));
945 ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LE_2M_PHY));
946 ASSERT_TRUE(le_features &
947 static_cast<uint64_t>(LLFeaturesBits::LE_CODED_PHY));
948 ASSERT_TRUE(le_features &
949 static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING));
950
951 std::vector<uint8_t> num_adv_set_event;
952 send_and_wait_for_cmd_complete(
953 LeReadNumberOfSupportedAdvertisingSetsBuilder::Create(),
954 num_adv_set_event);
955 auto num_adv_set_view =
956 LeReadNumberOfSupportedAdvertisingSetsCompleteView::Create(
957 CommandCompleteView::Create(EventView::Create(PacketView<true>(
958 std::make_shared<std::vector<uint8_t>>(num_adv_set_event)))));
959 ASSERT_TRUE(num_adv_set_view.IsValid());
960 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, num_adv_set_view.GetStatus());
961 auto num_adv_set = num_adv_set_view.GetNumberSupportedAdvertisingSets();
962 ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5);
963
964 std::vector<uint8_t> num_resolving_list_event;
965 send_and_wait_for_cmd_complete(LeReadResolvingListSizeBuilder::Create(),
966 num_resolving_list_event);
967 auto num_resolving_list_view = LeReadResolvingListSizeCompleteView::Create(
968 CommandCompleteView::Create(EventView::Create(PacketView<true>(
969 std::make_shared<std::vector<uint8_t>>(num_resolving_list_event)))));
970 ASSERT_TRUE(num_resolving_list_view.IsValid());
971 ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS,
972 num_resolving_list_view.GetStatus());
973 auto num_resolving_list = num_resolving_list_view.GetResolvingListSize();
974 ASSERT_GE(num_resolving_list, kMinLeResolvingListForBt5);
975}
976
Dylan Tian8a6e09c2022-08-16 07:29:28 +0000977GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothAidlTest);
978INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothAidlTest,
979 testing::ValuesIn(android::getAidlHalInstanceNames(
980 IBluetoothHci::descriptor)),
981 android::PrintInstanceNameToString);
982
983int main(int argc, char** argv) {
984 ABinderProcess_startThreadPool();
985 ::testing::InitGoogleTest(&argc, argv);
986 int status = RUN_ALL_TESTS();
987 ALOGI("Test result = %d", status);
988 return status;
989}