xref: /btstack/src/classic/hfp_hf.c (revision ec3bfc1a211e0ffc53b5b7b4344f877c4bd498d2)
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 
126a473a009SMatthias Ringwald static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){
127a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
128d703d377SMatthias Ringwald     uint8_t event[33];
129a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
130a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
131a473a009SMatthias Ringwald     event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION;
132d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
133d703d377SMatthias Ringwald     event[5] = status;
134d703d377SMatthias Ringwald     event[6] = hfp_connection->bnip_type;
135d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 8);
136d703d377SMatthias Ringwald     strncpy((char*)&event[7], hfp_connection->bnip_number, size);
137d703d377SMatthias Ringwald     event[7 + size] = 0;
138a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
139a0ffb263SMatthias Ringwald }
140a0ffb263SMatthias Ringwald 
141a473a009SMatthias Ringwald static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){
142a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
143d703d377SMatthias Ringwald     uint8_t event[32];
144a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
145a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
146a0ffb263SMatthias Ringwald     event[2] = event_subtype;
147d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
148d703d377SMatthias Ringwald     event[5] = hfp_connection->bnip_type;
149d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 7);
150d703d377SMatthias Ringwald     strncpy((char*)&event[6], hfp_connection->bnip_number, size);
151d703d377SMatthias Ringwald     event[6 + size] = 0;
152a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
153a0ffb263SMatthias Ringwald }
154a0ffb263SMatthias Ringwald 
155a473a009SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){
156a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
157d703d377SMatthias Ringwald     uint8_t event[38];
1580aee97efSMilanka Ringwald     int pos = 0;
1590aee97efSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
1600aee97efSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
1610aee97efSMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
162d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
163d703d377SMatthias Ringwald     pos += 2;
164a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_idx;
165a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_dir;
166a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_status;
167a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mode;
168a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mpty;
169a473a009SMatthias Ringwald     event[pos++] = hfp_connection->bnip_type;
170d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - pos - 1);
171a473a009SMatthias Ringwald     strncpy((char*)&event[pos], hfp_connection->bnip_number, size);
1720aee97efSMilanka Ringwald     pos += size;
1730aee97efSMilanka Ringwald     event[pos++] = 0;
174a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
175a0ffb263SMatthias Ringwald }
176a0ffb263SMatthias Ringwald 
17776cc1527SMatthias Ringwald 
178a473a009SMatthias Ringwald static void hfp_emit_ag_indicator_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
179a473a009SMatthias Ringwald 	if (hfp_hf_callback == NULL) return;
180d703d377SMatthias Ringwald 	uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE+1];
18176cc1527SMatthias Ringwald 	int pos = 0;
18276cc1527SMatthias Ringwald 	event[pos++] = HCI_EVENT_HFP_META;
18376cc1527SMatthias Ringwald 	event[pos++] = sizeof(event) - 2;
18476cc1527SMatthias Ringwald 	event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
185d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
186d703d377SMatthias Ringwald     pos += 2;
187a473a009SMatthias Ringwald 	event[pos++] = indicator->index;
188a473a009SMatthias Ringwald 	event[pos++] = indicator->status;
189a473a009SMatthias Ringwald 	event[pos++] = indicator->min_range;
190a473a009SMatthias Ringwald 	event[pos++] = indicator->max_range;
191a473a009SMatthias Ringwald 	event[pos++] = indicator->mandatory;
192a473a009SMatthias Ringwald 	event[pos++] = indicator->enabled;
193a473a009SMatthias Ringwald 	event[pos++] = indicator->status_changed;
194a473a009SMatthias Ringwald 	strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
19576cc1527SMatthias Ringwald 	pos += HFP_MAX_INDICATOR_DESC_SIZE;
19676cc1527SMatthias Ringwald 	event[pos] = 0;
197a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1983deb3ec6SMatthias Ringwald }
1993deb3ec6SMatthias Ringwald 
200a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){
201a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
202d703d377SMatthias Ringwald 	uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
20376cc1527SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
20476cc1527SMatthias Ringwald 	event[1] = sizeof(event) - 2;
20576cc1527SMatthias Ringwald 	event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
206d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
207d703d377SMatthias Ringwald 	event[5] = hfp_connection->network_operator.mode;
208d703d377SMatthias Ringwald 	event[6] = hfp_connection->network_operator.format;
209d703d377SMatthias Ringwald 	strncpy((char*)&event[7], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
210d703d377SMatthias Ringwald 	event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
211a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2123deb3ec6SMatthias Ringwald }
2133deb3ec6SMatthias Ringwald 
21476cc1527SMatthias Ringwald /* send commands */
21589425bfcSMilanka Ringwald 
21689425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2173deb3ec6SMatthias Ringwald     char buffer[20];
2181599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
21989425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
22089425bfcSMilanka Ringwald }
22189425bfcSMilanka Ringwald 
22289425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
22389425bfcSMilanka Ringwald     char buffer[20];
2241599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
22589425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
22689425bfcSMilanka Ringwald }
22789425bfcSMilanka Ringwald 
22886da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
22989425bfcSMilanka Ringwald     char buffer[40];
2301599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
2313deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2323deb3ec6SMatthias Ringwald }
2333deb3ec6SMatthias Ringwald 
2343deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2353deb3ec6SMatthias Ringwald     char buffer[30];
23689425bfcSMilanka Ringwald     const int size = sizeof(buffer);
23789425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
23889425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
2391599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2403deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2413deb3ec6SMatthias Ringwald }
2423deb3ec6SMatthias Ringwald 
2433deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2443deb3ec6SMatthias Ringwald     char buffer[50];
24589425bfcSMilanka Ringwald     const int size = sizeof(buffer);
24689425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
24789425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
2481599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2493deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2503deb3ec6SMatthias Ringwald }
2513deb3ec6SMatthias Ringwald 
2523deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2533deb3ec6SMatthias Ringwald     char buffer[30];
25489425bfcSMilanka Ringwald     const int size = sizeof(buffer);
25589425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
25689425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
2571599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2583deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2593deb3ec6SMatthias Ringwald }
2603deb3ec6SMatthias Ringwald 
26189425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2623deb3ec6SMatthias Ringwald     char buffer[20];
2631599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
264ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
265ce263fc8SMatthias Ringwald }
266ce263fc8SMatthias Ringwald 
267ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
268ce263fc8SMatthias Ringwald     char buffer[40];
2691599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, phone_number);
270ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
271ce263fc8SMatthias Ringwald }
272ce263fc8SMatthias Ringwald 
273a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
274ce263fc8SMatthias Ringwald     char buffer[40];
2751599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
276ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
277ce263fc8SMatthias Ringwald }
278ce263fc8SMatthias Ringwald 
279f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
28089425bfcSMilanka Ringwald     char buffer[40];
2811599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
282ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
283ce263fc8SMatthias Ringwald }
284ce263fc8SMatthias Ringwald 
285ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
286ce263fc8SMatthias Ringwald     char buffer[20];
2871599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
288ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
289ce263fc8SMatthias Ringwald }
290ce263fc8SMatthias Ringwald 
29197d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
2921599fe57SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "ATA\r");
29397d2cadbSMatthias Ringwald }
29497d2cadbSMatthias Ringwald 
29589425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
29689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
29789425bfcSMilanka Ringwald }
29889425bfcSMilanka Ringwald 
29989425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
30089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
30189425bfcSMilanka Ringwald }
30289425bfcSMilanka Ringwald 
30389425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
30489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
30589425bfcSMilanka Ringwald }
30689425bfcSMilanka Ringwald 
30789425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
30889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
30989425bfcSMilanka Ringwald }
31089425bfcSMilanka Ringwald 
31189425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
31289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
31389425bfcSMilanka Ringwald }
31489425bfcSMilanka Ringwald 
31589425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
31689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
31789425bfcSMilanka Ringwald }
31889425bfcSMilanka Ringwald 
31989425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
32089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
32189425bfcSMilanka Ringwald }
32289425bfcSMilanka Ringwald 
32389425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
32489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
32589425bfcSMilanka Ringwald }
32689425bfcSMilanka Ringwald 
32789425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
32889425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
32989425bfcSMilanka Ringwald }
33089425bfcSMilanka Ringwald 
33189425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
33289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
33389425bfcSMilanka Ringwald }
33489425bfcSMilanka Ringwald 
33589425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
33689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
33789425bfcSMilanka Ringwald }
33889425bfcSMilanka Ringwald 
33989425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
34089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
34189425bfcSMilanka Ringwald }
34289425bfcSMilanka Ringwald 
34389425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
34489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
34589425bfcSMilanka Ringwald }
34689425bfcSMilanka Ringwald 
34789425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
34889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
34989425bfcSMilanka Ringwald }
35089425bfcSMilanka Ringwald 
35189425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
35289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
35389425bfcSMilanka Ringwald }
35489425bfcSMilanka Ringwald 
35589425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
35689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
35789425bfcSMilanka Ringwald }
35889425bfcSMilanka Ringwald 
35989425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
36089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
36189425bfcSMilanka Ringwald }
36289425bfcSMilanka Ringwald 
36389425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
36489425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
36589425bfcSMilanka Ringwald }
36689425bfcSMilanka Ringwald 
36789425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
36889425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
36989425bfcSMilanka Ringwald }
37089425bfcSMilanka Ringwald 
371ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
37289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
373ce263fc8SMatthias Ringwald }
374ce263fc8SMatthias Ringwald 
375667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
37689425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
377667ec068SMatthias Ringwald }
378667ec068SMatthias Ringwald 
37976cc1527SMatthias Ringwald /* state machines */
3803deb3ec6SMatthias Ringwald 
381a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
382a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
383a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
384aa4dd815SMatthias Ringwald     int done = 1;
3853deb3ec6SMatthias Ringwald 
386a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
3873deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
388d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
389a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
390a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
3913deb3ec6SMatthias Ringwald             break;
3923deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
393a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
394a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
3953deb3ec6SMatthias Ringwald             break;
3963deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
397a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
398a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
3993deb3ec6SMatthias Ringwald             break;
4003deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
401a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
402a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4033deb3ec6SMatthias Ringwald             break;
4043deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
405a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
406a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4073deb3ec6SMatthias Ringwald             break;
4083deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
409a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
410a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4113deb3ec6SMatthias Ringwald             break;
4123deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
413a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
414a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4153deb3ec6SMatthias Ringwald             break;
4163deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
417a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
418a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4193deb3ec6SMatthias Ringwald             break;
4203deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
421a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
422a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4233deb3ec6SMatthias Ringwald             break;
4243deb3ec6SMatthias Ringwald         default:
425aa4dd815SMatthias Ringwald             done = 0;
4263deb3ec6SMatthias Ringwald             break;
4273deb3ec6SMatthias Ringwald     }
4283deb3ec6SMatthias Ringwald     return done;
4293deb3ec6SMatthias Ringwald }
4303deb3ec6SMatthias Ringwald 
431ce263fc8SMatthias Ringwald 
432a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
433a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
434a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
435ce263fc8SMatthias Ringwald 
436ce263fc8SMatthias Ringwald     int done = 0;
437a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
438a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
439ce263fc8SMatthias Ringwald         done = 1;
440a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
441ce263fc8SMatthias Ringwald         return done;
442ce263fc8SMatthias Ringwald     };
443a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
444a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
445ce263fc8SMatthias Ringwald         done = 1;
446a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
447a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
448a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
449ce263fc8SMatthias Ringwald         return done;
450ce263fc8SMatthias Ringwald     }
451ce263fc8SMatthias Ringwald 
452a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
453ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
454a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
455a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
456a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
457ce263fc8SMatthias Ringwald             return 1;
458ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
459a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
460a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
461a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
462ce263fc8SMatthias Ringwald             return 1;
463ce263fc8SMatthias Ringwald         default:
464ce263fc8SMatthias Ringwald             break;
465ce263fc8SMatthias Ringwald     }
466ce263fc8SMatthias Ringwald 
467a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
468a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
469ce263fc8SMatthias Ringwald         done = 1;
470a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
471ce263fc8SMatthias Ringwald         return done;
472ce263fc8SMatthias Ringwald     }
473ce263fc8SMatthias Ringwald 
474ce263fc8SMatthias Ringwald     return done;
475ce263fc8SMatthias Ringwald }
476ce263fc8SMatthias Ringwald 
477a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
478ce263fc8SMatthias Ringwald 
479a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
480ce263fc8SMatthias Ringwald 
481332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
482332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
483ce263fc8SMatthias Ringwald 
484a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
485a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
486332ca98fSMatthias Ringwald 		return 1;
487332ca98fSMatthias Ringwald     }
488332ca98fSMatthias Ringwald 
4891cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
4901cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
491ce263fc8SMatthias Ringwald 
492a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
493fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
4941cc65c4fSMatthias Ringwald 		return 1;
4951cc65c4fSMatthias Ringwald     }
4961cc65c4fSMatthias Ringwald 
4971cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
4981cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
4991cc65c4fSMatthias Ringwald 
500a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
501a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
5021cc65c4fSMatthias Ringwald 		return 1;
5031cc65c4fSMatthias Ringwald     }
504ce263fc8SMatthias Ringwald 
505ce263fc8SMatthias Ringwald     return 0;
506ce263fc8SMatthias Ringwald }
507ce263fc8SMatthias Ringwald 
508a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
509505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
510505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
511ce263fc8SMatthias Ringwald 
51264f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
513a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
514a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
515a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
516ce263fc8SMatthias Ringwald         return 1;
517ce263fc8SMatthias Ringwald     }
518ce263fc8SMatthias Ringwald 
519a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
520ce263fc8SMatthias Ringwald 
521ce263fc8SMatthias Ringwald     // run codecs exchange
522a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
523ce263fc8SMatthias Ringwald     if (done) return 1;
524ce263fc8SMatthias Ringwald 
52538200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
52638200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
52738200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
52838200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
52938200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
53038200c1dSMilanka Ringwald         return 1;
53138200c1dSMilanka Ringwald     }
53238200c1dSMilanka Ringwald 
533ce263fc8SMatthias Ringwald     return 0;
534ce263fc8SMatthias Ringwald }
535ce263fc8SMatthias Ringwald 
53638200c1dSMilanka Ringwald 
537a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
538eaf2b0a1SMatthias Ringwald 
539eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
540eaf2b0a1SMatthias Ringwald 
541a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
542a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
543a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
544ce263fc8SMatthias Ringwald         return 1;
545ce263fc8SMatthias Ringwald     }
546ce263fc8SMatthias Ringwald     return 0;
547ce263fc8SMatthias Ringwald }
548ce263fc8SMatthias Ringwald 
5491c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
5507522e673SMatthias Ringwald 
55176cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
55276cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
55376cc1527SMatthias Ringwald 
55476cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
55576cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
55622387625SMatthias Ringwald 
5573721a235SMatthias Ringwald 	// assert command could be sent
5583721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
5593721a235SMatthias Ringwald 
5603721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
5613721a235SMatthias Ringwald     // WBS Disassociate
5623721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
5633721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
5643721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
5653721a235SMatthias Ringwald         return;
5663721a235SMatthias Ringwald     }
5673721a235SMatthias Ringwald     // Write Codec Config
5683721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
5693721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
5703721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
5713721a235SMatthias Ringwald         return;
5723721a235SMatthias Ringwald     }
5733721a235SMatthias Ringwald     // WBS Associate
5743721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
5753721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
5763721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
5773721a235SMatthias Ringwald         return;
5783721a235SMatthias Ringwald     }
5793721a235SMatthias Ringwald #endif
580689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
581689d4323SMatthias Ringwald     // Enable WBS
582689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
583689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
584689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
585689d4323SMatthias Ringwald         return;
586689d4323SMatthias Ringwald     }
587689d4323SMatthias Ringwald     // Write I2S/PCM params
588689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
589689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
590689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
591689d4323SMatthias Ringwald         return;
592689d4323SMatthias Ringwald     }
593689d4323SMatthias Ringwald     // Disable WBS
594689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
595689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
596689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
597689d4323SMatthias Ringwald         return;
598689d4323SMatthias Ringwald     }
599689d4323SMatthias Ringwald #endif
60048e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
60148e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
60248e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
60348e6eeeeSMatthias Ringwald         return;
60448e6eeeeSMatthias Ringwald     }
60548e6eeeeSMatthias Ringwald #endif
6063721a235SMatthias Ringwald 
607cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
608cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
609cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
6107522e673SMatthias Ringwald         // notify about codec selection if not done already
6117522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
6127522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
6137522e673SMatthias Ringwald         }
614cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
6157522e673SMatthias Ringwald         return;
6167522e673SMatthias Ringwald     }
6177522e673SMatthias Ringwald 
618d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
619d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
620d4dd47ffSMatthias Ringwald         return;
621d4dd47ffSMatthias Ringwald     }
622a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
623ce263fc8SMatthias Ringwald     if (!done){
624a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
625ce263fc8SMatthias Ringwald     }
626ce263fc8SMatthias Ringwald     if (!done){
627a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
628ce263fc8SMatthias Ringwald     }
629ce263fc8SMatthias Ringwald     if (!done){
630a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
631ce263fc8SMatthias Ringwald     }
632ce263fc8SMatthias Ringwald 
6331016a228SMatthias Ringwald     // don't send a new command while ok still pending
6341016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6351016a228SMatthias Ringwald 
636a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
637a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
638a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
639a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
640ce263fc8SMatthias Ringwald         return;
641ce263fc8SMatthias Ringwald     }
642ce263fc8SMatthias Ringwald 
643a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
644a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
645a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
646a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
647ce263fc8SMatthias Ringwald         return;
648ce263fc8SMatthias Ringwald     }
649ce263fc8SMatthias Ringwald 
650a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
651a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
652a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
653a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
654ce263fc8SMatthias Ringwald         return;
655ce263fc8SMatthias Ringwald     }
656ce263fc8SMatthias Ringwald 
657a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
658a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
659a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
660a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
661ce263fc8SMatthias Ringwald         return;
662ce263fc8SMatthias Ringwald     }
663ce263fc8SMatthias Ringwald 
664a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
665a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
666a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
667a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
668ce263fc8SMatthias Ringwald         return;
669ce263fc8SMatthias Ringwald     }
670ce263fc8SMatthias Ringwald 
671a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
672a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
673a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
674a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
675ce263fc8SMatthias Ringwald         return;
676ce263fc8SMatthias Ringwald     }
677ce263fc8SMatthias Ringwald 
678a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
679a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
680a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
681a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
682ce263fc8SMatthias Ringwald         return;
683ce263fc8SMatthias Ringwald     }
684ce263fc8SMatthias Ringwald 
685a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
686a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
687a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
688a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
689ce263fc8SMatthias Ringwald         return;
690ce263fc8SMatthias Ringwald     }
691ce263fc8SMatthias Ringwald 
692ce263fc8SMatthias Ringwald 
693a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
694a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
695a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
696a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
697ce263fc8SMatthias Ringwald         return;
698ce263fc8SMatthias Ringwald     }
699ce263fc8SMatthias Ringwald 
700a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
701a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
702a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
703a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
704ce263fc8SMatthias Ringwald         return;
705ce263fc8SMatthias Ringwald     }
706ce263fc8SMatthias Ringwald 
707a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
708a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
709a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
710a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
711ce263fc8SMatthias Ringwald         return;
712ce263fc8SMatthias Ringwald     }
713ce263fc8SMatthias Ringwald 
714a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
715a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
716a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
717a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
718ce263fc8SMatthias Ringwald         return;
719ce263fc8SMatthias Ringwald     }
720ce263fc8SMatthias Ringwald 
721a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
722a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
723a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
724a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
725ce263fc8SMatthias Ringwald         return;
726ce263fc8SMatthias Ringwald     }
727ce263fc8SMatthias Ringwald 
728a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
729a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
730a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
731a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
732ce263fc8SMatthias Ringwald         return;
733ce263fc8SMatthias Ringwald     }
734ce263fc8SMatthias Ringwald 
735a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
736a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
737a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
738a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
739ce263fc8SMatthias Ringwald         return;
740ce263fc8SMatthias Ringwald     }
741ce263fc8SMatthias Ringwald 
742a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
743a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
744a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
745a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
746ce263fc8SMatthias Ringwald         return;
747ce263fc8SMatthias Ringwald     }
748ce263fc8SMatthias Ringwald 
749a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
750a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
751a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
752a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
753ce263fc8SMatthias Ringwald         return;
754ce263fc8SMatthias Ringwald     }
755ce263fc8SMatthias Ringwald 
756a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
757a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
758a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
759a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
760ce263fc8SMatthias Ringwald         return;
761ce263fc8SMatthias Ringwald     }
762ce263fc8SMatthias Ringwald 
763a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
764a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
765a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
766a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
767ce263fc8SMatthias Ringwald         return;
768ce263fc8SMatthias Ringwald     }
769ce263fc8SMatthias Ringwald 
770a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
771a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
772a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
773a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
774667ec068SMatthias Ringwald         return;
775667ec068SMatthias Ringwald     }
776667ec068SMatthias Ringwald 
777a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
778a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
779a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
780a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
781a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
782ce263fc8SMatthias Ringwald         return;
783ce263fc8SMatthias Ringwald     }
784ce263fc8SMatthias Ringwald 
785a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
786a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
787a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
788a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
789ce263fc8SMatthias Ringwald         return;
790ce263fc8SMatthias Ringwald     }
791ce263fc8SMatthias Ringwald 
792a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
793a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
794a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
795a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
796667ec068SMatthias Ringwald         return;
797667ec068SMatthias Ringwald     }
798667ec068SMatthias Ringwald 
799a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
800a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
801667ec068SMatthias Ringwald         char buffer[20];
802a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
803667ec068SMatthias Ringwald             case '?':
8041599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
805ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
806ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
807a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
808667ec068SMatthias Ringwald                 return;
809667ec068SMatthias Ringwald             case '0':
810667ec068SMatthias Ringwald             case '1':
811667ec068SMatthias Ringwald             case '2':
8121599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
813ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
814ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
815ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
816a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
817667ec068SMatthias Ringwald                 return;
818667ec068SMatthias Ringwald             default:
819667ec068SMatthias Ringwald                 break;
820667ec068SMatthias Ringwald         }
821667ec068SMatthias Ringwald         return;
822667ec068SMatthias Ringwald     }
823667ec068SMatthias Ringwald 
824a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
825a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
826667ec068SMatthias Ringwald         char buffer[20];
8271599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
828ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
829ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
830a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
831667ec068SMatthias Ringwald         return;
832667ec068SMatthias Ringwald     }
833667ec068SMatthias Ringwald 
834667ec068SMatthias Ringwald     // update HF indicators
835a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
836667ec068SMatthias Ringwald         int i;
837667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
838a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
839a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
840a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
841a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
842667ec068SMatthias Ringwald                     char buffer[30];
8431599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
844ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
845ff7d6aeaSMatthias Ringwald                              hfp_indicators[i],
846ff7d6aeaSMatthias Ringwald                              (unsigned int)hfp_indicators_value[i]);
847ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
848a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
849667ec068SMatthias Ringwald                 } else {
85060ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
851667ec068SMatthias Ringwald                 }
852667ec068SMatthias Ringwald                 return;
853667ec068SMatthias Ringwald             }
854667ec068SMatthias Ringwald         }
855667ec068SMatthias Ringwald     }
856667ec068SMatthias Ringwald 
857ce263fc8SMatthias Ringwald     if (done) return;
858ce263fc8SMatthias Ringwald     // deal with disconnect
859a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
860ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
861a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
862a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
863ce263fc8SMatthias Ringwald             break;
864ce263fc8SMatthias Ringwald 
865ce263fc8SMatthias Ringwald         default:
866ce263fc8SMatthias Ringwald             break;
867ce263fc8SMatthias Ringwald     }
868ce263fc8SMatthias Ringwald }
869ce263fc8SMatthias Ringwald 
870a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
871a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
8726a7f44bdSMilanka Ringwald 
873ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
8747522e673SMatthias Ringwald 
875667ec068SMatthias Ringwald     // restore volume settings
876a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
877a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
878ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
879a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
880a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
881ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
882667ec068SMatthias Ringwald     // enable all indicators
883667ec068SMatthias Ringwald     int i;
884667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
885a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
886a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
887667ec068SMatthias Ringwald     }
888ce263fc8SMatthias Ringwald }
889ce263fc8SMatthias Ringwald 
8901cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
8911cc65c4fSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
8921cc65c4fSMatthias Ringwald 		// Codec supported, confirm
8931cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
8941cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
8951cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
8961cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
8971cc65c4fSMatthias Ringwald 
8981cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
8991cc65c4fSMatthias Ringwald 	} else {
9001cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
9011cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
9021cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
9031cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
9041cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9051cc65c4fSMatthias Ringwald 
9061cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
9071cc65c4fSMatthias Ringwald 	}
9081cc65c4fSMatthias Ringwald }
9091cc65c4fSMatthias Ringwald 
910a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
911a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
912a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
9133deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
914a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
915a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
9163deb3ec6SMatthias Ringwald                 break;
9173deb3ec6SMatthias Ringwald             }
918a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9193deb3ec6SMatthias Ringwald             break;
9203deb3ec6SMatthias Ringwald 
9213deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
922a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9233deb3ec6SMatthias Ringwald             break;
9243deb3ec6SMatthias Ringwald 
9253deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
926a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
9273deb3ec6SMatthias Ringwald             break;
9283deb3ec6SMatthias Ringwald 
9293deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
930a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
9313deb3ec6SMatthias Ringwald             break;
9323deb3ec6SMatthias Ringwald 
9333deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
934a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
935a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
9363deb3ec6SMatthias Ringwald                 break;
9373deb3ec6SMatthias Ringwald             }
938a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
939a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9403deb3ec6SMatthias Ringwald                 break;
9413deb3ec6SMatthias Ringwald             }
942a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9433deb3ec6SMatthias Ringwald             break;
9443deb3ec6SMatthias Ringwald 
9453deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
946a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
947a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9483deb3ec6SMatthias Ringwald                 break;
9493deb3ec6SMatthias Ringwald             }
950a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9513deb3ec6SMatthias Ringwald             break;
9523deb3ec6SMatthias Ringwald 
9533deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
954a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
9553deb3ec6SMatthias Ringwald             break;
9563deb3ec6SMatthias Ringwald 
9573deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
958a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9593deb3ec6SMatthias Ringwald             break;
9603deb3ec6SMatthias Ringwald 
9613deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
962a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9633deb3ec6SMatthias Ringwald             break;
964ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
965a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
966a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
967ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
968ce263fc8SMatthias Ringwald                 break;
969ce263fc8SMatthias Ringwald             }
9703deb3ec6SMatthias Ringwald 
971a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
972a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
973ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
974ce263fc8SMatthias Ringwald                 break;
9753deb3ec6SMatthias Ringwald             }
9763deb3ec6SMatthias Ringwald 
977a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
978ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
979a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
980ce263fc8SMatthias Ringwald                     break;
981ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
982a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
983a473a009SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_connection);
984ce263fc8SMatthias Ringwald                     break;
985ce263fc8SMatthias Ringwald                 default:
986ce263fc8SMatthias Ringwald                     break;
9873deb3ec6SMatthias Ringwald             }
988ce263fc8SMatthias Ringwald 
989a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
990a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
991ce263fc8SMatthias Ringwald                 break;
9923deb3ec6SMatthias Ringwald             }
9933deb3ec6SMatthias Ringwald 
994a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
995aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
996a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9973deb3ec6SMatthias Ringwald                     break;
998ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
999a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1000ce263fc8SMatthias Ringwald                     break;
10013deb3ec6SMatthias Ringwald                 default:
10023deb3ec6SMatthias Ringwald                     break;
10033deb3ec6SMatthias Ringwald             }
10043deb3ec6SMatthias Ringwald             break;
10053deb3ec6SMatthias Ringwald         default:
10063deb3ec6SMatthias Ringwald             break;
10073deb3ec6SMatthias Ringwald     }
10083deb3ec6SMatthias Ringwald 
10093deb3ec6SMatthias Ringwald     // done
1010a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
10113deb3ec6SMatthias Ringwald }
10123deb3ec6SMatthias Ringwald 
1013b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
10144562e2a2SMatthias Ringwald     uint16_t i;
10154562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
10164562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
10174562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
10184562e2a2SMatthias Ringwald                 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
10194562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
10204562e2a2SMatthias Ringwald                 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
10214562e2a2SMatthias Ringwald                 // avoid set but not used warning
10224562e2a2SMatthias Ringwald                 (void) hfp_callheld_status;
10234562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
10244562e2a2SMatthias Ringwald                 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
10254562e2a2SMatthias Ringwald             }
10264562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1027a473a009SMatthias Ringwald             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
10284562e2a2SMatthias Ringwald             break;
10294562e2a2SMatthias Ringwald         }
10304562e2a2SMatthias Ringwald     }
10314562e2a2SMatthias Ringwald }
10324562e2a2SMatthias Ringwald 
1033426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1034186dd3d2SMatthias Ringwald     int value;
1035186dd3d2SMatthias Ringwald     int i;
1036a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1037667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1038a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1039a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1040667ec068SMatthias Ringwald             break;
1041667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1042a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1043ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1044667ec068SMatthias Ringwald             break;
1045667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1046a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1047a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1048667ec068SMatthias Ringwald             break;
1049ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1050a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10512308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1052667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1053ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1054ce263fc8SMatthias Ringwald             break;
1055ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1056a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10572308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1058667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1059ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1060ce263fc8SMatthias Ringwald             break;
1061ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1062a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1063ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1064a0ffb263SMatthias Ringwald             break;
1065a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1066a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1067a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1068a0ffb263SMatthias Ringwald             break;
1069a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1070a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1071a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1072ce263fc8SMatthias Ringwald             break;
1073ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1074a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10755a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1076a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1077ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1078ce263fc8SMatthias Ringwald             break;
1079ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
10805a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1081a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1082a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
108390244c92SMilanka Ringwald             switch (hfp_connection->state){
108490244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
108590244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
108690244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
108790244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
108890244c92SMilanka Ringwald                             return;
108990244c92SMilanka Ringwald                         default:
109090244c92SMilanka Ringwald                             break;
109190244c92SMilanka Ringwald                     }
109290244c92SMilanka Ringwald                 default:
109390244c92SMilanka Ringwald                     break;
109490244c92SMilanka Ringwald             }
1095ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1096ce263fc8SMatthias Ringwald             break;
1097ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1098a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1099ce263fc8SMatthias Ringwald             break;
1100ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
11015a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1102ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1103ce263fc8SMatthias Ringwald             break;
1104ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
11055a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11064562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1107ce263fc8SMatthias Ringwald             break;
1108c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
11095a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1110c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1111a473a009SMatthias Ringwald                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1112c741b032SMilanka Ringwald             }
1113c741b032SMilanka Ringwald             break;
11141cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
11151cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11165a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
11171cc65c4fSMatthias Ringwald 			break;
1118eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1119eac56539SMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_IN_BAND_RING_TONE, get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE));
1120ce263fc8SMatthias Ringwald         default:
1121ce263fc8SMatthias Ringwald             break;
11223deb3ec6SMatthias Ringwald     }
11230cef86faSMatthias Ringwald }
1124426f9988SMatthias Ringwald 
112576cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
112676cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
112776cc1527SMatthias Ringwald }
112876cc1527SMatthias Ringwald 
1129426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1130426f9988SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
1131426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1132426f9988SMatthias Ringwald     if (size < 1) return;
1133426f9988SMatthias Ringwald 
1134426f9988SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1135426f9988SMatthias Ringwald     if (!hfp_connection) return;
1136426f9988SMatthias Ringwald 
1137426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1138e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1139e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1140e43d1938SMatthias Ringwald #endif
1141426f9988SMatthias Ringwald 
1142426f9988SMatthias Ringwald     // process messages byte-wise
1143426f9988SMatthias Ringwald     int pos;
1144426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1145426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
1146426f9988SMatthias Ringwald 
11471599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1148426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1149426f9988SMatthias Ringwald 
1150426f9988SMatthias Ringwald         hfp_hf_handle_rfcomm_command(hfp_connection);
1151426f9988SMatthias Ringwald     }
11521c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
11533deb3ec6SMatthias Ringwald }
11543deb3ec6SMatthias Ringwald 
11551c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1156665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1157665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1158665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1159a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
116022387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
11611c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
11623deb3ec6SMatthias Ringwald     }
11633deb3ec6SMatthias Ringwald }
11643deb3ec6SMatthias Ringwald 
11651c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
11663deb3ec6SMatthias Ringwald     switch (packet_type){
11673deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1168426f9988SMatthias Ringwald             hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size);
11693deb3ec6SMatthias Ringwald             break;
11703deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1171d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1172d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
11731c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1174d4dd47ffSMatthias Ringwald                 return;
1175d4dd47ffSMatthias Ringwald             }
117627950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1177202c8a4cSMatthias Ringwald             break;
11783deb3ec6SMatthias Ringwald         default:
11793deb3ec6SMatthias Ringwald             break;
11803deb3ec6SMatthias Ringwald     }
11811c6a0fc0SMatthias Ringwald     hfp_hf_run();
11823deb3ec6SMatthias Ringwald }
11833deb3ec6SMatthias Ringwald 
11841c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1185405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
11861c6a0fc0SMatthias Ringwald     hfp_hf_run();
1187405014fbSMatthias Ringwald }
1188405014fbSMatthias Ringwald 
1189a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1190520c92d5SMatthias Ringwald     hfp_init();
119120b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
119220b2edb6SMatthias Ringwald     hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
119320b2edb6SMatthias Ringwald     hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
119420b2edb6SMatthias Ringwald     hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
119520b2edb6SMatthias Ringwald     hfp_codecs_nr = 0;
119620b2edb6SMatthias Ringwald     hfp_hf_speaker_gain = 9;
119720b2edb6SMatthias Ringwald     hfp_hf_microphone_gain = 9;
119820b2edb6SMatthias Ringwald     hfp_indicators_nr = 0;
119920b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1200d63c37a1SMatthias Ringwald 
12011c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
12021c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
120327950165SMatthias Ringwald 
12041c6a0fc0SMatthias Ringwald     rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
120527950165SMatthias Ringwald 
120627950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
12071c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
120820b2edb6SMatthias Ringwald }
120927950165SMatthias Ringwald 
121020b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
121120b2edb6SMatthias Ringwald     hfp_deinit();
121220b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
121320b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t));
121420b2edb6SMatthias Ringwald     (void) memset(phone_number, 0, sizeof(phone_number));
1215a0ffb263SMatthias Ringwald }
1216a0ffb263SMatthias Ringwald 
12177ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
12183deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1219a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
12203deb3ec6SMatthias Ringwald         return;
12213deb3ec6SMatthias Ringwald     }
12223deb3ec6SMatthias Ringwald 
12233deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
12243deb3ec6SMatthias Ringwald     int i;
12253deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
12263deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
12273deb3ec6SMatthias Ringwald     }
12283deb3ec6SMatthias Ringwald }
12293deb3ec6SMatthias Ringwald 
1230a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
12313deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1232a0ffb263SMatthias Ringwald }
12333deb3ec6SMatthias Ringwald 
12347ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
12353deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
12363deb3ec6SMatthias Ringwald     int i;
1237a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
12383deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
12393deb3ec6SMatthias Ringwald     }
12403deb3ec6SMatthias Ringwald }
12413deb3ec6SMatthias Ringwald 
12423deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1243323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
12443deb3ec6SMatthias Ringwald }
12453deb3ec6SMatthias Ringwald 
1246c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
12479c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1248a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1249a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1250a33eb0c4SMilanka Ringwald         return;
1251a33eb0c4SMilanka Ringwald     }
1252a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
12531c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
12543deb3ec6SMatthias Ringwald }
12553deb3ec6SMatthias Ringwald 
1256c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
12579c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1258a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1259a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12603deb3ec6SMatthias Ringwald         return;
12613deb3ec6SMatthias Ringwald     }
1262a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
12631c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
12643deb3ec6SMatthias Ringwald }
12653deb3ec6SMatthias Ringwald 
1266c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1267c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1268ce263fc8SMatthias Ringwald }
1269ce263fc8SMatthias Ringwald 
1270c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1271c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1272ce263fc8SMatthias Ringwald }
1273ce263fc8SMatthias Ringwald 
12743deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1275c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
12769c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1277a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1278a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12793deb3ec6SMatthias Ringwald         return;
12803deb3ec6SMatthias Ringwald     }
1281a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1282a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
12831c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
12843deb3ec6SMatthias Ringwald }
12853deb3ec6SMatthias Ringwald 
1286c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
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     switch (hfp_connection->hf_query_operator_state){
1293ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1294a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1295ce263fc8SMatthias Ringwald             break;
1296ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1297a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1298ce263fc8SMatthias Ringwald             break;
1299ce263fc8SMatthias Ringwald         default:
1300ce263fc8SMatthias Ringwald             break;
1301ce263fc8SMatthias Ringwald     }
13021c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13033deb3ec6SMatthias Ringwald }
13043deb3ec6SMatthias Ringwald 
1305c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
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->enable_extended_audio_gateway_error_report = enable;
13121c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13133deb3ec6SMatthias Ringwald }
13143deb3ec6SMatthias Ringwald 
1315ce263fc8SMatthias Ringwald 
1316c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1317c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1318ce263fc8SMatthias Ringwald }
1319ce263fc8SMatthias Ringwald 
1320c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1321c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1322ce263fc8SMatthias Ringwald }
1323ce263fc8SMatthias Ringwald 
132438200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
132538200c1dSMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
132638200c1dSMilanka Ringwald }
1327ce263fc8SMatthias Ringwald 
1328c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
13299c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1330a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1331a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1332a33eb0c4SMilanka Ringwald         return;
1333a33eb0c4SMilanka Ringwald     }
1334a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1335ce263fc8SMatthias Ringwald 
1336a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1337a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
13383deb3ec6SMatthias Ringwald 
133938200c1dSMilanka Ringwald     hfp_connection->trigger_codec_exchange = 0;
1340a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1341332ca98fSMatthias Ringwald         log_info("no codec negotiation feature, using NBS");
1342a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
134338200c1dSMilanka Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
134438200c1dSMilanka Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
134538200c1dSMilanka Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
134638200c1dSMilanka Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
134740e8d6c5SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
134838200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1349ce263fc8SMatthias Ringwald     } else {
1350a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1351aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1352aa4dd815SMatthias Ringwald                 break;
1353*ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1354*ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1355*ec3bfc1aSMatthias Ringwald                 break;
1356aa4dd815SMatthias Ringwald             default:
13571cc65c4fSMatthias Ringwald 				hfp_connection->codec_confirmed = 0;
13581cc65c4fSMatthias Ringwald 				hfp_connection->suggested_codec = 0;
13591cc65c4fSMatthias Ringwald 				hfp_connection->negotiated_codec = 0;
13601cc65c4fSMatthias Ringwald 				hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
136138200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1362aa4dd815SMatthias Ringwald                 break;
13633deb3ec6SMatthias Ringwald         }
1364ce263fc8SMatthias Ringwald     }
1365ce263fc8SMatthias Ringwald 
13661c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13673deb3ec6SMatthias Ringwald }
13683deb3ec6SMatthias Ringwald 
1369c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
13709c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1371a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1372a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1373a33eb0c4SMilanka Ringwald         return;
1374a33eb0c4SMilanka Ringwald     }
1375a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
13761c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13773deb3ec6SMatthias Ringwald }
13783deb3ec6SMatthias Ringwald 
1379c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
13809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1381a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1382a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1383a33eb0c4SMilanka Ringwald         return;
1384a33eb0c4SMilanka Ringwald     }
1385ce263fc8SMatthias Ringwald 
1386ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1387a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
13881c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1389ce263fc8SMatthias Ringwald     } else {
1390ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1391ce263fc8SMatthias Ringwald     }
1392ce263fc8SMatthias Ringwald }
1393ce263fc8SMatthias Ringwald 
1394c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
13959c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1396a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1397a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1398a33eb0c4SMilanka Ringwald         return;
1399a33eb0c4SMilanka Ringwald     }
1400a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
14011c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1402ce263fc8SMatthias Ringwald }
1403ce263fc8SMatthias Ringwald 
1404c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
14059c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1406a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1407a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1408a33eb0c4SMilanka Ringwald         return;
1409a33eb0c4SMilanka Ringwald     }
1410ce263fc8SMatthias Ringwald 
1411ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1412a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
14131c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1414ce263fc8SMatthias Ringwald     }
1415ce263fc8SMatthias Ringwald }
1416ce263fc8SMatthias Ringwald 
1417c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){
14189c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1419a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1420a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1421a33eb0c4SMilanka Ringwald         return;
1422a33eb0c4SMilanka Ringwald     }
1423ce263fc8SMatthias Ringwald 
1424ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1425a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
14261c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1427ce263fc8SMatthias Ringwald     }
1428ce263fc8SMatthias Ringwald }
1429ce263fc8SMatthias Ringwald 
1430c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
14319c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1432a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1433a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1434a33eb0c4SMilanka Ringwald         return;
1435a33eb0c4SMilanka Ringwald     }
1436ce263fc8SMatthias Ringwald 
1437505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1438505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1439a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
14401c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1441ce263fc8SMatthias Ringwald     }
1442ce263fc8SMatthias Ringwald }
1443ce263fc8SMatthias Ringwald 
1444c8626498SMilanka Ringwald void hfp_hf_swap_calls(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 
1451505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1452505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1453a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
14541c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1455ce263fc8SMatthias Ringwald     }
1456ce263fc8SMatthias Ringwald }
1457ce263fc8SMatthias Ringwald 
1458c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
14599c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1460a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1461a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1462a33eb0c4SMilanka Ringwald         return;
1463a33eb0c4SMilanka Ringwald     }
1464ce263fc8SMatthias Ringwald 
1465505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1466505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1467a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
14681c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1469ce263fc8SMatthias Ringwald     }
1470ce263fc8SMatthias Ringwald }
1471ce263fc8SMatthias Ringwald 
1472c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
14739c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1474a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1475a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1476a33eb0c4SMilanka Ringwald         return;
1477a33eb0c4SMilanka Ringwald     }
1478ce263fc8SMatthias Ringwald 
1479505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1480505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1481a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
14821c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1483ce263fc8SMatthias Ringwald     }
1484ce263fc8SMatthias Ringwald }
1485ce263fc8SMatthias Ringwald 
1486c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
14879c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1488a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1489a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1490a33eb0c4SMilanka Ringwald         return;
1491a33eb0c4SMilanka Ringwald     }
1492667ec068SMatthias Ringwald 
1493505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1494505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1495a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1496a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
14971c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1498667ec068SMatthias Ringwald     }
1499667ec068SMatthias Ringwald }
1500667ec068SMatthias Ringwald 
1501c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
15029c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1503a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1504a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1505a33eb0c4SMilanka Ringwald         return;
1506a33eb0c4SMilanka Ringwald     }
1507667ec068SMatthias Ringwald 
1508505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1509505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1510a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1511a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
15121c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1513667ec068SMatthias Ringwald     }
1514667ec068SMatthias Ringwald }
1515ce263fc8SMatthias Ringwald 
1516c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
15179c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1518a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1519a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1520a33eb0c4SMilanka Ringwald         return;
1521a33eb0c4SMilanka Ringwald     }
1522ce263fc8SMatthias Ringwald 
1523a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1524ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
15251c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1526ce263fc8SMatthias Ringwald }
1527ce263fc8SMatthias Ringwald 
1528c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
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     }
1534ce263fc8SMatthias Ringwald 
1535a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1536a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1537a0ffb263SMatthias Ringwald 
15381c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1539ce263fc8SMatthias Ringwald }
1540ce263fc8SMatthias Ringwald 
1541c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
15429c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1543a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1544a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1545a33eb0c4SMilanka Ringwald         return;
1546a33eb0c4SMilanka Ringwald     }
1547ce263fc8SMatthias Ringwald 
1548a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
15491c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1550ce263fc8SMatthias Ringwald }
1551ce263fc8SMatthias Ringwald 
1552c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
15539c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1554a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1555a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1556a33eb0c4SMilanka Ringwald         return;
1557a33eb0c4SMilanka Ringwald     }
1558ce263fc8SMatthias Ringwald 
1559a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
15601c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1561ce263fc8SMatthias Ringwald }
1562ce263fc8SMatthias Ringwald 
1563ce263fc8SMatthias Ringwald 
1564c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
15659c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1566a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1567a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1568a33eb0c4SMilanka Ringwald         return;
1569a33eb0c4SMilanka Ringwald     }
1570ce263fc8SMatthias Ringwald 
1571a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
15721c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1573ce263fc8SMatthias Ringwald }
1574ce263fc8SMatthias Ringwald 
1575ce263fc8SMatthias Ringwald 
1576c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
15779c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1578a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1579a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1580a33eb0c4SMilanka Ringwald         return;
1581a33eb0c4SMilanka Ringwald     }
1582ce263fc8SMatthias Ringwald 
1583a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
15841c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1585ce263fc8SMatthias Ringwald }
1586ce263fc8SMatthias Ringwald 
1587c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
15889c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1589a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1590a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1591a33eb0c4SMilanka Ringwald         return;
1592a33eb0c4SMilanka Ringwald     }
1593ce263fc8SMatthias Ringwald 
1594a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
15951c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1596ce263fc8SMatthias Ringwald }
1597ce263fc8SMatthias Ringwald 
1598ce263fc8SMatthias Ringwald 
1599c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
16009c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1601a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1602a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1603a33eb0c4SMilanka Ringwald         return;
1604a33eb0c4SMilanka Ringwald     }
1605ce263fc8SMatthias Ringwald 
1606a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
16071c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1608ce263fc8SMatthias Ringwald }
1609ce263fc8SMatthias Ringwald 
1610c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
16119c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1612a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1613a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1614a33eb0c4SMilanka Ringwald         return;
1615a33eb0c4SMilanka Ringwald     }
1616ce263fc8SMatthias Ringwald 
1617a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
16181c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1619ce263fc8SMatthias Ringwald }
1620ce263fc8SMatthias Ringwald 
1621c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
16229c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1623a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1624a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1625a33eb0c4SMilanka Ringwald         return;
1626a33eb0c4SMilanka Ringwald     }
1627ce263fc8SMatthias Ringwald 
1628a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
16291c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1630ce263fc8SMatthias Ringwald }
1631ce263fc8SMatthias Ringwald 
1632c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
16339c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1634a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1635a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1636a33eb0c4SMilanka Ringwald         return;
1637a33eb0c4SMilanka Ringwald     }
1638ce263fc8SMatthias Ringwald 
1639a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
16401c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1641ce263fc8SMatthias Ringwald }
1642ce263fc8SMatthias Ringwald 
1643c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
16449c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1645a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1646a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1647a33eb0c4SMilanka Ringwald         return;
1648a33eb0c4SMilanka Ringwald     }
1649c8626498SMilanka Ringwald 
1650a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1651c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1652a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1653a0ffb263SMatthias Ringwald         return;
1654a0ffb263SMatthias Ringwald     }
1655a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1656a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
16571c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1658ce263fc8SMatthias Ringwald }
1659ce263fc8SMatthias Ringwald 
1660c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
16619c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1662a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1663a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1664a33eb0c4SMilanka Ringwald         return;
1665a33eb0c4SMilanka Ringwald     }
1666c8626498SMilanka Ringwald 
1667a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1668c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1669a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1670a0ffb263SMatthias Ringwald         return;
1671a0ffb263SMatthias Ringwald     }
1672a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1673a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
16741c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1675ce263fc8SMatthias Ringwald }
1676ce263fc8SMatthias Ringwald 
1677c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
16789c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1679a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1680a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1681a33eb0c4SMilanka Ringwald         return;
1682a33eb0c4SMilanka Ringwald     }
1683a33eb0c4SMilanka Ringwald 
1684a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
16851c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1686ce263fc8SMatthias Ringwald }
1687ce263fc8SMatthias Ringwald 
1688c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
16899c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1690a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1691a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1692a33eb0c4SMilanka Ringwald         return;
1693a33eb0c4SMilanka Ringwald     }
1694a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
16951c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1696ce263fc8SMatthias Ringwald }
16973deb3ec6SMatthias Ringwald 
1698c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
16999c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1700a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1701a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1702a33eb0c4SMilanka Ringwald         return;
1703a33eb0c4SMilanka Ringwald     }
1704a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
17051c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1706667ec068SMatthias Ringwald }
1707667ec068SMatthias Ringwald 
1708667ec068SMatthias Ringwald 
1709c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
17109c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1711a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1712a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1713a33eb0c4SMilanka Ringwald         return;
1714a33eb0c4SMilanka Ringwald     }
1715a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1716a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
17171c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1718667ec068SMatthias Ringwald }
1719667ec068SMatthias Ringwald 
1720c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
17219c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1722a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1723a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1724a33eb0c4SMilanka Ringwald         return;
1725a33eb0c4SMilanka Ringwald     }
1726a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1727a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
17281c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1729667ec068SMatthias Ringwald }
1730667ec068SMatthias Ringwald 
1731c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
17329c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1733a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1734a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1735a33eb0c4SMilanka Ringwald         return;
1736a33eb0c4SMilanka Ringwald     }
1737a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1738a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
17391c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1740667ec068SMatthias Ringwald }
1741667ec068SMatthias Ringwald 
1742c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
17439c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1744a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1745a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1746a33eb0c4SMilanka Ringwald         return;
1747a33eb0c4SMilanka Ringwald     }
1748a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1749a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
17501c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1751667ec068SMatthias Ringwald }
1752667ec068SMatthias Ringwald 
1753c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
17549c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1755a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1756a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1757a33eb0c4SMilanka Ringwald         return;
1758a33eb0c4SMilanka Ringwald     }
1759a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
17601c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1761667ec068SMatthias Ringwald }
1762667ec068SMatthias Ringwald 
1763c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
17649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1765a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1766a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1767a33eb0c4SMilanka Ringwald         return;
1768a33eb0c4SMilanka Ringwald     }
1769667ec068SMatthias Ringwald     // find index for assigned number
1770667ec068SMatthias Ringwald     int i;
1771667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1772667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1773667ec068SMatthias Ringwald             // set value
1774667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1775667ec068SMatthias Ringwald             // mark for update
1776a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1777a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1778667ec068SMatthias Ringwald                 // send update
17791c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
1780a0ffb263SMatthias Ringwald             }
1781667ec068SMatthias Ringwald             return;
1782667ec068SMatthias Ringwald         }
1783667ec068SMatthias Ringwald     }
1784667ec068SMatthias Ringwald }
1785667ec068SMatthias Ringwald 
1786d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
1787d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1788d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
1789d7f6b5cbSMatthias Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1790d7f6b5cbSMatthias Ringwald         return 0;
1791d7f6b5cbSMatthias Ringwald     }
1792d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1793d7f6b5cbSMatthias Ringwald }
179476cc1527SMatthias Ringwald 
179576cc1527SMatthias 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){
179676cc1527SMatthias Ringwald 	if (!name){
179776cc1527SMatthias Ringwald 		name = default_hfp_hf_service_name;
179876cc1527SMatthias Ringwald 	}
179976cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
180076cc1527SMatthias Ringwald 
180176cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
180276cc1527SMatthias Ringwald 	//
180376cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
180476cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
180576cc1527SMatthias Ringwald 	//
180676cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
180776cc1527SMatthias Ringwald 	//
180876cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
180976cc1527SMatthias Ringwald 	if (wide_band_speech && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
181076cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
181176cc1527SMatthias Ringwald 	}
181276cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
181376cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
181476cc1527SMatthias Ringwald }
181576cc1527SMatthias Ringwald 
181676cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
181776cc1527SMatthias Ringwald 	if (callback == NULL){
181876cc1527SMatthias Ringwald 		log_error("hfp_hf_register_packet_handler called with NULL callback");
181976cc1527SMatthias Ringwald 		return;
182076cc1527SMatthias Ringwald 	}
182176cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
182276cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
182376cc1527SMatthias Ringwald }
1824