xref: /btstack/test/gatt_client/mock.c (revision 5611a760af48d1ce1beea59c7908be73bd2393f1)
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 
103edc84c5SMatthias Ringwald #include "ble/att.h"
113edc84c5SMatthias Ringwald #include "ble/gatt_client.h"
123edc84c5SMatthias Ringwald #include "ble/sm.h"
134f5e8608S[email protected] 
144f5e8608S[email protected] static btstack_packet_handler_t att_packet_handler;
159293bfa4SMatthias Ringwald static void (*registered_l2cap_packet_handler) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL;
164f5e8608S[email protected] 
178f2a52f4SMatthias Ringwald static btstack_linked_list_t     connections;
18c442c7c2S[email protected] static const uint16_t max_mtu = 23;
19cea167a2SMatthias Ringwald static uint8_t  l2cap_stack_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 8 + max_mtu];	// pre buffer + HCI Header + L2CAP header
20ee988ca9S[email protected] uint16_t gatt_client_handle = 0x40;
214f5e8608S[email protected] 
2271de195eSMatthias Ringwald uint16_t get_gatt_client_handle(void){
23567c20c9S[email protected] 	return gatt_client_handle;
24567c20c9S[email protected] }
25567c20c9S[email protected] 
269638642bS[email protected] void mock_simulate_command_complete(const hci_cmd_t *cmd){
274f5e8608S[email protected] 	uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, cmd->opcode & 0xff, cmd->opcode >> 8, 0};
289293bfa4SMatthias Ringwald 	registered_l2cap_packet_handler(HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
294f5e8608S[email protected] }
304f5e8608S[email protected] 
3171de195eSMatthias Ringwald void mock_simulate_hci_state_working(void){
329638642bS[email protected] 	uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
339293bfa4SMatthias Ringwald 	registered_l2cap_packet_handler(HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, 3);
349638642bS[email protected] }
359638642bS[email protected] 
3671de195eSMatthias Ringwald void mock_simulate_connected(void){
37c442c7c2S[email protected] 	uint8_t packet[] = {0x3E, 0x13, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x50, 0x00, 0x00, 0x00, 0xD0, 0x07, 0x05};
389293bfa4SMatthias Ringwald 	registered_l2cap_packet_handler(HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
394f5e8608S[email protected] }
40f45e14b1S[email protected] 
4171de195eSMatthias Ringwald void mock_simulate_scan_response(void){
422edb2b23SMilanka 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};
439293bfa4SMatthias Ringwald 	registered_l2cap_packet_handler(HCI_EVENT_PACKET, NULL, (uint8_t *)&packet, sizeof(packet));
44ffe2840fSMilanka Ringwald }
45ffe2840fSMilanka Ringwald 
46616edd56SMatthias Ringwald uint8_t le_central_start_scan(void){
47616edd56SMatthias Ringwald 	return 0;
48ffe2840fSMilanka Ringwald }
49616edd56SMatthias Ringwald uint8_t le_central_stop_scan(void){
50616edd56SMatthias Ringwald 	return 0;
51ffe2840fSMilanka Ringwald }
52616edd56SMatthias Ringwald uint8_t le_central_connect(bd_addr_t addr, bd_addr_type_t addr_type){
53616edd56SMatthias Ringwald 	return 0;
54ffe2840fSMilanka Ringwald }
55ffe2840fSMilanka Ringwald void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
56c442c7c2S[email protected] }
57c442c7c2S[email protected] 
584d890b2dS[email protected] static void att_init_connection(att_connection_t * att_connection){
594d890b2dS[email protected]     att_connection->mtu = 23;
60d7d7d1e4SMilanka Ringwald     att_connection->max_mtu = 23;
614d890b2dS[email protected]     att_connection->encryption_key_size = 0;
624d890b2dS[email protected]     att_connection->authenticated = 0;
634d890b2dS[email protected] 	att_connection->authorized = 0;
64c442c7c2S[email protected] }
65c442c7c2S[email protected] 
6671de195eSMatthias Ringwald int  l2cap_can_send_connectionless_packet_now(void){
674f5e8608S[email protected] 	return 1;
68f45e14b1S[email protected] }
69f45e14b1S[email protected] 
70f45e14b1S[email protected] 
71f45e14b1S[email protected] uint8_t *l2cap_get_outgoing_buffer(void){
72f5bdf8a1S[email protected] 	// printf("l2cap_get_outgoing_buffer\n");
73f45e14b1S[email protected] 	return (uint8_t *)&l2cap_stack_buffer; // 8 bytes
74f45e14b1S[email protected] }
75f45e14b1S[email protected] 
76f45e14b1S[email protected] uint16_t l2cap_max_mtu(void){
77f5bdf8a1S[email protected] 	// printf("l2cap_max_mtu\n");
78c442c7c2S[email protected]     return max_mtu;
79f45e14b1S[email protected] }
80f45e14b1S[email protected] 
81567c20c9S[email protected] uint16_t l2cap_max_le_mtu(void){
82567c20c9S[email protected] 	// printf("l2cap_max_mtu\n");
83567c20c9S[email protected]     return max_mtu;
84567c20c9S[email protected] }
85f45e14b1S[email protected] 
86ffe2840fSMilanka Ringwald void l2cap_init(){}
87ffe2840fSMilanka Ringwald 
88f45e14b1S[email protected] void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
894f5e8608S[email protected]     att_packet_handler = packet_handler;
90f45e14b1S[email protected] }
91f45e14b1S[email protected] 
929293bfa4SMatthias Ringwald void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
932edb2b23SMilanka Ringwald 	registered_l2cap_packet_handler = handler;
94f45e14b1S[email protected] }
95f45e14b1S[email protected] 
96ffe2840fSMilanka Ringwald 
97f45e14b1S[email protected] int l2cap_reserve_packet_buffer(void){
98567c20c9S[email protected] 	return 1;
99567c20c9S[email protected] }
100567c20c9S[email protected] 
101567c20c9S[email protected] int l2cap_can_send_fixed_channel_packet_now(uint16_t handle){
102c442c7c2S[email protected] 	return 1;
103f45e14b1S[email protected] }
104f45e14b1S[email protected] 
105f45e14b1S[email protected] int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
1064d890b2dS[email protected] 	att_connection_t att_connection;
1074d890b2dS[email protected] 	att_init_connection(&att_connection);
1084d890b2dS[email protected] 	uint8_t response[max_mtu];
1094d890b2dS[email protected] 	uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, &response[0]);
110ecedf63aS[email protected] 	if (response_len){
111ee988ca9S[email protected] 		att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len);
112ecedf63aS[email protected] 	}
113f45e14b1S[email protected] 	return 0;
114f45e14b1S[email protected] }
115f45e14b1S[email protected] 
11671de195eSMatthias Ringwald int  sm_cmac_ready(void){
117567c20c9S[email protected] 	return 1;
118567c20c9S[email protected] }
1190dbb2346SMatthias Ringwald 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])){
120*5611a760SMatthias Ringwald 	//sm_notify_client(SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched);
121567c20c9S[email protected] }
12257fe2af8SMatthias Ringwald int sm_le_device_index(uint16_t handle ){
12357fe2af8SMatthias Ringwald 	return -1;
12457fe2af8SMatthias Ringwald }
125567c20c9S[email protected] 
126ec820d77SMatthias Ringwald void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
1271bf0402bS[email protected] }
1281bf0402bS[email protected] 
1291bf0402bS[email protected] // Set callback that will be executed when timer expires.
130ec820d77SMatthias Ringwald void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){
1311bf0402bS[email protected] }
1321bf0402bS[email protected] 
1331bf0402bS[email protected] // Add/Remove timer source.
134ec820d77SMatthias Ringwald void btstack_run_loop_add_timer(btstack_timer_source_t *timer){
1351bf0402bS[email protected] }
1361bf0402bS[email protected] 
137ec820d77SMatthias Ringwald int  btstack_run_loop_remove_timer(btstack_timer_source_t *timer){
1381bf0402bS[email protected] 	return 1;
1391bf0402bS[email protected] }
1401bf0402bS[email protected] 
141ffe2840fSMilanka Ringwald // todo:
142ffe2840fSMilanka Ringwald hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){
143ffe2840fSMilanka Ringwald 	printf("hci_connection_for_bd_addr_and_type not implemented in mock backend\n");
144ffe2840fSMilanka Ringwald 	return NULL;
145ffe2840fSMilanka Ringwald }
146ffe2840fSMilanka Ringwald hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
147ffe2840fSMilanka Ringwald 	printf("hci_connection_for_handle not implemented in mock backend\n");
148ffe2840fSMilanka Ringwald 	return NULL;
149ffe2840fSMilanka Ringwald }
150665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){
151ffe2840fSMilanka Ringwald 	// printf("hci_connections_get_iterator not implemented in mock backend\n");
152665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(it, &connections);
153ffe2840fSMilanka Ringwald }
154567c20c9S[email protected] 
155f6424062S[email protected] // int hci_send_cmd(const hci_cmd_t *cmd, ...){
156f6424062S[email protected] // //	printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
157f6424062S[email protected] // 	return 0;
158f6424062S[email protected] // }
159f45e14b1S[email protected] 
160f6424062S[email protected] // int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
161f6424062S[email protected] // 	return 1;
162f6424062S[email protected] // }
1634f5e8608S[email protected] 
164f6424062S[email protected] // void hci_disconnect_security_block(hci_con_handle_t con_handle){
165f6424062S[email protected] // 	printf("hci_disconnect_security_block \n");
166f6424062S[email protected] // }
167f6424062S[email protected] 
168f6424062S[email protected] // void hci_dump_log(const char * format, ...){
169f6424062S[email protected] // 	printf("hci_disconnect_security_block \n");
170f6424062S[email protected] // }
1714f5e8608S[email protected] 
172f45e14b1S[email protected] void l2cap_run(void){
173f45e14b1S[email protected] }
174