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 */ 37*ab2c6ae4SMatthias Ringwald 38*ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hfp_hf.c" 393deb3ec6SMatthias Ringwald 403deb3ec6SMatthias Ringwald // ***************************************************************************** 413deb3ec6SMatthias Ringwald // 42fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit 433deb3ec6SMatthias Ringwald // 443deb3ec6SMatthias Ringwald // ***************************************************************************** 453deb3ec6SMatthias Ringwald 467907f069SMatthias Ringwald #include "btstack_config.h" 473deb3ec6SMatthias Ringwald 483deb3ec6SMatthias Ringwald #include <stdint.h> 493deb3ec6SMatthias Ringwald #include <stdio.h> 503deb3ec6SMatthias Ringwald #include <stdlib.h> 513deb3ec6SMatthias Ringwald #include <string.h> 523deb3ec6SMatthias Ringwald 5359c6af15SMatthias Ringwald #include "btstack_debug.h" 543deb3ec6SMatthias Ringwald #include "btstack_memory.h" 5559c6af15SMatthias Ringwald #include "btstack_run_loop.h" 5659c6af15SMatthias Ringwald #include "classic/core.h" 5759c6af15SMatthias Ringwald #include "classic/hfp.h" 5859c6af15SMatthias Ringwald #include "classic/hfp_hf.h" 59efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 60746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 61023f2764SMatthias Ringwald #include "classic/sdp_util.h" 6259c6af15SMatthias Ringwald #include "hci.h" 6359c6af15SMatthias Ringwald #include "hci_cmd.h" 6459c6af15SMatthias Ringwald #include "hci_dump.h" 6559c6af15SMatthias Ringwald #include "l2cap.h" 663deb3ec6SMatthias Ringwald 673deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit"; 683deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 693deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0; 703deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 713deb3ec6SMatthias Ringwald 723deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0; 733deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS]; 74667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS]; 75667ec068SMatthias Ringwald 76667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9; 77667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9; 783deb3ec6SMatthias Ringwald 7913839019SMatthias Ringwald static btstack_packet_handler_t hfp_callback; 803deb3ec6SMatthias Ringwald 81ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status; 82ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status; 83ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status; 84ce263fc8SMatthias Ringwald 85ce263fc8SMatthias Ringwald static char phone_number[25]; 86ce263fc8SMatthias Ringwald 87c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 88c5b64319SMatthias Ringwald 8913839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 903deb3ec6SMatthias Ringwald hfp_callback = callback; 913deb3ec6SMatthias Ringwald if (callback == NULL){ 923deb3ec6SMatthias Ringwald log_error("hfp_hf_register_packet_handler called with NULL callback"); 933deb3ec6SMatthias Ringwald return; 943deb3ec6SMatthias Ringwald } 953deb3ec6SMatthias Ringwald hfp_callback = callback; 96f4000eebSMatthias Ringwald hfp_set_callback(callback); 973deb3ec6SMatthias Ringwald } 983deb3ec6SMatthias Ringwald 9913839019SMatthias 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){ 100a0ffb263SMatthias Ringwald if (!callback) return; 101a0ffb263SMatthias Ringwald uint8_t event[31]; 102a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 103a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 104a0ffb263SMatthias Ringwald event[2] = event_subtype; 105a0ffb263SMatthias Ringwald event[3] = status; 106a0ffb263SMatthias Ringwald event[4] = bnip_type; 1077c959318SMatthias Ringwald int size = (strlen(bnip_number) < sizeof(event) - 6) ? (int) strlen(bnip_number) : sizeof(event) - 6; 108a0ffb263SMatthias Ringwald strncpy((char*)&event[5], bnip_number, size); 109a0ffb263SMatthias Ringwald event[5 + size] = 0; 11013839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 111a0ffb263SMatthias Ringwald } 112a0ffb263SMatthias Ringwald 11313839019SMatthias 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){ 114a0ffb263SMatthias Ringwald if (!callback) return; 115a0ffb263SMatthias Ringwald uint8_t event[30]; 116a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 117a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 118a0ffb263SMatthias Ringwald event[2] = event_subtype; 119a0ffb263SMatthias Ringwald event[3] = bnip_type; 1207c959318SMatthias Ringwald int size = (strlen(bnip_number) < sizeof(event) - 5) ? (int) strlen(bnip_number) : sizeof(event) - 5; 121a0ffb263SMatthias Ringwald strncpy((char*)&event[4], bnip_number, size); 122a0ffb263SMatthias Ringwald event[4 + size] = 0; 12313839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 124a0ffb263SMatthias Ringwald } 125a0ffb263SMatthias Ringwald 12613839019SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir, 127a0ffb263SMatthias Ringwald uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){ 128a0ffb263SMatthias Ringwald if (!callback) return; 129a0ffb263SMatthias Ringwald uint8_t event[35]; 130a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 131a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 132a0ffb263SMatthias Ringwald event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 133a0ffb263SMatthias Ringwald event[3] = clcc_idx; 134a0ffb263SMatthias Ringwald event[4] = clcc_dir; 135a0ffb263SMatthias Ringwald event[6] = clcc_status; 136a0ffb263SMatthias Ringwald event[7] = clcc_mpty; 137a0ffb263SMatthias Ringwald event[8] = bnip_type; 1387c959318SMatthias Ringwald int size = (strlen(bnip_number) < sizeof(event) - 10) ? (int) strlen(bnip_number) : sizeof(event) - 10; 139a0ffb263SMatthias Ringwald strncpy((char*)&event[9], bnip_number, size); 140a0ffb263SMatthias Ringwald event[9 + size] = 0; 14113839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 142a0ffb263SMatthias Ringwald } 143a0ffb263SMatthias Ringwald 144a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 1453deb3ec6SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 146a0ffb263SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 1473deb3ec6SMatthias Ringwald return hf && ag; 1483deb3ec6SMatthias Ringwald } 1493deb3ec6SMatthias Ringwald 150a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 1513deb3ec6SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING); 152a0ffb263SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING); 1533deb3ec6SMatthias Ringwald return hf && ag; 1543deb3ec6SMatthias Ringwald } 1553deb3ec6SMatthias Ringwald 1563deb3ec6SMatthias Ringwald 157a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 1583deb3ec6SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS); 159a0ffb263SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS); 1603deb3ec6SMatthias Ringwald return hf && ag; 1613deb3ec6SMatthias Ringwald } 1623deb3ec6SMatthias Ringwald 163ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 1643deb3ec6SMatthias Ringwald 1654f84bf36SMatthias 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){ 1663deb3ec6SMatthias Ringwald if (!name){ 1673deb3ec6SMatthias Ringwald name = default_hfp_hf_service_name; 1683deb3ec6SMatthias Ringwald } 1692ef54b27SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, SDP_Handsfree, rfcomm_channel_nr, name); 1703deb3ec6SMatthias Ringwald 1714f84bf36SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 1724f84bf36SMatthias Ringwald // 1734f84bf36SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 1744f84bf36SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 1754f84bf36SMatthias Ringwald // 1764f84bf36SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 1774f84bf36SMatthias Ringwald if (supported_features & wide_band_speech){ 1784f84bf36SMatthias Ringwald sdp_features |= 1 << 5; // Wide band speech bit 1794f84bf36SMatthias Ringwald } 180aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 1814f84bf36SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 182aa4dd815SMatthias Ringwald } 1833deb3ec6SMatthias Ringwald 1843deb3ec6SMatthias Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 1853deb3ec6SMatthias Ringwald char buffer[20]; 1863deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features); 1873deb3ec6SMatthias Ringwald // printf("exchange_supported_features %s\n", buffer); 1883deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 1893deb3ec6SMatthias Ringwald } 1903deb3ec6SMatthias Ringwald 1913deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 1923deb3ec6SMatthias Ringwald char buffer[30]; 1933deb3ec6SMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_AVAILABLE_CODECS); 1943deb3ec6SMatthias Ringwald offset += join(buffer+offset, sizeof(buffer)-offset, hfp_codecs, hfp_codecs_nr); 1953deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 1963deb3ec6SMatthias Ringwald buffer[offset] = 0; 1973deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 1983deb3ec6SMatthias Ringwald } 1993deb3ec6SMatthias Ringwald 2003deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){ 2013deb3ec6SMatthias Ringwald char buffer[20]; 2023deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=?\r\n", HFP_INDICATOR); 2033deb3ec6SMatthias Ringwald // printf("retrieve_indicators %s\n", buffer); 2043deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2053deb3ec6SMatthias Ringwald } 2063deb3ec6SMatthias Ringwald 2073deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){ 2083deb3ec6SMatthias Ringwald char buffer[20]; 2093deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_INDICATOR); 2103deb3ec6SMatthias Ringwald // printf("retrieve_indicators_status %s\n", buffer); 2113deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2123deb3ec6SMatthias Ringwald } 2133deb3ec6SMatthias Ringwald 2143deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 2153deb3ec6SMatthias Ringwald char buffer[20]; 2163deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 2173deb3ec6SMatthias Ringwald // printf("toggle_indicator_status_update %s\n", buffer); 2183deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2193deb3ec6SMatthias Ringwald } 2203deb3ec6SMatthias Ringwald 2213deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 2223deb3ec6SMatthias Ringwald char buffer[50]; 2233deb3ec6SMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 2243deb3ec6SMatthias Ringwald offset += join_bitmap(buffer+offset, sizeof(buffer)-offset, indicators_status, indicators_nr); 2253deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 2263deb3ec6SMatthias Ringwald buffer[offset] = 0; 2273deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2283deb3ec6SMatthias Ringwald } 2293deb3ec6SMatthias Ringwald 2303deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 2313deb3ec6SMatthias Ringwald char buffer[20]; 2323deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=?\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 2333deb3ec6SMatthias Ringwald // printf("retrieve_can_hold_call %s\n", buffer); 2343deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2353deb3ec6SMatthias Ringwald } 2363deb3ec6SMatthias Ringwald 2373deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 2383deb3ec6SMatthias Ringwald char buffer[30]; 2393deb3ec6SMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 2403deb3ec6SMatthias Ringwald offset += join(buffer+offset, sizeof(buffer)-offset, hfp_indicators, hfp_indicators_nr); 2413deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 2423deb3ec6SMatthias Ringwald buffer[offset] = 0; 2433deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2443deb3ec6SMatthias Ringwald } 2453deb3ec6SMatthias Ringwald 2463deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){ 2473deb3ec6SMatthias Ringwald char buffer[20]; 2483deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=?\r\n", HFP_GENERIC_STATUS_INDICATOR); 2493deb3ec6SMatthias Ringwald // printf("retrieve_supported_generic_status_indicators %s\n", buffer); 2503deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2513deb3ec6SMatthias Ringwald } 2523deb3ec6SMatthias Ringwald 2533deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){ 2543deb3ec6SMatthias Ringwald char buffer[20]; 2553deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_GENERIC_STATUS_INDICATOR); 2563deb3ec6SMatthias Ringwald // printf("list_initital_supported_generic_status_indicators %s\n", buffer); 2573deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2583deb3ec6SMatthias Ringwald } 2593deb3ec6SMatthias Ringwald 2603deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 2613deb3ec6SMatthias Ringwald char buffer[20]; 2623deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=3,0\r\n", HFP_QUERY_OPERATOR_SELECTION); 2633deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2643deb3ec6SMatthias Ringwald } 2653deb3ec6SMatthias Ringwald 2663deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 2673deb3ec6SMatthias Ringwald char buffer[20]; 2683deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_QUERY_OPERATOR_SELECTION); 2693deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2703deb3ec6SMatthias Ringwald } 2713deb3ec6SMatthias Ringwald 2723deb3ec6SMatthias Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 2733deb3ec6SMatthias Ringwald char buffer[20]; 2743deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 2753deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2763deb3ec6SMatthias Ringwald } 2773deb3ec6SMatthias Ringwald 2783deb3ec6SMatthias Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 2793deb3ec6SMatthias Ringwald char buffer[20]; 2803deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_TRIGGER_CODEC_CONNECTION_SETUP); 2813deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2823deb3ec6SMatthias Ringwald } 2833deb3ec6SMatthias Ringwald 2843deb3ec6SMatthias Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 2853deb3ec6SMatthias Ringwald char buffer[20]; 2863deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec); 2873deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2883deb3ec6SMatthias Ringwald } 2893deb3ec6SMatthias Ringwald 290ce263fc8SMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){ 291ce263fc8SMatthias Ringwald char buffer[10]; 292ce263fc8SMatthias Ringwald sprintf(buffer, "%s\r\n", HFP_CALL_ANSWERED); 293ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 294ce263fc8SMatthias Ringwald } 295ce263fc8SMatthias Ringwald 296ce263fc8SMatthias Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 297ce263fc8SMatthias Ringwald char buffer[40]; 298ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_SET_MICROPHONE_GAIN, gain); 299ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 300ce263fc8SMatthias Ringwald } 301ce263fc8SMatthias Ringwald 302ce263fc8SMatthias Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 303ce263fc8SMatthias Ringwald char buffer[40]; 304ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_SET_SPEAKER_GAIN, gain); 305ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 306ce263fc8SMatthias Ringwald } 307ce263fc8SMatthias Ringwald 308ce263fc8SMatthias Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 309ce263fc8SMatthias Ringwald char buffer[40]; 310ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CLIP, activate); 311ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 312ce263fc8SMatthias Ringwald } 313ce263fc8SMatthias Ringwald 314ce263fc8SMatthias Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){ 315ce263fc8SMatthias Ringwald char buffer[40]; 316ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_TURN_OFF_EC_AND_NR, activate); 317ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 318ce263fc8SMatthias Ringwald } 319ce263fc8SMatthias Ringwald 320ce263fc8SMatthias Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 321ce263fc8SMatthias Ringwald char buffer[40]; 322ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate); 323ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 324ce263fc8SMatthias Ringwald } 325ce263fc8SMatthias Ringwald 326ce263fc8SMatthias Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 327ce263fc8SMatthias Ringwald char buffer[40]; 328ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 329ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 330ce263fc8SMatthias Ringwald } 331ce263fc8SMatthias Ringwald 332ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 333ce263fc8SMatthias Ringwald char buffer[40]; 334ce263fc8SMatthias Ringwald sprintf(buffer, "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number); 335ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 336ce263fc8SMatthias Ringwald } 337ce263fc8SMatthias Ringwald 338a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){ 339ce263fc8SMatthias Ringwald char buffer[40]; 340a0ffb263SMatthias Ringwald sprintf(buffer, "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id); 341ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 342ce263fc8SMatthias Ringwald } 343ce263fc8SMatthias Ringwald 344ce263fc8SMatthias Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 345ce263fc8SMatthias Ringwald char buffer[20]; 346ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_REDIAL_LAST_NUMBER); 347ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 348ce263fc8SMatthias Ringwald } 349ce263fc8SMatthias Ringwald 350ce263fc8SMatthias Ringwald static int hfp_hf_send_chup(uint16_t cid){ 351ce263fc8SMatthias Ringwald char buffer[20]; 352ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_HANG_UP_CALL); 353ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 354ce263fc8SMatthias Ringwald } 355ce263fc8SMatthias Ringwald 356f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){ 357ce263fc8SMatthias Ringwald char buffer[20]; 358ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 359ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 360ce263fc8SMatthias Ringwald } 361ce263fc8SMatthias Ringwald 362ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){ 363ce263fc8SMatthias Ringwald char buffer[20]; 364ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code); 365ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 366ce263fc8SMatthias Ringwald } 367ce263fc8SMatthias Ringwald 368ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){ 369ce263fc8SMatthias Ringwald char buffer[20]; 370ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=1\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG); 371ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 372ce263fc8SMatthias Ringwald } 373ce263fc8SMatthias Ringwald 374667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){ 375667ec068SMatthias Ringwald char buffer[20]; 376667ec068SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_LIST_CURRENT_CALLS); 377667ec068SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 378667ec068SMatthias Ringwald } 379667ec068SMatthias Ringwald 38013839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){ 3813deb3ec6SMatthias Ringwald if (!callback) return; 382a0ffb263SMatthias Ringwald uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1]; 3833deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3843deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3853deb3ec6SMatthias Ringwald event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 386a0ffb263SMatthias Ringwald event[3] = indicator.index; 387a0ffb263SMatthias Ringwald event[4] = indicator.status; 388a0ffb263SMatthias Ringwald strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE); 389a0ffb263SMatthias Ringwald event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0; 39013839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 3913deb3ec6SMatthias Ringwald } 3923deb3ec6SMatthias Ringwald 39313839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){ 3943deb3ec6SMatthias Ringwald if (!callback) return; 3953deb3ec6SMatthias Ringwald uint8_t event[24]; 3963deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3973deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3983deb3ec6SMatthias Ringwald event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED; 399a0ffb263SMatthias Ringwald event[3] = network_operator.mode; 400a0ffb263SMatthias Ringwald event[4] = network_operator.format; 401a0ffb263SMatthias Ringwald strcpy((char*)&event[5], network_operator.name); 40213839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 4033deb3ec6SMatthias Ringwald } 4043deb3ec6SMatthias Ringwald 405a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 406a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 407a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 408aa4dd815SMatthias Ringwald int done = 1; 4093deb3ec6SMatthias Ringwald 410a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 4113deb3ec6SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 412d715cf51SMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr); 413a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 414a0ffb263SMatthias Ringwald hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 4153deb3ec6SMatthias Ringwald break; 4163deb3ec6SMatthias Ringwald case HFP_NOTIFY_ON_CODECS: 417a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 418a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 4193deb3ec6SMatthias Ringwald break; 4203deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: 421a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 422a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 4233deb3ec6SMatthias Ringwald break; 4243deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS_STATUS: 425a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 426a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 4273deb3ec6SMatthias Ringwald break; 4283deb3ec6SMatthias Ringwald case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 429a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 430a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 4313deb3ec6SMatthias Ringwald break; 4323deb3ec6SMatthias Ringwald case HFP_RETRIEVE_CAN_HOLD_CALL: 433a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 434a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 4353deb3ec6SMatthias Ringwald break; 4363deb3ec6SMatthias Ringwald case HFP_LIST_GENERIC_STATUS_INDICATORS: 437a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 438a0ffb263SMatthias Ringwald hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4393deb3ec6SMatthias Ringwald break; 4403deb3ec6SMatthias Ringwald case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 441a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 442a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4433deb3ec6SMatthias Ringwald break; 4443deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 445a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 446a0ffb263SMatthias Ringwald hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4473deb3ec6SMatthias Ringwald break; 4483deb3ec6SMatthias Ringwald default: 449aa4dd815SMatthias Ringwald done = 0; 4503deb3ec6SMatthias Ringwald break; 4513deb3ec6SMatthias Ringwald } 4523deb3ec6SMatthias Ringwald return done; 4533deb3ec6SMatthias Ringwald } 4543deb3ec6SMatthias Ringwald 455ce263fc8SMatthias Ringwald 456a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 457a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 458a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 459ce263fc8SMatthias Ringwald 460ce263fc8SMatthias Ringwald int done = 0; 461a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 462a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 463ce263fc8SMatthias Ringwald done = 1; 464a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 465ce263fc8SMatthias Ringwald return done; 466ce263fc8SMatthias Ringwald }; 467a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators){ 468a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 469ce263fc8SMatthias Ringwald done = 1; 470a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 471a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap, 472a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr); 473ce263fc8SMatthias Ringwald return done; 474ce263fc8SMatthias Ringwald } 475ce263fc8SMatthias Ringwald 476a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 477ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 478a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 479a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 480a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 481ce263fc8SMatthias Ringwald return 1; 482ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 483a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 484a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 485a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 486ce263fc8SMatthias Ringwald return 1; 487ce263fc8SMatthias Ringwald default: 488ce263fc8SMatthias Ringwald break; 489ce263fc8SMatthias Ringwald } 490ce263fc8SMatthias Ringwald 491a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 492a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 493ce263fc8SMatthias Ringwald done = 1; 494a0ffb263SMatthias Ringwald hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 495ce263fc8SMatthias Ringwald return done; 496ce263fc8SMatthias Ringwald } 497ce263fc8SMatthias Ringwald 498ce263fc8SMatthias Ringwald return done; 499ce263fc8SMatthias Ringwald } 500ce263fc8SMatthias Ringwald 501a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 502ce263fc8SMatthias Ringwald /* events ( == commands): 503ce263fc8SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 504ce263fc8SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 505ce263fc8SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC 506ce263fc8SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS 507ce263fc8SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 508ce263fc8SMatthias Ringwald */ 509ce263fc8SMatthias Ringwald 510a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 511ce263fc8SMatthias Ringwald 512a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 513ce263fc8SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 514a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0; 515ce263fc8SMatthias Ringwald 516a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 517a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 518a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 519ce263fc8SMatthias Ringwald return 1; 520ce263fc8SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 521a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 522a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 523a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 524ce263fc8SMatthias Ringwald 525a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 526a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 527a0ffb263SMatthias Ringwald hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 528ce263fc8SMatthias Ringwald break; 529ce263fc8SMatthias Ringwald 5306a7f44bdSMilanka Ringwald case HFP_CMD_AG_SUGGESTED_CODEC:{ 5316a7f44bdSMilanka Ringwald if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){ 532a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 533a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 534a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 5350e0be203SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 5360e0be203SMatthias Ringwald log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD"); 537fcb08cdbSMilanka Ringwald hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 538ce263fc8SMatthias Ringwald } else { 539a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 540a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 541a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 542a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 543a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 544a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 545ce263fc8SMatthias Ringwald 546ce263fc8SMatthias Ringwald } 547ce263fc8SMatthias Ringwald break; 5486a7f44bdSMilanka Ringwald } 549ce263fc8SMatthias Ringwald default: 550ce263fc8SMatthias Ringwald break; 551ce263fc8SMatthias Ringwald } 552ce263fc8SMatthias Ringwald return 0; 553ce263fc8SMatthias Ringwald } 554ce263fc8SMatthias Ringwald 555a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 556a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 557a0ffb263SMatthias Ringwald hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0; 558ce263fc8SMatthias Ringwald 559ce263fc8SMatthias Ringwald 560a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){ 561a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 562a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 563a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 564ce263fc8SMatthias Ringwald return 1; 565ce263fc8SMatthias Ringwald } 566ce263fc8SMatthias Ringwald 567a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 568ce263fc8SMatthias Ringwald 569ce263fc8SMatthias Ringwald // run codecs exchange 570a0ffb263SMatthias Ringwald int done = codecs_exchange_state_machine(hfp_connection); 571ce263fc8SMatthias Ringwald if (done) return 1; 572ce263fc8SMatthias Ringwald 573ce263fc8SMatthias Ringwald return 0; 574ce263fc8SMatthias Ringwald } 575ce263fc8SMatthias Ringwald 576a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 577a0ffb263SMatthias Ringwald if (hfp_connection->hf_answer_incoming_call){ 578a0ffb263SMatthias Ringwald hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 579a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 0; 580ce263fc8SMatthias Ringwald return 1; 581ce263fc8SMatthias Ringwald } 582ce263fc8SMatthias Ringwald return 0; 583ce263fc8SMatthias Ringwald } 584ce263fc8SMatthias Ringwald 585a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){ 586a0ffb263SMatthias Ringwald if (!hfp_connection) return; 587724f400dSMatthias Ringwald if (!hfp_connection->rfcomm_cid) return; 5887522e673SMatthias Ringwald 589b72c4a9eSMatthias Ringwald if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){ 590b72c4a9eSMatthias Ringwald 591b72c4a9eSMatthias Ringwald hfp_connection->hf_accept_sco = 0; 5927522e673SMatthias Ringwald 5937522e673SMatthias Ringwald // notify about codec selection if not done already 5947522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 5957522e673SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 5967522e673SMatthias Ringwald } 5977522e673SMatthias Ringwald 5987522e673SMatthias Ringwald // remote supported feature eSCO is set if link type is eSCO 5997522e673SMatthias Ringwald // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 6007522e673SMatthias Ringwald uint16_t max_latency; 6017522e673SMatthias Ringwald uint8_t retransmission_effort; 6027522e673SMatthias Ringwald uint16_t packet_types; 6037522e673SMatthias Ringwald 604d9f77559SMatthias Ringwald if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 6057522e673SMatthias Ringwald max_latency = 0x000c; 6067522e673SMatthias Ringwald retransmission_effort = 0x02; 6077522e673SMatthias Ringwald packet_types = 0x388; 6087522e673SMatthias Ringwald } else { 6097522e673SMatthias Ringwald max_latency = 0xffff; 6107522e673SMatthias Ringwald retransmission_effort = 0xff; 6117522e673SMatthias Ringwald packet_types = 0x003f; 6127522e673SMatthias Ringwald } 6137522e673SMatthias Ringwald 6147522e673SMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 6157522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 6167522e673SMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data 6177522e673SMatthias Ringwald } 6187522e673SMatthias Ringwald 619b72c4a9eSMatthias Ringwald log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting); 6207522e673SMatthias Ringwald hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 6217522e673SMatthias Ringwald sco_voice_setting, retransmission_effort, packet_types); 6227522e673SMatthias Ringwald return; 6237522e673SMatthias Ringwald } 6247522e673SMatthias Ringwald 625a0ffb263SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return; 626ce263fc8SMatthias Ringwald 627a0ffb263SMatthias Ringwald int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 628ce263fc8SMatthias Ringwald if (!done){ 629a0ffb263SMatthias Ringwald done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 630ce263fc8SMatthias Ringwald } 631ce263fc8SMatthias Ringwald if (!done){ 632a0ffb263SMatthias Ringwald done = hfp_hf_run_for_audio_connection(hfp_connection); 633ce263fc8SMatthias Ringwald } 634ce263fc8SMatthias Ringwald if (!done){ 635a0ffb263SMatthias Ringwald done = call_setup_state_machine(hfp_connection); 636ce263fc8SMatthias Ringwald } 637ce263fc8SMatthias Ringwald 638a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 639a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 640a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 641a0ffb263SMatthias Ringwald hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 642ce263fc8SMatthias Ringwald return; 643ce263fc8SMatthias Ringwald } 644ce263fc8SMatthias Ringwald 645a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 646a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 647a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 648a0ffb263SMatthias Ringwald hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 649ce263fc8SMatthias Ringwald return; 650ce263fc8SMatthias Ringwald } 651ce263fc8SMatthias Ringwald 652a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_calling_line_notification){ 653a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 0; 654a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 655a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 656ce263fc8SMatthias Ringwald return; 657ce263fc8SMatthias Ringwald } 658ce263fc8SMatthias Ringwald 659a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_calling_line_notification){ 660a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 0; 661a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 662a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 663ce263fc8SMatthias Ringwald return; 664ce263fc8SMatthias Ringwald } 665ce263fc8SMatthias Ringwald 666a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 667a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 668a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 669a0ffb263SMatthias Ringwald hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0); 670ce263fc8SMatthias Ringwald return; 671ce263fc8SMatthias Ringwald } 672ce263fc8SMatthias Ringwald 673a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){ 674a0ffb263SMatthias Ringwald hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0; 675a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 676a0ffb263SMatthias Ringwald hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1); 677ce263fc8SMatthias Ringwald return; 678ce263fc8SMatthias Ringwald } 679ce263fc8SMatthias Ringwald 680a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_voice_recognition_notification){ 681a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_voice_recognition_notification = 0; 682a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 683a0ffb263SMatthias Ringwald hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 684ce263fc8SMatthias Ringwald return; 685ce263fc8SMatthias Ringwald } 686ce263fc8SMatthias Ringwald 687a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_voice_recognition_notification){ 688a0ffb263SMatthias Ringwald hfp_connection->hf_activate_voice_recognition_notification = 0; 689a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 690a0ffb263SMatthias Ringwald hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 691ce263fc8SMatthias Ringwald return; 692ce263fc8SMatthias Ringwald } 693ce263fc8SMatthias Ringwald 694ce263fc8SMatthias Ringwald 695a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_call_waiting_notification){ 696a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 0; 697a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 698a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 699ce263fc8SMatthias Ringwald return; 700ce263fc8SMatthias Ringwald } 701ce263fc8SMatthias Ringwald 702a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_call_waiting_notification){ 703a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 0; 704a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 705a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 706ce263fc8SMatthias Ringwald return; 707ce263fc8SMatthias Ringwald } 708ce263fc8SMatthias Ringwald 709a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_outgoing_call){ 710a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 0; 711a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 712a0ffb263SMatthias Ringwald hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 713ce263fc8SMatthias Ringwald return; 714ce263fc8SMatthias Ringwald } 715ce263fc8SMatthias Ringwald 716a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_memory_dialing){ 717a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 0; 718a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 719a0ffb263SMatthias Ringwald hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 720ce263fc8SMatthias Ringwald return; 721ce263fc8SMatthias Ringwald } 722ce263fc8SMatthias Ringwald 723a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_redial_last_number){ 724a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 0; 725a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 726a0ffb263SMatthias Ringwald hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 727ce263fc8SMatthias Ringwald return; 728ce263fc8SMatthias Ringwald } 729ce263fc8SMatthias Ringwald 730a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chup){ 731a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 0; 732a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 733a0ffb263SMatthias Ringwald hfp_hf_send_chup(hfp_connection->rfcomm_cid); 734ce263fc8SMatthias Ringwald return; 735ce263fc8SMatthias Ringwald } 736ce263fc8SMatthias Ringwald 737a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_0){ 738a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 0; 739a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 740a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 741ce263fc8SMatthias Ringwald return; 742ce263fc8SMatthias Ringwald } 743ce263fc8SMatthias Ringwald 744a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_1){ 745a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 0; 746a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 747a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 748ce263fc8SMatthias Ringwald return; 749ce263fc8SMatthias Ringwald } 750ce263fc8SMatthias Ringwald 751a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_2){ 752a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 0; 753a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 754a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 755ce263fc8SMatthias Ringwald return; 756ce263fc8SMatthias Ringwald } 757ce263fc8SMatthias Ringwald 758a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_3){ 759a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 0; 760a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 761a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 762ce263fc8SMatthias Ringwald return; 763ce263fc8SMatthias Ringwald } 764ce263fc8SMatthias Ringwald 765a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_4){ 766a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 0; 767a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 768a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 769ce263fc8SMatthias Ringwald return; 770ce263fc8SMatthias Ringwald } 771ce263fc8SMatthias Ringwald 772a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_x){ 773a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 0; 774a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 775a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 776667ec068SMatthias Ringwald return; 777667ec068SMatthias Ringwald } 778667ec068SMatthias Ringwald 779a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_dtmf_code){ 780a0ffb263SMatthias Ringwald char code = hfp_connection->hf_send_dtmf_code; 781a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = 0; 782a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 783a0ffb263SMatthias Ringwald hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 784ce263fc8SMatthias Ringwald return; 785ce263fc8SMatthias Ringwald } 786ce263fc8SMatthias Ringwald 787a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_binp){ 788a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 0; 789a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 790a0ffb263SMatthias Ringwald hfp_hf_send_binp(hfp_connection->rfcomm_cid); 791ce263fc8SMatthias Ringwald return; 792ce263fc8SMatthias Ringwald } 793ce263fc8SMatthias Ringwald 794a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_clcc){ 795a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 0; 796a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 797a0ffb263SMatthias Ringwald hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 798667ec068SMatthias Ringwald return; 799667ec068SMatthias Ringwald } 800667ec068SMatthias Ringwald 801a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_rrh){ 802a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 0; 803667ec068SMatthias Ringwald char buffer[20]; 804a0ffb263SMatthias Ringwald switch (hfp_connection->hf_send_rrh_command){ 805667ec068SMatthias Ringwald case '?': 806667ec068SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD); 807a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 808667ec068SMatthias Ringwald return; 809667ec068SMatthias Ringwald case '0': 810667ec068SMatthias Ringwald case '1': 811667ec068SMatthias Ringwald case '2': 812a0ffb263SMatthias Ringwald sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command); 813a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 814667ec068SMatthias Ringwald return; 815667ec068SMatthias Ringwald default: 816667ec068SMatthias Ringwald break; 817667ec068SMatthias Ringwald } 818667ec068SMatthias Ringwald return; 819667ec068SMatthias Ringwald } 820667ec068SMatthias Ringwald 821a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_cnum){ 822a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 0; 823667ec068SMatthias Ringwald char buffer[20]; 824667ec068SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION); 825a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 826667ec068SMatthias Ringwald return; 827667ec068SMatthias Ringwald } 828667ec068SMatthias Ringwald 829667ec068SMatthias Ringwald // update HF indicators 830a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_update_bitmap){ 831667ec068SMatthias Ringwald int i; 832667ec068SMatthias Ringwald for (i=0;i<hfp_indicators_nr;i++){ 833a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 834a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].state){ 835a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 836a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 837667ec068SMatthias Ringwald char buffer[30]; 838ecb7d461SMatthias Ringwald sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]); 839a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 840667ec068SMatthias Ringwald } else { 84160ebb071SMilanka Ringwald log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]); 842667ec068SMatthias Ringwald } 843667ec068SMatthias Ringwald return; 844667ec068SMatthias Ringwald } 845667ec068SMatthias Ringwald } 846667ec068SMatthias Ringwald } 847667ec068SMatthias Ringwald 848ce263fc8SMatthias Ringwald if (done) return; 849ce263fc8SMatthias Ringwald // deal with disconnect 850a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 851ce263fc8SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 852a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 853a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 854ce263fc8SMatthias Ringwald break; 855ce263fc8SMatthias Ringwald 856ce263fc8SMatthias Ringwald default: 857ce263fc8SMatthias Ringwald break; 858ce263fc8SMatthias Ringwald } 859ce263fc8SMatthias Ringwald } 860ce263fc8SMatthias Ringwald 861a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 862a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 8636a7f44bdSMilanka Ringwald 864d0c4aea6SMilanka Ringwald hfp_emit_slc_connection_event(hfp_callback, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 8657522e673SMatthias Ringwald 866667ec068SMatthias Ringwald // restore volume settings 867a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = hfp_hf_speaker_gain; 868a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 869667ec068SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 870a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = hfp_hf_microphone_gain; 871a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 872667ec068SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 873667ec068SMatthias Ringwald // enable all indicators 874667ec068SMatthias Ringwald int i; 875667ec068SMatthias Ringwald for (i=0;i<hfp_indicators_nr;i++){ 876a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i]; 877a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].state = 1; 878667ec068SMatthias Ringwald } 879ce263fc8SMatthias Ringwald } 880ce263fc8SMatthias Ringwald 881a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){ 882a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 883a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 8843deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 885a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 886a0ffb263SMatthias Ringwald hfp_connection->state = HFP_NOTIFY_ON_CODECS; 8873deb3ec6SMatthias Ringwald break; 8883deb3ec6SMatthias Ringwald } 889a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 8903deb3ec6SMatthias Ringwald break; 8913deb3ec6SMatthias Ringwald 8923deb3ec6SMatthias Ringwald case HFP_W4_NOTIFY_ON_CODECS: 893a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 8943deb3ec6SMatthias Ringwald break; 8953deb3ec6SMatthias Ringwald 8963deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 897a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 8983deb3ec6SMatthias Ringwald break; 8993deb3ec6SMatthias Ringwald 9003deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 901a0ffb263SMatthias Ringwald hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 9023deb3ec6SMatthias Ringwald break; 9033deb3ec6SMatthias Ringwald 9043deb3ec6SMatthias Ringwald case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 905a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 906a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 9073deb3ec6SMatthias Ringwald break; 9083deb3ec6SMatthias Ringwald } 909a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 910a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 9113deb3ec6SMatthias Ringwald break; 9123deb3ec6SMatthias Ringwald } 913a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9143deb3ec6SMatthias Ringwald break; 9153deb3ec6SMatthias Ringwald 9163deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 917a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 918a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 9193deb3ec6SMatthias Ringwald break; 9203deb3ec6SMatthias Ringwald } 921a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9223deb3ec6SMatthias Ringwald break; 9233deb3ec6SMatthias Ringwald 9243deb3ec6SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 925a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 9263deb3ec6SMatthias Ringwald break; 9273deb3ec6SMatthias Ringwald 9283deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 929a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 9303deb3ec6SMatthias Ringwald break; 9313deb3ec6SMatthias Ringwald 9323deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 933a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9343deb3ec6SMatthias Ringwald break; 935ce263fc8SMatthias Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 936a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 937a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 9383deb3ec6SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0); 939ce263fc8SMatthias Ringwald break; 940ce263fc8SMatthias Ringwald } 9413deb3ec6SMatthias Ringwald 942a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 943a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 9443deb3ec6SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0); 945ce263fc8SMatthias Ringwald break; 9463deb3ec6SMatthias Ringwald } 9473deb3ec6SMatthias Ringwald 948a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 949ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 95060ebb071SMilanka Ringwald // printf("Format set, querying name\n"); 951a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 952ce263fc8SMatthias Ringwald break; 953ce263fc8SMatthias Ringwald case HPF_HF_QUERY_OPERATOR_W4_RESULT: 954a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 955a0ffb263SMatthias Ringwald hfp_emit_network_operator_event(hfp_callback, hfp_connection->network_operator); 956ce263fc8SMatthias Ringwald break; 957ce263fc8SMatthias Ringwald default: 958ce263fc8SMatthias Ringwald break; 9593deb3ec6SMatthias Ringwald } 960ce263fc8SMatthias Ringwald 961a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 962a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 963ce263fc8SMatthias Ringwald break; 9643deb3ec6SMatthias Ringwald } 9653deb3ec6SMatthias Ringwald 966a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 967aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 968a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 9693deb3ec6SMatthias Ringwald break; 970ce263fc8SMatthias Ringwald case HFP_CODECS_HF_CONFIRMED_CODEC: 971a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 972ce263fc8SMatthias Ringwald break; 9733deb3ec6SMatthias Ringwald default: 9743deb3ec6SMatthias Ringwald break; 9753deb3ec6SMatthias Ringwald } 9763deb3ec6SMatthias Ringwald break; 9773deb3ec6SMatthias Ringwald default: 9783deb3ec6SMatthias Ringwald break; 9793deb3ec6SMatthias Ringwald } 9803deb3ec6SMatthias Ringwald 9813deb3ec6SMatthias Ringwald // done 982a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 9833deb3ec6SMatthias Ringwald } 9843deb3ec6SMatthias Ringwald 9853deb3ec6SMatthias Ringwald 9863deb3ec6SMatthias Ringwald static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 9879ec2630cSMatthias Ringwald UNUSED(packet_type); 9889ec2630cSMatthias Ringwald 989a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 990a0ffb263SMatthias Ringwald if (!hfp_connection) return; 9913deb3ec6SMatthias Ringwald 9921e35c04dSMatthias Ringwald char last_char = packet[size-1]; 9931e35c04dSMatthias Ringwald packet[size-1] = 0; 9941e35c04dSMatthias Ringwald log_info("HFP_RX %s", packet); 9951e35c04dSMatthias Ringwald packet[size-1] = last_char; 9961e35c04dSMatthias Ringwald 997667ec068SMatthias Ringwald int pos, i, value; 9983deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 999a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 1); 1000ce263fc8SMatthias Ringwald } 10013deb3ec6SMatthias Ringwald 1002a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1003667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1004a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1005a0ffb263SMatthias Ringwald // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type); 1006a0ffb263SMatthias Ringwald hfp_hf_emit_subscriber_information(hfp_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number); 1007667ec068SMatthias Ringwald break; 1008667ec068SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1009a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1010a0ffb263SMatthias Ringwald // printf("Response and Hold status: %s\n", hfp_connection->line_buffer); 10112308e108SMilanka Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1012667ec068SMatthias Ringwald break; 1013667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1014a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1015a0ffb263SMatthias Ringwald // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n", 1016a0ffb263SMatthias Ringwald // hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty, 1017a0ffb263SMatthias Ringwald // hfp_connection->bnip_number, hfp_connection->bnip_type); 1018a0ffb263SMatthias Ringwald hfp_hf_emit_enhanced_call_status(hfp_callback, hfp_connection->clcc_idx, 1019a0ffb263SMatthias Ringwald hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty, 1020a0ffb263SMatthias Ringwald hfp_connection->bnip_type, hfp_connection->bnip_number); 1021667ec068SMatthias Ringwald break; 1022ce263fc8SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1023a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10242308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1025667ec068SMatthias Ringwald hfp_hf_speaker_gain = value; 1026667ec068SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1027ce263fc8SMatthias Ringwald break; 1028ce263fc8SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1029a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10302308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1031667ec068SMatthias Ringwald hfp_hf_microphone_gain = value; 1032667ec068SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1033ce263fc8SMatthias Ringwald break; 1034ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1035a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1036a0ffb263SMatthias Ringwald hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1037a0ffb263SMatthias Ringwald break; 1038a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1039a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1040a0ffb263SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1041a0ffb263SMatthias Ringwald break; 1042a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1043a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1044a0ffb263SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALLING_LINE_INDETIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1045ce263fc8SMatthias Ringwald break; 1046ce263fc8SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1047a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1048a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1049a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1050a0ffb263SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1051ce263fc8SMatthias Ringwald break; 1052ce263fc8SMatthias Ringwald case HFP_CMD_ERROR: 1053a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1054a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 1055a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1056ce263fc8SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1); 1057ce263fc8SMatthias Ringwald break; 1058ce263fc8SMatthias Ringwald case HFP_CMD_OK: 1059a0ffb263SMatthias Ringwald hfp_hf_switch_on_ok(hfp_connection); 1060ce263fc8SMatthias Ringwald break; 1061ce263fc8SMatthias Ringwald case HFP_CMD_RING: 1062a0ffb263SMatthias Ringwald hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_RING); 1063ce263fc8SMatthias Ringwald break; 1064ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1065a0ffb263SMatthias Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1066a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators[i].status_changed) { 1067a0ffb263SMatthias Ringwald if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1068a0ffb263SMatthias Ringwald hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1069a0ffb263SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1070a0ffb263SMatthias Ringwald hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 1071a0ffb263SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1072a0ffb263SMatthias Ringwald hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1073ce263fc8SMatthias Ringwald } 1074a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1075a0ffb263SMatthias Ringwald hfp_emit_ag_indicator_event(hfp_callback, hfp_connection->ag_indicators[i]); 10763deb3ec6SMatthias Ringwald break; 10773deb3ec6SMatthias Ringwald } 10783deb3ec6SMatthias Ringwald } 1079ce263fc8SMatthias Ringwald break; 1080ce263fc8SMatthias Ringwald default: 1081ce263fc8SMatthias Ringwald break; 10823deb3ec6SMatthias Ringwald } 1083a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 10843deb3ec6SMatthias Ringwald } 10853deb3ec6SMatthias Ringwald 10860cb5b971SMatthias Ringwald static void hfp_run(void){ 1087665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1088665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1089665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1090a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1091a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 10923deb3ec6SMatthias Ringwald } 10933deb3ec6SMatthias Ringwald } 10943deb3ec6SMatthias Ringwald 1095ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 10963deb3ec6SMatthias Ringwald switch (packet_type){ 10973deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 10983deb3ec6SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size); 10993deb3ec6SMatthias Ringwald break; 11003deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1101e30a6a47SMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size); 1102202c8a4cSMatthias Ringwald break; 11033deb3ec6SMatthias Ringwald default: 11043deb3ec6SMatthias Ringwald break; 11053deb3ec6SMatthias Ringwald } 11063deb3ec6SMatthias Ringwald hfp_run(); 11073deb3ec6SMatthias Ringwald } 11083deb3ec6SMatthias Ringwald 1109a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1110d63c37a1SMatthias Ringwald // register for HCI events 1111d63c37a1SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 1112d63c37a1SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 1113d63c37a1SMatthias Ringwald 1114d63c37a1SMatthias Ringwald rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff); 1115a0ffb263SMatthias Ringwald 1116d68dcce1SMatthias Ringwald hfp_set_packet_handler_for_rfcomm_connections(&packet_handler); 1117d68dcce1SMatthias Ringwald 1118a0ffb263SMatthias Ringwald hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1119a0ffb263SMatthias Ringwald hfp_codecs_nr = 0; 1120a0ffb263SMatthias Ringwald hfp_indicators_nr = 0; 1121a0ffb263SMatthias Ringwald hfp_hf_speaker_gain = 9; 1122a0ffb263SMatthias Ringwald hfp_hf_microphone_gain = 9; 1123a0ffb263SMatthias Ringwald } 1124a0ffb263SMatthias Ringwald 1125a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){ 11263deb3ec6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS){ 1127a0ffb263SMatthias Ringwald log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 11283deb3ec6SMatthias Ringwald return; 11293deb3ec6SMatthias Ringwald } 11303deb3ec6SMatthias Ringwald 11313deb3ec6SMatthias Ringwald hfp_codecs_nr = codecs_nr; 11323deb3ec6SMatthias Ringwald int i; 11333deb3ec6SMatthias Ringwald for (i=0; i<codecs_nr; i++){ 11343deb3ec6SMatthias Ringwald hfp_codecs[i] = codecs[i]; 11353deb3ec6SMatthias Ringwald } 11363deb3ec6SMatthias Ringwald } 11373deb3ec6SMatthias Ringwald 1138a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){ 11393deb3ec6SMatthias Ringwald hfp_supported_features = supported_features; 1140a0ffb263SMatthias Ringwald } 11413deb3ec6SMatthias Ringwald 1142a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){ 11433deb3ec6SMatthias Ringwald hfp_indicators_nr = indicators_nr; 11443deb3ec6SMatthias Ringwald int i; 1145a0ffb263SMatthias Ringwald for (i = 0; i < hfp_indicators_nr ; i++){ 11463deb3ec6SMatthias Ringwald hfp_indicators[i] = indicators[i]; 11473deb3ec6SMatthias Ringwald } 11483deb3ec6SMatthias Ringwald } 11493deb3ec6SMatthias Ringwald 11503deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 11513deb3ec6SMatthias Ringwald hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway); 11523deb3ec6SMatthias Ringwald } 11533deb3ec6SMatthias Ringwald 1154c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 1155c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1156a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1157a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1158a33eb0c4SMilanka Ringwald return; 1159a33eb0c4SMilanka Ringwald } 1160a0ffb263SMatthias Ringwald hfp_release_service_level_connection(hfp_connection); 1161a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11623deb3ec6SMatthias Ringwald } 11633deb3ec6SMatthias Ringwald 1164c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 1165c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1166a0ffb263SMatthias Ringwald if (!hfp_connection) { 1167a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 11683deb3ec6SMatthias Ringwald return; 11693deb3ec6SMatthias Ringwald } 1170a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = enable; 1171a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11723deb3ec6SMatthias Ringwald } 11733deb3ec6SMatthias Ringwald 1174c8626498SMilanka Ringwald void hfp_hf_enable_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, 1); 1176ce263fc8SMatthias Ringwald } 1177ce263fc8SMatthias Ringwald 1178c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1179c8626498SMilanka Ringwald hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1180ce263fc8SMatthias Ringwald } 1181ce263fc8SMatthias Ringwald 11823deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format 1183c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 1184c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1185a0ffb263SMatthias Ringwald if (!hfp_connection) { 1186a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 11873deb3ec6SMatthias Ringwald return; 11883deb3ec6SMatthias Ringwald } 1189a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1190a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1191a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11923deb3ec6SMatthias Ringwald } 11933deb3ec6SMatthias Ringwald 1194c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 1195c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1196a0ffb263SMatthias Ringwald if (!hfp_connection) { 1197a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 11983deb3ec6SMatthias Ringwald return; 11993deb3ec6SMatthias Ringwald } 1200a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1201ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1202a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1203ce263fc8SMatthias Ringwald break; 1204ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1205a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1206ce263fc8SMatthias Ringwald break; 1207ce263fc8SMatthias Ringwald default: 1208ce263fc8SMatthias Ringwald break; 1209ce263fc8SMatthias Ringwald } 1210a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12113deb3ec6SMatthias Ringwald } 12123deb3ec6SMatthias Ringwald 1213c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 1214c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1215a0ffb263SMatthias Ringwald if (!hfp_connection) { 1216a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12173deb3ec6SMatthias Ringwald return; 12183deb3ec6SMatthias Ringwald } 1219a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = enable; 1220a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12213deb3ec6SMatthias Ringwald } 12223deb3ec6SMatthias Ringwald 1223ce263fc8SMatthias Ringwald 1224c8626498SMilanka Ringwald void hfp_hf_enable_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, 1); 1226ce263fc8SMatthias Ringwald } 1227ce263fc8SMatthias Ringwald 1228c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1229c8626498SMilanka Ringwald hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1230ce263fc8SMatthias Ringwald } 1231ce263fc8SMatthias Ringwald 1232ce263fc8SMatthias Ringwald 1233c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 1234c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1235a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1236a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1237a33eb0c4SMilanka Ringwald return; 1238a33eb0c4SMilanka Ringwald } 1239a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 1240ce263fc8SMatthias Ringwald 1241a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1242a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 12433deb3ec6SMatthias Ringwald 1244a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 1245ce263fc8SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); 1246a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1247a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 1248ce263fc8SMatthias Ringwald } else { 1249a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 1250aa4dd815SMatthias Ringwald case HFP_CODECS_W4_AG_COMMON_CODEC: 1251aa4dd815SMatthias Ringwald break; 1252aa4dd815SMatthias Ringwald default: 1253a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 1254aa4dd815SMatthias Ringwald break; 12553deb3ec6SMatthias Ringwald } 1256ce263fc8SMatthias Ringwald } 1257ce263fc8SMatthias Ringwald 1258a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12593deb3ec6SMatthias Ringwald } 12603deb3ec6SMatthias Ringwald 1261c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 1262c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1263a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1264a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1265a33eb0c4SMilanka Ringwald return; 1266a33eb0c4SMilanka Ringwald } 1267a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 1268a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12693deb3ec6SMatthias Ringwald } 12703deb3ec6SMatthias Ringwald 1271c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 1272c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1273a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1274a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1275a33eb0c4SMilanka Ringwald return; 1276a33eb0c4SMilanka Ringwald } 1277ce263fc8SMatthias Ringwald 1278ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1279a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 1; 1280a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1281ce263fc8SMatthias Ringwald } else { 1282ce263fc8SMatthias Ringwald log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1283ce263fc8SMatthias Ringwald } 1284ce263fc8SMatthias Ringwald } 1285ce263fc8SMatthias Ringwald 1286c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 1287c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1288a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1289a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1290a33eb0c4SMilanka Ringwald return; 1291a33eb0c4SMilanka Ringwald } 1292a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 1293a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1294ce263fc8SMatthias Ringwald } 1295ce263fc8SMatthias Ringwald 1296c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 1297c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1298a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1299a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1300a33eb0c4SMilanka Ringwald return; 1301a33eb0c4SMilanka Ringwald } 1302ce263fc8SMatthias Ringwald 1303ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1304a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 1305a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1306ce263fc8SMatthias Ringwald } 1307ce263fc8SMatthias Ringwald } 1308ce263fc8SMatthias Ringwald 1309c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){ 1310c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1311a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1312a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1313a33eb0c4SMilanka Ringwald return; 1314a33eb0c4SMilanka Ringwald } 1315ce263fc8SMatthias Ringwald 1316ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1317a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 1; 1318a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1319ce263fc8SMatthias Ringwald } 1320ce263fc8SMatthias Ringwald } 1321ce263fc8SMatthias Ringwald 1322c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 1323c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1324a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1325a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1326a33eb0c4SMilanka Ringwald return; 1327a33eb0c4SMilanka Ringwald } 1328ce263fc8SMatthias Ringwald 1329ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1330ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1331a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 1; 1332a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1333ce263fc8SMatthias Ringwald } 1334ce263fc8SMatthias Ringwald } 1335ce263fc8SMatthias Ringwald 1336c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 1337c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1338a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1339a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1340a33eb0c4SMilanka Ringwald return; 1341a33eb0c4SMilanka Ringwald } 1342ce263fc8SMatthias Ringwald 1343ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1344ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1345a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 1; 1346a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1347ce263fc8SMatthias Ringwald } 1348ce263fc8SMatthias Ringwald } 1349ce263fc8SMatthias Ringwald 1350c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 1351c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1352a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1353a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1354a33eb0c4SMilanka Ringwald return; 1355a33eb0c4SMilanka Ringwald } 1356ce263fc8SMatthias Ringwald 1357ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1358ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1359a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 1; 1360a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1361ce263fc8SMatthias Ringwald } 1362ce263fc8SMatthias Ringwald } 1363ce263fc8SMatthias Ringwald 1364c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 1365c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1366a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1367a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1368a33eb0c4SMilanka Ringwald return; 1369a33eb0c4SMilanka Ringwald } 1370ce263fc8SMatthias Ringwald 1371ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1372ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1373a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 1; 1374a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1375ce263fc8SMatthias Ringwald } 1376ce263fc8SMatthias Ringwald } 1377ce263fc8SMatthias Ringwald 1378c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 1379c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1380a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1381a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1382a33eb0c4SMilanka Ringwald return; 1383a33eb0c4SMilanka Ringwald } 1384667ec068SMatthias Ringwald 1385667ec068SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1386667ec068SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1387a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1388a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 10 + index; 1389a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1390667ec068SMatthias Ringwald } 1391667ec068SMatthias Ringwald } 1392667ec068SMatthias Ringwald 1393c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 1394c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1395a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1396a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1397a33eb0c4SMilanka Ringwald return; 1398a33eb0c4SMilanka Ringwald } 1399667ec068SMatthias Ringwald 1400667ec068SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1401667ec068SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1402a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1403a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 20 + index; 1404a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1405667ec068SMatthias Ringwald } 1406667ec068SMatthias Ringwald } 1407ce263fc8SMatthias Ringwald 1408c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 1409c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1410a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1411a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1412a33eb0c4SMilanka Ringwald return; 1413a33eb0c4SMilanka Ringwald } 1414ce263fc8SMatthias Ringwald 1415a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 1; 1416ce263fc8SMatthias Ringwald snprintf(phone_number, sizeof(phone_number), "%s", number); 1417a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1418ce263fc8SMatthias Ringwald } 1419ce263fc8SMatthias Ringwald 1420c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 1421c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1422a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1423a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1424a33eb0c4SMilanka Ringwald return; 1425a33eb0c4SMilanka Ringwald } 1426ce263fc8SMatthias Ringwald 1427a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 1; 1428a0ffb263SMatthias Ringwald hfp_connection->memory_id = memory_id; 1429a0ffb263SMatthias Ringwald 1430a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1431ce263fc8SMatthias Ringwald } 1432ce263fc8SMatthias Ringwald 1433c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 1434c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1435a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1436a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1437a33eb0c4SMilanka Ringwald return; 1438a33eb0c4SMilanka Ringwald } 1439ce263fc8SMatthias Ringwald 1440a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 1; 1441a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1442ce263fc8SMatthias Ringwald } 1443ce263fc8SMatthias Ringwald 1444c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 1445c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1446a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1447a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1448a33eb0c4SMilanka Ringwald return; 1449a33eb0c4SMilanka Ringwald } 1450ce263fc8SMatthias Ringwald 1451a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 1; 1452a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1453ce263fc8SMatthias Ringwald } 1454ce263fc8SMatthias Ringwald 1455ce263fc8SMatthias Ringwald 1456c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 1457c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1458a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1459a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1460a33eb0c4SMilanka Ringwald return; 1461a33eb0c4SMilanka Ringwald } 1462ce263fc8SMatthias Ringwald 1463a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 1; 1464a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1465ce263fc8SMatthias Ringwald } 1466ce263fc8SMatthias Ringwald 1467ce263fc8SMatthias Ringwald 1468c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 1469c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1470a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1471a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1472a33eb0c4SMilanka Ringwald return; 1473a33eb0c4SMilanka Ringwald } 1474ce263fc8SMatthias Ringwald 1475a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 1; 1476a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1477ce263fc8SMatthias Ringwald } 1478ce263fc8SMatthias Ringwald 1479c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 1480c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1481a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1482a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1483a33eb0c4SMilanka Ringwald return; 1484a33eb0c4SMilanka Ringwald } 1485ce263fc8SMatthias Ringwald 1486a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 1; 1487a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1488ce263fc8SMatthias Ringwald } 1489ce263fc8SMatthias Ringwald 1490ce263fc8SMatthias Ringwald 1491c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1492c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1493a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1494a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1495a33eb0c4SMilanka Ringwald return; 1496a33eb0c4SMilanka Ringwald } 1497ce263fc8SMatthias Ringwald 1498a0ffb263SMatthias Ringwald hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1; 1499a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1500ce263fc8SMatthias Ringwald } 1501ce263fc8SMatthias Ringwald 1502c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 1503c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1504a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1505a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1506a33eb0c4SMilanka Ringwald return; 1507a33eb0c4SMilanka Ringwald } 1508ce263fc8SMatthias Ringwald 1509a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1510a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1511ce263fc8SMatthias Ringwald } 1512ce263fc8SMatthias Ringwald 1513c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1514c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1515a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1516a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1517a33eb0c4SMilanka Ringwald return; 1518a33eb0c4SMilanka Ringwald } 1519ce263fc8SMatthias Ringwald 1520a0ffb263SMatthias Ringwald hfp_connection->hf_activate_voice_recognition_notification = 1; 1521a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1522ce263fc8SMatthias Ringwald } 1523ce263fc8SMatthias Ringwald 1524c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){ 1525c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1526a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1527a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1528a33eb0c4SMilanka Ringwald return; 1529a33eb0c4SMilanka Ringwald } 1530ce263fc8SMatthias Ringwald 1531a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_voice_recognition_notification = 1; 1532a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1533ce263fc8SMatthias Ringwald } 1534ce263fc8SMatthias Ringwald 1535c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 1536c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1537a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1538a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1539a33eb0c4SMilanka Ringwald return; 1540a33eb0c4SMilanka Ringwald } 1541c8626498SMilanka Ringwald 1542a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain == gain) return; 1543a0ffb263SMatthias Ringwald if (gain < 0 || gain > 15){ 1544a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1545a0ffb263SMatthias Ringwald return; 1546a0ffb263SMatthias Ringwald } 1547a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 1548a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 1549a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1550ce263fc8SMatthias Ringwald } 1551ce263fc8SMatthias Ringwald 1552c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 1553c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1554a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1555a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1556a33eb0c4SMilanka Ringwald return; 1557a33eb0c4SMilanka Ringwald } 1558c8626498SMilanka Ringwald 1559a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain == gain) return; 1560a0ffb263SMatthias Ringwald if (gain < 0 || gain > 15){ 1561a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1562a0ffb263SMatthias Ringwald return; 1563a0ffb263SMatthias Ringwald } 1564a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 1565a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 1566a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1567ce263fc8SMatthias Ringwald } 1568ce263fc8SMatthias Ringwald 1569c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 1570c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1571a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1572a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1573a33eb0c4SMilanka Ringwald return; 1574a33eb0c4SMilanka Ringwald } 1575a33eb0c4SMilanka Ringwald 1576a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = code; 1577a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1578ce263fc8SMatthias Ringwald } 1579ce263fc8SMatthias Ringwald 1580c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 1581c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1582a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1583a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1584a33eb0c4SMilanka Ringwald return; 1585a33eb0c4SMilanka Ringwald } 1586a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 1; 1587a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1588ce263fc8SMatthias Ringwald } 15893deb3ec6SMatthias Ringwald 1590c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 1591c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1592a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1593a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1594a33eb0c4SMilanka Ringwald return; 1595a33eb0c4SMilanka Ringwald } 1596a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 1; 1597a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1598667ec068SMatthias Ringwald } 1599667ec068SMatthias Ringwald 1600667ec068SMatthias Ringwald 1601c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 1602c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1603a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1604a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1605a33eb0c4SMilanka Ringwald return; 1606a33eb0c4SMilanka Ringwald } 1607a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1608a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '?'; 1609a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1610667ec068SMatthias Ringwald } 1611667ec068SMatthias Ringwald 1612c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 1613c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1614a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1615a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1616a33eb0c4SMilanka Ringwald return; 1617a33eb0c4SMilanka Ringwald } 1618a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1619a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '0'; 1620a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1621667ec068SMatthias Ringwald } 1622667ec068SMatthias Ringwald 1623c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 1624c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1625a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1626a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1627a33eb0c4SMilanka Ringwald return; 1628a33eb0c4SMilanka Ringwald } 1629a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1630a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '1'; 1631a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1632667ec068SMatthias Ringwald } 1633667ec068SMatthias Ringwald 1634c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 1635c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1636a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1637a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1638a33eb0c4SMilanka Ringwald return; 1639a33eb0c4SMilanka Ringwald } 1640a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1641a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '2'; 1642a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1643667ec068SMatthias Ringwald } 1644667ec068SMatthias Ringwald 1645c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 1646c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1647a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1648a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1649a33eb0c4SMilanka Ringwald return; 1650a33eb0c4SMilanka Ringwald } 1651a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 1; 1652a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1653667ec068SMatthias Ringwald } 1654667ec068SMatthias Ringwald 1655c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 1656c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1657a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1658a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1659a33eb0c4SMilanka Ringwald return; 1660a33eb0c4SMilanka Ringwald } 1661667ec068SMatthias Ringwald // find index for assigned number 1662667ec068SMatthias Ringwald int i; 1663667ec068SMatthias Ringwald for (i = 0; i < hfp_indicators_nr ; i++){ 1664667ec068SMatthias Ringwald if (hfp_indicators[i] == assigned_number){ 1665667ec068SMatthias Ringwald // set value 1666667ec068SMatthias Ringwald hfp_indicators_value[i] = value; 1667667ec068SMatthias Ringwald // mark for update 1668a0ffb263SMatthias Ringwald if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1669a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap |= (1<<i); 1670667ec068SMatthias Ringwald // send update 1671a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1672a0ffb263SMatthias Ringwald } 1673667ec068SMatthias Ringwald return; 1674667ec068SMatthias Ringwald } 1675667ec068SMatthias Ringwald } 1676667ec068SMatthias Ringwald } 1677667ec068SMatthias Ringwald 1678