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