xref: /btstack/test/hfp/mock.c (revision bc37f7b0d0a3eaa5763a873c5730bc14b849aaa0)
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 
55 #include "mock.h"
56 
57 static uint8_t sdp_rfcomm_channel_nr = 1;
58 const char sdp_rfcomm_service_name[] = "BTstackMock";
59 static uint16_t rfcomm_cid = 1;
60 static bd_addr_t dev_addr;
61 static uint16_t sco_handle = 10;
62 static uint8_t rfcomm_payload[1000];
63 static uint16_t rfcomm_payload_len = 0;
64 
65 static uint8_t outgoing_rfcomm_payload[1000];
66 static uint16_t outgoing_rfcomm_payload_len = 0;
67 
68 static uint8_t rfcomm_reserved_buffer[1000];
69 
70 hfp_connection_t * hfp_context;
71 
72 void (*registered_rfcomm_packet_handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
73 void (*registered_sdp_app_callback)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
74 
75 uint8_t * get_rfcomm_payload(void){
76 	return &rfcomm_payload[0];
77 }
78 
79 uint16_t get_rfcomm_payload_len(void){
80 	return rfcomm_payload_len;
81 }
82 
83 static int hfp_command_start_index = 0;
84 
85 
86 int has_more_hfp_commands(int start_command_offset, int end_command_offset){
87     int has_cmd = get_rfcomm_payload_len() - hfp_command_start_index >= 2 + start_command_offset + end_command_offset;
88     //printf("has more: payload len %d, start %d, has more %d\n", get_rfcomm_payload_len(), hfp_command_start_index, has_cmd);
89     return has_cmd;
90 }
91 
92 char * get_next_hfp_command(int start_command_offset, int end_command_offset){
93     //printf("get next: payload len %d, start %d\n", get_rfcomm_payload_len(), hfp_command_start_index);
94     char * data = (char *)(&get_rfcomm_payload()[hfp_command_start_index + start_command_offset]);
95     int data_len = get_rfcomm_payload_len() - hfp_command_start_index - start_command_offset;
96 
97     int i;
98 
99     for (i = 0; i < data_len; i++){
100         if ( *(data+i) == '\r' || *(data+i) == '\n' ) {
101             data[i]=0;
102             // update state
103             //printf("!!! command %s\n", data);
104             hfp_command_start_index = hfp_command_start_index + i + start_command_offset + end_command_offset;
105             return data;
106         }
107     }
108     printf("should not got here\n");
109     return NULL;
110 }
111 
112 void print_without_newlines(uint8_t *data, uint16_t len);
113 void print_without_newlines(uint8_t *data, uint16_t len){
114     int found_newline = 0;
115     int found_item = 0;
116 
117     for (int i=0; i<len; i++){
118         if (data[i] == '\r' || data[i] == '\n'){
119             if (!found_newline && found_item) printf("\n");
120             found_newline = 1;
121         } else {
122             printf("%c", data[i]);
123             found_newline = 0;
124             found_item = 1;
125         }
126     }
127     printf("\n");
128 }
129 
130 extern "C" void l2cap_init(void){}
131 extern "C" void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
132 }
133 
134 int  rfcomm_send(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){
135 
136     // printf("mock: rfcomm send: ");
137     print_without_newlines(data, len);
138 
139 	int start_command_offset = 2;
140     int end_command_offset = 2;
141 
142     if (strncmp((char*)data, "AT", 2) == 0){
143 		start_command_offset = 0;
144 	}
145 
146     if (has_more_hfp_commands(start_command_offset, end_command_offset)){
147         //printf("Buffer response: ");
148         strncpy((char*)&rfcomm_payload[rfcomm_payload_len], (char*)data, len);
149         rfcomm_payload_len += len;
150     } else {
151         hfp_command_start_index = 0;
152         //printf("Copy response: ");
153         strncpy((char*)&rfcomm_payload[0], (char*)data, len);
154         rfcomm_payload_len = len;
155     }
156 
157     // print_without_newlines(rfcomm_payload,rfcomm_payload_len);
158     return 0;
159 }
160 
161 void rfcomm_request_can_send_now_event(uint16_t rfcomm_cid){
162 
163     // printf("mock: rfcomm_request_can_send_now_event\n");
164 
165     uint8_t event[] = { RFCOMM_EVENT_CAN_SEND_NOW, 2, 0, 0};
166     little_endian_store_16(event, 2, rfcomm_cid);
167     registered_rfcomm_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event));
168 }
169 
170 int       rfcomm_reserve_packet_buffer(void){
171     // printf("mock: rfcomm_reserve_packet_buffer\n");
172     return 1;
173 };
174 void      rfcomm_release_packet_buffer(void){};
175 uint8_t * rfcomm_get_outgoing_buffer(void) {
176     return rfcomm_reserved_buffer;
177 }
178 uint16_t rfcomm_get_max_frame_size(uint16_t rfcomm_cid){
179     return sizeof(rfcomm_reserved_buffer);
180 }
181 int rfcomm_send_prepared(uint16_t rfcomm_cid, uint16_t len){
182     // printf("--- rfcomm_send_prepared with len %u ---\n", len);
183     return rfcomm_send(rfcomm_cid, rfcomm_reserved_buffer, len);
184 }
185 
186 static void hci_event_sco_complete(void){
187     uint8_t event[19];
188     uint8_t pos = 0;
189     event[pos++] = HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE;
190     event[pos++] = sizeof(event) - 2;
191 
192     event[pos++] = 0; //status
193     little_endian_store_16(event,  pos, sco_handle);   pos += 2; // sco handle
194     reverse_bd_addr(dev_addr, &event[pos]);    pos += 6;
195 
196     event[pos++] = 0; // link_type
197     event[pos++] = 0; // transmission_interval
198     event[pos++] = 0; // retransmission_interval
199 
200     little_endian_store_16(event,  pos, 0);   pos += 2; // rx_packet_length
201     little_endian_store_16(event,  pos, 0);   pos += 2; // tx_packet_length
202 
203     event[pos++] = 0; // air_mode
204     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
205 }
206 
207 int hci_send_cmd(const hci_cmd_t *cmd, ...){
208 	//printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
209     if (cmd->opcode == 0x428){
210         hci_event_sco_complete();
211     }
212 	return 0;
213 }
214 
215 int hci_can_send_command_packet_now(void){
216     return 1;
217 }
218 
219 static void sdp_query_complete_response(uint8_t status){
220     uint8_t event[3];
221     event[0] = SDP_EVENT_QUERY_COMPLETE;
222     event[1] = 1;
223     event[2] = status;
224     (*registered_sdp_app_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
225 }
226 
227 static void sdp_client_query_rfcomm_service_response(uint8_t status){
228     int sdp_service_name_len = strlen(sdp_rfcomm_service_name);
229     uint8_t event[3+SDP_SERVICE_NAME_LEN+1];
230     event[0] = SDP_EVENT_QUERY_RFCOMM_SERVICE;
231     event[1] = sdp_service_name_len + 1;
232     event[2] = sdp_rfcomm_channel_nr;
233     memcpy(&event[3], sdp_rfcomm_service_name, sdp_service_name_len);
234     event[3+sdp_service_name_len] = 0;
235     (*registered_sdp_app_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
236 }
237 
238 uint8_t sdp_client_query_rfcomm_channel_and_name_for_uuid(btstack_packet_handler_t callback, bd_addr_t remote, uint16_t uuid){
239 	// printf("sdp_client_query_rfcomm_channel_and_name_for_uuid %p\n", registered_sdp_app_callback);
240     registered_sdp_app_callback = callback;
241 	sdp_client_query_rfcomm_service_response(0);
242 	sdp_query_complete_response(0);
243     return 0;
244 }
245 
246 
247 uint8_t rfcomm_create_channel(btstack_packet_handler_t handler, bd_addr_t addr, uint8_t channel, uint16_t * out_cid){
248 
249     // printf("mock: rfcomm_create_channel addr %s\n", bd_addr_to_str(addr));
250 
251     registered_rfcomm_packet_handler = handler;
252 
253 	// RFCOMM_EVENT_CHANNEL_OPENED
254     uint8_t event[16];
255     uint8_t pos = 0;
256     event[pos++] = RFCOMM_EVENT_CHANNEL_OPENED;
257     event[pos++] = sizeof(event) - 2;
258     event[pos++] = 0;
259 
260     reverse_bd_addr(addr, &event[pos]);
261     memcpy(dev_addr, addr, 6);
262     pos += 6;
263 
264     little_endian_store_16(event,  pos, 1);   pos += 2;
265 	event[pos++] = 0;
266 
267 	little_endian_store_16(event, pos, rfcomm_cid); pos += 2;       // channel ID
268 	little_endian_store_16(event, pos, 200); pos += 2;   // max frame size
269     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, pos);
270 
271     if (out_cid){
272         *out_cid = rfcomm_cid;
273     }
274     return 0;
275 }
276 
277 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){
278     // printf("mock: rfcomm_can_send_packet_now\n");
279 	return 1;
280 }
281 
282 void rfcomm_disconnect(uint16_t rfcomm_cid){
283 	uint8_t event[4];
284 	event[0] = RFCOMM_EVENT_CHANNEL_CLOSED;
285     event[1] = sizeof(event) - 2;
286     little_endian_store_16(event, 2, rfcomm_cid);
287     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
288 }
289 
290 uint8_t rfcomm_register_service(btstack_packet_handler_t handler, uint8_t channel, uint16_t max_frame_size){
291 	// printf("rfcomm_register_service\n");
292     registered_rfcomm_packet_handler = handler;
293     return 0;
294 }
295 
296 
297 void sdp_client_query_rfcomm_channel_and_name_for_search_pattern(bd_addr_t remote, uint8_t * des_serviceSearchPattern){
298 	// printf("sdp_client_query_rfcomm_channel_and_name_for_search_pattern\n");
299 }
300 
301 
302 void rfcomm_accept_connection(uint16_t rfcomm_cid){
303 	// printf("rfcomm_accept_connection \n");
304 }
305 
306 void btstack_run_loop_add_timer(btstack_timer_source_t *timer){
307 }
308 
309 int  btstack_run_loop_remove_timer(btstack_timer_source_t *timer){
310     return 0;
311 }
312 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){
313 }
314 
315 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
316 }
317 
318 
319 void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
320     uint8_t event[6];
321     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
322     event[1] = sizeof(event) - 2;
323     event[2] = 0; // status = OK
324     little_endian_store_16(event, 3, handle);
325     event[5] = reason;
326     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
327 }
328 
329 uint8_t gap_disconnect(hci_con_handle_t handle){
330     hci_emit_disconnection_complete(handle, 0);
331     return 0;
332 }
333 
334 uint16_t hci_get_sco_voice_setting(void){
335     return 0x40;
336 }
337 
338 int hci_remote_esco_supported(hci_con_handle_t handle){
339     return 0;
340 }
341 
342 static void add_new_lines_to_hfp_command(uint8_t * data, int len){
343     if (len <= 0) return;
344     memset(&outgoing_rfcomm_payload, 0, 200);
345     int pos = 0;
346 
347     if (strncmp((char*)data, "AT", 2) == 0){
348         strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len);
349         pos += len;
350     } else {
351         outgoing_rfcomm_payload[pos++] = '\r';
352         outgoing_rfcomm_payload[pos++] = '\n';
353         strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len);
354         pos += len;
355     }
356     outgoing_rfcomm_payload[pos++] = '\r';
357     outgoing_rfcomm_payload[pos++] = '\n';
358     outgoing_rfcomm_payload[pos] = 0;
359     outgoing_rfcomm_payload_len = pos;
360 }
361 
362 void inject_hfp_command_to_hf(uint8_t * data, int len){
363     if (memcmp((char*)data, "AT", 2) == 0) return;
364     add_new_lines_to_hfp_command(data, len);
365     // printf("inject_hfp_command_to_hf to HF: ");
366     // print_without_newlines(outgoing_rfcomm_payload,outgoing_rfcomm_payload_len);
367     (*registered_rfcomm_packet_handler)(RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
368 
369 }
370 
371 void inject_hfp_command_to_ag(uint8_t * data, int len){
372     if (data[0] == '+') return;
373 
374     add_new_lines_to_hfp_command(data, len);
375 
376     // printf("mock: inject command to ag: ");
377     // print_without_newlines(data, len);
378 
379     (*registered_rfcomm_packet_handler)(RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
380 }
381 
382 
383 int hci_extended_sco_link_supported(void){
384     return 1;
385 }
386