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" 55d4dd47ffSMatthias Ringwald #include "btstack_event.h" 563deb3ec6SMatthias Ringwald #include "btstack_memory.h" 5759c6af15SMatthias Ringwald #include "btstack_run_loop.h" 5859c6af15SMatthias Ringwald #include "classic/core.h" 5959c6af15SMatthias Ringwald #include "classic/hfp.h" 6059c6af15SMatthias Ringwald #include "classic/hfp_hf.h" 61efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 62746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 63023f2764SMatthias Ringwald #include "classic/sdp_util.h" 6459c6af15SMatthias Ringwald #include "hci.h" 6559c6af15SMatthias Ringwald #include "hci_cmd.h" 6659c6af15SMatthias Ringwald #include "hci_dump.h" 6759c6af15SMatthias Ringwald #include "l2cap.h" 683deb3ec6SMatthias Ringwald 6927950165SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 7027950165SMatthias Ringwald 713deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit"; 723deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 733deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0; 743deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 753deb3ec6SMatthias Ringwald 763deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0; 773deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS]; 78667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS]; 79667ec068SMatthias Ringwald 80667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9; 81667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9; 823deb3ec6SMatthias Ringwald 83ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback; 843deb3ec6SMatthias Ringwald 85ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status; 86ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status; 87ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status; 88ce263fc8SMatthias Ringwald 89ce263fc8SMatthias Ringwald static char phone_number[25]; 90ce263fc8SMatthias Ringwald 919c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){ 929c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 939c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 949c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 959c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 969c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 979c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 989c9c64c1SMatthias Ringwald return hfp_connection; 999c9c64c1SMatthias Ringwald } 1009c9c64c1SMatthias Ringwald return NULL; 1019c9c64c1SMatthias Ringwald } 1029c9c64c1SMatthias Ringwald 10313839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 1043deb3ec6SMatthias Ringwald if (callback == NULL){ 1053deb3ec6SMatthias Ringwald log_error("hfp_hf_register_packet_handler called with NULL callback"); 1063deb3ec6SMatthias Ringwald return; 1073deb3ec6SMatthias Ringwald } 108ca59be51SMatthias Ringwald hfp_hf_callback = callback; 109ca59be51SMatthias Ringwald hfp_set_hf_callback(callback); 1103deb3ec6SMatthias Ringwald } 1113deb3ec6SMatthias Ringwald 11213839019SMatthias 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){ 113a0ffb263SMatthias Ringwald if (!callback) return; 114a0ffb263SMatthias Ringwald uint8_t event[31]; 115a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 116a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 117a0ffb263SMatthias Ringwald event[2] = event_subtype; 118a0ffb263SMatthias Ringwald event[3] = status; 119a0ffb263SMatthias Ringwald event[4] = bnip_type; 120adaba9f3SMatthias Ringwald int size = (strlen(bnip_number) < sizeof(event) - 6) ? (int) strlen(bnip_number) : (int) sizeof(event) - 6; 121a0ffb263SMatthias Ringwald strncpy((char*)&event[5], bnip_number, size); 122a0ffb263SMatthias Ringwald event[5 + size] = 0; 12313839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 124a0ffb263SMatthias Ringwald } 125a0ffb263SMatthias Ringwald 12613839019SMatthias 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){ 127a0ffb263SMatthias Ringwald if (!callback) return; 128a0ffb263SMatthias Ringwald uint8_t event[30]; 129a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 130a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 131a0ffb263SMatthias Ringwald event[2] = event_subtype; 132a0ffb263SMatthias Ringwald event[3] = bnip_type; 133adaba9f3SMatthias Ringwald int size = (strlen(bnip_number) < sizeof(event) - 5) ? (int) strlen(bnip_number) : (int) sizeof(event) - 5; 134a0ffb263SMatthias Ringwald strncpy((char*)&event[4], bnip_number, size); 135a0ffb263SMatthias Ringwald event[4 + size] = 0; 13613839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 137a0ffb263SMatthias Ringwald } 138a0ffb263SMatthias Ringwald 13913839019SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir, 140a0ffb263SMatthias Ringwald uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){ 141a0ffb263SMatthias Ringwald if (!callback) return; 142a0ffb263SMatthias Ringwald uint8_t event[35]; 143a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 144a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 145a0ffb263SMatthias Ringwald event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 146a0ffb263SMatthias Ringwald event[3] = clcc_idx; 147a0ffb263SMatthias Ringwald event[4] = clcc_dir; 148a0ffb263SMatthias Ringwald event[6] = clcc_status; 149a0ffb263SMatthias Ringwald event[7] = clcc_mpty; 150a0ffb263SMatthias Ringwald event[8] = bnip_type; 151adaba9f3SMatthias Ringwald int size = (strlen(bnip_number) < sizeof(event) - 10) ? (int) strlen(bnip_number) : (int) sizeof(event) - 10; 152a0ffb263SMatthias Ringwald strncpy((char*)&event[9], bnip_number, size); 153a0ffb263SMatthias Ringwald event[9 + size] = 0; 15413839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 155a0ffb263SMatthias Ringwald } 156a0ffb263SMatthias Ringwald 157a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 1583deb3ec6SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 159a0ffb263SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 1603deb3ec6SMatthias Ringwald return hf && ag; 1613deb3ec6SMatthias Ringwald } 1623deb3ec6SMatthias Ringwald 163a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 1643deb3ec6SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING); 165a0ffb263SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING); 1663deb3ec6SMatthias Ringwald return hf && ag; 1673deb3ec6SMatthias Ringwald } 1683deb3ec6SMatthias Ringwald 1693deb3ec6SMatthias Ringwald 170a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 1713deb3ec6SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS); 172a0ffb263SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS); 1733deb3ec6SMatthias Ringwald return hf && ag; 1743deb3ec6SMatthias Ringwald } 1753deb3ec6SMatthias Ringwald 1764f84bf36SMatthias 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){ 1773deb3ec6SMatthias Ringwald if (!name){ 1783deb3ec6SMatthias Ringwald name = default_hfp_hf_service_name; 1793deb3ec6SMatthias Ringwald } 180235946f1SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 1813deb3ec6SMatthias Ringwald 1824f84bf36SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 1834f84bf36SMatthias Ringwald // 1844f84bf36SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 1854f84bf36SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 1864f84bf36SMatthias Ringwald // 18742bafa11SMatthias Ringwald // Wide band speech (bit 5) requires Codec negotiation 18842bafa11SMatthias Ringwald // 1894f84bf36SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 19042bafa11SMatthias Ringwald if (wide_band_speech && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 19142bafa11SMatthias Ringwald sdp_features |= 1 << 5; 1924f84bf36SMatthias Ringwald } 193aa4dd815SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 1944f84bf36SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 195aa4dd815SMatthias Ringwald } 1963deb3ec6SMatthias Ringwald 19789425bfcSMilanka Ringwald 19889425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){ 1993deb3ec6SMatthias Ringwald char buffer[20]; 20089425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "AT%s\r\n", cmd); 20189425bfcSMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 20289425bfcSMilanka Ringwald } 20389425bfcSMilanka Ringwald 20489425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){ 20589425bfcSMilanka Ringwald char buffer[20]; 20689425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "AT%s%s\r\n", cmd, mark); 20789425bfcSMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 20889425bfcSMilanka Ringwald } 20989425bfcSMilanka Ringwald 21086da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){ 21189425bfcSMilanka Ringwald char buffer[40]; 21289425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%d\r\n", cmd, value); 2133deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2143deb3ec6SMatthias Ringwald } 2153deb3ec6SMatthias Ringwald 2163deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 2173deb3ec6SMatthias Ringwald char buffer[30]; 21889425bfcSMilanka Ringwald const int size = sizeof(buffer); 21989425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS); 22089425bfcSMilanka Ringwald offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr); 22189425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n"); 2223deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2233deb3ec6SMatthias Ringwald } 2243deb3ec6SMatthias Ringwald 2253deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 2263deb3ec6SMatthias Ringwald char buffer[50]; 22789425bfcSMilanka Ringwald const int size = sizeof(buffer); 22889425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 22989425bfcSMilanka Ringwald offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr); 23089425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n"); 2313deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2323deb3ec6SMatthias Ringwald } 2333deb3ec6SMatthias Ringwald 2343deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 2353deb3ec6SMatthias Ringwald char buffer[30]; 23689425bfcSMilanka Ringwald const int size = sizeof(buffer); 23789425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 23889425bfcSMilanka Ringwald offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr); 23989425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n"); 2403deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2413deb3ec6SMatthias Ringwald } 2423deb3ec6SMatthias Ringwald 24389425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 2443deb3ec6SMatthias Ringwald char buffer[20]; 24589425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 246ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 247ce263fc8SMatthias Ringwald } 248ce263fc8SMatthias Ringwald 249ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 250ce263fc8SMatthias Ringwald char buffer[40]; 25189425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number); 252ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 253ce263fc8SMatthias Ringwald } 254ce263fc8SMatthias Ringwald 255a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){ 256ce263fc8SMatthias Ringwald char buffer[40]; 25789425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id); 258ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 259ce263fc8SMatthias Ringwald } 260ce263fc8SMatthias Ringwald 261f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){ 26289425bfcSMilanka Ringwald char buffer[40]; 26389425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 264ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 265ce263fc8SMatthias Ringwald } 266ce263fc8SMatthias Ringwald 267ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){ 268ce263fc8SMatthias Ringwald char buffer[20]; 26989425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code); 270ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 271ce263fc8SMatthias Ringwald } 272ce263fc8SMatthias Ringwald 27397d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){ 27497d2cadbSMatthias Ringwald return send_str_over_rfcomm(cid, "ATA\r\n"); 27597d2cadbSMatthias Ringwald } 27697d2cadbSMatthias Ringwald 27789425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 27889425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features); 27989425bfcSMilanka Ringwald } 28089425bfcSMilanka Ringwald 28189425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){ 28289425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?"); 28389425bfcSMilanka Ringwald } 28489425bfcSMilanka Ringwald 28589425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){ 28689425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?"); 28789425bfcSMilanka Ringwald } 28889425bfcSMilanka Ringwald 28989425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 29089425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?"); 29189425bfcSMilanka Ringwald } 29289425bfcSMilanka Ringwald 29389425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){ 29489425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?"); 29589425bfcSMilanka Ringwald } 29689425bfcSMilanka Ringwald 29789425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){ 29889425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?"); 29989425bfcSMilanka Ringwald } 30089425bfcSMilanka Ringwald 30189425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 30289425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0"); 30389425bfcSMilanka Ringwald } 30489425bfcSMilanka Ringwald 30589425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 30689425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?"); 30789425bfcSMilanka Ringwald } 30889425bfcSMilanka Ringwald 30989425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 31089425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP); 31189425bfcSMilanka Ringwald } 31289425bfcSMilanka Ringwald 31389425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 31489425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 31589425bfcSMilanka Ringwald } 31689425bfcSMilanka Ringwald 31789425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 31889425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 31989425bfcSMilanka Ringwald } 32089425bfcSMilanka Ringwald 32189425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 32289425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate); 32389425bfcSMilanka Ringwald } 32489425bfcSMilanka Ringwald 32589425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){ 32689425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate); 32789425bfcSMilanka Ringwald } 32889425bfcSMilanka Ringwald 32989425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 33089425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate); 33189425bfcSMilanka Ringwald } 33289425bfcSMilanka Ringwald 33389425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 33489425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 33589425bfcSMilanka Ringwald } 33689425bfcSMilanka Ringwald 33789425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 33889425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 33989425bfcSMilanka Ringwald } 34089425bfcSMilanka Ringwald 34189425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 34289425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 34389425bfcSMilanka Ringwald } 34489425bfcSMilanka Ringwald 34589425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 34689425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER); 34789425bfcSMilanka Ringwald } 34889425bfcSMilanka Ringwald 34989425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){ 35089425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL); 35189425bfcSMilanka Ringwald } 35289425bfcSMilanka Ringwald 353ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){ 35489425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1"); 355ce263fc8SMatthias Ringwald } 356ce263fc8SMatthias Ringwald 357667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){ 35889425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS); 359667ec068SMatthias Ringwald } 360667ec068SMatthias Ringwald 36113839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){ 3623deb3ec6SMatthias Ringwald if (!callback) return; 363a0ffb263SMatthias Ringwald uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1]; 3643deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3653deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3663deb3ec6SMatthias Ringwald event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 367a0ffb263SMatthias Ringwald event[3] = indicator.index; 368a0ffb263SMatthias Ringwald event[4] = indicator.status; 369a0ffb263SMatthias Ringwald strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE); 370a0ffb263SMatthias Ringwald event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0; 37113839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 3723deb3ec6SMatthias Ringwald } 3733deb3ec6SMatthias Ringwald 37413839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){ 3753deb3ec6SMatthias Ringwald if (!callback) return; 37689425bfcSMilanka Ringwald uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1]; 3773deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3783deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3793deb3ec6SMatthias Ringwald event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED; 380a0ffb263SMatthias Ringwald event[3] = network_operator.mode; 381a0ffb263SMatthias Ringwald event[4] = network_operator.format; 38289425bfcSMilanka Ringwald strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE); 38389425bfcSMilanka Ringwald event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0; 38413839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 3853deb3ec6SMatthias Ringwald } 3863deb3ec6SMatthias Ringwald 387a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 388a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 389a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 390aa4dd815SMatthias Ringwald int done = 1; 3913deb3ec6SMatthias Ringwald 392a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 3933deb3ec6SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 394d715cf51SMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr); 395a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 396a0ffb263SMatthias Ringwald hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 3973deb3ec6SMatthias Ringwald break; 3983deb3ec6SMatthias Ringwald case HFP_NOTIFY_ON_CODECS: 399a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 400a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 4013deb3ec6SMatthias Ringwald break; 4023deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: 403a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 404a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 4053deb3ec6SMatthias Ringwald break; 4063deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS_STATUS: 407a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 408a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 4093deb3ec6SMatthias Ringwald break; 4103deb3ec6SMatthias Ringwald case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 411a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 412a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 4133deb3ec6SMatthias Ringwald break; 4143deb3ec6SMatthias Ringwald case HFP_RETRIEVE_CAN_HOLD_CALL: 415a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 416a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 4173deb3ec6SMatthias Ringwald break; 4183deb3ec6SMatthias Ringwald case HFP_LIST_GENERIC_STATUS_INDICATORS: 419a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 420a0ffb263SMatthias Ringwald hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4213deb3ec6SMatthias Ringwald break; 4223deb3ec6SMatthias Ringwald case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 423a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 424a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4253deb3ec6SMatthias Ringwald break; 4263deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 427a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 428a0ffb263SMatthias Ringwald hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4293deb3ec6SMatthias Ringwald break; 4303deb3ec6SMatthias Ringwald default: 431aa4dd815SMatthias Ringwald done = 0; 4323deb3ec6SMatthias Ringwald break; 4333deb3ec6SMatthias Ringwald } 4343deb3ec6SMatthias Ringwald return done; 4353deb3ec6SMatthias Ringwald } 4363deb3ec6SMatthias Ringwald 437ce263fc8SMatthias Ringwald 438a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 439a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 440a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 441ce263fc8SMatthias Ringwald 442ce263fc8SMatthias Ringwald int done = 0; 443a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 444a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 445ce263fc8SMatthias Ringwald done = 1; 446a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 447ce263fc8SMatthias Ringwald return done; 448ce263fc8SMatthias Ringwald }; 449a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators){ 450a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 451ce263fc8SMatthias Ringwald done = 1; 452a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 453a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap, 454a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr); 455ce263fc8SMatthias Ringwald return done; 456ce263fc8SMatthias Ringwald } 457ce263fc8SMatthias Ringwald 458a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 459ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 460a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 461a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 462a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 463ce263fc8SMatthias Ringwald return 1; 464ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 465a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 466a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 467a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 468ce263fc8SMatthias Ringwald return 1; 469ce263fc8SMatthias Ringwald default: 470ce263fc8SMatthias Ringwald break; 471ce263fc8SMatthias Ringwald } 472ce263fc8SMatthias Ringwald 473a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 474a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 475ce263fc8SMatthias Ringwald done = 1; 476a0ffb263SMatthias Ringwald hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 477ce263fc8SMatthias Ringwald return done; 478ce263fc8SMatthias Ringwald } 479ce263fc8SMatthias Ringwald 480ce263fc8SMatthias Ringwald return done; 481ce263fc8SMatthias Ringwald } 482ce263fc8SMatthias Ringwald 483a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 484ce263fc8SMatthias Ringwald /* events ( == commands): 485ce263fc8SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 486ce263fc8SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 487ce263fc8SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC 488ce263fc8SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS 489ce263fc8SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 490ce263fc8SMatthias Ringwald */ 491ce263fc8SMatthias Ringwald 492a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 493ce263fc8SMatthias Ringwald 494a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 495ce263fc8SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 496a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0; 497ce263fc8SMatthias Ringwald 498a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 499a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 500a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 501ce263fc8SMatthias Ringwald return 1; 502ce263fc8SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 503a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 504a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 505a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 506ce263fc8SMatthias Ringwald 507a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 508a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 509a0ffb263SMatthias Ringwald hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 510ce263fc8SMatthias Ringwald break; 511ce263fc8SMatthias Ringwald 5126a7f44bdSMilanka Ringwald case HFP_CMD_AG_SUGGESTED_CODEC:{ 5136a7f44bdSMilanka Ringwald if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){ 514a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 515a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 516a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 5170e0be203SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 5180e0be203SMatthias Ringwald log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD"); 519fcb08cdbSMilanka Ringwald hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 520ce263fc8SMatthias Ringwald } else { 521a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 522a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 523a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 524a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 525a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 526a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 527ce263fc8SMatthias Ringwald 528ce263fc8SMatthias Ringwald } 529ce263fc8SMatthias Ringwald break; 5306a7f44bdSMilanka Ringwald } 531ce263fc8SMatthias Ringwald default: 532ce263fc8SMatthias Ringwald break; 533ce263fc8SMatthias Ringwald } 534ce263fc8SMatthias Ringwald return 0; 535ce263fc8SMatthias Ringwald } 536ce263fc8SMatthias Ringwald 537a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 538a0ffb263SMatthias Ringwald if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || 539a0ffb263SMatthias Ringwald hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0; 540ce263fc8SMatthias Ringwald 541ce263fc8SMatthias Ringwald 542a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){ 543a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 544a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 545a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 546ce263fc8SMatthias Ringwald return 1; 547ce263fc8SMatthias Ringwald } 548ce263fc8SMatthias Ringwald 549a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 550ce263fc8SMatthias Ringwald 551ce263fc8SMatthias Ringwald // run codecs exchange 552a0ffb263SMatthias Ringwald int done = codecs_exchange_state_machine(hfp_connection); 553ce263fc8SMatthias Ringwald if (done) return 1; 554ce263fc8SMatthias Ringwald 55538200c1dSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 55638200c1dSMilanka Ringwald if (hfp_connection->establish_audio_connection){ 55738200c1dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 55838200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 55938200c1dSMilanka Ringwald hfp_setup_synchronous_connection(hfp_connection); 56038200c1dSMilanka Ringwald return 1; 56138200c1dSMilanka Ringwald } 56238200c1dSMilanka Ringwald 563ce263fc8SMatthias Ringwald return 0; 564ce263fc8SMatthias Ringwald } 565ce263fc8SMatthias Ringwald 56638200c1dSMilanka Ringwald 567a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 568a0ffb263SMatthias Ringwald if (hfp_connection->hf_answer_incoming_call){ 569a0ffb263SMatthias Ringwald hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 570a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 0; 571ce263fc8SMatthias Ringwald return 1; 572ce263fc8SMatthias Ringwald } 573ce263fc8SMatthias Ringwald return 0; 574ce263fc8SMatthias Ringwald } 575ce263fc8SMatthias Ringwald 576a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){ 577a0ffb263SMatthias Ringwald if (!hfp_connection) return; 578724f400dSMatthias Ringwald if (!hfp_connection->rfcomm_cid) return; 5797522e673SMatthias Ringwald 58022387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) { 58122387625SMatthias Ringwald log_info("HFP HF%p, wrong role %u", hfp_connection, hfp_connection->local_role); 58222387625SMatthias Ringwald return; 58322387625SMatthias Ringwald } 58422387625SMatthias Ringwald 585b72c4a9eSMatthias Ringwald if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){ 586b72c4a9eSMatthias Ringwald 587b72c4a9eSMatthias Ringwald hfp_connection->hf_accept_sco = 0; 5887522e673SMatthias Ringwald 5897522e673SMatthias Ringwald // notify about codec selection if not done already 5907522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 5917522e673SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 5927522e673SMatthias Ringwald } 5937522e673SMatthias Ringwald 5947522e673SMatthias Ringwald // remote supported feature eSCO is set if link type is eSCO 5957522e673SMatthias Ringwald // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 5967522e673SMatthias Ringwald uint16_t max_latency; 5977522e673SMatthias Ringwald uint8_t retransmission_effort; 5987522e673SMatthias Ringwald uint16_t packet_types; 5997522e673SMatthias Ringwald 600d9f77559SMatthias Ringwald if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 6017522e673SMatthias Ringwald max_latency = 0x000c; 6027522e673SMatthias Ringwald retransmission_effort = 0x02; 6037522e673SMatthias Ringwald packet_types = 0x388; 6047522e673SMatthias Ringwald } else { 6057522e673SMatthias Ringwald max_latency = 0xffff; 6067522e673SMatthias Ringwald retransmission_effort = 0xff; 6077522e673SMatthias Ringwald packet_types = 0x003f; 6087522e673SMatthias Ringwald } 6097522e673SMatthias Ringwald 6107522e673SMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 6117522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 6127522e673SMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data 6137522e673SMatthias Ringwald } 6147522e673SMatthias Ringwald 615b72c4a9eSMatthias Ringwald log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting); 6167522e673SMatthias Ringwald hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 6177522e673SMatthias Ringwald sco_voice_setting, retransmission_effort, packet_types); 6187522e673SMatthias Ringwald return; 6197522e673SMatthias Ringwald } 6207522e673SMatthias Ringwald 621d4dd47ffSMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 622d4dd47ffSMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 623d4dd47ffSMatthias Ringwald return; 624d4dd47ffSMatthias Ringwald } 625a0ffb263SMatthias Ringwald int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 626ce263fc8SMatthias Ringwald if (!done){ 627a0ffb263SMatthias Ringwald done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 628ce263fc8SMatthias Ringwald } 629ce263fc8SMatthias Ringwald if (!done){ 630a0ffb263SMatthias Ringwald done = hfp_hf_run_for_audio_connection(hfp_connection); 631ce263fc8SMatthias Ringwald } 632ce263fc8SMatthias Ringwald if (!done){ 633a0ffb263SMatthias Ringwald done = call_setup_state_machine(hfp_connection); 634ce263fc8SMatthias Ringwald } 635ce263fc8SMatthias Ringwald 6361016a228SMatthias Ringwald // don't send a new command while ok still pending 6371016a228SMatthias Ringwald if (hfp_connection->ok_pending) return; 6381016a228SMatthias Ringwald 639a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 640a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 641a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 642a0ffb263SMatthias Ringwald hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 643ce263fc8SMatthias Ringwald return; 644ce263fc8SMatthias Ringwald } 645ce263fc8SMatthias Ringwald 646a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 647a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 648a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 649a0ffb263SMatthias Ringwald hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 650ce263fc8SMatthias Ringwald return; 651ce263fc8SMatthias Ringwald } 652ce263fc8SMatthias Ringwald 653a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_calling_line_notification){ 654a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 0; 655a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 656a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 657ce263fc8SMatthias Ringwald return; 658ce263fc8SMatthias Ringwald } 659ce263fc8SMatthias Ringwald 660a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_calling_line_notification){ 661a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 0; 662a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 663a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 664ce263fc8SMatthias Ringwald return; 665ce263fc8SMatthias Ringwald } 666ce263fc8SMatthias Ringwald 667a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 668a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 669a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 670a0ffb263SMatthias Ringwald hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0); 671ce263fc8SMatthias Ringwald return; 672ce263fc8SMatthias Ringwald } 673ce263fc8SMatthias Ringwald 674a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){ 675a0ffb263SMatthias Ringwald hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0; 676a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 677a0ffb263SMatthias Ringwald hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1); 678ce263fc8SMatthias Ringwald return; 679ce263fc8SMatthias Ringwald } 680ce263fc8SMatthias Ringwald 681a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_voice_recognition_notification){ 682a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_voice_recognition_notification = 0; 683a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 684a0ffb263SMatthias Ringwald hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 685ce263fc8SMatthias Ringwald return; 686ce263fc8SMatthias Ringwald } 687ce263fc8SMatthias Ringwald 688a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_voice_recognition_notification){ 689a0ffb263SMatthias Ringwald hfp_connection->hf_activate_voice_recognition_notification = 0; 690a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 691a0ffb263SMatthias Ringwald hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 692ce263fc8SMatthias Ringwald return; 693ce263fc8SMatthias Ringwald } 694ce263fc8SMatthias Ringwald 695ce263fc8SMatthias Ringwald 696a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_call_waiting_notification){ 697a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 0; 698a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 699a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 700ce263fc8SMatthias Ringwald return; 701ce263fc8SMatthias Ringwald } 702ce263fc8SMatthias Ringwald 703a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_call_waiting_notification){ 704a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 0; 705a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 706a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 707ce263fc8SMatthias Ringwald return; 708ce263fc8SMatthias Ringwald } 709ce263fc8SMatthias Ringwald 710a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_outgoing_call){ 711a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 0; 712a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 713a0ffb263SMatthias Ringwald hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 714ce263fc8SMatthias Ringwald return; 715ce263fc8SMatthias Ringwald } 716ce263fc8SMatthias Ringwald 717a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_memory_dialing){ 718a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 0; 719a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 720a0ffb263SMatthias Ringwald hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 721ce263fc8SMatthias Ringwald return; 722ce263fc8SMatthias Ringwald } 723ce263fc8SMatthias Ringwald 724a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_redial_last_number){ 725a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 0; 726a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 727a0ffb263SMatthias Ringwald hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 728ce263fc8SMatthias Ringwald return; 729ce263fc8SMatthias Ringwald } 730ce263fc8SMatthias Ringwald 731a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chup){ 732a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 0; 733a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 734a0ffb263SMatthias Ringwald hfp_hf_send_chup(hfp_connection->rfcomm_cid); 735ce263fc8SMatthias Ringwald return; 736ce263fc8SMatthias Ringwald } 737ce263fc8SMatthias Ringwald 738a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_0){ 739a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 0; 740a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 741a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 742ce263fc8SMatthias Ringwald return; 743ce263fc8SMatthias Ringwald } 744ce263fc8SMatthias Ringwald 745a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_1){ 746a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 0; 747a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 748a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 749ce263fc8SMatthias Ringwald return; 750ce263fc8SMatthias Ringwald } 751ce263fc8SMatthias Ringwald 752a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_2){ 753a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 0; 754a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 755a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 756ce263fc8SMatthias Ringwald return; 757ce263fc8SMatthias Ringwald } 758ce263fc8SMatthias Ringwald 759a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_3){ 760a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 0; 761a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 762a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 763ce263fc8SMatthias Ringwald return; 764ce263fc8SMatthias Ringwald } 765ce263fc8SMatthias Ringwald 766a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_4){ 767a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 0; 768a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 769a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 770ce263fc8SMatthias Ringwald return; 771ce263fc8SMatthias Ringwald } 772ce263fc8SMatthias Ringwald 773a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_x){ 774a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 0; 775a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 776a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 777667ec068SMatthias Ringwald return; 778667ec068SMatthias Ringwald } 779667ec068SMatthias Ringwald 780a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_dtmf_code){ 781a0ffb263SMatthias Ringwald char code = hfp_connection->hf_send_dtmf_code; 782a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = 0; 783a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 784a0ffb263SMatthias Ringwald hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 785ce263fc8SMatthias Ringwald return; 786ce263fc8SMatthias Ringwald } 787ce263fc8SMatthias Ringwald 788a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_binp){ 789a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 0; 790a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 791a0ffb263SMatthias Ringwald hfp_hf_send_binp(hfp_connection->rfcomm_cid); 792ce263fc8SMatthias Ringwald return; 793ce263fc8SMatthias Ringwald } 794ce263fc8SMatthias Ringwald 795a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_clcc){ 796a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 0; 797a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 798a0ffb263SMatthias Ringwald hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 799667ec068SMatthias Ringwald return; 800667ec068SMatthias Ringwald } 801667ec068SMatthias Ringwald 802a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_rrh){ 803a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 0; 804667ec068SMatthias Ringwald char buffer[20]; 805a0ffb263SMatthias Ringwald switch (hfp_connection->hf_send_rrh_command){ 806667ec068SMatthias Ringwald case '?': 807667ec068SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD); 808a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 809667ec068SMatthias Ringwald return; 810667ec068SMatthias Ringwald case '0': 811667ec068SMatthias Ringwald case '1': 812667ec068SMatthias Ringwald case '2': 813a0ffb263SMatthias Ringwald sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command); 814a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 815667ec068SMatthias Ringwald return; 816667ec068SMatthias Ringwald default: 817667ec068SMatthias Ringwald break; 818667ec068SMatthias Ringwald } 819667ec068SMatthias Ringwald return; 820667ec068SMatthias Ringwald } 821667ec068SMatthias Ringwald 822a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_cnum){ 823a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 0; 824667ec068SMatthias Ringwald char buffer[20]; 825667ec068SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION); 826a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 827667ec068SMatthias Ringwald return; 828667ec068SMatthias Ringwald } 829667ec068SMatthias Ringwald 830667ec068SMatthias Ringwald // update HF indicators 831a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_update_bitmap){ 832667ec068SMatthias Ringwald int i; 833667ec068SMatthias Ringwald for (i=0;i<hfp_indicators_nr;i++){ 834a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 835a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].state){ 836a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 837a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 838667ec068SMatthias Ringwald char buffer[30]; 839ecb7d461SMatthias Ringwald sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]); 840a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 841667ec068SMatthias Ringwald } else { 84260ebb071SMilanka Ringwald log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]); 843667ec068SMatthias Ringwald } 844667ec068SMatthias Ringwald return; 845667ec068SMatthias Ringwald } 846667ec068SMatthias Ringwald } 847667ec068SMatthias Ringwald } 848667ec068SMatthias Ringwald 849ce263fc8SMatthias Ringwald if (done) return; 850ce263fc8SMatthias Ringwald // deal with disconnect 851a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 852ce263fc8SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 853a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 854a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 855ce263fc8SMatthias Ringwald break; 856ce263fc8SMatthias Ringwald 857ce263fc8SMatthias Ringwald default: 858ce263fc8SMatthias Ringwald break; 859ce263fc8SMatthias Ringwald } 860ce263fc8SMatthias Ringwald } 861ce263fc8SMatthias Ringwald 862a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 863a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 8646a7f44bdSMilanka Ringwald 865ca59be51SMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 8667522e673SMatthias Ringwald 867667ec068SMatthias Ringwald // restore volume settings 868a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = hfp_hf_speaker_gain; 869a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 870ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 871a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = hfp_hf_microphone_gain; 872a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 873ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 874667ec068SMatthias Ringwald // enable all indicators 875667ec068SMatthias Ringwald int i; 876667ec068SMatthias Ringwald for (i=0;i<hfp_indicators_nr;i++){ 877a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i]; 878a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].state = 1; 879667ec068SMatthias Ringwald } 880ce263fc8SMatthias Ringwald } 881ce263fc8SMatthias Ringwald 882a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){ 883a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 884a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 8853deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 886a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 887a0ffb263SMatthias Ringwald hfp_connection->state = HFP_NOTIFY_ON_CODECS; 8883deb3ec6SMatthias Ringwald break; 8893deb3ec6SMatthias Ringwald } 890a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 8913deb3ec6SMatthias Ringwald break; 8923deb3ec6SMatthias Ringwald 8933deb3ec6SMatthias Ringwald case HFP_W4_NOTIFY_ON_CODECS: 894a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 8953deb3ec6SMatthias Ringwald break; 8963deb3ec6SMatthias Ringwald 8973deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 898a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 8993deb3ec6SMatthias Ringwald break; 9003deb3ec6SMatthias Ringwald 9013deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 902a0ffb263SMatthias Ringwald hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 9033deb3ec6SMatthias Ringwald break; 9043deb3ec6SMatthias Ringwald 9053deb3ec6SMatthias Ringwald case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 906a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 907a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 9083deb3ec6SMatthias Ringwald break; 9093deb3ec6SMatthias Ringwald } 910a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 911a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 9123deb3ec6SMatthias Ringwald break; 9133deb3ec6SMatthias Ringwald } 914a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9153deb3ec6SMatthias Ringwald break; 9163deb3ec6SMatthias Ringwald 9173deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 918a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 919a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 9203deb3ec6SMatthias Ringwald break; 9213deb3ec6SMatthias Ringwald } 922a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9233deb3ec6SMatthias Ringwald break; 9243deb3ec6SMatthias Ringwald 9253deb3ec6SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 926a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 9273deb3ec6SMatthias Ringwald break; 9283deb3ec6SMatthias Ringwald 9293deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 930a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 9313deb3ec6SMatthias Ringwald break; 9323deb3ec6SMatthias Ringwald 9333deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 934a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9353deb3ec6SMatthias Ringwald break; 936ce263fc8SMatthias Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 937a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 938a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 939ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 940ce263fc8SMatthias Ringwald break; 941ce263fc8SMatthias Ringwald } 9423deb3ec6SMatthias Ringwald 943a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 944a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 945ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 946ce263fc8SMatthias Ringwald break; 9473deb3ec6SMatthias Ringwald } 9483deb3ec6SMatthias Ringwald 949a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 950ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 95160ebb071SMilanka Ringwald // printf("Format set, querying name\n"); 952a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 953ce263fc8SMatthias Ringwald break; 954ce263fc8SMatthias Ringwald case HPF_HF_QUERY_OPERATOR_W4_RESULT: 955a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 956ca59be51SMatthias Ringwald hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator); 957ce263fc8SMatthias Ringwald break; 958ce263fc8SMatthias Ringwald default: 959ce263fc8SMatthias Ringwald break; 9603deb3ec6SMatthias Ringwald } 961ce263fc8SMatthias Ringwald 962a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 963a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 964ce263fc8SMatthias Ringwald break; 9653deb3ec6SMatthias Ringwald } 9663deb3ec6SMatthias Ringwald 967a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 968aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 969a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 9703deb3ec6SMatthias Ringwald break; 971ce263fc8SMatthias Ringwald case HFP_CODECS_HF_CONFIRMED_CODEC: 972a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 973ce263fc8SMatthias Ringwald break; 9743deb3ec6SMatthias Ringwald default: 9753deb3ec6SMatthias Ringwald break; 9763deb3ec6SMatthias Ringwald } 9773deb3ec6SMatthias Ringwald break; 9783deb3ec6SMatthias Ringwald default: 9793deb3ec6SMatthias Ringwald break; 9803deb3ec6SMatthias Ringwald } 9813deb3ec6SMatthias Ringwald 9823deb3ec6SMatthias Ringwald // done 983a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 9843deb3ec6SMatthias Ringwald } 9853deb3ec6SMatthias Ringwald 986*e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 987*e0d09929SMatthias Ringwald return byte == '\n' || byte == '\r'; 988*e0d09929SMatthias Ringwald } 989*e0d09929SMatthias Ringwald 990e9c22d4eSMatthias Ringwald static void hfp_hf_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 9918a46ec40SMatthias Ringwald UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 9928a46ec40SMatthias Ringwald 9938a46ec40SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 9948a46ec40SMatthias 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 999186dd3d2SMatthias Ringwald hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 10001e35c04dSMatthias Ringwald 10018a46ec40SMatthias Ringwald // process messages byte-wise 1002186dd3d2SMatthias Ringwald int pos; 10033deb3ec6SMatthias Ringwald for (pos = 0; pos < size ; pos++){ 1004a0ffb263SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 1); 10053deb3ec6SMatthias Ringwald 1006*e0d09929SMatthias Ringwald // parse until end of line 1007*e0d09929SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1008*e0d09929SMatthias Ringwald 1009186dd3d2SMatthias Ringwald int value; 1010186dd3d2SMatthias Ringwald int i; 1011a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1012667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1013a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1014ca59be51SMatthias Ringwald hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number); 1015667ec068SMatthias Ringwald break; 1016667ec068SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1017a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1018ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1019667ec068SMatthias Ringwald break; 1020667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1021a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1022ca59be51SMatthias Ringwald hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection->clcc_idx, 1023a0ffb263SMatthias Ringwald hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty, 1024a0ffb263SMatthias Ringwald hfp_connection->bnip_type, hfp_connection->bnip_number); 1025667ec068SMatthias Ringwald break; 1026ce263fc8SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1027a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10282308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1029667ec068SMatthias Ringwald hfp_hf_speaker_gain = value; 1030ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1031ce263fc8SMatthias Ringwald break; 1032ce263fc8SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1033a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10342308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1035667ec068SMatthias Ringwald hfp_hf_microphone_gain = value; 1036ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1037ce263fc8SMatthias Ringwald break; 1038ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1039a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1040ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1041a0ffb263SMatthias Ringwald break; 1042a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1043a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1044ca59be51SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1045a0ffb263SMatthias Ringwald break; 1046a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1047a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1048ca59be51SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1049ce263fc8SMatthias Ringwald break; 1050ce263fc8SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1051a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1052a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1053a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1054ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1055ce263fc8SMatthias Ringwald break; 1056ce263fc8SMatthias Ringwald case HFP_CMD_ERROR: 1057a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1058a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 1059a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1060ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1061ce263fc8SMatthias Ringwald break; 1062ce263fc8SMatthias Ringwald case HFP_CMD_OK: 1063a0ffb263SMatthias Ringwald hfp_hf_switch_on_ok(hfp_connection); 1064ce263fc8SMatthias Ringwald break; 1065ce263fc8SMatthias Ringwald case HFP_CMD_RING: 1066ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1067ce263fc8SMatthias Ringwald break; 1068ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 1069a0ffb263SMatthias Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1070a0ffb263SMatthias Ringwald if (hfp_connection->ag_indicators[i].status_changed) { 1071a0ffb263SMatthias Ringwald if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 1072a0ffb263SMatthias Ringwald hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 1073a0ffb263SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 1074a0ffb263SMatthias Ringwald hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 1075d5ff3b26SMatthias Ringwald // avoid set but not used warning 1076d5ff3b26SMatthias Ringwald (void) hfp_callheld_status; 1077a0ffb263SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 1078a0ffb263SMatthias Ringwald hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 1079ce263fc8SMatthias Ringwald } 1080a0ffb263SMatthias Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 1081ca59be51SMatthias Ringwald hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]); 10823deb3ec6SMatthias Ringwald break; 10833deb3ec6SMatthias Ringwald } 10843deb3ec6SMatthias Ringwald } 1085ce263fc8SMatthias Ringwald break; 1086ce263fc8SMatthias Ringwald default: 1087ce263fc8SMatthias Ringwald break; 10883deb3ec6SMatthias Ringwald } 10890cef86faSMatthias Ringwald } 1090a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 10913deb3ec6SMatthias Ringwald } 10923deb3ec6SMatthias Ringwald 10930cb5b971SMatthias Ringwald static void hfp_run(void){ 1094665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1095665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1096665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1097a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 109822387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1099a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11003deb3ec6SMatthias Ringwald } 11013deb3ec6SMatthias Ringwald } 11023deb3ec6SMatthias Ringwald 1103e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 11043deb3ec6SMatthias Ringwald switch (packet_type){ 11053deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 1106e9c22d4eSMatthias Ringwald hfp_hf_handle_rfcomm_event(packet_type, channel, packet, size); 11073deb3ec6SMatthias Ringwald break; 11083deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1109d4dd47ffSMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1110d4dd47ffSMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 1111d4dd47ffSMatthias Ringwald hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1112d4dd47ffSMatthias Ringwald return; 1113d4dd47ffSMatthias Ringwald } 111427950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1115202c8a4cSMatthias Ringwald break; 11163deb3ec6SMatthias Ringwald default: 11173deb3ec6SMatthias Ringwald break; 11183deb3ec6SMatthias Ringwald } 11193deb3ec6SMatthias Ringwald hfp_run(); 11203deb3ec6SMatthias Ringwald } 11213deb3ec6SMatthias Ringwald 1122405014fbSMatthias Ringwald static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1123405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 112438200c1dSMilanka Ringwald 112538200c1dSMilanka Ringwald // allow for sco established -> ring transition and sco retry 112638200c1dSMilanka Ringwald if (packet_type != HCI_EVENT_PACKET) return; 112738200c1dSMilanka Ringwald if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return; 112838200c1dSMilanka Ringwald hfp_run(); 1129405014fbSMatthias Ringwald } 1130405014fbSMatthias Ringwald 1131a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1132520c92d5SMatthias Ringwald hfp_init(); 1133d63c37a1SMatthias Ringwald 1134405014fbSMatthias Ringwald hci_event_callback_registration.callback = &hci_event_packet_handler; 113527950165SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 113627950165SMatthias Ringwald 1137e9c22d4eSMatthias Ringwald rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 113827950165SMatthias Ringwald 113927950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 1140e9c22d4eSMatthias Ringwald hfp_set_hf_rfcomm_packet_handler(&rfcomm_packet_handler); 114127950165SMatthias Ringwald 11421b005648SMatthias Ringwald hfp_set_hf_run_for_context(hfp_run_for_context); 1143d68dcce1SMatthias Ringwald 1144a0ffb263SMatthias Ringwald hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1145a0ffb263SMatthias Ringwald hfp_codecs_nr = 0; 1146a0ffb263SMatthias Ringwald hfp_indicators_nr = 0; 1147a0ffb263SMatthias Ringwald hfp_hf_speaker_gain = 9; 1148a0ffb263SMatthias Ringwald hfp_hf_microphone_gain = 9; 1149a0ffb263SMatthias Ringwald } 1150a0ffb263SMatthias Ringwald 1151a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){ 11523deb3ec6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS){ 1153a0ffb263SMatthias Ringwald log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 11543deb3ec6SMatthias Ringwald return; 11553deb3ec6SMatthias Ringwald } 11563deb3ec6SMatthias Ringwald 11573deb3ec6SMatthias Ringwald hfp_codecs_nr = codecs_nr; 11583deb3ec6SMatthias Ringwald int i; 11593deb3ec6SMatthias Ringwald for (i=0; i<codecs_nr; i++){ 11603deb3ec6SMatthias Ringwald hfp_codecs[i] = codecs[i]; 11613deb3ec6SMatthias Ringwald } 11623deb3ec6SMatthias Ringwald } 11633deb3ec6SMatthias Ringwald 1164a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){ 11653deb3ec6SMatthias Ringwald hfp_supported_features = supported_features; 1166a0ffb263SMatthias Ringwald } 11673deb3ec6SMatthias Ringwald 1168a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){ 11693deb3ec6SMatthias Ringwald hfp_indicators_nr = indicators_nr; 11703deb3ec6SMatthias Ringwald int i; 1171a0ffb263SMatthias Ringwald for (i = 0; i < hfp_indicators_nr ; i++){ 11723deb3ec6SMatthias Ringwald hfp_indicators[i] = indicators[i]; 11733deb3ec6SMatthias Ringwald } 11743deb3ec6SMatthias Ringwald } 11753deb3ec6SMatthias Ringwald 11763deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1177323d3000SMatthias Ringwald hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 11783deb3ec6SMatthias Ringwald } 11793deb3ec6SMatthias Ringwald 1180c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 11819c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1182a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1183a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1184a33eb0c4SMilanka Ringwald return; 1185a33eb0c4SMilanka Ringwald } 1186a0ffb263SMatthias Ringwald hfp_release_service_level_connection(hfp_connection); 1187a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11883deb3ec6SMatthias Ringwald } 11893deb3ec6SMatthias Ringwald 1190c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 11919c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1192a0ffb263SMatthias Ringwald if (!hfp_connection) { 1193a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 11943deb3ec6SMatthias Ringwald return; 11953deb3ec6SMatthias Ringwald } 1196a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = enable; 1197a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11983deb3ec6SMatthias Ringwald } 11993deb3ec6SMatthias Ringwald 1200c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1201c8626498SMilanka Ringwald hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1202ce263fc8SMatthias Ringwald } 1203ce263fc8SMatthias Ringwald 1204c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1205c8626498SMilanka Ringwald hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1206ce263fc8SMatthias Ringwald } 1207ce263fc8SMatthias Ringwald 12083deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format 1209c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 12109c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1211a0ffb263SMatthias Ringwald if (!hfp_connection) { 1212a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12133deb3ec6SMatthias Ringwald return; 12143deb3ec6SMatthias Ringwald } 1215a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1216a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1217a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12183deb3ec6SMatthias Ringwald } 12193deb3ec6SMatthias Ringwald 1220c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 12219c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1222a0ffb263SMatthias Ringwald if (!hfp_connection) { 1223a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12243deb3ec6SMatthias Ringwald return; 12253deb3ec6SMatthias Ringwald } 1226a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1227ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1228a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1229ce263fc8SMatthias Ringwald break; 1230ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1231a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1232ce263fc8SMatthias Ringwald break; 1233ce263fc8SMatthias Ringwald default: 1234ce263fc8SMatthias Ringwald break; 1235ce263fc8SMatthias Ringwald } 1236a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12373deb3ec6SMatthias Ringwald } 12383deb3ec6SMatthias Ringwald 1239c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 12409c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1241a0ffb263SMatthias Ringwald if (!hfp_connection) { 1242a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12433deb3ec6SMatthias Ringwald return; 12443deb3ec6SMatthias Ringwald } 1245a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = enable; 1246a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12473deb3ec6SMatthias Ringwald } 12483deb3ec6SMatthias Ringwald 1249ce263fc8SMatthias Ringwald 1250c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1251c8626498SMilanka Ringwald hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1252ce263fc8SMatthias Ringwald } 1253ce263fc8SMatthias Ringwald 1254c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1255c8626498SMilanka Ringwald hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1256ce263fc8SMatthias Ringwald } 1257ce263fc8SMatthias Ringwald 125838200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 125938200c1dSMilanka Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_supported_features & (1<<HFP_HFSF_ESCO_S4)); 126038200c1dSMilanka Ringwald } 1261ce263fc8SMatthias Ringwald 1262c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 12639c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1264a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1265a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1266a33eb0c4SMilanka Ringwald return; 1267a33eb0c4SMilanka Ringwald } 1268a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 1269ce263fc8SMatthias Ringwald 1270a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1271a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 12723deb3ec6SMatthias Ringwald 127338200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 0; 127438200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 1; 1275a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 1276ce263fc8SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); 1277a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 127838200c1dSMilanka Ringwald hfp_connection->suggested_codec = HFP_CODEC_CVSD; 127938200c1dSMilanka Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 128038200c1dSMilanka Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 128138200c1dSMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 128238200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 0; 128338200c1dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1284ce263fc8SMatthias Ringwald } else { 1285a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 1286aa4dd815SMatthias Ringwald case HFP_CODECS_W4_AG_COMMON_CODEC: 1287aa4dd815SMatthias Ringwald break; 1288aa4dd815SMatthias Ringwald default: 128938200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 1290a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 1291aa4dd815SMatthias Ringwald break; 12923deb3ec6SMatthias Ringwald } 1293ce263fc8SMatthias Ringwald } 1294ce263fc8SMatthias Ringwald 1295a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12963deb3ec6SMatthias Ringwald } 12973deb3ec6SMatthias Ringwald 1298c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 12999c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1300a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1301a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1302a33eb0c4SMilanka Ringwald return; 1303a33eb0c4SMilanka Ringwald } 1304a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 1305a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 13063deb3ec6SMatthias Ringwald } 13073deb3ec6SMatthias Ringwald 1308c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 13099c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1310a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1311a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1312a33eb0c4SMilanka Ringwald return; 1313a33eb0c4SMilanka Ringwald } 1314ce263fc8SMatthias Ringwald 1315ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1316a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 1; 1317a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1318ce263fc8SMatthias Ringwald } else { 1319ce263fc8SMatthias Ringwald log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1320ce263fc8SMatthias Ringwald } 1321ce263fc8SMatthias Ringwald } 1322ce263fc8SMatthias Ringwald 1323c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 13249c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1325a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1326a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1327a33eb0c4SMilanka Ringwald return; 1328a33eb0c4SMilanka Ringwald } 1329a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 1330a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1331ce263fc8SMatthias Ringwald } 1332ce263fc8SMatthias Ringwald 1333c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 13349c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_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){ 1341a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 1342a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1343ce263fc8SMatthias Ringwald } 1344ce263fc8SMatthias Ringwald } 1345ce263fc8SMatthias Ringwald 1346c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){ 13479c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1348a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1349a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1350a33eb0c4SMilanka Ringwald return; 1351a33eb0c4SMilanka Ringwald } 1352ce263fc8SMatthias Ringwald 1353ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1354a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 1; 1355a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1356ce263fc8SMatthias Ringwald } 1357ce263fc8SMatthias Ringwald } 1358ce263fc8SMatthias Ringwald 1359c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 13609c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1361a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1362a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1363a33eb0c4SMilanka Ringwald return; 1364a33eb0c4SMilanka Ringwald } 1365ce263fc8SMatthias Ringwald 1366ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1367ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1368a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 1; 1369a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1370ce263fc8SMatthias Ringwald } 1371ce263fc8SMatthias Ringwald } 1372ce263fc8SMatthias Ringwald 1373c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 13749c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1375a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1376a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1377a33eb0c4SMilanka Ringwald return; 1378a33eb0c4SMilanka Ringwald } 1379ce263fc8SMatthias Ringwald 1380ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1381ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1382a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 1; 1383a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1384ce263fc8SMatthias Ringwald } 1385ce263fc8SMatthias Ringwald } 1386ce263fc8SMatthias Ringwald 1387c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 13889c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1389a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1390a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1391a33eb0c4SMilanka Ringwald return; 1392a33eb0c4SMilanka Ringwald } 1393ce263fc8SMatthias Ringwald 1394ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1395ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1396a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 1; 1397a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1398ce263fc8SMatthias Ringwald } 1399ce263fc8SMatthias Ringwald } 1400ce263fc8SMatthias Ringwald 1401c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 14029c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1403a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1404a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1405a33eb0c4SMilanka Ringwald return; 1406a33eb0c4SMilanka Ringwald } 1407ce263fc8SMatthias Ringwald 1408ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1409ce263fc8SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1410a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 1; 1411a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1412ce263fc8SMatthias Ringwald } 1413ce263fc8SMatthias Ringwald } 1414ce263fc8SMatthias Ringwald 1415c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 14169c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1417a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1418a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1419a33eb0c4SMilanka Ringwald return; 1420a33eb0c4SMilanka Ringwald } 1421667ec068SMatthias Ringwald 1422667ec068SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1423667ec068SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1424a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1425a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 10 + index; 1426a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1427667ec068SMatthias Ringwald } 1428667ec068SMatthias Ringwald } 1429667ec068SMatthias Ringwald 1430c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 14319c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1432a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1433a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1434a33eb0c4SMilanka Ringwald return; 1435a33eb0c4SMilanka Ringwald } 1436667ec068SMatthias Ringwald 1437667ec068SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS || 1438667ec068SMatthias Ringwald hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){ 1439a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1440a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 20 + index; 1441a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1442667ec068SMatthias Ringwald } 1443667ec068SMatthias Ringwald } 1444ce263fc8SMatthias Ringwald 1445c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 14469c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1447a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1448a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1449a33eb0c4SMilanka Ringwald return; 1450a33eb0c4SMilanka Ringwald } 1451ce263fc8SMatthias Ringwald 1452a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 1; 1453ce263fc8SMatthias Ringwald snprintf(phone_number, sizeof(phone_number), "%s", number); 1454a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1455ce263fc8SMatthias Ringwald } 1456ce263fc8SMatthias Ringwald 1457c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 14589c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1459a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1460a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1461a33eb0c4SMilanka Ringwald return; 1462a33eb0c4SMilanka Ringwald } 1463ce263fc8SMatthias Ringwald 1464a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 1; 1465a0ffb263SMatthias Ringwald hfp_connection->memory_id = memory_id; 1466a0ffb263SMatthias Ringwald 1467a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1468ce263fc8SMatthias Ringwald } 1469ce263fc8SMatthias Ringwald 1470c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 14719c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1472a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1473a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1474a33eb0c4SMilanka Ringwald return; 1475a33eb0c4SMilanka Ringwald } 1476ce263fc8SMatthias Ringwald 1477a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 1; 1478a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1479ce263fc8SMatthias Ringwald } 1480ce263fc8SMatthias Ringwald 1481c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 14829c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1483a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1484a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1485a33eb0c4SMilanka Ringwald return; 1486a33eb0c4SMilanka Ringwald } 1487ce263fc8SMatthias Ringwald 1488a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 1; 1489a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1490ce263fc8SMatthias Ringwald } 1491ce263fc8SMatthias Ringwald 1492ce263fc8SMatthias Ringwald 1493c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 14949c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1495a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1496a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1497a33eb0c4SMilanka Ringwald return; 1498a33eb0c4SMilanka Ringwald } 1499ce263fc8SMatthias Ringwald 1500a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 1; 1501a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1502ce263fc8SMatthias Ringwald } 1503ce263fc8SMatthias Ringwald 1504ce263fc8SMatthias Ringwald 1505c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 15069c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1507a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1508a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1509a33eb0c4SMilanka Ringwald return; 1510a33eb0c4SMilanka Ringwald } 1511ce263fc8SMatthias Ringwald 1512a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 1; 1513a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1514ce263fc8SMatthias Ringwald } 1515ce263fc8SMatthias Ringwald 1516c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 15179c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1518a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1519a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1520a33eb0c4SMilanka Ringwald return; 1521a33eb0c4SMilanka Ringwald } 1522ce263fc8SMatthias Ringwald 1523a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 1; 1524a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1525ce263fc8SMatthias Ringwald } 1526ce263fc8SMatthias Ringwald 1527ce263fc8SMatthias Ringwald 1528c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 15299c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1530a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1531a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1532a33eb0c4SMilanka Ringwald return; 1533a33eb0c4SMilanka Ringwald } 1534ce263fc8SMatthias Ringwald 1535a0ffb263SMatthias Ringwald hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1; 1536a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1537ce263fc8SMatthias Ringwald } 1538ce263fc8SMatthias Ringwald 1539c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 15409c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1541a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1542a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1543a33eb0c4SMilanka Ringwald return; 1544a33eb0c4SMilanka Ringwald } 1545ce263fc8SMatthias Ringwald 1546a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1547a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1548ce263fc8SMatthias Ringwald } 1549ce263fc8SMatthias Ringwald 1550c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){ 15519c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1552a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1553a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1554a33eb0c4SMilanka Ringwald return; 1555a33eb0c4SMilanka Ringwald } 1556ce263fc8SMatthias Ringwald 1557a0ffb263SMatthias Ringwald hfp_connection->hf_activate_voice_recognition_notification = 1; 1558a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1559ce263fc8SMatthias Ringwald } 1560ce263fc8SMatthias Ringwald 1561c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){ 15629c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1563a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1564a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1565a33eb0c4SMilanka Ringwald return; 1566a33eb0c4SMilanka Ringwald } 1567ce263fc8SMatthias Ringwald 1568a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_voice_recognition_notification = 1; 1569a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1570ce263fc8SMatthias Ringwald } 1571ce263fc8SMatthias Ringwald 1572c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 15739c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1574a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1575a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1576a33eb0c4SMilanka Ringwald return; 1577a33eb0c4SMilanka Ringwald } 1578c8626498SMilanka Ringwald 1579a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain == gain) return; 1580a0ffb263SMatthias Ringwald if (gain < 0 || gain > 15){ 1581a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1582a0ffb263SMatthias Ringwald return; 1583a0ffb263SMatthias Ringwald } 1584a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 1585a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 1586a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1587ce263fc8SMatthias Ringwald } 1588ce263fc8SMatthias Ringwald 1589c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 15909c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1591a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1592a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1593a33eb0c4SMilanka Ringwald return; 1594a33eb0c4SMilanka Ringwald } 1595c8626498SMilanka Ringwald 1596a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain == gain) return; 1597a0ffb263SMatthias Ringwald if (gain < 0 || gain > 15){ 1598a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1599a0ffb263SMatthias Ringwald return; 1600a0ffb263SMatthias Ringwald } 1601a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 1602a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 1603a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1604ce263fc8SMatthias Ringwald } 1605ce263fc8SMatthias Ringwald 1606c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 16079c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1608a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1609a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1610a33eb0c4SMilanka Ringwald return; 1611a33eb0c4SMilanka Ringwald } 1612a33eb0c4SMilanka Ringwald 1613a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = code; 1614a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1615ce263fc8SMatthias Ringwald } 1616ce263fc8SMatthias Ringwald 1617c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 16189c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1619a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1620a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1621a33eb0c4SMilanka Ringwald return; 1622a33eb0c4SMilanka Ringwald } 1623a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 1; 1624a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1625ce263fc8SMatthias Ringwald } 16263deb3ec6SMatthias Ringwald 1627c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 16289c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1629a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1630a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1631a33eb0c4SMilanka Ringwald return; 1632a33eb0c4SMilanka Ringwald } 1633a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 1; 1634a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1635667ec068SMatthias Ringwald } 1636667ec068SMatthias Ringwald 1637667ec068SMatthias Ringwald 1638c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 16399c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1640a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1641a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1642a33eb0c4SMilanka Ringwald return; 1643a33eb0c4SMilanka Ringwald } 1644a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1645a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '?'; 1646a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1647667ec068SMatthias Ringwald } 1648667ec068SMatthias Ringwald 1649c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 16509c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1651a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1652a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1653a33eb0c4SMilanka Ringwald return; 1654a33eb0c4SMilanka Ringwald } 1655a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1656a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '0'; 1657a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1658667ec068SMatthias Ringwald } 1659667ec068SMatthias Ringwald 1660c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 16619c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1662a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1663a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1664a33eb0c4SMilanka Ringwald return; 1665a33eb0c4SMilanka Ringwald } 1666a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1667a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '1'; 1668a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1669667ec068SMatthias Ringwald } 1670667ec068SMatthias Ringwald 1671c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 16729c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1673a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1674a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1675a33eb0c4SMilanka Ringwald return; 1676a33eb0c4SMilanka Ringwald } 1677a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1678a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '2'; 1679a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1680667ec068SMatthias Ringwald } 1681667ec068SMatthias Ringwald 1682c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 16839c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1684a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1685a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1686a33eb0c4SMilanka Ringwald return; 1687a33eb0c4SMilanka Ringwald } 1688a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 1; 1689a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1690667ec068SMatthias Ringwald } 1691667ec068SMatthias Ringwald 1692c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 16939c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1694a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1695a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1696a33eb0c4SMilanka Ringwald return; 1697a33eb0c4SMilanka Ringwald } 1698667ec068SMatthias Ringwald // find index for assigned number 1699667ec068SMatthias Ringwald int i; 1700667ec068SMatthias Ringwald for (i = 0; i < hfp_indicators_nr ; i++){ 1701667ec068SMatthias Ringwald if (hfp_indicators[i] == assigned_number){ 1702667ec068SMatthias Ringwald // set value 1703667ec068SMatthias Ringwald hfp_indicators_value[i] = value; 1704667ec068SMatthias Ringwald // mark for update 1705a0ffb263SMatthias Ringwald if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1706a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap |= (1<<i); 1707667ec068SMatthias Ringwald // send update 1708a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1709a0ffb263SMatthias Ringwald } 1710667ec068SMatthias Ringwald return; 1711667ec068SMatthias Ringwald } 1712667ec068SMatthias Ringwald } 1713667ec068SMatthias Ringwald } 1714667ec068SMatthias Ringwald 1715d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 1716d7f6b5cbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1717d7f6b5cbSMatthias Ringwald if (!hfp_connection) { 1718d7f6b5cbSMatthias Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1719d7f6b5cbSMatthias Ringwald return 0; 1720d7f6b5cbSMatthias Ringwald } 1721d7f6b5cbSMatthias Ringwald return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1722d7f6b5cbSMatthias Ringwald } 1723