xref: /btstack/test/hfp/mock.c (revision f5054c0028135915921b5f31909b986101cbedd2)
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_query_rfcomm.h"
52 #include "classic/rfcomm.h"
53 #include "classic/hfp_hf.h"
54 
55 #include "mock.h"
56 
57 static void *registered_sdp_app_context;
58 static uint8_t sdp_rfcomm_channel_nr = 1;
59 const char sdp_rfcomm_service_name[] = "BTstackMock";
60 static uint16_t rfcomm_cid = 1;
61 static bd_addr_t dev_addr;
62 static uint16_t sco_handle = 10;
63 static uint8_t rfcomm_payload[200];
64 static uint16_t rfcomm_payload_len = 0;
65 
66 static uint8_t outgoing_rfcomm_payload[200];
67 static uint16_t outgoing_rfcomm_payload_len = 0;
68 
69 void * active_connection;
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)(sdp_query_event_t * event, void * context);
74 
75 uint8_t * get_rfcomm_payload(){
76 	return &rfcomm_payload[0];
77 }
78 
79 uint16_t get_rfcomm_payload_len(){
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 static void print_without_newlines(uint8_t *data, uint16_t len){
113     int found_newline = 0;
114     int found_item = 0;
115 
116     for (int i=0; i<len; i++){
117         if (data[i] == '\r' || data[i] == '\n'){
118             if (!found_newline && found_item) printf("\n");
119             found_newline = 1;
120         } else {
121             printf("%c", data[i]);
122             found_newline = 0;
123             found_item = 1;
124         }
125     }
126     printf("\n");
127 }
128 
129 extern "C" void l2cap_init(void){}
130 
131 extern "C" void l2cap_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
132 }
133 
134 
135 int  rfcomm_send_internal(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){
136 	int start_command_offset = 2;
137     int end_command_offset = 2;
138 
139     if (strncmp((char*)data, "AT", 2) == 0){
140 		start_command_offset = 0;
141 	}
142 
143     if (has_more_hfp_commands(start_command_offset, end_command_offset)){
144         //printf("Buffer response: ");
145         strncpy((char*)&rfcomm_payload[rfcomm_payload_len], (char*)data, len);
146         rfcomm_payload_len += len;
147     } else {
148         hfp_command_start_index = 0;
149         //printf("Copy response: ");
150         strncpy((char*)&rfcomm_payload[0], (char*)data, len);
151         rfcomm_payload_len = len;
152     }
153 
154     //print_without_newlines(rfcomm_payload,rfcomm_payload_len);
155     return 0;
156 }
157 
158 static void hci_event_sco_complete(){
159     uint8_t event[19];
160     uint8_t pos = 0;
161     event[pos++] = HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE;
162     event[pos++] = sizeof(event) - 2;
163 
164     event[pos++] = 0; //status
165     bt_store_16(event,  pos, sco_handle);   pos += 2; // sco handle
166     bt_flip_addr(&event[pos], dev_addr);    pos += 6;
167 
168     event[pos++] = 0; // link_type
169     event[pos++] = 0; // transmission_interval
170     event[pos++] = 0; // retransmission_interval
171 
172     bt_store_16(event,  pos, 0);   pos += 2; // rx_packet_length
173     bt_store_16(event,  pos, 0);   pos += 2; // tx_packet_length
174 
175     event[pos++] = 0; // air_mode
176     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
177 }
178 
179 int hci_send_cmd(const hci_cmd_t *cmd, ...){
180 	//printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
181     if (cmd->opcode == 0x428){
182         hci_event_sco_complete();
183     }
184 	return 0;
185 }
186 
187 
188 void sdp_query_rfcomm_register_callback(void(*sdp_app_callback)(sdp_query_event_t * event, void * context), void * context){
189 	registered_sdp_app_callback = sdp_app_callback;
190 	registered_sdp_app_context = context;
191 }
192 
193 static void sdp_query_complete_response(uint8_t status){
194     sdp_query_complete_event_t complete_event = {
195         SDP_QUERY_COMPLETE,
196         status
197     };
198     (*registered_sdp_app_callback)((sdp_query_event_t*)&complete_event, registered_sdp_app_context);
199 }
200 
201 static void sdp_query_rfcomm_service_response(uint8_t status){
202     sdp_query_rfcomm_service_event_t service_event = {
203         SDP_QUERY_RFCOMM_SERVICE,
204         sdp_rfcomm_channel_nr,
205         (uint8_t *)sdp_rfcomm_service_name
206     };
207     (*registered_sdp_app_callback)((sdp_query_event_t*)&service_event, registered_sdp_app_context);
208 }
209 
210 void sdp_query_rfcomm_channel_and_name_for_uuid(bd_addr_t remote, uint16_t uuid){
211 	// printf("sdp_query_rfcomm_channel_and_name_for_uuid %p\n", registered_sdp_app_callback);
212 	sdp_query_rfcomm_service_response(0);
213 	sdp_query_complete_response(0);
214 }
215 
216 
217 uint8_t rfcomm_create_channel(bd_addr_t addr, uint8_t channel, uint16_t * out_cid){
218 	// RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE
219     uint8_t event[16];
220     uint8_t pos = 0;
221     event[pos++] = RFCOMM_EVENT_OPEN_CHANNEL_COMPLETE;
222     event[pos++] = sizeof(event) - 2;
223     event[pos++] = 0;
224 
225     bt_flip_addr(&event[pos], addr);
226     memcpy(dev_addr, addr, 6);
227     pos += 6;
228 
229     bt_store_16(event,  pos, 1);   pos += 2;
230 	event[pos++] = 0;
231 
232 	bt_store_16(event, pos, rfcomm_cid); pos += 2;       // channel ID
233 	bt_store_16(event, pos, 200); pos += 2;   // max frame size
234     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, pos);
235 
236     if (out_cid){
237         *out_cid = rfcomm_cid;
238     }
239     return 0;
240 }
241 
242 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){
243 	return 1;
244 }
245 
246 void rfcomm_disconnect_internal(uint16_t rfcomm_cid){
247 	uint8_t event[4];
248 	event[0] = RFCOMM_EVENT_CHANNEL_CLOSED;
249     event[1] = sizeof(event) - 2;
250     bt_store_16(event, 2, rfcomm_cid);
251     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
252 }
253 
254 void rfcomm_register_packet_handler(void (*handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size)){
255 	registered_rfcomm_packet_handler = handler;
256 }
257 
258 uint8_t rfcomm_register_service(uint8_t channel, uint16_t max_frame_size){
259 	printf("rfcomm_register_service\n");
260     return 0;
261 }
262 
263 
264 void sdp_query_rfcomm_channel_and_name_for_search_pattern(bd_addr_t remote, uint8_t * des_serviceSearchPattern){
265 	printf("sdp_query_rfcomm_channel_and_name_for_search_pattern\n");
266 }
267 
268 
269 void rfcomm_accept_connection_internal(uint16_t rfcomm_cid){
270 	printf("rfcomm_accept_connection_internal \n");
271 }
272 
273 void run_loop_add_timer(timer_source_t *timer){
274 }
275 
276 int  run_loop_remove_timer(timer_source_t *timer){
277     return 0;
278 }
279 void run_loop_set_timer_handler(timer_source_t *ts, void (*process)(timer_source_t *_ts)){
280 }
281 
282 void run_loop_set_timer(timer_source_t *a, uint32_t timeout_in_ms){
283 }
284 
285 
286 void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
287     uint8_t event[6];
288     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
289     event[1] = sizeof(event) - 2;
290     event[2] = 0; // status = OK
291     bt_store_16(event, 3, handle);
292     event[5] = reason;
293     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
294 }
295 
296 uint8_t gap_disconnect(hci_con_handle_t handle){
297     hci_emit_disconnection_complete(handle, 0);
298     return 0;
299 }
300 
301 uint16_t hci_get_sco_voice_setting(){
302     return 0x40;
303 }
304 
305 int hci_remote_eSCO_supported(hci_con_handle_t handle){
306     return 0;
307 }
308 
309 static void add_new_lines_to_hfp_command(uint8_t * data, int len){
310     if (len <= 0) return;
311     memset(&outgoing_rfcomm_payload, 0, 200);
312     int pos = 0;
313 
314     if (strncmp((char*)data, "AT", 2) == 0){
315         strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len);
316         pos += len;
317     } else {
318         outgoing_rfcomm_payload[pos++] = '\r';
319         outgoing_rfcomm_payload[pos++] = '\n';
320         strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len);
321         pos += len;
322     }
323     outgoing_rfcomm_payload[pos++] = '\r';
324     outgoing_rfcomm_payload[pos++] = '\n';
325     outgoing_rfcomm_payload[pos] = 0;
326     outgoing_rfcomm_payload_len = pos;
327 }
328 
329 void inject_hfp_command_to_hf(uint8_t * data, int len){
330     if (memcmp((char*)data, "AT", 2) == 0) return;
331     add_new_lines_to_hfp_command(data, len);
332     // printf("inject_hfp_command_to_hf to HF: ");
333     // print_without_newlines(outgoing_rfcomm_payload,outgoing_rfcomm_payload_len);
334     (*registered_rfcomm_packet_handler)(active_connection, RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
335 
336 }
337 
338 void inject_hfp_command_to_ag(uint8_t * data, int len){
339     if (data[0] == '+') return;
340 
341     add_new_lines_to_hfp_command(data, len);
342     (*registered_rfcomm_packet_handler)(active_connection, RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
343 }
344 
345 
346 
347