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 BLUEKITCHEN 24 * GMBH 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 <stdio.h> 50 #include <string.h> 51 52 #include "hci_cmd.h" 53 #include "btstack_run_loop.h" 54 55 #include "bluetooth_sdp.h" 56 #include "hci.h" 57 #include "btstack_memory.h" 58 #include "hci_dump.h" 59 #include "l2cap.h" 60 #include "btstack_debug.h" 61 #include "btstack_event.h" 62 #include "classic/core.h" 63 #include "classic/hfp.h" 64 #include "classic/hfp_ag.h" 65 #include "classic/hfp_gsm_model.h" 66 #include "classic/sdp_client_rfcomm.h" 67 #include "classic/sdp_server.h" 68 #include "classic/sdp_util.h" 69 70 // private prototypes 71 static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection); 72 static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection); 73 static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection); 74 static uint8_t hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection); 75 76 // public prototypes 77 hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void); 78 int get_hfp_generic_status_indicators_nr(void); 79 void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr); 80 void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr); 81 int get_hfp_ag_indicators_nr(hfp_connection_t * context); 82 hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context); 83 84 #define HFP_SUBEVENT_INVALID 0xFFFF 85 86 // const 87 static const char hfp_ag_default_service_name[] = "Voice gateway"; 88 89 // globals 90 91 // higher layer callbacks 92 static btstack_packet_handler_t hfp_ag_callback; 93 94 static bool (*hfp_ag_custom_call_sm_handler)(hfp_ag_call_event_t event); 95 96 static btstack_packet_callback_registration_t hfp_ag_hci_event_callback_registration; 97 98 static uint16_t hfp_ag_supported_features; 99 100 // in-band ring tone is active on SLC if supported 101 static bool hfp_ag_in_band_ring_tone_active; 102 103 // codecs 104 static uint8_t hfp_ag_codecs_nr; 105 static uint8_t hfp_ag_codecs[HFP_MAX_NUM_CODECS]; 106 107 // AG indicators 108 static uint16_t hfp_ag_indicators_nr; 109 static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS]; 110 111 // generic status indicators 112 static uint16_t hfp_ag_generic_status_indicators_nr; 113 static hfp_generic_status_indicator_t hfp_ag_generic_status_indicators[HFP_MAX_NUM_INDICATORS]; 114 115 static uint16_t hfp_ag_call_hold_services_nr; 116 static char * hfp_ag_call_hold_services[6]; 117 118 static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state; 119 static bool hfp_ag_response_and_hold_active = false; 120 121 // Subscriber information entries 122 static hfp_phone_number_t * hfp_ag_subscriber_numbers; 123 static int hfp_ag_subscriber_numbers_count; 124 125 // call state 126 static btstack_timer_source_t hfp_ag_ring_timeout; 127 128 // code 129 static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){ 130 if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 131 hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 132 (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 133 hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 134 } 135 return hfp_connection->ag_indicators_nr; 136 } 137 138 hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){ 139 // TODO: save only value, and value changed in the hfp_connection? 140 if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){ 141 hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr; 142 (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, 143 hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 144 } 145 return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators); 146 } 147 148 static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){ 149 int i; 150 for (i = 0; i < hfp_ag_indicators_nr; i++){ 151 if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 152 return &hfp_ag_indicators[i]; 153 } 154 } 155 return NULL; 156 } 157 158 static int get_ag_indicator_index_for_name(const char * name){ 159 int i; 160 for (i = 0; i < hfp_ag_indicators_nr; i++){ 161 if (strcmp(hfp_ag_indicators[i].name, name) == 0){ 162 return i; 163 } 164 } 165 return -1; 166 } 167 168 static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){ 169 btstack_linked_list_iterator_t it; 170 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 171 while (btstack_linked_list_iterator_has_next(&it)){ 172 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 173 if (hfp_connection->acl_handle != handle) continue; 174 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 175 return hfp_connection; 176 } 177 return NULL; 178 } 179 180 static bool has_in_band_ring_tone(void){ 181 return get_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE) != 0; 182 } 183 184 static int use_in_band_tone(hfp_connection_t *hfp_connection) { 185 return hfp_connection->ag_in_band_ring_tone_active ? 1 : 0; 186 } 187 188 static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 189 int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 190 int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 191 return hf && ag; 192 } 193 194 static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 195 int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING); 196 int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_THREE_WAY_CALLING); 197 return hf && ag; 198 } 199 200 static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 201 int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS); 202 int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_HF_INDICATORS); 203 return hf && ag; 204 } 205 206 /* unsolicited responses */ 207 208 static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection_t * hfp_connection){ 209 char buffer[40]; 210 snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", 211 HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone(hfp_connection)); 212 buffer[sizeof(buffer) - 1] = 0; 213 return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 214 } 215 216 static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){ 217 char buffer[40]; 218 snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n\r\nOK\r\n", 219 HFP_SUPPORTED_FEATURES, hfp_ag_supported_features); 220 buffer[sizeof(buffer) - 1] = 0; 221 return send_str_over_rfcomm(cid, buffer); 222 } 223 224 static int hfp_ag_send_ok(uint16_t cid){ 225 char buffer[10]; 226 snprintf(buffer, sizeof(buffer), "\r\nOK\r\n"); 227 buffer[sizeof(buffer) - 1] = 0; 228 return send_str_over_rfcomm(cid, buffer); 229 } 230 231 static int hfp_ag_send_ring(uint16_t cid){ 232 return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n"); 233 } 234 235 static int hfp_ag_send_no_carrier(uint16_t cid){ 236 char buffer[15]; 237 snprintf(buffer, sizeof(buffer), "\r\nNO CARRIER\r\n"); 238 buffer[sizeof(buffer) - 1] = 0; 239 return send_str_over_rfcomm(cid, buffer); 240 } 241 242 static int hfp_ag_send_clip(uint16_t cid){ 243 char buffer[50]; 244 snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, 245 hfp_gsm_clip_number(), hfp_gsm_clip_type()); 246 buffer[sizeof(buffer) - 1] = 0; 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 snprintf(buffer, sizeof(buffer), "\r\n%s: ,\"%s\",%u, , \r\n", 253 HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type); 254 buffer[sizeof(buffer) - 1] = 0; 255 return send_str_over_rfcomm(cid, buffer); 256 } 257 258 static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){ 259 char buffer[50]; 260 snprintf(buffer, sizeof(buffer), "\r\n%s: %s\r\n", 261 HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number()); 262 buffer[sizeof(buffer) - 1] = 0; 263 return send_str_over_rfcomm(cid, buffer); 264 } 265 266 static int hfp_ag_send_call_waiting_notification(uint16_t cid){ 267 char buffer[50]; 268 snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", 269 HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), 270 hfp_gsm_clip_type()); 271 buffer[sizeof(buffer) - 1] = 0; 272 return send_str_over_rfcomm(cid, buffer); 273 } 274 275 static int hfp_ag_send_error(uint16_t cid){ 276 char buffer[10]; 277 snprintf(buffer, sizeof(buffer), "\r\nERROR\r\n"); 278 buffer[sizeof(buffer) - 1] = 0; 279 return send_str_over_rfcomm(cid, buffer); 280 } 281 282 static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){ 283 char buffer[20]; 284 snprintf(buffer, sizeof(buffer), "\r\n%s=%d\r\n", 285 HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error); 286 buffer[sizeof(buffer) - 1] = 0; 287 return send_str_over_rfcomm(cid, buffer); 288 } 289 290 // get size for indicator string 291 static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){ 292 // template: ("$NAME",($MIN,$MAX)) 293 return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name) 294 + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range) 295 + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range); 296 } 297 298 // store indicator 299 static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer, uint16_t buffer_size){ 300 snprintf((char *)buffer, buffer_size, "(\"%s\",(%d,%d)),", 301 hfp_ag_get_ag_indicators(hfp_connection)[i].name, 302 hfp_ag_get_ag_indicators(hfp_connection)[i].min_range, 303 hfp_ag_get_ag_indicators(hfp_connection)[i].max_range); 304 ((char *)buffer)[buffer_size - 1] = 0; 305 } 306 307 // structure: header [indicator [comma indicator]] footer 308 static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){ 309 int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 310 if (!num_indicators) return 2; 311 return 3 + ((num_indicators-1) * 2); 312 } 313 314 // get size of individual segment for hfp_ag_retrieve_indicators_cmd 315 static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){ 316 if (index == 0) { 317 return (uint16_t) strlen(HFP_INDICATOR) + 3; // "\n\r%s:"" 318 } 319 index--; 320 int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 321 int indicator_index = index >> 1; 322 if ((index & 1) == 0){ 323 return hfp_ag_indicators_string_size(hfp_connection, indicator_index); 324 } 325 if (indicator_index == (num_indicators - 1)){ 326 return 8; // "\r\n\r\nOK\r\n" 327 } 328 return 1; // comma 329 } 330 331 static void hfp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer, uint16_t buffer_size){ 332 if (index == 0){ 333 *buffer++ = '\r'; 334 *buffer++ = '\n'; 335 int len = (uint16_t) strlen(HFP_INDICATOR); 336 (void)memcpy(buffer, HFP_INDICATOR, len); 337 buffer += len; 338 *buffer++ = ':'; 339 return; 340 } 341 index--; 342 int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection); 343 int indicator_index = index >> 1; 344 if ((index & 1) == 0){ 345 hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer, buffer_size); 346 return; 347 } 348 if (indicator_index == (num_indicators-1)){ 349 (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8); 350 return; 351 } 352 *buffer = ','; 353 } 354 355 static int hfp_ag_generic_indicators_join(char * buffer, int buffer_size){ 356 if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 357 int i; 358 int offset = 0; 359 for (i = 0; i < (hfp_ag_generic_status_indicators_nr - 1); i++) { 360 offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_generic_status_indicators[i].uuid); 361 } 362 if (i < hfp_ag_generic_status_indicators_nr){ 363 offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_generic_status_indicators[i].uuid); 364 } 365 return offset; 366 } 367 368 static int hfp_ag_generic_indicators_initial_status_join(char * buffer, int buffer_size){ 369 if (buffer_size < (hfp_ag_generic_status_indicators_nr * 3)) return 0; 370 int i; 371 int offset = 0; 372 for (i = 0; i < hfp_ag_generic_status_indicators_nr; i++) { 373 offset += snprintf(buffer+offset, buffer_size-offset, "\r\n%s:%d,%d\r\n", HFP_GENERIC_STATUS_INDICATOR, hfp_ag_generic_status_indicators[i].uuid, hfp_ag_generic_status_indicators[i].state); 374 } 375 return offset; 376 } 377 378 static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){ 379 if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0; 380 int i; 381 int offset = 0; 382 for (i = 0; i < (hfp_ag_indicators_nr-1); i++) { 383 offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status); 384 } 385 if (i<hfp_ag_indicators_nr){ 386 offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status); 387 } 388 return offset; 389 } 390 391 static int hfp_ag_call_services_join(char * buffer, int buffer_size){ 392 if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0; 393 int i; 394 int offset = snprintf(buffer, buffer_size, "("); 395 for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) { 396 offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]); 397 } 398 if (i<hfp_ag_call_hold_services_nr){ 399 offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]); 400 } 401 return offset; 402 } 403 404 static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, 405 int start_segment, int num_segments, 406 int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment), 407 void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer, uint16_t buffer_size)){ 408 409 // assumes: can send now == true 410 // assumes: num segments > 0 411 // assumes: individual segments are smaller than MTU 412 rfcomm_reserve_packet_buffer(); 413 int mtu = rfcomm_get_max_frame_size(cid); 414 uint8_t * data = rfcomm_get_outgoing_buffer(); 415 int offset = 0; 416 int segment = start_segment; 417 while (segment < num_segments){ 418 int segment_len = get_segment_len(hfp_connection, segment); 419 if ((offset + segment_len + 1) > mtu) break; 420 // append segment. As it appends a '\0', we provide a buffer one byte larger 421 store_segment(hfp_connection, segment, data+offset, segment_len + 1); 422 offset += segment_len; 423 segment++; 424 } 425 rfcomm_send_prepared(cid, offset); 426 #ifdef ENABLE_HFP_AT_MESSAGES 427 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, (char *) data); 428 #endif 429 return segment; 430 } 431 432 // returns next segment to store 433 static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){ 434 int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 435 return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments, 436 hfp_ag_indicators_cmd_generator_get_segment_len, 437 hfp_ag_indicators_cmd_generator_store_segment); 438 } 439 440 static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){ 441 char buffer[40]; 442 const int size = sizeof(buffer); 443 int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR); 444 offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9); 445 offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 446 return send_str_over_rfcomm(cid, buffer); 447 } 448 449 static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){ 450 char buffer[40]; 451 const int size = sizeof(buffer); 452 int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 453 offset += hfp_ag_call_services_join(buffer+offset, size-offset-9); 454 offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n"); 455 return send_str_over_rfcomm(cid, buffer); 456 } 457 458 459 static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){ 460 return hfp_ag_send_ok(cid); 461 } 462 463 static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){ 464 char buffer[40]; 465 const int size = sizeof(buffer); 466 int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR); 467 offset += hfp_ag_generic_indicators_join(buffer + offset, size - offset - 10); 468 offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n"); 469 return send_str_over_rfcomm(cid, buffer); 470 } 471 472 static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){ 473 char buffer[40]; 474 int offset = hfp_ag_generic_indicators_initial_status_join(buffer, sizeof(buffer) - 7); 475 snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n"); 476 return send_str_over_rfcomm(cid, buffer); 477 } 478 479 static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){ 480 char buffer[20]; 481 snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d\r\n", 482 HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, 483 indicator->status); 484 buffer[sizeof(buffer) - 1] = 0; 485 return send_str_over_rfcomm(cid, buffer); 486 } 487 488 static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){ 489 char buffer[41]; 490 if (strlen(op.name) == 0){ 491 snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode); 492 } else { 493 int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format); 494 offset += snprintf(buffer+offset, 16, "%s", op.name); 495 snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n"); 496 } 497 return send_str_over_rfcomm(cid, buffer); 498 } 499 500 static inline int hfp_ag_send_cmd_with_space_and_int(uint16_t cid, const char * cmd, uint8_t value){ 501 char buffer[30]; 502 snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", cmd, value); 503 return send_str_over_rfcomm(cid, buffer); 504 } 505 506 507 static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){ 508 char buffer[30]; 509 snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value); 510 return send_str_over_rfcomm(cid, buffer); 511 } 512 513 static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){ 514 return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 515 } 516 517 static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){ 518 return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 519 } 520 521 static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){ 522 return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 523 } 524 525 static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){ 526 return hfp_ag_send_cmd_with_space_and_int(cid, HFP_RESPONSE_AND_HOLD, state); 527 } 528 529 static int hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection_t * hfp_connection){ 530 char buffer[30]; 531 uint8_t evra_enabled = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0; 532 snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, evra_enabled, hfp_connection->ag_vra_state); 533 return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 534 } 535 536 static int hfp_ag_send_voice_recognition_cmd(hfp_connection_t * hfp_connection, uint8_t activate_voice_recognition){ 537 char buffer[30]; 538 if (hfp_connection->enhanced_voice_recognition_enabled){ 539 snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition, hfp_connection->ag_vra_state); 540 } else { 541 snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition); 542 } 543 return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 544 } 545 546 static int hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection_t * hfp_connection){ 547 char buffer[HFP_VR_TEXT_HEADER_SIZE + HFP_MAX_VR_TEXT_SIZE]; 548 snprintf(buffer, sizeof(buffer), "\r\n%s: 1,%d,%X,%d,%d,\"%s\"\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, 549 hfp_connection->ag_vra_state, 550 hfp_connection->ag_msg.text_id, 551 hfp_connection->ag_msg.text_type, 552 hfp_connection->ag_msg.text_operation, 553 hfp_connection->ag_msg.text); 554 return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 555 } 556 557 static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){ 558 if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD; 559 560 #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH 561 if (hfp_supports_codec(HFP_CODEC_LC3_SWB, hfp_ag_codecs_nr, hfp_ag_codecs)){ 562 if (hfp_supports_codec(HFP_CODEC_LC3_SWB, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){ 563 return HFP_CODEC_LC3_SWB; 564 } 565 } 566 #endif 567 568 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH 569 if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_ag_codecs_nr, hfp_ag_codecs)){ 570 if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){ 571 return HFP_CODEC_MSBC; 572 } 573 } 574 #endif 575 576 return HFP_CODEC_CVSD; 577 } 578 579 /* state machines */ 580 581 static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){ 582 return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) && (hfp_ag_supported_features & (1 << HFP_AGSF_ESCO_S4)); 583 } 584 585 static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 586 /* events ( == commands): 587 HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 588 HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 589 hf_trigger_codec_connection_setup == received BCC 590 ag_trigger_codec_connection_setup == received from AG to send BCS 591 HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 592 */ 593 switch (hfp_connection->codecs_state){ 594 case HFP_CODECS_EXCHANGED: 595 if (hfp_connection->command == HFP_CMD_AVAILABLE_CODECS){ 596 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 597 return 1; 598 } 599 break; 600 601 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 602 case HFP_CODECS_AG_RESEND_COMMON_CODEC: 603 hfp_connection->ag_send_common_codec = true; 604 break; 605 default: 606 break; 607 } 608 609 if (hfp_connection->ag_send_common_codec){ 610 hfp_connection->ag_send_common_codec = false; 611 hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC; 612 hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection); 613 hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec); 614 return 1; 615 } 616 617 switch (hfp_connection->command){ 618 case HFP_CMD_AVAILABLE_CODECS: 619 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){ 620 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST; 621 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 622 return 1; 623 } 624 625 switch (hfp_connection->codecs_state){ 626 case HFP_CODECS_AG_SENT_COMMON_CODEC: 627 hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC; 628 break; 629 default: 630 break; 631 } 632 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 633 return 1; 634 635 case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 636 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 637 hfp_connection->establish_audio_connection = 1; 638 hfp_connection->sco_for_msbc_failed = 0; 639 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 640 return 1; 641 642 case HFP_CMD_HF_CONFIRMED_CODEC: 643 if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){ 644 hfp_connection->codecs_state = HFP_CODECS_ERROR; 645 hfp_ag_send_error(hfp_connection->rfcomm_cid); 646 return 1; 647 } 648 hfp_connection->negotiated_codec = hfp_connection->codec_confirmed; 649 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 650 log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 651 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 652 // now, pick link settings 653 hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 654 // configure SBC coded if needed 655 hfp_prepare_for_sco(hfp_connection); 656 return 1; 657 default: 658 break; 659 } 660 return 0; 661 } 662 663 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 664 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 665 hfp_emit_slc_connection_event(hfp_connection->local_role, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 666 667 // in-band ring tone is active if supported 668 hfp_connection->ag_in_band_ring_tone_active = has_in_band_ring_tone(); 669 670 // HFP 4.35: "When [...] a new Service Level Connection is established all indicators are activated by default." 671 uint16_t i; 672 for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 673 hfp_connection->ag_indicators[i].enabled = 1; 674 } 675 676 // if active call exist, set per-hfp_connection state active, too (when audio is on) 677 if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 678 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 679 } 680 // if AG is ringing, also start ringing on the HF 681 if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){ 682 switch (hfp_gsm_callsetup_status()){ 683 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 684 hfp_ag_hf_start_ringing_incoming(hfp_connection); 685 break; 686 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 687 hfp_ag_hf_start_ringing_outgoing(hfp_connection); 688 break; 689 default: 690 break; 691 } 692 } 693 } 694 695 static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 696 // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command); 697 if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 698 int sent = 0; 699 switch(hfp_connection->command){ 700 case HFP_CMD_SUPPORTED_FEATURES: 701 switch(hfp_connection->state){ 702 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 703 case HFP_EXCHANGE_SUPPORTED_FEATURES: 704 hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_ag_codecs, &hfp_ag_codecs_nr); 705 if (has_codec_negotiation_feature(hfp_connection)){ 706 hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 707 } else { 708 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 709 } 710 hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid); 711 return 1; 712 default: 713 break; 714 } 715 break; 716 case HFP_CMD_AVAILABLE_CODECS: 717 sent = codecs_exchange_state_machine(hfp_connection); 718 if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){ 719 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 720 } 721 return sent; 722 723 case HFP_CMD_RETRIEVE_AG_INDICATORS: 724 if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) { 725 // HF requested AG Indicators and we did expect it 726 hfp_connection->send_ag_indicators_segment = 0; 727 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 728 // continue below in state switch 729 } 730 break; 731 732 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 733 if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break; 734 hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 735 hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 736 return 1; 737 738 case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 739 if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break; 740 if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 741 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 742 } else if (has_hf_indicators_feature(hfp_connection)){ 743 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 744 } else { 745 hfp_ag_slc_established(hfp_connection); 746 } 747 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 748 return 1; 749 750 case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES: 751 if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break; 752 if (has_hf_indicators_feature(hfp_connection)){ 753 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 754 } 755 hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid); 756 if (!has_hf_indicators_feature(hfp_connection)){ 757 hfp_ag_slc_established(hfp_connection); 758 } 759 return 1; 760 761 case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS: 762 if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break; 763 hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 764 hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 765 return 1; 766 767 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS: 768 if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break; 769 hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 770 hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 771 return 1; 772 773 case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE: 774 if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break; 775 hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid); 776 hfp_ag_slc_established(hfp_connection); 777 return 1; 778 default: 779 break; 780 } 781 782 switch (hfp_connection->state){ 783 case HFP_RETRIEVE_INDICATORS: { 784 int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment); 785 int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection); 786 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments); 787 if (next_segment < num_segments){ 788 // prepare sending of next segment 789 hfp_connection->send_ag_indicators_segment = next_segment; 790 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment); 791 } else { 792 // done, go to next state 793 hfp_connection->send_ag_indicators_segment = 0; 794 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 795 } 796 return 1; 797 } 798 default: 799 break; 800 } 801 return 0; 802 } 803 804 static bool hfp_ag_vra_flag_supported(hfp_connection_t * hfp_connection){ 805 int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION); 806 int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION); 807 return hf && ag; 808 } 809 810 static bool hfp_ag_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){ 811 int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS); 812 int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS); 813 return hf && ag; 814 } 815 816 static bool hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection_t * hfp_connection){ 817 int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_TEXT); 818 int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_TEXT); 819 return hf && ag; 820 } 821 822 static bool hfp_ag_can_activate_voice_recognition(hfp_connection_t * hfp_connection){ 823 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 824 return false; 825 } 826 if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){ 827 return false; 828 } 829 if (hfp_connection->ag_vra_send_command){ 830 return false; 831 } 832 return true; 833 } 834 835 static bool hfp_ag_voice_recognition_session_active(hfp_connection_t * hfp_connection){ 836 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){ 837 return false; 838 } 839 if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 840 if (hfp_connection->vra_state == HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 841 return true; 842 } 843 return false; 844 } 845 return true; 846 } 847 848 static bool hfp_ag_is_audio_connection_active(hfp_connection_t * hfp_connection){ 849 switch (hfp_connection->state){ 850 case HFP_W2_CONNECT_SCO: 851 case HFP_W4_SCO_CONNECTED: 852 case HFP_AUDIO_CONNECTION_ESTABLISHED: 853 return true; 854 default: 855 return false; 856 } 857 } 858 859 static void hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection_t * hfp_connection, uint8_t status){ 860 hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 861 862 uint8_t event[6]; 863 event[0] = HCI_EVENT_HFP_META; 864 event[1] = sizeof(event) - 2; 865 event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE_SENT; 866 little_endian_store_16(event, 3, acl_handle); 867 event[5] = status; 868 (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 869 } 870 871 872 static void hfp_ag_emit_hf_indicator_value(hfp_connection_t * hfp_connection, uint16_t uuid, uint8_t value){ 873 hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID; 874 875 uint8_t event[8]; 876 event[0] = HCI_EVENT_HFP_META; 877 event[1] = sizeof(event) - 2; 878 event[2] = HFP_SUBEVENT_HF_INDICATOR; 879 little_endian_store_16(event, 3, acl_handle); 880 little_endian_store_16(event, 5, uuid); 881 event[7] = value; 882 (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 883 } 884 885 static void hfp_ag_emit_general_simple_event(uint8_t event_subtype){ 886 if (!hfp_ag_callback) return; 887 888 uint8_t event[5]; 889 event[0] = HCI_EVENT_HFP_META; 890 event[1] = sizeof(event) - 2; 891 event[2] = event_subtype; 892 little_endian_store_16(event, 3, HCI_CON_HANDLE_INVALID); 893 (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 894 } 895 896 static void hfp_ag_emit_custom_command_event(hfp_connection_t * hfp_connection){ 897 btstack_assert(sizeof(hfp_connection->line_buffer) < (255-5)); 898 899 uint16_t line_len = (uint16_t) strlen((const char*)hfp_connection->line_buffer) + 1; 900 uint8_t event[7 + sizeof(hfp_connection->line_buffer)]; 901 event[0] = HCI_EVENT_HFP_META; 902 event[1] = 5 + line_len; 903 event[2] = HFP_SUBEVENT_CUSTOM_AT_COMMAND; 904 little_endian_store_16(event, 3, hfp_connection->acl_handle); 905 little_endian_store_16(event, 5, hfp_connection->custom_at_command_id); 906 memcpy(&event[7], hfp_connection->line_buffer, line_len); 907 (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, 7 + line_len); 908 } 909 910 // @return status 911 static uint8_t hfp_ag_vra_state_machine_two(hfp_connection_t * hfp_connection){ 912 uint8_t status = ERROR_CODE_SUCCESS; 913 switch (hfp_connection->vra_state_requested){ 914 case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO: 915 if (!hfp_ag_is_audio_connection_active(hfp_connection)){ 916 status = hfp_ag_setup_audio_connection(hfp_connection); 917 if (status != ERROR_CODE_SUCCESS){ 918 hfp_connection->vra_state_requested = hfp_connection->vra_state; 919 hfp_emit_voice_recognition_enabled(hfp_connection, status); 920 break; 921 } 922 } 923 hfp_connection->enhanced_voice_recognition_enabled = true; 924 hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 925 hfp_connection->vra_state_requested = hfp_connection->vra_state; 926 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS); 927 break; 928 929 case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 930 hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF; 931 hfp_connection->vra_state_requested = hfp_connection->vra_state; 932 933 // release audio connection only if it was opened after audio VR activated 934 if (hfp_ag_is_audio_connection_active(hfp_connection) && !hfp_connection->ag_audio_connection_opened_before_vra){ 935 hfp_trigger_release_audio_connection(hfp_connection); 936 } 937 938 hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS); 939 break; 940 941 case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 942 // open only if audio connection does not already exist 943 if (!hfp_ag_is_audio_connection_active(hfp_connection)){ 944 status = hfp_ag_setup_audio_connection(hfp_connection); 945 if (status != ERROR_CODE_SUCCESS){ 946 hfp_connection->vra_state_requested = hfp_connection->vra_state; 947 hfp_emit_voice_recognition_enabled(hfp_connection, status); 948 break; 949 } 950 } 951 952 hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED; 953 hfp_connection->vra_state_requested = hfp_connection->vra_state; 954 hfp_connection->enhanced_voice_recognition_enabled = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 955 956 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 957 hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 958 } else { 959 hfp_connection->emit_vra_enabled_after_audio_established = true; 960 } 961 break; 962 963 default: 964 break; 965 } 966 return status; 967 } 968 969 static uint8_t hfp_ag_vra_send_command(hfp_connection_t * hfp_connection){ 970 uint8_t done = 0; 971 uint8_t status; 972 973 switch (hfp_connection->vra_state_requested){ 974 case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG: 975 done = hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection); 976 if (done == 0){ 977 hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 978 } else { 979 hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_SUCCESS); 980 } 981 hfp_connection->vra_state_requested = hfp_connection->vra_state; 982 return done; 983 984 case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS: 985 done = hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection); 986 if (done == 0){ 987 hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR); 988 } else { 989 hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS); 990 } 991 hfp_connection->vra_state_requested = hfp_connection->vra_state; 992 return done; 993 994 case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED: 995 case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF: 996 done = hfp_ag_send_voice_recognition_cmd(hfp_connection, hfp_connection->ag_activate_voice_recognition_value); 997 if (done == 0){ 998 hfp_connection->vra_state_requested = hfp_connection->vra_state; 999 1000 if (hfp_connection->ag_activate_voice_recognition_value == 1){ 1001 hfp_emit_voice_recognition_enabled(hfp_connection, done); 1002 } else { 1003 hfp_emit_voice_recognition_disabled(hfp_connection, done); 1004 } 1005 return 0; 1006 } 1007 status = hfp_ag_vra_state_machine_two(hfp_connection); 1008 return (status == ERROR_CODE_SUCCESS) ? 1 : 0; 1009 1010 default: 1011 log_error("state %u", hfp_connection->vra_state_requested); 1012 btstack_assert(false); 1013 return 0; 1014 } 1015 } 1016 1017 static int hfp_ag_voice_recognition_state_machine(hfp_connection_t * hfp_connection){ 1018 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 1019 return 0; 1020 } 1021 int done = 0; 1022 uint8_t status; 1023 1024 // VRA action initiated by AG 1025 if (hfp_connection->ag_vra_send_command){ 1026 hfp_connection->ag_vra_send_command = false; 1027 return hfp_ag_vra_send_command(hfp_connection); 1028 } 1029 1030 if (hfp_connection->ag_vra_requested_by_hf){ 1031 hfp_connection->ag_vra_requested_by_hf = false; 1032 1033 // HF initiatied voice recognition, parser extracted activation value 1034 switch (hfp_connection->ag_activate_voice_recognition_value){ 1035 case 0: 1036 if (hfp_ag_voice_recognition_session_active(hfp_connection)){ 1037 hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 1038 done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1039 } 1040 break; 1041 1042 case 1: 1043 if (hfp_ag_can_activate_voice_recognition(hfp_connection)){ 1044 hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 1045 done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1046 } 1047 break; 1048 1049 case 2: 1050 if (hfp_ag_voice_recognition_session_active(hfp_connection)){ 1051 hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO; 1052 done = hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1053 } 1054 break; 1055 default: 1056 break; 1057 } 1058 if (done == 0){ 1059 hfp_connection->vra_state_requested = hfp_connection->vra_state; 1060 done = hfp_ag_send_error(hfp_connection->rfcomm_cid); 1061 return 1; 1062 } 1063 1064 status = hfp_ag_vra_state_machine_two(hfp_connection); 1065 return (status == ERROR_CODE_SUCCESS) ? 1 : 0; 1066 } 1067 1068 return 0; 1069 } 1070 1071 static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 1072 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) { 1073 return 0; 1074 } 1075 1076 int sent = codecs_exchange_state_machine(hfp_connection); 1077 if (sent) return 1; 1078 1079 if (has_in_band_ring_tone()){ 1080 if (hfp_connection->ag_in_band_ring_tone_active != hfp_ag_in_band_ring_tone_active) { 1081 hfp_connection->ag_in_band_ring_tone_active = hfp_ag_in_band_ring_tone_active; 1082 hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection); 1083 return 1; 1084 } 1085 } 1086 1087 switch(hfp_connection->command){ 1088 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME: 1089 hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator); 1090 return 1; 1091 case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT: 1092 if (hfp_connection->network_operator.format != 0){ 1093 hfp_ag_send_error(hfp_connection->rfcomm_cid); 1094 } else { 1095 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1096 } 1097 return 1; 1098 case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE: 1099 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1100 return 1; 1101 case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR: 1102 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1103 return 1; 1104 case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE: 1105 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1106 return 1; 1107 default: 1108 break; 1109 } 1110 return 0; 1111 } 1112 1113 static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){ 1114 if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 1115 (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 1116 1117 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1118 1119 if (hfp_connection->release_audio_connection){ 1120 hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 1121 hfp_connection->release_audio_connection = 0; 1122 gap_disconnect(hfp_connection->sco_handle); 1123 return 1; 1124 } 1125 1126 // accept incoming audio connection (codec negotiation is not used) 1127 if (hfp_connection->accept_sco && (hfp_sco_setup_active() == false)){ 1128 // notify about codec selection if not done already 1129 if (hfp_connection->negotiated_codec == 0){ 1130 hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 1131 } 1132 // 1133 bool incoming_eSCO = hfp_connection->accept_sco == 2; 1134 hfp_connection->state = HFP_W4_SCO_CONNECTED; 1135 hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO); 1136 return 1; 1137 } 1138 1139 // run codecs exchange 1140 int sent = codecs_exchange_state_machine(hfp_connection); 1141 if (sent) return 1; 1142 1143 if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 1144 if (hfp_sco_setup_active()) return 0; 1145 if (hci_can_send_command_packet_now() == false) return 0; 1146 if (hfp_connection->establish_audio_connection){ 1147 hfp_connection->state = HFP_W4_SCO_CONNECTED; 1148 hfp_connection->establish_audio_connection = 0; 1149 hfp_setup_synchronous_connection(hfp_connection); 1150 return 1; 1151 } 1152 return 0; 1153 } 1154 1155 static void hfp_ag_set_callsetup_indicator(void){ 1156 hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup"); 1157 if (!indicator){ 1158 log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing"); 1159 return; 1160 }; 1161 indicator->status = hfp_gsm_callsetup_status(); 1162 } 1163 1164 static void hfp_ag_set_callheld_indicator(void){ 1165 hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld"); 1166 if (!indicator){ 1167 log_error("hfp_ag_set_callheld_state: callheld indicator is missing"); 1168 return; 1169 }; 1170 indicator->status = hfp_gsm_callheld_status(); 1171 } 1172 1173 // 1174 // transition implementations for hfp_ag_call_state_machine 1175 // 1176 1177 static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection){ 1178 if (use_in_band_tone(hfp_connection)){ 1179 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING; 1180 hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1181 } else { 1182 hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1183 } 1184 } 1185 1186 static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection){ 1187 hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1188 } 1189 1190 static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){ 1191 hfp_connection->ag_ring = 0; 1192 hfp_connection->ag_send_clip = 0; 1193 } 1194 1195 1196 static void hfp_ag_trigger_incoming_call_during_active_one(void){ 1197 btstack_linked_list_iterator_t it; 1198 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1199 while (btstack_linked_list_iterator_has_next(&it)){ 1200 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1201 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1202 if (hfp_connection->call_state != HFP_CALL_ACTIVE) continue; 1203 1204 hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING; 1205 hfp_ag_run_for_context(hfp_connection); 1206 } 1207 } 1208 1209 static void hfp_ag_trigger_ring_and_clip(void) { 1210 btstack_linked_list_iterator_t it; 1211 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1212 while (btstack_linked_list_iterator_has_next(&it)){ 1213 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1214 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1215 switch (hfp_connection->call_state){ 1216 case HFP_CALL_INCOMING_RINGING: 1217 case HFP_CALL_OUTGOING_RINGING: 1218 case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1219 // Queue RING on this connection 1220 hfp_connection->ag_ring = 1; 1221 1222 // HFP v1.8, 4.23 Calling Line Identification (CLI) Notification 1223 // "If the calling subscriber number information is available from the network, the AG shall issue the 1224 // +CLIP unsolicited result code just after every RING indication when the HF is alerted in an incoming call." 1225 hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled; 1226 1227 // trigger next message 1228 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1229 break; 1230 default: 1231 break; 1232 } 1233 } 1234 } 1235 1236 // trigger RING and CLIP messages on connections that are ringing 1237 static void hfp_ag_ring_timeout_handler(btstack_timer_source_t * timer){ 1238 hfp_ag_trigger_ring_and_clip(); 1239 1240 btstack_run_loop_set_timer(timer, HFP_RING_PERIOD_MS); // 2 seconds timeout 1241 btstack_run_loop_add_timer(timer); 1242 } 1243 1244 static void hfp_ag_ring_timeout_stop(void){ 1245 btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 1246 } 1247 1248 static void hfp_ag_start_ringing(void){ 1249 // setup ring timer 1250 btstack_run_loop_remove_timer(&hfp_ag_ring_timeout); 1251 btstack_run_loop_set_timer_handler(&hfp_ag_ring_timeout, hfp_ag_ring_timeout_handler); 1252 btstack_run_loop_set_timer(&hfp_ag_ring_timeout, HFP_RING_PERIOD_MS); // 2 seconds timeout 1253 btstack_run_loop_add_timer(&hfp_ag_ring_timeout); 1254 1255 // emit start ringing 1256 hfp_ag_emit_general_simple_event(HFP_SUBEVENT_START_RINGING); 1257 1258 // send initial RING + CLIP 1259 hfp_ag_trigger_ring_and_clip(); 1260 } 1261 1262 static void hfp_ag_stop_ringing(void){ 1263 hfp_ag_ring_timeout_stop(); 1264 hfp_ag_emit_general_simple_event(HFP_SUBEVENT_STOP_RINGING); 1265 1266 btstack_linked_list_iterator_t it; 1267 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1268 while (btstack_linked_list_iterator_has_next(&it)){ 1269 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1270 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1271 if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1272 (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1273 hfp_ag_hf_stop_ringing(hfp_connection); 1274 } 1275 } 1276 1277 static void hfp_ag_trigger_incoming_call_idle(void){ 1278 int indicator_index = get_ag_indicator_index_for_name("callsetup"); 1279 if (indicator_index < 0) return; 1280 1281 // update callsetup state in connections 1282 btstack_linked_list_iterator_t it; 1283 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1284 while (btstack_linked_list_iterator_has_next(&it)){ 1285 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1286 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1287 if (hfp_connection->call_state != HFP_CALL_IDLE) continue; 1288 1289 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1290 1291 hfp_ag_hf_start_ringing_incoming(hfp_connection); 1292 1293 hfp_ag_run_for_context(hfp_connection); 1294 } 1295 1296 // start local ringing - started after connection state has been updated 1297 hfp_ag_start_ringing(); 1298 } 1299 1300 static void hfp_ag_trigger_outgoing_call(void){ 1301 btstack_linked_list_iterator_t it; 1302 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1303 while (btstack_linked_list_iterator_has_next(&it)){ 1304 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1305 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1306 hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1307 if (hfp_connection->call_state == HFP_CALL_IDLE){ 1308 hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1309 } 1310 } 1311 } 1312 1313 static void hfp_ag_handle_outgoing_call_accepted(hfp_connection_t * hfp_connection){ 1314 hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING; 1315 1316 // trigger callsetup to be 1317 int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 1318 hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL); 1319 1320 int indicator_index ; 1321 1322 hfp_ag_set_callsetup_indicator(); 1323 indicator_index = get_ag_indicator_index_for_name("callsetup"); 1324 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1325 1326 // put current call on hold if active 1327 if (put_call_on_hold){ 1328 log_info("AG putting current call on hold for new outgoing call"); 1329 hfp_ag_set_callheld_indicator(); 1330 indicator_index = get_ag_indicator_index_for_name("callheld"); 1331 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 1332 } 1333 1334 // start audio if needed 1335 hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1336 } 1337 1338 static void hfp_ag_transfer_indicator(const char * name){ 1339 int indicator_index = get_ag_indicator_index_for_name(name); 1340 if (indicator_index < 0) return; 1341 1342 btstack_linked_list_iterator_t it; 1343 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1344 while (btstack_linked_list_iterator_has_next(&it)){ 1345 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1346 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1347 hfp_ag_establish_service_level_connection(hfp_connection->remote_addr); 1348 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1349 hfp_ag_run_for_context(hfp_connection); 1350 } 1351 } 1352 1353 static void hfp_ag_transfer_callsetup_state(void){ 1354 hfp_ag_transfer_indicator("callsetup"); 1355 } 1356 1357 static void hfp_ag_transfer_call_state(void){ 1358 hfp_ag_transfer_indicator("call"); 1359 } 1360 1361 static void hfp_ag_transfer_callheld_state(void){ 1362 hfp_ag_transfer_indicator("callheld"); 1363 } 1364 1365 static void hfp_ag_hf_accept_call(hfp_connection_t * source){ 1366 int call_indicator_index = get_ag_indicator_index_for_name("call"); 1367 int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1368 1369 hfp_ag_stop_ringing(); 1370 1371 btstack_linked_list_iterator_t it; 1372 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1373 while (btstack_linked_list_iterator_has_next(&it)){ 1374 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1375 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1376 if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1377 (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1378 1379 if (hfp_connection == source){ 1380 1381 if (use_in_band_tone(hfp_connection)){ 1382 hfp_connection->call_state = HFP_CALL_ACTIVE; 1383 } else { 1384 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE; 1385 hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1386 } 1387 1388 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1389 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1390 1391 } else { 1392 hfp_connection->call_state = HFP_CALL_IDLE; 1393 } 1394 hfp_ag_run_for_context(hfp_connection); 1395 } 1396 } 1397 1398 static void hfp_ag_ag_accept_call(void){ 1399 int call_indicator_index = get_ag_indicator_index_for_name("call"); 1400 int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1401 1402 hfp_ag_stop_ringing(); 1403 1404 btstack_linked_list_iterator_t it; 1405 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1406 while (btstack_linked_list_iterator_has_next(&it)){ 1407 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1408 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1409 if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) && 1410 (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue; 1411 1412 hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION; 1413 1414 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1415 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1416 1417 hfp_ag_run_for_context(hfp_connection); 1418 break; // only single 1419 } 1420 } 1421 1422 static void hfp_ag_trigger_reject_call(void){ 1423 int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1424 1425 hfp_ag_stop_ringing(); 1426 1427 btstack_linked_list_iterator_t it; 1428 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1429 while (btstack_linked_list_iterator_has_next(&it)){ 1430 hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1431 if (connection->local_role != HFP_ROLE_AG) continue; 1432 1433 switch (connection->call_state){ 1434 case HFP_CALL_INCOMING_RINGING: 1435 case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1436 case HFP_CALL_OUTGOING_RINGING: 1437 case HFP_CALL_OUTGOING_INITIATED: 1438 case HFP_CALL_OUTGOING_DIALING: 1439 break; 1440 default: 1441 continue; 1442 } 1443 1444 connection->call_state = HFP_CALL_IDLE; 1445 connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1446 1447 if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1448 connection->release_audio_connection = 1; 1449 } 1450 hfp_emit_simple_event(connection, HFP_SUBEVENT_CALL_TERMINATED); 1451 hfp_ag_run_for_context(connection); 1452 rfcomm_request_can_send_now_event(connection->rfcomm_cid); 1453 } 1454 } 1455 1456 static void hfp_ag_trigger_terminate_call(void){ 1457 int call_indicator_index = get_ag_indicator_index_for_name("call"); 1458 1459 // no ringing during call 1460 1461 btstack_linked_list_iterator_t it; 1462 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1463 while (btstack_linked_list_iterator_has_next(&it)){ 1464 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1465 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1466 if (hfp_connection->call_state == HFP_CALL_IDLE) continue; 1467 1468 hfp_connection->call_state = HFP_CALL_IDLE; 1469 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1470 1471 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 1472 hfp_connection->release_audio_connection = 1; 1473 } 1474 1475 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1476 hfp_ag_run_for_context(hfp_connection); 1477 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1478 } 1479 } 1480 1481 static void hfp_ag_set_call_indicator(void){ 1482 hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call"); 1483 if (!indicator){ 1484 log_error("hfp_ag_set_call_state: call indicator is missing"); 1485 return; 1486 }; 1487 indicator->status = hfp_gsm_call_status(); 1488 } 1489 1490 static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){ 1491 btstack_linked_list_iterator_t it; 1492 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1493 while (btstack_linked_list_iterator_has_next(&it)){ 1494 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1495 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1496 if (hfp_connection->call_state == call_state) return hfp_connection; 1497 } 1498 return NULL; 1499 } 1500 1501 static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){ 1502 btstack_linked_list_iterator_t it; 1503 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1504 while (btstack_linked_list_iterator_has_next(&it)){ 1505 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1506 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 1507 hfp_connection->send_response_and_hold_status = state + 1; 1508 } 1509 } 1510 1511 static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 1512 int indicator_index; 1513 switch (hfp_connection->call_state){ 1514 case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING: 1515 if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1516 // we got event: audio hfp_connection established 1517 hfp_connection->call_state = HFP_CALL_INCOMING_RINGING; 1518 break; 1519 case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE: 1520 if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 1521 // we got event: audio hfp_connection established 1522 hfp_connection->call_state = HFP_CALL_ACTIVE; 1523 break; 1524 case HFP_CALL_W2_SEND_CALL_WAITING: 1525 hfp_connection->call_state = HFP_CALL_W4_CHLD; 1526 hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1527 indicator_index = get_ag_indicator_index_for_name("callsetup"); 1528 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1529 break; 1530 default: 1531 break; 1532 } 1533 return 0; 1534 } 1535 1536 // queue 'error' to HF 1537 static void hfp_ag_queue_error(hfp_connection_t * hfp_connection){ 1538 hfp_connection->send_error++; 1539 } 1540 1541 // queue 'ok' to HF 1542 static void hfp_ag_queue_ok(hfp_connection_t * hfp_connection){ 1543 hfp_connection->ok_pending++; 1544 } 1545 1546 // functions extracted from hfp_ag_call_sm below 1547 static void hfp_ag_handle_reject_outgoing_call(void){ 1548 hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1549 if (!hfp_connection){ 1550 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1551 return; 1552 } 1553 1554 hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL); 1555 hfp_connection->call_state = HFP_CALL_IDLE; 1556 hfp_ag_queue_error(hfp_connection); 1557 hfp_ag_run_for_context(hfp_connection); 1558 } 1559 1560 // hfp_connection is used to identify originating HF 1561 static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){ 1562 1563 // allow to intercept call statemachine events 1564 if (hfp_ag_custom_call_sm_handler != NULL){ 1565 bool handle_event = (*hfp_ag_custom_call_sm_handler)(event); 1566 if (!handle_event) return; 1567 } 1568 1569 int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup"); 1570 int callheld_indicator_index = get_ag_indicator_index_for_name("callheld"); 1571 int call_indicator_index = get_ag_indicator_index_for_name("call"); 1572 1573 switch (event){ 1574 case HFP_AG_INCOMING_CALL: 1575 switch (hfp_gsm_call_status()){ 1576 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1577 switch (hfp_gsm_callsetup_status()){ 1578 case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1579 hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1580 hfp_ag_set_callsetup_indicator(); 1581 hfp_ag_trigger_incoming_call_idle(); 1582 log_info("AG rings"); 1583 break; 1584 default: 1585 break; 1586 } 1587 break; 1588 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1589 switch (hfp_gsm_callsetup_status()){ 1590 case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1591 hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL); 1592 hfp_ag_set_callsetup_indicator(); 1593 hfp_ag_trigger_incoming_call_during_active_one(); 1594 log_info("AG call waiting"); 1595 break; 1596 default: 1597 break; 1598 } 1599 break; 1600 default: 1601 break; 1602 } 1603 break; 1604 case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1605 switch (hfp_gsm_call_status()){ 1606 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1607 switch (hfp_gsm_callsetup_status()){ 1608 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1609 hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1610 hfp_ag_set_call_indicator(); 1611 hfp_ag_set_callsetup_indicator(); 1612 hfp_ag_ag_accept_call(); 1613 log_info("AG answers call, accept call by GSM"); 1614 break; 1615 default: 1616 break; 1617 } 1618 break; 1619 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1620 switch (hfp_gsm_callsetup_status()){ 1621 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1622 log_info("AG: current call is placed on hold, incoming call gets active"); 1623 hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL); 1624 hfp_ag_set_callsetup_indicator(); 1625 hfp_ag_set_callheld_indicator(); 1626 hfp_ag_transfer_callsetup_state(); 1627 hfp_ag_transfer_callheld_state(); 1628 break; 1629 default: 1630 break; 1631 } 1632 break; 1633 default: 1634 break; 1635 } 1636 break; 1637 1638 case HFP_AG_HELD_CALL_JOINED_BY_AG: 1639 switch (hfp_gsm_call_status()){ 1640 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1641 switch (hfp_gsm_callheld_status()){ 1642 case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 1643 log_info("AG: joining held call with active call"); 1644 hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL); 1645 hfp_ag_set_callheld_indicator(); 1646 hfp_ag_transfer_callheld_state(); 1647 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1648 break; 1649 default: 1650 break; 1651 } 1652 break; 1653 default: 1654 break; 1655 } 1656 break; 1657 1658 case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1659 switch (hfp_gsm_call_status()){ 1660 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1661 switch (hfp_gsm_callsetup_status()){ 1662 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1663 hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL); 1664 hfp_ag_set_callsetup_indicator(); 1665 hfp_ag_set_call_indicator(); 1666 hfp_ag_hf_accept_call(hfp_connection); 1667 hfp_ag_queue_ok(hfp_connection); 1668 log_info("HF answers call, accept call by GSM"); 1669 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1670 break; 1671 default: 1672 break; 1673 } 1674 break; 1675 default: 1676 break; 1677 } 1678 break; 1679 1680 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1681 switch (hfp_gsm_call_status()){ 1682 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1683 switch (hfp_gsm_callsetup_status()){ 1684 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1685 hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL); 1686 hfp_ag_response_and_hold_active = true; 1687 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1688 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1689 // as with regular call 1690 hfp_ag_set_call_indicator(); 1691 hfp_ag_set_callsetup_indicator(); 1692 hfp_ag_ag_accept_call(); 1693 log_info("AG response and hold - hold by AG"); 1694 break; 1695 default: 1696 break; 1697 } 1698 break; 1699 default: 1700 break; 1701 } 1702 break; 1703 1704 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1705 switch (hfp_gsm_call_status()){ 1706 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1707 switch (hfp_gsm_callsetup_status()){ 1708 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1709 hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL); 1710 hfp_ag_response_and_hold_active = true; 1711 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1712 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1713 // as with regular call 1714 hfp_ag_set_call_indicator(); 1715 hfp_ag_set_callsetup_indicator(); 1716 hfp_ag_hf_accept_call(hfp_connection); 1717 log_info("AG response and hold - hold by HF"); 1718 break; 1719 default: 1720 break; 1721 } 1722 break; 1723 default: 1724 break; 1725 } 1726 break; 1727 1728 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1729 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1730 if (!hfp_ag_response_and_hold_active) break; 1731 if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1732 hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL); 1733 hfp_ag_response_and_hold_active = false; 1734 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1735 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1736 log_info("Held Call accepted and active"); 1737 break; 1738 1739 case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1740 case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1741 if (!hfp_ag_response_and_hold_active) break; 1742 if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1743 hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL); 1744 hfp_ag_response_and_hold_active = false; 1745 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1746 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1747 // from terminate by ag 1748 hfp_ag_set_call_indicator(); 1749 hfp_ag_trigger_terminate_call(); 1750 break; 1751 1752 case HFP_AG_TERMINATE_CALL_BY_HF: 1753 switch (hfp_gsm_call_status()){ 1754 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1755 switch (hfp_gsm_callsetup_status()){ 1756 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1757 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1758 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1759 hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 1760 hfp_ag_set_callsetup_indicator(); 1761 hfp_ag_transfer_callsetup_state(); 1762 hfp_ag_trigger_reject_call(); 1763 log_info("HF Rejected Incoming call, AG terminate call"); 1764 break; 1765 default: 1766 break; 1767 } 1768 break; 1769 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1770 hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL); 1771 hfp_ag_set_callsetup_indicator(); 1772 hfp_ag_set_call_indicator(); 1773 hfp_ag_trigger_terminate_call(); 1774 log_info("AG terminate call"); 1775 break; 1776 default: 1777 break; 1778 } 1779 break; 1780 1781 case HFP_AG_TERMINATE_CALL_BY_AG: 1782 switch (hfp_gsm_call_status()){ 1783 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1784 switch (hfp_gsm_callsetup_status()){ 1785 case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1786 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1787 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1788 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1789 hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1790 hfp_ag_set_callsetup_indicator(); 1791 hfp_ag_trigger_reject_call(); 1792 log_info("AG Rejected Incoming call, AG terminate call"); 1793 break; 1794 default: 1795 break; 1796 } 1797 break; 1798 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1799 hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL); 1800 hfp_ag_set_callsetup_indicator(); 1801 hfp_ag_set_call_indicator(); 1802 hfp_ag_trigger_terminate_call(); 1803 log_info("AG terminate call"); 1804 break; 1805 default: 1806 break; 1807 } 1808 break; 1809 case HFP_AG_CALL_DROPPED: 1810 switch (hfp_gsm_call_status()){ 1811 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1812 switch (hfp_gsm_callsetup_status()){ 1813 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1814 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1815 hfp_ag_stop_ringing(); 1816 break; 1817 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1818 log_info("Outgoing call interrupted\n"); 1819 break; 1820 default: 1821 break; 1822 } 1823 hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1824 hfp_ag_set_callsetup_indicator(); 1825 hfp_ag_transfer_callsetup_state(); 1826 hfp_ag_trigger_terminate_call(); 1827 break; 1828 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1829 if (hfp_ag_response_and_hold_active) { 1830 hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1831 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1832 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1833 } 1834 hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL); 1835 hfp_ag_set_callsetup_indicator(); 1836 hfp_ag_set_call_indicator(); 1837 hfp_ag_trigger_terminate_call(); 1838 log_info("AG notify call dropped"); 1839 break; 1840 default: 1841 break; 1842 } 1843 break; 1844 1845 case HFP_AG_OUTGOING_CALL_INITIATED_BY_HF: 1846 // directly reject call if number of free slots is exceeded 1847 if (!hfp_gsm_call_possible()){ 1848 hfp_ag_queue_error(hfp_connection); 1849 hfp_ag_run_for_context(hfp_connection); 1850 break; 1851 } 1852 1853 // note: number not used currently 1854 hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, 0, 0, (const char *) &hfp_connection->line_buffer[3]); 1855 1856 hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1857 1858 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]); 1859 break; 1860 1861 case HFP_AG_OUTGOING_CALL_INITIATED_BY_AG: 1862 // directly reject call if number of free slots is exceeded 1863 if (!hfp_gsm_call_possible()) { 1864 // TODO: no error for user 1865 break; 1866 } 1867 switch (hfp_gsm_call_status()) { 1868 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1869 switch (hfp_gsm_callsetup_status()) { 1870 case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: 1871 // note: number not used currently 1872 hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, 0, 0, "1234567"); 1873 // init call state for all connections 1874 hfp_ag_trigger_outgoing_call(); 1875 // get first one and accept call 1876 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1877 if (hfp_connection) { 1878 hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1879 } 1880 break; 1881 default: 1882 // TODO: no error for user 1883 break; 1884 } 1885 break; 1886 default: 1887 // TODO: no error for user 1888 break; 1889 } 1890 break; 1891 case HFP_AG_OUTGOING_REDIAL_INITIATED:{ 1892 // directly reject call if number of free slots is exceeded 1893 if (!hfp_gsm_call_possible()){ 1894 hfp_ag_queue_error(hfp_connection); 1895 hfp_ag_run_for_context(hfp_connection); 1896 break; 1897 } 1898 1899 hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL); 1900 hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1901 1902 log_info("Redial last number"); 1903 char * last_dialed_number = hfp_gsm_last_dialed_number(); 1904 1905 if (strlen(last_dialed_number) > 0){ 1906 log_info("Last number exists: accept call"); 1907 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number); 1908 } else { 1909 log_info("log_infoLast number missing: reject call"); 1910 hfp_ag_handle_reject_outgoing_call(); 1911 } 1912 break; 1913 } 1914 case HFP_AG_OUTGOING_CALL_REJECTED: 1915 hfp_ag_handle_reject_outgoing_call(); 1916 break; 1917 1918 case HFP_AG_OUTGOING_CALL_ACCEPTED:{ 1919 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1920 if (!hfp_connection){ 1921 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1922 break; 1923 } 1924 1925 hfp_ag_queue_ok(hfp_connection); 1926 hfp_ag_handle_outgoing_call_accepted(hfp_connection); 1927 break; 1928 } 1929 case HFP_AG_OUTGOING_CALL_RINGING: 1930 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1931 if (!hfp_connection){ 1932 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state"); 1933 break; 1934 } 1935 1936 hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL); 1937 hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1938 hfp_ag_set_callsetup_indicator(); 1939 hfp_ag_transfer_callsetup_state(); 1940 hfp_ag_hf_start_ringing_outgoing(hfp_connection); 1941 break; 1942 1943 case HFP_AG_OUTGOING_CALL_ESTABLISHED:{ 1944 // get outgoing call 1945 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1946 if (!hfp_connection){ 1947 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1948 } 1949 if (!hfp_connection){ 1950 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection"); 1951 break; 1952 } 1953 1954 bool callheld_status_call_on_hold_and_no_active_calls = hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS; 1955 hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL); 1956 hfp_connection->call_state = HFP_CALL_ACTIVE; 1957 hfp_ag_set_callsetup_indicator(); 1958 hfp_ag_set_call_indicator(); 1959 hfp_ag_transfer_call_state(); 1960 hfp_ag_transfer_callsetup_state(); 1961 if (callheld_status_call_on_hold_and_no_active_calls){ 1962 hfp_ag_set_callheld_indicator(); 1963 hfp_ag_transfer_callheld_state(); 1964 } 1965 hfp_ag_hf_stop_ringing(hfp_connection); 1966 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1967 break; 1968 } 1969 1970 case HFP_AG_CALL_HOLD_USER_BUSY: 1971 hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL); 1972 hfp_ag_set_callsetup_indicator(); 1973 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1974 hfp_connection->call_state = HFP_CALL_ACTIVE; 1975 log_info("AG: Call Waiting, User Busy"); 1976 break; 1977 1978 case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1979 int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1980 int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1981 1982 // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1983 if (call_held || call_setup_in_progress){ 1984 hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 1985 0, NULL); 1986 1987 } 1988 1989 if (call_setup_in_progress){ 1990 log_info("AG: Call Dropped, Accept new call"); 1991 hfp_ag_set_callsetup_indicator(); 1992 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1993 } else { 1994 log_info("AG: Call Dropped, Resume held call"); 1995 } 1996 if (call_held){ 1997 hfp_ag_set_callheld_indicator(); 1998 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1999 } 2000 2001 hfp_connection->call_state = HFP_CALL_ACTIVE; 2002 break; 2003 } 2004 2005 case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 2006 // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 2007 // only update if callsetup changed 2008 int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 2009 hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0, 2010 NULL); 2011 2012 if (call_setup_in_progress){ 2013 log_info("AG: Call on Hold, Accept new call"); 2014 hfp_ag_set_callsetup_indicator(); 2015 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 2016 } else { 2017 log_info("AG: Swap calls"); 2018 } 2019 2020 hfp_ag_set_callheld_indicator(); 2021 // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 2022 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2023 hfp_connection->call_state = HFP_CALL_ACTIVE; 2024 break; 2025 } 2026 2027 case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 2028 // Adds a held call to the conversation. 2029 if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 2030 log_info("AG: Join 3-way-call"); 2031 hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL); 2032 hfp_ag_set_callheld_indicator(); 2033 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2034 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 2035 } 2036 hfp_connection->call_state = HFP_CALL_ACTIVE; 2037 break; 2038 case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 2039 // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 2040 hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL); 2041 log_info("AG: Transfer call -> Connect two calls and disconnect"); 2042 hfp_ag_set_call_indicator(); 2043 hfp_ag_set_callheld_indicator(); 2044 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 2045 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 2046 hfp_connection->call_state = HFP_CALL_IDLE; 2047 break; 2048 2049 default: 2050 break; 2051 } 2052 2053 2054 } 2055 2056 2057 static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 2058 hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 2059 if (!active_call) return; 2060 2061 int idx = active_call->index; 2062 hfp_enhanced_call_dir_t dir = active_call->direction; 2063 hfp_enhanced_call_status_t status = active_call->enhanced_status; 2064 hfp_enhanced_call_mode_t mode = active_call->mode; 2065 hfp_enhanced_call_mpty_t mpty = active_call->mpty; 2066 uint8_t type = active_call->clip_type; 2067 char * number = active_call->clip_number; 2068 2069 char buffer[100]; 2070 // TODO: check length of a buffer, to fit the MTU 2071 int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 2072 if (number){ 2073 offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type); 2074 } 2075 snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 2076 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, 2077 mode, mpty, type, number); 2078 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 2079 } 2080 2081 // sends pending command, returns if command was sent 2082 static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){ 2083 if (hfp_connection->send_status_of_current_calls){ 2084 if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 2085 hfp_connection->next_call_index++; 2086 hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 2087 return 1; 2088 } else { 2089 // 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 2090 hfp_connection->next_call_index = 0; 2091 hfp_ag_queue_ok(hfp_connection); 2092 hfp_connection->send_status_of_current_calls = 0; 2093 } 2094 } 2095 2096 if (hfp_connection->ag_notify_incoming_call_waiting){ 2097 hfp_connection->ag_notify_incoming_call_waiting = 0; 2098 hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 2099 return 1; 2100 } 2101 2102 if (hfp_connection->send_error > 0){ 2103 hfp_connection->send_error--; 2104 hfp_connection->command = HFP_CMD_NONE; 2105 hfp_ag_send_error(hfp_connection->rfcomm_cid); 2106 return 1; 2107 } 2108 2109 // note: before update AG indicators and ok_pending 2110 if (hfp_connection->send_response_and_hold_status){ 2111 int status = hfp_connection->send_response_and_hold_status - 1; 2112 hfp_connection->send_response_and_hold_status = 0; 2113 hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status); 2114 return 1; 2115 } 2116 2117 // note: before ok_pending and send_error to allow for unsolicited result on custom command 2118 if (hfp_connection->send_custom_message != NULL){ 2119 const char * message = hfp_connection->send_custom_message; 2120 hfp_connection->send_custom_message = NULL; 2121 send_str_over_rfcomm(hfp_connection->rfcomm_cid, message); 2122 hfp_emit_event(hfp_connection, HFP_SUBEVENT_CUSTOM_AT_MESSAGE_SENT, ERROR_CODE_SUCCESS); 2123 return 1; 2124 } 2125 2126 if (hfp_connection->ok_pending > 0){ 2127 hfp_connection->ok_pending--; 2128 hfp_connection->command = HFP_CMD_NONE; 2129 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2130 return 1; 2131 } 2132 2133 // update AG indicators only if enabled by AT+CMER=3,0,0,1 2134 if ((hfp_connection->enable_status_update_for_ag_indicators == 1) && (hfp_connection->ag_indicators_status_update_bitmap != 0)){ 2135 uint16_t i; 2136 for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 2137 if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 2138 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 2139 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 2140 return 1; 2141 } 2142 } 2143 } 2144 2145 if (hfp_connection->ag_send_no_carrier){ 2146 hfp_connection->ag_send_no_carrier = false; 2147 hfp_ag_send_no_carrier(hfp_connection->rfcomm_cid); 2148 return 1; 2149 } 2150 2151 if (hfp_connection->send_phone_number_for_voice_tag){ 2152 hfp_connection->send_phone_number_for_voice_tag = 0; 2153 hfp_connection->command = HFP_CMD_NONE; 2154 hfp_ag_queue_ok(hfp_connection); 2155 hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 2156 return 1; 2157 } 2158 2159 if (hfp_connection->send_subscriber_number){ 2160 if (hfp_connection->next_subscriber_number_to_send < hfp_ag_subscriber_numbers_count){ 2161 hfp_phone_number_t phone = hfp_ag_subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 2162 hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 2163 } else { 2164 hfp_connection->send_subscriber_number = 0; 2165 hfp_connection->next_subscriber_number_to_send = 0; 2166 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2167 } 2168 hfp_connection->command = HFP_CMD_NONE; 2169 return 1; 2170 } 2171 2172 if (hfp_connection->send_microphone_gain){ 2173 hfp_connection->send_microphone_gain = 0; 2174 hfp_connection->command = HFP_CMD_NONE; 2175 hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 2176 return 1; 2177 } 2178 2179 if (hfp_connection->send_speaker_gain){ 2180 hfp_connection->send_speaker_gain = 0; 2181 hfp_connection->command = HFP_CMD_NONE; 2182 hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 2183 return 1; 2184 } 2185 2186 if (hfp_connection->send_ag_status_indicators){ 2187 hfp_connection->send_ag_status_indicators = 0; 2188 hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 2189 return 1; 2190 } 2191 2192 if (hfp_connection->extended_audio_gateway_error){ 2193 uint8_t extended_audio_gateway_error = hfp_connection->extended_audio_gateway_error; 2194 hfp_connection->extended_audio_gateway_error = 0; 2195 hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, extended_audio_gateway_error); 2196 2197 } 2198 return 0; 2199 } 2200 2201 // sends pending command, returns if command was sent 2202 static int hfp_ag_run_ring_and_clip(hfp_connection_t *hfp_connection){ 2203 // delay RING/CLIP until audio connection has been established for incoming calls 2204 // hfp_ag_hf_trigger_ring_and_clip is called in call_setup_state_machine 2205 if (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING){ 2206 if (hfp_connection->ag_ring){ 2207 hfp_connection->ag_ring = 0; 2208 hfp_connection->command = HFP_CMD_NONE; 2209 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 2210 hfp_ag_send_ring(hfp_connection->rfcomm_cid); 2211 return 1; 2212 } 2213 2214 if (hfp_connection->ag_send_clip){ 2215 hfp_connection->ag_send_clip = 0; 2216 hfp_connection->command = HFP_CMD_NONE; 2217 hfp_ag_send_clip(hfp_connection->rfcomm_cid); 2218 return 1; 2219 } 2220 } 2221 return 0; 2222 } 2223 2224 static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){ 2225 2226 btstack_assert(hfp_connection != NULL); 2227 btstack_assert(hfp_connection->local_role == HFP_ROLE_AG); 2228 2229 // during SDP query, RFCOMM CID is not set 2230 if (hfp_connection->rfcomm_cid == 0) return; 2231 2232 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){ 2233 hfp_connection->emit_vra_enabled_after_audio_established = false; 2234 hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS); 2235 } 2236 2237 if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){ 2238 hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 2239 hfp_connection->release_audio_connection = 0; 2240 gap_disconnect(hfp_connection->sco_handle); 2241 return; 2242 } 2243 2244 // configure NBS/WBS if needed using vendor-specific HCI commands 2245 if (hci_can_send_command_packet_now()) { 2246 #ifdef ENABLE_CC256X_ASSISTED_HFP 2247 // WBS Disassociate 2248 if (hfp_connection->cc256x_send_wbs_disassociate){ 2249 hfp_connection->cc256x_send_wbs_disassociate = false; 2250 hci_send_cmd(&hci_ti_wbs_disassociate); 2251 return; 2252 } 2253 // Write Codec Config 2254 if (hfp_connection->cc256x_send_write_codec_config){ 2255 hfp_connection->cc256x_send_write_codec_config = false; 2256 hfp_cc256x_write_codec_config(hfp_connection); 2257 return; 2258 } 2259 // WBS Associate 2260 if (hfp_connection->cc256x_send_wbs_associate){ 2261 hfp_connection->cc256x_send_wbs_associate = false; 2262 hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle); 2263 return; 2264 } 2265 #endif 2266 #ifdef ENABLE_BCM_PCM_WBS 2267 // Enable WBS 2268 if (hfp_connection->bcm_send_enable_wbs){ 2269 hfp_connection->bcm_send_enable_wbs = false; 2270 hci_send_cmd(&hci_bcm_enable_wbs, 1, 2); 2271 return; 2272 } 2273 // Write I2S/PCM params 2274 if (hfp_connection->bcm_send_write_i2spcm_interface_param){ 2275 hfp_connection->bcm_send_write_i2spcm_interface_param = false; 2276 hfp_bcm_write_i2spcm_interface_param(hfp_connection); 2277 return; 2278 } 2279 // Disable WBS 2280 if (hfp_connection->bcm_send_disable_wbs){ 2281 hfp_connection->bcm_send_disable_wbs = false; 2282 hci_send_cmd(&hci_bcm_enable_wbs, 0, 2); 2283 return; 2284 } 2285 #endif 2286 #ifdef ENABLE_RTK_PCM_WBS 2287 // Configure CVSD vs. mSBC 2288 if (hfp_connection->rtk_send_sco_config){ 2289 hfp_connection->rtk_send_sco_config = false; 2290 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 2291 log_info("RTK SCO: 16k + mSBC"); 2292 hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x00, 0x00, 0x41); 2293 } else { 2294 log_info("RTK SCO: 16k + CVSD"); 2295 hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x0c, 0x00, 0x01); 2296 } 2297 return; 2298 } 2299 #endif 2300 #ifdef ENABLE_NXP_PCM_WBS 2301 if (hfp_connection->nxp_start_audio_handle != HCI_CON_HANDLE_INVALID){ 2302 hci_con_handle_t sco_handle = hfp_connection->nxp_start_audio_handle; 2303 hfp_connection->nxp_start_audio_handle = HCI_CON_HANDLE_INVALID; 2304 hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 0, 0, sco_handle, 0); 2305 return; 2306 } 2307 if (hfp_connection->nxp_stop_audio_handle != HCI_CON_HANDLE_INVALID){ 2308 hci_con_handle_t sco_handle = hfp_connection->nxp_stop_audio_handle; 2309 hfp_connection->nxp_stop_audio_handle = HCI_CON_HANDLE_INVALID; 2310 hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 1, 0, sco_handle, 0); 2311 return; 2312 } 2313 #endif 2314 } 2315 2316 #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS) 2317 if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){ 2318 hfp_finalize_connection_context(hfp_connection); 2319 return; 2320 } 2321 #endif 2322 2323 if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 2324 log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid); 2325 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2326 return; 2327 } 2328 2329 int cmd_sent = hfp_ag_send_commands(hfp_connection); 2330 2331 if (!cmd_sent){ 2332 cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection); 2333 } 2334 2335 if (!cmd_sent){ 2336 cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 2337 } 2338 2339 if (!cmd_sent){ 2340 cmd_sent = call_setup_state_machine(hfp_connection); 2341 } 2342 2343 if (!cmd_sent){ 2344 cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection); 2345 } 2346 2347 if (!cmd_sent){ 2348 cmd_sent = hfp_ag_run_ring_and_clip(hfp_connection); 2349 } 2350 2351 if (!cmd_sent){ 2352 cmd_sent = hfp_ag_voice_recognition_state_machine(hfp_connection); 2353 } 2354 2355 // disconnect 2356 if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && (hfp_connection->state == HFP_W2_DISCONNECT_RFCOMM)){ 2357 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 2358 rfcomm_disconnect(hfp_connection->rfcomm_cid); 2359 } 2360 2361 if (cmd_sent){ 2362 hfp_connection->command = HFP_CMD_NONE; 2363 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 2364 } 2365 } 2366 2367 static int hfp_parser_is_end_of_line(uint8_t byte){ 2368 return (byte == '\n') || (byte == '\r'); 2369 } 2370 2371 static void hfp_ag_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){ 2372 // assertion: size >= 1 as rfcomm.c does not deliver empty packets 2373 if (size < 1) return; 2374 uint8_t status = ERROR_CODE_SUCCESS; 2375 2376 hfp_log_rfcomm_message("HFP_AG_RX", packet, size); 2377 #ifdef ENABLE_HFP_AT_MESSAGES 2378 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet); 2379 #endif 2380 2381 // process messages byte-wise 2382 int pos; 2383 for (pos = 0; pos < size ; pos++){ 2384 hfp_parse(hfp_connection, packet[pos], 0); 2385 2386 // parse until end of line 2387 if (!hfp_parser_is_end_of_line(packet[pos])) continue; 2388 2389 hfp_generic_status_indicator_t * indicator; 2390 switch(hfp_connection->command){ 2391 case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION: 2392 hfp_connection->command = HFP_CMD_NONE; 2393 hfp_connection->ag_vra_requested_by_hf = true; 2394 break; 2395 case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 2396 hfp_connection->command = HFP_CMD_NONE; 2397 if (hfp_ag_response_and_hold_active){ 2398 hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 2399 } 2400 hfp_ag_queue_ok(hfp_connection); 2401 break; 2402 case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 2403 hfp_connection->command = HFP_CMD_NONE; 2404 switch(hfp_connection->ag_response_and_hold_action){ 2405 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 2406 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 2407 break; 2408 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 2409 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 2410 break; 2411 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 2412 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 2413 break; 2414 default: 2415 break; 2416 } 2417 hfp_ag_queue_ok(hfp_connection); 2418 break; 2419 case HFP_CMD_HF_INDICATOR_STATUS: 2420 hfp_connection->command = HFP_CMD_NONE; 2421 2422 if (hfp_connection->parser_indicator_index < hfp_ag_generic_status_indicators_nr){ 2423 indicator = &hfp_ag_generic_status_indicators[hfp_connection->parser_indicator_index]; 2424 switch (indicator->uuid){ 2425 case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY: 2426 if (hfp_connection->parser_indicator_value > 1) { 2427 hfp_ag_queue_error(hfp_connection); 2428 break; 2429 } 2430 hfp_ag_queue_ok(hfp_connection); 2431 hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2432 break; 2433 case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL: 2434 if (hfp_connection->parser_indicator_value > 100){ 2435 hfp_ag_queue_error(hfp_connection); 2436 break; 2437 } 2438 hfp_ag_queue_ok(hfp_connection); 2439 hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value); 2440 break; 2441 default: 2442 hfp_ag_queue_error(hfp_connection); 2443 break; 2444 } 2445 } else { 2446 hfp_ag_queue_error(hfp_connection); 2447 } 2448 break; 2449 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 2450 // expected by SLC state machine 2451 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 2452 hfp_connection->send_ag_indicators_segment = 0; 2453 hfp_connection->send_ag_status_indicators = 1; 2454 break; 2455 case HFP_CMD_LIST_CURRENT_CALLS: 2456 hfp_connection->command = HFP_CMD_NONE; 2457 hfp_connection->next_call_index = 0; 2458 hfp_connection->send_status_of_current_calls = 1; 2459 break; 2460 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 2461 hfp_connection->command = HFP_CMD_NONE; 2462 if (hfp_ag_subscriber_numbers_count == 0){ 2463 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 2464 break; 2465 } 2466 hfp_connection->next_subscriber_number_to_send = 0; 2467 hfp_connection->send_subscriber_number = 1; 2468 break; 2469 case HFP_CMD_TRANSMIT_DTMF_CODES: 2470 { 2471 hfp_connection->command = HFP_CMD_NONE; 2472 char buffer[2]; 2473 buffer[0] = (char) hfp_connection->ag_dtmf_code; 2474 buffer[1] = 0; 2475 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer); 2476 break; 2477 } 2478 case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 2479 hfp_connection->command = HFP_CMD_NONE; 2480 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 2481 break; 2482 case HFP_CMD_TURN_OFF_EC_AND_NR: 2483 hfp_connection->command = HFP_CMD_NONE; 2484 2485 if (get_bit(hfp_ag_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 2486 hfp_ag_queue_ok(hfp_connection); 2487 status = ERROR_CODE_SUCCESS; 2488 } else { 2489 hfp_ag_queue_error(hfp_connection); 2490 status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 2491 } 2492 hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status); 2493 break; 2494 case HFP_CMD_CALL_ANSWERED: 2495 hfp_connection->command = HFP_CMD_NONE; 2496 log_info("HFP: ATA"); 2497 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 2498 break; 2499 case HFP_CMD_HANG_UP_CALL: 2500 hfp_connection->command = HFP_CMD_NONE; 2501 hfp_ag_queue_ok(hfp_connection); 2502 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 2503 break; 2504 case HFP_CMD_CALL_HOLD: { 2505 hfp_connection->command = HFP_CMD_NONE; 2506 hfp_ag_queue_ok(hfp_connection); 2507 2508 switch (hfp_connection->ag_call_hold_action){ 2509 case 0: 2510 // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 2511 hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 2512 break; 2513 case 1: 2514 // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 2515 // Where both a held and a waiting call exist, the above procedures shall apply to the 2516 // waiting call (i.e., not to the held call) in conflicting situation. 2517 hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2518 break; 2519 case 2: 2520 // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 2521 // Where both a held and a waiting call exist, the above procedures shall apply to the 2522 // waiting call (i.e., not to the held call) in conflicting situation. 2523 hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 2524 break; 2525 case 3: 2526 // Adds a held call to the conversation. 2527 hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 2528 break; 2529 case 4: 2530 // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 2531 hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 2532 break; 2533 default: 2534 break; 2535 } 2536 hfp_connection->call_index = 0; 2537 break; 2538 } 2539 case HFP_CMD_CALL_PHONE_NUMBER: 2540 hfp_connection->command = HFP_CMD_NONE; 2541 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, hfp_connection); 2542 break; 2543 case HFP_CMD_REDIAL_LAST_NUMBER: 2544 hfp_connection->command = HFP_CMD_NONE; 2545 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 2546 break; 2547 case HFP_CMD_ENABLE_CLIP: 2548 hfp_connection->command = HFP_CMD_NONE; 2549 log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 2550 hfp_ag_queue_ok(hfp_connection); 2551 break; 2552 case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 2553 hfp_connection->command = HFP_CMD_NONE; 2554 log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 2555 hfp_ag_queue_ok(hfp_connection); 2556 break; 2557 case HFP_CMD_SET_SPEAKER_GAIN: 2558 hfp_connection->command = HFP_CMD_NONE; 2559 hfp_ag_queue_ok(hfp_connection); 2560 log_info("HF speaker gain = %u", hfp_connection->speaker_gain); 2561 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_connection->speaker_gain); 2562 break; 2563 case HFP_CMD_SET_MICROPHONE_GAIN: 2564 hfp_connection->command = HFP_CMD_NONE; 2565 hfp_ag_queue_ok(hfp_connection); 2566 log_info("HF microphone gain = %u", hfp_connection->microphone_gain); 2567 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_connection->microphone_gain); 2568 break; 2569 case HFP_CMD_CUSTOM_MESSAGE: 2570 hfp_connection->command = HFP_CMD_NONE; 2571 hfp_parser_reset_line_buffer(hfp_connection); 2572 log_info("Custom AT Command ID 0x%04x", hfp_connection->custom_at_command_id); 2573 hfp_ag_emit_custom_command_event(hfp_connection); 2574 break; 2575 case HFP_CMD_UNKNOWN: 2576 hfp_connection->command = HFP_CMD_NONE; 2577 hfp_ag_queue_error(hfp_connection); 2578 break; 2579 default: 2580 break; 2581 } 2582 } 2583 } 2584 2585 static void hfp_ag_run(void){ 2586 btstack_linked_list_iterator_t it; 2587 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2588 while (btstack_linked_list_iterator_has_next(&it)){ 2589 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2590 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2591 hfp_ag_run_for_context(hfp_connection); 2592 } 2593 } 2594 2595 static void hfp_ag_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2596 hfp_connection_t * hfp_connection = NULL; 2597 switch (packet_type){ 2598 case RFCOMM_DATA_PACKET: 2599 hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 2600 btstack_assert(hfp_connection != NULL); 2601 2602 hfp_ag_handle_rfcomm_data(hfp_connection, packet, size); 2603 hfp_ag_run_for_context(hfp_connection); 2604 return; 2605 case HCI_EVENT_PACKET: 2606 if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 2607 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 2608 hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid); 2609 btstack_assert(hfp_connection != NULL); 2610 2611 hfp_ag_run_for_context(hfp_connection); 2612 return; 2613 } 2614 hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2615 break; 2616 default: 2617 break; 2618 } 2619 2620 hfp_ag_run(); 2621 } 2622 2623 static void hfp_ag_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2624 hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2625 hfp_ag_run(); 2626 } 2627 2628 void hfp_ag_init_codecs(uint8_t codecs_nr, const uint8_t * codecs){ 2629 btstack_assert(codecs_nr <= HFP_MAX_NUM_CODECS); 2630 if (codecs_nr > HFP_MAX_NUM_CODECS) return; 2631 2632 hfp_ag_codecs_nr = codecs_nr; 2633 uint8_t i; 2634 for (i=0; i < codecs_nr; i++){ 2635 hfp_ag_codecs[i] = codecs[i]; 2636 } 2637 } 2638 2639 void hfp_ag_init_supported_features(uint32_t supported_features){ 2640 hfp_ag_supported_features = supported_features; 2641 hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone(); 2642 } 2643 2644 void hfp_ag_init_ag_indicators(int ag_indicators_nr, const hfp_ag_indicator_t * ag_indicators){ 2645 btstack_assert(ag_indicators_nr <= HFP_MAX_NUM_INDICATORS); 2646 if (ag_indicators_nr > HFP_MAX_NUM_CODECS) return; 2647 2648 hfp_ag_indicators_nr = ag_indicators_nr; 2649 (void)memcpy(hfp_ag_indicators, ag_indicators, 2650 ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2651 } 2652 2653 void hfp_ag_init_hf_indicators(int hf_indicators_nr, const hfp_generic_status_indicator_t * hf_indicators){ 2654 btstack_assert(hf_indicators_nr <= HFP_MAX_NUM_INDICATORS); 2655 if (hf_indicators_nr > HFP_MAX_NUM_CODECS) return; 2656 2657 hfp_ag_generic_status_indicators_nr = hf_indicators_nr; 2658 (void)memcpy(hfp_ag_generic_status_indicators, hf_indicators, 2659 hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2660 } 2661 2662 void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 2663 hfp_ag_call_hold_services_nr = call_hold_services_nr; 2664 (void)memcpy(hfp_ag_call_hold_services, call_hold_services, 2665 call_hold_services_nr * sizeof(char *)); 2666 } 2667 2668 2669 void hfp_ag_init(uint8_t rfcomm_channel_nr){ 2670 2671 hfp_init(); 2672 hfp_ag_call_hold_services_nr = 0; 2673 hfp_ag_response_and_hold_active = false; 2674 hfp_ag_indicators_nr = 0; 2675 hfp_ag_codecs_nr = 0; 2676 hfp_ag_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES; 2677 hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone(); 2678 hfp_ag_subscriber_numbers = NULL; 2679 hfp_ag_subscriber_numbers_count = 0; 2680 2681 hfp_ag_hci_event_callback_registration.callback = &hfp_ag_hci_event_packet_handler; 2682 hci_add_event_handler(&hfp_ag_hci_event_callback_registration); 2683 2684 rfcomm_register_service(&hfp_ag_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 2685 2686 // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 2687 hfp_set_ag_rfcomm_packet_handler(&hfp_ag_rfcomm_packet_handler); 2688 2689 hfp_gsm_init(); 2690 } 2691 2692 void hfp_ag_deinit(void){ 2693 hfp_deinit(); 2694 hfp_gsm_deinit(); 2695 2696 hfp_ag_callback = NULL; 2697 hfp_ag_supported_features = 0; 2698 hfp_ag_codecs_nr = 0; 2699 hfp_ag_indicators_nr = 0; 2700 hfp_ag_call_hold_services_nr = 0; 2701 (void) memset(&hfp_ag_call_hold_services, 0, sizeof(hfp_ag_call_hold_services)); 2702 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 2703 (void) memset(&hfp_ag_response_and_hold_state, 0, sizeof(hfp_response_and_hold_state_t)); 2704 hfp_ag_subscriber_numbers = NULL; 2705 (void) memset(&hfp_ag_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t)); 2706 hfp_ag_custom_call_sm_handler = NULL; 2707 } 2708 2709 uint8_t hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 2710 return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); 2711 } 2712 2713 uint8_t hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ 2714 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2715 if (!hfp_connection){ 2716 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2717 } 2718 2719 hfp_trigger_release_service_level_connection(hfp_connection); 2720 hfp_ag_run_for_context(hfp_connection); 2721 return ERROR_CODE_SUCCESS; 2722 } 2723 2724 uint8_t hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){ 2725 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2726 if (!hfp_connection){ 2727 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2728 } 2729 2730 if (!hfp_connection->enable_extended_audio_gateway_error_report){ 2731 return ERROR_CODE_COMMAND_DISALLOWED; 2732 } 2733 2734 hfp_connection->extended_audio_gateway_error = error; 2735 hfp_ag_run_for_context(hfp_connection); 2736 return ERROR_CODE_SUCCESS; 2737 } 2738 2739 static uint8_t hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){ 2740 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){ 2741 return ERROR_CODE_COMMAND_DISALLOWED; 2742 } 2743 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){ 2744 return ERROR_CODE_COMMAND_DISALLOWED; 2745 } 2746 2747 hfp_connection->establish_audio_connection = 1; 2748 hfp_connection->sco_for_msbc_failed = 0; 2749 if (!has_codec_negotiation_feature(hfp_connection)){ 2750 log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD"); 2751 hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 2752 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 2753 // now, pick link settings 2754 hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 2755 #ifdef ENABLE_CC256X_ASSISTED_HFP 2756 hfp_cc256x_prepare_for_sco(hfp_connection); 2757 #endif 2758 return ERROR_CODE_SUCCESS; 2759 } 2760 2761 uint8_t i; 2762 bool codec_was_in_use = false; 2763 bool better_codec_can_be_used = false; 2764 2765 for (i = 0; i<hfp_connection->remote_codecs_nr; i++){ 2766 if (hfp_connection->negotiated_codec == hfp_connection->remote_codecs[i]){ 2767 codec_was_in_use = true; 2768 } else if (hfp_connection->negotiated_codec < hfp_connection->remote_codecs[i]){ 2769 better_codec_can_be_used = true; 2770 } 2771 } 2772 2773 if (!codec_was_in_use || better_codec_can_be_used){ 2774 hfp_connection->ag_send_common_codec = true; 2775 hfp_connection->codecs_state = HFP_CODECS_IDLE; 2776 } 2777 return ERROR_CODE_SUCCESS; 2778 } 2779 2780 uint8_t hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){ 2781 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2782 if (!hfp_connection){ 2783 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2784 } 2785 uint8_t status = hfp_ag_setup_audio_connection(hfp_connection); 2786 if (status != ERROR_CODE_SUCCESS){ 2787 return status; 2788 } 2789 hfp_ag_run_for_context(hfp_connection); 2790 return ERROR_CODE_SUCCESS; 2791 } 2792 2793 uint8_t hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){ 2794 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2795 if (!hfp_connection){ 2796 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2797 } 2798 2799 if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){ 2800 return ERROR_CODE_COMMAND_DISALLOWED; 2801 } 2802 2803 uint8_t status = hfp_trigger_release_audio_connection(hfp_connection); 2804 if (status == ERROR_CODE_SUCCESS){ 2805 hfp_ag_run_for_context(hfp_connection); 2806 } 2807 return status; 2808 } 2809 2810 /** 2811 * @brief Enable in-band ring tone 2812 */ 2813 void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 2814 if (has_in_band_ring_tone() == false){ 2815 return; 2816 } 2817 if (hfp_ag_in_band_ring_tone_active == use_in_band_ring_tone){ 2818 return; 2819 } 2820 2821 hfp_ag_in_band_ring_tone_active = use_in_band_ring_tone; 2822 2823 btstack_linked_list_iterator_t it; 2824 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2825 while (btstack_linked_list_iterator_has_next(&it)){ 2826 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2827 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2828 hfp_connection->ag_send_in_band_ring_tone_setting = true; 2829 hfp_ag_run_for_context(hfp_connection); 2830 } 2831 } 2832 2833 /** 2834 * @brief Called from GSM 2835 */ 2836 void hfp_ag_incoming_call(void){ 2837 hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 2838 } 2839 2840 void hfp_ag_outgoing_call_initiated(void) { 2841 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, NULL); 2842 } 2843 2844 /** 2845 * @brief number is stored. 2846 */ 2847 void hfp_ag_set_clip(uint8_t type, const char * number){ 2848 hfp_gsm_handler(HFP_AG_SET_CLIP, 0, type, number); 2849 } 2850 2851 void hfp_ag_call_dropped(void){ 2852 hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2853 } 2854 2855 // call from AG UI 2856 void hfp_ag_answer_incoming_call(void){ 2857 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 2858 } 2859 2860 void hfp_ag_join_held_call(void){ 2861 hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 2862 } 2863 2864 void hfp_ag_terminate_call(void){ 2865 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 2866 } 2867 2868 void hfp_ag_outgoing_call_ringing(void){ 2869 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 2870 } 2871 2872 void hfp_ag_outgoing_call_established(void){ 2873 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 2874 } 2875 2876 void hfp_ag_outgoing_call_rejected(void){ 2877 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 2878 } 2879 2880 void hfp_ag_outgoing_call_accepted(void){ 2881 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 2882 } 2883 2884 void hfp_ag_hold_incoming_call(void){ 2885 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 2886 } 2887 2888 void hfp_ag_accept_held_incoming_call(void) { 2889 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 2890 } 2891 2892 void hfp_ag_reject_held_incoming_call(void){ 2893 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 2894 } 2895 2896 static void hfp_ag_set_ag_indicator(const char * name, int value){ 2897 int indicator_index = get_ag_indicator_index_for_name(name); 2898 if (indicator_index < 0) return; 2899 hfp_ag_indicators[indicator_index].status = value; 2900 2901 btstack_linked_list_iterator_t it; 2902 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2903 while (btstack_linked_list_iterator_has_next(&it)){ 2904 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2905 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2906 if (!hfp_connection->ag_indicators[indicator_index].enabled) { 2907 log_info("Requested AG indicator '%s' update to %u, but it is not enabled", hfp_ag_indicators[indicator_index].name, value); 2908 continue; 2909 } 2910 log_info("AG indicator '%s' changed to %u, request transfer status", hfp_ag_indicators[indicator_index].name, value); 2911 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 2912 hfp_ag_run_for_context(hfp_connection); 2913 } 2914 } 2915 2916 uint8_t hfp_ag_set_registration_status(int registration_status){ 2917 if ((registration_status < 0) || (registration_status > 1)){ 2918 return ERROR_CODE_COMMAND_DISALLOWED; 2919 } 2920 2921 if ( (registration_status == 0) && (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 2922 2923 // if network goes away wihle a call is active: 2924 // - the call gets dropped 2925 // - we send NO CARRIER 2926 // NOTE: the CALL=0 has to be sent before NO CARRIER 2927 2928 hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2929 2930 btstack_linked_list_iterator_t it; 2931 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2932 while (btstack_linked_list_iterator_has_next(&it)){ 2933 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2934 hfp_connection->ag_send_no_carrier = true; 2935 } 2936 } 2937 hfp_ag_set_ag_indicator("service", registration_status); 2938 return ERROR_CODE_SUCCESS; 2939 } 2940 2941 uint8_t hfp_ag_set_signal_strength(int signal_strength){ 2942 if ((signal_strength < 0) || (signal_strength > 5)){ 2943 return ERROR_CODE_COMMAND_DISALLOWED; 2944 } 2945 2946 hfp_ag_set_ag_indicator("signal", signal_strength); 2947 return ERROR_CODE_SUCCESS; 2948 } 2949 2950 uint8_t hfp_ag_set_roaming_status(int roaming_status){ 2951 if ((roaming_status < 0) || (roaming_status > 1)){ 2952 return ERROR_CODE_COMMAND_DISALLOWED; 2953 } 2954 2955 hfp_ag_set_ag_indicator("roam", roaming_status); 2956 return ERROR_CODE_SUCCESS; 2957 } 2958 2959 uint8_t hfp_ag_set_battery_level(int battery_level){ 2960 if ((battery_level < 0) || (battery_level > 5)){ 2961 return ERROR_CODE_COMMAND_DISALLOWED; 2962 } 2963 hfp_ag_set_ag_indicator("battchg", battery_level); 2964 return ERROR_CODE_SUCCESS; 2965 } 2966 2967 uint8_t hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle){ 2968 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2969 if (!hfp_connection){ 2970 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2971 } 2972 2973 if (hfp_connection->emit_vra_enabled_after_audio_established){ 2974 return ERROR_CODE_COMMAND_DISALLOWED; 2975 } 2976 2977 bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 2978 bool legacy_vra_supported = hfp_ag_vra_flag_supported(hfp_connection); 2979 if (!enhanced_vra_supported && !legacy_vra_supported){ 2980 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 2981 } 2982 2983 if (!hfp_ag_can_activate_voice_recognition(hfp_connection)){ 2984 return ERROR_CODE_COMMAND_DISALLOWED; 2985 } 2986 2987 hfp_connection->ag_activate_voice_recognition_value = 1; 2988 hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED; 2989 hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported; 2990 hfp_connection->ag_audio_connection_opened_before_vra = hfp_ag_is_audio_connection_active(hfp_connection); 2991 hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 2992 hfp_connection->ag_vra_send_command = true; 2993 hfp_ag_run_for_context(hfp_connection); 2994 return ERROR_CODE_SUCCESS; 2995 } 2996 2997 uint8_t hfp_ag_deactivate_voice_recognition(hci_con_handle_t acl_handle){ 2998 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2999 if (!hfp_connection){ 3000 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3001 } 3002 3003 if (hfp_connection->emit_vra_enabled_after_audio_established){ 3004 return ERROR_CODE_COMMAND_DISALLOWED; 3005 } 3006 3007 bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 3008 bool legacy_vra_supported = hfp_ag_vra_flag_supported(hfp_connection); 3009 3010 if (!enhanced_vra_supported && !legacy_vra_supported){ 3011 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 3012 } 3013 3014 if (!hfp_ag_voice_recognition_session_active(hfp_connection)){ 3015 return ERROR_CODE_COMMAND_DISALLOWED; 3016 } 3017 3018 hfp_connection->ag_activate_voice_recognition_value = 0; 3019 hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF; 3020 hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY; 3021 hfp_connection->ag_vra_send_command = true; 3022 hfp_ag_run_for_context(hfp_connection); 3023 return ERROR_CODE_SUCCESS; 3024 } 3025 3026 static uint8_t hfp_ag_enhanced_voice_recognition_send_state(hci_con_handle_t acl_handle, hfp_voice_recognition_state_t state){ 3027 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3028 if (!hfp_connection){ 3029 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3030 } 3031 3032 if (hfp_connection->emit_vra_enabled_after_audio_established){ 3033 return ERROR_CODE_COMMAND_DISALLOWED; 3034 } 3035 3036 if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 3037 return ERROR_CODE_COMMAND_DISALLOWED; 3038 } 3039 3040 if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 3041 return ERROR_CODE_COMMAND_DISALLOWED; 3042 } 3043 3044 bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 3045 if (!enhanced_vra_supported ){ 3046 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 3047 } 3048 3049 3050 hfp_connection->ag_vra_state = state; 3051 hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS; 3052 hfp_connection->ag_vra_send_command = true; 3053 hfp_ag_run_for_context(hfp_connection); 3054 return ERROR_CODE_SUCCESS; 3055 } 3056 3057 uint8_t hfp_ag_enhanced_voice_recognition_report_sending_audio(hci_con_handle_t acl_handle){ 3058 return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND); 3059 } 3060 uint8_t hfp_ag_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){ 3061 return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT); 3062 } 3063 uint8_t hfp_ag_enhanced_voice_recognition_report_processing_input(hci_con_handle_t acl_handle){ 3064 return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT); 3065 } 3066 3067 3068 uint8_t hfp_ag_enhanced_voice_recognition_send_message(hci_con_handle_t acl_handle, hfp_voice_recognition_state_t state, hfp_voice_recognition_message_t msg){ 3069 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3070 if (!hfp_connection){ 3071 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3072 } 3073 3074 if (hfp_connection->emit_vra_enabled_after_audio_established){ 3075 return ERROR_CODE_COMMAND_DISALLOWED; 3076 } 3077 3078 if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){ 3079 return ERROR_CODE_COMMAND_DISALLOWED; 3080 } 3081 3082 if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){ 3083 return ERROR_CODE_COMMAND_DISALLOWED; 3084 } 3085 3086 bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection); 3087 if (!enhanced_vra_supported ){ 3088 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 3089 } 3090 3091 bool enhanced_vra_msg_supported = hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection); 3092 if (!enhanced_vra_msg_supported ){ 3093 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 3094 } 3095 3096 uint16_t message_len = (uint16_t) strlen(msg.text); 3097 3098 if (message_len > HFP_MAX_VR_TEXT_SIZE){ 3099 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 3100 } 3101 3102 if ((HFP_VR_TEXT_HEADER_SIZE + message_len) > hfp_connection->rfcomm_mtu){ 3103 return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 3104 } 3105 3106 hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG; 3107 hfp_connection->ag_msg = msg; 3108 hfp_connection->ag_vra_state = state; 3109 hfp_connection->ag_vra_send_command = true; 3110 hfp_ag_run_for_context(hfp_connection); 3111 3112 return ERROR_CODE_SUCCESS; 3113 } 3114 3115 3116 uint8_t hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 3117 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3118 if (!hfp_connection){ 3119 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3120 } 3121 3122 if ((gain < 0) || (gain > 15)){ 3123 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 3124 return ERROR_CODE_COMMAND_DISALLOWED; 3125 } 3126 3127 if (hfp_connection->microphone_gain != gain){ 3128 hfp_connection->microphone_gain = gain; 3129 hfp_connection->send_microphone_gain = 1; 3130 } 3131 hfp_ag_run_for_context(hfp_connection); 3132 return ERROR_CODE_SUCCESS; 3133 } 3134 3135 uint8_t hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 3136 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3137 if (!hfp_connection){ 3138 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3139 } 3140 3141 if ((gain < 0) || (gain > 15)){ 3142 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 3143 return ERROR_CODE_COMMAND_DISALLOWED; 3144 } 3145 3146 if (hfp_connection->speaker_gain != gain){ 3147 hfp_connection->speaker_gain = gain; 3148 hfp_connection->send_speaker_gain = 1; 3149 } 3150 hfp_ag_run_for_context(hfp_connection); 3151 return ERROR_CODE_SUCCESS; 3152 } 3153 3154 uint8_t hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){ 3155 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3156 if (!hfp_connection){ 3157 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3158 } 3159 3160 hfp_ag_set_clip(0, number); 3161 hfp_connection->send_phone_number_for_voice_tag = 1; 3162 return ERROR_CODE_SUCCESS; 3163 } 3164 3165 uint8_t hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 3166 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3167 if (!hfp_connection){ 3168 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3169 } 3170 3171 hfp_ag_queue_error(hfp_connection); 3172 return ERROR_CODE_SUCCESS; 3173 } 3174 3175 uint8_t hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){ 3176 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3177 if (!hfp_connection){ 3178 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3179 } 3180 3181 hfp_ag_queue_ok(hfp_connection); 3182 return ERROR_CODE_SUCCESS; 3183 } 3184 3185 uint8_t hfp_ag_send_unsolicited_result_code(hci_con_handle_t acl_handle, const char * unsolicited_result_code){ 3186 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3187 if (!hfp_connection){ 3188 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3189 } 3190 if (hfp_connection->send_custom_message != NULL){ 3191 return ERROR_CODE_COMMAND_DISALLOWED; 3192 } 3193 hfp_connection->send_custom_message = unsolicited_result_code; 3194 hfp_ag_run_for_context(hfp_connection); 3195 return ERROR_CODE_SUCCESS; 3196 } 3197 3198 uint8_t hfp_ag_send_command_result_code(hci_con_handle_t acl_handle, bool ok){ 3199 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3200 if (!hfp_connection){ 3201 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3202 } 3203 if (ok){ 3204 hfp_ag_queue_ok(hfp_connection); 3205 } else { 3206 hfp_ag_queue_error(hfp_connection); 3207 } 3208 hfp_ag_run_for_context(hfp_connection); 3209 return ERROR_CODE_SUCCESS; 3210 } 3211 3212 void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 3213 hfp_ag_subscriber_numbers = numbers; 3214 hfp_ag_subscriber_numbers_count = numbers_count; 3215 } 3216 3217 void hfp_ag_clear_last_dialed_number(void){ 3218 hfp_gsm_clear_last_dialed_number(); 3219 } 3220 3221 void hfp_ag_set_last_dialed_number(const char * number){ 3222 hfp_gsm_set_last_dialed_number(number); 3223 } 3224 3225 uint8_t hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){ 3226 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 3227 if (!hfp_connection){ 3228 return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 3229 } 3230 3231 if (!hfp_connection->call_waiting_notification_enabled){ 3232 return ERROR_CODE_COMMAND_DISALLOWED; 3233 } 3234 3235 hfp_connection->ag_notify_incoming_call_waiting = 1; 3236 hfp_ag_run_for_context(hfp_connection); 3237 return ERROR_CODE_SUCCESS; 3238 } 3239 void hfp_ag_create_sdp_record_with_codecs(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, 3240 const char * name, uint8_t ability_to_reject_call, uint16_t supported_features, 3241 uint8_t codecs_nr, const uint8_t * codecs){ 3242 if (!name){ 3243 name = hfp_ag_default_service_name; 3244 } 3245 hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name); 3246 3247 /* 3248 * 0x01 – Ability to reject a call 3249 * 0x00 – No ability to reject a call 3250 */ 3251 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 3252 de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 3253 3254 // Construct SupportedFeatures for SDP bitmap: 3255 // 3256 // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 3257 // of the Bits 0 to 4 of the unsolicited result code +BRSF" 3258 // 3259 // Wide band speech (bit 5) and LC3-SWB (bit 8) require Codec negotiation 3260 // 3261 uint16_t sdp_features = supported_features & 0x1f; 3262 3263 if (supported_features & (1 << HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS)){ 3264 sdp_features |= 1 << 6; 3265 } 3266 3267 if (supported_features & (1 << HFP_AGSF_VOICE_RECOGNITION_TEXT)){ 3268 sdp_features |= 1 << 7; 3269 } 3270 3271 // codecs 3272 if ((supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION)) != 0){ 3273 uint8_t i; 3274 for (i=0;i<codecs_nr;i++){ 3275 switch (codecs[i]){ 3276 case HFP_CODEC_MSBC: 3277 sdp_features |= 1 << 5; 3278 break; 3279 case HFP_CODEC_LC3_SWB: 3280 sdp_features |= 1 << 8; 3281 break; 3282 default: 3283 break; 3284 } 3285 } 3286 } 3287 3288 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 3289 de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 3290 } 3291 3292 void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, 3293 uint8_t ability_to_reject_call, uint16_t supported_features, int wide_band_speech){ 3294 uint8_t codecs_nr; 3295 const uint8_t * codecs; 3296 const uint8_t wide_band_codecs[] = { HFP_CODEC_MSBC }; 3297 if (wide_band_speech == 0){ 3298 codecs_nr = 0; 3299 codecs = NULL; 3300 } else { 3301 codecs_nr = 1; 3302 codecs = wide_band_codecs; 3303 } 3304 hfp_ag_create_sdp_record_with_codecs(service, service_record_handle, rfcomm_channel_nr, name, 3305 ability_to_reject_call, supported_features, codecs_nr, codecs); 3306 } 3307 3308 void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){ 3309 btstack_assert(callback != NULL); 3310 3311 hfp_ag_callback = callback; 3312 hfp_set_ag_callback(callback); 3313 } 3314 3315 void hfp_ag_register_custom_call_sm_handler(bool (*handler)(hfp_ag_call_event_t event)){ 3316 hfp_ag_custom_call_sm_handler = handler; 3317 } 3318 3319 void hfp_ag_register_custom_at_command(hfp_custom_at_command_t * custom_at_command){ 3320 hfp_register_custom_ag_command(custom_at_command); 3321 } 3322