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 38e501bae0SMatthias 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 <string.h> 503deb3ec6SMatthias Ringwald 51235946f1SMatthias Ringwald #include "bluetooth_sdp.h" 5259c6af15SMatthias Ringwald #include "btstack_debug.h" 53d4dd47ffSMatthias Ringwald #include "btstack_event.h" 543deb3ec6SMatthias Ringwald #include "btstack_memory.h" 5559c6af15SMatthias Ringwald #include "btstack_run_loop.h" 5659c6af15SMatthias Ringwald #include "classic/core.h" 5759c6af15SMatthias Ringwald #include "classic/hfp.h" 5859c6af15SMatthias Ringwald #include "classic/hfp_hf.h" 59efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h" 60746ccb7eSMatthias Ringwald #include "classic/sdp_server.h" 61023f2764SMatthias Ringwald #include "classic/sdp_util.h" 6259c6af15SMatthias Ringwald #include "hci.h" 6359c6af15SMatthias Ringwald #include "hci_cmd.h" 6459c6af15SMatthias Ringwald #include "hci_dump.h" 6559c6af15SMatthias Ringwald #include "l2cap.h" 663deb3ec6SMatthias Ringwald 6727950165SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 6827950165SMatthias Ringwald 693deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit"; 703deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 713deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0; 723deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS]; 733deb3ec6SMatthias Ringwald 743deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0; 7525789943SMilanka Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_INDICATORS]; 7625789943SMilanka Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_INDICATORS]; 77667ec068SMatthias Ringwald 78667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9; 79667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9; 803deb3ec6SMatthias Ringwald 81ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback; 823deb3ec6SMatthias Ringwald 83ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status; 84ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status; 85ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status; 86ce263fc8SMatthias Ringwald 87ce263fc8SMatthias Ringwald static char phone_number[25]; 88ce263fc8SMatthias Ringwald 899c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){ 909c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 919c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 929c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 939c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 949c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 959c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 969c9c64c1SMatthias Ringwald return hfp_connection; 979c9c64c1SMatthias Ringwald } 989c9c64c1SMatthias Ringwald return NULL; 999c9c64c1SMatthias Ringwald } 1009c9c64c1SMatthias Ringwald 10113839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 1023deb3ec6SMatthias Ringwald if (callback == NULL){ 1033deb3ec6SMatthias Ringwald log_error("hfp_hf_register_packet_handler called with NULL callback"); 1043deb3ec6SMatthias Ringwald return; 1053deb3ec6SMatthias Ringwald } 106ca59be51SMatthias Ringwald hfp_hf_callback = callback; 107ca59be51SMatthias Ringwald hfp_set_hf_callback(callback); 1083deb3ec6SMatthias Ringwald } 1093deb3ec6SMatthias Ringwald 11013839019SMatthias 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){ 111a0ffb263SMatthias Ringwald if (!callback) return; 112a0ffb263SMatthias Ringwald uint8_t event[31]; 113a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 114a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 115a0ffb263SMatthias Ringwald event[2] = event_subtype; 116a0ffb263SMatthias Ringwald event[3] = status; 117a0ffb263SMatthias Ringwald event[4] = bnip_type; 1186d9a4cd0SMilanka Ringwald uint16_t size = btstack_min(strlen(bnip_number), sizeof(event) - 6); 119a0ffb263SMatthias Ringwald strncpy((char*)&event[5], bnip_number, size); 120a0ffb263SMatthias Ringwald event[5 + size] = 0; 12113839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 122a0ffb263SMatthias Ringwald } 123a0ffb263SMatthias Ringwald 12413839019SMatthias 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){ 125a0ffb263SMatthias Ringwald if (!callback) return; 126a0ffb263SMatthias Ringwald uint8_t event[30]; 127a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 128a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 129a0ffb263SMatthias Ringwald event[2] = event_subtype; 130a0ffb263SMatthias Ringwald event[3] = bnip_type; 1316d9a4cd0SMilanka Ringwald uint16_t size = btstack_min(strlen(bnip_number), sizeof(event) - 5); 132a0ffb263SMatthias Ringwald strncpy((char*)&event[4], bnip_number, size); 133a0ffb263SMatthias Ringwald event[4 + size] = 0; 13413839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 135a0ffb263SMatthias Ringwald } 136a0ffb263SMatthias Ringwald 1372b99f6edSMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, hfp_connection_t * connection){ 138a0ffb263SMatthias Ringwald if (!callback) return; 1390aee97efSMilanka Ringwald uint8_t event[36]; 1400aee97efSMilanka Ringwald int pos = 0; 1410aee97efSMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 1420aee97efSMilanka Ringwald event[pos++] = sizeof(event) - 2; 1430aee97efSMilanka Ringwald event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 1442b99f6edSMatthias Ringwald event[pos++] = connection->clcc_idx; 1452b99f6edSMatthias Ringwald event[pos++] = connection->clcc_dir; 1462b99f6edSMatthias Ringwald event[pos++] = connection->clcc_status; 1472b99f6edSMatthias Ringwald event[pos++] = connection->clcc_mode; 1482b99f6edSMatthias Ringwald event[pos++] = connection->clcc_mpty; 1492b99f6edSMatthias Ringwald event[pos++] = connection->bnip_type; 1502b99f6edSMatthias Ringwald uint16_t size = btstack_min(strlen(connection->bnip_number), sizeof(event) - pos); 1512b99f6edSMatthias Ringwald strncpy((char*)&event[pos], connection->bnip_number, size); 1520aee97efSMilanka Ringwald pos += size; 1530aee97efSMilanka Ringwald event[pos++] = 0; 1540aee97efSMilanka Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, pos); 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){ 274560b7bdbSMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "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; 363c741b032SMilanka Ringwald uint8_t event[10+HFP_MAX_INDICATOR_DESC_SIZE+1]; 364c741b032SMilanka Ringwald int pos = 0; 365c741b032SMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 366c741b032SMilanka Ringwald event[pos++] = sizeof(event) - 2; 367c741b032SMilanka Ringwald event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 368c741b032SMilanka Ringwald event[pos++] = indicator.index; 369c741b032SMilanka Ringwald event[pos++] = indicator.status; 370c741b032SMilanka Ringwald event[pos++] = indicator.min_range; 371c741b032SMilanka Ringwald event[pos++] = indicator.max_range; 372c741b032SMilanka Ringwald event[pos++] = indicator.mandatory; 373c741b032SMilanka Ringwald event[pos++] = indicator.enabled; 374c741b032SMilanka Ringwald event[pos++] = indicator.status_changed; 375c741b032SMilanka Ringwald strncpy((char*)&event[pos], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE); 376c741b032SMilanka Ringwald pos += HFP_MAX_INDICATOR_DESC_SIZE; 377c741b032SMilanka Ringwald event[pos] = 0; 37813839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 3793deb3ec6SMatthias Ringwald } 3803deb3ec6SMatthias Ringwald 38113839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){ 3823deb3ec6SMatthias Ringwald if (!callback) return; 38389425bfcSMilanka Ringwald uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1]; 3843deb3ec6SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 3853deb3ec6SMatthias Ringwald event[1] = sizeof(event) - 2; 3863deb3ec6SMatthias Ringwald event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED; 387a0ffb263SMatthias Ringwald event[3] = network_operator.mode; 388a0ffb263SMatthias Ringwald event[4] = network_operator.format; 38989425bfcSMilanka Ringwald strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE); 39089425bfcSMilanka Ringwald event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0; 39113839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 3923deb3ec6SMatthias Ringwald } 3933deb3ec6SMatthias Ringwald 394a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 395a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 396a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 397aa4dd815SMatthias Ringwald int done = 1; 3983deb3ec6SMatthias Ringwald 399a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 4003deb3ec6SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 401d715cf51SMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr); 402a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 403a0ffb263SMatthias Ringwald hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 4043deb3ec6SMatthias Ringwald break; 4053deb3ec6SMatthias Ringwald case HFP_NOTIFY_ON_CODECS: 406a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 407a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 4083deb3ec6SMatthias Ringwald break; 4093deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: 410a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 411a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 4123deb3ec6SMatthias Ringwald break; 4133deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS_STATUS: 414a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 415a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 4163deb3ec6SMatthias Ringwald break; 4173deb3ec6SMatthias Ringwald case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 418a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 419a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 4203deb3ec6SMatthias Ringwald break; 4213deb3ec6SMatthias Ringwald case HFP_RETRIEVE_CAN_HOLD_CALL: 422a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 423a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 4243deb3ec6SMatthias Ringwald break; 4253deb3ec6SMatthias Ringwald case HFP_LIST_GENERIC_STATUS_INDICATORS: 426a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 427a0ffb263SMatthias Ringwald hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4283deb3ec6SMatthias Ringwald break; 4293deb3ec6SMatthias Ringwald case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 430a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 431a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4323deb3ec6SMatthias Ringwald break; 4333deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 434a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 435a0ffb263SMatthias Ringwald hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4363deb3ec6SMatthias Ringwald break; 4373deb3ec6SMatthias Ringwald default: 438aa4dd815SMatthias Ringwald done = 0; 4393deb3ec6SMatthias Ringwald break; 4403deb3ec6SMatthias Ringwald } 4413deb3ec6SMatthias Ringwald return done; 4423deb3ec6SMatthias Ringwald } 4433deb3ec6SMatthias Ringwald 444ce263fc8SMatthias Ringwald 445a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 446a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 447a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 448ce263fc8SMatthias Ringwald 449ce263fc8SMatthias Ringwald int done = 0; 450a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 451a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 452ce263fc8SMatthias Ringwald done = 1; 453a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 454ce263fc8SMatthias Ringwald return done; 455ce263fc8SMatthias Ringwald }; 456a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators){ 457a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 458ce263fc8SMatthias Ringwald done = 1; 459a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 460a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap, 461a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr); 462ce263fc8SMatthias Ringwald return done; 463ce263fc8SMatthias Ringwald } 464ce263fc8SMatthias Ringwald 465a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 466ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 467a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 468a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 469a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 470ce263fc8SMatthias Ringwald return 1; 471ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 472a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 473a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 474a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 475ce263fc8SMatthias Ringwald return 1; 476ce263fc8SMatthias Ringwald default: 477ce263fc8SMatthias Ringwald break; 478ce263fc8SMatthias Ringwald } 479ce263fc8SMatthias Ringwald 480a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 481a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 482ce263fc8SMatthias Ringwald done = 1; 483a0ffb263SMatthias Ringwald hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 484ce263fc8SMatthias Ringwald return done; 485ce263fc8SMatthias Ringwald } 486ce263fc8SMatthias Ringwald 487ce263fc8SMatthias Ringwald return done; 488ce263fc8SMatthias Ringwald } 489ce263fc8SMatthias Ringwald 490a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 491ce263fc8SMatthias Ringwald /* events ( == commands): 492ce263fc8SMatthias Ringwald HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs 493ce263fc8SMatthias Ringwald HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 494ce263fc8SMatthias Ringwald hf_trigger_codec_connection_setup == received BCC 495ce263fc8SMatthias Ringwald ag_trigger_codec_connection_setup == received from AG to send BCS 496ce263fc8SMatthias Ringwald HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS 497ce263fc8SMatthias Ringwald */ 498ce263fc8SMatthias Ringwald 499a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 500ce263fc8SMatthias Ringwald 501a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 502ce263fc8SMatthias Ringwald case HFP_CMD_AVAILABLE_CODECS: 503a0ffb263SMatthias Ringwald if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0; 504ce263fc8SMatthias Ringwald 505a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 506a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 507a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 508ce263fc8SMatthias Ringwald return 1; 509ce263fc8SMatthias Ringwald case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP: 510a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 511a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 512a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 513ce263fc8SMatthias Ringwald 514a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 515a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 516a0ffb263SMatthias Ringwald hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 517ce263fc8SMatthias Ringwald break; 518ce263fc8SMatthias Ringwald 5196a7f44bdSMilanka Ringwald case HFP_CMD_AG_SUGGESTED_CODEC:{ 5206a7f44bdSMilanka Ringwald if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){ 521a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 522a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 523a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 5240e0be203SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 525c1ab6cc1SMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 526fcb08cdbSMilanka Ringwald hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 527ce263fc8SMatthias Ringwald } else { 528a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 529a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 530a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 531a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 532a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 533a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 534ce263fc8SMatthias Ringwald 535ce263fc8SMatthias Ringwald } 536ce263fc8SMatthias Ringwald break; 5376a7f44bdSMilanka Ringwald } 538ce263fc8SMatthias Ringwald default: 539ce263fc8SMatthias Ringwald break; 540ce263fc8SMatthias Ringwald } 541ce263fc8SMatthias Ringwald return 0; 542ce263fc8SMatthias Ringwald } 543ce263fc8SMatthias Ringwald 544a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 545505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 546505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 547ce263fc8SMatthias Ringwald 54864f19dedSMilanka Ringwald if (hfp_connection->release_audio_connection){ 549a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 550a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 551a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 552ce263fc8SMatthias Ringwald return 1; 553ce263fc8SMatthias Ringwald } 554ce263fc8SMatthias Ringwald 555a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 556ce263fc8SMatthias Ringwald 557ce263fc8SMatthias Ringwald // run codecs exchange 558a0ffb263SMatthias Ringwald int done = codecs_exchange_state_machine(hfp_connection); 559ce263fc8SMatthias Ringwald if (done) return 1; 560ce263fc8SMatthias Ringwald 56138200c1dSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 56238200c1dSMilanka Ringwald if (hfp_connection->establish_audio_connection){ 56338200c1dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 56438200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 56538200c1dSMilanka Ringwald hfp_setup_synchronous_connection(hfp_connection); 56638200c1dSMilanka Ringwald return 1; 56738200c1dSMilanka Ringwald } 56838200c1dSMilanka Ringwald 569ce263fc8SMatthias Ringwald return 0; 570ce263fc8SMatthias Ringwald } 571ce263fc8SMatthias Ringwald 57238200c1dSMilanka Ringwald 573a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 574a0ffb263SMatthias Ringwald if (hfp_connection->hf_answer_incoming_call){ 575a0ffb263SMatthias Ringwald hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 576a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 0; 577ce263fc8SMatthias Ringwald return 1; 578ce263fc8SMatthias Ringwald } 579ce263fc8SMatthias Ringwald return 0; 580ce263fc8SMatthias Ringwald } 581ce263fc8SMatthias Ringwald 582a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){ 583a0ffb263SMatthias Ringwald if (!hfp_connection) return; 584724f400dSMatthias Ringwald if (!hfp_connection->rfcomm_cid) return; 5857522e673SMatthias Ringwald 58622387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) { 58722387625SMatthias Ringwald log_info("HFP HF%p, wrong role %u", hfp_connection, hfp_connection->local_role); 58822387625SMatthias Ringwald return; 58922387625SMatthias Ringwald } 59022387625SMatthias Ringwald 591b72c4a9eSMatthias Ringwald if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){ 592b72c4a9eSMatthias Ringwald 593c169df2fSMatthias Ringwald bool eSCO = hfp_connection->hf_accept_sco == 2; 594b72c4a9eSMatthias Ringwald hfp_connection->hf_accept_sco = 0; 5957522e673SMatthias Ringwald 5967522e673SMatthias Ringwald // notify about codec selection if not done already 5977522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 5987522e673SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 5997522e673SMatthias Ringwald } 6007522e673SMatthias Ringwald 6017522e673SMatthias Ringwald // remote supported feature eSCO is set if link type is eSCO 6027522e673SMatthias Ringwald // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 6037522e673SMatthias Ringwald uint16_t max_latency; 6047522e673SMatthias Ringwald uint8_t retransmission_effort; 6057522e673SMatthias Ringwald uint16_t packet_types; 6067522e673SMatthias Ringwald 607c169df2fSMatthias Ringwald if (eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 6087522e673SMatthias Ringwald max_latency = 0x000c; 6097522e673SMatthias Ringwald retransmission_effort = 0x02; 610*352a0504SMatthias Ringwald // eSCO: EV3 and 2-EV3 611*352a0504SMatthias Ringwald packet_types = 0x0048; 6127522e673SMatthias Ringwald } else { 6137522e673SMatthias Ringwald max_latency = 0xffff; 6147522e673SMatthias Ringwald retransmission_effort = 0xff; 615*352a0504SMatthias Ringwald // sco: HV1 and HV3 616*352a0504SMatthias Ringwald packet_types = 0x005; 6177522e673SMatthias Ringwald } 6187522e673SMatthias Ringwald 619*352a0504SMatthias Ringwald // mSBC only allows for transparent data 6207522e673SMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 6217522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 6227522e673SMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data 6237522e673SMatthias Ringwald } 6247522e673SMatthias Ringwald 625*352a0504SMatthias Ringwald // filter packet types 626*352a0504SMatthias Ringwald packet_types &= hfp_get_sco_packet_types(); 627*352a0504SMatthias Ringwald 628*352a0504SMatthias Ringwald // bits 6-9 are 'don't allow' 629*352a0504SMatthias Ringwald packet_types ^= 0x3c0; 630*352a0504SMatthias Ringwald 631991c26beSMatthias Ringwald log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting); 6327522e673SMatthias Ringwald hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 6337522e673SMatthias Ringwald sco_voice_setting, retransmission_effort, packet_types); 6347522e673SMatthias Ringwald return; 6357522e673SMatthias Ringwald } 6367522e673SMatthias Ringwald 637d4dd47ffSMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 638d4dd47ffSMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 639d4dd47ffSMatthias Ringwald return; 640d4dd47ffSMatthias Ringwald } 641a0ffb263SMatthias Ringwald int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 642ce263fc8SMatthias Ringwald if (!done){ 643a0ffb263SMatthias Ringwald done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 644ce263fc8SMatthias Ringwald } 645ce263fc8SMatthias Ringwald if (!done){ 646a0ffb263SMatthias Ringwald done = hfp_hf_run_for_audio_connection(hfp_connection); 647ce263fc8SMatthias Ringwald } 648ce263fc8SMatthias Ringwald if (!done){ 649a0ffb263SMatthias Ringwald done = call_setup_state_machine(hfp_connection); 650ce263fc8SMatthias Ringwald } 651ce263fc8SMatthias Ringwald 6521016a228SMatthias Ringwald // don't send a new command while ok still pending 6531016a228SMatthias Ringwald if (hfp_connection->ok_pending) return; 6541016a228SMatthias Ringwald 655a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 656a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 657a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 658a0ffb263SMatthias Ringwald hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 659ce263fc8SMatthias Ringwald return; 660ce263fc8SMatthias Ringwald } 661ce263fc8SMatthias Ringwald 662a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 663a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 664a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 665a0ffb263SMatthias Ringwald hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 666ce263fc8SMatthias Ringwald return; 667ce263fc8SMatthias Ringwald } 668ce263fc8SMatthias Ringwald 669a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_calling_line_notification){ 670a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 0; 671a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 672a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 673ce263fc8SMatthias Ringwald return; 674ce263fc8SMatthias Ringwald } 675ce263fc8SMatthias Ringwald 676a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_calling_line_notification){ 677a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 0; 678a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 679a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 680ce263fc8SMatthias Ringwald return; 681ce263fc8SMatthias Ringwald } 682ce263fc8SMatthias Ringwald 683a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 684a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 685a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 686a0ffb263SMatthias Ringwald hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0); 687ce263fc8SMatthias Ringwald return; 688ce263fc8SMatthias Ringwald } 689ce263fc8SMatthias Ringwald 690a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){ 691a0ffb263SMatthias Ringwald hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0; 692a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 693a0ffb263SMatthias Ringwald hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1); 694ce263fc8SMatthias Ringwald return; 695ce263fc8SMatthias Ringwald } 696ce263fc8SMatthias Ringwald 697a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_voice_recognition_notification){ 698a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_voice_recognition_notification = 0; 699a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 700a0ffb263SMatthias Ringwald hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 701ce263fc8SMatthias Ringwald return; 702ce263fc8SMatthias Ringwald } 703ce263fc8SMatthias Ringwald 704a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_voice_recognition_notification){ 705a0ffb263SMatthias Ringwald hfp_connection->hf_activate_voice_recognition_notification = 0; 706a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 707a0ffb263SMatthias Ringwald hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 708ce263fc8SMatthias Ringwald return; 709ce263fc8SMatthias Ringwald } 710ce263fc8SMatthias Ringwald 711ce263fc8SMatthias Ringwald 712a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_call_waiting_notification){ 713a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 0; 714a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 715a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 716ce263fc8SMatthias Ringwald return; 717ce263fc8SMatthias Ringwald } 718ce263fc8SMatthias Ringwald 719a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_call_waiting_notification){ 720a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 0; 721a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 722a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 723ce263fc8SMatthias Ringwald return; 724ce263fc8SMatthias Ringwald } 725ce263fc8SMatthias Ringwald 726a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_outgoing_call){ 727a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 0; 728a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 729a0ffb263SMatthias Ringwald hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 730ce263fc8SMatthias Ringwald return; 731ce263fc8SMatthias Ringwald } 732ce263fc8SMatthias Ringwald 733a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_memory_dialing){ 734a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 0; 735a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 736a0ffb263SMatthias Ringwald hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 737ce263fc8SMatthias Ringwald return; 738ce263fc8SMatthias Ringwald } 739ce263fc8SMatthias Ringwald 740a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_redial_last_number){ 741a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 0; 742a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 743a0ffb263SMatthias Ringwald hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 744ce263fc8SMatthias Ringwald return; 745ce263fc8SMatthias Ringwald } 746ce263fc8SMatthias Ringwald 747a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chup){ 748a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 0; 749a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 750a0ffb263SMatthias Ringwald hfp_hf_send_chup(hfp_connection->rfcomm_cid); 751ce263fc8SMatthias Ringwald return; 752ce263fc8SMatthias Ringwald } 753ce263fc8SMatthias Ringwald 754a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_0){ 755a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 0; 756a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 757a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 758ce263fc8SMatthias Ringwald return; 759ce263fc8SMatthias Ringwald } 760ce263fc8SMatthias Ringwald 761a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_1){ 762a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 0; 763a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 764a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 765ce263fc8SMatthias Ringwald return; 766ce263fc8SMatthias Ringwald } 767ce263fc8SMatthias Ringwald 768a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_2){ 769a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 0; 770a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 771a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 772ce263fc8SMatthias Ringwald return; 773ce263fc8SMatthias Ringwald } 774ce263fc8SMatthias Ringwald 775a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_3){ 776a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 0; 777a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 778a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 779ce263fc8SMatthias Ringwald return; 780ce263fc8SMatthias Ringwald } 781ce263fc8SMatthias Ringwald 782a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_4){ 783a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 0; 784a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 785a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 786ce263fc8SMatthias Ringwald return; 787ce263fc8SMatthias Ringwald } 788ce263fc8SMatthias Ringwald 789a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_x){ 790a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 0; 791a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 792a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 793667ec068SMatthias Ringwald return; 794667ec068SMatthias Ringwald } 795667ec068SMatthias Ringwald 796a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_dtmf_code){ 797a0ffb263SMatthias Ringwald char code = hfp_connection->hf_send_dtmf_code; 798a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = 0; 799a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 800a0ffb263SMatthias Ringwald hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 801ce263fc8SMatthias Ringwald return; 802ce263fc8SMatthias Ringwald } 803ce263fc8SMatthias Ringwald 804a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_binp){ 805a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 0; 806a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 807a0ffb263SMatthias Ringwald hfp_hf_send_binp(hfp_connection->rfcomm_cid); 808ce263fc8SMatthias Ringwald return; 809ce263fc8SMatthias Ringwald } 810ce263fc8SMatthias Ringwald 811a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_clcc){ 812a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 0; 813a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 814a0ffb263SMatthias Ringwald hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 815667ec068SMatthias Ringwald return; 816667ec068SMatthias Ringwald } 817667ec068SMatthias Ringwald 818a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_rrh){ 819a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 0; 820667ec068SMatthias Ringwald char buffer[20]; 821a0ffb263SMatthias Ringwald switch (hfp_connection->hf_send_rrh_command){ 822667ec068SMatthias Ringwald case '?': 823667ec068SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD); 824a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 825667ec068SMatthias Ringwald return; 826667ec068SMatthias Ringwald case '0': 827667ec068SMatthias Ringwald case '1': 828667ec068SMatthias Ringwald case '2': 829a0ffb263SMatthias Ringwald sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command); 830a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 831667ec068SMatthias Ringwald return; 832667ec068SMatthias Ringwald default: 833667ec068SMatthias Ringwald break; 834667ec068SMatthias Ringwald } 835667ec068SMatthias Ringwald return; 836667ec068SMatthias Ringwald } 837667ec068SMatthias Ringwald 838a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_cnum){ 839a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 0; 840667ec068SMatthias Ringwald char buffer[20]; 841667ec068SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION); 842a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 843667ec068SMatthias Ringwald return; 844667ec068SMatthias Ringwald } 845667ec068SMatthias Ringwald 846667ec068SMatthias Ringwald // update HF indicators 847a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_update_bitmap){ 848667ec068SMatthias Ringwald int i; 849667ec068SMatthias Ringwald for (i=0;i<hfp_indicators_nr;i++){ 850a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 851a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].state){ 852a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 853a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 854667ec068SMatthias Ringwald char buffer[30]; 855ecb7d461SMatthias Ringwald sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]); 856a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 857667ec068SMatthias Ringwald } else { 85860ebb071SMilanka Ringwald log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]); 859667ec068SMatthias Ringwald } 860667ec068SMatthias Ringwald return; 861667ec068SMatthias Ringwald } 862667ec068SMatthias Ringwald } 863667ec068SMatthias Ringwald } 864667ec068SMatthias Ringwald 865ce263fc8SMatthias Ringwald if (done) return; 866ce263fc8SMatthias Ringwald // deal with disconnect 867a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 868ce263fc8SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 869a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 870a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 871ce263fc8SMatthias Ringwald break; 872ce263fc8SMatthias Ringwald 873ce263fc8SMatthias Ringwald default: 874ce263fc8SMatthias Ringwald break; 875ce263fc8SMatthias Ringwald } 876ce263fc8SMatthias Ringwald } 877ce263fc8SMatthias Ringwald 878a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 879a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 8806a7f44bdSMilanka Ringwald 881ca59be51SMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 8827522e673SMatthias Ringwald 883667ec068SMatthias Ringwald // restore volume settings 884a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = hfp_hf_speaker_gain; 885a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 886ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 887a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = hfp_hf_microphone_gain; 888a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 889ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 890667ec068SMatthias Ringwald // enable all indicators 891667ec068SMatthias Ringwald int i; 892667ec068SMatthias Ringwald for (i=0;i<hfp_indicators_nr;i++){ 893a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i]; 894a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].state = 1; 895667ec068SMatthias Ringwald } 896ce263fc8SMatthias Ringwald } 897ce263fc8SMatthias Ringwald 898a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){ 899a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 900a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 9013deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 902a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 903a0ffb263SMatthias Ringwald hfp_connection->state = HFP_NOTIFY_ON_CODECS; 9043deb3ec6SMatthias Ringwald break; 9053deb3ec6SMatthias Ringwald } 906a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 9073deb3ec6SMatthias Ringwald break; 9083deb3ec6SMatthias Ringwald 9093deb3ec6SMatthias Ringwald case HFP_W4_NOTIFY_ON_CODECS: 910a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 9113deb3ec6SMatthias Ringwald break; 9123deb3ec6SMatthias Ringwald 9133deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 914a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 9153deb3ec6SMatthias Ringwald break; 9163deb3ec6SMatthias Ringwald 9173deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 918a0ffb263SMatthias Ringwald hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 9193deb3ec6SMatthias Ringwald break; 9203deb3ec6SMatthias Ringwald 9213deb3ec6SMatthias Ringwald case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 922a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 923a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 9243deb3ec6SMatthias Ringwald break; 9253deb3ec6SMatthias Ringwald } 926a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 927a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 9283deb3ec6SMatthias Ringwald break; 9293deb3ec6SMatthias Ringwald } 930a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9313deb3ec6SMatthias Ringwald break; 9323deb3ec6SMatthias Ringwald 9333deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 934a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 935a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 9363deb3ec6SMatthias Ringwald break; 9373deb3ec6SMatthias Ringwald } 938a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9393deb3ec6SMatthias Ringwald break; 9403deb3ec6SMatthias Ringwald 9413deb3ec6SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 942a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 9433deb3ec6SMatthias Ringwald break; 9443deb3ec6SMatthias Ringwald 9453deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 946a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 9473deb3ec6SMatthias Ringwald break; 9483deb3ec6SMatthias Ringwald 9493deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 950a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9513deb3ec6SMatthias Ringwald break; 952ce263fc8SMatthias Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 953a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 954a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 955ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 956ce263fc8SMatthias Ringwald break; 957ce263fc8SMatthias Ringwald } 9583deb3ec6SMatthias Ringwald 959a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 960a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 961ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 962ce263fc8SMatthias Ringwald break; 9633deb3ec6SMatthias Ringwald } 9643deb3ec6SMatthias Ringwald 965a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 966ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 967a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 968ce263fc8SMatthias Ringwald break; 969ce263fc8SMatthias Ringwald case HPF_HF_QUERY_OPERATOR_W4_RESULT: 970a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 971ca59be51SMatthias Ringwald hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator); 972ce263fc8SMatthias Ringwald break; 973ce263fc8SMatthias Ringwald default: 974ce263fc8SMatthias Ringwald break; 9753deb3ec6SMatthias Ringwald } 976ce263fc8SMatthias Ringwald 977a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 978a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 979ce263fc8SMatthias Ringwald break; 9803deb3ec6SMatthias Ringwald } 9813deb3ec6SMatthias Ringwald 982a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 983aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 984a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 9853deb3ec6SMatthias Ringwald break; 986ce263fc8SMatthias Ringwald case HFP_CODECS_HF_CONFIRMED_CODEC: 987a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 988ce263fc8SMatthias Ringwald break; 9893deb3ec6SMatthias Ringwald default: 9903deb3ec6SMatthias Ringwald break; 9913deb3ec6SMatthias Ringwald } 9923deb3ec6SMatthias Ringwald break; 9933deb3ec6SMatthias Ringwald default: 9943deb3ec6SMatthias Ringwald break; 9953deb3ec6SMatthias Ringwald } 9963deb3ec6SMatthias Ringwald 9973deb3ec6SMatthias Ringwald // done 998a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 9993deb3ec6SMatthias Ringwald } 10003deb3ec6SMatthias Ringwald 1001e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 1002c1ab6cc1SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 1003e0d09929SMatthias Ringwald } 1004e0d09929SMatthias Ringwald 1005b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 10064562e2a2SMatthias Ringwald uint16_t i; 10074562e2a2SMatthias Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 10084562e2a2SMatthias Ringwald if (hfp_connection->ag_indicators[i].status_changed) { 10094562e2a2SMatthias Ringwald if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 10104562e2a2SMatthias Ringwald hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 10114562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 10124562e2a2SMatthias Ringwald hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 10134562e2a2SMatthias Ringwald // avoid set but not used warning 10144562e2a2SMatthias Ringwald (void) hfp_callheld_status; 10154562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 10164562e2a2SMatthias Ringwald hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 10174562e2a2SMatthias Ringwald } 10184562e2a2SMatthias Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 10194562e2a2SMatthias Ringwald hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]); 10204562e2a2SMatthias Ringwald break; 10214562e2a2SMatthias Ringwald } 10224562e2a2SMatthias Ringwald } 10234562e2a2SMatthias Ringwald } 10244562e2a2SMatthias Ringwald 1025426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 1026186dd3d2SMatthias Ringwald int value; 1027186dd3d2SMatthias Ringwald int i; 1028a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 1029667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 1030a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1031ca59be51SMatthias Ringwald hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number); 1032667ec068SMatthias Ringwald break; 1033667ec068SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 1034a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1035ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1036667ec068SMatthias Ringwald break; 1037667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1038a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10392b99f6edSMatthias Ringwald hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection); 1040667ec068SMatthias Ringwald break; 1041ce263fc8SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1042a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10432308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1044667ec068SMatthias Ringwald hfp_hf_speaker_gain = value; 1045ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1046ce263fc8SMatthias Ringwald break; 1047ce263fc8SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1048a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10492308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1050667ec068SMatthias Ringwald hfp_hf_microphone_gain = value; 1051ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1052ce263fc8SMatthias Ringwald break; 1053ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1054a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1055ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1056a0ffb263SMatthias Ringwald break; 1057a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1058a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1059ca59be51SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1060a0ffb263SMatthias Ringwald break; 1061a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1062a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1063ca59be51SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1064ce263fc8SMatthias Ringwald break; 1065ce263fc8SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1066a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1067a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1068a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1069ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1070ce263fc8SMatthias Ringwald break; 1071ce263fc8SMatthias Ringwald case HFP_CMD_ERROR: 1072a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1073a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 1074a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 107590244c92SMilanka Ringwald 107690244c92SMilanka Ringwald switch (hfp_connection->state){ 107790244c92SMilanka Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 107890244c92SMilanka Ringwald switch (hfp_connection->codecs_state){ 107990244c92SMilanka Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 108090244c92SMilanka Ringwald hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 108190244c92SMilanka Ringwald return; 108290244c92SMilanka Ringwald default: 108390244c92SMilanka Ringwald break; 108490244c92SMilanka Ringwald } 108590244c92SMilanka Ringwald default: 108690244c92SMilanka Ringwald break; 108790244c92SMilanka Ringwald } 1088ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1089ce263fc8SMatthias Ringwald break; 1090ce263fc8SMatthias Ringwald case HFP_CMD_OK: 1091a0ffb263SMatthias Ringwald hfp_hf_switch_on_ok(hfp_connection); 1092ce263fc8SMatthias Ringwald break; 1093ce263fc8SMatthias Ringwald case HFP_CMD_RING: 1094ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1095ce263fc8SMatthias Ringwald break; 1096ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 10974562e2a2SMatthias Ringwald hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1098ce263fc8SMatthias Ringwald break; 1099c741b032SMilanka Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1100c741b032SMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1101c741b032SMilanka Ringwald hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]); 1102c741b032SMilanka Ringwald } 1103c741b032SMilanka Ringwald hfp_connection->command = HFP_CMD_NONE; 1104c741b032SMilanka Ringwald break; 1105ce263fc8SMatthias Ringwald default: 1106ce263fc8SMatthias Ringwald break; 11073deb3ec6SMatthias Ringwald } 11080cef86faSMatthias Ringwald } 1109426f9988SMatthias Ringwald 1110426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1111426f9988SMatthias Ringwald UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 1112426f9988SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1113426f9988SMatthias Ringwald if (size < 1) return; 1114426f9988SMatthias Ringwald 1115426f9988SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1116426f9988SMatthias Ringwald if (!hfp_connection) return; 1117426f9988SMatthias Ringwald 1118426f9988SMatthias Ringwald hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1119426f9988SMatthias Ringwald 1120426f9988SMatthias Ringwald // process messages byte-wise 1121426f9988SMatthias Ringwald int pos; 1122426f9988SMatthias Ringwald for (pos = 0; pos < size; pos++){ 1123426f9988SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 1); 1124426f9988SMatthias Ringwald 1125426f9988SMatthias Ringwald // parse until end of line "\r\n" 1126426f9988SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1127426f9988SMatthias Ringwald 1128426f9988SMatthias Ringwald hfp_hf_handle_rfcomm_command(hfp_connection); 1129426f9988SMatthias Ringwald } 1130a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11313deb3ec6SMatthias Ringwald } 11323deb3ec6SMatthias Ringwald 11330cb5b971SMatthias Ringwald static void hfp_run(void){ 1134665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1135665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1136665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1137a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 113822387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1139a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11403deb3ec6SMatthias Ringwald } 11413deb3ec6SMatthias Ringwald } 11423deb3ec6SMatthias Ringwald 1143e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 11443deb3ec6SMatthias Ringwald switch (packet_type){ 11453deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 1146426f9988SMatthias Ringwald hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size); 11473deb3ec6SMatthias Ringwald break; 11483deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1149d4dd47ffSMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1150d4dd47ffSMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 1151d4dd47ffSMatthias Ringwald hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1152d4dd47ffSMatthias Ringwald return; 1153d4dd47ffSMatthias Ringwald } 115427950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1155202c8a4cSMatthias Ringwald break; 11563deb3ec6SMatthias Ringwald default: 11573deb3ec6SMatthias Ringwald break; 11583deb3ec6SMatthias Ringwald } 11593deb3ec6SMatthias Ringwald hfp_run(); 11603deb3ec6SMatthias Ringwald } 11613deb3ec6SMatthias Ringwald 1162405014fbSMatthias Ringwald static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1163405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 116438200c1dSMilanka Ringwald 116538200c1dSMilanka Ringwald // allow for sco established -> ring transition and sco retry 116638200c1dSMilanka Ringwald if (packet_type != HCI_EVENT_PACKET) return; 116738200c1dSMilanka Ringwald if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return; 116838200c1dSMilanka Ringwald hfp_run(); 1169405014fbSMatthias Ringwald } 1170405014fbSMatthias Ringwald 1171a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1172520c92d5SMatthias Ringwald hfp_init(); 1173d63c37a1SMatthias Ringwald 1174405014fbSMatthias Ringwald hci_event_callback_registration.callback = &hci_event_packet_handler; 117527950165SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 117627950165SMatthias Ringwald 1177e9c22d4eSMatthias Ringwald rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 117827950165SMatthias Ringwald 117927950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 1180e9c22d4eSMatthias Ringwald hfp_set_hf_rfcomm_packet_handler(&rfcomm_packet_handler); 118127950165SMatthias Ringwald 11821b005648SMatthias Ringwald hfp_set_hf_run_for_context(hfp_run_for_context); 1183d68dcce1SMatthias Ringwald 1184a0ffb263SMatthias Ringwald hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1185a0ffb263SMatthias Ringwald hfp_codecs_nr = 0; 1186a0ffb263SMatthias Ringwald hfp_indicators_nr = 0; 1187a0ffb263SMatthias Ringwald hfp_hf_speaker_gain = 9; 1188a0ffb263SMatthias Ringwald hfp_hf_microphone_gain = 9; 1189a0ffb263SMatthias Ringwald } 1190a0ffb263SMatthias Ringwald 1191a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){ 11923deb3ec6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS){ 1193a0ffb263SMatthias Ringwald log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 11943deb3ec6SMatthias Ringwald return; 11953deb3ec6SMatthias Ringwald } 11963deb3ec6SMatthias Ringwald 11973deb3ec6SMatthias Ringwald hfp_codecs_nr = codecs_nr; 11983deb3ec6SMatthias Ringwald int i; 11993deb3ec6SMatthias Ringwald for (i=0; i<codecs_nr; i++){ 12003deb3ec6SMatthias Ringwald hfp_codecs[i] = codecs[i]; 12013deb3ec6SMatthias Ringwald } 12023deb3ec6SMatthias Ringwald } 12033deb3ec6SMatthias Ringwald 1204a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){ 12053deb3ec6SMatthias Ringwald hfp_supported_features = supported_features; 1206a0ffb263SMatthias Ringwald } 12073deb3ec6SMatthias Ringwald 1208a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){ 12093deb3ec6SMatthias Ringwald hfp_indicators_nr = indicators_nr; 12103deb3ec6SMatthias Ringwald int i; 1211a0ffb263SMatthias Ringwald for (i = 0; i < hfp_indicators_nr ; i++){ 12123deb3ec6SMatthias Ringwald hfp_indicators[i] = indicators[i]; 12133deb3ec6SMatthias Ringwald } 12143deb3ec6SMatthias Ringwald } 12153deb3ec6SMatthias Ringwald 12163deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1217323d3000SMatthias Ringwald hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 12183deb3ec6SMatthias Ringwald } 12193deb3ec6SMatthias Ringwald 1220c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 12219c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1222a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1223a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1224a33eb0c4SMilanka Ringwald return; 1225a33eb0c4SMilanka Ringwald } 1226a0ffb263SMatthias Ringwald hfp_release_service_level_connection(hfp_connection); 1227a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12283deb3ec6SMatthias Ringwald } 12293deb3ec6SMatthias Ringwald 1230c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 12319c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1232a0ffb263SMatthias Ringwald if (!hfp_connection) { 1233a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12343deb3ec6SMatthias Ringwald return; 12353deb3ec6SMatthias Ringwald } 1236a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = enable; 1237a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12383deb3ec6SMatthias Ringwald } 12393deb3ec6SMatthias Ringwald 1240c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1241c8626498SMilanka Ringwald hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1242ce263fc8SMatthias Ringwald } 1243ce263fc8SMatthias Ringwald 1244c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1245c8626498SMilanka Ringwald hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1246ce263fc8SMatthias Ringwald } 1247ce263fc8SMatthias Ringwald 12483deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format 1249c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 12509c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1251a0ffb263SMatthias Ringwald if (!hfp_connection) { 1252a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12533deb3ec6SMatthias Ringwald return; 12543deb3ec6SMatthias Ringwald } 1255a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1256a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1257a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12583deb3ec6SMatthias Ringwald } 12593deb3ec6SMatthias Ringwald 1260c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 12619c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1262a0ffb263SMatthias Ringwald if (!hfp_connection) { 1263a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12643deb3ec6SMatthias Ringwald return; 12653deb3ec6SMatthias Ringwald } 1266a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1267ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1268a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1269ce263fc8SMatthias Ringwald break; 1270ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1271a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1272ce263fc8SMatthias Ringwald break; 1273ce263fc8SMatthias Ringwald default: 1274ce263fc8SMatthias Ringwald break; 1275ce263fc8SMatthias Ringwald } 1276a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12773deb3ec6SMatthias Ringwald } 12783deb3ec6SMatthias Ringwald 1279c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 12809c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1281a0ffb263SMatthias Ringwald if (!hfp_connection) { 1282a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12833deb3ec6SMatthias Ringwald return; 12843deb3ec6SMatthias Ringwald } 1285a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = enable; 1286a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12873deb3ec6SMatthias Ringwald } 12883deb3ec6SMatthias Ringwald 1289ce263fc8SMatthias Ringwald 1290c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1291c8626498SMilanka Ringwald hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1292ce263fc8SMatthias Ringwald } 1293ce263fc8SMatthias Ringwald 1294c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1295c8626498SMilanka Ringwald hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1296ce263fc8SMatthias Ringwald } 1297ce263fc8SMatthias Ringwald 129838200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 129938200c1dSMilanka Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_supported_features & (1<<HFP_HFSF_ESCO_S4)); 130038200c1dSMilanka Ringwald } 1301ce263fc8SMatthias Ringwald 1302c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 13039c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1304a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1305a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1306a33eb0c4SMilanka Ringwald return; 1307a33eb0c4SMilanka Ringwald } 1308a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 1309ce263fc8SMatthias Ringwald 1310a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1311a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 13123deb3ec6SMatthias Ringwald 131338200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 0; 131438200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 1; 1315a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 1316ce263fc8SMatthias Ringwald log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults"); 1317a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 131838200c1dSMilanka Ringwald hfp_connection->suggested_codec = HFP_CODEC_CVSD; 131938200c1dSMilanka Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 132038200c1dSMilanka Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 132138200c1dSMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 132238200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 0; 132338200c1dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1324ce263fc8SMatthias Ringwald } else { 1325a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 1326aa4dd815SMatthias Ringwald case HFP_CODECS_W4_AG_COMMON_CODEC: 1327aa4dd815SMatthias Ringwald break; 1328aa4dd815SMatthias Ringwald default: 132938200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 1330a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP; 1331aa4dd815SMatthias Ringwald break; 13323deb3ec6SMatthias Ringwald } 1333ce263fc8SMatthias Ringwald } 1334ce263fc8SMatthias Ringwald 1335a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 13363deb3ec6SMatthias Ringwald } 13373deb3ec6SMatthias Ringwald 1338c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 13399c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1340a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1341a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1342a33eb0c4SMilanka Ringwald return; 1343a33eb0c4SMilanka Ringwald } 1344a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 1345a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 13463deb3ec6SMatthias Ringwald } 13473deb3ec6SMatthias Ringwald 1348c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 13499c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1350a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1351a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1352a33eb0c4SMilanka Ringwald return; 1353a33eb0c4SMilanka Ringwald } 1354ce263fc8SMatthias Ringwald 1355ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1356a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 1; 1357a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1358ce263fc8SMatthias Ringwald } else { 1359ce263fc8SMatthias Ringwald log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1360ce263fc8SMatthias Ringwald } 1361ce263fc8SMatthias Ringwald } 1362ce263fc8SMatthias Ringwald 1363c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 13649c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1365a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1366a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1367a33eb0c4SMilanka Ringwald return; 1368a33eb0c4SMilanka Ringwald } 1369a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 1370a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1371ce263fc8SMatthias Ringwald } 1372ce263fc8SMatthias Ringwald 1373c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(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){ 1381a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 1382a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1383ce263fc8SMatthias Ringwald } 1384ce263fc8SMatthias Ringwald } 1385ce263fc8SMatthias Ringwald 1386c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){ 13879c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1388a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1389a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1390a33eb0c4SMilanka Ringwald return; 1391a33eb0c4SMilanka Ringwald } 1392ce263fc8SMatthias Ringwald 1393ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1394a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 1; 1395a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1396ce263fc8SMatthias Ringwald } 1397ce263fc8SMatthias Ringwald } 1398ce263fc8SMatthias Ringwald 1399c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 14009c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1401a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1402a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1403a33eb0c4SMilanka Ringwald return; 1404a33eb0c4SMilanka Ringwald } 1405ce263fc8SMatthias Ringwald 1406505f1c30SMatthias Ringwald if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1407505f1c30SMatthias Ringwald (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1408a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 1; 1409a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1410ce263fc8SMatthias Ringwald } 1411ce263fc8SMatthias Ringwald } 1412ce263fc8SMatthias Ringwald 1413c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 14149c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1415a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1416a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1417a33eb0c4SMilanka Ringwald return; 1418a33eb0c4SMilanka Ringwald } 1419ce263fc8SMatthias Ringwald 1420505f1c30SMatthias Ringwald if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1421505f1c30SMatthias Ringwald (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1422a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 1; 1423a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1424ce263fc8SMatthias Ringwald } 1425ce263fc8SMatthias Ringwald } 1426ce263fc8SMatthias Ringwald 1427c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 14289c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1429a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1430a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1431a33eb0c4SMilanka Ringwald return; 1432a33eb0c4SMilanka Ringwald } 1433ce263fc8SMatthias Ringwald 1434505f1c30SMatthias Ringwald if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1435505f1c30SMatthias Ringwald (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1436a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 1; 1437a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1438ce263fc8SMatthias Ringwald } 1439ce263fc8SMatthias Ringwald } 1440ce263fc8SMatthias Ringwald 1441c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 14429c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1443a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1444a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1445a33eb0c4SMilanka Ringwald return; 1446a33eb0c4SMilanka Ringwald } 1447ce263fc8SMatthias Ringwald 1448505f1c30SMatthias Ringwald if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1449505f1c30SMatthias Ringwald (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1450a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 1; 1451a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1452ce263fc8SMatthias Ringwald } 1453ce263fc8SMatthias Ringwald } 1454ce263fc8SMatthias Ringwald 1455c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 14569c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1457a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1458a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1459a33eb0c4SMilanka Ringwald return; 1460a33eb0c4SMilanka Ringwald } 1461667ec068SMatthias Ringwald 1462505f1c30SMatthias Ringwald if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1463505f1c30SMatthias Ringwald (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1464a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1465a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 10 + index; 1466a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1467667ec068SMatthias Ringwald } 1468667ec068SMatthias Ringwald } 1469667ec068SMatthias Ringwald 1470c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 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 } 1476667ec068SMatthias Ringwald 1477505f1c30SMatthias Ringwald if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1478505f1c30SMatthias Ringwald (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1479a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1480a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 20 + index; 1481a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1482667ec068SMatthias Ringwald } 1483667ec068SMatthias Ringwald } 1484ce263fc8SMatthias Ringwald 1485c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 14869c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1487a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1488a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1489a33eb0c4SMilanka Ringwald return; 1490a33eb0c4SMilanka Ringwald } 1491ce263fc8SMatthias Ringwald 1492a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 1; 1493ce263fc8SMatthias Ringwald snprintf(phone_number, sizeof(phone_number), "%s", number); 1494a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1495ce263fc8SMatthias Ringwald } 1496ce263fc8SMatthias Ringwald 1497c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 14989c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1499a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1500a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1501a33eb0c4SMilanka Ringwald return; 1502a33eb0c4SMilanka Ringwald } 1503ce263fc8SMatthias Ringwald 1504a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 1; 1505a0ffb263SMatthias Ringwald hfp_connection->memory_id = memory_id; 1506a0ffb263SMatthias Ringwald 1507a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1508ce263fc8SMatthias Ringwald } 1509ce263fc8SMatthias Ringwald 1510c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 15119c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1512a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1513a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1514a33eb0c4SMilanka Ringwald return; 1515a33eb0c4SMilanka Ringwald } 1516ce263fc8SMatthias Ringwald 1517a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 1; 1518a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1519ce263fc8SMatthias Ringwald } 1520ce263fc8SMatthias Ringwald 1521c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 15229c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1523a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1524a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1525a33eb0c4SMilanka Ringwald return; 1526a33eb0c4SMilanka Ringwald } 1527ce263fc8SMatthias Ringwald 1528a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 1; 1529a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1530ce263fc8SMatthias Ringwald } 1531ce263fc8SMatthias Ringwald 1532ce263fc8SMatthias Ringwald 1533c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 15349c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1535a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1536a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1537a33eb0c4SMilanka Ringwald return; 1538a33eb0c4SMilanka Ringwald } 1539ce263fc8SMatthias Ringwald 1540a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 1; 1541a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1542ce263fc8SMatthias Ringwald } 1543ce263fc8SMatthias Ringwald 1544ce263fc8SMatthias Ringwald 1545c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 15469c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1547a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1548a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1549a33eb0c4SMilanka Ringwald return; 1550a33eb0c4SMilanka Ringwald } 1551ce263fc8SMatthias Ringwald 1552a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 1; 1553a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1554ce263fc8SMatthias Ringwald } 1555ce263fc8SMatthias Ringwald 1556c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 15579c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1558a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1559a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1560a33eb0c4SMilanka Ringwald return; 1561a33eb0c4SMilanka Ringwald } 1562ce263fc8SMatthias Ringwald 1563a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 1; 1564a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1565ce263fc8SMatthias Ringwald } 1566ce263fc8SMatthias Ringwald 1567ce263fc8SMatthias Ringwald 1568c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 15699c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1570a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1571a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1572a33eb0c4SMilanka Ringwald return; 1573a33eb0c4SMilanka Ringwald } 1574ce263fc8SMatthias Ringwald 1575a0ffb263SMatthias Ringwald hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1; 1576a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1577ce263fc8SMatthias Ringwald } 1578ce263fc8SMatthias Ringwald 1579c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 15809c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1581a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1582a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1583a33eb0c4SMilanka Ringwald return; 1584a33eb0c4SMilanka Ringwald } 1585ce263fc8SMatthias Ringwald 1586a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1587a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1588ce263fc8SMatthias Ringwald } 1589ce263fc8SMatthias Ringwald 1590c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){ 15919c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1592a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1593a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1594a33eb0c4SMilanka Ringwald return; 1595a33eb0c4SMilanka Ringwald } 1596ce263fc8SMatthias Ringwald 1597a0ffb263SMatthias Ringwald hfp_connection->hf_activate_voice_recognition_notification = 1; 1598a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1599ce263fc8SMatthias Ringwald } 1600ce263fc8SMatthias Ringwald 1601c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){ 16029c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1603a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1604a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1605a33eb0c4SMilanka Ringwald return; 1606a33eb0c4SMilanka Ringwald } 1607ce263fc8SMatthias Ringwald 1608a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_voice_recognition_notification = 1; 1609a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1610ce263fc8SMatthias Ringwald } 1611ce263fc8SMatthias Ringwald 1612c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 16139c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1614a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1615a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1616a33eb0c4SMilanka Ringwald return; 1617a33eb0c4SMilanka Ringwald } 1618c8626498SMilanka Ringwald 1619a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain == gain) return; 1620c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 1621a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1622a0ffb263SMatthias Ringwald return; 1623a0ffb263SMatthias Ringwald } 1624a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 1625a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 1626a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1627ce263fc8SMatthias Ringwald } 1628ce263fc8SMatthias Ringwald 1629c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 16309c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1631a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1632a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1633a33eb0c4SMilanka Ringwald return; 1634a33eb0c4SMilanka Ringwald } 1635c8626498SMilanka Ringwald 1636a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain == gain) return; 1637c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 1638a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1639a0ffb263SMatthias Ringwald return; 1640a0ffb263SMatthias Ringwald } 1641a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 1642a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 1643a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1644ce263fc8SMatthias Ringwald } 1645ce263fc8SMatthias Ringwald 1646c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 16479c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1648a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1649a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1650a33eb0c4SMilanka Ringwald return; 1651a33eb0c4SMilanka Ringwald } 1652a33eb0c4SMilanka Ringwald 1653a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = code; 1654a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1655ce263fc8SMatthias Ringwald } 1656ce263fc8SMatthias Ringwald 1657c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 16589c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1659a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1660a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1661a33eb0c4SMilanka Ringwald return; 1662a33eb0c4SMilanka Ringwald } 1663a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 1; 1664a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1665ce263fc8SMatthias Ringwald } 16663deb3ec6SMatthias Ringwald 1667c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 16689c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1669a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1670a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1671a33eb0c4SMilanka Ringwald return; 1672a33eb0c4SMilanka Ringwald } 1673a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 1; 1674a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1675667ec068SMatthias Ringwald } 1676667ec068SMatthias Ringwald 1677667ec068SMatthias Ringwald 1678c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 16799c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1680a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1681a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1682a33eb0c4SMilanka Ringwald return; 1683a33eb0c4SMilanka Ringwald } 1684a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1685a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '?'; 1686a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1687667ec068SMatthias Ringwald } 1688667ec068SMatthias Ringwald 1689c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 16909c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1691a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1692a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1693a33eb0c4SMilanka Ringwald return; 1694a33eb0c4SMilanka Ringwald } 1695a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1696a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '0'; 1697a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1698667ec068SMatthias Ringwald } 1699667ec068SMatthias Ringwald 1700c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){ 17019c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1702a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1703a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1704a33eb0c4SMilanka Ringwald return; 1705a33eb0c4SMilanka Ringwald } 1706a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1707a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '1'; 1708a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1709667ec068SMatthias Ringwald } 1710667ec068SMatthias Ringwald 1711c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){ 17129c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1713a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1714a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1715a33eb0c4SMilanka Ringwald return; 1716a33eb0c4SMilanka Ringwald } 1717a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1718a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '2'; 1719a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1720667ec068SMatthias Ringwald } 1721667ec068SMatthias Ringwald 1722c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){ 17239c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1724a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1725a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1726a33eb0c4SMilanka Ringwald return; 1727a33eb0c4SMilanka Ringwald } 1728a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 1; 1729a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1730667ec068SMatthias Ringwald } 1731667ec068SMatthias Ringwald 1732c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 17339c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1734a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1735a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1736a33eb0c4SMilanka Ringwald return; 1737a33eb0c4SMilanka Ringwald } 1738667ec068SMatthias Ringwald // find index for assigned number 1739667ec068SMatthias Ringwald int i; 1740667ec068SMatthias Ringwald for (i = 0; i < hfp_indicators_nr ; i++){ 1741667ec068SMatthias Ringwald if (hfp_indicators[i] == assigned_number){ 1742667ec068SMatthias Ringwald // set value 1743667ec068SMatthias Ringwald hfp_indicators_value[i] = value; 1744667ec068SMatthias Ringwald // mark for update 1745a0ffb263SMatthias Ringwald if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1746a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap |= (1<<i); 1747667ec068SMatthias Ringwald // send update 1748a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1749a0ffb263SMatthias Ringwald } 1750667ec068SMatthias Ringwald return; 1751667ec068SMatthias Ringwald } 1752667ec068SMatthias Ringwald } 1753667ec068SMatthias Ringwald } 1754667ec068SMatthias Ringwald 1755d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 1756d7f6b5cbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1757d7f6b5cbSMatthias Ringwald if (!hfp_connection) { 1758d7f6b5cbSMatthias Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1759d7f6b5cbSMatthias Ringwald return 0; 1760d7f6b5cbSMatthias Ringwald } 1761d7f6b5cbSMatthias Ringwald return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1762d7f6b5cbSMatthias Ringwald } 1763