xref: /btstack/src/classic/hfp_hf.c (revision ca59be5193cced59664efef288ea86d02eaef3a4)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hfp_hf.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
42fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493deb3ec6SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <stdlib.h>
513deb3ec6SMatthias Ringwald #include <string.h>
523deb3ec6SMatthias Ringwald 
53235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
5459c6af15SMatthias Ringwald #include "btstack_debug.h"
553deb3ec6SMatthias Ringwald #include "btstack_memory.h"
5659c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5759c6af15SMatthias Ringwald #include "classic/core.h"
5859c6af15SMatthias Ringwald #include "classic/hfp.h"
5959c6af15SMatthias Ringwald #include "classic/hfp_hf.h"
60efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
61746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
62023f2764SMatthias Ringwald #include "classic/sdp_util.h"
6359c6af15SMatthias Ringwald #include "hci.h"
6459c6af15SMatthias Ringwald #include "hci_cmd.h"
6559c6af15SMatthias Ringwald #include "hci_dump.h"
6659c6af15SMatthias Ringwald #include "l2cap.h"
673deb3ec6SMatthias Ringwald 
683deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
693deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
703deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
713deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
723deb3ec6SMatthias Ringwald 
733deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0;
743deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS];
75667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS];
76667ec068SMatthias Ringwald 
77667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9;
78667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9;
793deb3ec6SMatthias Ringwald 
80*ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
813deb3ec6SMatthias Ringwald 
82ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
83ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
84ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
85ce263fc8SMatthias Ringwald 
86ce263fc8SMatthias Ringwald static char phone_number[25];
87ce263fc8SMatthias Ringwald 
88c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
89c5b64319SMatthias Ringwald 
9013839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
913deb3ec6SMatthias Ringwald     if (callback == NULL){
923deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
933deb3ec6SMatthias Ringwald         return;
943deb3ec6SMatthias Ringwald     }
95*ca59be51SMatthias Ringwald     hfp_hf_callback = callback;
96*ca59be51SMatthias Ringwald     hfp_set_hf_callback(callback);
973deb3ec6SMatthias Ringwald }
983deb3ec6SMatthias Ringwald 
9913839019SMatthias 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){
100a0ffb263SMatthias Ringwald     if (!callback) return;
101a0ffb263SMatthias Ringwald     uint8_t event[31];
102a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
103a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
104a0ffb263SMatthias Ringwald     event[2] = event_subtype;
105a0ffb263SMatthias Ringwald     event[3] = status;
106a0ffb263SMatthias Ringwald     event[4] = bnip_type;
107adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 6) ? (int) strlen(bnip_number) : (int) sizeof(event) - 6;
108a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], bnip_number, size);
109a0ffb263SMatthias Ringwald     event[5 + size] = 0;
11013839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
111a0ffb263SMatthias Ringwald }
112a0ffb263SMatthias Ringwald 
11313839019SMatthias 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){
114a0ffb263SMatthias Ringwald     if (!callback) return;
115a0ffb263SMatthias Ringwald     uint8_t event[30];
116a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
117a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
118a0ffb263SMatthias Ringwald     event[2] = event_subtype;
119a0ffb263SMatthias Ringwald     event[3] = bnip_type;
120adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 5) ? (int) strlen(bnip_number) : (int) sizeof(event) - 5;
121a0ffb263SMatthias Ringwald     strncpy((char*)&event[4], bnip_number, size);
122a0ffb263SMatthias Ringwald     event[4 + size] = 0;
12313839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
124a0ffb263SMatthias Ringwald }
125a0ffb263SMatthias Ringwald 
12613839019SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir,
127a0ffb263SMatthias Ringwald                 uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){
128a0ffb263SMatthias Ringwald     if (!callback) return;
129a0ffb263SMatthias Ringwald     uint8_t event[35];
130a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
131a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
132a0ffb263SMatthias Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
133a0ffb263SMatthias Ringwald     event[3] = clcc_idx;
134a0ffb263SMatthias Ringwald     event[4] = clcc_dir;
135a0ffb263SMatthias Ringwald     event[6] = clcc_status;
136a0ffb263SMatthias Ringwald     event[7] = clcc_mpty;
137a0ffb263SMatthias Ringwald     event[8] = bnip_type;
138adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 10) ? (int) strlen(bnip_number) : (int) sizeof(event) - 10;
139a0ffb263SMatthias Ringwald     strncpy((char*)&event[9], bnip_number, size);
140a0ffb263SMatthias Ringwald     event[9 + size] = 0;
14113839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
142a0ffb263SMatthias Ringwald }
143a0ffb263SMatthias Ringwald 
144a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
1453deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
146a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1473deb3ec6SMatthias Ringwald     return hf && ag;
1483deb3ec6SMatthias Ringwald }
1493deb3ec6SMatthias Ringwald 
150a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
1513deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
152a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1533deb3ec6SMatthias Ringwald     return hf && ag;
1543deb3ec6SMatthias Ringwald }
1553deb3ec6SMatthias Ringwald 
1563deb3ec6SMatthias Ringwald 
157a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
1583deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
159a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
1603deb3ec6SMatthias Ringwald     return hf && ag;
1613deb3ec6SMatthias Ringwald }
1623deb3ec6SMatthias Ringwald 
163ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
1643deb3ec6SMatthias Ringwald 
1654f84bf36SMatthias 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){
1663deb3ec6SMatthias Ringwald     if (!name){
1673deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1683deb3ec6SMatthias Ringwald     }
169235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
1703deb3ec6SMatthias Ringwald 
1714f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
1724f84bf36SMatthias Ringwald     //
1734f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1744f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
1754f84bf36SMatthias Ringwald     //
1764f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
1774f84bf36SMatthias Ringwald     if (supported_features & wide_band_speech){
1784f84bf36SMatthias Ringwald         sdp_features |= 1 << 5; // Wide band speech bit
1794f84bf36SMatthias Ringwald     }
180aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
1814f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
182aa4dd815SMatthias Ringwald }
1833deb3ec6SMatthias Ringwald 
18489425bfcSMilanka Ringwald 
18589425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
1863deb3ec6SMatthias Ringwald     char buffer[20];
18789425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r\n", cmd);
18889425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
18989425bfcSMilanka Ringwald }
19089425bfcSMilanka Ringwald 
19189425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
19289425bfcSMilanka Ringwald     char buffer[20];
19389425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r\n", cmd, mark);
19489425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
19589425bfcSMilanka Ringwald }
19689425bfcSMilanka Ringwald 
19789425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
19889425bfcSMilanka Ringwald     char buffer[40];
19989425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r\n", cmd, value);
2003deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2013deb3ec6SMatthias Ringwald }
2023deb3ec6SMatthias Ringwald 
2033deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2043deb3ec6SMatthias Ringwald     char buffer[30];
20589425bfcSMilanka Ringwald     const int size = sizeof(buffer);
20689425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
20789425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
20889425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2093deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2103deb3ec6SMatthias Ringwald }
2113deb3ec6SMatthias Ringwald 
2123deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2133deb3ec6SMatthias Ringwald     char buffer[50];
21489425bfcSMilanka Ringwald     const int size = sizeof(buffer);
21589425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
21689425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
21789425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2183deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2193deb3ec6SMatthias Ringwald }
2203deb3ec6SMatthias Ringwald 
2213deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2223deb3ec6SMatthias Ringwald     char buffer[30];
22389425bfcSMilanka Ringwald     const int size = sizeof(buffer);
22489425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
22589425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
22689425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2273deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2283deb3ec6SMatthias Ringwald }
2293deb3ec6SMatthias Ringwald 
23089425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2313deb3ec6SMatthias Ringwald     char buffer[20];
23289425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
233ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
234ce263fc8SMatthias Ringwald }
235ce263fc8SMatthias Ringwald 
236ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
237ce263fc8SMatthias Ringwald     char buffer[40];
23889425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
239ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
240ce263fc8SMatthias Ringwald }
241ce263fc8SMatthias Ringwald 
242a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
243ce263fc8SMatthias Ringwald     char buffer[40];
24489425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id);
245ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
246ce263fc8SMatthias Ringwald }
247ce263fc8SMatthias Ringwald 
248f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
24989425bfcSMilanka Ringwald     char buffer[40];
25089425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
251ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
252ce263fc8SMatthias Ringwald }
253ce263fc8SMatthias Ringwald 
254ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
255ce263fc8SMatthias Ringwald     char buffer[20];
25689425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
257ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
258ce263fc8SMatthias Ringwald }
259ce263fc8SMatthias Ringwald 
26089425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
26189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
26289425bfcSMilanka Ringwald }
26389425bfcSMilanka Ringwald 
26489425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
26589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
26689425bfcSMilanka Ringwald }
26789425bfcSMilanka Ringwald 
26889425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
26989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
27089425bfcSMilanka Ringwald }
27189425bfcSMilanka Ringwald 
27289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
27389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
27489425bfcSMilanka Ringwald }
27589425bfcSMilanka Ringwald 
27689425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
27789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
27889425bfcSMilanka Ringwald }
27989425bfcSMilanka Ringwald 
28089425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
28189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
28289425bfcSMilanka Ringwald }
28389425bfcSMilanka Ringwald 
28489425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
28589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
28689425bfcSMilanka Ringwald }
28789425bfcSMilanka Ringwald 
28889425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
28989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
29089425bfcSMilanka Ringwald }
29189425bfcSMilanka Ringwald 
29289425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
29389425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
29489425bfcSMilanka Ringwald }
29589425bfcSMilanka Ringwald 
29689425bfcSMilanka Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
29789425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_CALL_ANSWERED);
29889425bfcSMilanka Ringwald }
29989425bfcSMilanka Ringwald 
30089425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
30189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
30289425bfcSMilanka Ringwald }
30389425bfcSMilanka Ringwald 
30489425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
30589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
30689425bfcSMilanka Ringwald }
30789425bfcSMilanka Ringwald 
30889425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
30989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
31089425bfcSMilanka Ringwald }
31189425bfcSMilanka Ringwald 
31289425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
31389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
31489425bfcSMilanka Ringwald }
31589425bfcSMilanka Ringwald 
31689425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
31789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
31889425bfcSMilanka Ringwald }
31989425bfcSMilanka Ringwald 
32089425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
32189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
32289425bfcSMilanka Ringwald }
32389425bfcSMilanka Ringwald 
32489425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
32589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
32689425bfcSMilanka Ringwald }
32789425bfcSMilanka Ringwald 
32889425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
32989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
33089425bfcSMilanka Ringwald }
33189425bfcSMilanka Ringwald 
33289425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
33389425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
33489425bfcSMilanka Ringwald }
33589425bfcSMilanka Ringwald 
33689425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
33789425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
33889425bfcSMilanka Ringwald }
33989425bfcSMilanka Ringwald 
340ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
34189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
342ce263fc8SMatthias Ringwald }
343ce263fc8SMatthias Ringwald 
344667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
34589425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
346667ec068SMatthias Ringwald }
347667ec068SMatthias Ringwald 
34813839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){
3493deb3ec6SMatthias Ringwald     if (!callback) return;
350a0ffb263SMatthias Ringwald     uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1];
3513deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3523deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3533deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
354a0ffb263SMatthias Ringwald     event[3] = indicator.index;
355a0ffb263SMatthias Ringwald     event[4] = indicator.status;
356a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
357a0ffb263SMatthias Ringwald     event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0;
35813839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3593deb3ec6SMatthias Ringwald }
3603deb3ec6SMatthias Ringwald 
36113839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){
3623deb3ec6SMatthias Ringwald     if (!callback) return;
36389425bfcSMilanka Ringwald     uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
3643deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3653deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3663deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
367a0ffb263SMatthias Ringwald     event[3] = network_operator.mode;
368a0ffb263SMatthias Ringwald     event[4] = network_operator.format;
36989425bfcSMilanka Ringwald     strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
37089425bfcSMilanka Ringwald     event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
37113839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3723deb3ec6SMatthias Ringwald }
3733deb3ec6SMatthias Ringwald 
374a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
375a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
376a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
377aa4dd815SMatthias Ringwald     int done = 1;
3783deb3ec6SMatthias Ringwald 
379a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
3803deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
381d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
382a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
383a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
3843deb3ec6SMatthias Ringwald             break;
3853deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
386a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
387a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
3883deb3ec6SMatthias Ringwald             break;
3893deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
390a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
391a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
3923deb3ec6SMatthias Ringwald             break;
3933deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
394a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
395a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
3963deb3ec6SMatthias Ringwald             break;
3973deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
398a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
399a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4003deb3ec6SMatthias Ringwald             break;
4013deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
402a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
403a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4043deb3ec6SMatthias Ringwald             break;
4053deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
406a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
407a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4083deb3ec6SMatthias Ringwald             break;
4093deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
410a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
411a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4123deb3ec6SMatthias Ringwald             break;
4133deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
414a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
415a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4163deb3ec6SMatthias Ringwald             break;
4173deb3ec6SMatthias Ringwald         default:
418aa4dd815SMatthias Ringwald             done = 0;
4193deb3ec6SMatthias Ringwald             break;
4203deb3ec6SMatthias Ringwald     }
4213deb3ec6SMatthias Ringwald     return done;
4223deb3ec6SMatthias Ringwald }
4233deb3ec6SMatthias Ringwald 
424ce263fc8SMatthias Ringwald 
425a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
426a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
427a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
428ce263fc8SMatthias Ringwald 
429ce263fc8SMatthias Ringwald     int done = 0;
430a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
431a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
432ce263fc8SMatthias Ringwald         done = 1;
433a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
434ce263fc8SMatthias Ringwald         return done;
435ce263fc8SMatthias Ringwald     };
436a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
437a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
438ce263fc8SMatthias Ringwald         done = 1;
439a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
440a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
441a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
442ce263fc8SMatthias Ringwald         return done;
443ce263fc8SMatthias Ringwald     }
444ce263fc8SMatthias Ringwald 
445a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
446ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
447a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
448a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
449a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
450ce263fc8SMatthias Ringwald             return 1;
451ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
452a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
453a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
454a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
455ce263fc8SMatthias Ringwald             return 1;
456ce263fc8SMatthias Ringwald         default:
457ce263fc8SMatthias Ringwald             break;
458ce263fc8SMatthias Ringwald     }
459ce263fc8SMatthias Ringwald 
460a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
461a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
462ce263fc8SMatthias Ringwald         done = 1;
463a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
464ce263fc8SMatthias Ringwald         return done;
465ce263fc8SMatthias Ringwald     }
466ce263fc8SMatthias Ringwald 
467ce263fc8SMatthias Ringwald     return done;
468ce263fc8SMatthias Ringwald }
469ce263fc8SMatthias Ringwald 
470a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
471ce263fc8SMatthias Ringwald     /* events ( == commands):
472ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
473ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
474ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
475ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
476ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
477ce263fc8SMatthias Ringwald     */
478ce263fc8SMatthias Ringwald 
479a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
480ce263fc8SMatthias Ringwald 
481a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
482ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
483a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
484ce263fc8SMatthias Ringwald 
485a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
486a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
487a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
488ce263fc8SMatthias Ringwald             return 1;
489ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
490a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = 0;
491a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = 0;
492a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = 0;
493ce263fc8SMatthias Ringwald 
494a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
495a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
496a0ffb263SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
497ce263fc8SMatthias Ringwald             break;
498ce263fc8SMatthias Ringwald 
4996a7f44bdSMilanka Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:{
5006a7f44bdSMilanka Ringwald             if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
501a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
502a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
503a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
5040e0be203SMatthias Ringwald                 hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
5050e0be203SMatthias Ringwald                 log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
506fcb08cdbSMilanka Ringwald                 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
507ce263fc8SMatthias Ringwald             } else {
508a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
509a0ffb263SMatthias Ringwald                 hfp_connection->suggested_codec = 0;
510a0ffb263SMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
511a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
512a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
513a0ffb263SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
514ce263fc8SMatthias Ringwald 
515ce263fc8SMatthias Ringwald             }
516ce263fc8SMatthias Ringwald             break;
5176a7f44bdSMilanka Ringwald         }
518ce263fc8SMatthias Ringwald         default:
519ce263fc8SMatthias Ringwald             break;
520ce263fc8SMatthias Ringwald     }
521ce263fc8SMatthias Ringwald     return 0;
522ce263fc8SMatthias Ringwald }
523ce263fc8SMatthias Ringwald 
524a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
525a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
526a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
527ce263fc8SMatthias Ringwald 
528ce263fc8SMatthias Ringwald 
529a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
530a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
531a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
532a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
533ce263fc8SMatthias Ringwald         return 1;
534ce263fc8SMatthias Ringwald     }
535ce263fc8SMatthias Ringwald 
536a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
537ce263fc8SMatthias Ringwald 
538ce263fc8SMatthias Ringwald     // run codecs exchange
539a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
540ce263fc8SMatthias Ringwald     if (done) return 1;
541ce263fc8SMatthias Ringwald 
542ce263fc8SMatthias Ringwald     return 0;
543ce263fc8SMatthias Ringwald }
544ce263fc8SMatthias Ringwald 
545a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
546a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
547a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
548a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
549ce263fc8SMatthias Ringwald         return 1;
550ce263fc8SMatthias Ringwald     }
551ce263fc8SMatthias Ringwald     return 0;
552ce263fc8SMatthias Ringwald }
553ce263fc8SMatthias Ringwald 
554a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){
555a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
556724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
5577522e673SMatthias Ringwald 
558b72c4a9eSMatthias Ringwald     if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){
559b72c4a9eSMatthias Ringwald 
560b72c4a9eSMatthias Ringwald         hfp_connection->hf_accept_sco = 0;
5617522e673SMatthias Ringwald 
5627522e673SMatthias Ringwald         // notify about codec selection if not done already
5637522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
5647522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
5657522e673SMatthias Ringwald         }
5667522e673SMatthias Ringwald 
5677522e673SMatthias Ringwald         // remote supported feature eSCO is set if link type is eSCO
5687522e673SMatthias Ringwald         // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
5697522e673SMatthias Ringwald         uint16_t max_latency;
5707522e673SMatthias Ringwald         uint8_t  retransmission_effort;
5717522e673SMatthias Ringwald         uint16_t packet_types;
5727522e673SMatthias Ringwald 
573d9f77559SMatthias Ringwald         if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
5747522e673SMatthias Ringwald             max_latency = 0x000c;
5757522e673SMatthias Ringwald             retransmission_effort = 0x02;
5767522e673SMatthias Ringwald             packet_types = 0x388;
5777522e673SMatthias Ringwald         } else {
5787522e673SMatthias Ringwald             max_latency = 0xffff;
5797522e673SMatthias Ringwald             retransmission_effort = 0xff;
5807522e673SMatthias Ringwald             packet_types = 0x003f;
5817522e673SMatthias Ringwald         }
5827522e673SMatthias Ringwald 
5837522e673SMatthias Ringwald         uint16_t sco_voice_setting = hci_get_sco_voice_setting();
5847522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
5857522e673SMatthias Ringwald             sco_voice_setting = 0x0043; // Transparent data
5867522e673SMatthias Ringwald         }
5877522e673SMatthias Ringwald 
588b72c4a9eSMatthias Ringwald         log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting);
5897522e673SMatthias Ringwald         hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
5907522e673SMatthias Ringwald                         sco_voice_setting, retransmission_effort, packet_types);
5917522e673SMatthias Ringwald         return;
5927522e673SMatthias Ringwald     }
5937522e673SMatthias Ringwald 
594a0ffb263SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return;
595ce263fc8SMatthias Ringwald 
596a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
597ce263fc8SMatthias Ringwald     if (!done){
598a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
599ce263fc8SMatthias Ringwald     }
600ce263fc8SMatthias Ringwald     if (!done){
601a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
602ce263fc8SMatthias Ringwald     }
603ce263fc8SMatthias Ringwald     if (!done){
604a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
605ce263fc8SMatthias Ringwald     }
606ce263fc8SMatthias Ringwald 
6071016a228SMatthias Ringwald     // don't send a new command while ok still pending
6081016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6091016a228SMatthias Ringwald 
610a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
611a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
612a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
613a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
614ce263fc8SMatthias Ringwald         return;
615ce263fc8SMatthias Ringwald     }
616ce263fc8SMatthias Ringwald 
617a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
618a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
619a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
620a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
621ce263fc8SMatthias Ringwald         return;
622ce263fc8SMatthias Ringwald     }
623ce263fc8SMatthias Ringwald 
624a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
625a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
626a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
627a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
628ce263fc8SMatthias Ringwald         return;
629ce263fc8SMatthias Ringwald     }
630ce263fc8SMatthias Ringwald 
631a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
632a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
633a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
634a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
635ce263fc8SMatthias Ringwald         return;
636ce263fc8SMatthias Ringwald     }
637ce263fc8SMatthias Ringwald 
638a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
639a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
640a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
641a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
642ce263fc8SMatthias Ringwald         return;
643ce263fc8SMatthias Ringwald     }
644ce263fc8SMatthias Ringwald 
645a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
646a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
647a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
648a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
649ce263fc8SMatthias Ringwald         return;
650ce263fc8SMatthias Ringwald     }
651ce263fc8SMatthias Ringwald 
652a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
653a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
654a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
655a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
656ce263fc8SMatthias Ringwald         return;
657ce263fc8SMatthias Ringwald     }
658ce263fc8SMatthias Ringwald 
659a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
660a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
661a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
662a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
663ce263fc8SMatthias Ringwald         return;
664ce263fc8SMatthias Ringwald     }
665ce263fc8SMatthias Ringwald 
666ce263fc8SMatthias Ringwald 
667a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
668a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
669a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
670a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
671ce263fc8SMatthias Ringwald         return;
672ce263fc8SMatthias Ringwald     }
673ce263fc8SMatthias Ringwald 
674a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
675a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
676a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
677a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
678ce263fc8SMatthias Ringwald         return;
679ce263fc8SMatthias Ringwald     }
680ce263fc8SMatthias Ringwald 
681a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
682a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
683a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
684a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
685ce263fc8SMatthias Ringwald         return;
686ce263fc8SMatthias Ringwald     }
687ce263fc8SMatthias Ringwald 
688a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
689a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
690a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
691a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
692ce263fc8SMatthias Ringwald         return;
693ce263fc8SMatthias Ringwald     }
694ce263fc8SMatthias Ringwald 
695a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
696a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
697a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
698a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
699ce263fc8SMatthias Ringwald         return;
700ce263fc8SMatthias Ringwald     }
701ce263fc8SMatthias Ringwald 
702a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
703a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
704a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
705a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
706ce263fc8SMatthias Ringwald         return;
707ce263fc8SMatthias Ringwald     }
708ce263fc8SMatthias Ringwald 
709a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
710a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
711a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
712a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
713ce263fc8SMatthias Ringwald         return;
714ce263fc8SMatthias Ringwald     }
715ce263fc8SMatthias Ringwald 
716a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
717a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
718a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
719a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
720ce263fc8SMatthias Ringwald         return;
721ce263fc8SMatthias Ringwald     }
722ce263fc8SMatthias Ringwald 
723a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
724a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
725a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
726a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
727ce263fc8SMatthias Ringwald         return;
728ce263fc8SMatthias Ringwald     }
729ce263fc8SMatthias Ringwald 
730a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
731a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
732a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
733a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
734ce263fc8SMatthias Ringwald         return;
735ce263fc8SMatthias Ringwald     }
736ce263fc8SMatthias Ringwald 
737a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
738a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
739a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
740a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
741ce263fc8SMatthias Ringwald         return;
742ce263fc8SMatthias Ringwald     }
743ce263fc8SMatthias Ringwald 
744a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
745a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
746a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
747a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
748667ec068SMatthias Ringwald         return;
749667ec068SMatthias Ringwald     }
750667ec068SMatthias Ringwald 
751a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
752a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
753a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
754a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
755a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
756ce263fc8SMatthias Ringwald         return;
757ce263fc8SMatthias Ringwald     }
758ce263fc8SMatthias Ringwald 
759a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
760a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
761a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
762a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
763ce263fc8SMatthias Ringwald         return;
764ce263fc8SMatthias Ringwald     }
765ce263fc8SMatthias Ringwald 
766a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
767a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
768a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
769a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
770667ec068SMatthias Ringwald         return;
771667ec068SMatthias Ringwald     }
772667ec068SMatthias Ringwald 
773a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
774a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
775667ec068SMatthias Ringwald         char buffer[20];
776a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
777667ec068SMatthias Ringwald             case '?':
778667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
779a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
780667ec068SMatthias Ringwald                 return;
781667ec068SMatthias Ringwald             case '0':
782667ec068SMatthias Ringwald             case '1':
783667ec068SMatthias Ringwald             case '2':
784a0ffb263SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
785a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
786667ec068SMatthias Ringwald                 return;
787667ec068SMatthias Ringwald             default:
788667ec068SMatthias Ringwald                 break;
789667ec068SMatthias Ringwald         }
790667ec068SMatthias Ringwald         return;
791667ec068SMatthias Ringwald     }
792667ec068SMatthias Ringwald 
793a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
794a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
795667ec068SMatthias Ringwald         char buffer[20];
796667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
797a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
798667ec068SMatthias Ringwald         return;
799667ec068SMatthias Ringwald     }
800667ec068SMatthias Ringwald 
801667ec068SMatthias Ringwald     // update HF indicators
802a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
803667ec068SMatthias Ringwald         int i;
804667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
805a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
806a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
807a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
808a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
809667ec068SMatthias Ringwald                     char buffer[30];
810ecb7d461SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]);
811a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
812667ec068SMatthias Ringwald                 } else {
81360ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
814667ec068SMatthias Ringwald                 }
815667ec068SMatthias Ringwald                 return;
816667ec068SMatthias Ringwald             }
817667ec068SMatthias Ringwald         }
818667ec068SMatthias Ringwald     }
819667ec068SMatthias Ringwald 
820ce263fc8SMatthias Ringwald     if (done) return;
821ce263fc8SMatthias Ringwald     // deal with disconnect
822a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
823ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
824a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
825a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
826ce263fc8SMatthias Ringwald             break;
827ce263fc8SMatthias Ringwald 
828ce263fc8SMatthias Ringwald         default:
829ce263fc8SMatthias Ringwald             break;
830ce263fc8SMatthias Ringwald     }
831ce263fc8SMatthias Ringwald }
832ce263fc8SMatthias Ringwald 
833a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
834a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
8356a7f44bdSMilanka Ringwald 
836*ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
8377522e673SMatthias Ringwald 
838667ec068SMatthias Ringwald     // restore volume settings
839a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
840a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
841*ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
842a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
843a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
844*ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
845667ec068SMatthias Ringwald     // enable all indicators
846667ec068SMatthias Ringwald     int i;
847667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
848a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
849a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
850667ec068SMatthias Ringwald     }
851ce263fc8SMatthias Ringwald }
852ce263fc8SMatthias Ringwald 
853a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
854a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
855a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
8563deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
857a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
858a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
8593deb3ec6SMatthias Ringwald                 break;
8603deb3ec6SMatthias Ringwald             }
861a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8623deb3ec6SMatthias Ringwald             break;
8633deb3ec6SMatthias Ringwald 
8643deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
865a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8663deb3ec6SMatthias Ringwald             break;
8673deb3ec6SMatthias Ringwald 
8683deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
869a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
8703deb3ec6SMatthias Ringwald             break;
8713deb3ec6SMatthias Ringwald 
8723deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
873a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
8743deb3ec6SMatthias Ringwald             break;
8753deb3ec6SMatthias Ringwald 
8763deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
877a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
878a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
8793deb3ec6SMatthias Ringwald                 break;
8803deb3ec6SMatthias Ringwald             }
881a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
882a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8833deb3ec6SMatthias Ringwald                 break;
8843deb3ec6SMatthias Ringwald             }
885a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8863deb3ec6SMatthias Ringwald             break;
8873deb3ec6SMatthias Ringwald 
8883deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
889a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
890a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8913deb3ec6SMatthias Ringwald                 break;
8923deb3ec6SMatthias Ringwald             }
893a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8943deb3ec6SMatthias Ringwald             break;
8953deb3ec6SMatthias Ringwald 
8963deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
897a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
8983deb3ec6SMatthias Ringwald             break;
8993deb3ec6SMatthias Ringwald 
9003deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
901a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9023deb3ec6SMatthias Ringwald             break;
9033deb3ec6SMatthias Ringwald 
9043deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
905a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9063deb3ec6SMatthias Ringwald             break;
907ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
908a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
909a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
910*ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
911ce263fc8SMatthias Ringwald                 break;
912ce263fc8SMatthias Ringwald             }
9133deb3ec6SMatthias Ringwald 
914a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
915a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
916*ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
917ce263fc8SMatthias Ringwald                 break;
9183deb3ec6SMatthias Ringwald             }
9193deb3ec6SMatthias Ringwald 
920a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
921ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
92260ebb071SMilanka Ringwald                     // printf("Format set, querying name\n");
923a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
924ce263fc8SMatthias Ringwald                     break;
925ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
926a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
927*ca59be51SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator);
928ce263fc8SMatthias Ringwald                     break;
929ce263fc8SMatthias Ringwald                 default:
930ce263fc8SMatthias Ringwald                     break;
9313deb3ec6SMatthias Ringwald             }
932ce263fc8SMatthias Ringwald 
933a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
934a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
935ce263fc8SMatthias Ringwald                 break;
9363deb3ec6SMatthias Ringwald             }
9373deb3ec6SMatthias Ringwald 
938a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
939aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
940a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9413deb3ec6SMatthias Ringwald                     break;
942ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
943a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
944ce263fc8SMatthias Ringwald                     break;
9453deb3ec6SMatthias Ringwald                 default:
9463deb3ec6SMatthias Ringwald                     break;
9473deb3ec6SMatthias Ringwald             }
9483deb3ec6SMatthias Ringwald             break;
9493deb3ec6SMatthias Ringwald         default:
9503deb3ec6SMatthias Ringwald             break;
9513deb3ec6SMatthias Ringwald     }
9523deb3ec6SMatthias Ringwald 
9533deb3ec6SMatthias Ringwald     // done
954a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
9553deb3ec6SMatthias Ringwald }
9563deb3ec6SMatthias Ringwald 
9573deb3ec6SMatthias Ringwald 
9583deb3ec6SMatthias Ringwald static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
9598a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
9608a46ec40SMatthias Ringwald 
9618a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
9628a46ec40SMatthias Ringwald     if (size < 1) return;
9639ec2630cSMatthias Ringwald 
964a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
965a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
9663deb3ec6SMatthias Ringwald 
9678a46ec40SMatthias Ringwald     // temp overwrite last byte (most likely \n for log_info)
9681e35c04dSMatthias Ringwald     char last_char = packet[size-1];
9691e35c04dSMatthias Ringwald     packet[size-1] = 0;
9701e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
9711e35c04dSMatthias Ringwald     packet[size-1] = last_char;
9721e35c04dSMatthias Ringwald 
9738a46ec40SMatthias Ringwald     // process messages byte-wise
974667ec068SMatthias Ringwald     int pos, i, value;
9753deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
976a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
977ce263fc8SMatthias Ringwald     }
9783deb3ec6SMatthias Ringwald 
979a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
980667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
981a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
982a0ffb263SMatthias Ringwald             // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type);
983*ca59be51SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
984667ec068SMatthias Ringwald             break;
985667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
986a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
987a0ffb263SMatthias Ringwald             // printf("Response and Hold status: %s\n", hfp_connection->line_buffer);
988*ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
989667ec068SMatthias Ringwald             break;
990667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
991a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
992a0ffb263SMatthias Ringwald             // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
993a0ffb263SMatthias Ringwald             //      hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
994a0ffb263SMatthias Ringwald             //      hfp_connection->bnip_number, hfp_connection->bnip_type);
995*ca59be51SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection->clcc_idx,
996a0ffb263SMatthias Ringwald                 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
997a0ffb263SMatthias Ringwald                 hfp_connection->bnip_type, hfp_connection->bnip_number);
998667ec068SMatthias Ringwald             break;
999ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1000a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10012308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1002667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1003*ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1004ce263fc8SMatthias Ringwald             break;
1005ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1006a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10072308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1008667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1009*ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1010ce263fc8SMatthias Ringwald             break;
1011ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1012a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1013*ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1014a0ffb263SMatthias Ringwald             break;
1015a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1016a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1017*ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1018a0ffb263SMatthias Ringwald             break;
1019a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1020a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1021*ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1022ce263fc8SMatthias Ringwald             break;
1023ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1024a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1025a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1026a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1027*ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1028ce263fc8SMatthias Ringwald             break;
1029ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1030a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1031a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1032a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1033*ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1034ce263fc8SMatthias Ringwald             break;
1035ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1036a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1037ce263fc8SMatthias Ringwald             break;
1038ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
1039*ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1040ce263fc8SMatthias Ringwald             break;
1041ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1042a0ffb263SMatthias Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1043a0ffb263SMatthias Ringwald                 if (hfp_connection->ag_indicators[i].status_changed) {
1044a0ffb263SMatthias Ringwald                     if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1045a0ffb263SMatthias Ringwald                         hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1046a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1047a0ffb263SMatthias Ringwald                         hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1048d5ff3b26SMatthias Ringwald                         // avoid set but not used warning
1049d5ff3b26SMatthias Ringwald                         (void) hfp_callheld_status;
1050a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1051a0ffb263SMatthias Ringwald                         hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1052ce263fc8SMatthias Ringwald                     }
1053a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[i].status_changed = 0;
1054*ca59be51SMatthias Ringwald                     hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]);
10553deb3ec6SMatthias Ringwald                     break;
10563deb3ec6SMatthias Ringwald                 }
10573deb3ec6SMatthias Ringwald             }
1058ce263fc8SMatthias Ringwald             break;
1059ce263fc8SMatthias Ringwald         default:
1060ce263fc8SMatthias Ringwald             break;
10613deb3ec6SMatthias Ringwald     }
1062a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
10633deb3ec6SMatthias Ringwald }
10643deb3ec6SMatthias Ringwald 
10650cb5b971SMatthias Ringwald static void hfp_run(void){
1066665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1067665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1068665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1069a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1070a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
10713deb3ec6SMatthias Ringwald     }
10723deb3ec6SMatthias Ringwald }
10733deb3ec6SMatthias Ringwald 
1074ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10753deb3ec6SMatthias Ringwald     switch (packet_type){
10763deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
10773deb3ec6SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size);
10783deb3ec6SMatthias Ringwald             break;
10793deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1080323d3000SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1081202c8a4cSMatthias Ringwald             break;
10823deb3ec6SMatthias Ringwald         default:
10833deb3ec6SMatthias Ringwald             break;
10843deb3ec6SMatthias Ringwald     }
10853deb3ec6SMatthias Ringwald     hfp_run();
10863deb3ec6SMatthias Ringwald }
10873deb3ec6SMatthias Ringwald 
1088a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1089d63c37a1SMatthias Ringwald     // register for HCI events
1090d63c37a1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
1091d63c37a1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
1092d63c37a1SMatthias Ringwald 
1093d63c37a1SMatthias Ringwald     rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff);
1094a0ffb263SMatthias Ringwald 
1095*ca59be51SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&packet_handler);
1096d68dcce1SMatthias Ringwald 
1097a0ffb263SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1098a0ffb263SMatthias Ringwald     hfp_codecs_nr = 0;
1099a0ffb263SMatthias Ringwald     hfp_indicators_nr = 0;
1100a0ffb263SMatthias Ringwald     hfp_hf_speaker_gain = 9;
1101a0ffb263SMatthias Ringwald     hfp_hf_microphone_gain = 9;
1102a0ffb263SMatthias Ringwald }
1103a0ffb263SMatthias Ringwald 
1104a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
11053deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1106a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
11073deb3ec6SMatthias Ringwald         return;
11083deb3ec6SMatthias Ringwald     }
11093deb3ec6SMatthias Ringwald 
11103deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
11113deb3ec6SMatthias Ringwald     int i;
11123deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
11133deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
11143deb3ec6SMatthias Ringwald     }
11153deb3ec6SMatthias Ringwald }
11163deb3ec6SMatthias Ringwald 
1117a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
11183deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1119a0ffb263SMatthias Ringwald }
11203deb3ec6SMatthias Ringwald 
1121a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
11223deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
11233deb3ec6SMatthias Ringwald     int i;
1124a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
11253deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
11263deb3ec6SMatthias Ringwald     }
11273deb3ec6SMatthias Ringwald }
11283deb3ec6SMatthias Ringwald 
11293deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1130323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
11313deb3ec6SMatthias Ringwald }
11323deb3ec6SMatthias Ringwald 
1133c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
1134c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1135a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1136a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1137a33eb0c4SMilanka Ringwald         return;
1138a33eb0c4SMilanka Ringwald     }
1139a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
1140a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11413deb3ec6SMatthias Ringwald }
11423deb3ec6SMatthias Ringwald 
1143c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
1144c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1145a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1146a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11473deb3ec6SMatthias Ringwald         return;
11483deb3ec6SMatthias Ringwald     }
1149a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
1150a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11513deb3ec6SMatthias Ringwald }
11523deb3ec6SMatthias Ringwald 
1153c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1154c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1155ce263fc8SMatthias Ringwald }
1156ce263fc8SMatthias Ringwald 
1157c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1158c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1159ce263fc8SMatthias Ringwald }
1160ce263fc8SMatthias Ringwald 
11613deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1162c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
1163c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1164a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1165a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11663deb3ec6SMatthias Ringwald         return;
11673deb3ec6SMatthias Ringwald     }
1168a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1169a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1170a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11713deb3ec6SMatthias Ringwald }
11723deb3ec6SMatthias Ringwald 
1173c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
1174c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1175a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1176a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11773deb3ec6SMatthias Ringwald         return;
11783deb3ec6SMatthias Ringwald     }
1179a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1180ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1181a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1182ce263fc8SMatthias Ringwald             break;
1183ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1184a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1185ce263fc8SMatthias Ringwald             break;
1186ce263fc8SMatthias Ringwald         default:
1187ce263fc8SMatthias Ringwald             break;
1188ce263fc8SMatthias Ringwald     }
1189a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11903deb3ec6SMatthias Ringwald }
11913deb3ec6SMatthias Ringwald 
1192c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
1193c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1194a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1195a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11963deb3ec6SMatthias Ringwald         return;
11973deb3ec6SMatthias Ringwald     }
1198a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1199a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12003deb3ec6SMatthias Ringwald }
12013deb3ec6SMatthias Ringwald 
1202ce263fc8SMatthias Ringwald 
1203c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1204c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1205ce263fc8SMatthias Ringwald }
1206ce263fc8SMatthias Ringwald 
1207c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1208c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1209ce263fc8SMatthias Ringwald }
1210ce263fc8SMatthias Ringwald 
1211ce263fc8SMatthias Ringwald 
1212c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
1213c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1214a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1215a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1216a33eb0c4SMilanka Ringwald         return;
1217a33eb0c4SMilanka Ringwald     }
1218a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1219ce263fc8SMatthias Ringwald 
1220a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1221a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
12223deb3ec6SMatthias Ringwald 
1223a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1224ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1225a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1226a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1227ce263fc8SMatthias Ringwald     } else {
1228a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1229aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1230aa4dd815SMatthias Ringwald                 break;
1231aa4dd815SMatthias Ringwald             default:
1232a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1233aa4dd815SMatthias Ringwald                 break;
12343deb3ec6SMatthias Ringwald         }
1235ce263fc8SMatthias Ringwald     }
1236ce263fc8SMatthias Ringwald 
1237a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12383deb3ec6SMatthias Ringwald }
12393deb3ec6SMatthias Ringwald 
1240c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
1241c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1242a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1243a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1244a33eb0c4SMilanka Ringwald         return;
1245a33eb0c4SMilanka Ringwald     }
1246a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
1247a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12483deb3ec6SMatthias Ringwald }
12493deb3ec6SMatthias Ringwald 
1250c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
1251c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1252a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1253a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1254a33eb0c4SMilanka Ringwald         return;
1255a33eb0c4SMilanka Ringwald     }
1256ce263fc8SMatthias Ringwald 
1257ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1258a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
1259a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1260ce263fc8SMatthias Ringwald     } else {
1261ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1262ce263fc8SMatthias Ringwald     }
1263ce263fc8SMatthias Ringwald }
1264ce263fc8SMatthias Ringwald 
1265c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
1266c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1267a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1268a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1269a33eb0c4SMilanka Ringwald         return;
1270a33eb0c4SMilanka Ringwald     }
1271a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
1272a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1273ce263fc8SMatthias Ringwald }
1274ce263fc8SMatthias Ringwald 
1275c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
1276c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1277a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1278a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1279a33eb0c4SMilanka Ringwald         return;
1280a33eb0c4SMilanka Ringwald     }
1281ce263fc8SMatthias Ringwald 
1282ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1283a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
1284a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1285ce263fc8SMatthias Ringwald     }
1286ce263fc8SMatthias Ringwald }
1287ce263fc8SMatthias Ringwald 
1288c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){
1289c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1290a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1291a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1292a33eb0c4SMilanka Ringwald         return;
1293a33eb0c4SMilanka Ringwald     }
1294ce263fc8SMatthias Ringwald 
1295ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1296a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
1297a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1298ce263fc8SMatthias Ringwald     }
1299ce263fc8SMatthias Ringwald }
1300ce263fc8SMatthias Ringwald 
1301c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
1302c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1303a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1304a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1305a33eb0c4SMilanka Ringwald         return;
1306a33eb0c4SMilanka Ringwald     }
1307ce263fc8SMatthias Ringwald 
1308ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1309ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1310a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
1311a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1312ce263fc8SMatthias Ringwald     }
1313ce263fc8SMatthias Ringwald }
1314ce263fc8SMatthias Ringwald 
1315c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){
1316c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1317a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1318a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1319a33eb0c4SMilanka Ringwald         return;
1320a33eb0c4SMilanka Ringwald     }
1321ce263fc8SMatthias Ringwald 
1322ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1323ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1324a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
1325a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1326ce263fc8SMatthias Ringwald     }
1327ce263fc8SMatthias Ringwald }
1328ce263fc8SMatthias Ringwald 
1329c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
1330c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1331a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1332a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1333a33eb0c4SMilanka Ringwald         return;
1334a33eb0c4SMilanka Ringwald     }
1335ce263fc8SMatthias Ringwald 
1336ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1337ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1338a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
1339a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1340ce263fc8SMatthias Ringwald     }
1341ce263fc8SMatthias Ringwald }
1342ce263fc8SMatthias Ringwald 
1343c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
1344c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1345a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1346a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1347a33eb0c4SMilanka Ringwald         return;
1348a33eb0c4SMilanka Ringwald     }
1349ce263fc8SMatthias Ringwald 
1350ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1351ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1352a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
1353a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1354ce263fc8SMatthias Ringwald     }
1355ce263fc8SMatthias Ringwald }
1356ce263fc8SMatthias Ringwald 
1357c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
1358c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1359a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1360a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1361a33eb0c4SMilanka Ringwald         return;
1362a33eb0c4SMilanka Ringwald     }
1363667ec068SMatthias Ringwald 
1364667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1365667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1366a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1367a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
1368a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1369667ec068SMatthias Ringwald     }
1370667ec068SMatthias Ringwald }
1371667ec068SMatthias Ringwald 
1372c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
1373c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1374a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1375a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1376a33eb0c4SMilanka Ringwald         return;
1377a33eb0c4SMilanka Ringwald     }
1378667ec068SMatthias Ringwald 
1379667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1380667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1381a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1382a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
1383a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1384667ec068SMatthias Ringwald     }
1385667ec068SMatthias Ringwald }
1386ce263fc8SMatthias Ringwald 
1387c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
1388c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1389a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1390a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1391a33eb0c4SMilanka Ringwald         return;
1392a33eb0c4SMilanka Ringwald     }
1393ce263fc8SMatthias Ringwald 
1394a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1395ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1396a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1397ce263fc8SMatthias Ringwald }
1398ce263fc8SMatthias Ringwald 
1399c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
1400c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1401a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1402a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1403a33eb0c4SMilanka Ringwald         return;
1404a33eb0c4SMilanka Ringwald     }
1405ce263fc8SMatthias Ringwald 
1406a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1407a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1408a0ffb263SMatthias Ringwald 
1409a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1410ce263fc8SMatthias Ringwald }
1411ce263fc8SMatthias Ringwald 
1412c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
1413c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1414a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1415a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1416a33eb0c4SMilanka Ringwald         return;
1417a33eb0c4SMilanka Ringwald     }
1418ce263fc8SMatthias Ringwald 
1419a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
1420a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1421ce263fc8SMatthias Ringwald }
1422ce263fc8SMatthias Ringwald 
1423c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
1424c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1425a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1426a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1427a33eb0c4SMilanka Ringwald         return;
1428a33eb0c4SMilanka Ringwald     }
1429ce263fc8SMatthias Ringwald 
1430a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
1431a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1432ce263fc8SMatthias Ringwald }
1433ce263fc8SMatthias Ringwald 
1434ce263fc8SMatthias Ringwald 
1435c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
1436c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1437a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1438a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1439a33eb0c4SMilanka Ringwald         return;
1440a33eb0c4SMilanka Ringwald     }
1441ce263fc8SMatthias Ringwald 
1442a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1443a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1444ce263fc8SMatthias Ringwald }
1445ce263fc8SMatthias Ringwald 
1446ce263fc8SMatthias Ringwald 
1447c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
1448c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1449a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1450a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1451a33eb0c4SMilanka Ringwald         return;
1452a33eb0c4SMilanka Ringwald     }
1453ce263fc8SMatthias Ringwald 
1454a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
1455a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1456ce263fc8SMatthias Ringwald }
1457ce263fc8SMatthias Ringwald 
1458c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
1459c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1460a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1461a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1462a33eb0c4SMilanka Ringwald         return;
1463a33eb0c4SMilanka Ringwald     }
1464ce263fc8SMatthias Ringwald 
1465a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
1466a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1467ce263fc8SMatthias Ringwald }
1468ce263fc8SMatthias Ringwald 
1469ce263fc8SMatthias Ringwald 
1470c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1471c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1472a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1473a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1474a33eb0c4SMilanka Ringwald         return;
1475a33eb0c4SMilanka Ringwald     }
1476ce263fc8SMatthias Ringwald 
1477a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1478a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1479ce263fc8SMatthias Ringwald }
1480ce263fc8SMatthias Ringwald 
1481c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1482c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1483a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1484a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1485a33eb0c4SMilanka Ringwald         return;
1486a33eb0c4SMilanka Ringwald     }
1487ce263fc8SMatthias Ringwald 
1488a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1489a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1490ce263fc8SMatthias Ringwald }
1491ce263fc8SMatthias Ringwald 
1492c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
1493c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1494a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1495a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1496a33eb0c4SMilanka Ringwald         return;
1497a33eb0c4SMilanka Ringwald     }
1498ce263fc8SMatthias Ringwald 
1499a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
1500a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1501ce263fc8SMatthias Ringwald }
1502ce263fc8SMatthias Ringwald 
1503c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
1504c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1505a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1506a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1507a33eb0c4SMilanka Ringwald         return;
1508a33eb0c4SMilanka Ringwald     }
1509ce263fc8SMatthias Ringwald 
1510a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1511a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1512ce263fc8SMatthias Ringwald }
1513ce263fc8SMatthias Ringwald 
1514c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
1515c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1516a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1517a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1518a33eb0c4SMilanka Ringwald         return;
1519a33eb0c4SMilanka Ringwald     }
1520c8626498SMilanka Ringwald 
1521a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1522a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1523a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1524a0ffb263SMatthias Ringwald         return;
1525a0ffb263SMatthias Ringwald     }
1526a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1527a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1528a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1529ce263fc8SMatthias Ringwald }
1530ce263fc8SMatthias Ringwald 
1531c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
1532c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1533a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1534a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1535a33eb0c4SMilanka Ringwald         return;
1536a33eb0c4SMilanka Ringwald     }
1537c8626498SMilanka Ringwald 
1538a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1539a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1540a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1541a0ffb263SMatthias Ringwald         return;
1542a0ffb263SMatthias Ringwald     }
1543a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1544a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1545a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1546ce263fc8SMatthias Ringwald }
1547ce263fc8SMatthias Ringwald 
1548c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
1549c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1550a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1551a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1552a33eb0c4SMilanka Ringwald         return;
1553a33eb0c4SMilanka Ringwald     }
1554a33eb0c4SMilanka Ringwald 
1555a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
1556a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1557ce263fc8SMatthias Ringwald }
1558ce263fc8SMatthias Ringwald 
1559c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
1560c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1561a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1562a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1563a33eb0c4SMilanka Ringwald         return;
1564a33eb0c4SMilanka Ringwald     }
1565a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
1566a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1567ce263fc8SMatthias Ringwald }
15683deb3ec6SMatthias Ringwald 
1569c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
1570c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1571a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1572a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1573a33eb0c4SMilanka Ringwald         return;
1574a33eb0c4SMilanka Ringwald     }
1575a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
1576a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1577667ec068SMatthias Ringwald }
1578667ec068SMatthias Ringwald 
1579667ec068SMatthias Ringwald 
1580c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
1581c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1582a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1583a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1584a33eb0c4SMilanka Ringwald         return;
1585a33eb0c4SMilanka Ringwald     }
1586a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1587a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
1588a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1589667ec068SMatthias Ringwald }
1590667ec068SMatthias Ringwald 
1591c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
1592c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1593a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1594a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1595a33eb0c4SMilanka Ringwald         return;
1596a33eb0c4SMilanka Ringwald     }
1597a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1598a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
1599a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1600667ec068SMatthias Ringwald }
1601667ec068SMatthias Ringwald 
1602c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
1603c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1604a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1605a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1606a33eb0c4SMilanka Ringwald         return;
1607a33eb0c4SMilanka Ringwald     }
1608a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1609a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
1610a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1611667ec068SMatthias Ringwald }
1612667ec068SMatthias Ringwald 
1613c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
1614c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1615a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1616a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1617a33eb0c4SMilanka Ringwald         return;
1618a33eb0c4SMilanka Ringwald     }
1619a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1620a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
1621a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1622667ec068SMatthias Ringwald }
1623667ec068SMatthias Ringwald 
1624c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
1625c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1626a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1627a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1628a33eb0c4SMilanka Ringwald         return;
1629a33eb0c4SMilanka Ringwald     }
1630a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
1631a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1632667ec068SMatthias Ringwald }
1633667ec068SMatthias Ringwald 
1634c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
1635c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1636a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1637a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1638a33eb0c4SMilanka Ringwald         return;
1639a33eb0c4SMilanka Ringwald     }
1640667ec068SMatthias Ringwald     // find index for assigned number
1641667ec068SMatthias Ringwald     int i;
1642667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1643667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1644667ec068SMatthias Ringwald             // set value
1645667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1646667ec068SMatthias Ringwald             // mark for update
1647a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1648a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1649667ec068SMatthias Ringwald                 // send update
1650a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1651a0ffb263SMatthias Ringwald             }
1652667ec068SMatthias Ringwald             return;
1653667ec068SMatthias Ringwald         }
1654667ec068SMatthias Ringwald     }
1655667ec068SMatthias Ringwald }
1656667ec068SMatthias Ringwald 
1657