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 // ***************************************************************************** 39 // 40 // Minimal setup for HFP Hands-Free (HF) unit (!! UNDER DEVELOPMENT !!) 41 // 42 // ***************************************************************************** 43 44 #include "btstack-config.h" 45 46 #include <stdint.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 #include "hci_cmds.h" 52 #include "run_loop.h" 53 54 #include "hci.h" 55 #include "btstack_memory.h" 56 #include "hci_dump.h" 57 #include "l2cap.h" 58 #include "classic/sdp_query_rfcomm.h" 59 #include "classic/sdp.h" 60 #include "btstack_debug.h" 61 #include "classic/hfp.h" 62 #include "classic/hfp_hf.h" 63 64 65 static const char default_hfp_hf_service_name[] = "Hands-Free unit"; 66 static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 67 static uint8_t hfp_codecs_nr = 0; 68 static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 69 70 static uint8_t hfp_indicators_nr = 0; 71 static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS]; 72 static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS]; 73 static uint16_t hfp_indicators_status; 74 75 static uint8_t hfp_hf_speaker_gain = 9; 76 static uint8_t hfp_hf_microphone_gain = 9; 77 78 static hfp_callback_t hfp_callback; 79 80 static hfp_call_status_t hfp_call_status; 81 static hfp_callsetup_status_t hfp_callsetup_status; 82 static hfp_callheld_status_t hfp_callheld_status; 83 84 static char phone_number[25]; 85 86 void hfp_hf_register_packet_handler(hfp_callback_t callback){ 87 hfp_callback = callback; 88 if (callback == NULL){ 89 log_error("hfp_hf_register_packet_handler called with NULL callback"); 90 return; 91 } 92 hfp_callback = callback; 93 } 94 95 static int hfp_hf_supports_codec(uint8_t codec){ 96 int i; 97 for (i = 0; i < hfp_codecs_nr; i++){ 98 if (hfp_codecs[i] == codec) return 1; 99 } 100 return HFP_CODEC_CVSD; 101 } 102 static int has_codec_negotiation_feature(hfp_connection_t * connection){ 103 int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 104 int ag = get_bit(connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 105 return hf && ag; 106 } 107 108 static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * connection){ 109 int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING); 110 int ag = get_bit(connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING); 111 return hf && ag; 112 } 113 114 115 static int has_hf_indicators_feature(hfp_connection_t * connection){ 116 int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS); 117 int ag = get_bit(connection->remote_supported_features, HFP_AGSF_HF_INDICATORS); 118 return hf && ag; 119 } 120 121 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 122 123 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){ 124 if (!name){ 125 name = default_hfp_hf_service_name; 126 } 127 hfp_create_sdp_record(service, service_record_handle, SDP_Handsfree, rfcomm_channel_nr, name); 128 129 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 130 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 131 } 132 133 static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 134 char buffer[20]; 135 sprintf(buffer, "AT%s=%d\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features); 136 // printf("exchange_supported_features %s\n", buffer); 137 return send_str_over_rfcomm(cid, buffer); 138 } 139 140 static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 141 char buffer[30]; 142 int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_AVAILABLE_CODECS); 143 offset += join(buffer+offset, sizeof(buffer)-offset, hfp_codecs, hfp_codecs_nr); 144 offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 145 buffer[offset] = 0; 146 return send_str_over_rfcomm(cid, buffer); 147 } 148 149 static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){ 150 char buffer[20]; 151 sprintf(buffer, "AT%s=?\r\n", HFP_INDICATOR); 152 // printf("retrieve_indicators %s\n", buffer); 153 return send_str_over_rfcomm(cid, buffer); 154 } 155 156 static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){ 157 char buffer[20]; 158 sprintf(buffer, "AT%s?\r\n", HFP_INDICATOR); 159 // printf("retrieve_indicators_status %s\n", buffer); 160 return send_str_over_rfcomm(cid, buffer); 161 } 162 163 static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 164 char buffer[20]; 165 sprintf(buffer, "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 166 // printf("toggle_indicator_status_update %s\n", buffer); 167 return send_str_over_rfcomm(cid, buffer); 168 } 169 170 static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 171 char buffer[50]; 172 int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 173 offset += join_bitmap(buffer+offset, sizeof(buffer)-offset, indicators_status, indicators_nr); 174 offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 175 buffer[offset] = 0; 176 return send_str_over_rfcomm(cid, buffer); 177 } 178 179 static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 180 char buffer[20]; 181 sprintf(buffer, "AT%s=?\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 182 // printf("retrieve_can_hold_call %s\n", buffer); 183 return send_str_over_rfcomm(cid, buffer); 184 } 185 186 static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 187 char buffer[30]; 188 int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 189 offset += join(buffer+offset, sizeof(buffer)-offset, hfp_indicators, hfp_indicators_nr); 190 offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 191 buffer[offset] = 0; 192 return send_str_over_rfcomm(cid, buffer); 193 } 194 195 static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){ 196 char buffer[20]; 197 sprintf(buffer, "AT%s=?\r\n", HFP_GENERIC_STATUS_INDICATOR); 198 // printf("retrieve_supported_generic_status_indicators %s\n", buffer); 199 return send_str_over_rfcomm(cid, buffer); 200 } 201 202 static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){ 203 char buffer[20]; 204 sprintf(buffer, "AT%s?\r\n", HFP_GENERIC_STATUS_INDICATOR); 205 // printf("list_initital_supported_generic_status_indicators %s\n", buffer); 206 return send_str_over_rfcomm(cid, buffer); 207 } 208 209 static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 210 char buffer[20]; 211 sprintf(buffer, "AT%s=3,0\r\n", HFP_QUERY_OPERATOR_SELECTION); 212 return send_str_over_rfcomm(cid, buffer); 213 } 214 215 static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 216 char buffer[20]; 217 sprintf(buffer, "AT%s?\r\n", HFP_QUERY_OPERATOR_SELECTION); 218 return send_str_over_rfcomm(cid, buffer); 219 } 220 221 static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 222 char buffer[20]; 223 sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 224 return send_str_over_rfcomm(cid, buffer); 225 } 226 227 static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 228 char buffer[20]; 229 sprintf(buffer, "AT%s\r\n", HFP_TRIGGER_CODEC_CONNECTION_SETUP); 230 return send_str_over_rfcomm(cid, buffer); 231 } 232 233 static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 234 char buffer[20]; 235 sprintf(buffer, "AT%s=%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec); 236 return send_str_over_rfcomm(cid, buffer); 237 } 238 239 static int hfp_hf_cmd_ata(uint16_t cid){ 240 char buffer[10]; 241 sprintf(buffer, "%s\r\n", HFP_CALL_ANSWERED); 242 return send_str_over_rfcomm(cid, buffer); 243 } 244 245 static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 246 char buffer[40]; 247 sprintf(buffer, "AT%s=%d\r\n", HFP_SET_MICROPHONE_GAIN, gain); 248 return send_str_over_rfcomm(cid, buffer); 249 } 250 251 static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 252 char buffer[40]; 253 sprintf(buffer, "AT%s=%d\r\n", HFP_SET_SPEAKER_GAIN, gain); 254 return send_str_over_rfcomm(cid, buffer); 255 } 256 257 static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 258 char buffer[40]; 259 sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CLIP, activate); 260 return send_str_over_rfcomm(cid, buffer); 261 } 262 263 static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){ 264 char buffer[40]; 265 sprintf(buffer, "AT%s=%d\r\n", HFP_TURN_OFF_EC_AND_NR, activate); 266 return send_str_over_rfcomm(cid, buffer); 267 } 268 269 static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 270 char buffer[40]; 271 sprintf(buffer, "AT%s=%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate); 272 return send_str_over_rfcomm(cid, buffer); 273 } 274 275 static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 276 char buffer[40]; 277 sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 278 return send_str_over_rfcomm(cid, buffer); 279 } 280 281 static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 282 char buffer[40]; 283 sprintf(buffer, "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number); 284 return send_str_over_rfcomm(cid, buffer); 285 } 286 287 static int hfp_hf_send_memory_dial_cmd(uint16_t cid){ 288 char buffer[40]; 289 sprintf(buffer, "%s>%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number); 290 return send_str_over_rfcomm(cid, buffer); 291 } 292 293 static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 294 char buffer[20]; 295 sprintf(buffer, "AT%s\r\n", HFP_REDIAL_LAST_NUMBER); 296 return send_str_over_rfcomm(cid, buffer); 297 } 298 299 static int hfp_hf_send_chup(uint16_t cid){ 300 char buffer[20]; 301 sprintf(buffer, "AT%s\r\n", HFP_HANG_UP_CALL); 302 return send_str_over_rfcomm(cid, buffer); 303 } 304 305 static int hfp_hf_send_chld(uint16_t cid, int number){ 306 char buffer[20]; 307 sprintf(buffer, "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 308 return send_str_over_rfcomm(cid, buffer); 309 } 310 311 static int hfp_hf_send_dtmf(uint16_t cid, char code){ 312 char buffer[20]; 313 sprintf(buffer, "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code); 314 return send_str_over_rfcomm(cid, buffer); 315 } 316 317 static int hfp_hf_send_binp(uint16_t cid){ 318 char buffer[20]; 319 sprintf(buffer, "AT%s=1\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG); 320 return send_str_over_rfcomm(cid, buffer); 321 } 322 323 static int hfp_hf_send_clcc(uint16_t cid){ 324 char buffer[20]; 325 sprintf(buffer, "AT%s\r\n", HFP_LIST_CURRENT_CALLS); 326 return send_str_over_rfcomm(cid, buffer); 327 } 328 329 static void hfp_emit_ag_indicator_event(hfp_callback_t callback, int status, hfp_ag_indicator_t indicator){ 330 if (!callback) return; 331 uint8_t event[6+HFP_MAX_INDICATOR_DESC_SIZE+1]; 332 event[0] = HCI_EVENT_HFP_META; 333 event[1] = sizeof(event) - 2; 334 event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 335 event[3] = status; 336 event[4] = indicator.index; 337 event[5] = indicator.status; 338 strncpy((char*)&event[6], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE); 339 event[6+HFP_MAX_INDICATOR_DESC_SIZE] = 0; 340 (*callback)(event, sizeof(event)); 341 } 342 343 static void hfp_emit_network_operator_event(hfp_callback_t callback, int status, hfp_network_opearator_t network_operator){ 344 if (!callback) return; 345 uint8_t event[24]; 346 event[0] = HCI_EVENT_HFP_META; 347 event[1] = sizeof(event) - 2; 348 event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED; 349 event[3] = status; 350 event[4] = network_operator.mode; 351 event[5] = network_operator.format; 352 strcpy((char*)&event[6], network_operator.name); 353 (*callback)(event, sizeof(event)); 354 } 355 356 static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * context){ 357 if (context->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 358 if (context->ok_pending) return 0; 359 int done = 1; 360 361 switch (context->state){ 362 case HFP_EXCHANGE_SUPPORTED_FEATURES: 363 context->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 364 hfp_hf_cmd_exchange_supported_features(context->rfcomm_cid); 365 break; 366 case HFP_NOTIFY_ON_CODECS: 367 context->state = HFP_W4_NOTIFY_ON_CODECS; 368 hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid); 369 break; 370 case HFP_RETRIEVE_INDICATORS: 371 context->state = HFP_W4_RETRIEVE_INDICATORS; 372 hfp_hf_cmd_retrieve_indicators(context->rfcomm_cid); 373 break; 374 case HFP_RETRIEVE_INDICATORS_STATUS: 375 context->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 376 hfp_hf_cmd_retrieve_indicators_status(context->rfcomm_cid); 377 break; 378 case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 379 context->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 380 hfp_hf_cmd_activate_status_update_for_all_ag_indicators(context->rfcomm_cid, 1); 381 break; 382 case HFP_RETRIEVE_CAN_HOLD_CALL: 383 context->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 384 hfp_hf_cmd_retrieve_can_hold_call(context->rfcomm_cid); 385 break; 386 case HFP_LIST_GENERIC_STATUS_INDICATORS: 387 context->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 388 hfp_hf_cmd_list_supported_generic_status_indicators(context->rfcomm_cid); 389 break; 390 case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 391 context->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 392 hfp_hf_cmd_retrieve_supported_generic_status_indicators(context->rfcomm_cid); 393 break; 394 case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 395 context->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 396 hfp_hf_cmd_list_initital_supported_generic_status_indicators(context->rfcomm_cid); 397 break; 398 default: 399 done = 0; 400 break; 401 } 402 return done; 403 } 404 405 406 static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * context){ 407 if (context->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 408 if (context->ok_pending) return 0; 409 410 int done = 0; 411 if (context->enable_status_update_for_ag_indicators != 0xFF){ 412 context->ok_pending = 1; 413 done = 1; 414 hfp_hf_cmd_activate_status_update_for_all_ag_indicators(context->rfcomm_cid, context->enable_status_update_for_ag_indicators); 415 return done; 416 }; 417 if (context->change_status_update_for_individual_ag_indicators){ 418 context->ok_pending = 1; 419 done = 1; 420 hfp_hf_cmd_activate_status_update_for_ag_indicator(context->rfcomm_cid, 421 context->ag_indicators_status_update_bitmap, 422 context->ag_indicators_nr); 423 return done; 424 } 425 426 switch (context->hf_query_operator_state){ 427 case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 428 context->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 429 context->ok_pending = 1; 430 hfp_hf_cmd_query_operator_name_format(context->rfcomm_cid); 431 return 1; 432 case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 433 context->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 434 context->ok_pending = 1; 435 hfp_hf_cmd_query_operator_name(context->rfcomm_cid); 436 return 1; 437 default: 438 break; 439 } 440 441 if (context->enable_extended_audio_gateway_error_report){ 442 context->ok_pending = 1; 443 done = 1; 444 hfp_hf_cmd_enable_extended_audio_gateway_error_report(context->rfcomm_cid, context->enable_extended_audio_gateway_error_report); 445 return done; 446 } 447 448 return done; 449 } 450 451 static int codecs_exchange_state_machine(hfp_connection_t * context){ 452 /* events ( == commands): 453 HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 454 HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 455 hf_trigger_codec_connection_setup == received BCC 456 ag_trigger_codec_connection_setup == received from AG to send BCS 457 HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 458 */ 459 460 if (context->ok_pending) return 0; 461 462 switch (context->command){ 463 case HFP_CMD_AVAILABLE_CODECS: 464 if (context->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0; 465 466 context->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 467 context->ok_pending = 1; 468 hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid); 469 return 1; 470 case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 471 context->codec_confirmed = 0; 472 context->suggested_codec = 0; 473 context->negotiated_codec = 0; 474 475 context->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 476 context->ok_pending = 1; 477 hfp_hf_cmd_trigger_codec_connection_setup(context->rfcomm_cid); 478 break; 479 480 case HFP_CMD_AG_SUGGESTED_CODEC: 481 if (hfp_hf_supports_codec(context->suggested_codec)){ 482 context->codec_confirmed = context->suggested_codec; 483 context->ok_pending = 1; 484 context->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 485 hfp_hf_cmd_confirm_codec(context->rfcomm_cid, context->suggested_codec); 486 } else { 487 context->codec_confirmed = 0; 488 context->suggested_codec = 0; 489 context->negotiated_codec = 0; 490 context->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 491 context->ok_pending = 1; 492 hfp_hf_cmd_notify_on_codecs(context->rfcomm_cid); 493 494 } 495 break; 496 497 default: 498 break; 499 } 500 return 0; 501 } 502 503 static int hfp_hf_run_for_audio_connection(hfp_connection_t * context){ 504 if (context->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 505 context->state > HFP_W2_DISCONNECT_SCO) return 0; 506 507 508 if (context->state == HFP_AUDIO_CONNECTION_ESTABLISHED && context->release_audio_connection){ 509 context->state = HFP_W4_SCO_DISCONNECTED; 510 context->release_audio_connection = 0; 511 gap_disconnect(context->sco_handle); 512 return 1; 513 } 514 515 if (context->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 516 517 // run codecs exchange 518 int done = codecs_exchange_state_machine(context); 519 if (done) return 1; 520 521 if (context->establish_audio_connection){ 522 context->state = HFP_W4_SCO_CONNECTED; 523 context->establish_audio_connection = 0; 524 hfp_setup_synchronous_connection(context->con_handle, context->link_setting); 525 return 1; 526 } 527 528 return 0; 529 } 530 531 static int call_setup_state_machine(hfp_connection_t * context){ 532 if (context->hf_answer_incoming_call){ 533 hfp_hf_cmd_ata(context->rfcomm_cid); 534 context->hf_answer_incoming_call = 0; 535 return 1; 536 } 537 return 0; 538 } 539 540 static void hfp_run_for_context(hfp_connection_t * context){ 541 if (!context) return; 542 if (!rfcomm_can_send_packet_now(context->rfcomm_cid)) return; 543 544 int done = hfp_hf_run_for_context_service_level_connection(context); 545 if (!done){ 546 done = hfp_hf_run_for_context_service_level_connection_queries(context); 547 } 548 if (!done){ 549 done = hfp_hf_run_for_audio_connection(context); 550 } 551 if (!done){ 552 done = call_setup_state_machine(context); 553 } 554 555 if (context->send_microphone_gain){ 556 context->send_microphone_gain = 0; 557 context->ok_pending = 1; 558 hfp_hf_set_microphone_gain_cmd(context->rfcomm_cid, context->microphone_gain); 559 return; 560 } 561 562 if (context->send_speaker_gain){ 563 context->send_speaker_gain = 0; 564 context->ok_pending = 1; 565 hfp_hf_set_speaker_gain_cmd(context->rfcomm_cid, context->speaker_gain); 566 return; 567 } 568 569 if (context->hf_deactivate_calling_line_notification){ 570 context->hf_deactivate_calling_line_notification = 0; 571 context->ok_pending = 1; 572 hfp_hf_set_calling_line_notification_cmd(context->rfcomm_cid, 0); 573 return; 574 } 575 576 if (context->hf_activate_calling_line_notification){ 577 context->hf_activate_calling_line_notification = 0; 578 context->ok_pending = 1; 579 hfp_hf_set_calling_line_notification_cmd(context->rfcomm_cid, 1); 580 return; 581 } 582 583 if (context->hf_deactivate_echo_canceling_and_noise_reduction){ 584 context->hf_deactivate_echo_canceling_and_noise_reduction = 0; 585 context->ok_pending = 1; 586 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(context->rfcomm_cid, 0); 587 return; 588 } 589 590 if (context->hf_activate_echo_canceling_and_noise_reduction){ 591 context->hf_activate_echo_canceling_and_noise_reduction = 0; 592 context->ok_pending = 1; 593 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(context->rfcomm_cid, 1); 594 return; 595 } 596 597 if (context->hf_deactivate_voice_recognition_notification){ 598 context->hf_deactivate_voice_recognition_notification = 0; 599 context->ok_pending = 1; 600 hfp_hf_set_voice_recognition_notification_cmd(context->rfcomm_cid, 0); 601 return; 602 } 603 604 if (context->hf_activate_voice_recognition_notification){ 605 context->hf_activate_voice_recognition_notification = 0; 606 context->ok_pending = 1; 607 hfp_hf_set_voice_recognition_notification_cmd(context->rfcomm_cid, 1); 608 return; 609 } 610 611 612 if (context->hf_deactivate_call_waiting_notification){ 613 context->hf_deactivate_call_waiting_notification = 0; 614 context->ok_pending = 1; 615 hfp_hf_set_call_waiting_notification_cmd(context->rfcomm_cid, 0); 616 return; 617 } 618 619 if (context->hf_activate_call_waiting_notification){ 620 context->hf_activate_call_waiting_notification = 0; 621 context->ok_pending = 1; 622 hfp_hf_set_call_waiting_notification_cmd(context->rfcomm_cid, 1); 623 return; 624 } 625 626 if (context->hf_initiate_outgoing_call){ 627 context->hf_initiate_outgoing_call = 0; 628 context->ok_pending = 1; 629 hfp_hf_initiate_outgoing_call_cmd(context->rfcomm_cid); 630 return; 631 } 632 633 if (context->hf_initiate_memory_dialing){ 634 context->hf_initiate_memory_dialing = 0; 635 context->ok_pending = 1; 636 hfp_hf_send_memory_dial_cmd(context->rfcomm_cid); 637 return; 638 } 639 640 if (context->hf_initiate_redial_last_number){ 641 context->hf_initiate_redial_last_number = 0; 642 context->ok_pending = 1; 643 hfp_hf_send_redial_last_number_cmd(context->rfcomm_cid); 644 return; 645 } 646 647 if (context->hf_send_chup){ 648 context->hf_send_chup = 0; 649 context->ok_pending = 1; 650 hfp_hf_send_chup(context->rfcomm_cid); 651 return; 652 } 653 654 if (context->hf_send_chld_0){ 655 context->hf_send_chld_0 = 0; 656 context->ok_pending = 1; 657 hfp_hf_send_chld(context->rfcomm_cid, 0); 658 return; 659 } 660 661 if (context->hf_send_chld_1){ 662 context->hf_send_chld_1 = 0; 663 context->ok_pending = 1; 664 hfp_hf_send_chld(context->rfcomm_cid, 1); 665 return; 666 } 667 668 if (context->hf_send_chld_2){ 669 context->hf_send_chld_2 = 0; 670 context->ok_pending = 1; 671 hfp_hf_send_chld(context->rfcomm_cid, 2); 672 return; 673 } 674 675 if (context->hf_send_chld_3){ 676 context->hf_send_chld_3 = 0; 677 context->ok_pending = 1; 678 hfp_hf_send_chld(context->rfcomm_cid, 3); 679 return; 680 } 681 682 if (context->hf_send_chld_4){ 683 context->hf_send_chld_4 = 0; 684 context->ok_pending = 1; 685 hfp_hf_send_chld(context->rfcomm_cid, 4); 686 return; 687 } 688 689 if (context->hf_send_chld_x){ 690 context->hf_send_chld_x = 0; 691 context->ok_pending = 1; 692 hfp_hf_send_chld(context->rfcomm_cid, context->hf_send_chld_x_index); 693 return; 694 } 695 696 if (context->hf_send_dtmf_code){ 697 char code = context->hf_send_dtmf_code; 698 context->hf_send_dtmf_code = 0; 699 context->ok_pending = 1; 700 hfp_hf_send_dtmf(context->rfcomm_cid, code); 701 return; 702 } 703 704 if (context->hf_send_binp){ 705 context->hf_send_binp = 0; 706 context->ok_pending = 1; 707 hfp_hf_send_binp(context->rfcomm_cid); 708 return; 709 } 710 711 if (context->hf_send_clcc){ 712 context->hf_send_clcc = 0; 713 context->ok_pending = 1; 714 hfp_hf_send_clcc(context->rfcomm_cid); 715 return; 716 } 717 718 if (context->hf_send_rrh){ 719 context->hf_send_rrh = 0; 720 char buffer[20]; 721 switch (context->hf_send_rrh_command){ 722 case '?': 723 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD); 724 send_str_over_rfcomm(context->rfcomm_cid, buffer); 725 return; 726 case '0': 727 case '1': 728 case '2': 729 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, context->hf_send_rrh_command); 730 send_str_over_rfcomm(context->rfcomm_cid, buffer); 731 return; 732 default: 733 break; 734 } 735 return; 736 } 737 738 if (context->hf_send_cnum){ 739 context->hf_send_cnum = 0; 740 char buffer[20]; 741 sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION); 742 send_str_over_rfcomm(context->rfcomm_cid, buffer); 743 return; 744 } 745 746 // update HF indicators 747 if (context->generic_status_update_bitmap){ 748 int i; 749 for (i=0;i<hfp_indicators_nr;i++){ 750 if (get_bit(context->generic_status_update_bitmap, i)){ 751 if (context->generic_status_indicators[i].state){ 752 context->ok_pending = 1; 753 context->generic_status_update_bitmap = store_bit(context->generic_status_update_bitmap, i, 0); 754 char buffer[30]; 755 sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], hfp_indicators_value[i]); 756 send_str_over_rfcomm(context->rfcomm_cid, buffer); 757 } else { 758 printf("Not sending HF indicator %u as it is disabled\n", hfp_indicators[i]); 759 } 760 return; 761 } 762 } 763 } 764 765 if (done) return; 766 // deal with disconnect 767 switch (context->state){ 768 case HFP_W2_DISCONNECT_RFCOMM: 769 context->state = HFP_W4_RFCOMM_DISCONNECTED; 770 rfcomm_disconnect_internal(context->rfcomm_cid); 771 break; 772 773 default: 774 break; 775 } 776 } 777 778 static void hfp_init_link_settings(hfp_connection_t * context){ 779 // determine highest possible link setting 780 context->link_setting = HFP_LINK_SETTINGS_D1; 781 if (hci_remote_eSCO_supported(context->con_handle)){ 782 context->link_setting = HFP_LINK_SETTINGS_S3; 783 if ((hfp_supported_features & (1<<HFP_HFSF_ESCO_S4)) 784 && (context->remote_supported_features & (1<<HFP_AGSF_ESCO_S4))){ 785 context->link_setting = HFP_LINK_SETTINGS_S4; 786 } 787 } 788 } 789 790 static void hfp_ag_slc_established(hfp_connection_t * context){ 791 context->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 792 hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0); 793 hfp_init_link_settings(context); 794 // restore volume settings 795 context->speaker_gain = hfp_hf_speaker_gain; 796 context->send_speaker_gain = 1; 797 hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 798 context->microphone_gain = hfp_hf_microphone_gain; 799 context->send_microphone_gain = 1; 800 hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 801 // enable all indicators 802 int i; 803 for (i=0;i<hfp_indicators_nr;i++){ 804 context->generic_status_indicators[i].uuid = hfp_indicators[i]; 805 context->generic_status_indicators[i].state = 1; 806 } 807 } 808 809 static void hfp_hf_switch_on_ok(hfp_connection_t *context){ 810 context->ok_pending = 0; 811 int done = 1; 812 switch (context->state){ 813 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 814 if (has_codec_negotiation_feature(context)){ 815 context->state = HFP_NOTIFY_ON_CODECS; 816 break; 817 } 818 context->state = HFP_RETRIEVE_INDICATORS; 819 break; 820 821 case HFP_W4_NOTIFY_ON_CODECS: 822 context->state = HFP_RETRIEVE_INDICATORS; 823 break; 824 825 case HFP_W4_RETRIEVE_INDICATORS: 826 context->state = HFP_RETRIEVE_INDICATORS_STATUS; 827 break; 828 829 case HFP_W4_RETRIEVE_INDICATORS_STATUS: 830 context->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 831 break; 832 833 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 834 if (has_call_waiting_and_3way_calling_feature(context)){ 835 context->state = HFP_RETRIEVE_CAN_HOLD_CALL; 836 break; 837 } 838 if (has_hf_indicators_feature(context)){ 839 context->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 840 break; 841 } 842 hfp_ag_slc_established(context); 843 break; 844 845 case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 846 if (has_hf_indicators_feature(context)){ 847 context->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 848 break; 849 } 850 hfp_ag_slc_established(context); 851 break; 852 853 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 854 context->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 855 break; 856 857 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 858 context->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 859 break; 860 861 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 862 hfp_ag_slc_established(context); 863 break; 864 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 865 if (context->enable_status_update_for_ag_indicators != 0xFF){ 866 context->enable_status_update_for_ag_indicators = 0xFF; 867 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0); 868 break; 869 } 870 871 if (context->change_status_update_for_individual_ag_indicators == 1){ 872 context->change_status_update_for_individual_ag_indicators = 0; 873 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0); 874 break; 875 } 876 877 switch (context->hf_query_operator_state){ 878 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 879 printf("Format set, querying name\n"); 880 context->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 881 break; 882 case HPF_HF_QUERY_OPERATOR_W4_RESULT: 883 context->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 884 hfp_emit_network_operator_event(hfp_callback, 0, context->network_operator); 885 break; 886 default: 887 break; 888 } 889 890 if (context->enable_extended_audio_gateway_error_report){ 891 context->enable_extended_audio_gateway_error_report = 0; 892 break; 893 } 894 895 switch (context->codecs_state){ 896 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 897 context->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 898 break; 899 case HFP_CODECS_HF_CONFIRMED_CODEC: 900 context->codecs_state = HFP_CODECS_EXCHANGED; 901 hfp_emit_event(hfp_callback, HFP_SUBEVENT_CODECS_CONNECTION_COMPLETE, 0); 902 break; 903 default: 904 done = 0; 905 break; 906 } 907 break; 908 default: 909 done = 0; 910 break; 911 } 912 913 // done 914 context->command = HFP_CMD_NONE; 915 } 916 917 918 static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 919 hfp_connection_t * context = get_hfp_connection_context_for_rfcomm_cid(channel); 920 if (!context) return; 921 922 char last_char = packet[size-1]; 923 packet[size-1] = 0; 924 log_info("HFP_RX %s", packet); 925 packet[size-1] = last_char; 926 927 int pos, i, value; 928 for (pos = 0; pos < size ; pos++){ 929 hfp_parse(context, packet[pos], 1); 930 } 931 932 switch (context->command){ 933 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 934 context->command = HFP_CMD_NONE; 935 printf("Subscriber Number: number %s, type %u\n", context->bnip_number, context->bnip_type); 936 break; 937 case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 938 context->command = HFP_CMD_NONE; 939 printf("Response and Hold status: %s\n", context->line_buffer); 940 break; 941 case HFP_CMD_LIST_CURRENT_CALLS: 942 context->command = HFP_CMD_NONE; 943 printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n", 944 context->clcc_idx, context->clcc_dir, context->clcc_status, context->clcc_mpty, 945 context->bnip_number, context->bnip_type); 946 break; 947 case HFP_CMD_SET_SPEAKER_GAIN: 948 context->command = HFP_CMD_NONE; 949 value = atoi((char*)context->line_buffer); 950 hfp_hf_speaker_gain = value; 951 hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value); 952 break; 953 case HFP_CMD_SET_MICROPHONE_GAIN: 954 context->command = HFP_CMD_NONE; 955 value = atoi((char*)context->line_buffer); 956 hfp_hf_microphone_gain = value; 957 hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 958 break; 959 case HFP_CMD_AG_SENT_PHONE_NUMBER: 960 context->command = HFP_CMD_NONE; 961 hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, context->bnip_number); 962 break; 963 case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 964 context->ok_pending = 0; 965 context->extended_audio_gateway_error = 0; 966 context->command = HFP_CMD_NONE; 967 hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, context->extended_audio_gateway_error); 968 break; 969 case HFP_CMD_ERROR: 970 context->ok_pending = 0; 971 hfp_reset_context_flags(context); 972 context->command = HFP_CMD_NONE; 973 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1); 974 break; 975 case HFP_CMD_OK: 976 hfp_hf_switch_on_ok(context); 977 break; 978 case HFP_CMD_RING: 979 hfp_emit_event(hfp_callback, HFP_SUBEVENT_RING, 0); 980 break; 981 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 982 for (i = 0; i < context->ag_indicators_nr; i++){ 983 if (context->ag_indicators[i].status_changed) { 984 if (strcmp(context->ag_indicators[i].name, "callsetup") == 0){ 985 hfp_callsetup_status = (hfp_callsetup_status_t) context->ag_indicators[i].status; 986 } else if (strcmp(context->ag_indicators[i].name, "callheld") == 0){ 987 hfp_callheld_status = (hfp_callheld_status_t) context->ag_indicators[i].status; 988 } else if (strcmp(context->ag_indicators[i].name, "call") == 0){ 989 hfp_call_status = (hfp_call_status_t) context->ag_indicators[i].status; 990 } 991 context->ag_indicators[i].status_changed = 0; 992 hfp_emit_ag_indicator_event(hfp_callback, 0, context->ag_indicators[i]); 993 break; 994 } 995 } 996 break; 997 default: 998 break; 999 } 1000 hfp_run_for_context(context); 1001 } 1002 1003 static void hfp_run(){ 1004 btstack_linked_list_iterator_t it; 1005 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1006 while (btstack_linked_list_iterator_has_next(&it)){ 1007 hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1008 hfp_run_for_context(connection); 1009 } 1010 } 1011 1012 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1013 switch (packet_type){ 1014 case RFCOMM_DATA_PACKET: 1015 hfp_handle_rfcomm_event(packet_type, channel, packet, size); 1016 break; 1017 case HCI_EVENT_PACKET: 1018 hfp_handle_hci_event(hfp_callback, packet_type, packet, size); 1019 default: 1020 break; 1021 } 1022 hfp_run(); 1023 } 1024 1025 void hfp_hf_set_codecs(uint8_t * codecs, int codecs_nr){ 1026 if (codecs_nr > HFP_MAX_NUM_CODECS){ 1027 log_error("hfp_hf_set_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 1028 return; 1029 } 1030 1031 hfp_codecs_nr = codecs_nr; 1032 int i; 1033 for (i=0; i<codecs_nr; i++){ 1034 hfp_codecs[i] = codecs[i]; 1035 } 1036 1037 char buffer[30]; 1038 int offset = join(buffer, sizeof(buffer), hfp_codecs, hfp_codecs_nr); 1039 buffer[offset] = 0; 1040 btstack_linked_list_iterator_t it; 1041 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1042 while (btstack_linked_list_iterator_has_next(&it)){ 1043 hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1044 if (!connection) continue; 1045 connection->command = HFP_CMD_AVAILABLE_CODECS; 1046 hfp_run_for_context(connection); 1047 } 1048 } 1049 1050 void hfp_hf_init(uint16_t rfcomm_channel_nr, uint32_t supported_features, uint16_t * indicators, int indicators_nr, uint32_t indicators_status){ 1051 l2cap_init(); 1052 l2cap_register_packet_handler(packet_handler); 1053 rfcomm_register_packet_handler(packet_handler); 1054 hfp_init(rfcomm_channel_nr); 1055 1056 hfp_supported_features = supported_features; 1057 1058 hfp_indicators_nr = indicators_nr; 1059 hfp_indicators_status = indicators_status; 1060 int i; 1061 for (i=0; i<indicators_nr; i++){ 1062 hfp_indicators[i] = indicators[i]; 1063 } 1064 } 1065 1066 void hfp_hf_set_supported_features(uint32_t supported_features){ 1067 hfp_supported_features = supported_features; 1068 } 1069 1070 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1071 hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway); 1072 } 1073 1074 void hfp_hf_release_service_level_connection(bd_addr_t bd_addr){ 1075 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1076 hfp_release_service_level_connection(connection); 1077 hfp_run_for_context(connection); 1078 } 1079 1080 static void hfp_hf_set_status_update_for_all_ag_indicators(bd_addr_t bd_addr, uint8_t enable){ 1081 hfp_hf_establish_service_level_connection(bd_addr); 1082 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1083 if (!connection){ 1084 log_error("HFP HF: connection doesn't exist."); 1085 return; 1086 } 1087 connection->enable_status_update_for_ag_indicators = enable; 1088 hfp_run_for_context(connection); 1089 } 1090 1091 void hfp_hf_enable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){ 1092 hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 1); 1093 } 1094 1095 void hfp_hf_disable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){ 1096 hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 0); 1097 } 1098 1099 // TODO: returned ERROR - wrong format 1100 void hfp_hf_set_status_update_for_individual_ag_indicators(bd_addr_t bd_addr, uint32_t indicators_status_bitmap){ 1101 hfp_hf_establish_service_level_connection(bd_addr); 1102 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1103 if (!connection){ 1104 log_error("HFP HF: connection doesn't exist."); 1105 return; 1106 } 1107 connection->change_status_update_for_individual_ag_indicators = 1; 1108 connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1109 hfp_run_for_context(connection); 1110 } 1111 1112 void hfp_hf_query_operator_selection(bd_addr_t bd_addr){ 1113 hfp_hf_establish_service_level_connection(bd_addr); 1114 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1115 if (!connection){ 1116 log_error("HFP HF: connection doesn't exist."); 1117 return; 1118 } 1119 switch (connection->hf_query_operator_state){ 1120 case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1121 connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1122 break; 1123 case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1124 connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1125 break; 1126 default: 1127 break; 1128 } 1129 hfp_run_for_context(connection); 1130 } 1131 1132 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, uint8_t enable){ 1133 hfp_hf_establish_service_level_connection(bd_addr); 1134 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1135 if (!connection){ 1136 log_error("HFP HF: connection doesn't exist."); 1137 return; 1138 } 1139 connection->enable_extended_audio_gateway_error_report = enable; 1140 hfp_run_for_context(connection); 1141 } 1142 1143 1144 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){ 1145 hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 1); 1146 } 1147 1148 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){ 1149 hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 0); 1150 } 1151 1152 1153 void hfp_hf_establish_audio_connection(bd_addr_t bd_addr){ 1154 hfp_hf_establish_service_level_connection(bd_addr); 1155 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1156 connection->establish_audio_connection = 0; 1157 1158 if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1159 if (connection->state >= HFP_W2_DISCONNECT_SCO) return; 1160 1161 if (!has_codec_negotiation_feature(connection)){ 1162 log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); 1163 connection->codecs_state = HFP_CODECS_EXCHANGED; 1164 connection->establish_audio_connection = 1; 1165 } else { 1166 switch (connection->codecs_state){ 1167 case HFP_CODECS_W4_AG_COMMON_CODEC: 1168 break; 1169 default: 1170 connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 1171 break; 1172 } 1173 } 1174 1175 hfp_run_for_context(connection); 1176 } 1177 1178 void hfp_hf_release_audio_connection(bd_addr_t bd_addr){ 1179 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1180 hfp_release_audio_connection(connection); 1181 hfp_run_for_context(connection); 1182 } 1183 1184 void hfp_hf_answer_incoming_call(bd_addr_t bd_addr){ 1185 hfp_hf_establish_service_level_connection(bd_addr); 1186 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1187 1188 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1189 connection->hf_answer_incoming_call = 1; 1190 hfp_run_for_context(connection); 1191 } else { 1192 log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1193 } 1194 } 1195 1196 void hfp_hf_terminate_call(bd_addr_t bd_addr){ 1197 hfp_hf_establish_service_level_connection(bd_addr); 1198 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1199 1200 // if (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1201 connection->hf_send_chup = 1; 1202 hfp_run_for_context(connection); 1203 // } else { 1204 // log_error("HFP HF: terminating incoming call with wrong call status %u", hfp_call_status); 1205 // } 1206 } 1207 1208 void hfp_hf_reject_call(bd_addr_t bd_addr){ 1209 hfp_hf_establish_service_level_connection(bd_addr); 1210 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1211 1212 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1213 connection->hf_send_chup = 1; 1214 hfp_run_for_context(connection); 1215 } 1216 } 1217 1218 void hfp_hf_user_busy(bd_addr_t addr){ 1219 hfp_hf_establish_service_level_connection(addr); 1220 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1221 1222 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1223 connection->hf_send_chld_0 = 1; 1224 hfp_run_for_context(connection); 1225 } 1226 } 1227 1228 void hfp_hf_end_active_and_accept_other(bd_addr_t addr){ 1229 hfp_hf_establish_service_level_connection(addr); 1230 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1231 1232 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1233 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1234 connection->hf_send_chld_1 = 1; 1235 hfp_run_for_context(connection); 1236 } 1237 } 1238 1239 void hfp_hf_swap_calls(bd_addr_t addr){ 1240 hfp_hf_establish_service_level_connection(addr); 1241 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1242 1243 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1244 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1245 connection->hf_send_chld_2 = 1; 1246 hfp_run_for_context(connection); 1247 } 1248 } 1249 1250 void hfp_hf_join_held_call(bd_addr_t addr){ 1251 hfp_hf_establish_service_level_connection(addr); 1252 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1253 1254 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1255 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1256 connection->hf_send_chld_3 = 1; 1257 hfp_run_for_context(connection); 1258 } 1259 } 1260 1261 void hfp_hf_connect_calls(bd_addr_t addr){ 1262 hfp_hf_establish_service_level_connection(addr); 1263 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1264 1265 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1266 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1267 connection->hf_send_chld_4 = 1; 1268 hfp_run_for_context(connection); 1269 } 1270 } 1271 1272 void hfp_hf_release_call_with_index(bd_addr_t addr, int index){ 1273 hfp_hf_establish_service_level_connection(addr); 1274 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1275 1276 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1277 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1278 connection->hf_send_chld_x = 1; 1279 connection->hf_send_chld_x_index = 10 + index; 1280 hfp_run_for_context(connection); 1281 } 1282 } 1283 1284 void hfp_hf_private_consultation_with_call(bd_addr_t addr, int index){ 1285 hfp_hf_establish_service_level_connection(addr); 1286 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1287 1288 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1289 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1290 connection->hf_send_chld_x = 1; 1291 connection->hf_send_chld_x_index = 20 + index; 1292 hfp_run_for_context(connection); 1293 } 1294 } 1295 1296 void hfp_hf_dial_number(bd_addr_t bd_addr, char * number){ 1297 hfp_hf_establish_service_level_connection(bd_addr); 1298 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1299 1300 connection->hf_initiate_outgoing_call = 1; 1301 snprintf(phone_number, sizeof(phone_number), "%s", number); 1302 hfp_run_for_context(connection); 1303 } 1304 1305 void hfp_hf_dial_memory(bd_addr_t bd_addr, char * number){ 1306 hfp_hf_establish_service_level_connection(bd_addr); 1307 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1308 1309 connection->hf_initiate_memory_dialing = 1; 1310 snprintf(phone_number, sizeof(phone_number), "%s", number); 1311 hfp_run_for_context(connection); 1312 } 1313 1314 void hfp_hf_redial_last_number(bd_addr_t bd_addr){ 1315 hfp_hf_establish_service_level_connection(bd_addr); 1316 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1317 1318 connection->hf_initiate_redial_last_number = 1; 1319 hfp_run_for_context(connection); 1320 } 1321 1322 void hfp_hf_activate_call_waiting_notification(bd_addr_t bd_addr){ 1323 hfp_hf_establish_service_level_connection(bd_addr); 1324 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1325 1326 connection->hf_activate_call_waiting_notification = 1; 1327 hfp_run_for_context(connection); 1328 } 1329 1330 1331 void hfp_hf_deactivate_call_waiting_notification(bd_addr_t bd_addr){ 1332 hfp_hf_establish_service_level_connection(bd_addr); 1333 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1334 1335 connection->hf_deactivate_call_waiting_notification = 1; 1336 hfp_run_for_context(connection); 1337 } 1338 1339 1340 void hfp_hf_activate_calling_line_notification(bd_addr_t bd_addr){ 1341 hfp_hf_establish_service_level_connection(bd_addr); 1342 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1343 1344 connection->hf_activate_calling_line_notification = 1; 1345 hfp_run_for_context(connection); 1346 } 1347 1348 /* 1349 * @brief 1350 */ 1351 void hfp_hf_deactivate_calling_line_notification(bd_addr_t bd_addr){ 1352 hfp_hf_establish_service_level_connection(bd_addr); 1353 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1354 1355 connection->hf_deactivate_calling_line_notification = 1; 1356 hfp_run_for_context(connection); 1357 } 1358 1359 1360 /* 1361 * @brief 1362 */ 1363 void hfp_hf_activate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){ 1364 hfp_hf_establish_service_level_connection(bd_addr); 1365 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1366 1367 connection->hf_activate_echo_canceling_and_noise_reduction = 1; 1368 hfp_run_for_context(connection); 1369 } 1370 1371 /* 1372 * @brief 1373 */ 1374 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){ 1375 hfp_hf_establish_service_level_connection(bd_addr); 1376 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1377 1378 connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1379 hfp_run_for_context(connection); 1380 } 1381 1382 /* 1383 * @brief 1384 */ 1385 void hfp_hf_activate_voice_recognition_notification(bd_addr_t bd_addr){ 1386 hfp_hf_establish_service_level_connection(bd_addr); 1387 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1388 1389 connection->hf_activate_voice_recognition_notification = 1; 1390 hfp_run_for_context(connection); 1391 } 1392 1393 /* 1394 * @brief 1395 */ 1396 void hfp_hf_deactivate_voice_recognition_notification(bd_addr_t bd_addr){ 1397 hfp_hf_establish_service_level_connection(bd_addr); 1398 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1399 1400 connection->hf_deactivate_voice_recognition_notification = 1; 1401 hfp_run_for_context(connection); 1402 } 1403 1404 /* 1405 * @brief 1406 */ 1407 void hfp_hf_set_microphone_gain(bd_addr_t bd_addr, int gain){ 1408 hfp_hf_establish_service_level_connection(bd_addr); 1409 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1410 if (connection->microphone_gain == gain) return; 1411 1412 connection->microphone_gain = gain; 1413 connection->send_microphone_gain = 1; 1414 hfp_run_for_context(connection); 1415 } 1416 1417 /* 1418 * @brief 1419 */ 1420 void hfp_hf_set_speaker_gain(bd_addr_t bd_addr, int gain){ 1421 hfp_hf_establish_service_level_connection(bd_addr); 1422 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1423 if (connection->speaker_gain == gain) return; 1424 1425 connection->speaker_gain = gain; 1426 connection->send_speaker_gain = 1; 1427 hfp_run_for_context(connection); 1428 } 1429 1430 /* 1431 * @brief 1432 */ 1433 void hfp_hf_send_dtmf_code(bd_addr_t addr, char code){ 1434 hfp_hf_establish_service_level_connection(addr); 1435 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1436 connection->hf_send_dtmf_code = code; 1437 hfp_run_for_context(connection); 1438 } 1439 1440 /* 1441 * @brief 1442 */ 1443 void hfp_hf_request_phone_number_for_voice_tag(bd_addr_t addr){ 1444 hfp_hf_establish_service_level_connection(addr); 1445 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1446 connection->hf_send_binp = 1; 1447 hfp_run_for_context(connection); 1448 } 1449 1450 /* 1451 * @brief 1452 */ 1453 void hfp_hf_query_current_call_status(bd_addr_t addr){ 1454 hfp_hf_establish_service_level_connection(addr); 1455 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1456 connection->hf_send_clcc = 1; 1457 hfp_run_for_context(connection); 1458 } 1459 1460 1461 /* 1462 * @brief 1463 */ 1464 void hfp_hf_rrh_query_status(bd_addr_t addr){ 1465 hfp_hf_establish_service_level_connection(addr); 1466 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1467 connection->hf_send_rrh = 1; 1468 connection->hf_send_rrh_command = '?'; 1469 hfp_run_for_context(connection); 1470 } 1471 1472 /* 1473 * @brief 1474 */ 1475 void hfp_hf_rrh_hold_call(bd_addr_t addr){ 1476 hfp_hf_establish_service_level_connection(addr); 1477 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1478 connection->hf_send_rrh = 1; 1479 connection->hf_send_rrh_command = '0'; 1480 hfp_run_for_context(connection); 1481 } 1482 1483 /* 1484 * @brief 1485 */ 1486 void hfp_hf_rrh_accept_held_call(bd_addr_t addr){ 1487 hfp_hf_establish_service_level_connection(addr); 1488 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1489 connection->hf_send_rrh = 1; 1490 connection->hf_send_rrh_command = '1'; 1491 hfp_run_for_context(connection); 1492 } 1493 1494 /* 1495 * @brief 1496 */ 1497 void hfp_hf_rrh_reject_held_call(bd_addr_t addr) 1498 { 1499 hfp_hf_establish_service_level_connection(addr); 1500 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1501 connection->hf_send_rrh = 1; 1502 connection->hf_send_rrh_command = '2'; 1503 hfp_run_for_context(connection); 1504 } 1505 1506 /* 1507 * @brief 1508 */ 1509 void hfp_hf_query_subscriber_number(bd_addr_t addr) 1510 { 1511 hfp_hf_establish_service_level_connection(addr); 1512 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1513 connection->hf_send_cnum = 1; 1514 hfp_run_for_context(connection); 1515 } 1516 1517 /* 1518 * @brief 1519 */ 1520 void hfp_hf_set_hf_indicator(bd_addr_t addr, int assigned_number, int value){ 1521 hfp_hf_establish_service_level_connection(addr); 1522 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr); 1523 // find index for assigned number 1524 int i; 1525 for (i = 0; i < hfp_indicators_nr ; i++){ 1526 if (hfp_indicators[i] == assigned_number){ 1527 // set value 1528 hfp_indicators_value[i] = value; 1529 // mark for update 1530 connection->generic_status_update_bitmap |= (1<<i); 1531 // send update 1532 hfp_run_for_context(connection); 1533 return; 1534 } 1535 } 1536 } 1537 1538