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 BLUEKITCHEN 24 * GMBH 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 #define BTSTACK_FILE__ "hfp.c" 39 40 41 #include "btstack_config.h" 42 43 #include <stdint.h> 44 #include <stdio.h> 45 #include <string.h> 46 #include <inttypes.h> 47 48 #include "bluetooth_sdp.h" 49 #include "btstack_debug.h" 50 #include "btstack_event.h" 51 #include "btstack_memory.h" 52 #include "btstack_run_loop.h" 53 #include "classic/sdp_client_rfcomm.h" 54 #include "classic/sdp_server.h" 55 #include "classic/sdp_util.h" 56 #include "classic/sdp_client.h" 57 #include "hci.h" 58 #include "hci_cmd.h" 59 #include "hci_dump.h" 60 61 #if defined(ENABLE_CC256X_ASSISTED_HFP) && !defined(ENABLE_SCO_OVER_PCM) 62 #error "Assisted HFP is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM" 63 #endif 64 65 #if defined(ENABLE_BCM_PCM_WBS) && !defined(ENABLE_SCO_OVER_PCM) 66 #error "WBS for PCM is only possible over PCM/I2S. Please add define: ENABLE_SCO_OVER_PCM" 67 #endif 68 69 #define HFP_HF_FEATURES_SIZE 10 70 #define HFP_AG_FEATURES_SIZE 12 71 72 typedef struct { 73 hfp_role_t local_role; 74 bd_addr_t remote_address; 75 } hfp_sdp_query_context_t; 76 77 // globals 78 79 static btstack_linked_list_t hfp_connections ; 80 81 static btstack_packet_handler_t hfp_hf_callback; 82 static btstack_packet_handler_t hfp_ag_callback; 83 84 static btstack_packet_handler_t hfp_hf_rfcomm_packet_handler; 85 static btstack_packet_handler_t hfp_ag_rfcomm_packet_handler; 86 87 static uint16_t hfp_allowed_sco_packet_types; 88 static hfp_connection_t * hfp_sco_establishment_active; 89 90 static hfp_sdp_query_context_t hfp_sdp_query_context; 91 static btstack_context_callback_registration_t hfp_sdp_query_request; 92 93 94 95 96 97 98 // prototypes 99 static hfp_link_settings_t hfp_next_link_setting_for_connection(hfp_link_settings_t current_setting, hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported); 100 static void parse_sequence(hfp_connection_t * context); 101 102 static const char * hfp_hf_features[] = { 103 "EC and/or NR function", 104 "Three-way calling", 105 "CLI presentation capability", 106 "Voice recognition activation", 107 "Remote volume control", 108 109 "Enhanced call status", 110 "Enhanced call control", 111 112 "Codec negotiation", 113 114 "HF Indicators", 115 "eSCO S4 (and T2) Settings Supported", 116 "Reserved for future definition" 117 }; 118 119 static const char * hfp_ag_features[] = { 120 "Three-way calling", 121 "EC and/or NR function", 122 "Voice recognition function", 123 "In-band ring tone capability", 124 "Attach a number to a voice tag", 125 "Ability to reject a call", 126 "Enhanced call status", 127 "Enhanced call control", 128 "Extended Error Result Codes", 129 "Codec negotiation", 130 "HF Indicators", 131 "eSCO S4 (and T2) Settings Supported", 132 "Reserved for future definition" 133 }; 134 135 static const char * hfp_enhanced_call_dir[] = { 136 "outgoing", 137 "incoming" 138 }; 139 140 static const char * hfp_enhanced_call_status[] = { 141 "active", 142 "held", 143 "outgoing dialing", 144 "outgoing alerting", 145 "incoming", 146 "incoming waiting", 147 "call held by response and hold" 148 }; 149 150 static const char * hfp_enhanced_call_mode[] = { 151 "voice", 152 "data", 153 "fax" 154 }; 155 156 static const char * hfp_enhanced_call_mpty[] = { 157 "not a conference call", 158 "conference call" 159 }; 160 161 const char * hfp_enhanced_call_dir2str(uint16_t index){ 162 if (index <= HFP_ENHANCED_CALL_DIR_INCOMING) return hfp_enhanced_call_dir[index]; 163 return "not defined"; 164 } 165 166 const char * hfp_enhanced_call_status2str(uint16_t index){ 167 if (index <= HFP_ENHANCED_CALL_STATUS_CALL_HELD_BY_RESPONSE_AND_HOLD) return hfp_enhanced_call_status[index]; 168 return "not defined"; 169 } 170 171 const char * hfp_enhanced_call_mode2str(uint16_t index){ 172 if (index <= HFP_ENHANCED_CALL_MODE_FAX) return hfp_enhanced_call_mode[index]; 173 return "not defined"; 174 } 175 176 const char * hfp_enhanced_call_mpty2str(uint16_t index){ 177 if (index <= HFP_ENHANCED_CALL_MPTY_CONFERENCE_CALL) return hfp_enhanced_call_mpty[index]; 178 return "not defined"; 179 } 180 181 static uint16_t hfp_parse_indicator_index(hfp_connection_t * hfp_connection){ 182 uint16_t index = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 183 184 if (index > HFP_MAX_NUM_INDICATORS){ 185 log_info("ignoring invalid indicator index bigger then HFP_MAX_NUM_INDICATORS"); 186 return HFP_MAX_NUM_INDICATORS - 1; 187 } 188 189 // indicator index enumeration starts with 1, we substract 1 to store in array with starting index 0 190 if (index > 0){ 191 index -= 1; 192 } else { 193 log_info("ignoring invalid indicator index 0"); 194 return 0; 195 } 196 return index; 197 } 198 199 static void hfp_next_indicators_index(hfp_connection_t * hfp_connection){ 200 if (hfp_connection->parser_item_index < HFP_MAX_NUM_INDICATORS - 1){ 201 hfp_connection->parser_item_index++; 202 } else { 203 log_info("Ignoring additional indicator"); 204 } 205 } 206 207 static void hfp_next_codec_index(hfp_connection_t * hfp_connection){ 208 if (hfp_connection->parser_item_index < HFP_MAX_NUM_CODECS - 1){ 209 hfp_connection->parser_item_index++; 210 } else { 211 log_info("Ignoring additional codec index"); 212 } 213 } 214 215 static void hfp_next_remote_call_services_index(hfp_connection_t * hfp_connection){ 216 if (hfp_connection->remote_call_services_index < HFP_MAX_NUM_CALL_SERVICES - 1){ 217 hfp_connection->remote_call_services_index++; 218 } else { 219 log_info("Ignoring additional remote_call_services"); 220 } 221 } 222 223 const char * hfp_hf_feature(int index){ 224 if (index > HFP_HF_FEATURES_SIZE){ 225 return hfp_hf_features[HFP_HF_FEATURES_SIZE]; 226 } 227 return hfp_hf_features[index]; 228 } 229 230 const char * hfp_ag_feature(int index){ 231 if (index > HFP_AG_FEATURES_SIZE){ 232 return hfp_ag_features[HFP_AG_FEATURES_SIZE]; 233 } 234 return hfp_ag_features[index]; 235 } 236 237 int send_str_over_rfcomm(uint16_t cid, char * command){ 238 if (!rfcomm_can_send_packet_now(cid)) return 1; 239 log_info("HFP_TX %s", command); 240 int err = rfcomm_send(cid, (uint8_t*) command, strlen(command)); 241 if (err){ 242 log_error("rfcomm_send -> error 0x%02x \n", err); 243 } 244 #ifdef ENABLE_HFP_AT_MESSAGES 245 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(cid); 246 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, command); 247 #endif 248 return 1; 249 } 250 251 int hfp_supports_codec(uint8_t codec, int codecs_nr, uint8_t * codecs){ 252 253 // mSBC requires support for eSCO connections 254 if ((codec == HFP_CODEC_MSBC) && !hci_extended_sco_link_supported()) return 0; 255 256 int i; 257 for (i = 0; i < codecs_nr; i++){ 258 if (codecs[i] != codec) continue; 259 return 1; 260 } 261 return 0; 262 } 263 264 void hfp_hf_drop_mSBC_if_eSCO_not_supported(uint8_t * codecs, uint8_t * codecs_nr){ 265 if (hci_extended_sco_link_supported()) return; 266 uint8_t tmp_codecs[HFP_MAX_NUM_CODECS]; 267 int i; 268 int tmp_codec_nr = 0; 269 for (i=0; i < *codecs_nr; i++){ 270 if (codecs[i] == HFP_CODEC_MSBC) continue; 271 tmp_codecs[tmp_codec_nr++] = codecs[i]; 272 } 273 *codecs_nr = tmp_codec_nr; 274 (void)memcpy(codecs, tmp_codecs, tmp_codec_nr); 275 } 276 277 // UTILS 278 int get_bit(uint16_t bitmap, int position){ 279 return (bitmap >> position) & 1; 280 } 281 282 int store_bit(uint32_t bitmap, int position, uint8_t value){ 283 if (value){ 284 bitmap |= 1 << position; 285 } else { 286 bitmap &= ~ (1 << position); 287 } 288 return bitmap; 289 } 290 291 int join(char * buffer, int buffer_size, uint8_t * values, int values_nr){ 292 if (buffer_size < (values_nr * 3)) return 0; 293 int i; 294 int offset = 0; 295 for (i = 0; i < (values_nr-1); i++) { 296 offset += snprintf(buffer+offset, buffer_size-offset, "%d,", values[i]); // puts string into buffer 297 } 298 if (i<values_nr){ 299 offset += snprintf(buffer+offset, buffer_size-offset, "%d", values[i]); 300 } 301 return offset; 302 } 303 304 int join_bitmap(char * buffer, int buffer_size, uint32_t values, int values_nr){ 305 if (buffer_size < (values_nr * 3)) return 0; 306 307 int i; 308 int offset = 0; 309 for (i = 0; i < (values_nr-1); i++) { 310 offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_bit(values,i)); // puts string into buffer 311 } 312 313 if (i<values_nr){ 314 offset += snprintf(buffer+offset, buffer_size-offset, "%d", get_bit(values,i)); 315 } 316 return offset; 317 } 318 static void hfp_emit_event_for_role(hfp_role_t local_role, uint8_t * packet, uint16_t size){ 319 switch (local_role){ 320 case HFP_ROLE_HF: 321 (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, packet, size); 322 break; 323 case HFP_ROLE_AG: 324 (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, packet, size); 325 break; 326 default: 327 btstack_unreachable(); 328 break; 329 } 330 } 331 332 static void hfp_emit_event_for_context(hfp_connection_t * hfp_connection, uint8_t * packet, uint16_t size){ 333 if (!hfp_connection) return; 334 hfp_emit_event_for_role(hfp_connection->local_role, packet, size); 335 } 336 337 void hfp_emit_simple_event(hfp_connection_t * hfp_connection, uint8_t event_subtype){ 338 hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 339 uint8_t event[5]; 340 event[0] = HCI_EVENT_HFP_META; 341 event[1] = sizeof(event) - 2; 342 event[2] = event_subtype; 343 little_endian_store_16(event, 3, acl_handle); 344 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 345 } 346 347 void hfp_emit_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, uint8_t value){ 348 hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 349 uint8_t event[6]; 350 event[0] = HCI_EVENT_HFP_META; 351 event[1] = sizeof(event) - 2; 352 event[2] = event_subtype; 353 little_endian_store_16(event, 3, acl_handle); 354 event[5] = value; // status 0 == OK 355 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 356 } 357 358 void hfp_emit_voice_recognition_enabled(hfp_connection_t * hfp_connection, uint8_t status){ 359 btstack_assert(hfp_connection != NULL); 360 361 uint8_t event[7]; 362 event[0] = HCI_EVENT_HFP_META; 363 event[1] = sizeof(event) - 2; 364 event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_ACTIVATED; 365 366 little_endian_store_16(event, 3, hfp_connection->acl_handle); 367 event[5] = status; // 0:success 368 event[6] = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0; 369 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 370 } 371 372 void hfp_emit_voice_recognition_disabled(hfp_connection_t * hfp_connection, uint8_t status){ 373 btstack_assert(hfp_connection != NULL); 374 375 uint8_t event[6]; 376 event[0] = HCI_EVENT_HFP_META; 377 event[1] = sizeof(event) - 2; 378 event[2] = HFP_SUBEVENT_VOICE_RECOGNITION_DEACTIVATED; 379 380 little_endian_store_16(event, 3, hfp_connection->acl_handle); 381 event[5] = status; // 0:success 382 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 383 } 384 385 void hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection_t * hfp_connection, uint8_t status){ 386 hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 387 388 uint8_t event[6]; 389 event[0] = HCI_EVENT_HFP_META; 390 event[1] = sizeof(event) - 2; 391 event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_HF_READY_FOR_AUDIO; 392 little_endian_store_16(event, 3, acl_handle); 393 event[5] = status; 394 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 395 } 396 397 void hfp_emit_enhanced_voice_recognition_state_event(hfp_connection_t * hfp_connection, uint8_t status){ 398 hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 399 400 uint8_t event[6]; 401 event[0] = HCI_EVENT_HFP_META; 402 event[1] = sizeof(event) - 2; 403 switch (hfp_connection->ag_vra_state){ 404 case HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT: 405 event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_READY_TO_ACCEPT_AUDIO_INPUT; 406 break; 407 case HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND: 408 event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_STARTING_SOUND; 409 break; 410 case HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT: 411 event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_IS_PROCESSING_AUDIO_INPUT; 412 break; 413 default: 414 btstack_unreachable(); 415 break; 416 } 417 418 little_endian_store_16(event, 3, acl_handle); 419 event[5] = status; 420 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 421 } 422 423 void hfp_emit_slc_connection_event(hfp_role_t local_role, uint8_t status, hci_con_handle_t con_handle, bd_addr_t addr){ 424 uint8_t event[12]; 425 int pos = 0; 426 event[pos++] = HCI_EVENT_HFP_META; 427 event[pos++] = sizeof(event) - 2; 428 event[pos++] = HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 429 little_endian_store_16(event, pos, con_handle); 430 pos += 2; 431 event[pos++] = status; // status 0 == OK 432 reverse_bd_addr(addr,&event[pos]); 433 pos += 6; 434 hfp_emit_event_for_role(local_role, event, sizeof(event)); 435 } 436 437 static void hfp_emit_audio_connection_released(hfp_connection_t * hfp_connection, hci_con_handle_t sco_handle){ 438 btstack_assert(hfp_connection != NULL); 439 uint8_t event[7]; 440 int pos = 0; 441 event[pos++] = HCI_EVENT_HFP_META; 442 event[pos++] = sizeof(event) - 2; 443 event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_RELEASED; 444 little_endian_store_16(event, pos, hfp_connection->acl_handle); 445 pos += 2; 446 little_endian_store_16(event, pos, sco_handle); 447 pos += 2; 448 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 449 } 450 451 void hfp_emit_sco_event(hfp_connection_t * hfp_connection, uint8_t status, hci_con_handle_t sco_handle, bd_addr_t addr, uint8_t negotiated_codec){ 452 btstack_assert(hfp_connection != NULL); 453 uint8_t event[15]; 454 int pos = 0; 455 event[pos++] = HCI_EVENT_HFP_META; 456 event[pos++] = sizeof(event) - 2; 457 event[pos++] = HFP_SUBEVENT_AUDIO_CONNECTION_ESTABLISHED; 458 little_endian_store_16(event, pos, hfp_connection->acl_handle); 459 pos += 2; 460 event[pos++] = status; // status 0 == OK 461 little_endian_store_16(event, pos, sco_handle); 462 pos += 2; 463 reverse_bd_addr(addr,&event[pos]); 464 pos += 6; 465 event[pos++] = negotiated_codec; 466 hfp_emit_event_for_context(hfp_connection, event, sizeof(event)); 467 } 468 469 void hfp_emit_string_event(hfp_connection_t * hfp_connection, uint8_t event_subtype, const char * value){ 470 btstack_assert(hfp_connection != NULL); 471 #ifdef ENABLE_HFP_AT_MESSAGES 472 uint8_t event[256]; 473 #else 474 uint8_t event[40]; 475 #endif 476 uint16_t string_len = btstack_min(strlen(value), sizeof(event) - 6); 477 event[0] = HCI_EVENT_HFP_META; 478 event[1] = 4 + string_len; 479 event[2] = event_subtype; 480 little_endian_store_16(event, 3, hfp_connection->acl_handle); 481 memcpy((char*)&event[5], value, string_len); 482 event[5 + string_len] = 0; 483 hfp_emit_event_for_context(hfp_connection, event, 6 + string_len); 484 } 485 486 btstack_linked_list_t * hfp_get_connections(void){ 487 return (btstack_linked_list_t *) &hfp_connections; 488 } 489 490 hfp_connection_t * get_hfp_connection_context_for_rfcomm_cid(uint16_t cid){ 491 btstack_linked_list_iterator_t it; 492 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 493 while (btstack_linked_list_iterator_has_next(&it)){ 494 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 495 if (hfp_connection->rfcomm_cid == cid){ 496 return hfp_connection; 497 } 498 } 499 return NULL; 500 } 501 502 hfp_connection_t * get_hfp_connection_context_for_bd_addr(bd_addr_t bd_addr, hfp_role_t hfp_role){ 503 btstack_linked_list_iterator_t it; 504 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 505 while (btstack_linked_list_iterator_has_next(&it)){ 506 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 507 if ((memcmp(hfp_connection->remote_addr, bd_addr, 6) == 0) && (hfp_connection->local_role == hfp_role)) { 508 return hfp_connection; 509 } 510 } 511 return NULL; 512 } 513 514 hfp_connection_t * get_hfp_connection_context_for_sco_handle(uint16_t handle, hfp_role_t hfp_role){ 515 btstack_linked_list_iterator_t it; 516 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 517 while (btstack_linked_list_iterator_has_next(&it)){ 518 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 519 if ((hfp_connection->sco_handle == handle) && (hfp_connection->local_role == hfp_role)){ 520 return hfp_connection; 521 } 522 } 523 return NULL; 524 } 525 526 hfp_connection_t * get_hfp_connection_context_for_acl_handle(uint16_t handle, hfp_role_t hfp_role){ 527 btstack_linked_list_iterator_t it; 528 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 529 while (btstack_linked_list_iterator_has_next(&it)){ 530 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 531 if ((hfp_connection->acl_handle == handle) && (hfp_connection->local_role == hfp_role)){ 532 return hfp_connection; 533 } 534 } 535 return NULL; 536 } 537 538 539 static void hfp_reset_voice_recognition(hfp_connection_t * hfp_connection){ 540 btstack_assert(hfp_connection != NULL); 541 hfp_voice_recognition_activation_status_t current_vra_state = hfp_connection->vra_state; 542 hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 543 544 if (current_vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){ 545 hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 546 } else if (hfp_connection->vra_state_requested != HFP_VRA_VOICE_RECOGNITION_OFF){ 547 hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 548 } 549 550 hfp_connection->vra_state_requested = HFP_VRA_VOICE_RECOGNITION_OFF; 551 hfp_connection->activate_voice_recognition = false; 552 hfp_connection->deactivate_voice_recognition = false; 553 hfp_connection->enhanced_voice_recognition_enabled = false; 554 hfp_connection->ag_vra_status = 0; 555 hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 556 } 557 558 void hfp_reset_context_flags(hfp_connection_t * hfp_connection){ 559 if (!hfp_connection) return; 560 hfp_connection->ok_pending = 0; 561 hfp_connection->send_error = 0; 562 563 hfp_connection->found_equal_sign = false; 564 565 hfp_connection->change_status_update_for_individual_ag_indicators = 0; 566 hfp_connection->operator_name_changed = 0; 567 568 hfp_connection->enable_extended_audio_gateway_error_report = 0; 569 hfp_connection->extended_audio_gateway_error = 0; 570 571 // establish codecs hfp_connection 572 hfp_connection->suggested_codec = 0; 573 hfp_connection->negotiated_codec = 0; 574 hfp_connection->codec_confirmed = 0; 575 576 hfp_connection->establish_audio_connection = 0; 577 hfp_connection->call_waiting_notification_enabled = 0; 578 hfp_connection->command = HFP_CMD_NONE; 579 hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 580 hfp_connection->clip_have_alpha = false; 581 hfp_reset_voice_recognition(hfp_connection); 582 } 583 584 static hfp_connection_t * create_hfp_connection_context(void){ 585 hfp_connection_t * hfp_connection = btstack_memory_hfp_connection_get(); 586 if (!hfp_connection) return NULL; 587 588 hfp_connection->state = HFP_IDLE; 589 hfp_connection->call_state = HFP_CALL_IDLE; 590 hfp_connection->codecs_state = HFP_CODECS_IDLE; 591 592 hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 593 594 hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 595 hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID; 596 597 hfp_reset_context_flags(hfp_connection); 598 599 btstack_linked_list_add(&hfp_connections, (btstack_linked_item_t*)hfp_connection); 600 return hfp_connection; 601 } 602 603 void hfp_finalize_connection_context(hfp_connection_t * hfp_connection){ 604 btstack_linked_list_remove(&hfp_connections, (btstack_linked_item_t*) hfp_connection); 605 btstack_memory_hfp_connection_free(hfp_connection); 606 } 607 608 static hfp_connection_t * hfp_create_connection(bd_addr_t bd_addr, hfp_role_t local_role){ 609 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role); 610 if (hfp_connection) return hfp_connection; 611 hfp_connection = create_hfp_connection_context(); 612 (void)memcpy(hfp_connection->remote_addr, bd_addr, 6); 613 hfp_connection->local_role = local_role; 614 log_info("Create HFP context %p: role %u, addr %s", hfp_connection, local_role, bd_addr_to_str(bd_addr)); 615 616 return hfp_connection; 617 } 618 619 /* @param network. 620 * 0 == no ability to reject a call. 621 * 1 == ability to reject a call. 622 */ 623 624 /* @param suported_features 625 * HF bit 0: EC and/or NR function (yes/no, 1 = yes, 0 = no) 626 * HF bit 1: Call waiting or three-way calling(yes/no, 1 = yes, 0 = no) 627 * HF bit 2: CLI presentation capability (yes/no, 1 = yes, 0 = no) 628 * HF bit 3: Voice recognition activation (yes/no, 1= yes, 0 = no) 629 * HF bit 4: Remote volume control (yes/no, 1 = yes, 0 = no) 630 * HF bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 631 */ 632 /* Bit position: 633 * AG bit 0: Three-way calling (yes/no, 1 = yes, 0 = no) 634 * AG bit 1: EC and/or NR function (yes/no, 1 = yes, 0 = no) 635 * AG bit 2: Voice recognition function (yes/no, 1 = yes, 0 = no) 636 * AG bit 3: In-band ring tone capability (yes/no, 1 = yes, 0 = no) 637 * AG bit 4: Attach a phone number to a voice tag (yes/no, 1 = yes, 0 = no) 638 * AG bit 5: Wide band speech (yes/no, 1 = yes, 0 = no) 639 */ 640 641 void hfp_create_sdp_record(uint8_t * service, uint32_t service_record_handle, uint16_t service_uuid, int rfcomm_channel_nr, const char * name){ 642 uint8_t* attribute; 643 de_create_sequence(service); 644 645 // 0x0000 "Service Record Handle" 646 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 647 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 648 649 // 0x0001 "Service Class ID List" 650 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 651 attribute = de_push_sequence(service); 652 { 653 // "UUID for Service" 654 de_add_number(attribute, DE_UUID, DE_SIZE_16, service_uuid); 655 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_GENERIC_AUDIO); 656 } 657 de_pop_sequence(service, attribute); 658 659 // 0x0004 "Protocol Descriptor List" 660 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 661 attribute = de_push_sequence(service); 662 { 663 uint8_t* l2cpProtocol = de_push_sequence(attribute); 664 { 665 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 666 } 667 de_pop_sequence(attribute, l2cpProtocol); 668 669 uint8_t* rfcomm = de_push_sequence(attribute); 670 { 671 de_add_number(rfcomm, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_RFCOMM); // rfcomm_service 672 de_add_number(rfcomm, DE_UINT, DE_SIZE_8, rfcomm_channel_nr); // rfcomm channel 673 } 674 de_pop_sequence(attribute, rfcomm); 675 } 676 de_pop_sequence(service, attribute); 677 678 679 // 0x0005 "Public Browse Group" 680 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BROWSE_GROUP_LIST); // public browse group 681 attribute = de_push_sequence(service); 682 { 683 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PUBLIC_BROWSE_ROOT); 684 } 685 de_pop_sequence(service, attribute); 686 687 // 0x0009 "Bluetooth Profile Descriptor List" 688 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 689 attribute = de_push_sequence(service); 690 { 691 uint8_t *sppProfile = de_push_sequence(attribute); 692 { 693 de_add_number(sppProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HANDSFREE); 694 de_add_number(sppProfile, DE_UINT, DE_SIZE_16, 0x0108); // Verision 1.8 695 } 696 de_pop_sequence(attribute, sppProfile); 697 } 698 de_pop_sequence(service, attribute); 699 700 // 0x0100 "Service Name" 701 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 702 de_add_data(service, DE_STRING, strlen(name), (uint8_t *) name); 703 } 704 705 static void hfp_handle_slc_setup_error(hfp_connection_t * hfp_connection, uint8_t status){ 706 // cache fields for event 707 hfp_role_t local_role = hfp_connection->local_role; 708 bd_addr_t remote_addr; 709 (void)memcpy(remote_addr, hfp_connection->remote_addr, 6); 710 // finalize connection struct 711 hfp_finalize_connection_context(hfp_connection); 712 // emit event 713 hfp_emit_slc_connection_event(local_role, status, HCI_CON_HANDLE_INVALID, remote_addr); 714 } 715 716 static void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 717 UNUSED(packet_type); // ok: handling own sdp events 718 UNUSED(channel); // ok: no channel 719 UNUSED(size); // ok: handling own sdp events 720 721 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(hfp_sdp_query_context.remote_address, hfp_sdp_query_context.local_role); 722 if (hfp_connection == NULL) { 723 log_info("connection with %s and local role %d not found", hfp_sdp_query_context.remote_address, hfp_sdp_query_context.local_role); 724 return; 725 } 726 727 switch (hci_event_packet_get_type(packet)){ 728 case SDP_EVENT_QUERY_RFCOMM_SERVICE: 729 hfp_connection->rfcomm_channel_nr = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet); 730 break; 731 case SDP_EVENT_QUERY_COMPLETE: 732 if (hfp_connection->rfcomm_channel_nr > 0){ 733 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 734 btstack_packet_handler_t packet_handler; 735 switch (hfp_connection->local_role){ 736 case HFP_ROLE_AG: 737 packet_handler = hfp_ag_rfcomm_packet_handler; 738 break; 739 case HFP_ROLE_HF: 740 packet_handler = hfp_hf_rfcomm_packet_handler; 741 break; 742 default: 743 btstack_assert(false); 744 return; 745 } 746 747 rfcomm_create_channel(packet_handler, hfp_connection->remote_addr, hfp_connection->rfcomm_channel_nr, NULL); 748 749 } else { 750 uint8_t status = sdp_event_query_complete_get_status(packet); 751 if (status == ERROR_CODE_SUCCESS){ 752 // report service not found 753 status = SDP_SERVICE_NOT_FOUND; 754 } 755 hfp_handle_slc_setup_error(hfp_connection, status); 756 log_info("rfcomm service not found, status 0x%02x", status); 757 } 758 759 // register the SDP Query request to check if there is another connection waiting for the query 760 // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 761 (void) sdp_client_register_query_callback(&hfp_sdp_query_request); 762 break; 763 default: 764 break; 765 } 766 } 767 768 // returns 0 if unexpected error or no other link options remained, otherwise 1 769 static int hfp_handle_failed_sco_connection(uint8_t status){ 770 771 if (!hfp_sco_establishment_active){ 772 log_info("(e)SCO Connection failed but not started by us"); 773 return 0; 774 } 775 776 log_info("(e)SCO Connection failed 0x%02x", status); 777 switch (status){ 778 case ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE: 779 case ERROR_CODE_UNSPECIFIED_ERROR: 780 case ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES: 781 break; 782 default: 783 return 0; 784 } 785 786 // note: eSCO_S4 supported flag not available, but it's only relevant for highest CVSD link setting (and the current failed) 787 hfp_link_settings_t next_setting = hfp_next_link_setting_for_connection(hfp_sco_establishment_active->link_setting, hfp_sco_establishment_active, false); 788 789 // handle no valid setting found 790 if (next_setting == HFP_LINK_SETTINGS_NONE) { 791 if (hfp_sco_establishment_active->negotiated_codec == HFP_CODEC_MSBC){ 792 log_info("T2/T1 failed, fallback to CVSD - D1"); 793 hfp_sco_establishment_active->negotiated_codec = HFP_CODEC_CVSD; 794 hfp_sco_establishment_active->sco_for_msbc_failed = 1; 795 hfp_sco_establishment_active->ag_send_common_codec = true; 796 hfp_sco_establishment_active->link_setting = HFP_LINK_SETTINGS_D1; 797 } else { 798 // no other options 799 return 0; 800 } 801 } 802 803 log_info("e)SCO Connection: try new link_setting %d", next_setting); 804 hfp_sco_establishment_active->establish_audio_connection = 1; 805 hfp_sco_establishment_active->link_setting = next_setting; 806 hfp_sco_establishment_active = NULL; 807 return 1; 808 } 809 810 void hfp_handle_hci_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ 811 UNUSED(packet_type); 812 UNUSED(channel); // ok: no channel 813 UNUSED(size); 814 815 bd_addr_t event_addr; 816 hci_con_handle_t handle; 817 hfp_connection_t * hfp_connection = NULL; 818 uint8_t status; 819 820 log_debug("HFP HCI event handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 821 822 switch (hci_event_packet_get_type(packet)) { 823 824 case HCI_EVENT_CONNECTION_REQUEST: 825 switch(hci_event_connection_request_get_link_type(packet)){ 826 case 0: // SCO 827 case 2: // eSCO 828 hci_event_connection_request_get_bd_addr(packet, event_addr); 829 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 830 if (!hfp_connection) break; 831 if (hci_event_connection_request_get_link_type(packet) == 2){ 832 hfp_connection->accept_sco = 2; 833 } else { 834 hfp_connection->accept_sco = 1; 835 } 836 #ifdef ENABLE_CC256X_ASSISTED_HFP 837 hfp_cc256x_prepare_for_sco(hfp_connection); 838 #endif 839 #ifdef ENABLE_BCM_PCM_WBS 840 hfp_bcm_prepare_for_sco(hfp_connection); 841 #endif 842 #ifdef ENABLE_RTK_PCM_WBS 843 hfp_connection->rtk_send_sco_config = true; 844 #endif 845 log_info("accept sco %u\n", hfp_connection->accept_sco); 846 hfp_sco_establishment_active = hfp_connection; 847 break; 848 default: 849 break; 850 } 851 break; 852 853 case HCI_EVENT_COMMAND_STATUS: 854 if (hci_event_command_status_get_command_opcode(packet) == hci_setup_synchronous_connection.opcode) { 855 if (hfp_sco_establishment_active == NULL) break; 856 status = hci_event_command_status_get_status(packet); 857 if (status == ERROR_CODE_SUCCESS) break; 858 859 hfp_connection = hfp_sco_establishment_active; 860 if (hfp_handle_failed_sco_connection(status)) break; 861 hfp_connection->establish_audio_connection = 0; 862 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 863 hfp_emit_sco_event(hfp_connection, status, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 864 } 865 break; 866 867 case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE:{ 868 if (hfp_sco_establishment_active == NULL) break; 869 hci_event_synchronous_connection_complete_get_bd_addr(packet, event_addr); 870 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 871 if (!hfp_connection) { 872 log_error("HFP: connection does not exist for remote with addr %s.", bd_addr_to_str(event_addr)); 873 return; 874 } 875 876 status = hci_event_synchronous_connection_complete_get_status(packet); 877 if (status != ERROR_CODE_SUCCESS){ 878 hfp_connection->accept_sco = 0; 879 if (hfp_handle_failed_sco_connection(status)) break; 880 881 hfp_connection->establish_audio_connection = 0; 882 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 883 hfp_emit_sco_event(hfp_connection, status, 0, event_addr, hfp_connection->negotiated_codec); 884 break; 885 } 886 887 uint16_t sco_handle = hci_event_synchronous_connection_complete_get_handle(packet); 888 uint8_t link_type = hci_event_synchronous_connection_complete_get_link_type(packet); 889 uint8_t transmission_interval = hci_event_synchronous_connection_complete_get_transmission_interval(packet); // measured in slots 890 uint8_t retransmission_interval = hci_event_synchronous_connection_complete_get_retransmission_interval(packet);// measured in slots 891 uint16_t rx_packet_length = hci_event_synchronous_connection_complete_get_rx_packet_length(packet); // measured in bytes 892 uint16_t tx_packet_length = hci_event_synchronous_connection_complete_get_tx_packet_length(packet); // measured in bytes 893 894 switch (link_type){ 895 case 0x00: 896 log_info("SCO Connection established."); 897 if (transmission_interval != 0) log_error("SCO Connection: transmission_interval not zero: %d.", transmission_interval); 898 if (retransmission_interval != 0) log_error("SCO Connection: retransmission_interval not zero: %d.", retransmission_interval); 899 if (rx_packet_length != 0) log_error("SCO Connection: rx_packet_length not zero: %d.", rx_packet_length); 900 if (tx_packet_length != 0) log_error("SCO Connection: tx_packet_length not zero: %d.", tx_packet_length); 901 break; 902 case 0x02: 903 log_info("eSCO Connection established. \n"); 904 break; 905 default: 906 log_error("(e)SCO reserved link_type 0x%2x", link_type); 907 break; 908 } 909 910 log_info("sco_handle 0x%2x, address %s, transmission_interval %u slots, retransmission_interval %u slots, " 911 " rx_packet_length %u bytes, tx_packet_length %u bytes, air_mode 0x%2x (0x02 == CVSD)\n", sco_handle, 912 bd_addr_to_str(event_addr), transmission_interval, retransmission_interval, rx_packet_length, tx_packet_length, 913 hci_event_synchronous_connection_complete_get_air_mode(packet)); 914 915 if (hfp_connection->state == HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN){ 916 log_info("SCO about to disconnect: HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN"); 917 hfp_connection->state = HFP_W2_DISCONNECT_SCO; 918 break; 919 } 920 hfp_connection->sco_handle = sco_handle; 921 hfp_connection->establish_audio_connection = 0; 922 923 hfp_connection->state = HFP_AUDIO_CONNECTION_ESTABLISHED; 924 925 switch (hfp_connection->vra_state){ 926 case HFP_VRA_VOICE_RECOGNITION_ACTIVATED: 927 hfp_connection->ag_audio_connection_opened_before_vra = false; 928 break; 929 default: 930 hfp_connection->ag_audio_connection_opened_before_vra = true; 931 break; 932 } 933 hfp_emit_sco_event(hfp_connection, status, sco_handle, event_addr, hfp_connection->negotiated_codec); 934 break; 935 } 936 937 case HCI_EVENT_DISCONNECTION_COMPLETE: 938 handle = little_endian_read_16(packet,3); 939 hfp_connection = get_hfp_connection_context_for_sco_handle(handle, local_role); 940 941 if (!hfp_connection) break; 942 943 #ifdef ENABLE_CC256X_ASSISTED_HFP 944 hfp_connection->cc256x_send_wbs_disassociate = true; 945 #endif 946 #ifdef ENABLE_BCM_PCM_WBS 947 hfp_connection->bcm_send_disable_wbs = true; 948 #endif 949 if (hfp_connection->sco_handle == handle){ 950 hfp_connection->sco_handle = HCI_CON_HANDLE_INVALID; 951 hfp_connection->release_audio_connection = 0; 952 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 953 hfp_emit_audio_connection_released(hfp_connection, handle); 954 955 hfp_connection->ag_audio_connection_opened_before_vra = false; 956 957 if (hfp_connection->acl_handle == HCI_CON_HANDLE_INVALID){ 958 hfp_reset_voice_recognition(hfp_connection); 959 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 960 hfp_finalize_connection_context(hfp_connection); 961 break; 962 } else if (hfp_connection->release_slc_connection == 1){ 963 hfp_connection->release_slc_connection = 0; 964 hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 965 rfcomm_disconnect(hfp_connection->acl_handle); 966 } 967 } 968 969 if (hfp_connection->state == HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN){ 970 // RFCOMM already closed -> remote power off 971 #if defined(ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 972 hfp_connection->state = HFP_W4_WBS_SHUTDOWN; 973 #else 974 hfp_finalize_connection_context(hfp_connection); 975 #endif 976 break; 977 } 978 break; 979 980 default: 981 break; 982 } 983 } 984 985 986 void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size, hfp_role_t local_role){ 987 UNUSED(packet_type); 988 UNUSED(channel); // ok: no channel 989 UNUSED(size); 990 991 bd_addr_t event_addr; 992 uint16_t rfcomm_cid; 993 hfp_connection_t * hfp_connection = NULL; 994 uint8_t status; 995 996 log_debug("HFP packet_handler type %u, event type %x, size %u", packet_type, hci_event_packet_get_type(packet), size); 997 998 switch (hci_event_packet_get_type(packet)) { 999 1000 case RFCOMM_EVENT_INCOMING_CONNECTION: 1001 // data: event (8), len(8), address(48), channel (8), rfcomm_cid (16) 1002 rfcomm_event_incoming_connection_get_bd_addr(packet, event_addr); 1003 hfp_connection = hfp_create_connection(event_addr, local_role); 1004 if (!hfp_connection){ 1005 log_info("hfp: no memory to accept incoming connection - decline"); 1006 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); 1007 return; 1008 } 1009 if (hfp_connection->state != HFP_IDLE) { 1010 log_error("hfp: incoming connection but not idle, reject"); 1011 rfcomm_decline_connection(rfcomm_event_incoming_connection_get_rfcomm_cid(packet)); 1012 return; 1013 } 1014 1015 hfp_connection->rfcomm_cid = rfcomm_event_incoming_connection_get_rfcomm_cid(packet); 1016 hfp_connection->state = HFP_W4_RFCOMM_CONNECTED; 1017 rfcomm_accept_connection(hfp_connection->rfcomm_cid); 1018 break; 1019 1020 case RFCOMM_EVENT_CHANNEL_OPENED: 1021 rfcomm_event_channel_opened_get_bd_addr(packet, event_addr); 1022 1023 hfp_connection = get_hfp_connection_context_for_bd_addr(event_addr, local_role); 1024 btstack_assert(hfp_connection != NULL); 1025 1026 if (hfp_connection->state != HFP_W4_RFCOMM_CONNECTED){ 1027 break; 1028 } 1029 1030 status = rfcomm_event_channel_opened_get_status(packet); 1031 if (status != ERROR_CODE_SUCCESS) { 1032 hfp_handle_slc_setup_error(hfp_connection, status); 1033 break; 1034 } 1035 1036 hfp_connection->acl_handle = rfcomm_event_channel_opened_get_con_handle(packet); 1037 hfp_connection->rfcomm_cid = rfcomm_event_channel_opened_get_rfcomm_cid(packet); 1038 hfp_connection->rfcomm_mtu = rfcomm_event_channel_opened_get_max_frame_size(packet); 1039 bd_addr_copy(hfp_connection->remote_addr, event_addr); 1040 hfp_connection->state = HFP_EXCHANGE_SUPPORTED_FEATURES; 1041 1042 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1043 break; 1044 1045 case RFCOMM_EVENT_CHANNEL_CLOSED: 1046 rfcomm_cid = little_endian_read_16(packet,2); 1047 hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 1048 if (!hfp_connection) break; 1049 switch (hfp_connection->state){ 1050 case HFP_W4_RFCOMM_DISCONNECTED_AND_RESTART: 1051 hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 1052 hfp_connection->state = HFP_IDLE; 1053 hfp_establish_service_level_connection(hfp_connection->remote_addr, hfp_connection->service_uuid, local_role); 1054 break; 1055 1056 case HFP_AUDIO_CONNECTION_ESTABLISHED: 1057 // service connection was released, this implicitly releases audio connection as well 1058 hfp_connection->release_audio_connection = 0; 1059 hfp_connection->acl_handle = HCI_CON_HANDLE_INVALID; 1060 hfp_connection->state = HFP_W4_SCO_DISCONNECTED_TO_SHUTDOWN; 1061 gap_disconnect(hfp_connection->sco_handle); 1062 break; 1063 1064 default: 1065 // regular case 1066 hfp_reset_voice_recognition(hfp_connection); 1067 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_RELEASED, 0); 1068 hfp_finalize_connection_context(hfp_connection); 1069 break; 1070 } 1071 break; 1072 1073 default: 1074 break; 1075 } 1076 } 1077 // translates command string into hfp_command_t CMD 1078 1079 typedef struct { 1080 const char * command; 1081 hfp_command_t command_id; 1082 } hfp_command_entry_t; 1083 1084 static hfp_command_entry_t hfp_ag_commmand_table[] = { 1085 { "AT+BAC=", HFP_CMD_AVAILABLE_CODECS }, 1086 { "AT+BCC", HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP }, 1087 { "AT+BCS=", HFP_CMD_HF_CONFIRMED_CODEC }, 1088 { "AT+BIA=", HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE, }, // +BIA:<enabled>,,<enabled>,,,<enabled> 1089 { "AT+BIEV=", HFP_CMD_HF_INDICATOR_STATUS }, 1090 { "AT+BIND=", HFP_CMD_LIST_GENERIC_STATUS_INDICATORS }, 1091 { "AT+BIND=?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS }, 1092 { "AT+BIND?", HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE }, 1093 { "AT+BINP=", HFP_CMD_HF_REQUEST_PHONE_NUMBER }, 1094 { "AT+BLDN", HFP_CMD_REDIAL_LAST_NUMBER }, 1095 { "AT+BRSF=", HFP_CMD_SUPPORTED_FEATURES }, 1096 { "AT+BTRH=", HFP_CMD_RESPONSE_AND_HOLD_COMMAND }, 1097 { "AT+BTRH?", HFP_CMD_RESPONSE_AND_HOLD_QUERY }, 1098 { "AT+BVRA=", HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION }, 1099 { "AT+CCWA=", HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION}, 1100 { "AT+CHLD=", HFP_CMD_CALL_HOLD }, 1101 { "AT+CHLD=?", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES }, 1102 { "AT+CHUP", HFP_CMD_HANG_UP_CALL }, 1103 { "AT+CIND=?", HFP_CMD_RETRIEVE_AG_INDICATORS }, 1104 { "AT+CIND?", HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS }, 1105 { "AT+CLCC", HFP_CMD_LIST_CURRENT_CALLS }, 1106 { "AT+CLIP=", HFP_CMD_ENABLE_CLIP}, 1107 { "AT+CMEE=", HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR}, 1108 { "AT+CMER=", HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE }, 1109 { "AT+CNUM", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION }, 1110 { "AT+COPS=", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT }, 1111 { "AT+COPS?", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME }, 1112 { "AT+NREC=", HFP_CMD_TURN_OFF_EC_AND_NR, }, 1113 { "AT+VGM=", HFP_CMD_SET_MICROPHONE_GAIN }, 1114 { "AT+VGS=", HFP_CMD_SET_SPEAKER_GAIN }, 1115 { "AT+VTS=", HFP_CMD_TRANSMIT_DTMF_CODES }, 1116 { "ATA", HFP_CMD_CALL_ANSWERED }, 1117 }; 1118 1119 static hfp_command_entry_t hfp_hf_commmand_table[] = { 1120 { "+BCS:", HFP_CMD_AG_SUGGESTED_CODEC }, 1121 { "+BIND:", HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS }, 1122 { "+BINP", HFP_CMD_AG_SENT_PHONE_NUMBER }, 1123 { "+BRSF:", HFP_CMD_SUPPORTED_FEATURES }, 1124 { "+BSIR:", HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING }, 1125 { "+BTRH:", HFP_CMD_RESPONSE_AND_HOLD_STATUS }, 1126 { "+BVRA:", HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION }, 1127 { "+CCWA:", HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE, }, 1128 { "+CHLD:", HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES }, 1129 { "+CIEV:", HFP_CMD_TRANSFER_AG_INDICATOR_STATUS}, 1130 { "+CIND:", HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC }, 1131 { "+CLCC:", HFP_CMD_LIST_CURRENT_CALLS }, 1132 { "+CLIP:", HFP_CMD_AG_SENT_CLIP_INFORMATION }, 1133 { "+CME ERROR:", HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR }, 1134 { "+CNUM:", HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION}, 1135 { "+COPS:", HFP_CMD_QUERY_OPERATOR_SELECTION_NAME }, 1136 { "+VGM:", HFP_CMD_SET_MICROPHONE_GAIN }, 1137 { "+VGM=", HFP_CMD_SET_MICROPHONE_GAIN }, 1138 { "+VGS:", HFP_CMD_SET_SPEAKER_GAIN}, 1139 { "+VGS=", HFP_CMD_SET_SPEAKER_GAIN}, 1140 { "ERROR", HFP_CMD_ERROR}, 1141 { "NOP", HFP_CMD_NONE}, // dummy command used by unit tests 1142 { "OK", HFP_CMD_OK }, 1143 { "RING", HFP_CMD_RING }, 1144 }; 1145 1146 static hfp_command_t parse_command(const char * line_buffer, int isHandsFree){ 1147 1148 // table lookup based on role 1149 uint16_t num_entries; 1150 hfp_command_entry_t * table; 1151 if (isHandsFree == 0){ 1152 table = hfp_ag_commmand_table; 1153 num_entries = sizeof(hfp_ag_commmand_table) / sizeof(hfp_command_entry_t); 1154 } else { 1155 table = hfp_hf_commmand_table; 1156 num_entries = sizeof(hfp_hf_commmand_table) / sizeof(hfp_command_entry_t); 1157 } 1158 // binary search 1159 uint16_t left = 0; 1160 uint16_t right = num_entries - 1; 1161 while (left <= right){ 1162 uint16_t middle = left + (right - left) / 2; 1163 hfp_command_entry_t *entry = &table[middle]; 1164 int match = strcmp(line_buffer, entry->command); 1165 if (match < 0){ 1166 // search term is lower than middle element 1167 if (right == 0) break; 1168 right = middle - 1; 1169 } else if (match == 0){ 1170 return entry->command_id; 1171 } else { 1172 // search term is higher than middle element 1173 left = middle + 1; 1174 } 1175 } 1176 1177 // note: if parser in CMD_HEADER state would treats digits and maybe '+' as separator, match on "ATD" would work. 1178 // note: phone number is currently expected in line_buffer[3..] 1179 // prefix match on 'ATD', AG only 1180 if ((isHandsFree == 0) && (strncmp(line_buffer, HFP_CALL_PHONE_NUMBER, strlen(HFP_CALL_PHONE_NUMBER)) == 0)){ 1181 return HFP_CMD_CALL_PHONE_NUMBER; 1182 } 1183 1184 // Valid looking, but unknown commands/responses 1185 if ((isHandsFree == 0) && (strncmp(line_buffer, "AT+", 3) == 0)){ 1186 return HFP_CMD_UNKNOWN; 1187 } 1188 1189 if ((isHandsFree != 0) && (strncmp(line_buffer, "+", 1) == 0)){ 1190 return HFP_CMD_UNKNOWN; 1191 } 1192 1193 return HFP_CMD_NONE; 1194 } 1195 1196 static void hfp_parser_store_byte(hfp_connection_t * hfp_connection, uint8_t byte){ 1197 if ((hfp_connection->line_size + 1) >= HFP_MAX_VR_TEXT_SIZE) return; 1198 hfp_connection->line_buffer[hfp_connection->line_size++] = byte; 1199 hfp_connection->line_buffer[hfp_connection->line_size] = 0; 1200 } 1201 static int hfp_parser_is_buffer_empty(hfp_connection_t * hfp_connection){ 1202 return hfp_connection->line_size == 0; 1203 } 1204 1205 static int hfp_parser_is_end_of_line(uint8_t byte){ 1206 return (byte == '\n') || (byte == '\r'); 1207 } 1208 1209 static void hfp_parser_reset_line_buffer(hfp_connection_t *hfp_connection) { 1210 hfp_connection->line_size = 0; 1211 // we don't set the first byte to '\0' to allow access to last argument e.g. in hfp_hf_handle_rfcommand 1212 } 1213 1214 static void hfp_parser_store_if_token(hfp_connection_t * hfp_connection, uint8_t byte){ 1215 switch (byte){ 1216 case ',': 1217 case '-': 1218 case ';': 1219 case '(': 1220 case ')': 1221 case '\n': 1222 case '\r': 1223 break; 1224 default: 1225 hfp_parser_store_byte(hfp_connection, byte); 1226 break; 1227 } 1228 } 1229 1230 static bool hfp_parser_is_separator( uint8_t byte){ 1231 switch (byte){ 1232 case ',': 1233 case '-': 1234 case ';': 1235 case '\n': 1236 case '\r': 1237 return true; 1238 default: 1239 return false; 1240 } 1241 } 1242 1243 static bool hfp_parse_byte(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 1244 1245 // handle doubles quotes 1246 if (byte == '"'){ 1247 hfp_connection->parser_quoted = !hfp_connection->parser_quoted; 1248 return true; 1249 } 1250 if (hfp_connection->parser_quoted) { 1251 hfp_parser_store_byte(hfp_connection, byte); 1252 return true; 1253 } 1254 1255 // ignore spaces outside command or double quotes (required e.g. for '+CME ERROR:..") command 1256 if ((byte == ' ') && (hfp_connection->parser_state != HFP_PARSER_CMD_HEADER)) return true; 1257 1258 bool processed = true; 1259 1260 switch (hfp_connection->parser_state) { 1261 case HFP_PARSER_CMD_HEADER: 1262 switch (byte) { 1263 case '\n': 1264 case '\r': 1265 case ';': 1266 // ignore separator 1267 break; 1268 case ':': 1269 case '?': 1270 // store separator 1271 hfp_parser_store_byte(hfp_connection, byte); 1272 break; 1273 case '=': 1274 // equal sign: remember and wait for next char to decided between '=?' and '=\?' 1275 hfp_connection->found_equal_sign = true; 1276 hfp_parser_store_byte(hfp_connection, byte); 1277 return true; 1278 default: 1279 // store if not lookahead 1280 if (!hfp_connection->found_equal_sign) { 1281 hfp_parser_store_byte(hfp_connection, byte); 1282 return true; 1283 } 1284 // mark as lookahead 1285 processed = false; 1286 break; 1287 } 1288 1289 // ignore empty tokens 1290 if (hfp_parser_is_buffer_empty(hfp_connection)) return true; 1291 1292 // parse 1293 hfp_connection->command = parse_command((char *)hfp_connection->line_buffer, isHandsFree); 1294 1295 // pick +CIND version based on connection state: descriptions during SLC vs. states later 1296 if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS_GENERIC){ 1297 switch(hfp_connection->state){ 1298 case HFP_W4_RETRIEVE_INDICATORS_STATUS: 1299 hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS; 1300 break; 1301 case HFP_W4_RETRIEVE_INDICATORS: 1302 hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS; 1303 break; 1304 default: 1305 hfp_connection->command = HFP_CMD_UNKNOWN; 1306 break; 1307 } 1308 } 1309 1310 log_info("command string '%s', handsfree %u -> cmd id %u", (char *)hfp_connection->line_buffer, isHandsFree, hfp_connection->command); 1311 1312 // next state 1313 hfp_connection->found_equal_sign = false; 1314 hfp_parser_reset_line_buffer(hfp_connection); 1315 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 1316 1317 return processed; 1318 1319 case HFP_PARSER_CMD_SEQUENCE: 1320 // handle empty fields 1321 if ((byte == ',' ) && (hfp_connection->line_size == 0)){ 1322 hfp_connection->line_buffer[0] = 0; 1323 hfp_connection->ignore_value = 1; 1324 parse_sequence(hfp_connection); 1325 return true; 1326 } 1327 1328 hfp_parser_store_if_token(hfp_connection, byte); 1329 if (!hfp_parser_is_separator(byte)) return true; 1330 1331 // ignore empty tokens 1332 switch (hfp_connection->command){ 1333 case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 1334 // don't ignore empty string 1335 break; 1336 default: 1337 if (hfp_parser_is_buffer_empty(hfp_connection) && (hfp_connection->ignore_value == 0)) { 1338 return true; 1339 } 1340 break; 1341 } 1342 1343 parse_sequence(hfp_connection); 1344 1345 hfp_parser_reset_line_buffer(hfp_connection); 1346 1347 switch (hfp_connection->command){ 1348 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1349 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1350 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1351 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1352 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1353 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1354 case HFP_CMD_RETRIEVE_AG_INDICATORS: 1355 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 1356 case HFP_CMD_HF_INDICATOR_STATUS: 1357 hfp_connection->parser_state = HFP_PARSER_SECOND_ITEM; 1358 break; 1359 default: 1360 break; 1361 } 1362 return true; 1363 1364 case HFP_PARSER_SECOND_ITEM: 1365 1366 hfp_parser_store_if_token(hfp_connection, byte); 1367 if (!hfp_parser_is_separator(byte)) return true; 1368 1369 switch (hfp_connection->command){ 1370 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1371 log_info("format %s, ", hfp_connection->line_buffer); 1372 hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1373 break; 1374 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1375 log_info("format %s \n", hfp_connection->line_buffer); 1376 hfp_connection->network_operator.format = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1377 break; 1378 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1379 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1380 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 1381 hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].state = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1382 break; 1383 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1384 hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1385 log_info("%d \n", hfp_connection->ag_indicators[hfp_connection->parser_item_index].status); 1386 hfp_connection->ag_indicators[hfp_connection->parser_item_index].status_changed = 1; 1387 break; 1388 case HFP_CMD_RETRIEVE_AG_INDICATORS: 1389 hfp_connection->ag_indicators[hfp_connection->parser_item_index].min_range = btstack_atoi((char *)hfp_connection->line_buffer); 1390 log_info("%s, ", hfp_connection->line_buffer); 1391 break; 1392 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1393 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1394 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1395 hfp_connection->bnip_type = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1396 break; 1397 case HFP_CMD_HF_INDICATOR_STATUS: 1398 hfp_connection->parser_indicator_value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1399 break; 1400 default: 1401 break; 1402 } 1403 1404 hfp_parser_reset_line_buffer(hfp_connection); 1405 1406 hfp_connection->parser_state = HFP_PARSER_THIRD_ITEM; 1407 1408 return true; 1409 1410 case HFP_PARSER_THIRD_ITEM: 1411 1412 hfp_parser_store_if_token(hfp_connection, byte); 1413 if (!hfp_parser_is_separator(byte)) return true; 1414 1415 switch (hfp_connection->command){ 1416 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1417 btstack_strcpy(hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE, (char *)hfp_connection->line_buffer); 1418 break; 1419 case HFP_CMD_RETRIEVE_AG_INDICATORS: 1420 hfp_connection->ag_indicators[hfp_connection->parser_item_index].max_range = btstack_atoi((char *)hfp_connection->line_buffer); 1421 hfp_next_indicators_index(hfp_connection); 1422 hfp_connection->ag_indicators_nr = hfp_connection->parser_item_index; 1423 break; 1424 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1425 // track if last argument exists 1426 hfp_connection->clip_have_alpha = hfp_connection->line_size != 0; 1427 break; 1428 default: 1429 break; 1430 } 1431 1432 hfp_parser_reset_line_buffer(hfp_connection); 1433 1434 if (hfp_connection->command == HFP_CMD_RETRIEVE_AG_INDICATORS){ 1435 hfp_connection->parser_state = HFP_PARSER_CMD_SEQUENCE; 1436 } 1437 return true; 1438 1439 default: 1440 btstack_assert(false); 1441 return true; 1442 } 1443 } 1444 1445 void hfp_parse(hfp_connection_t * hfp_connection, uint8_t byte, int isHandsFree){ 1446 bool processed = false; 1447 while (!processed){ 1448 processed = hfp_parse_byte(hfp_connection, byte, isHandsFree); 1449 } 1450 // reset parser state on end-of-line 1451 if (hfp_parser_is_end_of_line(byte)){ 1452 hfp_connection->parser_item_index = 0; 1453 hfp_connection->parser_state = HFP_PARSER_CMD_HEADER; 1454 } 1455 } 1456 1457 static void parse_sequence(hfp_connection_t * hfp_connection){ 1458 int value; 1459 switch (hfp_connection->command){ 1460 case HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS: 1461 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1462 int i; 1463 switch (hfp_connection->parser_item_index){ 1464 case 0: 1465 for (i=0;i<hfp_connection->generic_status_indicators_nr;i++){ 1466 if (hfp_connection->generic_status_indicators[i].uuid == value){ 1467 hfp_connection->parser_indicator_index = i; 1468 break; 1469 } 1470 } 1471 break; 1472 case 1: 1473 if (hfp_connection->parser_indicator_index <0) break; 1474 hfp_connection->generic_status_indicators[hfp_connection->parser_indicator_index].state = value; 1475 log_info("HFP_CMD_SET_GENERIC_STATUS_INDICATOR_STATUS set indicator at index %u, to %u\n", 1476 hfp_connection->parser_item_index, value); 1477 break; 1478 default: 1479 break; 1480 } 1481 hfp_next_indicators_index(hfp_connection); 1482 break; 1483 1484 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1485 switch(hfp_connection->parser_item_index){ 1486 case 0: 1487 // <alpha>: This optional field is not supported, and shall be left blank. 1488 break; 1489 case 1: 1490 // <number>: Quoted string containing the phone number in the format specified by <type>. 1491 btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer); 1492 break; 1493 case 2: 1494 /* 1495 <type> field specifies the format of the phone number provided, and can be one of the following values: 1496 - values 128-143: The phone number format may be a national or international format, and may contain prefix and/or escape digits. No changes on the number presentation are required. 1497 - values 144-159: The phone number format is an international number, including the country code prefix. If the plus sign ("+") is not included as part of the number and shall be added by the AG as needed. 1498 - values 160-175: National number. No prefix nor escape digits included. 1499 */ 1500 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1501 hfp_connection->bnip_type = value; 1502 break; 1503 case 3: 1504 // <speed>: This optional field is not supported, and shall be left blank. 1505 break; 1506 case 4: 1507 // <service>: Indicates which service this phone number relates to. Shall be either 4 (voice) or 5 (fax). 1508 default: 1509 break; 1510 } 1511 // index > 2 are ignored in switch above 1512 hfp_connection->parser_item_index++; 1513 break; 1514 case HFP_CMD_LIST_CURRENT_CALLS: 1515 switch(hfp_connection->parser_item_index){ 1516 case 0: 1517 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1518 hfp_connection->clcc_idx = value; 1519 break; 1520 case 1: 1521 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1522 hfp_connection->clcc_dir = value; 1523 break; 1524 case 2: 1525 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1526 hfp_connection->clcc_status = value; 1527 break; 1528 case 3: 1529 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1530 hfp_connection->clcc_mode = value; 1531 break; 1532 case 4: 1533 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1534 hfp_connection->clcc_mpty = value; 1535 break; 1536 case 5: 1537 btstack_strcpy(hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer); 1538 break; 1539 case 6: 1540 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1541 hfp_connection->bnip_type = value; 1542 break; 1543 default: 1544 break; 1545 } 1546 // index > 6 are ignored in switch above 1547 hfp_connection->parser_item_index++; 1548 break; 1549 case HFP_CMD_SET_MICROPHONE_GAIN: 1550 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1551 hfp_connection->microphone_gain = value; 1552 log_info("hfp parse HFP_CMD_SET_MICROPHONE_GAIN %d\n", value); 1553 break; 1554 case HFP_CMD_SET_SPEAKER_GAIN: 1555 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1556 hfp_connection->speaker_gain = value; 1557 log_info("hfp parse HFP_CMD_SET_SPEAKER_GAIN %d\n", value); 1558 break; 1559 case HFP_CMD_TURN_OFF_EC_AND_NR: 1560 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1561 hfp_connection->ag_echo_and_noise_reduction = value; 1562 log_info("hfp parse HFP_CMD_TURN_OFF_EC_AND_NR %d\n", value); 1563 break; 1564 case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 1565 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1566 hfp_connection->remote_supported_features = store_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE, value); 1567 log_info("hfp parse HFP_CHANGE_IN_BAND_RING_TONE_SETTING %d\n", value); 1568 break; 1569 case HFP_CMD_HF_CONFIRMED_CODEC: 1570 hfp_connection->codec_confirmed = btstack_atoi((char*)hfp_connection->line_buffer); 1571 log_info("hfp parse HFP_CMD_HF_CONFIRMED_CODEC %d\n", hfp_connection->codec_confirmed); 1572 break; 1573 case HFP_CMD_AG_SUGGESTED_CODEC: 1574 hfp_connection->suggested_codec = btstack_atoi((char*)hfp_connection->line_buffer); 1575 log_info("hfp parse HFP_CMD_AG_SUGGESTED_CODEC %d\n", hfp_connection->suggested_codec); 1576 break; 1577 case HFP_CMD_SUPPORTED_FEATURES: 1578 hfp_connection->remote_supported_features = btstack_atoi((char*)hfp_connection->line_buffer); 1579 log_info("Parsed supported feature %d\n", (int) hfp_connection->remote_supported_features); 1580 break; 1581 case HFP_CMD_AVAILABLE_CODECS: 1582 log_info("Parsed codec %s\n", hfp_connection->line_buffer); 1583 hfp_connection->remote_codecs[hfp_connection->parser_item_index] = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer); 1584 hfp_next_codec_index(hfp_connection); 1585 hfp_connection->remote_codecs_nr = hfp_connection->parser_item_index; 1586 break; 1587 case HFP_CMD_RETRIEVE_AG_INDICATORS: 1588 btstack_strcpy((char *)hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, HFP_MAX_INDICATOR_DESC_SIZE, (char *)hfp_connection->line_buffer); 1589 hfp_connection->ag_indicators[hfp_connection->parser_item_index].index = hfp_connection->parser_item_index+1; 1590 log_info("Indicator %d: %s (", hfp_connection->ag_indicators_nr+1, hfp_connection->line_buffer); 1591 break; 1592 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1593 log_info("Parsed Indicator %d with status: %s\n", hfp_connection->parser_item_index+1, hfp_connection->line_buffer); 1594 hfp_connection->ag_indicators[hfp_connection->parser_item_index].status = btstack_atoi((char *) hfp_connection->line_buffer); 1595 hfp_next_indicators_index(hfp_connection); 1596 break; 1597 case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 1598 hfp_next_indicators_index(hfp_connection); 1599 if (hfp_connection->parser_item_index != 4) break; 1600 log_info("Parsed Enable indicators: %s\n", hfp_connection->line_buffer); 1601 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1602 hfp_connection->enable_status_update_for_ag_indicators = (uint8_t) value; 1603 break; 1604 case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 1605 log_info("Parsed Support call hold: %s\n", hfp_connection->line_buffer); 1606 if (hfp_connection->line_size > 2 ) break; 1607 memcpy((char *)hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name, (char *)hfp_connection->line_buffer, HFP_CALL_SERVICE_SIZE-1); 1608 hfp_connection->remote_call_services[hfp_connection->remote_call_services_index].name[HFP_CALL_SERVICE_SIZE - 1] = 0; 1609 hfp_next_remote_call_services_index(hfp_connection); 1610 break; 1611 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 1612 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 1613 log_info("Parsed Generic status indicator: %s\n", hfp_connection->line_buffer); 1614 hfp_connection->generic_status_indicators[hfp_connection->parser_item_index].uuid = (uint16_t)btstack_atoi((char*)hfp_connection->line_buffer); 1615 hfp_next_indicators_index(hfp_connection); 1616 hfp_connection->generic_status_indicators_nr = hfp_connection->parser_item_index; 1617 break; 1618 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 1619 // HF parses inital AG gen. ind. state 1620 log_info("Parsed List generic status indicator %s state: ", hfp_connection->line_buffer); 1621 hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection); 1622 break; 1623 case HFP_CMD_HF_INDICATOR_STATUS: 1624 hfp_connection->parser_indicator_index = hfp_parse_indicator_index(hfp_connection); 1625 log_info("Parsed HF indicator index %u", hfp_connection->parser_indicator_index); 1626 break; 1627 case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 1628 // AG parses new gen. ind. state 1629 if (hfp_connection->ignore_value){ 1630 hfp_connection->ignore_value = 0; 1631 log_info("Parsed Enable AG indicator pos %u('%s') - unchanged (stays %u)\n", hfp_connection->parser_item_index, 1632 hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled); 1633 } 1634 else if (hfp_connection->ag_indicators[hfp_connection->parser_item_index].mandatory){ 1635 log_info("Parsed Enable AG indicator pos %u('%s') - ignore (mandatory)\n", 1636 hfp_connection->parser_item_index, hfp_connection->ag_indicators[hfp_connection->parser_item_index].name); 1637 } else { 1638 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1639 hfp_connection->ag_indicators[hfp_connection->parser_item_index].enabled = value; 1640 log_info("Parsed Enable AG indicator pos %u('%s'): %u\n", hfp_connection->parser_item_index, 1641 hfp_connection->ag_indicators[hfp_connection->parser_item_index].name, value); 1642 } 1643 hfp_next_indicators_index(hfp_connection); 1644 break; 1645 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1646 // indicators are indexed starting with 1 1647 hfp_connection->parser_item_index = hfp_parse_indicator_index(hfp_connection); 1648 log_info("Parsed status of the AG indicator %d, status ", hfp_connection->parser_item_index); 1649 break; 1650 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1651 hfp_connection->network_operator.mode = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1652 log_info("Parsed network operator mode: %d, ", hfp_connection->network_operator.mode); 1653 break; 1654 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1655 if (hfp_connection->line_buffer[0] == '3'){ 1656 log_info("Parsed Set network operator format : %s, ", hfp_connection->line_buffer); 1657 break; 1658 } 1659 // TODO emit ERROR, wrong format 1660 log_info("ERROR Set network operator format: index %s not supported\n", hfp_connection->line_buffer); 1661 break; 1662 case HFP_CMD_ERROR: 1663 break; 1664 case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1665 hfp_connection->extended_audio_gateway_error = 1; 1666 hfp_connection->extended_audio_gateway_error_value = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1667 break; 1668 case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 1669 hfp_connection->enable_extended_audio_gateway_error_report = (uint8_t)btstack_atoi((char*)hfp_connection->line_buffer); 1670 hfp_connection->ok_pending = 1; 1671 hfp_connection->extended_audio_gateway_error = 0; 1672 break; 1673 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1674 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1675 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1676 btstack_strcpy((char *)hfp_connection->bnip_number, sizeof(hfp_connection->bnip_number), (char *)hfp_connection->line_buffer); 1677 break; 1678 case HFP_CMD_CALL_HOLD: 1679 hfp_connection->ag_call_hold_action = hfp_connection->line_buffer[0] - '0'; 1680 if (hfp_connection->line_buffer[1] != '\0'){ 1681 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]); 1682 } 1683 break; 1684 case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 1685 hfp_connection->ag_response_and_hold_action = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1686 break; 1687 case HFP_CMD_TRANSMIT_DTMF_CODES: 1688 hfp_connection->ag_dtmf_code = hfp_connection->line_buffer[0]; 1689 break; 1690 case HFP_CMD_ENABLE_CLIP: 1691 hfp_connection->clip_enabled = hfp_connection->line_buffer[0] != '0'; 1692 break; 1693 case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 1694 hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[0] != '0'; 1695 break; 1696 case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 1697 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1698 hfp_connection->ag_activate_voice_recognition_value = value; 1699 break; 1700 case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 1701 switch(hfp_connection->parser_item_index){ 1702 case 0: 1703 hfp_connection->ag_vra_status = btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1704 break; 1705 case 1: 1706 hfp_connection->ag_vra_state = (hfp_voice_recognition_state_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1707 break; 1708 case 2: 1709 hfp_connection->ag_msg.text_id = 0; 1710 for (i = 0 ; i < 4; i++){ 1711 hfp_connection->ag_msg.text_id = (hfp_connection->ag_msg.text_id << 4) | nibble_for_char(hfp_connection->line_buffer[i]); 1712 } 1713 break; 1714 case 3: 1715 hfp_connection->ag_msg.text_type = (hfp_text_type_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1716 break; 1717 case 4: 1718 hfp_connection->ag_msg.text_operation = (hfp_text_operation_t) btstack_atoi((char *)&hfp_connection->line_buffer[0]); 1719 break; 1720 case 5: 1721 hfp_connection->line_buffer[hfp_connection->line_size] = 0; 1722 hfp_connection->ag_vra_msg_length = hfp_connection->line_size + 1; 1723 break; 1724 default: 1725 break; 1726 } 1727 hfp_connection->parser_item_index++; 1728 break; 1729 default: 1730 break; 1731 } 1732 } 1733 1734 static void hfp_handle_start_sdp_client_query(void * context){ 1735 UNUSED(context); 1736 1737 btstack_linked_list_iterator_t it; 1738 btstack_linked_list_iterator_init(&it, &hfp_connections); 1739 while (btstack_linked_list_iterator_has_next(&it)){ 1740 hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1741 1742 if (connection->state != HFP_W2_SEND_SDP_QUERY) continue; 1743 1744 connection->state = HFP_W4_SDP_QUERY_COMPLETE; 1745 hfp_sdp_query_context.local_role = connection->local_role; 1746 (void)memcpy(hfp_sdp_query_context.remote_address, connection->remote_addr, 6); 1747 sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, connection->remote_addr, connection->service_uuid); 1748 return; 1749 } 1750 } 1751 1752 uint8_t hfp_establish_service_level_connection(bd_addr_t bd_addr, uint16_t service_uuid, hfp_role_t local_role){ 1753 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr, local_role); 1754 if (connection){ 1755 return ERROR_CODE_COMMAND_DISALLOWED; 1756 } 1757 1758 connection = hfp_create_connection(bd_addr, local_role); 1759 if (!connection){ 1760 return BTSTACK_MEMORY_ALLOC_FAILED; 1761 } 1762 1763 connection->state = HFP_W2_SEND_SDP_QUERY; 1764 1765 bd_addr_copy(connection->remote_addr, bd_addr); 1766 connection->service_uuid = service_uuid; 1767 1768 hfp_sdp_query_request.callback = &hfp_handle_start_sdp_client_query; 1769 // ignore ERROR_CODE_COMMAND_DISALLOWED because in that case, we already have requested an SDP callback 1770 (void) sdp_client_register_query_callback(&hfp_sdp_query_request); 1771 return ERROR_CODE_SUCCESS; 1772 } 1773 1774 void hfp_trigger_release_service_level_connection(hfp_connection_t * hfp_connection){ 1775 // called internally, NULL check already performed 1776 btstack_assert(hfp_connection != NULL); 1777 1778 hfp_trigger_release_audio_connection(hfp_connection); 1779 if (hfp_connection->state < HFP_W4_RFCOMM_CONNECTED){ 1780 hfp_connection->state = HFP_IDLE; 1781 return; 1782 } 1783 1784 if (hfp_connection->state == HFP_W4_RFCOMM_CONNECTED){ 1785 hfp_connection->state = HFP_W4_CONNECTION_ESTABLISHED_TO_SHUTDOWN; 1786 return; 1787 } 1788 hfp_connection->release_slc_connection = 1; 1789 if (hfp_connection->state < HFP_W4_SCO_CONNECTED){ 1790 hfp_connection->state = HFP_W2_DISCONNECT_RFCOMM; 1791 return; 1792 } 1793 1794 if (hfp_connection->state < HFP_W4_SCO_DISCONNECTED){ 1795 hfp_connection->state = HFP_W2_DISCONNECT_SCO; 1796 hfp_connection->release_audio_connection = 1; 1797 return; 1798 } 1799 } 1800 1801 uint8_t hfp_trigger_release_audio_connection(hfp_connection_t * hfp_connection){ 1802 // called internally, NULL check already performed 1803 btstack_assert(hfp_connection != NULL); 1804 if (hfp_connection->state <= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 1805 return ERROR_CODE_COMMAND_DISALLOWED; 1806 } 1807 switch (hfp_connection->state) { 1808 case HFP_W2_CONNECT_SCO: 1809 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 1810 break; 1811 case HFP_W4_SCO_CONNECTED: 1812 case HFP_AUDIO_CONNECTION_ESTABLISHED: 1813 hfp_connection->release_audio_connection = 1; 1814 break; 1815 default: 1816 return ERROR_CODE_COMMAND_DISALLOWED; 1817 } 1818 return ERROR_CODE_SUCCESS; 1819 } 1820 1821 static const struct link_settings { 1822 const uint16_t max_latency; 1823 const uint8_t retransmission_effort; 1824 const uint16_t packet_types; 1825 const bool eSCO; 1826 const uint8_t codec; 1827 } hfp_link_settings [] = { 1828 { 0xffff, 0xff, SCO_PACKET_TYPES_HV1, false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D0 1829 { 0xffff, 0xff, SCO_PACKET_TYPES_HV3, false, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_D1 1830 { 0x0007, 0x01, SCO_PACKET_TYPES_EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S1 1831 { 0x0007, 0x01, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S2 1832 { 0x000a, 0x01, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S3 1833 { 0x000c, 0x02, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_CVSD }, // HFP_LINK_SETTINGS_S4 1834 { 0x0008, 0x02, SCO_PACKET_TYPES_EV3, true, HFP_CODEC_MSBC }, // HFP_LINK_SETTINGS_T1 1835 { 0x000d, 0x02, SCO_PACKET_TYPES_2EV3, true, HFP_CODEC_MSBC } // HFP_LINK_SETTINGS_T2 1836 }; 1837 1838 void hfp_setup_synchronous_connection(hfp_connection_t * hfp_connection){ 1839 // all packet types, fixed bandwidth 1840 int setting = hfp_connection->link_setting; 1841 log_info("hfp_setup_synchronous_connection using setting nr %u", setting); 1842 hfp_sco_establishment_active = hfp_connection; 1843 uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1844 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1845 #ifdef ENABLE_BCM_PCM_WBS 1846 sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers 1847 #else 1848 sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise 1849 #endif 1850 } 1851 // get packet types - bits 6-9 are 'don't allow' 1852 uint16_t packet_types = hfp_link_settings[setting].packet_types ^ 0x03c0; 1853 hci_send_cmd(&hci_setup_synchronous_connection, hfp_connection->acl_handle, 8000, 8000, hfp_link_settings[setting].max_latency, 1854 sco_voice_setting, hfp_link_settings[setting].retransmission_effort, packet_types); 1855 } 1856 1857 void hfp_accept_synchronous_connection(hfp_connection_t * hfp_connection, bool incoming_eSCO){ 1858 1859 // remote supported feature eSCO is set if link type is eSCO 1860 // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 1861 uint16_t max_latency; 1862 uint8_t retransmission_effort; 1863 uint16_t packet_types; 1864 1865 if (incoming_eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 1866 max_latency = 0x000c; 1867 retransmission_effort = 0x02; 1868 // eSCO: EV3 and 2-EV3 1869 packet_types = 0x0048; 1870 } else { 1871 max_latency = 0xffff; 1872 retransmission_effort = 0xff; 1873 // sco: HV1 and HV3 1874 packet_types = 0x005; 1875 } 1876 1877 // mSBC only allows for transparent data 1878 uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 1879 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1880 #ifdef ENABLE_BCM_PCM_WBS 1881 sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers 1882 #else 1883 sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise 1884 #endif 1885 } 1886 1887 // filter packet types 1888 packet_types &= hfp_get_sco_packet_types(); 1889 1890 // bits 6-9 are 'don't allow' 1891 packet_types ^= 0x3c0; 1892 1893 log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting); 1894 hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 1895 sco_voice_setting, retransmission_effort, packet_types); 1896 } 1897 1898 #ifdef ENABLE_CC256X_ASSISTED_HFP 1899 void hfp_cc256x_prepare_for_sco(hfp_connection_t * hfp_connection){ 1900 hfp_connection->cc256x_send_write_codec_config = true; 1901 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1902 hfp_connection->cc256x_send_wbs_associate = true; 1903 } 1904 } 1905 1906 void hfp_cc256x_write_codec_config(hfp_connection_t * hfp_connection){ 1907 uint32_t sample_rate_hz; 1908 uint16_t clock_rate_khz; 1909 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1910 clock_rate_khz = 512; 1911 sample_rate_hz = 16000; 1912 } else { 1913 clock_rate_khz = 256; 1914 sample_rate_hz = 8000; 1915 } 1916 uint8_t clock_direction = 0; // master 1917 uint16_t frame_sync_duty_cycle = 0; // i2s with 50% 1918 uint8_t frame_sync_edge = 1; // rising edge 1919 uint8_t frame_sync_polarity = 0; // active high 1920 uint8_t reserved = 0; 1921 uint16_t size = 16; 1922 uint16_t chan_1_offset = 1; 1923 uint16_t chan_2_offset = chan_1_offset + size; 1924 uint8_t out_edge = 1; // rising 1925 uint8_t in_edge = 0; // falling 1926 hci_send_cmd(&hci_ti_write_codec_config, clock_rate_khz, clock_direction, sample_rate_hz, frame_sync_duty_cycle, 1927 frame_sync_edge, frame_sync_polarity, reserved, 1928 size, chan_1_offset, out_edge, size, chan_1_offset, in_edge, reserved, 1929 size, chan_2_offset, out_edge, size, chan_2_offset, in_edge, reserved); 1930 } 1931 #endif 1932 1933 #ifdef ENABLE_BCM_PCM_WBS 1934 void hfp_bcm_prepare_for_sco(hfp_connection_t * hfp_connection){ 1935 hfp_connection->bcm_send_write_i2spcm_interface_param = true; 1936 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 1937 hfp_connection->bcm_send_enable_wbs = true; 1938 } 1939 } 1940 void hfp_bcm_write_i2spcm_interface_param(hfp_connection_t * hfp_connection){ 1941 uint8_t sample_rate = (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? 1 : 0; 1942 // i2s enable, master, 8/16 kHz, 512 kHz 1943 hci_send_cmd(&hci_bcm_write_i2spcm_interface_paramhci_bcm_write_i2spcm_interface_param, 1, 1, sample_rate, 2); 1944 } 1945 #endif 1946 1947 void hfp_set_hf_callback(btstack_packet_handler_t callback){ 1948 hfp_hf_callback = callback; 1949 } 1950 1951 void hfp_set_ag_callback(btstack_packet_handler_t callback){ 1952 hfp_ag_callback = callback; 1953 } 1954 1955 void hfp_set_ag_rfcomm_packet_handler(btstack_packet_handler_t handler){ 1956 hfp_ag_rfcomm_packet_handler = handler; 1957 } 1958 1959 void hfp_set_hf_rfcomm_packet_handler(btstack_packet_handler_t handler){ 1960 hfp_hf_rfcomm_packet_handler = handler; 1961 } 1962 1963 void hfp_init(void){ 1964 hfp_allowed_sco_packet_types = SCO_PACKET_TYPES_ALL; 1965 } 1966 1967 void hfp_deinit(void){ 1968 hfp_allowed_sco_packet_types = 0; 1969 hfp_connections = NULL; 1970 hfp_hf_callback = NULL; 1971 hfp_ag_callback = NULL; 1972 hfp_hf_rfcomm_packet_handler = NULL; 1973 hfp_ag_rfcomm_packet_handler = NULL; 1974 hfp_sco_establishment_active = NULL; 1975 (void) memset(&hfp_sdp_query_context, 0, sizeof(hfp_sdp_query_context_t)); 1976 (void) memset(&hfp_sdp_query_request, 0, sizeof(btstack_context_callback_registration_t)); 1977 } 1978 1979 void hfp_set_sco_packet_types(uint16_t packet_types){ 1980 hfp_allowed_sco_packet_types = packet_types; 1981 } 1982 1983 uint16_t hfp_get_sco_packet_types(void){ 1984 return hfp_allowed_sco_packet_types; 1985 } 1986 1987 hfp_link_settings_t hfp_next_link_setting(hfp_link_settings_t current_setting, bool local_eSCO_supported, bool remote_eSCO_supported, bool eSCO_S4_supported, uint8_t negotiated_codec){ 1988 int8_t setting = (int8_t) current_setting; 1989 bool can_use_eSCO = local_eSCO_supported && remote_eSCO_supported; 1990 while (setting > 0){ 1991 setting--; 1992 // skip if eSCO required but not available 1993 if (hfp_link_settings[setting].eSCO && !can_use_eSCO) continue; 1994 // skip if S4 but not supported 1995 if ((setting == (int8_t) HFP_LINK_SETTINGS_S4) && !eSCO_S4_supported) continue; 1996 // skip wrong codec 1997 if ( hfp_link_settings[setting].codec != negotiated_codec) continue; 1998 // skip disabled packet types 1999 uint16_t required_packet_types = hfp_link_settings[setting].packet_types; 2000 uint16_t allowed_packet_types = hfp_allowed_sco_packet_types; 2001 if ((required_packet_types & allowed_packet_types) == 0) continue; 2002 2003 // found matching setting 2004 return (hfp_link_settings_t) setting; 2005 } 2006 return HFP_LINK_SETTINGS_NONE; 2007 } 2008 2009 static hfp_link_settings_t hfp_next_link_setting_for_connection(hfp_link_settings_t current_setting, hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){ 2010 bool local_eSCO_supported = hci_extended_sco_link_supported(); 2011 bool remote_eSCO_supported = hci_remote_esco_supported(hfp_connection->acl_handle); 2012 uint8_t negotiated_codec = hfp_connection->negotiated_codec; 2013 return hfp_next_link_setting(current_setting, local_eSCO_supported, remote_eSCO_supported, eSCO_S4_supported, negotiated_codec); 2014 } 2015 2016 void hfp_init_link_settings(hfp_connection_t * hfp_connection, uint8_t eSCO_S4_supported){ 2017 // get highest possible link setting 2018 hfp_connection->link_setting = hfp_next_link_setting_for_connection(HFP_LINK_SETTINGS_NONE, hfp_connection, eSCO_S4_supported); 2019 log_info("hfp_init_link_settings: %u", hfp_connection->link_setting); 2020 } 2021 2022 #define HFP_HF_RX_DEBUG_PRINT_LINE 80 2023 2024 void hfp_log_rfcomm_message(const char * tag, uint8_t * packet, uint16_t size){ 2025 #ifdef ENABLE_LOG_INFO 2026 // encode \n\r 2027 char printable[HFP_HF_RX_DEBUG_PRINT_LINE+2]; 2028 int i = 0; 2029 int pos; 2030 for (pos=0 ; (pos < size) && (i < (HFP_HF_RX_DEBUG_PRINT_LINE - 3)) ; pos++){ 2031 switch (packet[pos]){ 2032 case '\n': 2033 printable[i++] = '\\'; 2034 printable[i++] = 'n'; 2035 break; 2036 case '\r': 2037 printable[i++] = '\\'; 2038 printable[i++] = 'r'; 2039 break; 2040 default: 2041 printable[i++] = packet[pos]; 2042 break; 2043 } 2044 } 2045 printable[i] = 0; 2046 log_info("%s: '%s'", tag, printable); 2047 #endif 2048 } 2049