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