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