xref: /btstack/src/classic/hfp_hf.c (revision 323d300063a7e0193cf61fbe114062cf000a3fe5)
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 
8013839019SMatthias Ringwald static btstack_packet_handler_t hfp_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     hfp_callback = callback;
923deb3ec6SMatthias Ringwald     if (callback == NULL){
933deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
943deb3ec6SMatthias Ringwald         return;
953deb3ec6SMatthias Ringwald     }
963deb3ec6SMatthias Ringwald     hfp_callback = callback;
97f4000eebSMatthias Ringwald     hfp_set_callback(callback);
983deb3ec6SMatthias Ringwald }
993deb3ec6SMatthias Ringwald 
10013839019SMatthias 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){
101a0ffb263SMatthias Ringwald     if (!callback) return;
102a0ffb263SMatthias Ringwald     uint8_t event[31];
103a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
104a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
105a0ffb263SMatthias Ringwald     event[2] = event_subtype;
106a0ffb263SMatthias Ringwald     event[3] = status;
107a0ffb263SMatthias Ringwald     event[4] = bnip_type;
108adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 6) ? (int) strlen(bnip_number) : (int) sizeof(event) - 6;
109a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], bnip_number, size);
110a0ffb263SMatthias Ringwald     event[5 + size] = 0;
11113839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
112a0ffb263SMatthias Ringwald }
113a0ffb263SMatthias Ringwald 
11413839019SMatthias 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){
115a0ffb263SMatthias Ringwald     if (!callback) return;
116a0ffb263SMatthias Ringwald     uint8_t event[30];
117a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
118a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
119a0ffb263SMatthias Ringwald     event[2] = event_subtype;
120a0ffb263SMatthias Ringwald     event[3] = bnip_type;
121adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 5) ? (int) strlen(bnip_number) : (int) sizeof(event) - 5;
122a0ffb263SMatthias Ringwald     strncpy((char*)&event[4], bnip_number, size);
123a0ffb263SMatthias Ringwald     event[4 + size] = 0;
12413839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
125a0ffb263SMatthias Ringwald }
126a0ffb263SMatthias Ringwald 
12713839019SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir,
128a0ffb263SMatthias Ringwald                 uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){
129a0ffb263SMatthias Ringwald     if (!callback) return;
130a0ffb263SMatthias Ringwald     uint8_t event[35];
131a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
132a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
133a0ffb263SMatthias Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
134a0ffb263SMatthias Ringwald     event[3] = clcc_idx;
135a0ffb263SMatthias Ringwald     event[4] = clcc_dir;
136a0ffb263SMatthias Ringwald     event[6] = clcc_status;
137a0ffb263SMatthias Ringwald     event[7] = clcc_mpty;
138a0ffb263SMatthias Ringwald     event[8] = bnip_type;
139adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 10) ? (int) strlen(bnip_number) : (int) sizeof(event) - 10;
140a0ffb263SMatthias Ringwald     strncpy((char*)&event[9], bnip_number, size);
141a0ffb263SMatthias Ringwald     event[9 + size] = 0;
14213839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
143a0ffb263SMatthias Ringwald }
144a0ffb263SMatthias Ringwald 
145a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
1463deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
147a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1483deb3ec6SMatthias Ringwald     return hf && ag;
1493deb3ec6SMatthias Ringwald }
1503deb3ec6SMatthias Ringwald 
151a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
1523deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
153a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1543deb3ec6SMatthias Ringwald     return hf && ag;
1553deb3ec6SMatthias Ringwald }
1563deb3ec6SMatthias Ringwald 
1573deb3ec6SMatthias Ringwald 
158a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
1593deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
160a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
1613deb3ec6SMatthias Ringwald     return hf && ag;
1623deb3ec6SMatthias Ringwald }
1633deb3ec6SMatthias Ringwald 
164ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
1653deb3ec6SMatthias Ringwald 
1664f84bf36SMatthias 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){
1673deb3ec6SMatthias Ringwald     if (!name){
1683deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1693deb3ec6SMatthias Ringwald     }
170235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
1713deb3ec6SMatthias Ringwald 
1724f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
1734f84bf36SMatthias Ringwald     //
1744f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1754f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
1764f84bf36SMatthias Ringwald     //
1774f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
1784f84bf36SMatthias Ringwald     if (supported_features & wide_band_speech){
1794f84bf36SMatthias Ringwald         sdp_features |= 1 << 5; // Wide band speech bit
1804f84bf36SMatthias Ringwald     }
181aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
1824f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
183aa4dd815SMatthias Ringwald }
1843deb3ec6SMatthias Ringwald 
18589425bfcSMilanka Ringwald 
18689425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
1873deb3ec6SMatthias Ringwald     char buffer[20];
18889425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r\n", cmd);
18989425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
19089425bfcSMilanka Ringwald }
19189425bfcSMilanka Ringwald 
19289425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
19389425bfcSMilanka Ringwald     char buffer[20];
19489425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r\n", cmd, mark);
19589425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
19689425bfcSMilanka Ringwald }
19789425bfcSMilanka Ringwald 
19889425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
19989425bfcSMilanka Ringwald     char buffer[40];
20089425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r\n", cmd, value);
2013deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2023deb3ec6SMatthias Ringwald }
2033deb3ec6SMatthias Ringwald 
2043deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2053deb3ec6SMatthias Ringwald     char buffer[30];
20689425bfcSMilanka Ringwald     const int size = sizeof(buffer);
20789425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
20889425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
20989425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2103deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2113deb3ec6SMatthias Ringwald }
2123deb3ec6SMatthias Ringwald 
2133deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2143deb3ec6SMatthias Ringwald     char buffer[50];
21589425bfcSMilanka Ringwald     const int size = sizeof(buffer);
21689425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
21789425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
21889425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2193deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2203deb3ec6SMatthias Ringwald }
2213deb3ec6SMatthias Ringwald 
2223deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2233deb3ec6SMatthias Ringwald     char buffer[30];
22489425bfcSMilanka Ringwald     const int size = sizeof(buffer);
22589425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
22689425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
22789425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2283deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2293deb3ec6SMatthias Ringwald }
2303deb3ec6SMatthias Ringwald 
23189425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2323deb3ec6SMatthias Ringwald     char buffer[20];
23389425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
234ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
235ce263fc8SMatthias Ringwald }
236ce263fc8SMatthias Ringwald 
237ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
238ce263fc8SMatthias Ringwald     char buffer[40];
23989425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
240ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
241ce263fc8SMatthias Ringwald }
242ce263fc8SMatthias Ringwald 
243a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
244ce263fc8SMatthias Ringwald     char buffer[40];
24589425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id);
246ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
247ce263fc8SMatthias Ringwald }
248ce263fc8SMatthias Ringwald 
249f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
25089425bfcSMilanka Ringwald     char buffer[40];
25189425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
252ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
253ce263fc8SMatthias Ringwald }
254ce263fc8SMatthias Ringwald 
255ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
256ce263fc8SMatthias Ringwald     char buffer[20];
25789425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
258ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
259ce263fc8SMatthias Ringwald }
260ce263fc8SMatthias Ringwald 
26189425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
26289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
26389425bfcSMilanka Ringwald }
26489425bfcSMilanka Ringwald 
26589425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
26689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
26789425bfcSMilanka Ringwald }
26889425bfcSMilanka Ringwald 
26989425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
27089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
27189425bfcSMilanka Ringwald }
27289425bfcSMilanka Ringwald 
27389425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
27489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
27589425bfcSMilanka Ringwald }
27689425bfcSMilanka Ringwald 
27789425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
27889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
27989425bfcSMilanka Ringwald }
28089425bfcSMilanka Ringwald 
28189425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
28289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
28389425bfcSMilanka Ringwald }
28489425bfcSMilanka Ringwald 
28589425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
28689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
28789425bfcSMilanka Ringwald }
28889425bfcSMilanka Ringwald 
28989425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
29089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
29189425bfcSMilanka Ringwald }
29289425bfcSMilanka Ringwald 
29389425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
29489425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
29589425bfcSMilanka Ringwald }
29689425bfcSMilanka Ringwald 
29789425bfcSMilanka Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
29889425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_CALL_ANSWERED);
29989425bfcSMilanka Ringwald }
30089425bfcSMilanka Ringwald 
30189425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
30289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
30389425bfcSMilanka Ringwald }
30489425bfcSMilanka Ringwald 
30589425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
30689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
30789425bfcSMilanka Ringwald }
30889425bfcSMilanka Ringwald 
30989425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
31089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
31189425bfcSMilanka Ringwald }
31289425bfcSMilanka Ringwald 
31389425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
31489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
31589425bfcSMilanka Ringwald }
31689425bfcSMilanka Ringwald 
31789425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
31889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
31989425bfcSMilanka Ringwald }
32089425bfcSMilanka Ringwald 
32189425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
32289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
32389425bfcSMilanka Ringwald }
32489425bfcSMilanka Ringwald 
32589425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
32689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
32789425bfcSMilanka Ringwald }
32889425bfcSMilanka Ringwald 
32989425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
33089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
33189425bfcSMilanka Ringwald }
33289425bfcSMilanka Ringwald 
33389425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
33489425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
33589425bfcSMilanka Ringwald }
33689425bfcSMilanka Ringwald 
33789425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
33889425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
33989425bfcSMilanka Ringwald }
34089425bfcSMilanka Ringwald 
341ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
34289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
343ce263fc8SMatthias Ringwald }
344ce263fc8SMatthias Ringwald 
345667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
34689425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
347667ec068SMatthias Ringwald }
348667ec068SMatthias Ringwald 
34913839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){
3503deb3ec6SMatthias Ringwald     if (!callback) return;
351a0ffb263SMatthias Ringwald     uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1];
3523deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3533deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3543deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
355a0ffb263SMatthias Ringwald     event[3] = indicator.index;
356a0ffb263SMatthias Ringwald     event[4] = indicator.status;
357a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
358a0ffb263SMatthias Ringwald     event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0;
35913839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3603deb3ec6SMatthias Ringwald }
3613deb3ec6SMatthias Ringwald 
36213839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){
3633deb3ec6SMatthias Ringwald     if (!callback) return;
36489425bfcSMilanka Ringwald     uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
3653deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3663deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3673deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
368a0ffb263SMatthias Ringwald     event[3] = network_operator.mode;
369a0ffb263SMatthias Ringwald     event[4] = network_operator.format;
37089425bfcSMilanka Ringwald     strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
37189425bfcSMilanka Ringwald     event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
37213839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3733deb3ec6SMatthias Ringwald }
3743deb3ec6SMatthias Ringwald 
375a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
376a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
377a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
378aa4dd815SMatthias Ringwald     int done = 1;
3793deb3ec6SMatthias Ringwald 
380a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
3813deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
382d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
383a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
384a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
3853deb3ec6SMatthias Ringwald             break;
3863deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
387a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
388a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
3893deb3ec6SMatthias Ringwald             break;
3903deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
391a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
392a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
3933deb3ec6SMatthias Ringwald             break;
3943deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
395a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
396a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
3973deb3ec6SMatthias Ringwald             break;
3983deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
399a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
400a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4013deb3ec6SMatthias Ringwald             break;
4023deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
403a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
404a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4053deb3ec6SMatthias Ringwald             break;
4063deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
407a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
408a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4093deb3ec6SMatthias Ringwald             break;
4103deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
411a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
412a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4133deb3ec6SMatthias Ringwald             break;
4143deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
415a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
416a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4173deb3ec6SMatthias Ringwald             break;
4183deb3ec6SMatthias Ringwald         default:
419aa4dd815SMatthias Ringwald             done = 0;
4203deb3ec6SMatthias Ringwald             break;
4213deb3ec6SMatthias Ringwald     }
4223deb3ec6SMatthias Ringwald     return done;
4233deb3ec6SMatthias Ringwald }
4243deb3ec6SMatthias Ringwald 
425ce263fc8SMatthias Ringwald 
426a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
427a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
428a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
429ce263fc8SMatthias Ringwald 
430ce263fc8SMatthias Ringwald     int done = 0;
431a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
432a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
433ce263fc8SMatthias Ringwald         done = 1;
434a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
435ce263fc8SMatthias Ringwald         return done;
436ce263fc8SMatthias Ringwald     };
437a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
438a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
439ce263fc8SMatthias Ringwald         done = 1;
440a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
441a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
442a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
443ce263fc8SMatthias Ringwald         return done;
444ce263fc8SMatthias Ringwald     }
445ce263fc8SMatthias Ringwald 
446a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
447ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
448a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
449a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
450a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
451ce263fc8SMatthias Ringwald             return 1;
452ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
453a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
454a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
455a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
456ce263fc8SMatthias Ringwald             return 1;
457ce263fc8SMatthias Ringwald         default:
458ce263fc8SMatthias Ringwald             break;
459ce263fc8SMatthias Ringwald     }
460ce263fc8SMatthias Ringwald 
461a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
462a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
463ce263fc8SMatthias Ringwald         done = 1;
464a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
465ce263fc8SMatthias Ringwald         return done;
466ce263fc8SMatthias Ringwald     }
467ce263fc8SMatthias Ringwald 
468ce263fc8SMatthias Ringwald     return done;
469ce263fc8SMatthias Ringwald }
470ce263fc8SMatthias Ringwald 
471a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
472ce263fc8SMatthias Ringwald     /* events ( == commands):
473ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
474ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
475ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
476ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
477ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
478ce263fc8SMatthias Ringwald     */
479ce263fc8SMatthias Ringwald 
480a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
481ce263fc8SMatthias Ringwald 
482a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
483ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
484a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
485ce263fc8SMatthias Ringwald 
486a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
487a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
488a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
489ce263fc8SMatthias Ringwald             return 1;
490ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
491a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = 0;
492a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = 0;
493a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = 0;
494ce263fc8SMatthias Ringwald 
495a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
496a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
497a0ffb263SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
498ce263fc8SMatthias Ringwald             break;
499ce263fc8SMatthias Ringwald 
5006a7f44bdSMilanka Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:{
5016a7f44bdSMilanka Ringwald             if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
502a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
503a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
504a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
5050e0be203SMatthias Ringwald                 hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
5060e0be203SMatthias Ringwald                 log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
507fcb08cdbSMilanka Ringwald                 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
508ce263fc8SMatthias Ringwald             } else {
509a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
510a0ffb263SMatthias Ringwald                 hfp_connection->suggested_codec = 0;
511a0ffb263SMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
512a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
513a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
514a0ffb263SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
515ce263fc8SMatthias Ringwald 
516ce263fc8SMatthias Ringwald             }
517ce263fc8SMatthias Ringwald             break;
5186a7f44bdSMilanka Ringwald         }
519ce263fc8SMatthias Ringwald         default:
520ce263fc8SMatthias Ringwald             break;
521ce263fc8SMatthias Ringwald     }
522ce263fc8SMatthias Ringwald     return 0;
523ce263fc8SMatthias Ringwald }
524ce263fc8SMatthias Ringwald 
525a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
526a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
527a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
528ce263fc8SMatthias Ringwald 
529ce263fc8SMatthias Ringwald 
530a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
531a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
532a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
533a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
534ce263fc8SMatthias Ringwald         return 1;
535ce263fc8SMatthias Ringwald     }
536ce263fc8SMatthias Ringwald 
537a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
538ce263fc8SMatthias Ringwald 
539ce263fc8SMatthias Ringwald     // run codecs exchange
540a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
541ce263fc8SMatthias Ringwald     if (done) return 1;
542ce263fc8SMatthias Ringwald 
543ce263fc8SMatthias Ringwald     return 0;
544ce263fc8SMatthias Ringwald }
545ce263fc8SMatthias Ringwald 
546a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
547a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
548a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
549a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
550ce263fc8SMatthias Ringwald         return 1;
551ce263fc8SMatthias Ringwald     }
552ce263fc8SMatthias Ringwald     return 0;
553ce263fc8SMatthias Ringwald }
554ce263fc8SMatthias Ringwald 
555a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){
556a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
557724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
5587522e673SMatthias Ringwald 
559b72c4a9eSMatthias Ringwald     if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){
560b72c4a9eSMatthias Ringwald 
561b72c4a9eSMatthias Ringwald         hfp_connection->hf_accept_sco = 0;
5627522e673SMatthias Ringwald 
5637522e673SMatthias Ringwald         // notify about codec selection if not done already
5647522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
5657522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
5667522e673SMatthias Ringwald         }
5677522e673SMatthias Ringwald 
5687522e673SMatthias Ringwald         // remote supported feature eSCO is set if link type is eSCO
5697522e673SMatthias Ringwald         // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
5707522e673SMatthias Ringwald         uint16_t max_latency;
5717522e673SMatthias Ringwald         uint8_t  retransmission_effort;
5727522e673SMatthias Ringwald         uint16_t packet_types;
5737522e673SMatthias Ringwald 
574d9f77559SMatthias Ringwald         if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
5757522e673SMatthias Ringwald             max_latency = 0x000c;
5767522e673SMatthias Ringwald             retransmission_effort = 0x02;
5777522e673SMatthias Ringwald             packet_types = 0x388;
5787522e673SMatthias Ringwald         } else {
5797522e673SMatthias Ringwald             max_latency = 0xffff;
5807522e673SMatthias Ringwald             retransmission_effort = 0xff;
5817522e673SMatthias Ringwald             packet_types = 0x003f;
5827522e673SMatthias Ringwald         }
5837522e673SMatthias Ringwald 
5847522e673SMatthias Ringwald         uint16_t sco_voice_setting = hci_get_sco_voice_setting();
5857522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
5867522e673SMatthias Ringwald             sco_voice_setting = 0x0043; // Transparent data
5877522e673SMatthias Ringwald         }
5887522e673SMatthias Ringwald 
589b72c4a9eSMatthias Ringwald         log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting);
5907522e673SMatthias Ringwald         hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
5917522e673SMatthias Ringwald                         sco_voice_setting, retransmission_effort, packet_types);
5927522e673SMatthias Ringwald         return;
5937522e673SMatthias Ringwald     }
5947522e673SMatthias Ringwald 
595a0ffb263SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return;
596ce263fc8SMatthias Ringwald 
597a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
598ce263fc8SMatthias Ringwald     if (!done){
599a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
600ce263fc8SMatthias Ringwald     }
601ce263fc8SMatthias Ringwald     if (!done){
602a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
603ce263fc8SMatthias Ringwald     }
604ce263fc8SMatthias Ringwald     if (!done){
605a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
606ce263fc8SMatthias Ringwald     }
607ce263fc8SMatthias Ringwald 
6081016a228SMatthias Ringwald     // don't send a new command while ok still pending
6091016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6101016a228SMatthias Ringwald 
611a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
612a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
613a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
614a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
615ce263fc8SMatthias Ringwald         return;
616ce263fc8SMatthias Ringwald     }
617ce263fc8SMatthias Ringwald 
618a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
619a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
620a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
621a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
622ce263fc8SMatthias Ringwald         return;
623ce263fc8SMatthias Ringwald     }
624ce263fc8SMatthias Ringwald 
625a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
626a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
627a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
628a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
629ce263fc8SMatthias Ringwald         return;
630ce263fc8SMatthias Ringwald     }
631ce263fc8SMatthias Ringwald 
632a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
633a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
634a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
635a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
636ce263fc8SMatthias Ringwald         return;
637ce263fc8SMatthias Ringwald     }
638ce263fc8SMatthias Ringwald 
639a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
640a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
641a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
642a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
643ce263fc8SMatthias Ringwald         return;
644ce263fc8SMatthias Ringwald     }
645ce263fc8SMatthias Ringwald 
646a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
647a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
648a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
649a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
650ce263fc8SMatthias Ringwald         return;
651ce263fc8SMatthias Ringwald     }
652ce263fc8SMatthias Ringwald 
653a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
654a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
655a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
656a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
657ce263fc8SMatthias Ringwald         return;
658ce263fc8SMatthias Ringwald     }
659ce263fc8SMatthias Ringwald 
660a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
661a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
662a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
663a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
664ce263fc8SMatthias Ringwald         return;
665ce263fc8SMatthias Ringwald     }
666ce263fc8SMatthias Ringwald 
667ce263fc8SMatthias Ringwald 
668a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
669a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
670a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
671a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
672ce263fc8SMatthias Ringwald         return;
673ce263fc8SMatthias Ringwald     }
674ce263fc8SMatthias Ringwald 
675a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
676a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
677a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
678a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
679ce263fc8SMatthias Ringwald         return;
680ce263fc8SMatthias Ringwald     }
681ce263fc8SMatthias Ringwald 
682a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
683a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
684a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
685a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
686ce263fc8SMatthias Ringwald         return;
687ce263fc8SMatthias Ringwald     }
688ce263fc8SMatthias Ringwald 
689a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
690a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
691a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
692a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
693ce263fc8SMatthias Ringwald         return;
694ce263fc8SMatthias Ringwald     }
695ce263fc8SMatthias Ringwald 
696a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
697a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
698a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
699a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
700ce263fc8SMatthias Ringwald         return;
701ce263fc8SMatthias Ringwald     }
702ce263fc8SMatthias Ringwald 
703a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
704a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
705a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
706a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
707ce263fc8SMatthias Ringwald         return;
708ce263fc8SMatthias Ringwald     }
709ce263fc8SMatthias Ringwald 
710a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
711a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
712a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
713a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
714ce263fc8SMatthias Ringwald         return;
715ce263fc8SMatthias Ringwald     }
716ce263fc8SMatthias Ringwald 
717a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
718a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
719a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
720a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
721ce263fc8SMatthias Ringwald         return;
722ce263fc8SMatthias Ringwald     }
723ce263fc8SMatthias Ringwald 
724a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
725a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
726a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
727a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
728ce263fc8SMatthias Ringwald         return;
729ce263fc8SMatthias Ringwald     }
730ce263fc8SMatthias Ringwald 
731a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
732a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
733a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
734a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
735ce263fc8SMatthias Ringwald         return;
736ce263fc8SMatthias Ringwald     }
737ce263fc8SMatthias Ringwald 
738a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
739a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
740a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
741a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
742ce263fc8SMatthias Ringwald         return;
743ce263fc8SMatthias Ringwald     }
744ce263fc8SMatthias Ringwald 
745a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
746a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
747a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
748a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
749667ec068SMatthias Ringwald         return;
750667ec068SMatthias Ringwald     }
751667ec068SMatthias Ringwald 
752a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
753a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
754a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
755a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
756a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
757ce263fc8SMatthias Ringwald         return;
758ce263fc8SMatthias Ringwald     }
759ce263fc8SMatthias Ringwald 
760a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
761a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
762a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
763a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
764ce263fc8SMatthias Ringwald         return;
765ce263fc8SMatthias Ringwald     }
766ce263fc8SMatthias Ringwald 
767a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
768a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
769a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
770a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
771667ec068SMatthias Ringwald         return;
772667ec068SMatthias Ringwald     }
773667ec068SMatthias Ringwald 
774a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
775a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
776667ec068SMatthias Ringwald         char buffer[20];
777a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
778667ec068SMatthias Ringwald             case '?':
779667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
780a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
781667ec068SMatthias Ringwald                 return;
782667ec068SMatthias Ringwald             case '0':
783667ec068SMatthias Ringwald             case '1':
784667ec068SMatthias Ringwald             case '2':
785a0ffb263SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
786a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
787667ec068SMatthias Ringwald                 return;
788667ec068SMatthias Ringwald             default:
789667ec068SMatthias Ringwald                 break;
790667ec068SMatthias Ringwald         }
791667ec068SMatthias Ringwald         return;
792667ec068SMatthias Ringwald     }
793667ec068SMatthias Ringwald 
794a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
795a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
796667ec068SMatthias Ringwald         char buffer[20];
797667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
798a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
799667ec068SMatthias Ringwald         return;
800667ec068SMatthias Ringwald     }
801667ec068SMatthias Ringwald 
802667ec068SMatthias Ringwald     // update HF indicators
803a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
804667ec068SMatthias Ringwald         int i;
805667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
806a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
807a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
808a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
809a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
810667ec068SMatthias Ringwald                     char buffer[30];
811ecb7d461SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]);
812a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
813667ec068SMatthias Ringwald                 } else {
81460ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
815667ec068SMatthias Ringwald                 }
816667ec068SMatthias Ringwald                 return;
817667ec068SMatthias Ringwald             }
818667ec068SMatthias Ringwald         }
819667ec068SMatthias Ringwald     }
820667ec068SMatthias Ringwald 
821ce263fc8SMatthias Ringwald     if (done) return;
822ce263fc8SMatthias Ringwald     // deal with disconnect
823a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
824ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
825a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
826a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
827ce263fc8SMatthias Ringwald             break;
828ce263fc8SMatthias Ringwald 
829ce263fc8SMatthias Ringwald         default:
830ce263fc8SMatthias Ringwald             break;
831ce263fc8SMatthias Ringwald     }
832ce263fc8SMatthias Ringwald }
833ce263fc8SMatthias Ringwald 
834a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
835a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
8366a7f44bdSMilanka Ringwald 
837d0c4aea6SMilanka Ringwald     hfp_emit_slc_connection_event(hfp_callback, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
8387522e673SMatthias Ringwald 
839667ec068SMatthias Ringwald     // restore volume settings
840a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
841a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
842667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
843a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
844a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
845667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
846667ec068SMatthias Ringwald     // enable all indicators
847667ec068SMatthias Ringwald     int i;
848667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
849a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
850a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
851667ec068SMatthias Ringwald     }
852ce263fc8SMatthias Ringwald }
853ce263fc8SMatthias Ringwald 
854a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
855a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
856a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
8573deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
858a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
859a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
8603deb3ec6SMatthias Ringwald                 break;
8613deb3ec6SMatthias Ringwald             }
862a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8633deb3ec6SMatthias Ringwald             break;
8643deb3ec6SMatthias Ringwald 
8653deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
866a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8673deb3ec6SMatthias Ringwald             break;
8683deb3ec6SMatthias Ringwald 
8693deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
870a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
8713deb3ec6SMatthias Ringwald             break;
8723deb3ec6SMatthias Ringwald 
8733deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
874a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
8753deb3ec6SMatthias Ringwald             break;
8763deb3ec6SMatthias Ringwald 
8773deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
878a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
879a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
8803deb3ec6SMatthias Ringwald                 break;
8813deb3ec6SMatthias Ringwald             }
882a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
883a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8843deb3ec6SMatthias Ringwald                 break;
8853deb3ec6SMatthias Ringwald             }
886a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8873deb3ec6SMatthias Ringwald             break;
8883deb3ec6SMatthias Ringwald 
8893deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
890a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
891a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8923deb3ec6SMatthias Ringwald                 break;
8933deb3ec6SMatthias Ringwald             }
894a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8953deb3ec6SMatthias Ringwald             break;
8963deb3ec6SMatthias Ringwald 
8973deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
898a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
8993deb3ec6SMatthias Ringwald             break;
9003deb3ec6SMatthias Ringwald 
9013deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
902a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9033deb3ec6SMatthias Ringwald             break;
9043deb3ec6SMatthias Ringwald 
9053deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
906a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9073deb3ec6SMatthias Ringwald             break;
908ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
909a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
910a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
9113deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
912ce263fc8SMatthias Ringwald                 break;
913ce263fc8SMatthias Ringwald             }
9143deb3ec6SMatthias Ringwald 
915a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
916a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
9173deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
918ce263fc8SMatthias Ringwald                 break;
9193deb3ec6SMatthias Ringwald             }
9203deb3ec6SMatthias Ringwald 
921a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
922ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
92360ebb071SMilanka Ringwald                     // printf("Format set, querying name\n");
924a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
925ce263fc8SMatthias Ringwald                     break;
926ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
927a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
928a0ffb263SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_callback, hfp_connection->network_operator);
929ce263fc8SMatthias Ringwald                     break;
930ce263fc8SMatthias Ringwald                 default:
931ce263fc8SMatthias Ringwald                     break;
9323deb3ec6SMatthias Ringwald             }
933ce263fc8SMatthias Ringwald 
934a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
935a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
936ce263fc8SMatthias Ringwald                 break;
9373deb3ec6SMatthias Ringwald             }
9383deb3ec6SMatthias Ringwald 
939a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
940aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
941a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9423deb3ec6SMatthias Ringwald                     break;
943ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
944a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
945ce263fc8SMatthias Ringwald                     break;
9463deb3ec6SMatthias Ringwald                 default:
9473deb3ec6SMatthias Ringwald                     break;
9483deb3ec6SMatthias Ringwald             }
9493deb3ec6SMatthias Ringwald             break;
9503deb3ec6SMatthias Ringwald         default:
9513deb3ec6SMatthias Ringwald             break;
9523deb3ec6SMatthias Ringwald     }
9533deb3ec6SMatthias Ringwald 
9543deb3ec6SMatthias Ringwald     // done
955a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
9563deb3ec6SMatthias Ringwald }
9573deb3ec6SMatthias Ringwald 
9583deb3ec6SMatthias Ringwald 
9593deb3ec6SMatthias Ringwald static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
9608a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
9618a46ec40SMatthias Ringwald 
9628a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
9638a46ec40SMatthias Ringwald     if (size < 1) return;
9649ec2630cSMatthias Ringwald 
965a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
966a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
9673deb3ec6SMatthias Ringwald 
9688a46ec40SMatthias Ringwald     // temp overwrite last byte (most likely \n for log_info)
9691e35c04dSMatthias Ringwald     char last_char = packet[size-1];
9701e35c04dSMatthias Ringwald     packet[size-1] = 0;
9711e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
9721e35c04dSMatthias Ringwald     packet[size-1] = last_char;
9731e35c04dSMatthias Ringwald 
9748a46ec40SMatthias Ringwald     // process messages byte-wise
975667ec068SMatthias Ringwald     int pos, i, value;
9763deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
977a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
978ce263fc8SMatthias Ringwald     }
9793deb3ec6SMatthias Ringwald 
980a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
981667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
982a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
983a0ffb263SMatthias Ringwald             // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type);
984a0ffb263SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
985667ec068SMatthias Ringwald             break;
986667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
987a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
988a0ffb263SMatthias Ringwald             // printf("Response and Hold status: %s\n", hfp_connection->line_buffer);
9892308e108SMilanka Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
990667ec068SMatthias Ringwald             break;
991667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
992a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
993a0ffb263SMatthias Ringwald             // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
994a0ffb263SMatthias Ringwald             //      hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
995a0ffb263SMatthias Ringwald             //      hfp_connection->bnip_number, hfp_connection->bnip_type);
996a0ffb263SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_callback, hfp_connection->clcc_idx,
997a0ffb263SMatthias Ringwald                 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
998a0ffb263SMatthias Ringwald                 hfp_connection->bnip_type, hfp_connection->bnip_number);
999667ec068SMatthias Ringwald             break;
1000ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1001a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10022308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1003667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1004667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1005ce263fc8SMatthias Ringwald             break;
1006ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1007a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10082308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1009667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1010667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1011ce263fc8SMatthias Ringwald             break;
1012ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1013a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1014a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1015a0ffb263SMatthias Ringwald             break;
1016a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1017a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1018a0ffb263SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1019a0ffb263SMatthias Ringwald             break;
1020a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1021a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
102289f1e358SMilanka Ringwald             hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1023ce263fc8SMatthias Ringwald             break;
1024ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1025a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1026a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1027a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1028a0ffb263SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1029ce263fc8SMatthias Ringwald             break;
1030ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1031a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1032a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1033a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1034ce263fc8SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1);
1035ce263fc8SMatthias Ringwald             break;
1036ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1037a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1038ce263fc8SMatthias Ringwald             break;
1039ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
1040a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_RING);
1041ce263fc8SMatthias Ringwald             break;
1042ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1043a0ffb263SMatthias Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1044a0ffb263SMatthias Ringwald                 if (hfp_connection->ag_indicators[i].status_changed) {
1045a0ffb263SMatthias Ringwald                     if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1046a0ffb263SMatthias Ringwald                         hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1047a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1048a0ffb263SMatthias Ringwald                         hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1049d5ff3b26SMatthias Ringwald                         // avoid set but not used warning
1050d5ff3b26SMatthias Ringwald                         (void) hfp_callheld_status;
1051a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1052a0ffb263SMatthias Ringwald                         hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1053ce263fc8SMatthias Ringwald                     }
1054a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[i].status_changed = 0;
1055a0ffb263SMatthias Ringwald                     hfp_emit_ag_indicator_event(hfp_callback, hfp_connection->ag_indicators[i]);
10563deb3ec6SMatthias Ringwald                     break;
10573deb3ec6SMatthias Ringwald                 }
10583deb3ec6SMatthias Ringwald             }
1059ce263fc8SMatthias Ringwald             break;
1060ce263fc8SMatthias Ringwald         default:
1061ce263fc8SMatthias Ringwald             break;
10623deb3ec6SMatthias Ringwald     }
1063a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
10643deb3ec6SMatthias Ringwald }
10653deb3ec6SMatthias Ringwald 
10660cb5b971SMatthias Ringwald static void hfp_run(void){
1067665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1068665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1069665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1070a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1071a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
10723deb3ec6SMatthias Ringwald     }
10733deb3ec6SMatthias Ringwald }
10743deb3ec6SMatthias Ringwald 
1075ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10763deb3ec6SMatthias Ringwald     switch (packet_type){
10773deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
10783deb3ec6SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size);
10793deb3ec6SMatthias Ringwald             break;
10803deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1081*323d3000SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1082202c8a4cSMatthias Ringwald             break;
10833deb3ec6SMatthias Ringwald         default:
10843deb3ec6SMatthias Ringwald             break;
10853deb3ec6SMatthias Ringwald     }
10863deb3ec6SMatthias Ringwald     hfp_run();
10873deb3ec6SMatthias Ringwald }
10883deb3ec6SMatthias Ringwald 
1089a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1090d63c37a1SMatthias Ringwald     // register for HCI events
1091d63c37a1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
1092d63c37a1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
1093d63c37a1SMatthias Ringwald 
1094d63c37a1SMatthias Ringwald     rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff);
1095a0ffb263SMatthias Ringwald 
1096d68dcce1SMatthias Ringwald     hfp_set_packet_handler_for_rfcomm_connections(&packet_handler);
1097d68dcce1SMatthias Ringwald 
1098a0ffb263SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1099a0ffb263SMatthias Ringwald     hfp_codecs_nr = 0;
1100a0ffb263SMatthias Ringwald     hfp_indicators_nr = 0;
1101a0ffb263SMatthias Ringwald     hfp_hf_speaker_gain = 9;
1102a0ffb263SMatthias Ringwald     hfp_hf_microphone_gain = 9;
1103a0ffb263SMatthias Ringwald }
1104a0ffb263SMatthias Ringwald 
1105a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
11063deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1107a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
11083deb3ec6SMatthias Ringwald         return;
11093deb3ec6SMatthias Ringwald     }
11103deb3ec6SMatthias Ringwald 
11113deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
11123deb3ec6SMatthias Ringwald     int i;
11133deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
11143deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
11153deb3ec6SMatthias Ringwald     }
11163deb3ec6SMatthias Ringwald }
11173deb3ec6SMatthias Ringwald 
1118a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
11193deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1120a0ffb263SMatthias Ringwald }
11213deb3ec6SMatthias Ringwald 
1122a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
11233deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
11243deb3ec6SMatthias Ringwald     int i;
1125a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
11263deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
11273deb3ec6SMatthias Ringwald     }
11283deb3ec6SMatthias Ringwald }
11293deb3ec6SMatthias Ringwald 
11303deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1131*323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
11323deb3ec6SMatthias Ringwald }
11333deb3ec6SMatthias Ringwald 
1134c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
1135c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1136a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1137a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1138a33eb0c4SMilanka Ringwald         return;
1139a33eb0c4SMilanka Ringwald     }
1140a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
1141a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11423deb3ec6SMatthias Ringwald }
11433deb3ec6SMatthias Ringwald 
1144c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
1145c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1146a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1147a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11483deb3ec6SMatthias Ringwald         return;
11493deb3ec6SMatthias Ringwald     }
1150a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
1151a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11523deb3ec6SMatthias Ringwald }
11533deb3ec6SMatthias Ringwald 
1154c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1155c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1156ce263fc8SMatthias Ringwald }
1157ce263fc8SMatthias Ringwald 
1158c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1159c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1160ce263fc8SMatthias Ringwald }
1161ce263fc8SMatthias Ringwald 
11623deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1163c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
1164c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1165a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1166a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11673deb3ec6SMatthias Ringwald         return;
11683deb3ec6SMatthias Ringwald     }
1169a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1170a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1171a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11723deb3ec6SMatthias Ringwald }
11733deb3ec6SMatthias Ringwald 
1174c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
1175c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1176a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1177a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11783deb3ec6SMatthias Ringwald         return;
11793deb3ec6SMatthias Ringwald     }
1180a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1181ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1182a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1183ce263fc8SMatthias Ringwald             break;
1184ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1185a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1186ce263fc8SMatthias Ringwald             break;
1187ce263fc8SMatthias Ringwald         default:
1188ce263fc8SMatthias Ringwald             break;
1189ce263fc8SMatthias Ringwald     }
1190a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11913deb3ec6SMatthias Ringwald }
11923deb3ec6SMatthias Ringwald 
1193c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
1194c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1195a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1196a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11973deb3ec6SMatthias Ringwald         return;
11983deb3ec6SMatthias Ringwald     }
1199a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1200a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12013deb3ec6SMatthias Ringwald }
12023deb3ec6SMatthias Ringwald 
1203ce263fc8SMatthias Ringwald 
1204c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1205c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1206ce263fc8SMatthias Ringwald }
1207ce263fc8SMatthias Ringwald 
1208c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1209c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1210ce263fc8SMatthias Ringwald }
1211ce263fc8SMatthias Ringwald 
1212ce263fc8SMatthias Ringwald 
1213c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
1214c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1215a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1216a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1217a33eb0c4SMilanka Ringwald         return;
1218a33eb0c4SMilanka Ringwald     }
1219a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1220ce263fc8SMatthias Ringwald 
1221a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1222a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
12233deb3ec6SMatthias Ringwald 
1224a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1225ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1226a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1227a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1228ce263fc8SMatthias Ringwald     } else {
1229a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1230aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1231aa4dd815SMatthias Ringwald                 break;
1232aa4dd815SMatthias Ringwald             default:
1233a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1234aa4dd815SMatthias Ringwald                 break;
12353deb3ec6SMatthias Ringwald         }
1236ce263fc8SMatthias Ringwald     }
1237ce263fc8SMatthias Ringwald 
1238a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12393deb3ec6SMatthias Ringwald }
12403deb3ec6SMatthias Ringwald 
1241c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
1242c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1243a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1244a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1245a33eb0c4SMilanka Ringwald         return;
1246a33eb0c4SMilanka Ringwald     }
1247a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
1248a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12493deb3ec6SMatthias Ringwald }
12503deb3ec6SMatthias Ringwald 
1251c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
1252c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1253a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1254a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1255a33eb0c4SMilanka Ringwald         return;
1256a33eb0c4SMilanka Ringwald     }
1257ce263fc8SMatthias Ringwald 
1258ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1259a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
1260a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1261ce263fc8SMatthias Ringwald     } else {
1262ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1263ce263fc8SMatthias Ringwald     }
1264ce263fc8SMatthias Ringwald }
1265ce263fc8SMatthias Ringwald 
1266c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
1267c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1268a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1269a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1270a33eb0c4SMilanka Ringwald         return;
1271a33eb0c4SMilanka Ringwald     }
1272a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
1273a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1274ce263fc8SMatthias Ringwald }
1275ce263fc8SMatthias Ringwald 
1276c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
1277c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1278a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1279a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1280a33eb0c4SMilanka Ringwald         return;
1281a33eb0c4SMilanka Ringwald     }
1282ce263fc8SMatthias Ringwald 
1283ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1284a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
1285a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1286ce263fc8SMatthias Ringwald     }
1287ce263fc8SMatthias Ringwald }
1288ce263fc8SMatthias Ringwald 
1289c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){
1290c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1291a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1292a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1293a33eb0c4SMilanka Ringwald         return;
1294a33eb0c4SMilanka Ringwald     }
1295ce263fc8SMatthias Ringwald 
1296ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1297a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
1298a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1299ce263fc8SMatthias Ringwald     }
1300ce263fc8SMatthias Ringwald }
1301ce263fc8SMatthias Ringwald 
1302c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
1303c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1304a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1305a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1306a33eb0c4SMilanka Ringwald         return;
1307a33eb0c4SMilanka Ringwald     }
1308ce263fc8SMatthias Ringwald 
1309ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1310ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1311a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
1312a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1313ce263fc8SMatthias Ringwald     }
1314ce263fc8SMatthias Ringwald }
1315ce263fc8SMatthias Ringwald 
1316c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){
1317c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1318a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1319a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1320a33eb0c4SMilanka Ringwald         return;
1321a33eb0c4SMilanka Ringwald     }
1322ce263fc8SMatthias Ringwald 
1323ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1324ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1325a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
1326a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1327ce263fc8SMatthias Ringwald     }
1328ce263fc8SMatthias Ringwald }
1329ce263fc8SMatthias Ringwald 
1330c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
1331c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1332a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1333a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1334a33eb0c4SMilanka Ringwald         return;
1335a33eb0c4SMilanka Ringwald     }
1336ce263fc8SMatthias Ringwald 
1337ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1338ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1339a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
1340a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1341ce263fc8SMatthias Ringwald     }
1342ce263fc8SMatthias Ringwald }
1343ce263fc8SMatthias Ringwald 
1344c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
1345c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1346a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1347a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1348a33eb0c4SMilanka Ringwald         return;
1349a33eb0c4SMilanka Ringwald     }
1350ce263fc8SMatthias Ringwald 
1351ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1352ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1353a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
1354a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1355ce263fc8SMatthias Ringwald     }
1356ce263fc8SMatthias Ringwald }
1357ce263fc8SMatthias Ringwald 
1358c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
1359c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1360a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1361a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1362a33eb0c4SMilanka Ringwald         return;
1363a33eb0c4SMilanka Ringwald     }
1364667ec068SMatthias Ringwald 
1365667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1366667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1367a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1368a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
1369a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1370667ec068SMatthias Ringwald     }
1371667ec068SMatthias Ringwald }
1372667ec068SMatthias Ringwald 
1373c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
1374c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1375a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1376a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1377a33eb0c4SMilanka Ringwald         return;
1378a33eb0c4SMilanka Ringwald     }
1379667ec068SMatthias Ringwald 
1380667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1381667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1382a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1383a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
1384a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1385667ec068SMatthias Ringwald     }
1386667ec068SMatthias Ringwald }
1387ce263fc8SMatthias Ringwald 
1388c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
1389c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1390a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1391a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1392a33eb0c4SMilanka Ringwald         return;
1393a33eb0c4SMilanka Ringwald     }
1394ce263fc8SMatthias Ringwald 
1395a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1396ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1397a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1398ce263fc8SMatthias Ringwald }
1399ce263fc8SMatthias Ringwald 
1400c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
1401c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1402a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1403a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1404a33eb0c4SMilanka Ringwald         return;
1405a33eb0c4SMilanka Ringwald     }
1406ce263fc8SMatthias Ringwald 
1407a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1408a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1409a0ffb263SMatthias Ringwald 
1410a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1411ce263fc8SMatthias Ringwald }
1412ce263fc8SMatthias Ringwald 
1413c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
1414c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1415a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1416a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1417a33eb0c4SMilanka Ringwald         return;
1418a33eb0c4SMilanka Ringwald     }
1419ce263fc8SMatthias Ringwald 
1420a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
1421a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1422ce263fc8SMatthias Ringwald }
1423ce263fc8SMatthias Ringwald 
1424c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
1425c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1426a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1427a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1428a33eb0c4SMilanka Ringwald         return;
1429a33eb0c4SMilanka Ringwald     }
1430ce263fc8SMatthias Ringwald 
1431a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
1432a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1433ce263fc8SMatthias Ringwald }
1434ce263fc8SMatthias Ringwald 
1435ce263fc8SMatthias Ringwald 
1436c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
1437c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1438a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1439a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1440a33eb0c4SMilanka Ringwald         return;
1441a33eb0c4SMilanka Ringwald     }
1442ce263fc8SMatthias Ringwald 
1443a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1444a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1445ce263fc8SMatthias Ringwald }
1446ce263fc8SMatthias Ringwald 
1447ce263fc8SMatthias Ringwald 
1448c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
1449c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1450a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1451a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1452a33eb0c4SMilanka Ringwald         return;
1453a33eb0c4SMilanka Ringwald     }
1454ce263fc8SMatthias Ringwald 
1455a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
1456a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1457ce263fc8SMatthias Ringwald }
1458ce263fc8SMatthias Ringwald 
1459c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
1460c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1461a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1462a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1463a33eb0c4SMilanka Ringwald         return;
1464a33eb0c4SMilanka Ringwald     }
1465ce263fc8SMatthias Ringwald 
1466a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
1467a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1468ce263fc8SMatthias Ringwald }
1469ce263fc8SMatthias Ringwald 
1470ce263fc8SMatthias Ringwald 
1471c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1472c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1473a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1474a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1475a33eb0c4SMilanka Ringwald         return;
1476a33eb0c4SMilanka Ringwald     }
1477ce263fc8SMatthias Ringwald 
1478a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1479a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1480ce263fc8SMatthias Ringwald }
1481ce263fc8SMatthias Ringwald 
1482c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1483c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1484a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1485a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1486a33eb0c4SMilanka Ringwald         return;
1487a33eb0c4SMilanka Ringwald     }
1488ce263fc8SMatthias Ringwald 
1489a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1490a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1491ce263fc8SMatthias Ringwald }
1492ce263fc8SMatthias Ringwald 
1493c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
1494c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1495a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1496a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1497a33eb0c4SMilanka Ringwald         return;
1498a33eb0c4SMilanka Ringwald     }
1499ce263fc8SMatthias Ringwald 
1500a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
1501a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1502ce263fc8SMatthias Ringwald }
1503ce263fc8SMatthias Ringwald 
1504c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
1505c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1506a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1507a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1508a33eb0c4SMilanka Ringwald         return;
1509a33eb0c4SMilanka Ringwald     }
1510ce263fc8SMatthias Ringwald 
1511a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1512a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1513ce263fc8SMatthias Ringwald }
1514ce263fc8SMatthias Ringwald 
1515c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
1516c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1517a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1518a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1519a33eb0c4SMilanka Ringwald         return;
1520a33eb0c4SMilanka Ringwald     }
1521c8626498SMilanka Ringwald 
1522a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1523a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1524a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1525a0ffb263SMatthias Ringwald         return;
1526a0ffb263SMatthias Ringwald     }
1527a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1528a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1529a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1530ce263fc8SMatthias Ringwald }
1531ce263fc8SMatthias Ringwald 
1532c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
1533c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1534a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1535a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1536a33eb0c4SMilanka Ringwald         return;
1537a33eb0c4SMilanka Ringwald     }
1538c8626498SMilanka Ringwald 
1539a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1540a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1541a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1542a0ffb263SMatthias Ringwald         return;
1543a0ffb263SMatthias Ringwald     }
1544a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1545a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1546a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1547ce263fc8SMatthias Ringwald }
1548ce263fc8SMatthias Ringwald 
1549c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
1550c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1551a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1552a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1553a33eb0c4SMilanka Ringwald         return;
1554a33eb0c4SMilanka Ringwald     }
1555a33eb0c4SMilanka Ringwald 
1556a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
1557a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1558ce263fc8SMatthias Ringwald }
1559ce263fc8SMatthias Ringwald 
1560c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
1561c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1562a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1563a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1564a33eb0c4SMilanka Ringwald         return;
1565a33eb0c4SMilanka Ringwald     }
1566a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
1567a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1568ce263fc8SMatthias Ringwald }
15693deb3ec6SMatthias Ringwald 
1570c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
1571c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1572a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1573a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1574a33eb0c4SMilanka Ringwald         return;
1575a33eb0c4SMilanka Ringwald     }
1576a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
1577a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1578667ec068SMatthias Ringwald }
1579667ec068SMatthias Ringwald 
1580667ec068SMatthias Ringwald 
1581c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
1582c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1583a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1584a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1585a33eb0c4SMilanka Ringwald         return;
1586a33eb0c4SMilanka Ringwald     }
1587a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1588a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
1589a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1590667ec068SMatthias Ringwald }
1591667ec068SMatthias Ringwald 
1592c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
1593c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1594a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1595a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1596a33eb0c4SMilanka Ringwald         return;
1597a33eb0c4SMilanka Ringwald     }
1598a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1599a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
1600a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1601667ec068SMatthias Ringwald }
1602667ec068SMatthias Ringwald 
1603c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
1604c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1605a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1606a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1607a33eb0c4SMilanka Ringwald         return;
1608a33eb0c4SMilanka Ringwald     }
1609a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1610a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
1611a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1612667ec068SMatthias Ringwald }
1613667ec068SMatthias Ringwald 
1614c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
1615c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1616a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1617a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1618a33eb0c4SMilanka Ringwald         return;
1619a33eb0c4SMilanka Ringwald     }
1620a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1621a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
1622a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1623667ec068SMatthias Ringwald }
1624667ec068SMatthias Ringwald 
1625c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
1626c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1627a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1628a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1629a33eb0c4SMilanka Ringwald         return;
1630a33eb0c4SMilanka Ringwald     }
1631a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
1632a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1633667ec068SMatthias Ringwald }
1634667ec068SMatthias Ringwald 
1635c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
1636c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1637a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1638a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1639a33eb0c4SMilanka Ringwald         return;
1640a33eb0c4SMilanka Ringwald     }
1641667ec068SMatthias Ringwald     // find index for assigned number
1642667ec068SMatthias Ringwald     int i;
1643667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1644667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1645667ec068SMatthias Ringwald             // set value
1646667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1647667ec068SMatthias Ringwald             // mark for update
1648a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1649a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1650667ec068SMatthias Ringwald                 // send update
1651a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1652a0ffb263SMatthias Ringwald             }
1653667ec068SMatthias Ringwald             return;
1654667ec068SMatthias Ringwald         }
1655667ec068SMatthias Ringwald     }
1656667ec068SMatthias Ringwald }
1657667ec068SMatthias Ringwald 
1658