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