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