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(hfp_response_and_hold_state_t state){ 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 = state + 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 hfp_emit_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL, 0); 1096 break; 1097 default: 1098 break; 1099 } 1100 break; 1101 default: 1102 break; 1103 } 1104 break; 1105 1106 case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1107 // clear CLIP 1108 clip_type = 0; 1109 switch (hfp_ag_call_state){ 1110 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1111 switch (hfp_ag_callsetup_state){ 1112 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1113 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1114 hfp_ag_set_call_state(HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT); 1115 hfp_ag_hf_accept_call(connection); 1116 printf("HF answers call, accept call by GSM\n"); 1117 hfp_emit_event(hfp_callback, HFP_CMD_CALL_ANSWERED, 0); 1118 break; 1119 default: 1120 break; 1121 } 1122 break; 1123 default: 1124 break; 1125 } 1126 break; 1127 1128 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1129 // clear CLIP 1130 clip_type = 0; 1131 switch (hfp_ag_call_state){ 1132 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1133 switch (hfp_ag_callsetup_state){ 1134 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1135 hfp_ag_response_and_hold_active = 1; 1136 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1137 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1138 // as with regualr call 1139 hfp_ag_set_call_state(HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT); 1140 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1141 hfp_ag_ag_accept_call(); 1142 printf("AG response and hold - hold by AG\n"); 1143 break; 1144 default: 1145 break; 1146 } 1147 break; 1148 default: 1149 break; 1150 } 1151 break; 1152 1153 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1154 // clear CLIP 1155 clip_type = 0; 1156 switch (hfp_ag_call_state){ 1157 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1158 switch (hfp_ag_callsetup_state){ 1159 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1160 hfp_ag_response_and_hold_active = 1; 1161 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1162 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1163 // as with regualr call 1164 hfp_ag_set_call_state(HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT); 1165 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1166 hfp_ag_hf_accept_call(connection); 1167 printf("AG response and hold - hold by HF\n"); 1168 break; 1169 default: 1170 break; 1171 } 1172 break; 1173 default: 1174 break; 1175 } 1176 break; 1177 1178 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1179 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1180 if (!hfp_ag_response_and_hold_active) break; 1181 if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1182 hfp_ag_response_and_hold_active = 0; 1183 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1184 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1185 printf("Held Call accepted and active\n"); 1186 break; 1187 1188 case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1189 case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1190 if (!hfp_ag_response_and_hold_active) break; 1191 if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1192 hfp_ag_response_and_hold_active = 0; 1193 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1194 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1195 // from terminate by ag 1196 hfp_ag_set_call_state(HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS); 1197 hfp_ag_trigger_terminate_call(); 1198 break; 1199 1200 case HFP_AG_TERMINATE_CALL_BY_HF: 1201 // clear CLIP 1202 clip_type = 0; 1203 switch (hfp_ag_call_state){ 1204 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1205 switch (hfp_ag_callsetup_state){ 1206 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1207 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1208 hfp_ag_transfer_callsetup_state(); 1209 hfp_ag_trigger_reject_call(); 1210 printf("HF Rejected Incoming call, AG terminate call\n"); 1211 break; 1212 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1213 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1214 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1215 hfp_ag_transfer_callsetup_state(); 1216 printf("AG terminate outgoing call process\n"); 1217 default: 1218 break; 1219 } 1220 break; 1221 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1222 hfp_ag_set_call_state(HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS); 1223 hfp_ag_transfer_call_state(); 1224 connection->call_state = HFP_CALL_IDLE; 1225 printf("AG terminate call\n"); 1226 break; 1227 } 1228 break; 1229 1230 case HFP_AG_TERMINATE_CALL_BY_AG: 1231 // clear CLIP 1232 clip_type = 0; 1233 switch (hfp_ag_call_state){ 1234 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1235 switch (hfp_ag_callsetup_state){ 1236 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1237 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1238 hfp_ag_trigger_reject_call(); 1239 printf("AG Rejected Incoming call, AG terminate call\n"); 1240 break; 1241 default: 1242 break; 1243 } 1244 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1245 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1246 hfp_ag_set_call_state(HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS); 1247 hfp_ag_trigger_terminate_call(); 1248 printf("AG terminate call\n"); 1249 break; 1250 default: 1251 break; 1252 } 1253 break; 1254 case HFP_AG_CALL_DROPPED: 1255 // clear CLIP 1256 clip_type = 0; 1257 switch (hfp_ag_call_state){ 1258 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1259 switch (hfp_ag_callsetup_state){ 1260 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1261 hfp_ag_stop_ringing(); 1262 printf("Incoming call interrupted\n"); 1263 break; 1264 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1265 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1266 printf("Outgoing call interrupted\n"); 1267 printf("AG notify call dropped\n"); 1268 break; 1269 default: 1270 break; 1271 } 1272 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1273 hfp_ag_transfer_callsetup_state(); 1274 break; 1275 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1276 if (hfp_ag_response_and_hold_active) { 1277 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1278 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1279 } 1280 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1281 hfp_ag_set_call_state(HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS); 1282 hfp_ag_trigger_terminate_call(); 1283 printf("AG notify call dropped\n"); 1284 break; 1285 default: 1286 break; 1287 } 1288 break; 1289 1290 case HFP_AG_OUTGOING_CALL_INITIATED: 1291 connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1292 hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &connection->line_buffer[3]); 1293 break; 1294 1295 case HFP_AG_OUTGOING_REDIAL_INITIATED: 1296 connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1297 hfp_emit_event(hfp_callback, HFP_SUBEVENT_REDIAL_LAST_NUMBER, 0); 1298 break; 1299 1300 case HFP_AG_OUTGOING_CALL_REJECTED: 1301 connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1302 if (!connection){ 1303 log_info("hfp_ag_call_sm: did not find outgoing connection in initiated state"); 1304 break; 1305 } 1306 connection->call_state = HFP_CALL_IDLE; 1307 connection->send_error = 1; 1308 hfp_run_for_context(connection); 1309 break; 1310 1311 case HFP_AG_OUTGOING_CALL_ACCEPTED: 1312 connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1313 if (!connection){ 1314 log_info("hfp_ag_call_sm: did not find outgoing connection in initiated state"); 1315 break; 1316 } 1317 1318 connection->ok_pending = 1; 1319 connection->call_state = HFP_CALL_OUTGOING_DIALING; 1320 1321 // trigger callsetup to be 1322 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE); 1323 indicator_index = get_ag_indicator_index_for_name("callsetup"); 1324 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1325 1326 // put current call on hold if active 1327 if (hfp_ag_call_state == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1328 printf("AG putting current call on hold for new outgoing call\n"); 1329 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS); 1330 indicator_index = get_ag_indicator_index_for_name("callheld"); 1331 hfp_ag_transfer_ag_indicators_status_cmd(connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 1332 } 1333 1334 // start audio if needed 1335 hfp_ag_establish_audio_connection(connection->remote_addr); 1336 break; 1337 1338 case HFP_AG_OUTGOING_CALL_RINGING: 1339 connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1340 if (!connection){ 1341 log_info("hfp_ag_call_sm: did not find outgoing connection in dialing state"); 1342 break; 1343 } 1344 connection->call_state = HFP_CALL_OUTGOING_RINGING; 1345 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE); 1346 hfp_ag_transfer_callsetup_state(); 1347 break; 1348 1349 case HFP_AG_OUTGOING_CALL_ESTABLISHED: 1350 // get outgoing call 1351 connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1352 if (!connection){ 1353 connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1354 } 1355 if (!connection){ 1356 log_info("hfp_ag_call_sm: did not find outgoing connection"); 1357 break; 1358 } 1359 connection->call_state = HFP_CALL_ACTIVE; 1360 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1361 hfp_ag_set_call_state(HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT); 1362 hfp_ag_transfer_call_state(); 1363 hfp_ag_transfer_callsetup_state(); 1364 if (hfp_ag_callheld_state == HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){ 1365 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1366 hfp_ag_transfer_callheld_state(); 1367 } 1368 break; 1369 1370 default: 1371 break; 1372 } 1373 } 1374 1375 static void hfp_run_for_context(hfp_connection_t *context){ 1376 if (!context) return; 1377 if (!rfcomm_can_send_packet_now(context->rfcomm_cid)) return; 1378 1379 if (context->send_status_of_current_calls){ 1380 hfp_emit_event(hfp_callback, HFP_SUBEVENT_TRANSMIT_STATUS_OF_CURRENT_CALL, 0); 1381 return; 1382 } 1383 1384 if (context->command == HFP_CMD_UNKNOWN){ 1385 context->ok_pending = 0; 1386 context->send_error = 0; 1387 context->command = HFP_CMD_NONE; 1388 hfp_ag_error(context->rfcomm_cid); 1389 return; 1390 } 1391 1392 if (context->send_error){ 1393 context->send_error = 0; 1394 context->command = HFP_CMD_NONE; 1395 hfp_ag_error(context->rfcomm_cid); 1396 return; 1397 } 1398 1399 // note: before update AG indicators and ok_pending 1400 if (context->send_response_and_hold_status){ 1401 int status = context->send_response_and_hold_status - 1; 1402 context->send_response_and_hold_status = 0; 1403 hfp_ag_set_response_and_hold(context->rfcomm_cid, status); 1404 return; 1405 } 1406 1407 if (context->ok_pending){ 1408 context->ok_pending = 0; 1409 context->command = HFP_CMD_NONE; 1410 hfp_ag_ok(context->rfcomm_cid); 1411 return; 1412 } 1413 1414 // update AG indicators 1415 if (context->ag_indicators_status_update_bitmap){ 1416 int i; 1417 for (i=0;i<context->ag_indicators_nr;i++){ 1418 if (get_bit(context->ag_indicators_status_update_bitmap, i)){ 1419 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, i, 0); 1420 if (!context->enable_status_update_for_ag_indicators) { 1421 log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name); 1422 break; 1423 } 1424 hfp_ag_transfer_ag_indicators_status_cmd(context->rfcomm_cid, &hfp_ag_indicators[i]); 1425 return; 1426 } 1427 } 1428 } 1429 1430 if (context->ag_ring){ 1431 context->ag_ring = 0; 1432 context->command = HFP_CMD_NONE; 1433 hfp_ag_ring(context->rfcomm_cid); 1434 return; 1435 } 1436 1437 if (context->ag_send_clip){ 1438 context->ag_send_clip = 0; 1439 context->command = HFP_CMD_NONE; 1440 hfp_ag_send_clip(context->rfcomm_cid); 1441 return; 1442 } 1443 1444 if (context->send_phone_number_for_voice_tag){ 1445 context->send_phone_number_for_voice_tag = 0; 1446 context->command = HFP_CMD_NONE; 1447 context->ok_pending = 1; 1448 hfp_ag_send_phone_number_for_voice_tag_cmd(context->rfcomm_cid); 1449 return; 1450 } 1451 1452 if (context->send_subscriber_number){ 1453 if (context->next_subscriber_number_to_send < subscriber_numbers_count){ 1454 hfp_phone_number_t phone = subscriber_numbers[context->next_subscriber_number_to_send++]; 1455 hfp_send_subscriber_number_cmd(context->rfcomm_cid, phone.type, phone.number); 1456 } else { 1457 context->send_subscriber_number = 0; 1458 context->next_subscriber_number_to_send = 0; 1459 hfp_ag_ok(context->rfcomm_cid); 1460 } 1461 context->command = HFP_CMD_NONE; 1462 } 1463 1464 if (context->send_microphone_gain){ 1465 context->send_microphone_gain = 0; 1466 context->command = HFP_CMD_NONE; 1467 hfp_ag_set_microphone_gain_cmd(context->rfcomm_cid, context->microphone_gain); 1468 return; 1469 } 1470 1471 if (context->send_speaker_gain){ 1472 context->send_speaker_gain = 0; 1473 context->command = HFP_CMD_NONE; 1474 hfp_ag_set_speaker_gain_cmd(context->rfcomm_cid, context->speaker_gain); 1475 return; 1476 } 1477 1478 if (context->send_ag_status_indicators){ 1479 context->send_ag_status_indicators = 0; 1480 hfp_ag_retrieve_indicators_cmd(context->rfcomm_cid, context); 1481 return; 1482 } 1483 1484 int done = hfp_ag_run_for_context_service_level_connection(context); 1485 if (!done){ 1486 done = hfp_ag_run_for_context_service_level_connection_queries(context); 1487 } 1488 1489 if (!done){ 1490 done = call_setup_state_machine(context); 1491 } 1492 1493 if (!done){ 1494 done = hfp_ag_run_for_audio_connection(context); 1495 } 1496 1497 if (context->command == HFP_CMD_NONE && !done){ 1498 // log_info("context->command == HFP_CMD_NONE"); 1499 switch(context->state){ 1500 case HFP_W2_DISCONNECT_RFCOMM: 1501 context->state = HFP_W4_RFCOMM_DISCONNECTED; 1502 rfcomm_disconnect_internal(context->rfcomm_cid); 1503 break; 1504 default: 1505 break; 1506 } 1507 } 1508 if (done){ 1509 context->command = HFP_CMD_NONE; 1510 } 1511 } 1512 static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){ 1513 int i; 1514 for (i=0;i< get_hfp_generic_status_indicators_nr();i++){ 1515 hfp_generic_status_indicator_t * indicator = &get_hfp_generic_status_indicators()[i]; 1516 if (indicator->uuid == number){ 1517 return indicator; 1518 } 1519 } 1520 return NULL; 1521 } 1522 1523 static void hfp_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1524 hfp_connection_t * context = get_hfp_connection_context_for_rfcomm_cid(channel); 1525 if (!context) return; 1526 1527 char last_char = packet[size-1]; 1528 packet[size-1] = 0; 1529 log_info("HFP_RX %s", packet); 1530 packet[size-1] = last_char; 1531 1532 int pos; 1533 for (pos = 0; pos < size ; pos++){ 1534 hfp_parse(context, packet[pos], 0); 1535 } 1536 hfp_generic_status_indicator_t * indicator; 1537 int value; 1538 switch(context->command){ 1539 case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 1540 if (hfp_ag_response_and_hold_active){ 1541 context->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 1542 } 1543 context->ok_pending = 1; 1544 break; 1545 case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 1546 value = atoi((char *)&context->line_buffer[0]); 1547 printf("HF Response and Hold: %u\n", value); 1548 switch(value){ 1549 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 1550 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, context); 1551 break; 1552 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 1553 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, context); 1554 break; 1555 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 1556 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, context); 1557 break; 1558 default: 1559 break; 1560 } 1561 context->ok_pending = 1; 1562 break; 1563 case HFP_CMD_HF_INDICATOR_STATUS: 1564 context->command = HFP_CMD_NONE; 1565 // find indicator by assigned number 1566 indicator = get_hf_indicator_by_number(context->parser_indicator_index); 1567 if (!indicator){ 1568 context->send_error = 1; 1569 break; 1570 } 1571 value = atoi((char *)&context->line_buffer[0]); 1572 switch (indicator->uuid){ 1573 case 1: // enhanced security 1574 if (value > 1) { 1575 context->send_error = 1; 1576 return; 1577 } 1578 printf("HF Indicator 'enhanced security' set to %u\n", value); 1579 break; 1580 case 2: // battery level 1581 if (value > 100){ 1582 context->send_error = 1; 1583 return; 1584 } 1585 printf("HF Indicator 'battery' set to %u\n", value); 1586 break; 1587 default: 1588 printf("HF Indicator unknown set to %u\n", value); 1589 break; 1590 } 1591 context->ok_pending = 1; 1592 break; 1593 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1594 // expected by SLC state machine 1595 if (context->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 1596 context->send_ag_status_indicators = 1; 1597 break; 1598 case HFP_CMD_LIST_CURRENT_CALLS: 1599 context->command = HFP_CMD_NONE; 1600 context->send_status_of_current_calls = 1; 1601 hfp_emit_event(hfp_callback, HFP_SUBEVENT_TRANSMIT_STATUS_OF_CURRENT_CALL, 0); 1602 break; 1603 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1604 if (subscriber_numbers_count == 0){ 1605 hfp_ag_ok(context->rfcomm_cid); 1606 break; 1607 } 1608 context->next_subscriber_number_to_send = 0; 1609 context->send_subscriber_number = 1; 1610 break; 1611 case HFP_CMD_TRANSMIT_DTMF_CODES: 1612 context->command = HFP_CMD_NONE; 1613 hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &context->line_buffer[0]); 1614 break; 1615 case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 1616 context->command = HFP_CMD_NONE; 1617 hfp_emit_event(hfp_callback, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG, 0); 1618 break; 1619 case HFP_CMD_TURN_OFF_EC_AND_NR: 1620 context->command = HFP_CMD_NONE; 1621 if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 1622 context->ok_pending = 1; 1623 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, context->ag_echo_and_noise_reduction); 1624 printf("AG: EC/NR = %u\n", context->ag_echo_and_noise_reduction); 1625 } else { 1626 context->send_error = 1; 1627 } 1628 break; 1629 case HFP_CMD_CALL_ANSWERED: 1630 context->command = HFP_CMD_NONE; 1631 printf("HFP: ATA\n"); 1632 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, context); 1633 break; 1634 case HFP_CMD_HANG_UP_CALL: 1635 context->command = HFP_CMD_NONE; 1636 context->ok_pending = 1; 1637 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, context); 1638 break; 1639 case HFP_CMD_CALL_HOLD: { 1640 // TODO: fully implement this 1641 log_error("HFP: unhandled call hold type %c", context->line_buffer[0]); 1642 int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1643 int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1644 int call_indicator_index = get_ag_indicator_index_for_name("call"); 1645 switch (context->line_buffer[0]){ 1646 case '0': 1647 context->command = HFP_CMD_NONE; 1648 context->ok_pending = 1; 1649 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1650 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1651 context->call_state = HFP_CALL_ACTIVE; 1652 printf("AG: Call Waiting, User Busy\n"); 1653 break; 1654 case '1': 1655 // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1656 context->command = HFP_CMD_NONE; 1657 context->ok_pending = 1; 1658 if (hfp_ag_callsetup_state != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS){ 1659 printf("AG: Call Dropped, Accept new call\n"); 1660 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1661 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1662 } else { 1663 printf("AG: Call Dropped, Resume held call\n"); 1664 } 1665 if (hfp_ag_callheld_state != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 1666 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_NO_CALLS_HELD); 1667 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1668 } 1669 context->call_state = HFP_CALL_ACTIVE; 1670 break; 1671 case '2': 1672 // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1673 context->command = HFP_CMD_NONE; 1674 context->ok_pending = 1; 1675 // only update if callsetup changed 1676 if (hfp_ag_callsetup_state != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS){ 1677 printf("AG: Call on Hold, Accept new call\n"); 1678 hfp_ag_set_callsetup_state(HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS); 1679 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1680 } else { 1681 printf("AG: Swap calls\n"); 1682 } 1683 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1684 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1685 context->call_state = HFP_CALL_ACTIVE; 1686 break; 1687 case '3': 1688 // Adds a held call to the conversation. 1689 context->command = HFP_CMD_NONE; 1690 context->ok_pending = 1; 1691 if (hfp_ag_callheld_state != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 1692 printf("AG: Join 3-way-call\n"); 1693 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_NO_CALLS_HELD); 1694 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1695 hfp_emit_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL, 0); 1696 } 1697 context->call_state = HFP_CALL_ACTIVE; 1698 break; 1699 case '4': 1700 // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 1701 context->command = HFP_CMD_NONE; 1702 context->ok_pending = 1; 1703 printf("AG: Transfer call -> Connect two calls and disconnect\n"); 1704 hfp_ag_set_call_state(HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS); 1705 hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_NO_CALLS_HELD); 1706 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1707 context->ag_indicators_status_update_bitmap = store_bit(context->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1708 context->call_state = HFP_CALL_IDLE; 1709 break; 1710 default: 1711 break; 1712 } 1713 break; 1714 } 1715 case HFP_CMD_CALL_PHONE_NUMBER: 1716 context->command = HFP_CMD_NONE; 1717 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, context); 1718 break; 1719 case HFP_CMD_REDIAL_LAST_NUMBER: 1720 context->command = HFP_CMD_NONE; 1721 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, context); 1722 break; 1723 case HFP_CMD_ENABLE_CLIP: 1724 context->command = HFP_CMD_NONE; 1725 context->clip_enabled = context->line_buffer[8] != '0'; 1726 log_info("hfp: clip set, now: %u", context->clip_enabled); 1727 context->ok_pending = 1; 1728 break; 1729 case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 1730 context->command = HFP_CMD_NONE; 1731 context->call_waiting_notification_enabled = context->line_buffer[8] != '0'; 1732 log_info("hfp: call waiting notification set, now: %u", context->call_waiting_notification_enabled); 1733 context->ok_pending = 1; 1734 break; 1735 case HFP_CMD_SET_SPEAKER_GAIN: 1736 context->command = HFP_CMD_NONE; 1737 context->ok_pending = 1; 1738 printf("HF speaker gain = %u\n", context->speaker_gain); 1739 break; 1740 case HFP_CMD_SET_MICROPHONE_GAIN: 1741 context->command = HFP_CMD_NONE; 1742 context->ok_pending = 1; 1743 printf("HF microphone gain = %u\n", context->microphone_gain); 1744 break; 1745 default: 1746 break; 1747 } 1748 } 1749 1750 static void hfp_run(void){ 1751 linked_list_iterator_t it; 1752 linked_list_iterator_init(&it, hfp_get_connections()); 1753 while (linked_list_iterator_has_next(&it)){ 1754 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 1755 hfp_run_for_context(connection); 1756 } 1757 } 1758 1759 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1760 switch (packet_type){ 1761 case RFCOMM_DATA_PACKET: 1762 hfp_handle_rfcomm_data(packet_type, channel, packet, size); 1763 break; 1764 case HCI_EVENT_PACKET: 1765 hfp_handle_hci_event(hfp_callback, packet_type, packet, size); 1766 break; 1767 default: 1768 break; 1769 } 1770 1771 hfp_run(); 1772 } 1773 1774 static void hfp_ag_set_ag_indicators(hfp_ag_indicator_t * ag_indicators, int ag_indicators_nr){ 1775 hfp_ag_indicators_nr = ag_indicators_nr; 1776 memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 1777 } 1778 1779 void hfp_ag_init(uint16_t rfcomm_channel_nr, uint32_t supported_features, 1780 uint8_t * codecs, int codecs_nr, 1781 hfp_ag_indicator_t * ag_indicators, int ag_indicators_nr, 1782 hfp_generic_status_indicator_t * hf_indicators, int hf_indicators_nr, 1783 const char *call_hold_services[], int call_hold_services_nr){ 1784 if (codecs_nr > HFP_MAX_NUM_CODECS){ 1785 log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 1786 return; 1787 } 1788 l2cap_init(); 1789 l2cap_register_packet_handler(packet_handler); 1790 1791 rfcomm_register_packet_handler(packet_handler); 1792 1793 hfp_init(rfcomm_channel_nr); 1794 1795 hfp_supported_features = supported_features; 1796 hfp_codecs_nr = codecs_nr; 1797 1798 int i; 1799 for (i=0; i<codecs_nr; i++){ 1800 hfp_codecs[i] = codecs[i]; 1801 } 1802 1803 hfp_ag_set_ag_indicators(ag_indicators, ag_indicators_nr); 1804 1805 set_hfp_generic_status_indicators(hf_indicators, hf_indicators_nr); 1806 1807 hfp_ag_call_hold_services_nr = call_hold_services_nr; 1808 memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *)); 1809 1810 hfp_ag_call_state = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS; 1811 hfp_ag_callsetup_state = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1812 hfp_ag_callheld_state = HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1813 } 1814 1815 void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 1816 hfp_establish_service_level_connection(bd_addr, SDP_Handsfree); 1817 } 1818 1819 void hfp_ag_release_service_level_connection(bd_addr_t bd_addr){ 1820 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1821 hfp_release_service_level_connection(connection); 1822 hfp_run_for_context(connection); 1823 } 1824 1825 void hfp_ag_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, hfp_cme_error_t error){ 1826 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1827 if (!connection){ 1828 log_error("HFP HF: connection doesn't exist."); 1829 return; 1830 } 1831 connection->extended_audio_gateway_error = 0; 1832 if (!connection->enable_extended_audio_gateway_error_report){ 1833 return; 1834 } 1835 connection->extended_audio_gateway_error = error; 1836 hfp_run_for_context(connection); 1837 } 1838 1839 static void hfp_ag_setup_audio_connection(hfp_connection_t * connection){ 1840 if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1841 if (connection->state >= HFP_W2_DISCONNECT_SCO) return; 1842 1843 connection->establish_audio_connection = 1; 1844 1845 if (!has_codec_negotiation_feature(connection)){ 1846 log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); 1847 connection->codecs_state = HFP_CODECS_EXCHANGED; 1848 } 1849 1850 switch (connection->codecs_state){ 1851 case HFP_CODECS_IDLE: 1852 case HFP_CODECS_RECEIVED_LIST: 1853 case HFP_CODECS_AG_RESEND_COMMON_CODEC: 1854 case HFP_CODECS_ERROR: 1855 connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 1856 break; 1857 default: 1858 break; 1859 } 1860 } 1861 1862 void hfp_ag_establish_audio_connection(bd_addr_t bd_addr){ 1863 hfp_ag_establish_service_level_connection(bd_addr); 1864 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1865 1866 connection->establish_audio_connection = 0; 1867 hfp_ag_setup_audio_connection(connection); 1868 hfp_run_for_context(connection); 1869 } 1870 1871 void hfp_ag_release_audio_connection(bd_addr_t bd_addr){ 1872 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 1873 hfp_release_audio_connection(connection); 1874 hfp_run_for_context(connection); 1875 } 1876 1877 /** 1878 * @brief Enable in-band ring tone 1879 */ 1880 void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 1881 if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){ 1882 return; 1883 } 1884 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone); 1885 1886 linked_list_iterator_t it; 1887 linked_list_iterator_init(&it, hfp_get_connections()); 1888 while (linked_list_iterator_has_next(&it)){ 1889 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 1890 connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 1891 hfp_run_for_context(connection); 1892 } 1893 } 1894 1895 /** 1896 * @brief Called from GSM 1897 */ 1898 void hfp_ag_incoming_call(void){ 1899 hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 1900 } 1901 1902 /** 1903 * @brief number is stored. 1904 */ 1905 void hfp_ag_set_clip(uint8_t type, const char * number){ 1906 clip_type = type; 1907 // copy and terminate 1908 strncpy(clip_number, number, sizeof(clip_number)); 1909 clip_number[sizeof(clip_number)-1] = '\0'; 1910 } 1911 1912 void hfp_ag_call_dropped(void){ 1913 hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 1914 } 1915 1916 // call from AG UI 1917 void hfp_ag_answer_incoming_call(void){ 1918 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 1919 } 1920 1921 void hfp_ag_join_held_call(void){ 1922 hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 1923 } 1924 1925 void hfp_ag_terminate_call(void){ 1926 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 1927 } 1928 1929 void hfp_ag_outgoing_call_ringing(void){ 1930 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 1931 } 1932 1933 void hfp_ag_outgoing_call_established(void){ 1934 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 1935 } 1936 1937 void hfp_ag_outgoing_call_rejected(void){ 1938 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 1939 } 1940 1941 void hfp_ag_outgoing_call_accepted(void){ 1942 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 1943 } 1944 1945 void hfp_ag_hold_incoming_call(void){ 1946 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 1947 } 1948 1949 void hfp_ag_accept_held_incoming_call(void) { 1950 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 1951 } 1952 1953 void hfp_ag_reject_held_incoming_call(void){ 1954 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 1955 } 1956 1957 static void hfp_ag_set_ag_indicator(const char * name, int value){ 1958 int indicator_index = get_ag_indicator_index_for_name(name); 1959 if (indicator_index < 0) return; 1960 hfp_ag_indicators[indicator_index].status = value; 1961 1962 1963 linked_list_iterator_t it; 1964 linked_list_iterator_init(&it, hfp_get_connections()); 1965 while (linked_list_iterator_has_next(&it)){ 1966 hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it); 1967 if (!connection->ag_indicators[indicator_index].enabled) { 1968 log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value); 1969 continue; 1970 } 1971 log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value); 1972 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1973 hfp_run_for_context(connection); 1974 } 1975 } 1976 1977 /* 1978 * @brief 1979 */ 1980 void hfp_ag_set_registration_status(int status){ 1981 hfp_ag_set_ag_indicator("service", status); 1982 } 1983 1984 /* 1985 * @brief 1986 */ 1987 void hfp_ag_set_signal_strength(int strength){ 1988 hfp_ag_set_ag_indicator("signal", strength); 1989 } 1990 1991 /* 1992 * @brief 1993 */ 1994 void hfp_ag_set_roaming_status(int status){ 1995 hfp_ag_set_ag_indicator("roam", status); 1996 } 1997 1998 /* 1999 * @brief 2000 */ 2001 void hfp_ag_set_battery_level(int level){ 2002 hfp_ag_set_ag_indicator("battchg", level); 2003 } 2004 2005 /* 2006 * @brief 2007 */ 2008 void hfp_ag_activate_voice_recognition(bd_addr_t bd_addr, int activate){ 2009 if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return; 2010 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2011 2012 if (!get_bit(connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) { 2013 printf("AG cannot acivate voice recognition - not supported by HF\n"); 2014 return; 2015 } 2016 2017 if (activate){ 2018 hfp_ag_establish_audio_connection(bd_addr); 2019 } 2020 2021 connection->ag_activate_voice_recognition = activate; 2022 connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 2023 hfp_run_for_context(connection); 2024 } 2025 2026 /* 2027 * @brief 2028 */ 2029 void hfp_ag_set_microphone_gain(bd_addr_t bd_addr, int gain){ 2030 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2031 if (connection->microphone_gain != gain){ 2032 connection->command = HFP_CMD_SET_MICROPHONE_GAIN; 2033 connection->microphone_gain = gain; 2034 connection->send_microphone_gain = 1; 2035 } 2036 hfp_run_for_context(connection); 2037 } 2038 2039 /* 2040 * @brief 2041 */ 2042 void hfp_ag_set_speaker_gain(bd_addr_t bd_addr, int gain){ 2043 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2044 if (connection->speaker_gain != gain){ 2045 connection->speaker_gain = gain; 2046 connection->send_speaker_gain = 1; 2047 } 2048 hfp_run_for_context(connection); 2049 } 2050 2051 /* 2052 * @brief 2053 */ 2054 void hfp_ag_send_phone_number_for_voice_tag(bd_addr_t bd_addr, const char * number){ 2055 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2056 hfp_ag_set_clip(0, number); 2057 connection->send_phone_number_for_voice_tag = 1; 2058 } 2059 2060 void hfp_ag_reject_phone_number_for_voice_tag(bd_addr_t bd_addr){ 2061 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2062 connection->send_error = 1; 2063 } 2064 2065 void hfp_ag_send_dtmf_code_done(bd_addr_t bd_addr){ 2066 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2067 connection->ok_pending = 1; 2068 } 2069 2070 void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 2071 subscriber_numbers = numbers; 2072 subscriber_numbers_count = numbers_count; 2073 } 2074 2075 void hfp_ag_send_current_call_status(bd_addr_t bd_addr, int idx, hfp_enhanced_call_dir_t dir, 2076 hfp_enhanced_call_status_t status, hfp_enhanced_call_mode_t mode, 2077 hfp_enhanced_call_mpty_t mpty, uint8_t type, const char * number){ 2078 2079 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2080 2081 char buffer[100]; 2082 int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 2083 if (number){ 2084 offset += snprintf(buffer+offset, sizeof(buffer)-offset, ", \"%s\",%u", number, type); 2085 } 2086 snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 2087 send_str_over_rfcomm(connection->rfcomm_cid, buffer); 2088 } 2089 2090 2091 void hfp_ag_send_current_call_status_done(bd_addr_t bd_addr){ 2092 hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr); 2093 connection->ok_pending = 1; 2094 connection->send_status_of_current_calls = 0; 2095 } 2096 2097