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