xref: /btstack/src/classic/hfp_hf.c (revision 520c92d527d72961be25b6e424c5df3f67d1106f)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hfp_hf.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
42fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493deb3ec6SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <stdlib.h>
513deb3ec6SMatthias Ringwald #include <string.h>
523deb3ec6SMatthias Ringwald 
53235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
5459c6af15SMatthias Ringwald #include "btstack_debug.h"
553deb3ec6SMatthias Ringwald #include "btstack_memory.h"
5659c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5759c6af15SMatthias Ringwald #include "classic/core.h"
5859c6af15SMatthias Ringwald #include "classic/hfp.h"
5959c6af15SMatthias Ringwald #include "classic/hfp_hf.h"
60efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
61746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
62023f2764SMatthias Ringwald #include "classic/sdp_util.h"
6359c6af15SMatthias Ringwald #include "hci.h"
6459c6af15SMatthias Ringwald #include "hci_cmd.h"
6559c6af15SMatthias Ringwald #include "hci_dump.h"
6659c6af15SMatthias Ringwald #include "l2cap.h"
673deb3ec6SMatthias Ringwald 
683deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
693deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
703deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
713deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
723deb3ec6SMatthias Ringwald 
733deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0;
743deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS];
75667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS];
76667ec068SMatthias Ringwald 
77667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9;
78667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9;
793deb3ec6SMatthias Ringwald 
80ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
813deb3ec6SMatthias Ringwald 
82ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
83ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
84ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
85ce263fc8SMatthias Ringwald 
86ce263fc8SMatthias Ringwald static char phone_number[25];
87ce263fc8SMatthias Ringwald 
8813839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
893deb3ec6SMatthias Ringwald     if (callback == NULL){
903deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
913deb3ec6SMatthias Ringwald         return;
923deb3ec6SMatthias Ringwald     }
93ca59be51SMatthias Ringwald     hfp_hf_callback = callback;
94ca59be51SMatthias Ringwald     hfp_set_hf_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;
105adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 6) ? (int) strlen(bnip_number) : (int) 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;
118adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 5) ? (int) strlen(bnip_number) : (int) 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;
136adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 10) ? (int) strlen(bnip_number) : (int) 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 
1614f84bf36SMatthias Ringwald void hfp_hf_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint16_t supported_features, int wide_band_speech){
1623deb3ec6SMatthias Ringwald     if (!name){
1633deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1643deb3ec6SMatthias Ringwald     }
165235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
1663deb3ec6SMatthias Ringwald 
1674f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
1684f84bf36SMatthias Ringwald     //
1694f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1704f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
1714f84bf36SMatthias Ringwald     //
1724f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
1734f84bf36SMatthias Ringwald     if (supported_features & wide_band_speech){
1744f84bf36SMatthias Ringwald         sdp_features |= 1 << 5; // Wide band speech bit
1754f84bf36SMatthias Ringwald     }
176aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
1774f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
178aa4dd815SMatthias Ringwald }
1793deb3ec6SMatthias Ringwald 
18089425bfcSMilanka Ringwald 
18189425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
1823deb3ec6SMatthias Ringwald     char buffer[20];
18389425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r\n", cmd);
18489425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
18589425bfcSMilanka Ringwald }
18689425bfcSMilanka Ringwald 
18789425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
18889425bfcSMilanka Ringwald     char buffer[20];
18989425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r\n", cmd, mark);
19089425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
19189425bfcSMilanka Ringwald }
19289425bfcSMilanka Ringwald 
19389425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
19489425bfcSMilanka Ringwald     char buffer[40];
19589425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r\n", cmd, value);
1963deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1973deb3ec6SMatthias Ringwald }
1983deb3ec6SMatthias Ringwald 
1993deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2003deb3ec6SMatthias Ringwald     char buffer[30];
20189425bfcSMilanka Ringwald     const int size = sizeof(buffer);
20289425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
20389425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
20489425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2053deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2063deb3ec6SMatthias Ringwald }
2073deb3ec6SMatthias Ringwald 
2083deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2093deb3ec6SMatthias Ringwald     char buffer[50];
21089425bfcSMilanka Ringwald     const int size = sizeof(buffer);
21189425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
21289425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
21389425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2143deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2153deb3ec6SMatthias Ringwald }
2163deb3ec6SMatthias Ringwald 
2173deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2183deb3ec6SMatthias Ringwald     char buffer[30];
21989425bfcSMilanka Ringwald     const int size = sizeof(buffer);
22089425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
22189425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
22289425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2233deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2243deb3ec6SMatthias Ringwald }
2253deb3ec6SMatthias Ringwald 
22689425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2273deb3ec6SMatthias Ringwald     char buffer[20];
22889425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
229ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
230ce263fc8SMatthias Ringwald }
231ce263fc8SMatthias Ringwald 
232ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
233ce263fc8SMatthias Ringwald     char buffer[40];
23489425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
235ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
236ce263fc8SMatthias Ringwald }
237ce263fc8SMatthias Ringwald 
238a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
239ce263fc8SMatthias Ringwald     char buffer[40];
24089425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id);
241ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
242ce263fc8SMatthias Ringwald }
243ce263fc8SMatthias Ringwald 
244f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
24589425bfcSMilanka Ringwald     char buffer[40];
24689425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
247ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
248ce263fc8SMatthias Ringwald }
249ce263fc8SMatthias Ringwald 
250ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
251ce263fc8SMatthias Ringwald     char buffer[20];
25289425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
253ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
254ce263fc8SMatthias Ringwald }
255ce263fc8SMatthias Ringwald 
25689425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
25789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
25889425bfcSMilanka Ringwald }
25989425bfcSMilanka Ringwald 
26089425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
26189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
26289425bfcSMilanka Ringwald }
26389425bfcSMilanka Ringwald 
26489425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
26589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
26689425bfcSMilanka Ringwald }
26789425bfcSMilanka Ringwald 
26889425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
26989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
27089425bfcSMilanka Ringwald }
27189425bfcSMilanka Ringwald 
27289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
27389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
27489425bfcSMilanka Ringwald }
27589425bfcSMilanka Ringwald 
27689425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
27789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
27889425bfcSMilanka Ringwald }
27989425bfcSMilanka Ringwald 
28089425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
28189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
28289425bfcSMilanka Ringwald }
28389425bfcSMilanka Ringwald 
28489425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
28589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
28689425bfcSMilanka Ringwald }
28789425bfcSMilanka Ringwald 
28889425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
28989425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
29089425bfcSMilanka Ringwald }
29189425bfcSMilanka Ringwald 
29289425bfcSMilanka Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
29389425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_CALL_ANSWERED);
29489425bfcSMilanka Ringwald }
29589425bfcSMilanka Ringwald 
29689425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
29789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
29889425bfcSMilanka Ringwald }
29989425bfcSMilanka Ringwald 
30089425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
30189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
30289425bfcSMilanka Ringwald }
30389425bfcSMilanka Ringwald 
30489425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
30589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
30689425bfcSMilanka Ringwald }
30789425bfcSMilanka Ringwald 
30889425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
30989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
31089425bfcSMilanka Ringwald }
31189425bfcSMilanka Ringwald 
31289425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
31389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
31489425bfcSMilanka Ringwald }
31589425bfcSMilanka Ringwald 
31689425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
31789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
31889425bfcSMilanka Ringwald }
31989425bfcSMilanka Ringwald 
32089425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
32189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
32289425bfcSMilanka Ringwald }
32389425bfcSMilanka Ringwald 
32489425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
32589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
32689425bfcSMilanka Ringwald }
32789425bfcSMilanka Ringwald 
32889425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
32989425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
33089425bfcSMilanka Ringwald }
33189425bfcSMilanka Ringwald 
33289425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
33389425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
33489425bfcSMilanka Ringwald }
33589425bfcSMilanka Ringwald 
336ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
33789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
338ce263fc8SMatthias Ringwald }
339ce263fc8SMatthias Ringwald 
340667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
34189425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
342667ec068SMatthias Ringwald }
343667ec068SMatthias Ringwald 
34413839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){
3453deb3ec6SMatthias Ringwald     if (!callback) return;
346a0ffb263SMatthias Ringwald     uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1];
3473deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3483deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3493deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
350a0ffb263SMatthias Ringwald     event[3] = indicator.index;
351a0ffb263SMatthias Ringwald     event[4] = indicator.status;
352a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
353a0ffb263SMatthias Ringwald     event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0;
35413839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3553deb3ec6SMatthias Ringwald }
3563deb3ec6SMatthias Ringwald 
35713839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){
3583deb3ec6SMatthias Ringwald     if (!callback) return;
35989425bfcSMilanka Ringwald     uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
3603deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3613deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3623deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
363a0ffb263SMatthias Ringwald     event[3] = network_operator.mode;
364a0ffb263SMatthias Ringwald     event[4] = network_operator.format;
36589425bfcSMilanka Ringwald     strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
36689425bfcSMilanka Ringwald     event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
36713839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3683deb3ec6SMatthias Ringwald }
3693deb3ec6SMatthias Ringwald 
370a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
371a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
372a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
373aa4dd815SMatthias Ringwald     int done = 1;
3743deb3ec6SMatthias Ringwald 
375a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
3763deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
377d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
378a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
379a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
3803deb3ec6SMatthias Ringwald             break;
3813deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
382a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
383a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
3843deb3ec6SMatthias Ringwald             break;
3853deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
386a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
387a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
3883deb3ec6SMatthias Ringwald             break;
3893deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
390a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
391a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
3923deb3ec6SMatthias Ringwald             break;
3933deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
394a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
395a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
3963deb3ec6SMatthias Ringwald             break;
3973deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
398a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
399a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4003deb3ec6SMatthias Ringwald             break;
4013deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
402a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
403a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4043deb3ec6SMatthias Ringwald             break;
4053deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
406a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
407a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4083deb3ec6SMatthias Ringwald             break;
4093deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
410a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
411a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4123deb3ec6SMatthias Ringwald             break;
4133deb3ec6SMatthias Ringwald         default:
414aa4dd815SMatthias Ringwald             done = 0;
4153deb3ec6SMatthias Ringwald             break;
4163deb3ec6SMatthias Ringwald     }
4173deb3ec6SMatthias Ringwald     return done;
4183deb3ec6SMatthias Ringwald }
4193deb3ec6SMatthias Ringwald 
420ce263fc8SMatthias Ringwald 
421a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
422a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
423a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
424ce263fc8SMatthias Ringwald 
425ce263fc8SMatthias Ringwald     int done = 0;
426a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
427a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
428ce263fc8SMatthias Ringwald         done = 1;
429a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
430ce263fc8SMatthias Ringwald         return done;
431ce263fc8SMatthias Ringwald     };
432a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
433a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
434ce263fc8SMatthias Ringwald         done = 1;
435a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
436a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
437a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
438ce263fc8SMatthias Ringwald         return done;
439ce263fc8SMatthias Ringwald     }
440ce263fc8SMatthias Ringwald 
441a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
442ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
443a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
444a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
445a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
446ce263fc8SMatthias Ringwald             return 1;
447ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
448a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
449a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
450a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
451ce263fc8SMatthias Ringwald             return 1;
452ce263fc8SMatthias Ringwald         default:
453ce263fc8SMatthias Ringwald             break;
454ce263fc8SMatthias Ringwald     }
455ce263fc8SMatthias Ringwald 
456a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
457a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
458ce263fc8SMatthias Ringwald         done = 1;
459a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
460ce263fc8SMatthias Ringwald         return done;
461ce263fc8SMatthias Ringwald     }
462ce263fc8SMatthias Ringwald 
463ce263fc8SMatthias Ringwald     return done;
464ce263fc8SMatthias Ringwald }
465ce263fc8SMatthias Ringwald 
466a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
467ce263fc8SMatthias Ringwald     /* events ( == commands):
468ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
469ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
470ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
471ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
472ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
473ce263fc8SMatthias Ringwald     */
474ce263fc8SMatthias Ringwald 
475a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
476ce263fc8SMatthias Ringwald 
477a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
478ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
479a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
480ce263fc8SMatthias Ringwald 
481a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
482a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
483a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
484ce263fc8SMatthias Ringwald             return 1;
485ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
486a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = 0;
487a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = 0;
488a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = 0;
489ce263fc8SMatthias Ringwald 
490a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
491a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
492a0ffb263SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
493ce263fc8SMatthias Ringwald             break;
494ce263fc8SMatthias Ringwald 
4956a7f44bdSMilanka Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:{
4966a7f44bdSMilanka Ringwald             if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
497a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
498a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
499a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
5000e0be203SMatthias Ringwald                 hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
5010e0be203SMatthias Ringwald                 log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
502fcb08cdbSMilanka Ringwald                 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
503ce263fc8SMatthias Ringwald             } else {
504a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
505a0ffb263SMatthias Ringwald                 hfp_connection->suggested_codec = 0;
506a0ffb263SMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
507a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
508a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
509a0ffb263SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
510ce263fc8SMatthias Ringwald 
511ce263fc8SMatthias Ringwald             }
512ce263fc8SMatthias Ringwald             break;
5136a7f44bdSMilanka Ringwald         }
514ce263fc8SMatthias Ringwald         default:
515ce263fc8SMatthias Ringwald             break;
516ce263fc8SMatthias Ringwald     }
517ce263fc8SMatthias Ringwald     return 0;
518ce263fc8SMatthias Ringwald }
519ce263fc8SMatthias Ringwald 
520a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
521a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
522a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
523ce263fc8SMatthias Ringwald 
524ce263fc8SMatthias Ringwald 
525a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
526a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
527a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
528a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
529ce263fc8SMatthias Ringwald         return 1;
530ce263fc8SMatthias Ringwald     }
531ce263fc8SMatthias Ringwald 
532a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
533ce263fc8SMatthias Ringwald 
534ce263fc8SMatthias Ringwald     // run codecs exchange
535a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
536ce263fc8SMatthias Ringwald     if (done) return 1;
537ce263fc8SMatthias Ringwald 
538ce263fc8SMatthias Ringwald     return 0;
539ce263fc8SMatthias Ringwald }
540ce263fc8SMatthias Ringwald 
541a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
542a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
543a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
544a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
545ce263fc8SMatthias Ringwald         return 1;
546ce263fc8SMatthias Ringwald     }
547ce263fc8SMatthias Ringwald     return 0;
548ce263fc8SMatthias Ringwald }
549ce263fc8SMatthias Ringwald 
550a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){
551a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
552724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
5537522e673SMatthias Ringwald 
554b72c4a9eSMatthias Ringwald     if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){
555b72c4a9eSMatthias Ringwald 
556b72c4a9eSMatthias Ringwald         hfp_connection->hf_accept_sco = 0;
5577522e673SMatthias Ringwald 
5587522e673SMatthias Ringwald         // notify about codec selection if not done already
5597522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
5607522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
5617522e673SMatthias Ringwald         }
5627522e673SMatthias Ringwald 
5637522e673SMatthias Ringwald         // remote supported feature eSCO is set if link type is eSCO
5647522e673SMatthias Ringwald         // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
5657522e673SMatthias Ringwald         uint16_t max_latency;
5667522e673SMatthias Ringwald         uint8_t  retransmission_effort;
5677522e673SMatthias Ringwald         uint16_t packet_types;
5687522e673SMatthias Ringwald 
569d9f77559SMatthias Ringwald         if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
5707522e673SMatthias Ringwald             max_latency = 0x000c;
5717522e673SMatthias Ringwald             retransmission_effort = 0x02;
5727522e673SMatthias Ringwald             packet_types = 0x388;
5737522e673SMatthias Ringwald         } else {
5747522e673SMatthias Ringwald             max_latency = 0xffff;
5757522e673SMatthias Ringwald             retransmission_effort = 0xff;
5767522e673SMatthias Ringwald             packet_types = 0x003f;
5777522e673SMatthias Ringwald         }
5787522e673SMatthias Ringwald 
5797522e673SMatthias Ringwald         uint16_t sco_voice_setting = hci_get_sco_voice_setting();
5807522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
5817522e673SMatthias Ringwald             sco_voice_setting = 0x0043; // Transparent data
5827522e673SMatthias Ringwald         }
5837522e673SMatthias Ringwald 
584b72c4a9eSMatthias Ringwald         log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting);
5857522e673SMatthias Ringwald         hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
5867522e673SMatthias Ringwald                         sco_voice_setting, retransmission_effort, packet_types);
5877522e673SMatthias Ringwald         return;
5887522e673SMatthias Ringwald     }
5897522e673SMatthias Ringwald 
590a0ffb263SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return;
591ce263fc8SMatthias Ringwald 
592a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
593ce263fc8SMatthias Ringwald     if (!done){
594a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
595ce263fc8SMatthias Ringwald     }
596ce263fc8SMatthias Ringwald     if (!done){
597a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
598ce263fc8SMatthias Ringwald     }
599ce263fc8SMatthias Ringwald     if (!done){
600a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
601ce263fc8SMatthias Ringwald     }
602ce263fc8SMatthias Ringwald 
6031016a228SMatthias Ringwald     // don't send a new command while ok still pending
6041016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6051016a228SMatthias Ringwald 
606a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
607a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
608a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
609a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
610ce263fc8SMatthias Ringwald         return;
611ce263fc8SMatthias Ringwald     }
612ce263fc8SMatthias Ringwald 
613a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
614a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
615a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
616a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
617ce263fc8SMatthias Ringwald         return;
618ce263fc8SMatthias Ringwald     }
619ce263fc8SMatthias Ringwald 
620a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
621a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
622a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
623a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
624ce263fc8SMatthias Ringwald         return;
625ce263fc8SMatthias Ringwald     }
626ce263fc8SMatthias Ringwald 
627a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
628a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
629a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
630a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
631ce263fc8SMatthias Ringwald         return;
632ce263fc8SMatthias Ringwald     }
633ce263fc8SMatthias Ringwald 
634a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
635a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
636a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
637a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
638ce263fc8SMatthias Ringwald         return;
639ce263fc8SMatthias Ringwald     }
640ce263fc8SMatthias Ringwald 
641a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
642a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
643a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
644a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
645ce263fc8SMatthias Ringwald         return;
646ce263fc8SMatthias Ringwald     }
647ce263fc8SMatthias Ringwald 
648a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
649a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
650a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
651a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
652ce263fc8SMatthias Ringwald         return;
653ce263fc8SMatthias Ringwald     }
654ce263fc8SMatthias Ringwald 
655a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
656a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
657a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
658a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
659ce263fc8SMatthias Ringwald         return;
660ce263fc8SMatthias Ringwald     }
661ce263fc8SMatthias Ringwald 
662ce263fc8SMatthias Ringwald 
663a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
664a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
665a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
666a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
667ce263fc8SMatthias Ringwald         return;
668ce263fc8SMatthias Ringwald     }
669ce263fc8SMatthias Ringwald 
670a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
671a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
672a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
673a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
674ce263fc8SMatthias Ringwald         return;
675ce263fc8SMatthias Ringwald     }
676ce263fc8SMatthias Ringwald 
677a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
678a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
679a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
680a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
681ce263fc8SMatthias Ringwald         return;
682ce263fc8SMatthias Ringwald     }
683ce263fc8SMatthias Ringwald 
684a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
685a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
686a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
687a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
688ce263fc8SMatthias Ringwald         return;
689ce263fc8SMatthias Ringwald     }
690ce263fc8SMatthias Ringwald 
691a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
692a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
693a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
694a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
695ce263fc8SMatthias Ringwald         return;
696ce263fc8SMatthias Ringwald     }
697ce263fc8SMatthias Ringwald 
698a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
699a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
700a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
701a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
702ce263fc8SMatthias Ringwald         return;
703ce263fc8SMatthias Ringwald     }
704ce263fc8SMatthias Ringwald 
705a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
706a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
707a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
708a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
709ce263fc8SMatthias Ringwald         return;
710ce263fc8SMatthias Ringwald     }
711ce263fc8SMatthias Ringwald 
712a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
713a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
714a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
715a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
716ce263fc8SMatthias Ringwald         return;
717ce263fc8SMatthias Ringwald     }
718ce263fc8SMatthias Ringwald 
719a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
720a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
721a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
722a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
723ce263fc8SMatthias Ringwald         return;
724ce263fc8SMatthias Ringwald     }
725ce263fc8SMatthias Ringwald 
726a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
727a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
728a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
729a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
730ce263fc8SMatthias Ringwald         return;
731ce263fc8SMatthias Ringwald     }
732ce263fc8SMatthias Ringwald 
733a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
734a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
735a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
736a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
737ce263fc8SMatthias Ringwald         return;
738ce263fc8SMatthias Ringwald     }
739ce263fc8SMatthias Ringwald 
740a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
741a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
742a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
743a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
744667ec068SMatthias Ringwald         return;
745667ec068SMatthias Ringwald     }
746667ec068SMatthias Ringwald 
747a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
748a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
749a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
750a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
751a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
752ce263fc8SMatthias Ringwald         return;
753ce263fc8SMatthias Ringwald     }
754ce263fc8SMatthias Ringwald 
755a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
756a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
757a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
758a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
759ce263fc8SMatthias Ringwald         return;
760ce263fc8SMatthias Ringwald     }
761ce263fc8SMatthias Ringwald 
762a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
763a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
764a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
765a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
766667ec068SMatthias Ringwald         return;
767667ec068SMatthias Ringwald     }
768667ec068SMatthias Ringwald 
769a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
770a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
771667ec068SMatthias Ringwald         char buffer[20];
772a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
773667ec068SMatthias Ringwald             case '?':
774667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
775a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
776667ec068SMatthias Ringwald                 return;
777667ec068SMatthias Ringwald             case '0':
778667ec068SMatthias Ringwald             case '1':
779667ec068SMatthias Ringwald             case '2':
780a0ffb263SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
781a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
782667ec068SMatthias Ringwald                 return;
783667ec068SMatthias Ringwald             default:
784667ec068SMatthias Ringwald                 break;
785667ec068SMatthias Ringwald         }
786667ec068SMatthias Ringwald         return;
787667ec068SMatthias Ringwald     }
788667ec068SMatthias Ringwald 
789a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
790a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
791667ec068SMatthias Ringwald         char buffer[20];
792667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
793a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
794667ec068SMatthias Ringwald         return;
795667ec068SMatthias Ringwald     }
796667ec068SMatthias Ringwald 
797667ec068SMatthias Ringwald     // update HF indicators
798a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
799667ec068SMatthias Ringwald         int i;
800667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
801a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
802a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
803a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
804a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
805667ec068SMatthias Ringwald                     char buffer[30];
806ecb7d461SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]);
807a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
808667ec068SMatthias Ringwald                 } else {
80960ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
810667ec068SMatthias Ringwald                 }
811667ec068SMatthias Ringwald                 return;
812667ec068SMatthias Ringwald             }
813667ec068SMatthias Ringwald         }
814667ec068SMatthias Ringwald     }
815667ec068SMatthias Ringwald 
816ce263fc8SMatthias Ringwald     if (done) return;
817ce263fc8SMatthias Ringwald     // deal with disconnect
818a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
819ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
820a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
821a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
822ce263fc8SMatthias Ringwald             break;
823ce263fc8SMatthias Ringwald 
824ce263fc8SMatthias Ringwald         default:
825ce263fc8SMatthias Ringwald             break;
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;
8316a7f44bdSMilanka Ringwald 
832ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
8337522e673SMatthias Ringwald 
834667ec068SMatthias Ringwald     // restore volume settings
835a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
836a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
837ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, 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;
840ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, 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;
906ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, 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;
912ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, 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:
91860ebb071SMilanka 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;
923ca59be51SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_hf_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;
940ce263fc8SMatthias Ringwald                     break;
9413deb3ec6SMatthias Ringwald                 default:
9423deb3ec6SMatthias Ringwald                     break;
9433deb3ec6SMatthias Ringwald             }
9443deb3ec6SMatthias Ringwald             break;
9453deb3ec6SMatthias Ringwald         default:
9463deb3ec6SMatthias Ringwald             break;
9473deb3ec6SMatthias Ringwald     }
9483deb3ec6SMatthias Ringwald 
9493deb3ec6SMatthias Ringwald     // done
950a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
9513deb3ec6SMatthias Ringwald }
9523deb3ec6SMatthias Ringwald 
9533deb3ec6SMatthias Ringwald 
954e9c22d4eSMatthias Ringwald static void hfp_hf_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
9558a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
9568a46ec40SMatthias Ringwald 
9578a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
9588a46ec40SMatthias Ringwald     if (size < 1) return;
9599ec2630cSMatthias Ringwald 
960a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
961a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
9623deb3ec6SMatthias Ringwald 
9638a46ec40SMatthias Ringwald     // temp overwrite last byte (most likely \n for log_info)
9641e35c04dSMatthias Ringwald     char last_char = packet[size-1];
9651e35c04dSMatthias Ringwald     packet[size-1] = 0;
9661e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
9671e35c04dSMatthias Ringwald     packet[size-1] = last_char;
9681e35c04dSMatthias Ringwald 
9698a46ec40SMatthias Ringwald     // process messages byte-wise
970667ec068SMatthias Ringwald     int pos, i, value;
9713deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
972a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
973ce263fc8SMatthias Ringwald     }
9743deb3ec6SMatthias Ringwald 
975a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
976667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
977a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
978a0ffb263SMatthias Ringwald             // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type);
979ca59be51SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
980667ec068SMatthias Ringwald             break;
981667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
982a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
983a0ffb263SMatthias Ringwald             // printf("Response and Hold status: %s\n", hfp_connection->line_buffer);
984ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
985667ec068SMatthias Ringwald             break;
986667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
987a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
988a0ffb263SMatthias Ringwald             // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
989a0ffb263SMatthias Ringwald             //      hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
990a0ffb263SMatthias Ringwald             //      hfp_connection->bnip_number, hfp_connection->bnip_type);
991ca59be51SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection->clcc_idx,
992a0ffb263SMatthias Ringwald                 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
993a0ffb263SMatthias Ringwald                 hfp_connection->bnip_type, hfp_connection->bnip_number);
994667ec068SMatthias Ringwald             break;
995ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
996a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
9972308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
998667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
999ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1000ce263fc8SMatthias Ringwald             break;
1001ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1002a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10032308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1004667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1005ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1006ce263fc8SMatthias Ringwald             break;
1007ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1008a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1009ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1010a0ffb263SMatthias Ringwald             break;
1011a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1012a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1013ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1014a0ffb263SMatthias Ringwald             break;
1015a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1016a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1017ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1018ce263fc8SMatthias Ringwald             break;
1019ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1020a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1021a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1022a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1023ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1024ce263fc8SMatthias Ringwald             break;
1025ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1026a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1027a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1028a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1029ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1030ce263fc8SMatthias Ringwald             break;
1031ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1032a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1033ce263fc8SMatthias Ringwald             break;
1034ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
1035ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1036ce263fc8SMatthias Ringwald             break;
1037ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1038a0ffb263SMatthias Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1039a0ffb263SMatthias Ringwald                 if (hfp_connection->ag_indicators[i].status_changed) {
1040a0ffb263SMatthias Ringwald                     if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1041a0ffb263SMatthias Ringwald                         hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1042a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1043a0ffb263SMatthias Ringwald                         hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1044d5ff3b26SMatthias Ringwald                         // avoid set but not used warning
1045d5ff3b26SMatthias Ringwald                         (void) hfp_callheld_status;
1046a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1047a0ffb263SMatthias Ringwald                         hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1048ce263fc8SMatthias Ringwald                     }
1049a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[i].status_changed = 0;
1050ca59be51SMatthias Ringwald                     hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]);
10513deb3ec6SMatthias Ringwald                     break;
10523deb3ec6SMatthias Ringwald                 }
10533deb3ec6SMatthias Ringwald             }
1054ce263fc8SMatthias Ringwald             break;
1055ce263fc8SMatthias Ringwald         default:
1056ce263fc8SMatthias Ringwald             break;
10573deb3ec6SMatthias Ringwald     }
1058a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
10593deb3ec6SMatthias Ringwald }
10603deb3ec6SMatthias Ringwald 
10610cb5b971SMatthias Ringwald static void hfp_run(void){
1062665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1063665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1064665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1065a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1066a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
10673deb3ec6SMatthias Ringwald     }
10683deb3ec6SMatthias Ringwald }
10693deb3ec6SMatthias Ringwald 
1070e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10713deb3ec6SMatthias Ringwald     switch (packet_type){
10723deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1073e9c22d4eSMatthias Ringwald             hfp_hf_handle_rfcomm_event(packet_type, channel, packet, size);
10743deb3ec6SMatthias Ringwald             break;
10753deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1076323d3000SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1077202c8a4cSMatthias Ringwald             break;
10783deb3ec6SMatthias Ringwald         default:
10793deb3ec6SMatthias Ringwald             break;
10803deb3ec6SMatthias Ringwald     }
10813deb3ec6SMatthias Ringwald     hfp_run();
10823deb3ec6SMatthias Ringwald }
10833deb3ec6SMatthias Ringwald 
1084a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1085*520c92d5SMatthias Ringwald     hfp_init();
1086d63c37a1SMatthias Ringwald 
1087e9c22d4eSMatthias Ringwald     rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1088e9c22d4eSMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&rfcomm_packet_handler);
1089d68dcce1SMatthias Ringwald 
1090a0ffb263SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1091a0ffb263SMatthias Ringwald     hfp_codecs_nr = 0;
1092a0ffb263SMatthias Ringwald     hfp_indicators_nr = 0;
1093a0ffb263SMatthias Ringwald     hfp_hf_speaker_gain = 9;
1094a0ffb263SMatthias Ringwald     hfp_hf_microphone_gain = 9;
1095a0ffb263SMatthias Ringwald }
1096a0ffb263SMatthias Ringwald 
1097a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
10983deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1099a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
11003deb3ec6SMatthias Ringwald         return;
11013deb3ec6SMatthias Ringwald     }
11023deb3ec6SMatthias Ringwald 
11033deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
11043deb3ec6SMatthias Ringwald     int i;
11053deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
11063deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
11073deb3ec6SMatthias Ringwald     }
11083deb3ec6SMatthias Ringwald }
11093deb3ec6SMatthias Ringwald 
1110a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
11113deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1112a0ffb263SMatthias Ringwald }
11133deb3ec6SMatthias Ringwald 
1114a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
11153deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
11163deb3ec6SMatthias Ringwald     int i;
1117a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
11183deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
11193deb3ec6SMatthias Ringwald     }
11203deb3ec6SMatthias Ringwald }
11213deb3ec6SMatthias Ringwald 
11223deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1123323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
11243deb3ec6SMatthias Ringwald }
11253deb3ec6SMatthias Ringwald 
1126c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
1127c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1128a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1129a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1130a33eb0c4SMilanka Ringwald         return;
1131a33eb0c4SMilanka Ringwald     }
1132a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
1133a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11343deb3ec6SMatthias Ringwald }
11353deb3ec6SMatthias Ringwald 
1136c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
1137c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1138a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1139a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11403deb3ec6SMatthias Ringwald         return;
11413deb3ec6SMatthias Ringwald     }
1142a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
1143a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11443deb3ec6SMatthias Ringwald }
11453deb3ec6SMatthias Ringwald 
1146c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1147c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1148ce263fc8SMatthias Ringwald }
1149ce263fc8SMatthias Ringwald 
1150c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1151c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1152ce263fc8SMatthias Ringwald }
1153ce263fc8SMatthias Ringwald 
11543deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1155c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
1156c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1157a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1158a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11593deb3ec6SMatthias Ringwald         return;
11603deb3ec6SMatthias Ringwald     }
1161a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1162a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1163a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11643deb3ec6SMatthias Ringwald }
11653deb3ec6SMatthias Ringwald 
1166c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
1167c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1168a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1169a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11703deb3ec6SMatthias Ringwald         return;
11713deb3ec6SMatthias Ringwald     }
1172a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1173ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1174a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1175ce263fc8SMatthias Ringwald             break;
1176ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1177a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1178ce263fc8SMatthias Ringwald             break;
1179ce263fc8SMatthias Ringwald         default:
1180ce263fc8SMatthias Ringwald             break;
1181ce263fc8SMatthias Ringwald     }
1182a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11833deb3ec6SMatthias Ringwald }
11843deb3ec6SMatthias Ringwald 
1185c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
1186c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1187a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1188a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11893deb3ec6SMatthias Ringwald         return;
11903deb3ec6SMatthias Ringwald     }
1191a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1192a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11933deb3ec6SMatthias Ringwald }
11943deb3ec6SMatthias Ringwald 
1195ce263fc8SMatthias Ringwald 
1196c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1197c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1198ce263fc8SMatthias Ringwald }
1199ce263fc8SMatthias Ringwald 
1200c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1201c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1202ce263fc8SMatthias Ringwald }
1203ce263fc8SMatthias Ringwald 
1204ce263fc8SMatthias Ringwald 
1205c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
1206c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1207a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1208a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1209a33eb0c4SMilanka Ringwald         return;
1210a33eb0c4SMilanka Ringwald     }
1211a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1212ce263fc8SMatthias Ringwald 
1213a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1214a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
12153deb3ec6SMatthias Ringwald 
1216a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1217ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1218a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1219a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1220ce263fc8SMatthias Ringwald     } else {
1221a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1222aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1223aa4dd815SMatthias Ringwald                 break;
1224aa4dd815SMatthias Ringwald             default:
1225a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1226aa4dd815SMatthias Ringwald                 break;
12273deb3ec6SMatthias Ringwald         }
1228ce263fc8SMatthias Ringwald     }
1229ce263fc8SMatthias Ringwald 
1230a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12313deb3ec6SMatthias Ringwald }
12323deb3ec6SMatthias Ringwald 
1233c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
1234c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1235a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1236a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1237a33eb0c4SMilanka Ringwald         return;
1238a33eb0c4SMilanka Ringwald     }
1239a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
1240a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12413deb3ec6SMatthias Ringwald }
12423deb3ec6SMatthias Ringwald 
1243c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
1244c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1245a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1246a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1247a33eb0c4SMilanka Ringwald         return;
1248a33eb0c4SMilanka Ringwald     }
1249ce263fc8SMatthias Ringwald 
1250ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1251a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
1252a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1253ce263fc8SMatthias Ringwald     } else {
1254ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1255ce263fc8SMatthias Ringwald     }
1256ce263fc8SMatthias Ringwald }
1257ce263fc8SMatthias Ringwald 
1258c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
1259c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1260a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1261a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1262a33eb0c4SMilanka Ringwald         return;
1263a33eb0c4SMilanka Ringwald     }
1264a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
1265a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1266ce263fc8SMatthias Ringwald }
1267ce263fc8SMatthias Ringwald 
1268c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
1269c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1270a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1271a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1272a33eb0c4SMilanka Ringwald         return;
1273a33eb0c4SMilanka Ringwald     }
1274ce263fc8SMatthias Ringwald 
1275ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1276a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
1277a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1278ce263fc8SMatthias Ringwald     }
1279ce263fc8SMatthias Ringwald }
1280ce263fc8SMatthias Ringwald 
1281c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){
1282c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1283a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1284a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1285a33eb0c4SMilanka Ringwald         return;
1286a33eb0c4SMilanka Ringwald     }
1287ce263fc8SMatthias Ringwald 
1288ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1289a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
1290a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1291ce263fc8SMatthias Ringwald     }
1292ce263fc8SMatthias Ringwald }
1293ce263fc8SMatthias Ringwald 
1294c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
1295c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1296a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1297a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1298a33eb0c4SMilanka Ringwald         return;
1299a33eb0c4SMilanka Ringwald     }
1300ce263fc8SMatthias Ringwald 
1301ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1302ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1303a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
1304a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1305ce263fc8SMatthias Ringwald     }
1306ce263fc8SMatthias Ringwald }
1307ce263fc8SMatthias Ringwald 
1308c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){
1309c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1310a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1311a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1312a33eb0c4SMilanka Ringwald         return;
1313a33eb0c4SMilanka Ringwald     }
1314ce263fc8SMatthias Ringwald 
1315ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1316ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1317a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
1318a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1319ce263fc8SMatthias Ringwald     }
1320ce263fc8SMatthias Ringwald }
1321ce263fc8SMatthias Ringwald 
1322c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
1323c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1324a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1325a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1326a33eb0c4SMilanka Ringwald         return;
1327a33eb0c4SMilanka Ringwald     }
1328ce263fc8SMatthias Ringwald 
1329ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1330ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1331a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
1332a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1333ce263fc8SMatthias Ringwald     }
1334ce263fc8SMatthias Ringwald }
1335ce263fc8SMatthias Ringwald 
1336c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
1337c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1338a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1339a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1340a33eb0c4SMilanka Ringwald         return;
1341a33eb0c4SMilanka Ringwald     }
1342ce263fc8SMatthias Ringwald 
1343ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1344ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1345a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
1346a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1347ce263fc8SMatthias Ringwald     }
1348ce263fc8SMatthias Ringwald }
1349ce263fc8SMatthias Ringwald 
1350c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
1351c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1352a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1353a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1354a33eb0c4SMilanka Ringwald         return;
1355a33eb0c4SMilanka Ringwald     }
1356667ec068SMatthias Ringwald 
1357667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1358667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1359a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1360a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
1361a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1362667ec068SMatthias Ringwald     }
1363667ec068SMatthias Ringwald }
1364667ec068SMatthias Ringwald 
1365c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
1366c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1367a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1368a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1369a33eb0c4SMilanka Ringwald         return;
1370a33eb0c4SMilanka Ringwald     }
1371667ec068SMatthias Ringwald 
1372667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1373667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1374a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1375a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
1376a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1377667ec068SMatthias Ringwald     }
1378667ec068SMatthias Ringwald }
1379ce263fc8SMatthias Ringwald 
1380c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
1381c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1382a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1383a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1384a33eb0c4SMilanka Ringwald         return;
1385a33eb0c4SMilanka Ringwald     }
1386ce263fc8SMatthias Ringwald 
1387a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1388ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1389a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1390ce263fc8SMatthias Ringwald }
1391ce263fc8SMatthias Ringwald 
1392c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
1393c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1394a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1395a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1396a33eb0c4SMilanka Ringwald         return;
1397a33eb0c4SMilanka Ringwald     }
1398ce263fc8SMatthias Ringwald 
1399a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1400a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1401a0ffb263SMatthias Ringwald 
1402a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1403ce263fc8SMatthias Ringwald }
1404ce263fc8SMatthias Ringwald 
1405c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
1406c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1407a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1408a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1409a33eb0c4SMilanka Ringwald         return;
1410a33eb0c4SMilanka Ringwald     }
1411ce263fc8SMatthias Ringwald 
1412a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
1413a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1414ce263fc8SMatthias Ringwald }
1415ce263fc8SMatthias Ringwald 
1416c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
1417c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1418a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1419a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1420a33eb0c4SMilanka Ringwald         return;
1421a33eb0c4SMilanka Ringwald     }
1422ce263fc8SMatthias Ringwald 
1423a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
1424a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1425ce263fc8SMatthias Ringwald }
1426ce263fc8SMatthias Ringwald 
1427ce263fc8SMatthias Ringwald 
1428c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
1429c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1430a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1431a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1432a33eb0c4SMilanka Ringwald         return;
1433a33eb0c4SMilanka Ringwald     }
1434ce263fc8SMatthias Ringwald 
1435a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1436a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1437ce263fc8SMatthias Ringwald }
1438ce263fc8SMatthias Ringwald 
1439ce263fc8SMatthias Ringwald 
1440c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
1441c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1442a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1443a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1444a33eb0c4SMilanka Ringwald         return;
1445a33eb0c4SMilanka Ringwald     }
1446ce263fc8SMatthias Ringwald 
1447a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
1448a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1449ce263fc8SMatthias Ringwald }
1450ce263fc8SMatthias Ringwald 
1451c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
1452c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1453a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1454a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1455a33eb0c4SMilanka Ringwald         return;
1456a33eb0c4SMilanka Ringwald     }
1457ce263fc8SMatthias Ringwald 
1458a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
1459a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1460ce263fc8SMatthias Ringwald }
1461ce263fc8SMatthias Ringwald 
1462ce263fc8SMatthias Ringwald 
1463c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1464c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1465a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1466a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1467a33eb0c4SMilanka Ringwald         return;
1468a33eb0c4SMilanka Ringwald     }
1469ce263fc8SMatthias Ringwald 
1470a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1471a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1472ce263fc8SMatthias Ringwald }
1473ce263fc8SMatthias Ringwald 
1474c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1475c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1476a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1477a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1478a33eb0c4SMilanka Ringwald         return;
1479a33eb0c4SMilanka Ringwald     }
1480ce263fc8SMatthias Ringwald 
1481a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1482a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1483ce263fc8SMatthias Ringwald }
1484ce263fc8SMatthias Ringwald 
1485c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
1486c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1487a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1488a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1489a33eb0c4SMilanka Ringwald         return;
1490a33eb0c4SMilanka Ringwald     }
1491ce263fc8SMatthias Ringwald 
1492a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
1493a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1494ce263fc8SMatthias Ringwald }
1495ce263fc8SMatthias Ringwald 
1496c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
1497c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1498a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1499a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1500a33eb0c4SMilanka Ringwald         return;
1501a33eb0c4SMilanka Ringwald     }
1502ce263fc8SMatthias Ringwald 
1503a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1504a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1505ce263fc8SMatthias Ringwald }
1506ce263fc8SMatthias Ringwald 
1507c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
1508c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1509a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1510a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1511a33eb0c4SMilanka Ringwald         return;
1512a33eb0c4SMilanka Ringwald     }
1513c8626498SMilanka Ringwald 
1514a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1515a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1516a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1517a0ffb263SMatthias Ringwald         return;
1518a0ffb263SMatthias Ringwald     }
1519a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1520a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1521a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1522ce263fc8SMatthias Ringwald }
1523ce263fc8SMatthias Ringwald 
1524c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
1525c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1526a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1527a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1528a33eb0c4SMilanka Ringwald         return;
1529a33eb0c4SMilanka Ringwald     }
1530c8626498SMilanka Ringwald 
1531a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1532a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1533a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1534a0ffb263SMatthias Ringwald         return;
1535a0ffb263SMatthias Ringwald     }
1536a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1537a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1538a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1539ce263fc8SMatthias Ringwald }
1540ce263fc8SMatthias Ringwald 
1541c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
1542c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1543a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1544a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1545a33eb0c4SMilanka Ringwald         return;
1546a33eb0c4SMilanka Ringwald     }
1547a33eb0c4SMilanka Ringwald 
1548a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
1549a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1550ce263fc8SMatthias Ringwald }
1551ce263fc8SMatthias Ringwald 
1552c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
1553c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1554a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1555a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1556a33eb0c4SMilanka Ringwald         return;
1557a33eb0c4SMilanka Ringwald     }
1558a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
1559a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1560ce263fc8SMatthias Ringwald }
15613deb3ec6SMatthias Ringwald 
1562c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
1563c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1564a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1565a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1566a33eb0c4SMilanka Ringwald         return;
1567a33eb0c4SMilanka Ringwald     }
1568a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
1569a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1570667ec068SMatthias Ringwald }
1571667ec068SMatthias Ringwald 
1572667ec068SMatthias Ringwald 
1573c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
1574c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1575a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1576a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1577a33eb0c4SMilanka Ringwald         return;
1578a33eb0c4SMilanka Ringwald     }
1579a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1580a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
1581a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1582667ec068SMatthias Ringwald }
1583667ec068SMatthias Ringwald 
1584c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
1585c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1586a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1587a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1588a33eb0c4SMilanka Ringwald         return;
1589a33eb0c4SMilanka Ringwald     }
1590a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1591a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
1592a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1593667ec068SMatthias Ringwald }
1594667ec068SMatthias Ringwald 
1595c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
1596c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1597a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1598a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1599a33eb0c4SMilanka Ringwald         return;
1600a33eb0c4SMilanka Ringwald     }
1601a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1602a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
1603a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1604667ec068SMatthias Ringwald }
1605667ec068SMatthias Ringwald 
1606c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
1607c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1608a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1609a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1610a33eb0c4SMilanka Ringwald         return;
1611a33eb0c4SMilanka Ringwald     }
1612a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1613a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
1614a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1615667ec068SMatthias Ringwald }
1616667ec068SMatthias Ringwald 
1617c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
1618c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1619a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1620a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1621a33eb0c4SMilanka Ringwald         return;
1622a33eb0c4SMilanka Ringwald     }
1623a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
1624a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1625667ec068SMatthias Ringwald }
1626667ec068SMatthias Ringwald 
1627c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
1628c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1629a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1630a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1631a33eb0c4SMilanka Ringwald         return;
1632a33eb0c4SMilanka Ringwald     }
1633667ec068SMatthias Ringwald     // find index for assigned number
1634667ec068SMatthias Ringwald     int i;
1635667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1636667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1637667ec068SMatthias Ringwald             // set value
1638667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1639667ec068SMatthias Ringwald             // mark for update
1640a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1641a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1642667ec068SMatthias Ringwald                 // send update
1643a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1644a0ffb263SMatthias Ringwald             }
1645667ec068SMatthias Ringwald             return;
1646667ec068SMatthias Ringwald         }
1647667ec068SMatthias Ringwald     }
1648667ec068SMatthias Ringwald }
1649667ec068SMatthias Ringwald 
1650