1 #include <stdint.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 #include "hci.h" 6 #include "hci_dump.h" 7 #include "l2cap.h" 8 9 #include "ble/att_db.h" 10 #include "ble/sm.h" 11 #include "gap.h" 12 #include "btstack_debug.h" 13 14 #define PREBUFFER_SIZE (HCI_INCOMING_PRE_BUFFER_SIZE + 8) 15 #define TEST_MAX_MTU 23 16 17 static btstack_packet_handler_t att_packet_handler; 18 static void (*registered_hci_event_handler) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL; 19 20 static btstack_linked_list_t connections; 21 static uint8_t l2cap_stack_buffer[PREBUFFER_SIZE + TEST_MAX_MTU]; // pre buffer + HCI Header + L2CAP header 22 static uint16_t gatt_client_handle = 0x40; 23 static hci_connection_t hci_connection; 24 25 static uint8_t packet_buffer[256]; 26 static uint16_t packet_buffer_len; 27 28 uint16_t get_gatt_client_handle(void){ 29 return gatt_client_handle; 30 } 31 32 void mock_simulate_command_complete(const hci_cmd_t *cmd){ 33 uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, (uint8_t) (cmd->opcode & 0xff), (uint8_t) (cmd->opcode >> 8), 0}; 34 registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet)); 35 } 36 37 void mock_simulate_hci_state_working(void){ 38 uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING}; 39 registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, 3); 40 } 41 42 static void hci_create_gap_connection_complete_event(const uint8_t * hci_event, uint8_t * gap_event) { 43 gap_event[0] = HCI_EVENT_META_GAP; 44 gap_event[1] = 36 - 2; 45 gap_event[2] = GAP_SUBEVENT_LE_CONNECTION_COMPLETE; 46 switch (hci_event[2]){ 47 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 48 memcpy(&gap_event[3], &hci_event[3], 11); 49 memset(&gap_event[14], 0, 12); 50 memcpy(&gap_event[26], &hci_event[14], 7); 51 memset(&gap_event[33], 0xff, 3); 52 break; 53 case HCI_SUBEVENT_LE_ENHANCED_CONNECTION_COMPLETE_V1: 54 memcpy(&gap_event[3], &hci_event[3], 30); 55 memset(&gap_event[33], 0xff, 3); 56 break; 57 case HCI_SUBEVENT_LE_ENHANCED_CONNECTION_COMPLETE_V2: 58 memcpy(&gap_event[3], &hci_event[3], 33); 59 break; 60 default: 61 btstack_unreachable(); 62 break; 63 } 64 } 65 66 void mock_simulate_connected(void){ 67 uint8_t packet[] = {HCI_EVENT_LE_META, 0x13, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x50, 0x00, 0x00, 0x00, 0xD0, 0x07, 0x05}; 68 uint8_t gap_event[36]; 69 hci_create_gap_connection_complete_event(packet, gap_event); 70 registered_hci_event_handler(HCI_EVENT_PACKET, 0, gap_event, sizeof(gap_event)); 71 } 72 73 void mock_simulate_scan_response(void){ 74 uint8_t packet[] = {GAP_EVENT_ADVERTISING_REPORT, 0x13, 0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B, 0xCC, 0x09, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; 75 registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet)); 76 } 77 bool gap_authenticated(hci_con_handle_t con_handle){ 78 UNUSED(con_handle); 79 return false; 80 } 81 uint8_t gap_encryption_key_size(hci_con_handle_t con_handle){ 82 UNUSED(con_handle); 83 return 0; 84 } 85 bool gap_bonded(hci_con_handle_t con_handle){ 86 UNUSED(con_handle); 87 return true; 88 } 89 void sm_request_pairing(hci_con_handle_t con_handle){ 90 UNUSED(con_handle); 91 } 92 void gap_start_scan(void){ 93 } 94 void gap_stop_scan(void){ 95 } 96 uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){ 97 return 0; 98 } 99 void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 100 } 101 102 int gap_reconnect_security_setup_active(hci_con_handle_t con_handle){ 103 UNUSED(con_handle); 104 return 0; 105 } 106 107 static void att_init_connection(att_connection_t * att_connection){ 108 att_connection->mtu = TEST_MAX_MTU; 109 att_connection->max_mtu = TEST_MAX_MTU; 110 att_connection->encryption_key_size = 0; 111 att_connection->authenticated = 0; 112 att_connection->authorized = 0; 113 } 114 115 HCI_STATE hci_get_state(void){ 116 return HCI_STATE_WORKING; 117 } 118 119 uint8_t hci_send_cmd(const hci_cmd_t *cmd, ...){ 120 121 btstack_assert(false); 122 123 va_list argptr; 124 va_start(argptr, cmd); 125 uint16_t len = hci_cmd_create_from_template(packet_buffer, cmd, argptr); 126 va_end(argptr); 127 hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet_buffer, len); 128 // dump_packet(HCI_COMMAND_DATA_PACKET, packet_buffer, len); 129 packet_buffer_len = len; 130 return ERROR_CODE_SUCCESS; 131 } 132 133 bool hci_can_send_command_packet_now(void){ 134 return true; 135 } 136 137 bool hci_can_send_acl_le_packet_now(void){ 138 return true; 139 } 140 141 bool l2cap_can_send_connectionless_packet_now(void){ 142 return true; 143 } 144 145 uint8_t *l2cap_get_outgoing_buffer(void){ 146 return (uint8_t *)&l2cap_stack_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 8]; 147 } 148 149 uint16_t l2cap_max_mtu(void){ 150 return TEST_MAX_MTU; 151 } 152 153 uint16_t l2cap_max_le_mtu(void){ 154 return TEST_MAX_MTU; 155 } 156 157 void l2cap_init(void){} 158 159 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) { 160 att_packet_handler = packet_handler; 161 } 162 163 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 164 registered_hci_event_handler = callback_handler->callback; 165 } 166 167 bool l2cap_reserve_packet_buffer(void){ 168 return true; 169 } 170 171 bool l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){ 172 return true; 173 } 174 175 void l2cap_request_can_send_fix_channel_now_event(uint16_t handle, uint16_t channel_id){ 176 uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 1, 0}; 177 att_packet_handler(HCI_EVENT_PACKET, 0, (uint8_t*)event, sizeof(event)); 178 } 179 180 uint8_t l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 181 att_connection_t att_connection; 182 att_init_connection(&att_connection); 183 uint8_t response_buffer[PREBUFFER_SIZE + TEST_MAX_MTU]; 184 uint8_t * response = &response_buffer[PREBUFFER_SIZE]; 185 uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, response); 186 if (response_len){ 187 att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len); 188 } 189 return ERROR_CODE_SUCCESS; 190 } 191 192 void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 193 } 194 195 int sm_cmac_ready(void){ 196 return 1; 197 } 198 void sm_cmac_signed_write_start(const sm_key_t key, uint8_t opcode, uint16_t attribute_handle, uint16_t message_len, const uint8_t * message, uint32_t sign_counter, void (*done_callback)(uint8_t * hash)){ 199 //sm_notify_client(SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched); 200 } 201 int sm_le_device_index(uint16_t handle ){ 202 return 0; 203 } 204 void sm_send_security_request(hci_con_handle_t con_handle){ 205 } 206 207 uint8_t sm_get_ltk(hci_con_handle_t con_handle, sm_key_t ltk){ 208 memset((uint8_t*) ltk, 0x22, 16); 209 return ERROR_CODE_SUCCESS; 210 } 211 212 irk_lookup_state_t sm_identity_resolving_state(hci_con_handle_t con_handle){ 213 return IRK_LOOKUP_SUCCEEDED; 214 } 215 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 216 } 217 218 // Set callback that will be executed when timer expires. 219 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){ 220 } 221 222 // Add/Remove timer source. 223 void btstack_run_loop_add_timer(btstack_timer_source_t *timer){ 224 } 225 226 int btstack_run_loop_remove_timer(btstack_timer_source_t *timer){ 227 return 1; 228 } 229 230 // todo: 231 hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){ 232 printf("hci_connection_for_bd_addr_and_type not implemented in mock backend\n"); 233 return NULL; 234 } 235 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 236 if (con_handle == hci_connection.con_handle){ 237 return &hci_connection; 238 } else { 239 return NULL; 240 } 241 } 242 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 243 // printf("hci_connections_get_iterator not implemented in mock backend\n"); 244 btstack_linked_list_iterator_init(it, &connections); 245 } 246 247 static void hci_setup_connection(uint16_t con_handle, bd_addr_type_t type){ 248 hci_connection.att_connection.mtu = 23; 249 hci_connection.att_connection.con_handle = con_handle; 250 hci_connection.att_connection.max_mtu = 23; 251 hci_connection.att_connection.encryption_key_size = 0; 252 hci_connection.att_connection.authenticated = 0; 253 hci_connection.att_connection.authorized = 0; 254 255 hci_connection.att_server.ir_le_device_db_index = 0; 256 257 hci_connection.con_handle = con_handle; 258 hci_connection.address_type = type; 259 260 if (btstack_linked_list_empty(&connections)){ 261 btstack_linked_list_add(&connections, (btstack_linked_item_t *)&hci_connection); 262 } 263 } 264 265 void hci_setup_le_connection(uint16_t con_handle){ 266 hci_setup_connection(con_handle, BD_ADDR_TYPE_LE_PUBLIC); 267 } 268 269 // int hci_send_cmd(const hci_cmd_t *cmd, ...){ 270 // // printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode); 271 // return 0; 272 // } 273 274 // int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){ 275 // return 1; 276 // } 277 278 // void hci_disconnect_security_block(hci_con_handle_t con_handle){ 279 // printf("hci_disconnect_security_block \n"); 280 // } 281 282 // void hci_dump_log(const char * format, ...){ 283 // printf("hci_disconnect_security_block \n"); 284 // } 285 286 void l2cap_run(void){ 287 } 288