13deb3ec6SMatthias Ringwald /* 23deb3ec6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 33deb3ec6SMatthias Ringwald * 43deb3ec6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 53deb3ec6SMatthias Ringwald * modification, are permitted provided that the following conditions 63deb3ec6SMatthias Ringwald * are met: 73deb3ec6SMatthias Ringwald * 83deb3ec6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 93deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 103deb3ec6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 113deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 123deb3ec6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 133deb3ec6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 143deb3ec6SMatthias Ringwald * contributors may be used to endorse or promote products derived 153deb3ec6SMatthias Ringwald * from this software without specific prior written permission. 163deb3ec6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 173deb3ec6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 183deb3ec6SMatthias Ringwald * monetary gain. 193deb3ec6SMatthias Ringwald * 203deb3ec6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 213deb3ec6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 223deb3ec6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 233deb3ec6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 243deb3ec6SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313deb3ec6SMatthias Ringwald * SUCH DAMAGE. 323deb3ec6SMatthias Ringwald * 333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at 343deb3ec6SMatthias Ringwald * [email protected] 353deb3ec6SMatthias Ringwald * 363deb3ec6SMatthias Ringwald */ 373deb3ec6SMatthias Ringwald 383deb3ec6SMatthias Ringwald // ***************************************************************************** 393deb3ec6SMatthias Ringwald // 40fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit 413deb3ec6SMatthias Ringwald // 423deb3ec6SMatthias Ringwald // ***************************************************************************** 433deb3ec6SMatthias Ringwald 447907f069SMatthias Ringwald #include "btstack_config.h" 453deb3ec6SMatthias Ringwald 463deb3ec6SMatthias Ringwald #include <stdint.h> 473deb3ec6SMatthias Ringwald #include <stdio.h> 483deb3ec6SMatthias Ringwald #include <stdlib.h> 493deb3ec6SMatthias Ringwald #include <string.h> 503deb3ec6SMatthias Ringwald 5159c6af15SMatthias Ringwald #include "btstack_debug.h" 523deb3ec6SMatthias Ringwald #include "btstack_memory.h" 5359c6af15SMatthias Ringwald #include "btstack_run_loop.h" 5459c6af15SMatthias Ringwald #include "classic/core.h" 5559c6af15SMatthias Ringwald #include "classic/hfp.h" 5659c6af15SMatthias Ringwald #include "classic/hfp_hf.h" 57efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 58746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 59023f2764SMatthias Ringwald #include "classic/sdp_util.h" 6059c6af15SMatthias Ringwald #include "hci.h" 6159c6af15SMatthias Ringwald #include "hci_cmd.h" 6259c6af15SMatthias Ringwald #include "hci_dump.h" 6359c6af15SMatthias Ringwald #include "l2cap.h" 643deb3ec6SMatthias Ringwald 653deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit"; 663deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 673deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0; 683deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 693deb3ec6SMatthias Ringwald 703deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0; 713deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS]; 72667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS]; 73667ec068SMatthias Ringwald 74667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9; 75667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9; 763deb3ec6SMatthias Ringwald 7713839019SMatthias Ringwald static btstack_packet_handler_t hfp_callback; 783deb3ec6SMatthias Ringwald 79ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status; 80ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status; 81ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status; 82ce263fc8SMatthias Ringwald 83ce263fc8SMatthias Ringwald static char phone_number[25]; 84ce263fc8SMatthias Ringwald 85c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 86c5b64319SMatthias Ringwald 8713839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 883deb3ec6SMatthias Ringwald hfp_callback = callback; 893deb3ec6SMatthias Ringwald if (callback == NULL){ 903deb3ec6SMatthias Ringwald log_error("hfp_hf_register_packet_handler called with NULL callback"); 913deb3ec6SMatthias Ringwald return; 923deb3ec6SMatthias Ringwald } 933deb3ec6SMatthias Ringwald hfp_callback = callback; 94f4000eebSMatthias Ringwald hfp_set_callback(callback); 953deb3ec6SMatthias Ringwald } 963deb3ec6SMatthias Ringwald 9713839019SMatthias Ringwald 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){ 98a0ffb263SMatthias Ringwald if (!callback) return; 99a0ffb263SMatthias Ringwald uint8_t event[31]; 100a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 101a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 102a0ffb263SMatthias Ringwald event[2] = event_subtype; 103a0ffb263SMatthias Ringwald event[3] = status; 104a0ffb263SMatthias Ringwald event[4] = bnip_type; 1057c959318SMatthias Ringwald int size = (strlen(bnip_number) < sizeof(event) - 6) ? (int) strlen(bnip_number) : sizeof(event) - 6; 106a0ffb263SMatthias Ringwald strncpy((char*)&event[5], bnip_number, size); 107a0ffb263SMatthias Ringwald event[5 + size] = 0; 10813839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 109a0ffb263SMatthias Ringwald } 110a0ffb263SMatthias Ringwald 11113839019SMatthias Ringwald 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){ 112a0ffb263SMatthias Ringwald if (!callback) return; 113a0ffb263SMatthias Ringwald uint8_t event[30]; 114a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 115a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 116a0ffb263SMatthias Ringwald event[2] = event_subtype; 117a0ffb263SMatthias Ringwald event[3] = bnip_type; 1187c959318SMatthias Ringwald int size = (strlen(bnip_number) < sizeof(event) - 5) ? (int) strlen(bnip_number) : sizeof(event) - 5; 119a0ffb263SMatthias Ringwald strncpy((char*)&event[4], bnip_number, size); 120a0ffb263SMatthias Ringwald event[4 + size] = 0; 12113839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 122a0ffb263SMatthias Ringwald } 123a0ffb263SMatthias Ringwald 12413839019SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir, 125a0ffb263SMatthias Ringwald uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){ 126a0ffb263SMatthias Ringwald if (!callback) return; 127a0ffb263SMatthias Ringwald uint8_t event[35]; 128a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 129a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 130a0ffb263SMatthias Ringwald event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 131a0ffb263SMatthias Ringwald event[3] = clcc_idx; 132a0ffb263SMatthias Ringwald event[4] = clcc_dir; 133a0ffb263SMatthias Ringwald event[6] = clcc_status; 134a0ffb263SMatthias Ringwald event[7] = clcc_mpty; 135a0ffb263SMatthias Ringwald event[8] = bnip_type; 1367c959318SMatthias Ringwald int size = (strlen(bnip_number) < sizeof(event) - 10) ? (int) strlen(bnip_number) : sizeof(event) - 10; 137a0ffb263SMatthias Ringwald strncpy((char*)&event[9], bnip_number, size); 138a0ffb263SMatthias Ringwald event[9 + size] = 0; 13913839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 140a0ffb263SMatthias Ringwald } 141a0ffb263SMatthias Ringwald 142a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 1433deb3ec6SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 144a0ffb263SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 1453deb3ec6SMatthias Ringwald return hf && ag; 1463deb3ec6SMatthias Ringwald } 1473deb3ec6SMatthias Ringwald 148a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 1493deb3ec6SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING); 150a0ffb263SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING); 1513deb3ec6SMatthias Ringwald return hf && ag; 1523deb3ec6SMatthias Ringwald } 1533deb3ec6SMatthias Ringwald 1543deb3ec6SMatthias Ringwald 155a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 1563deb3ec6SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS); 157a0ffb263SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS); 1583deb3ec6SMatthias Ringwald return hf && ag; 1593deb3ec6SMatthias Ringwald } 1603deb3ec6SMatthias Ringwald 161ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 1623deb3ec6SMatthias Ringwald 1634f84bf36SMatthias Ringwald 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){ 1643deb3ec6SMatthias Ringwald if (!name){ 1653deb3ec6SMatthias Ringwald name = default_hfp_hf_service_name; 1663deb3ec6SMatthias Ringwald } 1672ef54b27SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, SDP_Handsfree, rfcomm_channel_nr, name); 1683deb3ec6SMatthias Ringwald 1694f84bf36SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 1704f84bf36SMatthias Ringwald // 1714f84bf36SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 1724f84bf36SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 1734f84bf36SMatthias Ringwald // 1744f84bf36SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 1754f84bf36SMatthias Ringwald if (supported_features & wide_band_speech){ 1764f84bf36SMatthias Ringwald sdp_features |= 1 << 5; // Wide band speech bit 1774f84bf36SMatthias Ringwald } 178aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 1794f84bf36SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 180aa4dd815SMatthias Ringwald } 1813deb3ec6SMatthias Ringwald 1823deb3ec6SMatthias Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 1833deb3ec6SMatthias Ringwald char buffer[20]; 1843deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features); 1853deb3ec6SMatthias Ringwald // printf("exchange_supported_features %s\n", buffer); 1863deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 1873deb3ec6SMatthias Ringwald } 1883deb3ec6SMatthias Ringwald 1893deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 1903deb3ec6SMatthias Ringwald char buffer[30]; 1913deb3ec6SMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_AVAILABLE_CODECS); 1923deb3ec6SMatthias Ringwald offset += join(buffer+offset, sizeof(buffer)-offset, hfp_codecs, hfp_codecs_nr); 1933deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 1943deb3ec6SMatthias Ringwald buffer[offset] = 0; 1953deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 1963deb3ec6SMatthias Ringwald } 1973deb3ec6SMatthias Ringwald 1983deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){ 1993deb3ec6SMatthias Ringwald char buffer[20]; 2003deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=?\r\n", HFP_INDICATOR); 2013deb3ec6SMatthias Ringwald // printf("retrieve_indicators %s\n", buffer); 2023deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2033deb3ec6SMatthias Ringwald } 2043deb3ec6SMatthias Ringwald 2053deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){ 2063deb3ec6SMatthias Ringwald char buffer[20]; 2073deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_INDICATOR); 2083deb3ec6SMatthias Ringwald // printf("retrieve_indicators_status %s\n", buffer); 2093deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2103deb3ec6SMatthias Ringwald } 2113deb3ec6SMatthias Ringwald 2123deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 2133deb3ec6SMatthias Ringwald char buffer[20]; 2143deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 2153deb3ec6SMatthias Ringwald // printf("toggle_indicator_status_update %s\n", buffer); 2163deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2173deb3ec6SMatthias Ringwald } 2183deb3ec6SMatthias Ringwald 2193deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 2203deb3ec6SMatthias Ringwald char buffer[50]; 2213deb3ec6SMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 2223deb3ec6SMatthias Ringwald offset += join_bitmap(buffer+offset, sizeof(buffer)-offset, indicators_status, indicators_nr); 2233deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 2243deb3ec6SMatthias Ringwald buffer[offset] = 0; 2253deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2263deb3ec6SMatthias Ringwald } 2273deb3ec6SMatthias Ringwald 2283deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 2293deb3ec6SMatthias Ringwald char buffer[20]; 2303deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=?\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 2313deb3ec6SMatthias Ringwald // printf("retrieve_can_hold_call %s\n", buffer); 2323deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2333deb3ec6SMatthias Ringwald } 2343deb3ec6SMatthias Ringwald 2353deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 2363deb3ec6SMatthias Ringwald char buffer[30]; 2373deb3ec6SMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 2383deb3ec6SMatthias Ringwald offset += join(buffer+offset, sizeof(buffer)-offset, hfp_indicators, hfp_indicators_nr); 2393deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 2403deb3ec6SMatthias Ringwald buffer[offset] = 0; 2413deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2423deb3ec6SMatthias Ringwald } 2433deb3ec6SMatthias Ringwald 2443deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){ 2453deb3ec6SMatthias Ringwald char buffer[20]; 2463deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=?\r\n", HFP_GENERIC_STATUS_INDICATOR); 2473deb3ec6SMatthias Ringwald // printf("retrieve_supported_generic_status_indicators %s\n", buffer); 2483deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2493deb3ec6SMatthias Ringwald } 2503deb3ec6SMatthias Ringwald 2513deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){ 2523deb3ec6SMatthias Ringwald char buffer[20]; 2533deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_GENERIC_STATUS_INDICATOR); 2543deb3ec6SMatthias Ringwald // printf("list_initital_supported_generic_status_indicators %s\n", buffer); 2553deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2563deb3ec6SMatthias Ringwald } 2573deb3ec6SMatthias Ringwald 2583deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 2593deb3ec6SMatthias Ringwald char buffer[20]; 2603deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=3,0\r\n", HFP_QUERY_OPERATOR_SELECTION); 2613deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2623deb3ec6SMatthias Ringwald } 2633deb3ec6SMatthias Ringwald 2643deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 2653deb3ec6SMatthias Ringwald char buffer[20]; 2663deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_QUERY_OPERATOR_SELECTION); 2673deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2683deb3ec6SMatthias Ringwald } 2693deb3ec6SMatthias Ringwald 2703deb3ec6SMatthias Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 2713deb3ec6SMatthias Ringwald char buffer[20]; 2723deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 2733deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2743deb3ec6SMatthias Ringwald } 2753deb3ec6SMatthias Ringwald 2763deb3ec6SMatthias Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 2773deb3ec6SMatthias Ringwald char buffer[20]; 2783deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_TRIGGER_CODEC_CONNECTION_SETUP); 2793deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2803deb3ec6SMatthias Ringwald } 2813deb3ec6SMatthias Ringwald 2823deb3ec6SMatthias Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 2833deb3ec6SMatthias Ringwald char buffer[20]; 2843deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec); 2853deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2863deb3ec6SMatthias Ringwald } 2873deb3ec6SMatthias Ringwald 288ce263fc8SMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){ 289ce263fc8SMatthias Ringwald char buffer[10]; 290ce263fc8SMatthias Ringwald sprintf(buffer, "%s\r\n", HFP_CALL_ANSWERED); 291ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 292ce263fc8SMatthias Ringwald } 293ce263fc8SMatthias Ringwald 294ce263fc8SMatthias Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 295ce263fc8SMatthias Ringwald char buffer[40]; 296ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_SET_MICROPHONE_GAIN, gain); 297ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 298ce263fc8SMatthias Ringwald } 299ce263fc8SMatthias Ringwald 300ce263fc8SMatthias Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 301ce263fc8SMatthias Ringwald char buffer[40]; 302ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_SET_SPEAKER_GAIN, gain); 303ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 304ce263fc8SMatthias Ringwald } 305ce263fc8SMatthias Ringwald 306ce263fc8SMatthias Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 307ce263fc8SMatthias Ringwald char buffer[40]; 308ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CLIP, activate); 309ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 310ce263fc8SMatthias Ringwald } 311ce263fc8SMatthias Ringwald 312ce263fc8SMatthias Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){ 313ce263fc8SMatthias Ringwald char buffer[40]; 314ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_TURN_OFF_EC_AND_NR, activate); 315ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 316ce263fc8SMatthias Ringwald } 317ce263fc8SMatthias Ringwald 318ce263fc8SMatthias Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 319ce263fc8SMatthias Ringwald char buffer[40]; 320ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate); 321ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 322ce263fc8SMatthias Ringwald } 323ce263fc8SMatthias Ringwald 324ce263fc8SMatthias Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 325ce263fc8SMatthias Ringwald char buffer[40]; 326ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 327ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 328ce263fc8SMatthias Ringwald } 329ce263fc8SMatthias Ringwald 330ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 331ce263fc8SMatthias Ringwald char buffer[40]; 332ce263fc8SMatthias Ringwald sprintf(buffer, "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number); 333ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 334ce263fc8SMatthias Ringwald } 335ce263fc8SMatthias Ringwald 336a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){ 337ce263fc8SMatthias Ringwald char buffer[40]; 338a0ffb263SMatthias Ringwald sprintf(buffer, "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id); 339ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 340ce263fc8SMatthias Ringwald } 341ce263fc8SMatthias Ringwald 342ce263fc8SMatthias Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 343ce263fc8SMatthias Ringwald char buffer[20]; 344ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_REDIAL_LAST_NUMBER); 345ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 346ce263fc8SMatthias Ringwald } 347ce263fc8SMatthias Ringwald 348ce263fc8SMatthias Ringwald static int hfp_hf_send_chup(uint16_t cid){ 349ce263fc8SMatthias Ringwald char buffer[20]; 350ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_HANG_UP_CALL); 351ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 352ce263fc8SMatthias Ringwald } 353ce263fc8SMatthias Ringwald 354f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){ 355ce263fc8SMatthias Ringwald char buffer[20]; 356ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 357ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 358ce263fc8SMatthias Ringwald } 359ce263fc8SMatthias Ringwald 360ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){ 361ce263fc8SMatthias Ringwald char buffer[20]; 362ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code); 363ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 364ce263fc8SMatthias Ringwald } 365ce263fc8SMatthias Ringwald 366ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){ 367ce263fc8SMatthias Ringwald char buffer[20]; 368ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=1\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG); 369ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 370ce263fc8SMatthias Ringwald } 371ce263fc8SMatthias Ringwald 372667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){ 373667ec068SMatthias Ringwald char buffer[20]; 374667ec068SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_LIST_CURRENT_CALLS); 375667ec068SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 376667ec068SMatthias Ringwald } 377667ec068SMatthias Ringwald 37813839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){ 3793deb3ec6SMatthias Ringwald if (!callback) return; 380a0ffb263SMatthias Ringwald uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1]; 3813deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3823deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3833deb3ec6SMatthias Ringwald event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 384a0ffb263SMatthias Ringwald event[3] = indicator.index; 385a0ffb263SMatthias Ringwald event[4] = indicator.status; 386a0ffb263SMatthias Ringwald strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE); 387a0ffb263SMatthias Ringwald event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0; 38813839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 3893deb3ec6SMatthias Ringwald } 3903deb3ec6SMatthias Ringwald 39113839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){ 3923deb3ec6SMatthias Ringwald if (!callback) return; 3933deb3ec6SMatthias Ringwald uint8_t event[24]; 3943deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3953deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3963deb3ec6SMatthias Ringwald event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED; 397a0ffb263SMatthias Ringwald event[3] = network_operator.mode; 398a0ffb263SMatthias Ringwald event[4] = network_operator.format; 399a0ffb263SMatthias Ringwald strcpy((char*)&event[5], network_operator.name); 40013839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 4013deb3ec6SMatthias Ringwald } 4023deb3ec6SMatthias Ringwald 403a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 404a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 405a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 406aa4dd815SMatthias Ringwald int done = 1; 4073deb3ec6SMatthias Ringwald 408a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 4093deb3ec6SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 410d715cf51SMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr); 411a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 412a0ffb263SMatthias Ringwald hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 4133deb3ec6SMatthias Ringwald break; 4143deb3ec6SMatthias Ringwald case HFP_NOTIFY_ON_CODECS: 415a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 416a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 4173deb3ec6SMatthias Ringwald break; 4183deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: 419a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 420a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 4213deb3ec6SMatthias Ringwald break; 4223deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS_STATUS: 423a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 424a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 4253deb3ec6SMatthias Ringwald break; 4263deb3ec6SMatthias Ringwald case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 427a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 428a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 4293deb3ec6SMatthias Ringwald break; 4303deb3ec6SMatthias Ringwald case HFP_RETRIEVE_CAN_HOLD_CALL: 431a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 432a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 4333deb3ec6SMatthias Ringwald break; 4343deb3ec6SMatthias Ringwald case HFP_LIST_GENERIC_STATUS_INDICATORS: 435a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 436a0ffb263SMatthias Ringwald hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4373deb3ec6SMatthias Ringwald break; 4383deb3ec6SMatthias Ringwald case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 439a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 440a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4413deb3ec6SMatthias Ringwald break; 4423deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 443a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 444a0ffb263SMatthias Ringwald hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4453deb3ec6SMatthias Ringwald break; 4463deb3ec6SMatthias Ringwald default: 447aa4dd815SMatthias Ringwald done = 0; 4483deb3ec6SMatthias Ringwald break; 4493deb3ec6SMatthias Ringwald } 4503deb3ec6SMatthias Ringwald return done; 4513deb3ec6SMatthias Ringwald } 4523deb3ec6SMatthias Ringwald 453ce263fc8SMatthias Ringwald 454a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 455a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 456a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 457ce263fc8SMatthias Ringwald 458ce263fc8SMatthias Ringwald int done = 0; 459a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 460a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 461ce263fc8SMatthias Ringwald done = 1; 462a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 463ce263fc8SMatthias Ringwald return done; 464ce263fc8SMatthias Ringwald }; 465a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators){ 466a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 467ce263fc8SMatthias Ringwald done = 1; 468a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 469a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap, 470a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr); 471ce263fc8SMatthias Ringwald return done; 472ce263fc8SMatthias Ringwald } 473ce263fc8SMatthias Ringwald 474a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 475ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 476a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 477a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 478a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 479ce263fc8SMatthias Ringwald return 1; 480ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 481a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 482a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 483a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 484ce263fc8SMatthias Ringwald return 1; 485ce263fc8SMatthias Ringwald default: 486ce263fc8SMatthias Ringwald break; 487ce263fc8SMatthias Ringwald } 488ce263fc8SMatthias Ringwald 489a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 490a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 491ce263fc8SMatthias Ringwald done = 1; 492a0ffb263SMatthias Ringwald hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 493ce263fc8SMatthias Ringwald return done; 494ce263fc8SMatthias Ringwald } 495ce263fc8SMatthias Ringwald 496ce263fc8SMatthias Ringwald return done; 497ce263fc8SMatthias Ringwald } 498ce263fc8SMatthias Ringwald 499a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 500ce263fc8SMatthias Ringwald /* events ( == commands): 501ce263fc8SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 502ce263fc8SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 503ce263fc8SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC 504ce263fc8SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS 505ce263fc8SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 506ce263fc8SMatthias Ringwald */ 507ce263fc8SMatthias Ringwald 508a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 509ce263fc8SMatthias Ringwald 510a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 511ce263fc8SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 512a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0; 513ce263fc8SMatthias Ringwald 514a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 515a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 516a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 517ce263fc8SMatthias Ringwald return 1; 518ce263fc8SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 519a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 520a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 521a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 522ce263fc8SMatthias Ringwald 523a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 524a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 525a0ffb263SMatthias Ringwald hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 526ce263fc8SMatthias Ringwald break; 527ce263fc8SMatthias Ringwald 5286a7f44bdSMilanka Ringwald case HFP_CMD_AG_SUGGESTED_CODEC:{ 5296a7f44bdSMilanka Ringwald if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){ 530a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 531a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 532a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 533*0e0be203SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 534*0e0be203SMatthias Ringwald log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD"); 535fcb08cdbSMilanka Ringwald hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 536ce263fc8SMatthias Ringwald } else { 537a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 538a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 539a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 540a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 541a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 542a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 543ce263fc8SMatthias Ringwald 544ce263fc8SMatthias Ringwald } 545ce263fc8SMatthias Ringwald break; 5466a7f44bdSMilanka Ringwald } 547ce263fc8SMatthias Ringwald default: 548ce263fc8SMatthias Ringwald break; 549ce263fc8SMatthias Ringwald } 550ce263fc8SMatthias Ringwald return 0; 551ce263fc8SMatthias Ringwald } 552ce263fc8SMatthias Ringwald 553a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 554a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 555a0ffb263SMatthias Ringwald hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0; 556ce263fc8SMatthias Ringwald 557ce263fc8SMatthias Ringwald 558a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){ 559a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 560a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 561a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 562ce263fc8SMatthias Ringwald return 1; 563ce263fc8SMatthias Ringwald } 564ce263fc8SMatthias Ringwald 565a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 566ce263fc8SMatthias Ringwald 567ce263fc8SMatthias Ringwald // run codecs exchange 568a0ffb263SMatthias Ringwald int done = codecs_exchange_state_machine(hfp_connection); 569ce263fc8SMatthias Ringwald if (done) return 1; 570ce263fc8SMatthias Ringwald 571ce263fc8SMatthias Ringwald return 0; 572ce263fc8SMatthias Ringwald } 573ce263fc8SMatthias Ringwald 574a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 575a0ffb263SMatthias Ringwald if (hfp_connection->hf_answer_incoming_call){ 576a0ffb263SMatthias Ringwald hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 577a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 0; 578ce263fc8SMatthias Ringwald return 1; 579ce263fc8SMatthias Ringwald } 580ce263fc8SMatthias Ringwald return 0; 581ce263fc8SMatthias Ringwald } 582ce263fc8SMatthias Ringwald 583a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){ 584a0ffb263SMatthias Ringwald if (!hfp_connection) return; 585724f400dSMatthias Ringwald if (!hfp_connection->rfcomm_cid) return; 5867522e673SMatthias Ringwald 587b72c4a9eSMatthias Ringwald if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){ 588b72c4a9eSMatthias Ringwald 589b72c4a9eSMatthias Ringwald hfp_connection->hf_accept_sco = 0; 5907522e673SMatthias Ringwald 5917522e673SMatthias Ringwald // notify about codec selection if not done already 5927522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 5937522e673SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 5947522e673SMatthias Ringwald } 5957522e673SMatthias Ringwald 5967522e673SMatthias Ringwald // remote supported feature eSCO is set if link type is eSCO 5977522e673SMatthias Ringwald // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 5987522e673SMatthias Ringwald uint16_t max_latency; 5997522e673SMatthias Ringwald uint8_t retransmission_effort; 6007522e673SMatthias Ringwald uint16_t packet_types; 6017522e673SMatthias Ringwald 602d9f77559SMatthias Ringwald if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 6037522e673SMatthias Ringwald max_latency = 0x000c; 6047522e673SMatthias Ringwald retransmission_effort = 0x02; 6057522e673SMatthias Ringwald packet_types = 0x388; 6067522e673SMatthias Ringwald } else { 6077522e673SMatthias Ringwald max_latency = 0xffff; 6087522e673SMatthias Ringwald retransmission_effort = 0xff; 6097522e673SMatthias Ringwald packet_types = 0x003f; 6107522e673SMatthias Ringwald } 6117522e673SMatthias Ringwald 6127522e673SMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 6137522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 6147522e673SMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data 6157522e673SMatthias Ringwald } 6167522e673SMatthias Ringwald 617b72c4a9eSMatthias Ringwald log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting); 6187522e673SMatthias Ringwald hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 6197522e673SMatthias Ringwald sco_voice_setting, retransmission_effort, packet_types); 6207522e673SMatthias Ringwald return; 6217522e673SMatthias Ringwald } 6227522e673SMatthias Ringwald 623a0ffb263SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return; 624ce263fc8SMatthias Ringwald 625a0ffb263SMatthias Ringwald int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 626ce263fc8SMatthias Ringwald if (!done){ 627a0ffb263SMatthias Ringwald done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 628ce263fc8SMatthias Ringwald } 629ce263fc8SMatthias Ringwald if (!done){ 630a0ffb263SMatthias Ringwald done = hfp_hf_run_for_audio_connection(hfp_connection); 631ce263fc8SMatthias Ringwald } 632ce263fc8SMatthias Ringwald if (!done){ 633a0ffb263SMatthias Ringwald done = call_setup_state_machine(hfp_connection); 634ce263fc8SMatthias Ringwald } 635ce263fc8SMatthias Ringwald 636a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 637a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 638a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 639a0ffb263SMatthias Ringwald hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 640ce263fc8SMatthias Ringwald return; 641ce263fc8SMatthias Ringwald } 642ce263fc8SMatthias Ringwald 643a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 644a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 645a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 646a0ffb263SMatthias Ringwald hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 647ce263fc8SMatthias Ringwald return; 648ce263fc8SMatthias Ringwald } 649ce263fc8SMatthias Ringwald 650a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_calling_line_notification){ 651a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 0; 652a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 653a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 654ce263fc8SMatthias Ringwald return; 655ce263fc8SMatthias Ringwald } 656ce263fc8SMatthias Ringwald 657a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_calling_line_notification){ 658a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 0; 659a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 660a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 661ce263fc8SMatthias Ringwald return; 662ce263fc8SMatthias Ringwald } 663ce263fc8SMatthias Ringwald 664a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 665a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 666a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 667a0ffb263SMatthias Ringwald hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0); 668ce263fc8SMatthias Ringwald return; 669ce263fc8SMatthias Ringwald } 670ce263fc8SMatthias Ringwald 671a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){ 672a0ffb263SMatthias Ringwald hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0; 673a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 674a0ffb263SMatthias Ringwald hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1); 675ce263fc8SMatthias Ringwald return; 676ce263fc8SMatthias Ringwald } 677ce263fc8SMatthias Ringwald 678a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_voice_recognition_notification){ 679a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_voice_recognition_notification = 0; 680a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 681a0ffb263SMatthias Ringwald hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 682ce263fc8SMatthias Ringwald return; 683ce263fc8SMatthias Ringwald } 684ce263fc8SMatthias Ringwald 685a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_voice_recognition_notification){ 686a0ffb263SMatthias Ringwald hfp_connection->hf_activate_voice_recognition_notification = 0; 687a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 688a0ffb263SMatthias Ringwald hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 689ce263fc8SMatthias Ringwald return; 690ce263fc8SMatthias Ringwald } 691ce263fc8SMatthias Ringwald 692ce263fc8SMatthias Ringwald 693a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_call_waiting_notification){ 694a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 0; 695a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 696a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 697ce263fc8SMatthias Ringwald return; 698ce263fc8SMatthias Ringwald } 699ce263fc8SMatthias Ringwald 700a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_call_waiting_notification){ 701a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 0; 702a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 703a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 704ce263fc8SMatthias Ringwald return; 705ce263fc8SMatthias Ringwald } 706ce263fc8SMatthias Ringwald 707a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_outgoing_call){ 708a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 0; 709a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 710a0ffb263SMatthias Ringwald hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 711ce263fc8SMatthias Ringwald return; 712ce263fc8SMatthias Ringwald } 713ce263fc8SMatthias Ringwald 714a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_memory_dialing){ 715a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 0; 716a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 717a0ffb263SMatthias Ringwald hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 718ce263fc8SMatthias Ringwald return; 719ce263fc8SMatthias Ringwald } 720ce263fc8SMatthias Ringwald 721a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_redial_last_number){ 722a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 0; 723a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 724a0ffb263SMatthias Ringwald hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 725ce263fc8SMatthias Ringwald return; 726ce263fc8SMatthias Ringwald } 727ce263fc8SMatthias Ringwald 728a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chup){ 729a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 0; 730a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 731a0ffb263SMatthias Ringwald hfp_hf_send_chup(hfp_connection->rfcomm_cid); 732ce263fc8SMatthias Ringwald return; 733ce263fc8SMatthias Ringwald } 734ce263fc8SMatthias Ringwald 735a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_0){ 736a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 0; 737a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 738a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 739ce263fc8SMatthias Ringwald return; 740ce263fc8SMatthias Ringwald } 741ce263fc8SMatthias Ringwald 742a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_1){ 743a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 0; 744a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 745a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 746ce263fc8SMatthias Ringwald return; 747ce263fc8SMatthias Ringwald } 748ce263fc8SMatthias Ringwald 749a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_2){ 750a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 0; 751a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 752a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 753ce263fc8SMatthias Ringwald return; 754ce263fc8SMatthias Ringwald } 755ce263fc8SMatthias Ringwald 756a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_3){ 757a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 0; 758a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 759a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 760ce263fc8SMatthias Ringwald return; 761ce263fc8SMatthias Ringwald } 762ce263fc8SMatthias Ringwald 763a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_4){ 764a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 0; 765a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 766a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 767ce263fc8SMatthias Ringwald return; 768ce263fc8SMatthias Ringwald } 769ce263fc8SMatthias Ringwald 770a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_x){ 771a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 0; 772a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 773a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 774667ec068SMatthias Ringwald return; 775667ec068SMatthias Ringwald } 776667ec068SMatthias Ringwald 777a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_dtmf_code){ 778a0ffb263SMatthias Ringwald char code = hfp_connection->hf_send_dtmf_code; 779a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = 0; 780a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 781a0ffb263SMatthias Ringwald hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 782ce263fc8SMatthias Ringwald return; 783ce263fc8SMatthias Ringwald } 784ce263fc8SMatthias Ringwald 785a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_binp){ 786a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 0; 787a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 788a0ffb263SMatthias Ringwald hfp_hf_send_binp(hfp_connection->rfcomm_cid); 789ce263fc8SMatthias Ringwald return; 790ce263fc8SMatthias Ringwald } 791ce263fc8SMatthias Ringwald 792a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_clcc){ 793a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 0; 794a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 795a0ffb263SMatthias Ringwald hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 796667ec068SMatthias Ringwald return; 797667ec068SMatthias Ringwald } 798667ec068SMatthias Ringwald 799a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_rrh){ 800a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 0; 801667ec068SMatthias Ringwald char buffer[20]; 802a0ffb263SMatthias Ringwald switch (hfp_connection->hf_send_rrh_command){ 803667ec068SMatthias Ringwald case '?': 804667ec068SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD); 805a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 806667ec068SMatthias Ringwald return; 807667ec068SMatthias Ringwald case '0': 808667ec068SMatthias Ringwald case '1': 809667ec068SMatthias Ringwald case '2': 810a0ffb263SMatthias Ringwald sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command); 811a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 812667ec068SMatthias Ringwald return; 813667ec068SMatthias Ringwald default: 814667ec068SMatthias Ringwald break; 815667ec068SMatthias Ringwald } 816667ec068SMatthias Ringwald return; 817667ec068SMatthias Ringwald } 818667ec068SMatthias Ringwald 819a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_cnum){ 820a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 0; 821667ec068SMatthias Ringwald char buffer[20]; 822667ec068SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION); 823a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 824667ec068SMatthias Ringwald return; 825667ec068SMatthias Ringwald } 826667ec068SMatthias Ringwald 827667ec068SMatthias Ringwald // update HF indicators 828a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_update_bitmap){ 829667ec068SMatthias Ringwald int i; 830667ec068SMatthias Ringwald for (i=0;i<hfp_indicators_nr;i++){ 831a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 832a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].state){ 833a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 834a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 835667ec068SMatthias Ringwald char buffer[30]; 836667ec068SMatthias Ringwald sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], hfp_indicators_value[i]); 837a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 838667ec068SMatthias Ringwald } else { 83960ebb071SMilanka Ringwald log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]); 840667ec068SMatthias Ringwald } 841667ec068SMatthias Ringwald return; 842667ec068SMatthias Ringwald } 843667ec068SMatthias Ringwald } 844667ec068SMatthias Ringwald } 845667ec068SMatthias Ringwald 846ce263fc8SMatthias Ringwald if (done) return; 847ce263fc8SMatthias Ringwald // deal with disconnect 848a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 849ce263fc8SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 850a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 851a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 852ce263fc8SMatthias Ringwald break; 853ce263fc8SMatthias Ringwald 854ce263fc8SMatthias Ringwald default: 855ce263fc8SMatthias Ringwald break; 856ce263fc8SMatthias Ringwald } 857ce263fc8SMatthias Ringwald } 858ce263fc8SMatthias Ringwald 859a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 860a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 8616a7f44bdSMilanka Ringwald 862d0c4aea6SMilanka Ringwald hfp_emit_slc_connection_event(hfp_callback, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 8637522e673SMatthias Ringwald 864667ec068SMatthias Ringwald // restore volume settings 865a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = hfp_hf_speaker_gain; 866a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 867667ec068SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 868a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = hfp_hf_microphone_gain; 869a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 870667ec068SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 871667ec068SMatthias Ringwald // enable all indicators 872667ec068SMatthias Ringwald int i; 873667ec068SMatthias Ringwald for (i=0;i<hfp_indicators_nr;i++){ 874a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i]; 875a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].state = 1; 876667ec068SMatthias Ringwald } 877ce263fc8SMatthias Ringwald } 878ce263fc8SMatthias Ringwald 879a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){ 880a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 881a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 8823deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 883a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 884a0ffb263SMatthias Ringwald hfp_connection->state = HFP_NOTIFY_ON_CODECS; 8853deb3ec6SMatthias Ringwald break; 8863deb3ec6SMatthias Ringwald } 887a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 8883deb3ec6SMatthias Ringwald break; 8893deb3ec6SMatthias Ringwald 8903deb3ec6SMatthias Ringwald case HFP_W4_NOTIFY_ON_CODECS: 891a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 8923deb3ec6SMatthias Ringwald break; 8933deb3ec6SMatthias Ringwald 8943deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 895a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 8963deb3ec6SMatthias Ringwald break; 8973deb3ec6SMatthias Ringwald 8983deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 899a0ffb263SMatthias Ringwald hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 9003deb3ec6SMatthias Ringwald break; 9013deb3ec6SMatthias Ringwald 9023deb3ec6SMatthias Ringwald case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 903a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 904a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 9053deb3ec6SMatthias Ringwald break; 9063deb3ec6SMatthias Ringwald } 907a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 908a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 9093deb3ec6SMatthias Ringwald break; 9103deb3ec6SMatthias Ringwald } 911a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9123deb3ec6SMatthias Ringwald break; 9133deb3ec6SMatthias Ringwald 9143deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 915a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 916a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 9173deb3ec6SMatthias Ringwald break; 9183deb3ec6SMatthias Ringwald } 919a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9203deb3ec6SMatthias Ringwald break; 9213deb3ec6SMatthias Ringwald 9223deb3ec6SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 923a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 9243deb3ec6SMatthias Ringwald break; 9253deb3ec6SMatthias Ringwald 9263deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 927a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 9283deb3ec6SMatthias Ringwald break; 9293deb3ec6SMatthias Ringwald 9303deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 931a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9323deb3ec6SMatthias Ringwald break; 933ce263fc8SMatthias Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 934a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 935a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 9363deb3ec6SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0); 937ce263fc8SMatthias Ringwald break; 938ce263fc8SMatthias Ringwald } 9393deb3ec6SMatthias Ringwald 940a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 941a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 9423deb3ec6SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0); 943ce263fc8SMatthias Ringwald break; 9443deb3ec6SMatthias Ringwald } 9453deb3ec6SMatthias Ringwald 946a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 947ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 94860ebb071SMilanka Ringwald // printf("Format set, querying name\n"); 949a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 950ce263fc8SMatthias Ringwald break; 951ce263fc8SMatthias Ringwald case HPF_HF_QUERY_OPERATOR_W4_RESULT: 952a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 953a0ffb263SMatthias Ringwald hfp_emit_network_operator_event(hfp_callback, hfp_connection->network_operator); 954ce263fc8SMatthias Ringwald break; 955ce263fc8SMatthias Ringwald default: 956ce263fc8SMatthias Ringwald break; 9573deb3ec6SMatthias Ringwald } 958ce263fc8SMatthias Ringwald 959a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 960a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 961ce263fc8SMatthias Ringwald break; 9623deb3ec6SMatthias Ringwald } 9633deb3ec6SMatthias Ringwald 964a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 965aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 966a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 9673deb3ec6SMatthias Ringwald break; 968ce263fc8SMatthias Ringwald case HFP_CODECS_HF_CONFIRMED_CODEC: 969a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 970ce263fc8SMatthias Ringwald break; 9713deb3ec6SMatthias Ringwald default: 9723deb3ec6SMatthias Ringwald break; 9733deb3ec6SMatthias Ringwald } 9743deb3ec6SMatthias Ringwald break; 9753deb3ec6SMatthias Ringwald default: 9763deb3ec6SMatthias Ringwald break; 9773deb3ec6SMatthias Ringwald } 9783deb3ec6SMatthias Ringwald 9793deb3ec6SMatthias Ringwald // done 980a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 9813deb3ec6SMatthias Ringwald } 9823deb3ec6SMatthias Ringwald 9833deb3ec6SMatthias Ringwald 9843deb3ec6SMatthias Ringwald static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 985a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 986a0ffb263SMatthias Ringwald if (!hfp_connection) return; 9873deb3ec6SMatthias Ringwald 9881e35c04dSMatthias Ringwald char last_char = packet[size-1]; 9891e35c04dSMatthias Ringwald packet[size-1] = 0; 9901e35c04dSMatthias Ringwald log_info("HFP_RX %s", packet); 9911e35c04dSMatthias Ringwald packet[size-1] = last_char; 9921e35c04dSMatthias Ringwald 993667ec068SMatthias Ringwald int pos, i, value; 9943deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 995a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 1); 996ce263fc8SMatthias Ringwald } 9973deb3ec6SMatthias Ringwald 998a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 999667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1000a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1001a0ffb263SMatthias Ringwald // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type); 1002a0ffb263SMatthias Ringwald hfp_hf_emit_subscriber_information(hfp_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number); 1003667ec068SMatthias Ringwald break; 1004667ec068SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1005a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1006a0ffb263SMatthias Ringwald // printf("Response and Hold status: %s\n", hfp_connection->line_buffer); 10072308e108SMilanka Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1008667ec068SMatthias Ringwald break; 1009667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1010a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1011a0ffb263SMatthias Ringwald // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n", 1012a0ffb263SMatthias Ringwald // hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty, 1013a0ffb263SMatthias Ringwald // hfp_connection->bnip_number, hfp_connection->bnip_type); 1014a0ffb263SMatthias Ringwald hfp_hf_emit_enhanced_call_status(hfp_callback, hfp_connection->clcc_idx, 1015a0ffb263SMatthias Ringwald hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty, 1016a0ffb263SMatthias Ringwald hfp_connection->bnip_type, hfp_connection->bnip_number); 1017667ec068SMatthias Ringwald break; 1018ce263fc8SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1019a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10202308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1021667ec068SMatthias Ringwald hfp_hf_speaker_gain = value; 1022667ec068SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1023ce263fc8SMatthias Ringwald break; 1024ce263fc8SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1025a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10262308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1027667ec068SMatthias Ringwald hfp_hf_microphone_gain = value; 1028667ec068SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1029ce263fc8SMatthias Ringwald break; 1030ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1031a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1032a0ffb263SMatthias Ringwald hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1033a0ffb263SMatthias Ringwald break; 1034a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1035a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1036a0ffb263SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1037a0ffb263SMatthias Ringwald break; 1038a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1039a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1040a0ffb263SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALLING_LINE_INDETIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1041ce263fc8SMatthias Ringwald break; 1042ce263fc8SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1043a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1044a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1045a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1046a0ffb263SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1047ce263fc8SMatthias Ringwald break; 1048ce263fc8SMatthias Ringwald case HFP_CMD_ERROR: 1049a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1050a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 1051a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1052ce263fc8SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1); 1053ce263fc8SMatthias Ringwald break; 1054ce263fc8SMatthias Ringwald case HFP_CMD_OK: 1055a0ffb263SMatthias Ringwald hfp_hf_switch_on_ok(hfp_connection); 1056ce263fc8SMatthias Ringwald break; 1057ce263fc8SMatthias Ringwald case HFP_CMD_RING: 1058a0ffb263SMatthias Ringwald hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_RING); 1059ce263fc8SMatthias Ringwald break; 1060ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1061a0ffb263SMatthias Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1062a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators[i].status_changed) { 1063a0ffb263SMatthias Ringwald if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1064a0ffb263SMatthias Ringwald hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1065a0ffb263SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1066a0ffb263SMatthias Ringwald hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 1067a0ffb263SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1068a0ffb263SMatthias Ringwald hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1069ce263fc8SMatthias Ringwald } 1070a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1071a0ffb263SMatthias Ringwald hfp_emit_ag_indicator_event(hfp_callback, hfp_connection->ag_indicators[i]); 10723deb3ec6SMatthias Ringwald break; 10733deb3ec6SMatthias Ringwald } 10743deb3ec6SMatthias Ringwald } 1075ce263fc8SMatthias Ringwald break; 1076ce263fc8SMatthias Ringwald default: 1077ce263fc8SMatthias Ringwald break; 10783deb3ec6SMatthias Ringwald } 1079a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 10803deb3ec6SMatthias Ringwald } 10813deb3ec6SMatthias Ringwald 10820cb5b971SMatthias Ringwald static void hfp_run(void){ 1083665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1084665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1085665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1086a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1087a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 10883deb3ec6SMatthias Ringwald } 10893deb3ec6SMatthias Ringwald } 10903deb3ec6SMatthias Ringwald 1091ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 10923deb3ec6SMatthias Ringwald switch (packet_type){ 10933deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 10943deb3ec6SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size); 10953deb3ec6SMatthias Ringwald break; 10963deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1097e30a6a47SMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size); 1098202c8a4cSMatthias Ringwald break; 10993deb3ec6SMatthias Ringwald default: 11003deb3ec6SMatthias Ringwald break; 11013deb3ec6SMatthias Ringwald } 11023deb3ec6SMatthias Ringwald hfp_run(); 11033deb3ec6SMatthias Ringwald } 11043deb3ec6SMatthias Ringwald 1105a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1106d63c37a1SMatthias Ringwald // register for HCI events 1107d63c37a1SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 1108d63c37a1SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 1109d63c37a1SMatthias Ringwald 1110d63c37a1SMatthias Ringwald rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff); 1111a0ffb263SMatthias Ringwald 1112d68dcce1SMatthias Ringwald hfp_set_packet_handler_for_rfcomm_connections(&packet_handler); 1113d68dcce1SMatthias Ringwald 1114a0ffb263SMatthias Ringwald hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1115a0ffb263SMatthias Ringwald hfp_codecs_nr = 0; 1116a0ffb263SMatthias Ringwald hfp_indicators_nr = 0; 1117a0ffb263SMatthias Ringwald hfp_hf_speaker_gain = 9; 1118a0ffb263SMatthias Ringwald hfp_hf_microphone_gain = 9; 1119a0ffb263SMatthias Ringwald } 1120a0ffb263SMatthias Ringwald 1121a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){ 11223deb3ec6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS){ 1123a0ffb263SMatthias Ringwald log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 11243deb3ec6SMatthias Ringwald return; 11253deb3ec6SMatthias Ringwald } 11263deb3ec6SMatthias Ringwald 11273deb3ec6SMatthias Ringwald hfp_codecs_nr = codecs_nr; 11283deb3ec6SMatthias Ringwald int i; 11293deb3ec6SMatthias Ringwald for (i=0; i<codecs_nr; i++){ 11303deb3ec6SMatthias Ringwald hfp_codecs[i] = codecs[i]; 11313deb3ec6SMatthias Ringwald } 11323deb3ec6SMatthias Ringwald } 11333deb3ec6SMatthias Ringwald 1134a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){ 11353deb3ec6SMatthias Ringwald hfp_supported_features = supported_features; 1136a0ffb263SMatthias Ringwald } 11373deb3ec6SMatthias Ringwald 1138a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){ 11393deb3ec6SMatthias Ringwald hfp_indicators_nr = indicators_nr; 11403deb3ec6SMatthias Ringwald int i; 1141a0ffb263SMatthias Ringwald for (i = 0; i < hfp_indicators_nr ; i++){ 11423deb3ec6SMatthias Ringwald hfp_indicators[i] = indicators[i]; 11433deb3ec6SMatthias Ringwald } 11443deb3ec6SMatthias Ringwald } 11453deb3ec6SMatthias Ringwald 11463deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 11473deb3ec6SMatthias Ringwald hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway); 11483deb3ec6SMatthias Ringwald } 11493deb3ec6SMatthias Ringwald 1150c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 1151c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1152a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1153a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1154a33eb0c4SMilanka Ringwald return; 1155a33eb0c4SMilanka Ringwald } 1156a0ffb263SMatthias Ringwald hfp_release_service_level_connection(hfp_connection); 1157a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11583deb3ec6SMatthias Ringwald } 11593deb3ec6SMatthias Ringwald 1160c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 1161c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1162a0ffb263SMatthias Ringwald if (!hfp_connection) { 1163a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 11643deb3ec6SMatthias Ringwald return; 11653deb3ec6SMatthias Ringwald } 1166a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = enable; 1167a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11683deb3ec6SMatthias Ringwald } 11693deb3ec6SMatthias Ringwald 1170c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1171c8626498SMilanka Ringwald hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1172ce263fc8SMatthias Ringwald } 1173ce263fc8SMatthias Ringwald 1174c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1175c8626498SMilanka Ringwald hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1176ce263fc8SMatthias Ringwald } 1177ce263fc8SMatthias Ringwald 11783deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format 1179c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 1180c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1181a0ffb263SMatthias Ringwald if (!hfp_connection) { 1182a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 11833deb3ec6SMatthias Ringwald return; 11843deb3ec6SMatthias Ringwald } 1185a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1186a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1187a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11883deb3ec6SMatthias Ringwald } 11893deb3ec6SMatthias Ringwald 1190c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 1191c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1192a0ffb263SMatthias Ringwald if (!hfp_connection) { 1193a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 11943deb3ec6SMatthias Ringwald return; 11953deb3ec6SMatthias Ringwald } 1196a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1197ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1198a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1199ce263fc8SMatthias Ringwald break; 1200ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1201a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1202ce263fc8SMatthias Ringwald break; 1203ce263fc8SMatthias Ringwald default: 1204ce263fc8SMatthias Ringwald break; 1205ce263fc8SMatthias Ringwald } 1206a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12073deb3ec6SMatthias Ringwald } 12083deb3ec6SMatthias Ringwald 1209c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 1210c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1211a0ffb263SMatthias Ringwald if (!hfp_connection) { 1212a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12133deb3ec6SMatthias Ringwald return; 12143deb3ec6SMatthias Ringwald } 1215a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = enable; 1216a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12173deb3ec6SMatthias Ringwald } 12183deb3ec6SMatthias Ringwald 1219ce263fc8SMatthias Ringwald 1220c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1221c8626498SMilanka Ringwald hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1222ce263fc8SMatthias Ringwald } 1223ce263fc8SMatthias Ringwald 1224c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1225c8626498SMilanka Ringwald hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1226ce263fc8SMatthias Ringwald } 1227ce263fc8SMatthias Ringwald 1228ce263fc8SMatthias Ringwald 1229c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 1230c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1231a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1232a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1233a33eb0c4SMilanka Ringwald return; 1234a33eb0c4SMilanka Ringwald } 1235a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 1236ce263fc8SMatthias Ringwald 1237a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1238a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 12393deb3ec6SMatthias Ringwald 1240a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 1241ce263fc8SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); 1242a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1243a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 1244ce263fc8SMatthias Ringwald } else { 1245a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 1246aa4dd815SMatthias Ringwald case HFP_CODECS_W4_AG_COMMON_CODEC: 1247aa4dd815SMatthias Ringwald break; 1248aa4dd815SMatthias Ringwald default: 1249a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 1250aa4dd815SMatthias Ringwald break; 12513deb3ec6SMatthias Ringwald } 1252ce263fc8SMatthias Ringwald } 1253ce263fc8SMatthias Ringwald 1254a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12553deb3ec6SMatthias Ringwald } 12563deb3ec6SMatthias Ringwald 1257c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 1258c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1259a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1260a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1261a33eb0c4SMilanka Ringwald return; 1262a33eb0c4SMilanka Ringwald } 1263a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 1264a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12653deb3ec6SMatthias Ringwald } 12663deb3ec6SMatthias Ringwald 1267c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 1268c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1269a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1270a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1271a33eb0c4SMilanka Ringwald return; 1272a33eb0c4SMilanka Ringwald } 1273ce263fc8SMatthias Ringwald 1274ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1275a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 1; 1276a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1277ce263fc8SMatthias Ringwald } else { 1278ce263fc8SMatthias Ringwald log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1279ce263fc8SMatthias Ringwald } 1280ce263fc8SMatthias Ringwald } 1281ce263fc8SMatthias Ringwald 1282c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 1283c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1284a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1285a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1286a33eb0c4SMilanka Ringwald return; 1287a33eb0c4SMilanka Ringwald } 1288a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 1289a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1290ce263fc8SMatthias Ringwald } 1291ce263fc8SMatthias Ringwald 1292c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 1293c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1294a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1295a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1296a33eb0c4SMilanka Ringwald return; 1297a33eb0c4SMilanka Ringwald } 1298ce263fc8SMatthias Ringwald 1299ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1300a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 1301a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1302ce263fc8SMatthias Ringwald } 1303ce263fc8SMatthias Ringwald } 1304ce263fc8SMatthias Ringwald 1305c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){ 1306c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1307a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1308a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1309a33eb0c4SMilanka Ringwald return; 1310a33eb0c4SMilanka Ringwald } 1311ce263fc8SMatthias Ringwald 1312ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1313a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 1; 1314a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1315ce263fc8SMatthias Ringwald } 1316ce263fc8SMatthias Ringwald } 1317ce263fc8SMatthias Ringwald 1318c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 1319c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1320a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1321a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1322a33eb0c4SMilanka Ringwald return; 1323a33eb0c4SMilanka Ringwald } 1324ce263fc8SMatthias Ringwald 1325ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1326ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1327a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 1; 1328a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1329ce263fc8SMatthias Ringwald } 1330ce263fc8SMatthias Ringwald } 1331ce263fc8SMatthias Ringwald 1332c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 1333c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1334a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1335a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1336a33eb0c4SMilanka Ringwald return; 1337a33eb0c4SMilanka Ringwald } 1338ce263fc8SMatthias Ringwald 1339ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1340ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1341a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 1; 1342a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1343ce263fc8SMatthias Ringwald } 1344ce263fc8SMatthias Ringwald } 1345ce263fc8SMatthias Ringwald 1346c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 1347c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1348a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1349a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1350a33eb0c4SMilanka Ringwald return; 1351a33eb0c4SMilanka Ringwald } 1352ce263fc8SMatthias Ringwald 1353ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1354ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1355a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 1; 1356a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1357ce263fc8SMatthias Ringwald } 1358ce263fc8SMatthias Ringwald } 1359ce263fc8SMatthias Ringwald 1360c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 1361c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1362a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1363a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1364a33eb0c4SMilanka Ringwald return; 1365a33eb0c4SMilanka Ringwald } 1366ce263fc8SMatthias Ringwald 1367ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1368ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1369a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 1; 1370a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1371ce263fc8SMatthias Ringwald } 1372ce263fc8SMatthias Ringwald } 1373ce263fc8SMatthias Ringwald 1374c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 1375c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1376a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1377a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1378a33eb0c4SMilanka Ringwald return; 1379a33eb0c4SMilanka Ringwald } 1380667ec068SMatthias Ringwald 1381667ec068SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1382667ec068SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1383a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1384a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 10 + index; 1385a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1386667ec068SMatthias Ringwald } 1387667ec068SMatthias Ringwald } 1388667ec068SMatthias Ringwald 1389c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 1390c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1391a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1392a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1393a33eb0c4SMilanka Ringwald return; 1394a33eb0c4SMilanka Ringwald } 1395667ec068SMatthias Ringwald 1396667ec068SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1397667ec068SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1398a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1399a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 20 + index; 1400a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1401667ec068SMatthias Ringwald } 1402667ec068SMatthias Ringwald } 1403ce263fc8SMatthias Ringwald 1404c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 1405c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1406a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1407a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1408a33eb0c4SMilanka Ringwald return; 1409a33eb0c4SMilanka Ringwald } 1410ce263fc8SMatthias Ringwald 1411a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 1; 1412ce263fc8SMatthias Ringwald snprintf(phone_number, sizeof(phone_number), "%s", number); 1413a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1414ce263fc8SMatthias Ringwald } 1415ce263fc8SMatthias Ringwald 1416c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 1417c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1418a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1419a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1420a33eb0c4SMilanka Ringwald return; 1421a33eb0c4SMilanka Ringwald } 1422ce263fc8SMatthias Ringwald 1423a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 1; 1424a0ffb263SMatthias Ringwald hfp_connection->memory_id = memory_id; 1425a0ffb263SMatthias Ringwald 1426a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1427ce263fc8SMatthias Ringwald } 1428ce263fc8SMatthias Ringwald 1429c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 1430c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1431a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1432a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1433a33eb0c4SMilanka Ringwald return; 1434a33eb0c4SMilanka Ringwald } 1435ce263fc8SMatthias Ringwald 1436a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 1; 1437a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1438ce263fc8SMatthias Ringwald } 1439ce263fc8SMatthias Ringwald 1440c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 1441c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1442a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1443a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1444a33eb0c4SMilanka Ringwald return; 1445a33eb0c4SMilanka Ringwald } 1446ce263fc8SMatthias Ringwald 1447a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 1; 1448a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1449ce263fc8SMatthias Ringwald } 1450ce263fc8SMatthias Ringwald 1451ce263fc8SMatthias Ringwald 1452c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 1453c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1454a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1455a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1456a33eb0c4SMilanka Ringwald return; 1457a33eb0c4SMilanka Ringwald } 1458ce263fc8SMatthias Ringwald 1459a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 1; 1460a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1461ce263fc8SMatthias Ringwald } 1462ce263fc8SMatthias Ringwald 1463ce263fc8SMatthias Ringwald 1464c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 1465c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1466a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1467a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1468a33eb0c4SMilanka Ringwald return; 1469a33eb0c4SMilanka Ringwald } 1470ce263fc8SMatthias Ringwald 1471a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 1; 1472a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1473ce263fc8SMatthias Ringwald } 1474ce263fc8SMatthias Ringwald 1475c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 1476c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1477a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1478a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1479a33eb0c4SMilanka Ringwald return; 1480a33eb0c4SMilanka Ringwald } 1481ce263fc8SMatthias Ringwald 1482a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 1; 1483a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1484ce263fc8SMatthias Ringwald } 1485ce263fc8SMatthias Ringwald 1486ce263fc8SMatthias Ringwald 1487c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1488c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1489a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1490a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1491a33eb0c4SMilanka Ringwald return; 1492a33eb0c4SMilanka Ringwald } 1493ce263fc8SMatthias Ringwald 1494a0ffb263SMatthias Ringwald hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1; 1495a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1496ce263fc8SMatthias Ringwald } 1497ce263fc8SMatthias Ringwald 1498c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1499c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1500a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1501a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1502a33eb0c4SMilanka Ringwald return; 1503a33eb0c4SMilanka Ringwald } 1504ce263fc8SMatthias Ringwald 1505a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1506a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1507ce263fc8SMatthias Ringwald } 1508ce263fc8SMatthias Ringwald 1509c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1510c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1511a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1512a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1513a33eb0c4SMilanka Ringwald return; 1514a33eb0c4SMilanka Ringwald } 1515ce263fc8SMatthias Ringwald 1516a0ffb263SMatthias Ringwald hfp_connection->hf_activate_voice_recognition_notification = 1; 1517a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1518ce263fc8SMatthias Ringwald } 1519ce263fc8SMatthias Ringwald 1520c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1521c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1522a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1523a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1524a33eb0c4SMilanka Ringwald return; 1525a33eb0c4SMilanka Ringwald } 1526ce263fc8SMatthias Ringwald 1527a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_voice_recognition_notification = 1; 1528a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1529ce263fc8SMatthias Ringwald } 1530ce263fc8SMatthias Ringwald 1531c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 1532c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1533a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1534a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1535a33eb0c4SMilanka Ringwald return; 1536a33eb0c4SMilanka Ringwald } 1537c8626498SMilanka Ringwald 1538a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain == gain) return; 1539a0ffb263SMatthias Ringwald if (gain < 0 || gain > 15){ 1540a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1541a0ffb263SMatthias Ringwald return; 1542a0ffb263SMatthias Ringwald } 1543a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 1544a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 1545a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1546ce263fc8SMatthias Ringwald } 1547ce263fc8SMatthias Ringwald 1548c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 1549c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1550a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1551a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1552a33eb0c4SMilanka Ringwald return; 1553a33eb0c4SMilanka Ringwald } 1554c8626498SMilanka Ringwald 1555a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain == gain) return; 1556a0ffb263SMatthias Ringwald if (gain < 0 || gain > 15){ 1557a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1558a0ffb263SMatthias Ringwald return; 1559a0ffb263SMatthias Ringwald } 1560a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 1561a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 1562a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1563ce263fc8SMatthias Ringwald } 1564ce263fc8SMatthias Ringwald 1565c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 1566c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1567a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1568a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1569a33eb0c4SMilanka Ringwald return; 1570a33eb0c4SMilanka Ringwald } 1571a33eb0c4SMilanka Ringwald 1572a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = code; 1573a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1574ce263fc8SMatthias Ringwald } 1575ce263fc8SMatthias Ringwald 1576c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 1577c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1578a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1579a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1580a33eb0c4SMilanka Ringwald return; 1581a33eb0c4SMilanka Ringwald } 1582a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 1; 1583a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1584ce263fc8SMatthias Ringwald } 15853deb3ec6SMatthias Ringwald 1586c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 1587c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1588a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1589a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1590a33eb0c4SMilanka Ringwald return; 1591a33eb0c4SMilanka Ringwald } 1592a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 1; 1593a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1594667ec068SMatthias Ringwald } 1595667ec068SMatthias Ringwald 1596667ec068SMatthias Ringwald 1597c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 1598c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1599a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1600a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1601a33eb0c4SMilanka Ringwald return; 1602a33eb0c4SMilanka Ringwald } 1603a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1604a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '?'; 1605a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1606667ec068SMatthias Ringwald } 1607667ec068SMatthias Ringwald 1608c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 1609c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1610a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1611a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1612a33eb0c4SMilanka Ringwald return; 1613a33eb0c4SMilanka Ringwald } 1614a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1615a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '0'; 1616a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1617667ec068SMatthias Ringwald } 1618667ec068SMatthias Ringwald 1619c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 1620c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1621a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1622a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1623a33eb0c4SMilanka Ringwald return; 1624a33eb0c4SMilanka Ringwald } 1625a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1626a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '1'; 1627a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1628667ec068SMatthias Ringwald } 1629667ec068SMatthias Ringwald 1630c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 1631c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1632a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1633a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1634a33eb0c4SMilanka Ringwald return; 1635a33eb0c4SMilanka Ringwald } 1636a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1637a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '2'; 1638a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1639667ec068SMatthias Ringwald } 1640667ec068SMatthias Ringwald 1641c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 1642c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1643a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1644a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1645a33eb0c4SMilanka Ringwald return; 1646a33eb0c4SMilanka Ringwald } 1647a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 1; 1648a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1649667ec068SMatthias Ringwald } 1650667ec068SMatthias Ringwald 1651c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 1652c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1653a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1654a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1655a33eb0c4SMilanka Ringwald return; 1656a33eb0c4SMilanka Ringwald } 1657667ec068SMatthias Ringwald // find index for assigned number 1658667ec068SMatthias Ringwald int i; 1659667ec068SMatthias Ringwald for (i = 0; i < hfp_indicators_nr ; i++){ 1660667ec068SMatthias Ringwald if (hfp_indicators[i] == assigned_number){ 1661667ec068SMatthias Ringwald // set value 1662667ec068SMatthias Ringwald hfp_indicators_value[i] = value; 1663667ec068SMatthias Ringwald // mark for update 1664a0ffb263SMatthias Ringwald if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1665a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap |= (1<<i); 1666667ec068SMatthias Ringwald // send update 1667a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1668a0ffb263SMatthias Ringwald } 1669667ec068SMatthias Ringwald return; 1670667ec068SMatthias Ringwald } 1671667ec068SMatthias Ringwald } 1672667ec068SMatthias Ringwald } 1673667ec068SMatthias Ringwald 1674