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 | |
Treehugger Robot | 5e0277f | 2023-05-12 02:44:22 +0000 | [diff] [blame] | 17 | #include <VtsCoreUtil.h> |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 18 | #include <aidl/Gtest.h> |
| 19 | #include <aidl/Vintf.h> |
| 20 | #include <aidl/android/hardware/bluetooth/BnBluetoothHciCallbacks.h> |
| 21 | #include <aidl/android/hardware/bluetooth/IBluetoothHci.h> |
| 22 | #include <aidl/android/hardware/bluetooth/IBluetoothHciCallbacks.h> |
| 23 | #include <aidl/android/hardware/bluetooth/Status.h> |
Treehugger Robot | 5e0277f | 2023-05-12 02:44:22 +0000 | [diff] [blame] | 24 | #include <android-base/properties.h> |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 25 | #include <android/binder_auto_utils.h> |
| 26 | #include <android/binder_manager.h> |
| 27 | #include <android/binder_process.h> |
| 28 | #include <binder/IServiceManager.h> |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 29 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 30 | #include <chrono> |
| 31 | #include <condition_variable> |
| 32 | #include <future> |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 33 | #include <queue> |
| 34 | #include <thread> |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 35 | #include <utility> |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 36 | #include <vector> |
| 37 | |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 38 | // TODO: Remove custom logging defines from PDL packets. |
| 39 | #undef LOG_INFO |
| 40 | #undef LOG_DEBUG |
| 41 | #undef LOG_TAG |
| 42 | #define LOG_TAG "VtsHalBluetooth" |
| 43 | #include "hci/hci_packets.h" |
| 44 | #include "packet/raw_builder.h" |
| 45 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 46 | using aidl::android::hardware::bluetooth::IBluetoothHci; |
| 47 | using aidl::android::hardware::bluetooth::IBluetoothHciCallbacks; |
| 48 | using aidl::android::hardware::bluetooth::Status; |
| 49 | using ndk::ScopedAStatus; |
| 50 | using ndk::SpAIBinder; |
| 51 | |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 52 | using ::bluetooth::hci::CommandBuilder; |
| 53 | using ::bluetooth::hci::CommandCompleteView; |
| 54 | using ::bluetooth::hci::CommandView; |
| 55 | using ::bluetooth::hci::ErrorCode; |
| 56 | using ::bluetooth::hci::EventView; |
| 57 | using ::bluetooth::hci::LeReadLocalSupportedFeaturesBuilder; |
| 58 | using ::bluetooth::hci::LeReadLocalSupportedFeaturesCompleteView; |
| 59 | using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsBuilder; |
| 60 | using ::bluetooth::hci::LeReadNumberOfSupportedAdvertisingSetsCompleteView; |
| 61 | using ::bluetooth::hci::LeReadResolvingListSizeBuilder; |
| 62 | using ::bluetooth::hci::LeReadResolvingListSizeCompleteView; |
| 63 | using ::bluetooth::hci::LLFeaturesBits; |
| 64 | using ::bluetooth::hci::OpCode; |
| 65 | using ::bluetooth::hci::OpCodeText; |
| 66 | using ::bluetooth::hci::PacketView; |
| 67 | using ::bluetooth::hci::ReadLocalVersionInformationBuilder; |
| 68 | using ::bluetooth::hci::ReadLocalVersionInformationCompleteView; |
| 69 | |
Myles Watson | 864c904 | 2024-07-24 14:39:05 -0700 | [diff] [blame] | 70 | static constexpr uint8_t kMinLeAdvSetForBt5 = 10; |
| 71 | static constexpr uint8_t kMinLeAdvSetForBt5ForTv = 10; |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 72 | static constexpr uint8_t kMinLeResolvingListForBt5 = 8; |
| 73 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 74 | static constexpr size_t kNumHciCommandsBandwidth = 100; |
| 75 | static constexpr size_t kNumScoPacketsBandwidth = 100; |
| 76 | static constexpr size_t kNumAclPacketsBandwidth = 100; |
William Escande | ac0f34f | 2024-04-11 09:06:31 -0700 | [diff] [blame] | 77 | static constexpr std::chrono::milliseconds kWaitForInitTimeout(2000); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 78 | static constexpr std::chrono::milliseconds kWaitForHciEventTimeout(2000); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 79 | static constexpr std::chrono::milliseconds kWaitForScoDataTimeout(1000); |
| 80 | static constexpr std::chrono::milliseconds kWaitForAclDataTimeout(1000); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 81 | static constexpr std::chrono::milliseconds kInterfaceCloseDelayMs(200); |
| 82 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 83 | // To discard Qualcomm ACL debugging |
| 84 | static constexpr uint16_t kAclHandleQcaDebugMessage = 0xedc; |
| 85 | |
Treehugger Robot | 5e0277f | 2023-05-12 02:44:22 +0000 | [diff] [blame] | 86 | static int get_vsr_api_level() { |
| 87 | int vendor_api_level = |
| 88 | ::android::base::GetIntProperty("ro.vendor.api_level", -1); |
| 89 | if (vendor_api_level != -1) { |
| 90 | return vendor_api_level; |
| 91 | } |
| 92 | |
| 93 | // Android S and older devices do not define ro.vendor.api_level |
| 94 | vendor_api_level = ::android::base::GetIntProperty("ro.board.api_level", -1); |
| 95 | if (vendor_api_level == -1) { |
| 96 | vendor_api_level = |
| 97 | ::android::base::GetIntProperty("ro.board.first_api_level", -1); |
| 98 | } |
| 99 | |
| 100 | int product_api_level = |
| 101 | ::android::base::GetIntProperty("ro.product.first_api_level", -1); |
| 102 | if (product_api_level == -1) { |
| 103 | product_api_level = |
| 104 | ::android::base::GetIntProperty("ro.build.version.sdk", -1); |
| 105 | EXPECT_NE(product_api_level, -1) << "Could not find ro.build.version.sdk"; |
| 106 | } |
| 107 | |
| 108 | // VSR API level is the minimum of vendor_api_level and product_api_level. |
| 109 | if (vendor_api_level == -1 || vendor_api_level > product_api_level) { |
| 110 | return product_api_level; |
| 111 | } |
| 112 | return vendor_api_level; |
| 113 | } |
| 114 | |
| 115 | static bool isTv() { |
| 116 | return testing::deviceSupportsFeature("android.software.leanback") || |
| 117 | testing::deviceSupportsFeature("android.hardware.type.television"); |
| 118 | } |
| 119 | |
Doug Ferraz | 83b2267 | 2024-03-22 19:54:26 -0400 | [diff] [blame] | 120 | static bool isHandheld() { |
| 121 | return testing::deviceSupportsFeature("android.hardware.type.handheld"); |
| 122 | } |
| 123 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 124 | class ThroughputLogger { |
| 125 | public: |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 126 | explicit ThroughputLogger(std::string task) |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 127 | : total_bytes_(0), |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 128 | task_(std::move(task)), |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 129 | start_time_(std::chrono::steady_clock::now()) {} |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 130 | |
| 131 | ~ThroughputLogger() { |
| 132 | if (total_bytes_ == 0) { |
| 133 | return; |
| 134 | } |
| 135 | std::chrono::duration<double> duration = |
| 136 | std::chrono::steady_clock::now() - start_time_; |
| 137 | double s = duration.count(); |
| 138 | if (s == 0) { |
| 139 | return; |
| 140 | } |
| 141 | double rate_kb = (static_cast<double>(total_bytes_) / s) / 1024; |
| 142 | ALOGD("%s %.1f KB/s (%zu bytes in %.3fs)", task_.c_str(), rate_kb, |
| 143 | total_bytes_, s); |
| 144 | } |
| 145 | |
| 146 | void setTotalBytes(size_t total_bytes) { total_bytes_ = total_bytes; } |
| 147 | |
| 148 | private: |
| 149 | size_t total_bytes_; |
| 150 | std::string task_; |
| 151 | std::chrono::steady_clock::time_point start_time_; |
| 152 | }; |
| 153 | |
| 154 | // The main test class for Bluetooth HAL. |
| 155 | class BluetoothAidlTest : public ::testing::TestWithParam<std::string> { |
William Escande | 1a13990 | 2024-02-27 15:57:33 -0800 | [diff] [blame] | 156 | std::chrono::time_point<std::chrono::system_clock> |
| 157 | time_after_initialize_complete; |
| 158 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 159 | public: |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 160 | void SetUp() override { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 161 | // currently test passthrough mode only |
| 162 | hci = IBluetoothHci::fromBinder( |
| 163 | SpAIBinder(AServiceManager_waitForService(GetParam().c_str()))); |
| 164 | ASSERT_NE(hci, nullptr); |
| 165 | ALOGI("%s: getService() for bluetooth hci is %s", __func__, |
| 166 | hci->isRemote() ? "remote" : "local"); |
| 167 | |
| 168 | // Lambda function |
| 169 | auto on_binder_death = [](void* /*cookie*/) { FAIL(); }; |
| 170 | |
| 171 | bluetooth_hci_death_recipient = |
| 172 | AIBinder_DeathRecipient_new(on_binder_death); |
| 173 | ASSERT_NE(bluetooth_hci_death_recipient, nullptr); |
| 174 | ASSERT_EQ(STATUS_OK, |
| 175 | AIBinder_linkToDeath(hci->asBinder().get(), |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 176 | bluetooth_hci_death_recipient, nullptr)); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 177 | |
| 178 | hci_cb = ndk::SharedRefBase::make<BluetoothHciCallbacks>(*this); |
| 179 | ASSERT_NE(hci_cb, nullptr); |
| 180 | |
| 181 | max_acl_data_packet_length = 0; |
| 182 | max_sco_data_packet_length = 0; |
| 183 | max_acl_data_packets = 0; |
| 184 | max_sco_data_packets = 0; |
| 185 | |
| 186 | event_cb_count = 0; |
| 187 | acl_cb_count = 0; |
| 188 | sco_cb_count = 0; |
William Escande | 1a13990 | 2024-02-27 15:57:33 -0800 | [diff] [blame] | 189 | std::chrono::time_point<std::chrono::system_clock> |
| 190 | timeout_after_initialize = |
| 191 | std::chrono::system_clock::now() + kWaitForInitTimeout; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 192 | |
| 193 | ASSERT_TRUE(hci->initialize(hci_cb).isOk()); |
| 194 | auto future = initialized_promise.get_future(); |
| 195 | auto timeout_status = future.wait_for(kWaitForInitTimeout); |
| 196 | ASSERT_EQ(timeout_status, std::future_status::ready); |
| 197 | ASSERT_TRUE(future.get()); |
William Escande | 1a13990 | 2024-02-27 15:57:33 -0800 | [diff] [blame] | 198 | ASSERT_GE(timeout_after_initialize, time_after_initialize_complete); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 201 | void TearDown() override { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 202 | ALOGI("TearDown"); |
| 203 | // Should not be checked in production code |
| 204 | ASSERT_TRUE(hci->close().isOk()); |
| 205 | std::this_thread::sleep_for(kInterfaceCloseDelayMs); |
| 206 | handle_no_ops(); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 207 | discard_qca_debugging(); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 208 | EXPECT_EQ(static_cast<size_t>(0), event_queue.size()); |
| 209 | EXPECT_EQ(static_cast<size_t>(0), sco_queue.size()); |
| 210 | EXPECT_EQ(static_cast<size_t>(0), acl_queue.size()); |
| 211 | EXPECT_EQ(static_cast<size_t>(0), iso_queue.size()); |
| 212 | } |
| 213 | |
| 214 | void setBufferSizes(); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 215 | void setSynchronousFlowControlEnable(); |
| 216 | |
| 217 | // Functions called from within tests in loopback mode |
| 218 | void sendAndCheckHci(int num_packets); |
| 219 | void sendAndCheckSco(int num_packets, size_t size, uint16_t handle); |
| 220 | void sendAndCheckAcl(int num_packets, size_t size, uint16_t handle); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 221 | |
| 222 | // Helper functions to try to get a handle on verbosity |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 223 | void enterLoopbackMode(); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 224 | void handle_no_ops(); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 225 | void discard_qca_debugging(); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 226 | void wait_for_event(bool timeout_is_error); |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 227 | void wait_for_command_complete_event(OpCode opCode, |
| 228 | std::vector<uint8_t>& complete_event); |
| 229 | // Wait until a command complete is received. |
| 230 | // Command complete will be consumed after this method |
| 231 | void wait_and_validate_command_complete_event(OpCode opCode); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 232 | int wait_for_completed_packets_event(uint16_t handle); |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 233 | void send_and_wait_for_cmd_complete(std::unique_ptr<CommandBuilder> cmd, |
| 234 | std::vector<uint8_t>& cmd_complete); |
xiaoshun.xu | 4e85c09 | 2023-07-11 17:30:04 +0800 | [diff] [blame] | 235 | void reassemble_sco_loopback_pkt(std::vector<uint8_t>& scoPackets, |
| 236 | size_t size); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 237 | |
| 238 | // A simple test implementation of BluetoothHciCallbacks. |
| 239 | class BluetoothHciCallbacks |
| 240 | : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks { |
| 241 | BluetoothAidlTest& parent_; |
| 242 | |
| 243 | public: |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 244 | explicit BluetoothHciCallbacks(BluetoothAidlTest& parent) |
| 245 | : parent_(parent){}; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 246 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 247 | ~BluetoothHciCallbacks() override = default; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 248 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 249 | ndk::ScopedAStatus initializationComplete(Status status) override { |
William Escande | 1a13990 | 2024-02-27 15:57:33 -0800 | [diff] [blame] | 250 | if (status == Status::SUCCESS) { |
| 251 | parent_.time_after_initialize_complete = |
| 252 | std::chrono::system_clock::now(); |
| 253 | } |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 254 | parent_.initialized_promise.set_value(status == Status::SUCCESS); |
| 255 | ALOGV("%s (status = %d)", __func__, static_cast<int>(status)); |
| 256 | return ScopedAStatus::ok(); |
| 257 | }; |
| 258 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 259 | ndk::ScopedAStatus hciEventReceived( |
| 260 | const std::vector<uint8_t>& event) override { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 261 | parent_.event_cb_count++; |
| 262 | parent_.event_queue.push(event); |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 263 | ALOGI("Event received (length = %d)", static_cast<int>(event.size())); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 264 | return ScopedAStatus::ok(); |
| 265 | }; |
| 266 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 267 | ndk::ScopedAStatus aclDataReceived( |
| 268 | const std::vector<uint8_t>& data) override { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 269 | parent_.acl_cb_count++; |
| 270 | parent_.acl_queue.push(data); |
| 271 | return ScopedAStatus::ok(); |
| 272 | }; |
| 273 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 274 | ndk::ScopedAStatus scoDataReceived( |
| 275 | const std::vector<uint8_t>& data) override { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 276 | parent_.sco_cb_count++; |
| 277 | parent_.sco_queue.push(data); |
| 278 | return ScopedAStatus::ok(); |
| 279 | }; |
| 280 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 281 | ndk::ScopedAStatus isoDataReceived( |
| 282 | const std::vector<uint8_t>& data) override { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 283 | parent_.iso_cb_count++; |
| 284 | parent_.iso_queue.push(data); |
| 285 | return ScopedAStatus::ok(); |
| 286 | }; |
| 287 | }; |
| 288 | |
| 289 | template <class T> |
| 290 | class WaitQueue { |
| 291 | public: |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 292 | WaitQueue() = default; |
| 293 | ; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 294 | |
| 295 | virtual ~WaitQueue() = default; |
| 296 | |
| 297 | bool empty() const { |
| 298 | std::lock_guard<std::mutex> lock(m_); |
| 299 | return q_.empty(); |
| 300 | }; |
| 301 | |
| 302 | size_t size() const { |
| 303 | std::lock_guard<std::mutex> lock(m_); |
| 304 | return q_.size(); |
| 305 | }; |
| 306 | |
| 307 | void push(const T& v) { |
| 308 | std::lock_guard<std::mutex> lock(m_); |
| 309 | q_.push(v); |
| 310 | ready_.notify_one(); |
| 311 | }; |
| 312 | |
| 313 | bool pop(T& v) { |
| 314 | std::lock_guard<std::mutex> lock(m_); |
| 315 | if (q_.empty()) { |
| 316 | return false; |
| 317 | } |
| 318 | v = std::move(q_.front()); |
| 319 | q_.pop(); |
| 320 | return true; |
| 321 | }; |
| 322 | |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 323 | void pop() { |
| 324 | std::lock_guard<std::mutex> lock(m_); |
| 325 | if (q_.empty()) { |
| 326 | return; |
| 327 | } |
| 328 | q_.pop(); |
| 329 | }; |
| 330 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 331 | bool front(T& v) { |
| 332 | std::lock_guard<std::mutex> lock(m_); |
| 333 | if (q_.empty()) { |
| 334 | return false; |
| 335 | } |
| 336 | v = q_.front(); |
| 337 | return true; |
| 338 | }; |
| 339 | |
| 340 | void wait() { |
| 341 | std::unique_lock<std::mutex> lock(m_); |
| 342 | while (q_.empty()) { |
| 343 | ready_.wait(lock); |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | bool waitWithTimeout(std::chrono::milliseconds timeout) { |
| 348 | std::unique_lock<std::mutex> lock(m_); |
| 349 | while (q_.empty()) { |
| 350 | if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) { |
| 351 | return false; |
| 352 | } |
| 353 | } |
| 354 | return true; |
| 355 | }; |
| 356 | |
| 357 | bool tryPopWithTimeout(T& v, std::chrono::milliseconds timeout) { |
| 358 | std::unique_lock<std::mutex> lock(m_); |
| 359 | while (q_.empty()) { |
| 360 | if (ready_.wait_for(lock, timeout) == std::cv_status::timeout) { |
| 361 | return false; |
| 362 | } |
| 363 | } |
| 364 | v = std::move(q_.front()); |
| 365 | q_.pop(); |
| 366 | return true; |
| 367 | }; |
| 368 | |
| 369 | private: |
| 370 | mutable std::mutex m_; |
| 371 | std::queue<T> q_; |
| 372 | std::condition_variable_any ready_; |
| 373 | }; |
| 374 | |
| 375 | std::shared_ptr<IBluetoothHci> hci; |
| 376 | std::shared_ptr<BluetoothHciCallbacks> hci_cb; |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 377 | AIBinder_DeathRecipient* bluetooth_hci_death_recipient{}; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 378 | WaitQueue<std::vector<uint8_t>> event_queue; |
| 379 | WaitQueue<std::vector<uint8_t>> acl_queue; |
| 380 | WaitQueue<std::vector<uint8_t>> sco_queue; |
| 381 | WaitQueue<std::vector<uint8_t>> iso_queue; |
| 382 | |
| 383 | std::promise<bool> initialized_promise; |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 384 | int event_cb_count{}; |
| 385 | int sco_cb_count{}; |
| 386 | int acl_cb_count{}; |
| 387 | int iso_cb_count{}; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 388 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 389 | int max_acl_data_packet_length{}; |
| 390 | int max_sco_data_packet_length{}; |
| 391 | int max_acl_data_packets{}; |
| 392 | int max_sco_data_packets{}; |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 393 | |
| 394 | std::vector<uint16_t> sco_connection_handles; |
| 395 | std::vector<uint16_t> acl_connection_handles; |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 396 | }; |
| 397 | |
| 398 | // Discard NO-OPs from the event queue. |
| 399 | void BluetoothAidlTest::handle_no_ops() { |
| 400 | while (!event_queue.empty()) { |
| 401 | std::vector<uint8_t> event; |
| 402 | event_queue.front(event); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 403 | auto complete_view = ::bluetooth::hci::CommandCompleteView::Create( |
| 404 | ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>( |
| 405 | std::make_shared<std::vector<uint8_t>>(event)))); |
| 406 | auto status_view = ::bluetooth::hci::CommandCompleteView::Create( |
| 407 | ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>( |
| 408 | std::make_shared<std::vector<uint8_t>>(event)))); |
| 409 | bool is_complete_no_op = |
| 410 | complete_view.IsValid() && |
| 411 | complete_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE; |
| 412 | bool is_status_no_op = |
| 413 | status_view.IsValid() && |
| 414 | status_view.GetCommandOpCode() == ::bluetooth::hci::OpCode::NONE; |
| 415 | if (is_complete_no_op || is_status_no_op) { |
| 416 | event_queue.pop(); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 417 | } else { |
| 418 | break; |
| 419 | } |
| 420 | } |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | // Discard Qualcomm ACL debugging |
| 424 | void BluetoothAidlTest::discard_qca_debugging() { |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 425 | while (!acl_queue.empty()) { |
| 426 | std::vector<uint8_t> acl_packet; |
| 427 | acl_queue.front(acl_packet); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 428 | auto acl_view = |
| 429 | ::bluetooth::hci::AclView::Create(::bluetooth::hci::PacketView<true>( |
| 430 | std::make_shared<std::vector<uint8_t>>(acl_packet))); |
| 431 | EXPECT_TRUE(acl_view.IsValid()); |
| 432 | if (acl_view.GetHandle() == kAclHandleQcaDebugMessage) { |
| 433 | acl_queue.pop(); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 434 | } else { |
| 435 | break; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // Receive an event, discarding NO-OPs. |
| 441 | void BluetoothAidlTest::wait_for_event(bool timeout_is_error = true) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 442 | // Wait until we get something that's not a no-op. |
| 443 | while (true) { |
| 444 | bool event_ready = event_queue.waitWithTimeout(kWaitForHciEventTimeout); |
| 445 | ASSERT_TRUE(event_ready || !timeout_is_error); |
| 446 | if (event_queue.empty()) { |
| 447 | // waitWithTimeout timed out |
| 448 | return; |
| 449 | } |
| 450 | handle_no_ops(); |
| 451 | if (!event_queue.empty()) { |
| 452 | // There's an event in the queue that's not a no-op. |
| 453 | return; |
| 454 | } |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 455 | } |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 458 | void BluetoothAidlTest::wait_for_command_complete_event( |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 459 | OpCode opCode, std::vector<uint8_t>& complete_event) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 460 | ASSERT_NO_FATAL_FAILURE(wait_for_event()); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 461 | ASSERT_FALSE(event_queue.empty()); |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 462 | ASSERT_TRUE(event_queue.pop(complete_event)); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 463 | auto complete_view = ::bluetooth::hci::CommandCompleteView::Create( |
| 464 | ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>( |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 465 | std::make_shared<std::vector<uint8_t>>(complete_event)))); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 466 | ASSERT_TRUE(complete_view.IsValid()); |
| 467 | ASSERT_EQ(complete_view.GetCommandOpCode(), opCode); |
| 468 | ASSERT_EQ(complete_view.GetPayload()[0], |
| 469 | static_cast<uint8_t>(::bluetooth::hci::ErrorCode::SUCCESS)); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 472 | void BluetoothAidlTest::wait_and_validate_command_complete_event( |
| 473 | ::bluetooth::hci::OpCode opCode) { |
| 474 | std::vector<uint8_t> complete_event; |
| 475 | ASSERT_NO_FATAL_FAILURE( |
| 476 | wait_for_command_complete_event(opCode, complete_event)); |
| 477 | } |
| 478 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 479 | // Send the command to read the controller's buffer sizes. |
| 480 | void BluetoothAidlTest::setBufferSizes() { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 481 | std::vector<uint8_t> cmd; |
| 482 | ::bluetooth::packet::BitInserter bi{cmd}; |
| 483 | ::bluetooth::hci::ReadBufferSizeBuilder::Create()->Serialize(bi); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 484 | hci->sendHciCommand(cmd); |
| 485 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 486 | ASSERT_NO_FATAL_FAILURE(wait_for_event()); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 487 | std::vector<uint8_t> event; |
| 488 | ASSERT_TRUE(event_queue.pop(event)); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 489 | auto complete_view = ::bluetooth::hci::ReadBufferSizeCompleteView::Create( |
| 490 | ::bluetooth::hci::CommandCompleteView::Create( |
| 491 | ::bluetooth::hci::EventView::Create( |
| 492 | ::bluetooth::hci::PacketView<true>( |
| 493 | std::make_shared<std::vector<uint8_t>>(event))))); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 494 | |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 495 | ASSERT_TRUE(complete_view.IsValid()); |
| 496 | ASSERT_EQ(complete_view.GetStatus(), ::bluetooth::hci::ErrorCode::SUCCESS); |
| 497 | max_acl_data_packet_length = complete_view.GetAclDataPacketLength(); |
| 498 | max_sco_data_packet_length = complete_view.GetSynchronousDataPacketLength(); |
| 499 | max_acl_data_packets = complete_view.GetTotalNumAclDataPackets(); |
| 500 | max_sco_data_packets = complete_view.GetTotalNumSynchronousDataPackets(); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 501 | |
| 502 | ALOGD("%s: ACL max %d num %d SCO max %d num %d", __func__, |
| 503 | static_cast<int>(max_acl_data_packet_length), |
| 504 | static_cast<int>(max_acl_data_packets), |
| 505 | static_cast<int>(max_sco_data_packet_length), |
| 506 | static_cast<int>(max_sco_data_packets)); |
| 507 | } |
| 508 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 509 | // Enable flow control packets for SCO |
| 510 | void BluetoothAidlTest::setSynchronousFlowControlEnable() { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 511 | std::vector<uint8_t> cmd; |
| 512 | ::bluetooth::packet::BitInserter bi{cmd}; |
| 513 | ::bluetooth::hci::WriteSynchronousFlowControlEnableBuilder::Create( |
| 514 | ::bluetooth::hci::Enable::ENABLED) |
| 515 | ->Serialize(bi); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 516 | hci->sendHciCommand(cmd); |
| 517 | |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 518 | wait_and_validate_command_complete_event( |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 519 | ::bluetooth::hci::OpCode::WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | // Send an HCI command (in Loopback mode) and check the response. |
| 523 | void BluetoothAidlTest::sendAndCheckHci(int num_packets) { |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 524 | ThroughputLogger logger{__func__}; |
| 525 | size_t command_size = 0; |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 526 | char new_name[] = "John Jacob Jingleheimer Schmidt ___________________"; |
| 527 | size_t new_name_length = strlen(new_name); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 528 | for (int n = 0; n < num_packets; n++) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 529 | // The name to set is new_name |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 530 | std::array<uint8_t, 248> name_array{}; |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 531 | for (size_t i = 0; i < new_name_length; i++) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 532 | name_array[i] = new_name[i]; |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 533 | } |
| 534 | // And the packet number |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 535 | char number[11] = "0000000000"; |
| 536 | snprintf(number, sizeof(number), "%010d", static_cast<int>(n)); |
| 537 | for (size_t i = new_name_length; i < new_name_length + sizeof(number) - 1; |
| 538 | i++) { |
| 539 | name_array[new_name_length + i] = number[i]; |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 540 | } |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 541 | std::vector<uint8_t> write_name; |
| 542 | ::bluetooth::packet::BitInserter bi{write_name}; |
| 543 | ::bluetooth::hci::WriteLocalNameBuilder::Create(name_array)->Serialize(bi); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 544 | hci->sendHciCommand(write_name); |
| 545 | |
| 546 | // Check the loopback of the HCI packet |
| 547 | ASSERT_NO_FATAL_FAILURE(wait_for_event()); |
| 548 | |
| 549 | std::vector<uint8_t> event; |
| 550 | ASSERT_TRUE(event_queue.pop(event)); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 551 | auto event_view = ::bluetooth::hci::LoopbackCommandView::Create( |
| 552 | ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>( |
| 553 | std::make_shared<std::vector<uint8_t>>(event)))); |
| 554 | ASSERT_TRUE(event_view.IsValid()); |
| 555 | std::vector<uint8_t> looped_back_command{event_view.GetPayload().begin(), |
| 556 | event_view.GetPayload().end()}; |
| 557 | ASSERT_EQ(looped_back_command, write_name); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 558 | |
| 559 | if (n == num_packets - 1) { |
| 560 | command_size = write_name.size(); |
| 561 | } |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 562 | } |
| 563 | logger.setTotalBytes(command_size * num_packets * 2); |
| 564 | } |
| 565 | |
| 566 | // Send a SCO data packet (in Loopback mode) and check the response. |
| 567 | void BluetoothAidlTest::sendAndCheckSco(int num_packets, size_t size, |
| 568 | uint16_t handle) { |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 569 | ThroughputLogger logger{__func__}; |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 570 | for (int n = 0; n < num_packets; n++) { |
| 571 | // Send a SCO packet |
| 572 | std::vector<uint8_t> sco_packet; |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 573 | std::vector<uint8_t> payload; |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 574 | for (size_t i = 0; i < size; i++) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 575 | payload.push_back(static_cast<uint8_t>(i + n)); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 576 | } |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 577 | ::bluetooth::packet::BitInserter bi{sco_packet}; |
| 578 | ::bluetooth::hci::ScoBuilder::Create( |
| 579 | handle, ::bluetooth::hci::PacketStatusFlag::CORRECTLY_RECEIVED, payload) |
| 580 | ->Serialize(bi); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 581 | hci->sendScoData(sco_packet); |
| 582 | |
| 583 | // Check the loopback of the SCO packet |
| 584 | std::vector<uint8_t> sco_loopback; |
| 585 | ASSERT_TRUE( |
| 586 | sco_queue.tryPopWithTimeout(sco_loopback, kWaitForScoDataTimeout)); |
| 587 | |
xiaoshun.xu | 4e85c09 | 2023-07-11 17:30:04 +0800 | [diff] [blame] | 588 | if (sco_loopback.size() < size) { |
| 589 | // The packets may have been split for USB. Reassemble before checking. |
| 590 | reassemble_sco_loopback_pkt(sco_loopback, size); |
| 591 | } |
| 592 | |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 593 | ASSERT_EQ(sco_packet, sco_loopback); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 594 | } |
| 595 | logger.setTotalBytes(num_packets * size * 2); |
| 596 | } |
| 597 | |
| 598 | // Send an ACL data packet (in Loopback mode) and check the response. |
| 599 | void BluetoothAidlTest::sendAndCheckAcl(int num_packets, size_t size, |
| 600 | uint16_t handle) { |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 601 | ThroughputLogger logger{__func__}; |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 602 | for (int n = 0; n < num_packets; n++) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 603 | // Send an ACL packet with counting data |
| 604 | auto payload = std::make_unique<::bluetooth::packet::RawBuilder>(); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 605 | for (size_t i = 0; i < size; i++) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 606 | payload->AddOctets1(static_cast<uint8_t>(i + n)); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 607 | } |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 608 | std::vector<uint8_t> acl_packet; |
| 609 | ::bluetooth::packet::BitInserter bi{acl_packet}; |
| 610 | ::bluetooth::hci::AclBuilder::Create( |
| 611 | handle, |
| 612 | ::bluetooth::hci::PacketBoundaryFlag::FIRST_AUTOMATICALLY_FLUSHABLE, |
| 613 | ::bluetooth::hci::BroadcastFlag::POINT_TO_POINT, std::move(payload)) |
| 614 | ->Serialize(bi); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 615 | hci->sendAclData(acl_packet); |
| 616 | |
| 617 | std::vector<uint8_t> acl_loopback; |
| 618 | // Check the loopback of the ACL packet |
| 619 | ASSERT_TRUE( |
| 620 | acl_queue.tryPopWithTimeout(acl_loopback, kWaitForAclDataTimeout)); |
| 621 | |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 622 | ASSERT_EQ(acl_packet, acl_loopback); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 623 | } |
| 624 | logger.setTotalBytes(num_packets * size * 2); |
| 625 | } |
| 626 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 627 | // Return the number of completed packets reported by the controller. |
| 628 | int BluetoothAidlTest::wait_for_completed_packets_event(uint16_t handle) { |
| 629 | int packets_processed = 0; |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 630 | while (true) { |
| 631 | // There should be at least one event. |
| 632 | wait_for_event(packets_processed == 0); |
| 633 | if (event_queue.empty()) { |
| 634 | if (packets_processed == 0) { |
| 635 | ALOGW("%s: waitForBluetoothCallback timed out.", __func__); |
| 636 | } |
| 637 | return packets_processed; |
| 638 | } |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 639 | std::vector<uint8_t> event; |
| 640 | EXPECT_TRUE(event_queue.pop(event)); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 641 | auto event_view = ::bluetooth::hci::NumberOfCompletedPacketsView::Create( |
| 642 | ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>( |
| 643 | std::make_shared<std::vector<uint8_t>>(event)))); |
| 644 | if (!event_view.IsValid()) { |
| 645 | ADD_FAILURE(); |
| 646 | return packets_processed; |
| 647 | } |
| 648 | auto completed_packets = event_view.GetCompletedPackets(); |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 649 | for (const auto& entry : completed_packets) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 650 | EXPECT_EQ(handle, entry.connection_handle_); |
| 651 | packets_processed += entry.host_num_of_completed_packets_; |
| 652 | } |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 653 | } |
| 654 | return packets_processed; |
| 655 | } |
| 656 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 657 | // Send local loopback command and initialize SCO and ACL handles. |
| 658 | void BluetoothAidlTest::enterLoopbackMode() { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 659 | std::vector<uint8_t> cmd; |
| 660 | ::bluetooth::packet::BitInserter bi{cmd}; |
| 661 | ::bluetooth::hci::WriteLoopbackModeBuilder::Create( |
| 662 | bluetooth::hci::LoopbackMode::ENABLE_LOCAL) |
| 663 | ->Serialize(bi); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 664 | hci->sendHciCommand(cmd); |
| 665 | |
| 666 | // Receive connection complete events with data channels |
| 667 | int connection_event_count = 0; |
| 668 | bool command_complete_received = false; |
| 669 | while (true) { |
| 670 | wait_for_event(false); |
| 671 | if (event_queue.empty()) { |
| 672 | // Fail if there was no event received or no connections completed. |
| 673 | ASSERT_TRUE(command_complete_received); |
| 674 | ASSERT_LT(0, connection_event_count); |
| 675 | return; |
| 676 | } |
| 677 | std::vector<uint8_t> event; |
| 678 | ASSERT_TRUE(event_queue.pop(event)); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 679 | auto event_view = |
| 680 | ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>( |
| 681 | std::make_shared<std::vector<uint8_t>>(event))); |
| 682 | ASSERT_TRUE(event_view.IsValid()); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 683 | |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 684 | if (event_view.GetEventCode() == |
| 685 | ::bluetooth::hci::EventCode::CONNECTION_COMPLETE) { |
| 686 | auto complete_view = |
| 687 | ::bluetooth::hci::ConnectionCompleteView::Create(event_view); |
| 688 | ASSERT_TRUE(complete_view.IsValid()); |
| 689 | switch (complete_view.GetLinkType()) { |
| 690 | case ::bluetooth::hci::LinkType::ACL: |
| 691 | acl_connection_handles.push_back(complete_view.GetConnectionHandle()); |
| 692 | break; |
| 693 | case ::bluetooth::hci::LinkType::SCO: |
| 694 | sco_connection_handles.push_back(complete_view.GetConnectionHandle()); |
| 695 | break; |
| 696 | default: |
| 697 | ASSERT_EQ(complete_view.GetLinkType(), |
| 698 | ::bluetooth::hci::LinkType::ACL); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 699 | } |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 700 | connection_event_count++; |
| 701 | } else { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 702 | auto command_complete_view = |
| 703 | ::bluetooth::hci::WriteLoopbackModeCompleteView::Create( |
| 704 | ::bluetooth::hci::CommandCompleteView::Create(event_view)); |
| 705 | ASSERT_TRUE(command_complete_view.IsValid()); |
| 706 | ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, |
| 707 | command_complete_view.GetStatus()); |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 708 | command_complete_received = true; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 713 | void BluetoothAidlTest::send_and_wait_for_cmd_complete( |
| 714 | std::unique_ptr<CommandBuilder> cmd, std::vector<uint8_t>& cmd_complete) { |
| 715 | std::vector<uint8_t> cmd_bytes = cmd->SerializeToBytes(); |
| 716 | hci->sendHciCommand(cmd_bytes); |
| 717 | |
| 718 | auto view = CommandView::Create( |
| 719 | PacketView<true>(std::make_shared<std::vector<uint8_t>>(cmd_bytes))); |
| 720 | ASSERT_TRUE(view.IsValid()); |
| 721 | ALOGI("Waiting for %s[0x%x]", OpCodeText(view.GetOpCode()).c_str(), |
| 722 | static_cast<int>(view.GetOpCode())); |
| 723 | ASSERT_NO_FATAL_FAILURE( |
| 724 | wait_for_command_complete_event(view.GetOpCode(), cmd_complete)); |
| 725 | } |
| 726 | |
xiaoshun.xu | 4e85c09 | 2023-07-11 17:30:04 +0800 | [diff] [blame] | 727 | // Handle the loopback packet. |
| 728 | void BluetoothAidlTest::reassemble_sco_loopback_pkt(std::vector<uint8_t>& scoPackets, |
| 729 | size_t size) { |
| 730 | std::vector<uint8_t> sco_packet_whole; |
| 731 | sco_packet_whole.assign(scoPackets.begin(), scoPackets.end()); |
| 732 | while (size + 3 > sco_packet_whole.size()) { |
| 733 | std::vector<uint8_t> sco_packets; |
| 734 | ASSERT_TRUE( |
| 735 | sco_queue.tryPopWithTimeout(sco_packets, kWaitForScoDataTimeout)); |
| 736 | sco_packet_whole.insert(sco_packet_whole.end(), sco_packets.begin() + 3, |
| 737 | sco_packets.end()); |
| 738 | } |
| 739 | scoPackets.assign(sco_packet_whole.begin(), sco_packet_whole.end()); |
| 740 | scoPackets[2] = size; |
| 741 | } |
| 742 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 743 | // Empty test: Initialize()/Close() are called in SetUp()/TearDown(). |
| 744 | TEST_P(BluetoothAidlTest, InitializeAndClose) {} |
| 745 | |
| 746 | // 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] | 747 | TEST_P(BluetoothAidlTest, HciReset) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 748 | std::vector<uint8_t> reset; |
| 749 | ::bluetooth::packet::BitInserter bi{reset}; |
| 750 | ::bluetooth::hci::ResetBuilder::Create()->Serialize(bi); |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 751 | hci->sendHciCommand(reset); |
| 752 | |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 753 | wait_and_validate_command_complete_event(::bluetooth::hci::OpCode::RESET); |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 754 | } |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 755 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 756 | // Read and check the HCI version of the controller. |
| 757 | TEST_P(BluetoothAidlTest, HciVersionTest) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 758 | std::vector<uint8_t> cmd; |
| 759 | ::bluetooth::packet::BitInserter bi{cmd}; |
| 760 | ::bluetooth::hci::ReadLocalVersionInformationBuilder::Create()->Serialize(bi); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 761 | hci->sendHciCommand(cmd); |
| 762 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 763 | ASSERT_NO_FATAL_FAILURE(wait_for_event()); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 764 | |
| 765 | std::vector<uint8_t> event; |
| 766 | ASSERT_TRUE(event_queue.pop(event)); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 767 | auto complete_view = |
| 768 | ::bluetooth::hci::ReadLocalVersionInformationCompleteView::Create( |
| 769 | ::bluetooth::hci::CommandCompleteView::Create( |
| 770 | ::bluetooth::hci::EventView::Create( |
| 771 | ::bluetooth::hci::PacketView<true>( |
| 772 | std::make_shared<std::vector<uint8_t>>(event))))); |
| 773 | ASSERT_TRUE(complete_view.IsValid()); |
| 774 | ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, complete_view.GetStatus()); |
| 775 | auto version = complete_view.GetLocalVersionInformation(); |
| 776 | ASSERT_LE(::bluetooth::hci::HciVersion::V_3_0, version.hci_version_); |
| 777 | ASSERT_LE(::bluetooth::hci::LmpVersion::V_3_0, version.lmp_version_); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | // Send an unknown HCI command and wait for the error message. |
| 781 | TEST_P(BluetoothAidlTest, HciUnknownCommand) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 782 | std::vector<uint8_t> cmd; |
| 783 | ::bluetooth::packet::BitInserter bi{cmd}; |
| 784 | ::bluetooth::hci::CommandBuilder::Create( |
| 785 | static_cast<::bluetooth::hci::OpCode>(0x3cff), |
| 786 | std::make_unique<::bluetooth::packet::RawBuilder>()) |
| 787 | ->Serialize(bi); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 788 | hci->sendHciCommand(cmd); |
| 789 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 790 | ASSERT_NO_FATAL_FAILURE(wait_for_event()); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 791 | |
| 792 | std::vector<uint8_t> event; |
| 793 | ASSERT_TRUE(event_queue.pop(event)); |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 794 | auto event_view = |
| 795 | ::bluetooth::hci::EventView::Create(::bluetooth::hci::PacketView<true>( |
| 796 | std::make_shared<std::vector<uint8_t>>(event))); |
| 797 | ASSERT_TRUE(event_view.IsValid()); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 798 | |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 799 | switch (event_view.GetEventCode()) { |
| 800 | case ::bluetooth::hci::EventCode::COMMAND_COMPLETE: { |
| 801 | auto command_complete = |
| 802 | ::bluetooth::hci::CommandCompleteView::Create(event_view); |
| 803 | ASSERT_TRUE(command_complete.IsValid()); |
| 804 | ASSERT_EQ(command_complete.GetPayload()[0], |
| 805 | static_cast<uint8_t>( |
| 806 | ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND)); |
| 807 | } break; |
| 808 | case ::bluetooth::hci::EventCode::COMMAND_STATUS: { |
| 809 | auto command_status = |
| 810 | ::bluetooth::hci::CommandStatusView::Create(event_view); |
| 811 | ASSERT_TRUE(command_status.IsValid()); |
| 812 | ASSERT_EQ(command_status.GetStatus(), |
| 813 | ::bluetooth::hci::ErrorCode::UNKNOWN_HCI_COMMAND); |
| 814 | } break; |
| 815 | default: |
| 816 | ADD_FAILURE(); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 820 | // Enter loopback mode, but don't send any packets. |
Myles Watson | 65b47f5 | 2023-01-26 12:59:06 -0800 | [diff] [blame] | 821 | TEST_P(BluetoothAidlTest, WriteLoopbackMode) { enterLoopbackMode(); } |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 822 | |
| 823 | // Enter loopback mode and send a single command. |
| 824 | TEST_P(BluetoothAidlTest, LoopbackModeSingleCommand) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 825 | setBufferSizes(); |
| 826 | |
| 827 | enterLoopbackMode(); |
| 828 | |
| 829 | sendAndCheckHci(1); |
| 830 | } |
| 831 | |
| 832 | // Enter loopback mode and send a single SCO packet. |
| 833 | TEST_P(BluetoothAidlTest, LoopbackModeSingleSco) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 834 | setBufferSizes(); |
| 835 | setSynchronousFlowControlEnable(); |
| 836 | |
| 837 | enterLoopbackMode(); |
| 838 | |
| 839 | if (!sco_connection_handles.empty()) { |
| 840 | ASSERT_LT(0, max_sco_data_packet_length); |
| 841 | sendAndCheckSco(1, max_sco_data_packet_length, sco_connection_handles[0]); |
| 842 | int sco_packets_sent = 1; |
| 843 | int completed_packets = |
| 844 | wait_for_completed_packets_event(sco_connection_handles[0]); |
| 845 | if (sco_packets_sent != completed_packets) { |
| 846 | ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__, |
| 847 | sco_packets_sent, completed_packets); |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | // Enter loopback mode and send a single ACL packet. |
| 853 | TEST_P(BluetoothAidlTest, LoopbackModeSingleAcl) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 854 | setBufferSizes(); |
| 855 | |
| 856 | enterLoopbackMode(); |
| 857 | |
| 858 | if (!acl_connection_handles.empty()) { |
| 859 | ASSERT_LT(0, max_acl_data_packet_length); |
| 860 | sendAndCheckAcl(1, max_acl_data_packet_length - 1, |
| 861 | acl_connection_handles[0]); |
| 862 | int acl_packets_sent = 1; |
| 863 | int completed_packets = |
| 864 | wait_for_completed_packets_event(acl_connection_handles[0]); |
| 865 | if (acl_packets_sent != completed_packets) { |
| 866 | ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__, |
| 867 | acl_packets_sent, completed_packets); |
| 868 | } |
| 869 | } |
| 870 | ASSERT_GE(acl_cb_count, 1); |
| 871 | } |
| 872 | |
| 873 | // Enter loopback mode and send command packets for bandwidth measurements. |
| 874 | TEST_P(BluetoothAidlTest, LoopbackModeCommandBandwidth) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 875 | setBufferSizes(); |
| 876 | |
| 877 | enterLoopbackMode(); |
| 878 | |
| 879 | sendAndCheckHci(kNumHciCommandsBandwidth); |
| 880 | } |
| 881 | |
| 882 | // Enter loopback mode and send SCO packets for bandwidth measurements. |
| 883 | TEST_P(BluetoothAidlTest, LoopbackModeScoBandwidth) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 884 | setBufferSizes(); |
| 885 | setSynchronousFlowControlEnable(); |
| 886 | |
| 887 | enterLoopbackMode(); |
| 888 | |
| 889 | if (!sco_connection_handles.empty()) { |
| 890 | ASSERT_LT(0, max_sco_data_packet_length); |
| 891 | sendAndCheckSco(kNumScoPacketsBandwidth, max_sco_data_packet_length, |
| 892 | sco_connection_handles[0]); |
| 893 | int sco_packets_sent = kNumScoPacketsBandwidth; |
| 894 | int completed_packets = |
| 895 | wait_for_completed_packets_event(sco_connection_handles[0]); |
| 896 | if (sco_packets_sent != completed_packets) { |
| 897 | ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__, |
| 898 | sco_packets_sent, completed_packets); |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | // Enter loopback mode and send packets for ACL bandwidth measurements. |
| 904 | TEST_P(BluetoothAidlTest, LoopbackModeAclBandwidth) { |
Myles Watson | e1708c8 | 2023-01-18 17:07:58 -0800 | [diff] [blame] | 905 | setBufferSizes(); |
| 906 | |
| 907 | enterLoopbackMode(); |
| 908 | |
| 909 | if (!acl_connection_handles.empty()) { |
| 910 | ASSERT_LT(0, max_acl_data_packet_length); |
| 911 | sendAndCheckAcl(kNumAclPacketsBandwidth, max_acl_data_packet_length - 1, |
| 912 | acl_connection_handles[0]); |
| 913 | int acl_packets_sent = kNumAclPacketsBandwidth; |
| 914 | int completed_packets = |
| 915 | wait_for_completed_packets_event(acl_connection_handles[0]); |
| 916 | if (acl_packets_sent != completed_packets) { |
| 917 | ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__, |
| 918 | acl_packets_sent, completed_packets); |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 923 | // Set all bits in the event mask |
| 924 | TEST_P(BluetoothAidlTest, SetEventMask) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 925 | std::vector<uint8_t> cmd; |
| 926 | ::bluetooth::packet::BitInserter bi{cmd}; |
| 927 | uint64_t full_mask = UINT64_MAX; |
| 928 | ::bluetooth::hci::SetEventMaskBuilder::Create(full_mask)->Serialize(bi); |
| 929 | hci->sendHciCommand(cmd); |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 930 | wait_and_validate_command_complete_event( |
| 931 | ::bluetooth::hci::OpCode::SET_EVENT_MASK); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | // Set all bits in the LE event mask |
| 935 | TEST_P(BluetoothAidlTest, SetLeEventMask) { |
Myles Watson | 51b8bae | 2023-01-27 14:22:24 -0800 | [diff] [blame] | 936 | std::vector<uint8_t> cmd; |
| 937 | ::bluetooth::packet::BitInserter bi{cmd}; |
| 938 | uint64_t full_mask = UINT64_MAX; |
| 939 | ::bluetooth::hci::LeSetEventMaskBuilder::Create(full_mask)->Serialize(bi); |
| 940 | hci->sendHciCommand(cmd); |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 941 | wait_and_validate_command_complete_event( |
| 942 | ::bluetooth::hci::OpCode::LE_SET_EVENT_MASK); |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Myles Watson | 0daa164 | 2023-01-26 15:58:28 -0800 | [diff] [blame] | 945 | // Call initialize twice, second one should fail. |
| 946 | TEST_P(BluetoothAidlTest, CallInitializeTwice) { |
| 947 | class SecondCb |
| 948 | : public aidl::android::hardware::bluetooth::BnBluetoothHciCallbacks { |
| 949 | public: |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 950 | ndk::ScopedAStatus initializationComplete(Status status) override { |
Myles Watson | 0daa164 | 2023-01-26 15:58:28 -0800 | [diff] [blame] | 951 | EXPECT_EQ(status, Status::ALREADY_INITIALIZED); |
| 952 | init_promise.set_value(); |
| 953 | return ScopedAStatus::ok(); |
| 954 | }; |
| 955 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 956 | ndk::ScopedAStatus hciEventReceived( |
| 957 | const std::vector<uint8_t>& /*event*/) override { |
Myles Watson | 0daa164 | 2023-01-26 15:58:28 -0800 | [diff] [blame] | 958 | ADD_FAILURE(); |
| 959 | return ScopedAStatus::ok(); |
| 960 | }; |
| 961 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 962 | ndk::ScopedAStatus aclDataReceived( |
| 963 | const std::vector<uint8_t>& /*data*/) override { |
Myles Watson | 0daa164 | 2023-01-26 15:58:28 -0800 | [diff] [blame] | 964 | ADD_FAILURE(); |
| 965 | return ScopedAStatus::ok(); |
| 966 | }; |
| 967 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 968 | ndk::ScopedAStatus scoDataReceived( |
| 969 | const std::vector<uint8_t>& /*data*/) override { |
Myles Watson | 0daa164 | 2023-01-26 15:58:28 -0800 | [diff] [blame] | 970 | ADD_FAILURE(); |
| 971 | return ScopedAStatus::ok(); |
| 972 | }; |
| 973 | |
Jack He | dbceaca | 2023-03-27 18:06:38 -0700 | [diff] [blame] | 974 | ndk::ScopedAStatus isoDataReceived( |
| 975 | const std::vector<uint8_t>& /*data*/) override { |
Myles Watson | 0daa164 | 2023-01-26 15:58:28 -0800 | [diff] [blame] | 976 | ADD_FAILURE(); |
| 977 | return ScopedAStatus::ok(); |
| 978 | }; |
| 979 | std::promise<void> init_promise; |
| 980 | }; |
| 981 | |
| 982 | std::shared_ptr<SecondCb> second_cb = ndk::SharedRefBase::make<SecondCb>(); |
| 983 | ASSERT_NE(second_cb, nullptr); |
| 984 | |
| 985 | auto future = second_cb->init_promise.get_future(); |
| 986 | ASSERT_TRUE(hci->initialize(second_cb).isOk()); |
| 987 | auto status = future.wait_for(std::chrono::seconds(1)); |
| 988 | ASSERT_EQ(status, std::future_status::ready); |
| 989 | } |
| 990 | |
Myles Watson | 5bfb3a7 | 2023-05-10 14:06:32 -0700 | [diff] [blame] | 991 | TEST_P(BluetoothAidlTest, Vsr_Bluetooth5Requirements) { |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 992 | std::vector<uint8_t> version_event; |
| 993 | send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(), |
| 994 | version_event); |
| 995 | auto version_view = ReadLocalVersionInformationCompleteView::Create( |
| 996 | CommandCompleteView::Create(EventView::Create(PacketView<true>( |
| 997 | std::make_shared<std::vector<uint8_t>>(version_event))))); |
| 998 | ASSERT_TRUE(version_view.IsValid()); |
| 999 | ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus()); |
| 1000 | auto version = version_view.GetLocalVersionInformation(); |
| 1001 | if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) { |
| 1002 | // This test does not apply to controllers below 5.0 |
| 1003 | return; |
| 1004 | }; |
| 1005 | // When HCI version is 5.0, LMP version must also be at least 5.0 |
| 1006 | ASSERT_GE(static_cast<int>(version.lmp_version_), |
| 1007 | static_cast<int>(version.hci_version_)); |
| 1008 | |
| 1009 | std::vector<uint8_t> le_features_event; |
| 1010 | send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(), |
| 1011 | le_features_event); |
| 1012 | auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create( |
| 1013 | CommandCompleteView::Create(EventView::Create(PacketView<true>( |
| 1014 | std::make_shared<std::vector<uint8_t>>(le_features_event))))); |
| 1015 | ASSERT_TRUE(le_features_view.IsValid()); |
| 1016 | ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus()); |
| 1017 | auto le_features = le_features_view.GetLeFeatures(); |
| 1018 | ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LL_PRIVACY)); |
| 1019 | ASSERT_TRUE(le_features & static_cast<uint64_t>(LLFeaturesBits::LE_2M_PHY)); |
| 1020 | ASSERT_TRUE(le_features & |
| 1021 | static_cast<uint64_t>(LLFeaturesBits::LE_CODED_PHY)); |
| 1022 | ASSERT_TRUE(le_features & |
| 1023 | static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING)); |
| 1024 | |
| 1025 | std::vector<uint8_t> num_adv_set_event; |
| 1026 | send_and_wait_for_cmd_complete( |
| 1027 | LeReadNumberOfSupportedAdvertisingSetsBuilder::Create(), |
| 1028 | num_adv_set_event); |
| 1029 | auto num_adv_set_view = |
| 1030 | LeReadNumberOfSupportedAdvertisingSetsCompleteView::Create( |
| 1031 | CommandCompleteView::Create(EventView::Create(PacketView<true>( |
| 1032 | std::make_shared<std::vector<uint8_t>>(num_adv_set_event))))); |
| 1033 | ASSERT_TRUE(num_adv_set_view.IsValid()); |
| 1034 | ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, num_adv_set_view.GetStatus()); |
| 1035 | auto num_adv_set = num_adv_set_view.GetNumberSupportedAdvertisingSets(); |
Treehugger Robot | 5e0277f | 2023-05-12 02:44:22 +0000 | [diff] [blame] | 1036 | |
| 1037 | if (isTv() && get_vsr_api_level() == __ANDROID_API_U__) { |
Myles Watson | 864c904 | 2024-07-24 14:39:05 -0700 | [diff] [blame] | 1038 | ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5ForTv); |
Treehugger Robot | 5e0277f | 2023-05-12 02:44:22 +0000 | [diff] [blame] | 1039 | } else { |
| 1040 | ASSERT_GE(num_adv_set, kMinLeAdvSetForBt5); |
| 1041 | } |
Jack He | 2d2f3c2 | 2023-03-27 03:04:52 -0700 | [diff] [blame] | 1042 | |
| 1043 | std::vector<uint8_t> num_resolving_list_event; |
| 1044 | send_and_wait_for_cmd_complete(LeReadResolvingListSizeBuilder::Create(), |
| 1045 | num_resolving_list_event); |
| 1046 | auto num_resolving_list_view = LeReadResolvingListSizeCompleteView::Create( |
| 1047 | CommandCompleteView::Create(EventView::Create(PacketView<true>( |
| 1048 | std::make_shared<std::vector<uint8_t>>(num_resolving_list_event))))); |
| 1049 | ASSERT_TRUE(num_resolving_list_view.IsValid()); |
| 1050 | ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, |
| 1051 | num_resolving_list_view.GetStatus()); |
| 1052 | auto num_resolving_list = num_resolving_list_view.GetResolvingListSize(); |
| 1053 | ASSERT_GE(num_resolving_list, kMinLeResolvingListForBt5); |
| 1054 | } |
| 1055 | |
Doug Ferraz | 83b2267 | 2024-03-22 19:54:26 -0400 | [diff] [blame] | 1056 | /** |
Doug Ferraz | 83b2267 | 2024-03-22 19:54:26 -0400 | [diff] [blame] | 1057 | * VSR-5.3.14-007 MUST support Bluetooth 4.2 and Bluetooth LE Data Length Extension. |
| 1058 | * VSR-5.3.14-008 MUST support Bluetooth Low Energy (BLE). |
| 1059 | */ |
Doug Ferraz | eff7b91 | 2024-03-25 20:04:19 -0400 | [diff] [blame] | 1060 | // @VsrTest = 5.3.14-007 |
| 1061 | // @VsrTest = 5.3.14-008 |
Doug Ferraz | 83b2267 | 2024-03-22 19:54:26 -0400 | [diff] [blame] | 1062 | TEST_P(BluetoothAidlTest, Vsr_Bluetooth4_2Requirements) { |
| 1063 | // test only applies to handheld devices |
| 1064 | if (!isHandheld()) { |
| 1065 | return; |
| 1066 | } |
| 1067 | |
| 1068 | std::vector<uint8_t> version_event; |
| 1069 | send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(), |
| 1070 | version_event); |
| 1071 | auto version_view = ReadLocalVersionInformationCompleteView::Create( |
| 1072 | CommandCompleteView::Create(EventView::Create(PacketView<true>( |
| 1073 | std::make_shared<std::vector<uint8_t>>(version_event))))); |
| 1074 | ASSERT_TRUE(version_view.IsValid()); |
| 1075 | ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus()); |
| 1076 | auto version = version_view.GetLocalVersionInformation(); |
| 1077 | // Starting with Android 15, Fails when HCI version is lower than 4.2. |
| 1078 | ASSERT_GE(static_cast<int>(version.hci_version_), |
| 1079 | static_cast<int>(::bluetooth::hci::HciVersion::V_4_2)); |
| 1080 | ASSERT_GE(static_cast<int>(version.lmp_version_), |
| 1081 | static_cast<int>(::bluetooth::hci::LmpVersion::V_4_2)); |
| 1082 | |
| 1083 | std::vector<uint8_t> le_features_event; |
| 1084 | send_and_wait_for_cmd_complete(LeReadLocalSupportedFeaturesBuilder::Create(), |
| 1085 | le_features_event); |
| 1086 | auto le_features_view = LeReadLocalSupportedFeaturesCompleteView::Create( |
| 1087 | CommandCompleteView::Create(EventView::Create(PacketView<true>( |
| 1088 | std::make_shared<std::vector<uint8_t>>(le_features_event))))); |
| 1089 | ASSERT_TRUE(le_features_view.IsValid()); |
| 1090 | ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, le_features_view.GetStatus()); |
| 1091 | auto le_features = le_features_view.GetLeFeatures(); |
| 1092 | ASSERT_TRUE(le_features & |
| 1093 | static_cast<uint64_t>(LLFeaturesBits::LE_EXTENDED_ADVERTISING)); |
| 1094 | |
| 1095 | } |
| 1096 | |
Omair Kamil | 84a95be | 2024-04-22 13:29:19 -0700 | [diff] [blame] | 1097 | /** |
| 1098 | * VSR-5.3.14-012 MUST support at least eight LE concurrent connections with |
| 1099 | * three in peripheral role. |
| 1100 | */ |
| 1101 | // @VsrTest = 5.3.14-012 |
| 1102 | TEST_P(BluetoothAidlTest, Vsr_BlE_Connection_Requirement) { |
| 1103 | std::vector<uint8_t> version_event; |
| 1104 | send_and_wait_for_cmd_complete(ReadLocalVersionInformationBuilder::Create(), |
| 1105 | version_event); |
| 1106 | auto version_view = ReadLocalVersionInformationCompleteView::Create( |
| 1107 | CommandCompleteView::Create(EventView::Create(PacketView<true>( |
| 1108 | std::make_shared<std::vector<uint8_t>>(version_event))))); |
| 1109 | ASSERT_TRUE(version_view.IsValid()); |
| 1110 | ASSERT_EQ(::bluetooth::hci::ErrorCode::SUCCESS, version_view.GetStatus()); |
| 1111 | auto version = version_view.GetLocalVersionInformation(); |
| 1112 | if (version.hci_version_ < ::bluetooth::hci::HciVersion::V_5_0) { |
| 1113 | // This test does not apply to controllers below 5.0 |
| 1114 | return; |
| 1115 | }; |
| 1116 | |
| 1117 | int max_connections = ::android::base::GetIntProperty( |
| 1118 | "bluetooth.core.le.max_number_of_concurrent_connections", -1); |
| 1119 | if (max_connections == -1) { |
| 1120 | // With the property not set the default minimum of 8 will be used |
| 1121 | ALOGI("Max number of LE concurrent connections isn't set"); |
| 1122 | return; |
| 1123 | } |
| 1124 | ALOGI("Max number of LE concurrent connections = %d", max_connections); |
| 1125 | ASSERT_GE(max_connections, 8); |
| 1126 | } |
| 1127 | |
Dylan Tian | 8a6e09c | 2022-08-16 07:29:28 +0000 | [diff] [blame] | 1128 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothAidlTest); |
| 1129 | INSTANTIATE_TEST_SUITE_P(PerInstance, BluetoothAidlTest, |
| 1130 | testing::ValuesIn(android::getAidlHalInstanceNames( |
| 1131 | IBluetoothHci::descriptor)), |
| 1132 | android::PrintInstanceNameToString); |
| 1133 | |
| 1134 | int main(int argc, char** argv) { |
| 1135 | ABinderProcess_startThreadPool(); |
| 1136 | ::testing::InitGoogleTest(&argc, argv); |
| 1137 | int status = RUN_ALL_TESTS(); |
| 1138 | ALOGI("Test result = %d", status); |
| 1139 | return status; |
| 1140 | } |