xref: /btstack/src/classic/hfp_hf.c (revision 6a7f44bd8d4d9216f1f2d6e5fe1ba86636669eb7)
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  */
373deb3ec6SMatthias Ringwald 
383deb3ec6SMatthias Ringwald // *****************************************************************************
393deb3ec6SMatthias Ringwald //
40fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit
413deb3ec6SMatthias Ringwald //
423deb3ec6SMatthias Ringwald // *****************************************************************************
433deb3ec6SMatthias Ringwald 
447907f069SMatthias Ringwald #include "btstack_config.h"
453deb3ec6SMatthias Ringwald 
463deb3ec6SMatthias Ringwald #include <stdint.h>
473deb3ec6SMatthias Ringwald #include <stdio.h>
483deb3ec6SMatthias Ringwald #include <stdlib.h>
493deb3ec6SMatthias Ringwald #include <string.h>
503deb3ec6SMatthias Ringwald 
5159c6af15SMatthias Ringwald #include "btstack_debug.h"
523deb3ec6SMatthias Ringwald #include "btstack_memory.h"
5359c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5459c6af15SMatthias Ringwald #include "classic/core.h"
5559c6af15SMatthias Ringwald #include "classic/hfp.h"
5659c6af15SMatthias Ringwald #include "classic/hfp_hf.h"
57efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
58746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
59023f2764SMatthias Ringwald #include "classic/sdp_util.h"
6059c6af15SMatthias Ringwald #include "hci.h"
6159c6af15SMatthias Ringwald #include "hci_cmd.h"
6259c6af15SMatthias Ringwald #include "hci_dump.h"
6359c6af15SMatthias Ringwald #include "l2cap.h"
643deb3ec6SMatthias Ringwald 
653deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
663deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
673deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
683deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
693deb3ec6SMatthias Ringwald 
703deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0;
713deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS];
72667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS];
73667ec068SMatthias Ringwald 
74667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9;
75667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9;
763deb3ec6SMatthias Ringwald 
7713839019SMatthias Ringwald static btstack_packet_handler_t hfp_callback;
783deb3ec6SMatthias Ringwald 
79ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
80ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
81ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
82ce263fc8SMatthias Ringwald 
83ce263fc8SMatthias Ringwald static char phone_number[25];
84ce263fc8SMatthias Ringwald 
85c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
86c5b64319SMatthias Ringwald 
8713839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
883deb3ec6SMatthias Ringwald     hfp_callback = callback;
893deb3ec6SMatthias Ringwald     if (callback == NULL){
903deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
913deb3ec6SMatthias Ringwald         return;
923deb3ec6SMatthias Ringwald     }
933deb3ec6SMatthias Ringwald     hfp_callback = callback;
94f4000eebSMatthias Ringwald     hfp_set_callback(callback);
953deb3ec6SMatthias Ringwald }
963deb3ec6SMatthias Ringwald 
9713839019SMatthias 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){
98a0ffb263SMatthias Ringwald     if (!callback) return;
99a0ffb263SMatthias Ringwald     uint8_t event[31];
100a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
101a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
102a0ffb263SMatthias Ringwald     event[2] = event_subtype;
103a0ffb263SMatthias Ringwald     event[3] = status;
104a0ffb263SMatthias Ringwald     event[4] = bnip_type;
105a0ffb263SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 6) ? strlen(bnip_number) : sizeof(event) - 6;
106a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], bnip_number, size);
107a0ffb263SMatthias Ringwald     event[5 + size] = 0;
10813839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
109a0ffb263SMatthias Ringwald }
110a0ffb263SMatthias Ringwald 
11113839019SMatthias 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){
112a0ffb263SMatthias Ringwald     if (!callback) return;
113a0ffb263SMatthias Ringwald     uint8_t event[30];
114a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
115a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
116a0ffb263SMatthias Ringwald     event[2] = event_subtype;
117a0ffb263SMatthias Ringwald     event[3] = bnip_type;
118a0ffb263SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 5) ? strlen(bnip_number) : sizeof(event) - 5;
119a0ffb263SMatthias Ringwald     strncpy((char*)&event[4], bnip_number, size);
120a0ffb263SMatthias Ringwald     event[4 + size] = 0;
12113839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
122a0ffb263SMatthias Ringwald }
123a0ffb263SMatthias Ringwald 
12413839019SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir,
125a0ffb263SMatthias Ringwald                 uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){
126a0ffb263SMatthias Ringwald     if (!callback) return;
127a0ffb263SMatthias Ringwald     uint8_t event[35];
128a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
129a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
130a0ffb263SMatthias Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
131a0ffb263SMatthias Ringwald     event[3] = clcc_idx;
132a0ffb263SMatthias Ringwald     event[4] = clcc_dir;
133a0ffb263SMatthias Ringwald     event[6] = clcc_status;
134a0ffb263SMatthias Ringwald     event[7] = clcc_mpty;
135a0ffb263SMatthias Ringwald     event[8] = bnip_type;
136a0ffb263SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 10) ? strlen(bnip_number) : sizeof(event) - 10;
137a0ffb263SMatthias Ringwald     strncpy((char*)&event[9], bnip_number, size);
138a0ffb263SMatthias Ringwald     event[9 + size] = 0;
13913839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
140a0ffb263SMatthias Ringwald }
141a0ffb263SMatthias Ringwald 
142a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
1433deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
144a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1453deb3ec6SMatthias Ringwald     return hf && ag;
1463deb3ec6SMatthias Ringwald }
1473deb3ec6SMatthias Ringwald 
148a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
1493deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
150a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1513deb3ec6SMatthias Ringwald     return hf && ag;
1523deb3ec6SMatthias Ringwald }
1533deb3ec6SMatthias Ringwald 
1543deb3ec6SMatthias Ringwald 
155a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
1563deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
157a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
1583deb3ec6SMatthias Ringwald     return hf && ag;
1593deb3ec6SMatthias Ringwald }
1603deb3ec6SMatthias Ringwald 
161ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
1623deb3ec6SMatthias Ringwald 
1632ef54b27SMatthias 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){
1643deb3ec6SMatthias Ringwald     if (!name){
1653deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1663deb3ec6SMatthias Ringwald     }
1672ef54b27SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, SDP_Handsfree, rfcomm_channel_nr, name);
1683deb3ec6SMatthias Ringwald 
169aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
170aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
171aa4dd815SMatthias Ringwald }
1723deb3ec6SMatthias Ringwald 
1733deb3ec6SMatthias Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
1743deb3ec6SMatthias Ringwald     char buffer[20];
1753deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
1763deb3ec6SMatthias Ringwald     // printf("exchange_supported_features %s\n", buffer);
1773deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1783deb3ec6SMatthias Ringwald }
1793deb3ec6SMatthias Ringwald 
1803deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
1813deb3ec6SMatthias Ringwald     char buffer[30];
1823deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_AVAILABLE_CODECS);
1833deb3ec6SMatthias Ringwald     offset += join(buffer+offset, sizeof(buffer)-offset, hfp_codecs, hfp_codecs_nr);
1843deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
1853deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1863deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1873deb3ec6SMatthias Ringwald }
1883deb3ec6SMatthias Ringwald 
1893deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
1903deb3ec6SMatthias Ringwald     char buffer[20];
1913deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_INDICATOR);
1923deb3ec6SMatthias Ringwald     // printf("retrieve_indicators %s\n", buffer);
1933deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1943deb3ec6SMatthias Ringwald }
1953deb3ec6SMatthias Ringwald 
1963deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
1973deb3ec6SMatthias Ringwald     char buffer[20];
1983deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_INDICATOR);
1993deb3ec6SMatthias Ringwald     // printf("retrieve_indicators_status %s\n", buffer);
2003deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2013deb3ec6SMatthias Ringwald }
2023deb3ec6SMatthias Ringwald 
2033deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2043deb3ec6SMatthias Ringwald     char buffer[20];
2053deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
2063deb3ec6SMatthias Ringwald     // printf("toggle_indicator_status_update %s\n", buffer);
2073deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2083deb3ec6SMatthias Ringwald }
2093deb3ec6SMatthias Ringwald 
2103deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2113deb3ec6SMatthias Ringwald     char buffer[50];
2123deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
2133deb3ec6SMatthias Ringwald     offset += join_bitmap(buffer+offset, sizeof(buffer)-offset, indicators_status, indicators_nr);
2143deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
2153deb3ec6SMatthias Ringwald     buffer[offset] = 0;
2163deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2173deb3ec6SMatthias Ringwald }
2183deb3ec6SMatthias Ringwald 
2193deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
2203deb3ec6SMatthias Ringwald     char buffer[20];
2213deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
2223deb3ec6SMatthias Ringwald     // printf("retrieve_can_hold_call %s\n", buffer);
2233deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2243deb3ec6SMatthias Ringwald }
2253deb3ec6SMatthias Ringwald 
2263deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2273deb3ec6SMatthias Ringwald     char buffer[30];
2283deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
2293deb3ec6SMatthias Ringwald     offset += join(buffer+offset, sizeof(buffer)-offset, hfp_indicators, hfp_indicators_nr);
2303deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
2313deb3ec6SMatthias Ringwald     buffer[offset] = 0;
2323deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2333deb3ec6SMatthias Ringwald }
2343deb3ec6SMatthias Ringwald 
2353deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
2363deb3ec6SMatthias Ringwald     char buffer[20];
2373deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_GENERIC_STATUS_INDICATOR);
2383deb3ec6SMatthias Ringwald     // printf("retrieve_supported_generic_status_indicators %s\n", buffer);
2393deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2403deb3ec6SMatthias Ringwald }
2413deb3ec6SMatthias Ringwald 
2423deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
2433deb3ec6SMatthias Ringwald     char buffer[20];
2443deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_GENERIC_STATUS_INDICATOR);
2453deb3ec6SMatthias Ringwald     // printf("list_initital_supported_generic_status_indicators %s\n", buffer);
2463deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2473deb3ec6SMatthias Ringwald }
2483deb3ec6SMatthias Ringwald 
2493deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
2503deb3ec6SMatthias Ringwald     char buffer[20];
2513deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=3,0\r\n", HFP_QUERY_OPERATOR_SELECTION);
2523deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2533deb3ec6SMatthias Ringwald }
2543deb3ec6SMatthias Ringwald 
2553deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
2563deb3ec6SMatthias Ringwald     char buffer[20];
2573deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_QUERY_OPERATOR_SELECTION);
2583deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2593deb3ec6SMatthias Ringwald }
2603deb3ec6SMatthias Ringwald 
2613deb3ec6SMatthias Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
2623deb3ec6SMatthias Ringwald     char buffer[20];
2633deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
2643deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2653deb3ec6SMatthias Ringwald }
2663deb3ec6SMatthias Ringwald 
2673deb3ec6SMatthias Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
2683deb3ec6SMatthias Ringwald     char buffer[20];
2693deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_TRIGGER_CODEC_CONNECTION_SETUP);
2703deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2713deb3ec6SMatthias Ringwald }
2723deb3ec6SMatthias Ringwald 
2733deb3ec6SMatthias Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
2743deb3ec6SMatthias Ringwald     char buffer[20];
2753deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec);
2763deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2773deb3ec6SMatthias Ringwald }
2783deb3ec6SMatthias Ringwald 
279ce263fc8SMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
280ce263fc8SMatthias Ringwald     char buffer[10];
281ce263fc8SMatthias Ringwald     sprintf(buffer, "%s\r\n", HFP_CALL_ANSWERED);
282ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
283ce263fc8SMatthias Ringwald }
284ce263fc8SMatthias Ringwald 
285ce263fc8SMatthias Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
286ce263fc8SMatthias Ringwald     char buffer[40];
287ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SET_MICROPHONE_GAIN, gain);
288ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
289ce263fc8SMatthias Ringwald }
290ce263fc8SMatthias Ringwald 
291ce263fc8SMatthias Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
292ce263fc8SMatthias Ringwald     char buffer[40];
293ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SET_SPEAKER_GAIN, gain);
294ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
295ce263fc8SMatthias Ringwald }
296ce263fc8SMatthias Ringwald 
297ce263fc8SMatthias Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
298ce263fc8SMatthias Ringwald     char buffer[40];
299ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CLIP, activate);
300ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
301ce263fc8SMatthias Ringwald }
302ce263fc8SMatthias Ringwald 
303ce263fc8SMatthias Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
304ce263fc8SMatthias Ringwald     char buffer[40];
305ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_TURN_OFF_EC_AND_NR, activate);
306ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
307ce263fc8SMatthias Ringwald }
308ce263fc8SMatthias Ringwald 
309ce263fc8SMatthias Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
310ce263fc8SMatthias Ringwald     char buffer[40];
311ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate);
312ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
313ce263fc8SMatthias Ringwald }
314ce263fc8SMatthias Ringwald 
315ce263fc8SMatthias Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
316ce263fc8SMatthias Ringwald     char buffer[40];
317ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
318ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
319ce263fc8SMatthias Ringwald }
320ce263fc8SMatthias Ringwald 
321ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
322ce263fc8SMatthias Ringwald     char buffer[40];
323ce263fc8SMatthias Ringwald     sprintf(buffer, "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
324ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
325ce263fc8SMatthias Ringwald }
326ce263fc8SMatthias Ringwald 
327a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
328ce263fc8SMatthias Ringwald     char buffer[40];
329a0ffb263SMatthias Ringwald     sprintf(buffer, "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id);
330ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
331ce263fc8SMatthias Ringwald }
332ce263fc8SMatthias Ringwald 
333ce263fc8SMatthias Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
334ce263fc8SMatthias Ringwald     char buffer[20];
335ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_REDIAL_LAST_NUMBER);
336ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
337ce263fc8SMatthias Ringwald }
338ce263fc8SMatthias Ringwald 
339ce263fc8SMatthias Ringwald static int hfp_hf_send_chup(uint16_t cid){
340ce263fc8SMatthias Ringwald     char buffer[20];
341ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_HANG_UP_CALL);
342ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
343ce263fc8SMatthias Ringwald }
344ce263fc8SMatthias Ringwald 
345ce263fc8SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, int number){
346ce263fc8SMatthias Ringwald     char buffer[20];
347ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
348ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
349ce263fc8SMatthias Ringwald }
350ce263fc8SMatthias Ringwald 
351ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
352ce263fc8SMatthias Ringwald     char buffer[20];
353ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
354ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
355ce263fc8SMatthias Ringwald }
356ce263fc8SMatthias Ringwald 
357ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
358ce263fc8SMatthias Ringwald     char buffer[20];
359ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=1\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG);
360ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
361ce263fc8SMatthias Ringwald }
362ce263fc8SMatthias Ringwald 
363667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
364667ec068SMatthias Ringwald     char buffer[20];
365667ec068SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_LIST_CURRENT_CALLS);
366667ec068SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
367667ec068SMatthias Ringwald }
368667ec068SMatthias Ringwald 
36913839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){
3703deb3ec6SMatthias Ringwald     if (!callback) return;
371a0ffb263SMatthias Ringwald     uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1];
3723deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3733deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3743deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
375a0ffb263SMatthias Ringwald     event[3] = indicator.index;
376a0ffb263SMatthias Ringwald     event[4] = indicator.status;
377a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
378a0ffb263SMatthias Ringwald     event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0;
37913839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3803deb3ec6SMatthias Ringwald }
3813deb3ec6SMatthias Ringwald 
38213839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){
3833deb3ec6SMatthias Ringwald     if (!callback) return;
3843deb3ec6SMatthias Ringwald     uint8_t event[24];
3853deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3863deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3873deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
388a0ffb263SMatthias Ringwald     event[3] = network_operator.mode;
389a0ffb263SMatthias Ringwald     event[4] = network_operator.format;
390a0ffb263SMatthias Ringwald     strcpy((char*)&event[5], network_operator.name);
39113839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3923deb3ec6SMatthias Ringwald }
3933deb3ec6SMatthias Ringwald 
394a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
395a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
396a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
397aa4dd815SMatthias Ringwald     int done = 1;
3983deb3ec6SMatthias Ringwald 
399a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4003deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
401a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
402a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4033deb3ec6SMatthias Ringwald             break;
4043deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
405a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
406a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4073deb3ec6SMatthias Ringwald             break;
4083deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
409a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
410a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4113deb3ec6SMatthias Ringwald             break;
4123deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
413a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
414a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4153deb3ec6SMatthias Ringwald             break;
4163deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
417a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
418a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4193deb3ec6SMatthias Ringwald             break;
4203deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
421a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
422a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4233deb3ec6SMatthias Ringwald             break;
4243deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
425a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
426a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4273deb3ec6SMatthias Ringwald             break;
4283deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
429a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
430a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4313deb3ec6SMatthias Ringwald             break;
4323deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
433a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
434a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4353deb3ec6SMatthias Ringwald             break;
4363deb3ec6SMatthias Ringwald         default:
437aa4dd815SMatthias Ringwald             done = 0;
4383deb3ec6SMatthias Ringwald             break;
4393deb3ec6SMatthias Ringwald     }
4403deb3ec6SMatthias Ringwald     return done;
4413deb3ec6SMatthias Ringwald }
4423deb3ec6SMatthias Ringwald 
443ce263fc8SMatthias Ringwald 
444a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
445a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
446a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
447ce263fc8SMatthias Ringwald 
448ce263fc8SMatthias Ringwald     int done = 0;
449a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
450a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
451ce263fc8SMatthias Ringwald         done = 1;
452a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
453ce263fc8SMatthias Ringwald         return done;
454ce263fc8SMatthias Ringwald     };
455a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
456a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
457ce263fc8SMatthias Ringwald         done = 1;
458a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
459a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
460a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
461ce263fc8SMatthias Ringwald         return done;
462ce263fc8SMatthias Ringwald     }
463ce263fc8SMatthias Ringwald 
464a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
465ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
466a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
467a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
468a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
469ce263fc8SMatthias Ringwald             return 1;
470ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
471a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
472a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
473a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
474ce263fc8SMatthias Ringwald             return 1;
475ce263fc8SMatthias Ringwald         default:
476ce263fc8SMatthias Ringwald             break;
477ce263fc8SMatthias Ringwald     }
478ce263fc8SMatthias Ringwald 
479a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
480a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
481ce263fc8SMatthias Ringwald         done = 1;
482a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
483ce263fc8SMatthias Ringwald         return done;
484ce263fc8SMatthias Ringwald     }
485ce263fc8SMatthias Ringwald 
486ce263fc8SMatthias Ringwald     return done;
487ce263fc8SMatthias Ringwald }
488ce263fc8SMatthias Ringwald 
489a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
490ce263fc8SMatthias Ringwald     /* events ( == commands):
491ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
492ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
493ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
494ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
495ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
496ce263fc8SMatthias Ringwald     */
497ce263fc8SMatthias Ringwald 
498a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
499ce263fc8SMatthias Ringwald 
500a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
501ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
502a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
503ce263fc8SMatthias Ringwald 
504a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
505a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
506a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
507ce263fc8SMatthias Ringwald             return 1;
508ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
509a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = 0;
510a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = 0;
511a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = 0;
512ce263fc8SMatthias Ringwald 
513a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
514a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
515a0ffb263SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
516ce263fc8SMatthias Ringwald             break;
517ce263fc8SMatthias Ringwald 
518*6a7f44bdSMilanka Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:{
519*6a7f44bdSMilanka Ringwald             if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
520a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
521a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
522a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
523a0ffb263SMatthias Ringwald                 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
524ce263fc8SMatthias Ringwald             } else {
525a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
526a0ffb263SMatthias Ringwald                 hfp_connection->suggested_codec = 0;
527a0ffb263SMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
528a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
529a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
530a0ffb263SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
531ce263fc8SMatthias Ringwald 
532ce263fc8SMatthias Ringwald             }
533ce263fc8SMatthias Ringwald             break;
534*6a7f44bdSMilanka Ringwald         }
535ce263fc8SMatthias Ringwald         default:
536ce263fc8SMatthias Ringwald             break;
537ce263fc8SMatthias Ringwald     }
538ce263fc8SMatthias Ringwald     return 0;
539ce263fc8SMatthias Ringwald }
540ce263fc8SMatthias Ringwald 
541a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
542a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
543a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
544ce263fc8SMatthias Ringwald 
545ce263fc8SMatthias Ringwald 
546a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
547a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
548a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
549a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
550ce263fc8SMatthias Ringwald         return 1;
551ce263fc8SMatthias Ringwald     }
552ce263fc8SMatthias Ringwald 
553a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
554ce263fc8SMatthias Ringwald 
555ce263fc8SMatthias Ringwald     // run codecs exchange
556a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
557ce263fc8SMatthias Ringwald     if (done) return 1;
558ce263fc8SMatthias Ringwald 
559a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
560a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
561a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
562be2a1a78SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
563ce263fc8SMatthias Ringwald         return 1;
564ce263fc8SMatthias Ringwald     }
565ce263fc8SMatthias Ringwald 
566ce263fc8SMatthias Ringwald     return 0;
567ce263fc8SMatthias Ringwald }
568ce263fc8SMatthias Ringwald 
569a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
570a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
571a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
572a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
573ce263fc8SMatthias Ringwald         return 1;
574ce263fc8SMatthias Ringwald     }
575ce263fc8SMatthias Ringwald     return 0;
576ce263fc8SMatthias Ringwald }
577ce263fc8SMatthias Ringwald 
578a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){
579a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
580724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
581a0ffb263SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return;
582ce263fc8SMatthias Ringwald 
583a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
584ce263fc8SMatthias Ringwald     if (!done){
585a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
586ce263fc8SMatthias Ringwald     }
587ce263fc8SMatthias Ringwald     if (!done){
588a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
589ce263fc8SMatthias Ringwald     }
590ce263fc8SMatthias Ringwald     if (!done){
591a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
592ce263fc8SMatthias Ringwald     }
593ce263fc8SMatthias Ringwald 
594a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
595a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
596a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
597a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
598ce263fc8SMatthias Ringwald         return;
599ce263fc8SMatthias Ringwald     }
600ce263fc8SMatthias Ringwald 
601a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
602a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
603a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
604a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
605ce263fc8SMatthias Ringwald         return;
606ce263fc8SMatthias Ringwald     }
607ce263fc8SMatthias Ringwald 
608a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
609a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
610a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
611a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
612ce263fc8SMatthias Ringwald         return;
613ce263fc8SMatthias Ringwald     }
614ce263fc8SMatthias Ringwald 
615a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
616a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
617a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
618a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
619ce263fc8SMatthias Ringwald         return;
620ce263fc8SMatthias Ringwald     }
621ce263fc8SMatthias Ringwald 
622a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
623a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
624a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
625a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
626ce263fc8SMatthias Ringwald         return;
627ce263fc8SMatthias Ringwald     }
628ce263fc8SMatthias Ringwald 
629a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
630a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
631a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
632a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
633ce263fc8SMatthias Ringwald         return;
634ce263fc8SMatthias Ringwald     }
635ce263fc8SMatthias Ringwald 
636a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
637a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
638a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
639a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
640ce263fc8SMatthias Ringwald         return;
641ce263fc8SMatthias Ringwald     }
642ce263fc8SMatthias Ringwald 
643a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
644a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
645a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
646a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
647ce263fc8SMatthias Ringwald         return;
648ce263fc8SMatthias Ringwald     }
649ce263fc8SMatthias Ringwald 
650ce263fc8SMatthias Ringwald 
651a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
652a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
653a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
654a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
655ce263fc8SMatthias Ringwald         return;
656ce263fc8SMatthias Ringwald     }
657ce263fc8SMatthias Ringwald 
658a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
659a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
660a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
661a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
662ce263fc8SMatthias Ringwald         return;
663ce263fc8SMatthias Ringwald     }
664ce263fc8SMatthias Ringwald 
665a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
666a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
667a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
668a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
669ce263fc8SMatthias Ringwald         return;
670ce263fc8SMatthias Ringwald     }
671ce263fc8SMatthias Ringwald 
672a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
673a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
674a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
675a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
676ce263fc8SMatthias Ringwald         return;
677ce263fc8SMatthias Ringwald     }
678ce263fc8SMatthias Ringwald 
679a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
680a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
681a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
682a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
683ce263fc8SMatthias Ringwald         return;
684ce263fc8SMatthias Ringwald     }
685ce263fc8SMatthias Ringwald 
686a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
687a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
688a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
689a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
690ce263fc8SMatthias Ringwald         return;
691ce263fc8SMatthias Ringwald     }
692ce263fc8SMatthias Ringwald 
693a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
694a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
695a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
696a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
697ce263fc8SMatthias Ringwald         return;
698ce263fc8SMatthias Ringwald     }
699ce263fc8SMatthias Ringwald 
700a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
701a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
702a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
703a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
704ce263fc8SMatthias Ringwald         return;
705ce263fc8SMatthias Ringwald     }
706ce263fc8SMatthias Ringwald 
707a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
708a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
709a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
710a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
711ce263fc8SMatthias Ringwald         return;
712ce263fc8SMatthias Ringwald     }
713ce263fc8SMatthias Ringwald 
714a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
715a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
716a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
717a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
718ce263fc8SMatthias Ringwald         return;
719ce263fc8SMatthias Ringwald     }
720ce263fc8SMatthias Ringwald 
721a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
722a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
723a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
724a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
725ce263fc8SMatthias Ringwald         return;
726ce263fc8SMatthias Ringwald     }
727ce263fc8SMatthias Ringwald 
728a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
729a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
730a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
731a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
732667ec068SMatthias Ringwald         return;
733667ec068SMatthias Ringwald     }
734667ec068SMatthias Ringwald 
735a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
736a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
737a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
738a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
739a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
740ce263fc8SMatthias Ringwald         return;
741ce263fc8SMatthias Ringwald     }
742ce263fc8SMatthias Ringwald 
743a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
744a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
745a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
746a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
747ce263fc8SMatthias Ringwald         return;
748ce263fc8SMatthias Ringwald     }
749ce263fc8SMatthias Ringwald 
750a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
751a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
752a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
753a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
754667ec068SMatthias Ringwald         return;
755667ec068SMatthias Ringwald     }
756667ec068SMatthias Ringwald 
757a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
758a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
759667ec068SMatthias Ringwald         char buffer[20];
760a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
761667ec068SMatthias Ringwald             case '?':
762667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
763a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
764667ec068SMatthias Ringwald                 return;
765667ec068SMatthias Ringwald             case '0':
766667ec068SMatthias Ringwald             case '1':
767667ec068SMatthias Ringwald             case '2':
768a0ffb263SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
769a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
770667ec068SMatthias Ringwald                 return;
771667ec068SMatthias Ringwald             default:
772667ec068SMatthias Ringwald                 break;
773667ec068SMatthias Ringwald         }
774667ec068SMatthias Ringwald         return;
775667ec068SMatthias Ringwald     }
776667ec068SMatthias Ringwald 
777a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
778a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
779667ec068SMatthias Ringwald         char buffer[20];
780667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
781a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
782667ec068SMatthias Ringwald         return;
783667ec068SMatthias Ringwald     }
784667ec068SMatthias Ringwald 
785667ec068SMatthias Ringwald     // update HF indicators
786a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
787667ec068SMatthias Ringwald         int i;
788667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
789a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
790a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
791a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
792a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
793667ec068SMatthias Ringwald                     char buffer[30];
794667ec068SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], hfp_indicators_value[i]);
795a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
796667ec068SMatthias Ringwald                 } else {
797667ec068SMatthias Ringwald                     printf("Not sending HF indicator %u as it is disabled\n", hfp_indicators[i]);
798667ec068SMatthias Ringwald                 }
799667ec068SMatthias Ringwald                 return;
800667ec068SMatthias Ringwald             }
801667ec068SMatthias Ringwald         }
802667ec068SMatthias Ringwald     }
803667ec068SMatthias Ringwald 
804ce263fc8SMatthias Ringwald     if (done) return;
805ce263fc8SMatthias Ringwald     // deal with disconnect
806a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
807ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
808a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
809a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
810ce263fc8SMatthias Ringwald             break;
811ce263fc8SMatthias Ringwald 
812ce263fc8SMatthias Ringwald         default:
813ce263fc8SMatthias Ringwald             break;
814ce263fc8SMatthias Ringwald     }
815ce263fc8SMatthias Ringwald }
816ce263fc8SMatthias Ringwald 
817a0ffb263SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){
818ce263fc8SMatthias Ringwald     // determine highest possible link setting
819a0ffb263SMatthias Ringwald     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
820a0ffb263SMatthias Ringwald     if (hci_remote_esco_supported(hfp_connection->acl_handle)){
821a0ffb263SMatthias Ringwald         hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
822ce263fc8SMatthias Ringwald         if ((hfp_supported_features             & (1<<HFP_HFSF_ESCO_S4))
823a0ffb263SMatthias Ringwald         &&  (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4))){
824a0ffb263SMatthias Ringwald             hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
825ce263fc8SMatthias Ringwald         }
826ce263fc8SMatthias Ringwald     }
827ce263fc8SMatthias Ringwald }
828ce263fc8SMatthias Ringwald 
829a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
830a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
831*6a7f44bdSMilanka Ringwald 
832*6a7f44bdSMilanka Ringwald     hfp_emit_connection_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0, hfp_connection->acl_handle, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
833a0ffb263SMatthias Ringwald     hfp_init_link_settings(hfp_connection);
834667ec068SMatthias Ringwald     // restore volume settings
835a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
836a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
837667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
838a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
839a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
840667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
841667ec068SMatthias Ringwald     // enable all indicators
842667ec068SMatthias Ringwald     int i;
843667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
844a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
845a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
846667ec068SMatthias Ringwald     }
847ce263fc8SMatthias Ringwald }
848ce263fc8SMatthias Ringwald 
849a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
850a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
851a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
8523deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
853a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
854a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
8553deb3ec6SMatthias Ringwald                 break;
8563deb3ec6SMatthias Ringwald             }
857a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8583deb3ec6SMatthias Ringwald             break;
8593deb3ec6SMatthias Ringwald 
8603deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
861a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8623deb3ec6SMatthias Ringwald             break;
8633deb3ec6SMatthias Ringwald 
8643deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
865a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
8663deb3ec6SMatthias Ringwald             break;
8673deb3ec6SMatthias Ringwald 
8683deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
869a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
8703deb3ec6SMatthias Ringwald             break;
8713deb3ec6SMatthias Ringwald 
8723deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
873a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
874a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
8753deb3ec6SMatthias Ringwald                 break;
8763deb3ec6SMatthias Ringwald             }
877a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
878a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8793deb3ec6SMatthias Ringwald                 break;
8803deb3ec6SMatthias Ringwald             }
881a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8823deb3ec6SMatthias Ringwald             break;
8833deb3ec6SMatthias Ringwald 
8843deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
885a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
886a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8873deb3ec6SMatthias Ringwald                 break;
8883deb3ec6SMatthias Ringwald             }
889a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8903deb3ec6SMatthias Ringwald             break;
8913deb3ec6SMatthias Ringwald 
8923deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
893a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
8943deb3ec6SMatthias Ringwald             break;
8953deb3ec6SMatthias Ringwald 
8963deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
897a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
8983deb3ec6SMatthias Ringwald             break;
8993deb3ec6SMatthias Ringwald 
9003deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
901a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9023deb3ec6SMatthias Ringwald             break;
903ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
904a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
905a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
9063deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
907ce263fc8SMatthias Ringwald                 break;
908ce263fc8SMatthias Ringwald             }
9093deb3ec6SMatthias Ringwald 
910a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
911a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
9123deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
913ce263fc8SMatthias Ringwald                 break;
9143deb3ec6SMatthias Ringwald             }
9153deb3ec6SMatthias Ringwald 
916a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
917ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
918ce263fc8SMatthias Ringwald                     printf("Format set, querying name\n");
919a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
920ce263fc8SMatthias Ringwald                     break;
921ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
922a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
923a0ffb263SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_callback, hfp_connection->network_operator);
924ce263fc8SMatthias Ringwald                     break;
925ce263fc8SMatthias Ringwald                 default:
926ce263fc8SMatthias Ringwald                     break;
9273deb3ec6SMatthias Ringwald             }
928ce263fc8SMatthias Ringwald 
929a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
930a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
931ce263fc8SMatthias Ringwald                 break;
9323deb3ec6SMatthias Ringwald             }
9333deb3ec6SMatthias Ringwald 
934a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
935aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
936a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9373deb3ec6SMatthias Ringwald                     break;
938ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
939a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
940*6a7f44bdSMilanka Ringwald                     hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
941*6a7f44bdSMilanka Ringwald                     hfp_emit_codec_event(hfp_callback, 0, hfp_connection->negotiated_codec);
942ce263fc8SMatthias Ringwald                     break;
9433deb3ec6SMatthias Ringwald                 default:
9443deb3ec6SMatthias Ringwald                     break;
9453deb3ec6SMatthias Ringwald             }
9463deb3ec6SMatthias Ringwald             break;
9473deb3ec6SMatthias Ringwald         default:
9483deb3ec6SMatthias Ringwald             break;
9493deb3ec6SMatthias Ringwald     }
9503deb3ec6SMatthias Ringwald 
9513deb3ec6SMatthias Ringwald     // done
952a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
9533deb3ec6SMatthias Ringwald }
9543deb3ec6SMatthias Ringwald 
9553deb3ec6SMatthias Ringwald 
9563deb3ec6SMatthias Ringwald static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
957a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
958a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
9593deb3ec6SMatthias Ringwald 
9601e35c04dSMatthias Ringwald     char last_char = packet[size-1];
9611e35c04dSMatthias Ringwald     packet[size-1] = 0;
9621e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
9631e35c04dSMatthias Ringwald     packet[size-1] = last_char;
9641e35c04dSMatthias Ringwald 
965667ec068SMatthias Ringwald     int pos, i, value;
9663deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
967a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
968ce263fc8SMatthias Ringwald     }
9693deb3ec6SMatthias Ringwald 
970a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
971667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
972a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
973a0ffb263SMatthias Ringwald             // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type);
974a0ffb263SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
975667ec068SMatthias Ringwald             break;
976667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
977a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
978a0ffb263SMatthias Ringwald             // printf("Response and Hold status: %s\n", hfp_connection->line_buffer);
979a0ffb263SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, atoi((char *)&hfp_connection->line_buffer[0]));
980667ec068SMatthias Ringwald             break;
981667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
982a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
983a0ffb263SMatthias Ringwald             // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
984a0ffb263SMatthias Ringwald             //      hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
985a0ffb263SMatthias Ringwald             //      hfp_connection->bnip_number, hfp_connection->bnip_type);
986a0ffb263SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_callback, hfp_connection->clcc_idx,
987a0ffb263SMatthias Ringwald                 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
988a0ffb263SMatthias Ringwald                 hfp_connection->bnip_type, hfp_connection->bnip_number);
989667ec068SMatthias Ringwald             break;
990ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
991a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
992a0ffb263SMatthias Ringwald             value = atoi((char*)hfp_connection->line_buffer);
993667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
994667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value);
995ce263fc8SMatthias Ringwald             break;
996ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
997a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
998a0ffb263SMatthias Ringwald             value = atoi((char*)hfp_connection->line_buffer);
999667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1000667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1001ce263fc8SMatthias Ringwald             break;
1002ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1003a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1004a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1005a0ffb263SMatthias Ringwald             break;
1006a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1007a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1008a0ffb263SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1009a0ffb263SMatthias Ringwald             break;
1010a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1011a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1012a0ffb263SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALLING_LINE_INDETIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1013ce263fc8SMatthias Ringwald             break;
1014ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1015a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1016a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1017a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1018a0ffb263SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1019ce263fc8SMatthias Ringwald             break;
1020ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1021a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1022a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1023a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1024ce263fc8SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1);
1025ce263fc8SMatthias Ringwald             break;
1026ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1027a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1028ce263fc8SMatthias Ringwald             break;
1029ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
1030a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_RING);
1031ce263fc8SMatthias Ringwald             break;
1032ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1033a0ffb263SMatthias Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1034a0ffb263SMatthias Ringwald                 if (hfp_connection->ag_indicators[i].status_changed) {
1035a0ffb263SMatthias Ringwald                     if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1036a0ffb263SMatthias Ringwald                         hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1037a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1038a0ffb263SMatthias Ringwald                         hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1039a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1040a0ffb263SMatthias Ringwald                         hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1041ce263fc8SMatthias Ringwald                     }
1042a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[i].status_changed = 0;
1043a0ffb263SMatthias Ringwald                     hfp_emit_ag_indicator_event(hfp_callback, hfp_connection->ag_indicators[i]);
10443deb3ec6SMatthias Ringwald                     break;
10453deb3ec6SMatthias Ringwald                 }
10463deb3ec6SMatthias Ringwald             }
1047ce263fc8SMatthias Ringwald             break;
1048ce263fc8SMatthias Ringwald         default:
1049ce263fc8SMatthias Ringwald             break;
10503deb3ec6SMatthias Ringwald     }
1051a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
10523deb3ec6SMatthias Ringwald }
10533deb3ec6SMatthias Ringwald 
10540cb5b971SMatthias Ringwald static void hfp_run(void){
1055665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1056665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1057665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1058a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1059a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
10603deb3ec6SMatthias Ringwald     }
10613deb3ec6SMatthias Ringwald }
10623deb3ec6SMatthias Ringwald 
1063ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10643deb3ec6SMatthias Ringwald     switch (packet_type){
10653deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
10663deb3ec6SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size);
10673deb3ec6SMatthias Ringwald             break;
10683deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1069e30a6a47SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size);
10703deb3ec6SMatthias Ringwald         default:
10713deb3ec6SMatthias Ringwald             break;
10723deb3ec6SMatthias Ringwald     }
10733deb3ec6SMatthias Ringwald     hfp_run();
10743deb3ec6SMatthias Ringwald }
10753deb3ec6SMatthias Ringwald 
1076a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1077d63c37a1SMatthias Ringwald     // register for HCI events
1078d63c37a1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
1079d63c37a1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
1080d63c37a1SMatthias Ringwald 
1081d63c37a1SMatthias Ringwald     rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff);
1082a0ffb263SMatthias Ringwald 
1083d68dcce1SMatthias Ringwald     hfp_set_packet_handler_for_rfcomm_connections(&packet_handler);
1084d68dcce1SMatthias Ringwald 
1085a0ffb263SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1086a0ffb263SMatthias Ringwald     hfp_codecs_nr = 0;
1087a0ffb263SMatthias Ringwald     hfp_indicators_nr = 0;
1088a0ffb263SMatthias Ringwald     hfp_hf_speaker_gain = 9;
1089a0ffb263SMatthias Ringwald     hfp_hf_microphone_gain = 9;
1090a0ffb263SMatthias Ringwald }
1091a0ffb263SMatthias Ringwald 
1092a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
10933deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1094a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
10953deb3ec6SMatthias Ringwald         return;
10963deb3ec6SMatthias Ringwald     }
10973deb3ec6SMatthias Ringwald 
10983deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
10993deb3ec6SMatthias Ringwald     int i;
11003deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
11013deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
11023deb3ec6SMatthias Ringwald     }
11033deb3ec6SMatthias Ringwald 
11043deb3ec6SMatthias Ringwald     char buffer[30];
11053deb3ec6SMatthias Ringwald     int offset = join(buffer, sizeof(buffer), hfp_codecs, hfp_codecs_nr);
11063deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1107665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1108665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1109665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1110a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1111a0ffb263SMatthias Ringwald         if (! hfp_connection) continue;
1112a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_AVAILABLE_CODECS;
1113a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
11143deb3ec6SMatthias Ringwald     }
11153deb3ec6SMatthias Ringwald }
11163deb3ec6SMatthias Ringwald 
1117a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
11183deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1119a0ffb263SMatthias Ringwald }
11203deb3ec6SMatthias Ringwald 
1121a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
11223deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
11233deb3ec6SMatthias Ringwald     int i;
1124a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
11253deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
11263deb3ec6SMatthias Ringwald     }
11273deb3ec6SMatthias Ringwald }
11283deb3ec6SMatthias Ringwald 
11293deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
11303deb3ec6SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway);
11313deb3ec6SMatthias Ringwald }
11323deb3ec6SMatthias Ringwald 
11333deb3ec6SMatthias Ringwald void hfp_hf_release_service_level_connection(bd_addr_t bd_addr){
1134a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1135a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
1136a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11373deb3ec6SMatthias Ringwald }
11383deb3ec6SMatthias Ringwald 
1139ce263fc8SMatthias Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(bd_addr_t bd_addr, uint8_t enable){
11403deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1141a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1142a0ffb263SMatthias Ringwald     if (!hfp_connection){
1143a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
11443deb3ec6SMatthias Ringwald         return;
11453deb3ec6SMatthias Ringwald     }
1146a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
1147a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11483deb3ec6SMatthias Ringwald }
11493deb3ec6SMatthias Ringwald 
1150ce263fc8SMatthias Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1151ce263fc8SMatthias Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 1);
1152ce263fc8SMatthias Ringwald }
1153ce263fc8SMatthias Ringwald 
1154ce263fc8SMatthias Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1155ce263fc8SMatthias Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 0);
1156ce263fc8SMatthias Ringwald }
1157ce263fc8SMatthias Ringwald 
11583deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1159ce263fc8SMatthias Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(bd_addr_t bd_addr, uint32_t indicators_status_bitmap){
11603deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1161a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1162a0ffb263SMatthias Ringwald     if (!hfp_connection){
1163a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
11643deb3ec6SMatthias Ringwald         return;
11653deb3ec6SMatthias Ringwald     }
1166a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1167a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1168a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11693deb3ec6SMatthias Ringwald }
11703deb3ec6SMatthias Ringwald 
11713deb3ec6SMatthias Ringwald void hfp_hf_query_operator_selection(bd_addr_t bd_addr){
11723deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1173a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1174a0ffb263SMatthias Ringwald     if (!hfp_connection){
1175a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
11763deb3ec6SMatthias Ringwald         return;
11773deb3ec6SMatthias Ringwald     }
1178a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1179ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1180a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1181ce263fc8SMatthias Ringwald             break;
1182ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1183a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1184ce263fc8SMatthias Ringwald             break;
1185ce263fc8SMatthias Ringwald         default:
1186ce263fc8SMatthias Ringwald             break;
1187ce263fc8SMatthias Ringwald     }
1188a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11893deb3ec6SMatthias Ringwald }
11903deb3ec6SMatthias Ringwald 
1191ce263fc8SMatthias Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, uint8_t enable){
11923deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1193a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1194a0ffb263SMatthias Ringwald     if (!hfp_connection){
1195a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
11963deb3ec6SMatthias Ringwald         return;
11973deb3ec6SMatthias Ringwald     }
1198a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1199a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12003deb3ec6SMatthias Ringwald }
12013deb3ec6SMatthias Ringwald 
1202ce263fc8SMatthias Ringwald 
1203ce263fc8SMatthias Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1204ce263fc8SMatthias Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 1);
1205ce263fc8SMatthias Ringwald }
1206ce263fc8SMatthias Ringwald 
1207ce263fc8SMatthias Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1208ce263fc8SMatthias Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 0);
1209ce263fc8SMatthias Ringwald }
1210ce263fc8SMatthias Ringwald 
1211ce263fc8SMatthias Ringwald 
12123deb3ec6SMatthias Ringwald void hfp_hf_establish_audio_connection(bd_addr_t bd_addr){
12133deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1214a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1215a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1216ce263fc8SMatthias Ringwald 
1217a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1218a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
12193deb3ec6SMatthias Ringwald 
1220a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1221ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1222a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1223a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1224ce263fc8SMatthias Ringwald     } else {
1225a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1226aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1227aa4dd815SMatthias Ringwald                 break;
1228aa4dd815SMatthias Ringwald             default:
1229a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1230aa4dd815SMatthias Ringwald                 break;
12313deb3ec6SMatthias Ringwald         }
1232ce263fc8SMatthias Ringwald     }
1233ce263fc8SMatthias Ringwald 
1234a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12353deb3ec6SMatthias Ringwald }
12363deb3ec6SMatthias Ringwald 
12373deb3ec6SMatthias Ringwald void hfp_hf_release_audio_connection(bd_addr_t bd_addr){
1238a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1239a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
1240a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12413deb3ec6SMatthias Ringwald }
12423deb3ec6SMatthias Ringwald 
1243ce263fc8SMatthias Ringwald void hfp_hf_answer_incoming_call(bd_addr_t bd_addr){
1244ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1245a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1246ce263fc8SMatthias Ringwald 
1247ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1248a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
1249a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1250ce263fc8SMatthias Ringwald     } else {
1251ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1252ce263fc8SMatthias Ringwald     }
1253ce263fc8SMatthias Ringwald }
1254ce263fc8SMatthias Ringwald 
1255ce263fc8SMatthias Ringwald void hfp_hf_terminate_call(bd_addr_t bd_addr){
1256ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1257a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1258ce263fc8SMatthias Ringwald 
1259a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
1260a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1261ce263fc8SMatthias Ringwald }
1262ce263fc8SMatthias Ringwald 
1263a0ffb263SMatthias Ringwald void hfp_hf_reject_incoming_call(bd_addr_t bd_addr){
1264ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1265a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1266ce263fc8SMatthias Ringwald 
1267ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1268a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
1269a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1270ce263fc8SMatthias Ringwald     }
1271ce263fc8SMatthias Ringwald }
1272ce263fc8SMatthias Ringwald 
1273ce263fc8SMatthias Ringwald void hfp_hf_user_busy(bd_addr_t addr){
1274ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1275a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1276ce263fc8SMatthias Ringwald 
1277ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1278a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
1279a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1280ce263fc8SMatthias Ringwald     }
1281ce263fc8SMatthias Ringwald }
1282ce263fc8SMatthias Ringwald 
1283ce263fc8SMatthias Ringwald void hfp_hf_end_active_and_accept_other(bd_addr_t addr){
1284ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1285a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1286ce263fc8SMatthias Ringwald 
1287ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1288ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1289a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
1290a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1291ce263fc8SMatthias Ringwald     }
1292ce263fc8SMatthias Ringwald }
1293ce263fc8SMatthias Ringwald 
1294ce263fc8SMatthias Ringwald void hfp_hf_swap_calls(bd_addr_t addr){
1295ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1296a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1297ce263fc8SMatthias Ringwald 
1298ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1299ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1300a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
1301a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1302ce263fc8SMatthias Ringwald     }
1303ce263fc8SMatthias Ringwald }
1304ce263fc8SMatthias Ringwald 
1305ce263fc8SMatthias Ringwald void hfp_hf_join_held_call(bd_addr_t addr){
1306ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1307a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1308ce263fc8SMatthias Ringwald 
1309ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1310ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1311a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
1312a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1313ce263fc8SMatthias Ringwald     }
1314ce263fc8SMatthias Ringwald }
1315ce263fc8SMatthias Ringwald 
1316ce263fc8SMatthias Ringwald void hfp_hf_connect_calls(bd_addr_t addr){
1317ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1318a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1319ce263fc8SMatthias Ringwald 
1320ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1321ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1322a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
1323a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1324ce263fc8SMatthias Ringwald     }
1325ce263fc8SMatthias Ringwald }
1326ce263fc8SMatthias Ringwald 
1327667ec068SMatthias Ringwald void hfp_hf_release_call_with_index(bd_addr_t addr, int index){
1328667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1329a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1330667ec068SMatthias Ringwald 
1331667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1332667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1333a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1334a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
1335a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1336667ec068SMatthias Ringwald     }
1337667ec068SMatthias Ringwald }
1338667ec068SMatthias Ringwald 
1339667ec068SMatthias Ringwald void hfp_hf_private_consultation_with_call(bd_addr_t addr, int index){
1340667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1341a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1342667ec068SMatthias Ringwald 
1343667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1344667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1345a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1346a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
1347a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1348667ec068SMatthias Ringwald     }
1349667ec068SMatthias Ringwald }
1350ce263fc8SMatthias Ringwald 
1351ce263fc8SMatthias Ringwald void hfp_hf_dial_number(bd_addr_t bd_addr, char * number){
1352ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1353a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1354ce263fc8SMatthias Ringwald 
1355a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1356ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1357a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1358ce263fc8SMatthias Ringwald }
1359ce263fc8SMatthias Ringwald 
1360a0ffb263SMatthias Ringwald void hfp_hf_dial_memory(bd_addr_t bd_addr, int memory_id){
1361ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1362a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1363ce263fc8SMatthias Ringwald 
1364a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1365a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1366a0ffb263SMatthias Ringwald 
1367a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1368ce263fc8SMatthias Ringwald }
1369ce263fc8SMatthias Ringwald 
1370ce263fc8SMatthias Ringwald void hfp_hf_redial_last_number(bd_addr_t bd_addr){
1371ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1372a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1373ce263fc8SMatthias Ringwald 
1374a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
1375a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1376ce263fc8SMatthias Ringwald }
1377ce263fc8SMatthias Ringwald 
1378ce263fc8SMatthias Ringwald void hfp_hf_activate_call_waiting_notification(bd_addr_t bd_addr){
1379ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1380a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1381ce263fc8SMatthias Ringwald 
1382a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
1383a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1384ce263fc8SMatthias Ringwald }
1385ce263fc8SMatthias Ringwald 
1386ce263fc8SMatthias Ringwald 
1387ce263fc8SMatthias Ringwald void hfp_hf_deactivate_call_waiting_notification(bd_addr_t bd_addr){
1388ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1389a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1390ce263fc8SMatthias Ringwald 
1391a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1392a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1393ce263fc8SMatthias Ringwald }
1394ce263fc8SMatthias Ringwald 
1395ce263fc8SMatthias Ringwald 
1396ce263fc8SMatthias Ringwald void hfp_hf_activate_calling_line_notification(bd_addr_t bd_addr){
1397ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1398a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1399ce263fc8SMatthias Ringwald 
1400a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
1401a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1402ce263fc8SMatthias Ringwald }
1403ce263fc8SMatthias Ringwald 
1404ce263fc8SMatthias Ringwald void hfp_hf_deactivate_calling_line_notification(bd_addr_t bd_addr){
1405ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1406a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1407ce263fc8SMatthias Ringwald 
1408a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
1409a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1410ce263fc8SMatthias Ringwald }
1411ce263fc8SMatthias Ringwald 
1412ce263fc8SMatthias Ringwald 
1413ce263fc8SMatthias Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1414ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1415a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1416ce263fc8SMatthias Ringwald 
1417a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1418a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1419ce263fc8SMatthias Ringwald }
1420ce263fc8SMatthias Ringwald 
1421ce263fc8SMatthias Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1422ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1423a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1424ce263fc8SMatthias Ringwald 
1425a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1426a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1427ce263fc8SMatthias Ringwald }
1428ce263fc8SMatthias Ringwald 
1429ce263fc8SMatthias Ringwald void hfp_hf_activate_voice_recognition_notification(bd_addr_t bd_addr){
1430ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1431a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1432ce263fc8SMatthias Ringwald 
1433a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
1434a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1435ce263fc8SMatthias Ringwald }
1436ce263fc8SMatthias Ringwald 
1437ce263fc8SMatthias Ringwald void hfp_hf_deactivate_voice_recognition_notification(bd_addr_t bd_addr){
1438ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1439a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1440ce263fc8SMatthias Ringwald 
1441a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1442a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1443ce263fc8SMatthias Ringwald }
1444ce263fc8SMatthias Ringwald 
1445ce263fc8SMatthias Ringwald void hfp_hf_set_microphone_gain(bd_addr_t bd_addr, int gain){
1446ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1447a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1448a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1449a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1450a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1451a0ffb263SMatthias Ringwald         return;
1452a0ffb263SMatthias Ringwald     }
1453a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1454a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1455a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1456ce263fc8SMatthias Ringwald }
1457ce263fc8SMatthias Ringwald 
1458ce263fc8SMatthias Ringwald void hfp_hf_set_speaker_gain(bd_addr_t bd_addr, int gain){
1459ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1460a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1461a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1462a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1463a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1464a0ffb263SMatthias Ringwald         return;
1465a0ffb263SMatthias Ringwald     }
1466a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1467a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1468a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1469ce263fc8SMatthias Ringwald }
1470ce263fc8SMatthias Ringwald 
1471ce263fc8SMatthias Ringwald void hfp_hf_send_dtmf_code(bd_addr_t addr, char code){
1472ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1473a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1474a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
1475a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1476ce263fc8SMatthias Ringwald }
1477ce263fc8SMatthias Ringwald 
1478ce263fc8SMatthias Ringwald void hfp_hf_request_phone_number_for_voice_tag(bd_addr_t addr){
1479ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1480a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1481a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
1482a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1483ce263fc8SMatthias Ringwald }
14843deb3ec6SMatthias Ringwald 
1485667ec068SMatthias Ringwald void hfp_hf_query_current_call_status(bd_addr_t addr){
1486667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1487a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1488a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
1489a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1490667ec068SMatthias Ringwald }
1491667ec068SMatthias Ringwald 
1492667ec068SMatthias Ringwald 
1493667ec068SMatthias Ringwald void hfp_hf_rrh_query_status(bd_addr_t addr){
1494667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1495a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1496a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1497a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
1498a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1499667ec068SMatthias Ringwald }
1500667ec068SMatthias Ringwald 
1501667ec068SMatthias Ringwald void hfp_hf_rrh_hold_call(bd_addr_t addr){
1502667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1503a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1504a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1505a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
1506a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1507667ec068SMatthias Ringwald }
1508667ec068SMatthias Ringwald 
1509667ec068SMatthias Ringwald void hfp_hf_rrh_accept_held_call(bd_addr_t addr){
1510667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1511a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1512a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1513a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
1514a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1515667ec068SMatthias Ringwald }
1516667ec068SMatthias Ringwald 
1517a0ffb263SMatthias Ringwald void hfp_hf_rrh_reject_held_call(bd_addr_t addr){
1518667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1519a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1520a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1521a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
1522a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1523667ec068SMatthias Ringwald }
1524667ec068SMatthias Ringwald 
1525a0ffb263SMatthias Ringwald void hfp_hf_query_subscriber_number(bd_addr_t addr){
1526667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1527a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1528a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
1529a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1530667ec068SMatthias Ringwald }
1531667ec068SMatthias Ringwald 
1532667ec068SMatthias Ringwald void hfp_hf_set_hf_indicator(bd_addr_t addr, int assigned_number, int value){
1533667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1534a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1535667ec068SMatthias Ringwald     // find index for assigned number
1536667ec068SMatthias Ringwald     int i;
1537667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1538667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1539667ec068SMatthias Ringwald             // set value
1540667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1541667ec068SMatthias Ringwald             // mark for update
1542a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1543a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1544667ec068SMatthias Ringwald                 // send update
1545a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1546a0ffb263SMatthias Ringwald             }
1547667ec068SMatthias Ringwald             return;
1548667ec068SMatthias Ringwald         }
1549667ec068SMatthias Ringwald     }
1550667ec068SMatthias Ringwald }
1551667ec068SMatthias Ringwald 
1552