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