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