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.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, 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 uint16_t gatt_client_handle = 0x40; 21 22 uint16_t get_gatt_client_handle(void){ 23 return gatt_client_handle; 24 } 25 26 void mock_simulate_command_complete(const hci_cmd_t *cmd){ 27 uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, cmd->opcode & 0xff, cmd->opcode >> 8, 0}; 28 registered_hci_event_handler(HCI_EVENT_PACKET, (uint8_t *)&packet, sizeof(packet)); 29 } 30 31 void mock_simulate_hci_state_working(void){ 32 uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING}; 33 registered_hci_event_handler(HCI_EVENT_PACKET, (uint8_t *)&packet, 3); 34 } 35 36 void mock_simulate_connected(void){ 37 uint8_t packet[] = {0x3E, 0x13, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x50, 0x00, 0x00, 0x00, 0xD0, 0x07, 0x05}; 38 registered_hci_event_handler(HCI_EVENT_PACKET, (uint8_t *)&packet, sizeof(packet)); 39 } 40 41 void mock_simulate_scan_response(void){ 42 uint8_t packet[] = {0xE2, 0x13, 0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B, 0xCC, 0x09, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; 43 registered_hci_event_handler(HCI_EVENT_PACKET, (uint8_t *)&packet, sizeof(packet)); 44 } 45 46 uint8_t le_central_start_scan(void){ 47 return 0; 48 } 49 uint8_t le_central_stop_scan(void){ 50 return 0; 51 } 52 uint8_t le_central_connect(bd_addr_t addr, bd_addr_type_t addr_type){ 53 return 0; 54 } 55 void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 56 } 57 58 static void att_init_connection(att_connection_t * att_connection){ 59 att_connection->mtu = 23; 60 att_connection->max_mtu = 23; 61 att_connection->encryption_key_size = 0; 62 att_connection->authenticated = 0; 63 att_connection->authorized = 0; 64 } 65 66 int l2cap_can_send_connectionless_packet_now(void){ 67 return 1; 68 } 69 70 71 uint8_t *l2cap_get_outgoing_buffer(void){ 72 // printf("l2cap_get_outgoing_buffer\n"); 73 return (uint8_t *)&l2cap_stack_buffer; // 8 bytes 74 } 75 76 uint16_t l2cap_max_mtu(void){ 77 // printf("l2cap_max_mtu\n"); 78 return max_mtu; 79 } 80 81 uint16_t l2cap_max_le_mtu(void){ 82 // printf("l2cap_max_mtu\n"); 83 return max_mtu; 84 } 85 86 void l2cap_init(){} 87 88 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) { 89 att_packet_handler = packet_handler; 90 } 91 92 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 93 registered_hci_event_handler = callback_handler->callback; 94 } 95 96 int l2cap_reserve_packet_buffer(void){ 97 return 1; 98 } 99 100 int l2cap_can_send_fixed_channel_packet_now(uint16_t handle){ 101 return 1; 102 } 103 104 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 105 att_connection_t att_connection; 106 att_init_connection(&att_connection); 107 uint8_t response[max_mtu]; 108 uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, &response[0]); 109 if (response_len){ 110 att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len); 111 } 112 return 0; 113 } 114 115 int sm_cmac_ready(void){ 116 return 1; 117 } 118 void sm_cmac_start(sm_key_t k, uint8_t opcode, uint16_t attribute_handle, uint16_t message_len, uint8_t * message, uint32_t sign_counter, void (*done_handler)(uint8_t hash[8])){ 119 //sm_notify_client(SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched); 120 } 121 int sm_le_device_index(uint16_t handle ){ 122 return -1; 123 } 124 125 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 126 } 127 128 // Set callback that will be executed when timer expires. 129 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){ 130 } 131 132 // Add/Remove timer source. 133 void btstack_run_loop_add_timer(btstack_timer_source_t *timer){ 134 } 135 136 int btstack_run_loop_remove_timer(btstack_timer_source_t *timer){ 137 return 1; 138 } 139 140 // todo: 141 hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 142 printf("hci_connection_for_bd_addr_and_type not implemented in mock backend\n"); 143 return NULL; 144 } 145 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 146 printf("hci_connection_for_handle not implemented in mock backend\n"); 147 return NULL; 148 } 149 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 150 // printf("hci_connections_get_iterator not implemented in mock backend\n"); 151 btstack_linked_list_iterator_init(it, &connections); 152 } 153 154 // int hci_send_cmd(const hci_cmd_t *cmd, ...){ 155 // // printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode); 156 // return 0; 157 // } 158 159 // int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){ 160 // return 1; 161 // } 162 163 // void hci_disconnect_security_block(hci_con_handle_t con_handle){ 164 // printf("hci_disconnect_security_block \n"); 165 // } 166 167 // void hci_dump_log(const char * format, ...){ 168 // printf("hci_disconnect_security_block \n"); 169 // } 170 171 void l2cap_run(void){ 172 } 173