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 #define BTSTACK_FILE__ "hfp_ag.c" 39 40 // ***************************************************************************** 41 // 42 // HFP Audio Gateway (AG) unit 43 // 44 // ***************************************************************************** 45 46 #include "btstack_config.h" 47 48 #include <stdint.h> 49 #include <string.h> 50 51 #include "hci_cmd.h" 52 #include "btstack_run_loop.h" 53 54 #include "bluetooth_sdp.h" 55 #include "hci.h" 56 #include "btstack_memory.h" 57 #include "hci_dump.h" 58 #include "l2cap.h" 59 #include "btstack_debug.h" 60 #include "btstack_event.h" 61 #include "classic/core.h" 62 #include "classic/hfp.h" 63 #include "classic/hfp_ag.h" 64 #include "classic/hfp_gsm_model.h" 65 #include "classic/sdp_client_rfcomm.h" 66 #include "classic/sdp_server.h" 67 #include "classic/sdp_util.h" 68 69 // private prototypes 70 static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection); 71 static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection); 72 static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection); 73 74 // public prototypes 75 hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void); 76 int get_hfp_generic_status_indicators_nr(void); 77 void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr); 78 void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr); 79 int get_hfp_ag_indicators_nr(hfp_connection_t * context); 80 hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context); 81 82 static btstack_packet_callback_registration_t hci_event_callback_registration; 83 84 // gobals 85 static const char default_hfp_ag_service_name[] = "Voice gateway"; 86 87 static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES; 88 89 static uint8_t hfp_codecs_nr = 0; 90 static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 91 92 static int hfp_ag_indicators_nr = 0; 93 static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS]; 94 95 static int hfp_generic_status_indicators_nr = 0; 96 static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_INDICATORS]; 97 98 static int hfp_ag_call_hold_services_nr = 0; 99 static char *hfp_ag_call_hold_services[6]; 100 static btstack_packet_handler_t hfp_ag_callback; 101 102 static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state; 103 static int hfp_ag_response_and_hold_active = 0; 104 105 // Subcriber information entries 106 static hfp_phone_number_t * subscriber_numbers = NULL; 107 static int subscriber_numbers_count = 0; 108 109 hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection); 110 111 112 static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){ 113 if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 114 hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 115 (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 116 hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 117 } 118 return hfp_connection->ag_indicators_nr; 119 } 120 121 hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){ 122 // TODO: save only value, and value changed in the hfp_connection? 123 if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 124 hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 125 (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 126 hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 127 } 128 return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators); 129 } 130 131 static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){ 132 int i; 133 for (i = 0; i < hfp_ag_indicators_nr; i++){ 134 if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 135 return &hfp_ag_indicators[i]; 136 } 137 } 138 return NULL; 139 } 140 141 static int get_ag_indicator_index_for_name(const char * name){ 142 int i; 143 for (i = 0; i < hfp_ag_indicators_nr; i++){ 144 if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 145 return i; 146 } 147 } 148 return -1; 149 } 150 151 static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){ 152 btstack_linked_list_iterator_t it; 153 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 154 while (btstack_linked_list_iterator_has_next(&it)){ 155 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 156 if (hfp_connection->acl_handle != handle) continue; 157 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 158 return hfp_connection; 159 } 160 return NULL; 161 } 162 163 void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){ 164 if (callback == NULL){ 165 log_error("hfp_ag_register_packet_handler called with NULL callback"); 166 return; 167 } 168 hfp_ag_callback = callback; 169 hfp_set_ag_callback(callback); 170 } 171 172 static int use_in_band_tone(void){ 173 return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 174 } 175 176 static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 177 int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 178 int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 179 return hf && ag; 180 } 181 182 static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 183 int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING); 184 int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING); 185 return hf && ag; 186 } 187 188 static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 189 int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS); 190 int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS); 191 return hf && ag; 192 } 193 194 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, int wide_band_speech){ 195 if (!name){ 196 name = default_hfp_ag_service_name; 197 } 198 hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name); 199 200 /* 201 * 0x01 – Ability to reject a call 202 * 0x00 – No ability to reject a call 203 */ 204 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 205 de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 206 207 // Construct SupportedFeatures for SDP bitmap: 208 // 209 // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 210 // of the Bits 0 to 4 of the unsolicited result code +BRSF" 211 // 212 // Wide band speech (bit 5) requires Codec negotiation 213 // 214 uint16_t sdp_features = supported_features & 0x1f; 215 if (wide_band_speech && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){ 216 sdp_features |= 1 << 5; 217 } 218 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 219 de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 220 } 221 222 static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){ 223 char buffer[20]; 224 sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone()); 225 return send_str_over_rfcomm(cid, buffer); 226 } 227 228 static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){ 229 char buffer[40]; 230 sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features); 231 return send_str_over_rfcomm(cid, buffer); 232 } 233 234 static int hfp_ag_send_ok(uint16_t cid){ 235 char buffer[10]; 236 sprintf(buffer, "\r\nOK\r\n"); 237 return send_str_over_rfcomm(cid, buffer); 238 } 239 240 static int hfp_ag_send_ring(uint16_t cid){ 241 return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n"); 242 } 243 244 static int hfp_ag_send_clip(uint16_t cid){ 245 char buffer[50]; 246 sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type()); 247 return send_str_over_rfcomm(cid, buffer); 248 } 249 250 static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){ 251 char buffer[50]; 252 sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type); 253 return send_str_over_rfcomm(cid, buffer); 254 } 255 256 static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){ 257 char buffer[50]; 258 sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number()); 259 return send_str_over_rfcomm(cid, buffer); 260 } 261 262 static int hfp_ag_send_call_waiting_notification(uint16_t cid){ 263 char buffer[50]; 264 sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type()); 265 return send_str_over_rfcomm(cid, buffer); 266 } 267 268 static int hfp_ag_send_error(uint16_t cid){ 269 char buffer[10]; 270 sprintf(buffer, "\r\nERROR\r\n"); 271 return send_str_over_rfcomm(cid, buffer); 272 } 273 274 static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){ 275 char buffer[20]; 276 sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error); 277 return send_str_over_rfcomm(cid, buffer); 278 } 279 280 // get size for indicator string 281 static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){ 282 // template: ("$NAME",($MIN,$MAX)) 283 return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name) 284 + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range) 285 + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range); 286 } 287 288 // store indicator 289 static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){ 290 sprintf((char *) buffer, "(\"%s\",(%d,%d)),", 291 hfp_ag_get_ag_indicators(hfp_connection)[i].name, 292 hfp_ag_get_ag_indicators(hfp_connection)[i].min_range, 293 hfp_ag_get_ag_indicators(hfp_connection)[i].max_range); 294 } 295 296 // structure: header [indicator [comma indicator]] footer 297 static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){ 298 int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 299 if (!num_indicators) return 2; 300 return 3 + ((num_indicators-1) * 2); 301 } 302 303 // get size of individual segment for hfp_ag_retrieve_indicators_cmd 304 static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){ 305 if (index == 0) { 306 return strlen(HFP_INDICATOR) + 3; // "\n\r%s:"" 307 } 308 index--; 309 int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 310 int indicator_index = index >> 1; 311 if ((index & 1) == 0){ 312 return hfp_ag_indicators_string_size(hfp_connection, indicator_index); 313 } 314 if (indicator_index == (num_indicators - 1)){ 315 return 8; // "\r\n\r\nOK\r\n" 316 } 317 return 1; // comma 318 } 319 320 static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){ 321 if (index == 0){ 322 *buffer++ = '\r'; 323 *buffer++ = '\n'; 324 int len = strlen(HFP_INDICATOR); 325 (void)memcpy(buffer, HFP_INDICATOR, len); 326 buffer += len; 327 *buffer++ = ':'; 328 return; 329 } 330 index--; 331 int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 332 int indicator_index = index >> 1; 333 if ((index & 1) == 0){ 334 hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer); 335 return; 336 } 337 if (indicator_index == (num_indicators-1)){ 338 (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8); 339 return; 340 } 341 *buffer = ','; 342 } 343 344 static int hfp_hf_indicators_join(char * buffer, int buffer_size){ 345 if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 346 int i; 347 int offset = 0; 348 for (i = 0; i < (hfp_generic_status_indicators_nr-1); i++) { 349 offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid); 350 } 351 if (i < hfp_generic_status_indicators_nr){ 352 offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid); 353 } 354 return offset; 355 } 356 357 static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){ 358 if (buffer_size < (hfp_generic_status_indicators_nr * 3)) return 0; 359 int i; 360 int offset = 0; 361 for (i = 0; i < hfp_generic_status_indicators_nr; i++) { 362 offset += snprintf(buffer+offset, buffer_size-offset, "\r\n%s:%d,%d\r\n", HFP_GENERIC_STATUS_INDICATOR, hfp_generic_status_indicators[i].uuid, hfp_generic_status_indicators[i].state); 363 } 364 return offset; 365 } 366 367 static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){ 368 if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 369 int i; 370 int offset = 0; 371 for (i = 0; i < (hfp_ag_indicators_nr-1); i++) { 372 offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status); 373 } 374 if (i<hfp_ag_indicators_nr){ 375 offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status); 376 } 377 return offset; 378 } 379 380 static int hfp_ag_call_services_join(char * buffer, int buffer_size){ 381 if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0; 382 int i; 383 int offset = snprintf(buffer, buffer_size, "("); 384 for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) { 385 offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]); 386 } 387 if (i<hfp_ag_call_hold_services_nr){ 388 offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]); 389 } 390 return offset; 391 } 392 393 static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, 394 int start_segment, int num_segments, 395 int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment), 396 void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){ 397 398 // assumes: can send now == true 399 // assumes: num segments > 0 400 // assumes: individual segments are smaller than MTU 401 rfcomm_reserve_packet_buffer(); 402 int mtu = rfcomm_get_max_frame_size(cid); 403 uint8_t * data = rfcomm_get_outgoing_buffer(); 404 int offset = 0; 405 int segment = start_segment; 406 while (segment < num_segments){ 407 int segment_len = get_segment_len(hfp_connection, segment); 408 if ((offset + segment_len) > mtu) break; 409 // append segement 410 store_segment(hfp_connection, segment, data+offset); 411 offset += segment_len; 412 segment++; 413 } 414 rfcomm_send_prepared(cid, offset); 415 return segment; 416 } 417 418 // returns next segment to store 419 static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){ 420 int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 421 return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments, 422 hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment); 423 } 424 425 static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){ 426 char buffer[40]; 427 const int size = sizeof(buffer); 428 int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR); 429 offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9); 430 offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 431 return send_str_over_rfcomm(cid, buffer); 432 } 433 434 static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){ 435 char buffer[40]; 436 const int size = sizeof(buffer); 437 int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 438 offset += hfp_ag_call_services_join(buffer+offset, size-offset-9); 439 offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 440 return send_str_over_rfcomm(cid, buffer); 441 } 442 443 444 static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){ 445 return hfp_ag_send_ok(cid); 446 } 447 448 static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){ 449 char buffer[40]; 450 const int size = sizeof(buffer); 451 int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR); 452 offset += hfp_hf_indicators_join(buffer+offset, size-offset-10); 453 offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n"); 454 return send_str_over_rfcomm(cid, buffer); 455 } 456 457 static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){ 458 char buffer[40]; 459 int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7); 460 snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n"); 461 return send_str_over_rfcomm(cid, buffer); 462 } 463 464 static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){ 465 char buffer[20]; 466 sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status); 467 return send_str_over_rfcomm(cid, buffer); 468 } 469 470 static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){ 471 char buffer[41]; 472 if (strlen(op.name) == 0){ 473 snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode); 474 } else { 475 int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format); 476 offset += snprintf(buffer+offset, 16, "%s", op.name); 477 snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 478 } 479 return send_str_over_rfcomm(cid, buffer); 480 } 481 482 static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){ 483 char buffer[30]; 484 snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value); 485 return send_str_over_rfcomm(cid, buffer); 486 } 487 488 static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){ 489 return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 490 } 491 492 static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){ 493 return hfp_ag_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition); 494 } 495 496 static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){ 497 return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 498 } 499 500 static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){ 501 return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 502 } 503 504 static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){ 505 return hfp_ag_send_cmd_with_int(cid, HFP_RESPONSE_AND_HOLD, state); 506 } 507 508 static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){ 509 if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD; 510 511 if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){ 512 if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){ 513 return HFP_CODEC_MSBC; 514 } 515 } 516 return HFP_CODEC_CVSD; 517 } 518 519 static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){ 520 return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) && (hfp_supported_features & (1<<HFP_AGSF_ESCO_S4)); 521 } 522 523 static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 524 /* events ( == commands): 525 HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 526 HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 527 hf_trigger_codec_connection_setup == received BCC 528 ag_trigger_codec_connection_setup == received from AG to send BCS 529 HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 530 */ 531 532 switch (hfp_connection->codecs_state){ 533 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 534 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 535 break; 536 case HFP_CODECS_AG_RESEND_COMMON_CODEC: 537 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 538 break; 539 default: 540 break; 541 } 542 543 // printf(" -> State machine: CC\n"); 544 545 switch (hfp_connection->command){ 546 case HFP_CMD_AVAILABLE_CODECS: 547 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 548 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST; 549 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 550 return 1; 551 } 552 553 switch (hfp_connection->codecs_state){ 554 case HFP_CODECS_AG_SENT_COMMON_CODEC: 555 case HFP_CODECS_EXCHANGED: 556 hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC; 557 break; 558 default: 559 break; 560 } 561 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 562 return 1; 563 564 case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 565 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 566 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 567 return 1; 568 569 case HFP_CMD_AG_SEND_COMMON_CODEC: 570 hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC; 571 hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection); 572 hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec); 573 return 1; 574 575 case HFP_CMD_HF_CONFIRMED_CODEC: 576 if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){ 577 hfp_connection->codecs_state = HFP_CODECS_ERROR; 578 hfp_ag_send_error(hfp_connection->rfcomm_cid); 579 return 1; 580 } 581 hfp_connection->negotiated_codec = hfp_connection->codec_confirmed; 582 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 583 log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 584 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 585 // now, pick link settings 586 587 hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 588 return 1; 589 default: 590 break; 591 } 592 return 0; 593 } 594 595 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 596 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 597 hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 598 599 // if active call exist, set per-hfp_connection state active, too (when audio is on) 600 if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 601 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 602 } 603 // if AG is ringing, also start ringing on the HF 604 if ((hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) && 605 (hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS)){ 606 hfp_ag_hf_start_ringing(hfp_connection); 607 } 608 } 609 610 static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 611 // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command); 612 if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 613 int sent = 0; 614 switch(hfp_connection->command){ 615 case HFP_CMD_SUPPORTED_FEATURES: 616 switch(hfp_connection->state){ 617 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 618 case HFP_EXCHANGE_SUPPORTED_FEATURES: 619 hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr); 620 if (has_codec_negotiation_feature(hfp_connection)){ 621 hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 622 } else { 623 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 624 } 625 hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid); 626 return 1; 627 default: 628 break; 629 } 630 break; 631 case HFP_CMD_AVAILABLE_CODECS: 632 sent = codecs_exchange_state_machine(hfp_connection); 633 if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){ 634 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 635 } 636 return sent; 637 638 case HFP_CMD_RETRIEVE_AG_INDICATORS: 639 if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) { 640 // HF requested AG Indicators and we did expect it 641 hfp_connection->send_ag_indicators_segment = 0; 642 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 643 // continue below in state switch 644 } 645 break; 646 647 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 648 if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break; 649 hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 650 hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 651 return 1; 652 653 case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 654 if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break; 655 if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 656 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 657 } else if (has_hf_indicators_feature(hfp_connection)){ 658 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 659 } else { 660 hfp_ag_slc_established(hfp_connection); 661 } 662 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 663 return 1; 664 665 case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 666 if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break; 667 if (has_hf_indicators_feature(hfp_connection)){ 668 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 669 } 670 hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid); 671 if (!has_hf_indicators_feature(hfp_connection)){ 672 hfp_ag_slc_established(hfp_connection); 673 } 674 return 1; 675 676 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 677 if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break; 678 hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 679 hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 680 return 1; 681 682 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 683 if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break; 684 hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 685 hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 686 return 1; 687 688 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 689 if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break; 690 hfp_ag_slc_established(hfp_connection); 691 hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 692 return 1; 693 default: 694 break; 695 } 696 697 switch (hfp_connection->state){ 698 case HFP_RETRIEVE_INDICATORS: { 699 int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment); 700 int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 701 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments); 702 if (next_segment < num_segments){ 703 // prepare sending of next segment 704 hfp_connection->send_ag_indicators_segment = next_segment; 705 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment); 706 } else { 707 // done, go to next state 708 hfp_connection->send_ag_indicators_segment = 0; 709 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 710 } 711 return 1; 712 } 713 default: 714 break; 715 } 716 return 0; 717 } 718 719 static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 720 int sent = codecs_exchange_state_machine(hfp_connection); 721 if (sent) return 1; 722 723 switch(hfp_connection->command){ 724 case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION: 725 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition); 726 hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition); 727 return 1; 728 case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 729 if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){ 730 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition); 731 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 732 hfp_ag_setup_audio_connection(hfp_connection); 733 } else { 734 hfp_ag_send_error(hfp_connection->rfcomm_cid); 735 } 736 return 1; 737 case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING: 738 hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid); 739 return 1; 740 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 741 hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator); 742 return 1; 743 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 744 if (hfp_connection->network_operator.format != 0){ 745 hfp_ag_send_error(hfp_connection->rfcomm_cid); 746 } else { 747 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 748 } 749 return 1; 750 case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 751 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 752 return 1; 753 case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 754 if (hfp_connection->extended_audio_gateway_error){ 755 hfp_connection->extended_audio_gateway_error = 0; 756 hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value); 757 return 1; 758 } 759 break; 760 case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 761 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 762 return 1; 763 default: 764 break; 765 } 766 return 0; 767 } 768 769 static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){ 770 if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 771 (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 772 773 774 if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){ 775 hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 776 hfp_connection->release_audio_connection = 0; 777 gap_disconnect(hfp_connection->sco_handle); 778 return 1; 779 } 780 781 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 782 783 // run codecs exchange 784 int sent = codecs_exchange_state_machine(hfp_connection); 785 if (sent) return 1; 786 787 if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 788 if (hfp_connection->establish_audio_connection){ 789 hfp_connection->state = HFP_W4_SCO_CONNECTED; 790 hfp_connection->establish_audio_connection = 0; 791 hfp_setup_synchronous_connection(hfp_connection); 792 return 1; 793 } 794 return 0; 795 } 796 797 static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){ 798 btstack_linked_list_iterator_t it; 799 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 800 801 while (btstack_linked_list_iterator_has_next(&it)){ 802 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 803 if ( & hfp_connection->hfp_timeout == ts) { 804 return hfp_connection; 805 } 806 } 807 return NULL; 808 } 809 810 static void hfp_timeout_handler(btstack_timer_source_t * timer){ 811 hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer); 812 if (!hfp_connection) return; 813 814 log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle); 815 hfp_connection->ag_ring = 1; 816 hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 817 818 btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout 819 btstack_run_loop_add_timer(& hfp_connection->hfp_timeout); 820 821 hfp_ag_run_for_context(hfp_connection); 822 } 823 824 static void hfp_timeout_start(hfp_connection_t * hfp_connection){ 825 btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout); 826 btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler); 827 btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout 828 btstack_run_loop_add_timer(& hfp_connection->hfp_timeout); 829 } 830 831 static void hfp_timeout_stop(hfp_connection_t * hfp_connection){ 832 log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle); 833 btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout); 834 } 835 836 // 837 // transitition implementations for hfp_ag_call_state_machine 838 // 839 840 static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){ 841 if (use_in_band_tone()){ 842 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING; 843 hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 844 } else { 845 hfp_timeout_start(hfp_connection); 846 hfp_connection->ag_ring = 1; 847 hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 848 hfp_connection->call_state = HFP_CALL_RINGING; 849 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG); 850 } 851 } 852 853 static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){ 854 hfp_connection->ag_ring = 0; 855 hfp_connection->ag_send_clip = 0; 856 hfp_timeout_stop(hfp_connection); 857 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGINIG); 858 } 859 860 static void hfp_ag_trigger_incoming_call(void){ 861 int indicator_index = get_ag_indicator_index_for_name("callsetup"); 862 if (indicator_index < 0) return; 863 864 btstack_linked_list_iterator_t it; 865 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 866 while (btstack_linked_list_iterator_has_next(&it)){ 867 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 868 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 869 hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 870 if (hfp_connection->call_state == HFP_CALL_IDLE){ 871 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 872 hfp_ag_hf_start_ringing(hfp_connection); 873 } 874 if (hfp_connection->call_state == HFP_CALL_ACTIVE){ 875 hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING; 876 } 877 hfp_ag_run_for_context(hfp_connection); 878 } 879 } 880 881 static void hfp_ag_transfer_callsetup_state(void){ 882 int indicator_index = get_ag_indicator_index_for_name("callsetup"); 883 if (indicator_index < 0) return; 884 885 btstack_linked_list_iterator_t it; 886 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 887 while (btstack_linked_list_iterator_has_next(&it)){ 888 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 889 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 890 hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 891 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 892 hfp_ag_run_for_context(hfp_connection); 893 } 894 } 895 896 static void hfp_ag_transfer_call_state(void){ 897 int indicator_index = get_ag_indicator_index_for_name("call"); 898 if (indicator_index < 0) return; 899 900 btstack_linked_list_iterator_t it; 901 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 902 while (btstack_linked_list_iterator_has_next(&it)){ 903 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 904 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 905 hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 906 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 907 hfp_ag_run_for_context(hfp_connection); 908 } 909 } 910 911 static void hfp_ag_transfer_callheld_state(void){ 912 int indicator_index = get_ag_indicator_index_for_name("callheld"); 913 if (indicator_index < 0) return; 914 915 btstack_linked_list_iterator_t it; 916 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 917 while (btstack_linked_list_iterator_has_next(&it)){ 918 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 919 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 920 hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 921 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 922 hfp_ag_run_for_context(hfp_connection); 923 } 924 } 925 926 static void hfp_ag_hf_accept_call(hfp_connection_t * source){ 927 928 int call_indicator_index = get_ag_indicator_index_for_name("call"); 929 int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 930 931 btstack_linked_list_iterator_t it; 932 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 933 while (btstack_linked_list_iterator_has_next(&it)){ 934 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 935 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 936 if ((hfp_connection->call_state != HFP_CALL_RINGING) && 937 (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 938 939 hfp_ag_hf_stop_ringing(hfp_connection); 940 if (hfp_connection == source){ 941 942 if (use_in_band_tone()){ 943 hfp_connection->call_state = HFP_CALL_ACTIVE; 944 } else { 945 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 946 hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 947 } 948 949 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 950 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 951 952 } else { 953 hfp_connection->call_state = HFP_CALL_IDLE; 954 } 955 hfp_ag_run_for_context(hfp_connection); 956 } 957 } 958 959 static void hfp_ag_ag_accept_call(void){ 960 961 int call_indicator_index = get_ag_indicator_index_for_name("call"); 962 int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 963 964 btstack_linked_list_iterator_t it; 965 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 966 while (btstack_linked_list_iterator_has_next(&it)){ 967 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 968 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 969 if (hfp_connection->call_state != HFP_CALL_RINGING) continue; 970 971 hfp_ag_hf_stop_ringing(hfp_connection); 972 hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION; 973 974 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 975 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 976 977 hfp_ag_run_for_context(hfp_connection); 978 break; // only single 979 } 980 } 981 982 static void hfp_ag_trigger_reject_call(void){ 983 int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 984 btstack_linked_list_iterator_t it; 985 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 986 while (btstack_linked_list_iterator_has_next(&it)){ 987 hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 988 if (connection->local_role != HFP_ROLE_AG) continue; 989 if ((connection->call_state != HFP_CALL_RINGING) && 990 (connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 991 hfp_ag_hf_stop_ringing(connection); 992 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 993 connection->call_state = HFP_CALL_IDLE; 994 hfp_ag_run_for_context(connection); 995 } 996 } 997 998 static void hfp_ag_emit_simple_event(uint8_t event_subtype){ 999 uint8_t event[3]; 1000 event[0] = HCI_EVENT_HFP_META; 1001 event[1] = sizeof(event) - 2; 1002 event[2] = event_subtype; 1003 if (!hfp_ag_callback) return; 1004 (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1005 } 1006 1007 static void hfp_ag_trigger_terminate_call(void){ 1008 int call_indicator_index = get_ag_indicator_index_for_name("call"); 1009 1010 btstack_linked_list_iterator_t it; 1011 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1012 while (btstack_linked_list_iterator_has_next(&it)){ 1013 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1014 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1015 hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1016 if (hfp_connection->call_state == HFP_CALL_IDLE) continue; 1017 hfp_connection->call_state = HFP_CALL_IDLE; 1018 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1019 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1020 hfp_connection->release_audio_connection = 1; 1021 } 1022 hfp_ag_run_for_context(hfp_connection); 1023 } 1024 hfp_ag_emit_simple_event(HFP_SUBEVENT_CALL_TERMINATED); 1025 } 1026 1027 static void hfp_ag_set_callsetup_indicator(void){ 1028 hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup"); 1029 if (!indicator){ 1030 log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing"); 1031 return; 1032 }; 1033 indicator->status = hfp_gsm_callsetup_status(); 1034 } 1035 1036 static void hfp_ag_set_callheld_indicator(void){ 1037 hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld"); 1038 if (!indicator){ 1039 log_error("hfp_ag_set_callheld_state: callheld indicator is missing"); 1040 return; 1041 }; 1042 indicator->status = hfp_gsm_callheld_status(); 1043 } 1044 1045 static void hfp_ag_set_call_indicator(void){ 1046 hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call"); 1047 if (!indicator){ 1048 log_error("hfp_ag_set_call_state: call indicator is missing"); 1049 return; 1050 }; 1051 indicator->status = hfp_gsm_call_status(); 1052 } 1053 1054 static void hfp_ag_stop_ringing(void){ 1055 btstack_linked_list_iterator_t it; 1056 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1057 while (btstack_linked_list_iterator_has_next(&it)){ 1058 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1059 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1060 if ((hfp_connection->call_state != HFP_CALL_RINGING) && 1061 (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1062 hfp_ag_hf_stop_ringing(hfp_connection); 1063 } 1064 } 1065 1066 static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){ 1067 btstack_linked_list_iterator_t it; 1068 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1069 while (btstack_linked_list_iterator_has_next(&it)){ 1070 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1071 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1072 if (hfp_connection->call_state == call_state) return hfp_connection; 1073 } 1074 return NULL; 1075 } 1076 1077 static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){ 1078 btstack_linked_list_iterator_t it; 1079 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1080 while (btstack_linked_list_iterator_has_next(&it)){ 1081 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1082 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1083 hfp_connection->send_response_and_hold_status = state + 1; 1084 } 1085 } 1086 1087 static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 1088 int indicator_index; 1089 switch (hfp_connection->call_state){ 1090 case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1091 if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1092 // we got event: audio hfp_connection established 1093 hfp_timeout_start(hfp_connection); 1094 hfp_connection->ag_ring = 1; 1095 hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 1096 hfp_connection->call_state = HFP_CALL_RINGING; 1097 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG); 1098 break; 1099 case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE: 1100 if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1101 // we got event: audio hfp_connection established 1102 hfp_connection->call_state = HFP_CALL_ACTIVE; 1103 break; 1104 case HFP_CALL_W2_SEND_CALL_WAITING: 1105 hfp_connection->call_state = HFP_CALL_W4_CHLD; 1106 hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1107 indicator_index = get_ag_indicator_index_for_name("callsetup"); 1108 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1109 break; 1110 default: 1111 break; 1112 } 1113 return 0; 1114 } 1115 1116 // functions extracted from hfp_ag_call_sm below 1117 static void hfp_ag_handle_reject_incoming_call(void){ 1118 hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1119 if (!hfp_connection){ 1120 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1121 return; 1122 } 1123 1124 hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED); 1125 hfp_connection->call_state = HFP_CALL_IDLE; 1126 hfp_connection->send_error = 1; 1127 hfp_ag_run_for_context(hfp_connection); 1128 } 1129 1130 // hfp_connection is used to identify originating HF 1131 static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){ 1132 int indicator_index; 1133 int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1134 int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1135 int call_indicator_index = get_ag_indicator_index_for_name("call"); 1136 1137 //printf("hfp_ag_call_sm event %d \n", event); 1138 switch (event){ 1139 case HFP_AG_INCOMING_CALL: 1140 switch (hfp_gsm_call_status()){ 1141 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1142 switch (hfp_gsm_callsetup_status()){ 1143 case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1144 hfp_gsm_handle_event(HFP_AG_INCOMING_CALL); 1145 hfp_ag_set_callsetup_indicator(); 1146 hfp_ag_trigger_incoming_call(); 1147 log_info("AG rings"); 1148 break; 1149 default: 1150 break; 1151 } 1152 break; 1153 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1154 switch (hfp_gsm_callsetup_status()){ 1155 case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1156 hfp_gsm_handle_event(HFP_AG_INCOMING_CALL); 1157 hfp_ag_set_callsetup_indicator(); 1158 hfp_ag_trigger_incoming_call(); 1159 log_info("AG call waiting"); 1160 break; 1161 default: 1162 break; 1163 } 1164 break; 1165 } 1166 break; 1167 case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1168 switch (hfp_gsm_call_status()){ 1169 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1170 switch (hfp_gsm_callsetup_status()){ 1171 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1172 hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG); 1173 hfp_ag_set_call_indicator(); 1174 hfp_ag_set_callsetup_indicator(); 1175 hfp_ag_ag_accept_call(); 1176 log_info("AG answers call, accept call by GSM"); 1177 break; 1178 default: 1179 break; 1180 } 1181 break; 1182 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1183 switch (hfp_gsm_callsetup_status()){ 1184 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1185 log_info("AG: current call is placed on hold, incoming call gets active"); 1186 hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG); 1187 hfp_ag_set_callsetup_indicator(); 1188 hfp_ag_set_callheld_indicator(); 1189 hfp_ag_transfer_callsetup_state(); 1190 hfp_ag_transfer_callheld_state(); 1191 break; 1192 default: 1193 break; 1194 } 1195 break; 1196 } 1197 break; 1198 1199 case HFP_AG_HELD_CALL_JOINED_BY_AG: 1200 switch (hfp_gsm_call_status()){ 1201 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1202 switch (hfp_gsm_callheld_status()){ 1203 case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 1204 log_info("AG: joining held call with active call"); 1205 hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG); 1206 hfp_ag_set_callheld_indicator(); 1207 hfp_ag_transfer_callheld_state(); 1208 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1209 break; 1210 default: 1211 break; 1212 } 1213 break; 1214 default: 1215 break; 1216 } 1217 break; 1218 1219 case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1220 switch (hfp_gsm_call_status()){ 1221 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1222 switch (hfp_gsm_callsetup_status()){ 1223 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1224 hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF); 1225 hfp_ag_set_callsetup_indicator(); 1226 hfp_ag_set_call_indicator(); 1227 hfp_ag_hf_accept_call(hfp_connection); 1228 hfp_connection->ok_pending = 1; 1229 log_info("HF answers call, accept call by GSM"); 1230 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1231 break; 1232 default: 1233 break; 1234 } 1235 break; 1236 default: 1237 break; 1238 } 1239 break; 1240 1241 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1242 switch (hfp_gsm_call_status()){ 1243 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1244 switch (hfp_gsm_callsetup_status()){ 1245 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1246 hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG); 1247 hfp_ag_response_and_hold_active = 1; 1248 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1249 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1250 // as with regualr call 1251 hfp_ag_set_call_indicator(); 1252 hfp_ag_set_callsetup_indicator(); 1253 hfp_ag_ag_accept_call(); 1254 log_info("AG response and hold - hold by AG"); 1255 break; 1256 default: 1257 break; 1258 } 1259 break; 1260 default: 1261 break; 1262 } 1263 break; 1264 1265 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1266 switch (hfp_gsm_call_status()){ 1267 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1268 switch (hfp_gsm_callsetup_status()){ 1269 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1270 hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF); 1271 hfp_ag_response_and_hold_active = 1; 1272 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1273 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1274 // as with regualr call 1275 hfp_ag_set_call_indicator(); 1276 hfp_ag_set_callsetup_indicator(); 1277 hfp_ag_hf_accept_call(hfp_connection); 1278 log_info("AG response and hold - hold by HF"); 1279 break; 1280 default: 1281 break; 1282 } 1283 break; 1284 default: 1285 break; 1286 } 1287 break; 1288 1289 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1290 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1291 if (!hfp_ag_response_and_hold_active) break; 1292 if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1293 hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG); 1294 hfp_ag_response_and_hold_active = 0; 1295 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1296 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1297 log_info("Held Call accepted and active"); 1298 break; 1299 1300 case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1301 case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1302 if (!hfp_ag_response_and_hold_active) break; 1303 if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1304 hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG); 1305 hfp_ag_response_and_hold_active = 0; 1306 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1307 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1308 // from terminate by ag 1309 hfp_ag_set_call_indicator(); 1310 hfp_ag_trigger_terminate_call(); 1311 break; 1312 1313 case HFP_AG_TERMINATE_CALL_BY_HF: 1314 switch (hfp_gsm_call_status()){ 1315 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1316 switch (hfp_gsm_callsetup_status()){ 1317 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1318 hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1319 hfp_ag_set_callsetup_indicator(); 1320 hfp_ag_transfer_callsetup_state(); 1321 hfp_ag_trigger_reject_call(); 1322 log_info("HF Rejected Incoming call, AG terminate call"); 1323 break; 1324 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1325 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1326 hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1327 hfp_ag_set_callsetup_indicator(); 1328 hfp_ag_transfer_callsetup_state(); 1329 log_info("AG terminate outgoing call process"); 1330 break; 1331 default: 1332 break; 1333 } 1334 break; 1335 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1336 hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1337 hfp_ag_set_call_indicator(); 1338 hfp_ag_transfer_call_state(); 1339 hfp_connection->call_state = HFP_CALL_IDLE; 1340 log_info("AG terminate call"); 1341 break; 1342 } 1343 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1344 break; 1345 1346 case HFP_AG_TERMINATE_CALL_BY_AG: 1347 switch (hfp_gsm_call_status()){ 1348 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1349 switch (hfp_gsm_callsetup_status()){ 1350 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1351 hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG); 1352 hfp_ag_set_callsetup_indicator(); 1353 hfp_ag_trigger_reject_call(); 1354 log_info("AG Rejected Incoming call, AG terminate call"); 1355 break; 1356 default: 1357 break; 1358 } 1359 break; 1360 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1361 hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG); 1362 hfp_ag_set_callsetup_indicator(); 1363 hfp_ag_set_call_indicator(); 1364 hfp_ag_trigger_terminate_call(); 1365 log_info("AG terminate call"); 1366 break; 1367 default: 1368 break; 1369 } 1370 break; 1371 case HFP_AG_CALL_DROPPED: 1372 switch (hfp_gsm_call_status()){ 1373 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1374 switch (hfp_gsm_callsetup_status()){ 1375 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1376 hfp_ag_stop_ringing(); 1377 log_info("Incoming call interrupted"); 1378 break; 1379 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1380 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1381 log_info("Outgoing call interrupted\n"); 1382 log_info("AG notify call dropped\n"); 1383 break; 1384 default: 1385 break; 1386 } 1387 hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1388 hfp_ag_set_callsetup_indicator(); 1389 hfp_ag_transfer_callsetup_state(); 1390 hfp_ag_trigger_terminate_call(); 1391 break; 1392 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1393 if (hfp_ag_response_and_hold_active) { 1394 hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1395 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1396 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1397 } 1398 hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1399 hfp_ag_set_callsetup_indicator(); 1400 hfp_ag_set_call_indicator(); 1401 hfp_ag_trigger_terminate_call(); 1402 log_info("AG notify call dropped"); 1403 break; 1404 default: 1405 break; 1406 } 1407 break; 1408 1409 case HFP_AG_OUTGOING_CALL_INITIATED: 1410 // directly reject call if number of free slots is exceeded 1411 if (!hfp_gsm_call_possible()){ 1412 hfp_connection->send_error = 1; 1413 hfp_ag_run_for_context(hfp_connection); 1414 break; 1415 } 1416 hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]); 1417 1418 hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1419 1420 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]); 1421 break; 1422 1423 case HFP_AG_OUTGOING_REDIAL_INITIATED:{ 1424 // directly reject call if number of free slots is exceeded 1425 if (!hfp_gsm_call_possible()){ 1426 hfp_connection->send_error = 1; 1427 hfp_ag_run_for_context(hfp_connection); 1428 break; 1429 } 1430 1431 hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED); 1432 hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1433 1434 log_info("Redial last number"); 1435 char * last_dialed_number = hfp_gsm_last_dialed_number(); 1436 1437 if (strlen(last_dialed_number) > 0){ 1438 log_info("Last number exists: accept call"); 1439 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number); 1440 } else { 1441 log_info("log_infoLast number missing: reject call"); 1442 hfp_ag_handle_reject_incoming_call(); 1443 } 1444 break; 1445 } 1446 case HFP_AG_OUTGOING_CALL_REJECTED: 1447 hfp_ag_handle_reject_incoming_call(); 1448 break; 1449 1450 case HFP_AG_OUTGOING_CALL_ACCEPTED:{ 1451 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1452 if (!hfp_connection){ 1453 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1454 break; 1455 } 1456 1457 hfp_connection->ok_pending = 1; 1458 hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING; 1459 1460 // trigger callsetup to be 1461 int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 1462 hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED); 1463 1464 hfp_ag_set_callsetup_indicator(); 1465 indicator_index = get_ag_indicator_index_for_name("callsetup"); 1466 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1467 1468 // put current call on hold if active 1469 if (put_call_on_hold){ 1470 log_info("AG putting current call on hold for new outgoing calllog_info"); 1471 hfp_ag_set_callheld_indicator(); 1472 indicator_index = get_ag_indicator_index_for_name("callheld"); 1473 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 1474 } 1475 1476 // start audio if needed 1477 hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1478 break; 1479 } 1480 case HFP_AG_OUTGOING_CALL_RINGING: 1481 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1482 if (!hfp_connection){ 1483 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state"); 1484 break; 1485 } 1486 1487 hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING); 1488 hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1489 hfp_ag_set_callsetup_indicator(); 1490 hfp_ag_transfer_callsetup_state(); 1491 break; 1492 1493 case HFP_AG_OUTGOING_CALL_ESTABLISHED:{ 1494 // get outgoing call 1495 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1496 if (!hfp_connection){ 1497 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1498 } 1499 if (!hfp_connection){ 1500 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection"); 1501 break; 1502 } 1503 1504 int CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS = hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS; 1505 hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED); 1506 hfp_connection->call_state = HFP_CALL_ACTIVE; 1507 hfp_ag_set_callsetup_indicator(); 1508 hfp_ag_set_call_indicator(); 1509 hfp_ag_transfer_call_state(); 1510 hfp_ag_transfer_callsetup_state(); 1511 if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){ 1512 hfp_ag_set_callheld_indicator(); 1513 hfp_ag_transfer_callheld_state(); 1514 } 1515 break; 1516 } 1517 1518 case HFP_AG_CALL_HOLD_USER_BUSY: 1519 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY); 1520 hfp_ag_set_callsetup_indicator(); 1521 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1522 hfp_connection->call_state = HFP_CALL_ACTIVE; 1523 log_info("AG: Call Waiting, User Busy"); 1524 break; 1525 1526 case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1527 int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1528 int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1529 1530 // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1531 if (call_held || call_setup_in_progress){ 1532 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index); 1533 1534 } 1535 1536 if (call_setup_in_progress){ 1537 log_info("AG: Call Dropped, Accept new call"); 1538 hfp_ag_set_callsetup_indicator(); 1539 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1540 } else { 1541 log_info("AG: Call Dropped, Resume held call"); 1542 } 1543 if (call_held){ 1544 hfp_ag_set_callheld_indicator(); 1545 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1546 } 1547 1548 hfp_connection->call_state = HFP_CALL_ACTIVE; 1549 break; 1550 } 1551 1552 case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1553 // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1554 // only update if callsetup changed 1555 int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1556 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index); 1557 1558 if (call_setup_in_progress){ 1559 log_info("AG: Call on Hold, Accept new call"); 1560 hfp_ag_set_callsetup_indicator(); 1561 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1562 } else { 1563 log_info("AG: Swap calls"); 1564 } 1565 1566 hfp_ag_set_callheld_indicator(); 1567 // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1568 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1569 hfp_connection->call_state = HFP_CALL_ACTIVE; 1570 break; 1571 } 1572 1573 case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 1574 // Adds a held call to the conversation. 1575 if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 1576 log_info("AG: Join 3-way-call"); 1577 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL); 1578 hfp_ag_set_callheld_indicator(); 1579 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1580 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1581 } 1582 hfp_connection->call_state = HFP_CALL_ACTIVE; 1583 break; 1584 case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 1585 // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 1586 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS); 1587 log_info("AG: Transfer call -> Connect two calls and disconnect"); 1588 hfp_ag_set_call_indicator(); 1589 hfp_ag_set_callheld_indicator(); 1590 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1591 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1592 hfp_connection->call_state = HFP_CALL_IDLE; 1593 break; 1594 1595 default: 1596 break; 1597 } 1598 1599 1600 } 1601 1602 1603 static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 1604 hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 1605 if (!active_call) return; 1606 1607 int idx = active_call->index; 1608 hfp_enhanced_call_dir_t dir = active_call->direction; 1609 hfp_enhanced_call_status_t status = active_call->enhanced_status; 1610 hfp_enhanced_call_mode_t mode = active_call->mode; 1611 hfp_enhanced_call_mpty_t mpty = active_call->mpty; 1612 uint8_t type = active_call->clip_type; 1613 char * number = active_call->clip_number; 1614 1615 char buffer[100]; 1616 // TODO: check length of a buffer, to fit the MTU 1617 int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 1618 if (number){ 1619 offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type); 1620 } 1621 snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 1622 log_info("hfp_ag_send_current_call_status 000 index %d, dir %d, status %d, mode %d, mpty %d, type %d, number %s", idx, dir, status, 1623 mode, mpty, type, number); 1624 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 1625 } 1626 1627 1628 // sends pending command, returns if command was sent 1629 static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){ 1630 1631 if (hfp_connection->send_status_of_current_calls){ 1632 hfp_connection->ok_pending = 0; 1633 if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 1634 hfp_connection->next_call_index++; 1635 hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 1636 return 1; 1637 } else { 1638 // TODO: this code should be removed. check a) before setting send_status_of_current_calls, or b) right before hfp_ag_send_call_status above 1639 hfp_connection->next_call_index = 0; 1640 hfp_connection->ok_pending = 1; 1641 hfp_connection->send_status_of_current_calls = 0; 1642 } 1643 } 1644 1645 if (hfp_connection->ag_notify_incoming_call_waiting){ 1646 hfp_connection->ag_notify_incoming_call_waiting = 0; 1647 hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1648 return 1; 1649 } 1650 1651 if (hfp_connection->command == HFP_CMD_UNKNOWN){ 1652 hfp_connection->ok_pending = 0; 1653 hfp_connection->send_error = 0; 1654 hfp_connection->command = HFP_CMD_NONE; 1655 hfp_ag_send_error(hfp_connection->rfcomm_cid); 1656 return 1; 1657 } 1658 1659 if (hfp_connection->send_error){ 1660 hfp_connection->send_error = 0; 1661 hfp_connection->command = HFP_CMD_NONE; 1662 hfp_ag_send_error(hfp_connection->rfcomm_cid); 1663 return 1; 1664 } 1665 1666 // note: before update AG indicators and ok_pending 1667 if (hfp_connection->send_response_and_hold_status){ 1668 int status = hfp_connection->send_response_and_hold_status - 1; 1669 hfp_connection->send_response_and_hold_status = 0; 1670 hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status); 1671 return 1; 1672 } 1673 1674 if (hfp_connection->ok_pending){ 1675 hfp_connection->ok_pending = 0; 1676 hfp_connection->command = HFP_CMD_NONE; 1677 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1678 return 1; 1679 } 1680 1681 // update AG indicators 1682 if (hfp_connection->ag_indicators_status_update_bitmap){ 1683 int i; 1684 for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 1685 if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 1686 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 1687 if (!hfp_connection->enable_status_update_for_ag_indicators) { 1688 log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name); 1689 break; 1690 } 1691 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 1692 return 1; 1693 } 1694 } 1695 } 1696 1697 if (hfp_connection->ag_ring){ 1698 hfp_connection->ag_ring = 0; 1699 hfp_connection->command = HFP_CMD_NONE; 1700 hfp_ag_send_ring(hfp_connection->rfcomm_cid); 1701 return 1; 1702 } 1703 1704 if (hfp_connection->ag_send_clip){ 1705 hfp_connection->ag_send_clip = 0; 1706 hfp_connection->command = HFP_CMD_NONE; 1707 hfp_ag_send_clip(hfp_connection->rfcomm_cid); 1708 return 1; 1709 } 1710 1711 if (hfp_connection->send_phone_number_for_voice_tag){ 1712 hfp_connection->send_phone_number_for_voice_tag = 0; 1713 hfp_connection->command = HFP_CMD_NONE; 1714 hfp_connection->ok_pending = 1; 1715 hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 1716 return 1; 1717 } 1718 1719 if (hfp_connection->send_subscriber_number){ 1720 if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){ 1721 hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 1722 hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 1723 } else { 1724 hfp_connection->send_subscriber_number = 0; 1725 hfp_connection->next_subscriber_number_to_send = 0; 1726 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1727 } 1728 hfp_connection->command = HFP_CMD_NONE; 1729 return 1; 1730 } 1731 1732 if (hfp_connection->send_microphone_gain){ 1733 hfp_connection->send_microphone_gain = 0; 1734 hfp_connection->command = HFP_CMD_NONE; 1735 hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 1736 return 1; 1737 } 1738 1739 if (hfp_connection->send_speaker_gain){ 1740 hfp_connection->send_speaker_gain = 0; 1741 hfp_connection->command = HFP_CMD_NONE; 1742 hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 1743 return 1; 1744 } 1745 1746 if (hfp_connection->send_ag_status_indicators){ 1747 hfp_connection->send_ag_status_indicators = 0; 1748 hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 1749 return 1; 1750 } 1751 1752 return 0; 1753 } 1754 1755 static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){ 1756 1757 if (!hfp_connection) return; 1758 1759 if (!hfp_connection->rfcomm_cid) return; 1760 1761 if (hfp_connection->local_role != HFP_ROLE_AG) { 1762 log_info("HFP AG%p, wrong role %u", hfp_connection, hfp_connection->local_role); 1763 return; 1764 } 1765 1766 if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 1767 log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid); 1768 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1769 return; 1770 } 1771 1772 int cmd_sent = hfp_ag_send_commands(hfp_connection); 1773 1774 // trigger codec exchange if requested by app 1775 if (hfp_connection->trigger_codec_exchange){ 1776 log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state); 1777 } 1778 1779 if (hfp_connection->trigger_codec_exchange && (hfp_connection->command == HFP_CMD_NONE)){ 1780 switch (hfp_connection->codecs_state){ 1781 case HFP_CODECS_IDLE: 1782 case HFP_CODECS_RECEIVED_LIST: 1783 case HFP_CODECS_AG_RESEND_COMMON_CODEC: 1784 case HFP_CODECS_ERROR: 1785 hfp_connection->trigger_codec_exchange = 0; 1786 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 1787 break; 1788 default: 1789 break; 1790 } 1791 } 1792 1793 if (!cmd_sent){ 1794 cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection); 1795 } 1796 1797 if (!cmd_sent){ 1798 cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 1799 } 1800 1801 if (!cmd_sent){ 1802 cmd_sent = call_setup_state_machine(hfp_connection); 1803 } 1804 1805 if (!cmd_sent){ 1806 cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection); 1807 } 1808 1809 if ((hfp_connection->command == HFP_CMD_NONE) && !cmd_sent){ 1810 // log_info("hfp_connection->command == HFP_CMD_NONE"); 1811 switch(hfp_connection->state){ 1812 case HFP_W2_DISCONNECT_RFCOMM: 1813 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 1814 rfcomm_disconnect(hfp_connection->rfcomm_cid); 1815 break; 1816 default: 1817 break; 1818 } 1819 } 1820 1821 if (cmd_sent){ 1822 hfp_connection->command = HFP_CMD_NONE; 1823 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1824 } 1825 } 1826 1827 static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){ 1828 int i; 1829 for (i=0;i< hfp_generic_status_indicators_nr;i++){ 1830 hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i]; 1831 if (indicator->uuid == number){ 1832 return indicator; 1833 } 1834 } 1835 return NULL; 1836 } 1837 1838 static int hfp_parser_is_end_of_line(uint8_t byte){ 1839 return (byte == '\n') || (byte == '\r'); 1840 } 1841 1842 static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1843 UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 1844 1845 // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1846 if (size < 1) return; 1847 1848 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1849 if (!hfp_connection) return; 1850 1851 hfp_log_rfcomm_message("HFP_AG_RX", packet, size); 1852 1853 // process messages byte-wise 1854 int pos; 1855 for (pos = 0; pos < size ; pos++){ 1856 hfp_parse(hfp_connection, packet[pos], 0); 1857 1858 // parse until end of line 1859 if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1860 1861 hfp_generic_status_indicator_t * indicator; 1862 switch(hfp_connection->command){ 1863 case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 1864 if (hfp_ag_response_and_hold_active){ 1865 hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 1866 } 1867 hfp_connection->ok_pending = 1; 1868 break; 1869 case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 1870 switch(hfp_connection->ag_response_and_hold_action){ 1871 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 1872 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 1873 break; 1874 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 1875 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 1876 break; 1877 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 1878 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 1879 break; 1880 default: 1881 break; 1882 } 1883 hfp_connection->ok_pending = 1; 1884 break; 1885 case HFP_CMD_HF_INDICATOR_STATUS: 1886 hfp_connection->command = HFP_CMD_NONE; 1887 // find indicator by assigned number 1888 indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index); 1889 if (!indicator){ 1890 hfp_connection->send_error = 1; 1891 break; 1892 } 1893 switch (indicator->uuid){ 1894 case 1: // enhanced security 1895 if (hfp_connection->parser_indicator_value > 1) { 1896 hfp_connection->send_error = 1; 1897 return; 1898 } 1899 log_info("HF Indicator 'enhanced security' set to %u", hfp_connection->parser_indicator_value); 1900 break; 1901 case 2: // battery level 1902 if (hfp_connection->parser_indicator_value > 100){ 1903 hfp_connection->send_error = 1; 1904 return; 1905 } 1906 log_info("HF Indicator 'battery' set to %u", hfp_connection->parser_indicator_value); 1907 break; 1908 default: 1909 log_info("HF Indicator unknown set to %u", hfp_connection->parser_indicator_value); 1910 break; 1911 } 1912 hfp_connection->ok_pending = 1; 1913 break; 1914 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1915 // expected by SLC state machine 1916 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 1917 hfp_connection->send_ag_indicators_segment = 0; 1918 hfp_connection->send_ag_status_indicators = 1; 1919 break; 1920 case HFP_CMD_LIST_CURRENT_CALLS: 1921 hfp_connection->command = HFP_CMD_NONE; 1922 hfp_connection->next_call_index = 0; 1923 hfp_connection->send_status_of_current_calls = 1; 1924 break; 1925 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1926 if (subscriber_numbers_count == 0){ 1927 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1928 break; 1929 } 1930 hfp_connection->next_subscriber_number_to_send = 0; 1931 hfp_connection->send_subscriber_number = 1; 1932 break; 1933 case HFP_CMD_TRANSMIT_DTMF_CODES: 1934 { 1935 hfp_connection->command = HFP_CMD_NONE; 1936 char buffer[2]; 1937 buffer[0] = (char) hfp_connection->ag_dtmf_code; 1938 buffer[1] = 0; 1939 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer); 1940 break; 1941 } 1942 case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 1943 hfp_connection->command = HFP_CMD_NONE; 1944 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 1945 break; 1946 case HFP_CMD_TURN_OFF_EC_AND_NR: 1947 hfp_connection->command = HFP_CMD_NONE; 1948 if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 1949 hfp_connection->ok_pending = 1; 1950 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction); 1951 log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction); 1952 } else { 1953 hfp_connection->send_error = 1; 1954 } 1955 break; 1956 case HFP_CMD_CALL_ANSWERED: 1957 hfp_connection->command = HFP_CMD_NONE; 1958 log_info("HFP: ATA"); 1959 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 1960 break; 1961 case HFP_CMD_HANG_UP_CALL: 1962 hfp_connection->command = HFP_CMD_NONE; 1963 hfp_connection->ok_pending = 1; 1964 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 1965 break; 1966 case HFP_CMD_CALL_HOLD: { 1967 hfp_connection->command = HFP_CMD_NONE; 1968 hfp_connection->ok_pending = 1; 1969 1970 switch (hfp_connection->ag_call_hold_action){ 1971 case 0: 1972 // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 1973 hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 1974 break; 1975 case 1: 1976 // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1977 // Where both a held and a waiting call exist, the above procedures shall apply to the 1978 // waiting call (i.e., not to the held call) in conflicting situation. 1979 hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1980 break; 1981 case 2: 1982 // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1983 // Where both a held and a waiting call exist, the above procedures shall apply to the 1984 // waiting call (i.e., not to the held call) in conflicting situation. 1985 hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1986 break; 1987 case 3: 1988 // Adds a held call to the conversation. 1989 hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 1990 break; 1991 case 4: 1992 // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 1993 hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 1994 break; 1995 default: 1996 break; 1997 } 1998 hfp_connection->call_index = 0; 1999 break; 2000 } 2001 case HFP_CMD_CALL_PHONE_NUMBER: 2002 hfp_connection->command = HFP_CMD_NONE; 2003 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection); 2004 break; 2005 case HFP_CMD_REDIAL_LAST_NUMBER: 2006 hfp_connection->command = HFP_CMD_NONE; 2007 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 2008 break; 2009 case HFP_CMD_ENABLE_CLIP: 2010 hfp_connection->command = HFP_CMD_NONE; 2011 log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 2012 hfp_connection->ok_pending = 1; 2013 break; 2014 case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 2015 hfp_connection->command = HFP_CMD_NONE; 2016 log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 2017 hfp_connection->ok_pending = 1; 2018 break; 2019 case HFP_CMD_SET_SPEAKER_GAIN: 2020 hfp_connection->command = HFP_CMD_NONE; 2021 hfp_connection->ok_pending = 1; 2022 log_info("HF speaker gain = %u", hfp_connection->speaker_gain); 2023 break; 2024 case HFP_CMD_SET_MICROPHONE_GAIN: 2025 hfp_connection->command = HFP_CMD_NONE; 2026 hfp_connection->ok_pending = 1; 2027 log_info("HF microphone gain = %u", hfp_connection->microphone_gain); 2028 break; 2029 default: 2030 break; 2031 } 2032 } 2033 } 2034 2035 static void hfp_run(void){ 2036 btstack_linked_list_iterator_t it; 2037 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2038 while (btstack_linked_list_iterator_has_next(&it)){ 2039 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2040 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2041 hfp_ag_run_for_context(hfp_connection); 2042 } 2043 } 2044 2045 static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2046 switch (packet_type){ 2047 case RFCOMM_DATA_PACKET: 2048 hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size); 2049 break; 2050 case HCI_EVENT_PACKET: 2051 if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 2052 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 2053 hfp_ag_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 2054 return; 2055 } 2056 hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2057 break; 2058 default: 2059 break; 2060 } 2061 2062 hfp_run(); 2063 } 2064 2065 static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2066 hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2067 2068 // allow for sco established -> ring transition 2069 if (packet_type != HCI_EVENT_PACKET) return; 2070 if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return; 2071 hfp_run(); 2072 } 2073 2074 void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){ 2075 if (codecs_nr > HFP_MAX_NUM_CODECS){ 2076 log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 2077 return; 2078 } 2079 int i; 2080 hfp_codecs_nr = codecs_nr; 2081 for (i=0; i < codecs_nr; i++){ 2082 hfp_codecs[i] = codecs[i]; 2083 } 2084 } 2085 2086 void hfp_ag_init_supported_features(uint32_t supported_features){ 2087 hfp_supported_features = supported_features; 2088 } 2089 2090 void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){ 2091 hfp_ag_indicators_nr = ag_indicators_nr; 2092 (void)memcpy(hfp_ag_indicators, ag_indicators, 2093 ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2094 } 2095 2096 void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){ 2097 if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return; 2098 hfp_generic_status_indicators_nr = hf_indicators_nr; 2099 (void)memcpy(hfp_generic_status_indicators, hf_indicators, 2100 hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2101 } 2102 2103 void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 2104 hfp_ag_call_hold_services_nr = call_hold_services_nr; 2105 (void)memcpy(hfp_ag_call_hold_services, call_hold_services, 2106 call_hold_services_nr * sizeof(char *)); 2107 } 2108 2109 2110 void hfp_ag_init(uint16_t rfcomm_channel_nr){ 2111 2112 hfp_init(); 2113 2114 hci_event_callback_registration.callback = &hci_event_packet_handler; 2115 hci_add_event_handler(&hci_event_callback_registration); 2116 2117 rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 2118 2119 // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 2120 hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler); 2121 2122 hfp_ag_response_and_hold_active = 0; 2123 subscriber_numbers = NULL; 2124 subscriber_numbers_count = 0; 2125 2126 hfp_gsm_init(); 2127 } 2128 2129 void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 2130 hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); 2131 } 2132 2133 void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ 2134 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2135 if (!hfp_connection){ 2136 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2137 return; 2138 } 2139 hfp_release_service_level_connection(hfp_connection); 2140 hfp_ag_run_for_context(hfp_connection); 2141 } 2142 2143 void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){ 2144 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2145 if (!hfp_connection){ 2146 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2147 return; 2148 } 2149 hfp_connection->extended_audio_gateway_error = 0; 2150 if (!hfp_connection->enable_extended_audio_gateway_error_report){ 2151 return; 2152 } 2153 hfp_connection->extended_audio_gateway_error = error; 2154 hfp_ag_run_for_context(hfp_connection); 2155 } 2156 2157 static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){ 2158 log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state); 2159 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 2160 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 2161 2162 hfp_connection->establish_audio_connection = 1; 2163 if (!has_codec_negotiation_feature(hfp_connection)){ 2164 log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD"); 2165 hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 2166 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 2167 // now, pick link settings 2168 hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 2169 return; 2170 } 2171 2172 hfp_connection->trigger_codec_exchange = 1; 2173 } 2174 2175 void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){ 2176 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2177 if (!hfp_connection){ 2178 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2179 return; 2180 } 2181 hfp_connection->trigger_codec_exchange = 0; 2182 hfp_connection->establish_audio_connection = 0; 2183 hfp_ag_setup_audio_connection(hfp_connection); 2184 hfp_ag_run_for_context(hfp_connection); 2185 } 2186 2187 void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){ 2188 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2189 if (!hfp_connection){ 2190 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2191 return; 2192 } 2193 hfp_release_audio_connection(hfp_connection); 2194 hfp_ag_run_for_context(hfp_connection); 2195 } 2196 2197 /** 2198 * @brief Enable in-band ring tone 2199 */ 2200 void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 2201 if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){ 2202 return; 2203 } 2204 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone); 2205 2206 btstack_linked_list_iterator_t it; 2207 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2208 while (btstack_linked_list_iterator_has_next(&it)){ 2209 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2210 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2211 hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 2212 hfp_ag_run_for_context(hfp_connection); 2213 } 2214 } 2215 2216 /** 2217 * @brief Called from GSM 2218 */ 2219 void hfp_ag_incoming_call(void){ 2220 hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 2221 } 2222 2223 /** 2224 * @brief number is stored. 2225 */ 2226 void hfp_ag_set_clip(uint8_t type, const char * number){ 2227 hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number); 2228 } 2229 2230 void hfp_ag_call_dropped(void){ 2231 hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2232 } 2233 2234 // call from AG UI 2235 void hfp_ag_answer_incoming_call(void){ 2236 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 2237 } 2238 2239 void hfp_ag_join_held_call(void){ 2240 hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 2241 } 2242 2243 void hfp_ag_terminate_call(void){ 2244 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 2245 } 2246 2247 void hfp_ag_outgoing_call_ringing(void){ 2248 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 2249 } 2250 2251 void hfp_ag_outgoing_call_established(void){ 2252 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 2253 } 2254 2255 void hfp_ag_outgoing_call_rejected(void){ 2256 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 2257 } 2258 2259 void hfp_ag_outgoing_call_accepted(void){ 2260 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 2261 } 2262 2263 void hfp_ag_hold_incoming_call(void){ 2264 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 2265 } 2266 2267 void hfp_ag_accept_held_incoming_call(void) { 2268 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 2269 } 2270 2271 void hfp_ag_reject_held_incoming_call(void){ 2272 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 2273 } 2274 2275 static void hfp_ag_set_ag_indicator(const char * name, int value){ 2276 int indicator_index = get_ag_indicator_index_for_name(name); 2277 if (indicator_index < 0) return; 2278 hfp_ag_indicators[indicator_index].status = value; 2279 2280 2281 btstack_linked_list_iterator_t it; 2282 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2283 while (btstack_linked_list_iterator_has_next(&it)){ 2284 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2285 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2286 if (! hfp_connection->ag_indicators[indicator_index].enabled) { 2287 log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value); 2288 continue; 2289 } 2290 log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value); 2291 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 2292 hfp_ag_run_for_context(hfp_connection); 2293 } 2294 } 2295 2296 void hfp_ag_set_registration_status(int status){ 2297 hfp_ag_set_ag_indicator("service", status); 2298 } 2299 2300 void hfp_ag_set_signal_strength(int strength){ 2301 hfp_ag_set_ag_indicator("signal", strength); 2302 } 2303 2304 void hfp_ag_set_roaming_status(int status){ 2305 hfp_ag_set_ag_indicator("roam", status); 2306 } 2307 2308 void hfp_ag_set_battery_level(int level){ 2309 hfp_ag_set_ag_indicator("battchg", level); 2310 } 2311 2312 void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){ 2313 if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return; 2314 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2315 if (!hfp_connection){ 2316 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2317 return; 2318 } 2319 2320 if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) { 2321 log_info("AG cannot acivate voice recognition - not supported by HF"); 2322 return; 2323 } 2324 2325 if (activate){ 2326 hfp_ag_establish_audio_connection(acl_handle); 2327 } 2328 2329 hfp_connection->ag_activate_voice_recognition = activate; 2330 hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 2331 hfp_ag_run_for_context(hfp_connection); 2332 } 2333 2334 void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 2335 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2336 if (!hfp_connection){ 2337 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2338 return; 2339 } 2340 if (hfp_connection->microphone_gain != gain){ 2341 hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN; 2342 hfp_connection->microphone_gain = gain; 2343 hfp_connection->send_microphone_gain = 1; 2344 } 2345 hfp_ag_run_for_context(hfp_connection); 2346 } 2347 2348 void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 2349 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2350 if (!hfp_connection){ 2351 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2352 return; 2353 } 2354 if (hfp_connection->speaker_gain != gain){ 2355 hfp_connection->speaker_gain = gain; 2356 hfp_connection->send_speaker_gain = 1; 2357 } 2358 hfp_ag_run_for_context(hfp_connection); 2359 } 2360 2361 void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){ 2362 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2363 if (!hfp_connection){ 2364 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2365 return; 2366 } 2367 hfp_ag_set_clip(0, number); 2368 hfp_connection->send_phone_number_for_voice_tag = 1; 2369 } 2370 2371 void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 2372 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2373 if (!hfp_connection){ 2374 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2375 return; 2376 } 2377 hfp_connection->send_error = 1; 2378 } 2379 2380 void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){ 2381 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2382 if (!hfp_connection){ 2383 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2384 return; 2385 } 2386 hfp_connection->ok_pending = 1; 2387 } 2388 2389 void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 2390 subscriber_numbers = numbers; 2391 subscriber_numbers_count = numbers_count; 2392 } 2393 2394 void hfp_ag_clear_last_dialed_number(void){ 2395 hfp_gsm_clear_last_dialed_number(); 2396 } 2397 2398 void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){ 2399 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2400 if (!hfp_connection){ 2401 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2402 return; 2403 } 2404 if (!hfp_connection->call_waiting_notification_enabled) return; 2405 2406 hfp_connection->ag_notify_incoming_call_waiting = 1; 2407 hfp_ag_run_for_context(hfp_connection); 2408 } 2409 2410