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_hf.c" 39 40 // ***************************************************************************** 41 // 42 // HFP Hands-Free (HF) 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 "bluetooth_sdp.h" 54 #include "btstack_debug.h" 55 #include "btstack_event.h" 56 #include "btstack_memory.h" 57 #include "btstack_run_loop.h" 58 #include "classic/core.h" 59 #include "classic/hfp.h" 60 #include "classic/hfp_hf.h" 61 #include "classic/sdp_client_rfcomm.h" 62 #include "classic/sdp_server.h" 63 #include "classic/sdp_util.h" 64 #include "hci.h" 65 #include "hci_cmd.h" 66 #include "hci_dump.h" 67 #include "l2cap.h" 68 69 static btstack_packet_callback_registration_t hci_event_callback_registration; 70 71 static const char default_hfp_hf_service_name[] = "Hands-Free unit"; 72 static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 73 static uint8_t hfp_codecs_nr = 0; 74 static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 75 76 static uint8_t hfp_indicators_nr = 0; 77 static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS]; 78 static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS]; 79 80 static uint8_t hfp_hf_speaker_gain = 9; 81 static uint8_t hfp_hf_microphone_gain = 9; 82 83 static btstack_packet_handler_t hfp_hf_callback; 84 85 static hfp_call_status_t hfp_call_status; 86 static hfp_callsetup_status_t hfp_callsetup_status; 87 static hfp_callheld_status_t hfp_callheld_status; 88 89 static char phone_number[25]; 90 91 static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){ 92 btstack_linked_list_iterator_t it; 93 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 94 while (btstack_linked_list_iterator_has_next(&it)){ 95 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 96 if (hfp_connection->acl_handle != handle) continue; 97 if (hfp_connection->local_role != HFP_ROLE_HF) continue; 98 return hfp_connection; 99 } 100 return NULL; 101 } 102 103 void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 104 if (callback == NULL){ 105 log_error("hfp_hf_register_packet_handler called with NULL callback"); 106 return; 107 } 108 hfp_hf_callback = callback; 109 hfp_set_hf_callback(callback); 110 } 111 112 static void hfp_hf_emit_subscriber_information(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t status, uint8_t bnip_type, const char * bnip_number){ 113 if (!callback) return; 114 uint8_t event[31]; 115 event[0] = HCI_EVENT_HFP_META; 116 event[1] = sizeof(event) - 2; 117 event[2] = event_subtype; 118 event[3] = status; 119 event[4] = bnip_type; 120 int size = (strlen(bnip_number) < sizeof(event) - 6) ? (int) strlen(bnip_number) : (int) sizeof(event) - 6; 121 strncpy((char*)&event[5], bnip_number, size); 122 event[5 + size] = 0; 123 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 124 } 125 126 static void hfp_hf_emit_type_and_number(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t bnip_type, const char * bnip_number){ 127 if (!callback) return; 128 uint8_t event[30]; 129 event[0] = HCI_EVENT_HFP_META; 130 event[1] = sizeof(event) - 2; 131 event[2] = event_subtype; 132 event[3] = bnip_type; 133 int size = (strlen(bnip_number) < sizeof(event) - 5) ? (int) strlen(bnip_number) : (int) sizeof(event) - 5; 134 strncpy((char*)&event[4], bnip_number, size); 135 event[4 + size] = 0; 136 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 137 } 138 139 static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir, 140 uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){ 141 if (!callback) return; 142 uint8_t event[35]; 143 event[0] = HCI_EVENT_HFP_META; 144 event[1] = sizeof(event) - 2; 145 event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 146 event[3] = clcc_idx; 147 event[4] = clcc_dir; 148 event[6] = clcc_status; 149 event[7] = clcc_mpty; 150 event[8] = bnip_type; 151 int size = (strlen(bnip_number) < sizeof(event) - 10) ? (int) strlen(bnip_number) : (int) sizeof(event) - 10; 152 strncpy((char*)&event[9], bnip_number, size); 153 event[9 + size] = 0; 154 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 155 } 156 157 static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 158 int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 159 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 160 return hf && ag; 161 } 162 163 static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 164 int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING); 165 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING); 166 return hf && ag; 167 } 168 169 170 static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 171 int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS); 172 int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS); 173 return hf && ag; 174 } 175 176 void hfp_hf_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint16_t supported_features, int wide_band_speech){ 177 if (!name){ 178 name = default_hfp_hf_service_name; 179 } 180 hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 181 182 // Construct SupportedFeatures for SDP bitmap: 183 // 184 // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 185 // of the Bits 0 to 4 of the unsolicited result code +BRSF" 186 // 187 uint16_t sdp_features = supported_features & 0x1f; 188 if (supported_features & wide_band_speech){ 189 sdp_features |= 1 << 5; // Wide band speech bit 190 } 191 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 192 de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 193 } 194 195 196 static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){ 197 char buffer[20]; 198 snprintf(buffer, sizeof(buffer), "AT%s\r\n", cmd); 199 return send_str_over_rfcomm(cid, buffer); 200 } 201 202 static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){ 203 char buffer[20]; 204 snprintf(buffer, sizeof(buffer), "AT%s%s\r\n", cmd, mark); 205 return send_str_over_rfcomm(cid, buffer); 206 } 207 208 static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){ 209 char buffer[40]; 210 snprintf(buffer, sizeof(buffer), "AT%s=%d\r\n", cmd, value); 211 return send_str_over_rfcomm(cid, buffer); 212 } 213 214 static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 215 char buffer[30]; 216 const int size = sizeof(buffer); 217 int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS); 218 offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr); 219 offset += snprintf(buffer+offset, size-offset, "\r\n"); 220 return send_str_over_rfcomm(cid, buffer); 221 } 222 223 static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 224 char buffer[50]; 225 const int size = sizeof(buffer); 226 int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 227 offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr); 228 offset += snprintf(buffer+offset, size-offset, "\r\n"); 229 return send_str_over_rfcomm(cid, buffer); 230 } 231 232 static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 233 char buffer[30]; 234 const int size = sizeof(buffer); 235 int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 236 offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr); 237 offset += snprintf(buffer+offset, size-offset, "\r\n"); 238 return send_str_over_rfcomm(cid, buffer); 239 } 240 241 static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 242 char buffer[20]; 243 snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 244 return send_str_over_rfcomm(cid, buffer); 245 } 246 247 static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 248 char buffer[40]; 249 snprintf(buffer, sizeof(buffer), "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number); 250 return send_str_over_rfcomm(cid, buffer); 251 } 252 253 static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){ 254 char buffer[40]; 255 snprintf(buffer, sizeof(buffer), "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id); 256 return send_str_over_rfcomm(cid, buffer); 257 } 258 259 static int hfp_hf_send_chld(uint16_t cid, unsigned int number){ 260 char buffer[40]; 261 snprintf(buffer, sizeof(buffer), "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 262 return send_str_over_rfcomm(cid, buffer); 263 } 264 265 static int hfp_hf_send_dtmf(uint16_t cid, char code){ 266 char buffer[20]; 267 snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code); 268 return send_str_over_rfcomm(cid, buffer); 269 } 270 271 static int hfp_hf_cmd_ata(uint16_t cid){ 272 return send_str_over_rfcomm(cid, "ATA\r\n"); 273 } 274 275 static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 276 return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features); 277 } 278 279 static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){ 280 return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?"); 281 } 282 283 static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){ 284 return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?"); 285 } 286 287 static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 288 return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?"); 289 } 290 291 static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){ 292 return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?"); 293 } 294 295 static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){ 296 return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?"); 297 } 298 299 static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 300 return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0"); 301 } 302 303 static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 304 return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?"); 305 } 306 307 static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 308 return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP); 309 } 310 311 static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 312 return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 313 } 314 315 static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 316 return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 317 } 318 319 static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 320 return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate); 321 } 322 323 static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){ 324 return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate); 325 } 326 327 static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 328 return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate); 329 } 330 331 static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 332 return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 333 } 334 335 static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 336 return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 337 } 338 339 static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 340 return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 341 } 342 343 static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 344 return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER); 345 } 346 347 static int hfp_hf_send_chup(uint16_t cid){ 348 return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL); 349 } 350 351 static int hfp_hf_send_binp(uint16_t cid){ 352 return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1"); 353 } 354 355 static int hfp_hf_send_clcc(uint16_t cid){ 356 return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS); 357 } 358 359 static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){ 360 if (!callback) return; 361 uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1]; 362 event[0] = HCI_EVENT_HFP_META; 363 event[1] = sizeof(event) - 2; 364 event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 365 event[3] = indicator.index; 366 event[4] = indicator.status; 367 strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE); 368 event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0; 369 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 370 } 371 372 static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){ 373 if (!callback) return; 374 uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1]; 375 event[0] = HCI_EVENT_HFP_META; 376 event[1] = sizeof(event) - 2; 377 event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED; 378 event[3] = network_operator.mode; 379 event[4] = network_operator.format; 380 strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE); 381 event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0; 382 (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 383 } 384 385 static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 386 if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 387 if (hfp_connection->ok_pending) return 0; 388 int done = 1; 389 390 switch (hfp_connection->state){ 391 case HFP_EXCHANGE_SUPPORTED_FEATURES: 392 hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr); 393 hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 394 hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 395 break; 396 case HFP_NOTIFY_ON_CODECS: 397 hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 398 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 399 break; 400 case HFP_RETRIEVE_INDICATORS: 401 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 402 hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 403 break; 404 case HFP_RETRIEVE_INDICATORS_STATUS: 405 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 406 hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 407 break; 408 case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 409 hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 410 hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 411 break; 412 case HFP_RETRIEVE_CAN_HOLD_CALL: 413 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 414 hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 415 break; 416 case HFP_LIST_GENERIC_STATUS_INDICATORS: 417 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 418 hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 419 break; 420 case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 421 hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 422 hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 423 break; 424 case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 425 hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 426 hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 427 break; 428 default: 429 done = 0; 430 break; 431 } 432 return done; 433 } 434 435 436 static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 437 if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 438 if (hfp_connection->ok_pending) return 0; 439 440 int done = 0; 441 if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 442 hfp_connection->ok_pending = 1; 443 done = 1; 444 hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 445 return done; 446 }; 447 if (hfp_connection->change_status_update_for_individual_ag_indicators){ 448 hfp_connection->ok_pending = 1; 449 done = 1; 450 hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 451 hfp_connection->ag_indicators_status_update_bitmap, 452 hfp_connection->ag_indicators_nr); 453 return done; 454 } 455 456 switch (hfp_connection->hf_query_operator_state){ 457 case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 458 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 459 hfp_connection->ok_pending = 1; 460 hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 461 return 1; 462 case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 463 hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 464 hfp_connection->ok_pending = 1; 465 hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 466 return 1; 467 default: 468 break; 469 } 470 471 if (hfp_connection->enable_extended_audio_gateway_error_report){ 472 hfp_connection->ok_pending = 1; 473 done = 1; 474 hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 475 return done; 476 } 477 478 return done; 479 } 480 481 static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 482 /* events ( == commands): 483 HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 484 HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 485 hf_trigger_codec_connection_setup == received BCC 486 ag_trigger_codec_connection_setup == received from AG to send BCS 487 HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 488 */ 489 490 if (hfp_connection->ok_pending) return 0; 491 492 switch (hfp_connection->command){ 493 case HFP_CMD_AVAILABLE_CODECS: 494 if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0; 495 496 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 497 hfp_connection->ok_pending = 1; 498 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 499 return 1; 500 case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 501 hfp_connection->codec_confirmed = 0; 502 hfp_connection->suggested_codec = 0; 503 hfp_connection->negotiated_codec = 0; 504 505 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 506 hfp_connection->ok_pending = 1; 507 hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 508 break; 509 510 case HFP_CMD_AG_SUGGESTED_CODEC:{ 511 if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){ 512 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 513 hfp_connection->ok_pending = 1; 514 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 515 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 516 log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD"); 517 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 518 } else { 519 hfp_connection->codec_confirmed = 0; 520 hfp_connection->suggested_codec = 0; 521 hfp_connection->negotiated_codec = 0; 522 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 523 hfp_connection->ok_pending = 1; 524 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 525 526 } 527 break; 528 } 529 default: 530 break; 531 } 532 return 0; 533 } 534 535 static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 536 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 537 hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0; 538 539 540 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){ 541 hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 542 hfp_connection->release_audio_connection = 0; 543 gap_disconnect(hfp_connection->sco_handle); 544 return 1; 545 } 546 547 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 548 549 // run codecs exchange 550 int done = codecs_exchange_state_machine(hfp_connection); 551 if (done) return 1; 552 553 return 0; 554 } 555 556 static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 557 if (hfp_connection->hf_answer_incoming_call){ 558 hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 559 hfp_connection->hf_answer_incoming_call = 0; 560 return 1; 561 } 562 return 0; 563 } 564 565 static void hfp_run_for_context(hfp_connection_t * hfp_connection){ 566 if (!hfp_connection) return; 567 if (!hfp_connection->rfcomm_cid) return; 568 569 if (hfp_connection->local_role != HFP_ROLE_HF) { 570 log_info("HFP HF%p, wrong role %u", hfp_connection, hfp_connection->local_role); 571 return; 572 } 573 574 if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){ 575 576 hfp_connection->hf_accept_sco = 0; 577 578 // notify about codec selection if not done already 579 if (hfp_connection->negotiated_codec == 0){ 580 hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 581 } 582 583 // remote supported feature eSCO is set if link type is eSCO 584 // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 585 uint16_t max_latency; 586 uint8_t retransmission_effort; 587 uint16_t packet_types; 588 589 if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 590 max_latency = 0x000c; 591 retransmission_effort = 0x02; 592 packet_types = 0x388; 593 } else { 594 max_latency = 0xffff; 595 retransmission_effort = 0xff; 596 packet_types = 0x003f; 597 } 598 599 uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 600 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 601 sco_voice_setting = 0x0043; // Transparent data 602 } 603 604 log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting); 605 hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 606 sco_voice_setting, retransmission_effort, packet_types); 607 return; 608 } 609 610 if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 611 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 612 return; 613 } 614 int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 615 if (!done){ 616 done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 617 } 618 if (!done){ 619 done = hfp_hf_run_for_audio_connection(hfp_connection); 620 } 621 if (!done){ 622 done = call_setup_state_machine(hfp_connection); 623 } 624 625 // don't send a new command while ok still pending 626 if (hfp_connection->ok_pending) return; 627 628 if (hfp_connection->send_microphone_gain){ 629 hfp_connection->send_microphone_gain = 0; 630 hfp_connection->ok_pending = 1; 631 hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 632 return; 633 } 634 635 if (hfp_connection->send_speaker_gain){ 636 hfp_connection->send_speaker_gain = 0; 637 hfp_connection->ok_pending = 1; 638 hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 639 return; 640 } 641 642 if (hfp_connection->hf_deactivate_calling_line_notification){ 643 hfp_connection->hf_deactivate_calling_line_notification = 0; 644 hfp_connection->ok_pending = 1; 645 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 646 return; 647 } 648 649 if (hfp_connection->hf_activate_calling_line_notification){ 650 hfp_connection->hf_activate_calling_line_notification = 0; 651 hfp_connection->ok_pending = 1; 652 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 653 return; 654 } 655 656 if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 657 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 658 hfp_connection->ok_pending = 1; 659 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0); 660 return; 661 } 662 663 if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){ 664 hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0; 665 hfp_connection->ok_pending = 1; 666 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1); 667 return; 668 } 669 670 if (hfp_connection->hf_deactivate_voice_recognition_notification){ 671 hfp_connection->hf_deactivate_voice_recognition_notification = 0; 672 hfp_connection->ok_pending = 1; 673 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 674 return; 675 } 676 677 if (hfp_connection->hf_activate_voice_recognition_notification){ 678 hfp_connection->hf_activate_voice_recognition_notification = 0; 679 hfp_connection->ok_pending = 1; 680 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 681 return; 682 } 683 684 685 if (hfp_connection->hf_deactivate_call_waiting_notification){ 686 hfp_connection->hf_deactivate_call_waiting_notification = 0; 687 hfp_connection->ok_pending = 1; 688 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 689 return; 690 } 691 692 if (hfp_connection->hf_activate_call_waiting_notification){ 693 hfp_connection->hf_activate_call_waiting_notification = 0; 694 hfp_connection->ok_pending = 1; 695 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 696 return; 697 } 698 699 if (hfp_connection->hf_initiate_outgoing_call){ 700 hfp_connection->hf_initiate_outgoing_call = 0; 701 hfp_connection->ok_pending = 1; 702 hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 703 return; 704 } 705 706 if (hfp_connection->hf_initiate_memory_dialing){ 707 hfp_connection->hf_initiate_memory_dialing = 0; 708 hfp_connection->ok_pending = 1; 709 hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 710 return; 711 } 712 713 if (hfp_connection->hf_initiate_redial_last_number){ 714 hfp_connection->hf_initiate_redial_last_number = 0; 715 hfp_connection->ok_pending = 1; 716 hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 717 return; 718 } 719 720 if (hfp_connection->hf_send_chup){ 721 hfp_connection->hf_send_chup = 0; 722 hfp_connection->ok_pending = 1; 723 hfp_hf_send_chup(hfp_connection->rfcomm_cid); 724 return; 725 } 726 727 if (hfp_connection->hf_send_chld_0){ 728 hfp_connection->hf_send_chld_0 = 0; 729 hfp_connection->ok_pending = 1; 730 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 731 return; 732 } 733 734 if (hfp_connection->hf_send_chld_1){ 735 hfp_connection->hf_send_chld_1 = 0; 736 hfp_connection->ok_pending = 1; 737 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 738 return; 739 } 740 741 if (hfp_connection->hf_send_chld_2){ 742 hfp_connection->hf_send_chld_2 = 0; 743 hfp_connection->ok_pending = 1; 744 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 745 return; 746 } 747 748 if (hfp_connection->hf_send_chld_3){ 749 hfp_connection->hf_send_chld_3 = 0; 750 hfp_connection->ok_pending = 1; 751 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 752 return; 753 } 754 755 if (hfp_connection->hf_send_chld_4){ 756 hfp_connection->hf_send_chld_4 = 0; 757 hfp_connection->ok_pending = 1; 758 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 759 return; 760 } 761 762 if (hfp_connection->hf_send_chld_x){ 763 hfp_connection->hf_send_chld_x = 0; 764 hfp_connection->ok_pending = 1; 765 hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 766 return; 767 } 768 769 if (hfp_connection->hf_send_dtmf_code){ 770 char code = hfp_connection->hf_send_dtmf_code; 771 hfp_connection->hf_send_dtmf_code = 0; 772 hfp_connection->ok_pending = 1; 773 hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 774 return; 775 } 776 777 if (hfp_connection->hf_send_binp){ 778 hfp_connection->hf_send_binp = 0; 779 hfp_connection->ok_pending = 1; 780 hfp_hf_send_binp(hfp_connection->rfcomm_cid); 781 return; 782 } 783 784 if (hfp_connection->hf_send_clcc){ 785 hfp_connection->hf_send_clcc = 0; 786 hfp_connection->ok_pending = 1; 787 hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 788 return; 789 } 790 791 if (hfp_connection->hf_send_rrh){ 792 hfp_connection->hf_send_rrh = 0; 793 char buffer[20]; 794 switch (hfp_connection->hf_send_rrh_command){ 795 case '?': 796 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD); 797 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 798 return; 799 case '0': 800 case '1': 801 case '2': 802 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command); 803 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 804 return; 805 default: 806 break; 807 } 808 return; 809 } 810 811 if (hfp_connection->hf_send_cnum){ 812 hfp_connection->hf_send_cnum = 0; 813 char buffer[20]; 814 sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION); 815 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 816 return; 817 } 818 819 // update HF indicators 820 if (hfp_connection->generic_status_update_bitmap){ 821 int i; 822 for (i=0;i<hfp_indicators_nr;i++){ 823 if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 824 if (hfp_connection->generic_status_indicators[i].state){ 825 hfp_connection->ok_pending = 1; 826 hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 827 char buffer[30]; 828 sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]); 829 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 830 } else { 831 log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]); 832 } 833 return; 834 } 835 } 836 } 837 838 if (done) return; 839 // deal with disconnect 840 switch (hfp_connection->state){ 841 case HFP_W2_DISCONNECT_RFCOMM: 842 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 843 rfcomm_disconnect(hfp_connection->rfcomm_cid); 844 break; 845 846 default: 847 break; 848 } 849 } 850 851 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 852 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 853 854 hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 855 856 // restore volume settings 857 hfp_connection->speaker_gain = hfp_hf_speaker_gain; 858 hfp_connection->send_speaker_gain = 1; 859 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 860 hfp_connection->microphone_gain = hfp_hf_microphone_gain; 861 hfp_connection->send_microphone_gain = 1; 862 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 863 // enable all indicators 864 int i; 865 for (i=0;i<hfp_indicators_nr;i++){ 866 hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i]; 867 hfp_connection->generic_status_indicators[i].state = 1; 868 } 869 } 870 871 static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){ 872 hfp_connection->ok_pending = 0; 873 switch (hfp_connection->state){ 874 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 875 if (has_codec_negotiation_feature(hfp_connection)){ 876 hfp_connection->state = HFP_NOTIFY_ON_CODECS; 877 break; 878 } 879 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 880 break; 881 882 case HFP_W4_NOTIFY_ON_CODECS: 883 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 884 break; 885 886 case HFP_W4_RETRIEVE_INDICATORS: 887 hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 888 break; 889 890 case HFP_W4_RETRIEVE_INDICATORS_STATUS: 891 hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 892 break; 893 894 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 895 if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 896 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 897 break; 898 } 899 if (has_hf_indicators_feature(hfp_connection)){ 900 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 901 break; 902 } 903 hfp_ag_slc_established(hfp_connection); 904 break; 905 906 case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 907 if (has_hf_indicators_feature(hfp_connection)){ 908 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 909 break; 910 } 911 hfp_ag_slc_established(hfp_connection); 912 break; 913 914 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 915 hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 916 break; 917 918 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 919 hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 920 break; 921 922 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 923 hfp_ag_slc_established(hfp_connection); 924 break; 925 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 926 if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 927 hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 928 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 929 break; 930 } 931 932 if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 933 hfp_connection->change_status_update_for_individual_ag_indicators = 0; 934 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 935 break; 936 } 937 938 switch (hfp_connection->hf_query_operator_state){ 939 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 940 // printf("Format set, querying name\n"); 941 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 942 break; 943 case HPF_HF_QUERY_OPERATOR_W4_RESULT: 944 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 945 hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator); 946 break; 947 default: 948 break; 949 } 950 951 if (hfp_connection->enable_extended_audio_gateway_error_report){ 952 hfp_connection->enable_extended_audio_gateway_error_report = 0; 953 break; 954 } 955 956 switch (hfp_connection->codecs_state){ 957 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 958 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 959 break; 960 case HFP_CODECS_HF_CONFIRMED_CODEC: 961 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 962 break; 963 default: 964 break; 965 } 966 break; 967 default: 968 break; 969 } 970 971 // done 972 hfp_connection->command = HFP_CMD_NONE; 973 } 974 975 static void hfp_hf_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 976 UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 977 978 // assertion: size >= 1 as rfcomm.c does not deliver empty packets 979 if (size < 1) return; 980 981 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 982 if (!hfp_connection) return; 983 984 hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 985 986 // process messages byte-wise 987 int pos; 988 for (pos = 0; pos < size ; pos++){ 989 hfp_parse(hfp_connection, packet[pos], 1); 990 } 991 992 int value; 993 int i; 994 switch (hfp_connection->command){ 995 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 996 hfp_connection->command = HFP_CMD_NONE; 997 // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type); 998 hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number); 999 break; 1000 case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1001 hfp_connection->command = HFP_CMD_NONE; 1002 // printf("Response and Hold status: %s\n", hfp_connection->line_buffer); 1003 hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1004 break; 1005 case HFP_CMD_LIST_CURRENT_CALLS: 1006 hfp_connection->command = HFP_CMD_NONE; 1007 // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n", 1008 // hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty, 1009 // hfp_connection->bnip_number, hfp_connection->bnip_type); 1010 hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection->clcc_idx, 1011 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty, 1012 hfp_connection->bnip_type, hfp_connection->bnip_number); 1013 break; 1014 case HFP_CMD_SET_SPEAKER_GAIN: 1015 hfp_connection->command = HFP_CMD_NONE; 1016 value = btstack_atoi((char*)hfp_connection->line_buffer); 1017 hfp_hf_speaker_gain = value; 1018 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1019 break; 1020 case HFP_CMD_SET_MICROPHONE_GAIN: 1021 hfp_connection->command = HFP_CMD_NONE; 1022 value = btstack_atoi((char*)hfp_connection->line_buffer); 1023 hfp_hf_microphone_gain = value; 1024 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1025 break; 1026 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1027 hfp_connection->command = HFP_CMD_NONE; 1028 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1029 break; 1030 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1031 hfp_connection->command = HFP_CMD_NONE; 1032 hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1033 break; 1034 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1035 hfp_connection->command = HFP_CMD_NONE; 1036 hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1037 break; 1038 case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1039 hfp_connection->ok_pending = 0; 1040 hfp_connection->command = HFP_CMD_NONE; 1041 hfp_connection->extended_audio_gateway_error = 0; 1042 hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1043 break; 1044 case HFP_CMD_ERROR: 1045 hfp_connection->ok_pending = 0; 1046 hfp_reset_context_flags(hfp_connection); 1047 hfp_connection->command = HFP_CMD_NONE; 1048 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1049 break; 1050 case HFP_CMD_OK: 1051 hfp_hf_switch_on_ok(hfp_connection); 1052 break; 1053 case HFP_CMD_RING: 1054 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1055 break; 1056 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1057 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1058 if (hfp_connection->ag_indicators[i].status_changed) { 1059 if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1060 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1061 } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1062 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 1063 // avoid set but not used warning 1064 (void) hfp_callheld_status; 1065 } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1066 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1067 } 1068 hfp_connection->ag_indicators[i].status_changed = 0; 1069 hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]); 1070 break; 1071 } 1072 } 1073 break; 1074 default: 1075 break; 1076 } 1077 hfp_run_for_context(hfp_connection); 1078 } 1079 1080 static void hfp_run(void){ 1081 btstack_linked_list_iterator_t it; 1082 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1083 while (btstack_linked_list_iterator_has_next(&it)){ 1084 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1085 if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1086 hfp_run_for_context(hfp_connection); 1087 } 1088 } 1089 1090 static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1091 switch (packet_type){ 1092 case RFCOMM_DATA_PACKET: 1093 hfp_hf_handle_rfcomm_event(packet_type, channel, packet, size); 1094 break; 1095 case HCI_EVENT_PACKET: 1096 if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1097 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 1098 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1099 return; 1100 } 1101 hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1102 break; 1103 default: 1104 break; 1105 } 1106 hfp_run(); 1107 } 1108 1109 void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1110 hfp_init(); 1111 1112 hci_event_callback_registration.callback = &hfp_handle_hci_event; 1113 hci_add_event_handler(&hci_event_callback_registration); 1114 1115 rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 1116 1117 // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 1118 hfp_set_hf_rfcomm_packet_handler(&rfcomm_packet_handler); 1119 1120 hfp_set_hf_run_for_context(hfp_run_for_context); 1121 1122 hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1123 hfp_codecs_nr = 0; 1124 hfp_indicators_nr = 0; 1125 hfp_hf_speaker_gain = 9; 1126 hfp_hf_microphone_gain = 9; 1127 } 1128 1129 void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){ 1130 if (codecs_nr > HFP_MAX_NUM_CODECS){ 1131 log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 1132 return; 1133 } 1134 1135 hfp_codecs_nr = codecs_nr; 1136 int i; 1137 for (i=0; i<codecs_nr; i++){ 1138 hfp_codecs[i] = codecs[i]; 1139 } 1140 } 1141 1142 void hfp_hf_init_supported_features(uint32_t supported_features){ 1143 hfp_supported_features = supported_features; 1144 } 1145 1146 void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){ 1147 hfp_indicators_nr = indicators_nr; 1148 int i; 1149 for (i = 0; i < hfp_indicators_nr ; i++){ 1150 hfp_indicators[i] = indicators[i]; 1151 } 1152 } 1153 1154 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1155 hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 1156 } 1157 1158 void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 1159 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1160 if (!hfp_connection) { 1161 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1162 return; 1163 } 1164 hfp_release_service_level_connection(hfp_connection); 1165 hfp_run_for_context(hfp_connection); 1166 } 1167 1168 static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 1169 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1170 if (!hfp_connection) { 1171 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1172 return; 1173 } 1174 hfp_connection->enable_status_update_for_ag_indicators = enable; 1175 hfp_run_for_context(hfp_connection); 1176 } 1177 1178 void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1179 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1180 } 1181 1182 void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1183 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1184 } 1185 1186 // TODO: returned ERROR - wrong format 1187 void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 1188 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1189 if (!hfp_connection) { 1190 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1191 return; 1192 } 1193 hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1194 hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1195 hfp_run_for_context(hfp_connection); 1196 } 1197 1198 void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 1199 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1200 if (!hfp_connection) { 1201 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1202 return; 1203 } 1204 switch (hfp_connection->hf_query_operator_state){ 1205 case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1206 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1207 break; 1208 case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1209 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1210 break; 1211 default: 1212 break; 1213 } 1214 hfp_run_for_context(hfp_connection); 1215 } 1216 1217 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 1218 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1219 if (!hfp_connection) { 1220 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1221 return; 1222 } 1223 hfp_connection->enable_extended_audio_gateway_error_report = enable; 1224 hfp_run_for_context(hfp_connection); 1225 } 1226 1227 1228 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1229 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1230 } 1231 1232 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1233 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1234 } 1235 1236 1237 void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 1238 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1239 if (!hfp_connection) { 1240 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1241 return; 1242 } 1243 hfp_connection->establish_audio_connection = 0; 1244 1245 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1246 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1247 1248 if (!has_codec_negotiation_feature(hfp_connection)){ 1249 log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); 1250 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1251 hfp_connection->establish_audio_connection = 1; 1252 } else { 1253 switch (hfp_connection->codecs_state){ 1254 case HFP_CODECS_W4_AG_COMMON_CODEC: 1255 break; 1256 default: 1257 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 1258 break; 1259 } 1260 } 1261 1262 hfp_run_for_context(hfp_connection); 1263 } 1264 1265 void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 1266 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1267 if (!hfp_connection) { 1268 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1269 return; 1270 } 1271 hfp_release_audio_connection(hfp_connection); 1272 hfp_run_for_context(hfp_connection); 1273 } 1274 1275 void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 1276 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1277 if (!hfp_connection) { 1278 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1279 return; 1280 } 1281 1282 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1283 hfp_connection->hf_answer_incoming_call = 1; 1284 hfp_run_for_context(hfp_connection); 1285 } else { 1286 log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1287 } 1288 } 1289 1290 void hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 1291 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1292 if (!hfp_connection) { 1293 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1294 return; 1295 } 1296 hfp_connection->hf_send_chup = 1; 1297 hfp_run_for_context(hfp_connection); 1298 } 1299 1300 void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 1301 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1302 if (!hfp_connection) { 1303 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1304 return; 1305 } 1306 1307 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1308 hfp_connection->hf_send_chup = 1; 1309 hfp_run_for_context(hfp_connection); 1310 } 1311 } 1312 1313 void hfp_hf_user_busy(hci_con_handle_t acl_handle){ 1314 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1315 if (!hfp_connection) { 1316 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1317 return; 1318 } 1319 1320 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1321 hfp_connection->hf_send_chld_0 = 1; 1322 hfp_run_for_context(hfp_connection); 1323 } 1324 } 1325 1326 void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 1327 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1328 if (!hfp_connection) { 1329 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1330 return; 1331 } 1332 1333 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1334 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1335 hfp_connection->hf_send_chld_1 = 1; 1336 hfp_run_for_context(hfp_connection); 1337 } 1338 } 1339 1340 void hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 1341 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1342 if (!hfp_connection) { 1343 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1344 return; 1345 } 1346 1347 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1348 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1349 hfp_connection->hf_send_chld_2 = 1; 1350 hfp_run_for_context(hfp_connection); 1351 } 1352 } 1353 1354 void hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 1355 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1356 if (!hfp_connection) { 1357 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1358 return; 1359 } 1360 1361 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1362 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1363 hfp_connection->hf_send_chld_3 = 1; 1364 hfp_run_for_context(hfp_connection); 1365 } 1366 } 1367 1368 void hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 1369 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1370 if (!hfp_connection) { 1371 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1372 return; 1373 } 1374 1375 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1376 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1377 hfp_connection->hf_send_chld_4 = 1; 1378 hfp_run_for_context(hfp_connection); 1379 } 1380 } 1381 1382 void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 1383 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1384 if (!hfp_connection) { 1385 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1386 return; 1387 } 1388 1389 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1390 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1391 hfp_connection->hf_send_chld_x = 1; 1392 hfp_connection->hf_send_chld_x_index = 10 + index; 1393 hfp_run_for_context(hfp_connection); 1394 } 1395 } 1396 1397 void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 1398 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1399 if (!hfp_connection) { 1400 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1401 return; 1402 } 1403 1404 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1405 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1406 hfp_connection->hf_send_chld_x = 1; 1407 hfp_connection->hf_send_chld_x_index = 20 + index; 1408 hfp_run_for_context(hfp_connection); 1409 } 1410 } 1411 1412 void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 1413 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1414 if (!hfp_connection) { 1415 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1416 return; 1417 } 1418 1419 hfp_connection->hf_initiate_outgoing_call = 1; 1420 snprintf(phone_number, sizeof(phone_number), "%s", number); 1421 hfp_run_for_context(hfp_connection); 1422 } 1423 1424 void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 1425 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1426 if (!hfp_connection) { 1427 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1428 return; 1429 } 1430 1431 hfp_connection->hf_initiate_memory_dialing = 1; 1432 hfp_connection->memory_id = memory_id; 1433 1434 hfp_run_for_context(hfp_connection); 1435 } 1436 1437 void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 1438 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1439 if (!hfp_connection) { 1440 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1441 return; 1442 } 1443 1444 hfp_connection->hf_initiate_redial_last_number = 1; 1445 hfp_run_for_context(hfp_connection); 1446 } 1447 1448 void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 1449 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1450 if (!hfp_connection) { 1451 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1452 return; 1453 } 1454 1455 hfp_connection->hf_activate_call_waiting_notification = 1; 1456 hfp_run_for_context(hfp_connection); 1457 } 1458 1459 1460 void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 1461 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1462 if (!hfp_connection) { 1463 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1464 return; 1465 } 1466 1467 hfp_connection->hf_deactivate_call_waiting_notification = 1; 1468 hfp_run_for_context(hfp_connection); 1469 } 1470 1471 1472 void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 1473 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1474 if (!hfp_connection) { 1475 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1476 return; 1477 } 1478 1479 hfp_connection->hf_activate_calling_line_notification = 1; 1480 hfp_run_for_context(hfp_connection); 1481 } 1482 1483 void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 1484 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1485 if (!hfp_connection) { 1486 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1487 return; 1488 } 1489 1490 hfp_connection->hf_deactivate_calling_line_notification = 1; 1491 hfp_run_for_context(hfp_connection); 1492 } 1493 1494 1495 void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1496 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1497 if (!hfp_connection) { 1498 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1499 return; 1500 } 1501 1502 hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1; 1503 hfp_run_for_context(hfp_connection); 1504 } 1505 1506 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1507 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1508 if (!hfp_connection) { 1509 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1510 return; 1511 } 1512 1513 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1514 hfp_run_for_context(hfp_connection); 1515 } 1516 1517 void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1518 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1519 if (!hfp_connection) { 1520 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1521 return; 1522 } 1523 1524 hfp_connection->hf_activate_voice_recognition_notification = 1; 1525 hfp_run_for_context(hfp_connection); 1526 } 1527 1528 void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1529 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1530 if (!hfp_connection) { 1531 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1532 return; 1533 } 1534 1535 hfp_connection->hf_deactivate_voice_recognition_notification = 1; 1536 hfp_run_for_context(hfp_connection); 1537 } 1538 1539 void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 1540 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1541 if (!hfp_connection) { 1542 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1543 return; 1544 } 1545 1546 if (hfp_connection->microphone_gain == gain) return; 1547 if (gain < 0 || gain > 15){ 1548 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1549 return; 1550 } 1551 hfp_connection->microphone_gain = gain; 1552 hfp_connection->send_microphone_gain = 1; 1553 hfp_run_for_context(hfp_connection); 1554 } 1555 1556 void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 1557 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1558 if (!hfp_connection) { 1559 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1560 return; 1561 } 1562 1563 if (hfp_connection->speaker_gain == gain) return; 1564 if (gain < 0 || gain > 15){ 1565 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1566 return; 1567 } 1568 hfp_connection->speaker_gain = gain; 1569 hfp_connection->send_speaker_gain = 1; 1570 hfp_run_for_context(hfp_connection); 1571 } 1572 1573 void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 1574 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1575 if (!hfp_connection) { 1576 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1577 return; 1578 } 1579 1580 hfp_connection->hf_send_dtmf_code = code; 1581 hfp_run_for_context(hfp_connection); 1582 } 1583 1584 void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 1585 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1586 if (!hfp_connection) { 1587 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1588 return; 1589 } 1590 hfp_connection->hf_send_binp = 1; 1591 hfp_run_for_context(hfp_connection); 1592 } 1593 1594 void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 1595 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1596 if (!hfp_connection) { 1597 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1598 return; 1599 } 1600 hfp_connection->hf_send_clcc = 1; 1601 hfp_run_for_context(hfp_connection); 1602 } 1603 1604 1605 void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 1606 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1607 if (!hfp_connection) { 1608 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1609 return; 1610 } 1611 hfp_connection->hf_send_rrh = 1; 1612 hfp_connection->hf_send_rrh_command = '?'; 1613 hfp_run_for_context(hfp_connection); 1614 } 1615 1616 void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 1617 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1618 if (!hfp_connection) { 1619 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1620 return; 1621 } 1622 hfp_connection->hf_send_rrh = 1; 1623 hfp_connection->hf_send_rrh_command = '0'; 1624 hfp_run_for_context(hfp_connection); 1625 } 1626 1627 void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 1628 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1629 if (!hfp_connection) { 1630 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1631 return; 1632 } 1633 hfp_connection->hf_send_rrh = 1; 1634 hfp_connection->hf_send_rrh_command = '1'; 1635 hfp_run_for_context(hfp_connection); 1636 } 1637 1638 void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 1639 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1640 if (!hfp_connection) { 1641 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1642 return; 1643 } 1644 hfp_connection->hf_send_rrh = 1; 1645 hfp_connection->hf_send_rrh_command = '2'; 1646 hfp_run_for_context(hfp_connection); 1647 } 1648 1649 void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 1650 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1651 if (!hfp_connection) { 1652 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1653 return; 1654 } 1655 hfp_connection->hf_send_cnum = 1; 1656 hfp_run_for_context(hfp_connection); 1657 } 1658 1659 void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 1660 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1661 if (!hfp_connection) { 1662 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1663 return; 1664 } 1665 // find index for assigned number 1666 int i; 1667 for (i = 0; i < hfp_indicators_nr ; i++){ 1668 if (hfp_indicators[i] == assigned_number){ 1669 // set value 1670 hfp_indicators_value[i] = value; 1671 // mark for update 1672 if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1673 hfp_connection->generic_status_update_bitmap |= (1<<i); 1674 // send update 1675 hfp_run_for_context(hfp_connection); 1676 } 1677 return; 1678 } 1679 } 1680 } 1681 1682 int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 1683 hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1684 if (!hfp_connection) { 1685 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1686 return 0; 1687 } 1688 return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1689 } 1690