xref: /btstack/src/classic/hfp_hf.c (revision e9c22d4ea120f82e0a0f4f9d59236a70a8ab6236)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hfp_hf.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
42fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493deb3ec6SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <stdlib.h>
513deb3ec6SMatthias Ringwald #include <string.h>
523deb3ec6SMatthias Ringwald 
53235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
5459c6af15SMatthias Ringwald #include "btstack_debug.h"
553deb3ec6SMatthias Ringwald #include "btstack_memory.h"
5659c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5759c6af15SMatthias Ringwald #include "classic/core.h"
5859c6af15SMatthias Ringwald #include "classic/hfp.h"
5959c6af15SMatthias Ringwald #include "classic/hfp_hf.h"
60efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
61746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
62023f2764SMatthias Ringwald #include "classic/sdp_util.h"
6359c6af15SMatthias Ringwald #include "hci.h"
6459c6af15SMatthias Ringwald #include "hci_cmd.h"
6559c6af15SMatthias Ringwald #include "hci_dump.h"
6659c6af15SMatthias Ringwald #include "l2cap.h"
673deb3ec6SMatthias Ringwald 
683deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
693deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
703deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
713deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
723deb3ec6SMatthias Ringwald 
733deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0;
743deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS];
75667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS];
76667ec068SMatthias Ringwald 
77667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9;
78667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9;
793deb3ec6SMatthias Ringwald 
80ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
813deb3ec6SMatthias Ringwald 
82ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
83ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
84ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
85ce263fc8SMatthias Ringwald 
86ce263fc8SMatthias Ringwald static char phone_number[25];
87ce263fc8SMatthias Ringwald 
88c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
89c5b64319SMatthias Ringwald 
9013839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
913deb3ec6SMatthias Ringwald     if (callback == NULL){
923deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
933deb3ec6SMatthias Ringwald         return;
943deb3ec6SMatthias Ringwald     }
95ca59be51SMatthias Ringwald     hfp_hf_callback = callback;
96ca59be51SMatthias Ringwald     hfp_set_hf_callback(callback);
973deb3ec6SMatthias Ringwald }
983deb3ec6SMatthias Ringwald 
9913839019SMatthias Ringwald static void hfp_hf_emit_subscriber_information(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t status, uint8_t bnip_type, const char * bnip_number){
100a0ffb263SMatthias Ringwald     if (!callback) return;
101a0ffb263SMatthias Ringwald     uint8_t event[31];
102a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
103a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
104a0ffb263SMatthias Ringwald     event[2] = event_subtype;
105a0ffb263SMatthias Ringwald     event[3] = status;
106a0ffb263SMatthias Ringwald     event[4] = bnip_type;
107adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 6) ? (int) strlen(bnip_number) : (int) sizeof(event) - 6;
108a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], bnip_number, size);
109a0ffb263SMatthias Ringwald     event[5 + size] = 0;
11013839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
111a0ffb263SMatthias Ringwald }
112a0ffb263SMatthias Ringwald 
11313839019SMatthias Ringwald static void hfp_hf_emit_type_and_number(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t bnip_type, const char * bnip_number){
114a0ffb263SMatthias Ringwald     if (!callback) return;
115a0ffb263SMatthias Ringwald     uint8_t event[30];
116a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
117a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
118a0ffb263SMatthias Ringwald     event[2] = event_subtype;
119a0ffb263SMatthias Ringwald     event[3] = bnip_type;
120adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 5) ? (int) strlen(bnip_number) : (int) sizeof(event) - 5;
121a0ffb263SMatthias Ringwald     strncpy((char*)&event[4], bnip_number, size);
122a0ffb263SMatthias Ringwald     event[4 + size] = 0;
12313839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
124a0ffb263SMatthias Ringwald }
125a0ffb263SMatthias Ringwald 
12613839019SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir,
127a0ffb263SMatthias Ringwald                 uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){
128a0ffb263SMatthias Ringwald     if (!callback) return;
129a0ffb263SMatthias Ringwald     uint8_t event[35];
130a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
131a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
132a0ffb263SMatthias Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
133a0ffb263SMatthias Ringwald     event[3] = clcc_idx;
134a0ffb263SMatthias Ringwald     event[4] = clcc_dir;
135a0ffb263SMatthias Ringwald     event[6] = clcc_status;
136a0ffb263SMatthias Ringwald     event[7] = clcc_mpty;
137a0ffb263SMatthias Ringwald     event[8] = bnip_type;
138adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 10) ? (int) strlen(bnip_number) : (int) sizeof(event) - 10;
139a0ffb263SMatthias Ringwald     strncpy((char*)&event[9], bnip_number, size);
140a0ffb263SMatthias Ringwald     event[9 + size] = 0;
14113839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
142a0ffb263SMatthias Ringwald }
143a0ffb263SMatthias Ringwald 
144a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
1453deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
146a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1473deb3ec6SMatthias Ringwald     return hf && ag;
1483deb3ec6SMatthias Ringwald }
1493deb3ec6SMatthias Ringwald 
150a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
1513deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
152a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1533deb3ec6SMatthias Ringwald     return hf && ag;
1543deb3ec6SMatthias Ringwald }
1553deb3ec6SMatthias Ringwald 
1563deb3ec6SMatthias Ringwald 
157a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
1583deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
159a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
1603deb3ec6SMatthias Ringwald     return hf && ag;
1613deb3ec6SMatthias Ringwald }
1623deb3ec6SMatthias Ringwald 
1634f84bf36SMatthias 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){
1643deb3ec6SMatthias Ringwald     if (!name){
1653deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1663deb3ec6SMatthias Ringwald     }
167235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
1683deb3ec6SMatthias Ringwald 
1694f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
1704f84bf36SMatthias Ringwald     //
1714f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1724f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
1734f84bf36SMatthias Ringwald     //
1744f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
1754f84bf36SMatthias Ringwald     if (supported_features & wide_band_speech){
1764f84bf36SMatthias Ringwald         sdp_features |= 1 << 5; // Wide band speech bit
1774f84bf36SMatthias Ringwald     }
178aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
1794f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
180aa4dd815SMatthias Ringwald }
1813deb3ec6SMatthias Ringwald 
18289425bfcSMilanka Ringwald 
18389425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
1843deb3ec6SMatthias Ringwald     char buffer[20];
18589425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r\n", cmd);
18689425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
18789425bfcSMilanka Ringwald }
18889425bfcSMilanka Ringwald 
18989425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
19089425bfcSMilanka Ringwald     char buffer[20];
19189425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r\n", cmd, mark);
19289425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
19389425bfcSMilanka Ringwald }
19489425bfcSMilanka Ringwald 
19589425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
19689425bfcSMilanka Ringwald     char buffer[40];
19789425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r\n", cmd, value);
1983deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1993deb3ec6SMatthias Ringwald }
2003deb3ec6SMatthias Ringwald 
2013deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2023deb3ec6SMatthias Ringwald     char buffer[30];
20389425bfcSMilanka Ringwald     const int size = sizeof(buffer);
20489425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
20589425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
20689425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2073deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2083deb3ec6SMatthias Ringwald }
2093deb3ec6SMatthias Ringwald 
2103deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2113deb3ec6SMatthias Ringwald     char buffer[50];
21289425bfcSMilanka Ringwald     const int size = sizeof(buffer);
21389425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
21489425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
21589425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2163deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2173deb3ec6SMatthias Ringwald }
2183deb3ec6SMatthias Ringwald 
2193deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2203deb3ec6SMatthias Ringwald     char buffer[30];
22189425bfcSMilanka Ringwald     const int size = sizeof(buffer);
22289425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
22389425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
22489425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2253deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2263deb3ec6SMatthias Ringwald }
2273deb3ec6SMatthias Ringwald 
22889425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2293deb3ec6SMatthias Ringwald     char buffer[20];
23089425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
231ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
232ce263fc8SMatthias Ringwald }
233ce263fc8SMatthias Ringwald 
234ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
235ce263fc8SMatthias Ringwald     char buffer[40];
23689425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
237ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
238ce263fc8SMatthias Ringwald }
239ce263fc8SMatthias Ringwald 
240a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
241ce263fc8SMatthias Ringwald     char buffer[40];
24289425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id);
243ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
244ce263fc8SMatthias Ringwald }
245ce263fc8SMatthias Ringwald 
246f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
24789425bfcSMilanka Ringwald     char buffer[40];
24889425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
249ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
250ce263fc8SMatthias Ringwald }
251ce263fc8SMatthias Ringwald 
252ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
253ce263fc8SMatthias Ringwald     char buffer[20];
25489425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
255ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
256ce263fc8SMatthias Ringwald }
257ce263fc8SMatthias Ringwald 
25889425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
25989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
26089425bfcSMilanka Ringwald }
26189425bfcSMilanka Ringwald 
26289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
26389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
26489425bfcSMilanka Ringwald }
26589425bfcSMilanka Ringwald 
26689425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
26789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
26889425bfcSMilanka Ringwald }
26989425bfcSMilanka Ringwald 
27089425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
27189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
27289425bfcSMilanka Ringwald }
27389425bfcSMilanka Ringwald 
27489425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
27589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
27689425bfcSMilanka Ringwald }
27789425bfcSMilanka Ringwald 
27889425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
27989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
28089425bfcSMilanka Ringwald }
28189425bfcSMilanka Ringwald 
28289425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
28389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
28489425bfcSMilanka Ringwald }
28589425bfcSMilanka Ringwald 
28689425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
28789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
28889425bfcSMilanka Ringwald }
28989425bfcSMilanka Ringwald 
29089425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
29189425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
29289425bfcSMilanka Ringwald }
29389425bfcSMilanka Ringwald 
29489425bfcSMilanka Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
29589425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_CALL_ANSWERED);
29689425bfcSMilanka Ringwald }
29789425bfcSMilanka Ringwald 
29889425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
29989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
30089425bfcSMilanka Ringwald }
30189425bfcSMilanka Ringwald 
30289425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
30389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
30489425bfcSMilanka Ringwald }
30589425bfcSMilanka Ringwald 
30689425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
30789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
30889425bfcSMilanka Ringwald }
30989425bfcSMilanka Ringwald 
31089425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
31189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
31289425bfcSMilanka Ringwald }
31389425bfcSMilanka Ringwald 
31489425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
31589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
31689425bfcSMilanka Ringwald }
31789425bfcSMilanka Ringwald 
31889425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
31989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
32089425bfcSMilanka Ringwald }
32189425bfcSMilanka Ringwald 
32289425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
32389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
32489425bfcSMilanka Ringwald }
32589425bfcSMilanka Ringwald 
32689425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
32789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
32889425bfcSMilanka Ringwald }
32989425bfcSMilanka Ringwald 
33089425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
33189425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
33289425bfcSMilanka Ringwald }
33389425bfcSMilanka Ringwald 
33489425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
33589425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
33689425bfcSMilanka Ringwald }
33789425bfcSMilanka Ringwald 
338ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
33989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
340ce263fc8SMatthias Ringwald }
341ce263fc8SMatthias Ringwald 
342667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
34389425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
344667ec068SMatthias Ringwald }
345667ec068SMatthias Ringwald 
34613839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){
3473deb3ec6SMatthias Ringwald     if (!callback) return;
348a0ffb263SMatthias Ringwald     uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1];
3493deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3503deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3513deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
352a0ffb263SMatthias Ringwald     event[3] = indicator.index;
353a0ffb263SMatthias Ringwald     event[4] = indicator.status;
354a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
355a0ffb263SMatthias Ringwald     event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0;
35613839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3573deb3ec6SMatthias Ringwald }
3583deb3ec6SMatthias Ringwald 
35913839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){
3603deb3ec6SMatthias Ringwald     if (!callback) return;
36189425bfcSMilanka Ringwald     uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
3623deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3633deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3643deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
365a0ffb263SMatthias Ringwald     event[3] = network_operator.mode;
366a0ffb263SMatthias Ringwald     event[4] = network_operator.format;
36789425bfcSMilanka Ringwald     strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
36889425bfcSMilanka Ringwald     event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
36913839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3703deb3ec6SMatthias Ringwald }
3713deb3ec6SMatthias Ringwald 
372a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
373a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
374a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
375aa4dd815SMatthias Ringwald     int done = 1;
3763deb3ec6SMatthias Ringwald 
377a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
3783deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
379d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
380a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
381a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
3823deb3ec6SMatthias Ringwald             break;
3833deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
384a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
385a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
3863deb3ec6SMatthias Ringwald             break;
3873deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
388a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
389a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
3903deb3ec6SMatthias Ringwald             break;
3913deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
392a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
393a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
3943deb3ec6SMatthias Ringwald             break;
3953deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
396a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
397a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
3983deb3ec6SMatthias Ringwald             break;
3993deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
400a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
401a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4023deb3ec6SMatthias Ringwald             break;
4033deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
404a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
405a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4063deb3ec6SMatthias Ringwald             break;
4073deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
408a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
409a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4103deb3ec6SMatthias Ringwald             break;
4113deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
412a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
413a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4143deb3ec6SMatthias Ringwald             break;
4153deb3ec6SMatthias Ringwald         default:
416aa4dd815SMatthias Ringwald             done = 0;
4173deb3ec6SMatthias Ringwald             break;
4183deb3ec6SMatthias Ringwald     }
4193deb3ec6SMatthias Ringwald     return done;
4203deb3ec6SMatthias Ringwald }
4213deb3ec6SMatthias Ringwald 
422ce263fc8SMatthias Ringwald 
423a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
424a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
425a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
426ce263fc8SMatthias Ringwald 
427ce263fc8SMatthias Ringwald     int done = 0;
428a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
429a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
430ce263fc8SMatthias Ringwald         done = 1;
431a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
432ce263fc8SMatthias Ringwald         return done;
433ce263fc8SMatthias Ringwald     };
434a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
435a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
436ce263fc8SMatthias Ringwald         done = 1;
437a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
438a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
439a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
440ce263fc8SMatthias Ringwald         return done;
441ce263fc8SMatthias Ringwald     }
442ce263fc8SMatthias Ringwald 
443a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
444ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
445a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
446a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
447a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
448ce263fc8SMatthias Ringwald             return 1;
449ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
450a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
451a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
452a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
453ce263fc8SMatthias Ringwald             return 1;
454ce263fc8SMatthias Ringwald         default:
455ce263fc8SMatthias Ringwald             break;
456ce263fc8SMatthias Ringwald     }
457ce263fc8SMatthias Ringwald 
458a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
459a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
460ce263fc8SMatthias Ringwald         done = 1;
461a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
462ce263fc8SMatthias Ringwald         return done;
463ce263fc8SMatthias Ringwald     }
464ce263fc8SMatthias Ringwald 
465ce263fc8SMatthias Ringwald     return done;
466ce263fc8SMatthias Ringwald }
467ce263fc8SMatthias Ringwald 
468a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
469ce263fc8SMatthias Ringwald     /* events ( == commands):
470ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
471ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
472ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
473ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
474ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
475ce263fc8SMatthias Ringwald     */
476ce263fc8SMatthias Ringwald 
477a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
478ce263fc8SMatthias Ringwald 
479a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
480ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
481a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
482ce263fc8SMatthias Ringwald 
483a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
484a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
485a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
486ce263fc8SMatthias Ringwald             return 1;
487ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
488a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = 0;
489a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = 0;
490a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = 0;
491ce263fc8SMatthias Ringwald 
492a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
493a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
494a0ffb263SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
495ce263fc8SMatthias Ringwald             break;
496ce263fc8SMatthias Ringwald 
4976a7f44bdSMilanka Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:{
4986a7f44bdSMilanka Ringwald             if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
499a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
500a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
501a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
5020e0be203SMatthias Ringwald                 hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
5030e0be203SMatthias Ringwald                 log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
504fcb08cdbSMilanka Ringwald                 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
505ce263fc8SMatthias Ringwald             } else {
506a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
507a0ffb263SMatthias Ringwald                 hfp_connection->suggested_codec = 0;
508a0ffb263SMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
509a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
510a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
511a0ffb263SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
512ce263fc8SMatthias Ringwald 
513ce263fc8SMatthias Ringwald             }
514ce263fc8SMatthias Ringwald             break;
5156a7f44bdSMilanka Ringwald         }
516ce263fc8SMatthias Ringwald         default:
517ce263fc8SMatthias Ringwald             break;
518ce263fc8SMatthias Ringwald     }
519ce263fc8SMatthias Ringwald     return 0;
520ce263fc8SMatthias Ringwald }
521ce263fc8SMatthias Ringwald 
522a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
523a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
524a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
525ce263fc8SMatthias Ringwald 
526ce263fc8SMatthias Ringwald 
527a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
528a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
529a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
530a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
531ce263fc8SMatthias Ringwald         return 1;
532ce263fc8SMatthias Ringwald     }
533ce263fc8SMatthias Ringwald 
534a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
535ce263fc8SMatthias Ringwald 
536ce263fc8SMatthias Ringwald     // run codecs exchange
537a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
538ce263fc8SMatthias Ringwald     if (done) return 1;
539ce263fc8SMatthias Ringwald 
540ce263fc8SMatthias Ringwald     return 0;
541ce263fc8SMatthias Ringwald }
542ce263fc8SMatthias Ringwald 
543a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
544a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
545a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
546a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
547ce263fc8SMatthias Ringwald         return 1;
548ce263fc8SMatthias Ringwald     }
549ce263fc8SMatthias Ringwald     return 0;
550ce263fc8SMatthias Ringwald }
551ce263fc8SMatthias Ringwald 
552a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){
553a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
554724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
5557522e673SMatthias Ringwald 
556b72c4a9eSMatthias Ringwald     if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){
557b72c4a9eSMatthias Ringwald 
558b72c4a9eSMatthias Ringwald         hfp_connection->hf_accept_sco = 0;
5597522e673SMatthias Ringwald 
5607522e673SMatthias Ringwald         // notify about codec selection if not done already
5617522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
5627522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
5637522e673SMatthias Ringwald         }
5647522e673SMatthias Ringwald 
5657522e673SMatthias Ringwald         // remote supported feature eSCO is set if link type is eSCO
5667522e673SMatthias Ringwald         // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
5677522e673SMatthias Ringwald         uint16_t max_latency;
5687522e673SMatthias Ringwald         uint8_t  retransmission_effort;
5697522e673SMatthias Ringwald         uint16_t packet_types;
5707522e673SMatthias Ringwald 
571d9f77559SMatthias Ringwald         if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
5727522e673SMatthias Ringwald             max_latency = 0x000c;
5737522e673SMatthias Ringwald             retransmission_effort = 0x02;
5747522e673SMatthias Ringwald             packet_types = 0x388;
5757522e673SMatthias Ringwald         } else {
5767522e673SMatthias Ringwald             max_latency = 0xffff;
5777522e673SMatthias Ringwald             retransmission_effort = 0xff;
5787522e673SMatthias Ringwald             packet_types = 0x003f;
5797522e673SMatthias Ringwald         }
5807522e673SMatthias Ringwald 
5817522e673SMatthias Ringwald         uint16_t sco_voice_setting = hci_get_sco_voice_setting();
5827522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
5837522e673SMatthias Ringwald             sco_voice_setting = 0x0043; // Transparent data
5847522e673SMatthias Ringwald         }
5857522e673SMatthias Ringwald 
586b72c4a9eSMatthias Ringwald         log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting);
5877522e673SMatthias Ringwald         hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
5887522e673SMatthias Ringwald                         sco_voice_setting, retransmission_effort, packet_types);
5897522e673SMatthias Ringwald         return;
5907522e673SMatthias Ringwald     }
5917522e673SMatthias Ringwald 
592a0ffb263SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return;
593ce263fc8SMatthias Ringwald 
594a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
595ce263fc8SMatthias Ringwald     if (!done){
596a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
597ce263fc8SMatthias Ringwald     }
598ce263fc8SMatthias Ringwald     if (!done){
599a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
600ce263fc8SMatthias Ringwald     }
601ce263fc8SMatthias Ringwald     if (!done){
602a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
603ce263fc8SMatthias Ringwald     }
604ce263fc8SMatthias Ringwald 
6051016a228SMatthias Ringwald     // don't send a new command while ok still pending
6061016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6071016a228SMatthias Ringwald 
608a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
609a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
610a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
611a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
612ce263fc8SMatthias Ringwald         return;
613ce263fc8SMatthias Ringwald     }
614ce263fc8SMatthias Ringwald 
615a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
616a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
617a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
618a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
619ce263fc8SMatthias Ringwald         return;
620ce263fc8SMatthias Ringwald     }
621ce263fc8SMatthias Ringwald 
622a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
623a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
624a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
625a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
626ce263fc8SMatthias Ringwald         return;
627ce263fc8SMatthias Ringwald     }
628ce263fc8SMatthias Ringwald 
629a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
630a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
631a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
632a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
633ce263fc8SMatthias Ringwald         return;
634ce263fc8SMatthias Ringwald     }
635ce263fc8SMatthias Ringwald 
636a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
637a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
638a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
639a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
640ce263fc8SMatthias Ringwald         return;
641ce263fc8SMatthias Ringwald     }
642ce263fc8SMatthias Ringwald 
643a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
644a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
645a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
646a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
647ce263fc8SMatthias Ringwald         return;
648ce263fc8SMatthias Ringwald     }
649ce263fc8SMatthias Ringwald 
650a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
651a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
652a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
653a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
654ce263fc8SMatthias Ringwald         return;
655ce263fc8SMatthias Ringwald     }
656ce263fc8SMatthias Ringwald 
657a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
658a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
659a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
660a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
661ce263fc8SMatthias Ringwald         return;
662ce263fc8SMatthias Ringwald     }
663ce263fc8SMatthias Ringwald 
664ce263fc8SMatthias Ringwald 
665a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
666a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
667a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
668a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
669ce263fc8SMatthias Ringwald         return;
670ce263fc8SMatthias Ringwald     }
671ce263fc8SMatthias Ringwald 
672a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
673a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
674a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
675a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
676ce263fc8SMatthias Ringwald         return;
677ce263fc8SMatthias Ringwald     }
678ce263fc8SMatthias Ringwald 
679a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
680a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
681a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
682a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
683ce263fc8SMatthias Ringwald         return;
684ce263fc8SMatthias Ringwald     }
685ce263fc8SMatthias Ringwald 
686a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
687a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
688a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
689a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
690ce263fc8SMatthias Ringwald         return;
691ce263fc8SMatthias Ringwald     }
692ce263fc8SMatthias Ringwald 
693a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
694a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
695a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
696a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
697ce263fc8SMatthias Ringwald         return;
698ce263fc8SMatthias Ringwald     }
699ce263fc8SMatthias Ringwald 
700a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
701a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
702a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
703a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
704ce263fc8SMatthias Ringwald         return;
705ce263fc8SMatthias Ringwald     }
706ce263fc8SMatthias Ringwald 
707a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
708a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
709a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
710a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
711ce263fc8SMatthias Ringwald         return;
712ce263fc8SMatthias Ringwald     }
713ce263fc8SMatthias Ringwald 
714a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
715a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
716a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
717a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
718ce263fc8SMatthias Ringwald         return;
719ce263fc8SMatthias Ringwald     }
720ce263fc8SMatthias Ringwald 
721a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
722a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
723a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
724a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
725ce263fc8SMatthias Ringwald         return;
726ce263fc8SMatthias Ringwald     }
727ce263fc8SMatthias Ringwald 
728a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
729a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
730a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
731a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
732ce263fc8SMatthias Ringwald         return;
733ce263fc8SMatthias Ringwald     }
734ce263fc8SMatthias Ringwald 
735a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
736a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
737a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
738a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
739ce263fc8SMatthias Ringwald         return;
740ce263fc8SMatthias Ringwald     }
741ce263fc8SMatthias Ringwald 
742a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
743a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
744a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
745a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
746667ec068SMatthias Ringwald         return;
747667ec068SMatthias Ringwald     }
748667ec068SMatthias Ringwald 
749a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
750a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
751a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
752a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
753a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
754ce263fc8SMatthias Ringwald         return;
755ce263fc8SMatthias Ringwald     }
756ce263fc8SMatthias Ringwald 
757a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
758a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
759a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
760a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
761ce263fc8SMatthias Ringwald         return;
762ce263fc8SMatthias Ringwald     }
763ce263fc8SMatthias Ringwald 
764a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
765a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
766a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
767a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
768667ec068SMatthias Ringwald         return;
769667ec068SMatthias Ringwald     }
770667ec068SMatthias Ringwald 
771a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
772a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
773667ec068SMatthias Ringwald         char buffer[20];
774a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
775667ec068SMatthias Ringwald             case '?':
776667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
777a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
778667ec068SMatthias Ringwald                 return;
779667ec068SMatthias Ringwald             case '0':
780667ec068SMatthias Ringwald             case '1':
781667ec068SMatthias Ringwald             case '2':
782a0ffb263SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
783a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
784667ec068SMatthias Ringwald                 return;
785667ec068SMatthias Ringwald             default:
786667ec068SMatthias Ringwald                 break;
787667ec068SMatthias Ringwald         }
788667ec068SMatthias Ringwald         return;
789667ec068SMatthias Ringwald     }
790667ec068SMatthias Ringwald 
791a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
792a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
793667ec068SMatthias Ringwald         char buffer[20];
794667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
795a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
796667ec068SMatthias Ringwald         return;
797667ec068SMatthias Ringwald     }
798667ec068SMatthias Ringwald 
799667ec068SMatthias Ringwald     // update HF indicators
800a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
801667ec068SMatthias Ringwald         int i;
802667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
803a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
804a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
805a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
806a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
807667ec068SMatthias Ringwald                     char buffer[30];
808ecb7d461SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]);
809a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
810667ec068SMatthias Ringwald                 } else {
81160ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
812667ec068SMatthias Ringwald                 }
813667ec068SMatthias Ringwald                 return;
814667ec068SMatthias Ringwald             }
815667ec068SMatthias Ringwald         }
816667ec068SMatthias Ringwald     }
817667ec068SMatthias Ringwald 
818ce263fc8SMatthias Ringwald     if (done) return;
819ce263fc8SMatthias Ringwald     // deal with disconnect
820a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
821ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
822a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
823a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
824ce263fc8SMatthias Ringwald             break;
825ce263fc8SMatthias Ringwald 
826ce263fc8SMatthias Ringwald         default:
827ce263fc8SMatthias Ringwald             break;
828ce263fc8SMatthias Ringwald     }
829ce263fc8SMatthias Ringwald }
830ce263fc8SMatthias Ringwald 
831a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
832a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
8336a7f44bdSMilanka Ringwald 
834ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
8357522e673SMatthias Ringwald 
836667ec068SMatthias Ringwald     // restore volume settings
837a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
838a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
839ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
840a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
841a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
842ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
843667ec068SMatthias Ringwald     // enable all indicators
844667ec068SMatthias Ringwald     int i;
845667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
846a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
847a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
848667ec068SMatthias Ringwald     }
849ce263fc8SMatthias Ringwald }
850ce263fc8SMatthias Ringwald 
851a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
852a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
853a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
8543deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
855a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
856a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
8573deb3ec6SMatthias Ringwald                 break;
8583deb3ec6SMatthias Ringwald             }
859a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8603deb3ec6SMatthias Ringwald             break;
8613deb3ec6SMatthias Ringwald 
8623deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
863a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8643deb3ec6SMatthias Ringwald             break;
8653deb3ec6SMatthias Ringwald 
8663deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
867a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
8683deb3ec6SMatthias Ringwald             break;
8693deb3ec6SMatthias Ringwald 
8703deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
871a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
8723deb3ec6SMatthias Ringwald             break;
8733deb3ec6SMatthias Ringwald 
8743deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
875a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
876a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
8773deb3ec6SMatthias Ringwald                 break;
8783deb3ec6SMatthias Ringwald             }
879a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
880a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8813deb3ec6SMatthias Ringwald                 break;
8823deb3ec6SMatthias Ringwald             }
883a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8843deb3ec6SMatthias Ringwald             break;
8853deb3ec6SMatthias Ringwald 
8863deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
887a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
888a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8893deb3ec6SMatthias Ringwald                 break;
8903deb3ec6SMatthias Ringwald             }
891a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8923deb3ec6SMatthias Ringwald             break;
8933deb3ec6SMatthias Ringwald 
8943deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
895a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
8963deb3ec6SMatthias Ringwald             break;
8973deb3ec6SMatthias Ringwald 
8983deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
899a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9003deb3ec6SMatthias Ringwald             break;
9013deb3ec6SMatthias Ringwald 
9023deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
903a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9043deb3ec6SMatthias Ringwald             break;
905ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
906a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
907a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
908ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
909ce263fc8SMatthias Ringwald                 break;
910ce263fc8SMatthias Ringwald             }
9113deb3ec6SMatthias Ringwald 
912a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
913a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
914ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
915ce263fc8SMatthias Ringwald                 break;
9163deb3ec6SMatthias Ringwald             }
9173deb3ec6SMatthias Ringwald 
918a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
919ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
92060ebb071SMilanka Ringwald                     // printf("Format set, querying name\n");
921a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
922ce263fc8SMatthias Ringwald                     break;
923ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
924a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
925ca59be51SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator);
926ce263fc8SMatthias Ringwald                     break;
927ce263fc8SMatthias Ringwald                 default:
928ce263fc8SMatthias Ringwald                     break;
9293deb3ec6SMatthias Ringwald             }
930ce263fc8SMatthias Ringwald 
931a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
932a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
933ce263fc8SMatthias Ringwald                 break;
9343deb3ec6SMatthias Ringwald             }
9353deb3ec6SMatthias Ringwald 
936a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
937aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
938a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9393deb3ec6SMatthias Ringwald                     break;
940ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
941a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
942ce263fc8SMatthias Ringwald                     break;
9433deb3ec6SMatthias Ringwald                 default:
9443deb3ec6SMatthias Ringwald                     break;
9453deb3ec6SMatthias Ringwald             }
9463deb3ec6SMatthias Ringwald             break;
9473deb3ec6SMatthias Ringwald         default:
9483deb3ec6SMatthias Ringwald             break;
9493deb3ec6SMatthias Ringwald     }
9503deb3ec6SMatthias Ringwald 
9513deb3ec6SMatthias Ringwald     // done
952a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
9533deb3ec6SMatthias Ringwald }
9543deb3ec6SMatthias Ringwald 
9553deb3ec6SMatthias Ringwald 
956*e9c22d4eSMatthias Ringwald static void hfp_hf_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
9578a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
9588a46ec40SMatthias Ringwald 
9598a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
9608a46ec40SMatthias Ringwald     if (size < 1) return;
9619ec2630cSMatthias Ringwald 
962a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
963a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
9643deb3ec6SMatthias Ringwald 
9658a46ec40SMatthias Ringwald     // temp overwrite last byte (most likely \n for log_info)
9661e35c04dSMatthias Ringwald     char last_char = packet[size-1];
9671e35c04dSMatthias Ringwald     packet[size-1] = 0;
9681e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
9691e35c04dSMatthias Ringwald     packet[size-1] = last_char;
9701e35c04dSMatthias Ringwald 
9718a46ec40SMatthias Ringwald     // process messages byte-wise
972667ec068SMatthias Ringwald     int pos, i, value;
9733deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
974a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
975ce263fc8SMatthias Ringwald     }
9763deb3ec6SMatthias Ringwald 
977a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
978667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
979a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
980a0ffb263SMatthias Ringwald             // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type);
981ca59be51SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
982667ec068SMatthias Ringwald             break;
983667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
984a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
985a0ffb263SMatthias Ringwald             // printf("Response and Hold status: %s\n", hfp_connection->line_buffer);
986ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
987667ec068SMatthias Ringwald             break;
988667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
989a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
990a0ffb263SMatthias Ringwald             // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
991a0ffb263SMatthias Ringwald             //      hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
992a0ffb263SMatthias Ringwald             //      hfp_connection->bnip_number, hfp_connection->bnip_type);
993ca59be51SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection->clcc_idx,
994a0ffb263SMatthias Ringwald                 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
995a0ffb263SMatthias Ringwald                 hfp_connection->bnip_type, hfp_connection->bnip_number);
996667ec068SMatthias Ringwald             break;
997ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
998a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
9992308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1000667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1001ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1002ce263fc8SMatthias Ringwald             break;
1003ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1004a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10052308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1006667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1007ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1008ce263fc8SMatthias Ringwald             break;
1009ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1010a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1011ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1012a0ffb263SMatthias Ringwald             break;
1013a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1014a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1015ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1016a0ffb263SMatthias Ringwald             break;
1017a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1018a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1019ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1020ce263fc8SMatthias Ringwald             break;
1021ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1022a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1023a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1024a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1025ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1026ce263fc8SMatthias Ringwald             break;
1027ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1028a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1029a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1030a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1031ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1032ce263fc8SMatthias Ringwald             break;
1033ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1034a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1035ce263fc8SMatthias Ringwald             break;
1036ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
1037ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1038ce263fc8SMatthias Ringwald             break;
1039ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1040a0ffb263SMatthias Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1041a0ffb263SMatthias Ringwald                 if (hfp_connection->ag_indicators[i].status_changed) {
1042a0ffb263SMatthias Ringwald                     if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1043a0ffb263SMatthias Ringwald                         hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1044a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1045a0ffb263SMatthias Ringwald                         hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1046d5ff3b26SMatthias Ringwald                         // avoid set but not used warning
1047d5ff3b26SMatthias Ringwald                         (void) hfp_callheld_status;
1048a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1049a0ffb263SMatthias Ringwald                         hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1050ce263fc8SMatthias Ringwald                     }
1051a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[i].status_changed = 0;
1052ca59be51SMatthias Ringwald                     hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]);
10533deb3ec6SMatthias Ringwald                     break;
10543deb3ec6SMatthias Ringwald                 }
10553deb3ec6SMatthias Ringwald             }
1056ce263fc8SMatthias Ringwald             break;
1057ce263fc8SMatthias Ringwald         default:
1058ce263fc8SMatthias Ringwald             break;
10593deb3ec6SMatthias Ringwald     }
1060a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
10613deb3ec6SMatthias Ringwald }
10623deb3ec6SMatthias Ringwald 
10630cb5b971SMatthias Ringwald static void hfp_run(void){
1064665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1065665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1066665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1067a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1068a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
10693deb3ec6SMatthias Ringwald     }
10703deb3ec6SMatthias Ringwald }
10713deb3ec6SMatthias Ringwald 
1072*e9c22d4eSMatthias Ringwald static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1073*e9c22d4eSMatthias Ringwald     switch (packet_type){
1074*e9c22d4eSMatthias Ringwald         case HCI_EVENT_PACKET:
1075*e9c22d4eSMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1076*e9c22d4eSMatthias Ringwald             break;
1077*e9c22d4eSMatthias Ringwald         default:
1078*e9c22d4eSMatthias Ringwald             break;
1079*e9c22d4eSMatthias Ringwald     }
1080*e9c22d4eSMatthias Ringwald     hfp_run();
1081*e9c22d4eSMatthias Ringwald }
1082*e9c22d4eSMatthias Ringwald 
1083*e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10843deb3ec6SMatthias Ringwald     switch (packet_type){
10853deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1086*e9c22d4eSMatthias Ringwald             hfp_hf_handle_rfcomm_event(packet_type, channel, packet, size);
10873deb3ec6SMatthias Ringwald             break;
10883deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1089323d3000SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1090202c8a4cSMatthias Ringwald             break;
10913deb3ec6SMatthias Ringwald         default:
10923deb3ec6SMatthias Ringwald             break;
10933deb3ec6SMatthias Ringwald     }
10943deb3ec6SMatthias Ringwald     hfp_run();
10953deb3ec6SMatthias Ringwald }
10963deb3ec6SMatthias Ringwald 
1097a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1098d63c37a1SMatthias Ringwald     // register for HCI events
1099*e9c22d4eSMatthias Ringwald     hci_event_callback_registration.callback = &hci_packet_handler;
1100d63c37a1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
1101d63c37a1SMatthias Ringwald 
1102*e9c22d4eSMatthias Ringwald     rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1103*e9c22d4eSMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&rfcomm_packet_handler);
1104d68dcce1SMatthias Ringwald 
1105a0ffb263SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1106a0ffb263SMatthias Ringwald     hfp_codecs_nr = 0;
1107a0ffb263SMatthias Ringwald     hfp_indicators_nr = 0;
1108a0ffb263SMatthias Ringwald     hfp_hf_speaker_gain = 9;
1109a0ffb263SMatthias Ringwald     hfp_hf_microphone_gain = 9;
1110a0ffb263SMatthias Ringwald }
1111a0ffb263SMatthias Ringwald 
1112a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
11133deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1114a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
11153deb3ec6SMatthias Ringwald         return;
11163deb3ec6SMatthias Ringwald     }
11173deb3ec6SMatthias Ringwald 
11183deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
11193deb3ec6SMatthias Ringwald     int i;
11203deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
11213deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
11223deb3ec6SMatthias Ringwald     }
11233deb3ec6SMatthias Ringwald }
11243deb3ec6SMatthias Ringwald 
1125a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
11263deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1127a0ffb263SMatthias Ringwald }
11283deb3ec6SMatthias Ringwald 
1129a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
11303deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
11313deb3ec6SMatthias Ringwald     int i;
1132a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
11333deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
11343deb3ec6SMatthias Ringwald     }
11353deb3ec6SMatthias Ringwald }
11363deb3ec6SMatthias Ringwald 
11373deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1138323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
11393deb3ec6SMatthias Ringwald }
11403deb3ec6SMatthias Ringwald 
1141c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
1142c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1143a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1144a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1145a33eb0c4SMilanka Ringwald         return;
1146a33eb0c4SMilanka Ringwald     }
1147a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
1148a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11493deb3ec6SMatthias Ringwald }
11503deb3ec6SMatthias Ringwald 
1151c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
1152c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1153a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1154a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11553deb3ec6SMatthias Ringwald         return;
11563deb3ec6SMatthias Ringwald     }
1157a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
1158a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11593deb3ec6SMatthias Ringwald }
11603deb3ec6SMatthias Ringwald 
1161c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1162c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1163ce263fc8SMatthias Ringwald }
1164ce263fc8SMatthias Ringwald 
1165c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1166c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1167ce263fc8SMatthias Ringwald }
1168ce263fc8SMatthias Ringwald 
11693deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1170c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
1171c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1172a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1173a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11743deb3ec6SMatthias Ringwald         return;
11753deb3ec6SMatthias Ringwald     }
1176a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1177a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1178a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11793deb3ec6SMatthias Ringwald }
11803deb3ec6SMatthias Ringwald 
1181c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
1182c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1183a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1184a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11853deb3ec6SMatthias Ringwald         return;
11863deb3ec6SMatthias Ringwald     }
1187a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1188ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1189a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1190ce263fc8SMatthias Ringwald             break;
1191ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1192a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1193ce263fc8SMatthias Ringwald             break;
1194ce263fc8SMatthias Ringwald         default:
1195ce263fc8SMatthias Ringwald             break;
1196ce263fc8SMatthias Ringwald     }
1197a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11983deb3ec6SMatthias Ringwald }
11993deb3ec6SMatthias Ringwald 
1200c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
1201c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1202a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1203a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12043deb3ec6SMatthias Ringwald         return;
12053deb3ec6SMatthias Ringwald     }
1206a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1207a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12083deb3ec6SMatthias Ringwald }
12093deb3ec6SMatthias Ringwald 
1210ce263fc8SMatthias Ringwald 
1211c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1212c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1213ce263fc8SMatthias Ringwald }
1214ce263fc8SMatthias Ringwald 
1215c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1216c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1217ce263fc8SMatthias Ringwald }
1218ce263fc8SMatthias Ringwald 
1219ce263fc8SMatthias Ringwald 
1220c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
1221c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1222a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1223a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1224a33eb0c4SMilanka Ringwald         return;
1225a33eb0c4SMilanka Ringwald     }
1226a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1227ce263fc8SMatthias Ringwald 
1228a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1229a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
12303deb3ec6SMatthias Ringwald 
1231a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1232ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1233a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1234a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1235ce263fc8SMatthias Ringwald     } else {
1236a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1237aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1238aa4dd815SMatthias Ringwald                 break;
1239aa4dd815SMatthias Ringwald             default:
1240a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1241aa4dd815SMatthias Ringwald                 break;
12423deb3ec6SMatthias Ringwald         }
1243ce263fc8SMatthias Ringwald     }
1244ce263fc8SMatthias Ringwald 
1245a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12463deb3ec6SMatthias Ringwald }
12473deb3ec6SMatthias Ringwald 
1248c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
1249c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1250a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1251a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1252a33eb0c4SMilanka Ringwald         return;
1253a33eb0c4SMilanka Ringwald     }
1254a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
1255a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12563deb3ec6SMatthias Ringwald }
12573deb3ec6SMatthias Ringwald 
1258c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
1259c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1260a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1261a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1262a33eb0c4SMilanka Ringwald         return;
1263a33eb0c4SMilanka Ringwald     }
1264ce263fc8SMatthias Ringwald 
1265ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1266a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
1267a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1268ce263fc8SMatthias Ringwald     } else {
1269ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1270ce263fc8SMatthias Ringwald     }
1271ce263fc8SMatthias Ringwald }
1272ce263fc8SMatthias Ringwald 
1273c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
1274c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1275a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1276a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1277a33eb0c4SMilanka Ringwald         return;
1278a33eb0c4SMilanka Ringwald     }
1279a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
1280a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1281ce263fc8SMatthias Ringwald }
1282ce263fc8SMatthias Ringwald 
1283c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
1284c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1285a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1286a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1287a33eb0c4SMilanka Ringwald         return;
1288a33eb0c4SMilanka Ringwald     }
1289ce263fc8SMatthias Ringwald 
1290ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1291a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
1292a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1293ce263fc8SMatthias Ringwald     }
1294ce263fc8SMatthias Ringwald }
1295ce263fc8SMatthias Ringwald 
1296c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){
1297c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1298a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1299a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1300a33eb0c4SMilanka Ringwald         return;
1301a33eb0c4SMilanka Ringwald     }
1302ce263fc8SMatthias Ringwald 
1303ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1304a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
1305a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1306ce263fc8SMatthias Ringwald     }
1307ce263fc8SMatthias Ringwald }
1308ce263fc8SMatthias Ringwald 
1309c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
1310c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1311a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1312a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1313a33eb0c4SMilanka Ringwald         return;
1314a33eb0c4SMilanka Ringwald     }
1315ce263fc8SMatthias Ringwald 
1316ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1317ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1318a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
1319a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1320ce263fc8SMatthias Ringwald     }
1321ce263fc8SMatthias Ringwald }
1322ce263fc8SMatthias Ringwald 
1323c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){
1324c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1325a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1326a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1327a33eb0c4SMilanka Ringwald         return;
1328a33eb0c4SMilanka Ringwald     }
1329ce263fc8SMatthias Ringwald 
1330ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1331ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1332a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
1333a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1334ce263fc8SMatthias Ringwald     }
1335ce263fc8SMatthias Ringwald }
1336ce263fc8SMatthias Ringwald 
1337c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
1338c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1339a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1340a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1341a33eb0c4SMilanka Ringwald         return;
1342a33eb0c4SMilanka Ringwald     }
1343ce263fc8SMatthias Ringwald 
1344ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1345ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1346a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
1347a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1348ce263fc8SMatthias Ringwald     }
1349ce263fc8SMatthias Ringwald }
1350ce263fc8SMatthias Ringwald 
1351c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
1352c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1353a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1354a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1355a33eb0c4SMilanka Ringwald         return;
1356a33eb0c4SMilanka Ringwald     }
1357ce263fc8SMatthias Ringwald 
1358ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1359ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1360a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
1361a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1362ce263fc8SMatthias Ringwald     }
1363ce263fc8SMatthias Ringwald }
1364ce263fc8SMatthias Ringwald 
1365c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
1366c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1367a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1368a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1369a33eb0c4SMilanka Ringwald         return;
1370a33eb0c4SMilanka Ringwald     }
1371667ec068SMatthias Ringwald 
1372667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1373667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1374a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1375a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
1376a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1377667ec068SMatthias Ringwald     }
1378667ec068SMatthias Ringwald }
1379667ec068SMatthias Ringwald 
1380c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
1381c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1382a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1383a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1384a33eb0c4SMilanka Ringwald         return;
1385a33eb0c4SMilanka Ringwald     }
1386667ec068SMatthias Ringwald 
1387667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1388667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1389a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1390a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
1391a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1392667ec068SMatthias Ringwald     }
1393667ec068SMatthias Ringwald }
1394ce263fc8SMatthias Ringwald 
1395c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
1396c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1397a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1398a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1399a33eb0c4SMilanka Ringwald         return;
1400a33eb0c4SMilanka Ringwald     }
1401ce263fc8SMatthias Ringwald 
1402a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1403ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1404a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1405ce263fc8SMatthias Ringwald }
1406ce263fc8SMatthias Ringwald 
1407c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
1408c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1409a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1410a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1411a33eb0c4SMilanka Ringwald         return;
1412a33eb0c4SMilanka Ringwald     }
1413ce263fc8SMatthias Ringwald 
1414a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1415a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1416a0ffb263SMatthias Ringwald 
1417a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1418ce263fc8SMatthias Ringwald }
1419ce263fc8SMatthias Ringwald 
1420c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
1421c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1422a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1423a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1424a33eb0c4SMilanka Ringwald         return;
1425a33eb0c4SMilanka Ringwald     }
1426ce263fc8SMatthias Ringwald 
1427a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
1428a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1429ce263fc8SMatthias Ringwald }
1430ce263fc8SMatthias Ringwald 
1431c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
1432c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1433a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1434a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1435a33eb0c4SMilanka Ringwald         return;
1436a33eb0c4SMilanka Ringwald     }
1437ce263fc8SMatthias Ringwald 
1438a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
1439a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1440ce263fc8SMatthias Ringwald }
1441ce263fc8SMatthias Ringwald 
1442ce263fc8SMatthias Ringwald 
1443c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
1444c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1445a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1446a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1447a33eb0c4SMilanka Ringwald         return;
1448a33eb0c4SMilanka Ringwald     }
1449ce263fc8SMatthias Ringwald 
1450a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1451a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1452ce263fc8SMatthias Ringwald }
1453ce263fc8SMatthias Ringwald 
1454ce263fc8SMatthias Ringwald 
1455c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
1456c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1457a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1458a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1459a33eb0c4SMilanka Ringwald         return;
1460a33eb0c4SMilanka Ringwald     }
1461ce263fc8SMatthias Ringwald 
1462a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
1463a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1464ce263fc8SMatthias Ringwald }
1465ce263fc8SMatthias Ringwald 
1466c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
1467c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1468a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1469a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1470a33eb0c4SMilanka Ringwald         return;
1471a33eb0c4SMilanka Ringwald     }
1472ce263fc8SMatthias Ringwald 
1473a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
1474a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1475ce263fc8SMatthias Ringwald }
1476ce263fc8SMatthias Ringwald 
1477ce263fc8SMatthias Ringwald 
1478c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1479c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1480a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1481a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1482a33eb0c4SMilanka Ringwald         return;
1483a33eb0c4SMilanka Ringwald     }
1484ce263fc8SMatthias Ringwald 
1485a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1486a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1487ce263fc8SMatthias Ringwald }
1488ce263fc8SMatthias Ringwald 
1489c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1490c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1491a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1492a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1493a33eb0c4SMilanka Ringwald         return;
1494a33eb0c4SMilanka Ringwald     }
1495ce263fc8SMatthias Ringwald 
1496a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1497a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1498ce263fc8SMatthias Ringwald }
1499ce263fc8SMatthias Ringwald 
1500c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
1501c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1502a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1503a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1504a33eb0c4SMilanka Ringwald         return;
1505a33eb0c4SMilanka Ringwald     }
1506ce263fc8SMatthias Ringwald 
1507a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
1508a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1509ce263fc8SMatthias Ringwald }
1510ce263fc8SMatthias Ringwald 
1511c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
1512c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1513a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1514a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1515a33eb0c4SMilanka Ringwald         return;
1516a33eb0c4SMilanka Ringwald     }
1517ce263fc8SMatthias Ringwald 
1518a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1519a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1520ce263fc8SMatthias Ringwald }
1521ce263fc8SMatthias Ringwald 
1522c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
1523c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1524a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1525a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1526a33eb0c4SMilanka Ringwald         return;
1527a33eb0c4SMilanka Ringwald     }
1528c8626498SMilanka Ringwald 
1529a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1530a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1531a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1532a0ffb263SMatthias Ringwald         return;
1533a0ffb263SMatthias Ringwald     }
1534a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1535a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1536a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1537ce263fc8SMatthias Ringwald }
1538ce263fc8SMatthias Ringwald 
1539c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
1540c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1541a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1542a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1543a33eb0c4SMilanka Ringwald         return;
1544a33eb0c4SMilanka Ringwald     }
1545c8626498SMilanka Ringwald 
1546a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1547a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1548a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1549a0ffb263SMatthias Ringwald         return;
1550a0ffb263SMatthias Ringwald     }
1551a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1552a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1553a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1554ce263fc8SMatthias Ringwald }
1555ce263fc8SMatthias Ringwald 
1556c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
1557c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1558a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1559a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1560a33eb0c4SMilanka Ringwald         return;
1561a33eb0c4SMilanka Ringwald     }
1562a33eb0c4SMilanka Ringwald 
1563a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
1564a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1565ce263fc8SMatthias Ringwald }
1566ce263fc8SMatthias Ringwald 
1567c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
1568c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1569a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1570a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1571a33eb0c4SMilanka Ringwald         return;
1572a33eb0c4SMilanka Ringwald     }
1573a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
1574a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1575ce263fc8SMatthias Ringwald }
15763deb3ec6SMatthias Ringwald 
1577c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
1578c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1579a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1580a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1581a33eb0c4SMilanka Ringwald         return;
1582a33eb0c4SMilanka Ringwald     }
1583a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
1584a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1585667ec068SMatthias Ringwald }
1586667ec068SMatthias Ringwald 
1587667ec068SMatthias Ringwald 
1588c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
1589c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1590a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1591a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1592a33eb0c4SMilanka Ringwald         return;
1593a33eb0c4SMilanka Ringwald     }
1594a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1595a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
1596a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1597667ec068SMatthias Ringwald }
1598667ec068SMatthias Ringwald 
1599c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
1600c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1601a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1602a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1603a33eb0c4SMilanka Ringwald         return;
1604a33eb0c4SMilanka Ringwald     }
1605a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1606a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
1607a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1608667ec068SMatthias Ringwald }
1609667ec068SMatthias Ringwald 
1610c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
1611c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1612a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1613a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1614a33eb0c4SMilanka Ringwald         return;
1615a33eb0c4SMilanka Ringwald     }
1616a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1617a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
1618a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1619667ec068SMatthias Ringwald }
1620667ec068SMatthias Ringwald 
1621c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
1622c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1623a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1624a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1625a33eb0c4SMilanka Ringwald         return;
1626a33eb0c4SMilanka Ringwald     }
1627a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1628a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
1629a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1630667ec068SMatthias Ringwald }
1631667ec068SMatthias Ringwald 
1632c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
1633c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1634a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1635a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1636a33eb0c4SMilanka Ringwald         return;
1637a33eb0c4SMilanka Ringwald     }
1638a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
1639a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1640667ec068SMatthias Ringwald }
1641667ec068SMatthias Ringwald 
1642c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
1643c8626498SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1644a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1645a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1646a33eb0c4SMilanka Ringwald         return;
1647a33eb0c4SMilanka Ringwald     }
1648667ec068SMatthias Ringwald     // find index for assigned number
1649667ec068SMatthias Ringwald     int i;
1650667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1651667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1652667ec068SMatthias Ringwald             // set value
1653667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1654667ec068SMatthias Ringwald             // mark for update
1655a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1656a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1657667ec068SMatthias Ringwald                 // send update
1658a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1659a0ffb263SMatthias Ringwald             }
1660667ec068SMatthias Ringwald             return;
1661667ec068SMatthias Ringwald         }
1662667ec068SMatthias Ringwald     }
1663667ec068SMatthias Ringwald }
1664667ec068SMatthias Ringwald 
1665