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