1f45e14b1S[email protected] #include <stdint.h> 2f45e14b1S[email protected] #include <stdio.h> 3f45e14b1S[email protected] #include <stdlib.h> 4f45e14b1S[email protected] #include <string.h> 5f45e14b1S[email protected] 6f45e14b1S[email protected] #include "hci.h" 7f45e14b1S[email protected] #include "hci_dump.h" 8f45e14b1S[email protected] #include "l2cap.h" 93edc84c5SMatthias Ringwald 10591423b2SMatthias Ringwald #include "ble/att_db.h" 113edc84c5SMatthias Ringwald #include "ble/gatt_client.h" 123edc84c5SMatthias Ringwald #include "ble/sm.h" 1382b61e60SMatthias Ringwald #include "gap.h" 144f5e8608S[email protected] 15f959ac69SMatthias Ringwald #define PREBUFFER_SIZE (HCI_INCOMING_PRE_BUFFER_SIZE + 8) 16f959ac69SMatthias Ringwald 174f5e8608S[email protected] static btstack_packet_handler_t att_packet_handler; 18b6e96f14SMatthias Ringwald static void (*registered_hci_event_handler) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL; 194f5e8608S[email protected] 208f2a52f4SMatthias Ringwald static btstack_linked_list_t connections; 21c442c7c2S[email protected] static const uint16_t max_mtu = 23; 22f959ac69SMatthias Ringwald static uint8_t l2cap_stack_buffer[PREBUFFER_SIZE + max_mtu]; // pre buffer + HCI Header + L2CAP header 234a340927SMatthias Ringwald static uint16_t gatt_client_handle = 0x40; 244a340927SMatthias Ringwald static hci_connection_t hci_connection; 254f5e8608S[email protected] 2671de195eSMatthias Ringwald uint16_t get_gatt_client_handle(void){ 27567c20c9S[email protected] return gatt_client_handle; 28567c20c9S[email protected] } 29567c20c9S[email protected] 309638642bS[email protected] void mock_simulate_command_complete(const hci_cmd_t *cmd){ 3133a05ac6SMatthias Ringwald uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, (uint8_t) (cmd->opcode & 0xff), (uint8_t) (cmd->opcode >> 8), 0}; 327046994fSMatthias Ringwald registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet)); 334f5e8608S[email protected] } 344f5e8608S[email protected] 3571de195eSMatthias Ringwald void mock_simulate_hci_state_working(void){ 369638642bS[email protected] uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING}; 37b6e96f14SMatthias Ringwald registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, 3); 389638642bS[email protected] } 399638642bS[email protected] 4071de195eSMatthias Ringwald void mock_simulate_connected(void){ 41*1ebbe057SMatthias Ringwald 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}; 42b6e96f14SMatthias Ringwald registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet)); 434f5e8608S[email protected] } 44f45e14b1S[email protected] 4571de195eSMatthias Ringwald void mock_simulate_scan_response(void){ 46*1ebbe057SMatthias Ringwald 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}; 47b6e96f14SMatthias Ringwald registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet)); 48ffe2840fSMilanka Ringwald } 4938336af4SMatthias Ringwald int gap_authenticated(hci_con_handle_t con_handle){ 5038336af4SMatthias Ringwald UNUSED(con_handle); 5138336af4SMatthias Ringwald return false; 5238336af4SMatthias Ringwald } 5338336af4SMatthias Ringwald int gap_encryption_key_size(hci_con_handle_t con_handle){ 5438336af4SMatthias Ringwald UNUSED(con_handle); 5538336af4SMatthias Ringwald return 0; 5638336af4SMatthias Ringwald } 5738336af4SMatthias Ringwald void sm_request_pairing(hci_con_handle_t con_handle){ 5838336af4SMatthias Ringwald UNUSED(con_handle); 5938336af4SMatthias Ringwald } 60d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 61d8e8f12aSMatthias Ringwald } 62d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 63d8e8f12aSMatthias Ringwald } 6482b61e60SMatthias Ringwald uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){ 65616edd56SMatthias Ringwald return 0; 66ffe2840fSMilanka Ringwald } 67d8e8f12aSMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 68c442c7c2S[email protected] } 69c442c7c2S[email protected] 70489a58deSMatthias Ringwald int gap_reconnect_security_setup_active(hci_con_handle_t con_handle){ 71489a58deSMatthias Ringwald UNUSED(con_handle); 72489a58deSMatthias Ringwald return 0; 73489a58deSMatthias Ringwald } 74489a58deSMatthias Ringwald 754d890b2dS[email protected] static void att_init_connection(att_connection_t * att_connection){ 764d890b2dS[email protected] att_connection->mtu = 23; 77d7d7d1e4SMilanka Ringwald att_connection->max_mtu = 23; 784d890b2dS[email protected] att_connection->encryption_key_size = 0; 794d890b2dS[email protected] att_connection->authenticated = 0; 804d890b2dS[email protected] att_connection->authorized = 0; 81c442c7c2S[email protected] } 82c442c7c2S[email protected] 83d63c37a1SMatthias Ringwald int hci_can_send_acl_le_packet_now(void){ 844f5e8608S[email protected] return 1; 85f45e14b1S[email protected] } 86f45e14b1S[email protected] 87d63c37a1SMatthias Ringwald int l2cap_can_send_connectionless_packet_now(void){ 88d63c37a1SMatthias Ringwald return 1; 89d63c37a1SMatthias Ringwald } 90f45e14b1S[email protected] 91f45e14b1S[email protected] uint8_t *l2cap_get_outgoing_buffer(void){ 92f959ac69SMatthias Ringwald return (uint8_t *)&l2cap_stack_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 8]; 93f45e14b1S[email protected] } 94f45e14b1S[email protected] 95f45e14b1S[email protected] uint16_t l2cap_max_mtu(void){ 96f5bdf8a1S[email protected] // printf("l2cap_max_mtu\n"); 97c442c7c2S[email protected] return max_mtu; 98f45e14b1S[email protected] } 99f45e14b1S[email protected] 100567c20c9S[email protected] uint16_t l2cap_max_le_mtu(void){ 101567c20c9S[email protected] // printf("l2cap_max_mtu\n"); 102567c20c9S[email protected] return max_mtu; 103567c20c9S[email protected] } 104f45e14b1S[email protected] 1050cb5b971SMatthias Ringwald void l2cap_init(void){} 106ffe2840fSMilanka Ringwald 107f45e14b1S[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) { 1084f5e8608S[email protected] att_packet_handler = packet_handler; 109f45e14b1S[email protected] } 110f45e14b1S[email protected] 111c5b64319SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 112c5b64319SMatthias Ringwald registered_hci_event_handler = callback_handler->callback; 113f45e14b1S[email protected] } 114f45e14b1S[email protected] 115f45e14b1S[email protected] int l2cap_reserve_packet_buffer(void){ 116567c20c9S[email protected] return 1; 117567c20c9S[email protected] } 118567c20c9S[email protected] 119adbe29e8SMatthias Ringwald int l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){ 120c442c7c2S[email protected] return 1; 121f45e14b1S[email protected] } 122f45e14b1S[email protected] 1237046994fSMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(uint16_t handle, uint16_t channel_id){ 1247046994fSMatthias Ringwald uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 1, 0}; 1257046994fSMatthias Ringwald att_packet_handler(HCI_EVENT_PACKET, 0, (uint8_t*)event, sizeof(event)); 1267046994fSMatthias Ringwald } 1277046994fSMatthias Ringwald 128f45e14b1S[email protected] int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){ 1294d890b2dS[email protected] att_connection_t att_connection; 1304d890b2dS[email protected] att_init_connection(&att_connection); 131f959ac69SMatthias Ringwald uint8_t response_buffer[PREBUFFER_SIZE + max_mtu]; 132f959ac69SMatthias Ringwald uint8_t * response = &response_buffer[PREBUFFER_SIZE]; 133f959ac69SMatthias Ringwald uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, response); 134ecedf63aS[email protected] if (response_len){ 135ee988ca9S[email protected] att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len); 136ecedf63aS[email protected] } 137f45e14b1S[email protected] return 0; 138f45e14b1S[email protected] } 139f45e14b1S[email protected] 140e90da8a6SMatthias Ringwald void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 141e90da8a6SMatthias Ringwald } 142e90da8a6SMatthias Ringwald 14371de195eSMatthias Ringwald int sm_cmac_ready(void){ 144567c20c9S[email protected] return 1; 145567c20c9S[email protected] } 1464dfd504aSMatthias 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)){ 1475611a760SMatthias Ringwald //sm_notify_client(SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched); 148567c20c9S[email protected] } 14957fe2af8SMatthias Ringwald int sm_le_device_index(uint16_t handle ){ 15057fe2af8SMatthias Ringwald return -1; 15157fe2af8SMatthias Ringwald } 1527b4ad037SMatthias Ringwald void sm_send_security_request(hci_con_handle_t con_handle){ 1537b4ad037SMatthias Ringwald } 154567c20c9S[email protected] 155e90da8a6SMatthias Ringwald irk_lookup_state_t sm_identity_resolving_state(hci_con_handle_t con_handle){ 156e90da8a6SMatthias Ringwald return IRK_LOOKUP_SUCCEEDED; 157e90da8a6SMatthias Ringwald } 158ec820d77SMatthias Ringwald void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){ 1591bf0402bS[email protected] } 1601bf0402bS[email protected] 1611bf0402bS[email protected] // Set callback that will be executed when timer expires. 162ec820d77SMatthias Ringwald void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){ 1631bf0402bS[email protected] } 1641bf0402bS[email protected] 1651bf0402bS[email protected] // Add/Remove timer source. 166ec820d77SMatthias Ringwald void btstack_run_loop_add_timer(btstack_timer_source_t *timer){ 1671bf0402bS[email protected] } 1681bf0402bS[email protected] 169ec820d77SMatthias Ringwald int btstack_run_loop_remove_timer(btstack_timer_source_t *timer){ 1701bf0402bS[email protected] return 1; 1711bf0402bS[email protected] } 1721bf0402bS[email protected] 173ffe2840fSMilanka Ringwald // todo: 174ffe2840fSMilanka Ringwald hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 175ffe2840fSMilanka Ringwald printf("hci_connection_for_bd_addr_and_type not implemented in mock backend\n"); 176ffe2840fSMilanka Ringwald return NULL; 177ffe2840fSMilanka Ringwald } 178ffe2840fSMilanka Ringwald hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 1794a340927SMatthias Ringwald return &hci_connection; 180ffe2840fSMilanka Ringwald } 181665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 182ffe2840fSMilanka Ringwald // printf("hci_connections_get_iterator not implemented in mock backend\n"); 183665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(it, &connections); 184ffe2840fSMilanka Ringwald } 185567c20c9S[email protected] 186f6424062S[email protected] // int hci_send_cmd(const hci_cmd_t *cmd, ...){ 187f6424062S[email protected] // // printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode); 188f6424062S[email protected] // return 0; 189f6424062S[email protected] // } 190f45e14b1S[email protected] 191f6424062S[email protected] // int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){ 192f6424062S[email protected] // return 1; 193f6424062S[email protected] // } 1944f5e8608S[email protected] 195f6424062S[email protected] // void hci_disconnect_security_block(hci_con_handle_t con_handle){ 196f6424062S[email protected] // printf("hci_disconnect_security_block \n"); 197f6424062S[email protected] // } 198f6424062S[email protected] 199f6424062S[email protected] // void hci_dump_log(const char * format, ...){ 200f6424062S[email protected] // printf("hci_disconnect_security_block \n"); 201f6424062S[email protected] // } 2024f5e8608S[email protected] 203f45e14b1S[email protected] void l2cap_run(void){ 204f45e14b1S[email protected] } 205