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