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 #define BTSTACK_FILE__ "hfp_hf.c" 39 40 // ***************************************************************************** 41 // 42 // HFP Hands-Free (HF) unit 43 // 44 // ***************************************************************************** 45 46 #include "btstack_config.h" 47 48 #include <stdint.h> 49 #include <string.h> 50 51 #include "bluetooth_sdp.h" 52 #include "btstack_debug.h" 53 #include "btstack_event.h" 54 #include "btstack_memory.h" 55 #include "btstack_run_loop.h" 56 #include "classic/core.h" 57 #include "classic/hfp.h" 58 #include "classic/hfp_hf.h" 59 #include "classic/sdp_client_rfcomm.h" 60 #include "classic/sdp_server.h" 61 #include "classic/sdp_util.h" 62 #include "hci.h" 63 #include "hci_cmd.h" 64 #include "hci_dump.h" 65 #include "l2cap.h" 66 67 // const 68 static const char default_hfp_hf_service_name[] = "Hands-Free unit"; 69 70 // globals 71 static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration; 72 73 static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 74 static uint8_t hfp_codecs_nr; 75 static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 76 77 static uint8_t hfp_indicators_nr; 78 static uint8_t hfp_indicators[HFP_MAX_NUM_INDICATORS]; 79 static uint32_t hfp_indicators_value[HFP_MAX_NUM_INDICATORS]; 80 81 static uint8_t hfp_hf_speaker_gain; 82 static uint8_t hfp_hf_microphone_gain; 83 84 static btstack_packet_handler_t hfp_hf_callback; 85 86 static hfp_call_status_t hfp_call_status; 87 static hfp_callsetup_status_t hfp_callsetup_status; 88 static hfp_callheld_status_t hfp_callheld_status; 89 90 static char phone_number[25]; 91 92 static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 93 int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 94 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 95 return hf && ag; 96 } 97 98 static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 99 int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING); 100 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING); 101 return hf && ag; 102 } 103 104 105 static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 106 int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS); 107 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS); 108 return hf && ag; 109 } 110 111 112 static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){ 113 btstack_linked_list_iterator_t it; 114 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 115 while (btstack_linked_list_iterator_has_next(&it)){ 116 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 117 if (hfp_connection->acl_handle != handle) continue; 118 if (hfp_connection->local_role != HFP_ROLE_HF) continue; 119 return hfp_connection; 120 } 121 return NULL; 122 } 123 124 /* emit functinos */ 125 126 static void hfp_hf_emit_subscriber_information(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t status, uint8_t bnip_type, const char * bnip_number){ 127 if (!callback) return; 128 uint8_t event[31]; 129 event[0] = HCI_EVENT_HFP_META; 130 event[1] = sizeof(event) - 2; 131 event[2] = event_subtype; 132 event[3] = status; 133 event[4] = bnip_type; 134 uint16_t size = btstack_min(strlen(bnip_number), sizeof(event) - 6); 135 strncpy((char*)&event[5], bnip_number, size); 136 event[5 + size] = 0; 137 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 138 } 139 140 static void hfp_hf_emit_type_and_number(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t bnip_type, const char * bnip_number){ 141 if (!callback) return; 142 uint8_t event[30]; 143 event[0] = HCI_EVENT_HFP_META; 144 event[1] = sizeof(event) - 2; 145 event[2] = event_subtype; 146 event[3] = bnip_type; 147 uint16_t size = btstack_min(strlen(bnip_number), sizeof(event) - 5); 148 strncpy((char*)&event[4], bnip_number, size); 149 event[4 + size] = 0; 150 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 151 } 152 153 static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, hfp_connection_t * connection){ 154 if (!callback) return; 155 uint8_t event[36]; 156 int pos = 0; 157 event[pos++] = HCI_EVENT_HFP_META; 158 event[pos++] = sizeof(event) - 2; 159 event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 160 event[pos++] = connection->clcc_idx; 161 event[pos++] = connection->clcc_dir; 162 event[pos++] = connection->clcc_status; 163 event[pos++] = connection->clcc_mode; 164 event[pos++] = connection->clcc_mpty; 165 event[pos++] = connection->bnip_type; 166 uint16_t size = btstack_min(strlen(connection->bnip_number), sizeof(event) - pos); 167 strncpy((char*)&event[pos], connection->bnip_number, size); 168 pos += size; 169 event[pos++] = 0; 170 (*callback)(HCI_EVENT_PACKET, 0, event, pos); 171 } 172 173 174 static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){ 175 if (!callback) return; 176 uint8_t event[10+HFP_MAX_INDICATOR_DESC_SIZE+1]; 177 int pos = 0; 178 event[pos++] = HCI_EVENT_HFP_META; 179 event[pos++] = sizeof(event) - 2; 180 event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 181 event[pos++] = indicator.index; 182 event[pos++] = indicator.status; 183 event[pos++] = indicator.min_range; 184 event[pos++] = indicator.max_range; 185 event[pos++] = indicator.mandatory; 186 event[pos++] = indicator.enabled; 187 event[pos++] = indicator.status_changed; 188 strncpy((char*)&event[pos], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE); 189 pos += HFP_MAX_INDICATOR_DESC_SIZE; 190 event[pos] = 0; 191 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 192 } 193 194 static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){ 195 if (!callback) return; 196 uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1]; 197 event[0] = HCI_EVENT_HFP_META; 198 event[1] = sizeof(event) - 2; 199 event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED; 200 event[3] = network_operator.mode; 201 event[4] = network_operator.format; 202 strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE); 203 event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0; 204 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 205 } 206 207 /* send commands */ 208 209 static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){ 210 char buffer[20]; 211 snprintf(buffer, sizeof(buffer), "AT%s\r\n", cmd); 212 return send_str_over_rfcomm(cid, buffer); 213 } 214 215 static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){ 216 char buffer[20]; 217 snprintf(buffer, sizeof(buffer), "AT%s%s\r\n", cmd, mark); 218 return send_str_over_rfcomm(cid, buffer); 219 } 220 221 static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){ 222 char buffer[40]; 223 snprintf(buffer, sizeof(buffer), "AT%s=%d\r\n", cmd, value); 224 return send_str_over_rfcomm(cid, buffer); 225 } 226 227 static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 228 char buffer[30]; 229 const int size = sizeof(buffer); 230 int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS); 231 offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr); 232 offset += snprintf(buffer+offset, size-offset, "\r\n"); 233 return send_str_over_rfcomm(cid, buffer); 234 } 235 236 static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 237 char buffer[50]; 238 const int size = sizeof(buffer); 239 int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 240 offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr); 241 offset += snprintf(buffer+offset, size-offset, "\r\n"); 242 return send_str_over_rfcomm(cid, buffer); 243 } 244 245 static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 246 char buffer[30]; 247 const int size = sizeof(buffer); 248 int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 249 offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr); 250 offset += snprintf(buffer+offset, size-offset, "\r\n"); 251 return send_str_over_rfcomm(cid, buffer); 252 } 253 254 static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 255 char buffer[20]; 256 snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 257 return send_str_over_rfcomm(cid, buffer); 258 } 259 260 static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 261 char buffer[40]; 262 snprintf(buffer, sizeof(buffer), "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number); 263 return send_str_over_rfcomm(cid, buffer); 264 } 265 266 static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){ 267 char buffer[40]; 268 snprintf(buffer, sizeof(buffer), "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id); 269 return send_str_over_rfcomm(cid, buffer); 270 } 271 272 static int hfp_hf_send_chld(uint16_t cid, unsigned int number){ 273 char buffer[40]; 274 snprintf(buffer, sizeof(buffer), "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 275 return send_str_over_rfcomm(cid, buffer); 276 } 277 278 static int hfp_hf_send_dtmf(uint16_t cid, char code){ 279 char buffer[20]; 280 snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code); 281 return send_str_over_rfcomm(cid, buffer); 282 } 283 284 static int hfp_hf_cmd_ata(uint16_t cid){ 285 return send_str_over_rfcomm(cid, (char *) "ATA\r\n"); 286 } 287 288 static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 289 return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features); 290 } 291 292 static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){ 293 return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?"); 294 } 295 296 static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){ 297 return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?"); 298 } 299 300 static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 301 return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?"); 302 } 303 304 static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){ 305 return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?"); 306 } 307 308 static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){ 309 return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?"); 310 } 311 312 static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 313 return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0"); 314 } 315 316 static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 317 return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?"); 318 } 319 320 static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 321 return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP); 322 } 323 324 static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 325 return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 326 } 327 328 static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 329 return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 330 } 331 332 static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 333 return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate); 334 } 335 336 static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){ 337 return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate); 338 } 339 340 static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 341 return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate); 342 } 343 344 static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 345 return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 346 } 347 348 static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 349 return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 350 } 351 352 static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 353 return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 354 } 355 356 static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 357 return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER); 358 } 359 360 static int hfp_hf_send_chup(uint16_t cid){ 361 return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL); 362 } 363 364 static int hfp_hf_send_binp(uint16_t cid){ 365 return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1"); 366 } 367 368 static int hfp_hf_send_clcc(uint16_t cid){ 369 return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS); 370 } 371 372 /* state machines */ 373 374 static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 375 if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 376 if (hfp_connection->ok_pending) return 0; 377 int done = 1; 378 379 switch (hfp_connection->state){ 380 case HFP_EXCHANGE_SUPPORTED_FEATURES: 381 hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr); 382 hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 383 hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 384 break; 385 case HFP_NOTIFY_ON_CODECS: 386 hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 387 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 388 break; 389 case HFP_RETRIEVE_INDICATORS: 390 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 391 hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 392 break; 393 case HFP_RETRIEVE_INDICATORS_STATUS: 394 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 395 hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 396 break; 397 case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 398 hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 399 hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 400 break; 401 case HFP_RETRIEVE_CAN_HOLD_CALL: 402 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 403 hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 404 break; 405 case HFP_LIST_GENERIC_STATUS_INDICATORS: 406 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 407 hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 408 break; 409 case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 410 hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 411 hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 412 break; 413 case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 414 hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 415 hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 416 break; 417 default: 418 done = 0; 419 break; 420 } 421 return done; 422 } 423 424 425 static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 426 if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 427 if (hfp_connection->ok_pending) return 0; 428 429 int done = 0; 430 if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 431 hfp_connection->ok_pending = 1; 432 done = 1; 433 hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 434 return done; 435 }; 436 if (hfp_connection->change_status_update_for_individual_ag_indicators){ 437 hfp_connection->ok_pending = 1; 438 done = 1; 439 hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 440 hfp_connection->ag_indicators_status_update_bitmap, 441 hfp_connection->ag_indicators_nr); 442 return done; 443 } 444 445 switch (hfp_connection->hf_query_operator_state){ 446 case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 447 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 448 hfp_connection->ok_pending = 1; 449 hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 450 return 1; 451 case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 452 hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 453 hfp_connection->ok_pending = 1; 454 hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 455 return 1; 456 default: 457 break; 458 } 459 460 if (hfp_connection->enable_extended_audio_gateway_error_report){ 461 hfp_connection->ok_pending = 1; 462 done = 1; 463 hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 464 return done; 465 } 466 467 return done; 468 } 469 470 static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 471 472 if (hfp_connection->ok_pending) return 0; 473 474 if (hfp_connection->trigger_codec_exchange){ 475 hfp_connection->trigger_codec_exchange = 0; 476 477 hfp_connection->ok_pending = 1; 478 hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 479 return 1; 480 } 481 482 if (hfp_connection->hf_send_codec_confirm){ 483 hfp_connection->hf_send_codec_confirm = false; 484 485 hfp_connection->ok_pending = 1; 486 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 487 return 1; 488 } 489 490 if (hfp_connection->hf_send_supported_codecs){ 491 hfp_connection->hf_send_supported_codecs = false; 492 493 hfp_connection->ok_pending = 1; 494 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 495 return 1; 496 } 497 498 return 0; 499 } 500 501 static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 502 if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 503 (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 504 505 if (hfp_connection->release_audio_connection){ 506 hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 507 hfp_connection->release_audio_connection = 0; 508 gap_disconnect(hfp_connection->sco_handle); 509 return 1; 510 } 511 512 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 513 514 // run codecs exchange 515 int done = codecs_exchange_state_machine(hfp_connection); 516 if (done) return 1; 517 518 if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 519 if (hfp_connection->establish_audio_connection){ 520 hfp_connection->state = HFP_W4_SCO_CONNECTED; 521 hfp_connection->establish_audio_connection = 0; 522 hfp_setup_synchronous_connection(hfp_connection); 523 return 1; 524 } 525 526 return 0; 527 } 528 529 530 static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 531 532 if (hfp_connection->ok_pending) return 0; 533 534 if (hfp_connection->hf_answer_incoming_call){ 535 hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 536 hfp_connection->hf_answer_incoming_call = 0; 537 return 1; 538 } 539 return 0; 540 } 541 542 static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){ 543 544 btstack_assert(hfp_connection != NULL); 545 btstack_assert(hfp_connection->local_role == HFP_ROLE_HF); 546 547 // during SDP query, RFCOMM CID is not set 548 if (hfp_connection->rfcomm_cid == 0) return; 549 550 // assert command could be sent 551 if (hci_can_send_command_packet_now() == 0) return; 552 553 #ifdef ENABLE_CC256X_ASSISTED_HFP 554 // WBS Disassociate 555 if (hfp_connection->cc256x_send_wbs_disassociate){ 556 hfp_connection->cc256x_send_wbs_disassociate = false; 557 hci_send_cmd(&hci_ti_wbs_disassociate); 558 return; 559 } 560 // Write Codec Config 561 if (hfp_connection->cc256x_send_write_codec_config){ 562 hfp_connection->cc256x_send_write_codec_config = false; 563 hfp_cc256x_write_codec_config(hfp_connection); 564 return; 565 } 566 // WBS Associate 567 if (hfp_connection->cc256x_send_wbs_associate){ 568 hfp_connection->cc256x_send_wbs_associate = false; 569 hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 570 return; 571 } 572 #endif 573 #ifdef ENABLE_BCM_PCM_WBS 574 // Enable WBS 575 if (hfp_connection->bcm_send_enable_wbs){ 576 hfp_connection->bcm_send_enable_wbs = false; 577 hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 578 return; 579 } 580 // Write I2S/PCM params 581 if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 582 hfp_connection->bcm_send_write_i2spcm_interface_param = false; 583 hfp_bcm_write_i2spcm_interface_param(hfp_connection); 584 return; 585 } 586 // Disable WBS 587 if (hfp_connection->bcm_send_disable_wbs){ 588 hfp_connection->bcm_send_disable_wbs = false; 589 hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 590 return; 591 } 592 #endif 593 #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 594 if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 595 hfp_finalize_connection_context(hfp_connection); 596 return; 597 } 598 #endif 599 600 if (hfp_connection->hf_accept_sco){ 601 602 bool eSCO = hfp_connection->hf_accept_sco == 2; 603 hfp_connection->hf_accept_sco = 0; 604 605 // notify about codec selection if not done already 606 if (hfp_connection->negotiated_codec == 0){ 607 hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 608 } 609 610 // remote supported feature eSCO is set if link type is eSCO 611 // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 612 uint16_t max_latency; 613 uint8_t retransmission_effort; 614 uint16_t packet_types; 615 616 if (eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 617 max_latency = 0x000c; 618 retransmission_effort = 0x02; 619 // eSCO: EV3 and 2-EV3 620 packet_types = 0x0048; 621 } else { 622 max_latency = 0xffff; 623 retransmission_effort = 0xff; 624 // sco: HV1 and HV3 625 packet_types = 0x005; 626 } 627 628 // mSBC only allows for transparent data 629 uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 630 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 631 #ifdef ENABLE_BCM_PCM_WBS 632 sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers 633 #else 634 sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise 635 #endif 636 } 637 638 // filter packet types 639 packet_types &= hfp_get_sco_packet_types(); 640 641 // bits 6-9 are 'don't allow' 642 packet_types ^= 0x3c0; 643 644 log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting); 645 hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 646 sco_voice_setting, retransmission_effort, packet_types); 647 return; 648 } 649 650 if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 651 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 652 return; 653 } 654 int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 655 if (!done){ 656 done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 657 } 658 if (!done){ 659 done = hfp_hf_run_for_audio_connection(hfp_connection); 660 } 661 if (!done){ 662 done = call_setup_state_machine(hfp_connection); 663 } 664 665 // don't send a new command while ok still pending 666 if (hfp_connection->ok_pending) return; 667 668 if (hfp_connection->send_microphone_gain){ 669 hfp_connection->send_microphone_gain = 0; 670 hfp_connection->ok_pending = 1; 671 hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 672 return; 673 } 674 675 if (hfp_connection->send_speaker_gain){ 676 hfp_connection->send_speaker_gain = 0; 677 hfp_connection->ok_pending = 1; 678 hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 679 return; 680 } 681 682 if (hfp_connection->hf_deactivate_calling_line_notification){ 683 hfp_connection->hf_deactivate_calling_line_notification = 0; 684 hfp_connection->ok_pending = 1; 685 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 686 return; 687 } 688 689 if (hfp_connection->hf_activate_calling_line_notification){ 690 hfp_connection->hf_activate_calling_line_notification = 0; 691 hfp_connection->ok_pending = 1; 692 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 693 return; 694 } 695 696 if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 697 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 698 hfp_connection->ok_pending = 1; 699 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0); 700 return; 701 } 702 703 if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){ 704 hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0; 705 hfp_connection->ok_pending = 1; 706 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1); 707 return; 708 } 709 710 if (hfp_connection->hf_deactivate_voice_recognition_notification){ 711 hfp_connection->hf_deactivate_voice_recognition_notification = 0; 712 hfp_connection->ok_pending = 1; 713 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 714 return; 715 } 716 717 if (hfp_connection->hf_activate_voice_recognition_notification){ 718 hfp_connection->hf_activate_voice_recognition_notification = 0; 719 hfp_connection->ok_pending = 1; 720 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 721 return; 722 } 723 724 725 if (hfp_connection->hf_deactivate_call_waiting_notification){ 726 hfp_connection->hf_deactivate_call_waiting_notification = 0; 727 hfp_connection->ok_pending = 1; 728 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 729 return; 730 } 731 732 if (hfp_connection->hf_activate_call_waiting_notification){ 733 hfp_connection->hf_activate_call_waiting_notification = 0; 734 hfp_connection->ok_pending = 1; 735 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 736 return; 737 } 738 739 if (hfp_connection->hf_initiate_outgoing_call){ 740 hfp_connection->hf_initiate_outgoing_call = 0; 741 hfp_connection->ok_pending = 1; 742 hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 743 return; 744 } 745 746 if (hfp_connection->hf_initiate_memory_dialing){ 747 hfp_connection->hf_initiate_memory_dialing = 0; 748 hfp_connection->ok_pending = 1; 749 hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 750 return; 751 } 752 753 if (hfp_connection->hf_initiate_redial_last_number){ 754 hfp_connection->hf_initiate_redial_last_number = 0; 755 hfp_connection->ok_pending = 1; 756 hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 757 return; 758 } 759 760 if (hfp_connection->hf_send_chup){ 761 hfp_connection->hf_send_chup = 0; 762 hfp_connection->ok_pending = 1; 763 hfp_hf_send_chup(hfp_connection->rfcomm_cid); 764 return; 765 } 766 767 if (hfp_connection->hf_send_chld_0){ 768 hfp_connection->hf_send_chld_0 = 0; 769 hfp_connection->ok_pending = 1; 770 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 771 return; 772 } 773 774 if (hfp_connection->hf_send_chld_1){ 775 hfp_connection->hf_send_chld_1 = 0; 776 hfp_connection->ok_pending = 1; 777 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 778 return; 779 } 780 781 if (hfp_connection->hf_send_chld_2){ 782 hfp_connection->hf_send_chld_2 = 0; 783 hfp_connection->ok_pending = 1; 784 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 785 return; 786 } 787 788 if (hfp_connection->hf_send_chld_3){ 789 hfp_connection->hf_send_chld_3 = 0; 790 hfp_connection->ok_pending = 1; 791 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 792 return; 793 } 794 795 if (hfp_connection->hf_send_chld_4){ 796 hfp_connection->hf_send_chld_4 = 0; 797 hfp_connection->ok_pending = 1; 798 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 799 return; 800 } 801 802 if (hfp_connection->hf_send_chld_x){ 803 hfp_connection->hf_send_chld_x = 0; 804 hfp_connection->ok_pending = 1; 805 hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 806 return; 807 } 808 809 if (hfp_connection->hf_send_dtmf_code){ 810 char code = hfp_connection->hf_send_dtmf_code; 811 hfp_connection->hf_send_dtmf_code = 0; 812 hfp_connection->ok_pending = 1; 813 hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 814 return; 815 } 816 817 if (hfp_connection->hf_send_binp){ 818 hfp_connection->hf_send_binp = 0; 819 hfp_connection->ok_pending = 1; 820 hfp_hf_send_binp(hfp_connection->rfcomm_cid); 821 return; 822 } 823 824 if (hfp_connection->hf_send_clcc){ 825 hfp_connection->hf_send_clcc = 0; 826 hfp_connection->ok_pending = 1; 827 hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 828 return; 829 } 830 831 if (hfp_connection->hf_send_rrh){ 832 hfp_connection->hf_send_rrh = 0; 833 char buffer[20]; 834 switch (hfp_connection->hf_send_rrh_command){ 835 case '?': 836 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD); 837 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 838 return; 839 case '0': 840 case '1': 841 case '2': 842 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command); 843 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 844 return; 845 default: 846 break; 847 } 848 return; 849 } 850 851 if (hfp_connection->hf_send_cnum){ 852 hfp_connection->hf_send_cnum = 0; 853 char buffer[20]; 854 sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION); 855 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 856 return; 857 } 858 859 // update HF indicators 860 if (hfp_connection->generic_status_update_bitmap){ 861 int i; 862 for (i=0;i<hfp_indicators_nr;i++){ 863 if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 864 if (hfp_connection->generic_status_indicators[i].state){ 865 hfp_connection->ok_pending = 1; 866 hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 867 char buffer[30]; 868 sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]); 869 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 870 } else { 871 log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]); 872 } 873 return; 874 } 875 } 876 } 877 878 if (done) return; 879 // deal with disconnect 880 switch (hfp_connection->state){ 881 case HFP_W2_DISCONNECT_RFCOMM: 882 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 883 rfcomm_disconnect(hfp_connection->rfcomm_cid); 884 break; 885 886 default: 887 break; 888 } 889 } 890 891 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 892 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 893 894 hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 895 896 // restore volume settings 897 hfp_connection->speaker_gain = hfp_hf_speaker_gain; 898 hfp_connection->send_speaker_gain = 1; 899 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 900 hfp_connection->microphone_gain = hfp_hf_microphone_gain; 901 hfp_connection->send_microphone_gain = 1; 902 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 903 // enable all indicators 904 int i; 905 for (i=0;i<hfp_indicators_nr;i++){ 906 hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i]; 907 hfp_connection->generic_status_indicators[i].state = 1; 908 } 909 } 910 911 static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){ 912 if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){ 913 // Codec supported, confirm 914 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 915 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 916 log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 917 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 918 919 hfp_connection->hf_send_codec_confirm = true; 920 } else { 921 // Codec not supported, send supported codecs 922 hfp_connection->codec_confirmed = 0; 923 hfp_connection->suggested_codec = 0; 924 hfp_connection->negotiated_codec = 0; 925 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 926 927 hfp_connection->hf_send_supported_codecs = true; 928 } 929 } 930 931 static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){ 932 hfp_connection->ok_pending = 0; 933 switch (hfp_connection->state){ 934 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 935 if (has_codec_negotiation_feature(hfp_connection)){ 936 hfp_connection->state = HFP_NOTIFY_ON_CODECS; 937 break; 938 } 939 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 940 break; 941 942 case HFP_W4_NOTIFY_ON_CODECS: 943 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 944 break; 945 946 case HFP_W4_RETRIEVE_INDICATORS: 947 hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 948 break; 949 950 case HFP_W4_RETRIEVE_INDICATORS_STATUS: 951 hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 952 break; 953 954 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 955 if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 956 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 957 break; 958 } 959 if (has_hf_indicators_feature(hfp_connection)){ 960 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 961 break; 962 } 963 hfp_ag_slc_established(hfp_connection); 964 break; 965 966 case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 967 if (has_hf_indicators_feature(hfp_connection)){ 968 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 969 break; 970 } 971 hfp_ag_slc_established(hfp_connection); 972 break; 973 974 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 975 hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 976 break; 977 978 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 979 hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 980 break; 981 982 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 983 hfp_ag_slc_established(hfp_connection); 984 break; 985 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 986 if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 987 hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 988 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 989 break; 990 } 991 992 if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 993 hfp_connection->change_status_update_for_individual_ag_indicators = 0; 994 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 995 break; 996 } 997 998 switch (hfp_connection->hf_query_operator_state){ 999 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 1000 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1001 break; 1002 case HPF_HF_QUERY_OPERATOR_W4_RESULT: 1003 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 1004 hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator); 1005 break; 1006 default: 1007 break; 1008 } 1009 1010 if (hfp_connection->enable_extended_audio_gateway_error_report){ 1011 hfp_connection->enable_extended_audio_gateway_error_report = 0; 1012 break; 1013 } 1014 1015 switch (hfp_connection->codecs_state){ 1016 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1017 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 1018 break; 1019 case HFP_CODECS_HF_CONFIRMED_CODEC: 1020 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1021 break; 1022 default: 1023 break; 1024 } 1025 break; 1026 default: 1027 break; 1028 } 1029 1030 // done 1031 hfp_connection->command = HFP_CMD_NONE; 1032 } 1033 1034 static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 1035 uint16_t i; 1036 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1037 if (hfp_connection->ag_indicators[i].status_changed) { 1038 if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1039 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1040 } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1041 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 1042 // avoid set but not used warning 1043 (void) hfp_callheld_status; 1044 } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1045 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1046 } 1047 hfp_connection->ag_indicators[i].status_changed = 0; 1048 hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]); 1049 break; 1050 } 1051 } 1052 } 1053 1054 static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 1055 int value; 1056 int i; 1057 switch (hfp_connection->command){ 1058 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1059 hfp_connection->command = HFP_CMD_NONE; 1060 hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number); 1061 break; 1062 case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1063 hfp_connection->command = HFP_CMD_NONE; 1064 hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1065 break; 1066 case HFP_CMD_LIST_CURRENT_CALLS: 1067 hfp_connection->command = HFP_CMD_NONE; 1068 hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection); 1069 break; 1070 case HFP_CMD_SET_SPEAKER_GAIN: 1071 hfp_connection->command = HFP_CMD_NONE; 1072 value = btstack_atoi((char*)hfp_connection->line_buffer); 1073 hfp_hf_speaker_gain = value; 1074 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1075 break; 1076 case HFP_CMD_SET_MICROPHONE_GAIN: 1077 hfp_connection->command = HFP_CMD_NONE; 1078 value = btstack_atoi((char*)hfp_connection->line_buffer); 1079 hfp_hf_microphone_gain = value; 1080 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1081 break; 1082 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1083 hfp_connection->command = HFP_CMD_NONE; 1084 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1085 break; 1086 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1087 hfp_connection->command = HFP_CMD_NONE; 1088 hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1089 break; 1090 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1091 hfp_connection->command = HFP_CMD_NONE; 1092 hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1093 break; 1094 case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1095 hfp_connection->ok_pending = 0; 1096 hfp_connection->command = HFP_CMD_NONE; 1097 hfp_connection->extended_audio_gateway_error = 0; 1098 hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1099 break; 1100 case HFP_CMD_ERROR: 1101 hfp_connection->ok_pending = 0; 1102 hfp_reset_context_flags(hfp_connection); 1103 hfp_connection->command = HFP_CMD_NONE; 1104 1105 switch (hfp_connection->state){ 1106 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 1107 switch (hfp_connection->codecs_state){ 1108 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1109 hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 1110 return; 1111 default: 1112 break; 1113 } 1114 default: 1115 break; 1116 } 1117 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1118 break; 1119 case HFP_CMD_OK: 1120 hfp_hf_switch_on_ok(hfp_connection); 1121 break; 1122 case HFP_CMD_RING: 1123 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1124 break; 1125 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1126 hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1127 break; 1128 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1129 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1130 hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]); 1131 } 1132 hfp_connection->command = HFP_CMD_NONE; 1133 break; 1134 case HFP_CMD_AG_SUGGESTED_CODEC: 1135 hfp_hf_handle_suggested_codec(hfp_connection); 1136 hfp_connection->command = HFP_CMD_NONE; 1137 break; 1138 default: 1139 break; 1140 } 1141 } 1142 1143 static int hfp_parser_is_end_of_line(uint8_t byte){ 1144 return (byte == '\n') || (byte == '\r'); 1145 } 1146 1147 static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1148 UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 1149 // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1150 if (size < 1) return; 1151 1152 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1153 if (!hfp_connection) return; 1154 1155 hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1156 #ifdef ENABLE_HFP_AT_MESSAGES 1157 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 1158 #endif 1159 1160 // process messages byte-wise 1161 int pos; 1162 for (pos = 0; pos < size; pos++){ 1163 hfp_parse(hfp_connection, packet[pos], 1); 1164 1165 // parse until end of line "\r\n" 1166 if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1167 1168 hfp_hf_handle_rfcomm_command(hfp_connection); 1169 } 1170 hfp_hf_run_for_context(hfp_connection); 1171 } 1172 1173 static void hfp_hf_run(void){ 1174 btstack_linked_list_iterator_t it; 1175 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1176 while (btstack_linked_list_iterator_has_next(&it)){ 1177 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1178 if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1179 hfp_hf_run_for_context(hfp_connection); 1180 } 1181 } 1182 1183 static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1184 switch (packet_type){ 1185 case RFCOMM_DATA_PACKET: 1186 hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size); 1187 break; 1188 case HCI_EVENT_PACKET: 1189 if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1190 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 1191 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1192 return; 1193 } 1194 hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1195 break; 1196 default: 1197 break; 1198 } 1199 hfp_hf_run(); 1200 } 1201 1202 static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1203 hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1204 hfp_hf_run(); 1205 } 1206 1207 void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1208 hfp_init(); 1209 hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1210 hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 1211 hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1212 hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1213 hfp_codecs_nr = 0; 1214 hfp_hf_speaker_gain = 9; 1215 hfp_hf_microphone_gain = 9; 1216 hfp_indicators_nr = 0; 1217 hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1218 1219 hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler; 1220 hci_add_event_handler(&hfp_hf_hci_event_callback_registration); 1221 1222 rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1223 1224 // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 1225 hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler); 1226 } 1227 1228 void hfp_hf_deinit(void){ 1229 hfp_deinit(); 1230 (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 1231 (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t)); 1232 (void) memset(phone_number, 0, sizeof(phone_number)); 1233 } 1234 1235 void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){ 1236 if (codecs_nr > HFP_MAX_NUM_CODECS){ 1237 log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 1238 return; 1239 } 1240 1241 hfp_codecs_nr = codecs_nr; 1242 int i; 1243 for (i=0; i<codecs_nr; i++){ 1244 hfp_codecs[i] = codecs[i]; 1245 } 1246 } 1247 1248 void hfp_hf_init_supported_features(uint32_t supported_features){ 1249 hfp_supported_features = supported_features; 1250 } 1251 1252 void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){ 1253 hfp_indicators_nr = indicators_nr; 1254 int i; 1255 for (i = 0; i < hfp_indicators_nr ; i++){ 1256 hfp_indicators[i] = indicators[i]; 1257 } 1258 } 1259 1260 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1261 hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 1262 } 1263 1264 void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 1265 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1266 if (!hfp_connection) { 1267 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1268 return; 1269 } 1270 hfp_release_service_level_connection(hfp_connection); 1271 hfp_hf_run_for_context(hfp_connection); 1272 } 1273 1274 static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 1275 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1276 if (!hfp_connection) { 1277 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1278 return; 1279 } 1280 hfp_connection->enable_status_update_for_ag_indicators = enable; 1281 hfp_hf_run_for_context(hfp_connection); 1282 } 1283 1284 void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1285 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1286 } 1287 1288 void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1289 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1290 } 1291 1292 // TODO: returned ERROR - wrong format 1293 void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 1294 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1295 if (!hfp_connection) { 1296 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1297 return; 1298 } 1299 hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1300 hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1301 hfp_hf_run_for_context(hfp_connection); 1302 } 1303 1304 void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 1305 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1306 if (!hfp_connection) { 1307 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1308 return; 1309 } 1310 switch (hfp_connection->hf_query_operator_state){ 1311 case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1312 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1313 break; 1314 case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1315 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1316 break; 1317 default: 1318 break; 1319 } 1320 hfp_hf_run_for_context(hfp_connection); 1321 } 1322 1323 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 1324 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1325 if (!hfp_connection) { 1326 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1327 return; 1328 } 1329 hfp_connection->enable_extended_audio_gateway_error_report = enable; 1330 hfp_hf_run_for_context(hfp_connection); 1331 } 1332 1333 1334 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1335 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1336 } 1337 1338 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1339 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1340 } 1341 1342 static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 1343 return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_supported_features & (1<<HFP_HFSF_ESCO_S4)); 1344 } 1345 1346 void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 1347 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1348 if (!hfp_connection) { 1349 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1350 return; 1351 } 1352 hfp_connection->establish_audio_connection = 0; 1353 1354 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1355 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1356 1357 hfp_connection->trigger_codec_exchange = 0; 1358 hfp_connection->establish_audio_connection = 1; 1359 if (!has_codec_negotiation_feature(hfp_connection)){ 1360 log_info("no codec negotiation feature, using NBS"); 1361 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1362 hfp_connection->suggested_codec = HFP_CODEC_CVSD; 1363 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1364 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1365 hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 1366 hfp_connection->state = HFP_W4_SCO_CONNECTED; 1367 } else { 1368 switch (hfp_connection->codecs_state){ 1369 case HFP_CODECS_W4_AG_COMMON_CODEC: 1370 break; 1371 default: 1372 hfp_connection->codec_confirmed = 0; 1373 hfp_connection->suggested_codec = 0; 1374 hfp_connection->negotiated_codec = 0; 1375 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 1376 hfp_connection->trigger_codec_exchange = 1; 1377 break; 1378 } 1379 } 1380 1381 hfp_hf_run_for_context(hfp_connection); 1382 } 1383 1384 void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 1385 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1386 if (!hfp_connection) { 1387 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1388 return; 1389 } 1390 hfp_release_audio_connection(hfp_connection); 1391 hfp_hf_run_for_context(hfp_connection); 1392 } 1393 1394 void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 1395 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1396 if (!hfp_connection) { 1397 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1398 return; 1399 } 1400 1401 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1402 hfp_connection->hf_answer_incoming_call = 1; 1403 hfp_hf_run_for_context(hfp_connection); 1404 } else { 1405 log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1406 } 1407 } 1408 1409 void hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 1410 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1411 if (!hfp_connection) { 1412 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1413 return; 1414 } 1415 hfp_connection->hf_send_chup = 1; 1416 hfp_hf_run_for_context(hfp_connection); 1417 } 1418 1419 void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 1420 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1421 if (!hfp_connection) { 1422 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1423 return; 1424 } 1425 1426 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1427 hfp_connection->hf_send_chup = 1; 1428 hfp_hf_run_for_context(hfp_connection); 1429 } 1430 } 1431 1432 void hfp_hf_user_busy(hci_con_handle_t acl_handle){ 1433 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1434 if (!hfp_connection) { 1435 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1436 return; 1437 } 1438 1439 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1440 hfp_connection->hf_send_chld_0 = 1; 1441 hfp_hf_run_for_context(hfp_connection); 1442 } 1443 } 1444 1445 void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 1446 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1447 if (!hfp_connection) { 1448 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1449 return; 1450 } 1451 1452 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1453 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1454 hfp_connection->hf_send_chld_1 = 1; 1455 hfp_hf_run_for_context(hfp_connection); 1456 } 1457 } 1458 1459 void hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 1460 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1461 if (!hfp_connection) { 1462 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1463 return; 1464 } 1465 1466 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1467 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1468 hfp_connection->hf_send_chld_2 = 1; 1469 hfp_hf_run_for_context(hfp_connection); 1470 } 1471 } 1472 1473 void hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 1474 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1475 if (!hfp_connection) { 1476 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1477 return; 1478 } 1479 1480 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1481 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1482 hfp_connection->hf_send_chld_3 = 1; 1483 hfp_hf_run_for_context(hfp_connection); 1484 } 1485 } 1486 1487 void hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 1488 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1489 if (!hfp_connection) { 1490 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1491 return; 1492 } 1493 1494 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1495 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1496 hfp_connection->hf_send_chld_4 = 1; 1497 hfp_hf_run_for_context(hfp_connection); 1498 } 1499 } 1500 1501 void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 1502 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1503 if (!hfp_connection) { 1504 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1505 return; 1506 } 1507 1508 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1509 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1510 hfp_connection->hf_send_chld_x = 1; 1511 hfp_connection->hf_send_chld_x_index = 10 + index; 1512 hfp_hf_run_for_context(hfp_connection); 1513 } 1514 } 1515 1516 void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 1517 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1518 if (!hfp_connection) { 1519 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1520 return; 1521 } 1522 1523 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1524 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1525 hfp_connection->hf_send_chld_x = 1; 1526 hfp_connection->hf_send_chld_x_index = 20 + index; 1527 hfp_hf_run_for_context(hfp_connection); 1528 } 1529 } 1530 1531 void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 1532 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1533 if (!hfp_connection) { 1534 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1535 return; 1536 } 1537 1538 hfp_connection->hf_initiate_outgoing_call = 1; 1539 snprintf(phone_number, sizeof(phone_number), "%s", number); 1540 hfp_hf_run_for_context(hfp_connection); 1541 } 1542 1543 void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 1544 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1545 if (!hfp_connection) { 1546 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1547 return; 1548 } 1549 1550 hfp_connection->hf_initiate_memory_dialing = 1; 1551 hfp_connection->memory_id = memory_id; 1552 1553 hfp_hf_run_for_context(hfp_connection); 1554 } 1555 1556 void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 1557 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1558 if (!hfp_connection) { 1559 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1560 return; 1561 } 1562 1563 hfp_connection->hf_initiate_redial_last_number = 1; 1564 hfp_hf_run_for_context(hfp_connection); 1565 } 1566 1567 void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 1568 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1569 if (!hfp_connection) { 1570 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1571 return; 1572 } 1573 1574 hfp_connection->hf_activate_call_waiting_notification = 1; 1575 hfp_hf_run_for_context(hfp_connection); 1576 } 1577 1578 1579 void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 1580 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1581 if (!hfp_connection) { 1582 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1583 return; 1584 } 1585 1586 hfp_connection->hf_deactivate_call_waiting_notification = 1; 1587 hfp_hf_run_for_context(hfp_connection); 1588 } 1589 1590 1591 void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 1592 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1593 if (!hfp_connection) { 1594 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1595 return; 1596 } 1597 1598 hfp_connection->hf_activate_calling_line_notification = 1; 1599 hfp_hf_run_for_context(hfp_connection); 1600 } 1601 1602 void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 1603 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1604 if (!hfp_connection) { 1605 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1606 return; 1607 } 1608 1609 hfp_connection->hf_deactivate_calling_line_notification = 1; 1610 hfp_hf_run_for_context(hfp_connection); 1611 } 1612 1613 1614 void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1615 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1616 if (!hfp_connection) { 1617 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1618 return; 1619 } 1620 1621 hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1; 1622 hfp_hf_run_for_context(hfp_connection); 1623 } 1624 1625 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1626 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1627 if (!hfp_connection) { 1628 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1629 return; 1630 } 1631 1632 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1633 hfp_hf_run_for_context(hfp_connection); 1634 } 1635 1636 void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1637 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1638 if (!hfp_connection) { 1639 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1640 return; 1641 } 1642 1643 hfp_connection->hf_activate_voice_recognition_notification = 1; 1644 hfp_hf_run_for_context(hfp_connection); 1645 } 1646 1647 void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1648 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1649 if (!hfp_connection) { 1650 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1651 return; 1652 } 1653 1654 hfp_connection->hf_deactivate_voice_recognition_notification = 1; 1655 hfp_hf_run_for_context(hfp_connection); 1656 } 1657 1658 void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 1659 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1660 if (!hfp_connection) { 1661 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1662 return; 1663 } 1664 1665 if (hfp_connection->microphone_gain == gain) return; 1666 if ((gain < 0) || (gain > 15)){ 1667 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1668 return; 1669 } 1670 hfp_connection->microphone_gain = gain; 1671 hfp_connection->send_microphone_gain = 1; 1672 hfp_hf_run_for_context(hfp_connection); 1673 } 1674 1675 void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 1676 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1677 if (!hfp_connection) { 1678 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1679 return; 1680 } 1681 1682 if (hfp_connection->speaker_gain == gain) return; 1683 if ((gain < 0) || (gain > 15)){ 1684 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1685 return; 1686 } 1687 hfp_connection->speaker_gain = gain; 1688 hfp_connection->send_speaker_gain = 1; 1689 hfp_hf_run_for_context(hfp_connection); 1690 } 1691 1692 void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 1693 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1694 if (!hfp_connection) { 1695 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1696 return; 1697 } 1698 1699 hfp_connection->hf_send_dtmf_code = code; 1700 hfp_hf_run_for_context(hfp_connection); 1701 } 1702 1703 void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 1704 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1705 if (!hfp_connection) { 1706 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1707 return; 1708 } 1709 hfp_connection->hf_send_binp = 1; 1710 hfp_hf_run_for_context(hfp_connection); 1711 } 1712 1713 void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 1714 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1715 if (!hfp_connection) { 1716 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1717 return; 1718 } 1719 hfp_connection->hf_send_clcc = 1; 1720 hfp_hf_run_for_context(hfp_connection); 1721 } 1722 1723 1724 void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 1725 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1726 if (!hfp_connection) { 1727 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1728 return; 1729 } 1730 hfp_connection->hf_send_rrh = 1; 1731 hfp_connection->hf_send_rrh_command = '?'; 1732 hfp_hf_run_for_context(hfp_connection); 1733 } 1734 1735 void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 1736 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1737 if (!hfp_connection) { 1738 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1739 return; 1740 } 1741 hfp_connection->hf_send_rrh = 1; 1742 hfp_connection->hf_send_rrh_command = '0'; 1743 hfp_hf_run_for_context(hfp_connection); 1744 } 1745 1746 void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 1747 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1748 if (!hfp_connection) { 1749 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1750 return; 1751 } 1752 hfp_connection->hf_send_rrh = 1; 1753 hfp_connection->hf_send_rrh_command = '1'; 1754 hfp_hf_run_for_context(hfp_connection); 1755 } 1756 1757 void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 1758 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1759 if (!hfp_connection) { 1760 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1761 return; 1762 } 1763 hfp_connection->hf_send_rrh = 1; 1764 hfp_connection->hf_send_rrh_command = '2'; 1765 hfp_hf_run_for_context(hfp_connection); 1766 } 1767 1768 void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 1769 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1770 if (!hfp_connection) { 1771 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1772 return; 1773 } 1774 hfp_connection->hf_send_cnum = 1; 1775 hfp_hf_run_for_context(hfp_connection); 1776 } 1777 1778 void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 1779 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1780 if (!hfp_connection) { 1781 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1782 return; 1783 } 1784 // find index for assigned number 1785 int i; 1786 for (i = 0; i < hfp_indicators_nr ; i++){ 1787 if (hfp_indicators[i] == assigned_number){ 1788 // set value 1789 hfp_indicators_value[i] = value; 1790 // mark for update 1791 if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1792 hfp_connection->generic_status_update_bitmap |= (1<<i); 1793 // send update 1794 hfp_hf_run_for_context(hfp_connection); 1795 } 1796 return; 1797 } 1798 } 1799 } 1800 1801 int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 1802 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1803 if (!hfp_connection) { 1804 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1805 return 0; 1806 } 1807 return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1808 } 1809 1810 void hfp_hf_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint16_t supported_features, int wide_band_speech){ 1811 if (!name){ 1812 name = default_hfp_hf_service_name; 1813 } 1814 hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 1815 1816 // Construct SupportedFeatures for SDP bitmap: 1817 // 1818 // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 1819 // of the Bits 0 to 4 of the unsolicited result code +BRSF" 1820 // 1821 // Wide band speech (bit 5) requires Codec negotiation 1822 // 1823 uint16_t sdp_features = supported_features & 0x1f; 1824 if (wide_band_speech && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 1825 sdp_features |= 1 << 5; 1826 } 1827 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 1828 de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 1829 } 1830 1831 void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 1832 if (callback == NULL){ 1833 log_error("hfp_hf_register_packet_handler called with NULL callback"); 1834 return; 1835 } 1836 hfp_hf_callback = callback; 1837 hfp_set_hf_callback(callback); 1838 } 1839