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 Audio Gateway (AG) unit (!! UNDER DEVELOPMENT !!) 41 // 42 // ***************************************************************************** 43 44 #include "btstack-config.h" 45 46 #include <stdint.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 51 #include "hci_cmds.h" 52 #include "run_loop.h" 53 54 #include "hci.h" 55 #include "btstack_memory.h" 56 #include "hci_dump.h" 57 #include "l2cap.h" 58 #include "classic/sdp_query_rfcomm.h" 59 #include "classic/sdp.h" 60 #include "debug.h" 61 #include "classic/hfp.h" 62 #include "classic/hfp_ag.h" 63 64 static const char default_hfp_ag_service_name[] = "Voice gateway"; 65 66 static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES; 67 68 static uint8_t hfp_codecs_nr = 0; 69 static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 70 71 static int hfp_ag_indicators_nr = 0; 72 static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_AG_INDICATORS]; 73 74 static int hfp_ag_call_hold_services_nr = 0; 75 static char *hfp_ag_call_hold_services[6]; 76 static hfp_callback_t hfp_callback; 77 78 79 static hfp_call_status_t hfp_ag_call_state; 80 static hfp_callsetup_status_t hfp_ag_callsetup_state; 81 static hfp_callheld_status_t hfp_ag_callheld_state; 82 static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state; 83 static int hfp_ag_response_and_hold_active = 0; 84 85 // CLIP feature 86 static uint8_t clip_type; // 0 == not set 87 static char clip_number[25]; // 88 89 // Subcriber information entries 90 static hfp_phone_number_t * subscriber_numbers = NULL; 91 static int subscriber_numbers_count = 0; 92 93 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 94 static void hfp_run_for_context(hfp_connection_t *context); 95 static void hfp_ag_setup_audio_connection(hfp_connection_t * connection); 96 static void hfp_ag_hf_start_ringing(hfp_connection_t * context); 97 98 hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(); 99 int get_hfp_generic_status_indicators_nr(); 100 void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr); 101 void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr); 102 int get_hfp_ag_indicators_nr(hfp_connection_t * context); 103 hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context); 104 105 hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context){ 106 // TODO: save only value, and value changed in the context? 107 if (context->ag_indicators_nr != hfp_ag_indicators_nr){ 108 context->ag_indicators_nr = hfp_ag_indicators_nr; 109 memcpy(context->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 110 } 111 return (hfp_ag_indicator_t *)&(context->ag_indicators); 112 } 113 114 static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){ 115 int i; 116 for (i = 0; i < hfp_ag_indicators_nr; i++){ 117 if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 118 return &hfp_ag_indicators[i]; 119 } 120 } 121 return NULL; 122 } 123 124 static int get_ag_indicator_index_for_name(const char * name){ 125 int i; 126 for (i = 0; i < hfp_ag_indicators_nr; i++){ 127 if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 128 return i; 129 } 130 } 131 return -1; 132 } 133 134 void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr){ 135 memcpy(hfp_ag_indicators, indicators, indicator_nr * sizeof(hfp_ag_indicator_t)); 136 hfp_ag_indicators_nr = indicator_nr; 137 } 138 139 int get_hfp_ag_indicators_nr(hfp_connection_t * context){ 140 if (context->ag_indicators_nr != hfp_ag_indicators_nr){ 141 context->ag_indicators_nr = hfp_ag_indicators_nr; 142 memcpy(context->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 143 } 144 return context->ag_indicators_nr; 145 } 146 147 148 void hfp_ag_register_packet_handler(hfp_callback_t callback){ 149 if (callback == NULL){ 150 log_error("hfp_ag_register_packet_handler called with NULL callback"); 151 return; 152 } 153 hfp_callback = callback; 154 } 155 156 static int use_in_band_tone(){ 157 return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 158 } 159 160 static int has_codec_negotiation_feature(hfp_connection_t * connection){ 161 int hf = get_bit(connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 162 int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 163 return hf && ag; 164 } 165 166 static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * connection){ 167 int hf = get_bit(connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING); 168 int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING); 169 return hf && ag; 170 } 171 172 static int has_hf_indicators_feature(hfp_connection_t * connection){ 173 int hf = get_bit(connection->remote_supported_features, HFP_HFSF_HF_INDICATORS); 174 int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS); 175 return hf && ag; 176 } 177 178 void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint8_t ability_to_reject_call, uint16_t supported_features){ 179 if (!name){ 180 name = default_hfp_ag_service_name; 181 } 182 hfp_create_sdp_record(service, service_record_handle, SDP_HandsfreeAudioGateway, rfcomm_channel_nr, name); 183 184 /* 185 * 0x01 – Ability to reject a call 186 * 0x00 – No ability to reject a call 187 */ 188 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 189 de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 190 191 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 192 de_add_number(service, DE_UINT, DE_SIZE_16, supported_features); 193 } 194 195 static int hfp_ag_change_in_band_ring_tone_setting_cmd(uint16_t cid){ 196 char buffer[20]; 197 sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone()); 198 return send_str_over_rfcomm(cid, buffer); 199 } 200 201 static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){ 202 char buffer[40]; 203 sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features); 204 return send_str_over_rfcomm(cid, buffer); 205 } 206 207 static int hfp_ag_ok(uint16_t cid){ 208 char buffer[10]; 209 sprintf(buffer, "\r\nOK\r\n"); 210 return send_str_over_rfcomm(cid, buffer); 211 } 212 213 static int hfp_ag_ring(uint16_t cid){ 214 return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n"); 215 } 216 217 static int hfp_ag_send_clip(uint16_t cid){ 218 if (!clip_type){ 219 clip_number[0] = 0; 220 } 221 char buffer[50]; 222 sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, clip_number, clip_type); 223 return send_str_over_rfcomm(cid, buffer); 224 } 225 226 static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){ 227 char buffer[50]; 228 sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type); 229 return send_str_over_rfcomm(cid, buffer); 230 } 231 232 static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){ 233 char buffer[50]; 234 sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, clip_number); 235 return send_str_over_rfcomm(cid, buffer); 236 } 237 238 static int hfp_ag_send_call_waiting_notification(uint16_t cid){ 239 if (!clip_type){ 240 clip_number[0] = 0; 241 } 242 char buffer[50]; 243 sprintf(buffer, "\r\n+CCWA: \"%s\",%u\r\n", clip_number, clip_type); 244 return send_str_over_rfcomm(cid, buffer); 245 } 246 247 static int hfp_ag_error(uint16_t cid){ 248 char buffer[10]; 249 sprintf(buffer, "\r\nERROR\r\n"); 250 return send_str_over_rfcomm(cid, buffer); 251 } 252 253 static int hfp_ag_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){ 254 char buffer[20]; 255 sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error); 256 return send_str_over_rfcomm(cid, buffer); 257 } 258 259 static int hfp_ag_indicators_join(char * buffer, int buffer_size, hfp_connection_t * context){ 260 if (buffer_size < get_hfp_ag_indicators_nr(context) * (1 + sizeof(hfp_ag_indicator_t))) return 0; 261 int i; 262 int offset = 0; 263 for (i = 0; i < get_hfp_ag_indicators_nr(context)-1; i++) { 264 offset += snprintf(buffer+offset, buffer_size-offset, "(\"%s\",(%d,%d)),", 265 get_hfp_ag_indicators(context)[i].name, 266 get_hfp_ag_indicators(context)[i].min_range, 267 get_hfp_ag_indicators(context)[i].max_range); 268 } 269 if ( i < get_hfp_ag_indicators_nr(context)){ 270 offset += snprintf(buffer+offset, buffer_size-offset, "(\"%s\",(%d,%d))", 271 get_hfp_ag_indicators(context)[i].name, 272 get_hfp_ag_indicators(context)[i].min_range, 273 get_hfp_ag_indicators(context)[i].max_range); 274 } 275 return offset; 276 } 277 278 static int hfp_hf_indicators_join(char * buffer, int buffer_size){ 279 if (buffer_size < hfp_ag_indicators_nr * 3) return 0; 280 int i; 281 int offset = 0; 282 for (i = 0; i < get_hfp_generic_status_indicators_nr()-1; i++) { 283 offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_hfp_generic_status_indicators()[i].uuid); 284 } 285 if (i < get_hfp_generic_status_indicators_nr()){ 286 offset += snprintf(buffer+offset, buffer_size-offset, "%d,", get_hfp_generic_status_indicators()[i].uuid); 287 } 288 return offset; 289 } 290 291 static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){ 292 if (buffer_size < get_hfp_generic_status_indicators_nr() * 3) return 0; 293 int i; 294 int offset = 0; 295 for (i = 0; i < get_hfp_generic_status_indicators_nr(); i++) { 296 offset += snprintf(buffer+offset, buffer_size-offset, "\r\n%s:%d,%d\r\n", HFP_GENERIC_STATUS_INDICATOR, get_hfp_generic_status_indicators()[i].uuid, get_hfp_generic_status_indicators()[i].state); 297 } 298 return offset; 299 } 300 301 static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){ 302 if (buffer_size < hfp_ag_indicators_nr * 3) return 0; 303 int i; 304 int offset = 0; 305 for (i = 0; i < hfp_ag_indicators_nr-1; i++) { 306 offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status); 307 } 308 if (i<hfp_ag_indicators_nr){ 309 offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status); 310 } 311 return offset; 312 } 313 314 static int hfp_ag_call_services_join(char * buffer, int buffer_size){ 315 if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0; 316 int i; 317 int offset = snprintf(buffer, buffer_size, "("); 318 for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) { 319 offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]); 320 } 321 if (i<hfp_ag_call_hold_services_nr){ 322 offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]); 323 } 324 return offset; 325 } 326 327 static int hfp_ag_retrieve_indicators_cmd(uint16_t cid, hfp_connection_t * context){ 328 char buffer[250]; 329 int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_INDICATOR); 330 offset += hfp_ag_indicators_join(buffer+offset, sizeof(buffer)-offset, context); 331 332 buffer[offset] = 0; 333 334 offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 335 buffer[offset] = 0; 336 return send_str_over_rfcomm(cid, buffer); 337 } 338 339 static int hfp_ag_retrieve_indicators_status_cmd(uint16_t cid){ 340 char buffer[40]; 341 int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_INDICATOR); 342 offset += hfp_ag_indicators_status_join(buffer+offset, sizeof(buffer)-offset); 343 344 buffer[offset] = 0; 345 346 offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 347 buffer[offset] = 0; 348 return send_str_over_rfcomm(cid, buffer); 349 } 350 351 static int hfp_ag_set_indicator_status_update_cmd(uint16_t cid, uint8_t activate){ 352 // AT\r\n%s:3,0,0,%d\r\n 353 return hfp_ag_ok(cid); 354 } 355 356 357 static int hfp_ag_retrieve_can_hold_call_cmd(uint16_t cid){ 358 char buffer[100]; 359 int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 360 offset += hfp_ag_call_services_join(buffer+offset, sizeof(buffer)-offset); 361 362 buffer[offset] = 0; 363 364 offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 365 buffer[offset] = 0; 366 return send_str_over_rfcomm(cid, buffer); 367 } 368 369 370 static int hfp_ag_list_supported_generic_status_indicators_cmd(uint16_t cid){ 371 return hfp_ag_ok(cid); 372 } 373 374 static int hfp_ag_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){ 375 char buffer[40]; 376 int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR); 377 offset += hfp_hf_indicators_join(buffer+offset, sizeof(buffer)-offset); 378 379 buffer[offset] = 0; 380 381 offset += snprintf(buffer+offset, sizeof(buffer)-offset, ")\r\n\r\nOK\r\n"); 382 buffer[offset] = 0; 383 return send_str_over_rfcomm(cid, buffer); 384 } 385 386 static int hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){ 387 char buffer[40]; 388 int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer)); 389 390 buffer[offset] = 0; 391 offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n"); 392 buffer[offset] = 0; 393 return send_str_over_rfcomm(cid, buffer); 394 } 395 396 static int hfp_ag_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){ 397 char buffer[20]; 398 sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status); 399 return send_str_over_rfcomm(cid, buffer); 400 } 401 402 static int hfp_ag_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){ 403 char buffer[40]; 404 if (strlen(op.name) == 0){ 405 sprintf(buffer, "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode); 406 } else { 407 sprintf(buffer, "\r\n%s:%d,%d,%s\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format, op.name); 408 } 409 return send_str_over_rfcomm(cid, buffer); 410 } 411 412 413 static int hfp_ag_cmd_suggest_codec(uint16_t cid, uint8_t codec){ 414 char buffer[30]; 415 sprintf(buffer, "\r\n%s:%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec); 416 return send_str_over_rfcomm(cid, buffer); 417 } 418 419 static int hfp_ag_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){ 420 char buffer[30]; 421 sprintf(buffer, "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition); 422 return send_str_over_rfcomm(cid, buffer); 423 } 424 425 static int hfp_ag_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){ 426 char buffer[30]; 427 sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_SPEAKER_GAIN, gain); 428 return send_str_over_rfcomm(cid, buffer); 429 } 430 431 static int hfp_ag_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){ 432 char buffer[30]; 433 sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_MICROPHONE_GAIN, gain); 434 return send_str_over_rfcomm(cid, buffer); 435 } 436 437 static int hfp_ag_set_response_and_hold(uint16_t cid, int state){ 438 char buffer[30]; 439 sprintf(buffer, "\r\n%s: %d\r\n", HFP_RESPONSE_AND_HOLD, state); 440 return send_str_over_rfcomm(cid, buffer); 441 } 442 443 444 static uint8_t hfp_ag_suggest_codec(hfp_connection_t *context){ 445 int i,j; 446 uint8_t codec = HFP_CODEC_CVSD; 447 for (i = 0; i < hfp_codecs_nr; i++){ 448 for (j = 0; j < context->remote_codecs_nr; j++){ 449 if (context->remote_codecs[j] == hfp_codecs[i]){ 450 codec = context->remote_codecs[j]; 451 continue; 452 } 453 } 454 } 455 return codec; 456 } 457 458 static int codecs_exchange_state_machine(hfp_connection_t * context){ 459 /* events ( == commands): 460 HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 461 HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 462 hf_trigger_codec_connection_setup == received BCC 463 ag_trigger_codec_connection_setup == received from AG to send BCS 464 HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 465 */ 466 467 switch (context->codecs_state){ 468 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 469 context->command = HFP_CMD_AG_SEND_COMMON_CODEC; 470 break; 471 case HFP_CODECS_AG_RESEND_COMMON_CODEC: 472 context->command = HFP_CMD_AG_SEND_COMMON_CODEC; 473 break; 474 default: 475 break; 476 } 477 478 // printf(" -> State machine: CC\n"); 479 480 switch (context->command){ 481 case HFP_CMD_AVAILABLE_CODECS: 482 //printf("HFP_CODECS_RECEIVED_LIST \n"); 483 if (context->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 484 context->codecs_state = HFP_CODECS_RECEIVED_LIST; 485 hfp_ag_ok(context->rfcomm_cid); 486 return 1; 487 } 488 489 switch (context->codecs_state){ 490 case HFP_CODECS_AG_SENT_COMMON_CODEC: 491 case HFP_CODECS_EXCHANGED: 492 context->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC; 493 break; 494 default: 495 break; 496 } 497 hfp_ag_ok(context->rfcomm_cid); 498 return 1; 499 500 case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 501 //printf(" HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP \n"); 502 context->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 503 hfp_ag_ok(context->rfcomm_cid); 504 return 1; 505 506 case HFP_CMD_AG_SEND_COMMON_CODEC: 507 //printf(" HFP_CMD_AG_SEND_COMMON_CODEC \n"); 508 context->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC; 509 context->suggested_codec = hfp_ag_suggest_codec(context); 510 hfp_ag_cmd_suggest_codec(context->rfcomm_cid, context->suggested_codec); 511 return 1; 512 513 case HFP_CMD_HF_CONFIRMED_CODEC: 514 //printf("HFP_CMD_HF_CONFIRMED_CODEC \n"); 515 if (context->codec_confirmed != context->suggested_codec){ 516 context->codecs_state = HFP_CODECS_ERROR; 517 hfp_ag_error(context->rfcomm_cid); 518 return 1; 519 } 520 context->negotiated_codec = context->codec_confirmed; 521 context->codecs_state = HFP_CODECS_EXCHANGED; 522 hfp_emit_event(hfp_callback, HFP_SUBEVENT_CODECS_CONNECTION_COMPLETE, 0); 523 hfp_ag_ok(context->rfcomm_cid); 524 return 1; 525 default: 526 break; 527 } 528 return 0; 529 } 530 531 static void hfp_init_link_settings(hfp_connection_t * context){ 532 // determine highest possible link setting 533 context->link_setting = HFP_LINK_SETTINGS_D1; 534 if (hci_remote_eSCO_supported(context->con_handle)){ 535 context->link_setting = HFP_LINK_SETTINGS_S3; 536 if ((context->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) 537 && (hfp_supported_features & (1<<HFP_AGSF_ESCO_S4))){ 538 context->link_setting = HFP_LINK_SETTINGS_S4; 539 } 540 } 541 } 542 543 static void hfp_ag_slc_established(hfp_connection_t * context){ 544 context->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 545 hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0); 546 547 hfp_init_link_settings(context); 548 549 // if active call exist, set per-connection state active, too (when audio is on) 550 if (hfp_ag_call_state == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 551 context->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 552 } 553 // if AG is ringing, also start ringing on the HF 554 if (hfp_ag_call_state == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS && 555 hfp_ag_callsetup_state == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 556 hfp_ag_hf_start_ringing(context); 557 } 558 } 559 560 static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * context){ 561 if (context->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 562 int done = 0; 563 // printf(" -> State machine: SLC\n"); 564 switch(context->command){ 565 case HFP_CMD_SUPPORTED_FEATURES: 566 switch(context->state){ 567 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 568 case HFP_EXCHANGE_SUPPORTED_FEATURES: 569 if (has_codec_negotiation_feature(context)){ 570 context->state = HFP_W4_NOTIFY_ON_CODECS; 571 } else { 572 context->state = HFP_W4_RETRIEVE_INDICATORS; 573 } 574 hfp_ag_exchange_supported_features_cmd(context->rfcomm_cid); 575 return 1; 576 default: 577 break; 578 } 579 break; 580 case HFP_CMD_AVAILABLE_CODECS: 581 done = codecs_exchange_state_machine(context); 582 583 if (context->codecs_state == HFP_CODECS_RECEIVED_LIST){ 584 context->state = HFP_W4_RETRIEVE_INDICATORS; 585 } 586 return done; 587 588 case HFP_CMD_RETRIEVE_AG_INDICATORS: 589 if (context->state != HFP_W4_RETRIEVE_INDICATORS) break; 590 context->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 591 hfp_ag_retrieve_indicators_cmd(context->rfcomm_cid, context); 592 return 1; 593 594 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 595 if (context->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break; 596 context->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 597 hfp_ag_retrieve_indicators_status_cmd(context->rfcomm_cid); 598 return 1; 599 600 case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 601 if (context->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break; 602 if (has_call_waiting_and_3way_calling_feature(context)){ 603 context->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 604 } else if (has_hf_indicators_feature(context)){ 605 context->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 606 } else { 607 hfp_ag_slc_established(context); 608 } 609 hfp_ag_set_indicator_status_update_cmd(context->rfcomm_cid, 1); 610 return 1; 611 612 case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 613 if (context->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break; 614 if (has_hf_indicators_feature(context)){ 615 context->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 616 } 617 hfp_ag_retrieve_can_hold_call_cmd(context->rfcomm_cid); 618 if (!has_hf_indicators_feature(context)){ 619 hfp_ag_slc_established(context); 620 } 621 return 1; 622 623 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 624 if (context->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break; 625 context->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 626 hfp_ag_list_supported_generic_status_indicators_cmd(context->rfcomm_cid); 627 return 1; 628 629 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 630 if (context->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break; 631 context->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 632 hfp_ag_retrieve_supported_generic_status_indicators_cmd(context->rfcomm_cid); 633 return 1; 634 635 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 636 if (context->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break; 637 hfp_ag_slc_established(context); 638 hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(context->rfcomm_cid); 639 return 1; 640 default: 641 break; 642 } 643 return done; 644 } 645 646 static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * context){ 647 // if (context->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 648 649 int done = codecs_exchange_state_machine(context); 650 if (done) return done; 651 652 // printf(" -> State machine: SLC Queries\n"); 653 switch(context->command){ 654 case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 655 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, context->ag_activate_voice_recognition); 656 hfp_ag_activate_voice_recognition_cmd(context->rfcomm_cid, context->ag_activate_voice_recognition); 657 return 1; 658 case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 659 if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){ 660 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, context->ag_activate_voice_recognition); 661 hfp_ag_ok(context->rfcomm_cid); 662 hfp_ag_setup_audio_connection(context); 663 } else { 664 hfp_ag_error(context->rfcomm_cid); 665 } 666 return 1; 667 case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 668 hfp_ag_change_in_band_ring_tone_setting_cmd(context->rfcomm_cid); 669 return 1; 670 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 671 hfp_ag_report_network_operator_name_cmd(context->rfcomm_cid, context->network_operator); 672 return 1; 673 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 674 if (context->network_operator.format != 0){ 675 hfp_ag_error(context->rfcomm_cid); 676 } else { 677 hfp_ag_ok(context->rfcomm_cid); 678 } 679 return 1; 680 case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 681 hfp_ag_ok(context->rfcomm_cid); 682 return 1; 683 case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 684 if (context->extended_audio_gateway_error){ 685 context->extended_audio_gateway_error = 0; 686 hfp_ag_report_extended_audio_gateway_error(context->rfcomm_cid, context->extended_audio_gateway_error); 687 return 1; 688 } 689 case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 690 hfp_ag_ok(context->rfcomm_cid); 691 return 1; 692 default: 693 break; 694 } 695 return 0; 696 } 697 698 static int hfp_ag_run_for_audio_connection(hfp_connection_t * context){ 699 if (context->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 700 context->state > HFP_W2_DISCONNECT_SCO) return 0; 701 702 703 if (context->state == HFP_AUDIO_CONNECTION_ESTABLISHED && context->release_audio_connection){ 704 context->state = HFP_W4_SCO_DISCONNECTED; 705 context->release_audio_connection = 0; 706 gap_disconnect(context->sco_handle); 707 return 1; 708 } 709 710 if (context->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 711 712 // run codecs exchange 713 int done = codecs_exchange_state_machine(context); 714 if (done) return done; 715 // printf(" -> State machine: Audio Connection\n"); 716 717 if (context->codecs_state != HFP_CODECS_EXCHANGED) return done; 718 if (context->establish_audio_connection){ 719 context->state = HFP_W4_SCO_CONNECTED; 720 context->establish_audio_connection = 0; 721 hfp_setup_synchronous_connection(context->con_handle, context->link_setting); 722 return 1; 723 } 724 return 0; 725 } 726 727 static hfp_connection_t * hfp_ag_context_for_timer(timer_source_t * ts){ 728 linked_list_iterator_t it; 729 linked_list_iterator_init(&it, hfp_get_connections()); 730 731 while (linked_list_iterator_has_next(&it)){ 732 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 733 if ( &connection->hfp_timeout == ts) { 734 return connection; 735 } 736 } 737 return NULL; 738 } 739 740 static void hfp_timeout_handler(timer_source_t * timer){ 741 hfp_connection_t * context = hfp_ag_context_for_timer(timer); 742 if (!context) return; 743 744 log_info("HFP start ring timeout, con handle 0x%02x", context->con_handle); 745 context->ag_ring = 1; 746 context->ag_send_clip = clip_type && context->clip_enabled; 747 748 run_loop_set_timer(&context->hfp_timeout, 2000); // 5 seconds timeout 749 run_loop_add_timer(&context->hfp_timeout); 750 751 hfp_run_for_context(context); 752 } 753 754 static void hfp_timeout_start(hfp_connection_t * context){ 755 run_loop_remove_timer(&context->hfp_timeout); 756 run_loop_set_timer_handler(&context->hfp_timeout, hfp_timeout_handler); 757 run_loop_set_timer(&context->hfp_timeout, 2000); // 5 seconds timeout 758 run_loop_add_timer(&context->hfp_timeout); 759 } 760 761 static void hfp_timeout_stop(hfp_connection_t * context){ 762 log_info("HFP stop ring timeout, con handle 0x%02x", context->con_handle); 763 run_loop_remove_timer(&context->hfp_timeout); 764 } 765 766 // 767 // transitition implementations for hfp_ag_call_state_machine 768 // 769 770 static void hfp_ag_hf_start_ringing(hfp_connection_t * context){ 771 if (use_in_band_tone()){ 772 context->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING; 773 hfp_ag_establish_audio_connection(context->remote_addr); 774 } else { 775 hfp_timeout_start(context); 776 context->ag_ring = 1; 777 context->ag_send_clip = clip_type && context->clip_enabled; 778 context->call_state = HFP_CALL_RINGING; 779 hfp_emit_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG, 0); 780 } 781 } 782 783 static void hfp_ag_hf_stop_ringing(hfp_connection_t * context){ 784 context->ag_ring = 0; 785 context->ag_send_clip = 0; 786 hfp_timeout_stop(context); 787 hfp_emit_event(hfp_callback, HFP_SUBEVENT_STOP_RINGINIG, 0); 788 } 789 790 static void hfp_ag_trigger_incoming_call(void){ 791 int indicator_index = get_ag_indicator_index_for_name("callsetup"); 792 if (indicator_index < 0) return; 793 794 linked_list_iterator_t it; 795 linked_list_iterator_init(&it, hfp_get_connections()); 796 while (linked_list_iterator_has_next(&it)){ 797 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 798 hfp_ag_establish_service_level_connection(connection->remote_addr); 799 if (connection->call_state == HFP_CALL_IDLE){ 800 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, indicator_index, 1); 801 hfp_ag_hf_start_ringing(connection); 802 } 803 if (connection->call_state == HFP_CALL_ACTIVE){ 804 connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING; 805 } 806 hfp_run_for_context(connection); 807 } 808 } 809 810 static void hfp_ag_transfer_callsetup_state(void){ 811 int indicator_index = get_ag_indicator_index_for_name("callsetup"); 812 if (indicator_index < 0) return; 813 814 linked_list_iterator_t it; 815 linked_list_iterator_init(&it, hfp_get_connections()); 816 while (linked_list_iterator_has_next(&it)){ 817 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 818 hfp_ag_establish_service_level_connection(connection->remote_addr); 819 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, indicator_index, 1); 820 hfp_run_for_context(connection); 821 } 822 } 823 824 static void hfp_ag_transfer_call_state(void){ 825 int indicator_index = get_ag_indicator_index_for_name("call"); 826 if (indicator_index < 0) return; 827 828 linked_list_iterator_t it; 829 linked_list_iterator_init(&it, hfp_get_connections()); 830 while (linked_list_iterator_has_next(&it)){ 831 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 832 hfp_ag_establish_service_level_connection(connection->remote_addr); 833 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, indicator_index, 1); 834 hfp_run_for_context(connection); 835 } 836 } 837 838 static void hfp_ag_transfer_callheld_state(void){ 839 int indicator_index = get_ag_indicator_index_for_name("callheld"); 840 if (indicator_index < 0) return; 841 842 linked_list_iterator_t it; 843 linked_list_iterator_init(&it, hfp_get_connections()); 844 while (linked_list_iterator_has_next(&it)){ 845 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 846 hfp_ag_establish_service_level_connection(connection->remote_addr); 847 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, indicator_index, 1); 848 hfp_run_for_context(connection); 849 } 850 } 851 852 static void hfp_ag_hf_accept_call(hfp_connection_t * source){ 853 854 int call_indicator_index = get_ag_indicator_index_for_name("call"); 855 int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 856 857 linked_list_iterator_t it; 858 linked_list_iterator_init(&it, hfp_get_connections()); 859 while (linked_list_iterator_has_next(&it)){ 860 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 861 if (connection->call_state != HFP_CALL_RINGING && 862 connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue; 863 864 hfp_ag_hf_stop_ringing(connection); 865 if (connection == source){ 866 connection->ok_pending = 1; 867 868 if (use_in_band_tone()){ 869 connection->call_state = HFP_CALL_ACTIVE; 870 } else { 871 connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 872 hfp_ag_establish_audio_connection(connection->remote_addr); 873 } 874 875 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 876 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 877 878 } else { 879 connection->call_state = HFP_CALL_IDLE; 880 } 881 hfp_run_for_context(connection); 882 } 883 } 884 885 static void hfp_ag_ag_accept_call(void){ 886 887 int call_indicator_index = get_ag_indicator_index_for_name("call"); 888 int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 889 890 linked_list_iterator_t it; 891 linked_list_iterator_init(&it, hfp_get_connections()); 892 while (linked_list_iterator_has_next(&it)){ 893 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 894 if (connection->call_state != HFP_CALL_RINGING) continue; 895 896 hfp_ag_hf_stop_ringing(connection); 897 connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION; 898 899 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 900 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 901 902 hfp_run_for_context(connection); 903 break; // only single 904 } 905 } 906 907 static void hfp_ag_trigger_reject_call(void){ 908 int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 909 linked_list_iterator_t it; 910 linked_list_iterator_init(&it, hfp_get_connections()); 911 while (linked_list_iterator_has_next(&it)){ 912 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 913 if (connection->call_state != HFP_CALL_RINGING && 914 connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue; 915 hfp_ag_hf_stop_ringing(connection); 916 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 917 connection->call_state = HFP_CALL_IDLE; 918 hfp_run_for_context(connection); 919 } 920 } 921 922 static void hfp_ag_trigger_terminate_call(void){ 923 int call_indicator_index = get_ag_indicator_index_for_name("call"); 924 925 linked_list_iterator_t it; 926 linked_list_iterator_init(&it, hfp_get_connections()); 927 while (linked_list_iterator_has_next(&it)){ 928 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 929 hfp_ag_establish_service_level_connection(connection->remote_addr); 930 if (connection->call_state == HFP_CALL_IDLE) continue; 931 connection->call_state = HFP_CALL_IDLE; 932 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 933 connection->release_audio_connection = 1; 934 hfp_run_for_context(connection); 935 } 936 hfp_emit_event(hfp_callback, HFP_SUBEVENT_CALL_TERMINATED, 0); 937 } 938 939 static void hfp_ag_set_callsetup_state(hfp_callsetup_status_t state){ 940 hfp_ag_callsetup_state = state; 941 hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup"); 942 if (!indicator){ 943 log_error("hfp_ag_set_callsetup_state: callsetup indicator is missing"); 944 }; 945 indicator->status = state; 946 } 947 948 static void hfp_ag_set_callheld_state(hfp_callheld_status_t state){ 949 hfp_ag_callheld_state = state; 950 hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld"); 951 if (!indicator){ 952 log_error("hfp_ag_set_callheld_state: callheld indicator is missing"); 953 }; 954 indicator->status = state; 955 } 956 957 static void hfp_ag_set_call_state(hfp_call_status_t state){ 958 hfp_ag_call_state = state; 959 hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call"); 960 if (!indicator){ 961 log_error("hfp_ag_set_call_state: call indicator is missing"); 962 }; 963 indicator->status = state; 964 } 965 966 static void hfp_ag_stop_ringing(void){ 967 linked_list_iterator_t it; 968 linked_list_iterator_init(&it, hfp_get_connections()); 969 while (linked_list_iterator_has_next(&it)){ 970 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 971 if (connection->call_state != HFP_CALL_RINGING && 972 connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue; 973 hfp_ag_hf_stop_ringing(connection); 974 } 975 } 976 977 static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){ 978 linked_list_iterator_t it; 979 linked_list_iterator_init(&it, hfp_get_connections()); 980 while (linked_list_iterator_has_next(&it)){ 981 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 982 if (connection->call_state == call_state) return connection; 983 } 984 return NULL; 985 } 986 987 static void hfp_ag_send_response_and_hold_state(void){ 988 linked_list_iterator_t it; 989 linked_list_iterator_init(&it, hfp_get_connections()); 990 while (linked_list_iterator_has_next(&it)){ 991 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 992 connection->send_response_and_hold_status = 1; 993 } 994 } 995 996 static int call_setup_state_machine(hfp_connection_t * connection){ 997 int indicator_index; 998 switch (connection->call_state){ 999 case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1000 if (connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1001 // we got event: audio connection established 1002 hfp_timeout_start(connection); 1003 connection->ag_ring = 1; 1004 connection->ag_send_clip = clip_type && connection->clip_enabled; 1005 connection->call_state = HFP_CALL_RINGING; 1006 connection->call_state = HFP_CALL_RINGING; 1007 hfp_emit_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG, 0); 1008 break; 1009 case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE: 1010 if (connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1011 // we got event: audio connection established 1012 connection->call_state = HFP_CALL_ACTIVE; 1013 break; 1014 case HFP_CALL_W2_SEND_CALL_WAITING: 1015 connection->call_state = HFP_CALL_W4_CHLD; 1016 hfp_ag_send_call_waiting_notification(connection->rfcomm_cid); 1017 indicator_index = get_ag_indicator_index_for_name("callsetup"); 1018 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1019 break; 1020 default: 1021 break; 1022 } 1023 return 0; 1024 } 1025 // connection is used to identify originating HF 1026 static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * connection){ 1027 int indicator_index; 1028 switch (event){ 1029 case HFP_AG_INCOMING_CALL: 1030 switch (hfp_ag_call_state){ 1031 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1032 switch (hfp_ag_callsetup_state){ 1033 case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1034 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS); 1035 hfp_ag_trigger_incoming_call(); 1036 printf("AG rings\n"); 1037 break; 1038 default: 1039 break; 1040 } 1041 break; 1042 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1043 switch (hfp_ag_callsetup_state){ 1044 case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1045 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS); 1046 hfp_ag_trigger_incoming_call(); 1047 printf("AG call waiting\n"); 1048 break; 1049 default: 1050 break; 1051 } 1052 break; 1053 } 1054 break; 1055 case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1056 // clear CLIP 1057 clip_type = 0; 1058 switch (hfp_ag_call_state){ 1059 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1060 switch (hfp_ag_callsetup_state){ 1061 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1062 hfp_ag_set_call_state(HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT); 1063 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1064 hfp_ag_ag_accept_call(); 1065 printf("AG answers call, accept call by GSM\n"); 1066 break; 1067 default: 1068 break; 1069 } 1070 break; 1071 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1072 switch (hfp_ag_callsetup_state){ 1073 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1074 printf("AG: current call is placed on hold, incoming call gets active\n"); 1075 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1076 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1077 hfp_ag_transfer_callsetup_state(); 1078 hfp_ag_transfer_callheld_state(); 1079 break; 1080 default: 1081 break; 1082 } 1083 break; 1084 } 1085 break; 1086 1087 case HFP_AG_HELD_CALL_JOINED_BY_AG: 1088 switch (hfp_ag_call_state){ 1089 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1090 switch (hfp_ag_callheld_state){ 1091 case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 1092 printf("AG: joining held call with active call\n"); 1093 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_NO_CALLS_HELD); 1094 hfp_ag_transfer_callheld_state(); 1095 break; 1096 default: 1097 break; 1098 } 1099 break; 1100 default: 1101 break; 1102 } 1103 break; 1104 1105 case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1106 // clear CLIP 1107 clip_type = 0; 1108 switch (hfp_ag_call_state){ 1109 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1110 switch (hfp_ag_callsetup_state){ 1111 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1112 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1113 hfp_ag_set_call_state(HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT); 1114 hfp_ag_hf_accept_call(connection); 1115 printf("HF answers call, accept call by GSM\n"); 1116 hfp_emit_event(hfp_callback, HFP_CMD_CALL_ANSWERED, 0); 1117 break; 1118 default: 1119 break; 1120 } 1121 break; 1122 default: 1123 break; 1124 } 1125 break; 1126 1127 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1128 // clear CLIP 1129 clip_type = 0; 1130 switch (hfp_ag_call_state){ 1131 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1132 switch (hfp_ag_callsetup_state){ 1133 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1134 hfp_ag_response_and_hold_active = 1; 1135 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1136 hfp_ag_send_response_and_hold_state(); 1137 // as with regualr call 1138 hfp_ag_set_call_state(HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT); 1139 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1140 hfp_ag_ag_accept_call(); 1141 printf("AG response and hold - hold by AG\n"); 1142 break; 1143 default: 1144 break; 1145 } 1146 break; 1147 default: 1148 break; 1149 } 1150 break; 1151 1152 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1153 // clear CLIP 1154 clip_type = 0; 1155 switch (hfp_ag_call_state){ 1156 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1157 switch (hfp_ag_callsetup_state){ 1158 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1159 hfp_ag_response_and_hold_active = 1; 1160 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1161 hfp_ag_send_response_and_hold_state(); 1162 // as with regualr call 1163 hfp_ag_set_call_state(HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT); 1164 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1165 hfp_ag_hf_accept_call(connection); 1166 printf("AG response and hold - hold by HF\n"); 1167 break; 1168 default: 1169 break; 1170 } 1171 break; 1172 default: 1173 break; 1174 } 1175 break; 1176 1177 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1178 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1179 if (!hfp_ag_response_and_hold_active) break; 1180 if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1181 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1182 hfp_ag_send_response_and_hold_state(); 1183 printf("Held Call accepted and active\n"); 1184 break; 1185 1186 case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1187 case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1188 if (!hfp_ag_response_and_hold_active) break; 1189 if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1190 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1191 hfp_ag_send_response_and_hold_state(); 1192 // from terminate by ag 1193 hfp_ag_set_call_state(HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS); 1194 hfp_ag_trigger_terminate_call(); 1195 break; 1196 1197 case HFP_AG_TERMINATE_CALL_BY_HF: 1198 // clear CLIP 1199 clip_type = 0; 1200 switch (hfp_ag_call_state){ 1201 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1202 switch (hfp_ag_callsetup_state){ 1203 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1204 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1205 hfp_ag_transfer_callsetup_state(); 1206 hfp_ag_trigger_reject_call(); 1207 printf("HF Rejected Incoming call, AG terminate call\n"); 1208 break; 1209 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1210 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1211 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1212 hfp_ag_transfer_callsetup_state(); 1213 printf("AG terminate outgoing call process\n"); 1214 default: 1215 break; 1216 } 1217 break; 1218 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1219 hfp_ag_set_call_state(HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS); 1220 hfp_ag_transfer_call_state(); 1221 connection->call_state = HFP_CALL_IDLE; 1222 printf("AG terminate call\n"); 1223 break; 1224 } 1225 break; 1226 1227 case HFP_AG_TERMINATE_CALL_BY_AG: 1228 // clear CLIP 1229 clip_type = 0; 1230 switch (hfp_ag_call_state){ 1231 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1232 switch (hfp_ag_callsetup_state){ 1233 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1234 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1235 hfp_ag_trigger_reject_call(); 1236 printf("AG Rejected Incoming call, AG terminate call\n"); 1237 break; 1238 default: 1239 break; 1240 } 1241 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1242 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1243 hfp_ag_set_call_state(HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS); 1244 hfp_ag_trigger_terminate_call(); 1245 printf("AG terminate call\n"); 1246 break; 1247 default: 1248 break; 1249 } 1250 break; 1251 case HFP_AG_CALL_DROPPED: 1252 // clear CLIP 1253 clip_type = 0; 1254 switch (hfp_ag_call_state){ 1255 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1256 switch (hfp_ag_callsetup_state){ 1257 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1258 hfp_ag_stop_ringing(); 1259 printf("Incoming call interrupted\n"); 1260 break; 1261 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1262 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1263 printf("Outgoing call interrupted\n"); 1264 printf("AG notify call dropped\n"); 1265 break; 1266 default: 1267 break; 1268 } 1269 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1270 hfp_ag_transfer_callsetup_state(); 1271 break; 1272 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1273 if (hfp_ag_response_and_hold_active) { 1274 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1275 hfp_ag_send_response_and_hold_state(); 1276 } 1277 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1278 hfp_ag_set_call_state(HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS); 1279 hfp_ag_trigger_terminate_call(); 1280 printf("AG notify call dropped\n"); 1281 break; 1282 default: 1283 break; 1284 } 1285 break; 1286 1287 case HFP_AG_OUTGOING_CALL_INITIATED: 1288 connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1289 hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &connection->line_buffer[3]); 1290 break; 1291 1292 case HFP_AG_OUTGOING_REDIAL_INITIATED: 1293 connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1294 hfp_emit_event(hfp_callback, HFP_SUBEVENT_REDIAL_LAST_NUMBER, 0); 1295 break; 1296 1297 case HFP_AG_OUTGOING_CALL_REJECTED: 1298 connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1299 if (!connection){ 1300 log_info("hfp_ag_call_sm: did not find outgoing connection in initiated state"); 1301 break; 1302 } 1303 connection->call_state = HFP_CALL_IDLE; 1304 connection->send_error = 1; 1305 hfp_run_for_context(connection); 1306 break; 1307 1308 case HFP_AG_OUTGOING_CALL_ACCEPTED: 1309 connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1310 if (!connection){ 1311 log_info("hfp_ag_call_sm: did not find outgoing connection in initiated state"); 1312 break; 1313 } 1314 1315 connection->ok_pending = 1; 1316 connection->call_state = HFP_CALL_OUTGOING_DIALING; 1317 1318 // trigger callsetup to be 1319 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE); 1320 indicator_index = get_ag_indicator_index_for_name("callsetup"); 1321 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1322 1323 // put current call on hold if active 1324 if (hfp_ag_call_state == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1325 printf("AG putting current call on hold for new outgoing call\n"); 1326 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS); 1327 indicator_index = get_ag_indicator_index_for_name("callheld"); 1328 hfp_ag_transfer_ag_indicators_status_cmd(connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 1329 } 1330 1331 // start audio if needed 1332 hfp_ag_establish_audio_connection(connection->remote_addr); 1333 break; 1334 1335 case HFP_AG_OUTGOING_CALL_RINGING: 1336 connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1337 if (!connection){ 1338 log_info("hfp_ag_call_sm: did not find outgoing connection in dialing state"); 1339 break; 1340 } 1341 connection->call_state = HFP_CALL_OUTGOING_RINGING; 1342 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE); 1343 hfp_ag_transfer_callsetup_state(); 1344 break; 1345 1346 case HFP_AG_OUTGOING_CALL_ESTABLISHED: 1347 // get outgoing call 1348 connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1349 if (!connection){ 1350 connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1351 } 1352 if (!connection){ 1353 log_info("hfp_ag_call_sm: did not find outgoing connection"); 1354 break; 1355 } 1356 connection->call_state = HFP_CALL_ACTIVE; 1357 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1358 hfp_ag_set_call_state(HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT); 1359 hfp_ag_transfer_call_state(); 1360 hfp_ag_transfer_callsetup_state(); 1361 if (hfp_ag_callheld_state == HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){ 1362 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1363 hfp_ag_transfer_callheld_state(); 1364 } 1365 break; 1366 1367 default: 1368 break; 1369 } 1370 } 1371 1372 static void hfp_run_for_context(hfp_connection_t *context){ 1373 if (!context) return; 1374 if (!rfcomm_can_send_packet_now(context->rfcomm_cid)) return; 1375 1376 if (context->send_status_of_current_calls){ 1377 hfp_emit_event(hfp_callback, HFP_SUBEVENT_TRANSMIT_STATUS_OF_CURRENT_CALL, 0); 1378 return; 1379 } 1380 1381 if (context->command == HFP_CMD_UNKNOWN){ 1382 context->ok_pending = 0; 1383 context->send_error = 0; 1384 context->command = HFP_CMD_NONE; 1385 hfp_ag_error(context->rfcomm_cid); 1386 return; 1387 } 1388 1389 if (context->send_error){ 1390 context->send_error = 0; 1391 context->command = HFP_CMD_NONE; 1392 hfp_ag_error(context->rfcomm_cid); 1393 return; 1394 } 1395 1396 // note: before update AG indicators and ok_pending 1397 if (context->send_response_and_hold_status){ 1398 context->send_response_and_hold_status = 0; 1399 hfp_ag_set_response_and_hold(context->rfcomm_cid, hfp_ag_response_and_hold_state); 1400 return; 1401 } 1402 1403 if (context->send_response_and_hold_active){ 1404 context->send_response_and_hold_active = 0; 1405 hfp_ag_set_response_and_hold(context->rfcomm_cid, 0); 1406 return; 1407 } 1408 1409 if (context->ok_pending){ 1410 context->ok_pending = 0; 1411 context->command = HFP_CMD_NONE; 1412 hfp_ag_ok(context->rfcomm_cid); 1413 return; 1414 } 1415 1416 // update AG indicators 1417 if (context->ag_indicators_status_update_bitmap){ 1418 int i; 1419 for (i=0;i<context->ag_indicators_nr;i++){ 1420 if (get_bit(context->ag_indicators_status_update_bitmap, i)){ 1421 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, i, 0); 1422 if (!context->enable_status_update_for_ag_indicators) { 1423 log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name); 1424 break; 1425 } 1426 hfp_ag_transfer_ag_indicators_status_cmd(context->rfcomm_cid, &hfp_ag_indicators[i]); 1427 return; 1428 } 1429 } 1430 } 1431 1432 if (context->ag_ring){ 1433 context->ag_ring = 0; 1434 context->command = HFP_CMD_NONE; 1435 hfp_ag_ring(context->rfcomm_cid); 1436 return; 1437 } 1438 1439 if (context->ag_send_clip){ 1440 context->ag_send_clip = 0; 1441 context->command = HFP_CMD_NONE; 1442 hfp_ag_send_clip(context->rfcomm_cid); 1443 return; 1444 } 1445 1446 if (context->send_phone_number_for_voice_tag){ 1447 context->send_phone_number_for_voice_tag = 0; 1448 context->command = HFP_CMD_NONE; 1449 context->ok_pending = 1; 1450 hfp_ag_send_phone_number_for_voice_tag_cmd(context->rfcomm_cid); 1451 return; 1452 } 1453 1454 if (context->send_subscriber_number){ 1455 if (context->next_subscriber_number_to_send < subscriber_numbers_count){ 1456 hfp_phone_number_t phone = subscriber_numbers[context->next_subscriber_number_to_send++]; 1457 hfp_send_subscriber_number_cmd(context->rfcomm_cid, phone.type, phone.number); 1458 } else { 1459 context->send_subscriber_number = 0; 1460 context->next_subscriber_number_to_send = 0; 1461 hfp_ag_ok(context->rfcomm_cid); 1462 } 1463 context->command = HFP_CMD_NONE; 1464 } 1465 1466 if (context->send_microphone_gain){ 1467 context->send_microphone_gain = 0; 1468 context->command = HFP_CMD_NONE; 1469 hfp_ag_set_microphone_gain_cmd(context->rfcomm_cid, context->microphone_gain); 1470 return; 1471 } 1472 1473 if (context->send_speaker_gain){ 1474 context->send_speaker_gain = 0; 1475 context->command = HFP_CMD_NONE; 1476 hfp_ag_set_speaker_gain_cmd(context->rfcomm_cid, context->speaker_gain); 1477 return; 1478 } 1479 1480 if (context->send_ag_status_indicators){ 1481 context->send_ag_status_indicators = 0; 1482 hfp_ag_retrieve_indicators_cmd(context->rfcomm_cid, context); 1483 return; 1484 } 1485 1486 int done = hfp_ag_run_for_context_service_level_connection(context); 1487 if (!done){ 1488 done = hfp_ag_run_for_context_service_level_connection_queries(context); 1489 } 1490 1491 if (!done){ 1492 done = call_setup_state_machine(context); 1493 } 1494 1495 if (!done){ 1496 done = hfp_ag_run_for_audio_connection(context); 1497 } 1498 1499 if (context->command == HFP_CMD_NONE && !done){ 1500 // log_info("context->command == HFP_CMD_NONE"); 1501 switch(context->state){ 1502 case HFP_W2_DISCONNECT_RFCOMM: 1503 context->state = HFP_W4_RFCOMM_DISCONNECTED; 1504 rfcomm_disconnect_internal(context->rfcomm_cid); 1505 break; 1506 default: 1507 break; 1508 } 1509 } 1510 if (done){ 1511 context->command = HFP_CMD_NONE; 1512 } 1513 } 1514 static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){ 1515 int i; 1516 for (i=0;i< get_hfp_generic_status_indicators_nr();i++){ 1517 hfp_generic_status_indicator_t * indicator = &get_hfp_generic_status_indicators()[i]; 1518 if (indicator->uuid == number){ 1519 return indicator; 1520 } 1521 } 1522 return NULL; 1523 } 1524 1525 static void hfp_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1526 hfp_connection_t * context = get_hfp_connection_context_for_rfcomm_cid(channel); 1527 if (!context) return; 1528 int pos; 1529 for (pos = 0; pos < size ; pos++){ 1530 hfp_parse(context, packet[pos], 0); 1531 } 1532 hfp_generic_status_indicator_t * indicator; 1533 int value; 1534 switch(context->command){ 1535 case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 1536 if (hfp_ag_response_and_hold_active){ 1537 context->send_response_and_hold_active = 1; 1538 } 1539 context->ok_pending = 1; 1540 break; 1541 case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 1542 value = atoi((char *)&context->line_buffer[0]); 1543 printf("HF Response and Hold: %u\n", value); 1544 switch(value){ 1545 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 1546 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, context); 1547 break; 1548 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 1549 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, context); 1550 break; 1551 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 1552 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, context); 1553 break; 1554 default: 1555 break; 1556 } 1557 context->ok_pending = 1; 1558 break; 1559 case HFP_CMD_HF_INDICATOR_STATUS: 1560 context->command = HFP_CMD_NONE; 1561 // find indicator by assigned number 1562 indicator = get_hf_indicator_by_number(context->parser_indicator_index); 1563 if (!indicator){ 1564 context->send_error = 1; 1565 break; 1566 } 1567 value = atoi((char *)&context->line_buffer[0]); 1568 switch (indicator->uuid){ 1569 case 1: // enhanced security 1570 if (value > 1) { 1571 context->send_error = 1; 1572 return; 1573 } 1574 printf("HF Indicator 'enhanced security' set to %u\n", value); 1575 break; 1576 case 2: // battery level 1577 if (value > 100){ 1578 context->send_error = 1; 1579 return; 1580 } 1581 printf("HF Indicator 'battery' set to %u\n", value); 1582 break; 1583 default: 1584 printf("HF Indicator unknown set to %u\n", value); 1585 break; 1586 } 1587 context->ok_pending = 1; 1588 break; 1589 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1590 // expected by SLC state machine 1591 if (context->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 1592 context->send_ag_status_indicators = 1; 1593 break; 1594 case HFP_CMD_LIST_CURRENT_CALLS: 1595 context->command = HFP_CMD_NONE; 1596 context->send_status_of_current_calls = 1; 1597 hfp_emit_event(hfp_callback, HFP_SUBEVENT_TRANSMIT_STATUS_OF_CURRENT_CALL, 0); 1598 break; 1599 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1600 if (subscriber_numbers_count == 0){ 1601 hfp_ag_ok(context->rfcomm_cid); 1602 break; 1603 } 1604 context->next_subscriber_number_to_send = 0; 1605 context->send_subscriber_number = 1; 1606 break; 1607 case HFP_CMD_TRANSMIT_DTMF_CODES: 1608 context->command = HFP_CMD_NONE; 1609 hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &context->line_buffer[0]); 1610 break; 1611 case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 1612 context->command = HFP_CMD_NONE; 1613 hfp_emit_event(hfp_callback, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG, 0); 1614 break; 1615 case HFP_CMD_TURN_OFF_EC_AND_NR: 1616 context->command = HFP_CMD_NONE; 1617 if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 1618 context->ok_pending = 1; 1619 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, context->ag_echo_and_noise_reduction); 1620 printf("AG: EC/NR = %u\n", context->ag_echo_and_noise_reduction); 1621 } else { 1622 context->send_error = 1; 1623 } 1624 break; 1625 case HFP_CMD_CALL_ANSWERED: 1626 context->command = HFP_CMD_NONE; 1627 printf("HFP: ATA\n"); 1628 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, context); 1629 break; 1630 case HFP_CMD_HANG_UP_CALL: 1631 context->command = HFP_CMD_NONE; 1632 context->ok_pending = 1; 1633 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, context); 1634 break; 1635 case HFP_CMD_CALL_HOLD: { 1636 // TODO: fully implement this 1637 log_error("HFP: unhandled call hold type %c", context->line_buffer[0]); 1638 int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1639 int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1640 int call_indicator_index = get_ag_indicator_index_for_name("call"); 1641 switch (context->line_buffer[0]){ 1642 case '0': 1643 context->command = HFP_CMD_NONE; 1644 context->ok_pending = 1; 1645 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1646 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1647 context->call_state = HFP_CALL_ACTIVE; 1648 printf("AG: Call Waiting, User Busy\n"); 1649 break; 1650 case '1': 1651 // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1652 context->command = HFP_CMD_NONE; 1653 context->ok_pending = 1; 1654 if (hfp_ag_callsetup_state != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS){ 1655 printf("AG: Call Dropped, Accept new call\n"); 1656 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1657 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1658 } else { 1659 printf("AG: Call Dropped, Resume held call\n"); 1660 } 1661 if (hfp_ag_callheld_state != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 1662 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_NO_CALLS_HELD); 1663 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1664 } 1665 context->call_state = HFP_CALL_ACTIVE; 1666 break; 1667 case '2': 1668 // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1669 context->command = HFP_CMD_NONE; 1670 context->ok_pending = 1; 1671 // only update if callsetup changed 1672 if (hfp_ag_callsetup_state != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS){ 1673 printf("AG: Call on Hold, Accept new call\n"); 1674 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1675 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1676 } else { 1677 printf("AG: Swap calls\n"); 1678 } 1679 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1680 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1681 context->call_state = HFP_CALL_ACTIVE; 1682 break; 1683 case '3': 1684 // Adds a held call to the conversation. 1685 context->command = HFP_CMD_NONE; 1686 context->ok_pending = 1; 1687 if (hfp_ag_callheld_state != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 1688 printf("AG: Join 3-way-call\n"); 1689 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_NO_CALLS_HELD); 1690 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1691 hfp_emit_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL, 0); 1692 } 1693 context->call_state = HFP_CALL_ACTIVE; 1694 break; 1695 case '4': 1696 // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 1697 context->command = HFP_CMD_NONE; 1698 context->ok_pending = 1; 1699 printf("AG: Transfer call -> Connect two calls and disconnect\n"); 1700 hfp_ag_set_call_state(HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS); 1701 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_NO_CALLS_HELD); 1702 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1703 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1704 context->call_state = HFP_CALL_IDLE; 1705 break; 1706 default: 1707 break; 1708 } 1709 break; 1710 } 1711 case HFP_CMD_CALL_PHONE_NUMBER: 1712 context->command = HFP_CMD_NONE; 1713 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, context); 1714 break; 1715 case HFP_CMD_REDIAL_LAST_NUMBER: 1716 context->command = HFP_CMD_NONE; 1717 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, context); 1718 break; 1719 case HFP_CMD_ENABLE_CLIP: 1720 context->command = HFP_CMD_NONE; 1721 context->clip_enabled = context->line_buffer[8] != '0'; 1722 log_info("hfp: clip set, now: %u", context->clip_enabled); 1723 context->ok_pending = 1; 1724 break; 1725 case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 1726 context->command = HFP_CMD_NONE; 1727 context->call_waiting_notification_enabled = context->line_buffer[8] != '0'; 1728 log_info("hfp: call waiting notification set, now: %u", context->call_waiting_notification_enabled); 1729 context->ok_pending = 1; 1730 break; 1731 case HFP_CMD_SET_SPEAKER_GAIN: 1732 context->command = HFP_CMD_NONE; 1733 context->ok_pending = 1; 1734 printf("HF speaker gain = %u\n", context->speaker_gain); 1735 break; 1736 case HFP_CMD_SET_MICROPHONE_GAIN: 1737 context->command = HFP_CMD_NONE; 1738 context->ok_pending = 1; 1739 printf("HF microphone gain = %u\n", context->microphone_gain); 1740 break; 1741 default: 1742 break; 1743 } 1744 } 1745 1746 static void hfp_run(void){ 1747 linked_list_iterator_t it; 1748 linked_list_iterator_init(&it, hfp_get_connections()); 1749 while (linked_list_iterator_has_next(&it)){ 1750 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 1751 hfp_run_for_context(connection); 1752 } 1753 } 1754 1755 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1756 switch (packet_type){ 1757 case RFCOMM_DATA_PACKET: 1758 hfp_handle_rfcomm_data(packet_type, channel, packet, size); 1759 break; 1760 case HCI_EVENT_PACKET: 1761 hfp_handle_hci_event(hfp_callback, packet_type, packet, size); 1762 break; 1763 default: 1764 break; 1765 } 1766 1767 hfp_run(); 1768 } 1769 1770 void hfp_ag_init(uint16_t rfcomm_channel_nr, uint32_t supported_features, 1771 uint8_t * codecs, int codecs_nr, 1772 hfp_ag_indicator_t * ag_indicators, int ag_indicators_nr, 1773 hfp_generic_status_indicator_t * hf_indicators, int hf_indicators_nr, 1774 const char *call_hold_services[], int call_hold_services_nr){ 1775 if (codecs_nr > HFP_MAX_NUM_CODECS){ 1776 log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 1777 return; 1778 } 1779 l2cap_init(); 1780 l2cap_register_packet_handler(packet_handler); 1781 1782 rfcomm_register_packet_handler(packet_handler); 1783 1784 hfp_init(rfcomm_channel_nr); 1785 1786 hfp_supported_features = supported_features; 1787 hfp_codecs_nr = codecs_nr; 1788 1789 int i; 1790 for (i=0; i<codecs_nr; i++){ 1791 hfp_codecs[i] = codecs[i]; 1792 } 1793 1794 hfp_ag_indicators_nr = ag_indicators_nr; 1795 memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 1796 1797 set_hfp_generic_status_indicators(hf_indicators, hf_indicators_nr); 1798 1799 hfp_ag_call_hold_services_nr = call_hold_services_nr; 1800 memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *)); 1801 1802 hfp_ag_call_state = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 1803 hfp_ag_callsetup_state = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1804 hfp_ag_callheld_state = HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1805 } 1806 1807 void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 1808 hfp_establish_service_level_connection(bd_addr, SDP_Handsfree); 1809 } 1810 1811 void hfp_ag_release_service_level_connection(bd_addr_t bd_addr){ 1812 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1813 hfp_release_service_level_connection(connection); 1814 hfp_run_for_context(connection); 1815 } 1816 1817 void hfp_ag_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, hfp_cme_error_t error){ 1818 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1819 if (!connection){ 1820 log_error("HFP HF: connection doesn't exist."); 1821 return; 1822 } 1823 connection->extended_audio_gateway_error = 0; 1824 if (!connection->enable_extended_audio_gateway_error_report){ 1825 return; 1826 } 1827 connection->extended_audio_gateway_error = error; 1828 hfp_run_for_context(connection); 1829 } 1830 1831 static void hfp_ag_setup_audio_connection(hfp_connection_t * connection){ 1832 if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1833 if (connection->state >= HFP_W2_DISCONNECT_SCO) return; 1834 1835 connection->establish_audio_connection = 1; 1836 1837 if (!has_codec_negotiation_feature(connection)){ 1838 log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); 1839 connection->codecs_state = HFP_CODECS_EXCHANGED; 1840 } 1841 1842 switch (connection->codecs_state){ 1843 case HFP_CODECS_IDLE: 1844 case HFP_CODECS_RECEIVED_LIST: 1845 case HFP_CODECS_AG_RESEND_COMMON_CODEC: 1846 case HFP_CODECS_ERROR: 1847 connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 1848 break; 1849 default: 1850 break; 1851 } 1852 } 1853 1854 void hfp_ag_establish_audio_connection(bd_addr_t bd_addr){ 1855 hfp_ag_establish_service_level_connection(bd_addr); 1856 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1857 1858 connection->establish_audio_connection = 0; 1859 hfp_ag_setup_audio_connection(connection); 1860 hfp_run_for_context(connection); 1861 } 1862 1863 void hfp_ag_release_audio_connection(bd_addr_t bd_addr){ 1864 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1865 hfp_release_audio_connection(connection); 1866 hfp_run_for_context(connection); 1867 } 1868 1869 /** 1870 * @brief Enable in-band ring tone 1871 */ 1872 void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 1873 if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){ 1874 return; 1875 } 1876 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone); 1877 1878 linked_list_iterator_t it; 1879 linked_list_iterator_init(&it, hfp_get_connections()); 1880 while (linked_list_iterator_has_next(&it)){ 1881 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 1882 connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 1883 hfp_run_for_context(connection); 1884 } 1885 } 1886 1887 /** 1888 * @brief Called from GSM 1889 */ 1890 void hfp_ag_incoming_call(void){ 1891 hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 1892 } 1893 1894 /** 1895 * @brief number is stored. 1896 */ 1897 void hfp_ag_set_clip(uint8_t type, const char * number){ 1898 clip_type = type; 1899 // copy and terminate 1900 strncpy(clip_number, number, sizeof(clip_number)); 1901 clip_number[sizeof(clip_number)-1] = '\0'; 1902 } 1903 1904 void hfp_ag_call_dropped(void){ 1905 hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 1906 } 1907 1908 // call from AG UI 1909 void hfp_ag_answer_incoming_call(void){ 1910 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 1911 } 1912 1913 void hfp_ag_join_held_call(void){ 1914 hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 1915 } 1916 1917 void hfp_ag_terminate_call(void){ 1918 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 1919 } 1920 1921 void hfp_ag_outgoing_call_ringing(void){ 1922 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 1923 } 1924 1925 void hfp_ag_outgoing_call_established(void){ 1926 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 1927 } 1928 1929 void hfp_ag_outgoing_call_rejected(void){ 1930 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 1931 } 1932 1933 void hfp_ag_outgoing_call_accepted(void){ 1934 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 1935 } 1936 1937 void hfp_ag_hold_incoming_call(void){ 1938 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 1939 } 1940 1941 void hfp_ag_accept_held_incoming_call(void) { 1942 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 1943 } 1944 1945 void hfp_ag_reject_held_incoming_call(void){ 1946 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 1947 } 1948 1949 static void hfp_ag_set_ag_indicator(const char * name, int value){ 1950 int indicator_index = get_ag_indicator_index_for_name(name); 1951 if (indicator_index < 0) return; 1952 hfp_ag_indicators[indicator_index].status = value; 1953 1954 1955 linked_list_iterator_t it; 1956 linked_list_iterator_init(&it, hfp_get_connections()); 1957 while (linked_list_iterator_has_next(&it)){ 1958 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 1959 if (!connection->ag_indicators[indicator_index].enabled) { 1960 log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value); 1961 continue; 1962 } 1963 log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value); 1964 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1965 hfp_run_for_context(connection); 1966 } 1967 } 1968 1969 /* 1970 * @brief 1971 */ 1972 void hfp_ag_set_registration_status(int status){ 1973 hfp_ag_set_ag_indicator("service", status); 1974 } 1975 1976 /* 1977 * @brief 1978 */ 1979 void hfp_ag_set_signal_strength(int strength){ 1980 hfp_ag_set_ag_indicator("signal", strength); 1981 } 1982 1983 /* 1984 * @brief 1985 */ 1986 void hfp_ag_set_roaming_status(int status){ 1987 hfp_ag_set_ag_indicator("roam", status); 1988 } 1989 1990 /* 1991 * @brief 1992 */ 1993 void hfp_ag_set_battery_level(int level){ 1994 hfp_ag_set_ag_indicator("battchg", level); 1995 } 1996 1997 /* 1998 * @brief 1999 */ 2000 void hfp_ag_activate_voice_recognition(bd_addr_t bd_addr, int activate){ 2001 if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return; 2002 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2003 2004 if (!get_bit(connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) { 2005 printf("AG cannot acivate voice recognition - not supported by HF\n"); 2006 return; 2007 } 2008 2009 if (activate){ 2010 hfp_ag_establish_audio_connection(bd_addr); 2011 } 2012 2013 connection->ag_activate_voice_recognition = activate; 2014 connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 2015 hfp_run_for_context(connection); 2016 } 2017 2018 /* 2019 * @brief 2020 */ 2021 void hfp_ag_set_microphone_gain(bd_addr_t bd_addr, int gain){ 2022 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2023 if (connection->microphone_gain != gain){ 2024 connection->command = HFP_CMD_SET_MICROPHONE_GAIN; 2025 connection->microphone_gain = gain; 2026 connection->send_microphone_gain = 1; 2027 } 2028 hfp_run_for_context(connection); 2029 } 2030 2031 /* 2032 * @brief 2033 */ 2034 void hfp_ag_set_speaker_gain(bd_addr_t bd_addr, int gain){ 2035 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2036 if (connection->speaker_gain != gain){ 2037 connection->speaker_gain = gain; 2038 connection->send_speaker_gain = 1; 2039 } 2040 hfp_run_for_context(connection); 2041 } 2042 2043 /* 2044 * @brief 2045 */ 2046 void hfp_ag_send_phone_number_for_voice_tag(bd_addr_t bd_addr, const char * number){ 2047 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2048 hfp_ag_set_clip(0, number); 2049 connection->send_phone_number_for_voice_tag = 1; 2050 } 2051 2052 void hfp_ag_reject_phone_number_for_voice_tag(bd_addr_t bd_addr){ 2053 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2054 connection->send_error = 1; 2055 } 2056 2057 void hfp_ag_send_dtmf_code_done(bd_addr_t bd_addr){ 2058 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2059 connection->ok_pending = 1; 2060 } 2061 2062 void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 2063 subscriber_numbers = numbers; 2064 subscriber_numbers_count = numbers_count; 2065 } 2066 2067 void hfp_ag_send_current_call_status(bd_addr_t bd_addr, int idx, hfp_enhanced_call_dir_t dir, 2068 hfp_enhanced_call_status_t status, hfp_enhanced_call_mode_t mode, 2069 hfp_enhanced_call_mpty_t mpty, uint8_t type, const char * number){ 2070 2071 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2072 2073 char buffer[100]; 2074 int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 2075 if (number){ 2076 offset += snprintf(buffer+offset, sizeof(buffer)-offset, ", \"%s\",%u", number, type); 2077 } 2078 snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 2079 send_str_over_rfcomm(connection->rfcomm_cid, buffer); 2080 } 2081 2082 2083 void hfp_ag_send_current_call_status_done(bd_addr_t bd_addr){ 2084 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2085 connection->ok_pending = 1; 2086 connection->send_status_of_current_calls = 0; 2087 } 2088 2089