Merge "DRM 1.1: Add VINTF doc"
[android/platform-hardware-interfaces.git] / bluetooth / 1.0 / vts / functional / VtsHalBluetoothV1_0TargetTest.cpp
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  */
17 #define LOG_TAG "bluetooth_hidl_hal_test"
18 #include <android-base/logging.h>
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>
24 #include <utils/Log.h>
26 #include <VtsHalHidlTargetCallbackBase.h>
27 #include <VtsHalHidlTargetTestBase.h>
28 #include <VtsHalHidlTargetTestEnvBase.h>
29 #include <queue>
31 using ::android::sp;
32 using ::android::hardware::hidl_vec;
33 using ::android::hardware::Return;
34 using ::android::hardware::Void;
35 using ::android::hardware::bluetooth::V1_0::IBluetoothHci;
36 using ::android::hardware::bluetooth::V1_0::IBluetoothHciCallbacks;
37 using ::android::hardware::bluetooth::V1_0::Status;
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
44 #define WAIT_FOR_INIT_TIMEOUT std::chrono::milliseconds(2000)
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)
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
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
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
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
90 #define EVENT_NUMBER_OF_COMPLETED_PACKETS_NUM_HANDLES 2
92 #define ACL_BROADCAST_FLAG_OFFSET 6
93 #define ACL_BROADCAST_FLAG_POINT_TO_POINT 0x0
94 #define ACL_BROADCAST_POINT_TO_POINT \
95   (ACL_BROADCAST_FLAG_POINT_TO_POINT << ACL_BROADCAST_FLAG_OFFSET)
97 #define ACL_PACKET_BOUNDARY_FLAG_OFFSET 4
98 #define ACL_PACKET_BOUNDARY_FLAG_FIRST_AUTO_FLUSHABLE 0x2
99 #define ACL_PACKET_BOUNDARY_FIRST_AUTO_FLUSHABLE \
100   (ACL_PACKET_BOUNDARY_FLAG_FIRST_AUTO_FLUSHABLE \
101    << ACL_PACKET_BOUNDARY_FLAG_OFFSET)
103 constexpr char kCallbackNameAclEventReceived[] = "aclDataReceived";
104 constexpr char kCallbackNameHciEventReceived[] = "hciEventReceived";
105 constexpr char kCallbackNameInitializationComplete[] = "initializationComplete";
106 constexpr char kCallbackNameScoEventReceived[] = "scoDataReceived";
108 class ThroughputLogger {
109  public:
110   ThroughputLogger(std::string task)
111       : task_(task), start_time_(std::chrono::steady_clock::now()) {}
113   ~ThroughputLogger() {
114     if (total_bytes_ == 0) return;
115     std::chrono::duration<double> duration =
116         std::chrono::steady_clock::now() - start_time_;
117     double s = duration.count();
118     if (s == 0) return;
119     double rate_kb = (static_cast<double>(total_bytes_) / s) / 1024;
120     ALOGD("%s %.1f KB/s (%zu bytes in %.3fs)", task_.c_str(), rate_kb,
121           total_bytes_, s);
122   }
124   void setTotalBytes(size_t total_bytes) { total_bytes_ = total_bytes; }
126  private:
127   size_t total_bytes_;
128   std::string task_;
129   std::chrono::steady_clock::time_point start_time_;
130 };
132 // Test environment for Bluetooth HIDL HAL.
133 class BluetoothHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
134  public:
135   // get the test environment singleton
136   static BluetoothHidlEnvironment* Instance() {
137     static BluetoothHidlEnvironment* instance = new BluetoothHidlEnvironment;
138     return instance;
139   }
141   virtual void registerTestServices() override {
142     registerTestService<IBluetoothHci>();
143   }
145  private:
146   BluetoothHidlEnvironment() {}
147 };
149 // The main test class for Bluetooth HIDL HAL.
150 class BluetoothHidlTest : public ::testing::VtsHalHidlTargetTestBase {
151  public:
152   virtual void SetUp() override {
153     // currently test passthrough mode only
154     bluetooth =
155         ::testing::VtsHalHidlTargetTestBase::getService<IBluetoothHci>();
156     ASSERT_NE(bluetooth, nullptr);
157     ALOGI("%s: getService() for bluetooth is %s", __func__,
158           bluetooth->isRemote() ? "remote" : "local");
160     bluetooth_cb = new BluetoothHciCallbacks(*this);
161     ASSERT_NE(bluetooth_cb, nullptr);
163     max_acl_data_packet_length = 0;
164     max_sco_data_packet_length = 0;
165     max_acl_data_packets = 0;
166     max_sco_data_packets = 0;
168     initialized = false;
169     event_cb_count = 0;
170     acl_cb_count = 0;
171     sco_cb_count = 0;
173     ASSERT_FALSE(initialized);
174     // Should not be checked in production code
175     ASSERT_TRUE(bluetooth->initialize(bluetooth_cb).isOk());
177     bluetooth_cb->SetWaitTimeout(kCallbackNameInitializationComplete,
178                                  WAIT_FOR_INIT_TIMEOUT);
179     bluetooth_cb->SetWaitTimeout(kCallbackNameHciEventReceived,
180                                  WAIT_FOR_HCI_EVENT_TIMEOUT);
181     bluetooth_cb->SetWaitTimeout(kCallbackNameAclEventReceived,
182                                  WAIT_FOR_ACL_DATA_TIMEOUT);
183     bluetooth_cb->SetWaitTimeout(kCallbackNameScoEventReceived,
184                                  WAIT_FOR_SCO_DATA_TIMEOUT);
186     EXPECT_TRUE(
187         bluetooth_cb->WaitForCallback(kCallbackNameInitializationComplete)
188             .no_timeout);
190     ASSERT_TRUE(initialized);
191   }
193   virtual void TearDown() override {
194     // Should not be checked in production code
195     ASSERT_TRUE(bluetooth->close().isOk());
196     handle_no_ops();
197     EXPECT_EQ(static_cast<size_t>(0), event_queue.size());
198     EXPECT_EQ(static_cast<size_t>(0), sco_queue.size());
199     EXPECT_EQ(static_cast<size_t>(0), acl_queue.size());
200   }
202   void setBufferSizes();
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);
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);
217   // A simple test implementation of BluetoothHciCallbacks.
218   class BluetoothHciCallbacks
219       : public ::testing::VtsHalHidlTargetCallbackBase<BluetoothHidlTest>,
220         public IBluetoothHciCallbacks {
221     BluetoothHidlTest& parent_;
223    public:
224     BluetoothHciCallbacks(BluetoothHidlTest& parent) : parent_(parent){};
226     virtual ~BluetoothHciCallbacks() = default;
228     Return<void> initializationComplete(Status status) override {
229       parent_.initialized = (status == Status::SUCCESS);
230       NotifyFromCallback(kCallbackNameInitializationComplete);
231       ALOGV("%s (status = %d)", __func__, static_cast<int>(status));
232       return Void();
233     };
235     Return<void> hciEventReceived(
236         const ::android::hardware::hidl_vec<uint8_t>& event) override {
237       parent_.event_cb_count++;
238       parent_.event_queue.push(event);
239       NotifyFromCallback(kCallbackNameHciEventReceived);
240       ALOGV("Event received (length = %d)", static_cast<int>(event.size()));
241       return Void();
242     };
244     Return<void> aclDataReceived(
245         const ::android::hardware::hidl_vec<uint8_t>& data) override {
246       parent_.acl_cb_count++;
247       parent_.acl_queue.push(data);
248       NotifyFromCallback(kCallbackNameAclEventReceived);
249       return Void();
250     };
252     Return<void> scoDataReceived(
253         const ::android::hardware::hidl_vec<uint8_t>& data) override {
254       parent_.sco_cb_count++;
255       parent_.sco_queue.push(data);
256       NotifyFromCallback(kCallbackNameScoEventReceived);
257       return Void();
258     };
259   };
261   sp<IBluetoothHci> bluetooth;
262   sp<BluetoothHciCallbacks> bluetooth_cb;
263   std::queue<hidl_vec<uint8_t>> event_queue;
264   std::queue<hidl_vec<uint8_t>> acl_queue;
265   std::queue<hidl_vec<uint8_t>> sco_queue;
267   bool initialized;
269   int event_cb_count;
270   int sco_cb_count;
271   int acl_cb_count;
273   int max_acl_data_packet_length;
274   int max_sco_data_packet_length;
275   int max_acl_data_packets;
276   int max_sco_data_packets;
277 };
279 // Discard NO-OPs from the event queue.
280 void BluetoothHidlTest::handle_no_ops() {
281   while (event_queue.size() > 0) {
282     hidl_vec<uint8_t> event = event_queue.front();
283     EXPECT_GE(event.size(),
284               static_cast<size_t>(EVENT_COMMAND_COMPLETE_STATUS_BYTE));
285     bool event_is_no_op =
286         (event[EVENT_CODE_BYTE] == EVENT_COMMAND_COMPLETE) &&
287         (event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE] == 0x00) &&
288         (event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1] == 0x00);
289     event_is_no_op |= (event[EVENT_CODE_BYTE] == EVENT_COMMAND_STATUS) &&
290                       (event[EVENT_COMMAND_STATUS_OPCODE_LSBYTE] == 0x00) &&
291                       (event[EVENT_COMMAND_STATUS_OPCODE_LSBYTE + 1] == 0x00);
292     if (event_is_no_op) {
293       event_queue.pop();
294     } else {
295       return;
296     }
297   }
300 // Receive an event, discarding NO-OPs.
301 void BluetoothHidlTest::wait_for_event(bool timeout_is_error = true) {
302   hidl_vec<uint8_t> event;
303   do {
304     bool no_timeout =
305         bluetooth_cb->WaitForCallback(kCallbackNameHciEventReceived).no_timeout;
306     EXPECT_TRUE(no_timeout || !timeout_is_error);
307     if (no_timeout && timeout_is_error) {
308       EXPECT_LT(static_cast<size_t>(0), event_queue.size());
309     }
310     if (event_queue.size() == 0) {
311       // WaitForCallback timed out.
312       return;
313     }
314     handle_no_ops();
315   } while (event_queue.size() == 0);
318 // Wait until a COMMAND_COMPLETE is received.
319 void BluetoothHidlTest::wait_for_command_complete_event(hidl_vec<uint8_t> cmd) {
320   wait_for_event();
321   hidl_vec<uint8_t> event = event_queue.front();
322   event_queue.pop();
324   EXPECT_GT(event.size(),
325             static_cast<size_t>(EVENT_COMMAND_COMPLETE_STATUS_BYTE));
326   EXPECT_EQ(EVENT_COMMAND_COMPLETE, event[EVENT_CODE_BYTE]);
327   EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]);
328   EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]);
329   EXPECT_EQ(HCI_STATUS_SUCCESS, event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]);
332 // Send the command to read the controller's buffer sizes.
333 void BluetoothHidlTest::setBufferSizes() {
334   hidl_vec<uint8_t> cmd = COMMAND_HCI_READ_BUFFER_SIZE;
335   bluetooth->sendHciCommand(cmd);
337   wait_for_event();
338   if (event_queue.size() == 0) return;
340   hidl_vec<uint8_t> event = event_queue.front();
341   event_queue.pop();
343   EXPECT_EQ(EVENT_COMMAND_COMPLETE, event[EVENT_CODE_BYTE]);
344   EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]);
345   EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]);
346   EXPECT_EQ(HCI_STATUS_SUCCESS, event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]);
348   max_acl_data_packet_length =
349       event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 1] +
350       (event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 2] << 8);
351   max_sco_data_packet_length = event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 3];
352   max_acl_data_packets = event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 4] +
353                          (event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 5] << 8);
354   max_sco_data_packets = event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 6] +
355                          (event[EVENT_COMMAND_COMPLETE_STATUS_BYTE + 7] << 8);
357   ALOGD("%s: ACL max %d num %d SCO max %d num %d", __func__,
358         static_cast<int>(max_acl_data_packet_length),
359         static_cast<int>(max_acl_data_packets),
360         static_cast<int>(max_sco_data_packet_length),
361         static_cast<int>(max_sco_data_packets));
364 // Send an HCI command (in Loopback mode) and check the response.
365 void BluetoothHidlTest::sendAndCheckHCI(int num_packets) {
366   ThroughputLogger logger = {__func__};
367   int command_size = 0;
368   for (int n = 0; n < num_packets; n++) {
369     // Send an HCI packet
370     std::vector<uint8_t> write_name = COMMAND_HCI_WRITE_LOCAL_NAME;
371     // With a name
372     char new_name[] = "John Jacob Jingleheimer Schmidt ___________________0";
373     size_t new_name_length = strlen(new_name);
374     for (size_t i = 0; i < new_name_length; i++)
375       write_name.push_back(static_cast<uint8_t>(new_name[i]));
376     // And the packet number
377     size_t i = new_name_length - 1;
378     for (int digits = n; digits > 0; digits = digits / 10, i--)
379       write_name[i] = static_cast<uint8_t>('0' + digits % 10);
380     // And padding
381     for (size_t i = 0; i < 248 - new_name_length; i++)
382       write_name.push_back(static_cast<uint8_t>(0));
384     hidl_vec<uint8_t> cmd = write_name;
385     bluetooth->sendHciCommand(cmd);
387     // Check the loopback of the HCI packet
388     wait_for_event();
389     if (event_queue.size() == 0) return;
391     hidl_vec<uint8_t> event = event_queue.front();
392     event_queue.pop();
393     size_t compare_length =
394         (cmd.size() > static_cast<size_t>(0xff) ? static_cast<size_t>(0xff)
395                                                 : cmd.size());
396     EXPECT_GT(event.size(), compare_length + EVENT_FIRST_PAYLOAD_BYTE - 1);
398     EXPECT_EQ(EVENT_LOOPBACK_COMMAND, event[EVENT_CODE_BYTE]);
399     EXPECT_EQ(compare_length, event[EVENT_LENGTH_BYTE]);
401     // Don't compare past the end of the event.
402     if (compare_length + EVENT_FIRST_PAYLOAD_BYTE > event.size()) {
403       compare_length = event.size() - EVENT_FIRST_PAYLOAD_BYTE;
404       ALOGE("Only comparing %d bytes", static_cast<int>(compare_length));
405     }
407     if (n == num_packets - 1) {
408       command_size = cmd.size();
409     }
411     for (size_t i = 0; i < compare_length; i++)
412       EXPECT_EQ(cmd[i], event[EVENT_FIRST_PAYLOAD_BYTE + i]);
413   }
414   logger.setTotalBytes(command_size * num_packets * 2);
417 // Send a SCO data packet (in Loopback mode) and check the response.
418 void BluetoothHidlTest::sendAndCheckSCO(int num_packets, size_t size,
419                                         uint16_t handle) {
420   ThroughputLogger logger = {__func__};
421   for (int n = 0; n < num_packets; n++) {
422     // Send a SCO packet
423     hidl_vec<uint8_t> sco_packet;
424     std::vector<uint8_t> sco_vector;
425     sco_vector.push_back(static_cast<uint8_t>(handle & 0xff));
426     sco_vector.push_back(static_cast<uint8_t>((handle & 0x0f00) >> 8));
427     sco_vector.push_back(static_cast<uint8_t>(size & 0xff));
428     sco_vector.push_back(static_cast<uint8_t>((size & 0xff00) >> 8));
429     for (size_t i = 0; i < size; i++) {
430       sco_vector.push_back(static_cast<uint8_t>(i + n));
431     }
432     sco_packet = sco_vector;
433     bluetooth->sendScoData(sco_vector);
435     // Check the loopback of the SCO packet
436     EXPECT_TRUE(bluetooth_cb->WaitForCallback(kCallbackNameScoEventReceived)
437                     .no_timeout);
438     hidl_vec<uint8_t> sco_loopback = sco_queue.front();
439     sco_queue.pop();
441     EXPECT_EQ(sco_packet.size(), sco_loopback.size());
442     size_t successful_bytes = 0;
444     for (size_t i = 0; i < sco_packet.size(); i++) {
445       if (sco_packet[i] == sco_loopback[i]) {
446         successful_bytes = i;
447       } else {
448         ALOGE("Miscompare at %d (expected %x, got %x)", static_cast<int>(i),
449               sco_packet[i], sco_loopback[i]);
450         ALOGE("At %d (expected %x, got %x)", static_cast<int>(i + 1),
451               sco_packet[i + 1], sco_loopback[i + 1]);
452         break;
453       }
454     }
455     EXPECT_EQ(sco_packet.size(), successful_bytes + 1);
456   }
457   logger.setTotalBytes(num_packets * size * 2);
460 // Send an ACL data packet (in Loopback mode) and check the response.
461 void BluetoothHidlTest::sendAndCheckACL(int num_packets, size_t size,
462                                         uint16_t handle) {
463   ThroughputLogger logger = {__func__};
464   for (int n = 0; n < num_packets; n++) {
465     // Send an ACL packet
466     hidl_vec<uint8_t> acl_packet;
467     std::vector<uint8_t> acl_vector;
468     acl_vector.push_back(static_cast<uint8_t>(handle & 0xff));
469     acl_vector.push_back(static_cast<uint8_t>((handle & 0x0f00) >> 8) |
470                          ACL_BROADCAST_POINT_TO_POINT |
471                          ACL_PACKET_BOUNDARY_FIRST_AUTO_FLUSHABLE);
472     acl_vector.push_back(static_cast<uint8_t>(size & 0xff));
473     acl_vector.push_back(static_cast<uint8_t>((size & 0xff00) >> 8));
474     for (size_t i = 0; i < size; i++) {
475       acl_vector.push_back(static_cast<uint8_t>(i + n));
476     }
477     acl_packet = acl_vector;
478     bluetooth->sendAclData(acl_vector);
480     // Check the loopback of the ACL packet
481     EXPECT_TRUE(bluetooth_cb->WaitForCallback(kCallbackNameAclEventReceived)
482                     .no_timeout);
483     hidl_vec<uint8_t> acl_loopback = acl_queue.front();
484     acl_queue.pop();
486     EXPECT_EQ(acl_packet.size(), acl_loopback.size());
487     size_t successful_bytes = 0;
489     for (size_t i = 0; i < acl_packet.size(); i++) {
490       if (acl_packet[i] == acl_loopback[i]) {
491         successful_bytes = i;
492       } else {
493         ALOGE("Miscompare at %d (expected %x, got %x)", static_cast<int>(i),
494               acl_packet[i], acl_loopback[i]);
495         ALOGE("At %d (expected %x, got %x)", static_cast<int>(i + 1),
496               acl_packet[i + 1], acl_loopback[i + 1]);
497         break;
498       }
499     }
500     EXPECT_EQ(acl_packet.size(), successful_bytes + 1);
501   }
502   logger.setTotalBytes(num_packets * size * 2);
505 // Return the number of completed packets reported by the controller.
506 int BluetoothHidlTest::wait_for_completed_packets_event(uint16_t handle) {
507   int packets_processed = 0;
508   wait_for_event(false);
509   if (event_queue.size() == 0) {
510     ALOGW("%s: WaitForCallback timed out.", __func__);
511     return packets_processed;
512   }
513   while (event_queue.size() > 0) {
514     hidl_vec<uint8_t> event = event_queue.front();
515     event_queue.pop();
517     EXPECT_EQ(EVENT_NUMBER_OF_COMPLETED_PACKETS, event[EVENT_CODE_BYTE]);
518     EXPECT_EQ(1, event[EVENT_NUMBER_OF_COMPLETED_PACKETS_NUM_HANDLES]);
520     uint16_t event_handle = event[3] + (event[4] << 8);
521     EXPECT_EQ(handle, event_handle);
523     packets_processed += event[5] + (event[6] << 8);
524   }
525   return packets_processed;
528 // Send local loopback command and initialize SCO and ACL handles.
529 void BluetoothHidlTest::enterLoopbackMode(std::vector<uint16_t>& sco_handles,
530                                           std::vector<uint16_t>& acl_handles) {
531   hidl_vec<uint8_t> cmd = COMMAND_HCI_WRITE_LOOPBACK_MODE_LOCAL;
532   bluetooth->sendHciCommand(cmd);
534   // Receive connection complete events with data channels
535   int connection_event_count = 0;
536   bool command_complete_received = false;
537   while (true) {
538     wait_for_event(false);
539     if (event_queue.size() == 0) {
540       // Fail if there was no event received or no connections completed.
541       EXPECT_TRUE(command_complete_received);
542       EXPECT_LT(0, connection_event_count);
543       return;
544     }
545     hidl_vec<uint8_t> event = event_queue.front();
546     event_queue.pop();
547     EXPECT_GT(event.size(),
548               static_cast<size_t>(EVENT_COMMAND_COMPLETE_STATUS_BYTE));
549     if (event[EVENT_CODE_BYTE] == EVENT_CONNECTION_COMPLETE) {
550       EXPECT_GT(event.size(),
551                 static_cast<size_t>(EVENT_CONNECTION_COMPLETE_TYPE));
552       EXPECT_EQ(event[EVENT_LENGTH_BYTE],
553                 EVENT_CONNECTION_COMPLETE_PARAM_LENGTH);
554       uint8_t connection_type = event[EVENT_CONNECTION_COMPLETE_TYPE];
556       EXPECT_TRUE(connection_type == EVENT_CONNECTION_COMPLETE_TYPE_SCO ||
557                   connection_type == EVENT_CONNECTION_COMPLETE_TYPE_ACL);
559       // Save handles
560       uint16_t handle = event[EVENT_CONNECTION_COMPLETE_HANDLE_LSBYTE] |
561                         event[EVENT_CONNECTION_COMPLETE_HANDLE_LSBYTE + 1] << 8;
562       if (connection_type == EVENT_CONNECTION_COMPLETE_TYPE_SCO)
563         sco_handles.push_back(handle);
564       else
565         acl_handles.push_back(handle);
567       ALOGD("Connect complete type = %d handle = %d",
568             event[EVENT_CONNECTION_COMPLETE_TYPE], handle);
569       connection_event_count++;
570     } else {
571       EXPECT_EQ(EVENT_COMMAND_COMPLETE, event[EVENT_CODE_BYTE]);
572       EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]);
573       EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]);
574       EXPECT_EQ(HCI_STATUS_SUCCESS, event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]);
575       command_complete_received = true;
576     }
577   }
580 // Empty test: Initialize()/Close() are called in SetUp()/TearDown().
581 TEST_F(BluetoothHidlTest, InitializeAndClose) {}
583 // Send an HCI Reset with sendHciCommand and wait for a command complete event.
584 TEST_F(BluetoothHidlTest, HciReset) {
585   hidl_vec<uint8_t> cmd = COMMAND_HCI_RESET;
586   bluetooth->sendHciCommand(cmd);
588   wait_for_command_complete_event(cmd);
591 // Read and check the HCI version of the controller.
592 TEST_F(BluetoothHidlTest, HciVersionTest) {
593   hidl_vec<uint8_t> cmd = COMMAND_HCI_READ_LOCAL_VERSION_INFORMATION;
594   bluetooth->sendHciCommand(cmd);
596   wait_for_event();
597   if (event_queue.size() == 0) return;
599   hidl_vec<uint8_t> event = event_queue.front();
600   event_queue.pop();
601   EXPECT_GT(event.size(), static_cast<size_t>(EVENT_LOCAL_LMP_VERSION_BYTE));
603   EXPECT_EQ(EVENT_COMMAND_COMPLETE, event[EVENT_CODE_BYTE]);
604   EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]);
605   EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]);
606   EXPECT_EQ(HCI_STATUS_SUCCESS, event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]);
608   EXPECT_LE(HCI_MINIMUM_HCI_VERSION, event[EVENT_LOCAL_HCI_VERSION_BYTE]);
609   EXPECT_LE(HCI_MINIMUM_LMP_VERSION, event[EVENT_LOCAL_LMP_VERSION_BYTE]);
612 // Send an unknown HCI command and wait for the error message.
613 TEST_F(BluetoothHidlTest, HciUnknownCommand) {
614   hidl_vec<uint8_t> cmd = COMMAND_HCI_SHOULD_BE_UNKNOWN;
615   bluetooth->sendHciCommand(cmd);
617   wait_for_event();
618   if (event_queue.size() == 0) return;
620   hidl_vec<uint8_t> event = event_queue.front();
621   event_queue.pop();
623   EXPECT_GT(event.size(),
624             static_cast<size_t>(EVENT_COMMAND_COMPLETE_STATUS_BYTE));
625   if (event[EVENT_CODE_BYTE] == EVENT_COMMAND_COMPLETE) {
626     EXPECT_EQ(cmd[0], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE]);
627     EXPECT_EQ(cmd[1], event[EVENT_COMMAND_COMPLETE_OPCODE_LSBYTE + 1]);
628     EXPECT_EQ(HCI_STATUS_UNKNOWN_HCI_COMMAND,
629               event[EVENT_COMMAND_COMPLETE_STATUS_BYTE]);
630   } else {
631     EXPECT_EQ(EVENT_COMMAND_STATUS, event[EVENT_CODE_BYTE]);
632     EXPECT_EQ(cmd[0], event[EVENT_COMMAND_STATUS_OPCODE_LSBYTE]);
633     EXPECT_EQ(cmd[1], event[EVENT_COMMAND_STATUS_OPCODE_LSBYTE + 1]);
634     EXPECT_EQ(HCI_STATUS_UNKNOWN_HCI_COMMAND,
635               event[EVENT_COMMAND_STATUS_STATUS_BYTE]);
636   }
639 // Enter loopback mode, but don't send any packets.
640 TEST_F(BluetoothHidlTest, WriteLoopbackMode) {
641   std::vector<uint16_t> sco_connection_handles;
642   std::vector<uint16_t> acl_connection_handles;
643   enterLoopbackMode(sco_connection_handles, acl_connection_handles);
646 // Enter loopback mode and send single packets.
647 TEST_F(BluetoothHidlTest, LoopbackModeSinglePackets) {
648   setBufferSizes();
650   std::vector<uint16_t> sco_connection_handles;
651   std::vector<uint16_t> acl_connection_handles;
652   enterLoopbackMode(sco_connection_handles, acl_connection_handles);
654   sendAndCheckHCI(1);
656   // This should work, but breaks on some current platforms.  Figure out how to
657   // grandfather older devices but test new ones.
658   if (0 && sco_connection_handles.size() > 0) {
659     EXPECT_LT(0, max_sco_data_packet_length);
660     sendAndCheckSCO(1, max_sco_data_packet_length, sco_connection_handles[0]);
661     int sco_packets_sent = 1;
662     int completed_packets =
663         wait_for_completed_packets_event(sco_connection_handles[0]);
664     if (sco_packets_sent != completed_packets) {
665       ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
666             sco_packets_sent, completed_packets);
667     }
668   }
670   if (acl_connection_handles.size() > 0) {
671     EXPECT_LT(0, max_acl_data_packet_length);
672     sendAndCheckACL(1, max_acl_data_packet_length, acl_connection_handles[0]);
673     int acl_packets_sent = 1;
674     int completed_packets =
675         wait_for_completed_packets_event(acl_connection_handles[0]);
676     if (acl_packets_sent != completed_packets) {
677       ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
678             acl_packets_sent, completed_packets);
679     }
680   }
683 // Enter loopback mode and send packets for bandwidth measurements.
684 TEST_F(BluetoothHidlTest, LoopbackModeBandwidth) {
685   setBufferSizes();
687   std::vector<uint16_t> sco_connection_handles;
688   std::vector<uint16_t> acl_connection_handles;
689   enterLoopbackMode(sco_connection_handles, acl_connection_handles);
691   sendAndCheckHCI(NUM_HCI_COMMANDS_BANDWIDTH);
693   // This should work, but breaks on some current platforms.  Figure out how to
694   // grandfather older devices but test new ones.
695   if (0 && sco_connection_handles.size() > 0) {
696     EXPECT_LT(0, max_sco_data_packet_length);
697     sendAndCheckSCO(NUM_SCO_PACKETS_BANDWIDTH, max_sco_data_packet_length,
698                     sco_connection_handles[0]);
699     int sco_packets_sent = NUM_SCO_PACKETS_BANDWIDTH;
700     int completed_packets =
701         wait_for_completed_packets_event(sco_connection_handles[0]);
702     if (sco_packets_sent != completed_packets) {
703       ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
704             sco_packets_sent, completed_packets);
705     }
706   }
708   if (acl_connection_handles.size() > 0) {
709     EXPECT_LT(0, max_acl_data_packet_length);
710     sendAndCheckACL(NUM_ACL_PACKETS_BANDWIDTH, max_acl_data_packet_length,
711                     acl_connection_handles[0]);
712     int acl_packets_sent = NUM_ACL_PACKETS_BANDWIDTH;
713     int completed_packets =
714         wait_for_completed_packets_event(acl_connection_handles[0]);
715     if (acl_packets_sent != completed_packets) {
716       ALOGW("%s: packets_sent (%d) != completed_packets (%d)", __func__,
717             acl_packets_sent, completed_packets);
718     }
719   }
722 int main(int argc, char** argv) {
723   ::testing::AddGlobalTestEnvironment(BluetoothHidlEnvironment::Instance());
724   ::testing::InitGoogleTest(&argc, argv);
725   BluetoothHidlEnvironment::Instance()->init(&argc, argv);
726   int status = RUN_ALL_TESTS();
727   ALOGI("Test result = %d", status);
728   return status;