1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include <gtest/gtest.h>
20 
21 #include "AlarmTestHarness.h"
22 
23 extern "C" {
24 #include <stdint.h>
25 
26 #include "device/include/controller.h"
27 #include "osi/include/allocation_tracker.h"
28 #include "osi/include/allocator.h"
29 #include "osi/include/osi.h"
30 #include "osi/include/semaphore.h"
31 #include "btsnoop.h"
32 #include "hcimsgs.h"
33 #include "hci_hal.h"
34 #include "hci_inject.h"
35 #include "hci_layer.h"
36 #include "low_power_manager.h"
37 #include "module.h"
38 #include "packet_fragmenter.h"
39 #include "test_stubs.h"
40 #include "vendor.h"
41 
42 extern const module_t hci_module;
43 }
44 
45 DECLARE_TEST_MODES(
46   start_up_async,
47   shut_down,
48   postload,
49   transmit_simple,
50   receive_simple,
51   transmit_command_no_callbacks,
52   transmit_command_command_status,
53   transmit_command_command_complete,
54   ignoring_packets_ignored_packet,
55   ignoring_packets_following_packet
56 );
57 
58 static const char *small_sample_data = "\"It is easy to see,\" replied Don Quixote";
59 static const char *command_sample_data = "that thou art not used to this business of adventures; those are giants";
60 static const char *ignored_data = "and if thou art afraid, away with thee out of this and betake thyself to prayer";
61 static const char *unignored_data = "while I engage them in fierce and unequal combat";
62 
63 static const hci_t *hci;
64 static const hci_hal_callbacks_t *hal_callbacks;
65 static thread_t *internal_thread;
66 static vendor_cb firmware_config_callback;
67 static vendor_cb sco_config_callback;
68 static vendor_cb epilog_callback;
69 static semaphore_t *done;
70 static const uint16_t test_handle = (0x1992 & 0xCFFF);
71 static const uint16_t test_handle_continuation = (0x1992 & 0xCFFF) | 0x1000;
72 static int packet_index;
73 static unsigned int data_size_sum;
74 static BT_HDR *data_to_receive;
75 
signal_work_item(UNUSED_ATTR void * context)76 static void signal_work_item(UNUSED_ATTR void *context) {
77   semaphore_post(done);
78 }
79 
flush_thread(thread_t * thread)80 static void flush_thread(thread_t *thread) {
81   // Double flush to ensure we get the next reactor cycle
82   thread_post(thread, signal_work_item, NULL);
83   semaphore_wait(done);
84   thread_post(thread, signal_work_item, NULL);
85   semaphore_wait(done);
86 }
87 
88 // TODO move this to a common packet testing helper
manufacture_packet(uint16_t event,const char * data)89 static BT_HDR *manufacture_packet(uint16_t event, const char *data) {
90   uint16_t data_length = strlen(data);
91   uint16_t size = data_length;
92   if (event == MSG_STACK_TO_HC_HCI_ACL) {
93     size += 4; // 2 for the handle, 2 for the length;
94   }
95 
96   BT_HDR *packet = (BT_HDR *)osi_malloc(size + sizeof(BT_HDR));
97   packet->len = size;
98   packet->offset = 0;
99   packet->layer_specific = 0;
100 
101   // The command transmit interface adds the event type automatically.
102   // Make sure it works but omitting it here.
103   if (event != MSG_STACK_TO_HC_HCI_CMD)
104     packet->event = event;
105 
106   uint8_t *packet_data = packet->data;
107 
108   if (event == MSG_STACK_TO_HC_HCI_ACL) {
109     UINT16_TO_STREAM(packet_data, test_handle);
110     UINT16_TO_STREAM(packet_data, data_length);
111   }
112 
113   for (int i = 0; i < data_length; i++) {
114     packet_data[i] = data[i];
115   }
116 
117   if (event == MSG_STACK_TO_HC_HCI_CMD) {
118     STREAM_SKIP_UINT16(packet_data);
119     UINT8_TO_STREAM(packet_data, data_length - 3);
120   } else if (event == MSG_HC_TO_STACK_HCI_EVT) {
121     STREAM_SKIP_UINT8(packet_data);
122     UINT8_TO_STREAM(packet_data, data_length - 2);
123   }
124 
125   return packet;
126 }
127 
expect_packet(uint16_t event,int max_acl_data_size,const uint8_t * data,uint16_t data_length,const char * expected_data)128 static void expect_packet(uint16_t event, int max_acl_data_size, const uint8_t *data, uint16_t data_length, const char *expected_data) {
129   int expected_data_offset;
130   int length_to_check;
131 
132   if (event == MSG_STACK_TO_HC_HCI_ACL) {
133     uint16_t handle;
134     uint16_t length;
135     STREAM_TO_UINT16(handle, data);
136     STREAM_TO_UINT16(length, data);
137 
138     if (packet_index == 0)
139       EXPECT_EQ(test_handle, handle);
140     else
141       EXPECT_EQ(test_handle_continuation, handle);
142 
143     int length_remaining = strlen(expected_data) - data_size_sum;
144     int packet_data_length = data_length - HCI_ACL_PREAMBLE_SIZE;
145     EXPECT_EQ(length_remaining, length);
146 
147     if (length_remaining < max_acl_data_size)
148       EXPECT_EQ(length, packet_data_length);
149     else
150       EXPECT_EQ(max_acl_data_size, packet_data_length);
151 
152     length_to_check = packet_data_length;
153     expected_data_offset = packet_index * max_acl_data_size;
154     packet_index++;
155   } else {
156     length_to_check = strlen(expected_data);
157     expected_data_offset = 0;
158   }
159 
160   for (int i = 0; i < length_to_check; i++) {
161     if (event == MSG_STACK_TO_HC_HCI_CMD && (i == 2))
162       EXPECT_EQ(data_length - 3, data[i]);
163     else
164       EXPECT_EQ(expected_data[expected_data_offset + i], data[i]);
165 
166     data_size_sum++;
167   }
168 }
169 
170 STUB_FUNCTION(bool, hal_init, (const hci_hal_callbacks_t *callbacks, thread_t *working_thread))
DURING(start_up_async)171   DURING(start_up_async) AT_CALL(0) {
172     hal_callbacks = callbacks;
173     internal_thread = working_thread;
174     return true;
175   }
176 
177   UNEXPECTED_CALL;
178   return false;
179 }
180 
181 STUB_FUNCTION(bool, hal_open, ())
182   DURING(start_up_async) AT_CALL(0) return true;
183   UNEXPECTED_CALL;
184   return false;
185 }
186 
187 STUB_FUNCTION(void, hal_close, ())
188   DURING(shut_down) AT_CALL(0) return;
189   UNEXPECTED_CALL;
190 }
191 
192 STUB_FUNCTION(uint16_t, hal_transmit_data, (serial_data_type_t type, uint8_t *data, uint16_t length))
193   DURING(transmit_simple) AT_CALL(0) {
194     EXPECT_EQ(DATA_TYPE_ACL, type);
195     expect_packet(MSG_STACK_TO_HC_HCI_ACL, 1021, data, length, small_sample_data);
196     return length;
197   }
198 
199   DURING(
200       transmit_command_no_callbacks,
201       transmit_command_command_status,
202       transmit_command_command_complete
203     ) AT_CALL(0) {
204     EXPECT_EQ(DATA_TYPE_COMMAND, type);
205     expect_packet(MSG_STACK_TO_HC_HCI_CMD, 1021, data, length, command_sample_data);
206     return length;
207   }
208 
209   UNEXPECTED_CALL;
210   return 0;
211 }
212 
213 static size_t replay_data_to_receive(size_t max_size, uint8_t *buffer) {
214   for (size_t i = 0; i < max_size; i++) {
215     if (data_to_receive->offset >= data_to_receive->len)
216       break;
217 
218     buffer[i] = data_to_receive->data[data_to_receive->offset++];
219 
220     if (i == (max_size - 1))
221       return i + 1; // We return the length, not the index;
222   }
223 
224   return 0;
225 }
226 
227 STUB_FUNCTION(size_t, hal_read_data, (serial_data_type_t type, uint8_t *buffer, size_t max_size))
228   DURING(receive_simple, ignoring_packets_following_packet) {
229     EXPECT_EQ(DATA_TYPE_ACL, type);
230     return replay_data_to_receive(max_size, buffer);
231   }
232 
233   DURING(ignoring_packets_ignored_packet) {
234     EXPECT_EQ(DATA_TYPE_EVENT, type);
235     return replay_data_to_receive(max_size, buffer);
236   }
237 
238   DURING(
239       transmit_command_no_callbacks,
240       transmit_command_command_status,
241       transmit_command_command_complete) {
242     EXPECT_EQ(DATA_TYPE_EVENT, type);
243     return replay_data_to_receive(max_size, buffer);
244   }
245 
246   UNEXPECTED_CALL;
247   return 0;
248 }
249 
250 STUB_FUNCTION(void, hal_packet_finished, (serial_data_type_t type))
251   DURING(receive_simple, ignoring_packets_following_packet) AT_CALL(0) {
252     EXPECT_EQ(DATA_TYPE_ACL, type);
253     return;
254   }
255 
256   DURING(ignoring_packets_ignored_packet) AT_CALL(0) {
257     EXPECT_EQ(DATA_TYPE_EVENT, type);
258     return;
259   }
260 
261   DURING(
262       transmit_command_no_callbacks,
263       transmit_command_command_status,
264       transmit_command_command_complete
265     ) AT_CALL(0) {
266     EXPECT_EQ(DATA_TYPE_EVENT, type);
267     return;
268   }
269 
270   UNEXPECTED_CALL;
271 }
272 
273 STUB_FUNCTION(bool, hci_inject_open, (
274     UNUSED_ATTR const hci_t *hci_interface))
275   DURING(start_up_async) AT_CALL(0) return true;
276   UNEXPECTED_CALL;
277   return false;
278 }
279 
280 STUB_FUNCTION(void, hci_inject_close, ())
281   DURING(shut_down) AT_CALL(0) return;
282   UNEXPECTED_CALL;
283 }
284 
285 STUB_FUNCTION(void, btsnoop_capture, (const BT_HDR *buffer, bool is_received))
286   DURING(transmit_simple) AT_CALL(0) {
287     EXPECT_FALSE(is_received);
288     expect_packet(MSG_STACK_TO_HC_HCI_ACL, 1021, buffer->data + buffer->offset, buffer->len, small_sample_data);
289     packet_index = 0;
290     data_size_sum = 0;
291     return;
292   }
293 
294 
295   DURING(
296       transmit_command_no_callbacks,
297       transmit_command_command_status,
298       transmit_command_command_complete) {
299     AT_CALL(0) {
300       EXPECT_FALSE(is_received);
301       expect_packet(MSG_STACK_TO_HC_HCI_CMD, 1021, buffer->data + buffer->offset, buffer->len, command_sample_data);
302       packet_index = 0;
303       data_size_sum = 0;
304       return;
305     }
306     AT_CALL(1) {
307       EXPECT_TRUE(is_received);
308       // not super important to verify the contents right now
309       return;
310     }
311   }
312 
313   DURING(
314       receive_simple,
315       ignoring_packets_following_packet
316     ) AT_CALL(0) {
317     EXPECT_TRUE(is_received);
318     EXPECT_TRUE(buffer->len == data_to_receive->len);
319     const uint8_t *buffer_base = buffer->data + buffer->offset;
320     const uint8_t *expected_base = data_to_receive->data;
321     for (int i = 0; i < buffer->len; i++) {
322       EXPECT_EQ(expected_base[i], buffer_base[i]);
323     }
324 
325     return;
326   }
327 
328   UNEXPECTED_CALL;
329 }
330 
331 STUB_FUNCTION(void, low_power_init, (UNUSED_ATTR thread_t *thread))
332   DURING(start_up_async) AT_CALL(0) return;
333   UNEXPECTED_CALL;
334 }
335 
336 STUB_FUNCTION(void, low_power_cleanup, ())
337   DURING(shut_down) AT_CALL(0) return;
338   UNEXPECTED_CALL;
339 }
340 
341 STUB_FUNCTION(void, low_power_wake_assert, ())
342   DURING(
343       transmit_simple,
344       transmit_command_no_callbacks,
345       transmit_command_command_status,
346       transmit_command_command_complete) {
347     AT_CALL(0) return;
348   }
349 
350   UNEXPECTED_CALL;
351 }
352 
353 STUB_FUNCTION(void, low_power_transmit_done, ())
354   DURING(
355       transmit_simple,
356       transmit_command_no_callbacks,
357       transmit_command_command_status,
358       transmit_command_command_complete) {
359     AT_CALL(0) return;
360   }
361 
362   UNEXPECTED_CALL;
363 }
364 
365 STUB_FUNCTION(bool, vendor_open, (UNUSED_ATTR const uint8_t *addr, const hci_t *hci_interface))
366   DURING(start_up_async) AT_CALL(0) {
367     // TODO(zachoverflow): check address value when it gets put into a module
368     EXPECT_EQ(hci, hci_interface);
369     return true;
370   }
371 
372   UNEXPECTED_CALL;
373   return true;
374 }
375 
376 STUB_FUNCTION(void, vendor_close, ())
377   DURING(shut_down) AT_CALL(0) return;
378   UNEXPECTED_CALL;
379 }
380 
381 STUB_FUNCTION(void, vendor_set_callback, (vendor_async_opcode_t opcode, UNUSED_ATTR vendor_cb callback))
382   DURING(start_up_async) {
383     AT_CALL(0) {
384       EXPECT_EQ(VENDOR_CONFIGURE_FIRMWARE, opcode);
385       firmware_config_callback = callback;
386       return;
387     }
388     AT_CALL(1) {
389       EXPECT_EQ(VENDOR_CONFIGURE_SCO, opcode);
390       sco_config_callback = callback;
391       return;
392     }
393     AT_CALL(2) {
394       EXPECT_EQ(VENDOR_DO_EPILOG, opcode);
395       epilog_callback = callback;
396       return;
397     }
398   }
399 
400   UNEXPECTED_CALL;
401 }
402 
403 STUB_FUNCTION(int, vendor_send_command, (vendor_opcode_t opcode, void *param))
404   DURING(start_up_async) {
405 #if (defined (BT_CLEAN_TURN_ON_DISABLED) && BT_CLEAN_TURN_ON_DISABLED == TRUE)
406     AT_CALL(0) {
407       EXPECT_EQ(VENDOR_CHIP_POWER_CONTROL, opcode);
408       EXPECT_EQ(BT_VND_PWR_ON, *(int *)param);
409       return 0;
410     }
411 #else
412     AT_CALL(0) {
413       EXPECT_EQ(VENDOR_CHIP_POWER_CONTROL, opcode);
414       EXPECT_EQ(BT_VND_PWR_OFF, *(int *)param);
415       return 0;
416     }
417     AT_CALL(1) {
418       EXPECT_EQ(VENDOR_CHIP_POWER_CONTROL, opcode);
419       EXPECT_EQ(BT_VND_PWR_ON, *(int *)param);
420       return 0;
421     }
422 #endif
423   }
424 
425   DURING(shut_down) AT_CALL(0) {
426     EXPECT_EQ(VENDOR_CHIP_POWER_CONTROL, opcode);
427     EXPECT_EQ(BT_VND_PWR_OFF, *(int *)param);
428     return 0;
429   }
430 
431   UNEXPECTED_CALL;
432   return 0;
433 }
434 
435 STUB_FUNCTION(int, vendor_send_async_command, (UNUSED_ATTR vendor_async_opcode_t opcode, UNUSED_ATTR void *param))
436   DURING(start_up_async) AT_CALL(0) {
437     EXPECT_EQ(VENDOR_CONFIGURE_FIRMWARE, opcode);
438     firmware_config_callback(true);
439     return 0;
440   }
441 
442   DURING(postload) AT_CALL(0) {
443     EXPECT_EQ(VENDOR_CONFIGURE_SCO, opcode);
444     sco_config_callback(true);
445     return 0;
446   }
447 
448   DURING(shut_down) AT_CALL(0) {
449     EXPECT_EQ(VENDOR_DO_EPILOG, opcode);
450     epilog_callback(true);
451     return 0;
452   }
453 
454   UNEXPECTED_CALL;
455   return 0;
456 }
457 
458 STUB_FUNCTION(void, command_complete_callback, (BT_HDR *response, UNUSED_ATTR void *context))
459   DURING(transmit_command_command_complete) AT_CALL(0) {
460     osi_free(response);
461     return;
462   }
463 
464   UNEXPECTED_CALL;
465 }
466 
467 STUB_FUNCTION(void, command_status_callback, (UNUSED_ATTR uint8_t status, BT_HDR *command, UNUSED_ATTR void *context))
468   DURING(transmit_command_command_status) AT_CALL(0) {
469     osi_free(command);
470     return;
471   }
472 
473   UNEXPECTED_CALL;
474 }
475 
476 STUB_FUNCTION(uint16_t, controller_get_acl_data_size_classic, (void))
477   return 2048;
478 }
479 
480 STUB_FUNCTION(uint16_t, controller_get_acl_data_size_ble, (void))
481   return 2048;
482 }
483 
484 STUB_FUNCTION(void *, buffer_allocator_alloc, (size_t size))
485   DURING(ignoring_packets_ignored_packet) {
486     AT_CALL(0)
487       return NULL;
488 
489     UNEXPECTED_CALL;
490   }
491 
492   return allocator_malloc.alloc(size);
493 }
494 
495 STUB_FUNCTION(void, buffer_allocator_free, (void *ptr))
496   allocator_malloc.free(ptr);
497 }
498 
499 static void reset_for(TEST_MODES_T next) {
500   RESET_CALL_COUNT(vendor_open);
501   RESET_CALL_COUNT(vendor_close);
502   RESET_CALL_COUNT(vendor_set_callback);
503   RESET_CALL_COUNT(vendor_send_command);
504   RESET_CALL_COUNT(vendor_send_async_command);
505   RESET_CALL_COUNT(hal_init);
506   RESET_CALL_COUNT(hal_open);
507   RESET_CALL_COUNT(hal_close);
508   RESET_CALL_COUNT(hal_read_data);
509   RESET_CALL_COUNT(hal_packet_finished);
510   RESET_CALL_COUNT(hal_transmit_data);
511   RESET_CALL_COUNT(btsnoop_capture);
512   RESET_CALL_COUNT(hci_inject_open);
513   RESET_CALL_COUNT(hci_inject_close);
514   RESET_CALL_COUNT(low_power_init);
515   RESET_CALL_COUNT(low_power_cleanup);
516   RESET_CALL_COUNT(low_power_wake_assert);
517   RESET_CALL_COUNT(low_power_transmit_done);
518   RESET_CALL_COUNT(command_complete_callback);
519   RESET_CALL_COUNT(command_status_callback);
520   RESET_CALL_COUNT(controller_get_acl_data_size_classic);
521   RESET_CALL_COUNT(controller_get_acl_data_size_ble);
522   RESET_CALL_COUNT(buffer_allocator_alloc);
523   RESET_CALL_COUNT(buffer_allocator_free);
524   CURRENT_TEST_MODE = next;
525 }
526 
527 class HciLayerTest : public AlarmTestHarness {
528   protected:
529     virtual void SetUp() {
530       AlarmTestHarness::SetUp();
531       module_management_start();
532 
533       hci = hci_layer_get_test_interface(
534         &buffer_allocator,
535         &hal,
536         &btsnoop,
537         &hci_inject,
538         packet_fragmenter_get_test_interface(&controller, &allocator_malloc),
539         &vendor,
540         &low_power_manager
541       );
542 
543       packet_index = 0;
544       data_size_sum = 0;
545 
546       vendor.open = vendor_open;
547       vendor.close = vendor_close;
548       vendor.set_callback = vendor_set_callback;
549       vendor.send_command = vendor_send_command;
550       vendor.send_async_command = vendor_send_async_command;
551       hal.init = hal_init;
552       hal.open = hal_open;
553       hal.close = hal_close;
554       hal.read_data = hal_read_data;
555       hal.packet_finished = hal_packet_finished;
556       hal.transmit_data = hal_transmit_data;
557       btsnoop.capture = btsnoop_capture;
558       hci_inject.open = hci_inject_open;
559       hci_inject.close = hci_inject_close;
560       low_power_manager.init = low_power_init;
561       low_power_manager.cleanup = low_power_cleanup;
562       low_power_manager.wake_assert = low_power_wake_assert;
563       low_power_manager.transmit_done = low_power_transmit_done;
564       controller.get_acl_data_size_classic = controller_get_acl_data_size_classic;
565       controller.get_acl_data_size_ble = controller_get_acl_data_size_ble;
566       buffer_allocator.alloc = buffer_allocator_alloc;
567       buffer_allocator.free = buffer_allocator_free;
568 
569       done = semaphore_new(0);
570 
571       reset_for(start_up_async);
572       EXPECT_TRUE(module_start_up(&hci_module));
573 
574       EXPECT_CALL_COUNT(vendor_open, 1);
575       EXPECT_CALL_COUNT(hal_init, 1);
576       EXPECT_CALL_COUNT(low_power_init, 1);
577       EXPECT_CALL_COUNT(vendor_set_callback, 3);
578       EXPECT_CALL_COUNT(hal_open, 1);
579       EXPECT_CALL_COUNT(vendor_send_async_command, 1);
580     }
581 
582     virtual void TearDown() {
583       reset_for(shut_down);
584       module_shut_down(&hci_module);
585 
586       EXPECT_CALL_COUNT(low_power_cleanup, 1);
587       EXPECT_CALL_COUNT(hal_close, 1);
588       EXPECT_CALL_COUNT(vendor_send_command, 1);
589       EXPECT_CALL_COUNT(vendor_close, 1);
590 
591       semaphore_free(done);
592       hci_layer_cleanup_interface();
593       module_management_stop();
594       AlarmTestHarness::TearDown();
595     }
596 
597     hci_hal_t hal;
598     btsnoop_t btsnoop;
599     controller_t controller;
600     hci_inject_t hci_inject;
601     vendor_t vendor;
602     low_power_manager_t low_power_manager;
603     allocator_t buffer_allocator;
604 };
605 
606 TEST_F(HciLayerTest, test_postload) {
607   reset_for(postload);
608   hci->do_postload();
609 
610   flush_thread(internal_thread);
611   EXPECT_CALL_COUNT(vendor_send_async_command, 1);
612 }
613 
614 TEST_F(HciLayerTest, test_transmit_simple) {
615   reset_for(transmit_simple);
616   BT_HDR *packet = manufacture_packet(MSG_STACK_TO_HC_HCI_ACL, small_sample_data);
617   hci->transmit_downward(MSG_STACK_TO_HC_HCI_ACL, packet);
618 
619   flush_thread(internal_thread);
620   EXPECT_CALL_COUNT(hal_transmit_data, 1);
621   EXPECT_CALL_COUNT(btsnoop_capture, 1);
622   EXPECT_CALL_COUNT(low_power_transmit_done, 1);
623   EXPECT_CALL_COUNT(low_power_wake_assert, 1);
624 }
625 
626 TEST_F(HciLayerTest, test_receive_simple) {
627   reset_for(receive_simple);
628   data_to_receive = manufacture_packet(MSG_STACK_TO_HC_HCI_ACL, small_sample_data);
629 
630   // Not running on the internal thread, unlike the real hal
631   hal_callbacks->data_ready(DATA_TYPE_ACL);
632   EXPECT_CALL_COUNT(hal_packet_finished, 1);
633   EXPECT_CALL_COUNT(btsnoop_capture, 1);
634 
635   osi_free(data_to_receive);
636 }
637 
638 static BT_HDR *manufacture_command_complete(command_opcode_t opcode) {
639   BT_HDR *ret = (BT_HDR *)osi_calloc(sizeof(BT_HDR) + 5);
640   uint8_t *stream = ret->data;
641   UINT8_TO_STREAM(stream, HCI_COMMAND_COMPLETE_EVT);
642   UINT8_TO_STREAM(stream, 3); // length of the event parameters
643   UINT8_TO_STREAM(stream, 1); // the number of commands that can be sent
644   UINT16_TO_STREAM(stream, opcode);
645   ret->len = 5;
646 
647   return ret;
648 }
649 
650 static BT_HDR *manufacture_command_status(command_opcode_t opcode) {
651   BT_HDR *ret = (BT_HDR *)osi_calloc(sizeof(BT_HDR) + 6);
652   uint8_t *stream = ret->data;
653   UINT8_TO_STREAM(stream, HCI_COMMAND_STATUS_EVT);
654   UINT8_TO_STREAM(stream, 4); // length of the event parameters
655   UINT8_TO_STREAM(stream, HCI_PENDING); // status
656   UINT8_TO_STREAM(stream, 1); // the number of commands that can be sent
657   UINT16_TO_STREAM(stream, opcode);
658   ret->len = 6;
659 
660   return ret;
661 }
662 
663 TEST_F(HciLayerTest, test_transmit_command_no_callbacks) {
664   // Send a test command
665   reset_for(transmit_command_no_callbacks);
666   data_to_receive = manufacture_packet(MSG_STACK_TO_HC_HCI_CMD, command_sample_data);
667   hci->transmit_command(data_to_receive, NULL, NULL, NULL);
668 
669   flush_thread(internal_thread);
670   EXPECT_CALL_COUNT(hal_transmit_data, 1);
671   EXPECT_CALL_COUNT(btsnoop_capture, 1);
672   EXPECT_CALL_COUNT(low_power_transmit_done, 1);
673   EXPECT_CALL_COUNT(low_power_wake_assert, 1);
674 
675   // Send a response
676   command_opcode_t opcode = *((uint16_t *)command_sample_data);
677   data_to_receive = manufacture_command_complete(opcode);
678 
679   hal_callbacks->data_ready(DATA_TYPE_EVENT);
680   EXPECT_CALL_COUNT(hal_packet_finished, 1);
681   EXPECT_CALL_COUNT(btsnoop_capture, 2);
682 
683   osi_free(data_to_receive);
684 }
685 
686 TEST_F(HciLayerTest, test_transmit_command_command_status) {
687   // Send a test command
688   reset_for(transmit_command_command_status);
689   data_to_receive = manufacture_packet(MSG_STACK_TO_HC_HCI_CMD, command_sample_data);
690   hci->transmit_command(data_to_receive, command_complete_callback, command_status_callback, NULL);
691 
692   flush_thread(internal_thread);
693   EXPECT_CALL_COUNT(hal_transmit_data, 1);
694   EXPECT_CALL_COUNT(btsnoop_capture, 1);
695   EXPECT_CALL_COUNT(low_power_transmit_done, 1);
696   EXPECT_CALL_COUNT(low_power_wake_assert, 1);
697 
698   command_opcode_t opcode = *((uint16_t *)command_sample_data);
699 
700   // Send status event response
701   data_to_receive = manufacture_command_status(opcode);
702 
703   hal_callbacks->data_ready(DATA_TYPE_EVENT);
704   EXPECT_CALL_COUNT(hal_packet_finished, 1);
705   EXPECT_CALL_COUNT(btsnoop_capture, 2);
706   EXPECT_CALL_COUNT(command_status_callback, 1);
707 
708   osi_free(data_to_receive);
709 }
710 
711 TEST_F(HciLayerTest, test_transmit_command_command_complete) {
712   // Send a test command
713   reset_for(transmit_command_command_complete);
714   data_to_receive = manufacture_packet(MSG_STACK_TO_HC_HCI_CMD, command_sample_data);
715   hci->transmit_command(data_to_receive, command_complete_callback, command_status_callback, NULL);
716 
717   flush_thread(internal_thread);
718   EXPECT_CALL_COUNT(hal_transmit_data, 1);
719   EXPECT_CALL_COUNT(btsnoop_capture, 1);
720   EXPECT_CALL_COUNT(low_power_transmit_done, 1);
721   EXPECT_CALL_COUNT(low_power_wake_assert, 1);
722 
723   command_opcode_t opcode = *((uint16_t *)command_sample_data);
724 
725   // Send complete event response
726   data_to_receive = manufacture_command_complete(opcode);
727 
728   hal_callbacks->data_ready(DATA_TYPE_EVENT);
729   EXPECT_CALL_COUNT(hal_packet_finished, 1);
730   EXPECT_CALL_COUNT(btsnoop_capture, 2);
731   EXPECT_CALL_COUNT(command_complete_callback, 1);
732 
733   osi_free(data_to_receive);
734 }
735 
736 TEST_F(HciLayerTest, test_ignoring_packets) {
737   reset_for(ignoring_packets_ignored_packet);
738   data_to_receive = manufacture_packet(MSG_HC_TO_STACK_HCI_EVT, unignored_data);
739 
740   hal_callbacks->data_ready(DATA_TYPE_EVENT);
741   EXPECT_CALL_COUNT(buffer_allocator_alloc, 1);
742   EXPECT_CALL_COUNT(hal_packet_finished, 1);
743   EXPECT_CALL_COUNT(btsnoop_capture, 0);
744   osi_free(data_to_receive);
745 
746   reset_for(ignoring_packets_following_packet);
747   data_to_receive = manufacture_packet(MSG_STACK_TO_HC_HCI_ACL, ignored_data);
748 
749   hal_callbacks->data_ready(DATA_TYPE_ACL);
750   EXPECT_CALL_COUNT(buffer_allocator_alloc, 1);
751   EXPECT_CALL_COUNT(hal_packet_finished, 1);
752   EXPECT_CALL_COUNT(btsnoop_capture, 1);
753   osi_free(data_to_receive);
754 }
755 
756 // TODO(zachoverflow): test post-reassembly better, stub out fragmenter instead of using it
757