xref: /btstack/test/gatt_server/mock.c (revision d13e5cf6603f8d92493ebd8a19ad22c4dfb7a126)
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include "hci.h"
7 #include "gap.h"
8 #include "hci_dump.h"
9 #include "l2cap.h"
10 
11 #include "ble/att_db.h"
12 #include "ble/att_dispatch.h"
13 #include "ble/sm.h"
14 
15 #include "btstack_debug.h"
16 
17 static btstack_packet_handler_t att_server_packet_handler;
18 static void (*registered_hci_event_handler) (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = NULL;
19 
20 static btstack_linked_list_t     connections;
21 static uint16_t max_mtu = 23;
22 static uint8_t  l2cap_stack_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 8 + ATT_DEFAULT_MTU];	// pre buffer + HCI Header + L2CAP header
23 static uint16_t gatt_client_handle = 0x40;
24 static hci_connection_t hci_connection;
25 
26 uint16_t get_gatt_client_handle(void){
27 	return gatt_client_handle;
28 }
29 
30 void mock_simulate_command_complete(const hci_cmd_t *cmd){
31 	uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, (uint8_t) (cmd->opcode & 0xff), (uint8_t) (cmd->opcode >> 8), 0};
32 	registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet));
33 }
34 
35 void mock_simulate_hci_state_working(void){
36 	uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
37 	registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, 3);
38 }
39 
40 static void hci_create_gap_connection_complete_event(const uint8_t * hci_event, uint8_t * gap_event) {
41     gap_event[0] = HCI_EVENT_META_GAP;
42     gap_event[1] = 36 - 2;
43     gap_event[2] = GAP_SUBEVENT_LE_CONNECTION_COMPLETE;
44     switch (hci_event[2]){
45         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
46             memcpy(&gap_event[3], &hci_event[3], 11);
47             memset(&gap_event[14], 0, 12);
48             memcpy(&gap_event[26], &hci_event[14], 7);
49             memset(&gap_event[33], 0xff, 3);
50             break;
51         case HCI_SUBEVENT_LE_ENHANCED_CONNECTION_COMPLETE_V1:
52             memcpy(&gap_event[3], &hci_event[3], 30);
53             memset(&gap_event[33], 0xff, 3);
54             break;
55         case HCI_SUBEVENT_LE_ENHANCED_CONNECTION_COMPLETE_V2:
56             memcpy(&gap_event[3], &hci_event[3], 33);
57             break;
58         default:
59             btstack_unreachable();
60             break;
61     }
62 }
63 
64 void mock_simulate_connected(void){
65 	uint8_t packet[] = {0x3E, 0x13, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x50, 0x00, 0x00, 0x00, 0xD0, 0x07, 0x05};
66     uint8_t gap_event[36];
67     hci_create_gap_connection_complete_event(packet, gap_event);
68     registered_hci_event_handler(HCI_EVENT_PACKET, 0, gap_event, sizeof(gap_event));
69 }
70 
71 void mock_simulate_scan_response(void){
72 	uint8_t packet[] = {0xE2, 0x13, 0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B, 0xCC, 0x09, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
73 	registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet));
74 }
75 
76 void gap_start_scan(void){
77 }
78 void gap_stop_scan(void){
79 }
80 uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){
81 	return 0;
82 }
83 void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
84 }
85 
86 bool gap_reconnect_security_setup_active(hci_con_handle_t con_handle){
87     UNUSED(con_handle);
88     return false;
89 }
90 
91 static void hci_setup_connection(uint16_t con_handle, bd_addr_type_t type){
92     hci_connection.att_connection.mtu = 23;
93     hci_connection.att_connection.con_handle = con_handle;
94     hci_connection.att_connection.max_mtu = 23;
95     hci_connection.att_connection.encryption_key_size = 0;
96     hci_connection.att_connection.authenticated = 0;
97     hci_connection.att_connection.authorized = 0;
98 
99     hci_connection.att_server.ir_le_device_db_index = 0;
100 
101     hci_connection.con_handle = con_handle;
102     hci_connection.address_type = type;
103 
104     if (btstack_linked_list_empty(&connections)){
105         btstack_linked_list_add(&connections, (btstack_linked_item_t *)&hci_connection);
106     }
107 }
108 
109 void hci_setup_le_connection(uint16_t con_handle){
110     hci_setup_connection(con_handle, BD_ADDR_TYPE_LE_PUBLIC);
111 }
112 
113 void hci_setup_classic_connection(uint16_t con_handle){
114     hci_setup_connection(con_handle, BD_ADDR_TYPE_ACL);
115 }
116 
117 
118 void mock_l2cap_set_max_mtu(uint16_t mtu){
119     max_mtu = mtu;
120 }
121 
122 void hci_deinit(void){
123     hci_connection.att_connection.mtu = 0;
124     hci_connection.att_connection.con_handle = HCI_CON_HANDLE_INVALID;
125     hci_connection.att_connection.max_mtu = 0;
126     hci_connection.att_connection.encryption_key_size = 0;
127     hci_connection.att_connection.authenticated = 0;
128     hci_connection.att_connection.authorized = 0;
129     hci_connection.att_server.ir_le_device_db_index = 0;
130     hci_connection.att_server.notification_requests = NULL;
131     hci_connection.att_server.indication_requests = NULL;
132     connections = NULL;
133 }
134 
135 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
136     registered_hci_event_handler = callback_handler->callback;
137 }
138 
139 bool hci_can_send_command_packet_now(void){
140 	return true;
141 }
142 
143 HCI_STATE hci_get_state(void){
144 	return HCI_STATE_WORKING;
145 }
146 
147 uint8_t hci_send_cmd(const hci_cmd_t *cmd, ...){
148 	btstack_assert(false);
149 	return ERROR_CODE_SUCCESS;
150 }
151 
152 void hci_halting_defer(void){
153 }
154 
155 bool l2cap_can_send_connectionless_packet_now(void){
156 	return 1;
157 }
158 
159 void l2cap_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
160     UNUSED(callback_handler);
161 }
162 
163 uint8_t *l2cap_get_outgoing_buffer(void){
164 	// printf("l2cap_get_outgoing_buffer\n");
165 	return (uint8_t *)&l2cap_stack_buffer; // 8 bytes
166 }
167 
168 uint16_t l2cap_max_mtu(void){
169 	// printf("l2cap_max_mtu\n");
170     return max_mtu;
171 }
172 
173 uint16_t l2cap_max_le_mtu(void){
174 	return max_mtu;
175 }
176 
177 void l2cap_init(void){}
178 
179 bool l2cap_reserve_packet_buffer(void){
180 	return true;
181 }
182 
183 void l2cap_release_packet_buffer(void){
184 }
185 
186 static uint8_t l2cap_can_send_fixed_channel_packet_now_status = 1;
187 
188 void l2cap_can_send_fixed_channel_packet_now_set_status(uint8_t status){
189 	l2cap_can_send_fixed_channel_packet_now_status = status;
190 }
191 
192 bool l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){
193 	return l2cap_can_send_fixed_channel_packet_now_status;
194 }
195 
196 void l2cap_request_can_send_fix_channel_now_event(uint16_t handle, uint16_t channel_id){
197 	uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 1, 0};
198     att_server_packet_handler(HCI_EVENT_PACKET, 0, (uint8_t*)event, sizeof(event));
199 }
200 
201 uint8_t l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
202 	att_connection_t att_connection;
203     hci_setup_le_connection(handle);
204 	uint8_t response[max_mtu];
205 	uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, &response[0]);
206 	if (response_len){
207         att_server_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len);
208 	}
209 	return ERROR_CODE_SUCCESS;
210 }
211 
212 static int cmac_ready = 1;
213 void set_cmac_ready(int ready){
214     cmac_ready = ready;
215 }
216 
217 void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
218 }
219 
220 int  sm_cmac_ready(void){
221 	return cmac_ready;
222 }
223 
224 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)){
225 	// sm_notify_client(SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched);
226 }
227 
228 int sm_le_device_index(uint16_t handle ){
229 	return -1;
230 }
231 
232 irk_lookup_state_t sm_identity_resolving_state(hci_con_handle_t con_handle){
233 	return IRK_LOOKUP_SUCCEEDED;
234 }
235 
236 void sm_request_pairing(hci_con_handle_t con_handle){
237 }
238 
239 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
240 }
241 
242 // Set callback that will be executed when timer expires.
243 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){
244 }
245 
246 // Add/Remove timer source.
247 void btstack_run_loop_add_timer(btstack_timer_source_t *timer){
248 }
249 
250 int  btstack_run_loop_remove_timer(btstack_timer_source_t *timer){
251 	return 1;
252 }
253 
254 void * btstack_run_loop_get_timer_context(btstack_timer_source_t *ts){
255     return ts->context;
256 }
257 
258 // todo:
259 hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){
260 	printf("hci_connection_for_bd_addr_and_type not implemented in mock backend\n");
261 	return NULL;
262 }
263 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
264     if (hci_connection.con_handle != con_handle){
265         return NULL;
266     }
267     return &hci_connection;
268 }
269 
270 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){
271 	// printf("hci_connections_get_iterator not implemented in mock backend\n");
272     btstack_linked_list_iterator_init(it, &connections);
273 }
274 
275 
276 void l2cap_run(void){
277 }
278 
279 void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
280 }
281 
282 bool gap_authenticated(hci_con_handle_t con_handle){
283 	return false;
284 }
285 authorization_state_t gap_authorization_state(hci_con_handle_t con_handle){
286 	return AUTHORIZATION_UNKNOWN;
287 }
288 uint8_t gap_encryption_key_size(hci_con_handle_t con_handle){
289 	return 0;
290 }
291 bool gap_secure_connection(hci_con_handle_t con_handle){
292 	return false;
293 }
294 gap_connection_type_t gap_get_connection_type(hci_con_handle_t con_handle){
295     if (hci_connection.con_handle != con_handle){
296         return GAP_CONNECTION_INVALID;
297     }
298     switch (hci_connection.address_type){
299         case BD_ADDR_TYPE_LE_PUBLIC:
300         case BD_ADDR_TYPE_LE_RANDOM:
301             return GAP_CONNECTION_LE;
302         case BD_ADDR_TYPE_SCO:
303             return GAP_CONNECTION_SCO;
304         case BD_ADDR_TYPE_ACL:
305             return GAP_CONNECTION_ACL;
306         default:
307             return GAP_CONNECTION_INVALID;
308     }
309 }
310 
311 int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min,
312 	uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){
313 	return 0;
314 }
315 
316 void mock_call_att_server_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
317     (*att_server_packet_handler)(packet_type, channel, packet, size);
318 }
319 
320 void mock_call_att_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
321     (*registered_hci_event_handler)(packet_type, channel, packet, size);
322 }
323 
324 void att_dispatch_register_server(btstack_packet_handler_t packet_handler){
325     att_server_packet_handler = packet_handler;
326 }
327 
328 bool att_dispatch_server_can_send_now(hci_con_handle_t con_handle){
329     UNUSED(con_handle);
330     return l2cap_can_send_fixed_channel_packet_now_status;
331 }
332 
333 void att_dispatch_server_mtu_exchanged(hci_con_handle_t con_handle, uint16_t new_mtu){
334     UNUSED(con_handle);
335     UNUSED(new_mtu);
336 }
337 
338 void att_dispatch_server_request_can_send_now_event(hci_con_handle_t con_handle){
339     if (l2cap_can_send_fixed_channel_packet_now_status){
340         uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 1, 0};
341         att_server_packet_handler(HCI_EVENT_PACKET, 0, (uint8_t*)event, sizeof(event));
342     }
343 }
344 
345