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