1 #include <stdint.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 #include "hci.h" 7 #include "hci_dump.h" 8 #include "l2cap.h" 9 10 #include "ble/att_db.h" 11 #include "ble/gatt_client.h" 12 #include "ble/sm.h" 13 14 static btstack_packet_handler_t att_packet_handler; 15 static void (*registered_hci_event_handler) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL; 16 17 static btstack_linked_list_t connections; 18 static const uint16_t max_mtu = 23; 19 static uint8_t l2cap_stack_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 8 + max_mtu]; // pre buffer + HCI Header + L2CAP header 20 static uint16_t gatt_client_handle = 0x40; 21 static hci_connection_t hci_connection; 22 23 uint16_t get_gatt_client_handle(void){ 24 return gatt_client_handle; 25 } 26 27 void mock_simulate_command_complete(const hci_cmd_t *cmd){ 28 uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, (uint8_t) (cmd->opcode & 0xff), (uint8_t) (cmd->opcode >> 8), 0}; 29 registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet)); 30 } 31 32 void mock_simulate_hci_state_working(void){ 33 uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING}; 34 registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, 3); 35 } 36 37 void mock_simulate_connected(void){ 38 uint8_t packet[] = {0x3E, 0x13, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x50, 0x00, 0x00, 0x00, 0xD0, 0x07, 0x05}; 39 registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet)); 40 } 41 42 void mock_simulate_scan_response(void){ 43 uint8_t packet[] = {0xE2, 0x13, 0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B, 0xCC, 0x09, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; 44 registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet)); 45 } 46 47 void gap_start_scan(void){ 48 } 49 void gap_stop_scan(void){ 50 } 51 uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type){ 52 return 0; 53 } 54 void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 55 } 56 57 int gap_reconnect_security_setup_active(hci_con_handle_t con_handle){ 58 UNUSED(con_handle); 59 return 0; 60 } 61 62 static void att_init_connection(att_connection_t * att_connection){ 63 att_connection->mtu = 23; 64 att_connection->max_mtu = 23; 65 att_connection->encryption_key_size = 0; 66 att_connection->authenticated = 0; 67 att_connection->authorized = 0; 68 } 69 70 int hci_can_send_acl_le_packet_now(void){ 71 return 1; 72 } 73 74 int l2cap_can_send_connectionless_packet_now(void){ 75 return 1; 76 } 77 78 uint8_t *l2cap_get_outgoing_buffer(void){ 79 // printf("l2cap_get_outgoing_buffer\n"); 80 return (uint8_t *)&l2cap_stack_buffer; // 8 bytes 81 } 82 83 uint16_t l2cap_max_mtu(void){ 84 // printf("l2cap_max_mtu\n"); 85 return max_mtu; 86 } 87 88 uint16_t l2cap_max_le_mtu(void){ 89 // printf("l2cap_max_mtu\n"); 90 return max_mtu; 91 } 92 93 void l2cap_init(void){} 94 95 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) { 96 att_packet_handler = packet_handler; 97 } 98 99 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 100 registered_hci_event_handler = callback_handler->callback; 101 } 102 103 int l2cap_reserve_packet_buffer(void){ 104 return 1; 105 } 106 107 int l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){ 108 return 1; 109 } 110 111 void l2cap_request_can_send_fix_channel_now_event(uint16_t handle, uint16_t channel_id){ 112 uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 1, 0}; 113 att_packet_handler(HCI_EVENT_PACKET, 0, (uint8_t*)event, sizeof(event)); 114 } 115 116 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 117 att_connection_t att_connection; 118 att_init_connection(&att_connection); 119 uint8_t response[max_mtu]; 120 uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, &response[0]); 121 if (response_len){ 122 att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len); 123 } 124 return 0; 125 } 126 127 void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 128 } 129 130 int sm_cmac_ready(void){ 131 return 1; 132 } 133 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)){ 134 //sm_notify_client(SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched); 135 } 136 int sm_le_device_index(uint16_t handle ){ 137 return -1; 138 } 139 void sm_send_security_request(hci_con_handle_t con_handle){ 140 } 141 142 irk_lookup_state_t sm_identity_resolving_state(hci_con_handle_t con_handle){ 143 return IRK_LOOKUP_SUCCEEDED; 144 } 145 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 146 } 147 148 // Set callback that will be executed when timer expires. 149 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){ 150 } 151 152 // Add/Remove timer source. 153 void btstack_run_loop_add_timer(btstack_timer_source_t *timer){ 154 } 155 156 int btstack_run_loop_remove_timer(btstack_timer_source_t *timer){ 157 return 1; 158 } 159 160 // todo: 161 hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 162 printf("hci_connection_for_bd_addr_and_type not implemented in mock backend\n"); 163 return NULL; 164 } 165 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 166 return &hci_connection; 167 } 168 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 169 // printf("hci_connections_get_iterator not implemented in mock backend\n"); 170 btstack_linked_list_iterator_init(it, &connections); 171 } 172 173 // int hci_send_cmd(const hci_cmd_t *cmd, ...){ 174 // // printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode); 175 // return 0; 176 // } 177 178 // int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){ 179 // return 1; 180 // } 181 182 // void hci_disconnect_security_block(hci_con_handle_t con_handle){ 183 // printf("hci_disconnect_security_block \n"); 184 // } 185 186 // void hci_dump_log(const char * format, ...){ 187 // printf("hci_disconnect_security_block \n"); 188 // } 189 190 void l2cap_run(void){ 191 } 192