xref: /btstack/test/hfp/mock.c (revision ced70f9bfeafe291ec597a3a9cc862e39e0da3ce)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 // *****************************************************************************
39 //
40 // HFP BTstack Mocks
41 //
42 // *****************************************************************************
43 
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 
49 #include "hci.h"
50 #include "hci_dump.h"
51 #include "classic/sdp_client_rfcomm.h"
52 #include "classic/rfcomm.h"
53 #include "classic/hfp_hf.h"
54 #include "classic/sdp_client.h"
55 #include "classic/sdp_client_rfcomm.h"
56 #include "hci.h"
57 #include "l2cap.h"
58 
59 #include "mock.h"
60 
61 static uint8_t sdp_rfcomm_channel_nr = 1;
62 const char sdp_rfcomm_service_name[] = "BTstackMock";
63 static uint16_t rfcomm_cid = 1;
64 static bd_addr_t dev_addr;
65 static uint16_t sco_handle = 10;
66 static uint8_t rfcomm_payload[1000];
67 static uint16_t rfcomm_payload_len = 0;
68 
69 static uint8_t outgoing_rfcomm_payload[1000];
70 static uint16_t outgoing_rfcomm_payload_len = 0;
71 
72 static uint8_t rfcomm_reserved_buffer[1000];
73 
74 hfp_connection_t * hfp_context;
75 
76 void (*registered_hci_packet_handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
77 void (*registered_rfcomm_packet_handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
78 void (*registered_sdp_app_callback)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
79 
80 uint8_t * get_rfcomm_payload(void){
81 	return &rfcomm_payload[0];
82 }
83 
84 uint16_t get_rfcomm_payload_len(void){
85 	return rfcomm_payload_len;
86 }
87 
88 static int hfp_command_start_index = 0;
89 
90 
91 int has_more_hfp_commands(int start_command_offset, int end_command_offset){
92     int has_cmd = get_rfcomm_payload_len() - hfp_command_start_index >= 2 + start_command_offset + end_command_offset;
93     //printf("has more: payload len %d, start %d, has more %d\n", get_rfcomm_payload_len(), hfp_command_start_index, has_cmd);
94     return has_cmd;
95 }
96 
97 char * get_next_hfp_command(int start_command_offset, int end_command_offset){
98     //printf("get next: payload len %d, start %d\n", get_rfcomm_payload_len(), hfp_command_start_index);
99     char * data = (char *)(&get_rfcomm_payload()[hfp_command_start_index + start_command_offset]);
100     int data_len = get_rfcomm_payload_len() - hfp_command_start_index - start_command_offset;
101 
102     int i;
103 
104     for (i = 0; i < data_len; i++){
105         if ( *(data+i) == '\r' || *(data+i) == '\n' ) {
106             data[i]=0;
107             // update state
108             //printf("!!! command %s\n", data);
109             hfp_command_start_index = hfp_command_start_index + i + start_command_offset + end_command_offset;
110             return data;
111         }
112     }
113     printf("should not got here\n");
114     return NULL;
115 }
116 
117 void print_without_newlines(uint8_t *data, uint16_t len);
118 void print_without_newlines(uint8_t *data, uint16_t len){
119     int found_newline = 0;
120     int found_item = 0;
121 
122     for (int i=0; i<len; i++){
123         if (data[i] == '\r' || data[i] == '\n'){
124             if (!found_newline && found_item) printf("\n");
125             found_newline = 1;
126         } else {
127             printf("%c", data[i]);
128             found_newline = 0;
129             found_item = 1;
130         }
131     }
132     printf("\n");
133 }
134 
135 void l2cap_init(void){}
136 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
137     registered_hci_packet_handler = callback_handler->callback;
138 }
139 
140 uint8_t rfcomm_send(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){
141 
142     // printf("mock: rfcomm send: ");
143     // print_without_newlines(data, len);
144 
145 	int start_command_offset = 2;
146     int end_command_offset = 2;
147 
148     if (strncmp((char*)data, "AT", 2) == 0){
149 		start_command_offset = 0;
150 	}
151 
152     if (has_more_hfp_commands(start_command_offset, end_command_offset)){
153         //printf("Buffer response: ");
154         strncpy((char*)&rfcomm_payload[rfcomm_payload_len], (char*)data, len);
155         rfcomm_payload_len += len;
156     } else {
157         hfp_command_start_index = 0;
158         //printf("Copy response: ");
159         strncpy((char*)&rfcomm_payload[0], (char*)data, len);
160         rfcomm_payload_len = len;
161     }
162 
163     // print_without_newlines(rfcomm_payload,rfcomm_payload_len);
164     return ERROR_CODE_SUCCESS;
165 }
166 
167 uint8_t rfcomm_request_can_send_now_event(uint16_t rfcomm_cid){
168 
169     // printf("mock: rfcomm_request_can_send_now_event\n");
170 
171     uint8_t event[] = { RFCOMM_EVENT_CAN_SEND_NOW, 2, 0, 0};
172     little_endian_store_16(event, 2, rfcomm_cid);
173     registered_rfcomm_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
174     return ERROR_CODE_SUCCESS;
175 }
176 
177 bool rfcomm_reserve_packet_buffer(void){
178     // printf("mock: rfcomm_reserve_packet_buffer\n");
179     return true;
180 };
181 void rfcomm_release_packet_buffer(void){};
182 uint8_t * rfcomm_get_outgoing_buffer(void) {
183     return rfcomm_reserved_buffer;
184 }
185 uint16_t rfcomm_get_max_frame_size(uint16_t rfcomm_cid){
186     return sizeof(rfcomm_reserved_buffer);
187 }
188 uint8_t rfcomm_send_prepared(uint16_t rfcomm_cid, uint16_t len){
189     // printf("--- rfcomm_send_prepared with len %u ---\n", len);
190     return rfcomm_send(rfcomm_cid, rfcomm_reserved_buffer, len);
191 }
192 
193 static void hci_event_sco_complete(void){
194     uint8_t event[19];
195     uint8_t pos = 0;
196     event[pos++] = HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE;
197     event[pos++] = sizeof(event) - 2;
198 
199     event[pos++] = 0; //status
200     little_endian_store_16(event,  pos, sco_handle);   pos += 2; // sco handle
201     reverse_bd_addr(dev_addr, &event[pos]);    pos += 6;
202 
203     event[pos++] = 0; // link_type
204     event[pos++] = 0; // transmission_interval
205     event[pos++] = 0; // retransmission_interval
206 
207     little_endian_store_16(event,  pos, 0);   pos += 2; // rx_packet_length
208     little_endian_store_16(event,  pos, 0);   pos += 2; // tx_packet_length
209 
210     event[pos++] = 0; // air_mode
211     (*registered_hci_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
212 }
213 
214 uint8_t hci_send_cmd(const hci_cmd_t *cmd, ...){
215 	//printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
216     if (cmd->opcode == 0x428){
217         hci_event_sco_complete();
218     }
219 	return ERROR_CODE_SUCCESS;
220 }
221 
222 bool hci_can_send_command_packet_now(void){
223     return true;
224 }
225 
226 static void sdp_query_complete_response(uint8_t status){
227     uint8_t event[3];
228     event[0] = SDP_EVENT_QUERY_COMPLETE;
229     event[1] = 1;
230     event[2] = status;
231     (*registered_sdp_app_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
232 }
233 
234 static void sdp_client_query_rfcomm_service_response(uint8_t status){
235     int sdp_service_name_len = strlen(sdp_rfcomm_service_name);
236     uint8_t event[3+SDP_SERVICE_NAME_LEN+1];
237     event[0] = SDP_EVENT_QUERY_RFCOMM_SERVICE;
238     event[1] = sdp_service_name_len + 1;
239     event[2] = sdp_rfcomm_channel_nr;
240     memcpy(&event[3], sdp_rfcomm_service_name, sdp_service_name_len);
241     event[3+sdp_service_name_len] = 0;
242     (*registered_sdp_app_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
243 }
244 
245 uint8_t sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(btstack_packet_handler_t callback, bd_addr_t remote, uint16_t uuid){
246 	// printf("sdp_client_query_rfcomm_channel_and_name_for_uuid %p\n", registered_sdp_app_callback);
247     registered_sdp_app_callback = callback;
248 	sdp_client_query_rfcomm_service_response(0);
249 	sdp_query_complete_response(0);
250     return 0;
251 }
252 
253 uint8_t sdp_client_register_query_callback(btstack_context_callback_registration_t * callback_registration){
254     (callback_registration->callback)(callback_registration->context);
255     return ERROR_CODE_SUCCESS;
256 }
257 
258 uint8_t rfcomm_create_channel(btstack_packet_handler_t handler, bd_addr_t addr, uint8_t channel, uint16_t * out_cid){
259 
260     // printf("mock: rfcomm_create_channel addr %s\n", bd_addr_to_str(addr));
261 
262     registered_rfcomm_packet_handler = handler;
263 
264 	// RFCOMM_EVENT_CHANNEL_OPENED
265     uint8_t event[16];
266     uint8_t pos = 0;
267     event[pos++] = RFCOMM_EVENT_CHANNEL_OPENED;
268     event[pos++] = sizeof(event) - 2;
269     event[pos++] = 0;
270 
271     reverse_bd_addr(addr, &event[pos]);
272     memcpy(dev_addr, addr, 6);
273     pos += 6;
274 
275     little_endian_store_16(event,  pos, 1);   pos += 2;
276 	event[pos++] = 0;
277 
278 	little_endian_store_16(event, pos, rfcomm_cid); pos += 2;       // channel ID
279 	little_endian_store_16(event, pos, 200); pos += 2;   // max frame size
280     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, pos);
281 
282     if (out_cid){
283         *out_cid = rfcomm_cid;
284     }
285     return ERROR_CODE_SUCCESS;
286 }
287 
288 bool rfcomm_can_send_packet_now(uint16_t rfcomm_cid){
289     // printf("mock: rfcomm_can_send_packet_now\n");
290 	return true;
291 }
292 
293 uint8_t rfcomm_disconnect(uint16_t rfcomm_cid){
294 	uint8_t event[4];
295 	event[0] = RFCOMM_EVENT_CHANNEL_CLOSED;
296     event[1] = sizeof(event) - 2;
297     little_endian_store_16(event, 2, rfcomm_cid);
298     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
299     return ERROR_CODE_SUCCESS;
300 }
301 
302 uint8_t rfcomm_register_service(btstack_packet_handler_t handler, uint8_t channel, uint16_t max_frame_size){
303 	// printf("rfcomm_register_service\n");
304     registered_rfcomm_packet_handler = handler;
305     return ERROR_CODE_SUCCESS;
306 }
307 
308 
309 uint8_t sdp_client_query_rfcomm_channel_and_name_for_search_pattern(btstack_packet_handler_t callback, bd_addr_t remote, const uint8_t * des_serviceSearchPattern){
310 	// printf("sdp_client_query_rfcomm_channel_and_name_for_search_pattern\n");
311     return 0;
312 }
313 
314 
315 uint8_t rfcomm_accept_connection(uint16_t rfcomm_cid){
316 	// printf("rfcomm_accept_connection \n");
317     return ERROR_CODE_SUCCESS;
318 }
319 
320 uint8_t rfcomm_decline_connection(uint16_t rfcomm_cid){
321     // printf("rfcomm_accept_connection \n");
322     return ERROR_CODE_SUCCESS;
323 }
324 
325 void btstack_run_loop_add_timer(btstack_timer_source_t *timer){
326 }
327 
328 int  btstack_run_loop_remove_timer(btstack_timer_source_t *timer){
329     return 0;
330 }
331 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){
332 }
333 
334 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
335 }
336 
337 
338 static void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
339     uint8_t event[6];
340     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
341     event[1] = sizeof(event) - 2;
342     event[2] = 0; // status = OK
343     little_endian_store_16(event, 3, handle);
344     event[5] = reason;
345     (*registered_hci_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
346 }
347 
348 uint8_t gap_disconnect(hci_con_handle_t handle){
349     hci_emit_disconnection_complete(handle, 0);
350     return 0;
351 }
352 
353 uint16_t hci_get_sco_voice_setting(void){
354     return 0x40;
355 }
356 
357 bool hci_remote_esco_supported(hci_con_handle_t handle){
358     return false;
359 }
360 
361 static void add_new_lines_to_hfp_command(uint8_t * data, int len){
362     if (len <= 0) return;
363     memset(&outgoing_rfcomm_payload, 0, 200);
364     int pos = 0;
365 
366     if (strncmp((char*)data, "AT", 2) == 0){
367         strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len);
368         pos += len;
369     } else {
370         outgoing_rfcomm_payload[pos++] = '\r';
371         outgoing_rfcomm_payload[pos++] = '\n';
372         strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len);
373         pos += len;
374     }
375     outgoing_rfcomm_payload[pos++] = '\r';
376     outgoing_rfcomm_payload[pos++] = '\n';
377     outgoing_rfcomm_payload[pos] = 0;
378     outgoing_rfcomm_payload_len = pos;
379 }
380 
381 void inject_hfp_command_to_hf(uint8_t * data, int len){
382     if (memcmp((char*)data, "AT", 2) == 0) return;
383     add_new_lines_to_hfp_command(data, len);
384     // printf("inject_hfp_command_to_hf to HF: ");
385     // print_without_newlines(outgoing_rfcomm_payload,outgoing_rfcomm_payload_len);
386     (*registered_rfcomm_packet_handler)(RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
387 
388 }
389 
390 void inject_hfp_command_to_ag(uint8_t * data, int len){
391     if (data[0] == '+') return;
392 
393     add_new_lines_to_hfp_command(data, len);
394 
395     // printf("mock: inject command to ag: ");
396     // print_without_newlines(data, len);
397 
398     (*registered_rfcomm_packet_handler)(RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
399 }
400 
401 
402 bool hci_extended_sco_link_supported(void){
403     return true;
404 }
405 
406 bool gap_secure_connection(hci_con_handle_t con_handle){
407     UNUSED(con_handle);
408     return true;
409 }
410 
411 uint16_t hci_remote_sco_packet_types(hci_con_handle_t con_handle){
412     UNUSED(con_handle);
413     return SCO_PACKET_TYPES_ALL;
414 }
415 uint16_t hci_usable_sco_packet_types(void){
416     return SCO_PACKET_TYPES_ALL;
417 }
418