Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 1 | /* |
| 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 27 | |
| 28 | #include <atomic> |
| 29 | #include <chrono> |
| 30 | #include <condition_variable> |
| 31 | #include <future> |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 32 | #include <queue> |
| 33 | #include <thread> |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 34 | #include <utility> |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 35 | #include <vector> |
| 36 | |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 37 | // 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 45 | using aidl::android::hardware::bluetooth::IBluetoothHci; |
| 46 | using aidl::android::hardware::bluetooth::IBluetoothHciCallbacks; |
| 47 | using aidl::android::hardware::bluetooth::Status; |
| 48 | using ndk::ScopedAStatus; |
| 49 | using ndk::SpAIBinder; |
| 50 | |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 51 | using ::bluetooth::hci::CommandBuilder; |
| 52 | using ::bluetooth::hci::CommandCompleteView; |
| 53 | using ::bluetooth::hci::CommandView; |
| 54 | using ::bluetooth::hci::ErrorCode; |
| 55 | using ::bluetooth::hci::EventView; |
| 56 | using ::bluetooth::hci::LeReadLocalSupportedFeaturesBuilder; |
| 57 | using ::bluetooth::hci::LeReadLocalSupportedFeaturesCompleteView; |
| 58 | using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsBuilder; |
| 59 | using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsCompleteView; |
| 60 | using ::bluetooth::hci::LeReadResolvingListSizeBuilder; |
| 61 | using ::bluetooth::hci::LeReadResolvingListSizeCompleteView; |
| 62 | using ::bluetooth::hci::LLFeaturesBits; |
| 63 | using ::bluetooth::hci::OpCode; |
| 64 | using ::bluetooth::hci::OpCodeText; |
| 65 | using ::bluetooth::hci::PacketView; |
| 66 | using ::bluetooth::hci::ReadLocalVersionInformationBuilder; |
| 67 | using ::bluetooth::hci::ReadLocalVersionInformationCompleteView; |
| 68 | |
| 69 | static constexpr uint8_t kMinLeAdvSetForBt5 = 16; |
| 70 | static constexpr uint8_t kMinLeResolvingListForBt5 = 8; |
| 71 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 72 | static constexpr size_t kNumHciCommandsBandwidth = 100; |
| 73 | static constexpr size_t kNumScoPacketsBandwidth = 100; |
| 74 | static constexpr size_t kNumAclPacketsBandwidth = 100; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 75 | static constexpr std::chrono::milliseconds kWaitForInitTimeout(2000); |
| 76 | static constexpr std::chrono::milliseconds kWaitForHciEventTimeout(2000); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 77 | static constexpr std::chrono::milliseconds kWaitForScoDataTimeout(1000); |
| 78 | static constexpr std::chrono::milliseconds kWaitForAclDataTimeout(1000); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 79 | static constexpr std::chrono::milliseconds kInterfaceCloseDelayMs(200); |
| 80 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 81 | // To discard Qualcomm ACL debugging |
| 82 | static constexpr uint16_t kAclHandleQcaDebugMessage = 0xedc; |
| 83 | |
| 84 | class ThroughputLogger { |
| 85 | public: |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 86 | explicit ThroughputLogger(std::string task) |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 87 | : total_bytes_(0), |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 88 | task_(std::move(task)), |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 89 | start_time_(std::chrono::steady_clock::now()) {} |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 90 | |
| 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. |
| 115 | class BluetoothAidlTest : public ::testing::TestWithParam<std::string> { |
| 116 | public: |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 117 | void SetUp() override { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 118 | // 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 He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 133 | bluetooth_hci_death_recipient, nullptr)); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 134 | |
| 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 He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 154 | void TearDown() override { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 155 | 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 Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 160 | discard_qca_debugging(); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 161 | 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 Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 168 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 174 | |
| 175 | // Helper functions to try to get a handle on verbosity |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 176 | void enterLoopbackMode(); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 177 | void handle_no_ops(); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 178 | void discard_qca_debugging(); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 179 | void wait_for_event(bool timeout_is_error); |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 180 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 185 | int wait_for_completed_packets_event(uint16_t handle); |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 186 | void send_and_wait_for_cmd_complete(std::unique_ptr<CommandBuilder> cmd, |
| 187 | std::vector<uint8_t>& cmd_complete); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 188 | |
| 189 | // A simple test implementation of BluetoothHciCallbacks. |
| 190 | class BluetoothHciCallbacks |
| 191 | : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks { |
| 192 | BluetoothAidlTest& parent_; |
| 193 | |
| 194 | public: |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 195 | explicit BluetoothHciCallbacks(BluetoothAidlTest& parent) |
| 196 | : parent_(parent){}; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 197 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 198 | ~BluetoothHciCallbacks() override = default; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 199 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 200 | ndk::ScopedAStatus initializationComplete(Status status) override { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 201 | 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 He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 206 | ndk::ScopedAStatus hciEventReceived( |
| 207 | const std::vector<uint8_t>& event) override { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 208 | parent_.event_cb_count++; |
| 209 | parent_.event_queue.push(event); |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 210 | ALOGI("Event received (length = %d)", static_cast<int>(event.size())); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 211 | return ScopedAStatus::ok(); |
| 212 | }; |
| 213 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 214 | ndk::ScopedAStatus aclDataReceived( |
| 215 | const std::vector<uint8_t>& data) override { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 216 | parent_.acl_cb_count++; |
| 217 | parent_.acl_queue.push(data); |
| 218 | return ScopedAStatus::ok(); |
| 219 | }; |
| 220 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 221 | ndk::ScopedAStatus scoDataReceived( |
| 222 | const std::vector<uint8_t>& data) override { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 223 | parent_.sco_cb_count++; |
| 224 | parent_.sco_queue.push(data); |
| 225 | return ScopedAStatus::ok(); |
| 226 | }; |
| 227 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 228 | ndk::ScopedAStatus isoDataReceived( |
| 229 | const std::vector<uint8_t>& data) override { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 230 | 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 He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 239 | WaitQueue() = default; |
| 240 | ; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 241 | |
| 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 Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 270 | void pop() { |
| 271 | std::lock_guard<std::mutex> lock(m_); |
| 272 | if (q_.empty()) { |
| 273 | return; |
| 274 | } |
| 275 | q_.pop(); |
| 276 | }; |
| 277 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 278 | 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 He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 324 | AIBinder_DeathRecipient* bluetooth_hci_death_recipient{}; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 325 | 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 He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 331 | int event_cb_count{}; |
| 332 | int sco_cb_count{}; |
| 333 | int acl_cb_count{}; |
| 334 | int iso_cb_count{}; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 335 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 336 | 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 Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 340 | |
| 341 | std::vector<uint16_t> sco_connection_handles; |
| 342 | std::vector<uint16_t> acl_connection_handles; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | // Discard NO-OPs from the event queue. |
| 346 | void BluetoothAidlTest::handle_no_ops() { |
| 347 | while (!event_queue.empty()) { |
| 348 | std::vector<uint8_t> event; |
| 349 | event_queue.front(event); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 350 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 364 | } else { |
| 365 | break; |
| 366 | } |
| 367 | } |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | // Discard Qualcomm ACL debugging |
| 371 | void BluetoothAidlTest::discard_qca_debugging() { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 372 | while (!acl_queue.empty()) { |
| 373 | std::vector<uint8_t> acl_packet; |
| 374 | acl_queue.front(acl_packet); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 375 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 381 | } else { |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // Receive an event, discarding NO-OPs. |
| 388 | void BluetoothAidlTest::wait_for_event(bool timeout_is_error = true) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 389 | // 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 402 | } |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 405 | void BluetoothAidlTest::wait_for_command_complete_event( |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 406 | OpCode opCode, std::vector<uint8_t>& complete_event) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 407 | ASSERT_NO_FATAL_FAILURE(wait_for_event()); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 408 | ASSERT_FALSE(event_queue.empty()); |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 409 | ASSERT_TRUE(event_queue.pop(complete_event)); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 410 | auto complete_view = ::bluetooth::hci::CommandCompleteView::Create( |
| 411 | ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>( |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 412 | std::make_shared<std::vector<uint8_t>>(complete_event)))); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 413 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 419 | void 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 426 | // Send the command to read the controller's buffer sizes. |
| 427 | void BluetoothAidlTest::setBufferSizes() { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 428 | std::vector<uint8_t> cmd; |
| 429 | ::bluetooth::packet::BitInserter bi{cmd}; |
| 430 | ::bluetooth::hci::ReadBufferSizeBuilder::Create()->Serialize(bi); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 431 | hci->sendHciCommand(cmd); |
| 432 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 433 | ASSERT_NO_FATAL_FAILURE(wait_for_event()); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 434 | std::vector<uint8_t> event; |
| 435 | ASSERT_TRUE(event_queue.pop(event)); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 436 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 441 | |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 442 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 448 | |
| 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 Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 456 | // Enable flow control packets for SCO |
| 457 | void BluetoothAidlTest::setSynchronousFlowControlEnable() { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 458 | 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 Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 463 | hci->sendHciCommand(cmd); |
| 464 | |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 465 | wait_and_validate_command_complete_event( |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 466 | ::bluetooth::hci::OpCode::WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | // Send an HCI command (in Loopback mode) and check the response. |
| 470 | void BluetoothAidlTest::sendAndCheckHci(int num_packets) { |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 471 | ThroughputLogger logger{__func__}; |
| 472 | size_t command_size = 0; |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 473 | char new_name[] = "John Jacob Jingleheimer Schmidt ___________________"; |
| 474 | size_t new_name_length = strlen(new_name); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 475 | for (int n = 0; n < num_packets; n++) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 476 | // The name to set is new_name |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 477 | std::array<uint8_t, 248> name_array{}; |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 478 | for (size_t i = 0; i < new_name_length; i++) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 479 | name_array[i] = new_name[i]; |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 480 | } |
| 481 | // And the packet number |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 482 | 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 Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 487 | } |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 488 | std::vector<uint8_t> write_name; |
| 489 | ::bluetooth::packet::BitInserter bi{write_name}; |
| 490 | ::bluetooth::hci::WriteLocalNameBuilder::Create(name_array)->Serialize(bi); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 491 | 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 Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 498 | 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 Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 505 | |
| 506 | if (n == num_packets - 1) { |
| 507 | command_size = write_name.size(); |
| 508 | } |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 509 | } |
| 510 | logger.setTotalBytes(command_size * num_packets * 2); |
| 511 | } |
| 512 | |
| 513 | // Send a SCO data packet (in Loopback mode) and check the response. |
| 514 | void BluetoothAidlTest::sendAndCheckSco(int num_packets, size_t size, |
| 515 | uint16_t handle) { |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 516 | ThroughputLogger logger{__func__}; |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 517 | for (int n = 0; n < num_packets; n++) { |
| 518 | // Send a SCO packet |
| 519 | std::vector<uint8_t> sco_packet; |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 520 | std::vector<uint8_t> payload; |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 521 | for (size_t i = 0; i < size; i++) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 522 | payload.push_back(static_cast<uint8_t>(i + n)); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 523 | } |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 524 | ::bluetooth::packet::BitInserter bi{sco_packet}; |
| 525 | ::bluetooth::hci::ScoBuilder::Create( |
| 526 | handle, ::bluetooth::hci::PacketStatusFlag::CORRECTLY_RECEIVED, payload) |
| 527 | ->Serialize(bi); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 528 | 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 Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 535 | ASSERT_EQ(sco_packet, sco_loopback); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 536 | } |
| 537 | logger.setTotalBytes(num_packets * size * 2); |
| 538 | } |
| 539 | |
| 540 | // Send an ACL data packet (in Loopback mode) and check the response. |
| 541 | void BluetoothAidlTest::sendAndCheckAcl(int num_packets, size_t size, |
| 542 | uint16_t handle) { |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 543 | ThroughputLogger logger{__func__}; |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 544 | for (int n = 0; n < num_packets; n++) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 545 | // Send an ACL packet with counting data |
| 546 | auto payload = std::make_unique<::bluetooth::packet::RawBuilder>(); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 547 | for (size_t i = 0; i < size; i++) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 548 | payload->AddOctets1(static_cast<uint8_t>(i + n)); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 549 | } |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 550 | 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 Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 557 | 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 Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 564 | ASSERT_EQ(acl_packet, acl_loopback); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 565 | } |
| 566 | logger.setTotalBytes(num_packets * size * 2); |
| 567 | } |
| 568 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 569 | // Return the number of completed packets reported by the controller. |
| 570 | int BluetoothAidlTest::wait_for_completed_packets_event(uint16_t handle) { |
| 571 | int packets_processed = 0; |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 572 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 581 | std::vector<uint8_t> event; |
| 582 | EXPECT_TRUE(event_queue.pop(event)); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 583 | 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 He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 591 | for (const auto& entry : completed_packets) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 592 | EXPECT_EQ(handle, entry.connection_handle_); |
| 593 | packets_processed += entry.host_num_of_completed_packets_; |
| 594 | } |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 595 | } |
| 596 | return packets_processed; |
| 597 | } |
| 598 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 599 | // Send local loopback command and initialize SCO and ACL handles. |
| 600 | void BluetoothAidlTest::enterLoopbackMode() { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 601 | 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 Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 606 | 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 Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 621 | 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 Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 625 | |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 626 | 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 Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 641 | } |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 642 | connection_event_count++; |
| 643 | } else { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 644 | 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 Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 650 | command_complete_received = true; |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 655 | void 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 Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 669 | // Empty test: Initialize()/Close() are called in SetUp()/TearDown(). |
| 670 | TEST_P(BluetoothAidlTest, InitializeAndClose) {} |
| 671 | |
| 672 | // Send an HCI Reset with sendHciCommand and wait for a command complete event. |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 673 | TEST_P(BluetoothAidlTest, HciReset) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 674 | std::vector<uint8_t> reset; |
| 675 | ::bluetooth::packet::BitInserter bi{reset}; |
| 676 | ::bluetooth::hci::ResetBuilder::Create()->Serialize(bi); |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 677 | hci->sendHciCommand(reset); |
| 678 | |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 679 | wait_and_validate_command_complete_event(::bluetooth::hci::OpCode::RESET); |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 680 | } |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 681 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 682 | // Read and check the HCI version of the controller. |
| 683 | TEST_P(BluetoothAidlTest, HciVersionTest) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 684 | std::vector<uint8_t> cmd; |
| 685 | ::bluetooth::packet::BitInserter bi{cmd}; |
| 686 | ::bluetooth::hci::ReadLocalVersionInformationBuilder::Create()->Serialize(bi); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 687 | hci->sendHciCommand(cmd); |
| 688 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 689 | ASSERT_NO_FATAL_FAILURE(wait_for_event()); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 690 | |
| 691 | std::vector<uint8_t> event; |
| 692 | ASSERT_TRUE(event_queue.pop(event)); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 693 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | // Send an unknown HCI command and wait for the error message. |
| 707 | TEST_P(BluetoothAidlTest, HciUnknownCommand) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 708 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 714 | hci->sendHciCommand(cmd); |
| 715 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 716 | ASSERT_NO_FATAL_FAILURE(wait_for_event()); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 717 | |
| 718 | std::vector<uint8_t> event; |
| 719 | ASSERT_TRUE(event_queue.pop(event)); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 720 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 724 | |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 725 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 746 | // Enter loopback mode, but don't send any packets. |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 747 | TEST_P(BluetoothAidlTest, WriteLoopbackMode) { enterLoopbackMode(); } |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 748 | |
| 749 | // Enter loopback mode and send a single command. |
| 750 | TEST_P(BluetoothAidlTest, LoopbackModeSingleCommand) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 751 | setBufferSizes(); |
| 752 | |
| 753 | enterLoopbackMode(); |
| 754 | |
| 755 | sendAndCheckHci(1); |
| 756 | } |
| 757 | |
| 758 | // Enter loopback mode and send a single SCO packet. |
| 759 | TEST_P(BluetoothAidlTest, LoopbackModeSingleSco) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 760 | 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. |
| 779 | TEST_P(BluetoothAidlTest, LoopbackModeSingleAcl) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 780 | 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. |
| 800 | TEST_P(BluetoothAidlTest, LoopbackModeCommandBandwidth) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 801 | setBufferSizes(); |
| 802 | |
| 803 | enterLoopbackMode(); |
| 804 | |
| 805 | sendAndCheckHci(kNumHciCommandsBandwidth); |
| 806 | } |
| 807 | |
| 808 | // Enter loopback mode and send SCO packets for bandwidth measurements. |
| 809 | TEST_P(BluetoothAidlTest, LoopbackModeScoBandwidth) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 810 | 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. |
| 830 | TEST_P(BluetoothAidlTest, LoopbackModeAclBandwidth) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 831 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 849 | // Set all bits in the event mask |
| 850 | TEST_P(BluetoothAidlTest, SetEventMask) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 851 | 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 He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 856 | wait_and_validate_command_complete_event( |
| 857 | ::bluetooth::hci::OpCode::SET_EVENT_MASK); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | // Set all bits in the LE event mask |
| 861 | TEST_P(BluetoothAidlTest, SetLeEventMask) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 862 | 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 He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 867 | wait_and_validate_command_complete_event( |
| 868 | ::bluetooth::hci::OpCode::LE_SET_EVENT_MASK); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Myles Watson | 0daa164 | 2023-01-26 15:58:28 -0800 | [diff] [blame] | 871 | // Call initialize twice, second one should fail. |
| 872 | TEST_P(BluetoothAidlTest, CallInitializeTwice) { |
| 873 | class SecondCb |
| 874 | : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks { |
| 875 | public: |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 876 | ndk::ScopedAStatus initializationComplete(Status status) override { |
Myles Watson | 0daa164 | 2023-01-26 15:58:28 -0800 | [diff] [blame] | 877 | EXPECT_EQ(status, Status::ALREADY_INITIALIZED); |
| 878 | init_promise.set_value(); |
| 879 | return ScopedAStatus::ok(); |
| 880 | }; |
| 881 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 882 | ndk::ScopedAStatus hciEventReceived( |
| 883 | const std::vector<uint8_t>& /*event*/) override { |
Myles Watson | 0daa164 | 2023-01-26 15:58:28 -0800 | [diff] [blame] | 884 | ADD_FAILURE(); |
| 885 | return ScopedAStatus::ok(); |
| 886 | }; |
| 887 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 888 | ndk::ScopedAStatus aclDataReceived( |
| 889 | const std::vector<uint8_t>& /*data*/) override { |
Myles Watson | 0daa164 | 2023-01-26 15:58:28 -0800 | [diff] [blame] | 890 | ADD_FAILURE(); |
| 891 | return ScopedAStatus::ok(); |
| 892 | }; |
| 893 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 894 | ndk::ScopedAStatus scoDataReceived( |
| 895 | const std::vector<uint8_t>& /*data*/) override { |
Myles Watson | 0daa164 | 2023-01-26 15:58:28 -0800 | [diff] [blame] | 896 | ADD_FAILURE(); |
| 897 | return ScopedAStatus::ok(); |
| 898 | }; |
| 899 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 900 | ndk::ScopedAStatus isoDataReceived( |
| 901 | const std::vector<uint8_t>& /*data*/) override { |
Myles Watson | 0daa164 | 2023-01-26 15:58:28 -0800 | [diff] [blame] | 902 | 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 | |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 917 | TEST_P(BluetoothAidlTest, Cdd_C_12_1_Bluetooth5Requirements) { |
| 918 | 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 Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 977 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothAidlTest); |
| 978 | INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothAidlTest, |
| 979 | testing::ValuesIn(android::getAidlHalInstanceNames( |
| 980 | IBluetoothHci::descriptor)), |
| 981 | android::PrintInstanceNameToString); |
| 982 | |
| 983 | int 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 | } |