1 #include <stdint.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 #include "ble/att_db.h" 7 #include "hci.h" 8 #include "hci_dump.h" 9 #include "l2cap.h" 10 #include "rijndael.h" 11 #include "btstack_linked_list.h" 12 #include "btstack_run_loop_embedded.h" 13 #include "btstack_debug.h" 14 15 static btstack_packet_handler_t le_data_handler; 16 17 static uint8_t packet_buffer[256]; 18 static uint16_t packet_buffer_len = 0; 19 20 static uint8_t aes128_cyphertext[16]; 21 22 static hci_connection_t the_connection; 23 static btstack_linked_list_t connections; 24 static btstack_linked_list_t event_packet_handlers; 25 26 void mock_init(void){ 27 the_connection.item.next = NULL; 28 connections = (btstack_linked_item_t*) &the_connection; 29 } 30 31 uint8_t * mock_packet_buffer(void){ 32 return packet_buffer; 33 } 34 35 void mock_clear_packet_buffer(void){ 36 packet_buffer_len = 0; 37 memset(packet_buffer, 0, sizeof(packet_buffer)); 38 } 39 40 static void dump_packet(int packet_type, uint8_t * buffer, uint16_t size){ 41 #if 0 42 static int packet_counter = 1; 43 char var_name[80]; 44 sprintf(var_name, "test_%s_packet_%02u", packet_type == HCI_COMMAND_DATA_PACKET ? "command" : "acl", packet_counter); 45 printf("uint8_t %s[] = { ", var_name); 46 for (int i = 0; i < size ; i++){ 47 if ((i % 16) == 0) printf("\n "); 48 printf ("0x%02x, ", buffer[i]); 49 } 50 printf("};\n"); 51 packet_counter++; 52 #endif 53 } 54 55 void aes128_calc_cyphertext(uint8_t key[16], uint8_t plaintext[16], uint8_t cyphertext[16]){ 56 uint32_t rk[RKLENGTH(KEYBITS)]; 57 int nrounds = rijndaelSetupEncrypt(rk, &key[0], KEYBITS); 58 rijndaelEncrypt(rk, nrounds, plaintext, cyphertext); 59 } 60 61 void mock_simulate_hci_event(uint8_t * packet, uint16_t size){ 62 hci_dump_packet(HCI_EVENT_PACKET, 1, packet, size); 63 btstack_linked_list_iterator_t it; 64 btstack_linked_list_iterator_init(&it ,&event_packet_handlers); 65 while (btstack_linked_list_iterator_has_next(&it)){ 66 btstack_packet_callback_registration_t * item = (btstack_packet_callback_registration_t *) btstack_linked_list_iterator_next(&it); 67 item->callback(HCI_EVENT_PACKET, 0, packet, size); 68 } 69 if (le_data_handler){ 70 le_data_handler(HCI_EVENT_PACKET, 0, packet, size); 71 } 72 } 73 74 void aes128_report_result(void){ 75 uint8_t le_enc_result[22]; 76 uint8_t enc1_data[] = { 0x0e, 0x14, 0x01, 0x17, 0x20, 0x00 }; 77 memcpy (le_enc_result, enc1_data, 6); 78 reverse_128(aes128_cyphertext, &le_enc_result[6]); 79 mock_simulate_hci_event(&le_enc_result[0], sizeof(le_enc_result)); 80 } 81 82 void mock_simulate_sm_data_packet(uint8_t * packet, uint16_t len){ 83 84 uint16_t handle = 0x40; 85 uint16_t cid = 0x06; 86 87 uint8_t acl_buffer[len + 8]; 88 89 // 0 - Connection handle : PB=10 : BC=00 90 little_endian_store_16(acl_buffer, 0, handle | (0 << 12) | (0 << 14)); 91 // 2 - ACL length 92 little_endian_store_16(acl_buffer, 2, len + 4); 93 // 4 - L2CAP packet length 94 little_endian_store_16(acl_buffer, 4, len + 0); 95 // 6 - L2CAP channel DEST 96 little_endian_store_16(acl_buffer, 6, cid); 97 98 memcpy(&acl_buffer[8], packet, len); 99 hci_dump_packet(HCI_ACL_DATA_PACKET, 1, &acl_buffer[0], len + 8); 100 101 le_data_handler(SM_DATA_PACKET, handle, packet, len); 102 103 // process queued callbacks, might become obsolete after queued callback integration in run loop 104 btstack_run_loop_embedded_execute_once(); 105 } 106 107 void mock_simulate_command_complete(const hci_cmd_t *cmd){ 108 uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, (uint8_t) cmd->opcode & 0xff, (uint8_t) cmd->opcode >> 8, 0}; 109 mock_simulate_hci_event((uint8_t *)&packet, sizeof(packet)); 110 } 111 112 void mock_simulate_hci_state_working(void){ 113 uint8_t packet[] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING}; 114 mock_simulate_hci_event((uint8_t *)&packet, sizeof(packet)); 115 } 116 117 118 static void hci_create_gap_connection_complete_event(const uint8_t * hci_event, uint8_t * gap_event) { 119 gap_event[0] = HCI_EVENT_META_GAP; 120 gap_event[1] = 36 - 2; 121 gap_event[2] = GAP_SUBEVENT_LE_CONNECTION_COMPLETE; 122 switch (hci_event[2]){ 123 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 124 memcpy(&gap_event[3], &hci_event[3], 11); 125 memset(&gap_event[14], 0, 12); 126 memcpy(&gap_event[26], &hci_event[14], 7); 127 memset(&gap_event[33], 0xff, 3); 128 break; 129 case HCI_SUBEVENT_LE_ENHANCED_CONNECTION_COMPLETE_V1: 130 memcpy(&gap_event[3], &hci_event[3], 30); 131 memset(&gap_event[33], 0xff, 3); 132 break; 133 case HCI_SUBEVENT_LE_ENHANCED_CONNECTION_COMPLETE_V2: 134 memcpy(&gap_event[3], &hci_event[3], 33); 135 break; 136 default: 137 btstack_unreachable(); 138 break; 139 } 140 } 141 142 void mock_simulate_connected(void){ 143 uint8_t packet[] = { 0x3e, 0x13, 0x01, 0x00, 0x40, 0x00, 0x01, 0x01, 0x18, 0x12, 0x5e, 0x68, 0xc9, 0x73, 0x18, 0x00, 0x00, 0x00, 0x48, 0x00, 0x05}; 144 uint8_t gap_event[36]; 145 hci_create_gap_connection_complete_event(packet, gap_event); 146 mock_simulate_hci_event(gap_event, sizeof(gap_event)); 147 } 148 149 void att_init_connection(att_connection_t * att_connection){ 150 att_connection->mtu = 23; 151 att_connection->encryption_key_size = 0; 152 att_connection->authenticated = 0; 153 att_connection->authorized = 0; 154 } 155 156 void gap_local_bd_addr(bd_addr_t address_buffer){ 157 int i; 158 for (i=0;i<6;i++) { 159 address_buffer[i] = 0x11 * (i+1); 160 } 161 } 162 163 void hci_halting_defer(void){ 164 } 165 166 bool hci_can_send_command_packet_now(void){ 167 return true; 168 } 169 bool hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){ 170 return true; 171 } 172 173 hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){ 174 return &the_connection; 175 } 176 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 177 return &the_connection; 178 } 179 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 180 btstack_linked_list_iterator_init(it, &connections); 181 } 182 183 // get addr type and address used in different contexts 184 void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr){ 185 static uint8_t dummy[] = { 0x00, 0x1b, 0xdc, 0x07, 0x32, 0xef }; 186 *addr_type = 0; 187 memcpy(addr, dummy, 6); 188 } 189 void gap_le_get_own_advertisements_address(uint8_t * addr_type, bd_addr_t addr){ 190 gap_le_get_own_address(addr_type, addr); 191 } 192 void gap_le_get_own_connection_address(uint8_t * addr_type, bd_addr_t addr){ 193 gap_le_get_own_address(addr_type, addr); 194 } 195 196 void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 197 uint8_t direct_address_typ, bd_addr_t direct_address, uint8_t channel_map, uint8_t filter_policy) { 198 } 199 200 uint16_t hci_get_manufacturer(void){ 201 return 0xffff; 202 }; 203 204 void hci_le_set_own_address_type(uint8_t own_address){ 205 } 206 207 void hci_le_random_address_set(const bd_addr_t addr){ 208 } 209 210 void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t cid){ 211 if (packet_buffer_len) return; 212 uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 0, 0}; 213 little_endian_store_16(event, 2, cid); 214 le_data_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 215 } 216 217 bool l2cap_can_send_connectionless_packet_now(void){ 218 return packet_buffer_len == 0; 219 } 220 221 bool l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){ 222 return packet_buffer_len == 0; 223 } 224 225 uint8_t hci_send_cmd(const hci_cmd_t *cmd, ...){ 226 va_list argptr; 227 va_start(argptr, cmd); 228 uint16_t len = hci_cmd_create_from_template(packet_buffer, cmd, argptr); 229 va_end(argptr); 230 hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet_buffer, len); 231 dump_packet(HCI_COMMAND_DATA_PACKET, packet_buffer, len); 232 packet_buffer_len = len; 233 234 // track le encrypt and le rand 235 if (cmd->opcode == hci_le_encrypt.opcode){ 236 uint8_t * key_flipped = &packet_buffer[3]; 237 uint8_t key[16]; 238 reverse_128(key_flipped, key); 239 // printf("le_encrypt key "); 240 // hexdump(key, 16); 241 uint8_t * plaintext_flipped = &packet_buffer[19]; 242 uint8_t plaintext[16]; 243 reverse_128(plaintext_flipped, plaintext); 244 // printf("le_encrypt txt "); 245 // hexdump(plaintext, 16); 246 aes128_calc_cyphertext(key, plaintext, aes128_cyphertext); 247 // printf("le_encrypt res "); 248 // hexdump(aes128_cyphertext, 16); 249 } 250 return ERROR_CODE_SUCCESS; 251 } 252 253 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) { 254 le_data_handler = packet_handler; 255 } 256 257 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 258 btstack_linked_list_add(&event_packet_handlers, (btstack_linked_item_t *) callback_handler); 259 } 260 261 bool l2cap_reserve_packet_buffer(void){ 262 printf("l2cap_reserve_packet_buffer\n"); 263 return true; 264 } 265 266 uint8_t l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 267 printf("l2cap_send_prepared_connectionless\n"); 268 return 0; 269 } 270 271 uint8_t l2cap_send_connectionless(uint16_t handle, uint16_t cid, uint8_t * buffer, uint16_t len){ 272 // printf("l2cap_send_connectionless\n"); 273 274 int pb = hci_non_flushable_packet_boundary_flag_supported() ? 0x00 : 0x02; 275 276 // 0 - Connection handle : PB=pb : BC=00 277 little_endian_store_16(packet_buffer, 0, handle | (pb << 12) | (0 << 14)); 278 // 2 - ACL length 279 little_endian_store_16(packet_buffer, 2, len + 4); 280 // 4 - L2CAP packet length 281 little_endian_store_16(packet_buffer, 4, len + 0); 282 // 6 - L2CAP channel DEST 283 little_endian_store_16(packet_buffer, 6, cid); 284 285 memcpy(&packet_buffer[8], buffer, len); 286 hci_dump_packet(HCI_ACL_DATA_PACKET, 0, &packet_buffer[0], len + 8); 287 288 dump_packet(HCI_ACL_DATA_PACKET, packet_buffer, len + 8); 289 packet_buffer_len = len + 8; 290 291 return ERROR_CODE_SUCCESS; 292 } 293 294 void hci_disconnect_security_block(hci_con_handle_t con_handle){ 295 printf("hci_disconnect_security_block \n"); 296 } 297 298 bool hci_non_flushable_packet_boundary_flag_supported(void){ 299 return true; 300 } 301 302 void l2cap_run(void){ 303 } 304 305 HCI_STATE hci_get_state(void){ 306 return HCI_STATE_WORKING; 307 } 308 309 #include "hal_cpu.h" 310 void hal_cpu_disable_irqs(void){} 311 void hal_cpu_enable_irqs(void){} 312 void hal_cpu_enable_irqs_and_sleep(void){} 313 314 #include "hal_time_ms.h" 315 static uint32_t time_ms; 316 uint32_t hal_time_ms(void){ 317 return time_ms++; 318 } 319