xref: /btstack/test/gatt_server/mock.c (revision cd5f23a3250874824c01a2b3326a9522fea3f99f)
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include "hci.h"
7 #include "hci_dump.h"
8 #include "l2cap.h"
9 
10 #include "ble/att_db.h"
11 #include "ble/gatt_client.h"
12 #include "ble/sm.h"
13 
14 #include "btstack_debug.h"
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 const uint16_t max_mtu = 23;
21 static uint8_t  l2cap_stack_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 8 + max_mtu];	// pre buffer + HCI Header + L2CAP header
22 static uint16_t gatt_client_handle = 0x40;
23 static hci_connection_t hci_connection;
24 
25 uint16_t get_gatt_client_handle(void){
26 	return gatt_client_handle;
27 }
28 
29 void mock_simulate_command_complete(const hci_cmd_t *cmd){
30 	uint8_t packet[] = {HCI_EVENT_COMMAND_COMPLETE, 4, 1, (uint8_t) (cmd->opcode & 0xff), (uint8_t) (cmd->opcode >> 8), 0};
31 	registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet));
32 }
33 
34 void mock_simulate_hci_state_working(void){
35 	uint8_t packet[3] = {BTSTACK_EVENT_STATE, 0, HCI_STATE_WORKING};
36 	registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, 3);
37 }
38 
39 void mock_simulate_connected(void){
40 	uint8_t packet[] = {0x3E, 0x13, 0x01, 0x00, 0x40, 0x00, 0x00, 0x00, 0x9B, 0x77, 0xD1, 0xF7, 0xB1, 0x34, 0x50, 0x00, 0x00, 0x00, 0xD0, 0x07, 0x05};
41 	registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet));
42 }
43 
44 void mock_simulate_scan_response(void){
45 	uint8_t packet[] = {0xE2, 0x13, 0xE2, 0x01, 0x34, 0xB1, 0xF7, 0xD1, 0x77, 0x9B, 0xCC, 0x09, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
46 	registered_hci_event_handler(HCI_EVENT_PACKET, 0, (uint8_t *)&packet, sizeof(packet));
47 }
48 
49 void gap_start_scan(void){
50 }
51 void gap_stop_scan(void){
52 }
53 uint8_t gap_connect(bd_addr_t addr, bd_addr_type_t addr_type){
54 	return 0;
55 }
56 void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){
57 }
58 
59 int gap_reconnect_security_setup_active(hci_con_handle_t con_handle){
60 	UNUSED(con_handle);
61 	return 0;
62 }
63 
64 static void att_init_connection(att_connection_t * att_connection){
65     att_connection->mtu = 23;
66     att_connection->max_mtu = 23;
67     att_connection->encryption_key_size = 0;
68     att_connection->authenticated = 0;
69 	att_connection->authorized = 0;
70 }
71 
72 int hci_can_send_acl_le_packet_now(void){
73 	return 1;
74 }
75 
76 int hci_can_send_command_packet_now(void){
77 	return 1;
78 }
79 
80 HCI_STATE hci_get_state(void){
81 	return HCI_STATE_WORKING;
82 }
83 
84 int hci_send_cmd(const hci_cmd_t *cmd, ...){
85 	btstack_assert(false);
86 	return 0;
87 }
88 
89 void hci_halting_defer(void){
90 }
91 
92 int  l2cap_can_send_connectionless_packet_now(void){
93 	return 1;
94 }
95 
96 uint8_t *l2cap_get_outgoing_buffer(void){
97 	// printf("l2cap_get_outgoing_buffer\n");
98 	return (uint8_t *)&l2cap_stack_buffer; // 8 bytes
99 }
100 
101 uint16_t l2cap_max_mtu(void){
102 	// printf("l2cap_max_mtu\n");
103     return max_mtu;
104 }
105 
106 uint16_t l2cap_max_le_mtu(void){
107 	// printf("l2cap_max_mtu\n");
108     return max_mtu;
109 }
110 
111 void l2cap_init(void){}
112 
113 void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
114     att_packet_handler = packet_handler;
115 }
116 
117 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
118 	registered_hci_event_handler = callback_handler->callback;
119 }
120 
121 int l2cap_reserve_packet_buffer(void){
122 	return 1;
123 }
124 
125 void l2cap_release_packet_buffer(void){
126 }
127 
128 static uint8_t l2cap_can_send_fixed_channel_packet_now_status = 1;
129 
130 void l2cap_can_send_fixed_channel_packet_now_set_status(uint8_t status){
131 	l2cap_can_send_fixed_channel_packet_now_status = status;
132 }
133 
134 int l2cap_can_send_fixed_channel_packet_now(uint16_t handle, uint16_t channel_id){
135 	return l2cap_can_send_fixed_channel_packet_now_status;
136 }
137 
138 void l2cap_request_can_send_fix_channel_now_event(uint16_t handle, uint16_t channel_id){
139 	uint8_t event[] = { L2CAP_EVENT_CAN_SEND_NOW, 2, 1, 0};
140 	att_packet_handler(HCI_EVENT_PACKET, 0, (uint8_t*)event, sizeof(event));
141 }
142 
143 int l2cap_send_prepared_connectionless(uint16_t handle, uint16_t cid, uint16_t len){
144 	att_connection_t att_connection;
145 	att_init_connection(&att_connection);
146 	uint8_t response[max_mtu];
147 	uint16_t response_len = att_handle_request(&att_connection, l2cap_get_outgoing_buffer(), len, &response[0]);
148 	if (response_len){
149 		att_packet_handler(ATT_DATA_PACKET, gatt_client_handle, &response[0], response_len);
150 	}
151 	return 0;
152 }
153 
154 void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
155 }
156 
157 int  sm_cmac_ready(void){
158 	return 1;
159 }
160 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)){
161 	//sm_notify_client(SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED, sm_central_device_addr_type, sm_central_device_address, 0, sm_central_device_matched);
162 }
163 int sm_le_device_index(uint16_t handle ){
164 	return -1;
165 }
166 
167 irk_lookup_state_t sm_identity_resolving_state(hci_con_handle_t con_handle){
168 	return IRK_LOOKUP_SUCCEEDED;
169 }
170 
171 void sm_request_pairing(hci_con_handle_t con_handle){
172 }
173 
174 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
175 }
176 
177 // Set callback that will be executed when timer expires.
178 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){
179 }
180 
181 // Add/Remove timer source.
182 void btstack_run_loop_add_timer(btstack_timer_source_t *timer){
183 }
184 
185 int  btstack_run_loop_remove_timer(btstack_timer_source_t *timer){
186 	return 1;
187 }
188 
189 void * btstack_run_loop_get_timer_context(btstack_timer_source_t *ts){
190     return ts->context;
191 }
192 
193 // todo:
194 hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){
195 	printf("hci_connection_for_bd_addr_and_type not implemented in mock backend\n");
196 	return NULL;
197 }
198 hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){
199 	if (con_handle != 0) return NULL;
200 	return &hci_connection;
201 }
202 void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){
203 	// printf("hci_connections_get_iterator not implemented in mock backend\n");
204     btstack_linked_list_iterator_init(it, &connections);
205 }
206 
207 // int hci_send_cmd(const hci_cmd_t *cmd, ...){
208 // //	printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
209 // 	return 0;
210 // }
211 
212 // int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){
213 // 	return 1;
214 // }
215 
216 // void hci_disconnect_security_block(hci_con_handle_t con_handle){
217 // 	printf("hci_disconnect_security_block \n");
218 // }
219 
220 // void hci_dump_log(const char * format, ...){
221 // 	printf("hci_disconnect_security_block \n");
222 // }
223 
224 void l2cap_run(void){
225 }
226 
227 void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
228 }
229 
230 int gap_authenticated(hci_con_handle_t con_handle){
231 	return 0;
232 }
233 authorization_state_t gap_authorization_state(hci_con_handle_t con_handle){
234 	return AUTHORIZATION_UNKNOWN;
235 }
236 int gap_encryption_key_size(hci_con_handle_t con_handle){
237 	return 0;
238 }
239 int gap_secure_connection(hci_con_handle_t con_handle){
240 	return 0;
241 }
242 gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){
243 	return GAP_CONNECTION_INVALID;
244 }
245 int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min,
246 	uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){
247 	return 0;
248 }
249