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_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 412 hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 413 break; 414 case HFP_NOTIFY_ON_CODECS: 415 hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 416 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 417 break; 418 case HFP_RETRIEVE_INDICATORS: 419 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 420 hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 421 break; 422 case HFP_RETRIEVE_INDICATORS_STATUS: 423 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 424 hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 425 break; 426 case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 427 hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 428 hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 429 break; 430 case HFP_RETRIEVE_CAN_HOLD_CALL: 431 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 432 hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 433 break; 434 case HFP_LIST_GENERIC_STATUS_INDICATORS: 435 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 436 hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 437 break; 438 case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 439 hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 440 hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 441 break; 442 case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 443 hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 444 hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 445 break; 446 default: 447 done = 0; 448 break; 449 } 450 return done; 451 } 452 453 454 static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 455 if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 456 if (hfp_connection->ok_pending) return 0; 457 458 int done = 0; 459 if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 460 hfp_connection->ok_pending = 1; 461 done = 1; 462 hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 463 return done; 464 }; 465 if (hfp_connection->change_status_update_for_individual_ag_indicators){ 466 hfp_connection->ok_pending = 1; 467 done = 1; 468 hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 469 hfp_connection->ag_indicators_status_update_bitmap, 470 hfp_connection->ag_indicators_nr); 471 return done; 472 } 473 474 switch (hfp_connection->hf_query_operator_state){ 475 case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 476 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 477 hfp_connection->ok_pending = 1; 478 hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 479 return 1; 480 case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 481 hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 482 hfp_connection->ok_pending = 1; 483 hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 484 return 1; 485 default: 486 break; 487 } 488 489 if (hfp_connection->enable_extended_audio_gateway_error_report){ 490 hfp_connection->ok_pending = 1; 491 done = 1; 492 hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 493 return done; 494 } 495 496 return done; 497 } 498 499 static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 500 /* events ( == commands): 501 HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 502 HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 503 hf_trigger_codec_connection_setup == received BCC 504 ag_trigger_codec_connection_setup == received from AG to send BCS 505 HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 506 */ 507 508 if (hfp_connection->ok_pending) return 0; 509 510 switch (hfp_connection->command){ 511 case HFP_CMD_AVAILABLE_CODECS: 512 if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0; 513 514 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 515 hfp_connection->ok_pending = 1; 516 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 517 return 1; 518 case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 519 hfp_connection->codec_confirmed = 0; 520 hfp_connection->suggested_codec = 0; 521 hfp_connection->negotiated_codec = 0; 522 523 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 524 hfp_connection->ok_pending = 1; 525 hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 526 break; 527 528 case HFP_CMD_AG_SUGGESTED_CODEC:{ 529 if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){ 530 hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 531 hfp_connection->ok_pending = 1; 532 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 533 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 534 } else { 535 hfp_connection->codec_confirmed = 0; 536 hfp_connection->suggested_codec = 0; 537 hfp_connection->negotiated_codec = 0; 538 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 539 hfp_connection->ok_pending = 1; 540 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 541 542 } 543 break; 544 } 545 default: 546 break; 547 } 548 return 0; 549 } 550 551 static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 552 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 553 hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0; 554 555 556 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){ 557 hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 558 hfp_connection->release_audio_connection = 0; 559 gap_disconnect(hfp_connection->sco_handle); 560 return 1; 561 } 562 563 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 564 565 // run codecs exchange 566 int done = codecs_exchange_state_machine(hfp_connection); 567 if (done) return 1; 568 569 return 0; 570 } 571 572 static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 573 if (hfp_connection->hf_answer_incoming_call){ 574 hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 575 hfp_connection->hf_answer_incoming_call = 0; 576 return 1; 577 } 578 return 0; 579 } 580 581 static void hfp_run_for_context(hfp_connection_t * hfp_connection){ 582 if (!hfp_connection) return; 583 if (!hfp_connection->rfcomm_cid) return; 584 585 if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){ 586 587 hfp_connection->hf_accept_sco = 0; 588 589 // notify about codec selection if not done already 590 if (hfp_connection->negotiated_codec == 0){ 591 hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 592 } 593 594 // remote supported feature eSCO is set if link type is eSCO 595 // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 596 uint16_t max_latency; 597 uint8_t retransmission_effort; 598 uint16_t packet_types; 599 600 if (hci_remote_esco_supported(hfp_connection->acl_handle)){ 601 max_latency = 0x000c; 602 retransmission_effort = 0x02; 603 packet_types = 0x388; 604 } else { 605 max_latency = 0xffff; 606 retransmission_effort = 0xff; 607 packet_types = 0x003f; 608 } 609 610 uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 611 if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 612 sco_voice_setting = 0x0043; // Transparent data 613 } 614 615 log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting); 616 hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 617 sco_voice_setting, retransmission_effort, packet_types); 618 return; 619 } 620 621 if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return; 622 623 int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 624 if (!done){ 625 done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 626 } 627 if (!done){ 628 done = hfp_hf_run_for_audio_connection(hfp_connection); 629 } 630 if (!done){ 631 done = call_setup_state_machine(hfp_connection); 632 } 633 634 if (hfp_connection->send_microphone_gain){ 635 hfp_connection->send_microphone_gain = 0; 636 hfp_connection->ok_pending = 1; 637 hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 638 return; 639 } 640 641 if (hfp_connection->send_speaker_gain){ 642 hfp_connection->send_speaker_gain = 0; 643 hfp_connection->ok_pending = 1; 644 hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 645 return; 646 } 647 648 if (hfp_connection->hf_deactivate_calling_line_notification){ 649 hfp_connection->hf_deactivate_calling_line_notification = 0; 650 hfp_connection->ok_pending = 1; 651 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 652 return; 653 } 654 655 if (hfp_connection->hf_activate_calling_line_notification){ 656 hfp_connection->hf_activate_calling_line_notification = 0; 657 hfp_connection->ok_pending = 1; 658 hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 659 return; 660 } 661 662 if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 663 hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 664 hfp_connection->ok_pending = 1; 665 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0); 666 return; 667 } 668 669 if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){ 670 hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0; 671 hfp_connection->ok_pending = 1; 672 hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1); 673 return; 674 } 675 676 if (hfp_connection->hf_deactivate_voice_recognition_notification){ 677 hfp_connection->hf_deactivate_voice_recognition_notification = 0; 678 hfp_connection->ok_pending = 1; 679 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 680 return; 681 } 682 683 if (hfp_connection->hf_activate_voice_recognition_notification){ 684 hfp_connection->hf_activate_voice_recognition_notification = 0; 685 hfp_connection->ok_pending = 1; 686 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 687 return; 688 } 689 690 691 if (hfp_connection->hf_deactivate_call_waiting_notification){ 692 hfp_connection->hf_deactivate_call_waiting_notification = 0; 693 hfp_connection->ok_pending = 1; 694 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 695 return; 696 } 697 698 if (hfp_connection->hf_activate_call_waiting_notification){ 699 hfp_connection->hf_activate_call_waiting_notification = 0; 700 hfp_connection->ok_pending = 1; 701 hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 702 return; 703 } 704 705 if (hfp_connection->hf_initiate_outgoing_call){ 706 hfp_connection->hf_initiate_outgoing_call = 0; 707 hfp_connection->ok_pending = 1; 708 hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 709 return; 710 } 711 712 if (hfp_connection->hf_initiate_memory_dialing){ 713 hfp_connection->hf_initiate_memory_dialing = 0; 714 hfp_connection->ok_pending = 1; 715 hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 716 return; 717 } 718 719 if (hfp_connection->hf_initiate_redial_last_number){ 720 hfp_connection->hf_initiate_redial_last_number = 0; 721 hfp_connection->ok_pending = 1; 722 hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 723 return; 724 } 725 726 if (hfp_connection->hf_send_chup){ 727 hfp_connection->hf_send_chup = 0; 728 hfp_connection->ok_pending = 1; 729 hfp_hf_send_chup(hfp_connection->rfcomm_cid); 730 return; 731 } 732 733 if (hfp_connection->hf_send_chld_0){ 734 hfp_connection->hf_send_chld_0 = 0; 735 hfp_connection->ok_pending = 1; 736 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 737 return; 738 } 739 740 if (hfp_connection->hf_send_chld_1){ 741 hfp_connection->hf_send_chld_1 = 0; 742 hfp_connection->ok_pending = 1; 743 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 744 return; 745 } 746 747 if (hfp_connection->hf_send_chld_2){ 748 hfp_connection->hf_send_chld_2 = 0; 749 hfp_connection->ok_pending = 1; 750 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 751 return; 752 } 753 754 if (hfp_connection->hf_send_chld_3){ 755 hfp_connection->hf_send_chld_3 = 0; 756 hfp_connection->ok_pending = 1; 757 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 758 return; 759 } 760 761 if (hfp_connection->hf_send_chld_4){ 762 hfp_connection->hf_send_chld_4 = 0; 763 hfp_connection->ok_pending = 1; 764 hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 765 return; 766 } 767 768 if (hfp_connection->hf_send_chld_x){ 769 hfp_connection->hf_send_chld_x = 0; 770 hfp_connection->ok_pending = 1; 771 hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 772 return; 773 } 774 775 if (hfp_connection->hf_send_dtmf_code){ 776 char code = hfp_connection->hf_send_dtmf_code; 777 hfp_connection->hf_send_dtmf_code = 0; 778 hfp_connection->ok_pending = 1; 779 hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 780 return; 781 } 782 783 if (hfp_connection->hf_send_binp){ 784 hfp_connection->hf_send_binp = 0; 785 hfp_connection->ok_pending = 1; 786 hfp_hf_send_binp(hfp_connection->rfcomm_cid); 787 return; 788 } 789 790 if (hfp_connection->hf_send_clcc){ 791 hfp_connection->hf_send_clcc = 0; 792 hfp_connection->ok_pending = 1; 793 hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 794 return; 795 } 796 797 if (hfp_connection->hf_send_rrh){ 798 hfp_connection->hf_send_rrh = 0; 799 char buffer[20]; 800 switch (hfp_connection->hf_send_rrh_command){ 801 case '?': 802 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD); 803 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 804 return; 805 case '0': 806 case '1': 807 case '2': 808 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command); 809 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 810 return; 811 default: 812 break; 813 } 814 return; 815 } 816 817 if (hfp_connection->hf_send_cnum){ 818 hfp_connection->hf_send_cnum = 0; 819 char buffer[20]; 820 sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION); 821 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 822 return; 823 } 824 825 // update HF indicators 826 if (hfp_connection->generic_status_update_bitmap){ 827 int i; 828 for (i=0;i<hfp_indicators_nr;i++){ 829 if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 830 if (hfp_connection->generic_status_indicators[i].state){ 831 hfp_connection->ok_pending = 1; 832 hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 833 char buffer[30]; 834 sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], hfp_indicators_value[i]); 835 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 836 } else { 837 log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]); 838 } 839 return; 840 } 841 } 842 } 843 844 if (done) return; 845 // deal with disconnect 846 switch (hfp_connection->state){ 847 case HFP_W2_DISCONNECT_RFCOMM: 848 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 849 rfcomm_disconnect(hfp_connection->rfcomm_cid); 850 break; 851 852 default: 853 break; 854 } 855 } 856 857 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 858 hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 859 860 hfp_emit_slc_connection_event(hfp_callback, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 861 862 // restore volume settings 863 hfp_connection->speaker_gain = hfp_hf_speaker_gain; 864 hfp_connection->send_speaker_gain = 1; 865 hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 866 hfp_connection->microphone_gain = hfp_hf_microphone_gain; 867 hfp_connection->send_microphone_gain = 1; 868 hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 869 // enable all indicators 870 int i; 871 for (i=0;i<hfp_indicators_nr;i++){ 872 hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i]; 873 hfp_connection->generic_status_indicators[i].state = 1; 874 } 875 } 876 877 static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){ 878 hfp_connection->ok_pending = 0; 879 switch (hfp_connection->state){ 880 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 881 if (has_codec_negotiation_feature(hfp_connection)){ 882 hfp_connection->state = HFP_NOTIFY_ON_CODECS; 883 break; 884 } 885 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 886 break; 887 888 case HFP_W4_NOTIFY_ON_CODECS: 889 hfp_connection->state = HFP_RETRIEVE_INDICATORS; 890 break; 891 892 case HFP_W4_RETRIEVE_INDICATORS: 893 hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 894 break; 895 896 case HFP_W4_RETRIEVE_INDICATORS_STATUS: 897 hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 898 break; 899 900 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 901 if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 902 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 903 break; 904 } 905 if (has_hf_indicators_feature(hfp_connection)){ 906 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 907 break; 908 } 909 hfp_ag_slc_established(hfp_connection); 910 break; 911 912 case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 913 if (has_hf_indicators_feature(hfp_connection)){ 914 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 915 break; 916 } 917 hfp_ag_slc_established(hfp_connection); 918 break; 919 920 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 921 hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 922 break; 923 924 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 925 hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 926 break; 927 928 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 929 hfp_ag_slc_established(hfp_connection); 930 break; 931 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 932 if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 933 hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 934 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0); 935 break; 936 } 937 938 if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 939 hfp_connection->change_status_update_for_individual_ag_indicators = 0; 940 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0); 941 break; 942 } 943 944 switch (hfp_connection->hf_query_operator_state){ 945 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 946 // printf("Format set, querying name\n"); 947 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 948 break; 949 case HPF_HF_QUERY_OPERATOR_W4_RESULT: 950 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 951 hfp_emit_network_operator_event(hfp_callback, hfp_connection->network_operator); 952 break; 953 default: 954 break; 955 } 956 957 if (hfp_connection->enable_extended_audio_gateway_error_report){ 958 hfp_connection->enable_extended_audio_gateway_error_report = 0; 959 break; 960 } 961 962 switch (hfp_connection->codecs_state){ 963 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 964 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 965 break; 966 case HFP_CODECS_HF_CONFIRMED_CODEC: 967 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 968 hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 969 log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD"); 970 break; 971 default: 972 break; 973 } 974 break; 975 default: 976 break; 977 } 978 979 // done 980 hfp_connection->command = HFP_CMD_NONE; 981 } 982 983 984 static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 985 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 986 if (!hfp_connection) return; 987 988 char last_char = packet[size-1]; 989 packet[size-1] = 0; 990 log_info("HFP_RX %s", packet); 991 packet[size-1] = last_char; 992 993 int pos, i, value; 994 for (pos = 0; pos < size ; pos++){ 995 hfp_parse(hfp_connection, packet[pos], 1); 996 } 997 998 switch (hfp_connection->command){ 999 case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1000 hfp_connection->command = HFP_CMD_NONE; 1001 // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type); 1002 hfp_hf_emit_subscriber_information(hfp_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number); 1003 break; 1004 case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1005 hfp_connection->command = HFP_CMD_NONE; 1006 // printf("Response and Hold status: %s\n", hfp_connection->line_buffer); 1007 hfp_emit_event(hfp_callback, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, atoi((char *)&hfp_connection->line_buffer[0])); 1008 break; 1009 case HFP_CMD_LIST_CURRENT_CALLS: 1010 hfp_connection->command = HFP_CMD_NONE; 1011 // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n", 1012 // hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty, 1013 // hfp_connection->bnip_number, hfp_connection->bnip_type); 1014 hfp_hf_emit_enhanced_call_status(hfp_callback, hfp_connection->clcc_idx, 1015 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty, 1016 hfp_connection->bnip_type, hfp_connection->bnip_number); 1017 break; 1018 case HFP_CMD_SET_SPEAKER_GAIN: 1019 hfp_connection->command = HFP_CMD_NONE; 1020 value = atoi((char*)hfp_connection->line_buffer); 1021 hfp_hf_speaker_gain = value; 1022 hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1023 break; 1024 case HFP_CMD_SET_MICROPHONE_GAIN: 1025 hfp_connection->command = HFP_CMD_NONE; 1026 value = atoi((char*)hfp_connection->line_buffer); 1027 hfp_hf_microphone_gain = value; 1028 hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1029 break; 1030 case HFP_CMD_AG_SENT_PHONE_NUMBER: 1031 hfp_connection->command = HFP_CMD_NONE; 1032 hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1033 break; 1034 case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1035 hfp_connection->command = HFP_CMD_NONE; 1036 hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1037 break; 1038 case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1039 hfp_connection->command = HFP_CMD_NONE; 1040 hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALLING_LINE_INDETIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1041 break; 1042 case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1043 hfp_connection->ok_pending = 0; 1044 hfp_connection->command = HFP_CMD_NONE; 1045 hfp_connection->extended_audio_gateway_error = 0; 1046 hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1047 break; 1048 case HFP_CMD_ERROR: 1049 hfp_connection->ok_pending = 0; 1050 hfp_reset_context_flags(hfp_connection); 1051 hfp_connection->command = HFP_CMD_NONE; 1052 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1); 1053 break; 1054 case HFP_CMD_OK: 1055 hfp_hf_switch_on_ok(hfp_connection); 1056 break; 1057 case HFP_CMD_RING: 1058 hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_RING); 1059 break; 1060 case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1061 for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1062 if (hfp_connection->ag_indicators[i].status_changed) { 1063 if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1064 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1065 } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1066 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 1067 } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1068 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1069 } 1070 hfp_connection->ag_indicators[i].status_changed = 0; 1071 hfp_emit_ag_indicator_event(hfp_callback, hfp_connection->ag_indicators[i]); 1072 break; 1073 } 1074 } 1075 break; 1076 default: 1077 break; 1078 } 1079 hfp_run_for_context(hfp_connection); 1080 } 1081 1082 static void hfp_run(void){ 1083 btstack_linked_list_iterator_t it; 1084 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1085 while (btstack_linked_list_iterator_has_next(&it)){ 1086 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1087 hfp_run_for_context(hfp_connection); 1088 } 1089 } 1090 1091 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1092 switch (packet_type){ 1093 case RFCOMM_DATA_PACKET: 1094 hfp_handle_rfcomm_event(packet_type, channel, packet, size); 1095 break; 1096 case HCI_EVENT_PACKET: 1097 hfp_handle_hci_event(packet_type, channel, packet, size); 1098 break; 1099 default: 1100 break; 1101 } 1102 hfp_run(); 1103 } 1104 1105 void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1106 // register for HCI events 1107 hci_event_callback_registration.callback = &packet_handler; 1108 hci_add_event_handler(&hci_event_callback_registration); 1109 1110 rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff); 1111 1112 hfp_set_packet_handler_for_rfcomm_connections(&packet_handler); 1113 1114 hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1115 hfp_codecs_nr = 0; 1116 hfp_indicators_nr = 0; 1117 hfp_hf_speaker_gain = 9; 1118 hfp_hf_microphone_gain = 9; 1119 } 1120 1121 void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){ 1122 if (codecs_nr > HFP_MAX_NUM_CODECS){ 1123 log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 1124 return; 1125 } 1126 1127 hfp_codecs_nr = codecs_nr; 1128 int i; 1129 for (i=0; i<codecs_nr; i++){ 1130 hfp_codecs[i] = codecs[i]; 1131 } 1132 1133 char buffer[30]; 1134 int offset = join(buffer, sizeof(buffer), hfp_codecs, hfp_codecs_nr); 1135 buffer[offset] = 0; 1136 btstack_linked_list_iterator_t it; 1137 btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1138 while (btstack_linked_list_iterator_has_next(&it)){ 1139 hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1140 if (! hfp_connection) continue; 1141 hfp_connection->command = HFP_CMD_AVAILABLE_CODECS; 1142 hfp_run_for_context(hfp_connection); 1143 } 1144 } 1145 1146 void hfp_hf_init_supported_features(uint32_t supported_features){ 1147 hfp_supported_features = supported_features; 1148 } 1149 1150 void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){ 1151 hfp_indicators_nr = indicators_nr; 1152 int i; 1153 for (i = 0; i < hfp_indicators_nr ; i++){ 1154 hfp_indicators[i] = indicators[i]; 1155 } 1156 } 1157 1158 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1159 hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway); 1160 } 1161 1162 void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 1163 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1164 if (!hfp_connection) { 1165 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1166 return; 1167 } 1168 hfp_release_service_level_connection(hfp_connection); 1169 hfp_run_for_context(hfp_connection); 1170 } 1171 1172 static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 1173 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1174 if (!hfp_connection) { 1175 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1176 return; 1177 } 1178 hfp_connection->enable_status_update_for_ag_indicators = enable; 1179 hfp_run_for_context(hfp_connection); 1180 } 1181 1182 void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1183 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1184 } 1185 1186 void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1187 hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1188 } 1189 1190 // TODO: returned ERROR - wrong format 1191 void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 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 hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1198 hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1199 hfp_run_for_context(hfp_connection); 1200 } 1201 1202 void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 1203 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1204 if (!hfp_connection) { 1205 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1206 return; 1207 } 1208 switch (hfp_connection->hf_query_operator_state){ 1209 case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1210 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1211 break; 1212 case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1213 hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1214 break; 1215 default: 1216 break; 1217 } 1218 hfp_run_for_context(hfp_connection); 1219 } 1220 1221 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 1222 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1223 if (!hfp_connection) { 1224 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1225 return; 1226 } 1227 hfp_connection->enable_extended_audio_gateway_error_report = enable; 1228 hfp_run_for_context(hfp_connection); 1229 } 1230 1231 1232 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1233 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1234 } 1235 1236 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1237 hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1238 } 1239 1240 1241 void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 1242 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1243 if (!hfp_connection) { 1244 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1245 return; 1246 } 1247 hfp_connection->establish_audio_connection = 0; 1248 1249 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1250 if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 1251 1252 if (!has_codec_negotiation_feature(hfp_connection)){ 1253 log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); 1254 hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1255 hfp_connection->establish_audio_connection = 1; 1256 } else { 1257 switch (hfp_connection->codecs_state){ 1258 case HFP_CODECS_W4_AG_COMMON_CODEC: 1259 break; 1260 default: 1261 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 1262 break; 1263 } 1264 } 1265 1266 hfp_run_for_context(hfp_connection); 1267 } 1268 1269 void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 1270 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1271 if (!hfp_connection) { 1272 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1273 return; 1274 } 1275 hfp_release_audio_connection(hfp_connection); 1276 hfp_run_for_context(hfp_connection); 1277 } 1278 1279 void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 1280 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1281 if (!hfp_connection) { 1282 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1283 return; 1284 } 1285 1286 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1287 hfp_connection->hf_answer_incoming_call = 1; 1288 hfp_run_for_context(hfp_connection); 1289 } else { 1290 log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1291 } 1292 } 1293 1294 void hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 1295 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1296 if (!hfp_connection) { 1297 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1298 return; 1299 } 1300 hfp_connection->hf_send_chup = 1; 1301 hfp_run_for_context(hfp_connection); 1302 } 1303 1304 void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 1305 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1306 if (!hfp_connection) { 1307 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1308 return; 1309 } 1310 1311 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1312 hfp_connection->hf_send_chup = 1; 1313 hfp_run_for_context(hfp_connection); 1314 } 1315 } 1316 1317 void hfp_hf_user_busy(hci_con_handle_t acl_handle){ 1318 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1319 if (!hfp_connection) { 1320 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1321 return; 1322 } 1323 1324 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1325 hfp_connection->hf_send_chld_0 = 1; 1326 hfp_run_for_context(hfp_connection); 1327 } 1328 } 1329 1330 void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 1331 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1332 if (!hfp_connection) { 1333 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1334 return; 1335 } 1336 1337 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1338 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1339 hfp_connection->hf_send_chld_1 = 1; 1340 hfp_run_for_context(hfp_connection); 1341 } 1342 } 1343 1344 void hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 1345 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1346 if (!hfp_connection) { 1347 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1348 return; 1349 } 1350 1351 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1352 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1353 hfp_connection->hf_send_chld_2 = 1; 1354 hfp_run_for_context(hfp_connection); 1355 } 1356 } 1357 1358 void hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 1359 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1360 if (!hfp_connection) { 1361 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1362 return; 1363 } 1364 1365 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1366 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1367 hfp_connection->hf_send_chld_3 = 1; 1368 hfp_run_for_context(hfp_connection); 1369 } 1370 } 1371 1372 void hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 1373 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1374 if (!hfp_connection) { 1375 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1376 return; 1377 } 1378 1379 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1380 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1381 hfp_connection->hf_send_chld_4 = 1; 1382 hfp_run_for_context(hfp_connection); 1383 } 1384 } 1385 1386 void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 1387 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1388 if (!hfp_connection) { 1389 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1390 return; 1391 } 1392 1393 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1394 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1395 hfp_connection->hf_send_chld_x = 1; 1396 hfp_connection->hf_send_chld_x_index = 10 + index; 1397 hfp_run_for_context(hfp_connection); 1398 } 1399 } 1400 1401 void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 1402 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1403 if (!hfp_connection) { 1404 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1405 return; 1406 } 1407 1408 if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1409 hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1410 hfp_connection->hf_send_chld_x = 1; 1411 hfp_connection->hf_send_chld_x_index = 20 + index; 1412 hfp_run_for_context(hfp_connection); 1413 } 1414 } 1415 1416 void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 1417 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1418 if (!hfp_connection) { 1419 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1420 return; 1421 } 1422 1423 hfp_connection->hf_initiate_outgoing_call = 1; 1424 snprintf(phone_number, sizeof(phone_number), "%s", number); 1425 hfp_run_for_context(hfp_connection); 1426 } 1427 1428 void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 1429 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1430 if (!hfp_connection) { 1431 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1432 return; 1433 } 1434 1435 hfp_connection->hf_initiate_memory_dialing = 1; 1436 hfp_connection->memory_id = memory_id; 1437 1438 hfp_run_for_context(hfp_connection); 1439 } 1440 1441 void hfp_hf_redial_last_number(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_initiate_redial_last_number = 1; 1449 hfp_run_for_context(hfp_connection); 1450 } 1451 1452 void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 1453 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1454 if (!hfp_connection) { 1455 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1456 return; 1457 } 1458 1459 hfp_connection->hf_activate_call_waiting_notification = 1; 1460 hfp_run_for_context(hfp_connection); 1461 } 1462 1463 1464 void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 1465 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1466 if (!hfp_connection) { 1467 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1468 return; 1469 } 1470 1471 hfp_connection->hf_deactivate_call_waiting_notification = 1; 1472 hfp_run_for_context(hfp_connection); 1473 } 1474 1475 1476 void hfp_hf_activate_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_activate_calling_line_notification = 1; 1484 hfp_run_for_context(hfp_connection); 1485 } 1486 1487 void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 1488 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1489 if (!hfp_connection) { 1490 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1491 return; 1492 } 1493 1494 hfp_connection->hf_deactivate_calling_line_notification = 1; 1495 hfp_run_for_context(hfp_connection); 1496 } 1497 1498 1499 void hfp_hf_activate_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_activate_echo_canceling_and_noise_reduction = 1; 1507 hfp_run_for_context(hfp_connection); 1508 } 1509 1510 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(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_deactivate_echo_canceling_and_noise_reduction = 1; 1518 hfp_run_for_context(hfp_connection); 1519 } 1520 1521 void hfp_hf_activate_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_activate_voice_recognition_notification = 1; 1529 hfp_run_for_context(hfp_connection); 1530 } 1531 1532 void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){ 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 hfp_connection->hf_deactivate_voice_recognition_notification = 1; 1540 hfp_run_for_context(hfp_connection); 1541 } 1542 1543 void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 1544 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1545 if (!hfp_connection) { 1546 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1547 return; 1548 } 1549 1550 if (hfp_connection->microphone_gain == gain) return; 1551 if (gain < 0 || gain > 15){ 1552 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1553 return; 1554 } 1555 hfp_connection->microphone_gain = gain; 1556 hfp_connection->send_microphone_gain = 1; 1557 hfp_run_for_context(hfp_connection); 1558 } 1559 1560 void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 1561 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1562 if (!hfp_connection) { 1563 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1564 return; 1565 } 1566 1567 if (hfp_connection->speaker_gain == gain) return; 1568 if (gain < 0 || gain > 15){ 1569 log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1570 return; 1571 } 1572 hfp_connection->speaker_gain = gain; 1573 hfp_connection->send_speaker_gain = 1; 1574 hfp_run_for_context(hfp_connection); 1575 } 1576 1577 void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 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 1584 hfp_connection->hf_send_dtmf_code = code; 1585 hfp_run_for_context(hfp_connection); 1586 } 1587 1588 void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 1589 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1590 if (!hfp_connection) { 1591 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1592 return; 1593 } 1594 hfp_connection->hf_send_binp = 1; 1595 hfp_run_for_context(hfp_connection); 1596 } 1597 1598 void hfp_hf_query_current_call_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_clcc = 1; 1605 hfp_run_for_context(hfp_connection); 1606 } 1607 1608 1609 void hfp_hf_rrh_query_status(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 = '?'; 1617 hfp_run_for_context(hfp_connection); 1618 } 1619 1620 void hfp_hf_rrh_hold_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 = '0'; 1628 hfp_run_for_context(hfp_connection); 1629 } 1630 1631 void hfp_hf_rrh_accept_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 = '1'; 1639 hfp_run_for_context(hfp_connection); 1640 } 1641 1642 void hfp_hf_rrh_reject_held_call(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_rrh = 1; 1649 hfp_connection->hf_send_rrh_command = '2'; 1650 hfp_run_for_context(hfp_connection); 1651 } 1652 1653 void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 1654 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1655 if (!hfp_connection) { 1656 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1657 return; 1658 } 1659 hfp_connection->hf_send_cnum = 1; 1660 hfp_run_for_context(hfp_connection); 1661 } 1662 1663 void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 1664 hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1665 if (!hfp_connection) { 1666 log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1667 return; 1668 } 1669 // find index for assigned number 1670 int i; 1671 for (i = 0; i < hfp_indicators_nr ; i++){ 1672 if (hfp_indicators[i] == assigned_number){ 1673 // set value 1674 hfp_indicators_value[i] = value; 1675 // mark for update 1676 if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1677 hfp_connection->generic_status_update_bitmap |= (1<<i); 1678 // send update 1679 hfp_run_for_context(hfp_connection); 1680 } 1681 return; 1682 } 1683 } 1684 } 1685 1686