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