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 */ 37ab2c6ae4SMatthias Ringwald 38ab2c6ae4SMatthias 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 53235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 5459c6af15SMatthias Ringwald #include "btstack_debug.h" 553deb3ec6SMatthias Ringwald #include "btstack_memory.h" 5659c6af15SMatthias Ringwald #include "btstack_run_loop.h" 5759c6af15SMatthias Ringwald #include "classic/core.h" 5859c6af15SMatthias Ringwald #include "classic/hfp.h" 5959c6af15SMatthias Ringwald #include "classic/hfp_hf.h" 60efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 61746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 62023f2764SMatthias Ringwald #include "classic/sdp_util.h" 6359c6af15SMatthias Ringwald #include "hci.h" 6459c6af15SMatthias Ringwald #include "hci_cmd.h" 6559c6af15SMatthias Ringwald #include "hci_dump.h" 6659c6af15SMatthias Ringwald #include "l2cap.h" 673deb3ec6SMatthias Ringwald 683deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit"; 693deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 703deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0; 713deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 723deb3ec6SMatthias Ringwald 733deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0; 743deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS]; 75667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS]; 76667ec068SMatthias Ringwald 77667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9; 78667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9; 793deb3ec6SMatthias Ringwald 8013839019SMatthias Ringwald static btstack_packet_handler_t hfp_callback; 813deb3ec6SMatthias Ringwald 82ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status; 83ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status; 84ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status; 85ce263fc8SMatthias Ringwald 86ce263fc8SMatthias Ringwald static char phone_number[25]; 87ce263fc8SMatthias Ringwald 88c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 89c5b64319SMatthias Ringwald 9013839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 913deb3ec6SMatthias Ringwald hfp_callback = callback; 923deb3ec6SMatthias Ringwald if (callback == NULL){ 933deb3ec6SMatthias Ringwald log_error("hfp_hf_register_packet_handler called with NULL callback"); 943deb3ec6SMatthias Ringwald return; 953deb3ec6SMatthias Ringwald } 963deb3ec6SMatthias Ringwald hfp_callback = callback; 97f4000eebSMatthias Ringwald hfp_set_callback(callback); 983deb3ec6SMatthias Ringwald } 993deb3ec6SMatthias Ringwald 10013839019SMatthias 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){ 101a0ffb263SMatthias Ringwald if (!callback) return; 102a0ffb263SMatthias Ringwald uint8_t event[31]; 103a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 104a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 105a0ffb263SMatthias Ringwald event[2] = event_subtype; 106a0ffb263SMatthias Ringwald event[3] = status; 107a0ffb263SMatthias Ringwald event[4] = bnip_type; 108adaba9f3SMatthias Ringwald int size = (strlen(bnip_number) < sizeof(event) - 6) ? (int) strlen(bnip_number) : (int) sizeof(event) - 6; 109a0ffb263SMatthias Ringwald strncpy((char*)&event[5], bnip_number, size); 110a0ffb263SMatthias Ringwald event[5 + size] = 0; 11113839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 112a0ffb263SMatthias Ringwald } 113a0ffb263SMatthias Ringwald 11413839019SMatthias 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){ 115a0ffb263SMatthias Ringwald if (!callback) return; 116a0ffb263SMatthias Ringwald uint8_t event[30]; 117a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 118a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 119a0ffb263SMatthias Ringwald event[2] = event_subtype; 120a0ffb263SMatthias Ringwald event[3] = bnip_type; 121adaba9f3SMatthias Ringwald int size = (strlen(bnip_number) < sizeof(event) - 5) ? (int) strlen(bnip_number) : (int) sizeof(event) - 5; 122a0ffb263SMatthias Ringwald strncpy((char*)&event[4], bnip_number, size); 123a0ffb263SMatthias Ringwald event[4 + size] = 0; 12413839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 125a0ffb263SMatthias Ringwald } 126a0ffb263SMatthias Ringwald 12713839019SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir, 128a0ffb263SMatthias Ringwald uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){ 129a0ffb263SMatthias Ringwald if (!callback) return; 130a0ffb263SMatthias Ringwald uint8_t event[35]; 131a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 132a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 133a0ffb263SMatthias Ringwald event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 134a0ffb263SMatthias Ringwald event[3] = clcc_idx; 135a0ffb263SMatthias Ringwald event[4] = clcc_dir; 136a0ffb263SMatthias Ringwald event[6] = clcc_status; 137a0ffb263SMatthias Ringwald event[7] = clcc_mpty; 138a0ffb263SMatthias Ringwald event[8] = bnip_type; 139adaba9f3SMatthias Ringwald int size = (strlen(bnip_number) < sizeof(event) - 10) ? (int) strlen(bnip_number) : (int) sizeof(event) - 10; 140a0ffb263SMatthias Ringwald strncpy((char*)&event[9], bnip_number, size); 141a0ffb263SMatthias Ringwald event[9 + size] = 0; 14213839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 143a0ffb263SMatthias Ringwald } 144a0ffb263SMatthias Ringwald 145a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 1463deb3ec6SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 147a0ffb263SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 1483deb3ec6SMatthias Ringwald return hf && ag; 1493deb3ec6SMatthias Ringwald } 1503deb3ec6SMatthias Ringwald 151a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 1523deb3ec6SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING); 153a0ffb263SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING); 1543deb3ec6SMatthias Ringwald return hf && ag; 1553deb3ec6SMatthias Ringwald } 1563deb3ec6SMatthias Ringwald 1573deb3ec6SMatthias Ringwald 158a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 1593deb3ec6SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS); 160a0ffb263SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS); 1613deb3ec6SMatthias Ringwald return hf && ag; 1623deb3ec6SMatthias Ringwald } 1633deb3ec6SMatthias Ringwald 164ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size); 1653deb3ec6SMatthias Ringwald 1664f84bf36SMatthias 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){ 1673deb3ec6SMatthias Ringwald if (!name){ 1683deb3ec6SMatthias Ringwald name = default_hfp_hf_service_name; 1693deb3ec6SMatthias Ringwald } 170235946f1SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 1713deb3ec6SMatthias Ringwald 1724f84bf36SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 1734f84bf36SMatthias Ringwald // 1744f84bf36SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 1754f84bf36SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 1764f84bf36SMatthias Ringwald // 1774f84bf36SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 1784f84bf36SMatthias Ringwald if (supported_features & wide_band_speech){ 1794f84bf36SMatthias Ringwald sdp_features |= 1 << 5; // Wide band speech bit 1804f84bf36SMatthias Ringwald } 181aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 1824f84bf36SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 183aa4dd815SMatthias Ringwald } 1843deb3ec6SMatthias Ringwald 1853deb3ec6SMatthias Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 1863deb3ec6SMatthias Ringwald char buffer[20]; 1873deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features); 1883deb3ec6SMatthias Ringwald // printf("exchange_supported_features %s\n", buffer); 1893deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 1903deb3ec6SMatthias Ringwald } 1913deb3ec6SMatthias Ringwald 1923deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 1933deb3ec6SMatthias Ringwald char buffer[30]; 1943deb3ec6SMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_AVAILABLE_CODECS); 1953deb3ec6SMatthias Ringwald offset += join(buffer+offset, sizeof(buffer)-offset, hfp_codecs, hfp_codecs_nr); 1963deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 1973deb3ec6SMatthias Ringwald buffer[offset] = 0; 1983deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 1993deb3ec6SMatthias Ringwald } 2003deb3ec6SMatthias Ringwald 2013deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){ 2023deb3ec6SMatthias Ringwald char buffer[20]; 2033deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=?\r\n", HFP_INDICATOR); 2043deb3ec6SMatthias Ringwald // printf("retrieve_indicators %s\n", buffer); 2053deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2063deb3ec6SMatthias Ringwald } 2073deb3ec6SMatthias Ringwald 2083deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){ 2093deb3ec6SMatthias Ringwald char buffer[20]; 2103deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_INDICATOR); 2113deb3ec6SMatthias Ringwald // printf("retrieve_indicators_status %s\n", buffer); 2123deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2133deb3ec6SMatthias Ringwald } 2143deb3ec6SMatthias Ringwald 2153deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 2163deb3ec6SMatthias Ringwald char buffer[20]; 2173deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 2183deb3ec6SMatthias Ringwald // printf("toggle_indicator_status_update %s\n", buffer); 2193deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2203deb3ec6SMatthias Ringwald } 2213deb3ec6SMatthias Ringwald 2223deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 2233deb3ec6SMatthias Ringwald char buffer[50]; 2243deb3ec6SMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 2253deb3ec6SMatthias Ringwald offset += join_bitmap(buffer+offset, sizeof(buffer)-offset, indicators_status, indicators_nr); 2263deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 2273deb3ec6SMatthias Ringwald buffer[offset] = 0; 2283deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2293deb3ec6SMatthias Ringwald } 2303deb3ec6SMatthias Ringwald 2313deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 2323deb3ec6SMatthias Ringwald char buffer[20]; 2333deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=?\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES); 2343deb3ec6SMatthias Ringwald // printf("retrieve_can_hold_call %s\n", buffer); 2353deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2363deb3ec6SMatthias Ringwald } 2373deb3ec6SMatthias Ringwald 2383deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 2393deb3ec6SMatthias Ringwald char buffer[30]; 2403deb3ec6SMatthias Ringwald int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 2413deb3ec6SMatthias Ringwald offset += join(buffer+offset, sizeof(buffer)-offset, hfp_indicators, hfp_indicators_nr); 2423deb3ec6SMatthias Ringwald offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n"); 2433deb3ec6SMatthias Ringwald buffer[offset] = 0; 2443deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2453deb3ec6SMatthias Ringwald } 2463deb3ec6SMatthias Ringwald 2473deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){ 2483deb3ec6SMatthias Ringwald char buffer[20]; 2493deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=?\r\n", HFP_GENERIC_STATUS_INDICATOR); 2503deb3ec6SMatthias Ringwald // printf("retrieve_supported_generic_status_indicators %s\n", buffer); 2513deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2523deb3ec6SMatthias Ringwald } 2533deb3ec6SMatthias Ringwald 2543deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){ 2553deb3ec6SMatthias Ringwald char buffer[20]; 2563deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_GENERIC_STATUS_INDICATOR); 2573deb3ec6SMatthias Ringwald // printf("list_initital_supported_generic_status_indicators %s\n", buffer); 2583deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2593deb3ec6SMatthias Ringwald } 2603deb3ec6SMatthias Ringwald 2613deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 2623deb3ec6SMatthias Ringwald char buffer[20]; 2633deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=3,0\r\n", HFP_QUERY_OPERATOR_SELECTION); 2643deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2653deb3ec6SMatthias Ringwald } 2663deb3ec6SMatthias Ringwald 2673deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 2683deb3ec6SMatthias Ringwald char buffer[20]; 2693deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_QUERY_OPERATOR_SELECTION); 2703deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2713deb3ec6SMatthias Ringwald } 2723deb3ec6SMatthias Ringwald 2733deb3ec6SMatthias Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 2743deb3ec6SMatthias Ringwald char buffer[20]; 2753deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 2763deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2773deb3ec6SMatthias Ringwald } 2783deb3ec6SMatthias Ringwald 2793deb3ec6SMatthias Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 2803deb3ec6SMatthias Ringwald char buffer[20]; 2813deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_TRIGGER_CODEC_CONNECTION_SETUP); 2823deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2833deb3ec6SMatthias Ringwald } 2843deb3ec6SMatthias Ringwald 2853deb3ec6SMatthias Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 2863deb3ec6SMatthias Ringwald char buffer[20]; 2873deb3ec6SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec); 2883deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2893deb3ec6SMatthias Ringwald } 2903deb3ec6SMatthias Ringwald 291ce263fc8SMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){ 292ce263fc8SMatthias Ringwald char buffer[10]; 293ce263fc8SMatthias Ringwald sprintf(buffer, "%s\r\n", HFP_CALL_ANSWERED); 294ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 295ce263fc8SMatthias Ringwald } 296ce263fc8SMatthias Ringwald 297ce263fc8SMatthias Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 298ce263fc8SMatthias Ringwald char buffer[40]; 299ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_SET_MICROPHONE_GAIN, gain); 300ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 301ce263fc8SMatthias Ringwald } 302ce263fc8SMatthias Ringwald 303ce263fc8SMatthias Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 304ce263fc8SMatthias Ringwald char buffer[40]; 305ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_SET_SPEAKER_GAIN, gain); 306ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 307ce263fc8SMatthias Ringwald } 308ce263fc8SMatthias Ringwald 309ce263fc8SMatthias Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 310ce263fc8SMatthias Ringwald char buffer[40]; 311ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CLIP, activate); 312ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 313ce263fc8SMatthias Ringwald } 314ce263fc8SMatthias Ringwald 315ce263fc8SMatthias Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){ 316ce263fc8SMatthias Ringwald char buffer[40]; 317ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_TURN_OFF_EC_AND_NR, activate); 318ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 319ce263fc8SMatthias Ringwald } 320ce263fc8SMatthias Ringwald 321ce263fc8SMatthias Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 322ce263fc8SMatthias Ringwald char buffer[40]; 323ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate); 324ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 325ce263fc8SMatthias Ringwald } 326ce263fc8SMatthias Ringwald 327ce263fc8SMatthias Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 328ce263fc8SMatthias Ringwald char buffer[40]; 329ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 330ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 331ce263fc8SMatthias Ringwald } 332ce263fc8SMatthias Ringwald 333ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 334ce263fc8SMatthias Ringwald char buffer[40]; 335ce263fc8SMatthias Ringwald sprintf(buffer, "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number); 336ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 337ce263fc8SMatthias Ringwald } 338ce263fc8SMatthias Ringwald 339a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){ 340ce263fc8SMatthias Ringwald char buffer[40]; 341a0ffb263SMatthias Ringwald sprintf(buffer, "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id); 342ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 343ce263fc8SMatthias Ringwald } 344ce263fc8SMatthias Ringwald 345ce263fc8SMatthias Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 346ce263fc8SMatthias Ringwald char buffer[20]; 347ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_REDIAL_LAST_NUMBER); 348ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 349ce263fc8SMatthias Ringwald } 350ce263fc8SMatthias Ringwald 351ce263fc8SMatthias Ringwald static int hfp_hf_send_chup(uint16_t cid){ 352ce263fc8SMatthias Ringwald char buffer[20]; 353ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_HANG_UP_CALL); 354ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 355ce263fc8SMatthias Ringwald } 356ce263fc8SMatthias Ringwald 357f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){ 358ce263fc8SMatthias Ringwald char buffer[20]; 359ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 360ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 361ce263fc8SMatthias Ringwald } 362ce263fc8SMatthias Ringwald 363ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){ 364ce263fc8SMatthias Ringwald char buffer[20]; 365ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code); 366ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 367ce263fc8SMatthias Ringwald } 368ce263fc8SMatthias Ringwald 369ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){ 370ce263fc8SMatthias Ringwald char buffer[20]; 371ce263fc8SMatthias Ringwald sprintf(buffer, "AT%s=1\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG); 372ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 373ce263fc8SMatthias Ringwald } 374ce263fc8SMatthias Ringwald 375667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){ 376667ec068SMatthias Ringwald char buffer[20]; 377667ec068SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_LIST_CURRENT_CALLS); 378667ec068SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 379667ec068SMatthias Ringwald } 380667ec068SMatthias Ringwald 38113839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){ 3823deb3ec6SMatthias Ringwald if (!callback) return; 383a0ffb263SMatthias Ringwald uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1]; 3843deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3853deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3863deb3ec6SMatthias Ringwald event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 387a0ffb263SMatthias Ringwald event[3] = indicator.index; 388a0ffb263SMatthias Ringwald event[4] = indicator.status; 389a0ffb263SMatthias Ringwald strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE); 390a0ffb263SMatthias Ringwald event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0; 39113839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 3923deb3ec6SMatthias Ringwald } 3933deb3ec6SMatthias Ringwald 39413839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){ 3953deb3ec6SMatthias Ringwald if (!callback) return; 3963deb3ec6SMatthias Ringwald uint8_t event[24]; 3973deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3983deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3993deb3ec6SMatthias Ringwald event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED; 400a0ffb263SMatthias Ringwald event[3] = network_operator.mode; 401a0ffb263SMatthias Ringwald event[4] = network_operator.format; 402a0ffb263SMatthias Ringwald strcpy((char*)&event[5], network_operator.name); 40313839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 4043deb3ec6SMatthias Ringwald } 4053deb3ec6SMatthias Ringwald 406a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 407a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 408a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 409aa4dd815SMatthias Ringwald int done = 1; 4103deb3ec6SMatthias Ringwald 411a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 4123deb3ec6SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 413d715cf51SMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr); 414a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 415a0ffb263SMatthias Ringwald hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 4163deb3ec6SMatthias Ringwald break; 4173deb3ec6SMatthias Ringwald case HFP_NOTIFY_ON_CODECS: 418a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 419a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 4203deb3ec6SMatthias Ringwald break; 4213deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: 422a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 423a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 4243deb3ec6SMatthias Ringwald break; 4253deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS_STATUS: 426a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 427a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 4283deb3ec6SMatthias Ringwald break; 4293deb3ec6SMatthias Ringwald case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 430a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 431a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 4323deb3ec6SMatthias Ringwald break; 4333deb3ec6SMatthias Ringwald case HFP_RETRIEVE_CAN_HOLD_CALL: 434a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 435a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 4363deb3ec6SMatthias Ringwald break; 4373deb3ec6SMatthias Ringwald case HFP_LIST_GENERIC_STATUS_INDICATORS: 438a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 439a0ffb263SMatthias Ringwald hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4403deb3ec6SMatthias Ringwald break; 4413deb3ec6SMatthias Ringwald case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 442a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 443a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4443deb3ec6SMatthias Ringwald break; 4453deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 446a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 447a0ffb263SMatthias Ringwald hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4483deb3ec6SMatthias Ringwald break; 4493deb3ec6SMatthias Ringwald default: 450aa4dd815SMatthias Ringwald done = 0; 4513deb3ec6SMatthias Ringwald break; 4523deb3ec6SMatthias Ringwald } 4533deb3ec6SMatthias Ringwald return done; 4543deb3ec6SMatthias Ringwald } 4553deb3ec6SMatthias Ringwald 456ce263fc8SMatthias Ringwald 457a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 458a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 459a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 460ce263fc8SMatthias Ringwald 461ce263fc8SMatthias Ringwald int done = 0; 462a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 463a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 464ce263fc8SMatthias Ringwald done = 1; 465a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 466ce263fc8SMatthias Ringwald return done; 467ce263fc8SMatthias Ringwald }; 468a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators){ 469a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 470ce263fc8SMatthias Ringwald done = 1; 471a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 472a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap, 473a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr); 474ce263fc8SMatthias Ringwald return done; 475ce263fc8SMatthias Ringwald } 476ce263fc8SMatthias Ringwald 477a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 478ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 479a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 480a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 481a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 482ce263fc8SMatthias Ringwald return 1; 483ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 484a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 485a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 486a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 487ce263fc8SMatthias Ringwald return 1; 488ce263fc8SMatthias Ringwald default: 489ce263fc8SMatthias Ringwald break; 490ce263fc8SMatthias Ringwald } 491ce263fc8SMatthias Ringwald 492a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 493a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 494ce263fc8SMatthias Ringwald done = 1; 495a0ffb263SMatthias Ringwald hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 496ce263fc8SMatthias Ringwald return done; 497ce263fc8SMatthias Ringwald } 498ce263fc8SMatthias Ringwald 499ce263fc8SMatthias Ringwald return done; 500ce263fc8SMatthias Ringwald } 501ce263fc8SMatthias Ringwald 502a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 503ce263fc8SMatthias Ringwald /* events ( == commands): 504ce263fc8SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 505ce263fc8SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 506ce263fc8SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC 507ce263fc8SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS 508ce263fc8SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 509ce263fc8SMatthias Ringwald */ 510ce263fc8SMatthias Ringwald 511a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 512ce263fc8SMatthias Ringwald 513a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 514ce263fc8SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 515a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0; 516ce263fc8SMatthias Ringwald 517a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 518a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 519a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 520ce263fc8SMatthias Ringwald return 1; 521ce263fc8SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 522a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 523a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 524a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 525ce263fc8SMatthias Ringwald 526a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 527a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 528a0ffb263SMatthias Ringwald hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 529ce263fc8SMatthias Ringwald break; 530ce263fc8SMatthias Ringwald 5316a7f44bdSMilanka Ringwald case HFP_CMD_AG_SUGGESTED_CODEC:{ 5326a7f44bdSMilanka Ringwald if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){ 533a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 534a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 535a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 5360e0be203SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 5370e0be203SMatthias Ringwald log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD"); 538fcb08cdbSMilanka Ringwald hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 539ce263fc8SMatthias Ringwald } else { 540a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 541a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 542a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 543a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 544a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 545a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 546ce263fc8SMatthias Ringwald 547ce263fc8SMatthias Ringwald } 548ce263fc8SMatthias Ringwald break; 5496a7f44bdSMilanka Ringwald } 550ce263fc8SMatthias Ringwald default: 551ce263fc8SMatthias Ringwald break; 552ce263fc8SMatthias Ringwald } 553ce263fc8SMatthias Ringwald return 0; 554ce263fc8SMatthias Ringwald } 555ce263fc8SMatthias Ringwald 556a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 557a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 558a0ffb263SMatthias Ringwald hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0; 559ce263fc8SMatthias Ringwald 560ce263fc8SMatthias Ringwald 561a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){ 562a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 563a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 564a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 565ce263fc8SMatthias Ringwald return 1; 566ce263fc8SMatthias Ringwald } 567ce263fc8SMatthias Ringwald 568a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 569ce263fc8SMatthias Ringwald 570ce263fc8SMatthias Ringwald // run codecs exchange 571a0ffb263SMatthias Ringwald int done = codecs_exchange_state_machine(hfp_connection); 572ce263fc8SMatthias Ringwald if (done) return 1; 573ce263fc8SMatthias Ringwald 574ce263fc8SMatthias Ringwald return 0; 575ce263fc8SMatthias Ringwald } 576ce263fc8SMatthias Ringwald 577a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 578a0ffb263SMatthias Ringwald if (hfp_connection->hf_answer_incoming_call){ 579a0ffb263SMatthias Ringwald hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 580a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 0; 581ce263fc8SMatthias Ringwald return 1; 582ce263fc8SMatthias Ringwald } 583ce263fc8SMatthias Ringwald return 0; 584ce263fc8SMatthias Ringwald } 585ce263fc8SMatthias Ringwald 586a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){ 587a0ffb263SMatthias Ringwald if (!hfp_connection) return; 588724f400dSMatthias Ringwald if (!hfp_connection->rfcomm_cid) return; 5897522e673SMatthias Ringwald 590b72c4a9eSMatthias Ringwald if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){ 591b72c4a9eSMatthias Ringwald 592b72c4a9eSMatthias Ringwald hfp_connection->hf_accept_sco = 0; 5937522e673SMatthias Ringwald 5947522e673SMatthias Ringwald // notify about codec selection if not done already 5957522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 5967522e673SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 5977522e673SMatthias Ringwald } 5987522e673SMatthias Ringwald 5997522e673SMatthias Ringwald // remote supported feature eSCO is set if link type is eSCO 6007522e673SMatthias Ringwald // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 6017522e673SMatthias Ringwald uint16_t max_latency; 6027522e673SMatthias Ringwald uint8_t retransmission_effort; 6037522e673SMatthias Ringwald uint16_t packet_types; 6047522e673SMatthias Ringwald 605d9f77559SMatthias Ringwald if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 6067522e673SMatthias Ringwald max_latency = 0x000c; 6077522e673SMatthias Ringwald retransmission_effort = 0x02; 6087522e673SMatthias Ringwald packet_types = 0x388; 6097522e673SMatthias Ringwald } else { 6107522e673SMatthias Ringwald max_latency = 0xffff; 6117522e673SMatthias Ringwald retransmission_effort = 0xff; 6127522e673SMatthias Ringwald packet_types = 0x003f; 6137522e673SMatthias Ringwald } 6147522e673SMatthias Ringwald 6157522e673SMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 6167522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 6177522e673SMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data 6187522e673SMatthias Ringwald } 6197522e673SMatthias Ringwald 620b72c4a9eSMatthias Ringwald log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting); 6217522e673SMatthias Ringwald hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 6227522e673SMatthias Ringwald sco_voice_setting, retransmission_effort, packet_types); 6237522e673SMatthias Ringwald return; 6247522e673SMatthias Ringwald } 6257522e673SMatthias Ringwald 626a0ffb263SMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return; 627ce263fc8SMatthias Ringwald 628a0ffb263SMatthias Ringwald int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 629ce263fc8SMatthias Ringwald if (!done){ 630a0ffb263SMatthias Ringwald done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 631ce263fc8SMatthias Ringwald } 632ce263fc8SMatthias Ringwald if (!done){ 633a0ffb263SMatthias Ringwald done = hfp_hf_run_for_audio_connection(hfp_connection); 634ce263fc8SMatthias Ringwald } 635ce263fc8SMatthias Ringwald if (!done){ 636a0ffb263SMatthias Ringwald done = call_setup_state_machine(hfp_connection); 637ce263fc8SMatthias Ringwald } 638ce263fc8SMatthias Ringwald 6391016a228SMatthias Ringwald // don't send a new command while ok still pending 6401016a228SMatthias Ringwald if (hfp_connection->ok_pending) return; 6411016a228SMatthias Ringwald 642a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 643a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 644a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 645a0ffb263SMatthias Ringwald hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 646ce263fc8SMatthias Ringwald return; 647ce263fc8SMatthias Ringwald } 648ce263fc8SMatthias Ringwald 649a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 650a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 651a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 652a0ffb263SMatthias Ringwald hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 653ce263fc8SMatthias Ringwald return; 654ce263fc8SMatthias Ringwald } 655ce263fc8SMatthias Ringwald 656a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_calling_line_notification){ 657a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 0; 658a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 659a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 660ce263fc8SMatthias Ringwald return; 661ce263fc8SMatthias Ringwald } 662ce263fc8SMatthias Ringwald 663a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_calling_line_notification){ 664a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 0; 665a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 666a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 667ce263fc8SMatthias Ringwald return; 668ce263fc8SMatthias Ringwald } 669ce263fc8SMatthias Ringwald 670a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 671a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 672a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 673a0ffb263SMatthias Ringwald hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0); 674ce263fc8SMatthias Ringwald return; 675ce263fc8SMatthias Ringwald } 676ce263fc8SMatthias Ringwald 677a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){ 678a0ffb263SMatthias Ringwald hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0; 679a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 680a0ffb263SMatthias Ringwald hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1); 681ce263fc8SMatthias Ringwald return; 682ce263fc8SMatthias Ringwald } 683ce263fc8SMatthias Ringwald 684a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_voice_recognition_notification){ 685a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_voice_recognition_notification = 0; 686a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 687a0ffb263SMatthias Ringwald hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 688ce263fc8SMatthias Ringwald return; 689ce263fc8SMatthias Ringwald } 690ce263fc8SMatthias Ringwald 691a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_voice_recognition_notification){ 692a0ffb263SMatthias Ringwald hfp_connection->hf_activate_voice_recognition_notification = 0; 693a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 694a0ffb263SMatthias Ringwald hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 695ce263fc8SMatthias Ringwald return; 696ce263fc8SMatthias Ringwald } 697ce263fc8SMatthias Ringwald 698ce263fc8SMatthias Ringwald 699a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_call_waiting_notification){ 700a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 0; 701a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 702a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 703ce263fc8SMatthias Ringwald return; 704ce263fc8SMatthias Ringwald } 705ce263fc8SMatthias Ringwald 706a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_call_waiting_notification){ 707a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 0; 708a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 709a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 710ce263fc8SMatthias Ringwald return; 711ce263fc8SMatthias Ringwald } 712ce263fc8SMatthias Ringwald 713a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_outgoing_call){ 714a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 0; 715a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 716a0ffb263SMatthias Ringwald hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 717ce263fc8SMatthias Ringwald return; 718ce263fc8SMatthias Ringwald } 719ce263fc8SMatthias Ringwald 720a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_memory_dialing){ 721a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 0; 722a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 723a0ffb263SMatthias Ringwald hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 724ce263fc8SMatthias Ringwald return; 725ce263fc8SMatthias Ringwald } 726ce263fc8SMatthias Ringwald 727a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_redial_last_number){ 728a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 0; 729a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 730a0ffb263SMatthias Ringwald hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 731ce263fc8SMatthias Ringwald return; 732ce263fc8SMatthias Ringwald } 733ce263fc8SMatthias Ringwald 734a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chup){ 735a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 0; 736a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 737a0ffb263SMatthias Ringwald hfp_hf_send_chup(hfp_connection->rfcomm_cid); 738ce263fc8SMatthias Ringwald return; 739ce263fc8SMatthias Ringwald } 740ce263fc8SMatthias Ringwald 741a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_0){ 742a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 0; 743a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 744a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 745ce263fc8SMatthias Ringwald return; 746ce263fc8SMatthias Ringwald } 747ce263fc8SMatthias Ringwald 748a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_1){ 749a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 0; 750a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 751a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 752ce263fc8SMatthias Ringwald return; 753ce263fc8SMatthias Ringwald } 754ce263fc8SMatthias Ringwald 755a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_2){ 756a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 0; 757a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 758a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 759ce263fc8SMatthias Ringwald return; 760ce263fc8SMatthias Ringwald } 761ce263fc8SMatthias Ringwald 762a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_3){ 763a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 0; 764a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 765a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 766ce263fc8SMatthias Ringwald return; 767ce263fc8SMatthias Ringwald } 768ce263fc8SMatthias Ringwald 769a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_4){ 770a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 0; 771a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 772a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 773ce263fc8SMatthias Ringwald return; 774ce263fc8SMatthias Ringwald } 775ce263fc8SMatthias Ringwald 776a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_x){ 777a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 0; 778a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 779a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 780667ec068SMatthias Ringwald return; 781667ec068SMatthias Ringwald } 782667ec068SMatthias Ringwald 783a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_dtmf_code){ 784a0ffb263SMatthias Ringwald char code = hfp_connection->hf_send_dtmf_code; 785a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = 0; 786a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 787a0ffb263SMatthias Ringwald hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 788ce263fc8SMatthias Ringwald return; 789ce263fc8SMatthias Ringwald } 790ce263fc8SMatthias Ringwald 791a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_binp){ 792a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 0; 793a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 794a0ffb263SMatthias Ringwald hfp_hf_send_binp(hfp_connection->rfcomm_cid); 795ce263fc8SMatthias Ringwald return; 796ce263fc8SMatthias Ringwald } 797ce263fc8SMatthias Ringwald 798a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_clcc){ 799a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 0; 800a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 801a0ffb263SMatthias Ringwald hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 802667ec068SMatthias Ringwald return; 803667ec068SMatthias Ringwald } 804667ec068SMatthias Ringwald 805a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_rrh){ 806a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 0; 807667ec068SMatthias Ringwald char buffer[20]; 808a0ffb263SMatthias Ringwald switch (hfp_connection->hf_send_rrh_command){ 809667ec068SMatthias Ringwald case '?': 810667ec068SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD); 811a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 812667ec068SMatthias Ringwald return; 813667ec068SMatthias Ringwald case '0': 814667ec068SMatthias Ringwald case '1': 815667ec068SMatthias Ringwald case '2': 816a0ffb263SMatthias Ringwald sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command); 817a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 818667ec068SMatthias Ringwald return; 819667ec068SMatthias Ringwald default: 820667ec068SMatthias Ringwald break; 821667ec068SMatthias Ringwald } 822667ec068SMatthias Ringwald return; 823667ec068SMatthias Ringwald } 824667ec068SMatthias Ringwald 825a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_cnum){ 826a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 0; 827667ec068SMatthias Ringwald char buffer[20]; 828667ec068SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION); 829a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 830667ec068SMatthias Ringwald return; 831667ec068SMatthias Ringwald } 832667ec068SMatthias Ringwald 833667ec068SMatthias Ringwald // update HF indicators 834a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_update_bitmap){ 835667ec068SMatthias Ringwald int i; 836667ec068SMatthias Ringwald for (i=0;i<hfp_indicators_nr;i++){ 837a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 838a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].state){ 839a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 840a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 841667ec068SMatthias Ringwald char buffer[30]; 842ecb7d461SMatthias Ringwald sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]); 843a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 844667ec068SMatthias Ringwald } else { 84560ebb071SMilanka Ringwald log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]); 846667ec068SMatthias Ringwald } 847667ec068SMatthias Ringwald return; 848667ec068SMatthias Ringwald } 849667ec068SMatthias Ringwald } 850667ec068SMatthias Ringwald } 851667ec068SMatthias Ringwald 852ce263fc8SMatthias Ringwald if (done) return; 853ce263fc8SMatthias Ringwald // deal with disconnect 854a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 855ce263fc8SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 856a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 857a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 858ce263fc8SMatthias Ringwald break; 859ce263fc8SMatthias Ringwald 860ce263fc8SMatthias Ringwald default: 861ce263fc8SMatthias Ringwald break; 862ce263fc8SMatthias Ringwald } 863ce263fc8SMatthias Ringwald } 864ce263fc8SMatthias Ringwald 865a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 866a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 8676a7f44bdSMilanka Ringwald 868d0c4aea6SMilanka Ringwald hfp_emit_slc_connection_event(hfp_callback, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 8697522e673SMatthias Ringwald 870667ec068SMatthias Ringwald // restore volume settings 871a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = hfp_hf_speaker_gain; 872a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 873667ec068SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 874a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = hfp_hf_microphone_gain; 875a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 876667ec068SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 877667ec068SMatthias Ringwald // enable all indicators 878667ec068SMatthias Ringwald int i; 879667ec068SMatthias Ringwald for (i=0;i<hfp_indicators_nr;i++){ 880a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i]; 881a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].state = 1; 882667ec068SMatthias Ringwald } 883ce263fc8SMatthias Ringwald } 884ce263fc8SMatthias Ringwald 885a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){ 886a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 887a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 8883deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 889a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 890a0ffb263SMatthias Ringwald hfp_connection->state = HFP_NOTIFY_ON_CODECS; 8913deb3ec6SMatthias Ringwald break; 8923deb3ec6SMatthias Ringwald } 893a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 8943deb3ec6SMatthias Ringwald break; 8953deb3ec6SMatthias Ringwald 8963deb3ec6SMatthias Ringwald case HFP_W4_NOTIFY_ON_CODECS: 897a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 8983deb3ec6SMatthias Ringwald break; 8993deb3ec6SMatthias Ringwald 9003deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 901a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 9023deb3ec6SMatthias Ringwald break; 9033deb3ec6SMatthias Ringwald 9043deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 905a0ffb263SMatthias Ringwald hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 9063deb3ec6SMatthias Ringwald break; 9073deb3ec6SMatthias Ringwald 9083deb3ec6SMatthias Ringwald case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 909a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 910a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 9113deb3ec6SMatthias Ringwald break; 9123deb3ec6SMatthias Ringwald } 913a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 914a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 9153deb3ec6SMatthias Ringwald break; 9163deb3ec6SMatthias Ringwald } 917a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9183deb3ec6SMatthias Ringwald break; 9193deb3ec6SMatthias Ringwald 9203deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 921a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 922a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 9233deb3ec6SMatthias Ringwald break; 9243deb3ec6SMatthias Ringwald } 925a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9263deb3ec6SMatthias Ringwald break; 9273deb3ec6SMatthias Ringwald 9283deb3ec6SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 929a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 9303deb3ec6SMatthias Ringwald break; 9313deb3ec6SMatthias Ringwald 9323deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 933a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 9343deb3ec6SMatthias Ringwald break; 9353deb3ec6SMatthias Ringwald 9363deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 937a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9383deb3ec6SMatthias Ringwald break; 939ce263fc8SMatthias Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 940a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 941a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 9423deb3ec6SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0); 943ce263fc8SMatthias Ringwald break; 944ce263fc8SMatthias Ringwald } 9453deb3ec6SMatthias Ringwald 946a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 947a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 9483deb3ec6SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0); 949ce263fc8SMatthias Ringwald break; 9503deb3ec6SMatthias Ringwald } 9513deb3ec6SMatthias Ringwald 952a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 953ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 95460ebb071SMilanka Ringwald // printf("Format set, querying name\n"); 955a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 956ce263fc8SMatthias Ringwald break; 957ce263fc8SMatthias Ringwald case HPF_HF_QUERY_OPERATOR_W4_RESULT: 958a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 959a0ffb263SMatthias Ringwald hfp_emit_network_operator_event(hfp_callback, hfp_connection->network_operator); 960ce263fc8SMatthias Ringwald break; 961ce263fc8SMatthias Ringwald default: 962ce263fc8SMatthias Ringwald break; 9633deb3ec6SMatthias Ringwald } 964ce263fc8SMatthias Ringwald 965a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 966a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 967ce263fc8SMatthias Ringwald break; 9683deb3ec6SMatthias Ringwald } 9693deb3ec6SMatthias Ringwald 970a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 971aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 972a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 9733deb3ec6SMatthias Ringwald break; 974ce263fc8SMatthias Ringwald case HFP_CODECS_HF_CONFIRMED_CODEC: 975a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 976ce263fc8SMatthias Ringwald break; 9773deb3ec6SMatthias Ringwald default: 9783deb3ec6SMatthias Ringwald break; 9793deb3ec6SMatthias Ringwald } 9803deb3ec6SMatthias Ringwald break; 9813deb3ec6SMatthias Ringwald default: 9823deb3ec6SMatthias Ringwald break; 9833deb3ec6SMatthias Ringwald } 9843deb3ec6SMatthias Ringwald 9853deb3ec6SMatthias Ringwald // done 986a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 9873deb3ec6SMatthias Ringwald } 9883deb3ec6SMatthias Ringwald 9893deb3ec6SMatthias Ringwald 9903deb3ec6SMatthias Ringwald static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 991*8a46ec40SMatthias Ringwald UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 992*8a46ec40SMatthias Ringwald 993*8a46ec40SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 994*8a46ec40SMatthias Ringwald if (size < 1) return; 9959ec2630cSMatthias Ringwald 996a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 997a0ffb263SMatthias Ringwald if (!hfp_connection) return; 9983deb3ec6SMatthias Ringwald 999*8a46ec40SMatthias Ringwald // temp overwrite last byte (most likely \n for log_info) 10001e35c04dSMatthias Ringwald char last_char = packet[size-1]; 10011e35c04dSMatthias Ringwald packet[size-1] = 0; 10021e35c04dSMatthias Ringwald log_info("HFP_RX %s", packet); 10031e35c04dSMatthias Ringwald packet[size-1] = last_char; 10041e35c04dSMatthias Ringwald 1005*8a46ec40SMatthias Ringwald // process messages byte-wise 1006667ec068SMatthias Ringwald int pos, i, value; 10073deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 1008a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 1); 1009ce263fc8SMatthias Ringwald } 10103deb3ec6SMatthias Ringwald 1011a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1012667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1013a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1014a0ffb263SMatthias Ringwald // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type); 1015a0ffb263SMatthias Ringwald hfp_hf_emit_subscriber_information(hfp_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number); 1016667ec068SMatthias Ringwald break; 1017667ec068SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1018a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1019a0ffb263SMatthias Ringwald // printf("Response and Hold status: %s\n", hfp_connection->line_buffer); 10202308e108SMilanka Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1021667ec068SMatthias Ringwald break; 1022667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1023a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1024a0ffb263SMatthias Ringwald // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n", 1025a0ffb263SMatthias Ringwald // hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty, 1026a0ffb263SMatthias Ringwald // hfp_connection->bnip_number, hfp_connection->bnip_type); 1027a0ffb263SMatthias Ringwald hfp_hf_emit_enhanced_call_status(hfp_callback, hfp_connection->clcc_idx, 1028a0ffb263SMatthias Ringwald hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty, 1029a0ffb263SMatthias Ringwald hfp_connection->bnip_type, hfp_connection->bnip_number); 1030667ec068SMatthias Ringwald break; 1031ce263fc8SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1032a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10332308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1034667ec068SMatthias Ringwald hfp_hf_speaker_gain = value; 1035667ec068SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1036ce263fc8SMatthias Ringwald break; 1037ce263fc8SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1038a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10392308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1040667ec068SMatthias Ringwald hfp_hf_microphone_gain = value; 1041667ec068SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1042ce263fc8SMatthias Ringwald break; 1043ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1044a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1045a0ffb263SMatthias Ringwald hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1046a0ffb263SMatthias Ringwald break; 1047a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1048a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1049a0ffb263SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1050a0ffb263SMatthias Ringwald break; 1051a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1052a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 105389f1e358SMilanka Ringwald hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1054ce263fc8SMatthias Ringwald break; 1055ce263fc8SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1056a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1057a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1058a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1059a0ffb263SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1060ce263fc8SMatthias Ringwald break; 1061ce263fc8SMatthias Ringwald case HFP_CMD_ERROR: 1062a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1063a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 1064a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1065ce263fc8SMatthias Ringwald hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1); 1066ce263fc8SMatthias Ringwald break; 1067ce263fc8SMatthias Ringwald case HFP_CMD_OK: 1068a0ffb263SMatthias Ringwald hfp_hf_switch_on_ok(hfp_connection); 1069ce263fc8SMatthias Ringwald break; 1070ce263fc8SMatthias Ringwald case HFP_CMD_RING: 1071a0ffb263SMatthias Ringwald hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_RING); 1072ce263fc8SMatthias Ringwald break; 1073ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1074a0ffb263SMatthias Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1075a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators[i].status_changed) { 1076a0ffb263SMatthias Ringwald if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1077a0ffb263SMatthias Ringwald hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1078a0ffb263SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1079a0ffb263SMatthias Ringwald hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 1080d5ff3b26SMatthias Ringwald // avoid set but not used warning 1081d5ff3b26SMatthias Ringwald (void) hfp_callheld_status; 1082a0ffb263SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1083a0ffb263SMatthias Ringwald hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1084ce263fc8SMatthias Ringwald } 1085a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1086a0ffb263SMatthias Ringwald hfp_emit_ag_indicator_event(hfp_callback, hfp_connection->ag_indicators[i]); 10873deb3ec6SMatthias Ringwald break; 10883deb3ec6SMatthias Ringwald } 10893deb3ec6SMatthias Ringwald } 1090ce263fc8SMatthias Ringwald break; 1091ce263fc8SMatthias Ringwald default: 1092ce263fc8SMatthias Ringwald break; 10933deb3ec6SMatthias Ringwald } 1094a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 10953deb3ec6SMatthias Ringwald } 10963deb3ec6SMatthias Ringwald 10970cb5b971SMatthias Ringwald static void hfp_run(void){ 1098665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1099665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1100665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1101a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1102a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11033deb3ec6SMatthias Ringwald } 11043deb3ec6SMatthias Ringwald } 11053deb3ec6SMatthias Ringwald 1106ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 11073deb3ec6SMatthias Ringwald switch (packet_type){ 11083deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 11093deb3ec6SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size); 11103deb3ec6SMatthias Ringwald break; 11113deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1112e30a6a47SMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size); 1113202c8a4cSMatthias Ringwald break; 11143deb3ec6SMatthias Ringwald default: 11153deb3ec6SMatthias Ringwald break; 11163deb3ec6SMatthias Ringwald } 11173deb3ec6SMatthias Ringwald hfp_run(); 11183deb3ec6SMatthias Ringwald } 11193deb3ec6SMatthias Ringwald 1120a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1121d63c37a1SMatthias Ringwald // register for HCI events 1122d63c37a1SMatthias Ringwald hci_event_callback_registration.callback = &packet_handler; 1123d63c37a1SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 1124d63c37a1SMatthias Ringwald 1125d63c37a1SMatthias Ringwald rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff); 1126a0ffb263SMatthias Ringwald 1127d68dcce1SMatthias Ringwald hfp_set_packet_handler_for_rfcomm_connections(&packet_handler); 1128d68dcce1SMatthias Ringwald 1129a0ffb263SMatthias Ringwald hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1130a0ffb263SMatthias Ringwald hfp_codecs_nr = 0; 1131a0ffb263SMatthias Ringwald hfp_indicators_nr = 0; 1132a0ffb263SMatthias Ringwald hfp_hf_speaker_gain = 9; 1133a0ffb263SMatthias Ringwald hfp_hf_microphone_gain = 9; 1134a0ffb263SMatthias Ringwald } 1135a0ffb263SMatthias Ringwald 1136a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){ 11373deb3ec6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS){ 1138a0ffb263SMatthias Ringwald log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 11393deb3ec6SMatthias Ringwald return; 11403deb3ec6SMatthias Ringwald } 11413deb3ec6SMatthias Ringwald 11423deb3ec6SMatthias Ringwald hfp_codecs_nr = codecs_nr; 11433deb3ec6SMatthias Ringwald int i; 11443deb3ec6SMatthias Ringwald for (i=0; i<codecs_nr; i++){ 11453deb3ec6SMatthias Ringwald hfp_codecs[i] = codecs[i]; 11463deb3ec6SMatthias Ringwald } 11473deb3ec6SMatthias Ringwald } 11483deb3ec6SMatthias Ringwald 1149a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){ 11503deb3ec6SMatthias Ringwald hfp_supported_features = supported_features; 1151a0ffb263SMatthias Ringwald } 11523deb3ec6SMatthias Ringwald 1153a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){ 11543deb3ec6SMatthias Ringwald hfp_indicators_nr = indicators_nr; 11553deb3ec6SMatthias Ringwald int i; 1156a0ffb263SMatthias Ringwald for (i = 0; i < hfp_indicators_nr ; i++){ 11573deb3ec6SMatthias Ringwald hfp_indicators[i] = indicators[i]; 11583deb3ec6SMatthias Ringwald } 11593deb3ec6SMatthias Ringwald } 11603deb3ec6SMatthias Ringwald 11613deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1162235946f1SMatthias Ringwald hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY); 11633deb3ec6SMatthias Ringwald } 11643deb3ec6SMatthias Ringwald 1165c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 1166c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1167a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1168a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1169a33eb0c4SMilanka Ringwald return; 1170a33eb0c4SMilanka Ringwald } 1171a0ffb263SMatthias Ringwald hfp_release_service_level_connection(hfp_connection); 1172a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11733deb3ec6SMatthias Ringwald } 11743deb3ec6SMatthias Ringwald 1175c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 1176c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1177a0ffb263SMatthias Ringwald if (!hfp_connection) { 1178a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 11793deb3ec6SMatthias Ringwald return; 11803deb3ec6SMatthias Ringwald } 1181a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = enable; 1182a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11833deb3ec6SMatthias Ringwald } 11843deb3ec6SMatthias Ringwald 1185c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1186c8626498SMilanka Ringwald hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1187ce263fc8SMatthias Ringwald } 1188ce263fc8SMatthias Ringwald 1189c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1190c8626498SMilanka Ringwald hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1191ce263fc8SMatthias Ringwald } 1192ce263fc8SMatthias Ringwald 11933deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format 1194c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 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 hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1201a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1202a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12033deb3ec6SMatthias Ringwald } 12043deb3ec6SMatthias Ringwald 1205c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 1206c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1207a0ffb263SMatthias Ringwald if (!hfp_connection) { 1208a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12093deb3ec6SMatthias Ringwald return; 12103deb3ec6SMatthias Ringwald } 1211a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1212ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1213a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1214ce263fc8SMatthias Ringwald break; 1215ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1216a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1217ce263fc8SMatthias Ringwald break; 1218ce263fc8SMatthias Ringwald default: 1219ce263fc8SMatthias Ringwald break; 1220ce263fc8SMatthias Ringwald } 1221a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12223deb3ec6SMatthias Ringwald } 12233deb3ec6SMatthias Ringwald 1224c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 1225c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1226a0ffb263SMatthias Ringwald if (!hfp_connection) { 1227a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12283deb3ec6SMatthias Ringwald return; 12293deb3ec6SMatthias Ringwald } 1230a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = enable; 1231a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12323deb3ec6SMatthias Ringwald } 12333deb3ec6SMatthias Ringwald 1234ce263fc8SMatthias Ringwald 1235c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1236c8626498SMilanka Ringwald hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1237ce263fc8SMatthias Ringwald } 1238ce263fc8SMatthias Ringwald 1239c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1240c8626498SMilanka Ringwald hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1241ce263fc8SMatthias Ringwald } 1242ce263fc8SMatthias Ringwald 1243ce263fc8SMatthias Ringwald 1244c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 1245c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1246a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1247a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1248a33eb0c4SMilanka Ringwald return; 1249a33eb0c4SMilanka Ringwald } 1250a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 1251ce263fc8SMatthias Ringwald 1252a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1253a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 12543deb3ec6SMatthias Ringwald 1255a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 1256ce263fc8SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); 1257a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 1258a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 1; 1259ce263fc8SMatthias Ringwald } else { 1260a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 1261aa4dd815SMatthias Ringwald case HFP_CODECS_W4_AG_COMMON_CODEC: 1262aa4dd815SMatthias Ringwald break; 1263aa4dd815SMatthias Ringwald default: 1264a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 1265aa4dd815SMatthias Ringwald break; 12663deb3ec6SMatthias Ringwald } 1267ce263fc8SMatthias Ringwald } 1268ce263fc8SMatthias Ringwald 1269a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12703deb3ec6SMatthias Ringwald } 12713deb3ec6SMatthias Ringwald 1272c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 1273c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1274a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1275a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1276a33eb0c4SMilanka Ringwald return; 1277a33eb0c4SMilanka Ringwald } 1278a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 1279a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12803deb3ec6SMatthias Ringwald } 12813deb3ec6SMatthias Ringwald 1282c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 1283c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1284a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1285a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1286a33eb0c4SMilanka Ringwald return; 1287a33eb0c4SMilanka Ringwald } 1288ce263fc8SMatthias Ringwald 1289ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1290a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 1; 1291a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1292ce263fc8SMatthias Ringwald } else { 1293ce263fc8SMatthias Ringwald log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1294ce263fc8SMatthias Ringwald } 1295ce263fc8SMatthias Ringwald } 1296ce263fc8SMatthias Ringwald 1297c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 1298c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1299a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1300a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1301a33eb0c4SMilanka Ringwald return; 1302a33eb0c4SMilanka Ringwald } 1303a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 1304a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1305ce263fc8SMatthias Ringwald } 1306ce263fc8SMatthias Ringwald 1307c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 1308c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1309a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1310a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1311a33eb0c4SMilanka Ringwald return; 1312a33eb0c4SMilanka Ringwald } 1313ce263fc8SMatthias Ringwald 1314ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1315a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 1316a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1317ce263fc8SMatthias Ringwald } 1318ce263fc8SMatthias Ringwald } 1319ce263fc8SMatthias Ringwald 1320c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){ 1321c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1322a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1323a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1324a33eb0c4SMilanka Ringwald return; 1325a33eb0c4SMilanka Ringwald } 1326ce263fc8SMatthias Ringwald 1327ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1328a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 1; 1329a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1330ce263fc8SMatthias Ringwald } 1331ce263fc8SMatthias Ringwald } 1332ce263fc8SMatthias Ringwald 1333c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 1334c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1335a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1336a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1337a33eb0c4SMilanka Ringwald return; 1338a33eb0c4SMilanka Ringwald } 1339ce263fc8SMatthias Ringwald 1340ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1341ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1342a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 1; 1343a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1344ce263fc8SMatthias Ringwald } 1345ce263fc8SMatthias Ringwald } 1346ce263fc8SMatthias Ringwald 1347c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 1348c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1349a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1350a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1351a33eb0c4SMilanka Ringwald return; 1352a33eb0c4SMilanka Ringwald } 1353ce263fc8SMatthias Ringwald 1354ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1355ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1356a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 1; 1357a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1358ce263fc8SMatthias Ringwald } 1359ce263fc8SMatthias Ringwald } 1360ce263fc8SMatthias Ringwald 1361c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 1362c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1363a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1364a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1365a33eb0c4SMilanka Ringwald return; 1366a33eb0c4SMilanka Ringwald } 1367ce263fc8SMatthias Ringwald 1368ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1369ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1370a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 1; 1371a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1372ce263fc8SMatthias Ringwald } 1373ce263fc8SMatthias Ringwald } 1374ce263fc8SMatthias Ringwald 1375c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 1376c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1377a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1378a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1379a33eb0c4SMilanka Ringwald return; 1380a33eb0c4SMilanka Ringwald } 1381ce263fc8SMatthias Ringwald 1382ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1383ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1384a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 1; 1385a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1386ce263fc8SMatthias Ringwald } 1387ce263fc8SMatthias Ringwald } 1388ce263fc8SMatthias Ringwald 1389c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 1390c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1391a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1392a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1393a33eb0c4SMilanka Ringwald return; 1394a33eb0c4SMilanka Ringwald } 1395667ec068SMatthias Ringwald 1396667ec068SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1397667ec068SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1398a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1399a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 10 + index; 1400a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1401667ec068SMatthias Ringwald } 1402667ec068SMatthias Ringwald } 1403667ec068SMatthias Ringwald 1404c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 1405c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1406a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1407a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1408a33eb0c4SMilanka Ringwald return; 1409a33eb0c4SMilanka Ringwald } 1410667ec068SMatthias Ringwald 1411667ec068SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1412667ec068SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1413a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1414a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 20 + index; 1415a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1416667ec068SMatthias Ringwald } 1417667ec068SMatthias Ringwald } 1418ce263fc8SMatthias Ringwald 1419c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 1420c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1421a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1422a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1423a33eb0c4SMilanka Ringwald return; 1424a33eb0c4SMilanka Ringwald } 1425ce263fc8SMatthias Ringwald 1426a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 1; 1427ce263fc8SMatthias Ringwald snprintf(phone_number, sizeof(phone_number), "%s", number); 1428a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1429ce263fc8SMatthias Ringwald } 1430ce263fc8SMatthias Ringwald 1431c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 1432c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1433a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1434a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1435a33eb0c4SMilanka Ringwald return; 1436a33eb0c4SMilanka Ringwald } 1437ce263fc8SMatthias Ringwald 1438a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 1; 1439a0ffb263SMatthias Ringwald hfp_connection->memory_id = memory_id; 1440a0ffb263SMatthias Ringwald 1441a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1442ce263fc8SMatthias Ringwald } 1443ce263fc8SMatthias Ringwald 1444c8626498SMilanka Ringwald void hfp_hf_redial_last_number(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_initiate_redial_last_number = 1; 1452a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1453ce263fc8SMatthias Ringwald } 1454ce263fc8SMatthias Ringwald 1455c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 1456c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1457a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1458a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1459a33eb0c4SMilanka Ringwald return; 1460a33eb0c4SMilanka Ringwald } 1461ce263fc8SMatthias Ringwald 1462a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 1; 1463a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1464ce263fc8SMatthias Ringwald } 1465ce263fc8SMatthias Ringwald 1466ce263fc8SMatthias Ringwald 1467c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 1468c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1469a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1470a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1471a33eb0c4SMilanka Ringwald return; 1472a33eb0c4SMilanka Ringwald } 1473ce263fc8SMatthias Ringwald 1474a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 1; 1475a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1476ce263fc8SMatthias Ringwald } 1477ce263fc8SMatthias Ringwald 1478ce263fc8SMatthias Ringwald 1479c8626498SMilanka Ringwald void hfp_hf_activate_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_activate_calling_line_notification = 1; 1487a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1488ce263fc8SMatthias Ringwald } 1489ce263fc8SMatthias Ringwald 1490c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 1491c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1492a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1493a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1494a33eb0c4SMilanka Ringwald return; 1495a33eb0c4SMilanka Ringwald } 1496ce263fc8SMatthias Ringwald 1497a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 1; 1498a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1499ce263fc8SMatthias Ringwald } 1500ce263fc8SMatthias Ringwald 1501ce263fc8SMatthias Ringwald 1502c8626498SMilanka Ringwald void hfp_hf_activate_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_activate_echo_canceling_and_noise_reduction = 1; 1510a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1511ce263fc8SMatthias Ringwald } 1512ce263fc8SMatthias Ringwald 1513c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(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_deactivate_echo_canceling_and_noise_reduction = 1; 1521a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1522ce263fc8SMatthias Ringwald } 1523ce263fc8SMatthias Ringwald 1524c8626498SMilanka Ringwald void hfp_hf_activate_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_activate_voice_recognition_notification = 1; 1532a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1533ce263fc8SMatthias Ringwald } 1534ce263fc8SMatthias Ringwald 1535c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){ 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 } 1541ce263fc8SMatthias Ringwald 1542a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_voice_recognition_notification = 1; 1543a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1544ce263fc8SMatthias Ringwald } 1545ce263fc8SMatthias Ringwald 1546c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 1547c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1548a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1549a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1550a33eb0c4SMilanka Ringwald return; 1551a33eb0c4SMilanka Ringwald } 1552c8626498SMilanka Ringwald 1553a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain == gain) return; 1554a0ffb263SMatthias Ringwald if (gain < 0 || gain > 15){ 1555a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1556a0ffb263SMatthias Ringwald return; 1557a0ffb263SMatthias Ringwald } 1558a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 1559a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 1560a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1561ce263fc8SMatthias Ringwald } 1562ce263fc8SMatthias Ringwald 1563c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 1564c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1565a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1566a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1567a33eb0c4SMilanka Ringwald return; 1568a33eb0c4SMilanka Ringwald } 1569c8626498SMilanka Ringwald 1570a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain == gain) return; 1571a0ffb263SMatthias Ringwald if (gain < 0 || gain > 15){ 1572a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1573a0ffb263SMatthias Ringwald return; 1574a0ffb263SMatthias Ringwald } 1575a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 1576a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 1577a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1578ce263fc8SMatthias Ringwald } 1579ce263fc8SMatthias Ringwald 1580c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 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 } 1586a33eb0c4SMilanka Ringwald 1587a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = code; 1588a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1589ce263fc8SMatthias Ringwald } 1590ce263fc8SMatthias Ringwald 1591c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 1592c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1593a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1594a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1595a33eb0c4SMilanka Ringwald return; 1596a33eb0c4SMilanka Ringwald } 1597a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 1; 1598a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1599ce263fc8SMatthias Ringwald } 16003deb3ec6SMatthias Ringwald 1601c8626498SMilanka Ringwald void hfp_hf_query_current_call_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_clcc = 1; 1608a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1609667ec068SMatthias Ringwald } 1610667ec068SMatthias Ringwald 1611667ec068SMatthias Ringwald 1612c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(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 = '?'; 1620a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1621667ec068SMatthias Ringwald } 1622667ec068SMatthias Ringwald 1623c8626498SMilanka Ringwald void hfp_hf_rrh_hold_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 = '0'; 1631a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1632667ec068SMatthias Ringwald } 1633667ec068SMatthias Ringwald 1634c8626498SMilanka Ringwald void hfp_hf_rrh_accept_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 = '1'; 1642a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1643667ec068SMatthias Ringwald } 1644667ec068SMatthias Ringwald 1645c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(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_rrh = 1; 1652a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '2'; 1653a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1654667ec068SMatthias Ringwald } 1655667ec068SMatthias Ringwald 1656c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 1657c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1658a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1659a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1660a33eb0c4SMilanka Ringwald return; 1661a33eb0c4SMilanka Ringwald } 1662a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 1; 1663a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1664667ec068SMatthias Ringwald } 1665667ec068SMatthias Ringwald 1666c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 1667c8626498SMilanka Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle); 1668a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1669a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1670a33eb0c4SMilanka Ringwald return; 1671a33eb0c4SMilanka Ringwald } 1672667ec068SMatthias Ringwald // find index for assigned number 1673667ec068SMatthias Ringwald int i; 1674667ec068SMatthias Ringwald for (i = 0; i < hfp_indicators_nr ; i++){ 1675667ec068SMatthias Ringwald if (hfp_indicators[i] == assigned_number){ 1676667ec068SMatthias Ringwald // set value 1677667ec068SMatthias Ringwald hfp_indicators_value[i] = value; 1678667ec068SMatthias Ringwald // mark for update 1679a0ffb263SMatthias Ringwald if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1680a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap |= (1<<i); 1681667ec068SMatthias Ringwald // send update 1682a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1683a0ffb263SMatthias Ringwald } 1684667ec068SMatthias Ringwald return; 1685667ec068SMatthias Ringwald } 1686667ec068SMatthias Ringwald } 1687667ec068SMatthias Ringwald } 1688667ec068SMatthias Ringwald 1689