1f9739491SMatthias Ringwald #include <stdint.h> 2f9739491SMatthias Ringwald #include <stdio.h> 3f9739491SMatthias Ringwald #include <stdlib.h> 4f9739491SMatthias Ringwald #include <string.h> 5f9739491SMatthias Ringwald 6f9739491SMatthias Ringwald #include "hci.h" 7e0ff5d41SMatthias Ringwald #include "gap.h" 8f9739491SMatthias Ringwald #include "hci_dump.h" 9f9739491SMatthias Ringwald #include "l2cap.h" 10f9739491SMatthias Ringwald 11f9739491SMatthias Ringwald #include "ble/att_db.h" 121b37bf00SMilanka Ringwald #include "ble/att_dispatch.h" 13f9739491SMatthias Ringwald #include "ble/sm.h" 14f9739491SMatthias Ringwald 15ae970bb9SMilanka Ringwald #include "btstack_debug.h" 16ae970bb9SMilanka Ringwald 171b37bf00SMilanka Ringwald static btstack_packet_handler_t att_server_packet_handler; 18f9739491SMatthias Ringwald static void (*registered_hci_event_handler) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL; 19f9739491SMatthias Ringwald 20f9739491SMatthias Ringwald static btstack_linked_list_t connections; 218206902cSMilanka Ringwald static uint16_t max_mtu = 23; 228206902cSMilanka Ringwald static uint8_t l2cap_stack_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 8 + ATT_DEFAULT_MTU]; // pre buffer + HCI Header + L2CAP header 23f9739491SMatthias Ringwald static uint16_t gatt_client_handle = 0x40; 24f9739491SMatthias Ringwald static hci_connection_t hci_connection; 25f9739491SMatthias Ringwald 26f9739491SMatthias Ringwald uint16_t get_gatt_client_handle(void){ 27f9739491SMatthias Ringwald return gatt_client_handle; 28f9739491SMatthias Ringwald } 29f9739491SMatthias Ringwald 30f9739491SMatthias Ringwald void mock_simulate_command_complete(const hci_cmd_t *cmd){ 31f9739491SMatthias Ringwald uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, (uint8_t) (cmd->opcode & 0xff), (uint8_t) (cmd->opcode >> 8), 0}; 32f9739491SMatthias Ringwald registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet)); 33f9739491SMatthias Ringwald } 34f9739491SMatthias Ringwald 35f9739491SMatthias Ringwald void mock_simulate_hci_state_working(void){ 36f9739491SMatthias Ringwald uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING}; 37f9739491SMatthias Ringwald registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, 3); 38f9739491SMatthias Ringwald } 39f9739491SMatthias Ringwald 40f9739491SMatthias Ringwald void mock_simulate_connected(void){ 41f9739491SMatthias Ringwald uint8_t packet[] = {0x3E, 0x13, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x50, 0x00, 0x00, 0x00, 0xD0, 0x07, 0x05}; 42f9739491SMatthias Ringwald registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet)); 43f9739491SMatthias Ringwald } 44f9739491SMatthias Ringwald 45f9739491SMatthias Ringwald void mock_simulate_scan_response(void){ 46f9739491SMatthias Ringwald uint8_t packet[] = {0xE2, 0x13, 0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B, 0xCC, 0x09, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; 47f9739491SMatthias Ringwald registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet)); 48f9739491SMatthias Ringwald } 49f9739491SMatthias Ringwald 50f9739491SMatthias Ringwald void gap_start_scan(void){ 51f9739491SMatthias Ringwald } 52f9739491SMatthias Ringwald void gap_stop_scan(void){ 53f9739491SMatthias Ringwald } 54e0ff5d41SMatthias Ringwald uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){ 55f9739491SMatthias Ringwald return 0; 56f9739491SMatthias Ringwald } 57f9739491SMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 58f9739491SMatthias Ringwald } 59f9739491SMatthias Ringwald 60f9739491SMatthias Ringwald int gap_reconnect_security_setup_active(hci_con_handle_t con_handle){ 61f9739491SMatthias Ringwald UNUSED(con_handle); 62f9739491SMatthias Ringwald return 0; 63f9739491SMatthias Ringwald } 64f9739491SMatthias Ringwald 65*effffa37SMilanka Ringwald static void hci_setup_connection(uint16_t con_handle, bd_addr_type_t type){ 6625336c94SMilanka Ringwald hci_connection.att_connection.mtu = 23; 6725336c94SMilanka Ringwald hci_connection.att_connection.con_handle = con_handle; 6825336c94SMilanka Ringwald hci_connection.att_connection.max_mtu = 23; 6925336c94SMilanka Ringwald hci_connection.att_connection.encryption_key_size = 0; 7025336c94SMilanka Ringwald hci_connection.att_connection.authenticated = 0; 7125336c94SMilanka Ringwald hci_connection.att_connection.authorized = 0; 7225336c94SMilanka Ringwald 7325336c94SMilanka Ringwald hci_connection.att_server.ir_le_device_db_index = 0; 7425336c94SMilanka Ringwald 758206902cSMilanka Ringwald hci_connection.con_handle = con_handle; 76*effffa37SMilanka Ringwald hci_connection.address_type = type; 778206902cSMilanka Ringwald 7825336c94SMilanka Ringwald if (btstack_linked_list_empty(&connections)){ 7925336c94SMilanka Ringwald btstack_linked_list_add(&connections, (btstack_linked_item_t *)&hci_connection); 8025336c94SMilanka Ringwald } 81f9739491SMatthias Ringwald } 82f9739491SMatthias Ringwald 83*effffa37SMilanka Ringwald void hci_setup_le_connection(uint16_t con_handle){ 84*effffa37SMilanka Ringwald hci_setup_connection(con_handle, BD_ADDR_TYPE_LE_PUBLIC); 85*effffa37SMilanka Ringwald } 86*effffa37SMilanka Ringwald 87*effffa37SMilanka Ringwald void hci_setup_classic_connection(uint16_t con_handle){ 88*effffa37SMilanka Ringwald hci_setup_connection(con_handle, BD_ADDR_TYPE_ACL); 89*effffa37SMilanka Ringwald } 90*effffa37SMilanka Ringwald 91*effffa37SMilanka Ringwald 928206902cSMilanka Ringwald void mock_l2cap_set_max_mtu(uint16_t mtu){ 938206902cSMilanka Ringwald max_mtu = mtu; 941a47f80bSMatthias Ringwald } 951a47f80bSMatthias Ringwald 968206902cSMilanka Ringwald void hci_deinit(void){ 978206902cSMilanka Ringwald hci_connection.att_connection.mtu = 0; 988206902cSMilanka Ringwald hci_connection.att_connection.con_handle = HCI_CON_HANDLE_INVALID; 998206902cSMilanka Ringwald hci_connection.att_connection.max_mtu = 0; 1008206902cSMilanka Ringwald hci_connection.att_connection.encryption_key_size = 0; 1018206902cSMilanka Ringwald hci_connection.att_connection.authenticated = 0; 1028206902cSMilanka Ringwald hci_connection.att_connection.authorized = 0; 1038206902cSMilanka Ringwald hci_connection.att_server.ir_le_device_db_index = 0; 1048206902cSMilanka Ringwald hci_connection.att_server.notification_requests = NULL; 1058206902cSMilanka Ringwald hci_connection.att_server.indication_requests = NULL; 1068206902cSMilanka Ringwald connections = NULL; 1078206902cSMilanka Ringwald } 1088206902cSMilanka Ringwald 1098206902cSMilanka Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 1108206902cSMilanka Ringwald registered_hci_event_handler = callback_handler->callback; 111f9739491SMatthias Ringwald } 112f9739491SMatthias Ringwald 11377fc16acSMatthias Ringwald bool hci_can_send_command_packet_now(void){ 11477fc16acSMatthias Ringwald return true; 115ae970bb9SMilanka Ringwald } 116ae970bb9SMilanka Ringwald 117ae970bb9SMilanka Ringwald HCI_STATE hci_get_state(void){ 118ae970bb9SMilanka Ringwald return HCI_STATE_WORKING; 119ae970bb9SMilanka Ringwald } 120ae970bb9SMilanka Ringwald 12177fc16acSMatthias Ringwald uint8_t hci_send_cmd(const hci_cmd_t *cmd, ...){ 122ae970bb9SMilanka Ringwald btstack_assert(false); 12377fc16acSMatthias Ringwald return ERROR_CODE_SUCCESS; 124ae970bb9SMilanka Ringwald } 125ae970bb9SMilanka Ringwald 126ae970bb9SMilanka Ringwald void hci_halting_defer(void){ 127ae970bb9SMilanka Ringwald } 128ae970bb9SMilanka Ringwald 12977fc16acSMatthias Ringwald bool l2cap_can_send_connectionless_packet_now(void){ 130f9739491SMatthias Ringwald return 1; 131f9739491SMatthias Ringwald } 132f9739491SMatthias Ringwald 1331a47f80bSMatthias Ringwald void l2cap_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 1341a47f80bSMatthias Ringwald UNUSED(callback_handler); 1351a47f80bSMatthias Ringwald } 1361a47f80bSMatthias Ringwald 137f9739491SMatthias Ringwald uint8_t *l2cap_get_outgoing_buffer(void){ 138f9739491SMatthias Ringwald // printf("l2cap_get_outgoing_buffer\n"); 139f9739491SMatthias Ringwald return (uint8_t *)&l2cap_stack_buffer; // 8 bytes 140f9739491SMatthias Ringwald } 141f9739491SMatthias Ringwald 142f9739491SMatthias Ringwald uint16_t l2cap_max_mtu(void){ 143f9739491SMatthias Ringwald // printf("l2cap_max_mtu\n"); 144f9739491SMatthias Ringwald return max_mtu; 145f9739491SMatthias Ringwald } 146f9739491SMatthias Ringwald 147f9739491SMatthias Ringwald uint16_t l2cap_max_le_mtu(void){ 148f9739491SMatthias Ringwald return max_mtu; 149f9739491SMatthias Ringwald } 150f9739491SMatthias Ringwald 151f9739491SMatthias Ringwald void l2cap_init(void){} 152f9739491SMatthias Ringwald 15377fc16acSMatthias Ringwald bool l2cap_reserve_packet_buffer(void){ 15477fc16acSMatthias Ringwald return true; 155f9739491SMatthias Ringwald } 156f9739491SMatthias Ringwald 157f9739491SMatthias Ringwald void l2cap_release_packet_buffer(void){ 158f9739491SMatthias Ringwald } 159f9739491SMatthias Ringwald 160782c1ae9SMilanka Ringwald static uint8_t l2cap_can_send_fixed_channel_packet_now_status = 1; 161782c1ae9SMilanka Ringwald 162782c1ae9SMilanka Ringwald void l2cap_can_send_fixed_channel_packet_now_set_status(uint8_t status){ 163782c1ae9SMilanka Ringwald l2cap_can_send_fixed_channel_packet_now_status = status; 164782c1ae9SMilanka Ringwald } 165782c1ae9SMilanka Ringwald 16677fc16acSMatthias Ringwald bool l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){ 167782c1ae9SMilanka Ringwald return l2cap_can_send_fixed_channel_packet_now_status; 168f9739491SMatthias Ringwald } 169f9739491SMatthias Ringwald 170f9739491SMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(uint16_t handle, uint16_t channel_id){ 171f9739491SMatthias Ringwald uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 1, 0}; 1721b37bf00SMilanka Ringwald att_server_packet_handler(HCI_EVENT_PACKET, 0, (uint8_t*)event, sizeof(event)); 173f9739491SMatthias Ringwald } 174f9739491SMatthias Ringwald 17577fc16acSMatthias Ringwald uint8_t l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 176f9739491SMatthias Ringwald att_connection_t att_connection; 1778206902cSMilanka Ringwald hci_setup_le_connection(handle); 178f9739491SMatthias Ringwald uint8_t response[max_mtu]; 179f9739491SMatthias Ringwald uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, &response[0]); 180f9739491SMatthias Ringwald if (response_len){ 1811b37bf00SMilanka Ringwald att_server_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len); 182f9739491SMatthias Ringwald } 18377fc16acSMatthias Ringwald return ERROR_CODE_SUCCESS; 184f9739491SMatthias Ringwald } 185f9739491SMatthias Ringwald 186f9739491SMatthias Ringwald void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 187f9739491SMatthias Ringwald } 188f9739491SMatthias Ringwald 189f9739491SMatthias Ringwald int sm_cmac_ready(void){ 190f9739491SMatthias Ringwald return 1; 191f9739491SMatthias Ringwald } 192f9739491SMatthias Ringwald 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)){ 193f9739491SMatthias Ringwald //sm_notify_client(SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched); 194f9739491SMatthias Ringwald } 195f9739491SMatthias Ringwald int sm_le_device_index(uint16_t handle ){ 196f9739491SMatthias Ringwald return -1; 197f9739491SMatthias Ringwald } 198f9739491SMatthias Ringwald 199f9739491SMatthias Ringwald irk_lookup_state_t sm_identity_resolving_state(hci_con_handle_t con_handle){ 200f9739491SMatthias Ringwald return IRK_LOOKUP_SUCCEEDED; 201f9739491SMatthias Ringwald } 202f9739491SMatthias Ringwald 203f9739491SMatthias Ringwald void sm_request_pairing(hci_con_handle_t con_handle){ 204f9739491SMatthias Ringwald } 205f9739491SMatthias Ringwald 206f9739491SMatthias Ringwald void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 207f9739491SMatthias Ringwald } 208f9739491SMatthias Ringwald 209f9739491SMatthias Ringwald // Set callback that will be executed when timer expires. 210f9739491SMatthias Ringwald void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){ 211f9739491SMatthias Ringwald } 212f9739491SMatthias Ringwald 213f9739491SMatthias Ringwald // Add/Remove timer source. 214f9739491SMatthias Ringwald void btstack_run_loop_add_timer(btstack_timer_source_t *timer){ 215f9739491SMatthias Ringwald } 216f9739491SMatthias Ringwald 217f9739491SMatthias Ringwald int btstack_run_loop_remove_timer(btstack_timer_source_t *timer){ 218f9739491SMatthias Ringwald return 1; 219f9739491SMatthias Ringwald } 220f9739491SMatthias Ringwald 221f9739491SMatthias Ringwald void * btstack_run_loop_get_timer_context(btstack_timer_source_t *ts){ 222f9739491SMatthias Ringwald return ts->context; 223f9739491SMatthias Ringwald } 224f9739491SMatthias Ringwald 225f9739491SMatthias Ringwald // todo: 226e0ff5d41SMatthias Ringwald hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){ 227f9739491SMatthias Ringwald printf("hci_connection_for_bd_addr_and_type not implemented in mock backend\n"); 228f9739491SMatthias Ringwald return NULL; 229f9739491SMatthias Ringwald } 230f9739491SMatthias Ringwald hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 2318206902cSMilanka Ringwald if (hci_connection.con_handle != con_handle){ 2328206902cSMilanka Ringwald return NULL; 2338206902cSMilanka Ringwald } 234f9739491SMatthias Ringwald return &hci_connection; 235f9739491SMatthias Ringwald } 2368206902cSMilanka Ringwald 237f9739491SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 238f9739491SMatthias Ringwald // printf("hci_connections_get_iterator not implemented in mock backend\n"); 239f9739491SMatthias Ringwald btstack_linked_list_iterator_init(it, &connections); 240f9739491SMatthias Ringwald } 241f9739491SMatthias Ringwald 242f9739491SMatthias Ringwald 243f9739491SMatthias Ringwald void l2cap_run(void){ 244f9739491SMatthias Ringwald } 245f9739491SMatthias Ringwald 2461a449638SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){ 2471a449638SMatthias Ringwald } 2481a449638SMatthias Ringwald 2492e7076acSMatthias Ringwald bool gap_authenticated(hci_con_handle_t con_handle){ 2502e7076acSMatthias Ringwald return false; 251f9739491SMatthias Ringwald } 252f9739491SMatthias Ringwald authorization_state_t gap_authorization_state(hci_con_handle_t con_handle){ 253f9739491SMatthias Ringwald return AUTHORIZATION_UNKNOWN; 254f9739491SMatthias Ringwald } 2552e7076acSMatthias Ringwald uint8_t gap_encryption_key_size(hci_con_handle_t con_handle){ 256f9739491SMatthias Ringwald return 0; 257f9739491SMatthias Ringwald } 2582e7076acSMatthias Ringwald bool gap_secure_connection(hci_con_handle_t con_handle){ 2592e7076acSMatthias Ringwald return false; 260f9739491SMatthias Ringwald } 26118cd0f45SMilanka Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t con_handle){ 26218cd0f45SMilanka Ringwald if (hci_connection.con_handle != con_handle){ 263f9739491SMatthias Ringwald return GAP_CONNECTION_INVALID; 264f9739491SMatthias Ringwald } 26518cd0f45SMilanka Ringwald switch (hci_connection.address_type){ 26618cd0f45SMilanka Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 26718cd0f45SMilanka Ringwald case BD_ADDR_TYPE_LE_RANDOM: 26818cd0f45SMilanka Ringwald return GAP_CONNECTION_LE; 26918cd0f45SMilanka Ringwald case BD_ADDR_TYPE_SCO: 27018cd0f45SMilanka Ringwald return GAP_CONNECTION_SCO; 27118cd0f45SMilanka Ringwald case BD_ADDR_TYPE_ACL: 27218cd0f45SMilanka Ringwald return GAP_CONNECTION_ACL; 27318cd0f45SMilanka Ringwald default: 27418cd0f45SMilanka Ringwald return GAP_CONNECTION_INVALID; 27518cd0f45SMilanka Ringwald } 27618cd0f45SMilanka Ringwald } 27718cd0f45SMilanka Ringwald 2781a449638SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 2791a449638SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 2801a449638SMatthias Ringwald return 0; 2811a449638SMatthias Ringwald } 2821b37bf00SMilanka Ringwald 2831b37bf00SMilanka Ringwald void mock_call_att_server_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2841b37bf00SMilanka Ringwald (*att_server_packet_handler)(packet_type, channel, packet, size); 2851b37bf00SMilanka Ringwald } 2861b37bf00SMilanka Ringwald 2878206902cSMilanka Ringwald void mock_call_att_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2888206902cSMilanka Ringwald (*registered_hci_event_handler)(packet_type, channel, packet, size); 2898206902cSMilanka Ringwald } 2908206902cSMilanka Ringwald 2911b37bf00SMilanka Ringwald void att_dispatch_register_server(btstack_packet_handler_t packet_handler){ 2921b37bf00SMilanka Ringwald att_server_packet_handler = packet_handler; 2931b37bf00SMilanka Ringwald } 2941b37bf00SMilanka Ringwald 2951b37bf00SMilanka Ringwald int att_dispatch_server_can_send_now(hci_con_handle_t con_handle){ 2961b37bf00SMilanka Ringwald UNUSED(con_handle); 2971b37bf00SMilanka Ringwald return l2cap_can_send_fixed_channel_packet_now_status; 2981b37bf00SMilanka Ringwald } 2991b37bf00SMilanka Ringwald 3001b37bf00SMilanka Ringwald void att_dispatch_server_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){ 3011b37bf00SMilanka Ringwald UNUSED(con_handle); 3021b37bf00SMilanka Ringwald UNUSED(new_mtu); 3031b37bf00SMilanka Ringwald } 3041b37bf00SMilanka Ringwald 3051b37bf00SMilanka Ringwald void att_dispatch_server_request_can_send_now_event(hci_con_handle_t con_handle){ 30694c4ec6bSMilanka Ringwald if (l2cap_can_send_fixed_channel_packet_now_status){ 30725336c94SMilanka Ringwald uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 1, 0}; 30825336c94SMilanka Ringwald att_server_packet_handler(HCI_EVENT_PACKET, 0, (uint8_t*)event, sizeof(event)); 3091b37bf00SMilanka Ringwald } 31094c4ec6bSMilanka Ringwald } 3118206902cSMilanka Ringwald 312