xref: /btstack/src/classic/hfp_hf.c (revision a473a0097d11744933738d68d893365ee9e98f28)
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 
6720b2edb6SMatthias Ringwald // const
6820b2edb6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
6920b2edb6SMatthias Ringwald 
7020b2edb6SMatthias Ringwald // globals
711c6a0fc0SMatthias Ringwald static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration;
7227950165SMatthias Ringwald 
733deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
7420b2edb6SMatthias Ringwald static uint8_t hfp_codecs_nr;
753deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
763deb3ec6SMatthias Ringwald 
7720b2edb6SMatthias Ringwald static uint8_t hfp_indicators_nr;
7825789943SMilanka Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_INDICATORS];
7925789943SMilanka Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_INDICATORS];
80667ec068SMatthias Ringwald 
8120b2edb6SMatthias Ringwald static uint8_t hfp_hf_speaker_gain;
8220b2edb6SMatthias Ringwald static uint8_t hfp_hf_microphone_gain;
833deb3ec6SMatthias Ringwald 
84ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
853deb3ec6SMatthias Ringwald 
86ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
87ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
88ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
89ce263fc8SMatthias Ringwald 
90ce263fc8SMatthias Ringwald static char phone_number[25];
91ce263fc8SMatthias Ringwald 
9276cc1527SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
9376cc1527SMatthias Ringwald 	int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
9476cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
9576cc1527SMatthias Ringwald 	return hf && ag;
9676cc1527SMatthias Ringwald }
9776cc1527SMatthias Ringwald 
9876cc1527SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
9976cc1527SMatthias Ringwald 	int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
10076cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
10176cc1527SMatthias Ringwald 	return hf && ag;
10276cc1527SMatthias Ringwald }
10376cc1527SMatthias Ringwald 
10476cc1527SMatthias Ringwald 
10576cc1527SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
10676cc1527SMatthias Ringwald 	int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
10776cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
10876cc1527SMatthias Ringwald 	return hf && ag;
10976cc1527SMatthias Ringwald }
11076cc1527SMatthias Ringwald 
11176cc1527SMatthias Ringwald 
1129c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
1139c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1149c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1159c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1169c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1179c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1189c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1199c9c64c1SMatthias Ringwald         return hfp_connection;
1209c9c64c1SMatthias Ringwald     }
1219c9c64c1SMatthias Ringwald     return NULL;
1229c9c64c1SMatthias Ringwald }
1239c9c64c1SMatthias Ringwald 
12476cc1527SMatthias Ringwald /* emit functinos */
1253deb3ec6SMatthias Ringwald 
126*a473a009SMatthias Ringwald static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){
127*a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
128a0ffb263SMatthias Ringwald     uint8_t event[31];
129a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
130a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
131*a473a009SMatthias Ringwald     event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION;
132a0ffb263SMatthias Ringwald     event[3] = status;
133*a473a009SMatthias Ringwald     event[4] = hfp_connection->bnip_type;
134*a473a009SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 6);
135*a473a009SMatthias Ringwald     strncpy((char*)&event[5], hfp_connection->bnip_number, size);
136a0ffb263SMatthias Ringwald     event[5 + size] = 0;
137*a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
138a0ffb263SMatthias Ringwald }
139a0ffb263SMatthias Ringwald 
140*a473a009SMatthias Ringwald static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){
141*a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
142a0ffb263SMatthias Ringwald     uint8_t event[30];
143a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
144a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
145a0ffb263SMatthias Ringwald     event[2] = event_subtype;
146*a473a009SMatthias Ringwald     event[3] = hfp_connection->bnip_type;
147*a473a009SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 5);
148*a473a009SMatthias Ringwald     strncpy((char*)&event[4], hfp_connection->bnip_number, size);
149a0ffb263SMatthias Ringwald     event[4 + size] = 0;
150*a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
151a0ffb263SMatthias Ringwald }
152a0ffb263SMatthias Ringwald 
153*a473a009SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){
154*a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
1550aee97efSMilanka Ringwald     uint8_t event[36];
1560aee97efSMilanka Ringwald     int pos = 0;
1570aee97efSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
1580aee97efSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
1590aee97efSMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
160*a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_idx;
161*a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_dir;
162*a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_status;
163*a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mode;
164*a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mpty;
165*a473a009SMatthias Ringwald     event[pos++] = hfp_connection->bnip_type;
166*a473a009SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - pos);
167*a473a009SMatthias Ringwald     strncpy((char*)&event[pos], hfp_connection->bnip_number, size);
1680aee97efSMilanka Ringwald     pos += size;
1690aee97efSMilanka Ringwald     event[pos++] = 0;
170*a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
171a0ffb263SMatthias Ringwald }
172a0ffb263SMatthias Ringwald 
17376cc1527SMatthias Ringwald 
174*a473a009SMatthias Ringwald static void hfp_emit_ag_indicator_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
175*a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
17676cc1527SMatthias Ringwald 	uint8_t event[10+HFP_MAX_INDICATOR_DESC_SIZE+1];
17776cc1527SMatthias Ringwald 	int pos = 0;
17876cc1527SMatthias Ringwald 	event[pos++] = HCI_EVENT_HFP_META;
17976cc1527SMatthias Ringwald 	event[pos++] = sizeof(event) - 2;
18076cc1527SMatthias Ringwald 	event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
181*a473a009SMatthias Ringwald 	event[pos++] = indicator->index;
182*a473a009SMatthias Ringwald 	event[pos++] = indicator->status;
183*a473a009SMatthias Ringwald 	event[pos++] = indicator->min_range;
184*a473a009SMatthias Ringwald 	event[pos++] = indicator->max_range;
185*a473a009SMatthias Ringwald 	event[pos++] = indicator->mandatory;
186*a473a009SMatthias Ringwald 	event[pos++] = indicator->enabled;
187*a473a009SMatthias Ringwald 	event[pos++] = indicator->status_changed;
188*a473a009SMatthias Ringwald 	strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
18976cc1527SMatthias Ringwald 	pos += HFP_MAX_INDICATOR_DESC_SIZE;
19076cc1527SMatthias Ringwald 	event[pos] = 0;
191*a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1923deb3ec6SMatthias Ringwald }
1933deb3ec6SMatthias Ringwald 
194*a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){
195*a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
19676cc1527SMatthias Ringwald 	uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
19776cc1527SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
19876cc1527SMatthias Ringwald 	event[1] = sizeof(event) - 2;
19976cc1527SMatthias Ringwald 	event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
200*a473a009SMatthias Ringwald 	event[3] = hfp_connection->network_operator.mode;
201*a473a009SMatthias Ringwald 	event[4] = hfp_connection->network_operator.format;
202*a473a009SMatthias Ringwald 	strncpy((char*)&event[5], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
20376cc1527SMatthias Ringwald 	event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
204*a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2053deb3ec6SMatthias Ringwald }
2063deb3ec6SMatthias Ringwald 
20776cc1527SMatthias Ringwald /* send commands */
20889425bfcSMilanka Ringwald 
20989425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2103deb3ec6SMatthias Ringwald     char buffer[20];
2111599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
21289425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
21389425bfcSMilanka Ringwald }
21489425bfcSMilanka Ringwald 
21589425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
21689425bfcSMilanka Ringwald     char buffer[20];
2171599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
21889425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
21989425bfcSMilanka Ringwald }
22089425bfcSMilanka Ringwald 
22186da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
22289425bfcSMilanka Ringwald     char buffer[40];
2231599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
2243deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2253deb3ec6SMatthias Ringwald }
2263deb3ec6SMatthias Ringwald 
2273deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2283deb3ec6SMatthias Ringwald     char buffer[30];
22989425bfcSMilanka Ringwald     const int size = sizeof(buffer);
23089425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
23189425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
2321599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2333deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2343deb3ec6SMatthias Ringwald }
2353deb3ec6SMatthias Ringwald 
2363deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2373deb3ec6SMatthias Ringwald     char buffer[50];
23889425bfcSMilanka Ringwald     const int size = sizeof(buffer);
23989425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
24089425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
2411599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2423deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2433deb3ec6SMatthias Ringwald }
2443deb3ec6SMatthias Ringwald 
2453deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2463deb3ec6SMatthias Ringwald     char buffer[30];
24789425bfcSMilanka Ringwald     const int size = sizeof(buffer);
24889425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
24989425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
2501599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2513deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2523deb3ec6SMatthias Ringwald }
2533deb3ec6SMatthias Ringwald 
25489425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2553deb3ec6SMatthias Ringwald     char buffer[20];
2561599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
257ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
258ce263fc8SMatthias Ringwald }
259ce263fc8SMatthias Ringwald 
260ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
261ce263fc8SMatthias Ringwald     char buffer[40];
2621599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, phone_number);
263ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
264ce263fc8SMatthias Ringwald }
265ce263fc8SMatthias Ringwald 
266a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
267ce263fc8SMatthias Ringwald     char buffer[40];
2681599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
269ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
270ce263fc8SMatthias Ringwald }
271ce263fc8SMatthias Ringwald 
272f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
27389425bfcSMilanka Ringwald     char buffer[40];
2741599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
275ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
276ce263fc8SMatthias Ringwald }
277ce263fc8SMatthias Ringwald 
278ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
279ce263fc8SMatthias Ringwald     char buffer[20];
2801599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
281ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
282ce263fc8SMatthias Ringwald }
283ce263fc8SMatthias Ringwald 
28497d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
2851599fe57SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "ATA\r");
28697d2cadbSMatthias Ringwald }
28797d2cadbSMatthias Ringwald 
28889425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
28989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
29089425bfcSMilanka Ringwald }
29189425bfcSMilanka Ringwald 
29289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
29389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
29489425bfcSMilanka Ringwald }
29589425bfcSMilanka Ringwald 
29689425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
29789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
29889425bfcSMilanka Ringwald }
29989425bfcSMilanka Ringwald 
30089425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
30189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
30289425bfcSMilanka Ringwald }
30389425bfcSMilanka Ringwald 
30489425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
30589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
30689425bfcSMilanka Ringwald }
30789425bfcSMilanka Ringwald 
30889425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
30989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
31089425bfcSMilanka Ringwald }
31189425bfcSMilanka Ringwald 
31289425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
31389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
31489425bfcSMilanka Ringwald }
31589425bfcSMilanka Ringwald 
31689425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
31789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
31889425bfcSMilanka Ringwald }
31989425bfcSMilanka Ringwald 
32089425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
32189425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
32289425bfcSMilanka Ringwald }
32389425bfcSMilanka Ringwald 
32489425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
32589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
32689425bfcSMilanka Ringwald }
32789425bfcSMilanka Ringwald 
32889425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
32989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
33089425bfcSMilanka Ringwald }
33189425bfcSMilanka Ringwald 
33289425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
33389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
33489425bfcSMilanka Ringwald }
33589425bfcSMilanka Ringwald 
33689425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
33789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
33889425bfcSMilanka Ringwald }
33989425bfcSMilanka Ringwald 
34089425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
34189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
34289425bfcSMilanka Ringwald }
34389425bfcSMilanka Ringwald 
34489425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
34589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
34689425bfcSMilanka Ringwald }
34789425bfcSMilanka Ringwald 
34889425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
34989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
35089425bfcSMilanka Ringwald }
35189425bfcSMilanka Ringwald 
35289425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
35389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
35489425bfcSMilanka Ringwald }
35589425bfcSMilanka Ringwald 
35689425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
35789425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
35889425bfcSMilanka Ringwald }
35989425bfcSMilanka Ringwald 
36089425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
36189425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
36289425bfcSMilanka Ringwald }
36389425bfcSMilanka Ringwald 
364ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
36589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
366ce263fc8SMatthias Ringwald }
367ce263fc8SMatthias Ringwald 
368667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
36989425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
370667ec068SMatthias Ringwald }
371667ec068SMatthias Ringwald 
37276cc1527SMatthias Ringwald /* state machines */
3733deb3ec6SMatthias Ringwald 
374a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
375a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
376a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
377aa4dd815SMatthias Ringwald     int done = 1;
3783deb3ec6SMatthias Ringwald 
379a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
3803deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
381d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
382a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
383a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
3843deb3ec6SMatthias Ringwald             break;
3853deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
386a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
387a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
3883deb3ec6SMatthias Ringwald             break;
3893deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
390a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
391a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
3923deb3ec6SMatthias Ringwald             break;
3933deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
394a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
395a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
3963deb3ec6SMatthias Ringwald             break;
3973deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
398a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
399a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4003deb3ec6SMatthias Ringwald             break;
4013deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
402a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
403a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4043deb3ec6SMatthias Ringwald             break;
4053deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
406a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
407a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4083deb3ec6SMatthias Ringwald             break;
4093deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
410a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
411a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4123deb3ec6SMatthias Ringwald             break;
4133deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
414a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
415a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4163deb3ec6SMatthias Ringwald             break;
4173deb3ec6SMatthias Ringwald         default:
418aa4dd815SMatthias Ringwald             done = 0;
4193deb3ec6SMatthias Ringwald             break;
4203deb3ec6SMatthias Ringwald     }
4213deb3ec6SMatthias Ringwald     return done;
4223deb3ec6SMatthias Ringwald }
4233deb3ec6SMatthias Ringwald 
424ce263fc8SMatthias Ringwald 
425a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
426a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
427a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
428ce263fc8SMatthias Ringwald 
429ce263fc8SMatthias Ringwald     int done = 0;
430a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
431a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
432ce263fc8SMatthias Ringwald         done = 1;
433a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
434ce263fc8SMatthias Ringwald         return done;
435ce263fc8SMatthias Ringwald     };
436a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
437a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
438ce263fc8SMatthias Ringwald         done = 1;
439a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
440a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
441a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
442ce263fc8SMatthias Ringwald         return done;
443ce263fc8SMatthias Ringwald     }
444ce263fc8SMatthias Ringwald 
445a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
446ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
447a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
448a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
449a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
450ce263fc8SMatthias Ringwald             return 1;
451ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
452a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
453a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
454a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
455ce263fc8SMatthias Ringwald             return 1;
456ce263fc8SMatthias Ringwald         default:
457ce263fc8SMatthias Ringwald             break;
458ce263fc8SMatthias Ringwald     }
459ce263fc8SMatthias Ringwald 
460a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
461a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
462ce263fc8SMatthias Ringwald         done = 1;
463a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
464ce263fc8SMatthias Ringwald         return done;
465ce263fc8SMatthias Ringwald     }
466ce263fc8SMatthias Ringwald 
467ce263fc8SMatthias Ringwald     return done;
468ce263fc8SMatthias Ringwald }
469ce263fc8SMatthias Ringwald 
470a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
471ce263fc8SMatthias Ringwald 
472a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
473ce263fc8SMatthias Ringwald 
474332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
475332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
476ce263fc8SMatthias Ringwald 
477a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
478a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
479332ca98fSMatthias Ringwald 		return 1;
480332ca98fSMatthias Ringwald     }
481332ca98fSMatthias Ringwald 
4821cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
4831cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
484ce263fc8SMatthias Ringwald 
485a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
486fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
4871cc65c4fSMatthias Ringwald 		return 1;
4881cc65c4fSMatthias Ringwald     }
4891cc65c4fSMatthias Ringwald 
4901cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
4911cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
4921cc65c4fSMatthias Ringwald 
493a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
494a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4951cc65c4fSMatthias Ringwald 		return 1;
4961cc65c4fSMatthias Ringwald     }
497ce263fc8SMatthias Ringwald 
498ce263fc8SMatthias Ringwald     return 0;
499ce263fc8SMatthias Ringwald }
500ce263fc8SMatthias Ringwald 
501a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
502505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
503505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
504ce263fc8SMatthias Ringwald 
50564f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
506a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
507a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
508a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
509ce263fc8SMatthias Ringwald         return 1;
510ce263fc8SMatthias Ringwald     }
511ce263fc8SMatthias Ringwald 
512a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
513ce263fc8SMatthias Ringwald 
514ce263fc8SMatthias Ringwald     // run codecs exchange
515a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
516ce263fc8SMatthias Ringwald     if (done) return 1;
517ce263fc8SMatthias Ringwald 
51838200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
51938200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
52038200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
52138200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
52238200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
52338200c1dSMilanka Ringwald         return 1;
52438200c1dSMilanka Ringwald     }
52538200c1dSMilanka Ringwald 
526ce263fc8SMatthias Ringwald     return 0;
527ce263fc8SMatthias Ringwald }
528ce263fc8SMatthias Ringwald 
52938200c1dSMilanka Ringwald 
530a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
531eaf2b0a1SMatthias Ringwald 
532eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
533eaf2b0a1SMatthias Ringwald 
534a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
535a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
536a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
537ce263fc8SMatthias Ringwald         return 1;
538ce263fc8SMatthias Ringwald     }
539ce263fc8SMatthias Ringwald     return 0;
540ce263fc8SMatthias Ringwald }
541ce263fc8SMatthias Ringwald 
5421c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
5437522e673SMatthias Ringwald 
54476cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
54576cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
54676cc1527SMatthias Ringwald 
54776cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
54876cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
54922387625SMatthias Ringwald 
5503721a235SMatthias Ringwald 	// assert command could be sent
5513721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
5523721a235SMatthias Ringwald 
5533721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
5543721a235SMatthias Ringwald     // WBS Disassociate
5553721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
5563721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
5573721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
5583721a235SMatthias Ringwald         return;
5593721a235SMatthias Ringwald     }
5603721a235SMatthias Ringwald     // Write Codec Config
5613721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
5623721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
5633721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
5643721a235SMatthias Ringwald         return;
5653721a235SMatthias Ringwald     }
5663721a235SMatthias Ringwald     // WBS Associate
5673721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
5683721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
5693721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
5703721a235SMatthias Ringwald         return;
5713721a235SMatthias Ringwald     }
5723721a235SMatthias Ringwald #endif
573689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
574689d4323SMatthias Ringwald     // Enable WBS
575689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
576689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
577689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
578689d4323SMatthias Ringwald         return;
579689d4323SMatthias Ringwald     }
580689d4323SMatthias Ringwald     // Write I2S/PCM params
581689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
582689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
583689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
584689d4323SMatthias Ringwald         return;
585689d4323SMatthias Ringwald     }
586689d4323SMatthias Ringwald     // Disable WBS
587689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
588689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
589689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
590689d4323SMatthias Ringwald         return;
591689d4323SMatthias Ringwald     }
592689d4323SMatthias Ringwald #endif
59348e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
59448e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
59548e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
59648e6eeeeSMatthias Ringwald         return;
59748e6eeeeSMatthias Ringwald     }
59848e6eeeeSMatthias Ringwald #endif
5993721a235SMatthias Ringwald 
6003721a235SMatthias Ringwald     if (hfp_connection->hf_accept_sco){
601b72c4a9eSMatthias Ringwald 
602c169df2fSMatthias Ringwald         bool eSCO = hfp_connection->hf_accept_sco == 2;
603b72c4a9eSMatthias Ringwald         hfp_connection->hf_accept_sco = 0;
6047522e673SMatthias Ringwald 
6057522e673SMatthias Ringwald         // notify about codec selection if not done already
6067522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
6077522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
6087522e673SMatthias Ringwald         }
6097522e673SMatthias Ringwald 
6107522e673SMatthias Ringwald         // remote supported feature eSCO is set if link type is eSCO
6117522e673SMatthias Ringwald         // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
6127522e673SMatthias Ringwald         uint16_t max_latency;
6137522e673SMatthias Ringwald         uint8_t  retransmission_effort;
6147522e673SMatthias Ringwald         uint16_t packet_types;
6157522e673SMatthias Ringwald 
616c169df2fSMatthias Ringwald         if (eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
6177522e673SMatthias Ringwald             max_latency = 0x000c;
6187522e673SMatthias Ringwald             retransmission_effort = 0x02;
619352a0504SMatthias Ringwald             // eSCO: EV3 and 2-EV3
620352a0504SMatthias Ringwald             packet_types = 0x0048;
6217522e673SMatthias Ringwald         } else {
6227522e673SMatthias Ringwald             max_latency = 0xffff;
6237522e673SMatthias Ringwald             retransmission_effort = 0xff;
624352a0504SMatthias Ringwald             // sco: HV1 and HV3
625352a0504SMatthias Ringwald             packet_types = 0x005;
6267522e673SMatthias Ringwald         }
6277522e673SMatthias Ringwald 
628352a0504SMatthias Ringwald         // mSBC only allows for transparent data
6297522e673SMatthias Ringwald         uint16_t sco_voice_setting = hci_get_sco_voice_setting();
6307522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
631689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
632689d4323SMatthias Ringwald             sco_voice_setting = 0x0063; // Transparent data, 16-bit for BCM controllers
633689d4323SMatthias Ringwald #else
634689d4323SMatthias Ringwald             sco_voice_setting = 0x0043; // Transparent data, 8-bit otherwise
635689d4323SMatthias Ringwald #endif
6367522e673SMatthias Ringwald         }
6377522e673SMatthias Ringwald 
638352a0504SMatthias Ringwald         // filter packet types
639352a0504SMatthias Ringwald         packet_types &= hfp_get_sco_packet_types();
640352a0504SMatthias Ringwald 
641352a0504SMatthias Ringwald         // bits 6-9 are 'don't allow'
642352a0504SMatthias Ringwald         packet_types ^= 0x3c0;
643352a0504SMatthias Ringwald 
644991c26beSMatthias Ringwald         log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting);
6457522e673SMatthias Ringwald         hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
6467522e673SMatthias Ringwald                         sco_voice_setting, retransmission_effort, packet_types);
6477522e673SMatthias Ringwald         return;
6487522e673SMatthias Ringwald     }
6497522e673SMatthias Ringwald 
650d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
651d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
652d4dd47ffSMatthias Ringwald         return;
653d4dd47ffSMatthias Ringwald     }
654a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
655ce263fc8SMatthias Ringwald     if (!done){
656a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
657ce263fc8SMatthias Ringwald     }
658ce263fc8SMatthias Ringwald     if (!done){
659a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
660ce263fc8SMatthias Ringwald     }
661ce263fc8SMatthias Ringwald     if (!done){
662a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
663ce263fc8SMatthias Ringwald     }
664ce263fc8SMatthias Ringwald 
6651016a228SMatthias Ringwald     // don't send a new command while ok still pending
6661016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6671016a228SMatthias Ringwald 
668a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
669a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
670a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
671a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
672ce263fc8SMatthias Ringwald         return;
673ce263fc8SMatthias Ringwald     }
674ce263fc8SMatthias Ringwald 
675a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
676a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
677a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
678a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
679ce263fc8SMatthias Ringwald         return;
680ce263fc8SMatthias Ringwald     }
681ce263fc8SMatthias Ringwald 
682a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
683a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
684a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
685a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
686ce263fc8SMatthias Ringwald         return;
687ce263fc8SMatthias Ringwald     }
688ce263fc8SMatthias Ringwald 
689a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
690a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
691a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
692a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
693ce263fc8SMatthias Ringwald         return;
694ce263fc8SMatthias Ringwald     }
695ce263fc8SMatthias Ringwald 
696a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
697a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
698a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
699a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
700ce263fc8SMatthias Ringwald         return;
701ce263fc8SMatthias Ringwald     }
702ce263fc8SMatthias Ringwald 
703a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
704a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
705a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
706a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
707ce263fc8SMatthias Ringwald         return;
708ce263fc8SMatthias Ringwald     }
709ce263fc8SMatthias Ringwald 
710a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
711a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
712a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
713a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
714ce263fc8SMatthias Ringwald         return;
715ce263fc8SMatthias Ringwald     }
716ce263fc8SMatthias Ringwald 
717a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
718a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
719a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
720a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
721ce263fc8SMatthias Ringwald         return;
722ce263fc8SMatthias Ringwald     }
723ce263fc8SMatthias Ringwald 
724ce263fc8SMatthias Ringwald 
725a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
726a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
727a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
728a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
729ce263fc8SMatthias Ringwald         return;
730ce263fc8SMatthias Ringwald     }
731ce263fc8SMatthias Ringwald 
732a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
733a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
734a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
735a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
736ce263fc8SMatthias Ringwald         return;
737ce263fc8SMatthias Ringwald     }
738ce263fc8SMatthias Ringwald 
739a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
740a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
741a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
742a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
743ce263fc8SMatthias Ringwald         return;
744ce263fc8SMatthias Ringwald     }
745ce263fc8SMatthias Ringwald 
746a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
747a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
748a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
749a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
750ce263fc8SMatthias Ringwald         return;
751ce263fc8SMatthias Ringwald     }
752ce263fc8SMatthias Ringwald 
753a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
754a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
755a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
756a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
757ce263fc8SMatthias Ringwald         return;
758ce263fc8SMatthias Ringwald     }
759ce263fc8SMatthias Ringwald 
760a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
761a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
762a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
763a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
764ce263fc8SMatthias Ringwald         return;
765ce263fc8SMatthias Ringwald     }
766ce263fc8SMatthias Ringwald 
767a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
768a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
769a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
770a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
771ce263fc8SMatthias Ringwald         return;
772ce263fc8SMatthias Ringwald     }
773ce263fc8SMatthias Ringwald 
774a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
775a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
776a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
777a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
778ce263fc8SMatthias Ringwald         return;
779ce263fc8SMatthias Ringwald     }
780ce263fc8SMatthias Ringwald 
781a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
782a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
783a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
784a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
785ce263fc8SMatthias Ringwald         return;
786ce263fc8SMatthias Ringwald     }
787ce263fc8SMatthias Ringwald 
788a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
789a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
790a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
791a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
792ce263fc8SMatthias Ringwald         return;
793ce263fc8SMatthias Ringwald     }
794ce263fc8SMatthias Ringwald 
795a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
796a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
797a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
798a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
799ce263fc8SMatthias Ringwald         return;
800ce263fc8SMatthias Ringwald     }
801ce263fc8SMatthias Ringwald 
802a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
803a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
804a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
805a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
806667ec068SMatthias Ringwald         return;
807667ec068SMatthias Ringwald     }
808667ec068SMatthias Ringwald 
809a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
810a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
811a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
812a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
813a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
814ce263fc8SMatthias Ringwald         return;
815ce263fc8SMatthias Ringwald     }
816ce263fc8SMatthias Ringwald 
817a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
818a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
819a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
820a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
821ce263fc8SMatthias Ringwald         return;
822ce263fc8SMatthias Ringwald     }
823ce263fc8SMatthias Ringwald 
824a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
825a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
826a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
827a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
828667ec068SMatthias Ringwald         return;
829667ec068SMatthias Ringwald     }
830667ec068SMatthias Ringwald 
831a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
832a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
833667ec068SMatthias Ringwald         char buffer[20];
834a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
835667ec068SMatthias Ringwald             case '?':
8361599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
837ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
838ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
839a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
840667ec068SMatthias Ringwald                 return;
841667ec068SMatthias Ringwald             case '0':
842667ec068SMatthias Ringwald             case '1':
843667ec068SMatthias Ringwald             case '2':
8441599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
845ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
846ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
847ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
848a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
849667ec068SMatthias Ringwald                 return;
850667ec068SMatthias Ringwald             default:
851667ec068SMatthias Ringwald                 break;
852667ec068SMatthias Ringwald         }
853667ec068SMatthias Ringwald         return;
854667ec068SMatthias Ringwald     }
855667ec068SMatthias Ringwald 
856a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
857a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
858667ec068SMatthias Ringwald         char buffer[20];
8591599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
860ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
861ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
862a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
863667ec068SMatthias Ringwald         return;
864667ec068SMatthias Ringwald     }
865667ec068SMatthias Ringwald 
866667ec068SMatthias Ringwald     // update HF indicators
867a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
868667ec068SMatthias Ringwald         int i;
869667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
870a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
871a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
872a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
873a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
874667ec068SMatthias Ringwald                     char buffer[30];
8751599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
876ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
877ff7d6aeaSMatthias Ringwald                              hfp_indicators[i],
878ff7d6aeaSMatthias Ringwald                              (unsigned int)hfp_indicators_value[i]);
879ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
880a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
881667ec068SMatthias Ringwald                 } else {
88260ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
883667ec068SMatthias Ringwald                 }
884667ec068SMatthias Ringwald                 return;
885667ec068SMatthias Ringwald             }
886667ec068SMatthias Ringwald         }
887667ec068SMatthias Ringwald     }
888667ec068SMatthias Ringwald 
889ce263fc8SMatthias Ringwald     if (done) return;
890ce263fc8SMatthias Ringwald     // deal with disconnect
891a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
892ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
893a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
894a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
895ce263fc8SMatthias Ringwald             break;
896ce263fc8SMatthias Ringwald 
897ce263fc8SMatthias Ringwald         default:
898ce263fc8SMatthias Ringwald             break;
899ce263fc8SMatthias Ringwald     }
900ce263fc8SMatthias Ringwald }
901ce263fc8SMatthias Ringwald 
902a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
903a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
9046a7f44bdSMilanka Ringwald 
905ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
9067522e673SMatthias Ringwald 
907667ec068SMatthias Ringwald     // restore volume settings
908a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
909a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
910ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
911a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
912a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
913ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
914667ec068SMatthias Ringwald     // enable all indicators
915667ec068SMatthias Ringwald     int i;
916667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
917a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
918a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
919667ec068SMatthias Ringwald     }
920ce263fc8SMatthias Ringwald }
921ce263fc8SMatthias Ringwald 
9221cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
9231cc65c4fSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
9241cc65c4fSMatthias Ringwald 		// Codec supported, confirm
9251cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
9261cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
9271cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
9281cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
9291cc65c4fSMatthias Ringwald 
9301cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
9311cc65c4fSMatthias Ringwald 	} else {
9321cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
9331cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
9341cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
9351cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
9361cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9371cc65c4fSMatthias Ringwald 
9381cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
9391cc65c4fSMatthias Ringwald 	}
9401cc65c4fSMatthias Ringwald }
9411cc65c4fSMatthias Ringwald 
942a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
943a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
944a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
9453deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
946a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
947a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
9483deb3ec6SMatthias Ringwald                 break;
9493deb3ec6SMatthias Ringwald             }
950a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9513deb3ec6SMatthias Ringwald             break;
9523deb3ec6SMatthias Ringwald 
9533deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
954a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9553deb3ec6SMatthias Ringwald             break;
9563deb3ec6SMatthias Ringwald 
9573deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
958a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
9593deb3ec6SMatthias Ringwald             break;
9603deb3ec6SMatthias Ringwald 
9613deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
962a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
9633deb3ec6SMatthias Ringwald             break;
9643deb3ec6SMatthias Ringwald 
9653deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
966a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
967a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
9683deb3ec6SMatthias Ringwald                 break;
9693deb3ec6SMatthias Ringwald             }
970a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
971a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9723deb3ec6SMatthias Ringwald                 break;
9733deb3ec6SMatthias Ringwald             }
974a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9753deb3ec6SMatthias Ringwald             break;
9763deb3ec6SMatthias Ringwald 
9773deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
978a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
979a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9803deb3ec6SMatthias Ringwald                 break;
9813deb3ec6SMatthias Ringwald             }
982a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9833deb3ec6SMatthias Ringwald             break;
9843deb3ec6SMatthias Ringwald 
9853deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
986a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
9873deb3ec6SMatthias Ringwald             break;
9883deb3ec6SMatthias Ringwald 
9893deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
990a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9913deb3ec6SMatthias Ringwald             break;
9923deb3ec6SMatthias Ringwald 
9933deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
994a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9953deb3ec6SMatthias Ringwald             break;
996ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
997a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
998a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
999ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1000ce263fc8SMatthias Ringwald                 break;
1001ce263fc8SMatthias Ringwald             }
10023deb3ec6SMatthias Ringwald 
1003a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1004a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1005ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1006ce263fc8SMatthias Ringwald                 break;
10073deb3ec6SMatthias Ringwald             }
10083deb3ec6SMatthias Ringwald 
1009a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
1010ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1011a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1012ce263fc8SMatthias Ringwald                     break;
1013ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1014a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1015*a473a009SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_connection);
1016ce263fc8SMatthias Ringwald                     break;
1017ce263fc8SMatthias Ringwald                 default:
1018ce263fc8SMatthias Ringwald                     break;
10193deb3ec6SMatthias Ringwald             }
1020ce263fc8SMatthias Ringwald 
1021a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
1022a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
1023ce263fc8SMatthias Ringwald                 break;
10243deb3ec6SMatthias Ringwald             }
10253deb3ec6SMatthias Ringwald 
1026a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
1027aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1028a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10293deb3ec6SMatthias Ringwald                     break;
1030ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
1031a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1032ce263fc8SMatthias Ringwald                     break;
10333deb3ec6SMatthias Ringwald                 default:
10343deb3ec6SMatthias Ringwald                     break;
10353deb3ec6SMatthias Ringwald             }
10363deb3ec6SMatthias Ringwald             break;
10373deb3ec6SMatthias Ringwald         default:
10383deb3ec6SMatthias Ringwald             break;
10393deb3ec6SMatthias Ringwald     }
10403deb3ec6SMatthias Ringwald 
10413deb3ec6SMatthias Ringwald     // done
1042a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
10433deb3ec6SMatthias Ringwald }
10443deb3ec6SMatthias Ringwald 
1045b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
10464562e2a2SMatthias Ringwald     uint16_t i;
10474562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
10484562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
10494562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
10504562e2a2SMatthias Ringwald                 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
10514562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
10524562e2a2SMatthias Ringwald                 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
10534562e2a2SMatthias Ringwald                 // avoid set but not used warning
10544562e2a2SMatthias Ringwald                 (void) hfp_callheld_status;
10554562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
10564562e2a2SMatthias Ringwald                 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
10574562e2a2SMatthias Ringwald             }
10584562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1059*a473a009SMatthias Ringwald             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
10604562e2a2SMatthias Ringwald             break;
10614562e2a2SMatthias Ringwald         }
10624562e2a2SMatthias Ringwald     }
10634562e2a2SMatthias Ringwald }
10644562e2a2SMatthias Ringwald 
1065426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1066186dd3d2SMatthias Ringwald     int value;
1067186dd3d2SMatthias Ringwald     int i;
1068a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1069667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1070a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1071*a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1072667ec068SMatthias Ringwald             break;
1073667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1074a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1075ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1076667ec068SMatthias Ringwald             break;
1077667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1078a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1079*a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1080667ec068SMatthias Ringwald             break;
1081ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1082a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10832308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1084667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1085ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1086ce263fc8SMatthias Ringwald             break;
1087ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1088a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10892308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1090667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1091ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1092ce263fc8SMatthias Ringwald             break;
1093ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1094a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1095ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1096a0ffb263SMatthias Ringwald             break;
1097a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1098a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1099*a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1100a0ffb263SMatthias Ringwald             break;
1101a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1102a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1103*a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1104ce263fc8SMatthias Ringwald             break;
1105ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1106a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11075a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1108a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1109ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1110ce263fc8SMatthias Ringwald             break;
1111ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
11125a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1113a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1114a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
111590244c92SMilanka Ringwald             switch (hfp_connection->state){
111690244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
111790244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
111890244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
111990244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
112090244c92SMilanka Ringwald                             return;
112190244c92SMilanka Ringwald                         default:
112290244c92SMilanka Ringwald                             break;
112390244c92SMilanka Ringwald                     }
112490244c92SMilanka Ringwald                 default:
112590244c92SMilanka Ringwald                     break;
112690244c92SMilanka Ringwald             }
1127ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1128ce263fc8SMatthias Ringwald             break;
1129ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1130a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1131ce263fc8SMatthias Ringwald             break;
1132ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
11335a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1134ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1135ce263fc8SMatthias Ringwald             break;
1136ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
11375a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11384562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1139ce263fc8SMatthias Ringwald             break;
1140c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
11415a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1142c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1143*a473a009SMatthias Ringwald                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1144c741b032SMilanka Ringwald             }
1145c741b032SMilanka Ringwald             break;
11461cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
11471cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11485a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
11491cc65c4fSMatthias Ringwald 			break;
1150ce263fc8SMatthias Ringwald         default:
1151ce263fc8SMatthias Ringwald             break;
11523deb3ec6SMatthias Ringwald     }
11530cef86faSMatthias Ringwald }
1154426f9988SMatthias Ringwald 
115576cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
115676cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
115776cc1527SMatthias Ringwald }
115876cc1527SMatthias Ringwald 
1159426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1160426f9988SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
1161426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1162426f9988SMatthias Ringwald     if (size < 1) return;
1163426f9988SMatthias Ringwald 
1164426f9988SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1165426f9988SMatthias Ringwald     if (!hfp_connection) return;
1166426f9988SMatthias Ringwald 
1167426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1168e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1169e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1170e43d1938SMatthias Ringwald #endif
1171426f9988SMatthias Ringwald 
1172426f9988SMatthias Ringwald     // process messages byte-wise
1173426f9988SMatthias Ringwald     int pos;
1174426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1175426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
1176426f9988SMatthias Ringwald 
11771599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1178426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1179426f9988SMatthias Ringwald 
1180426f9988SMatthias Ringwald         hfp_hf_handle_rfcomm_command(hfp_connection);
1181426f9988SMatthias Ringwald     }
11821c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
11833deb3ec6SMatthias Ringwald }
11843deb3ec6SMatthias Ringwald 
11851c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1186665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1187665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1188665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1189a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
119022387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
11911c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
11923deb3ec6SMatthias Ringwald     }
11933deb3ec6SMatthias Ringwald }
11943deb3ec6SMatthias Ringwald 
11951c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
11963deb3ec6SMatthias Ringwald     switch (packet_type){
11973deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1198426f9988SMatthias Ringwald             hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size);
11993deb3ec6SMatthias Ringwald             break;
12003deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1201d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1202d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
12031c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1204d4dd47ffSMatthias Ringwald                 return;
1205d4dd47ffSMatthias Ringwald             }
120627950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1207202c8a4cSMatthias Ringwald             break;
12083deb3ec6SMatthias Ringwald         default:
12093deb3ec6SMatthias Ringwald             break;
12103deb3ec6SMatthias Ringwald     }
12111c6a0fc0SMatthias Ringwald     hfp_hf_run();
12123deb3ec6SMatthias Ringwald }
12133deb3ec6SMatthias Ringwald 
12141c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1215405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
12161c6a0fc0SMatthias Ringwald     hfp_hf_run();
1217405014fbSMatthias Ringwald }
1218405014fbSMatthias Ringwald 
1219a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1220520c92d5SMatthias Ringwald     hfp_init();
122120b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
122220b2edb6SMatthias Ringwald     hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
122320b2edb6SMatthias Ringwald     hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
122420b2edb6SMatthias Ringwald     hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
122520b2edb6SMatthias Ringwald     hfp_codecs_nr = 0;
122620b2edb6SMatthias Ringwald     hfp_hf_speaker_gain = 9;
122720b2edb6SMatthias Ringwald     hfp_hf_microphone_gain = 9;
122820b2edb6SMatthias Ringwald     hfp_indicators_nr = 0;
122920b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1230d63c37a1SMatthias Ringwald 
12311c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
12321c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
123327950165SMatthias Ringwald 
12341c6a0fc0SMatthias Ringwald     rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
123527950165SMatthias Ringwald 
123627950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
12371c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
123820b2edb6SMatthias Ringwald }
123927950165SMatthias Ringwald 
124020b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
124120b2edb6SMatthias Ringwald     hfp_deinit();
124220b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
124320b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t));
124420b2edb6SMatthias Ringwald     (void) memset(phone_number, 0, sizeof(phone_number));
1245a0ffb263SMatthias Ringwald }
1246a0ffb263SMatthias Ringwald 
12477ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
12483deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1249a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
12503deb3ec6SMatthias Ringwald         return;
12513deb3ec6SMatthias Ringwald     }
12523deb3ec6SMatthias Ringwald 
12533deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
12543deb3ec6SMatthias Ringwald     int i;
12553deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
12563deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
12573deb3ec6SMatthias Ringwald     }
12583deb3ec6SMatthias Ringwald }
12593deb3ec6SMatthias Ringwald 
1260a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
12613deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1262a0ffb263SMatthias Ringwald }
12633deb3ec6SMatthias Ringwald 
12647ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
12653deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
12663deb3ec6SMatthias Ringwald     int i;
1267a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
12683deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
12693deb3ec6SMatthias Ringwald     }
12703deb3ec6SMatthias Ringwald }
12713deb3ec6SMatthias Ringwald 
12723deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1273323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
12743deb3ec6SMatthias Ringwald }
12753deb3ec6SMatthias Ringwald 
1276c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
12779c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1278a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1279a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1280a33eb0c4SMilanka Ringwald         return;
1281a33eb0c4SMilanka Ringwald     }
1282a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
12831c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
12843deb3ec6SMatthias Ringwald }
12853deb3ec6SMatthias Ringwald 
1286c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
12879c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1288a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1289a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12903deb3ec6SMatthias Ringwald         return;
12913deb3ec6SMatthias Ringwald     }
1292a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
12931c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
12943deb3ec6SMatthias Ringwald }
12953deb3ec6SMatthias Ringwald 
1296c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1297c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1298ce263fc8SMatthias Ringwald }
1299ce263fc8SMatthias Ringwald 
1300c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1301c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1302ce263fc8SMatthias Ringwald }
1303ce263fc8SMatthias Ringwald 
13043deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1305c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
13069c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1307a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1308a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
13093deb3ec6SMatthias Ringwald         return;
13103deb3ec6SMatthias Ringwald     }
1311a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1312a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
13131c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13143deb3ec6SMatthias Ringwald }
13153deb3ec6SMatthias Ringwald 
1316c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
13179c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1318a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1319a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
13203deb3ec6SMatthias Ringwald         return;
13213deb3ec6SMatthias Ringwald     }
1322a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1323ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1324a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1325ce263fc8SMatthias Ringwald             break;
1326ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1327a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1328ce263fc8SMatthias Ringwald             break;
1329ce263fc8SMatthias Ringwald         default:
1330ce263fc8SMatthias Ringwald             break;
1331ce263fc8SMatthias Ringwald     }
13321c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13333deb3ec6SMatthias Ringwald }
13343deb3ec6SMatthias Ringwald 
1335c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
13369c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1337a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1338a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
13393deb3ec6SMatthias Ringwald         return;
13403deb3ec6SMatthias Ringwald     }
1341a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
13421c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13433deb3ec6SMatthias Ringwald }
13443deb3ec6SMatthias Ringwald 
1345ce263fc8SMatthias Ringwald 
1346c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1347c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1348ce263fc8SMatthias Ringwald }
1349ce263fc8SMatthias Ringwald 
1350c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1351c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1352ce263fc8SMatthias Ringwald }
1353ce263fc8SMatthias Ringwald 
135438200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
135538200c1dSMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
135638200c1dSMilanka Ringwald }
1357ce263fc8SMatthias Ringwald 
1358c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
13599c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1360a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1361a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1362a33eb0c4SMilanka Ringwald         return;
1363a33eb0c4SMilanka Ringwald     }
1364a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1365ce263fc8SMatthias Ringwald 
1366a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1367a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
13683deb3ec6SMatthias Ringwald 
136938200c1dSMilanka Ringwald     hfp_connection->trigger_codec_exchange = 0;
137038200c1dSMilanka Ringwald     hfp_connection->establish_audio_connection = 1;
1371a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1372332ca98fSMatthias Ringwald         log_info("no codec negotiation feature, using NBS");
1373a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
137438200c1dSMilanka Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
137538200c1dSMilanka Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
137638200c1dSMilanka Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
137738200c1dSMilanka Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
137838200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1379ce263fc8SMatthias Ringwald     } else {
1380a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1381aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1382aa4dd815SMatthias Ringwald                 break;
1383aa4dd815SMatthias Ringwald             default:
13841cc65c4fSMatthias Ringwald 				hfp_connection->codec_confirmed = 0;
13851cc65c4fSMatthias Ringwald 				hfp_connection->suggested_codec = 0;
13861cc65c4fSMatthias Ringwald 				hfp_connection->negotiated_codec = 0;
13871cc65c4fSMatthias Ringwald 				hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
138838200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1389aa4dd815SMatthias Ringwald                 break;
13903deb3ec6SMatthias Ringwald         }
1391ce263fc8SMatthias Ringwald     }
1392ce263fc8SMatthias Ringwald 
13931c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13943deb3ec6SMatthias Ringwald }
13953deb3ec6SMatthias Ringwald 
1396c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
13979c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1398a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1399a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1400a33eb0c4SMilanka Ringwald         return;
1401a33eb0c4SMilanka Ringwald     }
1402a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
14031c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14043deb3ec6SMatthias Ringwald }
14053deb3ec6SMatthias Ringwald 
1406c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
14079c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1408a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1409a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1410a33eb0c4SMilanka Ringwald         return;
1411a33eb0c4SMilanka Ringwald     }
1412ce263fc8SMatthias Ringwald 
1413ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1414a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
14151c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1416ce263fc8SMatthias Ringwald     } else {
1417ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1418ce263fc8SMatthias Ringwald     }
1419ce263fc8SMatthias Ringwald }
1420ce263fc8SMatthias Ringwald 
1421c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
14229c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1423a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1424a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1425a33eb0c4SMilanka Ringwald         return;
1426a33eb0c4SMilanka Ringwald     }
1427a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
14281c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1429ce263fc8SMatthias Ringwald }
1430ce263fc8SMatthias Ringwald 
1431c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
14329c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1433a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1434a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1435a33eb0c4SMilanka Ringwald         return;
1436a33eb0c4SMilanka Ringwald     }
1437ce263fc8SMatthias Ringwald 
1438ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1439a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
14401c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1441ce263fc8SMatthias Ringwald     }
1442ce263fc8SMatthias Ringwald }
1443ce263fc8SMatthias Ringwald 
1444c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){
14459c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1446a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1447a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1448a33eb0c4SMilanka Ringwald         return;
1449a33eb0c4SMilanka Ringwald     }
1450ce263fc8SMatthias Ringwald 
1451ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1452a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
14531c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1454ce263fc8SMatthias Ringwald     }
1455ce263fc8SMatthias Ringwald }
1456ce263fc8SMatthias Ringwald 
1457c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
14589c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1459a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1460a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1461a33eb0c4SMilanka Ringwald         return;
1462a33eb0c4SMilanka Ringwald     }
1463ce263fc8SMatthias Ringwald 
1464505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1465505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1466a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
14671c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1468ce263fc8SMatthias Ringwald     }
1469ce263fc8SMatthias Ringwald }
1470ce263fc8SMatthias Ringwald 
1471c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){
14729c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1473a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1474a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1475a33eb0c4SMilanka Ringwald         return;
1476a33eb0c4SMilanka Ringwald     }
1477ce263fc8SMatthias Ringwald 
1478505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1479505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1480a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
14811c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1482ce263fc8SMatthias Ringwald     }
1483ce263fc8SMatthias Ringwald }
1484ce263fc8SMatthias Ringwald 
1485c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
14869c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1487a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1488a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1489a33eb0c4SMilanka Ringwald         return;
1490a33eb0c4SMilanka Ringwald     }
1491ce263fc8SMatthias Ringwald 
1492505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1493505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1494a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
14951c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1496ce263fc8SMatthias Ringwald     }
1497ce263fc8SMatthias Ringwald }
1498ce263fc8SMatthias Ringwald 
1499c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
15009c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1501a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1502a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1503a33eb0c4SMilanka Ringwald         return;
1504a33eb0c4SMilanka Ringwald     }
1505ce263fc8SMatthias Ringwald 
1506505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1507505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1508a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
15091c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1510ce263fc8SMatthias Ringwald     }
1511ce263fc8SMatthias Ringwald }
1512ce263fc8SMatthias Ringwald 
1513c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
15149c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1515a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1516a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1517a33eb0c4SMilanka Ringwald         return;
1518a33eb0c4SMilanka Ringwald     }
1519667ec068SMatthias Ringwald 
1520505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1521505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1522a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1523a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
15241c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1525667ec068SMatthias Ringwald     }
1526667ec068SMatthias Ringwald }
1527667ec068SMatthias Ringwald 
1528c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
15299c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1530a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1531a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1532a33eb0c4SMilanka Ringwald         return;
1533a33eb0c4SMilanka Ringwald     }
1534667ec068SMatthias Ringwald 
1535505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1536505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1537a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1538a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
15391c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1540667ec068SMatthias Ringwald     }
1541667ec068SMatthias Ringwald }
1542ce263fc8SMatthias Ringwald 
1543c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
15449c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1545a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1546a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1547a33eb0c4SMilanka Ringwald         return;
1548a33eb0c4SMilanka Ringwald     }
1549ce263fc8SMatthias Ringwald 
1550a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1551ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
15521c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1553ce263fc8SMatthias Ringwald }
1554ce263fc8SMatthias Ringwald 
1555c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
15569c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1557a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1558a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1559a33eb0c4SMilanka Ringwald         return;
1560a33eb0c4SMilanka Ringwald     }
1561ce263fc8SMatthias Ringwald 
1562a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1563a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1564a0ffb263SMatthias Ringwald 
15651c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1566ce263fc8SMatthias Ringwald }
1567ce263fc8SMatthias Ringwald 
1568c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
15699c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1570a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1571a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1572a33eb0c4SMilanka Ringwald         return;
1573a33eb0c4SMilanka Ringwald     }
1574ce263fc8SMatthias Ringwald 
1575a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
15761c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1577ce263fc8SMatthias Ringwald }
1578ce263fc8SMatthias Ringwald 
1579c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
15809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1581a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1582a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1583a33eb0c4SMilanka Ringwald         return;
1584a33eb0c4SMilanka Ringwald     }
1585ce263fc8SMatthias Ringwald 
1586a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
15871c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1588ce263fc8SMatthias Ringwald }
1589ce263fc8SMatthias Ringwald 
1590ce263fc8SMatthias Ringwald 
1591c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
15929c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1593a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1594a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1595a33eb0c4SMilanka Ringwald         return;
1596a33eb0c4SMilanka Ringwald     }
1597ce263fc8SMatthias Ringwald 
1598a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
15991c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1600ce263fc8SMatthias Ringwald }
1601ce263fc8SMatthias Ringwald 
1602ce263fc8SMatthias Ringwald 
1603c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(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_activate_calling_line_notification = 1;
16111c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1612ce263fc8SMatthias Ringwald }
1613ce263fc8SMatthias Ringwald 
1614c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_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_deactivate_calling_line_notification = 1;
16221c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1623ce263fc8SMatthias Ringwald }
1624ce263fc8SMatthias Ringwald 
1625ce263fc8SMatthias Ringwald 
1626c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
16279c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1628a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1629a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1630a33eb0c4SMilanka Ringwald         return;
1631a33eb0c4SMilanka Ringwald     }
1632ce263fc8SMatthias Ringwald 
1633a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
16341c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1635ce263fc8SMatthias Ringwald }
1636ce263fc8SMatthias Ringwald 
1637c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
16389c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1639a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1640a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1641a33eb0c4SMilanka Ringwald         return;
1642a33eb0c4SMilanka Ringwald     }
1643ce263fc8SMatthias Ringwald 
1644a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
16451c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1646ce263fc8SMatthias Ringwald }
1647ce263fc8SMatthias Ringwald 
1648c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
16499c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1650a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1651a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1652a33eb0c4SMilanka Ringwald         return;
1653a33eb0c4SMilanka Ringwald     }
1654ce263fc8SMatthias Ringwald 
1655a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
16561c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1657ce263fc8SMatthias Ringwald }
1658ce263fc8SMatthias Ringwald 
1659c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
16609c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1661a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1662a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1663a33eb0c4SMilanka Ringwald         return;
1664a33eb0c4SMilanka Ringwald     }
1665ce263fc8SMatthias Ringwald 
1666a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
16671c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1668ce263fc8SMatthias Ringwald }
1669ce263fc8SMatthias Ringwald 
1670c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
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     }
1676c8626498SMilanka Ringwald 
1677a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1678c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1679a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1680a0ffb263SMatthias Ringwald         return;
1681a0ffb263SMatthias Ringwald     }
1682a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1683a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
16841c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1685ce263fc8SMatthias Ringwald }
1686ce263fc8SMatthias Ringwald 
1687c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
16889c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1689a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1690a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1691a33eb0c4SMilanka Ringwald         return;
1692a33eb0c4SMilanka Ringwald     }
1693c8626498SMilanka Ringwald 
1694a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1695c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1696a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1697a0ffb263SMatthias Ringwald         return;
1698a0ffb263SMatthias Ringwald     }
1699a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1700a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
17011c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1702ce263fc8SMatthias Ringwald }
1703ce263fc8SMatthias Ringwald 
1704c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
17059c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1706a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1707a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1708a33eb0c4SMilanka Ringwald         return;
1709a33eb0c4SMilanka Ringwald     }
1710a33eb0c4SMilanka Ringwald 
1711a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
17121c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1713ce263fc8SMatthias Ringwald }
1714ce263fc8SMatthias Ringwald 
1715c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
17169c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1717a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1718a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1719a33eb0c4SMilanka Ringwald         return;
1720a33eb0c4SMilanka Ringwald     }
1721a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
17221c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1723ce263fc8SMatthias Ringwald }
17243deb3ec6SMatthias Ringwald 
1725c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
17269c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1727a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1728a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1729a33eb0c4SMilanka Ringwald         return;
1730a33eb0c4SMilanka Ringwald     }
1731a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
17321c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1733667ec068SMatthias Ringwald }
1734667ec068SMatthias Ringwald 
1735667ec068SMatthias Ringwald 
1736c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
17379c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1738a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1739a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1740a33eb0c4SMilanka Ringwald         return;
1741a33eb0c4SMilanka Ringwald     }
1742a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1743a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
17441c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1745667ec068SMatthias Ringwald }
1746667ec068SMatthias Ringwald 
1747c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
17489c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1749a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1750a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1751a33eb0c4SMilanka Ringwald         return;
1752a33eb0c4SMilanka Ringwald     }
1753a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1754a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
17551c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1756667ec068SMatthias Ringwald }
1757667ec068SMatthias Ringwald 
1758c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
17599c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1760a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1761a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1762a33eb0c4SMilanka Ringwald         return;
1763a33eb0c4SMilanka Ringwald     }
1764a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1765a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
17661c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1767667ec068SMatthias Ringwald }
1768667ec068SMatthias Ringwald 
1769c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
17709c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1771a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1772a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1773a33eb0c4SMilanka Ringwald         return;
1774a33eb0c4SMilanka Ringwald     }
1775a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1776a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
17771c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1778667ec068SMatthias Ringwald }
1779667ec068SMatthias Ringwald 
1780c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
17819c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1782a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1783a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1784a33eb0c4SMilanka Ringwald         return;
1785a33eb0c4SMilanka Ringwald     }
1786a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
17871c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1788667ec068SMatthias Ringwald }
1789667ec068SMatthias Ringwald 
1790c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
17919c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1792a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1793a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1794a33eb0c4SMilanka Ringwald         return;
1795a33eb0c4SMilanka Ringwald     }
1796667ec068SMatthias Ringwald     // find index for assigned number
1797667ec068SMatthias Ringwald     int i;
1798667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1799667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1800667ec068SMatthias Ringwald             // set value
1801667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1802667ec068SMatthias Ringwald             // mark for update
1803a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1804a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1805667ec068SMatthias Ringwald                 // send update
18061c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
1807a0ffb263SMatthias Ringwald             }
1808667ec068SMatthias Ringwald             return;
1809667ec068SMatthias Ringwald         }
1810667ec068SMatthias Ringwald     }
1811667ec068SMatthias Ringwald }
1812667ec068SMatthias Ringwald 
1813d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
1814d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1815d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
1816d7f6b5cbSMatthias Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1817d7f6b5cbSMatthias Ringwald         return 0;
1818d7f6b5cbSMatthias Ringwald     }
1819d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1820d7f6b5cbSMatthias Ringwald }
182176cc1527SMatthias Ringwald 
182276cc1527SMatthias 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){
182376cc1527SMatthias Ringwald 	if (!name){
182476cc1527SMatthias Ringwald 		name = default_hfp_hf_service_name;
182576cc1527SMatthias Ringwald 	}
182676cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
182776cc1527SMatthias Ringwald 
182876cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
182976cc1527SMatthias Ringwald 	//
183076cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
183176cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
183276cc1527SMatthias Ringwald 	//
183376cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
183476cc1527SMatthias Ringwald 	//
183576cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
183676cc1527SMatthias Ringwald 	if (wide_band_speech && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
183776cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
183876cc1527SMatthias Ringwald 	}
183976cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
184076cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
184176cc1527SMatthias Ringwald }
184276cc1527SMatthias Ringwald 
184376cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
184476cc1527SMatthias Ringwald 	if (callback == NULL){
184576cc1527SMatthias Ringwald 		log_error("hfp_hf_register_packet_handler called with NULL callback");
184676cc1527SMatthias Ringwald 		return;
184776cc1527SMatthias Ringwald 	}
184876cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
184976cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
185076cc1527SMatthias Ringwald }
1851