Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #define LOG_TAG "bluetooth_hidl_hal_test" |
| 18 | #include <android-base/logging.h> |
| 19 | |
| 20 | #include <android/hardware/bluetooth/1.0/IBluetoothHci.h> |
| 21 | #include <android/hardware/bluetooth/1.0/IBluetoothHciCallbacks.h> |
| 22 | #include <android/hardware/bluetooth/1.0/types.h> |
| 23 | #include <hardware/bluetooth.h> |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <condition_variable> |
| 28 | #include <mutex> |
| 29 | #include <queue> |
| 30 | |
| 31 | using ::android::hardware::bluetooth::V1_0::IBluetoothHci; |
| 32 | using ::android::hardware::bluetooth::V1_0::IBluetoothHciCallbacks; |
| 33 | using ::android::hardware::bluetooth::V1_0::Status; |
| 34 | using ::android::hardware::hidl_vec; |
| 35 | using ::android::hardware::Return; |
| 36 | using ::android::hardware::Void; |
| 37 | using ::android::sp; |
| 38 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 39 | #define HCI_MINIMUM_HCI_VERSION 5 // Bluetooth Core Specification 3.0 + HS |
| 40 | #define HCI_MINIMUM_LMP_VERSION 5 // Bluetooth Core Specification 3.0 + HS |
| 41 | #define NUM_HCI_COMMANDS_BANDWIDTH 1000 |
| 42 | #define NUM_SCO_PACKETS_BANDWIDTH 1000 |
| 43 | #define NUM_ACL_PACKETS_BANDWIDTH 1000 |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 44 | #define WAIT_FOR_INIT_TIMEOUT std::chrono::milliseconds(2000) |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 45 | #define WAIT_FOR_HCI_EVENT_TIMEOUT std::chrono::milliseconds(2000) |
| 46 | #define WAIT_FOR_SCO_DATA_TIMEOUT std::chrono::milliseconds(1000) |
| 47 | #define WAIT_FOR_ACL_DATA_TIMEOUT std::chrono::milliseconds(1000) |
| 48 | |
| 49 | #define COMMAND_HCI_SHOULD_BE_UNKNOWN \ |
| 50 | { 0xff, 0x3B, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 } |
| 51 | #define COMMAND_HCI_READ_LOCAL_VERSION_INFORMATION \ |
| 52 | { 0x01, 0x10, 0x00 } |
| 53 | #define COMMAND_HCI_READ_BUFFER_SIZE \ |
| 54 | { 0x05, 0x10, 0x00 } |
| 55 | #define COMMAND_HCI_WRITE_LOOPBACK_MODE_LOCAL \ |
| 56 | { 0x02, 0x18, 0x01, 0x01 } |
| 57 | #define COMMAND_HCI_RESET \ |
| 58 | { 0x03, 0x0c, 0x00 } |
| 59 | #define COMMAND_HCI_WRITE_LOCAL_NAME \ |
| 60 | { 0x13, 0x0c, 0xf8 } |
| 61 | #define HCI_STATUS_SUCCESS 0x00 |
| 62 | #define HCI_STATUS_UNKNOWN_HCI_COMMAND 0x01 |
| 63 | |
| 64 | #define EVENT_CONNECTION_COMPLETE 0x03 |
| 65 | #define EVENT_COMMAND_COMPLETE 0x0e |
| 66 | #define EVENT_COMMAND_STATUS 0x0f |
| 67 | #define EVENT_NUMBER_OF_COMPLETED_PACKETS 0x13 |
| 68 | #define EVENT_LOOPBACK_COMMAND 0x19 |
| 69 | |
| 70 | #define EVENT_CODE_BYTE 0 |
| 71 | #define EVENT_LENGTH_BYTE 1 |
| 72 | #define EVENT_FIRST_PAYLOAD_BYTE 2 |
| 73 | #define EVENT_COMMAND_STATUS_STATUS_BYTE 2 |
| 74 | #define EVENT_COMMAND_STATUS_ALLOWED_PACKETS_BYTE 3 |
| 75 | #define EVENT_COMMAND_STATUS_OPCODE_LSBYTE 4 // Bytes 4 and 5 |
| 76 | #define EVENT_COMMAND_COMPLETE_ALLOWED_PACKETS_BYTE 2 |
| 77 | #define EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE 3 // Bytes 3 and 4 |
| 78 | #define EVENT_COMMAND_COMPLETE_STATUS_BYTE 5 |
| 79 | #define EVENT_COMMAND_COMPLETE_FIRST_PARAM_BYTE 6 |
| 80 | #define EVENT_LOCAL_HCI_VERSION_BYTE EVENT_COMMAND_COMPLETE_FIRST_PARAM_BYTE |
| 81 | #define EVENT_LOCAL_LMP_VERSION_BYTE EVENT_LOCAL_HCI_VERSION_BYTE + 3 |
| 82 | |
| 83 | #define EVENT_CONNECTION_COMPLETE_PARAM_LENGTH 11 |
| 84 | #define EVENT_CONNECTION_COMPLETE_TYPE 11 |
| 85 | #define EVENT_CONNECTION_COMPLETE_TYPE_SCO 0 |
| 86 | #define EVENT_CONNECTION_COMPLETE_TYPE_ACL 1 |
| 87 | #define EVENT_CONNECTION_COMPLETE_HANDLE_LSBYTE 3 |
| 88 | #define EVENT_COMMAND_STATUS_LENGTH 4 |
| 89 | |
| 90 | #define EVENT_NUMBER_OF_COMPLETED_PACKETS_NUM_HANDLES 2 |
| 91 | |
| 92 | #define ACL_BROADCAST_ACTIVE_SLAVE (0x1 << 4) |
| 93 | #define ACL_PACKET_BOUNDARY_COMPLETE (0x3 << 6) |
| 94 | |
| 95 | class ThroughputLogger { |
| 96 | public: |
| 97 | ThroughputLogger(std::string task) |
| 98 | : task_(task), start_time_(std::chrono::steady_clock::now()) {} |
| 99 | |
| 100 | ~ThroughputLogger() { |
| 101 | if (total_bytes_ == 0) return; |
| 102 | std::chrono::duration<double> duration = |
| 103 | std::chrono::steady_clock::now() - start_time_; |
| 104 | double s = duration.count(); |
| 105 | if (s == 0) return; |
| 106 | double rate_kb = (static_cast<double>(total_bytes_) / s) / 1024; |
| 107 | ALOGD("%s %.1f KB/s (%zu bytes in %.3fs)", task_.c_str(), rate_kb, |
| 108 | total_bytes_, s); |
| 109 | } |
| 110 | |
| 111 | void setTotalBytes(size_t total_bytes) { total_bytes_ = total_bytes; } |
| 112 | |
| 113 | private: |
| 114 | size_t total_bytes_; |
| 115 | std::string task_; |
| 116 | std::chrono::steady_clock::time_point start_time_; |
| 117 | }; |
| 118 | |
| 119 | // The main test class for Bluetooth HIDL HAL. |
| 120 | class BluetoothHidlTest : public ::testing::Test { |
| 121 | public: |
| 122 | virtual void SetUp() override { |
| 123 | // currently test passthrough mode only |
Chris Phoenix | b5558e3 | 2017-01-17 22:47:52 -0800 | [diff] [blame] | 124 | bluetooth = IBluetoothHci::getService(); |
Myles Watson | dbbdf3e | 2017-02-13 10:57:47 -0800 | [diff] [blame] | 125 | ASSERT_NE(bluetooth, nullptr); |
Myles Watson | bc7e587 | 2017-02-17 09:14:21 -0800 | [diff] [blame^] | 126 | ALOGI("%s: getService() for bluetooth is %s", __func__, |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 127 | bluetooth->isRemote() ? "remote" : "local"); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 128 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 129 | bluetooth_cb = new BluetoothHciCallbacks(*this); |
| 130 | ASSERT_NE(bluetooth_cb, nullptr); |
| 131 | |
| 132 | max_acl_data_packet_length = 0; |
| 133 | max_sco_data_packet_length = 0; |
| 134 | max_acl_data_packets = 0; |
| 135 | max_sco_data_packets = 0; |
| 136 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 137 | initialized = false; |
| 138 | initialized_count = 0; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 139 | event_count = 0; |
| 140 | acl_count = 0; |
| 141 | sco_count = 0; |
| 142 | event_cb_count = 0; |
| 143 | acl_cb_count = 0; |
| 144 | sco_cb_count = 0; |
| 145 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 146 | ASSERT_EQ(initialized, false); |
| 147 | bluetooth->initialize(bluetooth_cb); |
| 148 | |
| 149 | wait_for_init_callback(); |
| 150 | |
| 151 | ASSERT_EQ(initialized, true); |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | virtual void TearDown() override { |
| 155 | bluetooth->close(); |
| 156 | EXPECT_EQ(static_cast<size_t>(0), event_queue.size()); |
| 157 | EXPECT_EQ(static_cast<size_t>(0), sco_queue.size()); |
| 158 | EXPECT_EQ(static_cast<size_t>(0), acl_queue.size()); |
| 159 | } |
| 160 | |
| 161 | void setBufferSizes(); |
| 162 | |
| 163 | // Functions called from within tests in loopback mode |
| 164 | void sendAndCheckHCI(int num_packets); |
| 165 | void sendAndCheckSCO(int num_packets, size_t size, uint16_t handle); |
| 166 | void sendAndCheckACL(int num_packets, size_t size, uint16_t handle); |
| 167 | |
| 168 | // Helper functions to try to get a handle on verbosity |
| 169 | void enterLoopbackMode(std::vector<uint16_t>& sco_handles, |
| 170 | std::vector<uint16_t>& acl_handles); |
| 171 | void wait_for_command_complete_event(hidl_vec<uint8_t> cmd); |
| 172 | int wait_for_completed_packets_event(uint16_t handle); |
| 173 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 174 | // Inform the test about the initialization callback |
| 175 | inline void notify_initialized() { |
| 176 | std::unique_lock<std::mutex> lock(initialized_mutex); |
| 177 | initialized_count++; |
| 178 | initialized_condition.notify_one(); |
| 179 | } |
| 180 | |
| 181 | // Test code calls this function to wait for the init callback |
| 182 | inline void wait_for_init_callback() { |
| 183 | std::unique_lock<std::mutex> lock(initialized_mutex); |
| 184 | |
| 185 | auto start_time = std::chrono::steady_clock::now(); |
| 186 | while (initialized_count == 0) |
| 187 | if (initialized_condition.wait_until(lock, |
| 188 | start_time + WAIT_FOR_INIT_TIMEOUT) == |
| 189 | std::cv_status::timeout) |
| 190 | return; |
| 191 | initialized_count--; |
| 192 | } |
| 193 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 194 | // Inform the test about an event callback |
| 195 | inline void notify_event_received() { |
| 196 | std::unique_lock<std::mutex> lock(event_mutex); |
| 197 | event_count++; |
| 198 | event_condition.notify_one(); |
| 199 | } |
| 200 | |
| 201 | // Test code calls this function to wait for an event callback |
| 202 | inline void wait_for_event() { |
| 203 | std::unique_lock<std::mutex> lock(event_mutex); |
| 204 | |
| 205 | auto start_time = std::chrono::steady_clock::now(); |
| 206 | while (event_count == 0) |
| 207 | if (event_condition.wait_until(lock, |
| 208 | start_time + WAIT_FOR_HCI_EVENT_TIMEOUT) == |
| 209 | std::cv_status::timeout) |
| 210 | return; |
| 211 | event_count--; |
| 212 | } |
| 213 | |
| 214 | // Inform the test about an acl data callback |
| 215 | inline void notify_acl_data_received() { |
| 216 | std::unique_lock<std::mutex> lock(acl_mutex); |
| 217 | acl_count++; |
| 218 | acl_condition.notify_one(); |
| 219 | } |
| 220 | |
| 221 | // Test code calls this function to wait for an acl data callback |
| 222 | inline void wait_for_acl() { |
| 223 | std::unique_lock<std::mutex> lock(acl_mutex); |
| 224 | |
| 225 | while (acl_count == 0) |
| 226 | acl_condition.wait_until( |
| 227 | lock, std::chrono::steady_clock::now() + WAIT_FOR_ACL_DATA_TIMEOUT); |
| 228 | acl_count--; |
| 229 | } |
| 230 | |
| 231 | // Inform the test about a sco data callback |
| 232 | inline void notify_sco_data_received() { |
| 233 | std::unique_lock<std::mutex> lock(sco_mutex); |
| 234 | sco_count++; |
| 235 | sco_condition.notify_one(); |
| 236 | } |
| 237 | |
| 238 | // Test code calls this function to wait for a sco data callback |
| 239 | inline void wait_for_sco() { |
| 240 | std::unique_lock<std::mutex> lock(sco_mutex); |
| 241 | |
| 242 | while (sco_count == 0) |
| 243 | sco_condition.wait_until( |
| 244 | lock, std::chrono::steady_clock::now() + WAIT_FOR_SCO_DATA_TIMEOUT); |
| 245 | sco_count--; |
| 246 | } |
| 247 | |
| 248 | // A simple test implementation of BluetoothHciCallbacks. |
| 249 | class BluetoothHciCallbacks : public IBluetoothHciCallbacks { |
| 250 | BluetoothHidlTest& parent_; |
| 251 | |
| 252 | public: |
| 253 | BluetoothHciCallbacks(BluetoothHidlTest& parent) : parent_(parent){}; |
| 254 | |
| 255 | virtual ~BluetoothHciCallbacks() = default; |
| 256 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 257 | Return<void> initializationComplete(Status status) override { |
Myles Watson | bc7e587 | 2017-02-17 09:14:21 -0800 | [diff] [blame^] | 258 | parent_.initialized = (status == Status::SUCCESS); |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 259 | parent_.notify_initialized(); |
| 260 | ALOGV("%s (status = %d)", __func__, static_cast<int>(status)); |
| 261 | return Void(); |
| 262 | }; |
| 263 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 264 | Return<void> hciEventReceived( |
| 265 | const ::android::hardware::hidl_vec<uint8_t>& event) override { |
| 266 | parent_.event_cb_count++; |
| 267 | parent_.event_queue.push(event); |
| 268 | parent_.notify_event_received(); |
| 269 | ALOGV("Event received (length = %d)", static_cast<int>(event.size())); |
| 270 | return Void(); |
| 271 | }; |
| 272 | |
| 273 | Return<void> aclDataReceived( |
| 274 | const ::android::hardware::hidl_vec<uint8_t>& data) override { |
| 275 | parent_.acl_cb_count++; |
| 276 | parent_.acl_queue.push(data); |
| 277 | parent_.notify_acl_data_received(); |
| 278 | return Void(); |
| 279 | }; |
| 280 | |
| 281 | Return<void> scoDataReceived( |
| 282 | const ::android::hardware::hidl_vec<uint8_t>& data) override { |
| 283 | parent_.sco_cb_count++; |
| 284 | parent_.sco_queue.push(data); |
| 285 | parent_.notify_sco_data_received(); |
| 286 | return Void(); |
| 287 | }; |
| 288 | }; |
| 289 | |
| 290 | sp<IBluetoothHci> bluetooth; |
| 291 | sp<IBluetoothHciCallbacks> bluetooth_cb; |
| 292 | std::queue<hidl_vec<uint8_t>> event_queue; |
| 293 | std::queue<hidl_vec<uint8_t>> acl_queue; |
| 294 | std::queue<hidl_vec<uint8_t>> sco_queue; |
| 295 | |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 296 | bool initialized; |
| 297 | |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 298 | int event_cb_count; |
| 299 | int sco_cb_count; |
| 300 | int acl_cb_count; |
| 301 | |
| 302 | int max_acl_data_packet_length; |
| 303 | int max_sco_data_packet_length; |
| 304 | int max_acl_data_packets; |
| 305 | int max_sco_data_packets; |
| 306 | |
| 307 | private: |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 308 | std::mutex initialized_mutex; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 309 | std::mutex event_mutex; |
| 310 | std::mutex sco_mutex; |
| 311 | std::mutex acl_mutex; |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 312 | std::condition_variable initialized_condition; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 313 | std::condition_variable event_condition; |
| 314 | std::condition_variable sco_condition; |
| 315 | std::condition_variable acl_condition; |
Andre Eisenbach | 9041d97 | 2017-01-17 18:23:12 -0800 | [diff] [blame] | 316 | int initialized_count; |
Andre Eisenbach | 89ba528 | 2016-10-13 15:45:02 -0700 | [diff] [blame] | 317 | int event_count; |
| 318 | int sco_count; |
| 319 | int acl_count; |
| 320 | }; |
| 321 | |
| 322 | // A class for test environment setup (kept since this file is a template). |
| 323 | class BluetoothHidlEnvironment : public ::testing::Environment { |
| 324 | public: |
| 325 | virtual void SetUp() {} |
| 326 | virtual void TearDown() {} |
| 327 | |
| 328 | private: |
| 329 | }; |
| 330 | |
| 331 | // Receive and check status events until a COMMAND_COMPLETE is received. |
| 332 | void BluetoothHidlTest::wait_for_command_complete_event(hidl_vec<uint8_t> cmd) { |
| 333 | // Allow intermediate COMMAND_STATUS events |
| 334 | int status_event_count = 0; |
| 335 | hidl_vec<uint8_t> event; |
| 336 | do { |
| 337 | wait_for_event(); |
| 338 | EXPECT_LT(static_cast<size_t>(0), event_queue.size()); |
| 339 | if (event_queue.size() == 0) { |
| 340 | event.resize(0); |
| 341 | break; |
| 342 | } |
| 343 | event = event_queue.front(); |
| 344 | event_queue.pop(); |
| 345 | EXPECT_GT(event.size(), |
| 346 | static_cast<size_t>(EVENT_COMMAND_STATUS_OPCODE_LSBYTE + 1)); |
| 347 | if (event[EVENT_CODE_BYTE] == EVENT_COMMAND_STATUS) { |
| 348 | EXPECT_EQ(EVENT_COMMAND_STATUS_LENGTH, event[EVENT_LENGTH_BYTE]); |
| 349 | EXPECT_EQ(cmd[0], event[EVENT_COMMAND_STATUS_OPCODE_LSBYTE]); |
| 350 | EXPECT_EQ(cmd[1], event[EVENT_COMMAND_STATUS_OPCODE_LSBYTE + 1]); |
| 351 | EXPECT_EQ(event[EVENT_COMMAND_STATUS_STATUS_BYTE], HCI_STATUS_SUCCESS); |
| 352 | status_event_count++; |
| 353 | } |
| 354 | } while (event.size() > 0 && event[EVENT_CODE_BYTE] == EVENT_COMMAND_STATUS); |
| 355 | |
| 356 | EXPECT_GT(event.size(), |
| 357 | static_cast<size_t>(EVENT_COMMAND_COMPLETE_STATUS_BYTE)); |
| 358 | EXPECT_EQ(EVENT_COMMAND_COMPLETE, event[EVENT_CODE_BYTE]); |
| 359 | EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]); |
| 360 | EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]); |
| 361 | EXPECT_EQ(HCI_STATUS_SUCCESS, event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]); |
| 362 | } |
| 363 | |
| 364 | // Send the command to read the controller's buffer sizes. |
| 365 | void BluetoothHidlTest::setBufferSizes() { |
| 366 | hidl_vec<uint8_t> cmd = COMMAND_HCI_READ_BUFFER_SIZE; |
| 367 | bluetooth->sendHciCommand(cmd); |
| 368 | |
| 369 | wait_for_event(); |
| 370 | |
| 371 | EXPECT_LT(static_cast<size_t>(0), event_queue.size()); |
| 372 | if (event_queue.size() == 0) return; |
| 373 | |
| 374 | hidl_vec<uint8_t> event = event_queue.front(); |
| 375 | event_queue.pop(); |
| 376 | |
| 377 | EXPECT_EQ(EVENT_COMMAND_COMPLETE, event[EVENT_CODE_BYTE]); |
| 378 | EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]); |
| 379 | EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]); |
| 380 | EXPECT_EQ(HCI_STATUS_SUCCESS, event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]); |
| 381 | |
| 382 | max_acl_data_packet_length = |
| 383 | event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 1] + |
| 384 | (event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 2] << 8); |
| 385 | max_sco_data_packet_length = event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 3]; |
| 386 | max_acl_data_packets = event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 4] + |
| 387 | (event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 5] << 8); |
| 388 | max_sco_data_packets = event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 6] + |
| 389 | (event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 7] << 8); |
| 390 | |
| 391 | ALOGD("%s: ACL max %d num %d SCO max %d num %d", __func__, |
| 392 | static_cast<int>(max_acl_data_packet_length), |
| 393 | static_cast<int>(max_acl_data_packets), |
| 394 | static_cast<int>(max_sco_data_packet_length), |
| 395 | static_cast<int>(max_sco_data_packets)); |
| 396 | } |
| 397 | |
| 398 | // Send an HCI command (in Loopback mode) and check the response. |
| 399 | void BluetoothHidlTest::sendAndCheckHCI(int num_packets) { |
| 400 | ThroughputLogger logger = {__func__}; |
| 401 | for (int n = 0; n < num_packets; n++) { |
| 402 | // Send an HCI packet |
| 403 | std::vector<uint8_t> write_name = COMMAND_HCI_WRITE_LOCAL_NAME; |
| 404 | // With a name |
| 405 | char new_name[] = "John Jacob Jingleheimer Schmidt ___________________0"; |
| 406 | size_t new_name_length = strlen(new_name); |
| 407 | for (size_t i = 0; i < new_name_length; i++) |
| 408 | write_name.push_back(static_cast<uint8_t>(new_name[i])); |
| 409 | // And the packet number |
| 410 | { |
| 411 | size_t i = new_name_length - 1; |
| 412 | for (int digits = n; digits > 0; digits = digits / 10, i--) |
| 413 | write_name[i] = static_cast<uint8_t>('0' + digits % 10); |
| 414 | } |
| 415 | // And padding |
| 416 | for (size_t i = 0; i < 248 - new_name_length; i++) |
| 417 | write_name.push_back(static_cast<uint8_t>(0)); |
| 418 | |
| 419 | hidl_vec<uint8_t> cmd = write_name; |
| 420 | bluetooth->sendHciCommand(cmd); |
| 421 | |
| 422 | // Check the loopback of the HCI packet |
| 423 | wait_for_event(); |
| 424 | hidl_vec<uint8_t> event = event_queue.front(); |
| 425 | event_queue.pop(); |
| 426 | size_t compare_length = |
| 427 | (cmd.size() > static_cast<size_t>(0xff) ? static_cast<size_t>(0xff) |
| 428 | : cmd.size()); |
| 429 | EXPECT_GT(event.size(), compare_length + EVENT_FIRST_PAYLOAD_BYTE - 1); |
| 430 | |
| 431 | EXPECT_EQ(EVENT_LOOPBACK_COMMAND, event[EVENT_CODE_BYTE]); |
| 432 | EXPECT_EQ(compare_length, event[EVENT_LENGTH_BYTE]); |
| 433 | if (n == 0) logger.setTotalBytes(cmd.size() * num_packets * 2); |
| 434 | |
| 435 | for (size_t i = 0; i < compare_length; i++) |
| 436 | EXPECT_EQ(cmd[i], event[EVENT_FIRST_PAYLOAD_BYTE + i]); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // Send a SCO data packet (in Loopback mode) and check the response. |
| 441 | void BluetoothHidlTest::sendAndCheckSCO(int num_packets, size_t size, |
| 442 | uint16_t handle) { |
| 443 | ThroughputLogger logger = {__func__}; |
| 444 | for (int n = 0; n < num_packets; n++) { |
| 445 | // Send a SCO packet |
| 446 | hidl_vec<uint8_t> sco_packet; |
| 447 | std::vector<uint8_t> sco_vector; |
| 448 | sco_vector.push_back(static_cast<uint8_t>(handle & 0xff)); |
| 449 | sco_vector.push_back(static_cast<uint8_t>((handle & 0x0f00) >> 8)); |
| 450 | sco_vector.push_back(static_cast<uint8_t>(size & 0xff)); |
| 451 | sco_vector.push_back(static_cast<uint8_t>((size & 0xff00) >> 8)); |
| 452 | for (size_t i = 0; i < size; i++) { |
| 453 | sco_vector.push_back(static_cast<uint8_t>(i + n)); |
| 454 | } |
| 455 | sco_packet = sco_vector; |
| 456 | bluetooth->sendScoData(sco_vector); |
| 457 | |
| 458 | // Check the loopback of the SCO packet |
| 459 | wait_for_sco(); |
| 460 | hidl_vec<uint8_t> sco_loopback = sco_queue.front(); |
| 461 | sco_queue.pop(); |
| 462 | |
| 463 | EXPECT_EQ(sco_packet.size(), sco_loopback.size()); |
| 464 | size_t successful_bytes = 0; |
| 465 | |
| 466 | if (n == 0) logger.setTotalBytes(num_packets * size * 2); |
| 467 | |
| 468 | for (size_t i = 0; i < sco_packet.size(); i++) { |
| 469 | if (sco_packet[i] == sco_loopback[i]) { |
| 470 | successful_bytes = i; |
| 471 | } else { |
| 472 | ALOGE("Miscompare at %d (expected %x, got %x)", static_cast<int>(i), |
| 473 | sco_packet[i], sco_loopback[i]); |
| 474 | ALOGE("At %d (expected %x, got %x)", static_cast<int>(i + 1), |
| 475 | sco_packet[i + 1], sco_loopback[i + 1]); |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | EXPECT_EQ(sco_packet.size(), successful_bytes + 1); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | // Send an ACL data packet (in Loopback mode) and check the response. |
| 484 | void BluetoothHidlTest::sendAndCheckACL(int num_packets, size_t size, |
| 485 | uint16_t handle) { |
| 486 | ThroughputLogger logger = {__func__}; |
| 487 | for (int n = 0; n < num_packets; n++) { |
| 488 | // Send an ACL packet |
| 489 | hidl_vec<uint8_t> acl_packet; |
| 490 | std::vector<uint8_t> acl_vector; |
| 491 | acl_vector.push_back(static_cast<uint8_t>(handle & 0xff)); |
| 492 | acl_vector.push_back(static_cast<uint8_t>((handle & 0x0f00) >> 8) | |
| 493 | ACL_BROADCAST_ACTIVE_SLAVE | |
| 494 | ACL_PACKET_BOUNDARY_COMPLETE); |
| 495 | acl_vector.push_back(static_cast<uint8_t>(size & 0xff)); |
| 496 | acl_vector.push_back(static_cast<uint8_t>((size & 0xff00) >> 8)); |
| 497 | for (size_t i = 0; i < size; i++) { |
| 498 | acl_vector.push_back(static_cast<uint8_t>(i + n)); |
| 499 | } |
| 500 | acl_packet = acl_vector; |
| 501 | bluetooth->sendAclData(acl_vector); |
| 502 | |
| 503 | // Check the loopback of the ACL packet |
| 504 | wait_for_acl(); |
| 505 | hidl_vec<uint8_t> acl_loopback = acl_queue.front(); |
| 506 | acl_queue.pop(); |
| 507 | |
| 508 | EXPECT_EQ(acl_packet.size(), acl_loopback.size()); |
| 509 | size_t successful_bytes = 0; |
| 510 | |
| 511 | if (n == 0) logger.setTotalBytes(num_packets * size * 2); |
| 512 | |
| 513 | for (size_t i = 0; i < acl_packet.size(); i++) { |
| 514 | if (acl_packet[i] == acl_loopback[i]) { |
| 515 | successful_bytes = i; |
| 516 | } else { |
| 517 | ALOGE("Miscompare at %d (expected %x, got %x)", static_cast<int>(i), |
| 518 | acl_packet[i], acl_loopback[i]); |
| 519 | ALOGE("At %d (expected %x, got %x)", static_cast<int>(i + 1), |
| 520 | acl_packet[i + 1], acl_loopback[i + 1]); |
| 521 | break; |
| 522 | } |
| 523 | } |
| 524 | EXPECT_EQ(acl_packet.size(), successful_bytes + 1); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | // Return the number of completed packets reported by the controller. |
| 529 | int BluetoothHidlTest::wait_for_completed_packets_event(uint16_t handle) { |
| 530 | wait_for_event(); |
| 531 | int packets_processed = 0; |
| 532 | while (event_queue.size() > 0) { |
| 533 | hidl_vec<uint8_t> event = event_queue.front(); |
| 534 | event_queue.pop(); |
| 535 | |
| 536 | EXPECT_EQ(EVENT_NUMBER_OF_COMPLETED_PACKETS, event[EVENT_CODE_BYTE]); |
| 537 | EXPECT_EQ(1, event[EVENT_NUMBER_OF_COMPLETED_PACKETS_NUM_HANDLES]); |
| 538 | |
| 539 | uint16_t event_handle = event[3] + (event[4] << 8); |
| 540 | EXPECT_EQ(handle, event_handle); |
| 541 | |
| 542 | packets_processed += event[5] + (event[6] << 8); |
| 543 | } |
| 544 | return packets_processed; |
| 545 | } |
| 546 | |
| 547 | // Send local loopback command and initialize SCO and ACL handles. |
| 548 | void BluetoothHidlTest::enterLoopbackMode(std::vector<uint16_t>& sco_handles, |
| 549 | std::vector<uint16_t>& acl_handles) { |
| 550 | hidl_vec<uint8_t> cmd = COMMAND_HCI_WRITE_LOOPBACK_MODE_LOCAL; |
| 551 | bluetooth->sendHciCommand(cmd); |
| 552 | |
| 553 | // Receive connection complete events with data channels |
| 554 | int connection_event_count = 0; |
| 555 | hidl_vec<uint8_t> event; |
| 556 | do { |
| 557 | wait_for_event(); |
| 558 | event = event_queue.front(); |
| 559 | event_queue.pop(); |
| 560 | EXPECT_GT(event.size(), |
| 561 | static_cast<size_t>(EVENT_COMMAND_COMPLETE_STATUS_BYTE)); |
| 562 | if (event[EVENT_CODE_BYTE] == EVENT_CONNECTION_COMPLETE) { |
| 563 | EXPECT_GT(event.size(), |
| 564 | static_cast<size_t>(EVENT_CONNECTION_COMPLETE_TYPE)); |
| 565 | EXPECT_EQ(event[EVENT_LENGTH_BYTE], |
| 566 | EVENT_CONNECTION_COMPLETE_PARAM_LENGTH); |
| 567 | uint8_t connection_type = event[EVENT_CONNECTION_COMPLETE_TYPE]; |
| 568 | |
| 569 | EXPECT_TRUE(connection_type == EVENT_CONNECTION_COMPLETE_TYPE_SCO || |
| 570 | connection_type == EVENT_CONNECTION_COMPLETE_TYPE_ACL); |
| 571 | |
| 572 | // Save handles |
| 573 | uint16_t handle = event[EVENT_CONNECTION_COMPLETE_HANDLE_LSBYTE] | |
| 574 | event[EVENT_CONNECTION_COMPLETE_HANDLE_LSBYTE + 1] << 8; |
| 575 | if (connection_type == EVENT_CONNECTION_COMPLETE_TYPE_SCO) |
| 576 | sco_handles.push_back(handle); |
| 577 | else |
| 578 | acl_handles.push_back(handle); |
| 579 | |
| 580 | ALOGD("Connect complete type = %d handle = %d", |
| 581 | event[EVENT_CONNECTION_COMPLETE_TYPE], handle); |
| 582 | connection_event_count++; |
| 583 | } |
| 584 | } while (event[EVENT_CODE_BYTE] == EVENT_CONNECTION_COMPLETE); |
| 585 | |
| 586 | EXPECT_GT(connection_event_count, 0); |
| 587 | |
| 588 | EXPECT_EQ(EVENT_COMMAND_COMPLETE, event[EVENT_CODE_BYTE]); |
| 589 | EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]); |
| 590 | EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]); |
| 591 | EXPECT_EQ(HCI_STATUS_SUCCESS, event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]); |
| 592 | } |
| 593 | |
| 594 | // Empty test: Initialize()/Close() are called in SetUp()/TearDown(). |
| 595 | TEST_F(BluetoothHidlTest, InitializeAndClose) { } |
| 596 | |
| 597 | // Send an HCI Reset with sendHciCommand and wait for a command complete event. |
| 598 | TEST_F(BluetoothHidlTest, HciReset) { |
| 599 | hidl_vec<uint8_t> cmd = COMMAND_HCI_RESET; |
| 600 | bluetooth->sendHciCommand(cmd); |
| 601 | |
| 602 | wait_for_command_complete_event(cmd); |
| 603 | } |
| 604 | |
| 605 | // Read and check the HCI version of the controller. |
| 606 | TEST_F(BluetoothHidlTest, HciVersionTest) { |
| 607 | hidl_vec<uint8_t> cmd = COMMAND_HCI_READ_LOCAL_VERSION_INFORMATION; |
| 608 | bluetooth->sendHciCommand(cmd); |
| 609 | |
| 610 | wait_for_event(); |
| 611 | |
| 612 | hidl_vec<uint8_t> event = event_queue.front(); |
| 613 | event_queue.pop(); |
| 614 | EXPECT_GT(event.size(), static_cast<size_t>(EVENT_LOCAL_LMP_VERSION_BYTE)); |
| 615 | |
| 616 | EXPECT_EQ(EVENT_COMMAND_COMPLETE, event[EVENT_CODE_BYTE]); |
| 617 | EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]); |
| 618 | EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]); |
| 619 | EXPECT_EQ(HCI_STATUS_SUCCESS, event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]); |
| 620 | |
| 621 | EXPECT_LE(HCI_MINIMUM_HCI_VERSION, event[EVENT_LOCAL_HCI_VERSION_BYTE]); |
| 622 | EXPECT_LE(HCI_MINIMUM_LMP_VERSION, event[EVENT_LOCAL_LMP_VERSION_BYTE]); |
| 623 | } |
| 624 | |
| 625 | // Send an unknown HCI command and wait for the error message. |
| 626 | TEST_F(BluetoothHidlTest, HciUnknownCommand) { |
| 627 | hidl_vec<uint8_t> cmd = COMMAND_HCI_SHOULD_BE_UNKNOWN; |
| 628 | bluetooth->sendHciCommand(cmd); |
| 629 | |
| 630 | wait_for_event(); |
| 631 | |
| 632 | hidl_vec<uint8_t> event = event_queue.front(); |
| 633 | event_queue.pop(); |
| 634 | EXPECT_GT(event.size(), |
| 635 | static_cast<size_t>(EVENT_COMMAND_STATUS_OPCODE_LSBYTE + 1)); |
| 636 | |
| 637 | EXPECT_EQ(EVENT_COMMAND_COMPLETE, event[EVENT_CODE_BYTE]); |
| 638 | EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]); |
| 639 | EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]); |
| 640 | EXPECT_EQ(HCI_STATUS_UNKNOWN_HCI_COMMAND, |
| 641 | event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]); |
| 642 | } |
| 643 | |
| 644 | // Enter loopback mode, but don't send any packets. |
| 645 | TEST_F(BluetoothHidlTest, WriteLoopbackMode) { |
| 646 | std::vector<uint16_t> sco_connection_handles; |
| 647 | std::vector<uint16_t> acl_connection_handles; |
| 648 | enterLoopbackMode(sco_connection_handles, acl_connection_handles); |
| 649 | } |
| 650 | |
| 651 | // Enter loopback mode and send single packets. |
| 652 | TEST_F(BluetoothHidlTest, LoopbackModeSinglePackets) { |
| 653 | setBufferSizes(); |
| 654 | EXPECT_LT(0, max_sco_data_packet_length); |
| 655 | EXPECT_LT(0, max_acl_data_packet_length); |
| 656 | |
| 657 | std::vector<uint16_t> sco_connection_handles; |
| 658 | std::vector<uint16_t> acl_connection_handles; |
| 659 | enterLoopbackMode(sco_connection_handles, acl_connection_handles); |
| 660 | |
| 661 | sendAndCheckHCI(1); |
| 662 | |
| 663 | // This should work, but breaks on some current platforms. Figure out how to |
| 664 | // grandfather older devices but test new ones. |
| 665 | int sco_packets_sent = 0; |
| 666 | if (0 && sco_connection_handles.size() > 0) { |
| 667 | sendAndCheckSCO(1, max_sco_data_packet_length, sco_connection_handles[0]); |
| 668 | sco_packets_sent = 1; |
| 669 | EXPECT_EQ(sco_packets_sent, |
| 670 | wait_for_completed_packets_event(sco_connection_handles[0])); |
| 671 | } |
| 672 | |
| 673 | int acl_packets_sent = 0; |
| 674 | if (acl_connection_handles.size() > 0) { |
| 675 | sendAndCheckACL(1, max_acl_data_packet_length, acl_connection_handles[0]); |
| 676 | acl_packets_sent = 1; |
| 677 | EXPECT_EQ(acl_packets_sent, |
| 678 | wait_for_completed_packets_event(acl_connection_handles[0])); |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | // Enter loopback mode and send packets for bandwidth measurements. |
| 683 | TEST_F(BluetoothHidlTest, LoopbackModeBandwidth) { |
| 684 | setBufferSizes(); |
| 685 | |
| 686 | std::vector<uint16_t> sco_connection_handles; |
| 687 | std::vector<uint16_t> acl_connection_handles; |
| 688 | enterLoopbackMode(sco_connection_handles, acl_connection_handles); |
| 689 | |
| 690 | sendAndCheckHCI(NUM_HCI_COMMANDS_BANDWIDTH); |
| 691 | |
| 692 | // This should work, but breaks on some current platforms. Figure out how to |
| 693 | // grandfather older devices but test new ones. |
| 694 | int sco_packets_sent = 0; |
| 695 | if (0 && sco_connection_handles.size() > 0) { |
| 696 | sendAndCheckSCO(NUM_SCO_PACKETS_BANDWIDTH, max_sco_data_packet_length, |
| 697 | sco_connection_handles[0]); |
| 698 | sco_packets_sent = NUM_SCO_PACKETS_BANDWIDTH; |
| 699 | EXPECT_EQ(sco_packets_sent, |
| 700 | wait_for_completed_packets_event(sco_connection_handles[0])); |
| 701 | } |
| 702 | |
| 703 | int acl_packets_sent = 0; |
| 704 | if (acl_connection_handles.size() > 0) { |
| 705 | sendAndCheckACL(NUM_ACL_PACKETS_BANDWIDTH, max_acl_data_packet_length, |
| 706 | acl_connection_handles[0]); |
| 707 | acl_packets_sent = NUM_ACL_PACKETS_BANDWIDTH; |
| 708 | EXPECT_EQ(acl_packets_sent, |
| 709 | wait_for_completed_packets_event(acl_connection_handles[0])); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | int main(int argc, char** argv) { |
| 714 | ::testing::AddGlobalTestEnvironment(new BluetoothHidlEnvironment); |
| 715 | ::testing::InitGoogleTest(&argc, argv); |
| 716 | int status = RUN_ALL_TESTS(); |
| 717 | ALOGI("Test result = %d", status); |
| 718 | return status; |
| 719 | } |