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