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 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->codec_confirmed = 0; 475 hfp_connection->suggested_codec = 0; 476 hfp_connection->negotiated_codec = 0; 477 478 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 479 hfp_connection->ok_pending = 1; 480 hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 481 return 1; 482 } 483 484 switch (hfp_connection->command){ 485 486 case HFP_CMD_AG_SUGGESTED_CODEC:{ 487 if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){ 488 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 489 hfp_connection->ok_pending = 1; 490 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 491 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 492 log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 493 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 494 } else { 495 hfp_connection->codec_confirmed = 0; 496 hfp_connection->suggested_codec = 0; 497 hfp_connection->negotiated_codec = 0; 498 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 499 hfp_connection->ok_pending = 1; 500 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 501 502 } 503 break; 504 } 505 default: 506 break; 507 } 508 return 0; 509 } 510 511 static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 512 if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 513 (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 514 515 if (hfp_connection->release_audio_connection){ 516 hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 517 hfp_connection->release_audio_connection = 0; 518 gap_disconnect(hfp_connection->sco_handle); 519 return 1; 520 } 521 522 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 523 524 // run codecs exchange 525 int done = codecs_exchange_state_machine(hfp_connection); 526 if (done) return 1; 527 528 if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 529 if (hfp_connection->establish_audio_connection){ 530 hfp_connection->state = HFP_W4_SCO_CONNECTED; 531 hfp_connection->establish_audio_connection = 0; 532 hfp_setup_synchronous_connection(hfp_connection); 533 return 1; 534 } 535 536 return 0; 537 } 538 539 540 static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 541 542 if (hfp_connection->ok_pending) return 0; 543 544 if (hfp_connection->hf_answer_incoming_call){ 545 hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 546 hfp_connection->hf_answer_incoming_call = 0; 547 return 1; 548 } 549 return 0; 550 } 551 552 static void hfp_run_for_context(hfp_connection_t * hfp_connection){ 553 554 btstack_assert(hfp_connection != NULL); 555 btstack_assert(hfp_connection->local_role == HFP_ROLE_HF); 556 557 // during SDP query, RFCOMM CID is not set 558 if (hfp_connection->rfcomm_cid == 0) return; 559 560 if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){ 561 562 bool eSCO = hfp_connection->hf_accept_sco == 2; 563 hfp_connection->hf_accept_sco = 0; 564 565 // notify about codec selection if not done already 566 if (hfp_connection->negotiated_codec == 0){ 567 hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 568 } 569 570 // remote supported feature eSCO is set if link type is eSCO 571 // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 572 uint16_t max_latency; 573 uint8_t retransmission_effort; 574 uint16_t packet_types; 575 576 if (eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 577 max_latency = 0x000c; 578 retransmission_effort = 0x02; 579 // eSCO: EV3 and 2-EV3 580 packet_types = 0x0048; 581 } else { 582 max_latency = 0xffff; 583 retransmission_effort = 0xff; 584 // sco: HV1 and HV3 585 packet_types = 0x005; 586 } 587 588 // mSBC only allows for transparent data 589 uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 590 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 591 sco_voice_setting = 0x0043; // Transparent data 592 } 593 594 // filter packet types 595 packet_types &= hfp_get_sco_packet_types(); 596 597 // bits 6-9 are 'don't allow' 598 packet_types ^= 0x3c0; 599 600 log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting); 601 hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 602 sco_voice_setting, retransmission_effort, packet_types); 603 return; 604 } 605 606 if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 607 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 608 return; 609 } 610 int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 611 if (!done){ 612 done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 613 } 614 if (!done){ 615 done = hfp_hf_run_for_audio_connection(hfp_connection); 616 } 617 if (!done){ 618 done = call_setup_state_machine(hfp_connection); 619 } 620 621 // don't send a new command while ok still pending 622 if (hfp_connection->ok_pending) return; 623 624 if (hfp_connection->send_microphone_gain){ 625 hfp_connection->send_microphone_gain = 0; 626 hfp_connection->ok_pending = 1; 627 hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 628 return; 629 } 630 631 if (hfp_connection->send_speaker_gain){ 632 hfp_connection->send_speaker_gain = 0; 633 hfp_connection->ok_pending = 1; 634 hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 635 return; 636 } 637 638 if (hfp_connection->hf_deactivate_calling_line_notification){ 639 hfp_connection->hf_deactivate_calling_line_notification = 0; 640 hfp_connection->ok_pending = 1; 641 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 642 return; 643 } 644 645 if (hfp_connection->hf_activate_calling_line_notification){ 646 hfp_connection->hf_activate_calling_line_notification = 0; 647 hfp_connection->ok_pending = 1; 648 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 649 return; 650 } 651 652 if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 653 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 654 hfp_connection->ok_pending = 1; 655 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0); 656 return; 657 } 658 659 if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){ 660 hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0; 661 hfp_connection->ok_pending = 1; 662 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1); 663 return; 664 } 665 666 if (hfp_connection->hf_deactivate_voice_recognition_notification){ 667 hfp_connection->hf_deactivate_voice_recognition_notification = 0; 668 hfp_connection->ok_pending = 1; 669 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 670 return; 671 } 672 673 if (hfp_connection->hf_activate_voice_recognition_notification){ 674 hfp_connection->hf_activate_voice_recognition_notification = 0; 675 hfp_connection->ok_pending = 1; 676 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 677 return; 678 } 679 680 681 if (hfp_connection->hf_deactivate_call_waiting_notification){ 682 hfp_connection->hf_deactivate_call_waiting_notification = 0; 683 hfp_connection->ok_pending = 1; 684 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 685 return; 686 } 687 688 if (hfp_connection->hf_activate_call_waiting_notification){ 689 hfp_connection->hf_activate_call_waiting_notification = 0; 690 hfp_connection->ok_pending = 1; 691 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 692 return; 693 } 694 695 if (hfp_connection->hf_initiate_outgoing_call){ 696 hfp_connection->hf_initiate_outgoing_call = 0; 697 hfp_connection->ok_pending = 1; 698 hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 699 return; 700 } 701 702 if (hfp_connection->hf_initiate_memory_dialing){ 703 hfp_connection->hf_initiate_memory_dialing = 0; 704 hfp_connection->ok_pending = 1; 705 hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 706 return; 707 } 708 709 if (hfp_connection->hf_initiate_redial_last_number){ 710 hfp_connection->hf_initiate_redial_last_number = 0; 711 hfp_connection->ok_pending = 1; 712 hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 713 return; 714 } 715 716 if (hfp_connection->hf_send_chup){ 717 hfp_connection->hf_send_chup = 0; 718 hfp_connection->ok_pending = 1; 719 hfp_hf_send_chup(hfp_connection->rfcomm_cid); 720 return; 721 } 722 723 if (hfp_connection->hf_send_chld_0){ 724 hfp_connection->hf_send_chld_0 = 0; 725 hfp_connection->ok_pending = 1; 726 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 727 return; 728 } 729 730 if (hfp_connection->hf_send_chld_1){ 731 hfp_connection->hf_send_chld_1 = 0; 732 hfp_connection->ok_pending = 1; 733 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 734 return; 735 } 736 737 if (hfp_connection->hf_send_chld_2){ 738 hfp_connection->hf_send_chld_2 = 0; 739 hfp_connection->ok_pending = 1; 740 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 741 return; 742 } 743 744 if (hfp_connection->hf_send_chld_3){ 745 hfp_connection->hf_send_chld_3 = 0; 746 hfp_connection->ok_pending = 1; 747 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 748 return; 749 } 750 751 if (hfp_connection->hf_send_chld_4){ 752 hfp_connection->hf_send_chld_4 = 0; 753 hfp_connection->ok_pending = 1; 754 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 755 return; 756 } 757 758 if (hfp_connection->hf_send_chld_x){ 759 hfp_connection->hf_send_chld_x = 0; 760 hfp_connection->ok_pending = 1; 761 hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 762 return; 763 } 764 765 if (hfp_connection->hf_send_dtmf_code){ 766 char code = hfp_connection->hf_send_dtmf_code; 767 hfp_connection->hf_send_dtmf_code = 0; 768 hfp_connection->ok_pending = 1; 769 hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 770 return; 771 } 772 773 if (hfp_connection->hf_send_binp){ 774 hfp_connection->hf_send_binp = 0; 775 hfp_connection->ok_pending = 1; 776 hfp_hf_send_binp(hfp_connection->rfcomm_cid); 777 return; 778 } 779 780 if (hfp_connection->hf_send_clcc){ 781 hfp_connection->hf_send_clcc = 0; 782 hfp_connection->ok_pending = 1; 783 hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 784 return; 785 } 786 787 if (hfp_connection->hf_send_rrh){ 788 hfp_connection->hf_send_rrh = 0; 789 char buffer[20]; 790 switch (hfp_connection->hf_send_rrh_command){ 791 case '?': 792 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD); 793 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 794 return; 795 case '0': 796 case '1': 797 case '2': 798 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command); 799 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 800 return; 801 default: 802 break; 803 } 804 return; 805 } 806 807 if (hfp_connection->hf_send_cnum){ 808 hfp_connection->hf_send_cnum = 0; 809 char buffer[20]; 810 sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION); 811 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 812 return; 813 } 814 815 // update HF indicators 816 if (hfp_connection->generic_status_update_bitmap){ 817 int i; 818 for (i=0;i<hfp_indicators_nr;i++){ 819 if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 820 if (hfp_connection->generic_status_indicators[i].state){ 821 hfp_connection->ok_pending = 1; 822 hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 823 char buffer[30]; 824 sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]); 825 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 826 } else { 827 log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]); 828 } 829 return; 830 } 831 } 832 } 833 834 if (done) return; 835 // deal with disconnect 836 switch (hfp_connection->state){ 837 case HFP_W2_DISCONNECT_RFCOMM: 838 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 839 rfcomm_disconnect(hfp_connection->rfcomm_cid); 840 break; 841 842 default: 843 break; 844 } 845 } 846 847 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 848 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 849 850 hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 851 852 // restore volume settings 853 hfp_connection->speaker_gain = hfp_hf_speaker_gain; 854 hfp_connection->send_speaker_gain = 1; 855 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 856 hfp_connection->microphone_gain = hfp_hf_microphone_gain; 857 hfp_connection->send_microphone_gain = 1; 858 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 859 // enable all indicators 860 int i; 861 for (i=0;i<hfp_indicators_nr;i++){ 862 hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i]; 863 hfp_connection->generic_status_indicators[i].state = 1; 864 } 865 } 866 867 static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){ 868 hfp_connection->ok_pending = 0; 869 switch (hfp_connection->state){ 870 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 871 if (has_codec_negotiation_feature(hfp_connection)){ 872 hfp_connection->state = HFP_NOTIFY_ON_CODECS; 873 break; 874 } 875 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 876 break; 877 878 case HFP_W4_NOTIFY_ON_CODECS: 879 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 880 break; 881 882 case HFP_W4_RETRIEVE_INDICATORS: 883 hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 884 break; 885 886 case HFP_W4_RETRIEVE_INDICATORS_STATUS: 887 hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 888 break; 889 890 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 891 if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 892 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 893 break; 894 } 895 if (has_hf_indicators_feature(hfp_connection)){ 896 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 897 break; 898 } 899 hfp_ag_slc_established(hfp_connection); 900 break; 901 902 case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 903 if (has_hf_indicators_feature(hfp_connection)){ 904 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 905 break; 906 } 907 hfp_ag_slc_established(hfp_connection); 908 break; 909 910 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 911 hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 912 break; 913 914 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 915 hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 916 break; 917 918 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 919 hfp_ag_slc_established(hfp_connection); 920 break; 921 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 922 if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 923 hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 924 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 925 break; 926 } 927 928 if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 929 hfp_connection->change_status_update_for_individual_ag_indicators = 0; 930 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 931 break; 932 } 933 934 switch (hfp_connection->hf_query_operator_state){ 935 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 936 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 937 break; 938 case HPF_HF_QUERY_OPERATOR_W4_RESULT: 939 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 940 hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator); 941 break; 942 default: 943 break; 944 } 945 946 if (hfp_connection->enable_extended_audio_gateway_error_report){ 947 hfp_connection->enable_extended_audio_gateway_error_report = 0; 948 break; 949 } 950 951 switch (hfp_connection->codecs_state){ 952 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 953 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 954 break; 955 case HFP_CODECS_HF_CONFIRMED_CODEC: 956 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 957 break; 958 default: 959 break; 960 } 961 break; 962 default: 963 break; 964 } 965 966 // done 967 hfp_connection->command = HFP_CMD_NONE; 968 } 969 970 static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 971 uint16_t i; 972 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 973 if (hfp_connection->ag_indicators[i].status_changed) { 974 if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 975 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 976 } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 977 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 978 // avoid set but not used warning 979 (void) hfp_callheld_status; 980 } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 981 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 982 } 983 hfp_connection->ag_indicators[i].status_changed = 0; 984 hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]); 985 break; 986 } 987 } 988 } 989 990 static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 991 int value; 992 int i; 993 switch (hfp_connection->command){ 994 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 995 hfp_connection->command = HFP_CMD_NONE; 996 hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number); 997 break; 998 case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 999 hfp_connection->command = HFP_CMD_NONE; 1000 hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1001 break; 1002 case HFP_CMD_LIST_CURRENT_CALLS: 1003 hfp_connection->command = HFP_CMD_NONE; 1004 hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection); 1005 break; 1006 case HFP_CMD_SET_SPEAKER_GAIN: 1007 hfp_connection->command = HFP_CMD_NONE; 1008 value = btstack_atoi((char*)hfp_connection->line_buffer); 1009 hfp_hf_speaker_gain = value; 1010 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1011 break; 1012 case HFP_CMD_SET_MICROPHONE_GAIN: 1013 hfp_connection->command = HFP_CMD_NONE; 1014 value = btstack_atoi((char*)hfp_connection->line_buffer); 1015 hfp_hf_microphone_gain = value; 1016 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1017 break; 1018 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1019 hfp_connection->command = HFP_CMD_NONE; 1020 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1021 break; 1022 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1023 hfp_connection->command = HFP_CMD_NONE; 1024 hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1025 break; 1026 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1027 hfp_connection->command = HFP_CMD_NONE; 1028 hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1029 break; 1030 case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1031 hfp_connection->ok_pending = 0; 1032 hfp_connection->command = HFP_CMD_NONE; 1033 hfp_connection->extended_audio_gateway_error = 0; 1034 hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1035 break; 1036 case HFP_CMD_ERROR: 1037 hfp_connection->ok_pending = 0; 1038 hfp_reset_context_flags(hfp_connection); 1039 hfp_connection->command = HFP_CMD_NONE; 1040 1041 switch (hfp_connection->state){ 1042 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 1043 switch (hfp_connection->codecs_state){ 1044 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 1045 hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 1046 return; 1047 default: 1048 break; 1049 } 1050 default: 1051 break; 1052 } 1053 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1054 break; 1055 case HFP_CMD_OK: 1056 hfp_hf_switch_on_ok(hfp_connection); 1057 break; 1058 case HFP_CMD_RING: 1059 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1060 break; 1061 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1062 hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1063 break; 1064 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1065 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1066 hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]); 1067 } 1068 hfp_connection->command = HFP_CMD_NONE; 1069 break; 1070 default: 1071 break; 1072 } 1073 } 1074 1075 static int hfp_parser_is_end_of_line(uint8_t byte){ 1076 return (byte == '\n') || (byte == '\r'); 1077 } 1078 1079 static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1080 UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 1081 // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1082 if (size < 1) return; 1083 1084 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1085 if (!hfp_connection) return; 1086 1087 hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1088 1089 // process messages byte-wise 1090 int pos; 1091 for (pos = 0; pos < size; pos++){ 1092 hfp_parse(hfp_connection, packet[pos], 1); 1093 1094 // parse until end of line "\r\n" 1095 if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1096 1097 hfp_hf_handle_rfcomm_command(hfp_connection); 1098 } 1099 hfp_run_for_context(hfp_connection); 1100 } 1101 1102 static void hfp_run(void){ 1103 btstack_linked_list_iterator_t it; 1104 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1105 while (btstack_linked_list_iterator_has_next(&it)){ 1106 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1107 if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1108 hfp_run_for_context(hfp_connection); 1109 } 1110 } 1111 1112 static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1113 switch (packet_type){ 1114 case RFCOMM_DATA_PACKET: 1115 hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size); 1116 break; 1117 case HCI_EVENT_PACKET: 1118 if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1119 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 1120 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1121 return; 1122 } 1123 hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1124 break; 1125 default: 1126 break; 1127 } 1128 hfp_run(); 1129 } 1130 1131 static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1132 hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1133 1134 // allow for sco established -> ring transition and sco retry 1135 if (packet_type != HCI_EVENT_PACKET) return; 1136 if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return; 1137 hfp_run(); 1138 } 1139 1140 void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1141 hfp_init(); 1142 1143 hci_event_callback_registration.callback = &hci_event_packet_handler; 1144 hci_add_event_handler(&hci_event_callback_registration); 1145 1146 rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1147 1148 // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 1149 hfp_set_hf_rfcomm_packet_handler(&rfcomm_packet_handler); 1150 1151 hfp_set_hf_run_for_context(hfp_run_for_context); 1152 1153 hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1154 hfp_codecs_nr = 0; 1155 hfp_indicators_nr = 0; 1156 hfp_hf_speaker_gain = 9; 1157 hfp_hf_microphone_gain = 9; 1158 } 1159 1160 void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){ 1161 if (codecs_nr > HFP_MAX_NUM_CODECS){ 1162 log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 1163 return; 1164 } 1165 1166 hfp_codecs_nr = codecs_nr; 1167 int i; 1168 for (i=0; i<codecs_nr; i++){ 1169 hfp_codecs[i] = codecs[i]; 1170 } 1171 } 1172 1173 void hfp_hf_init_supported_features(uint32_t supported_features){ 1174 hfp_supported_features = supported_features; 1175 } 1176 1177 void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){ 1178 hfp_indicators_nr = indicators_nr; 1179 int i; 1180 for (i = 0; i < hfp_indicators_nr ; i++){ 1181 hfp_indicators[i] = indicators[i]; 1182 } 1183 } 1184 1185 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1186 hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 1187 } 1188 1189 void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 1190 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1191 if (!hfp_connection) { 1192 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1193 return; 1194 } 1195 hfp_release_service_level_connection(hfp_connection); 1196 hfp_run_for_context(hfp_connection); 1197 } 1198 1199 static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 1200 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1201 if (!hfp_connection) { 1202 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1203 return; 1204 } 1205 hfp_connection->enable_status_update_for_ag_indicators = enable; 1206 hfp_run_for_context(hfp_connection); 1207 } 1208 1209 void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1210 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1211 } 1212 1213 void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1214 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1215 } 1216 1217 // TODO: returned ERROR - wrong format 1218 void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 1219 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1220 if (!hfp_connection) { 1221 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1222 return; 1223 } 1224 hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1225 hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1226 hfp_run_for_context(hfp_connection); 1227 } 1228 1229 void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 1230 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1231 if (!hfp_connection) { 1232 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1233 return; 1234 } 1235 switch (hfp_connection->hf_query_operator_state){ 1236 case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1237 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1238 break; 1239 case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1240 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1241 break; 1242 default: 1243 break; 1244 } 1245 hfp_run_for_context(hfp_connection); 1246 } 1247 1248 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 1249 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1250 if (!hfp_connection) { 1251 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1252 return; 1253 } 1254 hfp_connection->enable_extended_audio_gateway_error_report = enable; 1255 hfp_run_for_context(hfp_connection); 1256 } 1257 1258 1259 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1260 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1261 } 1262 1263 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1264 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1265 } 1266 1267 static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 1268 return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_supported_features & (1<<HFP_HFSF_ESCO_S4)); 1269 } 1270 1271 void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 1272 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1273 if (!hfp_connection) { 1274 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1275 return; 1276 } 1277 hfp_connection->establish_audio_connection = 0; 1278 1279 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1280 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1281 1282 hfp_connection->trigger_codec_exchange = 0; 1283 hfp_connection->establish_audio_connection = 1; 1284 if (!has_codec_negotiation_feature(hfp_connection)){ 1285 log_info("no codec negotiation feature, using NBS"); 1286 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1287 hfp_connection->suggested_codec = HFP_CODEC_CVSD; 1288 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 1289 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 1290 hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 1291 hfp_connection->state = HFP_W4_SCO_CONNECTED; 1292 } else { 1293 switch (hfp_connection->codecs_state){ 1294 case HFP_CODECS_W4_AG_COMMON_CODEC: 1295 break; 1296 default: 1297 hfp_connection->trigger_codec_exchange = 1; 1298 break; 1299 } 1300 } 1301 1302 hfp_run_for_context(hfp_connection); 1303 } 1304 1305 void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 1306 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1307 if (!hfp_connection) { 1308 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1309 return; 1310 } 1311 hfp_release_audio_connection(hfp_connection); 1312 hfp_run_for_context(hfp_connection); 1313 } 1314 1315 void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 1316 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1317 if (!hfp_connection) { 1318 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1319 return; 1320 } 1321 1322 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1323 hfp_connection->hf_answer_incoming_call = 1; 1324 hfp_run_for_context(hfp_connection); 1325 } else { 1326 log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1327 } 1328 } 1329 1330 void hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 1331 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1332 if (!hfp_connection) { 1333 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1334 return; 1335 } 1336 hfp_connection->hf_send_chup = 1; 1337 hfp_run_for_context(hfp_connection); 1338 } 1339 1340 void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 1341 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1342 if (!hfp_connection) { 1343 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1344 return; 1345 } 1346 1347 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1348 hfp_connection->hf_send_chup = 1; 1349 hfp_run_for_context(hfp_connection); 1350 } 1351 } 1352 1353 void hfp_hf_user_busy(hci_con_handle_t acl_handle){ 1354 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1355 if (!hfp_connection) { 1356 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1357 return; 1358 } 1359 1360 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1361 hfp_connection->hf_send_chld_0 = 1; 1362 hfp_run_for_context(hfp_connection); 1363 } 1364 } 1365 1366 void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 1367 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1368 if (!hfp_connection) { 1369 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1370 return; 1371 } 1372 1373 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1374 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1375 hfp_connection->hf_send_chld_1 = 1; 1376 hfp_run_for_context(hfp_connection); 1377 } 1378 } 1379 1380 void hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 1381 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1382 if (!hfp_connection) { 1383 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1384 return; 1385 } 1386 1387 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1388 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1389 hfp_connection->hf_send_chld_2 = 1; 1390 hfp_run_for_context(hfp_connection); 1391 } 1392 } 1393 1394 void hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 1395 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1396 if (!hfp_connection) { 1397 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1398 return; 1399 } 1400 1401 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1402 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1403 hfp_connection->hf_send_chld_3 = 1; 1404 hfp_run_for_context(hfp_connection); 1405 } 1406 } 1407 1408 void hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 1409 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1410 if (!hfp_connection) { 1411 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1412 return; 1413 } 1414 1415 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1416 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1417 hfp_connection->hf_send_chld_4 = 1; 1418 hfp_run_for_context(hfp_connection); 1419 } 1420 } 1421 1422 void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 1423 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1424 if (!hfp_connection) { 1425 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1426 return; 1427 } 1428 1429 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1430 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1431 hfp_connection->hf_send_chld_x = 1; 1432 hfp_connection->hf_send_chld_x_index = 10 + index; 1433 hfp_run_for_context(hfp_connection); 1434 } 1435 } 1436 1437 void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 1438 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1439 if (!hfp_connection) { 1440 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1441 return; 1442 } 1443 1444 if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1445 (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1446 hfp_connection->hf_send_chld_x = 1; 1447 hfp_connection->hf_send_chld_x_index = 20 + index; 1448 hfp_run_for_context(hfp_connection); 1449 } 1450 } 1451 1452 void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 1453 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1454 if (!hfp_connection) { 1455 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1456 return; 1457 } 1458 1459 hfp_connection->hf_initiate_outgoing_call = 1; 1460 snprintf(phone_number, sizeof(phone_number), "%s", number); 1461 hfp_run_for_context(hfp_connection); 1462 } 1463 1464 void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 1465 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1466 if (!hfp_connection) { 1467 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1468 return; 1469 } 1470 1471 hfp_connection->hf_initiate_memory_dialing = 1; 1472 hfp_connection->memory_id = memory_id; 1473 1474 hfp_run_for_context(hfp_connection); 1475 } 1476 1477 void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 1478 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1479 if (!hfp_connection) { 1480 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1481 return; 1482 } 1483 1484 hfp_connection->hf_initiate_redial_last_number = 1; 1485 hfp_run_for_context(hfp_connection); 1486 } 1487 1488 void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 1489 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1490 if (!hfp_connection) { 1491 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1492 return; 1493 } 1494 1495 hfp_connection->hf_activate_call_waiting_notification = 1; 1496 hfp_run_for_context(hfp_connection); 1497 } 1498 1499 1500 void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 1501 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1502 if (!hfp_connection) { 1503 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1504 return; 1505 } 1506 1507 hfp_connection->hf_deactivate_call_waiting_notification = 1; 1508 hfp_run_for_context(hfp_connection); 1509 } 1510 1511 1512 void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 1513 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1514 if (!hfp_connection) { 1515 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1516 return; 1517 } 1518 1519 hfp_connection->hf_activate_calling_line_notification = 1; 1520 hfp_run_for_context(hfp_connection); 1521 } 1522 1523 void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 1524 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1525 if (!hfp_connection) { 1526 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1527 return; 1528 } 1529 1530 hfp_connection->hf_deactivate_calling_line_notification = 1; 1531 hfp_run_for_context(hfp_connection); 1532 } 1533 1534 1535 void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1536 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1537 if (!hfp_connection) { 1538 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1539 return; 1540 } 1541 1542 hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1; 1543 hfp_run_for_context(hfp_connection); 1544 } 1545 1546 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1547 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1548 if (!hfp_connection) { 1549 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1550 return; 1551 } 1552 1553 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1554 hfp_run_for_context(hfp_connection); 1555 } 1556 1557 void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1558 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1559 if (!hfp_connection) { 1560 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1561 return; 1562 } 1563 1564 hfp_connection->hf_activate_voice_recognition_notification = 1; 1565 hfp_run_for_context(hfp_connection); 1566 } 1567 1568 void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1569 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1570 if (!hfp_connection) { 1571 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1572 return; 1573 } 1574 1575 hfp_connection->hf_deactivate_voice_recognition_notification = 1; 1576 hfp_run_for_context(hfp_connection); 1577 } 1578 1579 void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 1580 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1581 if (!hfp_connection) { 1582 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1583 return; 1584 } 1585 1586 if (hfp_connection->microphone_gain == gain) return; 1587 if ((gain < 0) || (gain > 15)){ 1588 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1589 return; 1590 } 1591 hfp_connection->microphone_gain = gain; 1592 hfp_connection->send_microphone_gain = 1; 1593 hfp_run_for_context(hfp_connection); 1594 } 1595 1596 void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 1597 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1598 if (!hfp_connection) { 1599 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1600 return; 1601 } 1602 1603 if (hfp_connection->speaker_gain == gain) return; 1604 if ((gain < 0) || (gain > 15)){ 1605 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1606 return; 1607 } 1608 hfp_connection->speaker_gain = gain; 1609 hfp_connection->send_speaker_gain = 1; 1610 hfp_run_for_context(hfp_connection); 1611 } 1612 1613 void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 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_send_dtmf_code = code; 1621 hfp_run_for_context(hfp_connection); 1622 } 1623 1624 void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 1625 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1626 if (!hfp_connection) { 1627 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1628 return; 1629 } 1630 hfp_connection->hf_send_binp = 1; 1631 hfp_run_for_context(hfp_connection); 1632 } 1633 1634 void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 1635 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1636 if (!hfp_connection) { 1637 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1638 return; 1639 } 1640 hfp_connection->hf_send_clcc = 1; 1641 hfp_run_for_context(hfp_connection); 1642 } 1643 1644 1645 void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 1646 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1647 if (!hfp_connection) { 1648 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1649 return; 1650 } 1651 hfp_connection->hf_send_rrh = 1; 1652 hfp_connection->hf_send_rrh_command = '?'; 1653 hfp_run_for_context(hfp_connection); 1654 } 1655 1656 void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 1657 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1658 if (!hfp_connection) { 1659 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1660 return; 1661 } 1662 hfp_connection->hf_send_rrh = 1; 1663 hfp_connection->hf_send_rrh_command = '0'; 1664 hfp_run_for_context(hfp_connection); 1665 } 1666 1667 void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 1668 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1669 if (!hfp_connection) { 1670 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1671 return; 1672 } 1673 hfp_connection->hf_send_rrh = 1; 1674 hfp_connection->hf_send_rrh_command = '1'; 1675 hfp_run_for_context(hfp_connection); 1676 } 1677 1678 void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 1679 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1680 if (!hfp_connection) { 1681 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1682 return; 1683 } 1684 hfp_connection->hf_send_rrh = 1; 1685 hfp_connection->hf_send_rrh_command = '2'; 1686 hfp_run_for_context(hfp_connection); 1687 } 1688 1689 void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 1690 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1691 if (!hfp_connection) { 1692 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1693 return; 1694 } 1695 hfp_connection->hf_send_cnum = 1; 1696 hfp_run_for_context(hfp_connection); 1697 } 1698 1699 void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 1700 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1701 if (!hfp_connection) { 1702 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1703 return; 1704 } 1705 // find index for assigned number 1706 int i; 1707 for (i = 0; i < hfp_indicators_nr ; i++){ 1708 if (hfp_indicators[i] == assigned_number){ 1709 // set value 1710 hfp_indicators_value[i] = value; 1711 // mark for update 1712 if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1713 hfp_connection->generic_status_update_bitmap |= (1<<i); 1714 // send update 1715 hfp_run_for_context(hfp_connection); 1716 } 1717 return; 1718 } 1719 } 1720 } 1721 1722 int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 1723 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1724 if (!hfp_connection) { 1725 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1726 return 0; 1727 } 1728 return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1729 } 1730 1731 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){ 1732 if (!name){ 1733 name = default_hfp_hf_service_name; 1734 } 1735 hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 1736 1737 // Construct SupportedFeatures for SDP bitmap: 1738 // 1739 // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 1740 // of the Bits 0 to 4 of the unsolicited result code +BRSF" 1741 // 1742 // Wide band speech (bit 5) requires Codec negotiation 1743 // 1744 uint16_t sdp_features = supported_features & 0x1f; 1745 if (wide_band_speech && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 1746 sdp_features |= 1 << 5; 1747 } 1748 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 1749 de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 1750 } 1751 1752 void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 1753 if (callback == NULL){ 1754 log_error("hfp_hf_register_packet_handler called with NULL callback"); 1755 return; 1756 } 1757 hfp_hf_callback = callback; 1758 hfp_set_hf_callback(callback); 1759 }