1f45e14b1S[email protected] #include <stdint.h>
2f45e14b1S[email protected] #include <stdio.h>
3f45e14b1S[email protected] #include <string.h>
4f45e14b1S[email protected]
5f45e14b1S[email protected] #include "hci.h"
6f45e14b1S[email protected] #include "hci_dump.h"
7f45e14b1S[email protected] #include "l2cap.h"
83edc84c5SMatthias Ringwald
9591423b2SMatthias Ringwald #include "ble/att_db.h"
103edc84c5SMatthias Ringwald #include "ble/sm.h"
1182b61e60SMatthias Ringwald #include "gap.h"
127aef8288SMilanka Ringwald #include "btstack_debug.h"
134f5e8608S[email protected]
14f959ac69SMatthias Ringwald #define PREBUFFER_SIZE (HCI_INCOMING_PRE_BUFFER_SIZE + 8)
15e63c7784SMatthias Ringwald #define TEST_MAX_MTU 23
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;
21e63c7784SMatthias Ringwald static uint8_t l2cap_stack_buffer[PREBUFFER_SIZE + TEST_MAX_MTU]; // pre buffer + HCI Header + L2CAP header
224a340927SMatthias Ringwald static uint16_t gatt_client_handle = 0x40;
234a340927SMatthias Ringwald static hci_connection_t hci_connection;
244f5e8608S[email protected]
257aef8288SMilanka Ringwald static uint8_t packet_buffer[256];
267aef8288SMilanka Ringwald static uint16_t packet_buffer_len;
277aef8288SMilanka Ringwald
get_gatt_client_handle(void)2871de195eSMatthias Ringwald uint16_t get_gatt_client_handle(void){
29567c20c9S[email protected] return gatt_client_handle;
30567c20c9S[email protected] }
31567c20c9S[email protected]
mock_simulate_command_complete(const hci_cmd_t * cmd)329638642bS[email protected] void mock_simulate_command_complete(const hci_cmd_t *cmd){
3333a05ac6SMatthias Ringwald uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, (uint8_t) (cmd->opcode & 0xff), (uint8_t) (cmd->opcode >> 8), 0};
347046994fSMatthias Ringwald registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet));
354f5e8608S[email protected] }
364f5e8608S[email protected]
mock_simulate_hci_state_working(void)3771de195eSMatthias Ringwald void mock_simulate_hci_state_working(void){
389638642bS[email protected] uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
39b6e96f14SMatthias Ringwald registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, 3);
409638642bS[email protected] }
419638642bS[email protected]
hci_create_gap_connection_complete_event(const uint8_t * hci_event,uint8_t * gap_event)4238e85ba1SMatthias Ringwald static void hci_create_gap_connection_complete_event(const uint8_t * hci_event, uint8_t * gap_event) {
4338e85ba1SMatthias Ringwald gap_event[0] = HCI_EVENT_META_GAP;
4438e85ba1SMatthias Ringwald gap_event[1] = 36 - 2;
4538e85ba1SMatthias Ringwald gap_event[2] = GAP_SUBEVENT_LE_CONNECTION_COMPLETE;
4638e85ba1SMatthias Ringwald switch (hci_event[2]){
4738e85ba1SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
4838e85ba1SMatthias Ringwald memcpy(&gap_event[3], &hci_event[3], 11);
4938e85ba1SMatthias Ringwald memset(&gap_event[14], 0, 12);
5038e85ba1SMatthias Ringwald memcpy(&gap_event[26], &hci_event[14], 7);
5138e85ba1SMatthias Ringwald memset(&gap_event[33], 0xff, 3);
5238e85ba1SMatthias Ringwald break;
5338e85ba1SMatthias Ringwald case HCI_SUBEVENT_LE_ENHANCED_CONNECTION_COMPLETE_V1:
5438e85ba1SMatthias Ringwald memcpy(&gap_event[3], &hci_event[3], 30);
5538e85ba1SMatthias Ringwald memset(&gap_event[33], 0xff, 3);
5638e85ba1SMatthias Ringwald break;
5738e85ba1SMatthias Ringwald case HCI_SUBEVENT_LE_ENHANCED_CONNECTION_COMPLETE_V2:
5838e85ba1SMatthias Ringwald memcpy(&gap_event[3], &hci_event[3], 33);
5938e85ba1SMatthias Ringwald break;
6038e85ba1SMatthias Ringwald default:
6138e85ba1SMatthias Ringwald btstack_unreachable();
6238e85ba1SMatthias Ringwald break;
6338e85ba1SMatthias Ringwald }
6438e85ba1SMatthias Ringwald }
6538e85ba1SMatthias Ringwald
mock_simulate_connected(void)6671de195eSMatthias Ringwald void mock_simulate_connected(void){
671ebbe057SMatthias 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};
6838e85ba1SMatthias Ringwald uint8_t gap_event[36];
6938e85ba1SMatthias Ringwald hci_create_gap_connection_complete_event(packet, gap_event);
7038e85ba1SMatthias Ringwald registered_hci_event_handler(HCI_EVENT_PACKET, 0, gap_event, sizeof(gap_event));
714f5e8608S[email protected] }
72f45e14b1S[email protected]
mock_simulate_scan_response(void)7371de195eSMatthias Ringwald void mock_simulate_scan_response(void){
741ebbe057SMatthias 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};
75b6e96f14SMatthias Ringwald registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet));
76ffe2840fSMilanka Ringwald }
77ec4690afSMatthias Ringwald
gap_authenticated(hci_con_handle_t con_handle)782e7076acSMatthias Ringwald bool gap_authenticated(hci_con_handle_t con_handle){
7938336af4SMatthias Ringwald UNUSED(con_handle);
8038336af4SMatthias Ringwald return false;
8138336af4SMatthias Ringwald }
gap_encryption_key_size(hci_con_handle_t con_handle)822e7076acSMatthias Ringwald uint8_t gap_encryption_key_size(hci_con_handle_t con_handle){
8338336af4SMatthias Ringwald UNUSED(con_handle);
8438336af4SMatthias Ringwald return 0;
8538336af4SMatthias Ringwald }
gap_bonded(hci_con_handle_t con_handle)8698e87e77SMatthias Ringwald bool gap_bonded(hci_con_handle_t con_handle){
8798e87e77SMatthias Ringwald UNUSED(con_handle);
8898e87e77SMatthias Ringwald return true;
8998e87e77SMatthias Ringwald }
sm_request_pairing(hci_con_handle_t con_handle)9038336af4SMatthias Ringwald void sm_request_pairing(hci_con_handle_t con_handle){
9138336af4SMatthias Ringwald UNUSED(con_handle);
9238336af4SMatthias Ringwald }
gap_start_scan(void)93d8e8f12aSMatthias Ringwald void gap_start_scan(void){
94d8e8f12aSMatthias Ringwald }
gap_stop_scan(void)95d8e8f12aSMatthias Ringwald void gap_stop_scan(void){
96d8e8f12aSMatthias Ringwald }
gap_connect(const bd_addr_t addr,bd_addr_type_t addr_type)9782b61e60SMatthias Ringwald uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){
98616edd56SMatthias Ringwald return 0;
99ffe2840fSMilanka Ringwald }
gap_set_scan_parameters(uint8_t scan_type,uint16_t scan_interval,uint16_t scan_window)100d8e8f12aSMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
101c442c7c2S[email protected] }
102c442c7c2S[email protected]
gap_reconnect_security_setup_active(hci_con_handle_t con_handle)103e4d1c5dcSMatthias Ringwald bool gap_reconnect_security_setup_active(hci_con_handle_t con_handle){
104489a58deSMatthias Ringwald UNUSED(con_handle);
105e4d1c5dcSMatthias Ringwald return false;
106489a58deSMatthias Ringwald }
107489a58deSMatthias Ringwald
att_init_connection(att_connection_t * att_connection)1084d890b2dS[email protected] static void att_init_connection(att_connection_t * att_connection){
109e63c7784SMatthias Ringwald att_connection->mtu = TEST_MAX_MTU;
110e63c7784SMatthias Ringwald att_connection->max_mtu = TEST_MAX_MTU;
1114d890b2dS[email protected] att_connection->encryption_key_size = 0;
1124d890b2dS[email protected] att_connection->authenticated = 0;
1134d890b2dS[email protected] att_connection->authorized = 0;
114c442c7c2S[email protected] }
115c442c7c2S[email protected]
hci_get_state(void)1167aef8288SMilanka Ringwald HCI_STATE hci_get_state(void){
1177aef8288SMilanka Ringwald return HCI_STATE_WORKING;
1187aef8288SMilanka Ringwald }
1197aef8288SMilanka Ringwald
hci_send_cmd(const hci_cmd_t * cmd,...)1207aef8288SMilanka Ringwald uint8_t hci_send_cmd(const hci_cmd_t *cmd, ...){
1217aef8288SMilanka Ringwald
1227aef8288SMilanka Ringwald btstack_assert(false);
1237aef8288SMilanka Ringwald
1247aef8288SMilanka Ringwald va_list argptr;
1257aef8288SMilanka Ringwald va_start(argptr, cmd);
1267aef8288SMilanka Ringwald uint16_t len = hci_cmd_create_from_template(packet_buffer, cmd, argptr);
1277aef8288SMilanka Ringwald va_end(argptr);
1287aef8288SMilanka Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet_buffer, len);
1297aef8288SMilanka Ringwald // dump_packet(HCI_COMMAND_DATA_PACKET, packet_buffer, len);
1307aef8288SMilanka Ringwald packet_buffer_len = len;
1317aef8288SMilanka Ringwald return ERROR_CODE_SUCCESS;
1327aef8288SMilanka Ringwald }
1337aef8288SMilanka Ringwald
hci_can_send_command_packet_now(void)1347aef8288SMilanka Ringwald bool hci_can_send_command_packet_now(void){
1357aef8288SMilanka Ringwald return true;
1367aef8288SMilanka Ringwald }
1377aef8288SMilanka Ringwald
hci_can_send_acl_le_packet_now(void)13877fc16acSMatthias Ringwald bool hci_can_send_acl_le_packet_now(void){
13977fc16acSMatthias Ringwald return true;
140f45e14b1S[email protected] }
141f45e14b1S[email protected]
l2cap_can_send_connectionless_packet_now(void)14277fc16acSMatthias Ringwald bool l2cap_can_send_connectionless_packet_now(void){
14377fc16acSMatthias Ringwald return true;
144d63c37a1SMatthias Ringwald }
145f45e14b1S[email protected]
l2cap_get_outgoing_buffer(void)146f45e14b1S[email protected] uint8_t *l2cap_get_outgoing_buffer(void){
147f959ac69SMatthias Ringwald return (uint8_t *)&l2cap_stack_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 8];
148f45e14b1S[email protected] }
149f45e14b1S[email protected]
l2cap_max_mtu(void)150f45e14b1S[email protected] uint16_t l2cap_max_mtu(void){
151e63c7784SMatthias Ringwald return TEST_MAX_MTU;
152f45e14b1S[email protected] }
153f45e14b1S[email protected]
l2cap_max_le_mtu(void)154567c20c9S[email protected] uint16_t l2cap_max_le_mtu(void){
155e63c7784SMatthias Ringwald return TEST_MAX_MTU;
156567c20c9S[email protected] }
157f45e14b1S[email protected]
l2cap_init(void)1580cb5b971SMatthias Ringwald void l2cap_init(void){}
159ffe2840fSMilanka Ringwald
l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler,uint16_t channel_id)160f45e14b1S[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
1614f5e8608S[email protected] att_packet_handler = packet_handler;
162f45e14b1S[email protected] }
163f45e14b1S[email protected]
hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler)164c5b64319SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
165c5b64319SMatthias Ringwald registered_hci_event_handler = callback_handler->callback;
166f45e14b1S[email protected] }
167f45e14b1S[email protected]
l2cap_reserve_packet_buffer(void)1688f4649e3SMatthias Ringwald void l2cap_reserve_packet_buffer(void){}
169567c20c9S[email protected]
170*a68a4a75SMilanka Ringwald static bool _l2cap_can_send_fixed_channel_packet_now = true;
171*a68a4a75SMilanka Ringwald
l2cap_set_can_send_fixed_channel_packet_now(bool value)172*a68a4a75SMilanka Ringwald void l2cap_set_can_send_fixed_channel_packet_now(bool value){
173*a68a4a75SMilanka Ringwald _l2cap_can_send_fixed_channel_packet_now = value;
174*a68a4a75SMilanka Ringwald }
175*a68a4a75SMilanka Ringwald
l2cap_can_send_fixed_channel_packet_now(uint16_t handle,uint16_t channel_id)17677fc16acSMatthias Ringwald bool l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){
177*a68a4a75SMilanka Ringwald return _l2cap_can_send_fixed_channel_packet_now;
178f45e14b1S[email protected] }
179f45e14b1S[email protected]
l2cap_request_can_send_fix_channel_now_event(uint16_t handle,uint16_t channel_id)1807046994fSMatthias Ringwald void l2cap_request_can_send_fix_channel_now_event(uint16_t handle, uint16_t channel_id){
1817046994fSMatthias Ringwald uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 1, 0};
1827046994fSMatthias Ringwald att_packet_handler(HCI_EVENT_PACKET, 0, (uint8_t*)event, sizeof(event));
1837046994fSMatthias Ringwald }
1847046994fSMatthias Ringwald
l2cap_send_prepared_connectionless(uint16_t handle,uint16_t cid,uint16_t len)18577fc16acSMatthias Ringwald uint8_t l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
1864d890b2dS[email protected] att_connection_t att_connection;
1874d890b2dS[email protected] att_init_connection(&att_connection);
188e63c7784SMatthias Ringwald uint8_t response_buffer[PREBUFFER_SIZE + TEST_MAX_MTU];
189f959ac69SMatthias Ringwald uint8_t * response = &response_buffer[PREBUFFER_SIZE];
190f959ac69SMatthias Ringwald uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, response);
191ecedf63aS[email protected] if (response_len){
192ee988ca9S[email protected] att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len);
193ecedf63aS[email protected] }
19477fc16acSMatthias Ringwald return ERROR_CODE_SUCCESS;
195f45e14b1S[email protected] }
196f45e14b1S[email protected]
sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler)197e90da8a6SMatthias Ringwald void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
198e90da8a6SMatthias Ringwald }
199e90da8a6SMatthias Ringwald
sm_cmac_ready(void)20071de195eSMatthias Ringwald int sm_cmac_ready(void){
201567c20c9S[email protected] return 1;
202567c20c9S[email protected] }
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))2034dfd504aSMatthias 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)){
2045611a760SMatthias Ringwald //sm_notify_client(SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched);
205567c20c9S[email protected] }
sm_le_device_index(uint16_t handle)20657fe2af8SMatthias Ringwald int sm_le_device_index(uint16_t handle ){
2077deeca1cSMatthias Ringwald return 0;
20857fe2af8SMatthias Ringwald }
sm_send_security_request(hci_con_handle_t con_handle)2097b4ad037SMatthias Ringwald void sm_send_security_request(hci_con_handle_t con_handle){
2107b4ad037SMatthias Ringwald }
211567c20c9S[email protected]
sm_get_ltk(hci_con_handle_t con_handle,sm_key_t ltk)2127aef8288SMilanka Ringwald uint8_t sm_get_ltk(hci_con_handle_t con_handle, sm_key_t ltk){
2137aef8288SMilanka Ringwald memset((uint8_t*) ltk, 0x22, 16);
2147aef8288SMilanka Ringwald return ERROR_CODE_SUCCESS;
2157aef8288SMilanka Ringwald }
2167aef8288SMilanka Ringwald
sm_identity_resolving_state(hci_con_handle_t con_handle)217e90da8a6SMatthias Ringwald irk_lookup_state_t sm_identity_resolving_state(hci_con_handle_t con_handle){
218e90da8a6SMatthias Ringwald return IRK_LOOKUP_SUCCEEDED;
219e90da8a6SMatthias Ringwald }
btstack_run_loop_set_timer(btstack_timer_source_t * a,uint32_t timeout_in_ms)220ec820d77SMatthias Ringwald void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
2211bf0402bS[email protected] }
2221bf0402bS[email protected]
2231bf0402bS[email protected] // Set callback that will be executed when timer expires.
btstack_run_loop_set_timer_handler(btstack_timer_source_t * ts,void (* process)(btstack_timer_source_t * _ts))224ec820d77SMatthias Ringwald void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){
2251bf0402bS[email protected] }
2261bf0402bS[email protected]
2271bf0402bS[email protected] // Add/Remove timer source.
btstack_run_loop_add_timer(btstack_timer_source_t * timer)228ec820d77SMatthias Ringwald void btstack_run_loop_add_timer(btstack_timer_source_t *timer){
2291bf0402bS[email protected] }
2301bf0402bS[email protected]
btstack_run_loop_remove_timer(btstack_timer_source_t * timer)231ec820d77SMatthias Ringwald int btstack_run_loop_remove_timer(btstack_timer_source_t *timer){
2321bf0402bS[email protected] return 1;
2331bf0402bS[email protected] }
2341bf0402bS[email protected]
235ffe2840fSMilanka Ringwald // todo:
hci_connection_for_bd_addr_and_type(const bd_addr_t addr,bd_addr_type_t addr_type)2361d3bd1e5SMatthias Ringwald hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){
237ffe2840fSMilanka Ringwald printf("hci_connection_for_bd_addr_and_type not implemented in mock backend\n");
238ffe2840fSMilanka Ringwald return NULL;
239ffe2840fSMilanka Ringwald }
hci_connection_for_handle(hci_con_handle_t con_handle)240ffe2840fSMilanka Ringwald hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
241733e49f4SMilanka Ringwald if (con_handle == hci_connection.con_handle){
2424a340927SMatthias Ringwald return &hci_connection;
243733e49f4SMilanka Ringwald } else {
244733e49f4SMilanka Ringwald return NULL;
245733e49f4SMilanka Ringwald }
246ffe2840fSMilanka Ringwald }
hci_connections_get_iterator(btstack_linked_list_iterator_t * it)247665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){
248ffe2840fSMilanka Ringwald // printf("hci_connections_get_iterator not implemented in mock backend\n");
249665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(it, &connections);
250ffe2840fSMilanka Ringwald }
251567c20c9S[email protected]
hci_setup_connection(uint16_t con_handle,bd_addr_type_t type)252bbcf7688SMilanka Ringwald static void hci_setup_connection(uint16_t con_handle, bd_addr_type_t type){
253bbcf7688SMilanka Ringwald hci_connection.att_connection.mtu = 23;
254bbcf7688SMilanka Ringwald hci_connection.att_connection.con_handle = con_handle;
255bbcf7688SMilanka Ringwald hci_connection.att_connection.max_mtu = 23;
256bbcf7688SMilanka Ringwald hci_connection.att_connection.encryption_key_size = 0;
257bbcf7688SMilanka Ringwald hci_connection.att_connection.authenticated = 0;
258bbcf7688SMilanka Ringwald hci_connection.att_connection.authorized = 0;
259bbcf7688SMilanka Ringwald
260bbcf7688SMilanka Ringwald hci_connection.att_server.ir_le_device_db_index = 0;
261bbcf7688SMilanka Ringwald
262bbcf7688SMilanka Ringwald hci_connection.con_handle = con_handle;
263bbcf7688SMilanka Ringwald hci_connection.address_type = type;
264bbcf7688SMilanka Ringwald
265bbcf7688SMilanka Ringwald if (btstack_linked_list_empty(&connections)){
266bbcf7688SMilanka Ringwald btstack_linked_list_add(&connections, (btstack_linked_item_t *)&hci_connection);
267bbcf7688SMilanka Ringwald }
268bbcf7688SMilanka Ringwald }
269bbcf7688SMilanka Ringwald
hci_setup_le_connection(uint16_t con_handle)270bbcf7688SMilanka Ringwald void hci_setup_le_connection(uint16_t con_handle){
271bbcf7688SMilanka Ringwald hci_setup_connection(con_handle, BD_ADDR_TYPE_LE_PUBLIC);
272bbcf7688SMilanka Ringwald }
273bbcf7688SMilanka Ringwald
l2cap_run(void)274f45e14b1S[email protected] void l2cap_run(void){
275f45e14b1S[email protected] }
276ec4690afSMatthias Ringwald #include "btstack_run_loop.h"
277ec4690afSMatthias Ringwald
btstack_run_loop_execute_on_main_thread(btstack_context_callback_registration_t * callback_registration)278ec4690afSMatthias Ringwald void btstack_run_loop_execute_on_main_thread(btstack_context_callback_registration_t * callback_registration){
279ec4690afSMatthias Ringwald callback_registration->callback(callback_registration->context);
280ec4690afSMatthias Ringwald }
281