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 8976cc1527SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){ 9076cc1527SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION); 9176cc1527SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION); 9276cc1527SMatthias Ringwald return hf && ag; 9376cc1527SMatthias Ringwald } 9476cc1527SMatthias Ringwald 9576cc1527SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){ 9676cc1527SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING); 9776cc1527SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING); 9876cc1527SMatthias Ringwald return hf && ag; 9976cc1527SMatthias Ringwald } 10076cc1527SMatthias Ringwald 10176cc1527SMatthias Ringwald 10276cc1527SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){ 10376cc1527SMatthias Ringwald int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS); 10476cc1527SMatthias Ringwald int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS); 10576cc1527SMatthias Ringwald return hf && ag; 10676cc1527SMatthias Ringwald } 10776cc1527SMatthias Ringwald 10876cc1527SMatthias Ringwald 1099c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){ 1109c9c64c1SMatthias Ringwald btstack_linked_list_iterator_t it; 1119c9c64c1SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1129c9c64c1SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1139c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 1149c9c64c1SMatthias Ringwald if (hfp_connection->acl_handle != handle) continue; 1159c9c64c1SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1169c9c64c1SMatthias Ringwald return hfp_connection; 1179c9c64c1SMatthias Ringwald } 1189c9c64c1SMatthias Ringwald return NULL; 1199c9c64c1SMatthias Ringwald } 1209c9c64c1SMatthias Ringwald 12176cc1527SMatthias Ringwald /* emit functinos */ 1223deb3ec6SMatthias Ringwald 12313839019SMatthias 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){ 124a0ffb263SMatthias Ringwald if (!callback) return; 125a0ffb263SMatthias Ringwald uint8_t event[31]; 126a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 127a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 128a0ffb263SMatthias Ringwald event[2] = event_subtype; 129a0ffb263SMatthias Ringwald event[3] = status; 130a0ffb263SMatthias Ringwald event[4] = bnip_type; 1316d9a4cd0SMilanka Ringwald uint16_t size = btstack_min(strlen(bnip_number), sizeof(event) - 6); 132a0ffb263SMatthias Ringwald strncpy((char*)&event[5], bnip_number, size); 133a0ffb263SMatthias Ringwald event[5 + size] = 0; 13413839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 135a0ffb263SMatthias Ringwald } 136a0ffb263SMatthias Ringwald 13713839019SMatthias 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){ 138a0ffb263SMatthias Ringwald if (!callback) return; 139a0ffb263SMatthias Ringwald uint8_t event[30]; 140a0ffb263SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 141a0ffb263SMatthias Ringwald event[1] = sizeof(event) - 2; 142a0ffb263SMatthias Ringwald event[2] = event_subtype; 143a0ffb263SMatthias Ringwald event[3] = bnip_type; 1446d9a4cd0SMilanka Ringwald uint16_t size = btstack_min(strlen(bnip_number), sizeof(event) - 5); 145a0ffb263SMatthias Ringwald strncpy((char*)&event[4], bnip_number, size); 146a0ffb263SMatthias Ringwald event[4 + size] = 0; 14713839019SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 148a0ffb263SMatthias Ringwald } 149a0ffb263SMatthias Ringwald 1502b99f6edSMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, hfp_connection_t * connection){ 151a0ffb263SMatthias Ringwald if (!callback) return; 1520aee97efSMilanka Ringwald uint8_t event[36]; 1530aee97efSMilanka Ringwald int pos = 0; 1540aee97efSMilanka Ringwald event[pos++] = HCI_EVENT_HFP_META; 1550aee97efSMilanka Ringwald event[pos++] = sizeof(event) - 2; 1560aee97efSMilanka Ringwald event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS; 1572b99f6edSMatthias Ringwald event[pos++] = connection->clcc_idx; 1582b99f6edSMatthias Ringwald event[pos++] = connection->clcc_dir; 1592b99f6edSMatthias Ringwald event[pos++] = connection->clcc_status; 1602b99f6edSMatthias Ringwald event[pos++] = connection->clcc_mode; 1612b99f6edSMatthias Ringwald event[pos++] = connection->clcc_mpty; 1622b99f6edSMatthias Ringwald event[pos++] = connection->bnip_type; 1632b99f6edSMatthias Ringwald uint16_t size = btstack_min(strlen(connection->bnip_number), sizeof(event) - pos); 1642b99f6edSMatthias Ringwald strncpy((char*)&event[pos], connection->bnip_number, size); 1650aee97efSMilanka Ringwald pos += size; 1660aee97efSMilanka Ringwald event[pos++] = 0; 1670aee97efSMilanka Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, pos); 168a0ffb263SMatthias Ringwald } 169a0ffb263SMatthias Ringwald 17076cc1527SMatthias Ringwald 17176cc1527SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){ 17276cc1527SMatthias Ringwald if (!callback) return; 17376cc1527SMatthias Ringwald uint8_t event[10+HFP_MAX_INDICATOR_DESC_SIZE+1]; 17476cc1527SMatthias Ringwald int pos = 0; 17576cc1527SMatthias Ringwald event[pos++] = HCI_EVENT_HFP_META; 17676cc1527SMatthias Ringwald event[pos++] = sizeof(event) - 2; 17776cc1527SMatthias Ringwald event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED; 17876cc1527SMatthias Ringwald event[pos++] = indicator.index; 17976cc1527SMatthias Ringwald event[pos++] = indicator.status; 18076cc1527SMatthias Ringwald event[pos++] = indicator.min_range; 18176cc1527SMatthias Ringwald event[pos++] = indicator.max_range; 18276cc1527SMatthias Ringwald event[pos++] = indicator.mandatory; 18376cc1527SMatthias Ringwald event[pos++] = indicator.enabled; 18476cc1527SMatthias Ringwald event[pos++] = indicator.status_changed; 18576cc1527SMatthias Ringwald strncpy((char*)&event[pos], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE); 18676cc1527SMatthias Ringwald pos += HFP_MAX_INDICATOR_DESC_SIZE; 18776cc1527SMatthias Ringwald event[pos] = 0; 18876cc1527SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1893deb3ec6SMatthias Ringwald } 1903deb3ec6SMatthias Ringwald 19176cc1527SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){ 19276cc1527SMatthias Ringwald if (!callback) return; 19376cc1527SMatthias Ringwald uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1]; 19476cc1527SMatthias Ringwald event[0] = HCI_EVENT_HFP_META; 19576cc1527SMatthias Ringwald event[1] = sizeof(event) - 2; 19676cc1527SMatthias Ringwald event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED; 19776cc1527SMatthias Ringwald event[3] = network_operator.mode; 19876cc1527SMatthias Ringwald event[4] = network_operator.format; 19976cc1527SMatthias Ringwald strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE); 20076cc1527SMatthias Ringwald event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0; 20176cc1527SMatthias Ringwald (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event)); 2023deb3ec6SMatthias Ringwald } 2033deb3ec6SMatthias Ringwald 20476cc1527SMatthias Ringwald /* send commands */ 20589425bfcSMilanka Ringwald 20689425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){ 2073deb3ec6SMatthias Ringwald char buffer[20]; 20889425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "AT%s\r\n", cmd); 20989425bfcSMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 21089425bfcSMilanka Ringwald } 21189425bfcSMilanka Ringwald 21289425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){ 21389425bfcSMilanka Ringwald char buffer[20]; 21489425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "AT%s%s\r\n", cmd, mark); 21589425bfcSMilanka Ringwald return send_str_over_rfcomm(cid, buffer); 21689425bfcSMilanka Ringwald } 21789425bfcSMilanka Ringwald 21886da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){ 21989425bfcSMilanka Ringwald char buffer[40]; 22089425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%d\r\n", cmd, value); 2213deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2223deb3ec6SMatthias Ringwald } 2233deb3ec6SMatthias Ringwald 2243deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){ 2253deb3ec6SMatthias Ringwald char buffer[30]; 22689425bfcSMilanka Ringwald const int size = sizeof(buffer); 22789425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS); 22889425bfcSMilanka Ringwald offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr); 22989425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n"); 2303deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2313deb3ec6SMatthias Ringwald } 2323deb3ec6SMatthias Ringwald 2333deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){ 2343deb3ec6SMatthias Ringwald char buffer[50]; 23589425bfcSMilanka Ringwald const int size = sizeof(buffer); 23689425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS); 23789425bfcSMilanka Ringwald offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr); 23889425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n"); 2393deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2403deb3ec6SMatthias Ringwald } 2413deb3ec6SMatthias Ringwald 2423deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){ 2433deb3ec6SMatthias Ringwald char buffer[30]; 24489425bfcSMilanka Ringwald const int size = sizeof(buffer); 24589425bfcSMilanka Ringwald int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR); 24689425bfcSMilanka Ringwald offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr); 24789425bfcSMilanka Ringwald offset += snprintf(buffer+offset, size-offset, "\r\n"); 2483deb3ec6SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 2493deb3ec6SMatthias Ringwald } 2503deb3ec6SMatthias Ringwald 25189425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){ 2523deb3ec6SMatthias Ringwald char buffer[20]; 25389425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate); 254ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 255ce263fc8SMatthias Ringwald } 256ce263fc8SMatthias Ringwald 257ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){ 258ce263fc8SMatthias Ringwald char buffer[40]; 25989425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number); 260ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 261ce263fc8SMatthias Ringwald } 262ce263fc8SMatthias Ringwald 263a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){ 264ce263fc8SMatthias Ringwald char buffer[40]; 26589425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id); 266ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 267ce263fc8SMatthias Ringwald } 268ce263fc8SMatthias Ringwald 269f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){ 27089425bfcSMilanka Ringwald char buffer[40]; 27189425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number); 272ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 273ce263fc8SMatthias Ringwald } 274ce263fc8SMatthias Ringwald 275ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){ 276ce263fc8SMatthias Ringwald char buffer[20]; 27789425bfcSMilanka Ringwald snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code); 278ce263fc8SMatthias Ringwald return send_str_over_rfcomm(cid, buffer); 279ce263fc8SMatthias Ringwald } 280ce263fc8SMatthias Ringwald 28197d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){ 282560b7bdbSMatthias Ringwald return send_str_over_rfcomm(cid, (char *) "ATA\r\n"); 28397d2cadbSMatthias Ringwald } 28497d2cadbSMatthias Ringwald 28589425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){ 28689425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features); 28789425bfcSMilanka Ringwald } 28889425bfcSMilanka Ringwald 28989425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){ 29089425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?"); 29189425bfcSMilanka Ringwald } 29289425bfcSMilanka Ringwald 29389425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){ 29489425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?"); 29589425bfcSMilanka Ringwald } 29689425bfcSMilanka Ringwald 29789425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){ 29889425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?"); 29989425bfcSMilanka Ringwald } 30089425bfcSMilanka Ringwald 30189425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){ 30289425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?"); 30389425bfcSMilanka Ringwald } 30489425bfcSMilanka Ringwald 30589425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){ 30689425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?"); 30789425bfcSMilanka Ringwald } 30889425bfcSMilanka Ringwald 30989425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){ 31089425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0"); 31189425bfcSMilanka Ringwald } 31289425bfcSMilanka Ringwald 31389425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){ 31489425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?"); 31589425bfcSMilanka Ringwald } 31689425bfcSMilanka Ringwald 31789425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){ 31889425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP); 31989425bfcSMilanka Ringwald } 32089425bfcSMilanka Ringwald 32189425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){ 32289425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain); 32389425bfcSMilanka Ringwald } 32489425bfcSMilanka Ringwald 32589425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){ 32689425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain); 32789425bfcSMilanka Ringwald } 32889425bfcSMilanka Ringwald 32989425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){ 33089425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate); 33189425bfcSMilanka Ringwald } 33289425bfcSMilanka Ringwald 33389425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){ 33489425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate); 33589425bfcSMilanka Ringwald } 33689425bfcSMilanka Ringwald 33789425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){ 33889425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate); 33989425bfcSMilanka Ringwald } 34089425bfcSMilanka Ringwald 34189425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){ 34289425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate); 34389425bfcSMilanka Ringwald } 34489425bfcSMilanka Ringwald 34589425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){ 34689425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec); 34789425bfcSMilanka Ringwald } 34889425bfcSMilanka Ringwald 34989425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){ 35089425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable); 35189425bfcSMilanka Ringwald } 35289425bfcSMilanka Ringwald 35389425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){ 35489425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER); 35589425bfcSMilanka Ringwald } 35689425bfcSMilanka Ringwald 35789425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){ 35889425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL); 35989425bfcSMilanka Ringwald } 36089425bfcSMilanka Ringwald 361ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){ 36289425bfcSMilanka Ringwald return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1"); 363ce263fc8SMatthias Ringwald } 364ce263fc8SMatthias Ringwald 365667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){ 36689425bfcSMilanka Ringwald return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS); 367667ec068SMatthias Ringwald } 368667ec068SMatthias Ringwald 36976cc1527SMatthias Ringwald /* state machines */ 3703deb3ec6SMatthias Ringwald 371a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){ 372a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 373a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 374aa4dd815SMatthias Ringwald int done = 1; 3753deb3ec6SMatthias Ringwald 376a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 3773deb3ec6SMatthias Ringwald case HFP_EXCHANGE_SUPPORTED_FEATURES: 378d715cf51SMatthias Ringwald hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr); 379a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES; 380a0ffb263SMatthias Ringwald hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid); 3813deb3ec6SMatthias Ringwald break; 3823deb3ec6SMatthias Ringwald case HFP_NOTIFY_ON_CODECS: 383a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS; 384a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 3853deb3ec6SMatthias Ringwald break; 3863deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS: 387a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS; 388a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid); 3893deb3ec6SMatthias Ringwald break; 3903deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INDICATORS_STATUS: 391a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS; 392a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid); 3933deb3ec6SMatthias Ringwald break; 3943deb3ec6SMatthias Ringwald case HFP_ENABLE_INDICATORS_STATUS_UPDATE: 395a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE; 396a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1); 3973deb3ec6SMatthias Ringwald break; 3983deb3ec6SMatthias Ringwald case HFP_RETRIEVE_CAN_HOLD_CALL: 399a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL; 400a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid); 4013deb3ec6SMatthias Ringwald break; 4023deb3ec6SMatthias Ringwald case HFP_LIST_GENERIC_STATUS_INDICATORS: 403a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS; 404a0ffb263SMatthias Ringwald hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4053deb3ec6SMatthias Ringwald break; 4063deb3ec6SMatthias Ringwald case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS: 407a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS; 408a0ffb263SMatthias Ringwald hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4093deb3ec6SMatthias Ringwald break; 4103deb3ec6SMatthias Ringwald case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 411a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 412a0ffb263SMatthias Ringwald hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid); 4133deb3ec6SMatthias Ringwald break; 4143deb3ec6SMatthias Ringwald default: 415aa4dd815SMatthias Ringwald done = 0; 4163deb3ec6SMatthias Ringwald break; 4173deb3ec6SMatthias Ringwald } 4183deb3ec6SMatthias Ringwald return done; 4193deb3ec6SMatthias Ringwald } 4203deb3ec6SMatthias Ringwald 421ce263fc8SMatthias Ringwald 422a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){ 423a0ffb263SMatthias Ringwald if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0; 424a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 425ce263fc8SMatthias Ringwald 426ce263fc8SMatthias Ringwald int done = 0; 427a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 428a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 429ce263fc8SMatthias Ringwald done = 1; 430a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators); 431ce263fc8SMatthias Ringwald return done; 432ce263fc8SMatthias Ringwald }; 433a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators){ 434a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 435ce263fc8SMatthias Ringwald done = 1; 436a0ffb263SMatthias Ringwald hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid, 437a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap, 438a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_nr); 439ce263fc8SMatthias Ringwald return done; 440ce263fc8SMatthias Ringwald } 441ce263fc8SMatthias Ringwald 442a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 443ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SET_FORMAT: 444a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK; 445a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 446a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid); 447ce263fc8SMatthias Ringwald return 1; 448ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_SEND_QUERY: 449a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT; 450a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 451a0ffb263SMatthias Ringwald hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid); 452ce263fc8SMatthias Ringwald return 1; 453ce263fc8SMatthias Ringwald default: 454ce263fc8SMatthias Ringwald break; 455ce263fc8SMatthias Ringwald } 456ce263fc8SMatthias Ringwald 457a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 458a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 459ce263fc8SMatthias Ringwald done = 1; 460a0ffb263SMatthias Ringwald hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report); 461ce263fc8SMatthias Ringwald return done; 462ce263fc8SMatthias Ringwald } 463ce263fc8SMatthias Ringwald 464ce263fc8SMatthias Ringwald return done; 465ce263fc8SMatthias Ringwald } 466ce263fc8SMatthias Ringwald 467a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){ 468ce263fc8SMatthias Ringwald 469a0ffb263SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 470ce263fc8SMatthias Ringwald 471*332ca98fSMatthias Ringwald if (hfp_connection->trigger_codec_exchange){ 472*332ca98fSMatthias Ringwald hfp_connection->trigger_codec_exchange = 0; 473ce263fc8SMatthias Ringwald 474a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 475a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 476a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 477ce263fc8SMatthias Ringwald 478a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE; 479a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 480a0ffb263SMatthias Ringwald hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid); 481*332ca98fSMatthias Ringwald return 1; 482*332ca98fSMatthias Ringwald } 483*332ca98fSMatthias Ringwald 484*332ca98fSMatthias Ringwald switch (hfp_connection->command){ 485ce263fc8SMatthias Ringwald 4866a7f44bdSMilanka Ringwald case HFP_CMD_AG_SUGGESTED_CODEC:{ 4876a7f44bdSMilanka Ringwald if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){ 488a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 489a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 490a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC; 4910e0be203SMatthias Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 492c1ab6cc1SMatthias Ringwald log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD"); 493fcb08cdbSMilanka Ringwald hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed); 494ce263fc8SMatthias Ringwald } else { 495a0ffb263SMatthias Ringwald hfp_connection->codec_confirmed = 0; 496a0ffb263SMatthias Ringwald hfp_connection->suggested_codec = 0; 497a0ffb263SMatthias Ringwald hfp_connection->negotiated_codec = 0; 498a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 499a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 500a0ffb263SMatthias Ringwald hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid); 501ce263fc8SMatthias Ringwald 502ce263fc8SMatthias Ringwald } 503ce263fc8SMatthias Ringwald break; 5046a7f44bdSMilanka Ringwald } 505ce263fc8SMatthias Ringwald default: 506ce263fc8SMatthias Ringwald break; 507ce263fc8SMatthias Ringwald } 508ce263fc8SMatthias Ringwald return 0; 509ce263fc8SMatthias Ringwald } 510ce263fc8SMatthias Ringwald 511a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){ 512505f1c30SMatthias Ringwald if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) || 513505f1c30SMatthias Ringwald (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0; 514ce263fc8SMatthias Ringwald 51564f19dedSMilanka Ringwald if (hfp_connection->release_audio_connection){ 516a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_SCO_DISCONNECTED; 517a0ffb263SMatthias Ringwald hfp_connection->release_audio_connection = 0; 518a0ffb263SMatthias Ringwald gap_disconnect(hfp_connection->sco_handle); 519ce263fc8SMatthias Ringwald return 1; 520ce263fc8SMatthias Ringwald } 521ce263fc8SMatthias Ringwald 522a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0; 523ce263fc8SMatthias Ringwald 524ce263fc8SMatthias Ringwald // run codecs exchange 525a0ffb263SMatthias Ringwald int done = codecs_exchange_state_machine(hfp_connection); 526ce263fc8SMatthias Ringwald if (done) return 1; 527ce263fc8SMatthias Ringwald 52838200c1dSMilanka Ringwald if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0; 52938200c1dSMilanka Ringwald if (hfp_connection->establish_audio_connection){ 53038200c1dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 53138200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 0; 53238200c1dSMilanka Ringwald hfp_setup_synchronous_connection(hfp_connection); 53338200c1dSMilanka Ringwald return 1; 53438200c1dSMilanka Ringwald } 53538200c1dSMilanka Ringwald 536ce263fc8SMatthias Ringwald return 0; 537ce263fc8SMatthias Ringwald } 538ce263fc8SMatthias Ringwald 53938200c1dSMilanka Ringwald 540a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){ 541eaf2b0a1SMatthias Ringwald 542eaf2b0a1SMatthias Ringwald if (hfp_connection->ok_pending) return 0; 543eaf2b0a1SMatthias Ringwald 544a0ffb263SMatthias Ringwald if (hfp_connection->hf_answer_incoming_call){ 545a0ffb263SMatthias Ringwald hfp_hf_cmd_ata(hfp_connection->rfcomm_cid); 546a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 0; 547ce263fc8SMatthias Ringwald return 1; 548ce263fc8SMatthias Ringwald } 549ce263fc8SMatthias Ringwald return 0; 550ce263fc8SMatthias Ringwald } 551ce263fc8SMatthias Ringwald 552a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){ 5537522e673SMatthias Ringwald 55476cc1527SMatthias Ringwald btstack_assert(hfp_connection != NULL); 55576cc1527SMatthias Ringwald btstack_assert(hfp_connection->local_role == HFP_ROLE_HF); 55676cc1527SMatthias Ringwald 55776cc1527SMatthias Ringwald // during SDP query, RFCOMM CID is not set 55876cc1527SMatthias Ringwald if (hfp_connection->rfcomm_cid == 0) return; 55922387625SMatthias Ringwald 560b72c4a9eSMatthias Ringwald if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){ 561b72c4a9eSMatthias Ringwald 562c169df2fSMatthias Ringwald bool eSCO = hfp_connection->hf_accept_sco == 2; 563b72c4a9eSMatthias Ringwald hfp_connection->hf_accept_sco = 0; 5647522e673SMatthias Ringwald 5657522e673SMatthias Ringwald // notify about codec selection if not done already 5667522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == 0){ 5677522e673SMatthias Ringwald hfp_connection->negotiated_codec = HFP_CODEC_CVSD; 5687522e673SMatthias Ringwald } 5697522e673SMatthias Ringwald 5707522e673SMatthias Ringwald // remote supported feature eSCO is set if link type is eSCO 5717522e673SMatthias Ringwald // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms, 5727522e673SMatthias Ringwald uint16_t max_latency; 5737522e673SMatthias Ringwald uint8_t retransmission_effort; 5747522e673SMatthias Ringwald uint16_t packet_types; 5757522e673SMatthias Ringwald 576c169df2fSMatthias Ringwald if (eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){ 5777522e673SMatthias Ringwald max_latency = 0x000c; 5787522e673SMatthias Ringwald retransmission_effort = 0x02; 579352a0504SMatthias Ringwald // eSCO: EV3 and 2-EV3 580352a0504SMatthias Ringwald packet_types = 0x0048; 5817522e673SMatthias Ringwald } else { 5827522e673SMatthias Ringwald max_latency = 0xffff; 5837522e673SMatthias Ringwald retransmission_effort = 0xff; 584352a0504SMatthias Ringwald // sco: HV1 and HV3 585352a0504SMatthias Ringwald packet_types = 0x005; 5867522e673SMatthias Ringwald } 5877522e673SMatthias Ringwald 588352a0504SMatthias Ringwald // mSBC only allows for transparent data 5897522e673SMatthias Ringwald uint16_t sco_voice_setting = hci_get_sco_voice_setting(); 5907522e673SMatthias Ringwald if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){ 5917522e673SMatthias Ringwald sco_voice_setting = 0x0043; // Transparent data 5927522e673SMatthias Ringwald } 5937522e673SMatthias Ringwald 594352a0504SMatthias Ringwald // filter packet types 595352a0504SMatthias Ringwald packet_types &= hfp_get_sco_packet_types(); 596352a0504SMatthias Ringwald 597352a0504SMatthias Ringwald // bits 6-9 are 'don't allow' 598352a0504SMatthias Ringwald packet_types ^= 0x3c0; 599352a0504SMatthias Ringwald 600991c26beSMatthias Ringwald log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting); 6017522e673SMatthias Ringwald hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency, 6027522e673SMatthias Ringwald sco_voice_setting, retransmission_effort, packet_types); 6037522e673SMatthias Ringwald return; 6047522e673SMatthias Ringwald } 6057522e673SMatthias Ringwald 606d4dd47ffSMatthias Ringwald if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) { 607d4dd47ffSMatthias Ringwald rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid); 608d4dd47ffSMatthias Ringwald return; 609d4dd47ffSMatthias Ringwald } 610a0ffb263SMatthias Ringwald int done = hfp_hf_run_for_context_service_level_connection(hfp_connection); 611ce263fc8SMatthias Ringwald if (!done){ 612a0ffb263SMatthias Ringwald done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection); 613ce263fc8SMatthias Ringwald } 614ce263fc8SMatthias Ringwald if (!done){ 615a0ffb263SMatthias Ringwald done = hfp_hf_run_for_audio_connection(hfp_connection); 616ce263fc8SMatthias Ringwald } 617ce263fc8SMatthias Ringwald if (!done){ 618a0ffb263SMatthias Ringwald done = call_setup_state_machine(hfp_connection); 619ce263fc8SMatthias Ringwald } 620ce263fc8SMatthias Ringwald 6211016a228SMatthias Ringwald // don't send a new command while ok still pending 6221016a228SMatthias Ringwald if (hfp_connection->ok_pending) return; 6231016a228SMatthias Ringwald 624a0ffb263SMatthias Ringwald if (hfp_connection->send_microphone_gain){ 625a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 0; 626a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 627a0ffb263SMatthias Ringwald hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain); 628ce263fc8SMatthias Ringwald return; 629ce263fc8SMatthias Ringwald } 630ce263fc8SMatthias Ringwald 631a0ffb263SMatthias Ringwald if (hfp_connection->send_speaker_gain){ 632a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 0; 633a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 634a0ffb263SMatthias Ringwald hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain); 635ce263fc8SMatthias Ringwald return; 636ce263fc8SMatthias Ringwald } 637ce263fc8SMatthias Ringwald 638a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_calling_line_notification){ 639a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 0; 640a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 641a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0); 642ce263fc8SMatthias Ringwald return; 643ce263fc8SMatthias Ringwald } 644ce263fc8SMatthias Ringwald 645a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_calling_line_notification){ 646a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 0; 647a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 648a0ffb263SMatthias Ringwald hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1); 649ce263fc8SMatthias Ringwald return; 650ce263fc8SMatthias Ringwald } 651ce263fc8SMatthias Ringwald 652a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){ 653a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0; 654a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 655a0ffb263SMatthias Ringwald hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0); 656ce263fc8SMatthias Ringwald return; 657ce263fc8SMatthias Ringwald } 658ce263fc8SMatthias Ringwald 659a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){ 660a0ffb263SMatthias Ringwald hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0; 661a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 662a0ffb263SMatthias Ringwald hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1); 663ce263fc8SMatthias Ringwald return; 664ce263fc8SMatthias Ringwald } 665ce263fc8SMatthias Ringwald 666a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_voice_recognition_notification){ 667a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_voice_recognition_notification = 0; 668a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 669a0ffb263SMatthias Ringwald hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0); 670ce263fc8SMatthias Ringwald return; 671ce263fc8SMatthias Ringwald } 672ce263fc8SMatthias Ringwald 673a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_voice_recognition_notification){ 674a0ffb263SMatthias Ringwald hfp_connection->hf_activate_voice_recognition_notification = 0; 675a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 676a0ffb263SMatthias Ringwald hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1); 677ce263fc8SMatthias Ringwald return; 678ce263fc8SMatthias Ringwald } 679ce263fc8SMatthias Ringwald 680ce263fc8SMatthias Ringwald 681a0ffb263SMatthias Ringwald if (hfp_connection->hf_deactivate_call_waiting_notification){ 682a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 0; 683a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 684a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0); 685ce263fc8SMatthias Ringwald return; 686ce263fc8SMatthias Ringwald } 687ce263fc8SMatthias Ringwald 688a0ffb263SMatthias Ringwald if (hfp_connection->hf_activate_call_waiting_notification){ 689a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 0; 690a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 691a0ffb263SMatthias Ringwald hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1); 692ce263fc8SMatthias Ringwald return; 693ce263fc8SMatthias Ringwald } 694ce263fc8SMatthias Ringwald 695a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_outgoing_call){ 696a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 0; 697a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 698a0ffb263SMatthias Ringwald hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid); 699ce263fc8SMatthias Ringwald return; 700ce263fc8SMatthias Ringwald } 701ce263fc8SMatthias Ringwald 702a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_memory_dialing){ 703a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 0; 704a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 705a0ffb263SMatthias Ringwald hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id); 706ce263fc8SMatthias Ringwald return; 707ce263fc8SMatthias Ringwald } 708ce263fc8SMatthias Ringwald 709a0ffb263SMatthias Ringwald if (hfp_connection->hf_initiate_redial_last_number){ 710a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 0; 711a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 712a0ffb263SMatthias Ringwald hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid); 713ce263fc8SMatthias Ringwald return; 714ce263fc8SMatthias Ringwald } 715ce263fc8SMatthias Ringwald 716a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chup){ 717a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 0; 718a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 719a0ffb263SMatthias Ringwald hfp_hf_send_chup(hfp_connection->rfcomm_cid); 720ce263fc8SMatthias Ringwald return; 721ce263fc8SMatthias Ringwald } 722ce263fc8SMatthias Ringwald 723a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_0){ 724a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 0; 725a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 726a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0); 727ce263fc8SMatthias Ringwald return; 728ce263fc8SMatthias Ringwald } 729ce263fc8SMatthias Ringwald 730a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_1){ 731a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 0; 732a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 733a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1); 734ce263fc8SMatthias Ringwald return; 735ce263fc8SMatthias Ringwald } 736ce263fc8SMatthias Ringwald 737a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_2){ 738a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 0; 739a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 740a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2); 741ce263fc8SMatthias Ringwald return; 742ce263fc8SMatthias Ringwald } 743ce263fc8SMatthias Ringwald 744a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_3){ 745a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 0; 746a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 747a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3); 748ce263fc8SMatthias Ringwald return; 749ce263fc8SMatthias Ringwald } 750ce263fc8SMatthias Ringwald 751a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_4){ 752a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 0; 753a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 754a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4); 755ce263fc8SMatthias Ringwald return; 756ce263fc8SMatthias Ringwald } 757ce263fc8SMatthias Ringwald 758a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_chld_x){ 759a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 0; 760a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 761a0ffb263SMatthias Ringwald hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index); 762667ec068SMatthias Ringwald return; 763667ec068SMatthias Ringwald } 764667ec068SMatthias Ringwald 765a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_dtmf_code){ 766a0ffb263SMatthias Ringwald char code = hfp_connection->hf_send_dtmf_code; 767a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = 0; 768a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 769a0ffb263SMatthias Ringwald hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code); 770ce263fc8SMatthias Ringwald return; 771ce263fc8SMatthias Ringwald } 772ce263fc8SMatthias Ringwald 773a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_binp){ 774a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 0; 775a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 776a0ffb263SMatthias Ringwald hfp_hf_send_binp(hfp_connection->rfcomm_cid); 777ce263fc8SMatthias Ringwald return; 778ce263fc8SMatthias Ringwald } 779ce263fc8SMatthias Ringwald 780a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_clcc){ 781a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 0; 782a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 783a0ffb263SMatthias Ringwald hfp_hf_send_clcc(hfp_connection->rfcomm_cid); 784667ec068SMatthias Ringwald return; 785667ec068SMatthias Ringwald } 786667ec068SMatthias Ringwald 787a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_rrh){ 788a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 0; 789667ec068SMatthias Ringwald char buffer[20]; 790a0ffb263SMatthias Ringwald switch (hfp_connection->hf_send_rrh_command){ 791667ec068SMatthias Ringwald case '?': 792667ec068SMatthias Ringwald sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD); 793a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 794667ec068SMatthias Ringwald return; 795667ec068SMatthias Ringwald case '0': 796667ec068SMatthias Ringwald case '1': 797667ec068SMatthias Ringwald case '2': 798a0ffb263SMatthias Ringwald sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command); 799a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 800667ec068SMatthias Ringwald return; 801667ec068SMatthias Ringwald default: 802667ec068SMatthias Ringwald break; 803667ec068SMatthias Ringwald } 804667ec068SMatthias Ringwald return; 805667ec068SMatthias Ringwald } 806667ec068SMatthias Ringwald 807a0ffb263SMatthias Ringwald if (hfp_connection->hf_send_cnum){ 808a0ffb263SMatthias Ringwald hfp_connection->hf_send_cnum = 0; 809667ec068SMatthias Ringwald char buffer[20]; 810667ec068SMatthias Ringwald sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION); 811a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 812667ec068SMatthias Ringwald return; 813667ec068SMatthias Ringwald } 814667ec068SMatthias Ringwald 815667ec068SMatthias Ringwald // update HF indicators 816a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_update_bitmap){ 817667ec068SMatthias Ringwald int i; 818667ec068SMatthias Ringwald for (i=0;i<hfp_indicators_nr;i++){ 819a0ffb263SMatthias Ringwald if (get_bit(hfp_connection->generic_status_update_bitmap, i)){ 820a0ffb263SMatthias Ringwald if (hfp_connection->generic_status_indicators[i].state){ 821a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 1; 822a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0); 823667ec068SMatthias Ringwald char buffer[30]; 824ecb7d461SMatthias Ringwald sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]); 825a0ffb263SMatthias Ringwald send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer); 826667ec068SMatthias Ringwald } else { 82760ebb071SMilanka Ringwald log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]); 828667ec068SMatthias Ringwald } 829667ec068SMatthias Ringwald return; 830667ec068SMatthias Ringwald } 831667ec068SMatthias Ringwald } 832667ec068SMatthias Ringwald } 833667ec068SMatthias Ringwald 834ce263fc8SMatthias Ringwald if (done) return; 835ce263fc8SMatthias Ringwald // deal with disconnect 836a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 837ce263fc8SMatthias Ringwald case HFP_W2_DISCONNECT_RFCOMM: 838a0ffb263SMatthias Ringwald hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED; 839a0ffb263SMatthias Ringwald rfcomm_disconnect(hfp_connection->rfcomm_cid); 840ce263fc8SMatthias Ringwald break; 841ce263fc8SMatthias Ringwald 842ce263fc8SMatthias Ringwald default: 843ce263fc8SMatthias Ringwald break; 844ce263fc8SMatthias Ringwald } 845ce263fc8SMatthias Ringwald } 846ce263fc8SMatthias Ringwald 847a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){ 848a0ffb263SMatthias Ringwald hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED; 8496a7f44bdSMilanka Ringwald 850ca59be51SMatthias Ringwald hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr); 8517522e673SMatthias Ringwald 852667ec068SMatthias Ringwald // restore volume settings 853a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = hfp_hf_speaker_gain; 854a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 855ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain); 856a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = hfp_hf_microphone_gain; 857a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 858ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain); 859667ec068SMatthias Ringwald // enable all indicators 860667ec068SMatthias Ringwald int i; 861667ec068SMatthias Ringwald for (i=0;i<hfp_indicators_nr;i++){ 862a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i]; 863a0ffb263SMatthias Ringwald hfp_connection->generic_status_indicators[i].state = 1; 864667ec068SMatthias Ringwald } 865ce263fc8SMatthias Ringwald } 866ce263fc8SMatthias Ringwald 867a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){ 868a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 869a0ffb263SMatthias Ringwald switch (hfp_connection->state){ 8703deb3ec6SMatthias Ringwald case HFP_W4_EXCHANGE_SUPPORTED_FEATURES: 871a0ffb263SMatthias Ringwald if (has_codec_negotiation_feature(hfp_connection)){ 872a0ffb263SMatthias Ringwald hfp_connection->state = HFP_NOTIFY_ON_CODECS; 8733deb3ec6SMatthias Ringwald break; 8743deb3ec6SMatthias Ringwald } 875a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 8763deb3ec6SMatthias Ringwald break; 8773deb3ec6SMatthias Ringwald 8783deb3ec6SMatthias Ringwald case HFP_W4_NOTIFY_ON_CODECS: 879a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS; 8803deb3ec6SMatthias Ringwald break; 8813deb3ec6SMatthias Ringwald 8823deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS: 883a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS; 8843deb3ec6SMatthias Ringwald break; 8853deb3ec6SMatthias Ringwald 8863deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INDICATORS_STATUS: 887a0ffb263SMatthias Ringwald hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE; 8883deb3ec6SMatthias Ringwald break; 8893deb3ec6SMatthias Ringwald 8903deb3ec6SMatthias Ringwald case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE: 891a0ffb263SMatthias Ringwald if (has_call_waiting_and_3way_calling_feature(hfp_connection)){ 892a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL; 8933deb3ec6SMatthias Ringwald break; 8943deb3ec6SMatthias Ringwald } 895a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 896a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 8973deb3ec6SMatthias Ringwald break; 8983deb3ec6SMatthias Ringwald } 899a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9003deb3ec6SMatthias Ringwald break; 9013deb3ec6SMatthias Ringwald 9023deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_CAN_HOLD_CALL: 903a0ffb263SMatthias Ringwald if (has_hf_indicators_feature(hfp_connection)){ 904a0ffb263SMatthias Ringwald hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS; 9053deb3ec6SMatthias Ringwald break; 9063deb3ec6SMatthias Ringwald } 907a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9083deb3ec6SMatthias Ringwald break; 9093deb3ec6SMatthias Ringwald 9103deb3ec6SMatthias Ringwald case HFP_W4_LIST_GENERIC_STATUS_INDICATORS: 911a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS; 9123deb3ec6SMatthias Ringwald break; 9133deb3ec6SMatthias Ringwald 9143deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS: 915a0ffb263SMatthias Ringwald hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS; 9163deb3ec6SMatthias Ringwald break; 9173deb3ec6SMatthias Ringwald 9183deb3ec6SMatthias Ringwald case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS: 919a0ffb263SMatthias Ringwald hfp_ag_slc_established(hfp_connection); 9203deb3ec6SMatthias Ringwald break; 921ce263fc8SMatthias Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 922a0ffb263SMatthias Ringwald if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){ 923a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = 0xFF; 924ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 925ce263fc8SMatthias Ringwald break; 926ce263fc8SMatthias Ringwald } 9273deb3ec6SMatthias Ringwald 928a0ffb263SMatthias Ringwald if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){ 929a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 0; 930ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0); 931ce263fc8SMatthias Ringwald break; 9323deb3ec6SMatthias Ringwald } 9333deb3ec6SMatthias Ringwald 934a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 935ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK: 936a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 937ce263fc8SMatthias Ringwald break; 938ce263fc8SMatthias Ringwald case HPF_HF_QUERY_OPERATOR_W4_RESULT: 939a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET; 940ca59be51SMatthias Ringwald hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator); 941ce263fc8SMatthias Ringwald break; 942ce263fc8SMatthias Ringwald default: 943ce263fc8SMatthias Ringwald break; 9443deb3ec6SMatthias Ringwald } 945ce263fc8SMatthias Ringwald 946a0ffb263SMatthias Ringwald if (hfp_connection->enable_extended_audio_gateway_error_report){ 947a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = 0; 948ce263fc8SMatthias Ringwald break; 9493deb3ec6SMatthias Ringwald } 9503deb3ec6SMatthias Ringwald 951a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 952aa4dd815SMatthias Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 953a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC; 9543deb3ec6SMatthias Ringwald break; 955ce263fc8SMatthias Ringwald case HFP_CODECS_HF_CONFIRMED_CODEC: 956a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 957ce263fc8SMatthias Ringwald break; 9583deb3ec6SMatthias Ringwald default: 9593deb3ec6SMatthias Ringwald break; 9603deb3ec6SMatthias Ringwald } 9613deb3ec6SMatthias Ringwald break; 9623deb3ec6SMatthias Ringwald default: 9633deb3ec6SMatthias Ringwald break; 9643deb3ec6SMatthias Ringwald } 9653deb3ec6SMatthias Ringwald 9663deb3ec6SMatthias Ringwald // done 967a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 9683deb3ec6SMatthias Ringwald } 9693deb3ec6SMatthias Ringwald 970b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) { 9714562e2a2SMatthias Ringwald uint16_t i; 9724562e2a2SMatthias Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 9734562e2a2SMatthias Ringwald if (hfp_connection->ag_indicators[i].status_changed) { 9744562e2a2SMatthias Ringwald if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){ 9754562e2a2SMatthias Ringwald hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status; 9764562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){ 9774562e2a2SMatthias Ringwald hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status; 9784562e2a2SMatthias Ringwald // avoid set but not used warning 9794562e2a2SMatthias Ringwald (void) hfp_callheld_status; 9804562e2a2SMatthias Ringwald } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){ 9814562e2a2SMatthias Ringwald hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status; 9824562e2a2SMatthias Ringwald } 9834562e2a2SMatthias Ringwald hfp_connection->ag_indicators[i].status_changed = 0; 9844562e2a2SMatthias Ringwald hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]); 9854562e2a2SMatthias Ringwald break; 9864562e2a2SMatthias Ringwald } 9874562e2a2SMatthias Ringwald } 9884562e2a2SMatthias Ringwald } 9894562e2a2SMatthias Ringwald 990426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){ 991186dd3d2SMatthias Ringwald int value; 992186dd3d2SMatthias Ringwald int i; 993a0ffb263SMatthias Ringwald switch (hfp_connection->command){ 994667ec068SMatthias Ringwald case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION: 995a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 996ca59be51SMatthias Ringwald hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number); 997667ec068SMatthias Ringwald break; 998667ec068SMatthias Ringwald case HFP_CMD_RESPONSE_AND_HOLD_STATUS: 999a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1000ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0])); 1001667ec068SMatthias Ringwald break; 1002667ec068SMatthias Ringwald case HFP_CMD_LIST_CURRENT_CALLS: 1003a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10042b99f6edSMatthias Ringwald hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection); 1005667ec068SMatthias Ringwald break; 1006ce263fc8SMatthias Ringwald case HFP_CMD_SET_SPEAKER_GAIN: 1007a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10082308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1009667ec068SMatthias Ringwald hfp_hf_speaker_gain = value; 1010ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value); 1011ce263fc8SMatthias Ringwald break; 1012ce263fc8SMatthias Ringwald case HFP_CMD_SET_MICROPHONE_GAIN: 1013a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 10142308e108SMilanka Ringwald value = btstack_atoi((char*)hfp_connection->line_buffer); 1015667ec068SMatthias Ringwald hfp_hf_microphone_gain = value; 1016ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value); 1017ce263fc8SMatthias Ringwald break; 1018ce263fc8SMatthias Ringwald case HFP_CMD_AG_SENT_PHONE_NUMBER: 1019a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1020ca59be51SMatthias Ringwald hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number); 1021a0ffb263SMatthias Ringwald break; 1022a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE: 1023a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1024ca59be51SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1025a0ffb263SMatthias Ringwald break; 1026a0ffb263SMatthias Ringwald case HFP_CMD_AG_SENT_CLIP_INFORMATION: 1027a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1028ca59be51SMatthias Ringwald hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number); 1029ce263fc8SMatthias Ringwald break; 1030ce263fc8SMatthias Ringwald case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR: 1031a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1032a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 1033a0ffb263SMatthias Ringwald hfp_connection->extended_audio_gateway_error = 0; 1034ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value); 1035ce263fc8SMatthias Ringwald break; 1036ce263fc8SMatthias Ringwald case HFP_CMD_ERROR: 1037a0ffb263SMatthias Ringwald hfp_connection->ok_pending = 0; 1038a0ffb263SMatthias Ringwald hfp_reset_context_flags(hfp_connection); 1039a0ffb263SMatthias Ringwald hfp_connection->command = HFP_CMD_NONE; 104090244c92SMilanka Ringwald 104190244c92SMilanka Ringwald switch (hfp_connection->state){ 104290244c92SMilanka Ringwald case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED: 104390244c92SMilanka Ringwald switch (hfp_connection->codecs_state){ 104490244c92SMilanka Ringwald case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE: 104590244c92SMilanka Ringwald hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec); 104690244c92SMilanka Ringwald return; 104790244c92SMilanka Ringwald default: 104890244c92SMilanka Ringwald break; 104990244c92SMilanka Ringwald } 105090244c92SMilanka Ringwald default: 105190244c92SMilanka Ringwald break; 105290244c92SMilanka Ringwald } 1053ca59be51SMatthias Ringwald hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1); 1054ce263fc8SMatthias Ringwald break; 1055ce263fc8SMatthias Ringwald case HFP_CMD_OK: 1056a0ffb263SMatthias Ringwald hfp_hf_switch_on_ok(hfp_connection); 1057ce263fc8SMatthias Ringwald break; 1058ce263fc8SMatthias Ringwald case HFP_CMD_RING: 1059ca59be51SMatthias Ringwald hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING); 1060ce263fc8SMatthias Ringwald break; 1061ce263fc8SMatthias Ringwald case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS: 10624562e2a2SMatthias Ringwald hfp_hf_handle_transfer_ag_indicator_status(hfp_connection); 1063ce263fc8SMatthias Ringwald break; 1064c741b032SMilanka Ringwald case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS: 1065c741b032SMilanka Ringwald for (i = 0; i < hfp_connection->ag_indicators_nr; i++){ 1066c741b032SMilanka Ringwald hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]); 1067c741b032SMilanka Ringwald } 1068c741b032SMilanka Ringwald hfp_connection->command = HFP_CMD_NONE; 1069c741b032SMilanka Ringwald break; 1070ce263fc8SMatthias Ringwald default: 1071ce263fc8SMatthias Ringwald break; 10723deb3ec6SMatthias Ringwald } 10730cef86faSMatthias Ringwald } 1074426f9988SMatthias Ringwald 107576cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){ 107676cc1527SMatthias Ringwald return (byte == '\n') || (byte == '\r'); 107776cc1527SMatthias Ringwald } 107876cc1527SMatthias Ringwald 1079426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1080426f9988SMatthias Ringwald UNUSED(packet_type); // ok: only called with RFCOMM_DATA_PACKET 1081426f9988SMatthias Ringwald // assertion: size >= 1 as rfcomm.c does not deliver empty packets 1082426f9988SMatthias Ringwald if (size < 1) return; 1083426f9988SMatthias Ringwald 1084426f9988SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel); 1085426f9988SMatthias Ringwald if (!hfp_connection) return; 1086426f9988SMatthias Ringwald 1087426f9988SMatthias Ringwald hfp_log_rfcomm_message("HFP_HF_RX", packet, size); 1088426f9988SMatthias Ringwald 1089426f9988SMatthias Ringwald // process messages byte-wise 1090426f9988SMatthias Ringwald int pos; 1091426f9988SMatthias Ringwald for (pos = 0; pos < size; pos++){ 1092426f9988SMatthias Ringwald hfp_parse(hfp_connection, packet[pos], 1); 1093426f9988SMatthias Ringwald 1094426f9988SMatthias Ringwald // parse until end of line "\r\n" 1095426f9988SMatthias Ringwald if (!hfp_parser_is_end_of_line(packet[pos])) continue; 1096426f9988SMatthias Ringwald 1097426f9988SMatthias Ringwald hfp_hf_handle_rfcomm_command(hfp_connection); 1098426f9988SMatthias Ringwald } 1099a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11003deb3ec6SMatthias Ringwald } 11013deb3ec6SMatthias Ringwald 11020cb5b971SMatthias Ringwald static void hfp_run(void){ 1103665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1104665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, hfp_get_connections()); 1105665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1106a0ffb263SMatthias Ringwald hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it); 110722387625SMatthias Ringwald if (hfp_connection->local_role != HFP_ROLE_HF) continue; 1108a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11093deb3ec6SMatthias Ringwald } 11103deb3ec6SMatthias Ringwald } 11113deb3ec6SMatthias Ringwald 1112e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 11133deb3ec6SMatthias Ringwald switch (packet_type){ 11143deb3ec6SMatthias Ringwald case RFCOMM_DATA_PACKET: 1115426f9988SMatthias Ringwald hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size); 11163deb3ec6SMatthias Ringwald break; 11173deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1118d4dd47ffSMatthias Ringwald if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){ 1119d4dd47ffSMatthias Ringwald uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet); 1120d4dd47ffSMatthias Ringwald hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid)); 1121d4dd47ffSMatthias Ringwald return; 1122d4dd47ffSMatthias Ringwald } 112327950165SMatthias Ringwald hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF); 1124202c8a4cSMatthias Ringwald break; 11253deb3ec6SMatthias Ringwald default: 11263deb3ec6SMatthias Ringwald break; 11273deb3ec6SMatthias Ringwald } 11283deb3ec6SMatthias Ringwald hfp_run(); 11293deb3ec6SMatthias Ringwald } 11303deb3ec6SMatthias Ringwald 1131405014fbSMatthias Ringwald static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1132405014fbSMatthias Ringwald hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF); 113338200c1dSMilanka Ringwald 113438200c1dSMilanka Ringwald // allow for sco established -> ring transition and sco retry 113538200c1dSMilanka Ringwald if (packet_type != HCI_EVENT_PACKET) return; 113638200c1dSMilanka Ringwald if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return; 113738200c1dSMilanka Ringwald hfp_run(); 1138405014fbSMatthias Ringwald } 1139405014fbSMatthias Ringwald 1140a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){ 1141520c92d5SMatthias Ringwald hfp_init(); 1142d63c37a1SMatthias Ringwald 1143405014fbSMatthias Ringwald hci_event_callback_registration.callback = &hci_event_packet_handler; 114427950165SMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 114527950165SMatthias Ringwald 1146e9c22d4eSMatthias Ringwald rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 0xffff); 114727950165SMatthias Ringwald 114827950165SMatthias Ringwald // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us 1149e9c22d4eSMatthias Ringwald hfp_set_hf_rfcomm_packet_handler(&rfcomm_packet_handler); 115027950165SMatthias Ringwald 11511b005648SMatthias Ringwald hfp_set_hf_run_for_context(hfp_run_for_context); 1152d68dcce1SMatthias Ringwald 1153a0ffb263SMatthias Ringwald hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES; 1154a0ffb263SMatthias Ringwald hfp_codecs_nr = 0; 1155a0ffb263SMatthias Ringwald hfp_indicators_nr = 0; 1156a0ffb263SMatthias Ringwald hfp_hf_speaker_gain = 9; 1157a0ffb263SMatthias Ringwald hfp_hf_microphone_gain = 9; 1158a0ffb263SMatthias Ringwald } 1159a0ffb263SMatthias Ringwald 1160a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){ 11613deb3ec6SMatthias Ringwald if (codecs_nr > HFP_MAX_NUM_CODECS){ 1162a0ffb263SMatthias Ringwald log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS); 11633deb3ec6SMatthias Ringwald return; 11643deb3ec6SMatthias Ringwald } 11653deb3ec6SMatthias Ringwald 11663deb3ec6SMatthias Ringwald hfp_codecs_nr = codecs_nr; 11673deb3ec6SMatthias Ringwald int i; 11683deb3ec6SMatthias Ringwald for (i=0; i<codecs_nr; i++){ 11693deb3ec6SMatthias Ringwald hfp_codecs[i] = codecs[i]; 11703deb3ec6SMatthias Ringwald } 11713deb3ec6SMatthias Ringwald } 11723deb3ec6SMatthias Ringwald 1173a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){ 11743deb3ec6SMatthias Ringwald hfp_supported_features = supported_features; 1175a0ffb263SMatthias Ringwald } 11763deb3ec6SMatthias Ringwald 1177a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){ 11783deb3ec6SMatthias Ringwald hfp_indicators_nr = indicators_nr; 11793deb3ec6SMatthias Ringwald int i; 1180a0ffb263SMatthias Ringwald for (i = 0; i < hfp_indicators_nr ; i++){ 11813deb3ec6SMatthias Ringwald hfp_indicators[i] = indicators[i]; 11823deb3ec6SMatthias Ringwald } 11833deb3ec6SMatthias Ringwald } 11843deb3ec6SMatthias Ringwald 11853deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){ 1186323d3000SMatthias Ringwald hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF); 11873deb3ec6SMatthias Ringwald } 11883deb3ec6SMatthias Ringwald 1189c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){ 11909c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1191a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1192a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1193a33eb0c4SMilanka Ringwald return; 1194a33eb0c4SMilanka Ringwald } 1195a0ffb263SMatthias Ringwald hfp_release_service_level_connection(hfp_connection); 1196a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 11973deb3ec6SMatthias Ringwald } 11983deb3ec6SMatthias Ringwald 1199c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){ 12009c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1201a0ffb263SMatthias Ringwald if (!hfp_connection) { 1202a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12033deb3ec6SMatthias Ringwald return; 12043deb3ec6SMatthias Ringwald } 1205a0ffb263SMatthias Ringwald hfp_connection->enable_status_update_for_ag_indicators = enable; 1206a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12073deb3ec6SMatthias Ringwald } 12083deb3ec6SMatthias Ringwald 1209c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1210c8626498SMilanka Ringwald hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1); 1211ce263fc8SMatthias Ringwald } 1212ce263fc8SMatthias Ringwald 1213c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){ 1214c8626498SMilanka Ringwald hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0); 1215ce263fc8SMatthias Ringwald } 1216ce263fc8SMatthias Ringwald 12173deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format 1218c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){ 12199c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1220a0ffb263SMatthias Ringwald if (!hfp_connection) { 1221a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12223deb3ec6SMatthias Ringwald return; 12233deb3ec6SMatthias Ringwald } 1224a0ffb263SMatthias Ringwald hfp_connection->change_status_update_for_individual_ag_indicators = 1; 1225a0ffb263SMatthias Ringwald hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap; 1226a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12273deb3ec6SMatthias Ringwald } 12283deb3ec6SMatthias Ringwald 1229c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){ 12309c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1231a0ffb263SMatthias Ringwald if (!hfp_connection) { 1232a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12333deb3ec6SMatthias Ringwald return; 12343deb3ec6SMatthias Ringwald } 1235a0ffb263SMatthias Ringwald switch (hfp_connection->hf_query_operator_state){ 1236ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET: 1237a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT; 1238ce263fc8SMatthias Ringwald break; 1239ce263fc8SMatthias Ringwald case HFP_HF_QUERY_OPERATOR_FORMAT_SET: 1240a0ffb263SMatthias Ringwald hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY; 1241ce263fc8SMatthias Ringwald break; 1242ce263fc8SMatthias Ringwald default: 1243ce263fc8SMatthias Ringwald break; 1244ce263fc8SMatthias Ringwald } 1245a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12463deb3ec6SMatthias Ringwald } 12473deb3ec6SMatthias Ringwald 1248c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){ 12499c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1250a0ffb263SMatthias Ringwald if (!hfp_connection) { 1251a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 12523deb3ec6SMatthias Ringwald return; 12533deb3ec6SMatthias Ringwald } 1254a0ffb263SMatthias Ringwald hfp_connection->enable_extended_audio_gateway_error_report = enable; 1255a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 12563deb3ec6SMatthias Ringwald } 12573deb3ec6SMatthias Ringwald 1258ce263fc8SMatthias Ringwald 1259c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1260c8626498SMilanka Ringwald hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1); 1261ce263fc8SMatthias Ringwald } 1262ce263fc8SMatthias Ringwald 1263c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){ 1264c8626498SMilanka Ringwald hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0); 1265ce263fc8SMatthias Ringwald } 1266ce263fc8SMatthias Ringwald 126738200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){ 126838200c1dSMilanka Ringwald return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) && (hfp_supported_features & (1<<HFP_HFSF_ESCO_S4)); 126938200c1dSMilanka Ringwald } 1270ce263fc8SMatthias Ringwald 1271c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){ 12729c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1273a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1274a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1275a33eb0c4SMilanka Ringwald return; 1276a33eb0c4SMilanka Ringwald } 1277a0ffb263SMatthias Ringwald hfp_connection->establish_audio_connection = 0; 1278ce263fc8SMatthias Ringwald 1279a0ffb263SMatthias Ringwald if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return; 1280a0ffb263SMatthias Ringwald if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return; 12813deb3ec6SMatthias Ringwald 128238200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 0; 128338200c1dSMilanka Ringwald hfp_connection->establish_audio_connection = 1; 1284a0ffb263SMatthias Ringwald if (!has_codec_negotiation_feature(hfp_connection)){ 1285*332ca98fSMatthias Ringwald log_info("no codec negotiation feature, using NBS"); 1286a0ffb263SMatthias Ringwald hfp_connection->codecs_state = HFP_CODECS_EXCHANGED; 128738200c1dSMilanka Ringwald hfp_connection->suggested_codec = HFP_CODEC_CVSD; 128838200c1dSMilanka Ringwald hfp_connection->codec_confirmed = hfp_connection->suggested_codec; 128938200c1dSMilanka Ringwald hfp_connection->negotiated_codec = hfp_connection->suggested_codec; 129038200c1dSMilanka Ringwald hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection)); 129138200c1dSMilanka Ringwald hfp_connection->state = HFP_W4_SCO_CONNECTED; 1292ce263fc8SMatthias Ringwald } else { 1293a0ffb263SMatthias Ringwald switch (hfp_connection->codecs_state){ 1294aa4dd815SMatthias Ringwald case HFP_CODECS_W4_AG_COMMON_CODEC: 1295aa4dd815SMatthias Ringwald break; 1296aa4dd815SMatthias Ringwald default: 129738200c1dSMilanka Ringwald hfp_connection->trigger_codec_exchange = 1; 1298aa4dd815SMatthias Ringwald break; 12993deb3ec6SMatthias Ringwald } 1300ce263fc8SMatthias Ringwald } 1301ce263fc8SMatthias Ringwald 1302a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 13033deb3ec6SMatthias Ringwald } 13043deb3ec6SMatthias Ringwald 1305c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){ 13069c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1307a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1308a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1309a33eb0c4SMilanka Ringwald return; 1310a33eb0c4SMilanka Ringwald } 1311a0ffb263SMatthias Ringwald hfp_release_audio_connection(hfp_connection); 1312a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 13133deb3ec6SMatthias Ringwald } 13143deb3ec6SMatthias Ringwald 1315c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){ 13169c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1317a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1318a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1319a33eb0c4SMilanka Ringwald return; 1320a33eb0c4SMilanka Ringwald } 1321ce263fc8SMatthias Ringwald 1322ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1323a0ffb263SMatthias Ringwald hfp_connection->hf_answer_incoming_call = 1; 1324a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1325ce263fc8SMatthias Ringwald } else { 1326ce263fc8SMatthias Ringwald log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status); 1327ce263fc8SMatthias Ringwald } 1328ce263fc8SMatthias Ringwald } 1329ce263fc8SMatthias Ringwald 1330c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){ 13319c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1332a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1333a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1334a33eb0c4SMilanka Ringwald return; 1335a33eb0c4SMilanka Ringwald } 1336a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 1337a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1338ce263fc8SMatthias Ringwald } 1339ce263fc8SMatthias Ringwald 1340c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){ 13419c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1342a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1343a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1344a33eb0c4SMilanka Ringwald return; 1345a33eb0c4SMilanka Ringwald } 1346ce263fc8SMatthias Ringwald 1347ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1348a0ffb263SMatthias Ringwald hfp_connection->hf_send_chup = 1; 1349a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1350ce263fc8SMatthias Ringwald } 1351ce263fc8SMatthias Ringwald } 1352ce263fc8SMatthias Ringwald 1353c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){ 13549c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1355a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1356a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1357a33eb0c4SMilanka Ringwald return; 1358a33eb0c4SMilanka Ringwald } 1359ce263fc8SMatthias Ringwald 1360ce263fc8SMatthias Ringwald if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){ 1361a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_0 = 1; 1362a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1363ce263fc8SMatthias Ringwald } 1364ce263fc8SMatthias Ringwald } 1365ce263fc8SMatthias Ringwald 1366c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){ 13679c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1368a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1369a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1370a33eb0c4SMilanka Ringwald return; 1371a33eb0c4SMilanka Ringwald } 1372ce263fc8SMatthias Ringwald 1373505f1c30SMatthias Ringwald if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1374505f1c30SMatthias Ringwald (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1375a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_1 = 1; 1376a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1377ce263fc8SMatthias Ringwald } 1378ce263fc8SMatthias Ringwald } 1379ce263fc8SMatthias Ringwald 1380c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){ 13819c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1382a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1383a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1384a33eb0c4SMilanka Ringwald return; 1385a33eb0c4SMilanka Ringwald } 1386ce263fc8SMatthias Ringwald 1387505f1c30SMatthias Ringwald if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1388505f1c30SMatthias Ringwald (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1389a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_2 = 1; 1390a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1391ce263fc8SMatthias Ringwald } 1392ce263fc8SMatthias Ringwald } 1393ce263fc8SMatthias Ringwald 1394c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){ 13959c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1396a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1397a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1398a33eb0c4SMilanka Ringwald return; 1399a33eb0c4SMilanka Ringwald } 1400ce263fc8SMatthias Ringwald 1401505f1c30SMatthias Ringwald if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1402505f1c30SMatthias Ringwald (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1403a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_3 = 1; 1404a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1405ce263fc8SMatthias Ringwald } 1406ce263fc8SMatthias Ringwald } 1407ce263fc8SMatthias Ringwald 1408c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){ 14099c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1410a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1411a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1412a33eb0c4SMilanka Ringwald return; 1413a33eb0c4SMilanka Ringwald } 1414ce263fc8SMatthias Ringwald 1415505f1c30SMatthias Ringwald if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1416505f1c30SMatthias Ringwald (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1417a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_4 = 1; 1418a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1419ce263fc8SMatthias Ringwald } 1420ce263fc8SMatthias Ringwald } 1421ce263fc8SMatthias Ringwald 1422c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){ 14239c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1424a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1425a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1426a33eb0c4SMilanka Ringwald return; 1427a33eb0c4SMilanka Ringwald } 1428667ec068SMatthias Ringwald 1429505f1c30SMatthias Ringwald if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1430505f1c30SMatthias Ringwald (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1431a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1432a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 10 + index; 1433a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1434667ec068SMatthias Ringwald } 1435667ec068SMatthias Ringwald } 1436667ec068SMatthias Ringwald 1437c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){ 14389c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1439a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1440a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1441a33eb0c4SMilanka Ringwald return; 1442a33eb0c4SMilanka Ringwald } 1443667ec068SMatthias Ringwald 1444505f1c30SMatthias Ringwald if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) || 1445505f1c30SMatthias Ringwald (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){ 1446a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x = 1; 1447a0ffb263SMatthias Ringwald hfp_connection->hf_send_chld_x_index = 20 + index; 1448a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1449667ec068SMatthias Ringwald } 1450667ec068SMatthias Ringwald } 1451ce263fc8SMatthias Ringwald 1452c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){ 14539c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1454a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1455a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1456a33eb0c4SMilanka Ringwald return; 1457a33eb0c4SMilanka Ringwald } 1458ce263fc8SMatthias Ringwald 1459a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_outgoing_call = 1; 1460ce263fc8SMatthias Ringwald snprintf(phone_number, sizeof(phone_number), "%s", number); 1461a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1462ce263fc8SMatthias Ringwald } 1463ce263fc8SMatthias Ringwald 1464c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){ 14659c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1466a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1467a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1468a33eb0c4SMilanka Ringwald return; 1469a33eb0c4SMilanka Ringwald } 1470ce263fc8SMatthias Ringwald 1471a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_memory_dialing = 1; 1472a0ffb263SMatthias Ringwald hfp_connection->memory_id = memory_id; 1473a0ffb263SMatthias Ringwald 1474a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1475ce263fc8SMatthias Ringwald } 1476ce263fc8SMatthias Ringwald 1477c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){ 14789c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1479a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1480a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1481a33eb0c4SMilanka Ringwald return; 1482a33eb0c4SMilanka Ringwald } 1483ce263fc8SMatthias Ringwald 1484a0ffb263SMatthias Ringwald hfp_connection->hf_initiate_redial_last_number = 1; 1485a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1486ce263fc8SMatthias Ringwald } 1487ce263fc8SMatthias Ringwald 1488c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){ 14899c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1490a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1491a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1492a33eb0c4SMilanka Ringwald return; 1493a33eb0c4SMilanka Ringwald } 1494ce263fc8SMatthias Ringwald 1495a0ffb263SMatthias Ringwald hfp_connection->hf_activate_call_waiting_notification = 1; 1496a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1497ce263fc8SMatthias Ringwald } 1498ce263fc8SMatthias Ringwald 1499ce263fc8SMatthias Ringwald 1500c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){ 15019c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1502a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1503a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1504a33eb0c4SMilanka Ringwald return; 1505a33eb0c4SMilanka Ringwald } 1506ce263fc8SMatthias Ringwald 1507a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_call_waiting_notification = 1; 1508a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1509ce263fc8SMatthias Ringwald } 1510ce263fc8SMatthias Ringwald 1511ce263fc8SMatthias Ringwald 1512c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){ 15139c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1514a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1515a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1516a33eb0c4SMilanka Ringwald return; 1517a33eb0c4SMilanka Ringwald } 1518ce263fc8SMatthias Ringwald 1519a0ffb263SMatthias Ringwald hfp_connection->hf_activate_calling_line_notification = 1; 1520a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1521ce263fc8SMatthias Ringwald } 1522ce263fc8SMatthias Ringwald 1523c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){ 15249c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1525a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1526a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1527a33eb0c4SMilanka Ringwald return; 1528a33eb0c4SMilanka Ringwald } 1529ce263fc8SMatthias Ringwald 1530a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_calling_line_notification = 1; 1531a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1532ce263fc8SMatthias Ringwald } 1533ce263fc8SMatthias Ringwald 1534ce263fc8SMatthias Ringwald 1535c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 15369c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1537a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1538a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1539a33eb0c4SMilanka Ringwald return; 1540a33eb0c4SMilanka Ringwald } 1541ce263fc8SMatthias Ringwald 1542a0ffb263SMatthias Ringwald hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1; 1543a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1544ce263fc8SMatthias Ringwald } 1545ce263fc8SMatthias Ringwald 1546c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){ 15479c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1548a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1549a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1550a33eb0c4SMilanka Ringwald return; 1551a33eb0c4SMilanka Ringwald } 1552ce263fc8SMatthias Ringwald 1553a0ffb263SMatthias Ringwald hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1; 1554a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1555ce263fc8SMatthias Ringwald } 1556ce263fc8SMatthias Ringwald 1557c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){ 15589c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1559a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1560a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1561a33eb0c4SMilanka Ringwald return; 1562a33eb0c4SMilanka Ringwald } 1563ce263fc8SMatthias Ringwald 1564a0ffb263SMatthias Ringwald hfp_connection->hf_activate_voice_recognition_notification = 1; 1565a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1566ce263fc8SMatthias Ringwald } 1567ce263fc8SMatthias Ringwald 1568c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(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_deactivate_voice_recognition_notification = 1; 1576a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1577ce263fc8SMatthias Ringwald } 1578ce263fc8SMatthias Ringwald 1579c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){ 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 } 1585c8626498SMilanka Ringwald 1586a0ffb263SMatthias Ringwald if (hfp_connection->microphone_gain == gain) return; 1587c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 1588a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1589a0ffb263SMatthias Ringwald return; 1590a0ffb263SMatthias Ringwald } 1591a0ffb263SMatthias Ringwald hfp_connection->microphone_gain = gain; 1592a0ffb263SMatthias Ringwald hfp_connection->send_microphone_gain = 1; 1593a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1594ce263fc8SMatthias Ringwald } 1595ce263fc8SMatthias Ringwald 1596c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){ 15979c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1598a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1599a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1600a33eb0c4SMilanka Ringwald return; 1601a33eb0c4SMilanka Ringwald } 1602c8626498SMilanka Ringwald 1603a0ffb263SMatthias Ringwald if (hfp_connection->speaker_gain == gain) return; 1604c1ab6cc1SMatthias Ringwald if ((gain < 0) || (gain > 15)){ 1605a0ffb263SMatthias Ringwald log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain); 1606a0ffb263SMatthias Ringwald return; 1607a0ffb263SMatthias Ringwald } 1608a0ffb263SMatthias Ringwald hfp_connection->speaker_gain = gain; 1609a0ffb263SMatthias Ringwald hfp_connection->send_speaker_gain = 1; 1610a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1611ce263fc8SMatthias Ringwald } 1612ce263fc8SMatthias Ringwald 1613c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){ 16149c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1615a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1616a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1617a33eb0c4SMilanka Ringwald return; 1618a33eb0c4SMilanka Ringwald } 1619a33eb0c4SMilanka Ringwald 1620a0ffb263SMatthias Ringwald hfp_connection->hf_send_dtmf_code = code; 1621a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1622ce263fc8SMatthias Ringwald } 1623ce263fc8SMatthias Ringwald 1624c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){ 16259c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1626a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1627a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1628a33eb0c4SMilanka Ringwald return; 1629a33eb0c4SMilanka Ringwald } 1630a0ffb263SMatthias Ringwald hfp_connection->hf_send_binp = 1; 1631a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1632ce263fc8SMatthias Ringwald } 16333deb3ec6SMatthias Ringwald 1634c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){ 16359c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1636a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1637a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1638a33eb0c4SMilanka Ringwald return; 1639a33eb0c4SMilanka Ringwald } 1640a0ffb263SMatthias Ringwald hfp_connection->hf_send_clcc = 1; 1641a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1642667ec068SMatthias Ringwald } 1643667ec068SMatthias Ringwald 1644667ec068SMatthias Ringwald 1645c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){ 16469c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1647a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1648a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1649a33eb0c4SMilanka Ringwald return; 1650a33eb0c4SMilanka Ringwald } 1651a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1652a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '?'; 1653a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1654667ec068SMatthias Ringwald } 1655667ec068SMatthias Ringwald 1656c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){ 16579c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1658a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1659a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1660a33eb0c4SMilanka Ringwald return; 1661a33eb0c4SMilanka Ringwald } 1662a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh = 1; 1663a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '0'; 1664a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1665667ec068SMatthias Ringwald } 1666667ec068SMatthias Ringwald 1667c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(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_rrh = 1; 1674a0ffb263SMatthias Ringwald hfp_connection->hf_send_rrh_command = '1'; 1675a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1676667ec068SMatthias Ringwald } 1677667ec068SMatthias Ringwald 1678c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(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 = '2'; 1686a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1687667ec068SMatthias Ringwald } 1688667ec068SMatthias Ringwald 1689c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(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_cnum = 1; 1696a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1697667ec068SMatthias Ringwald } 1698667ec068SMatthias Ringwald 1699c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){ 17009c9c64c1SMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1701a33eb0c4SMilanka Ringwald if (!hfp_connection) { 1702a33eb0c4SMilanka Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1703a33eb0c4SMilanka Ringwald return; 1704a33eb0c4SMilanka Ringwald } 1705667ec068SMatthias Ringwald // find index for assigned number 1706667ec068SMatthias Ringwald int i; 1707667ec068SMatthias Ringwald for (i = 0; i < hfp_indicators_nr ; i++){ 1708667ec068SMatthias Ringwald if (hfp_indicators[i] == assigned_number){ 1709667ec068SMatthias Ringwald // set value 1710667ec068SMatthias Ringwald hfp_indicators_value[i] = value; 1711667ec068SMatthias Ringwald // mark for update 1712a0ffb263SMatthias Ringwald if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){ 1713a0ffb263SMatthias Ringwald hfp_connection->generic_status_update_bitmap |= (1<<i); 1714667ec068SMatthias Ringwald // send update 1715a0ffb263SMatthias Ringwald hfp_run_for_context(hfp_connection); 1716a0ffb263SMatthias Ringwald } 1717667ec068SMatthias Ringwald return; 1718667ec068SMatthias Ringwald } 1719667ec068SMatthias Ringwald } 1720667ec068SMatthias Ringwald } 1721667ec068SMatthias Ringwald 1722d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){ 1723d7f6b5cbSMatthias Ringwald hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle); 1724d7f6b5cbSMatthias Ringwald if (!hfp_connection) { 1725d7f6b5cbSMatthias Ringwald log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle); 1726d7f6b5cbSMatthias Ringwald return 0; 1727d7f6b5cbSMatthias Ringwald } 1728d7f6b5cbSMatthias Ringwald return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE); 1729d7f6b5cbSMatthias Ringwald } 173076cc1527SMatthias Ringwald 173176cc1527SMatthias 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){ 173276cc1527SMatthias Ringwald if (!name){ 173376cc1527SMatthias Ringwald name = default_hfp_hf_service_name; 173476cc1527SMatthias Ringwald } 173576cc1527SMatthias Ringwald hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name); 173676cc1527SMatthias Ringwald 173776cc1527SMatthias Ringwald // Construct SupportedFeatures for SDP bitmap: 173876cc1527SMatthias Ringwald // 173976cc1527SMatthias Ringwald // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values 174076cc1527SMatthias Ringwald // of the Bits 0 to 4 of the unsolicited result code +BRSF" 174176cc1527SMatthias Ringwald // 174276cc1527SMatthias Ringwald // Wide band speech (bit 5) requires Codec negotiation 174376cc1527SMatthias Ringwald // 174476cc1527SMatthias Ringwald uint16_t sdp_features = supported_features & 0x1f; 174576cc1527SMatthias Ringwald if (wide_band_speech && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){ 174676cc1527SMatthias Ringwald sdp_features |= 1 << 5; 174776cc1527SMatthias Ringwald } 174876cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311); // Hands-Free Profile - SupportedFeatures 174976cc1527SMatthias Ringwald de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features); 175076cc1527SMatthias Ringwald } 175176cc1527SMatthias Ringwald 175276cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){ 175376cc1527SMatthias Ringwald if (callback == NULL){ 175476cc1527SMatthias Ringwald log_error("hfp_hf_register_packet_handler called with NULL callback"); 175576cc1527SMatthias Ringwald return; 175676cc1527SMatthias Ringwald } 175776cc1527SMatthias Ringwald hfp_hf_callback = callback; 175876cc1527SMatthias Ringwald hfp_set_hf_callback(callback); 175976cc1527SMatthias Ringwald }