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
get_rfcomm_payload(void)80 uint8_t * get_rfcomm_payload(void){
81 return &rfcomm_payload[0];
82 }
83
get_rfcomm_payload_len(void)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
has_more_hfp_commands(int start_command_offset,int end_command_offset)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
get_next_hfp_command(int start_command_offset,int end_command_offset)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);
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
l2cap_init(void)135 void l2cap_init(void){}
hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler)136 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
137 registered_hci_packet_handler = callback_handler->callback;
138 }
139
rfcomm_send(uint16_t rfcomm_cid,uint8_t * data,uint16_t len)140 uint8_t 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 ERROR_CODE_SUCCESS;
165 }
166
rfcomm_request_can_send_now_event(uint16_t rfcomm_cid)167 uint8_t 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 return ERROR_CODE_SUCCESS;
175 }
176
rfcomm_reserve_packet_buffer(void)177 void rfcomm_reserve_packet_buffer(void){
178 // printf("mock: rfcomm_reserve_packet_buffer\n");
179 };
rfcomm_release_packet_buffer(void)180 void rfcomm_release_packet_buffer(void){};
rfcomm_get_outgoing_buffer(void)181 uint8_t * rfcomm_get_outgoing_buffer(void) {
182 return rfcomm_reserved_buffer;
183 }
rfcomm_get_max_frame_size(uint16_t rfcomm_cid)184 uint16_t rfcomm_get_max_frame_size(uint16_t rfcomm_cid){
185 return sizeof(rfcomm_reserved_buffer);
186 }
rfcomm_send_prepared(uint16_t rfcomm_cid,uint16_t len)187 uint8_t 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
hci_event_sco_complete(void)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
hci_send_cmd(const hci_cmd_t * cmd,...)213 uint8_t 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 ERROR_CODE_SUCCESS;
219 }
220
hci_can_send_command_packet_now(void)221 bool hci_can_send_command_packet_now(void){
222 return true;
223 }
224
sdp_query_complete_response(uint8_t status)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
sdp_client_query_rfcomm_service_response(uint8_t status)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
sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(btstack_packet_handler_t callback,bd_addr_t remote,uint16_t uuid)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
sdp_client_register_query_callback(btstack_context_callback_registration_t * callback_registration)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
rfcomm_create_channel(btstack_packet_handler_t handler,bd_addr_t addr,uint8_t channel,uint16_t * out_cid)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 ERROR_CODE_SUCCESS;
285 }
286
rfcomm_can_send_packet_now(uint16_t rfcomm_cid)287 bool rfcomm_can_send_packet_now(uint16_t rfcomm_cid){
288 // printf("mock: rfcomm_can_send_packet_now\n");
289 return true;
290 }
291
rfcomm_disconnect(uint16_t rfcomm_cid)292 uint8_t 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 return ERROR_CODE_SUCCESS;
299 }
300
rfcomm_register_service(btstack_packet_handler_t handler,uint8_t channel,uint16_t max_frame_size)301 uint8_t rfcomm_register_service(btstack_packet_handler_t handler, uint8_t channel, uint16_t max_frame_size){
302 // printf("rfcomm_register_service\n");
303 registered_rfcomm_packet_handler = handler;
304 return ERROR_CODE_SUCCESS;
305 }
306
307
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 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){
309 // printf("sdp_client_query_rfcomm_channel_and_name_for_search_pattern\n");
310 return 0;
311 }
312
313
rfcomm_accept_connection(uint16_t rfcomm_cid)314 uint8_t rfcomm_accept_connection(uint16_t rfcomm_cid){
315 // printf("rfcomm_accept_connection \n");
316 return ERROR_CODE_SUCCESS;
317 }
318
rfcomm_decline_connection(uint16_t rfcomm_cid)319 uint8_t rfcomm_decline_connection(uint16_t rfcomm_cid){
320 // printf("rfcomm_accept_connection \n");
321 return ERROR_CODE_SUCCESS;
322 }
323
btstack_run_loop_add_timer(btstack_timer_source_t * timer)324 void btstack_run_loop_add_timer(btstack_timer_source_t *timer){
325 }
326
btstack_run_loop_remove_timer(btstack_timer_source_t * timer)327 int btstack_run_loop_remove_timer(btstack_timer_source_t *timer){
328 return 0;
329 }
btstack_run_loop_set_timer_handler(btstack_timer_source_t * ts,void (* process)(btstack_timer_source_t * _ts))330 void btstack_run_loop_set_timer_handler(btstack_timer_source_t *ts, void (*process)(btstack_timer_source_t *_ts)){
331 }
332
btstack_run_loop_set_timer(btstack_timer_source_t * a,uint32_t timeout_in_ms)333 void btstack_run_loop_set_timer(btstack_timer_source_t *a, uint32_t timeout_in_ms){
334 }
335
336
hci_emit_disconnection_complete(uint16_t handle,uint8_t reason)337 static void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){
338 uint8_t event[6];
339 event[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
340 event[1] = sizeof(event) - 2;
341 event[2] = 0; // status = OK
342 little_endian_store_16(event, 3, handle);
343 event[5] = reason;
344 (*registered_hci_packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
345 }
346
gap_disconnect(hci_con_handle_t handle)347 uint8_t gap_disconnect(hci_con_handle_t handle){
348 hci_emit_disconnection_complete(handle, 0);
349 return 0;
350 }
351
hci_get_sco_voice_setting(void)352 uint16_t hci_get_sco_voice_setting(void){
353 return 0x40;
354 }
355
hci_remote_esco_supported(hci_con_handle_t handle)356 bool hci_remote_esco_supported(hci_con_handle_t handle){
357 return false;
358 }
359
add_new_lines_to_hfp_command(uint8_t * data,int len)360 static void add_new_lines_to_hfp_command(uint8_t * data, int len){
361 if (len <= 0) return;
362 memset(&outgoing_rfcomm_payload, 0, 200);
363 int pos = 0;
364
365 if (strncmp((char*)data, "AT", 2) == 0){
366 strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len);
367 pos += len;
368 } else {
369 outgoing_rfcomm_payload[pos++] = '\r';
370 outgoing_rfcomm_payload[pos++] = '\n';
371 strncpy((char*)&outgoing_rfcomm_payload[pos], (char*)data, len);
372 pos += len;
373 }
374 outgoing_rfcomm_payload[pos++] = '\r';
375 outgoing_rfcomm_payload[pos++] = '\n';
376 outgoing_rfcomm_payload[pos] = 0;
377 outgoing_rfcomm_payload_len = pos;
378 }
379
inject_hfp_command_to_hf(uint8_t * data,int len)380 void inject_hfp_command_to_hf(uint8_t * data, int len){
381 if (memcmp((char*)data, "AT", 2) == 0) return;
382 add_new_lines_to_hfp_command(data, len);
383 // printf("inject_hfp_command_to_hf to HF: ");
384 // print_without_newlines(outgoing_rfcomm_payload,outgoing_rfcomm_payload_len);
385 (*registered_rfcomm_packet_handler)(RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
386
387 }
388
inject_hfp_command_to_ag(uint8_t * data,int len)389 void inject_hfp_command_to_ag(uint8_t * data, int len){
390 if (data[0] == '+') return;
391
392 add_new_lines_to_hfp_command(data, len);
393
394 // printf("mock: inject command to ag: ");
395 // print_without_newlines(data, len);
396
397 (*registered_rfcomm_packet_handler)(RFCOMM_DATA_PACKET, rfcomm_cid, (uint8_t *) &outgoing_rfcomm_payload[0], outgoing_rfcomm_payload_len);
398 }
399
400
hci_extended_sco_link_supported(void)401 bool hci_extended_sco_link_supported(void){
402 return true;
403 }
404
gap_secure_connection(hci_con_handle_t con_handle)405 bool gap_secure_connection(hci_con_handle_t con_handle){
406 UNUSED(con_handle);
407 return true;
408 }
409
hci_remote_sco_packet_types(hci_con_handle_t con_handle)410 uint16_t hci_remote_sco_packet_types(hci_con_handle_t con_handle){
411 UNUSED(con_handle);
412 return SCO_PACKET_TYPES_ALL;
413 }
hci_usable_sco_packet_types(void)414 uint16_t hci_usable_sco_packet_types(void){
415 return SCO_PACKET_TYPES_ALL;
416 }
417