xref: /btstack/test/hfp/mock.c (revision c9eeefbc1a8d32c2c32450cacfb8ade67133fea8)
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[200];
63 static uint16_t rfcomm_payload_len = 0;
64 
65 static uint8_t outgoing_rfcomm_payload[200];
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 	int start_command_offset = 2;
136     int end_command_offset = 2;
137 
138     if (strncmp((char*)data, "AT", 2) == 0){
139 		start_command_offset = 0;
140 	}
141 
142     if (has_more_hfp_commands(start_command_offset, end_command_offset)){
143         //printf("Buffer response: ");
144         strncpy((char*)&rfcomm_payload[rfcomm_payload_len], (char*)data, len);
145         rfcomm_payload_len += len;
146     } else {
147         hfp_command_start_index = 0;
148         //printf("Copy response: ");
149         strncpy((char*)&rfcomm_payload[0], (char*)data, len);
150         rfcomm_payload_len = len;
151     }
152 
153     //print_without_newlines(rfcomm_payload,rfcomm_payload_len);
154     return 0;
155 }
156 
157 int       rfcomm_reserve_packet_buffer(void){
158     return 1;
159 };
160 void      rfcomm_release_packet_buffer(void){};
161 uint8_t * rfcomm_get_outgoing_buffer(void) {
162     return rfcomm_reserved_buffer;
163 }
164 uint16_t rfcomm_get_max_frame_size(uint16_t rfcomm_cid){
165     return sizeof(rfcomm_reserved_buffer);
166 }
167 int rfcomm_send_prepared(uint16_t rfcomm_cid, uint16_t len){
168     printf("--- rfcomm_send_prepared with len %u ---\n", len);
169     return rfcomm_send(rfcomm_cid, rfcomm_reserved_buffer, len);
170 }
171 
172 static void hci_event_sco_complete(void){
173     uint8_t event[19];
174     uint8_t pos = 0;
175     event[pos++] = HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE;
176     event[pos++] = sizeof(event) - 2;
177 
178     event[pos++] = 0; //status
179     little_endian_store_16(event,  pos, sco_handle);   pos += 2; // sco handle
180     reverse_bd_addr(dev_addr, &event[pos]);    pos += 6;
181 
182     event[pos++] = 0; // link_type
183     event[pos++] = 0; // transmission_interval
184     event[pos++] = 0; // retransmission_interval
185 
186     little_endian_store_16(event,  pos, 0);   pos += 2; // rx_packet_length
187     little_endian_store_16(event,  pos, 0);   pos += 2; // tx_packet_length
188 
189     event[pos++] = 0; // air_mode
190     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
191 }
192 
193 int hci_send_cmd(const hci_cmd_t *cmd, ...){
194 	//printf("hci_send_cmd opcode 0x%02x\n", cmd->opcode);
195     if (cmd->opcode == 0x428){
196         hci_event_sco_complete();
197     }
198 	return 0;
199 }
200 
201 static void sdp_query_complete_response(uint8_t status){
202     uint8_t event[3];
203     event[0] = SDP_EVENT_QUERY_COMPLETE;
204     event[1] = 1;
205     event[2] = status;
206     (*registered_sdp_app_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
207 }
208 
209 static void sdp_client_query_rfcomm_service_response(uint8_t status){
210     int sdp_service_name_len = strlen(sdp_rfcomm_service_name);
211     uint8_t event[3+SDP_SERVICE_NAME_LEN+1];
212     event[0] = SDP_EVENT_QUERY_RFCOMM_SERVICE;
213     event[1] = sdp_service_name_len + 1;
214     event[2] = sdp_rfcomm_channel_nr;
215     memcpy(&event[3], sdp_rfcomm_service_name, sdp_service_name_len);
216     event[3+sdp_service_name_len] = 0;
217     (*registered_sdp_app_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
218 }
219 
220 void sdp_client_query_rfcomm_channel_and_name_for_uuid(btstack_packet_handler_t callback, bd_addr_t remote, uint16_t uuid){
221 	// printf("sdp_client_query_rfcomm_channel_and_name_for_uuid %p\n", registered_sdp_app_callback);
222     registered_sdp_app_callback = callback;
223 	sdp_client_query_rfcomm_service_response(0);
224 	sdp_query_complete_response(0);
225 }
226 
227 
228 uint8_t rfcomm_create_channel(btstack_packet_handler_t handler, bd_addr_t addr, uint8_t channel, uint16_t * out_cid){
229 	// RFCOMM_EVENT_CHANNEL_OPENED
230     uint8_t event[16];
231     uint8_t pos = 0;
232     event[pos++] = RFCOMM_EVENT_CHANNEL_OPENED;
233     event[pos++] = sizeof(event) - 2;
234     event[pos++] = 0;
235 
236     reverse_bd_addr(addr, &event[pos]);
237     memcpy(dev_addr, addr, 6);
238     pos += 6;
239 
240     little_endian_store_16(event,  pos, 1);   pos += 2;
241 	event[pos++] = 0;
242 
243 	little_endian_store_16(event, pos, rfcomm_cid); pos += 2;       // channel ID
244 	little_endian_store_16(event, pos, 200); pos += 2;   // max frame size
245     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, pos);
246 
247     if (out_cid){
248         *out_cid = rfcomm_cid;
249     }
250     return 0;
251 }
252 
253 int rfcomm_can_send_packet_now(uint16_t rfcomm_cid){
254 	return 1;
255 }
256 
257 void rfcomm_disconnect(uint16_t rfcomm_cid){
258 	uint8_t event[4];
259 	event[0] = RFCOMM_EVENT_CHANNEL_CLOSED;
260     event[1] = sizeof(event) - 2;
261     little_endian_store_16(event, 2, rfcomm_cid);
262     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t *) event, sizeof(event));
263 }
264 
265 uint8_t rfcomm_register_service(btstack_packet_handler_t handler, uint8_t channel, uint16_t max_frame_size){
266 	printf("rfcomm_register_service\n");
267     registered_rfcomm_packet_handler = handler;
268     return 0;
269 }
270 
271 
272 void sdp_client_query_rfcomm_channel_and_name_for_search_pattern(bd_addr_t remote, uint8_t * des_serviceSearchPattern){
273 	printf("sdp_client_query_rfcomm_channel_and_name_for_search_pattern\n");
274 }
275 
276 
277 void rfcomm_accept_connection(uint16_t rfcomm_cid){
278 	printf("rfcomm_accept_connection \n");
279 }
280 
281 void btstack_run_loop_add_timer(btstack_timer_source_t *timer){
282 }
283 
284 int  btstack_run_loop_remove_timer(btstack_timer_source_t *timer){
285     return 0;
286 }
287 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){
288 }
289 
290 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
291 }
292 
293 
294 void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
295     uint8_t event[6];
296     event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
297     event[1] = sizeof(event) - 2;
298     event[2] = 0; // status = OK
299     little_endian_store_16(event, 3, handle);
300     event[5] = reason;
301     (*registered_rfcomm_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
302 }
303 
304 uint8_t gap_disconnect(hci_con_handle_t handle){
305     hci_emit_disconnection_complete(handle, 0);
306     return 0;
307 }
308 
309 uint16_t hci_get_sco_voice_setting(void){
310     return 0x40;
311 }
312 
313 int hci_remote_esco_supported(hci_con_handle_t handle){
314     return 0;
315 }
316 
317 static void add_new_lines_to_hfp_command(uint8_t * data, int len){
318     if (len <= 0) return;
319     memset(&outgoing_rfcomm_payload, 0, 200);
320     int pos = 0;
321 
322     if (strncmp((char*)data, "AT", 2) == 0){
323         strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len);
324         pos += len;
325     } else {
326         outgoing_rfcomm_payload[pos++] = '\r';
327         outgoing_rfcomm_payload[pos++] = '\n';
328         strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len);
329         pos += len;
330     }
331     outgoing_rfcomm_payload[pos++] = '\r';
332     outgoing_rfcomm_payload[pos++] = '\n';
333     outgoing_rfcomm_payload[pos] = 0;
334     outgoing_rfcomm_payload_len = pos;
335 }
336 
337 void inject_hfp_command_to_hf(uint8_t * data, int len){
338     if (memcmp((char*)data, "AT", 2) == 0) return;
339     add_new_lines_to_hfp_command(data, len);
340     // printf("inject_hfp_command_to_hf to HF: ");
341     // print_without_newlines(outgoing_rfcomm_payload,outgoing_rfcomm_payload_len);
342     (*registered_rfcomm_packet_handler)(RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
343 
344 }
345 
346 void inject_hfp_command_to_ag(uint8_t * data, int len){
347     if (data[0] == '+') return;
348 
349     add_new_lines_to_hfp_command(data, len);
350     (*registered_rfcomm_packet_handler)(RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
351 }
352 
353 
354 
355