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