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