xref: /btstack/test/gatt_client/mock.c (revision cbf509016daa1295a6a8b22d7314db50b88c6c9a)
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <string.h>
4 
5 #include "hci.h"
6 #include "hci_dump.h"
7 #include "l2cap.h"
8 
9 #include "ble/att_db.h"
10 #include "ble/sm.h"
11 #include "gap.h"
12 
13 #define PREBUFFER_SIZE (HCI_INCOMING_PRE_BUFFER_SIZE + 8)
14 #define TEST_MAX_MTU 23
15 
16 static btstack_packet_handler_t att_packet_handler;
17 static void (*registered_hci_event_handler) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL;
18 
19 static btstack_linked_list_t     connections;
20 static uint8_t  l2cap_stack_buffer[PREBUFFER_SIZE + TEST_MAX_MTU];	// pre buffer + HCI Header + L2CAP header
21 static uint16_t gatt_client_handle = 0x40;
22 static hci_connection_t hci_connection;
23 
24 uint16_t get_gatt_client_handle(void){
25 	return gatt_client_handle;
26 }
27 
28 void mock_simulate_command_complete(const hci_cmd_t *cmd){
29 	uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, (uint8_t) (cmd->opcode & 0xff), (uint8_t) (cmd->opcode >> 8), 0};
30 	registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet));
31 }
32 
33 void mock_simulate_hci_state_working(void){
34 	uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
35 	registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, 3);
36 }
37 
38 void mock_simulate_connected(void){
39 	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};
40 	registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet));
41 }
42 
43 void mock_simulate_scan_response(void){
44 	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};
45 	registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet));
46 }
47 bool gap_authenticated(hci_con_handle_t con_handle){
48 	UNUSED(con_handle);
49 	return false;
50 }
51 uint8_t gap_encryption_key_size(hci_con_handle_t con_handle){
52 	UNUSED(con_handle);
53 	return 0;
54 }
55 void sm_request_pairing(hci_con_handle_t con_handle){
56 	UNUSED(con_handle);
57 }
58 void gap_start_scan(void){
59 }
60 void gap_stop_scan(void){
61 }
62 uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){
63 	return 0;
64 }
65 void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
66 }
67 
68 int gap_reconnect_security_setup_active(hci_con_handle_t con_handle){
69 	UNUSED(con_handle);
70 	return 0;
71 }
72 
73 static void att_init_connection(att_connection_t * att_connection){
74     att_connection->mtu = TEST_MAX_MTU;
75     att_connection->max_mtu = TEST_MAX_MTU;
76     att_connection->encryption_key_size = 0;
77     att_connection->authenticated = 0;
78 	att_connection->authorized = 0;
79 }
80 
81 bool hci_can_send_acl_le_packet_now(void){
82 	return true;
83 }
84 
85 bool  l2cap_can_send_connectionless_packet_now(void){
86 	return true;
87 }
88 
89 uint8_t *l2cap_get_outgoing_buffer(void){
90 	return (uint8_t *)&l2cap_stack_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 8];
91 }
92 
93 uint16_t l2cap_max_mtu(void){
94     return TEST_MAX_MTU;
95 }
96 
97 uint16_t l2cap_max_le_mtu(void){
98     return TEST_MAX_MTU;
99 }
100 
101 void l2cap_init(void){}
102 
103 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
104     att_packet_handler = packet_handler;
105 }
106 
107 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
108 	registered_hci_event_handler = callback_handler->callback;
109 }
110 
111 bool l2cap_reserve_packet_buffer(void){
112 	return true;
113 }
114 
115 bool l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){
116 	return true;
117 }
118 
119 void l2cap_request_can_send_fix_channel_now_event(uint16_t handle, uint16_t channel_id){
120 	uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 1, 0};
121 	att_packet_handler(HCI_EVENT_PACKET, 0, (uint8_t*)event, sizeof(event));
122 }
123 
124 uint8_t l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
125 	att_connection_t att_connection;
126 	att_init_connection(&att_connection);
127 	uint8_t response_buffer[PREBUFFER_SIZE + TEST_MAX_MTU];
128 	uint8_t * response = &response_buffer[PREBUFFER_SIZE];
129 	uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, response);
130 	if (response_len){
131 		att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len);
132 	}
133 	return ERROR_CODE_SUCCESS;
134 }
135 
136 void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
137 }
138 
139 int  sm_cmac_ready(void){
140 	return 1;
141 }
142 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)){
143 	//sm_notify_client(SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched);
144 }
145 int sm_le_device_index(uint16_t handle ){
146 	return -1;
147 }
148 void sm_send_security_request(hci_con_handle_t con_handle){
149 }
150 
151 irk_lookup_state_t sm_identity_resolving_state(hci_con_handle_t con_handle){
152 	return IRK_LOOKUP_SUCCEEDED;
153 }
154 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
155 }
156 
157 // Set callback that will be executed when timer expires.
158 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){
159 }
160 
161 // Add/Remove timer source.
162 void btstack_run_loop_add_timer(btstack_timer_source_t *timer){
163 }
164 
165 int  btstack_run_loop_remove_timer(btstack_timer_source_t *timer){
166 	return 1;
167 }
168 
169 // todo:
170 hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){
171 	printf("hci_connection_for_bd_addr_and_type not implemented in mock backend\n");
172 	return NULL;
173 }
174 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
175 	return &hci_connection;
176 }
177 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){
178 	// printf("hci_connections_get_iterator not implemented in mock backend\n");
179     btstack_linked_list_iterator_init(it, &connections);
180 }
181 
182 // int hci_send_cmd(const hci_cmd_t *cmd, ...){
183 // //	printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
184 // 	return 0;
185 // }
186 
187 // int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
188 // 	return 1;
189 // }
190 
191 // void hci_disconnect_security_block(hci_con_handle_t con_handle){
192 // 	printf("hci_disconnect_security_block \n");
193 // }
194 
195 // void hci_dump_log(const char * format, ...){
196 // 	printf("hci_disconnect_security_block \n");
197 // }
198 
199 void l2cap_run(void){
200 }
201