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