1 /*
2 * Copyright (C) 2019 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/types.h>
21 #include <android/hardware/bluetooth/1.1/IBluetoothHci.h>
22 #include <android/hardware/bluetooth/1.1/IBluetoothHciCallbacks.h>
23 #include <hardware/bluetooth.h>
24 #include <utils/Log.h>
25
26 #include <VtsHalHidlTargetCallbackBase.h>
27 #include <gtest/gtest.h>
28 #include <hidl/GtestPrinter.h>
29 #include <hidl/ServiceManagement.h>
30
31 #include <chrono>
32 #include <queue>
33 #include <thread>
34
35 using ::android::sp;
36 using ::android::hardware::hidl_death_recipient;
37 using ::android::hardware::hidl_vec;
38 using ::android::hardware::Return;
39 using ::android::hardware::Void;
40 using ::android::hardware::bluetooth::V1_0::Status;
41 using ::android::hardware::bluetooth::V1_1::IBluetoothHci;
42 using ::android::hardware::bluetooth::V1_1::IBluetoothHciCallbacks;
43
44 #define HCI_MINIMUM_HCI_VERSION 5 // Bluetooth Core Specification 3.0 + HS
45 #define HCI_MINIMUM_LMP_VERSION 5 // Bluetooth Core Specification 3.0 + HS
46 #define HCI_BLUETOOTH4_2_HCI_VERSION 8 // Bluetooth 4.2
47 #define HCI_BLUETOOTH4_2_LMP_VERSION 8 // Bluetooth 4.2
48 #define NUM_HCI_COMMANDS_BANDWIDTH 1000
49 #define NUM_SCO_PACKETS_BANDWIDTH 1000
50 #define NUM_ACL_PACKETS_BANDWIDTH 1000
51 #define WAIT_FOR_INIT_TIMEOUT std::chrono::milliseconds(2000)
52 #define WAIT_FOR_HCI_EVENT_TIMEOUT std::chrono::milliseconds(2000)
53 #define WAIT_FOR_SCO_DATA_TIMEOUT std::chrono::milliseconds(1000)
54 #define WAIT_FOR_ACL_DATA_TIMEOUT std::chrono::milliseconds(1000)
55 #define INTERFACE_CLOSE_DELAY_MS std::chrono::milliseconds(200)
56
57 // { OCF, OGF << 2, Length of bytes of command parameters }
58 #define COMMAND_HCI_SHOULD_BE_UNKNOWN \
59 { 0xff, 0x3B, 0x08, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }
60 #define COMMAND_HCI_READ_LOCAL_VERSION_INFORMATION \
61 { 0x01, 0x10, 0x00 }
62 #define COMMAND_HCI_READ_BUFFER_SIZE \
63 { 0x05, 0x10, 0x00 }
64 #define COMMAND_HCI_WRITE_LOOPBACK_MODE_LOCAL \
65 { 0x02, 0x18, 0x01, 0x01 }
66 #define COMMAND_HCI_RESET \
67 { 0x03, 0x0c, 0x00 }
68 #define COMMAND_HCI_WRITE_LOCAL_NAME \
69 { 0x13, 0x0c, 0xf8 }
70 #define COMMAND_HCI_READ_LOCAL_SUPPORTED_FEATURES \
71 { 0x03, 0x04 << 2, 0x00 } // OGF=0x04, OCF=0x0003 / 7.4 INFORMATIONAL PARAMETERS
72 #define COMMAND_HCI_LE_READ_LOCAL_SUPPORTED_FEATURES \
73 { 0x03, 0x08 << 2, 0x00 } // OGF=0x08, OCF=0x0003 / 7.8 LE CONTROLLER COMMANDS
74 #define HCI_STATUS_SUCCESS 0x00
75 #define HCI_STATUS_UNKNOWN_HCI_COMMAND 0x01
76
77 #define EVENT_CONNECTION_COMPLETE 0x03
78 #define EVENT_COMMAND_COMPLETE 0x0e
79 #define EVENT_COMMAND_STATUS 0x0f
80 #define EVENT_NUMBER_OF_COMPLETED_PACKETS 0x13
81 #define EVENT_LOOPBACK_COMMAND 0x19
82
83 #define EVENT_CODE_BYTE 0
84 #define EVENT_LENGTH_BYTE 1
85 #define EVENT_FIRST_PAYLOAD_BYTE 2
86 #define EVENT_COMMAND_STATUS_STATUS_BYTE 2
87 #define EVENT_COMMAND_STATUS_ALLOWED_PACKETS_BYTE 3
88 #define EVENT_COMMAND_STATUS_OPCODE_LSBYTE 4 // Bytes 4 and 5
89 #define EVENT_COMMAND_COMPLETE_ALLOWED_PACKETS_BYTE 2
90 #define EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE 3 // Bytes 3 and 4
91 #define EVENT_COMMAND_COMPLETE_STATUS_BYTE 5
92 #define EVENT_COMMAND_COMPLETE_FIRST_PARAM_BYTE 6
93 #define EVENT_LOCAL_HCI_VERSION_BYTE EVENT_COMMAND_COMPLETE_FIRST_PARAM_BYTE
94 #define EVENT_LOCAL_LMP_VERSION_BYTE EVENT_LOCAL_HCI_VERSION_BYTE + 3
95 /**
96 * See Bluetooth Spec 5.4, Vol 2, Part C
97 * Link Manager Protocol, 3.3 Feature Mask Definition
98 *
99 * No | Supported Feature | Byte | Bit | Page
100 * ...
101 * 38 | LE Supported (Controller) | 4 | 6 | 0
102 * ...
103 */
104 #define EVENT_LOCAL_SUPPORTED_FEATURES_LE_SUPPORTED_BYTE \
105 (EVENT_COMMAND_COMPLETE_FIRST_PARAM_BYTE + 0x04)
106 #define EVENT_LOCAL_SUPPORTED_FEATURES_LE_SUPPORTED_BITMASK (0x01 << 6)
107 /**
108 * See Bluetooth Spec 5.4, Vol 6, Part B
109 * 4.6 Feature Support
110 *
111 * Bit | Link Layer Feature
112 * ...
113 * 5 | LE Data Packet Length Extension
114 * ...
115 */
116 #define EVENT_LOCAL_LE_SUPPORTED_FEATURES_DATA_LENGTH_EXTENSION_BYTE \
117 (EVENT_COMMAND_COMPLETE_FIRST_PARAM_BYTE + 0x00)
118 #define EVENT_LOCAL_LE_SUPPORTED_FEATURES_DATA_LENGTH_BITMASK (0x01 << 5)
119
120 #define EVENT_CONNECTION_COMPLETE_PARAM_LENGTH 11
121 #define EVENT_CONNECTION_COMPLETE_TYPE 11
122 #define EVENT_CONNECTION_COMPLETE_TYPE_SCO 0
123 #define EVENT_CONNECTION_COMPLETE_TYPE_ACL 1
124 #define EVENT_CONNECTION_COMPLETE_HANDLE_LSBYTE 3
125 #define EVENT_COMMAND_STATUS_LENGTH 4
126
127 #define EVENT_NUMBER_OF_COMPLETED_PACKETS_NUM_HANDLES 2
128
129 #define ACL_BROADCAST_FLAG_OFFSET 6
130 #define ACL_BROADCAST_FLAG_POINT_TO_POINT 0x0
131 #define ACL_BROADCAST_POINT_TO_POINT \
132 (ACL_BROADCAST_FLAG_POINT_TO_POINT << ACL_BROADCAST_FLAG_OFFSET)
133
134 #define ACL_PACKET_BOUNDARY_FLAG_OFFSET 4
135 #define ACL_PACKET_BOUNDARY_FLAG_FIRST_AUTO_FLUSHABLE 0x2
136 #define ACL_PACKET_BOUNDARY_FIRST_AUTO_FLUSHABLE \
137 (ACL_PACKET_BOUNDARY_FLAG_FIRST_AUTO_FLUSHABLE \
138 << ACL_PACKET_BOUNDARY_FLAG_OFFSET)
139
140 // To be removed in VTS release builds
141 #define ACL_HANDLE_QCA_DEBUG_MESSAGE 0xedc
142
143 constexpr char kCallbackNameAclEventReceived[] = "aclDataReceived";
144 constexpr char kCallbackNameHciEventReceived[] = "hciEventReceived";
145 constexpr char kCallbackNameInitializationComplete[] = "initializationComplete";
146 constexpr char kCallbackNameScoEventReceived[] = "scoDataReceived";
147 constexpr char kCallbackNameIsoEventReceived[] = "isoDataReceived";
148
149 class ThroughputLogger {
150 public:
ThroughputLogger(std::string task)151 ThroughputLogger(std::string task)
152 : task_(task), start_time_(std::chrono::steady_clock::now()) {}
153
~ThroughputLogger()154 ~ThroughputLogger() {
155 if (total_bytes_ == 0) return;
156 std::chrono::duration<double> duration =
157 std::chrono::steady_clock::now() - start_time_;
158 double s = duration.count();
159 if (s == 0) return;
160 double rate_kb = (static_cast<double>(total_bytes_) / s) / 1024;
161 ALOGD("%s %.1f KB/s (%zu bytes in %.3fs)", task_.c_str(), rate_kb,
162 total_bytes_, s);
163 }
164
setTotalBytes(size_t total_bytes)165 void setTotalBytes(size_t total_bytes) { total_bytes_ = total_bytes; }
166
167 private:
168 size_t total_bytes_;
169 std::string task_;
170 std::chrono::steady_clock::time_point start_time_;
171 };
172
173 // The main test class for Bluetooth HIDL HAL.
174 class BluetoothHidlTest : public ::testing::TestWithParam<std::string> {
175 public:
SetUp()176 virtual void SetUp() override {
177 // currently test passthrough mode only
178 bluetooth = IBluetoothHci::getService(GetParam());
179 ASSERT_NE(bluetooth, nullptr);
180 ALOGI("%s: getService() for bluetooth is %s", __func__,
181 bluetooth->isRemote() ? "remote" : "local");
182
183 bluetooth_hci_death_recipient = new BluetoothHciDeathRecipient();
184 ASSERT_NE(bluetooth_hci_death_recipient, nullptr);
185 ASSERT_TRUE(
186 bluetooth->linkToDeath(bluetooth_hci_death_recipient, 0).isOk());
187
188 bluetooth_cb = new BluetoothHciCallbacks(*this);
189 ASSERT_NE(bluetooth_cb, nullptr);
190
191 max_acl_data_packet_length = 0;
192 max_sco_data_packet_length = 0;
193 max_acl_data_packets = 0;
194 max_sco_data_packets = 0;
195
196 initialized = false;
197 event_cb_count = 0;
198 acl_cb_count = 0;
199 sco_cb_count = 0;
200
201 ASSERT_FALSE(initialized);
202 // Should not be checked in production code
203 ASSERT_TRUE(bluetooth->initialize(bluetooth_cb).isOk());
204
205 bluetooth_cb->SetWaitTimeout(kCallbackNameInitializationComplete,
206 WAIT_FOR_INIT_TIMEOUT);
207 bluetooth_cb->SetWaitTimeout(kCallbackNameHciEventReceived,
208 WAIT_FOR_HCI_EVENT_TIMEOUT);
209 bluetooth_cb->SetWaitTimeout(kCallbackNameAclEventReceived,
210 WAIT_FOR_ACL_DATA_TIMEOUT);
211 bluetooth_cb->SetWaitTimeout(kCallbackNameScoEventReceived,
212 WAIT_FOR_SCO_DATA_TIMEOUT);
213
214 EXPECT_TRUE(
215 bluetooth_cb->WaitForCallback(kCallbackNameInitializationComplete)
216 .no_timeout);
217
218 ASSERT_TRUE(initialized);
219 }
220
TearDown()221 virtual void TearDown() override {
222 ALOGI("TearDown");
223 // Should not be checked in production code
224 ASSERT_TRUE(bluetooth->close().isOk());
225 std::this_thread::sleep_for(INTERFACE_CLOSE_DELAY_MS);
226 handle_no_ops();
227 EXPECT_EQ(static_cast<size_t>(0), event_queue.size());
228 EXPECT_EQ(static_cast<size_t>(0), sco_queue.size());
229 EXPECT_EQ(static_cast<size_t>(0), acl_queue.size());
230 EXPECT_EQ(static_cast<size_t>(0), iso_queue.size());
231 }
232
233 void setBufferSizes();
234
235 // Functions called from within tests in loopback mode
236 void sendAndCheckHCI(int num_packets);
237 void sendAndCheckSCO(int num_packets, size_t size, uint16_t handle);
238 void sendAndCheckACL(int num_packets, size_t size, uint16_t handle);
239
240 // Helper functions to try to get a handle on verbosity
241 void enterLoopbackMode(std::vector<uint16_t>* sco_handles,
242 std::vector<uint16_t>* acl_handles);
243 void handle_no_ops();
244 void wait_for_event(bool timeout_is_error);
245 hidl_vec<uint8_t> wait_for_command_complete_event(hidl_vec<uint8_t> cmd);
246 int wait_for_completed_packets_event(uint16_t handle);
247
248 class BluetoothHciDeathRecipient : public hidl_death_recipient {
249 public:
serviceDied(uint64_t,const android::wp<::android::hidl::base::V1_0::IBase> &)250 void serviceDied(
251 uint64_t /*cookie*/,
252 const android::wp<::android::hidl::base::V1_0::IBase>& /*who*/)
253 override {
254 FAIL();
255 }
256 };
257
258 // A simple test implementation of BluetoothHciCallbacks.
259 class BluetoothHciCallbacks
260 : public ::testing::VtsHalHidlTargetCallbackBase<BluetoothHidlTest>,
261 public IBluetoothHciCallbacks {
262 BluetoothHidlTest& parent_;
263
264 public:
BluetoothHciCallbacks(BluetoothHidlTest & parent)265 BluetoothHciCallbacks(BluetoothHidlTest& parent) : parent_(parent){};
266
267 virtual ~BluetoothHciCallbacks() = default;
268
initializationComplete(Status status)269 Return<void> initializationComplete(Status status) override {
270 parent_.initialized = (status == Status::SUCCESS);
271 NotifyFromCallback(kCallbackNameInitializationComplete);
272 ALOGV("%s (status = %d)", __func__, static_cast<int>(status));
273 return Void();
274 };
275
hciEventReceived(const::android::hardware::hidl_vec<uint8_t> & event)276 Return<void> hciEventReceived(
277 const ::android::hardware::hidl_vec<uint8_t>& event) override {
278 parent_.event_cb_count++;
279 parent_.event_queue.push(event);
280 NotifyFromCallback(kCallbackNameHciEventReceived);
281 ALOGV("Event received (length = %d)", static_cast<int>(event.size()));
282 return Void();
283 };
284
aclDataReceived(const::android::hardware::hidl_vec<uint8_t> & data)285 Return<void> aclDataReceived(
286 const ::android::hardware::hidl_vec<uint8_t>& data) override {
287 parent_.acl_cb_count++;
288 parent_.acl_queue.push(data);
289 NotifyFromCallback(kCallbackNameAclEventReceived);
290 return Void();
291 };
292
scoDataReceived(const::android::hardware::hidl_vec<uint8_t> & data)293 Return<void> scoDataReceived(
294 const ::android::hardware::hidl_vec<uint8_t>& data) override {
295 parent_.sco_cb_count++;
296 parent_.sco_queue.push(data);
297 NotifyFromCallback(kCallbackNameScoEventReceived);
298 return Void();
299 };
300
isoDataReceived(const::android::hardware::hidl_vec<uint8_t> & data)301 Return<void> isoDataReceived(
302 const ::android::hardware::hidl_vec<uint8_t>& data) override {
303 parent_.iso_cb_count++;
304 parent_.iso_queue.push(data);
305 NotifyFromCallback(kCallbackNameIsoEventReceived);
306 return Void();
307 };
308 };
309
310 sp<IBluetoothHci> bluetooth;
311 sp<BluetoothHciCallbacks> bluetooth_cb;
312 sp<BluetoothHciDeathRecipient> bluetooth_hci_death_recipient;
313 std::queue<hidl_vec<uint8_t>> event_queue;
314 std::queue<hidl_vec<uint8_t>> acl_queue;
315 std::queue<hidl_vec<uint8_t>> sco_queue;
316 std::queue<hidl_vec<uint8_t>> iso_queue;
317
318 bool initialized;
319
320 int event_cb_count;
321 int sco_cb_count;
322 int acl_cb_count;
323 int iso_cb_count;
324
325 int max_acl_data_packet_length;
326 int max_sco_data_packet_length;
327 int max_acl_data_packets;
328 int max_sco_data_packets;
329 };
330
331 // Discard NO-OPs from the event queue.
handle_no_ops()332 void BluetoothHidlTest::handle_no_ops() {
333 while (event_queue.size() > 0) {
334 hidl_vec<uint8_t> event = event_queue.front();
335 EXPECT_GE(event.size(),
336 static_cast<size_t>(EVENT_COMMAND_COMPLETE_STATUS_BYTE));
337 bool event_is_no_op =
338 (event[EVENT_CODE_BYTE] == EVENT_COMMAND_COMPLETE) &&
339 (event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE] == 0x00) &&
340 (event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1] == 0x00);
341 event_is_no_op |= (event[EVENT_CODE_BYTE] == EVENT_COMMAND_STATUS) &&
342 (event[EVENT_COMMAND_STATUS_OPCODE_LSBYTE] == 0x00) &&
343 (event[EVENT_COMMAND_STATUS_OPCODE_LSBYTE + 1] == 0x00);
344 if (event_is_no_op) {
345 event_queue.pop();
346 } else {
347 break;
348 }
349 }
350 // To be removed in VTS release builds
351 while (acl_queue.size() > 0) {
352 hidl_vec<uint8_t> acl_packet = acl_queue.front();
353 uint16_t connection_handle = acl_packet[1] & 0xF;
354 connection_handle <<= 8;
355 connection_handle |= acl_packet[0];
356 bool packet_is_no_op = connection_handle == ACL_HANDLE_QCA_DEBUG_MESSAGE;
357 if (packet_is_no_op) {
358 acl_queue.pop();
359 } else {
360 break;
361 }
362 }
363 }
364
365 // Receive an event, discarding NO-OPs.
wait_for_event(bool timeout_is_error=true)366 void BluetoothHidlTest::wait_for_event(bool timeout_is_error = true) {
367 hidl_vec<uint8_t> event;
368 do {
369 bool no_timeout =
370 bluetooth_cb->WaitForCallback(kCallbackNameHciEventReceived).no_timeout;
371 EXPECT_TRUE(no_timeout || !timeout_is_error);
372 if (no_timeout && timeout_is_error) {
373 EXPECT_LT(static_cast<size_t>(0), event_queue.size());
374 }
375 if (event_queue.size() == 0) {
376 // WaitForCallback timed out.
377 return;
378 }
379 handle_no_ops();
380 } while (event_queue.size() == 0);
381 }
382
383 // Wait until a COMMAND_COMPLETE is received.
wait_for_command_complete_event(hidl_vec<uint8_t> cmd)384 hidl_vec<uint8_t> BluetoothHidlTest::wait_for_command_complete_event(hidl_vec<uint8_t> cmd) {
385 wait_for_event();
386 hidl_vec<uint8_t> event = event_queue.front();
387 event_queue.pop();
388
389 EXPECT_GT(event.size(),
390 static_cast<size_t>(EVENT_COMMAND_COMPLETE_STATUS_BYTE));
391 EXPECT_EQ(EVENT_COMMAND_COMPLETE, event[EVENT_CODE_BYTE]);
392 EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]);
393 EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]);
394 EXPECT_EQ(HCI_STATUS_SUCCESS, event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]);
395
396 return event;
397 }
398
399 // Send the command to read the controller's buffer sizes.
setBufferSizes()400 void BluetoothHidlTest::setBufferSizes() {
401 hidl_vec<uint8_t> cmd = COMMAND_HCI_READ_BUFFER_SIZE;
402 bluetooth->sendHciCommand(cmd);
403
404 wait_for_event();
405 if (event_queue.size() == 0) return;
406
407 hidl_vec<uint8_t> event = event_queue.front();
408 event_queue.pop();
409
410 EXPECT_EQ(EVENT_COMMAND_COMPLETE, event[EVENT_CODE_BYTE]);
411 EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]);
412 EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]);
413 EXPECT_EQ(HCI_STATUS_SUCCESS, event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]);
414
415 max_acl_data_packet_length =
416 event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 1] +
417 (event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 2] << 8);
418 max_sco_data_packet_length = event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 3];
419 max_acl_data_packets = event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 4] +
420 (event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 5] << 8);
421 max_sco_data_packets = event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 6] +
422 (event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 7] << 8);
423
424 ALOGD("%s: ACL max %d num %d SCO max %d num %d", __func__,
425 static_cast<int>(max_acl_data_packet_length),
426 static_cast<int>(max_acl_data_packets),
427 static_cast<int>(max_sco_data_packet_length),
428 static_cast<int>(max_sco_data_packets));
429 }
430
431 // Send an HCI command (in Loopback mode) and check the response.
sendAndCheckHCI(int num_packets)432 void BluetoothHidlTest::sendAndCheckHCI(int num_packets) {
433 ThroughputLogger logger = {__func__};
434 int command_size = 0;
435 for (int n = 0; n < num_packets; n++) {
436 // Send an HCI packet
437 std::vector<uint8_t> write_name = COMMAND_HCI_WRITE_LOCAL_NAME;
438 // With a name
439 char new_name[] = "John Jacob Jingleheimer Schmidt ___________________0";
440 size_t new_name_length = strlen(new_name);
441 for (size_t i = 0; i < new_name_length; i++)
442 write_name.push_back(static_cast<uint8_t>(new_name[i]));
443 // And the packet number
444 size_t i = new_name_length - 1;
445 for (int digits = n; digits > 0; digits = digits / 10, i--)
446 write_name[i] = static_cast<uint8_t>('0' + digits % 10);
447 // And padding
448 for (size_t i = 0; i < 248 - new_name_length; i++)
449 write_name.push_back(static_cast<uint8_t>(0));
450
451 hidl_vec<uint8_t> cmd = write_name;
452 bluetooth->sendHciCommand(cmd);
453
454 // Check the loopback of the HCI packet
455 wait_for_event();
456 if (event_queue.size() == 0) return;
457
458 hidl_vec<uint8_t> event = event_queue.front();
459 event_queue.pop();
460 size_t compare_length =
461 (cmd.size() > static_cast<size_t>(0xff) ? static_cast<size_t>(0xff)
462 : cmd.size());
463 EXPECT_GT(event.size(), compare_length + EVENT_FIRST_PAYLOAD_BYTE - 1);
464
465 EXPECT_EQ(EVENT_LOOPBACK_COMMAND, event[EVENT_CODE_BYTE]);
466 EXPECT_EQ(compare_length, event[EVENT_LENGTH_BYTE]);
467
468 // Don't compare past the end of the event.
469 if (compare_length + EVENT_FIRST_PAYLOAD_BYTE > event.size()) {
470 compare_length = event.size() - EVENT_FIRST_PAYLOAD_BYTE;
471 ALOGE("Only comparing %d bytes", static_cast<int>(compare_length));
472 }
473
474 if (n == num_packets - 1) {
475 command_size = cmd.size();
476 }
477
478 for (size_t i = 0; i < compare_length; i++)
479 EXPECT_EQ(cmd[i], event[EVENT_FIRST_PAYLOAD_BYTE + i]);
480 }
481 logger.setTotalBytes(command_size * num_packets * 2);
482 }
483
484 // Send a SCO data packet (in Loopback mode) and check the response.
sendAndCheckSCO(int num_packets,size_t size,uint16_t handle)485 void BluetoothHidlTest::sendAndCheckSCO(int num_packets, size_t size,
486 uint16_t handle) {
487 ThroughputLogger logger = {__func__};
488 for (int n = 0; n < num_packets; n++) {
489 // Send a SCO packet
490 hidl_vec<uint8_t> sco_packet;
491 std::vector<uint8_t> sco_vector;
492 sco_vector.push_back(static_cast<uint8_t>(handle & 0xff));
493 sco_vector.push_back(static_cast<uint8_t>((handle & 0x0f00) >> 8));
494 sco_vector.push_back(static_cast<uint8_t>(size & 0xff));
495 sco_vector.push_back(static_cast<uint8_t>((size & 0xff00) >> 8));
496 for (size_t i = 0; i < size; i++) {
497 sco_vector.push_back(static_cast<uint8_t>(i + n));
498 }
499 sco_packet = sco_vector;
500 bluetooth->sendScoData(sco_vector);
501
502 // Check the loopback of the SCO packet
503 EXPECT_TRUE(bluetooth_cb->WaitForCallback(kCallbackNameScoEventReceived)
504 .no_timeout);
505 hidl_vec<uint8_t> sco_loopback = sco_queue.front();
506 sco_queue.pop();
507
508 EXPECT_EQ(sco_packet.size(), sco_loopback.size());
509 size_t successful_bytes = 0;
510
511 for (size_t i = 0; i < sco_packet.size(); i++) {
512 if (sco_packet[i] == sco_loopback[i]) {
513 successful_bytes = i;
514 } else {
515 ALOGE("Miscompare at %d (expected %x, got %x)", static_cast<int>(i),
516 sco_packet[i], sco_loopback[i]);
517 ALOGE("At %d (expected %x, got %x)", static_cast<int>(i + 1),
518 sco_packet[i + 1], sco_loopback[i + 1]);
519 break;
520 }
521 }
522 EXPECT_EQ(sco_packet.size(), successful_bytes + 1);
523 }
524 logger.setTotalBytes(num_packets * size * 2);
525 }
526
527 // Send an ACL data packet (in Loopback mode) and check the response.
sendAndCheckACL(int num_packets,size_t size,uint16_t handle)528 void BluetoothHidlTest::sendAndCheckACL(int num_packets, size_t size,
529 uint16_t handle) {
530 ThroughputLogger logger = {__func__};
531 for (int n = 0; n < num_packets; n++) {
532 // Send an ACL packet
533 hidl_vec<uint8_t> acl_packet;
534 std::vector<uint8_t> acl_vector;
535 acl_vector.push_back(static_cast<uint8_t>(handle & 0xff));
536 acl_vector.push_back(static_cast<uint8_t>((handle & 0x0f00) >> 8) |
537 ACL_BROADCAST_POINT_TO_POINT |
538 ACL_PACKET_BOUNDARY_FIRST_AUTO_FLUSHABLE);
539 acl_vector.push_back(static_cast<uint8_t>(size & 0xff));
540 acl_vector.push_back(static_cast<uint8_t>((size & 0xff00) >> 8));
541 for (size_t i = 0; i < size; i++) {
542 acl_vector.push_back(static_cast<uint8_t>(i + n));
543 }
544 acl_packet = acl_vector;
545 bluetooth->sendAclData(acl_vector);
546
547 // Check the loopback of the ACL packet
548 EXPECT_TRUE(bluetooth_cb->WaitForCallback(kCallbackNameAclEventReceived)
549 .no_timeout);
550 hidl_vec<uint8_t> acl_loopback = acl_queue.front();
551 acl_queue.pop();
552
553 EXPECT_EQ(acl_packet.size(), acl_loopback.size());
554 size_t successful_bytes = 0;
555
556 for (size_t i = 0; i < acl_packet.size(); i++) {
557 if (acl_packet[i] == acl_loopback[i]) {
558 successful_bytes = i;
559 } else {
560 ALOGE("Miscompare at %d (expected %x, got %x)", static_cast<int>(i),
561 acl_packet[i], acl_loopback[i]);
562 ALOGE("At %d (expected %x, got %x)", static_cast<int>(i + 1),
563 acl_packet[i + 1], acl_loopback[i + 1]);
564 break;
565 }
566 }
567 EXPECT_EQ(acl_packet.size(), successful_bytes + 1);
568 }
569 logger.setTotalBytes(num_packets * size * 2);
570 }
571
572 // Return the number of completed packets reported by the controller.
wait_for_completed_packets_event(uint16_t handle)573 int BluetoothHidlTest::wait_for_completed_packets_event(uint16_t handle) {
574 int packets_processed = 0;
575 while (true) {
576 // There should be at least one event.
577 wait_for_event(packets_processed == 0);
578 if (event_queue.empty()) {
579 if (packets_processed == 0) {
580 ALOGW("%s: WaitForCallback timed out.", __func__);
581 }
582 return packets_processed;
583 }
584 hidl_vec<uint8_t> event = event_queue.front();
585 event_queue.pop();
586
587 EXPECT_EQ(EVENT_NUMBER_OF_COMPLETED_PACKETS, event[EVENT_CODE_BYTE]);
588 EXPECT_EQ(1, event[EVENT_NUMBER_OF_COMPLETED_PACKETS_NUM_HANDLES]);
589
590 uint16_t event_handle = event[3] + (event[4] << 8);
591 EXPECT_EQ(handle, event_handle);
592
593 packets_processed += event[5] + (event[6] << 8);
594 }
595 return packets_processed;
596 }
597
598 // Send local loopback command and initialize SCO and ACL handles.
enterLoopbackMode(std::vector<uint16_t> * sco_handles,std::vector<uint16_t> * acl_handles)599 void BluetoothHidlTest::enterLoopbackMode(std::vector<uint16_t>* sco_handles,
600 std::vector<uint16_t>* acl_handles) {
601 hidl_vec<uint8_t> cmd = COMMAND_HCI_WRITE_LOOPBACK_MODE_LOCAL;
602 bluetooth->sendHciCommand(cmd);
603
604 // Receive connection complete events with data channels
605 int connection_event_count = 0;
606 bool command_complete_received = false;
607 while (true) {
608 wait_for_event(false);
609 if (event_queue.size() == 0) {
610 // Fail if there was no event received or no connections completed.
611 EXPECT_TRUE(command_complete_received);
612 EXPECT_LT(0, connection_event_count);
613 return;
614 }
615 hidl_vec<uint8_t> event = event_queue.front();
616 event_queue.pop();
617 EXPECT_GT(event.size(),
618 static_cast<size_t>(EVENT_COMMAND_COMPLETE_STATUS_BYTE));
619 if (event[EVENT_CODE_BYTE] == EVENT_CONNECTION_COMPLETE) {
620 EXPECT_GT(event.size(),
621 static_cast<size_t>(EVENT_CONNECTION_COMPLETE_TYPE));
622 EXPECT_EQ(event[EVENT_LENGTH_BYTE],
623 EVENT_CONNECTION_COMPLETE_PARAM_LENGTH);
624 uint8_t connection_type = event[EVENT_CONNECTION_COMPLETE_TYPE];
625
626 EXPECT_TRUE(connection_type == EVENT_CONNECTION_COMPLETE_TYPE_SCO ||
627 connection_type == EVENT_CONNECTION_COMPLETE_TYPE_ACL);
628
629 // Save handles
630 uint16_t handle = event[EVENT_CONNECTION_COMPLETE_HANDLE_LSBYTE] |
631 event[EVENT_CONNECTION_COMPLETE_HANDLE_LSBYTE + 1] << 8;
632 if (connection_type == EVENT_CONNECTION_COMPLETE_TYPE_SCO)
633 sco_handles->push_back(handle);
634 else
635 acl_handles->push_back(handle);
636
637 ALOGD("Connect complete type = %d handle = %d",
638 event[EVENT_CONNECTION_COMPLETE_TYPE], handle);
639 connection_event_count++;
640 } else {
641 EXPECT_EQ(EVENT_COMMAND_COMPLETE, event[EVENT_CODE_BYTE]);
642 EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]);
643 EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]);
644 EXPECT_EQ(HCI_STATUS_SUCCESS, event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]);
645 command_complete_received = true;
646 }
647 }
648 }
649
650 // Empty test: Initialize()/Close() are called in SetUp()/TearDown().
TEST_P(BluetoothHidlTest,InitializeAndClose)651 TEST_P(BluetoothHidlTest, InitializeAndClose) {}
652
653 // Send an HCI Reset with sendHciCommand and wait for a command complete event.
TEST_P(BluetoothHidlTest,HciReset)654 TEST_P(BluetoothHidlTest, HciReset) {
655 hidl_vec<uint8_t> cmd = COMMAND_HCI_RESET;
656 bluetooth->sendHciCommand(cmd);
657
658 wait_for_command_complete_event(cmd);
659 }
660
661 // Read and check the HCI version of the controller.
TEST_P(BluetoothHidlTest,HciVersionTest)662 TEST_P(BluetoothHidlTest, HciVersionTest) {
663 hidl_vec<uint8_t> cmd = COMMAND_HCI_READ_LOCAL_VERSION_INFORMATION;
664 bluetooth->sendHciCommand(cmd);
665
666 wait_for_event();
667 if (event_queue.size() == 0) return;
668
669 hidl_vec<uint8_t> event = event_queue.front();
670 event_queue.pop();
671 EXPECT_GT(event.size(), static_cast<size_t>(EVENT_LOCAL_LMP_VERSION_BYTE));
672
673 EXPECT_EQ(EVENT_COMMAND_COMPLETE, event[EVENT_CODE_BYTE]);
674 EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]);
675 EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]);
676 EXPECT_EQ(HCI_STATUS_SUCCESS, event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]);
677
678 EXPECT_LE(HCI_MINIMUM_HCI_VERSION, event[EVENT_LOCAL_HCI_VERSION_BYTE]);
679 EXPECT_LE(HCI_MINIMUM_LMP_VERSION, event[EVENT_LOCAL_LMP_VERSION_BYTE]);
680 }
681
682 /**
683 * VSR-5.3.14-007 MUST support Bluetooth 4.2 and Bluetooth LE Data Length Extension.
684 * VSR-5.3.14-008 MUST support Bluetooth Low Energy (BLE).
685 */
686 // @VsrTest = 5.3.14-007
687 // @VsrTest = 5.3.14-008
TEST_P(BluetoothHidlTest,Bluetooth4_2)688 TEST_P(BluetoothHidlTest, Bluetooth4_2) {
689 // Bluetooth 4.2+
690 hidl_vec<uint8_t> cmd = COMMAND_HCI_READ_LOCAL_VERSION_INFORMATION;
691 bluetooth->sendHciCommand(cmd);
692 auto event = wait_for_command_complete_event(cmd);
693
694 EXPECT_LE(HCI_BLUETOOTH4_2_HCI_VERSION, event[EVENT_LOCAL_HCI_VERSION_BYTE]);
695 EXPECT_LE(HCI_BLUETOOTH4_2_LMP_VERSION, event[EVENT_LOCAL_LMP_VERSION_BYTE]);
696
697 // BLE
698 cmd = COMMAND_HCI_READ_LOCAL_SUPPORTED_FEATURES;
699 bluetooth->sendHciCommand(cmd);
700 event = wait_for_command_complete_event(cmd);
701 EXPECT_TRUE(event[EVENT_LOCAL_SUPPORTED_FEATURES_LE_SUPPORTED_BYTE] &
702 EVENT_LOCAL_SUPPORTED_FEATURES_LE_SUPPORTED_BITMASK);
703
704 // BLE Data Length Extension
705 cmd = COMMAND_HCI_LE_READ_LOCAL_SUPPORTED_FEATURES;
706 bluetooth->sendHciCommand(cmd);
707 event = wait_for_command_complete_event(cmd);
708 EXPECT_TRUE(event[EVENT_LOCAL_LE_SUPPORTED_FEATURES_DATA_LENGTH_EXTENSION_BYTE] &
709 EVENT_LOCAL_LE_SUPPORTED_FEATURES_DATA_LENGTH_BITMASK);
710 }
711
712 // Send an unknown HCI command and wait for the error message.
TEST_P(BluetoothHidlTest,HciUnknownCommand)713 TEST_P(BluetoothHidlTest, HciUnknownCommand) {
714 hidl_vec<uint8_t> cmd = COMMAND_HCI_SHOULD_BE_UNKNOWN;
715 bluetooth->sendHciCommand(cmd);
716
717 wait_for_event();
718 if (event_queue.size() == 0) return;
719
720 hidl_vec<uint8_t> event = event_queue.front();
721 event_queue.pop();
722
723 EXPECT_GT(event.size(),
724 static_cast<size_t>(EVENT_COMMAND_COMPLETE_STATUS_BYTE));
725 if (event[EVENT_CODE_BYTE] == EVENT_COMMAND_COMPLETE) {
726 EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]);
727 EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]);
728 EXPECT_EQ(HCI_STATUS_UNKNOWN_HCI_COMMAND,
729 event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]);
730 } else {
731 EXPECT_EQ(EVENT_COMMAND_STATUS, event[EVENT_CODE_BYTE]);
732 EXPECT_EQ(cmd[0], event[EVENT_COMMAND_STATUS_OPCODE_LSBYTE]);
733 EXPECT_EQ(cmd[1], event[EVENT_COMMAND_STATUS_OPCODE_LSBYTE + 1]);
734 EXPECT_EQ(HCI_STATUS_UNKNOWN_HCI_COMMAND,
735 event[EVENT_COMMAND_STATUS_STATUS_BYTE]);
736 }
737 }
738
739 // Enter loopback mode, but don't send any packets.
TEST_P(BluetoothHidlTest,WriteLoopbackMode)740 TEST_P(BluetoothHidlTest, WriteLoopbackMode) {
741 std::vector<uint16_t> sco_connection_handles;
742 std::vector<uint16_t> acl_connection_handles;
743 enterLoopbackMode(&sco_connection_handles, &acl_connection_handles);
744 }
745
746 // Enter loopback mode and send single packets.
TEST_P(BluetoothHidlTest,LoopbackModeSinglePackets)747 TEST_P(BluetoothHidlTest, LoopbackModeSinglePackets) {
748 setBufferSizes();
749
750 std::vector<uint16_t> sco_connection_handles;
751 std::vector<uint16_t> acl_connection_handles;
752 enterLoopbackMode(&sco_connection_handles, &acl_connection_handles);
753
754 sendAndCheckHCI(1);
755
756 // This should work, but breaks on some current platforms. Figure out how to
757 // grandfather older devices but test new ones.
758 if (0 && sco_connection_handles.size() > 0) {
759 EXPECT_LT(0, max_sco_data_packet_length);
760 sendAndCheckSCO(1, max_sco_data_packet_length, sco_connection_handles[0]);
761 int sco_packets_sent = 1;
762 int completed_packets =
763 wait_for_completed_packets_event(sco_connection_handles[0]);
764 if (sco_packets_sent != completed_packets) {
765 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
766 sco_packets_sent, completed_packets);
767 }
768 }
769
770 if (acl_connection_handles.size() > 0) {
771 EXPECT_LT(0, max_acl_data_packet_length);
772 sendAndCheckACL(1, max_acl_data_packet_length, acl_connection_handles[0]);
773 int acl_packets_sent = 1;
774 int completed_packets =
775 wait_for_completed_packets_event(acl_connection_handles[0]);
776 if (acl_packets_sent != completed_packets) {
777 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
778 acl_packets_sent, completed_packets);
779 }
780 }
781 }
782
783 // Enter loopback mode and send packets for bandwidth measurements.
TEST_P(BluetoothHidlTest,LoopbackModeBandwidth)784 TEST_P(BluetoothHidlTest, LoopbackModeBandwidth) {
785 setBufferSizes();
786
787 std::vector<uint16_t> sco_connection_handles;
788 std::vector<uint16_t> acl_connection_handles;
789 enterLoopbackMode(&sco_connection_handles, &acl_connection_handles);
790
791 sendAndCheckHCI(NUM_HCI_COMMANDS_BANDWIDTH);
792
793 // This should work, but breaks on some current platforms. Figure out how to
794 // grandfather older devices but test new ones.
795 if (0 && sco_connection_handles.size() > 0) {
796 EXPECT_LT(0, max_sco_data_packet_length);
797 sendAndCheckSCO(NUM_SCO_PACKETS_BANDWIDTH, max_sco_data_packet_length,
798 sco_connection_handles[0]);
799 int sco_packets_sent = NUM_SCO_PACKETS_BANDWIDTH;
800 int completed_packets =
801 wait_for_completed_packets_event(sco_connection_handles[0]);
802 if (sco_packets_sent != completed_packets) {
803 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
804 sco_packets_sent, completed_packets);
805 }
806 }
807
808 if (acl_connection_handles.size() > 0) {
809 EXPECT_LT(0, max_acl_data_packet_length);
810 sendAndCheckACL(NUM_ACL_PACKETS_BANDWIDTH, max_acl_data_packet_length,
811 acl_connection_handles[0]);
812 int acl_packets_sent = NUM_ACL_PACKETS_BANDWIDTH;
813 int completed_packets =
814 wait_for_completed_packets_event(acl_connection_handles[0]);
815 if (acl_packets_sent != completed_packets) {
816 ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
817 acl_packets_sent, completed_packets);
818 }
819 }
820 }
821
822 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BluetoothHidlTest);
823 INSTANTIATE_TEST_SUITE_P(
824 PerInstance, BluetoothHidlTest,
825 testing::ValuesIn(
826 android::hardware::getAllHalInstanceNames(IBluetoothHci::descriptor)),
827 android::hardware::PrintInstanceNameToString);
828