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