xref: /btstack/src/classic/hfp_hf.c (revision 689d4323e4f864e100adb6c55186bad11954d6c9)
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 
38e501bae0SMatthias 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 <string.h>
503deb3ec6SMatthias Ringwald 
51235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
5259c6af15SMatthias Ringwald #include "btstack_debug.h"
53d4dd47ffSMatthias Ringwald #include "btstack_event.h"
543deb3ec6SMatthias Ringwald #include "btstack_memory.h"
5559c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5659c6af15SMatthias Ringwald #include "classic/core.h"
5759c6af15SMatthias Ringwald #include "classic/hfp.h"
5859c6af15SMatthias Ringwald #include "classic/hfp_hf.h"
59efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
60746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
61023f2764SMatthias Ringwald #include "classic/sdp_util.h"
6259c6af15SMatthias Ringwald #include "hci.h"
6359c6af15SMatthias Ringwald #include "hci_cmd.h"
6459c6af15SMatthias Ringwald #include "hci_dump.h"
6559c6af15SMatthias Ringwald #include "l2cap.h"
663deb3ec6SMatthias Ringwald 
671c6a0fc0SMatthias Ringwald static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration;
6827950165SMatthias Ringwald 
693deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
703deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
713deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
723deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
733deb3ec6SMatthias Ringwald 
743deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0;
7525789943SMilanka Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_INDICATORS];
7625789943SMilanka Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_INDICATORS];
77667ec068SMatthias Ringwald 
78667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9;
79667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9;
803deb3ec6SMatthias Ringwald 
81ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
823deb3ec6SMatthias Ringwald 
83ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
84ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
85ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
86ce263fc8SMatthias Ringwald 
87ce263fc8SMatthias Ringwald static char phone_number[25];
88ce263fc8SMatthias Ringwald 
8976cc1527SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
9076cc1527SMatthias Ringwald 	int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
9176cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
9276cc1527SMatthias Ringwald 	return hf && ag;
9376cc1527SMatthias Ringwald }
9476cc1527SMatthias Ringwald 
9576cc1527SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
9676cc1527SMatthias Ringwald 	int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
9776cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
9876cc1527SMatthias Ringwald 	return hf && ag;
9976cc1527SMatthias Ringwald }
10076cc1527SMatthias Ringwald 
10176cc1527SMatthias Ringwald 
10276cc1527SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
10376cc1527SMatthias Ringwald 	int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
10476cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
10576cc1527SMatthias Ringwald 	return hf && ag;
10676cc1527SMatthias Ringwald }
10776cc1527SMatthias Ringwald 
10876cc1527SMatthias Ringwald 
1099c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
1109c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1119c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1129c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1139c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1149c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1159c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1169c9c64c1SMatthias Ringwald         return hfp_connection;
1179c9c64c1SMatthias Ringwald     }
1189c9c64c1SMatthias Ringwald     return NULL;
1199c9c64c1SMatthias Ringwald }
1209c9c64c1SMatthias Ringwald 
12176cc1527SMatthias Ringwald /* emit functinos */
1223deb3ec6SMatthias Ringwald 
12313839019SMatthias 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){
124a0ffb263SMatthias Ringwald     if (!callback) return;
125a0ffb263SMatthias Ringwald     uint8_t event[31];
126a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
127a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
128a0ffb263SMatthias Ringwald     event[2] = event_subtype;
129a0ffb263SMatthias Ringwald     event[3] = status;
130a0ffb263SMatthias Ringwald     event[4] = bnip_type;
1316d9a4cd0SMilanka Ringwald     uint16_t size = btstack_min(strlen(bnip_number), sizeof(event) - 6);
132a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], bnip_number, size);
133a0ffb263SMatthias Ringwald     event[5 + size] = 0;
13413839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
135a0ffb263SMatthias Ringwald }
136a0ffb263SMatthias Ringwald 
13713839019SMatthias 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){
138a0ffb263SMatthias Ringwald     if (!callback) return;
139a0ffb263SMatthias Ringwald     uint8_t event[30];
140a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
141a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
142a0ffb263SMatthias Ringwald     event[2] = event_subtype;
143a0ffb263SMatthias Ringwald     event[3] = bnip_type;
1446d9a4cd0SMilanka Ringwald     uint16_t size = btstack_min(strlen(bnip_number), sizeof(event) - 5);
145a0ffb263SMatthias Ringwald     strncpy((char*)&event[4], bnip_number, size);
146a0ffb263SMatthias Ringwald     event[4 + size] = 0;
14713839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
148a0ffb263SMatthias Ringwald }
149a0ffb263SMatthias Ringwald 
1502b99f6edSMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, hfp_connection_t * connection){
151a0ffb263SMatthias Ringwald     if (!callback) return;
1520aee97efSMilanka Ringwald     uint8_t event[36];
1530aee97efSMilanka Ringwald     int pos = 0;
1540aee97efSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
1550aee97efSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
1560aee97efSMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
1572b99f6edSMatthias Ringwald     event[pos++] = connection->clcc_idx;
1582b99f6edSMatthias Ringwald     event[pos++] = connection->clcc_dir;
1592b99f6edSMatthias Ringwald     event[pos++] = connection->clcc_status;
1602b99f6edSMatthias Ringwald     event[pos++] = connection->clcc_mode;
1612b99f6edSMatthias Ringwald     event[pos++] = connection->clcc_mpty;
1622b99f6edSMatthias Ringwald     event[pos++] = connection->bnip_type;
1632b99f6edSMatthias Ringwald     uint16_t size = btstack_min(strlen(connection->bnip_number), sizeof(event) - pos);
1642b99f6edSMatthias Ringwald     strncpy((char*)&event[pos], connection->bnip_number, size);
1650aee97efSMilanka Ringwald     pos += size;
1660aee97efSMilanka Ringwald     event[pos++] = 0;
1670aee97efSMilanka Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, pos);
168a0ffb263SMatthias Ringwald }
169a0ffb263SMatthias Ringwald 
17076cc1527SMatthias Ringwald 
17176cc1527SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){
17276cc1527SMatthias Ringwald 	if (!callback) return;
17376cc1527SMatthias Ringwald 	uint8_t event[10+HFP_MAX_INDICATOR_DESC_SIZE+1];
17476cc1527SMatthias Ringwald 	int pos = 0;
17576cc1527SMatthias Ringwald 	event[pos++] = HCI_EVENT_HFP_META;
17676cc1527SMatthias Ringwald 	event[pos++] = sizeof(event) - 2;
17776cc1527SMatthias Ringwald 	event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
17876cc1527SMatthias Ringwald 	event[pos++] = indicator.index;
17976cc1527SMatthias Ringwald 	event[pos++] = indicator.status;
18076cc1527SMatthias Ringwald 	event[pos++] = indicator.min_range;
18176cc1527SMatthias Ringwald 	event[pos++] = indicator.max_range;
18276cc1527SMatthias Ringwald 	event[pos++] = indicator.mandatory;
18376cc1527SMatthias Ringwald 	event[pos++] = indicator.enabled;
18476cc1527SMatthias Ringwald 	event[pos++] = indicator.status_changed;
18576cc1527SMatthias Ringwald 	strncpy((char*)&event[pos], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
18676cc1527SMatthias Ringwald 	pos += HFP_MAX_INDICATOR_DESC_SIZE;
18776cc1527SMatthias Ringwald 	event[pos] = 0;
18876cc1527SMatthias Ringwald 	(*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1893deb3ec6SMatthias Ringwald }
1903deb3ec6SMatthias Ringwald 
19176cc1527SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){
19276cc1527SMatthias Ringwald 	if (!callback) return;
19376cc1527SMatthias Ringwald 	uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
19476cc1527SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
19576cc1527SMatthias Ringwald 	event[1] = sizeof(event) - 2;
19676cc1527SMatthias Ringwald 	event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
19776cc1527SMatthias Ringwald 	event[3] = network_operator.mode;
19876cc1527SMatthias Ringwald 	event[4] = network_operator.format;
19976cc1527SMatthias Ringwald 	strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
20076cc1527SMatthias Ringwald 	event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
20176cc1527SMatthias Ringwald 	(*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2023deb3ec6SMatthias Ringwald }
2033deb3ec6SMatthias Ringwald 
20476cc1527SMatthias Ringwald /* send commands */
20589425bfcSMilanka Ringwald 
20689425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2073deb3ec6SMatthias Ringwald     char buffer[20];
20889425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r\n", cmd);
20989425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
21089425bfcSMilanka Ringwald }
21189425bfcSMilanka Ringwald 
21289425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
21389425bfcSMilanka Ringwald     char buffer[20];
21489425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r\n", cmd, mark);
21589425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
21689425bfcSMilanka Ringwald }
21789425bfcSMilanka Ringwald 
21886da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
21989425bfcSMilanka Ringwald     char buffer[40];
22089425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r\n", cmd, value);
2213deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2223deb3ec6SMatthias Ringwald }
2233deb3ec6SMatthias Ringwald 
2243deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2253deb3ec6SMatthias Ringwald     char buffer[30];
22689425bfcSMilanka Ringwald     const int size = sizeof(buffer);
22789425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
22889425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
22989425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2303deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2313deb3ec6SMatthias Ringwald }
2323deb3ec6SMatthias Ringwald 
2333deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2343deb3ec6SMatthias Ringwald     char buffer[50];
23589425bfcSMilanka Ringwald     const int size = sizeof(buffer);
23689425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
23789425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
23889425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2393deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2403deb3ec6SMatthias Ringwald }
2413deb3ec6SMatthias Ringwald 
2423deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2433deb3ec6SMatthias Ringwald     char buffer[30];
24489425bfcSMilanka Ringwald     const int size = sizeof(buffer);
24589425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
24689425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
24789425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2483deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2493deb3ec6SMatthias Ringwald }
2503deb3ec6SMatthias Ringwald 
25189425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2523deb3ec6SMatthias Ringwald     char buffer[20];
25389425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
254ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
255ce263fc8SMatthias Ringwald }
256ce263fc8SMatthias Ringwald 
257ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
258ce263fc8SMatthias Ringwald     char buffer[40];
25989425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
260ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
261ce263fc8SMatthias Ringwald }
262ce263fc8SMatthias Ringwald 
263a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
264ce263fc8SMatthias Ringwald     char buffer[40];
26589425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id);
266ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
267ce263fc8SMatthias Ringwald }
268ce263fc8SMatthias Ringwald 
269f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
27089425bfcSMilanka Ringwald     char buffer[40];
27189425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
272ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
273ce263fc8SMatthias Ringwald }
274ce263fc8SMatthias Ringwald 
275ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
276ce263fc8SMatthias Ringwald     char buffer[20];
27789425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
278ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
279ce263fc8SMatthias Ringwald }
280ce263fc8SMatthias Ringwald 
28197d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
282560b7bdbSMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "ATA\r\n");
28397d2cadbSMatthias Ringwald }
28497d2cadbSMatthias Ringwald 
28589425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
28689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
28789425bfcSMilanka Ringwald }
28889425bfcSMilanka Ringwald 
28989425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
29089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
29189425bfcSMilanka Ringwald }
29289425bfcSMilanka Ringwald 
29389425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
29489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
29589425bfcSMilanka Ringwald }
29689425bfcSMilanka Ringwald 
29789425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
29889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
29989425bfcSMilanka Ringwald }
30089425bfcSMilanka Ringwald 
30189425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
30289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
30389425bfcSMilanka Ringwald }
30489425bfcSMilanka Ringwald 
30589425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
30689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
30789425bfcSMilanka Ringwald }
30889425bfcSMilanka Ringwald 
30989425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
31089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
31189425bfcSMilanka Ringwald }
31289425bfcSMilanka Ringwald 
31389425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
31489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
31589425bfcSMilanka Ringwald }
31689425bfcSMilanka Ringwald 
31789425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
31889425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
31989425bfcSMilanka Ringwald }
32089425bfcSMilanka Ringwald 
32189425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
32289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
32389425bfcSMilanka Ringwald }
32489425bfcSMilanka Ringwald 
32589425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
32689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
32789425bfcSMilanka Ringwald }
32889425bfcSMilanka Ringwald 
32989425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
33089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
33189425bfcSMilanka Ringwald }
33289425bfcSMilanka Ringwald 
33389425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
33489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
33589425bfcSMilanka Ringwald }
33689425bfcSMilanka Ringwald 
33789425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
33889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
33989425bfcSMilanka Ringwald }
34089425bfcSMilanka Ringwald 
34189425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
34289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
34389425bfcSMilanka Ringwald }
34489425bfcSMilanka Ringwald 
34589425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
34689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
34789425bfcSMilanka Ringwald }
34889425bfcSMilanka Ringwald 
34989425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
35089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
35189425bfcSMilanka Ringwald }
35289425bfcSMilanka Ringwald 
35389425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
35489425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
35589425bfcSMilanka Ringwald }
35689425bfcSMilanka Ringwald 
35789425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
35889425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
35989425bfcSMilanka Ringwald }
36089425bfcSMilanka Ringwald 
361ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
36289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
363ce263fc8SMatthias Ringwald }
364ce263fc8SMatthias Ringwald 
365667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
36689425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
367667ec068SMatthias Ringwald }
368667ec068SMatthias Ringwald 
36976cc1527SMatthias Ringwald /* state machines */
3703deb3ec6SMatthias Ringwald 
371a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
372a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
373a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
374aa4dd815SMatthias Ringwald     int done = 1;
3753deb3ec6SMatthias Ringwald 
376a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
3773deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
378d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
379a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
380a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
3813deb3ec6SMatthias Ringwald             break;
3823deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
383a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
384a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
3853deb3ec6SMatthias Ringwald             break;
3863deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
387a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
388a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
3893deb3ec6SMatthias Ringwald             break;
3903deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
391a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
392a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
3933deb3ec6SMatthias Ringwald             break;
3943deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
395a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
396a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
3973deb3ec6SMatthias Ringwald             break;
3983deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
399a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
400a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4013deb3ec6SMatthias Ringwald             break;
4023deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
403a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
404a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4053deb3ec6SMatthias Ringwald             break;
4063deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
407a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
408a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4093deb3ec6SMatthias Ringwald             break;
4103deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
411a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
412a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4133deb3ec6SMatthias Ringwald             break;
4143deb3ec6SMatthias Ringwald         default:
415aa4dd815SMatthias Ringwald             done = 0;
4163deb3ec6SMatthias Ringwald             break;
4173deb3ec6SMatthias Ringwald     }
4183deb3ec6SMatthias Ringwald     return done;
4193deb3ec6SMatthias Ringwald }
4203deb3ec6SMatthias Ringwald 
421ce263fc8SMatthias Ringwald 
422a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
423a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
424a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
425ce263fc8SMatthias Ringwald 
426ce263fc8SMatthias Ringwald     int done = 0;
427a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
428a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
429ce263fc8SMatthias Ringwald         done = 1;
430a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
431ce263fc8SMatthias Ringwald         return done;
432ce263fc8SMatthias Ringwald     };
433a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
434a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
435ce263fc8SMatthias Ringwald         done = 1;
436a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
437a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
438a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
439ce263fc8SMatthias Ringwald         return done;
440ce263fc8SMatthias Ringwald     }
441ce263fc8SMatthias Ringwald 
442a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
443ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
444a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
445a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
446a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
447ce263fc8SMatthias Ringwald             return 1;
448ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
449a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
450a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
451a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
452ce263fc8SMatthias Ringwald             return 1;
453ce263fc8SMatthias Ringwald         default:
454ce263fc8SMatthias Ringwald             break;
455ce263fc8SMatthias Ringwald     }
456ce263fc8SMatthias Ringwald 
457a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
458a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
459ce263fc8SMatthias Ringwald         done = 1;
460a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
461ce263fc8SMatthias Ringwald         return done;
462ce263fc8SMatthias Ringwald     }
463ce263fc8SMatthias Ringwald 
464ce263fc8SMatthias Ringwald     return done;
465ce263fc8SMatthias Ringwald }
466ce263fc8SMatthias Ringwald 
467a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
468ce263fc8SMatthias Ringwald 
469a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
470ce263fc8SMatthias Ringwald 
471332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
472332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
473ce263fc8SMatthias Ringwald 
474a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
475a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
476332ca98fSMatthias Ringwald 		return 1;
477332ca98fSMatthias Ringwald     }
478332ca98fSMatthias Ringwald 
4791cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
4801cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
481ce263fc8SMatthias Ringwald 
482a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
483fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
4841cc65c4fSMatthias Ringwald 		return 1;
4851cc65c4fSMatthias Ringwald     }
4861cc65c4fSMatthias Ringwald 
4871cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
4881cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
4891cc65c4fSMatthias Ringwald 
490a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
491a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4921cc65c4fSMatthias Ringwald 		return 1;
4931cc65c4fSMatthias Ringwald     }
494ce263fc8SMatthias Ringwald 
495ce263fc8SMatthias Ringwald     return 0;
496ce263fc8SMatthias Ringwald }
497ce263fc8SMatthias Ringwald 
498a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
499505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
500505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
501ce263fc8SMatthias Ringwald 
50264f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
503a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
504a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
505a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
506ce263fc8SMatthias Ringwald         return 1;
507ce263fc8SMatthias Ringwald     }
508ce263fc8SMatthias Ringwald 
509a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
510ce263fc8SMatthias Ringwald 
511ce263fc8SMatthias Ringwald     // run codecs exchange
512a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
513ce263fc8SMatthias Ringwald     if (done) return 1;
514ce263fc8SMatthias Ringwald 
51538200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
51638200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
51738200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
51838200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
51938200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
52038200c1dSMilanka Ringwald         return 1;
52138200c1dSMilanka Ringwald     }
52238200c1dSMilanka Ringwald 
523ce263fc8SMatthias Ringwald     return 0;
524ce263fc8SMatthias Ringwald }
525ce263fc8SMatthias Ringwald 
52638200c1dSMilanka Ringwald 
527a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
528eaf2b0a1SMatthias Ringwald 
529eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
530eaf2b0a1SMatthias Ringwald 
531a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
532a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
533a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
534ce263fc8SMatthias Ringwald         return 1;
535ce263fc8SMatthias Ringwald     }
536ce263fc8SMatthias Ringwald     return 0;
537ce263fc8SMatthias Ringwald }
538ce263fc8SMatthias Ringwald 
5391c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
5407522e673SMatthias Ringwald 
54176cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
54276cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
54376cc1527SMatthias Ringwald 
54476cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
54576cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
54622387625SMatthias Ringwald 
5473721a235SMatthias Ringwald 	// assert command could be sent
5483721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
5493721a235SMatthias Ringwald 
5503721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
5513721a235SMatthias Ringwald     // WBS Disassociate
5523721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
5533721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
5543721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
5553721a235SMatthias Ringwald         return;
5563721a235SMatthias Ringwald     }
5573721a235SMatthias Ringwald     // Write Codec Config
5583721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
5593721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
5603721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
5613721a235SMatthias Ringwald         return;
5623721a235SMatthias Ringwald     }
5633721a235SMatthias Ringwald     // WBS Associate
5643721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
5653721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
5663721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
5673721a235SMatthias Ringwald         return;
5683721a235SMatthias Ringwald     }
5693721a235SMatthias Ringwald #endif
570*689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
571*689d4323SMatthias Ringwald     // Enable WBS
572*689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
573*689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
574*689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
575*689d4323SMatthias Ringwald         return;
576*689d4323SMatthias Ringwald     }
577*689d4323SMatthias Ringwald     // Write I2S/PCM params
578*689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
579*689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
580*689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
581*689d4323SMatthias Ringwald         return;
582*689d4323SMatthias Ringwald     }
583*689d4323SMatthias Ringwald     // Disable WBS
584*689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
585*689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
586*689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
587*689d4323SMatthias Ringwald         return;
588*689d4323SMatthias Ringwald     }
589*689d4323SMatthias Ringwald #endif
5903721a235SMatthias Ringwald 
5913721a235SMatthias Ringwald     if (hfp_connection->hf_accept_sco){
592b72c4a9eSMatthias Ringwald 
593c169df2fSMatthias Ringwald         bool eSCO = hfp_connection->hf_accept_sco == 2;
594b72c4a9eSMatthias Ringwald         hfp_connection->hf_accept_sco = 0;
5957522e673SMatthias Ringwald 
5967522e673SMatthias Ringwald         // notify about codec selection if not done already
5977522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
5987522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
5997522e673SMatthias Ringwald         }
6007522e673SMatthias Ringwald 
6017522e673SMatthias Ringwald         // remote supported feature eSCO is set if link type is eSCO
6027522e673SMatthias Ringwald         // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
6037522e673SMatthias Ringwald         uint16_t max_latency;
6047522e673SMatthias Ringwald         uint8_t  retransmission_effort;
6057522e673SMatthias Ringwald         uint16_t packet_types;
6067522e673SMatthias Ringwald 
607c169df2fSMatthias Ringwald         if (eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
6087522e673SMatthias Ringwald             max_latency = 0x000c;
6097522e673SMatthias Ringwald             retransmission_effort = 0x02;
610352a0504SMatthias Ringwald             // eSCO: EV3 and 2-EV3
611352a0504SMatthias Ringwald             packet_types = 0x0048;
6127522e673SMatthias Ringwald         } else {
6137522e673SMatthias Ringwald             max_latency = 0xffff;
6147522e673SMatthias Ringwald             retransmission_effort = 0xff;
615352a0504SMatthias Ringwald             // sco: HV1 and HV3
616352a0504SMatthias Ringwald             packet_types = 0x005;
6177522e673SMatthias Ringwald         }
6187522e673SMatthias Ringwald 
619352a0504SMatthias Ringwald         // mSBC only allows for transparent data
6207522e673SMatthias Ringwald         uint16_t sco_voice_setting = hci_get_sco_voice_setting();
6217522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
622*689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
623*689d4323SMatthias Ringwald             sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
624*689d4323SMatthias Ringwald #else
625*689d4323SMatthias Ringwald             sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
626*689d4323SMatthias Ringwald #endif
6277522e673SMatthias Ringwald         }
6287522e673SMatthias Ringwald 
629352a0504SMatthias Ringwald         // filter packet types
630352a0504SMatthias Ringwald         packet_types &= hfp_get_sco_packet_types();
631352a0504SMatthias Ringwald 
632352a0504SMatthias Ringwald         // bits 6-9 are 'don't allow'
633352a0504SMatthias Ringwald         packet_types ^= 0x3c0;
634352a0504SMatthias Ringwald 
635991c26beSMatthias Ringwald         log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting);
6367522e673SMatthias Ringwald         hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
6377522e673SMatthias Ringwald                         sco_voice_setting, retransmission_effort, packet_types);
6387522e673SMatthias Ringwald         return;
6397522e673SMatthias Ringwald     }
6407522e673SMatthias Ringwald 
641d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
642d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
643d4dd47ffSMatthias Ringwald         return;
644d4dd47ffSMatthias Ringwald     }
645a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
646ce263fc8SMatthias Ringwald     if (!done){
647a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
648ce263fc8SMatthias Ringwald     }
649ce263fc8SMatthias Ringwald     if (!done){
650a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
651ce263fc8SMatthias Ringwald     }
652ce263fc8SMatthias Ringwald     if (!done){
653a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
654ce263fc8SMatthias Ringwald     }
655ce263fc8SMatthias Ringwald 
6561016a228SMatthias Ringwald     // don't send a new command while ok still pending
6571016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6581016a228SMatthias Ringwald 
659a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
660a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
661a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
662a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
663ce263fc8SMatthias Ringwald         return;
664ce263fc8SMatthias Ringwald     }
665ce263fc8SMatthias Ringwald 
666a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
667a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
668a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
669a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
670ce263fc8SMatthias Ringwald         return;
671ce263fc8SMatthias Ringwald     }
672ce263fc8SMatthias Ringwald 
673a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
674a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
675a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
676a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
677ce263fc8SMatthias Ringwald         return;
678ce263fc8SMatthias Ringwald     }
679ce263fc8SMatthias Ringwald 
680a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
681a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
682a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
683a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
684ce263fc8SMatthias Ringwald         return;
685ce263fc8SMatthias Ringwald     }
686ce263fc8SMatthias Ringwald 
687a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
688a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
689a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
690a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
691ce263fc8SMatthias Ringwald         return;
692ce263fc8SMatthias Ringwald     }
693ce263fc8SMatthias Ringwald 
694a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
695a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
696a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
697a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
698ce263fc8SMatthias Ringwald         return;
699ce263fc8SMatthias Ringwald     }
700ce263fc8SMatthias Ringwald 
701a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
702a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
703a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
704a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
705ce263fc8SMatthias Ringwald         return;
706ce263fc8SMatthias Ringwald     }
707ce263fc8SMatthias Ringwald 
708a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
709a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
710a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
711a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
712ce263fc8SMatthias Ringwald         return;
713ce263fc8SMatthias Ringwald     }
714ce263fc8SMatthias Ringwald 
715ce263fc8SMatthias Ringwald 
716a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
717a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
718a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
719a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
720ce263fc8SMatthias Ringwald         return;
721ce263fc8SMatthias Ringwald     }
722ce263fc8SMatthias Ringwald 
723a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
724a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
725a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
726a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
727ce263fc8SMatthias Ringwald         return;
728ce263fc8SMatthias Ringwald     }
729ce263fc8SMatthias Ringwald 
730a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
731a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
732a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
733a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
734ce263fc8SMatthias Ringwald         return;
735ce263fc8SMatthias Ringwald     }
736ce263fc8SMatthias Ringwald 
737a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
738a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
739a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
740a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
741ce263fc8SMatthias Ringwald         return;
742ce263fc8SMatthias Ringwald     }
743ce263fc8SMatthias Ringwald 
744a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
745a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
746a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
747a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
748ce263fc8SMatthias Ringwald         return;
749ce263fc8SMatthias Ringwald     }
750ce263fc8SMatthias Ringwald 
751a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
752a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
753a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
754a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
755ce263fc8SMatthias Ringwald         return;
756ce263fc8SMatthias Ringwald     }
757ce263fc8SMatthias Ringwald 
758a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
759a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
760a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
761a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
762ce263fc8SMatthias Ringwald         return;
763ce263fc8SMatthias Ringwald     }
764ce263fc8SMatthias Ringwald 
765a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
766a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
767a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
768a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
769ce263fc8SMatthias Ringwald         return;
770ce263fc8SMatthias Ringwald     }
771ce263fc8SMatthias Ringwald 
772a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
773a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
774a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
775a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
776ce263fc8SMatthias Ringwald         return;
777ce263fc8SMatthias Ringwald     }
778ce263fc8SMatthias Ringwald 
779a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
780a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
781a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
782a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
783ce263fc8SMatthias Ringwald         return;
784ce263fc8SMatthias Ringwald     }
785ce263fc8SMatthias Ringwald 
786a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
787a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
788a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
789a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
790ce263fc8SMatthias Ringwald         return;
791ce263fc8SMatthias Ringwald     }
792ce263fc8SMatthias Ringwald 
793a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
794a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
795a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
796a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
797667ec068SMatthias Ringwald         return;
798667ec068SMatthias Ringwald     }
799667ec068SMatthias Ringwald 
800a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
801a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
802a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
803a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
804a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
805ce263fc8SMatthias Ringwald         return;
806ce263fc8SMatthias Ringwald     }
807ce263fc8SMatthias Ringwald 
808a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
809a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
810a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
811a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
812ce263fc8SMatthias Ringwald         return;
813ce263fc8SMatthias Ringwald     }
814ce263fc8SMatthias Ringwald 
815a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
816a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
817a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
818a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
819667ec068SMatthias Ringwald         return;
820667ec068SMatthias Ringwald     }
821667ec068SMatthias Ringwald 
822a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
823a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
824667ec068SMatthias Ringwald         char buffer[20];
825a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
826667ec068SMatthias Ringwald             case '?':
827667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
828a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
829667ec068SMatthias Ringwald                 return;
830667ec068SMatthias Ringwald             case '0':
831667ec068SMatthias Ringwald             case '1':
832667ec068SMatthias Ringwald             case '2':
833a0ffb263SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
834a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
835667ec068SMatthias Ringwald                 return;
836667ec068SMatthias Ringwald             default:
837667ec068SMatthias Ringwald                 break;
838667ec068SMatthias Ringwald         }
839667ec068SMatthias Ringwald         return;
840667ec068SMatthias Ringwald     }
841667ec068SMatthias Ringwald 
842a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
843a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
844667ec068SMatthias Ringwald         char buffer[20];
845667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
846a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
847667ec068SMatthias Ringwald         return;
848667ec068SMatthias Ringwald     }
849667ec068SMatthias Ringwald 
850667ec068SMatthias Ringwald     // update HF indicators
851a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
852667ec068SMatthias Ringwald         int i;
853667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
854a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
855a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
856a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
857a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
858667ec068SMatthias Ringwald                     char buffer[30];
859ecb7d461SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]);
860a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
861667ec068SMatthias Ringwald                 } else {
86260ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
863667ec068SMatthias Ringwald                 }
864667ec068SMatthias Ringwald                 return;
865667ec068SMatthias Ringwald             }
866667ec068SMatthias Ringwald         }
867667ec068SMatthias Ringwald     }
868667ec068SMatthias Ringwald 
869ce263fc8SMatthias Ringwald     if (done) return;
870ce263fc8SMatthias Ringwald     // deal with disconnect
871a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
872ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
873a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
874a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
875ce263fc8SMatthias Ringwald             break;
876ce263fc8SMatthias Ringwald 
877ce263fc8SMatthias Ringwald         default:
878ce263fc8SMatthias Ringwald             break;
879ce263fc8SMatthias Ringwald     }
880ce263fc8SMatthias Ringwald }
881ce263fc8SMatthias Ringwald 
882a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
883a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
8846a7f44bdSMilanka Ringwald 
885ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
8867522e673SMatthias Ringwald 
887667ec068SMatthias Ringwald     // restore volume settings
888a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
889a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
890ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
891a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
892a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
893ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
894667ec068SMatthias Ringwald     // enable all indicators
895667ec068SMatthias Ringwald     int i;
896667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
897a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
898a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
899667ec068SMatthias Ringwald     }
900ce263fc8SMatthias Ringwald }
901ce263fc8SMatthias Ringwald 
9021cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
9031cc65c4fSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
9041cc65c4fSMatthias Ringwald 		// Codec supported, confirm
9051cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
9061cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
9071cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
9081cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
9091cc65c4fSMatthias Ringwald 
9101cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
9111cc65c4fSMatthias Ringwald 	} else {
9121cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
9131cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
9141cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
9151cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
9161cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9171cc65c4fSMatthias Ringwald 
9181cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
9191cc65c4fSMatthias Ringwald 	}
9201cc65c4fSMatthias Ringwald }
9211cc65c4fSMatthias Ringwald 
922a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
923a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
924a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
9253deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
926a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
927a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
9283deb3ec6SMatthias Ringwald                 break;
9293deb3ec6SMatthias Ringwald             }
930a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9313deb3ec6SMatthias Ringwald             break;
9323deb3ec6SMatthias Ringwald 
9333deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
934a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9353deb3ec6SMatthias Ringwald             break;
9363deb3ec6SMatthias Ringwald 
9373deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
938a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
9393deb3ec6SMatthias Ringwald             break;
9403deb3ec6SMatthias Ringwald 
9413deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
942a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
9433deb3ec6SMatthias Ringwald             break;
9443deb3ec6SMatthias Ringwald 
9453deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
946a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
947a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
9483deb3ec6SMatthias Ringwald                 break;
9493deb3ec6SMatthias Ringwald             }
950a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
951a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9523deb3ec6SMatthias Ringwald                 break;
9533deb3ec6SMatthias Ringwald             }
954a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9553deb3ec6SMatthias Ringwald             break;
9563deb3ec6SMatthias Ringwald 
9573deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
958a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
959a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9603deb3ec6SMatthias Ringwald                 break;
9613deb3ec6SMatthias Ringwald             }
962a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9633deb3ec6SMatthias Ringwald             break;
9643deb3ec6SMatthias Ringwald 
9653deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
966a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
9673deb3ec6SMatthias Ringwald             break;
9683deb3ec6SMatthias Ringwald 
9693deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
970a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9713deb3ec6SMatthias Ringwald             break;
9723deb3ec6SMatthias Ringwald 
9733deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
974a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9753deb3ec6SMatthias Ringwald             break;
976ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
977a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
978a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
979ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
980ce263fc8SMatthias Ringwald                 break;
981ce263fc8SMatthias Ringwald             }
9823deb3ec6SMatthias Ringwald 
983a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
984a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
985ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
986ce263fc8SMatthias Ringwald                 break;
9873deb3ec6SMatthias Ringwald             }
9883deb3ec6SMatthias Ringwald 
989a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
990ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
991a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
992ce263fc8SMatthias Ringwald                     break;
993ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
994a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
995ca59be51SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator);
996ce263fc8SMatthias Ringwald                     break;
997ce263fc8SMatthias Ringwald                 default:
998ce263fc8SMatthias Ringwald                     break;
9993deb3ec6SMatthias Ringwald             }
1000ce263fc8SMatthias Ringwald 
1001a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
1002a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
1003ce263fc8SMatthias Ringwald                 break;
10043deb3ec6SMatthias Ringwald             }
10053deb3ec6SMatthias Ringwald 
1006a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
1007aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1008a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10093deb3ec6SMatthias Ringwald                     break;
1010ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
1011a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1012ce263fc8SMatthias Ringwald                     break;
10133deb3ec6SMatthias Ringwald                 default:
10143deb3ec6SMatthias Ringwald                     break;
10153deb3ec6SMatthias Ringwald             }
10163deb3ec6SMatthias Ringwald             break;
10173deb3ec6SMatthias Ringwald         default:
10183deb3ec6SMatthias Ringwald             break;
10193deb3ec6SMatthias Ringwald     }
10203deb3ec6SMatthias Ringwald 
10213deb3ec6SMatthias Ringwald     // done
1022a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
10233deb3ec6SMatthias Ringwald }
10243deb3ec6SMatthias Ringwald 
1025b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
10264562e2a2SMatthias Ringwald     uint16_t i;
10274562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
10284562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
10294562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
10304562e2a2SMatthias Ringwald                 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
10314562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
10324562e2a2SMatthias Ringwald                 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
10334562e2a2SMatthias Ringwald                 // avoid set but not used warning
10344562e2a2SMatthias Ringwald                 (void) hfp_callheld_status;
10354562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
10364562e2a2SMatthias Ringwald                 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
10374562e2a2SMatthias Ringwald             }
10384562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
10394562e2a2SMatthias Ringwald             hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]);
10404562e2a2SMatthias Ringwald             break;
10414562e2a2SMatthias Ringwald         }
10424562e2a2SMatthias Ringwald     }
10434562e2a2SMatthias Ringwald }
10444562e2a2SMatthias Ringwald 
1045426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1046186dd3d2SMatthias Ringwald     int value;
1047186dd3d2SMatthias Ringwald     int i;
1048a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1049667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1050a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1051ca59be51SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
1052667ec068SMatthias Ringwald             break;
1053667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1054a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1055ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1056667ec068SMatthias Ringwald             break;
1057667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1058a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10592b99f6edSMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection);
1060667ec068SMatthias Ringwald             break;
1061ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1062a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10632308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1064667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1065ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1066ce263fc8SMatthias Ringwald             break;
1067ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1068a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10692308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1070667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1071ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1072ce263fc8SMatthias Ringwald             break;
1073ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1074a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1075ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1076a0ffb263SMatthias Ringwald             break;
1077a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1078a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1079ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1080a0ffb263SMatthias Ringwald             break;
1081a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1082a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1083ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1084ce263fc8SMatthias Ringwald             break;
1085ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1086a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1087a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1088a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1089ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1090ce263fc8SMatthias Ringwald             break;
1091ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1092a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1093a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1094a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
109590244c92SMilanka Ringwald 
109690244c92SMilanka Ringwald             switch (hfp_connection->state){
109790244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
109890244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
109990244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
110090244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
110190244c92SMilanka Ringwald                             return;
110290244c92SMilanka Ringwald                         default:
110390244c92SMilanka Ringwald                             break;
110490244c92SMilanka Ringwald                     }
110590244c92SMilanka Ringwald                 default:
110690244c92SMilanka Ringwald                     break;
110790244c92SMilanka Ringwald             }
1108ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1109ce263fc8SMatthias Ringwald             break;
1110ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1111a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1112ce263fc8SMatthias Ringwald             break;
1113ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
1114ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1115ce263fc8SMatthias Ringwald             break;
1116ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
11174562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1118ce263fc8SMatthias Ringwald             break;
1119c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1120c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1121c741b032SMilanka Ringwald                 hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]);
1122c741b032SMilanka Ringwald             }
1123c741b032SMilanka Ringwald             hfp_connection->command = HFP_CMD_NONE;
1124c741b032SMilanka Ringwald             break;
11251cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
11261cc65c4fSMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
11271cc65c4fSMatthias Ringwald 			hfp_connection->command = HFP_CMD_NONE;
11281cc65c4fSMatthias Ringwald 			break;
1129ce263fc8SMatthias Ringwald         default:
1130ce263fc8SMatthias Ringwald             break;
11313deb3ec6SMatthias Ringwald     }
11320cef86faSMatthias Ringwald }
1133426f9988SMatthias Ringwald 
113476cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
113576cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
113676cc1527SMatthias Ringwald }
113776cc1527SMatthias Ringwald 
1138426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1139426f9988SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
1140426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1141426f9988SMatthias Ringwald     if (size < 1) return;
1142426f9988SMatthias Ringwald 
1143426f9988SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1144426f9988SMatthias Ringwald     if (!hfp_connection) return;
1145426f9988SMatthias Ringwald 
1146426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1147426f9988SMatthias Ringwald 
1148426f9988SMatthias Ringwald     // process messages byte-wise
1149426f9988SMatthias Ringwald     int pos;
1150426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1151426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
1152426f9988SMatthias Ringwald 
1153426f9988SMatthias Ringwald         // parse until end of line "\r\n"
1154426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1155426f9988SMatthias Ringwald 
1156426f9988SMatthias Ringwald         hfp_hf_handle_rfcomm_command(hfp_connection);
1157426f9988SMatthias Ringwald     }
11581c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
11593deb3ec6SMatthias Ringwald }
11603deb3ec6SMatthias Ringwald 
11611c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1162665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1163665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1164665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1165a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
116622387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
11671c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
11683deb3ec6SMatthias Ringwald     }
11693deb3ec6SMatthias Ringwald }
11703deb3ec6SMatthias Ringwald 
11711c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
11723deb3ec6SMatthias Ringwald     switch (packet_type){
11733deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1174426f9988SMatthias Ringwald             hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size);
11753deb3ec6SMatthias Ringwald             break;
11763deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1177d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1178d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
11791c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1180d4dd47ffSMatthias Ringwald                 return;
1181d4dd47ffSMatthias Ringwald             }
118227950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1183202c8a4cSMatthias Ringwald             break;
11843deb3ec6SMatthias Ringwald         default:
11853deb3ec6SMatthias Ringwald             break;
11863deb3ec6SMatthias Ringwald     }
11871c6a0fc0SMatthias Ringwald     hfp_hf_run();
11883deb3ec6SMatthias Ringwald }
11893deb3ec6SMatthias Ringwald 
11901c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1191405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
11921c6a0fc0SMatthias Ringwald     hfp_hf_run();
1193405014fbSMatthias Ringwald }
1194405014fbSMatthias Ringwald 
1195a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1196520c92d5SMatthias Ringwald     hfp_init();
1197d63c37a1SMatthias Ringwald 
11981c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
11991c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
120027950165SMatthias Ringwald 
12011c6a0fc0SMatthias Ringwald     rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
120227950165SMatthias Ringwald 
120327950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
12041c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
120527950165SMatthias Ringwald 
1206a0ffb263SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1207a0ffb263SMatthias Ringwald     hfp_codecs_nr = 0;
1208a0ffb263SMatthias Ringwald     hfp_indicators_nr = 0;
1209a0ffb263SMatthias Ringwald     hfp_hf_speaker_gain = 9;
1210a0ffb263SMatthias Ringwald     hfp_hf_microphone_gain = 9;
1211a0ffb263SMatthias Ringwald }
1212a0ffb263SMatthias Ringwald 
1213a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
12143deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1215a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
12163deb3ec6SMatthias Ringwald         return;
12173deb3ec6SMatthias Ringwald     }
12183deb3ec6SMatthias Ringwald 
12193deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
12203deb3ec6SMatthias Ringwald     int i;
12213deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
12223deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
12233deb3ec6SMatthias Ringwald     }
12243deb3ec6SMatthias Ringwald }
12253deb3ec6SMatthias Ringwald 
1226a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
12273deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1228a0ffb263SMatthias Ringwald }
12293deb3ec6SMatthias Ringwald 
1230a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
12313deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
12323deb3ec6SMatthias Ringwald     int i;
1233a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
12343deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
12353deb3ec6SMatthias Ringwald     }
12363deb3ec6SMatthias Ringwald }
12373deb3ec6SMatthias Ringwald 
12383deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1239323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
12403deb3ec6SMatthias Ringwald }
12413deb3ec6SMatthias Ringwald 
1242c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
12439c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1244a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1245a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1246a33eb0c4SMilanka Ringwald         return;
1247a33eb0c4SMilanka Ringwald     }
1248a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
12491c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
12503deb3ec6SMatthias Ringwald }
12513deb3ec6SMatthias Ringwald 
1252c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
12539c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1254a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1255a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12563deb3ec6SMatthias Ringwald         return;
12573deb3ec6SMatthias Ringwald     }
1258a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
12591c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
12603deb3ec6SMatthias Ringwald }
12613deb3ec6SMatthias Ringwald 
1262c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1263c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1264ce263fc8SMatthias Ringwald }
1265ce263fc8SMatthias Ringwald 
1266c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1267c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1268ce263fc8SMatthias Ringwald }
1269ce263fc8SMatthias Ringwald 
12703deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1271c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
12729c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1273a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1274a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12753deb3ec6SMatthias Ringwald         return;
12763deb3ec6SMatthias Ringwald     }
1277a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1278a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
12791c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
12803deb3ec6SMatthias Ringwald }
12813deb3ec6SMatthias Ringwald 
1282c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
12839c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1284a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1285a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12863deb3ec6SMatthias Ringwald         return;
12873deb3ec6SMatthias Ringwald     }
1288a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1289ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1290a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1291ce263fc8SMatthias Ringwald             break;
1292ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1293a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1294ce263fc8SMatthias Ringwald             break;
1295ce263fc8SMatthias Ringwald         default:
1296ce263fc8SMatthias Ringwald             break;
1297ce263fc8SMatthias Ringwald     }
12981c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
12993deb3ec6SMatthias Ringwald }
13003deb3ec6SMatthias Ringwald 
1301c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
13029c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1303a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1304a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
13053deb3ec6SMatthias Ringwald         return;
13063deb3ec6SMatthias Ringwald     }
1307a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
13081c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13093deb3ec6SMatthias Ringwald }
13103deb3ec6SMatthias Ringwald 
1311ce263fc8SMatthias Ringwald 
1312c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1313c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1314ce263fc8SMatthias Ringwald }
1315ce263fc8SMatthias Ringwald 
1316c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1317c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1318ce263fc8SMatthias Ringwald }
1319ce263fc8SMatthias Ringwald 
132038200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
132138200c1dSMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
132238200c1dSMilanka Ringwald }
1323ce263fc8SMatthias Ringwald 
1324c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
13259c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1326a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1327a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1328a33eb0c4SMilanka Ringwald         return;
1329a33eb0c4SMilanka Ringwald     }
1330a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1331ce263fc8SMatthias Ringwald 
1332a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1333a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
13343deb3ec6SMatthias Ringwald 
133538200c1dSMilanka Ringwald     hfp_connection->trigger_codec_exchange = 0;
133638200c1dSMilanka Ringwald     hfp_connection->establish_audio_connection = 1;
1337a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1338332ca98fSMatthias Ringwald         log_info("no codec negotiation feature, using NBS");
1339a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
134038200c1dSMilanka Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
134138200c1dSMilanka Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
134238200c1dSMilanka Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
134338200c1dSMilanka Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
134438200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1345ce263fc8SMatthias Ringwald     } else {
1346a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1347aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1348aa4dd815SMatthias Ringwald                 break;
1349aa4dd815SMatthias Ringwald             default:
13501cc65c4fSMatthias Ringwald 				hfp_connection->codec_confirmed = 0;
13511cc65c4fSMatthias Ringwald 				hfp_connection->suggested_codec = 0;
13521cc65c4fSMatthias Ringwald 				hfp_connection->negotiated_codec = 0;
13531cc65c4fSMatthias Ringwald 				hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
135438200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1355aa4dd815SMatthias Ringwald                 break;
13563deb3ec6SMatthias Ringwald         }
1357ce263fc8SMatthias Ringwald     }
1358ce263fc8SMatthias Ringwald 
13591c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13603deb3ec6SMatthias Ringwald }
13613deb3ec6SMatthias Ringwald 
1362c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
13639c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1364a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1365a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1366a33eb0c4SMilanka Ringwald         return;
1367a33eb0c4SMilanka Ringwald     }
1368a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
13691c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13703deb3ec6SMatthias Ringwald }
13713deb3ec6SMatthias Ringwald 
1372c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
13739c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1374a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1375a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1376a33eb0c4SMilanka Ringwald         return;
1377a33eb0c4SMilanka Ringwald     }
1378ce263fc8SMatthias Ringwald 
1379ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1380a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
13811c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1382ce263fc8SMatthias Ringwald     } else {
1383ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1384ce263fc8SMatthias Ringwald     }
1385ce263fc8SMatthias Ringwald }
1386ce263fc8SMatthias Ringwald 
1387c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
13889c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1389a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1390a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1391a33eb0c4SMilanka Ringwald         return;
1392a33eb0c4SMilanka Ringwald     }
1393a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
13941c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1395ce263fc8SMatthias Ringwald }
1396ce263fc8SMatthias Ringwald 
1397c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
13989c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1399a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1400a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1401a33eb0c4SMilanka Ringwald         return;
1402a33eb0c4SMilanka Ringwald     }
1403ce263fc8SMatthias Ringwald 
1404ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1405a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
14061c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1407ce263fc8SMatthias Ringwald     }
1408ce263fc8SMatthias Ringwald }
1409ce263fc8SMatthias Ringwald 
1410c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){
14119c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1412a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1413a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1414a33eb0c4SMilanka Ringwald         return;
1415a33eb0c4SMilanka Ringwald     }
1416ce263fc8SMatthias Ringwald 
1417ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1418a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
14191c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1420ce263fc8SMatthias Ringwald     }
1421ce263fc8SMatthias Ringwald }
1422ce263fc8SMatthias Ringwald 
1423c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
14249c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1425a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1426a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1427a33eb0c4SMilanka Ringwald         return;
1428a33eb0c4SMilanka Ringwald     }
1429ce263fc8SMatthias Ringwald 
1430505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1431505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1432a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
14331c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1434ce263fc8SMatthias Ringwald     }
1435ce263fc8SMatthias Ringwald }
1436ce263fc8SMatthias Ringwald 
1437c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){
14389c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1439a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1440a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1441a33eb0c4SMilanka Ringwald         return;
1442a33eb0c4SMilanka Ringwald     }
1443ce263fc8SMatthias Ringwald 
1444505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1445505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1446a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
14471c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1448ce263fc8SMatthias Ringwald     }
1449ce263fc8SMatthias Ringwald }
1450ce263fc8SMatthias Ringwald 
1451c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
14529c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1453a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1454a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1455a33eb0c4SMilanka Ringwald         return;
1456a33eb0c4SMilanka Ringwald     }
1457ce263fc8SMatthias Ringwald 
1458505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1459505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1460a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
14611c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1462ce263fc8SMatthias Ringwald     }
1463ce263fc8SMatthias Ringwald }
1464ce263fc8SMatthias Ringwald 
1465c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
14669c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1467a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1468a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1469a33eb0c4SMilanka Ringwald         return;
1470a33eb0c4SMilanka Ringwald     }
1471ce263fc8SMatthias Ringwald 
1472505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1473505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1474a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
14751c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1476ce263fc8SMatthias Ringwald     }
1477ce263fc8SMatthias Ringwald }
1478ce263fc8SMatthias Ringwald 
1479c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
14809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1481a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1482a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1483a33eb0c4SMilanka Ringwald         return;
1484a33eb0c4SMilanka Ringwald     }
1485667ec068SMatthias Ringwald 
1486505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1487505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1488a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1489a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
14901c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1491667ec068SMatthias Ringwald     }
1492667ec068SMatthias Ringwald }
1493667ec068SMatthias Ringwald 
1494c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
14959c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1496a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1497a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1498a33eb0c4SMilanka Ringwald         return;
1499a33eb0c4SMilanka Ringwald     }
1500667ec068SMatthias Ringwald 
1501505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1502505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1503a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1504a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
15051c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1506667ec068SMatthias Ringwald     }
1507667ec068SMatthias Ringwald }
1508ce263fc8SMatthias Ringwald 
1509c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
15109c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1511a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1512a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1513a33eb0c4SMilanka Ringwald         return;
1514a33eb0c4SMilanka Ringwald     }
1515ce263fc8SMatthias Ringwald 
1516a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1517ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
15181c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1519ce263fc8SMatthias Ringwald }
1520ce263fc8SMatthias Ringwald 
1521c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
15229c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1523a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1524a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1525a33eb0c4SMilanka Ringwald         return;
1526a33eb0c4SMilanka Ringwald     }
1527ce263fc8SMatthias Ringwald 
1528a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1529a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1530a0ffb263SMatthias Ringwald 
15311c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1532ce263fc8SMatthias Ringwald }
1533ce263fc8SMatthias Ringwald 
1534c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
15359c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1536a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1537a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1538a33eb0c4SMilanka Ringwald         return;
1539a33eb0c4SMilanka Ringwald     }
1540ce263fc8SMatthias Ringwald 
1541a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
15421c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1543ce263fc8SMatthias Ringwald }
1544ce263fc8SMatthias Ringwald 
1545c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
15469c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1547a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1548a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1549a33eb0c4SMilanka Ringwald         return;
1550a33eb0c4SMilanka Ringwald     }
1551ce263fc8SMatthias Ringwald 
1552a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
15531c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1554ce263fc8SMatthias Ringwald }
1555ce263fc8SMatthias Ringwald 
1556ce263fc8SMatthias Ringwald 
1557c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
15589c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1559a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1560a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1561a33eb0c4SMilanka Ringwald         return;
1562a33eb0c4SMilanka Ringwald     }
1563ce263fc8SMatthias Ringwald 
1564a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
15651c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1566ce263fc8SMatthias Ringwald }
1567ce263fc8SMatthias Ringwald 
1568ce263fc8SMatthias Ringwald 
1569c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
15709c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1571a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1572a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1573a33eb0c4SMilanka Ringwald         return;
1574a33eb0c4SMilanka Ringwald     }
1575ce263fc8SMatthias Ringwald 
1576a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
15771c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1578ce263fc8SMatthias Ringwald }
1579ce263fc8SMatthias Ringwald 
1580c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
15819c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1582a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1583a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1584a33eb0c4SMilanka Ringwald         return;
1585a33eb0c4SMilanka Ringwald     }
1586ce263fc8SMatthias Ringwald 
1587a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
15881c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1589ce263fc8SMatthias Ringwald }
1590ce263fc8SMatthias Ringwald 
1591ce263fc8SMatthias Ringwald 
1592c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
15939c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1594a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1595a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1596a33eb0c4SMilanka Ringwald         return;
1597a33eb0c4SMilanka Ringwald     }
1598ce263fc8SMatthias Ringwald 
1599a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
16001c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1601ce263fc8SMatthias Ringwald }
1602ce263fc8SMatthias Ringwald 
1603c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
16049c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1605a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1606a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1607a33eb0c4SMilanka Ringwald         return;
1608a33eb0c4SMilanka Ringwald     }
1609ce263fc8SMatthias Ringwald 
1610a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
16111c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1612ce263fc8SMatthias Ringwald }
1613ce263fc8SMatthias Ringwald 
1614c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
16159c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1616a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1617a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1618a33eb0c4SMilanka Ringwald         return;
1619a33eb0c4SMilanka Ringwald     }
1620ce263fc8SMatthias Ringwald 
1621a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
16221c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1623ce263fc8SMatthias Ringwald }
1624ce263fc8SMatthias Ringwald 
1625c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
16269c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1627a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1628a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1629a33eb0c4SMilanka Ringwald         return;
1630a33eb0c4SMilanka Ringwald     }
1631ce263fc8SMatthias Ringwald 
1632a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
16331c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1634ce263fc8SMatthias Ringwald }
1635ce263fc8SMatthias Ringwald 
1636c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
16379c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1638a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1639a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1640a33eb0c4SMilanka Ringwald         return;
1641a33eb0c4SMilanka Ringwald     }
1642c8626498SMilanka Ringwald 
1643a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1644c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1645a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1646a0ffb263SMatthias Ringwald         return;
1647a0ffb263SMatthias Ringwald     }
1648a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1649a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
16501c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1651ce263fc8SMatthias Ringwald }
1652ce263fc8SMatthias Ringwald 
1653c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
16549c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1655a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1656a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1657a33eb0c4SMilanka Ringwald         return;
1658a33eb0c4SMilanka Ringwald     }
1659c8626498SMilanka Ringwald 
1660a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1661c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1662a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1663a0ffb263SMatthias Ringwald         return;
1664a0ffb263SMatthias Ringwald     }
1665a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1666a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
16671c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1668ce263fc8SMatthias Ringwald }
1669ce263fc8SMatthias Ringwald 
1670c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
16719c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1672a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1673a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1674a33eb0c4SMilanka Ringwald         return;
1675a33eb0c4SMilanka Ringwald     }
1676a33eb0c4SMilanka Ringwald 
1677a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
16781c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1679ce263fc8SMatthias Ringwald }
1680ce263fc8SMatthias Ringwald 
1681c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
16829c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1683a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1684a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1685a33eb0c4SMilanka Ringwald         return;
1686a33eb0c4SMilanka Ringwald     }
1687a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
16881c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1689ce263fc8SMatthias Ringwald }
16903deb3ec6SMatthias Ringwald 
1691c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
16929c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1693a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1694a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1695a33eb0c4SMilanka Ringwald         return;
1696a33eb0c4SMilanka Ringwald     }
1697a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
16981c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1699667ec068SMatthias Ringwald }
1700667ec068SMatthias Ringwald 
1701667ec068SMatthias Ringwald 
1702c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
17039c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1704a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1705a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1706a33eb0c4SMilanka Ringwald         return;
1707a33eb0c4SMilanka Ringwald     }
1708a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1709a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
17101c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1711667ec068SMatthias Ringwald }
1712667ec068SMatthias Ringwald 
1713c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
17149c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1715a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1716a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1717a33eb0c4SMilanka Ringwald         return;
1718a33eb0c4SMilanka Ringwald     }
1719a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1720a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
17211c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1722667ec068SMatthias Ringwald }
1723667ec068SMatthias Ringwald 
1724c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
17259c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1726a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1727a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1728a33eb0c4SMilanka Ringwald         return;
1729a33eb0c4SMilanka Ringwald     }
1730a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1731a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
17321c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1733667ec068SMatthias Ringwald }
1734667ec068SMatthias Ringwald 
1735c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
17369c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1737a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1738a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1739a33eb0c4SMilanka Ringwald         return;
1740a33eb0c4SMilanka Ringwald     }
1741a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1742a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
17431c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1744667ec068SMatthias Ringwald }
1745667ec068SMatthias Ringwald 
1746c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
17479c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1748a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1749a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1750a33eb0c4SMilanka Ringwald         return;
1751a33eb0c4SMilanka Ringwald     }
1752a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
17531c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1754667ec068SMatthias Ringwald }
1755667ec068SMatthias Ringwald 
1756c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
17579c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1758a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1759a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1760a33eb0c4SMilanka Ringwald         return;
1761a33eb0c4SMilanka Ringwald     }
1762667ec068SMatthias Ringwald     // find index for assigned number
1763667ec068SMatthias Ringwald     int i;
1764667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1765667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1766667ec068SMatthias Ringwald             // set value
1767667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1768667ec068SMatthias Ringwald             // mark for update
1769a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1770a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1771667ec068SMatthias Ringwald                 // send update
17721c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
1773a0ffb263SMatthias Ringwald             }
1774667ec068SMatthias Ringwald             return;
1775667ec068SMatthias Ringwald         }
1776667ec068SMatthias Ringwald     }
1777667ec068SMatthias Ringwald }
1778667ec068SMatthias Ringwald 
1779d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
1780d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1781d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
1782d7f6b5cbSMatthias Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1783d7f6b5cbSMatthias Ringwald         return 0;
1784d7f6b5cbSMatthias Ringwald     }
1785d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1786d7f6b5cbSMatthias Ringwald }
178776cc1527SMatthias Ringwald 
178876cc1527SMatthias 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){
178976cc1527SMatthias Ringwald 	if (!name){
179076cc1527SMatthias Ringwald 		name = default_hfp_hf_service_name;
179176cc1527SMatthias Ringwald 	}
179276cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
179376cc1527SMatthias Ringwald 
179476cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
179576cc1527SMatthias Ringwald 	//
179676cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
179776cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
179876cc1527SMatthias Ringwald 	//
179976cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
180076cc1527SMatthias Ringwald 	//
180176cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
180276cc1527SMatthias Ringwald 	if (wide_band_speech && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
180376cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
180476cc1527SMatthias Ringwald 	}
180576cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
180676cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
180776cc1527SMatthias Ringwald }
180876cc1527SMatthias Ringwald 
180976cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
181076cc1527SMatthias Ringwald 	if (callback == NULL){
181176cc1527SMatthias Ringwald 		log_error("hfp_hf_register_packet_handler called with NULL callback");
181276cc1527SMatthias Ringwald 		return;
181376cc1527SMatthias Ringwald 	}
181476cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
181576cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
181676cc1527SMatthias Ringwald }