xref: /btstack/src/classic/hfp_hf.c (revision 22387625cf94157435d791d1c4eb2cac85b556df)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hfp_hf.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
42fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493deb3ec6SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <stdlib.h>
513deb3ec6SMatthias Ringwald #include <string.h>
523deb3ec6SMatthias Ringwald 
53235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
5459c6af15SMatthias Ringwald #include "btstack_debug.h"
55d4dd47ffSMatthias Ringwald #include "btstack_event.h"
563deb3ec6SMatthias Ringwald #include "btstack_memory.h"
5759c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5859c6af15SMatthias Ringwald #include "classic/core.h"
5959c6af15SMatthias Ringwald #include "classic/hfp.h"
6059c6af15SMatthias Ringwald #include "classic/hfp_hf.h"
61efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
62746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
63023f2764SMatthias Ringwald #include "classic/sdp_util.h"
6459c6af15SMatthias Ringwald #include "hci.h"
6559c6af15SMatthias Ringwald #include "hci_cmd.h"
6659c6af15SMatthias Ringwald #include "hci_dump.h"
6759c6af15SMatthias Ringwald #include "l2cap.h"
683deb3ec6SMatthias Ringwald 
693deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
703deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
713deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
723deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
733deb3ec6SMatthias Ringwald 
743deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0;
753deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS];
76667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS];
77667ec068SMatthias Ringwald 
78667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9;
79667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9;
803deb3ec6SMatthias Ringwald 
81ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
823deb3ec6SMatthias Ringwald 
83ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
84ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
85ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
86ce263fc8SMatthias Ringwald 
87ce263fc8SMatthias Ringwald static char phone_number[25];
88ce263fc8SMatthias Ringwald 
8913839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
903deb3ec6SMatthias Ringwald     if (callback == NULL){
913deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
923deb3ec6SMatthias Ringwald         return;
933deb3ec6SMatthias Ringwald     }
94ca59be51SMatthias Ringwald     hfp_hf_callback = callback;
95ca59be51SMatthias Ringwald     hfp_set_hf_callback(callback);
963deb3ec6SMatthias Ringwald }
973deb3ec6SMatthias Ringwald 
9813839019SMatthias 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){
99a0ffb263SMatthias Ringwald     if (!callback) return;
100a0ffb263SMatthias Ringwald     uint8_t event[31];
101a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
102a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
103a0ffb263SMatthias Ringwald     event[2] = event_subtype;
104a0ffb263SMatthias Ringwald     event[3] = status;
105a0ffb263SMatthias Ringwald     event[4] = bnip_type;
106adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 6) ? (int) strlen(bnip_number) : (int) sizeof(event) - 6;
107a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], bnip_number, size);
108a0ffb263SMatthias Ringwald     event[5 + size] = 0;
10913839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
110a0ffb263SMatthias Ringwald }
111a0ffb263SMatthias Ringwald 
11213839019SMatthias 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){
113a0ffb263SMatthias Ringwald     if (!callback) return;
114a0ffb263SMatthias Ringwald     uint8_t event[30];
115a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
116a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
117a0ffb263SMatthias Ringwald     event[2] = event_subtype;
118a0ffb263SMatthias Ringwald     event[3] = bnip_type;
119adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 5) ? (int) strlen(bnip_number) : (int) sizeof(event) - 5;
120a0ffb263SMatthias Ringwald     strncpy((char*)&event[4], bnip_number, size);
121a0ffb263SMatthias Ringwald     event[4 + size] = 0;
12213839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
123a0ffb263SMatthias Ringwald }
124a0ffb263SMatthias Ringwald 
12513839019SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir,
126a0ffb263SMatthias Ringwald                 uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){
127a0ffb263SMatthias Ringwald     if (!callback) return;
128a0ffb263SMatthias Ringwald     uint8_t event[35];
129a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
130a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
131a0ffb263SMatthias Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
132a0ffb263SMatthias Ringwald     event[3] = clcc_idx;
133a0ffb263SMatthias Ringwald     event[4] = clcc_dir;
134a0ffb263SMatthias Ringwald     event[6] = clcc_status;
135a0ffb263SMatthias Ringwald     event[7] = clcc_mpty;
136a0ffb263SMatthias Ringwald     event[8] = bnip_type;
137adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 10) ? (int) strlen(bnip_number) : (int) sizeof(event) - 10;
138a0ffb263SMatthias Ringwald     strncpy((char*)&event[9], bnip_number, size);
139a0ffb263SMatthias Ringwald     event[9 + size] = 0;
14013839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
141a0ffb263SMatthias Ringwald }
142a0ffb263SMatthias Ringwald 
143a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
1443deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
145a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1463deb3ec6SMatthias Ringwald     return hf && ag;
1473deb3ec6SMatthias Ringwald }
1483deb3ec6SMatthias Ringwald 
149a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
1503deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
151a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1523deb3ec6SMatthias Ringwald     return hf && ag;
1533deb3ec6SMatthias Ringwald }
1543deb3ec6SMatthias Ringwald 
1553deb3ec6SMatthias Ringwald 
156a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
1573deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
158a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
1593deb3ec6SMatthias Ringwald     return hf && ag;
1603deb3ec6SMatthias Ringwald }
1613deb3ec6SMatthias Ringwald 
1624f84bf36SMatthias 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){
1633deb3ec6SMatthias Ringwald     if (!name){
1643deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1653deb3ec6SMatthias Ringwald     }
166235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
1673deb3ec6SMatthias Ringwald 
1684f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
1694f84bf36SMatthias Ringwald     //
1704f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1714f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
1724f84bf36SMatthias Ringwald     //
1734f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
1744f84bf36SMatthias Ringwald     if (supported_features & wide_band_speech){
1754f84bf36SMatthias Ringwald         sdp_features |= 1 << 5; // Wide band speech bit
1764f84bf36SMatthias Ringwald     }
177aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
1784f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
179aa4dd815SMatthias Ringwald }
1803deb3ec6SMatthias Ringwald 
18189425bfcSMilanka Ringwald 
18289425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
1833deb3ec6SMatthias Ringwald     char buffer[20];
18489425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r\n", cmd);
18589425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
18689425bfcSMilanka Ringwald }
18789425bfcSMilanka Ringwald 
18889425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
18989425bfcSMilanka Ringwald     char buffer[20];
19089425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r\n", cmd, mark);
19189425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
19289425bfcSMilanka Ringwald }
19389425bfcSMilanka Ringwald 
19489425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
19589425bfcSMilanka Ringwald     char buffer[40];
19689425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r\n", cmd, value);
1973deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1983deb3ec6SMatthias Ringwald }
1993deb3ec6SMatthias Ringwald 
2003deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2013deb3ec6SMatthias Ringwald     char buffer[30];
20289425bfcSMilanka Ringwald     const int size = sizeof(buffer);
20389425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
20489425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
20589425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2063deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2073deb3ec6SMatthias Ringwald }
2083deb3ec6SMatthias Ringwald 
2093deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2103deb3ec6SMatthias Ringwald     char buffer[50];
21189425bfcSMilanka Ringwald     const int size = sizeof(buffer);
21289425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
21389425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
21489425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2153deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2163deb3ec6SMatthias Ringwald }
2173deb3ec6SMatthias Ringwald 
2183deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2193deb3ec6SMatthias Ringwald     char buffer[30];
22089425bfcSMilanka Ringwald     const int size = sizeof(buffer);
22189425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
22289425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
22389425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2243deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2253deb3ec6SMatthias Ringwald }
2263deb3ec6SMatthias Ringwald 
22789425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2283deb3ec6SMatthias Ringwald     char buffer[20];
22989425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
230ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
231ce263fc8SMatthias Ringwald }
232ce263fc8SMatthias Ringwald 
233ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
234ce263fc8SMatthias Ringwald     char buffer[40];
23589425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
236ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
237ce263fc8SMatthias Ringwald }
238ce263fc8SMatthias Ringwald 
239a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
240ce263fc8SMatthias Ringwald     char buffer[40];
24189425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id);
242ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
243ce263fc8SMatthias Ringwald }
244ce263fc8SMatthias Ringwald 
245f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
24689425bfcSMilanka Ringwald     char buffer[40];
24789425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
248ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
249ce263fc8SMatthias Ringwald }
250ce263fc8SMatthias Ringwald 
251ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
252ce263fc8SMatthias Ringwald     char buffer[20];
25389425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
254ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
255ce263fc8SMatthias Ringwald }
256ce263fc8SMatthias Ringwald 
25789425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
25889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
25989425bfcSMilanka Ringwald }
26089425bfcSMilanka Ringwald 
26189425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
26289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
26389425bfcSMilanka Ringwald }
26489425bfcSMilanka Ringwald 
26589425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
26689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
26789425bfcSMilanka Ringwald }
26889425bfcSMilanka Ringwald 
26989425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
27089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
27189425bfcSMilanka Ringwald }
27289425bfcSMilanka Ringwald 
27389425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
27489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
27589425bfcSMilanka Ringwald }
27689425bfcSMilanka Ringwald 
27789425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
27889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
27989425bfcSMilanka Ringwald }
28089425bfcSMilanka Ringwald 
28189425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
28289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
28389425bfcSMilanka Ringwald }
28489425bfcSMilanka Ringwald 
28589425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
28689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
28789425bfcSMilanka Ringwald }
28889425bfcSMilanka Ringwald 
28989425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
29089425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
29189425bfcSMilanka Ringwald }
29289425bfcSMilanka Ringwald 
29389425bfcSMilanka Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
29489425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_CALL_ANSWERED);
29589425bfcSMilanka Ringwald }
29689425bfcSMilanka Ringwald 
29789425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
29889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
29989425bfcSMilanka Ringwald }
30089425bfcSMilanka Ringwald 
30189425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
30289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
30389425bfcSMilanka Ringwald }
30489425bfcSMilanka Ringwald 
30589425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
30689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
30789425bfcSMilanka Ringwald }
30889425bfcSMilanka Ringwald 
30989425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
31089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
31189425bfcSMilanka Ringwald }
31289425bfcSMilanka Ringwald 
31389425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
31489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
31589425bfcSMilanka Ringwald }
31689425bfcSMilanka Ringwald 
31789425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
31889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
31989425bfcSMilanka Ringwald }
32089425bfcSMilanka Ringwald 
32189425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
32289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
32389425bfcSMilanka Ringwald }
32489425bfcSMilanka Ringwald 
32589425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
32689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
32789425bfcSMilanka Ringwald }
32889425bfcSMilanka Ringwald 
32989425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
33089425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
33189425bfcSMilanka Ringwald }
33289425bfcSMilanka Ringwald 
33389425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
33489425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
33589425bfcSMilanka Ringwald }
33689425bfcSMilanka Ringwald 
337ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
33889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
339ce263fc8SMatthias Ringwald }
340ce263fc8SMatthias Ringwald 
341667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
34289425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
343667ec068SMatthias Ringwald }
344667ec068SMatthias Ringwald 
34513839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){
3463deb3ec6SMatthias Ringwald     if (!callback) return;
347a0ffb263SMatthias Ringwald     uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1];
3483deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3493deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3503deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
351a0ffb263SMatthias Ringwald     event[3] = indicator.index;
352a0ffb263SMatthias Ringwald     event[4] = indicator.status;
353a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
354a0ffb263SMatthias Ringwald     event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0;
35513839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3563deb3ec6SMatthias Ringwald }
3573deb3ec6SMatthias Ringwald 
35813839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){
3593deb3ec6SMatthias Ringwald     if (!callback) return;
36089425bfcSMilanka Ringwald     uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
3613deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3623deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3633deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
364a0ffb263SMatthias Ringwald     event[3] = network_operator.mode;
365a0ffb263SMatthias Ringwald     event[4] = network_operator.format;
36689425bfcSMilanka Ringwald     strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
36789425bfcSMilanka Ringwald     event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
36813839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3693deb3ec6SMatthias Ringwald }
3703deb3ec6SMatthias Ringwald 
371a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
372a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
373a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
374aa4dd815SMatthias Ringwald     int done = 1;
3753deb3ec6SMatthias Ringwald 
376a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
3773deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
378d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
379a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
380a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
3813deb3ec6SMatthias Ringwald             break;
3823deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
383a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
384a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
3853deb3ec6SMatthias Ringwald             break;
3863deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
387a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
388a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
3893deb3ec6SMatthias Ringwald             break;
3903deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
391a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
392a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
3933deb3ec6SMatthias Ringwald             break;
3943deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
395a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
396a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
3973deb3ec6SMatthias Ringwald             break;
3983deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
399a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
400a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4013deb3ec6SMatthias Ringwald             break;
4023deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
403a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
404a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4053deb3ec6SMatthias Ringwald             break;
4063deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
407a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
408a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4093deb3ec6SMatthias Ringwald             break;
4103deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
411a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
412a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4133deb3ec6SMatthias Ringwald             break;
4143deb3ec6SMatthias Ringwald         default:
415aa4dd815SMatthias Ringwald             done = 0;
4163deb3ec6SMatthias Ringwald             break;
4173deb3ec6SMatthias Ringwald     }
4183deb3ec6SMatthias Ringwald     return done;
4193deb3ec6SMatthias Ringwald }
4203deb3ec6SMatthias Ringwald 
421ce263fc8SMatthias Ringwald 
422a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
423a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
424a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
425ce263fc8SMatthias Ringwald 
426ce263fc8SMatthias Ringwald     int done = 0;
427a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
428a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
429ce263fc8SMatthias Ringwald         done = 1;
430a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
431ce263fc8SMatthias Ringwald         return done;
432ce263fc8SMatthias Ringwald     };
433a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
434a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
435ce263fc8SMatthias Ringwald         done = 1;
436a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
437a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
438a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
439ce263fc8SMatthias Ringwald         return done;
440ce263fc8SMatthias Ringwald     }
441ce263fc8SMatthias Ringwald 
442a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
443ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
444a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
445a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
446a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
447ce263fc8SMatthias Ringwald             return 1;
448ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
449a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
450a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
451a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
452ce263fc8SMatthias Ringwald             return 1;
453ce263fc8SMatthias Ringwald         default:
454ce263fc8SMatthias Ringwald             break;
455ce263fc8SMatthias Ringwald     }
456ce263fc8SMatthias Ringwald 
457a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
458a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
459ce263fc8SMatthias Ringwald         done = 1;
460a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
461ce263fc8SMatthias Ringwald         return done;
462ce263fc8SMatthias Ringwald     }
463ce263fc8SMatthias Ringwald 
464ce263fc8SMatthias Ringwald     return done;
465ce263fc8SMatthias Ringwald }
466ce263fc8SMatthias Ringwald 
467a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
468ce263fc8SMatthias Ringwald     /* events ( == commands):
469ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
470ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
471ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
472ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
473ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
474ce263fc8SMatthias Ringwald     */
475ce263fc8SMatthias Ringwald 
476a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
477ce263fc8SMatthias Ringwald 
478a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
479ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
480a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
481ce263fc8SMatthias Ringwald 
482a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
483a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
484a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
485ce263fc8SMatthias Ringwald             return 1;
486ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
487a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = 0;
488a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = 0;
489a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = 0;
490ce263fc8SMatthias Ringwald 
491a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
492a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
493a0ffb263SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
494ce263fc8SMatthias Ringwald             break;
495ce263fc8SMatthias Ringwald 
4966a7f44bdSMilanka Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:{
4976a7f44bdSMilanka Ringwald             if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
498a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
499a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
500a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
5010e0be203SMatthias Ringwald                 hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
5020e0be203SMatthias Ringwald                 log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
503fcb08cdbSMilanka Ringwald                 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
504ce263fc8SMatthias Ringwald             } else {
505a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
506a0ffb263SMatthias Ringwald                 hfp_connection->suggested_codec = 0;
507a0ffb263SMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
508a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
509a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
510a0ffb263SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
511ce263fc8SMatthias Ringwald 
512ce263fc8SMatthias Ringwald             }
513ce263fc8SMatthias Ringwald             break;
5146a7f44bdSMilanka Ringwald         }
515ce263fc8SMatthias Ringwald         default:
516ce263fc8SMatthias Ringwald             break;
517ce263fc8SMatthias Ringwald     }
518ce263fc8SMatthias Ringwald     return 0;
519ce263fc8SMatthias Ringwald }
520ce263fc8SMatthias Ringwald 
521a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
522a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
523a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
524ce263fc8SMatthias Ringwald 
525ce263fc8SMatthias Ringwald 
526a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
527a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
528a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
529a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
530ce263fc8SMatthias Ringwald         return 1;
531ce263fc8SMatthias Ringwald     }
532ce263fc8SMatthias Ringwald 
533a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
534ce263fc8SMatthias Ringwald 
535ce263fc8SMatthias Ringwald     // run codecs exchange
536a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
537ce263fc8SMatthias Ringwald     if (done) return 1;
538ce263fc8SMatthias Ringwald 
539ce263fc8SMatthias Ringwald     return 0;
540ce263fc8SMatthias Ringwald }
541ce263fc8SMatthias Ringwald 
542a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
543a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
544a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
545a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
546ce263fc8SMatthias Ringwald         return 1;
547ce263fc8SMatthias Ringwald     }
548ce263fc8SMatthias Ringwald     return 0;
549ce263fc8SMatthias Ringwald }
550ce263fc8SMatthias Ringwald 
551a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){
552a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
553724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
5547522e673SMatthias Ringwald 
555*22387625SMatthias Ringwald     if (hfp_connection->local_role != HFP_ROLE_HF) {
556*22387625SMatthias Ringwald         log_info("HFP HF%p, wrong role %u", hfp_connection, hfp_connection->local_role);
557*22387625SMatthias Ringwald         return;
558*22387625SMatthias Ringwald     }
559*22387625SMatthias Ringwald 
560b72c4a9eSMatthias Ringwald     if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){
561b72c4a9eSMatthias Ringwald 
562b72c4a9eSMatthias Ringwald         hfp_connection->hf_accept_sco = 0;
5637522e673SMatthias Ringwald 
5647522e673SMatthias Ringwald         // notify about codec selection if not done already
5657522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
5667522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
5677522e673SMatthias Ringwald         }
5687522e673SMatthias Ringwald 
5697522e673SMatthias Ringwald         // remote supported feature eSCO is set if link type is eSCO
5707522e673SMatthias Ringwald         // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
5717522e673SMatthias Ringwald         uint16_t max_latency;
5727522e673SMatthias Ringwald         uint8_t  retransmission_effort;
5737522e673SMatthias Ringwald         uint16_t packet_types;
5747522e673SMatthias Ringwald 
575d9f77559SMatthias Ringwald         if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
5767522e673SMatthias Ringwald             max_latency = 0x000c;
5777522e673SMatthias Ringwald             retransmission_effort = 0x02;
5787522e673SMatthias Ringwald             packet_types = 0x388;
5797522e673SMatthias Ringwald         } else {
5807522e673SMatthias Ringwald             max_latency = 0xffff;
5817522e673SMatthias Ringwald             retransmission_effort = 0xff;
5827522e673SMatthias Ringwald             packet_types = 0x003f;
5837522e673SMatthias Ringwald         }
5847522e673SMatthias Ringwald 
5857522e673SMatthias Ringwald         uint16_t sco_voice_setting = hci_get_sco_voice_setting();
5867522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
5877522e673SMatthias Ringwald             sco_voice_setting = 0x0043; // Transparent data
5887522e673SMatthias Ringwald         }
5897522e673SMatthias Ringwald 
590b72c4a9eSMatthias Ringwald         log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting);
5917522e673SMatthias Ringwald         hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
5927522e673SMatthias Ringwald                         sco_voice_setting, retransmission_effort, packet_types);
5937522e673SMatthias Ringwald         return;
5947522e673SMatthias Ringwald     }
5957522e673SMatthias Ringwald 
596d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
597d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
598d4dd47ffSMatthias Ringwald         return;
599d4dd47ffSMatthias Ringwald     }
600a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
601ce263fc8SMatthias Ringwald     if (!done){
602a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
603ce263fc8SMatthias Ringwald     }
604ce263fc8SMatthias Ringwald     if (!done){
605a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
606ce263fc8SMatthias Ringwald     }
607ce263fc8SMatthias Ringwald     if (!done){
608a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
609ce263fc8SMatthias Ringwald     }
610ce263fc8SMatthias Ringwald 
6111016a228SMatthias Ringwald     // don't send a new command while ok still pending
6121016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6131016a228SMatthias Ringwald 
614a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
615a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
616a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
617a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
618ce263fc8SMatthias Ringwald         return;
619ce263fc8SMatthias Ringwald     }
620ce263fc8SMatthias Ringwald 
621a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
622a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
623a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
624a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
625ce263fc8SMatthias Ringwald         return;
626ce263fc8SMatthias Ringwald     }
627ce263fc8SMatthias Ringwald 
628a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
629a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
630a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
631a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
632ce263fc8SMatthias Ringwald         return;
633ce263fc8SMatthias Ringwald     }
634ce263fc8SMatthias Ringwald 
635a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
636a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
637a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
638a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
639ce263fc8SMatthias Ringwald         return;
640ce263fc8SMatthias Ringwald     }
641ce263fc8SMatthias Ringwald 
642a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
643a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
644a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
645a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
646ce263fc8SMatthias Ringwald         return;
647ce263fc8SMatthias Ringwald     }
648ce263fc8SMatthias Ringwald 
649a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
650a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
651a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
652a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
653ce263fc8SMatthias Ringwald         return;
654ce263fc8SMatthias Ringwald     }
655ce263fc8SMatthias Ringwald 
656a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
657a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
658a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
659a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
660ce263fc8SMatthias Ringwald         return;
661ce263fc8SMatthias Ringwald     }
662ce263fc8SMatthias Ringwald 
663a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
664a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
665a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
666a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
667ce263fc8SMatthias Ringwald         return;
668ce263fc8SMatthias Ringwald     }
669ce263fc8SMatthias Ringwald 
670ce263fc8SMatthias Ringwald 
671a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
672a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
673a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
674a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
675ce263fc8SMatthias Ringwald         return;
676ce263fc8SMatthias Ringwald     }
677ce263fc8SMatthias Ringwald 
678a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
679a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
680a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
681a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
682ce263fc8SMatthias Ringwald         return;
683ce263fc8SMatthias Ringwald     }
684ce263fc8SMatthias Ringwald 
685a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
686a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
687a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
688a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
689ce263fc8SMatthias Ringwald         return;
690ce263fc8SMatthias Ringwald     }
691ce263fc8SMatthias Ringwald 
692a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
693a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
694a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
695a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
696ce263fc8SMatthias Ringwald         return;
697ce263fc8SMatthias Ringwald     }
698ce263fc8SMatthias Ringwald 
699a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
700a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
701a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
702a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
703ce263fc8SMatthias Ringwald         return;
704ce263fc8SMatthias Ringwald     }
705ce263fc8SMatthias Ringwald 
706a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
707a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
708a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
709a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
710ce263fc8SMatthias Ringwald         return;
711ce263fc8SMatthias Ringwald     }
712ce263fc8SMatthias Ringwald 
713a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
714a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
715a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
716a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
717ce263fc8SMatthias Ringwald         return;
718ce263fc8SMatthias Ringwald     }
719ce263fc8SMatthias Ringwald 
720a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
721a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
722a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
723a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
724ce263fc8SMatthias Ringwald         return;
725ce263fc8SMatthias Ringwald     }
726ce263fc8SMatthias Ringwald 
727a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
728a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
729a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
730a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
731ce263fc8SMatthias Ringwald         return;
732ce263fc8SMatthias Ringwald     }
733ce263fc8SMatthias Ringwald 
734a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
735a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
736a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
737a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
738ce263fc8SMatthias Ringwald         return;
739ce263fc8SMatthias Ringwald     }
740ce263fc8SMatthias Ringwald 
741a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
742a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
743a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
744a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
745ce263fc8SMatthias Ringwald         return;
746ce263fc8SMatthias Ringwald     }
747ce263fc8SMatthias Ringwald 
748a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
749a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
750a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
751a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
752667ec068SMatthias Ringwald         return;
753667ec068SMatthias Ringwald     }
754667ec068SMatthias Ringwald 
755a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
756a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
757a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
758a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
759a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
760ce263fc8SMatthias Ringwald         return;
761ce263fc8SMatthias Ringwald     }
762ce263fc8SMatthias Ringwald 
763a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
764a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
765a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
766a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
767ce263fc8SMatthias Ringwald         return;
768ce263fc8SMatthias Ringwald     }
769ce263fc8SMatthias Ringwald 
770a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
771a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
772a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
773a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
774667ec068SMatthias Ringwald         return;
775667ec068SMatthias Ringwald     }
776667ec068SMatthias Ringwald 
777a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
778a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
779667ec068SMatthias Ringwald         char buffer[20];
780a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
781667ec068SMatthias Ringwald             case '?':
782667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
783a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
784667ec068SMatthias Ringwald                 return;
785667ec068SMatthias Ringwald             case '0':
786667ec068SMatthias Ringwald             case '1':
787667ec068SMatthias Ringwald             case '2':
788a0ffb263SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
789a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
790667ec068SMatthias Ringwald                 return;
791667ec068SMatthias Ringwald             default:
792667ec068SMatthias Ringwald                 break;
793667ec068SMatthias Ringwald         }
794667ec068SMatthias Ringwald         return;
795667ec068SMatthias Ringwald     }
796667ec068SMatthias Ringwald 
797a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
798a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
799667ec068SMatthias Ringwald         char buffer[20];
800667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
801a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
802667ec068SMatthias Ringwald         return;
803667ec068SMatthias Ringwald     }
804667ec068SMatthias Ringwald 
805667ec068SMatthias Ringwald     // update HF indicators
806a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
807667ec068SMatthias Ringwald         int i;
808667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
809a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
810a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
811a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
812a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
813667ec068SMatthias Ringwald                     char buffer[30];
814ecb7d461SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]);
815a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
816667ec068SMatthias Ringwald                 } else {
81760ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
818667ec068SMatthias Ringwald                 }
819667ec068SMatthias Ringwald                 return;
820667ec068SMatthias Ringwald             }
821667ec068SMatthias Ringwald         }
822667ec068SMatthias Ringwald     }
823667ec068SMatthias Ringwald 
824ce263fc8SMatthias Ringwald     if (done) return;
825ce263fc8SMatthias Ringwald     // deal with disconnect
826a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
827ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
828a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
829a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
830ce263fc8SMatthias Ringwald             break;
831ce263fc8SMatthias Ringwald 
832ce263fc8SMatthias Ringwald         default:
833ce263fc8SMatthias Ringwald             break;
834ce263fc8SMatthias Ringwald     }
835ce263fc8SMatthias Ringwald }
836ce263fc8SMatthias Ringwald 
837a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
838a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
8396a7f44bdSMilanka Ringwald 
840ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
8417522e673SMatthias Ringwald 
842667ec068SMatthias Ringwald     // restore volume settings
843a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
844a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
845ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
846a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
847a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
848ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
849667ec068SMatthias Ringwald     // enable all indicators
850667ec068SMatthias Ringwald     int i;
851667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
852a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
853a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
854667ec068SMatthias Ringwald     }
855ce263fc8SMatthias Ringwald }
856ce263fc8SMatthias Ringwald 
857a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
858a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
859a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
8603deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
861a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
862a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
8633deb3ec6SMatthias Ringwald                 break;
8643deb3ec6SMatthias Ringwald             }
865a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8663deb3ec6SMatthias Ringwald             break;
8673deb3ec6SMatthias Ringwald 
8683deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
869a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8703deb3ec6SMatthias Ringwald             break;
8713deb3ec6SMatthias Ringwald 
8723deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
873a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
8743deb3ec6SMatthias Ringwald             break;
8753deb3ec6SMatthias Ringwald 
8763deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
877a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
8783deb3ec6SMatthias Ringwald             break;
8793deb3ec6SMatthias Ringwald 
8803deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
881a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
882a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
8833deb3ec6SMatthias Ringwald                 break;
8843deb3ec6SMatthias Ringwald             }
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_RETRIEVE_CAN_HOLD_CALL:
893a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
894a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8953deb3ec6SMatthias Ringwald                 break;
8963deb3ec6SMatthias Ringwald             }
897a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8983deb3ec6SMatthias Ringwald             break;
8993deb3ec6SMatthias Ringwald 
9003deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
901a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
9023deb3ec6SMatthias Ringwald             break;
9033deb3ec6SMatthias Ringwald 
9043deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
905a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9063deb3ec6SMatthias Ringwald             break;
9073deb3ec6SMatthias Ringwald 
9083deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
909a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9103deb3ec6SMatthias Ringwald             break;
911ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
912a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
913a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
914ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
915ce263fc8SMatthias Ringwald                 break;
916ce263fc8SMatthias Ringwald             }
9173deb3ec6SMatthias Ringwald 
918a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
919a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
920ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
921ce263fc8SMatthias Ringwald                 break;
9223deb3ec6SMatthias Ringwald             }
9233deb3ec6SMatthias Ringwald 
924a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
925ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
92660ebb071SMilanka Ringwald                     // printf("Format set, querying name\n");
927a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
928ce263fc8SMatthias Ringwald                     break;
929ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
930a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
931ca59be51SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator);
932ce263fc8SMatthias Ringwald                     break;
933ce263fc8SMatthias Ringwald                 default:
934ce263fc8SMatthias Ringwald                     break;
9353deb3ec6SMatthias Ringwald             }
936ce263fc8SMatthias Ringwald 
937a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
938a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
939ce263fc8SMatthias Ringwald                 break;
9403deb3ec6SMatthias Ringwald             }
9413deb3ec6SMatthias Ringwald 
942a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
943aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
944a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9453deb3ec6SMatthias Ringwald                     break;
946ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
947a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
948ce263fc8SMatthias Ringwald                     break;
9493deb3ec6SMatthias Ringwald                 default:
9503deb3ec6SMatthias Ringwald                     break;
9513deb3ec6SMatthias Ringwald             }
9523deb3ec6SMatthias Ringwald             break;
9533deb3ec6SMatthias Ringwald         default:
9543deb3ec6SMatthias Ringwald             break;
9553deb3ec6SMatthias Ringwald     }
9563deb3ec6SMatthias Ringwald 
9573deb3ec6SMatthias Ringwald     // done
958a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
9593deb3ec6SMatthias Ringwald }
9603deb3ec6SMatthias Ringwald 
9613deb3ec6SMatthias Ringwald 
962e9c22d4eSMatthias Ringwald static void hfp_hf_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
9638a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
9648a46ec40SMatthias Ringwald 
9658a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
9668a46ec40SMatthias Ringwald     if (size < 1) return;
9679ec2630cSMatthias Ringwald 
968a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
969a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
9703deb3ec6SMatthias Ringwald 
9718a46ec40SMatthias Ringwald     // temp overwrite last byte (most likely \n for log_info)
9721e35c04dSMatthias Ringwald     char last_char = packet[size-1];
9731e35c04dSMatthias Ringwald     packet[size-1] = 0;
9741e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
9751e35c04dSMatthias Ringwald     packet[size-1] = last_char;
9761e35c04dSMatthias Ringwald 
9778a46ec40SMatthias Ringwald     // process messages byte-wise
978667ec068SMatthias Ringwald     int pos, i, value;
9793deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
980a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
981ce263fc8SMatthias Ringwald     }
9823deb3ec6SMatthias Ringwald 
983a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
984667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
985a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
986a0ffb263SMatthias Ringwald             // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type);
987ca59be51SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
988667ec068SMatthias Ringwald             break;
989667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
990a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
991a0ffb263SMatthias Ringwald             // printf("Response and Hold status: %s\n", hfp_connection->line_buffer);
992ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
993667ec068SMatthias Ringwald             break;
994667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
995a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
996a0ffb263SMatthias Ringwald             // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
997a0ffb263SMatthias Ringwald             //      hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
998a0ffb263SMatthias Ringwald             //      hfp_connection->bnip_number, hfp_connection->bnip_type);
999ca59be51SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection->clcc_idx,
1000a0ffb263SMatthias Ringwald                 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
1001a0ffb263SMatthias Ringwald                 hfp_connection->bnip_type, hfp_connection->bnip_number);
1002667ec068SMatthias Ringwald             break;
1003ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1004a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10052308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1006667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1007ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1008ce263fc8SMatthias Ringwald             break;
1009ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1010a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10112308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1012667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1013ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1014ce263fc8SMatthias Ringwald             break;
1015ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1016a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1017ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1018a0ffb263SMatthias Ringwald             break;
1019a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1020a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1021ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1022a0ffb263SMatthias Ringwald             break;
1023a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1024a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1025ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1026ce263fc8SMatthias Ringwald             break;
1027ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1028a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1029a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1030a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1031ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1032ce263fc8SMatthias Ringwald             break;
1033ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1034a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1035a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1036a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1037ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1038ce263fc8SMatthias Ringwald             break;
1039ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1040a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1041ce263fc8SMatthias Ringwald             break;
1042ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
1043ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1044ce263fc8SMatthias Ringwald             break;
1045ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1046a0ffb263SMatthias Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1047a0ffb263SMatthias Ringwald                 if (hfp_connection->ag_indicators[i].status_changed) {
1048a0ffb263SMatthias Ringwald                     if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1049a0ffb263SMatthias Ringwald                         hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1050a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1051a0ffb263SMatthias Ringwald                         hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1052d5ff3b26SMatthias Ringwald                         // avoid set but not used warning
1053d5ff3b26SMatthias Ringwald                         (void) hfp_callheld_status;
1054a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1055a0ffb263SMatthias Ringwald                         hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1056ce263fc8SMatthias Ringwald                     }
1057a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[i].status_changed = 0;
1058ca59be51SMatthias Ringwald                     hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]);
10593deb3ec6SMatthias Ringwald                     break;
10603deb3ec6SMatthias Ringwald                 }
10613deb3ec6SMatthias Ringwald             }
1062ce263fc8SMatthias Ringwald             break;
1063ce263fc8SMatthias Ringwald         default:
1064ce263fc8SMatthias Ringwald             break;
10653deb3ec6SMatthias Ringwald     }
1066a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
10673deb3ec6SMatthias Ringwald }
10683deb3ec6SMatthias Ringwald 
10690cb5b971SMatthias Ringwald static void hfp_run(void){
1070665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1071665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1072665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1073a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1074*22387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1075a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
10763deb3ec6SMatthias Ringwald     }
10773deb3ec6SMatthias Ringwald }
10783deb3ec6SMatthias Ringwald 
1079e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10803deb3ec6SMatthias Ringwald     switch (packet_type){
10813deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1082e9c22d4eSMatthias Ringwald             hfp_hf_handle_rfcomm_event(packet_type, channel, packet, size);
10833deb3ec6SMatthias Ringwald             break;
10843deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1085d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1086d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
1087d4dd47ffSMatthias Ringwald                 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1088d4dd47ffSMatthias Ringwald                 return;
1089d4dd47ffSMatthias Ringwald             }
1090323d3000SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1091202c8a4cSMatthias Ringwald             break;
10923deb3ec6SMatthias Ringwald         default:
10933deb3ec6SMatthias Ringwald             break;
10943deb3ec6SMatthias Ringwald     }
10953deb3ec6SMatthias Ringwald     hfp_run();
10963deb3ec6SMatthias Ringwald }
10973deb3ec6SMatthias Ringwald 
1098a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1099520c92d5SMatthias Ringwald     hfp_init();
1100d63c37a1SMatthias Ringwald 
1101e9c22d4eSMatthias Ringwald     rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1102e9c22d4eSMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&rfcomm_packet_handler);
1103d68dcce1SMatthias Ringwald 
1104a0ffb263SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1105a0ffb263SMatthias Ringwald     hfp_codecs_nr = 0;
1106a0ffb263SMatthias Ringwald     hfp_indicators_nr = 0;
1107a0ffb263SMatthias Ringwald     hfp_hf_speaker_gain = 9;
1108a0ffb263SMatthias Ringwald     hfp_hf_microphone_gain = 9;
1109a0ffb263SMatthias Ringwald }
1110a0ffb263SMatthias Ringwald 
1111a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
11123deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1113a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
11143deb3ec6SMatthias Ringwald         return;
11153deb3ec6SMatthias Ringwald     }
11163deb3ec6SMatthias Ringwald 
11173deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
11183deb3ec6SMatthias Ringwald     int i;
11193deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
11203deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
11213deb3ec6SMatthias Ringwald     }
11223deb3ec6SMatthias Ringwald }
11233deb3ec6SMatthias Ringwald 
1124a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
11253deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1126a0ffb263SMatthias Ringwald }
11273deb3ec6SMatthias Ringwald 
1128a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
11293deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
11303deb3ec6SMatthias Ringwald     int i;
1131a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
11323deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
11333deb3ec6SMatthias Ringwald     }
11343deb3ec6SMatthias Ringwald }
11353deb3ec6SMatthias Ringwald 
11363deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1137323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
11383deb3ec6SMatthias Ringwald }
11393deb3ec6SMatthias Ringwald 
1140c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
1141c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1142a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1143a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1144a33eb0c4SMilanka Ringwald         return;
1145a33eb0c4SMilanka Ringwald     }
1146a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
1147a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11483deb3ec6SMatthias Ringwald }
11493deb3ec6SMatthias Ringwald 
1150c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
1151c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1152a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1153a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11543deb3ec6SMatthias Ringwald         return;
11553deb3ec6SMatthias Ringwald     }
1156a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
1157a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11583deb3ec6SMatthias Ringwald }
11593deb3ec6SMatthias Ringwald 
1160c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1161c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1162ce263fc8SMatthias Ringwald }
1163ce263fc8SMatthias Ringwald 
1164c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1165c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1166ce263fc8SMatthias Ringwald }
1167ce263fc8SMatthias Ringwald 
11683deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1169c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
1170c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1171a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1172a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11733deb3ec6SMatthias Ringwald         return;
11743deb3ec6SMatthias Ringwald     }
1175a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1176a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1177a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11783deb3ec6SMatthias Ringwald }
11793deb3ec6SMatthias Ringwald 
1180c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
1181c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1182a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1183a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11843deb3ec6SMatthias Ringwald         return;
11853deb3ec6SMatthias Ringwald     }
1186a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1187ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1188a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1189ce263fc8SMatthias Ringwald             break;
1190ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1191a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1192ce263fc8SMatthias Ringwald             break;
1193ce263fc8SMatthias Ringwald         default:
1194ce263fc8SMatthias Ringwald             break;
1195ce263fc8SMatthias Ringwald     }
1196a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11973deb3ec6SMatthias Ringwald }
11983deb3ec6SMatthias Ringwald 
1199c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
1200c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1201a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1202a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12033deb3ec6SMatthias Ringwald         return;
12043deb3ec6SMatthias Ringwald     }
1205a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1206a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12073deb3ec6SMatthias Ringwald }
12083deb3ec6SMatthias Ringwald 
1209ce263fc8SMatthias Ringwald 
1210c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1211c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1212ce263fc8SMatthias Ringwald }
1213ce263fc8SMatthias Ringwald 
1214c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1215c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1216ce263fc8SMatthias Ringwald }
1217ce263fc8SMatthias Ringwald 
1218ce263fc8SMatthias Ringwald 
1219c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
1220c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1221a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1222a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1223a33eb0c4SMilanka Ringwald         return;
1224a33eb0c4SMilanka Ringwald     }
1225a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1226ce263fc8SMatthias Ringwald 
1227a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1228a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
12293deb3ec6SMatthias Ringwald 
1230a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1231ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1232a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1233a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1234ce263fc8SMatthias Ringwald     } else {
1235a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1236aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1237aa4dd815SMatthias Ringwald                 break;
1238aa4dd815SMatthias Ringwald             default:
1239a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1240aa4dd815SMatthias Ringwald                 break;
12413deb3ec6SMatthias Ringwald         }
1242ce263fc8SMatthias Ringwald     }
1243ce263fc8SMatthias Ringwald 
1244a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12453deb3ec6SMatthias Ringwald }
12463deb3ec6SMatthias Ringwald 
1247c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
1248c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1249a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1250a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1251a33eb0c4SMilanka Ringwald         return;
1252a33eb0c4SMilanka Ringwald     }
1253a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
1254a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12553deb3ec6SMatthias Ringwald }
12563deb3ec6SMatthias Ringwald 
1257c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
1258c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1259a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1260a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1261a33eb0c4SMilanka Ringwald         return;
1262a33eb0c4SMilanka Ringwald     }
1263ce263fc8SMatthias Ringwald 
1264ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1265a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
1266a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1267ce263fc8SMatthias Ringwald     } else {
1268ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1269ce263fc8SMatthias Ringwald     }
1270ce263fc8SMatthias Ringwald }
1271ce263fc8SMatthias Ringwald 
1272c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
1273c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1274a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1275a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1276a33eb0c4SMilanka Ringwald         return;
1277a33eb0c4SMilanka Ringwald     }
1278a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
1279a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1280ce263fc8SMatthias Ringwald }
1281ce263fc8SMatthias Ringwald 
1282c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
1283c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1284a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1285a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1286a33eb0c4SMilanka Ringwald         return;
1287a33eb0c4SMilanka Ringwald     }
1288ce263fc8SMatthias Ringwald 
1289ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1290a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
1291a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1292ce263fc8SMatthias Ringwald     }
1293ce263fc8SMatthias Ringwald }
1294ce263fc8SMatthias Ringwald 
1295c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){
1296c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1297a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1298a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1299a33eb0c4SMilanka Ringwald         return;
1300a33eb0c4SMilanka Ringwald     }
1301ce263fc8SMatthias Ringwald 
1302ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1303a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
1304a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1305ce263fc8SMatthias Ringwald     }
1306ce263fc8SMatthias Ringwald }
1307ce263fc8SMatthias Ringwald 
1308c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(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_1 = 1;
1318a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1319ce263fc8SMatthias Ringwald     }
1320ce263fc8SMatthias Ringwald }
1321ce263fc8SMatthias Ringwald 
1322c8626498SMilanka Ringwald void hfp_hf_swap_calls(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_2 = 1;
1332a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1333ce263fc8SMatthias Ringwald     }
1334ce263fc8SMatthias Ringwald }
1335ce263fc8SMatthias Ringwald 
1336c8626498SMilanka Ringwald void hfp_hf_join_held_call(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_3 = 1;
1346a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1347ce263fc8SMatthias Ringwald     }
1348ce263fc8SMatthias Ringwald }
1349ce263fc8SMatthias Ringwald 
1350c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
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     }
1356ce263fc8SMatthias Ringwald 
1357ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1358ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1359a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
1360a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1361ce263fc8SMatthias Ringwald     }
1362ce263fc8SMatthias Ringwald }
1363ce263fc8SMatthias Ringwald 
1364c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
1365c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1366a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1367a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1368a33eb0c4SMilanka Ringwald         return;
1369a33eb0c4SMilanka Ringwald     }
1370667ec068SMatthias Ringwald 
1371667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1372667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1373a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1374a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
1375a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1376667ec068SMatthias Ringwald     }
1377667ec068SMatthias Ringwald }
1378667ec068SMatthias Ringwald 
1379c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
1380c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1381a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1382a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1383a33eb0c4SMilanka Ringwald         return;
1384a33eb0c4SMilanka Ringwald     }
1385667ec068SMatthias Ringwald 
1386667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1387667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1388a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1389a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
1390a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1391667ec068SMatthias Ringwald     }
1392667ec068SMatthias Ringwald }
1393ce263fc8SMatthias Ringwald 
1394c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
1395c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1396a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1397a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1398a33eb0c4SMilanka Ringwald         return;
1399a33eb0c4SMilanka Ringwald     }
1400ce263fc8SMatthias Ringwald 
1401a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1402ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1403a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1404ce263fc8SMatthias Ringwald }
1405ce263fc8SMatthias Ringwald 
1406c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
1407c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1408a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1409a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1410a33eb0c4SMilanka Ringwald         return;
1411a33eb0c4SMilanka Ringwald     }
1412ce263fc8SMatthias Ringwald 
1413a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1414a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1415a0ffb263SMatthias Ringwald 
1416a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1417ce263fc8SMatthias Ringwald }
1418ce263fc8SMatthias Ringwald 
1419c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
1420c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1421a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1422a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1423a33eb0c4SMilanka Ringwald         return;
1424a33eb0c4SMilanka Ringwald     }
1425ce263fc8SMatthias Ringwald 
1426a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
1427a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1428ce263fc8SMatthias Ringwald }
1429ce263fc8SMatthias Ringwald 
1430c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
1431c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1432a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1433a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1434a33eb0c4SMilanka Ringwald         return;
1435a33eb0c4SMilanka Ringwald     }
1436ce263fc8SMatthias Ringwald 
1437a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
1438a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1439ce263fc8SMatthias Ringwald }
1440ce263fc8SMatthias Ringwald 
1441ce263fc8SMatthias Ringwald 
1442c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
1443c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1444a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1445a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1446a33eb0c4SMilanka Ringwald         return;
1447a33eb0c4SMilanka Ringwald     }
1448ce263fc8SMatthias Ringwald 
1449a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1450a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1451ce263fc8SMatthias Ringwald }
1452ce263fc8SMatthias Ringwald 
1453ce263fc8SMatthias Ringwald 
1454c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
1455c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1456a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1457a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1458a33eb0c4SMilanka Ringwald         return;
1459a33eb0c4SMilanka Ringwald     }
1460ce263fc8SMatthias Ringwald 
1461a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
1462a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1463ce263fc8SMatthias Ringwald }
1464ce263fc8SMatthias Ringwald 
1465c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
1466c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1467a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1468a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1469a33eb0c4SMilanka Ringwald         return;
1470a33eb0c4SMilanka Ringwald     }
1471ce263fc8SMatthias Ringwald 
1472a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
1473a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1474ce263fc8SMatthias Ringwald }
1475ce263fc8SMatthias Ringwald 
1476ce263fc8SMatthias Ringwald 
1477c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1478c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1479a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1480a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1481a33eb0c4SMilanka Ringwald         return;
1482a33eb0c4SMilanka Ringwald     }
1483ce263fc8SMatthias Ringwald 
1484a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1485a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1486ce263fc8SMatthias Ringwald }
1487ce263fc8SMatthias Ringwald 
1488c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1489c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1490a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1491a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1492a33eb0c4SMilanka Ringwald         return;
1493a33eb0c4SMilanka Ringwald     }
1494ce263fc8SMatthias Ringwald 
1495a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1496a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1497ce263fc8SMatthias Ringwald }
1498ce263fc8SMatthias Ringwald 
1499c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
1500c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1501a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1502a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1503a33eb0c4SMilanka Ringwald         return;
1504a33eb0c4SMilanka Ringwald     }
1505ce263fc8SMatthias Ringwald 
1506a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
1507a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1508ce263fc8SMatthias Ringwald }
1509ce263fc8SMatthias Ringwald 
1510c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
1511c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1512a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1513a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1514a33eb0c4SMilanka Ringwald         return;
1515a33eb0c4SMilanka Ringwald     }
1516ce263fc8SMatthias Ringwald 
1517a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1518a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1519ce263fc8SMatthias Ringwald }
1520ce263fc8SMatthias Ringwald 
1521c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
1522c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1523a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1524a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1525a33eb0c4SMilanka Ringwald         return;
1526a33eb0c4SMilanka Ringwald     }
1527c8626498SMilanka Ringwald 
1528a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1529a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1530a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1531a0ffb263SMatthias Ringwald         return;
1532a0ffb263SMatthias Ringwald     }
1533a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1534a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1535a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1536ce263fc8SMatthias Ringwald }
1537ce263fc8SMatthias Ringwald 
1538c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
1539c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1540a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1541a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1542a33eb0c4SMilanka Ringwald         return;
1543a33eb0c4SMilanka Ringwald     }
1544c8626498SMilanka Ringwald 
1545a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1546a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1547a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1548a0ffb263SMatthias Ringwald         return;
1549a0ffb263SMatthias Ringwald     }
1550a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1551a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1552a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1553ce263fc8SMatthias Ringwald }
1554ce263fc8SMatthias Ringwald 
1555c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
1556c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1557a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1558a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1559a33eb0c4SMilanka Ringwald         return;
1560a33eb0c4SMilanka Ringwald     }
1561a33eb0c4SMilanka Ringwald 
1562a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
1563a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1564ce263fc8SMatthias Ringwald }
1565ce263fc8SMatthias Ringwald 
1566c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
1567c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1568a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1569a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1570a33eb0c4SMilanka Ringwald         return;
1571a33eb0c4SMilanka Ringwald     }
1572a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
1573a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1574ce263fc8SMatthias Ringwald }
15753deb3ec6SMatthias Ringwald 
1576c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
1577c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1578a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1579a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1580a33eb0c4SMilanka Ringwald         return;
1581a33eb0c4SMilanka Ringwald     }
1582a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
1583a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1584667ec068SMatthias Ringwald }
1585667ec068SMatthias Ringwald 
1586667ec068SMatthias Ringwald 
1587c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
1588c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1589a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1590a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1591a33eb0c4SMilanka Ringwald         return;
1592a33eb0c4SMilanka Ringwald     }
1593a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1594a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
1595a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1596667ec068SMatthias Ringwald }
1597667ec068SMatthias Ringwald 
1598c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
1599c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1600a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1601a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1602a33eb0c4SMilanka Ringwald         return;
1603a33eb0c4SMilanka Ringwald     }
1604a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1605a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
1606a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1607667ec068SMatthias Ringwald }
1608667ec068SMatthias Ringwald 
1609c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
1610c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1611a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1612a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1613a33eb0c4SMilanka Ringwald         return;
1614a33eb0c4SMilanka Ringwald     }
1615a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1616a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
1617a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1618667ec068SMatthias Ringwald }
1619667ec068SMatthias Ringwald 
1620c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
1621c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1622a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1623a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1624a33eb0c4SMilanka Ringwald         return;
1625a33eb0c4SMilanka Ringwald     }
1626a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1627a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
1628a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1629667ec068SMatthias Ringwald }
1630667ec068SMatthias Ringwald 
1631c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
1632c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1633a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1634a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1635a33eb0c4SMilanka Ringwald         return;
1636a33eb0c4SMilanka Ringwald     }
1637a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
1638a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1639667ec068SMatthias Ringwald }
1640667ec068SMatthias Ringwald 
1641c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
1642c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1643a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1644a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1645a33eb0c4SMilanka Ringwald         return;
1646a33eb0c4SMilanka Ringwald     }
1647667ec068SMatthias Ringwald     // find index for assigned number
1648667ec068SMatthias Ringwald     int i;
1649667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1650667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1651667ec068SMatthias Ringwald             // set value
1652667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1653667ec068SMatthias Ringwald             // mark for update
1654a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1655a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1656667ec068SMatthias Ringwald                 // send update
1657a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1658a0ffb263SMatthias Ringwald             }
1659667ec068SMatthias Ringwald             return;
1660667ec068SMatthias Ringwald         }
1661667ec068SMatthias Ringwald     }
1662667ec068SMatthias Ringwald }
1663667ec068SMatthias Ringwald 
1664