xref: /btstack/test/hfp/mock.c (revision 4fd33db7036b1487a4b368055df28fd1c2023015)
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 int  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 0;
165 }
166 
167 void 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 }
175 
176 int       rfcomm_reserve_packet_buffer(void){
177     // printf("mock: rfcomm_reserve_packet_buffer\n");
178     return 1;
179 };
180 void      rfcomm_release_packet_buffer(void){};
181 uint8_t * rfcomm_get_outgoing_buffer(void) {
182     return rfcomm_reserved_buffer;
183 }
184 uint16_t rfcomm_get_max_frame_size(uint16_t rfcomm_cid){
185     return sizeof(rfcomm_reserved_buffer);
186 }
187 int rfcomm_send_prepared(uint16_t rfcomm_cid, uint16_t len){
188     // printf("--- rfcomm_send_prepared with len %u ---\n", len);
189     return rfcomm_send(rfcomm_cid, rfcomm_reserved_buffer, len);
190 }
191 
192 static void hci_event_sco_complete(void){
193     uint8_t event[19];
194     uint8_t pos = 0;
195     event[pos++] = HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE;
196     event[pos++] = sizeof(event) - 2;
197 
198     event[pos++] = 0; //status
199     little_endian_store_16(event,  pos, sco_handle);   pos += 2; // sco handle
200     reverse_bd_addr(dev_addr, &event[pos]);    pos += 6;
201 
202     event[pos++] = 0; // link_type
203     event[pos++] = 0; // transmission_interval
204     event[pos++] = 0; // retransmission_interval
205 
206     little_endian_store_16(event,  pos, 0);   pos += 2; // rx_packet_length
207     little_endian_store_16(event,  pos, 0);   pos += 2; // tx_packet_length
208 
209     event[pos++] = 0; // air_mode
210     (*registered_hci_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
211 }
212 
213 int hci_send_cmd(const hci_cmd_t *cmd, ...){
214 	//printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
215     if (cmd->opcode == 0x428){
216         hci_event_sco_complete();
217     }
218 	return 0;
219 }
220 
221 int hci_can_send_command_packet_now(void){
222     return 1;
223 }
224 
225 static void sdp_query_complete_response(uint8_t status){
226     uint8_t event[3];
227     event[0] = SDP_EVENT_QUERY_COMPLETE;
228     event[1] = 1;
229     event[2] = status;
230     (*registered_sdp_app_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
231 }
232 
233 static void sdp_client_query_rfcomm_service_response(uint8_t status){
234     int sdp_service_name_len = strlen(sdp_rfcomm_service_name);
235     uint8_t event[3+SDP_SERVICE_NAME_LEN+1];
236     event[0] = SDP_EVENT_QUERY_RFCOMM_SERVICE;
237     event[1] = sdp_service_name_len + 1;
238     event[2] = sdp_rfcomm_channel_nr;
239     memcpy(&event[3], sdp_rfcomm_service_name, sdp_service_name_len);
240     event[3+sdp_service_name_len] = 0;
241     (*registered_sdp_app_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
242 }
243 
244 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){
245 	// printf("sdp_client_query_rfcomm_channel_and_name_for_uuid %p\n", registered_sdp_app_callback);
246     registered_sdp_app_callback = callback;
247 	sdp_client_query_rfcomm_service_response(0);
248 	sdp_query_complete_response(0);
249     return 0;
250 }
251 
252 uint8_t sdp_client_register_query_callback(btstack_context_callback_registration_t * callback_registration){
253     (callback_registration->callback)(callback_registration->context);
254     return ERROR_CODE_SUCCESS;
255 }
256 
257 uint8_t rfcomm_create_channel(btstack_packet_handler_t handler, bd_addr_t addr, uint8_t channel, uint16_t * out_cid){
258 
259     // printf("mock: rfcomm_create_channel addr %s\n", bd_addr_to_str(addr));
260 
261     registered_rfcomm_packet_handler = handler;
262 
263 	// RFCOMM_EVENT_CHANNEL_OPENED
264     uint8_t event[16];
265     uint8_t pos = 0;
266     event[pos++] = RFCOMM_EVENT_CHANNEL_OPENED;
267     event[pos++] = sizeof(event) - 2;
268     event[pos++] = 0;
269 
270     reverse_bd_addr(addr, &event[pos]);
271     memcpy(dev_addr, addr, 6);
272     pos += 6;
273 
274     little_endian_store_16(event,  pos, 1);   pos += 2;
275 	event[pos++] = 0;
276 
277 	little_endian_store_16(event, pos, rfcomm_cid); pos += 2;       // channel ID
278 	little_endian_store_16(event, pos, 200); pos += 2;   // max frame size
279     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, pos);
280 
281     if (out_cid){
282         *out_cid = rfcomm_cid;
283     }
284     return 0;
285 }
286 
287 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){
288     // printf("mock: rfcomm_can_send_packet_now\n");
289 	return 1;
290 }
291 
292 void rfcomm_disconnect(uint16_t rfcomm_cid){
293 	uint8_t event[4];
294 	event[0] = RFCOMM_EVENT_CHANNEL_CLOSED;
295     event[1] = sizeof(event) - 2;
296     little_endian_store_16(event, 2, rfcomm_cid);
297     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
298 }
299 
300 uint8_t rfcomm_register_service(btstack_packet_handler_t handler, uint8_t channel, uint16_t max_frame_size){
301 	// printf("rfcomm_register_service\n");
302     registered_rfcomm_packet_handler = handler;
303     return 0;
304 }
305 
306 
307 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){
308 	// printf("sdp_client_query_rfcomm_channel_and_name_for_search_pattern\n");
309     return 0;
310 }
311 
312 
313 void rfcomm_accept_connection(uint16_t rfcomm_cid){
314 	// printf("rfcomm_accept_connection \n");
315 }
316 
317 void rfcomm_decline_connection(uint16_t rfcomm_cid){
318     // printf("rfcomm_accept_connection \n");
319 }
320 
321 void btstack_run_loop_add_timer(btstack_timer_source_t *timer){
322 }
323 
324 int  btstack_run_loop_remove_timer(btstack_timer_source_t *timer){
325     return 0;
326 }
327 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){
328 }
329 
330 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
331 }
332 
333 
334 static void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
335     uint8_t event[6];
336     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
337     event[1] = sizeof(event) - 2;
338     event[2] = 0; // status = OK
339     little_endian_store_16(event, 3, handle);
340     event[5] = reason;
341     (*registered_hci_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
342 }
343 
344 uint8_t gap_disconnect(hci_con_handle_t handle){
345     hci_emit_disconnection_complete(handle, 0);
346     return 0;
347 }
348 
349 uint16_t hci_get_sco_voice_setting(void){
350     return 0x40;
351 }
352 
353 int hci_remote_esco_supported(hci_con_handle_t handle){
354     return 0;
355 }
356 
357 static void add_new_lines_to_hfp_command(uint8_t * data, int len){
358     if (len <= 0) return;
359     memset(&outgoing_rfcomm_payload, 0, 200);
360     int pos = 0;
361 
362     if (strncmp((char*)data, "AT", 2) == 0){
363         strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len);
364         pos += len;
365     } else {
366         outgoing_rfcomm_payload[pos++] = '\r';
367         outgoing_rfcomm_payload[pos++] = '\n';
368         strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len);
369         pos += len;
370     }
371     outgoing_rfcomm_payload[pos++] = '\r';
372     outgoing_rfcomm_payload[pos++] = '\n';
373     outgoing_rfcomm_payload[pos] = 0;
374     outgoing_rfcomm_payload_len = pos;
375 }
376 
377 void inject_hfp_command_to_hf(uint8_t * data, int len){
378     if (memcmp((char*)data, "AT", 2) == 0) return;
379     add_new_lines_to_hfp_command(data, len);
380     // printf("inject_hfp_command_to_hf to HF: ");
381     // print_without_newlines(outgoing_rfcomm_payload,outgoing_rfcomm_payload_len);
382     (*registered_rfcomm_packet_handler)(RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
383 
384 }
385 
386 void inject_hfp_command_to_ag(uint8_t * data, int len){
387     if (data[0] == '+') return;
388 
389     add_new_lines_to_hfp_command(data, len);
390 
391     // printf("mock: inject command to ag: ");
392     // print_without_newlines(data, len);
393 
394     (*registered_rfcomm_packet_handler)(RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
395 }
396 
397 
398 int hci_extended_sco_link_supported(void){
399     return 1;
400 }
401