1 #include <stdint.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 #include "CppUTest/TestHarness.h" 7 #include "CppUTest/CommandLineTestRunner.h" 8 #include "CppUTestExt/MockSupport.h" 9 10 #include "hci_cmd.h" 11 12 #include "btstack_memory.h" 13 #include "hci.h" 14 #include "ble/gatt_client.h" 15 #include "btstack_event.h" 16 #include "hci_dump.h" 17 #include "hci_dump_posix_fs.h" 18 #include "btstack_debug.h" 19 20 typedef struct { 21 uint8_t type; 22 uint16_t size; 23 uint8_t buffer[258]; 24 } hci_packet_t; 25 26 #define MAX_HCI_PACKETS 10 27 static uint16_t transport_count_packets; 28 static hci_packet_t transport_packets[MAX_HCI_PACKETS]; 29 30 static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size); 31 32 static const uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 33 34 static int hci_transport_test_set_baudrate(uint32_t baudrate){ 35 return 0; 36 } 37 38 static int hci_transport_test_can_send_now(uint8_t packet_type){ 39 return 1; 40 } 41 42 static int hci_transport_test_send_packet(uint8_t packet_type, uint8_t * packet, int size){ 43 btstack_assert(transport_count_packets < MAX_HCI_PACKETS); 44 memcpy(transport_packets[transport_count_packets].buffer, packet, size); 45 transport_packets[transport_count_packets].type = packet_type; 46 transport_packets[transport_count_packets].size = size; 47 transport_count_packets++; 48 // notify upper stack that it can send again 49 packet_handler(HCI_EVENT_PACKET, (uint8_t *) &packet_sent_event[0], sizeof(packet_sent_event)); 50 return 0; 51 } 52 53 static void hci_transport_test_init(const void * transport_config){ 54 } 55 56 static int hci_transport_test_open(void){ 57 return 0; 58 } 59 60 static int hci_transport_test_close(void){ 61 return 0; 62 } 63 64 static void hci_transport_test_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 65 packet_handler = handler; 66 } 67 68 static const hci_transport_t hci_transport_test = { 69 /* const char * name; */ "TEST", 70 /* void (*init) (const void *transport_config); */ &hci_transport_test_init, 71 /* int (*open)(void); */ &hci_transport_test_open, 72 /* int (*close)(void); */ &hci_transport_test_close, 73 /* void (*register_packet_handler)(void (*handler)(...); */ &hci_transport_test_register_packet_handler, 74 /* int (*can_send_packet_now)(uint8_t packet_type); */ &hci_transport_test_can_send_now, 75 /* int (*send_packet)(...); */ &hci_transport_test_send_packet, 76 /* int (*set_baudrate)(uint32_t baudrate); */ &hci_transport_test_set_baudrate, 77 /* void (*reset_link)(void); */ NULL, 78 /* void (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL, 79 }; 80 81 static uint16_t next_hci_packet; 82 83 void CHECK_EQUAL_ARRAY(const uint8_t * expected, uint8_t * actual, int size){ 84 for (int i=0; i<size; i++){ 85 BYTES_EQUAL(expected[i], actual[i]); 86 } 87 } 88 89 void CHECK_HCI_COMMAND(const hci_cmd_t * expected_hci_command){ 90 uint16_t actual_opcode = little_endian_read_16(transport_packets[next_hci_packet].buffer, 0); 91 next_hci_packet++; 92 CHECK_EQUAL(expected_hci_command->opcode, actual_opcode); 93 } 94 95 TEST_GROUP(HCI){ 96 void setup(void){ 97 transport_count_packets = 0; 98 next_hci_packet = 0; 99 hci_init(&hci_transport_test, NULL); 100 hci_simulate_working_fuzz(); 101 hci_setup_test_connections_fuzz(); 102 // register for HCI events 103 mock().expectOneCall("hci_can_send_packet_now_using_packet_buffer").andReturnValue(1); 104 } 105 void teardown(void){ 106 mock().clear(); 107 } 108 }; 109 110 TEST(HCI, GetSetConnectionRange){ 111 le_connection_parameter_range_t range; 112 gap_get_connection_parameter_range(&range); 113 gap_set_connection_parameter_range(&range); 114 } 115 116 TEST(HCI, ConnectionRangeValid){ 117 le_connection_parameter_range_t range = { 118 1, 10, 119 1, 10, 120 1,10 121 }; 122 CHECK_EQUAL( 0, gap_connection_parameter_range_included(&range, 0, 0, 0, 0)); 123 CHECK_EQUAL( 0, gap_connection_parameter_range_included(&range, 2, 0, 0, 0)); 124 CHECK_EQUAL( 0, gap_connection_parameter_range_included(&range, 2, 9, 0, 0)); 125 CHECK_EQUAL( 0, gap_connection_parameter_range_included(&range, 2, 9, 10, 0)); 126 CHECK_EQUAL( 0, gap_connection_parameter_range_included(&range, 2, 9, 5, 0)); 127 CHECK_EQUAL( 0, gap_connection_parameter_range_included(&range, 2, 9, 5, 11)); 128 CHECK_EQUAL( 1, gap_connection_parameter_range_included(&range, 2, 9, 5, 5)); 129 } 130 131 TEST(HCI, NumPeripherals){ 132 gap_set_max_number_peripheral_connections(1); 133 } 134 135 TEST(HCI, MaxAclLen){ 136 hci_max_acl_data_packet_length(); 137 } 138 139 TEST(HCI, Flushable){ 140 hci_non_flushable_packet_boundary_flag_supported(); 141 } 142 143 TEST(HCI, DoubleReserve){ 144 CHECK_TRUE(hci_reserve_packet_buffer()); 145 CHECK_FALSE(hci_reserve_packet_buffer()); 146 } 147 148 TEST(HCI, RemovePacketHandler){ 149 hci_remove_event_handler(NULL); 150 } 151 152 static void dummy_fn(const void * config){}; 153 TEST(HCI, SetChipset){ 154 hci_set_chipset(NULL); 155 btstack_chipset_t chipset_driver = { 0 }; 156 hci_set_chipset(NULL); 157 chipset_driver.init = dummy_fn; 158 } 159 160 TEST(HCI, SetControl){ 161 btstack_control_t hardware_control = { .init = &dummy_fn}; 162 hci_set_control(&hardware_control); 163 } 164 165 //TEST(HCI, Close){ 166 // hci_close(); 167 //} 168 169 TEST(HCI, SetPublicAddress){ 170 bd_addr_t addr = { 0 }; 171 hci_set_bd_addr(addr); 172 } 173 174 TEST(HCI, DisconnectSecurityBlock){ 175 hci_disconnect_security_block(HCI_CON_HANDLE_INVALID); 176 hci_disconnect_security_block(3); 177 } 178 179 TEST(HCI, SetDuplicateFilter){ 180 gap_set_scan_duplicate_filter(true); 181 } 182 183 TEST(HCI, ConnectCancel){ 184 gap_connect_cancel(); 185 } 186 187 TEST(HCI, SetGapConnParams){ 188 gap_set_connection_parameters(0, 0, 0, 0, 0, 0, 0, 0); 189 } 190 191 TEST(HCI, UpdateConnParams){ 192 gap_update_connection_parameters(HCI_CON_HANDLE_INVALID, 0, 0, 0, 0); 193 gap_update_connection_parameters(5, 0, 0, 0, 0); 194 } 195 196 TEST(HCI, RequestConnParamUpdate){ 197 gap_request_connection_parameter_update(HCI_CON_HANDLE_INVALID, 0, 0, 0, 0); 198 gap_request_connection_parameter_update(5, 0, 0, 0, 0); 199 } 200 201 TEST(HCI, SetScanResponse){ 202 gap_scan_response_set_data(0, NULL); 203 } 204 205 TEST(HCI, SetAddrType){ 206 hci_le_set_own_address_type(0); 207 hci_le_set_own_address_type(1); 208 } 209 210 TEST(HCI, AdvEnable){ 211 gap_advertisements_enable(0); 212 gap_advertisements_enable(1); 213 } 214 215 TEST(HCI, SetRandomAddr){ 216 bd_addr_t addr = { 0 }; 217 hci_le_random_address_set(addr); 218 } 219 220 TEST(HCI, Disconnect){ 221 gap_disconnect(HCI_CON_HANDLE_INVALID); 222 gap_disconnect(5); 223 } 224 225 TEST(HCI, GetRole){ 226 gap_get_role(HCI_CON_HANDLE_INVALID); 227 gap_get_role(5); 228 } 229 230 int main (int argc, const char * argv[]){ 231 return CommandLineTestRunner::RunAllTests(argc, argv); 232 } 233