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 } 1133 break; 1134 case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG: 1135 switch (hfp_gsm_call_status()){ 1136 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1137 switch (hfp_gsm_callsetup_status()){ 1138 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1139 hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG); 1140 hfp_ag_set_call_indicator(); 1141 hfp_ag_set_callsetup_indicator(); 1142 hfp_ag_ag_accept_call(); 1143 log_info("AG answers call, accept call by GSM"); 1144 break; 1145 default: 1146 break; 1147 } 1148 break; 1149 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1150 switch (hfp_gsm_callsetup_status()){ 1151 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1152 log_info("AG: current call is placed on hold, incoming call gets active"); 1153 hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG); 1154 hfp_ag_set_callsetup_indicator(); 1155 hfp_ag_set_callheld_indicator(); 1156 hfp_ag_transfer_callsetup_state(); 1157 hfp_ag_transfer_callheld_state(); 1158 break; 1159 default: 1160 break; 1161 } 1162 break; 1163 } 1164 break; 1165 1166 case HFP_AG_HELD_CALL_JOINED_BY_AG: 1167 switch (hfp_gsm_call_status()){ 1168 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1169 switch (hfp_gsm_callheld_status()){ 1170 case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED: 1171 log_info("AG: joining held call with active call"); 1172 hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG); 1173 hfp_ag_set_callheld_indicator(); 1174 hfp_ag_transfer_callheld_state(); 1175 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1176 break; 1177 default: 1178 break; 1179 } 1180 break; 1181 default: 1182 break; 1183 } 1184 break; 1185 1186 case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF: 1187 switch (hfp_gsm_call_status()){ 1188 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1189 switch (hfp_gsm_callsetup_status()){ 1190 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1191 hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF); 1192 hfp_ag_set_callsetup_indicator(); 1193 hfp_ag_set_call_indicator(); 1194 hfp_ag_hf_accept_call(hfp_connection); 1195 hfp_connection->ok_pending = 1; 1196 log_info("HF answers call, accept call by GSM"); 1197 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED); 1198 break; 1199 default: 1200 break; 1201 } 1202 break; 1203 default: 1204 break; 1205 } 1206 break; 1207 1208 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG: 1209 switch (hfp_gsm_call_status()){ 1210 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1211 switch (hfp_gsm_callsetup_status()){ 1212 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1213 hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG); 1214 hfp_ag_response_and_hold_active = 1; 1215 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1216 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1217 // as with regualr call 1218 hfp_ag_set_call_indicator(); 1219 hfp_ag_set_callsetup_indicator(); 1220 hfp_ag_ag_accept_call(); 1221 log_info("AG response and hold - hold by AG"); 1222 break; 1223 default: 1224 break; 1225 } 1226 break; 1227 default: 1228 break; 1229 } 1230 break; 1231 1232 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF: 1233 switch (hfp_gsm_call_status()){ 1234 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1235 switch (hfp_gsm_callsetup_status()){ 1236 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1237 hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF); 1238 hfp_ag_response_and_hold_active = 1; 1239 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD; 1240 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1241 // as with regualr call 1242 hfp_ag_set_call_indicator(); 1243 hfp_ag_set_callsetup_indicator(); 1244 hfp_ag_hf_accept_call(hfp_connection); 1245 log_info("AG response and hold - hold by HF"); 1246 break; 1247 default: 1248 break; 1249 } 1250 break; 1251 default: 1252 break; 1253 } 1254 break; 1255 1256 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG: 1257 case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF: 1258 if (!hfp_ag_response_and_hold_active) break; 1259 if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1260 hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG); 1261 hfp_ag_response_and_hold_active = 0; 1262 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED; 1263 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1264 log_info("Held Call accepted and active"); 1265 break; 1266 1267 case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG: 1268 case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF: 1269 if (!hfp_ag_response_and_hold_active) break; 1270 if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break; 1271 hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG); 1272 hfp_ag_response_and_hold_active = 0; 1273 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1274 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1275 // from terminate by ag 1276 hfp_ag_set_call_indicator(); 1277 hfp_ag_trigger_terminate_call(); 1278 break; 1279 1280 case HFP_AG_TERMINATE_CALL_BY_HF: 1281 switch (hfp_gsm_call_status()){ 1282 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1283 switch (hfp_gsm_callsetup_status()){ 1284 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1285 hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1286 hfp_ag_set_callsetup_indicator(); 1287 hfp_ag_transfer_callsetup_state(); 1288 hfp_ag_trigger_reject_call(); 1289 log_info("HF Rejected Incoming call, AG terminate call"); 1290 break; 1291 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1292 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1293 hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1294 hfp_ag_set_callsetup_indicator(); 1295 hfp_ag_transfer_callsetup_state(); 1296 log_info("AG terminate outgoing call process"); 1297 break; 1298 default: 1299 break; 1300 } 1301 break; 1302 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1303 hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF); 1304 hfp_ag_set_call_indicator(); 1305 hfp_ag_transfer_call_state(); 1306 hfp_connection->call_state = HFP_CALL_IDLE; 1307 log_info("AG terminate call"); 1308 break; 1309 } 1310 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED); 1311 break; 1312 1313 case HFP_AG_TERMINATE_CALL_BY_AG: 1314 switch (hfp_gsm_call_status()){ 1315 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1316 switch (hfp_gsm_callsetup_status()){ 1317 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1318 hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG); 1319 hfp_ag_set_callsetup_indicator(); 1320 hfp_ag_trigger_reject_call(); 1321 log_info("AG Rejected Incoming call, AG terminate call"); 1322 break; 1323 default: 1324 break; 1325 } 1326 break; 1327 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1328 hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG); 1329 hfp_ag_set_callsetup_indicator(); 1330 hfp_ag_set_call_indicator(); 1331 hfp_ag_trigger_terminate_call(); 1332 log_info("AG terminate call"); 1333 break; 1334 default: 1335 break; 1336 } 1337 break; 1338 case HFP_AG_CALL_DROPPED: 1339 switch (hfp_gsm_call_status()){ 1340 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS: 1341 switch (hfp_gsm_callsetup_status()){ 1342 case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: 1343 hfp_ag_stop_ringing(); 1344 log_info("Incoming call interrupted"); 1345 break; 1346 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE: 1347 case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: 1348 log_info("Outgoing call interrupted\n"); 1349 log_info("AG notify call dropped\n"); 1350 break; 1351 default: 1352 break; 1353 } 1354 hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1355 hfp_ag_set_callsetup_indicator(); 1356 hfp_ag_transfer_callsetup_state(); 1357 hfp_ag_trigger_terminate_call(); 1358 break; 1359 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: 1360 if (hfp_ag_response_and_hold_active) { 1361 hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1362 hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED; 1363 hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state); 1364 } 1365 hfp_gsm_handle_event(HFP_AG_CALL_DROPPED); 1366 hfp_ag_set_callsetup_indicator(); 1367 hfp_ag_set_call_indicator(); 1368 hfp_ag_trigger_terminate_call(); 1369 log_info("AG notify call dropped"); 1370 break; 1371 default: 1372 break; 1373 } 1374 break; 1375 1376 case HFP_AG_OUTGOING_CALL_INITIATED: 1377 // directly reject call if number of free slots is exceeded 1378 if (!hfp_gsm_call_possible()){ 1379 hfp_connection->send_error = 1; 1380 hfp_ag_run_for_context(hfp_connection); 1381 break; 1382 } 1383 hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]); 1384 1385 hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1386 1387 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]); 1388 break; 1389 1390 case HFP_AG_OUTGOING_REDIAL_INITIATED:{ 1391 // directly reject call if number of free slots is exceeded 1392 if (!hfp_gsm_call_possible()){ 1393 hfp_connection->send_error = 1; 1394 hfp_ag_run_for_context(hfp_connection); 1395 break; 1396 } 1397 1398 hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED); 1399 hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED; 1400 1401 log_info("Redial last number"); 1402 char * last_dialed_number = hfp_gsm_last_dialed_number(); 1403 1404 if (strlen(last_dialed_number) > 0){ 1405 log_info("Last number exists: accept call"); 1406 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number); 1407 } else { 1408 log_info("log_infoLast number missing: reject call"); 1409 hfp_ag_handle_reject_incoming_call(); 1410 } 1411 break; 1412 } 1413 case HFP_AG_OUTGOING_CALL_REJECTED: 1414 hfp_ag_handle_reject_incoming_call(); 1415 break; 1416 1417 case HFP_AG_OUTGOING_CALL_ACCEPTED:{ 1418 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED); 1419 if (!hfp_connection){ 1420 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state"); 1421 break; 1422 } 1423 1424 hfp_connection->ok_pending = 1; 1425 hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING; 1426 1427 // trigger callsetup to be 1428 int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT; 1429 hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED); 1430 1431 hfp_ag_set_callsetup_indicator(); 1432 indicator_index = get_ag_indicator_index_for_name("callsetup"); 1433 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 1434 1435 // put current call on hold if active 1436 if (put_call_on_hold){ 1437 log_info("AG putting current call on hold for new outgoing calllog_info"); 1438 hfp_ag_set_callheld_indicator(); 1439 indicator_index = get_ag_indicator_index_for_name("callheld"); 1440 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]); 1441 } 1442 1443 // start audio if needed 1444 hfp_ag_establish_audio_connection(hfp_connection->acl_handle); 1445 break; 1446 } 1447 case HFP_AG_OUTGOING_CALL_RINGING: 1448 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1449 if (!hfp_connection){ 1450 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state"); 1451 break; 1452 } 1453 1454 hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING); 1455 hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING; 1456 hfp_ag_set_callsetup_indicator(); 1457 hfp_ag_transfer_callsetup_state(); 1458 break; 1459 1460 case HFP_AG_OUTGOING_CALL_ESTABLISHED:{ 1461 // get outgoing call 1462 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING); 1463 if (!hfp_connection){ 1464 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING); 1465 } 1466 if (!hfp_connection){ 1467 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection"); 1468 break; 1469 } 1470 1471 int CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS = hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS; 1472 hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED); 1473 hfp_connection->call_state = HFP_CALL_ACTIVE; 1474 hfp_ag_set_callsetup_indicator(); 1475 hfp_ag_set_call_indicator(); 1476 hfp_ag_transfer_call_state(); 1477 hfp_ag_transfer_callsetup_state(); 1478 if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){ 1479 hfp_ag_set_callheld_indicator(); 1480 hfp_ag_transfer_callheld_state(); 1481 } 1482 break; 1483 } 1484 1485 case HFP_AG_CALL_HOLD_USER_BUSY: 1486 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY); 1487 hfp_ag_set_callsetup_indicator(); 1488 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1489 hfp_connection->call_state = HFP_CALL_ACTIVE; 1490 log_info("AG: Call Waiting, User Busy"); 1491 break; 1492 1493 case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1494 int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1495 int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD; 1496 1497 // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1498 if (call_held || call_setup_in_progress){ 1499 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index); 1500 1501 } 1502 1503 if (call_setup_in_progress){ 1504 log_info("AG: Call Dropped, Accept new call"); 1505 hfp_ag_set_callsetup_indicator(); 1506 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1507 } else { 1508 log_info("AG: Call Dropped, Resume held call"); 1509 } 1510 if (call_held){ 1511 hfp_ag_set_callheld_indicator(); 1512 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1513 } 1514 1515 hfp_connection->call_state = HFP_CALL_ACTIVE; 1516 break; 1517 } 1518 1519 case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{ 1520 // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call. 1521 // only update if callsetup changed 1522 int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS; 1523 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index); 1524 1525 if (call_setup_in_progress){ 1526 log_info("AG: Call on Hold, Accept new call"); 1527 hfp_ag_set_callsetup_indicator(); 1528 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1); 1529 } else { 1530 log_info("AG: Swap calls"); 1531 } 1532 1533 hfp_ag_set_callheld_indicator(); 1534 // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED); 1535 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1536 hfp_connection->call_state = HFP_CALL_ACTIVE; 1537 break; 1538 } 1539 1540 case HFP_AG_CALL_HOLD_ADD_HELD_CALL: 1541 // Adds a held call to the conversation. 1542 if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){ 1543 log_info("AG: Join 3-way-call"); 1544 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL); 1545 hfp_ag_set_callheld_indicator(); 1546 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1547 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL); 1548 } 1549 hfp_connection->call_state = HFP_CALL_ACTIVE; 1550 break; 1551 case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS: 1552 // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer) 1553 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS); 1554 log_info("AG: Transfer call -> Connect two calls and disconnect"); 1555 hfp_ag_set_call_indicator(); 1556 hfp_ag_set_callheld_indicator(); 1557 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1); 1558 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1); 1559 hfp_connection->call_state = HFP_CALL_IDLE; 1560 break; 1561 1562 default: 1563 break; 1564 } 1565 1566 1567 } 1568 1569 1570 static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){ 1571 hfp_gsm_call_t * active_call = hfp_gsm_call(call_index); 1572 if (!active_call) return; 1573 1574 int idx = active_call->index; 1575 hfp_enhanced_call_dir_t dir = active_call->direction; 1576 hfp_enhanced_call_status_t status = active_call->enhanced_status; 1577 hfp_enhanced_call_mode_t mode = active_call->mode; 1578 hfp_enhanced_call_mpty_t mpty = active_call->mpty; 1579 uint8_t type = active_call->clip_type; 1580 char * number = active_call->clip_number; 1581 1582 char buffer[100]; 1583 // TODO: check length of a buffer, to fit the MTU 1584 int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty); 1585 if (number){ 1586 offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type); 1587 } 1588 snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 1589 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, 1590 mode, mpty, type, number); 1591 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 1592 } 1593 1594 1595 // sends pending command, returns if command was sent 1596 static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){ 1597 1598 if (hfp_connection->send_status_of_current_calls){ 1599 hfp_connection->ok_pending = 0; 1600 if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){ 1601 hfp_connection->next_call_index++; 1602 hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index); 1603 return 1; 1604 } else { 1605 // 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 1606 hfp_connection->next_call_index = 0; 1607 hfp_connection->ok_pending = 1; 1608 hfp_connection->send_status_of_current_calls = 0; 1609 } 1610 } 1611 1612 if (hfp_connection->ag_notify_incoming_call_waiting){ 1613 hfp_connection->ag_notify_incoming_call_waiting = 0; 1614 hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid); 1615 return 1; 1616 } 1617 1618 if (hfp_connection->command == HFP_CMD_UNKNOWN){ 1619 hfp_connection->ok_pending = 0; 1620 hfp_connection->send_error = 0; 1621 hfp_connection->command = HFP_CMD_NONE; 1622 hfp_ag_send_error(hfp_connection->rfcomm_cid); 1623 return 1; 1624 } 1625 1626 if (hfp_connection->send_error){ 1627 hfp_connection->send_error = 0; 1628 hfp_connection->command = HFP_CMD_NONE; 1629 hfp_ag_send_error(hfp_connection->rfcomm_cid); 1630 return 1; 1631 } 1632 1633 // note: before update AG indicators and ok_pending 1634 if (hfp_connection->send_response_and_hold_status){ 1635 int status = hfp_connection->send_response_and_hold_status - 1; 1636 hfp_connection->send_response_and_hold_status = 0; 1637 hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status); 1638 return 1; 1639 } 1640 1641 if (hfp_connection->ok_pending){ 1642 hfp_connection->ok_pending = 0; 1643 hfp_connection->command = HFP_CMD_NONE; 1644 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1645 return 1; 1646 } 1647 1648 // update AG indicators 1649 if (hfp_connection->ag_indicators_status_update_bitmap){ 1650 int i; 1651 for (i=0;i<hfp_connection->ag_indicators_nr;i++){ 1652 if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){ 1653 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0); 1654 if (!hfp_connection->enable_status_update_for_ag_indicators) { 1655 log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name); 1656 break; 1657 } 1658 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]); 1659 return 1; 1660 } 1661 } 1662 } 1663 1664 if (hfp_connection->ag_ring){ 1665 hfp_connection->ag_ring = 0; 1666 hfp_connection->command = HFP_CMD_NONE; 1667 hfp_ag_send_ring(hfp_connection->rfcomm_cid); 1668 return 1; 1669 } 1670 1671 if (hfp_connection->ag_send_clip){ 1672 hfp_connection->ag_send_clip = 0; 1673 hfp_connection->command = HFP_CMD_NONE; 1674 hfp_ag_send_clip(hfp_connection->rfcomm_cid); 1675 return 1; 1676 } 1677 1678 if (hfp_connection->send_phone_number_for_voice_tag){ 1679 hfp_connection->send_phone_number_for_voice_tag = 0; 1680 hfp_connection->command = HFP_CMD_NONE; 1681 hfp_connection->ok_pending = 1; 1682 hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid); 1683 return 1; 1684 } 1685 1686 if (hfp_connection->send_subscriber_number){ 1687 if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){ 1688 hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++]; 1689 hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number); 1690 } else { 1691 hfp_connection->send_subscriber_number = 0; 1692 hfp_connection->next_subscriber_number_to_send = 0; 1693 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1694 } 1695 hfp_connection->command = HFP_CMD_NONE; 1696 return 1; 1697 } 1698 1699 if (hfp_connection->send_microphone_gain){ 1700 hfp_connection->send_microphone_gain = 0; 1701 hfp_connection->command = HFP_CMD_NONE; 1702 hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 1703 return 1; 1704 } 1705 1706 if (hfp_connection->send_speaker_gain){ 1707 hfp_connection->send_speaker_gain = 0; 1708 hfp_connection->command = HFP_CMD_NONE; 1709 hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 1710 return 1; 1711 } 1712 1713 if (hfp_connection->send_ag_status_indicators){ 1714 hfp_connection->send_ag_status_indicators = 0; 1715 hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid); 1716 return 1; 1717 } 1718 1719 return 0; 1720 } 1721 1722 static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){ 1723 1724 btstack_assert(hfp_connection != NULL); 1725 btstack_assert(hfp_connection->local_role == HFP_ROLE_AG); 1726 1727 // during SDP query, RFCOMM CID is not set 1728 if (hfp_connection->rfcomm_cid == 0) return; 1729 1730 if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 1731 log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid); 1732 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1733 return; 1734 } 1735 1736 int cmd_sent = hfp_ag_send_commands(hfp_connection); 1737 1738 // trigger codec exchange if requested by app 1739 if (hfp_connection->trigger_codec_exchange){ 1740 log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state); 1741 } 1742 1743 if (hfp_connection->trigger_codec_exchange && (hfp_connection->command == HFP_CMD_NONE)){ 1744 switch (hfp_connection->codecs_state){ 1745 case HFP_CODECS_IDLE: 1746 case HFP_CODECS_RECEIVED_LIST: 1747 case HFP_CODECS_AG_RESEND_COMMON_CODEC: 1748 case HFP_CODECS_ERROR: 1749 hfp_connection->trigger_codec_exchange = 0; 1750 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC; 1751 break; 1752 default: 1753 break; 1754 } 1755 } 1756 1757 if (!cmd_sent){ 1758 cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection); 1759 } 1760 1761 if (!cmd_sent){ 1762 cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection); 1763 } 1764 1765 if (!cmd_sent){ 1766 cmd_sent = call_setup_state_machine(hfp_connection); 1767 } 1768 1769 if (!cmd_sent){ 1770 cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection); 1771 } 1772 1773 if ((hfp_connection->command == HFP_CMD_NONE) && !cmd_sent){ 1774 // log_info("hfp_connection->command == HFP_CMD_NONE"); 1775 switch(hfp_connection->state){ 1776 case HFP_W2_DISCONNECT_RFCOMM: 1777 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 1778 rfcomm_disconnect(hfp_connection->rfcomm_cid); 1779 break; 1780 default: 1781 break; 1782 } 1783 } 1784 1785 if (cmd_sent){ 1786 hfp_connection->command = HFP_CMD_NONE; 1787 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 1788 } 1789 } 1790 1791 static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){ 1792 int i; 1793 for (i=0;i< hfp_generic_status_indicators_nr;i++){ 1794 hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i]; 1795 if (indicator->uuid == number){ 1796 return indicator; 1797 } 1798 } 1799 return NULL; 1800 } 1801 1802 static int hfp_parser_is_end_of_line(uint8_t byte){ 1803 return (byte == '\n') || (byte == '\r'); 1804 } 1805 1806 static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1807 UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 1808 1809 // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1810 if (size < 1) return; 1811 1812 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1813 if (!hfp_connection) return; 1814 1815 hfp_log_rfcomm_message("HFP_AG_RX", packet, size); 1816 1817 // process messages byte-wise 1818 int pos; 1819 for (pos = 0; pos < size ; pos++){ 1820 hfp_parse(hfp_connection, packet[pos], 0); 1821 1822 // parse until end of line 1823 if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1824 1825 hfp_generic_status_indicator_t * indicator; 1826 switch(hfp_connection->command){ 1827 case HFP_CMD_RESPONSE_AND_HOLD_QUERY: 1828 if (hfp_ag_response_and_hold_active){ 1829 hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1; 1830 } 1831 hfp_connection->ok_pending = 1; 1832 break; 1833 case HFP_CMD_RESPONSE_AND_HOLD_COMMAND: 1834 switch(hfp_connection->ag_response_and_hold_action){ 1835 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD: 1836 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection); 1837 break; 1838 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED: 1839 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection); 1840 break; 1841 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED: 1842 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection); 1843 break; 1844 default: 1845 break; 1846 } 1847 hfp_connection->ok_pending = 1; 1848 break; 1849 case HFP_CMD_HF_INDICATOR_STATUS: 1850 hfp_connection->command = HFP_CMD_NONE; 1851 // find indicator by assigned number 1852 indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index); 1853 if (!indicator){ 1854 hfp_connection->send_error = 1; 1855 break; 1856 } 1857 switch (indicator->uuid){ 1858 case 1: // enhanced security 1859 if (hfp_connection->parser_indicator_value > 1) { 1860 hfp_connection->send_error = 1; 1861 return; 1862 } 1863 log_info("HF Indicator 'enhanced security' set to %u", hfp_connection->parser_indicator_value); 1864 break; 1865 case 2: // battery level 1866 if (hfp_connection->parser_indicator_value > 100){ 1867 hfp_connection->send_error = 1; 1868 return; 1869 } 1870 log_info("HF Indicator 'battery' set to %u", hfp_connection->parser_indicator_value); 1871 break; 1872 default: 1873 log_info("HF Indicator unknown set to %u", hfp_connection->parser_indicator_value); 1874 break; 1875 } 1876 hfp_connection->ok_pending = 1; 1877 break; 1878 case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1879 // expected by SLC state machine 1880 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break; 1881 hfp_connection->send_ag_indicators_segment = 0; 1882 hfp_connection->send_ag_status_indicators = 1; 1883 break; 1884 case HFP_CMD_LIST_CURRENT_CALLS: 1885 hfp_connection->command = HFP_CMD_NONE; 1886 hfp_connection->next_call_index = 0; 1887 hfp_connection->send_status_of_current_calls = 1; 1888 break; 1889 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1890 if (subscriber_numbers_count == 0){ 1891 hfp_ag_send_ok(hfp_connection->rfcomm_cid); 1892 break; 1893 } 1894 hfp_connection->next_subscriber_number_to_send = 0; 1895 hfp_connection->send_subscriber_number = 1; 1896 break; 1897 case HFP_CMD_TRANSMIT_DTMF_CODES: 1898 { 1899 hfp_connection->command = HFP_CMD_NONE; 1900 char buffer[2]; 1901 buffer[0] = (char) hfp_connection->ag_dtmf_code; 1902 buffer[1] = 0; 1903 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer); 1904 break; 1905 } 1906 case HFP_CMD_HF_REQUEST_PHONE_NUMBER: 1907 hfp_connection->command = HFP_CMD_NONE; 1908 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG); 1909 break; 1910 case HFP_CMD_TURN_OFF_EC_AND_NR: 1911 hfp_connection->command = HFP_CMD_NONE; 1912 if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){ 1913 hfp_connection->ok_pending = 1; 1914 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction); 1915 log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction); 1916 } else { 1917 hfp_connection->send_error = 1; 1918 } 1919 break; 1920 case HFP_CMD_CALL_ANSWERED: 1921 hfp_connection->command = HFP_CMD_NONE; 1922 log_info("HFP: ATA"); 1923 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection); 1924 break; 1925 case HFP_CMD_HANG_UP_CALL: 1926 hfp_connection->command = HFP_CMD_NONE; 1927 hfp_connection->ok_pending = 1; 1928 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection); 1929 break; 1930 case HFP_CMD_CALL_HOLD: { 1931 hfp_connection->command = HFP_CMD_NONE; 1932 hfp_connection->ok_pending = 1; 1933 1934 switch (hfp_connection->ag_call_hold_action){ 1935 case 0: 1936 // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call. 1937 hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection); 1938 break; 1939 case 1: 1940 // Releases all active calls (if any exist) and accepts the other (held or waiting) call. 1941 // Where both a held and a waiting call exist, the above procedures shall apply to the 1942 // waiting call (i.e., not to the held call) in conflicting situation. 1943 hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1944 break; 1945 case 2: 1946 // Places all active calls (if any exist) on hold 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_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection); 1950 break; 1951 case 3: 1952 // Adds a held call to the conversation. 1953 hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection); 1954 break; 1955 case 4: 1956 // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer). 1957 hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection); 1958 break; 1959 default: 1960 break; 1961 } 1962 hfp_connection->call_index = 0; 1963 break; 1964 } 1965 case HFP_CMD_CALL_PHONE_NUMBER: 1966 hfp_connection->command = HFP_CMD_NONE; 1967 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection); 1968 break; 1969 case HFP_CMD_REDIAL_LAST_NUMBER: 1970 hfp_connection->command = HFP_CMD_NONE; 1971 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection); 1972 break; 1973 case HFP_CMD_ENABLE_CLIP: 1974 hfp_connection->command = HFP_CMD_NONE; 1975 log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled); 1976 hfp_connection->ok_pending = 1; 1977 break; 1978 case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION: 1979 hfp_connection->command = HFP_CMD_NONE; 1980 log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled); 1981 hfp_connection->ok_pending = 1; 1982 break; 1983 case HFP_CMD_SET_SPEAKER_GAIN: 1984 hfp_connection->command = HFP_CMD_NONE; 1985 hfp_connection->ok_pending = 1; 1986 log_info("HF speaker gain = %u", hfp_connection->speaker_gain); 1987 break; 1988 case HFP_CMD_SET_MICROPHONE_GAIN: 1989 hfp_connection->command = HFP_CMD_NONE; 1990 hfp_connection->ok_pending = 1; 1991 log_info("HF microphone gain = %u", hfp_connection->microphone_gain); 1992 break; 1993 default: 1994 break; 1995 } 1996 } 1997 } 1998 1999 static void hfp_run(void){ 2000 btstack_linked_list_iterator_t it; 2001 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2002 while (btstack_linked_list_iterator_has_next(&it)){ 2003 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2004 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2005 hfp_ag_run_for_context(hfp_connection); 2006 } 2007 } 2008 2009 static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2010 switch (packet_type){ 2011 case RFCOMM_DATA_PACKET: 2012 hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size); 2013 break; 2014 case HCI_EVENT_PACKET: 2015 if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 2016 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 2017 hfp_ag_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 2018 return; 2019 } 2020 hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2021 break; 2022 default: 2023 break; 2024 } 2025 2026 hfp_run(); 2027 } 2028 2029 static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2030 hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG); 2031 2032 // allow for sco established -> ring transition 2033 if (packet_type != HCI_EVENT_PACKET) return; 2034 if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return; 2035 hfp_run(); 2036 } 2037 2038 void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){ 2039 if (codecs_nr > HFP_MAX_NUM_CODECS){ 2040 log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 2041 return; 2042 } 2043 int i; 2044 hfp_codecs_nr = codecs_nr; 2045 for (i=0; i < codecs_nr; i++){ 2046 hfp_codecs[i] = codecs[i]; 2047 } 2048 } 2049 2050 void hfp_ag_init_supported_features(uint32_t supported_features){ 2051 hfp_supported_features = supported_features; 2052 } 2053 2054 void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){ 2055 hfp_ag_indicators_nr = ag_indicators_nr; 2056 (void)memcpy(hfp_ag_indicators, ag_indicators, 2057 ag_indicators_nr * sizeof(hfp_ag_indicator_t)); 2058 } 2059 2060 void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){ 2061 if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return; 2062 hfp_generic_status_indicators_nr = hf_indicators_nr; 2063 (void)memcpy(hfp_generic_status_indicators, hf_indicators, 2064 hf_indicators_nr * sizeof(hfp_generic_status_indicator_t)); 2065 } 2066 2067 void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){ 2068 hfp_ag_call_hold_services_nr = call_hold_services_nr; 2069 (void)memcpy(hfp_ag_call_hold_services, call_hold_services, 2070 call_hold_services_nr * sizeof(char *)); 2071 } 2072 2073 2074 void hfp_ag_init(uint16_t rfcomm_channel_nr){ 2075 2076 hfp_init(); 2077 2078 hci_event_callback_registration.callback = &hci_event_packet_handler; 2079 hci_add_event_handler(&hci_event_callback_registration); 2080 2081 rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 2082 2083 // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 2084 hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler); 2085 2086 hfp_ag_response_and_hold_active = 0; 2087 subscriber_numbers = NULL; 2088 subscriber_numbers_count = 0; 2089 2090 hfp_gsm_init(); 2091 } 2092 2093 void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){ 2094 hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG); 2095 } 2096 2097 void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){ 2098 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2099 if (!hfp_connection){ 2100 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2101 return; 2102 } 2103 hfp_release_service_level_connection(hfp_connection); 2104 hfp_ag_run_for_context(hfp_connection); 2105 } 2106 2107 void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){ 2108 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2109 if (!hfp_connection){ 2110 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2111 return; 2112 } 2113 hfp_connection->extended_audio_gateway_error = 0; 2114 if (!hfp_connection->enable_extended_audio_gateway_error_report){ 2115 return; 2116 } 2117 hfp_connection->extended_audio_gateway_error = error; 2118 hfp_ag_run_for_context(hfp_connection); 2119 } 2120 2121 static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){ 2122 log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state); 2123 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 2124 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 2125 2126 hfp_connection->establish_audio_connection = 1; 2127 if (!has_codec_negotiation_feature(hfp_connection)){ 2128 log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD"); 2129 hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 2130 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 2131 // now, pick link settings 2132 hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection)); 2133 return; 2134 } 2135 2136 hfp_connection->trigger_codec_exchange = 1; 2137 } 2138 2139 void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){ 2140 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2141 if (!hfp_connection){ 2142 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2143 return; 2144 } 2145 hfp_connection->trigger_codec_exchange = 0; 2146 hfp_connection->establish_audio_connection = 0; 2147 hfp_ag_setup_audio_connection(hfp_connection); 2148 hfp_ag_run_for_context(hfp_connection); 2149 } 2150 2151 void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){ 2152 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2153 if (!hfp_connection){ 2154 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2155 return; 2156 } 2157 hfp_release_audio_connection(hfp_connection); 2158 hfp_ag_run_for_context(hfp_connection); 2159 } 2160 2161 /** 2162 * @brief Enable in-band ring tone 2163 */ 2164 void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){ 2165 if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){ 2166 return; 2167 } 2168 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone); 2169 2170 btstack_linked_list_iterator_t it; 2171 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2172 while (btstack_linked_list_iterator_has_next(&it)){ 2173 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2174 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2175 hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING; 2176 hfp_ag_run_for_context(hfp_connection); 2177 } 2178 } 2179 2180 /** 2181 * @brief Called from GSM 2182 */ 2183 void hfp_ag_incoming_call(void){ 2184 hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL); 2185 } 2186 2187 /** 2188 * @brief number is stored. 2189 */ 2190 void hfp_ag_set_clip(uint8_t type, const char * number){ 2191 hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number); 2192 } 2193 2194 void hfp_ag_call_dropped(void){ 2195 hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL); 2196 } 2197 2198 // call from AG UI 2199 void hfp_ag_answer_incoming_call(void){ 2200 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL); 2201 } 2202 2203 void hfp_ag_join_held_call(void){ 2204 hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL); 2205 } 2206 2207 void hfp_ag_terminate_call(void){ 2208 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL); 2209 } 2210 2211 void hfp_ag_outgoing_call_ringing(void){ 2212 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL); 2213 } 2214 2215 void hfp_ag_outgoing_call_established(void){ 2216 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL); 2217 } 2218 2219 void hfp_ag_outgoing_call_rejected(void){ 2220 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL); 2221 } 2222 2223 void hfp_ag_outgoing_call_accepted(void){ 2224 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL); 2225 } 2226 2227 void hfp_ag_hold_incoming_call(void){ 2228 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL); 2229 } 2230 2231 void hfp_ag_accept_held_incoming_call(void) { 2232 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL); 2233 } 2234 2235 void hfp_ag_reject_held_incoming_call(void){ 2236 hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL); 2237 } 2238 2239 static void hfp_ag_set_ag_indicator(const char * name, int value){ 2240 int indicator_index = get_ag_indicator_index_for_name(name); 2241 if (indicator_index < 0) return; 2242 hfp_ag_indicators[indicator_index].status = value; 2243 2244 2245 btstack_linked_list_iterator_t it; 2246 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 2247 while (btstack_linked_list_iterator_has_next(&it)){ 2248 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 2249 if (hfp_connection->local_role != HFP_ROLE_AG) continue; 2250 if (! hfp_connection->ag_indicators[indicator_index].enabled) { 2251 log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value); 2252 continue; 2253 } 2254 log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value); 2255 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1); 2256 hfp_ag_run_for_context(hfp_connection); 2257 } 2258 } 2259 2260 void hfp_ag_set_registration_status(int status){ 2261 hfp_ag_set_ag_indicator("service", status); 2262 } 2263 2264 void hfp_ag_set_signal_strength(int strength){ 2265 hfp_ag_set_ag_indicator("signal", strength); 2266 } 2267 2268 void hfp_ag_set_roaming_status(int status){ 2269 hfp_ag_set_ag_indicator("roam", status); 2270 } 2271 2272 void hfp_ag_set_battery_level(int level){ 2273 hfp_ag_set_ag_indicator("battchg", level); 2274 } 2275 2276 void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){ 2277 if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return; 2278 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2279 if (!hfp_connection){ 2280 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2281 return; 2282 } 2283 2284 if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) { 2285 log_info("AG cannot acivate voice recognition - not supported by HF"); 2286 return; 2287 } 2288 2289 if (activate){ 2290 hfp_ag_establish_audio_connection(acl_handle); 2291 } 2292 2293 hfp_connection->ag_activate_voice_recognition = activate; 2294 hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION; 2295 hfp_ag_run_for_context(hfp_connection); 2296 } 2297 2298 void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 2299 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2300 if (!hfp_connection){ 2301 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2302 return; 2303 } 2304 if (hfp_connection->microphone_gain != gain){ 2305 hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN; 2306 hfp_connection->microphone_gain = gain; 2307 hfp_connection->send_microphone_gain = 1; 2308 } 2309 hfp_ag_run_for_context(hfp_connection); 2310 } 2311 2312 void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 2313 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2314 if (!hfp_connection){ 2315 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2316 return; 2317 } 2318 if (hfp_connection->speaker_gain != gain){ 2319 hfp_connection->speaker_gain = gain; 2320 hfp_connection->send_speaker_gain = 1; 2321 } 2322 hfp_ag_run_for_context(hfp_connection); 2323 } 2324 2325 void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){ 2326 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2327 if (!hfp_connection){ 2328 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2329 return; 2330 } 2331 hfp_ag_set_clip(0, number); 2332 hfp_connection->send_phone_number_for_voice_tag = 1; 2333 } 2334 2335 void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 2336 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2337 if (!hfp_connection){ 2338 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2339 return; 2340 } 2341 hfp_connection->send_error = 1; 2342 } 2343 2344 void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){ 2345 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2346 if (!hfp_connection){ 2347 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2348 return; 2349 } 2350 hfp_connection->ok_pending = 1; 2351 } 2352 2353 void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){ 2354 subscriber_numbers = numbers; 2355 subscriber_numbers_count = numbers_count; 2356 } 2357 2358 void hfp_ag_clear_last_dialed_number(void){ 2359 hfp_gsm_clear_last_dialed_number(); 2360 } 2361 2362 void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){ 2363 hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle); 2364 if (!hfp_connection){ 2365 log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle); 2366 return; 2367 } 2368 if (!hfp_connection->call_waiting_notification_enabled) return; 2369 2370 hfp_connection->ag_notify_incoming_call_waiting = 1; 2371 hfp_ag_run_for_context(hfp_connection); 2372 } 2373 2374 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){ 2375 if (!name){ 2376 name = default_hfp_ag_service_name; 2377 } 2378 hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name); 2379 2380 /* 2381 * 0x01 – Ability to reject a call 2382 * 0x00 – No ability to reject a call 2383 */ 2384 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301); // Hands-Free Profile - Network 2385 de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call); 2386 2387 // Construct SupportedFeatures for SDP bitmap: 2388 // 2389 // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 2390 // of the Bits 0 to 4 of the unsolicited result code +BRSF" 2391 // 2392 // Wide band speech (bit 5) requires Codec negotiation 2393 // 2394 uint16_t sdp_features = supported_features & 0x1f; 2395 if (wide_band_speech && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){ 2396 sdp_features |= 1 << 5; 2397 } 2398 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 2399 de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 2400 } 2401 2402 void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){ 2403 if (callback == NULL){ 2404 log_error("hfp_ag_register_packet_handler called with NULL callback"); 2405 return; 2406 } 2407 hfp_ag_callback = callback; 2408 hfp_set_ag_callback(callback); 2409 } 2410