xref: /btstack/src/classic/hfp_hf.c (revision af97579e443f2c6b71882d1189eb88c19629f90f)
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>
493cfa4086SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <string.h>
513deb3ec6SMatthias Ringwald 
52235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
5359c6af15SMatthias Ringwald #include "btstack_debug.h"
54d4dd47ffSMatthias Ringwald #include "btstack_event.h"
553deb3ec6SMatthias Ringwald #include "btstack_memory.h"
5659c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5759c6af15SMatthias Ringwald #include "classic/core.h"
5859c6af15SMatthias Ringwald #include "classic/hfp.h"
5959c6af15SMatthias Ringwald #include "classic/hfp_hf.h"
60efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
61746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
62023f2764SMatthias Ringwald #include "classic/sdp_util.h"
6359c6af15SMatthias Ringwald #include "hci.h"
6459c6af15SMatthias Ringwald #include "hci_cmd.h"
6559c6af15SMatthias Ringwald #include "hci_dump.h"
6659c6af15SMatthias Ringwald #include "l2cap.h"
673deb3ec6SMatthias Ringwald 
6820b2edb6SMatthias Ringwald // const
6920b2edb6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
7020b2edb6SMatthias Ringwald 
7120b2edb6SMatthias Ringwald // globals
721c6a0fc0SMatthias Ringwald static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration;
7327950165SMatthias Ringwald 
743deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
7520b2edb6SMatthias Ringwald static uint8_t hfp_codecs_nr;
763deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
773deb3ec6SMatthias Ringwald 
7820b2edb6SMatthias Ringwald static uint8_t hfp_indicators_nr;
7925789943SMilanka Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_INDICATORS];
8025789943SMilanka Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_INDICATORS];
81667ec068SMatthias Ringwald 
8220b2edb6SMatthias Ringwald static uint8_t hfp_hf_speaker_gain;
8320b2edb6SMatthias Ringwald static uint8_t hfp_hf_microphone_gain;
843deb3ec6SMatthias Ringwald 
85ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
863deb3ec6SMatthias Ringwald 
87ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
88ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
89ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
90ce263fc8SMatthias Ringwald 
91ce263fc8SMatthias Ringwald static char phone_number[25];
92ce263fc8SMatthias Ringwald 
9376cc1527SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
9476cc1527SMatthias Ringwald 	int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
9576cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
9676cc1527SMatthias Ringwald 	return hf && ag;
9776cc1527SMatthias Ringwald }
9876cc1527SMatthias Ringwald 
9976cc1527SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
10076cc1527SMatthias Ringwald 	int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
10176cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
10276cc1527SMatthias Ringwald 	return hf && ag;
10376cc1527SMatthias Ringwald }
10476cc1527SMatthias Ringwald 
10576cc1527SMatthias Ringwald 
10676cc1527SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
10776cc1527SMatthias Ringwald 	int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
10876cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
10976cc1527SMatthias Ringwald 	return hf && ag;
11076cc1527SMatthias Ringwald }
11176cc1527SMatthias Ringwald 
11276cc1527SMatthias Ringwald 
1139c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
1149c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1159c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1169c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1179c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1189c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1199c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1209c9c64c1SMatthias Ringwald         return hfp_connection;
1219c9c64c1SMatthias Ringwald     }
1229c9c64c1SMatthias Ringwald     return NULL;
1239c9c64c1SMatthias Ringwald }
1249c9c64c1SMatthias Ringwald 
12576cc1527SMatthias Ringwald /* emit functinos */
1263deb3ec6SMatthias Ringwald 
127a473a009SMatthias Ringwald static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){
128a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
129d703d377SMatthias Ringwald     uint8_t event[33];
130a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
131a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
132a473a009SMatthias Ringwald     event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION;
133d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
134d703d377SMatthias Ringwald     event[5] = status;
135d703d377SMatthias Ringwald     event[6] = hfp_connection->bnip_type;
136d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 8);
137d703d377SMatthias Ringwald     strncpy((char*)&event[7], hfp_connection->bnip_number, size);
138d703d377SMatthias Ringwald     event[7 + size] = 0;
139a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
140a0ffb263SMatthias Ringwald }
141a0ffb263SMatthias Ringwald 
142a473a009SMatthias Ringwald static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){
143a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
144d703d377SMatthias Ringwald     uint8_t event[32];
145a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
146a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
147a0ffb263SMatthias Ringwald     event[2] = event_subtype;
148d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
149d703d377SMatthias Ringwald     event[5] = hfp_connection->bnip_type;
150d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 7);
151d703d377SMatthias Ringwald     strncpy((char*)&event[6], hfp_connection->bnip_number, size);
152d703d377SMatthias Ringwald     event[6 + size] = 0;
153a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
154a0ffb263SMatthias Ringwald }
155a0ffb263SMatthias Ringwald 
156a473a009SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){
157a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
158d703d377SMatthias Ringwald     uint8_t event[38];
1590aee97efSMilanka Ringwald     int pos = 0;
1600aee97efSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
1610aee97efSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
1620aee97efSMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
163d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
164d703d377SMatthias Ringwald     pos += 2;
165a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_idx;
166a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_dir;
167a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_status;
168a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mode;
169a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mpty;
170a473a009SMatthias Ringwald     event[pos++] = hfp_connection->bnip_type;
171d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - pos - 1);
172a473a009SMatthias Ringwald     strncpy((char*)&event[pos], hfp_connection->bnip_number, size);
1730aee97efSMilanka Ringwald     pos += size;
1740aee97efSMilanka Ringwald     event[pos++] = 0;
175a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
176a0ffb263SMatthias Ringwald }
177a0ffb263SMatthias Ringwald 
17876cc1527SMatthias Ringwald 
179a473a009SMatthias Ringwald static void hfp_emit_ag_indicator_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
180a473a009SMatthias Ringwald 	if (hfp_hf_callback == NULL) return;
181d703d377SMatthias Ringwald 	uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE+1];
18276cc1527SMatthias Ringwald 	int pos = 0;
18376cc1527SMatthias Ringwald 	event[pos++] = HCI_EVENT_HFP_META;
18476cc1527SMatthias Ringwald 	event[pos++] = sizeof(event) - 2;
18576cc1527SMatthias Ringwald 	event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
186d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
187d703d377SMatthias Ringwald     pos += 2;
188a473a009SMatthias Ringwald 	event[pos++] = indicator->index;
189a473a009SMatthias Ringwald 	event[pos++] = indicator->status;
190a473a009SMatthias Ringwald 	event[pos++] = indicator->min_range;
191a473a009SMatthias Ringwald 	event[pos++] = indicator->max_range;
192a473a009SMatthias Ringwald 	event[pos++] = indicator->mandatory;
193a473a009SMatthias Ringwald 	event[pos++] = indicator->enabled;
194a473a009SMatthias Ringwald 	event[pos++] = indicator->status_changed;
195a473a009SMatthias Ringwald 	strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
19676cc1527SMatthias Ringwald 	pos += HFP_MAX_INDICATOR_DESC_SIZE;
19776cc1527SMatthias Ringwald 	event[pos] = 0;
198a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1993deb3ec6SMatthias Ringwald }
2003deb3ec6SMatthias Ringwald 
201a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){
202a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
203d703d377SMatthias Ringwald 	uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
20476cc1527SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
20576cc1527SMatthias Ringwald 	event[1] = sizeof(event) - 2;
20676cc1527SMatthias Ringwald 	event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
207d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
208d703d377SMatthias Ringwald 	event[5] = hfp_connection->network_operator.mode;
209d703d377SMatthias Ringwald 	event[6] = hfp_connection->network_operator.format;
210d703d377SMatthias Ringwald 	strncpy((char*)&event[7], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
211d703d377SMatthias Ringwald 	event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
212a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2133deb3ec6SMatthias Ringwald }
2143deb3ec6SMatthias Ringwald 
21576cc1527SMatthias Ringwald /* send commands */
21689425bfcSMilanka Ringwald 
21789425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2183deb3ec6SMatthias Ringwald     char buffer[20];
2191599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
22089425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
22189425bfcSMilanka Ringwald }
22289425bfcSMilanka Ringwald 
22389425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
22489425bfcSMilanka Ringwald     char buffer[20];
2251599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
22689425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
22789425bfcSMilanka Ringwald }
22889425bfcSMilanka Ringwald 
22986da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
23089425bfcSMilanka Ringwald     char buffer[40];
2311599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
2323deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2333deb3ec6SMatthias Ringwald }
2343deb3ec6SMatthias Ringwald 
2353deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2363deb3ec6SMatthias Ringwald     char buffer[30];
23789425bfcSMilanka Ringwald     const int size = sizeof(buffer);
23889425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
23989425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
2401599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2413deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2423deb3ec6SMatthias Ringwald }
2433deb3ec6SMatthias Ringwald 
2443deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2453deb3ec6SMatthias Ringwald     char buffer[50];
24689425bfcSMilanka Ringwald     const int size = sizeof(buffer);
24789425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
24889425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
2491599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2503deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2513deb3ec6SMatthias Ringwald }
2523deb3ec6SMatthias Ringwald 
2533deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2543deb3ec6SMatthias Ringwald     char buffer[30];
25589425bfcSMilanka Ringwald     const int size = sizeof(buffer);
25689425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
25789425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
2581599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2593deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2603deb3ec6SMatthias Ringwald }
2613deb3ec6SMatthias Ringwald 
26289425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2633deb3ec6SMatthias Ringwald     char buffer[20];
2641599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
265ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
266ce263fc8SMatthias Ringwald }
267ce263fc8SMatthias Ringwald 
268ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
269ce263fc8SMatthias Ringwald     char buffer[40];
2701599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, phone_number);
271ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
272ce263fc8SMatthias Ringwald }
273ce263fc8SMatthias Ringwald 
274a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
275ce263fc8SMatthias Ringwald     char buffer[40];
2761599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
277ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
278ce263fc8SMatthias Ringwald }
279ce263fc8SMatthias Ringwald 
280f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
28189425bfcSMilanka Ringwald     char buffer[40];
2821599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
283ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
284ce263fc8SMatthias Ringwald }
285ce263fc8SMatthias Ringwald 
286ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
287ce263fc8SMatthias Ringwald     char buffer[20];
2881599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
289ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
290ce263fc8SMatthias Ringwald }
291ce263fc8SMatthias Ringwald 
29297d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
2931599fe57SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "ATA\r");
29497d2cadbSMatthias Ringwald }
29597d2cadbSMatthias Ringwald 
29689425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
29789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
29889425bfcSMilanka Ringwald }
29989425bfcSMilanka Ringwald 
30089425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
30189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
30289425bfcSMilanka Ringwald }
30389425bfcSMilanka Ringwald 
30489425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
30589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
30689425bfcSMilanka Ringwald }
30789425bfcSMilanka Ringwald 
30889425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
30989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
31089425bfcSMilanka Ringwald }
31189425bfcSMilanka Ringwald 
31289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
31389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
31489425bfcSMilanka Ringwald }
31589425bfcSMilanka Ringwald 
31689425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
31789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
31889425bfcSMilanka Ringwald }
31989425bfcSMilanka Ringwald 
32089425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
32189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
32289425bfcSMilanka Ringwald }
32389425bfcSMilanka Ringwald 
32489425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
32589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
32689425bfcSMilanka Ringwald }
32789425bfcSMilanka Ringwald 
32889425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
32989425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
33089425bfcSMilanka Ringwald }
33189425bfcSMilanka Ringwald 
33289425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
33389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
33489425bfcSMilanka Ringwald }
33589425bfcSMilanka Ringwald 
33689425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
33789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
33889425bfcSMilanka Ringwald }
33989425bfcSMilanka Ringwald 
34089425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
34189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
34289425bfcSMilanka Ringwald }
34389425bfcSMilanka Ringwald 
34489425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
34589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
34689425bfcSMilanka Ringwald }
34789425bfcSMilanka Ringwald 
34889425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
34989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
35089425bfcSMilanka Ringwald }
35189425bfcSMilanka Ringwald 
35289425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
35389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
35489425bfcSMilanka Ringwald }
35589425bfcSMilanka Ringwald 
35689425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
35789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
35889425bfcSMilanka Ringwald }
35989425bfcSMilanka Ringwald 
36089425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
36189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
36289425bfcSMilanka Ringwald }
36389425bfcSMilanka Ringwald 
36489425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
36589425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
36689425bfcSMilanka Ringwald }
36789425bfcSMilanka Ringwald 
36889425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
36989425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
37089425bfcSMilanka Ringwald }
37189425bfcSMilanka Ringwald 
372ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
37389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
374ce263fc8SMatthias Ringwald }
375ce263fc8SMatthias Ringwald 
376667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
37789425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
378667ec068SMatthias Ringwald }
379667ec068SMatthias Ringwald 
38076cc1527SMatthias Ringwald /* state machines */
3813deb3ec6SMatthias Ringwald 
382a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
383a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
384a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
385aa4dd815SMatthias Ringwald     int done = 1;
3863deb3ec6SMatthias Ringwald 
387a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
3883deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
389d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
390a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
391a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
3923deb3ec6SMatthias Ringwald             break;
3933deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
394a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
395a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
3963deb3ec6SMatthias Ringwald             break;
3973deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
398a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
399a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4003deb3ec6SMatthias Ringwald             break;
4013deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
402a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
403a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4043deb3ec6SMatthias Ringwald             break;
4053deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
406a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
407a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4083deb3ec6SMatthias Ringwald             break;
4093deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
410a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
411a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4123deb3ec6SMatthias Ringwald             break;
4133deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
414a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
415a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4163deb3ec6SMatthias Ringwald             break;
4173deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
418a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
419a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4203deb3ec6SMatthias Ringwald             break;
4213deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
422a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
423a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4243deb3ec6SMatthias Ringwald             break;
4253deb3ec6SMatthias Ringwald         default:
426aa4dd815SMatthias Ringwald             done = 0;
4273deb3ec6SMatthias Ringwald             break;
4283deb3ec6SMatthias Ringwald     }
4293deb3ec6SMatthias Ringwald     return done;
4303deb3ec6SMatthias Ringwald }
4313deb3ec6SMatthias Ringwald 
432ce263fc8SMatthias Ringwald 
433a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
434a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
435a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
436ce263fc8SMatthias Ringwald 
437ce263fc8SMatthias Ringwald     int done = 0;
438a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
439a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
440ce263fc8SMatthias Ringwald         done = 1;
441a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
442ce263fc8SMatthias Ringwald         return done;
443ce263fc8SMatthias Ringwald     };
444a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
445a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
446ce263fc8SMatthias Ringwald         done = 1;
447a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
448a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
449a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
450ce263fc8SMatthias Ringwald         return done;
451ce263fc8SMatthias Ringwald     }
452ce263fc8SMatthias Ringwald 
453a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
454ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
455a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
456a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
457a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
458ce263fc8SMatthias Ringwald             return 1;
459ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
460a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
461a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
462a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
463ce263fc8SMatthias Ringwald             return 1;
464ce263fc8SMatthias Ringwald         default:
465ce263fc8SMatthias Ringwald             break;
466ce263fc8SMatthias Ringwald     }
467ce263fc8SMatthias Ringwald 
468a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
469a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
470ce263fc8SMatthias Ringwald         done = 1;
471a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
472ce263fc8SMatthias Ringwald         return done;
473ce263fc8SMatthias Ringwald     }
474ce263fc8SMatthias Ringwald 
475ce263fc8SMatthias Ringwald     return done;
476ce263fc8SMatthias Ringwald }
477ce263fc8SMatthias Ringwald 
478*af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
479be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
480be55a11dSMilanka Ringwald         return 0;
481be55a11dSMilanka Ringwald     }
482*af97579eSMilanka Ringwald     printf("hfp_hf_voice_recognition_state_machine, status %d\n", hfp_connection->vra_status);
483be55a11dSMilanka Ringwald     int done = 0;
484be55a11dSMilanka Ringwald     if (hfp_connection->ok_pending == 0) {
485db3cdbd4SMilanka Ringwald         switch (hfp_connection->vra_status){
486be55a11dSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
487be55a11dSMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF:
488be55a11dSMilanka Ringwald                 hfp_connection->ok_pending = 1;
489be55a11dSMilanka Ringwald                 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
490be55a11dSMilanka Ringwald                 break;
491be55a11dSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
492be55a11dSMilanka Ringwald                 hfp_connection->ok_pending = 1;
493be55a11dSMilanka Ringwald                 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
494be55a11dSMilanka Ringwald                 break;
495ce263fc8SMatthias Ringwald 
496be55a11dSMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
497be55a11dSMilanka Ringwald                 if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
498be55a11dSMilanka Ringwald                     return 0;
499be55a11dSMilanka Ringwald                 }
500be55a11dSMilanka Ringwald                 hfp_connection->ok_pending = 1;
501be55a11dSMilanka Ringwald                 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
502be55a11dSMilanka Ringwald                 break;
503be55a11dSMilanka Ringwald 
504be55a11dSMilanka Ringwald             default:
505be55a11dSMilanka Ringwald                 break;
506be55a11dSMilanka Ringwald         }
507be55a11dSMilanka Ringwald     } else {
508db3cdbd4SMilanka Ringwald         switch (hfp_connection->vra_status){
509be55a11dSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
510db3cdbd4SMilanka Ringwald                 hfp_connection->vra_status = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
511be55a11dSMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_VOICE_RECOGNITION_STATUS, 1);
512be55a11dSMilanka Ringwald                 break;
513be55a11dSMilanka Ringwald 
514be55a11dSMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
515db3cdbd4SMilanka Ringwald                 hfp_connection->vra_status = HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED;
516be55a11dSMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_VOICE_RECOGNITION_STATUS, 2);
517be55a11dSMilanka Ringwald                 break;
518be55a11dSMilanka Ringwald 
519be55a11dSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
520be55a11dSMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF:
521db3cdbd4SMilanka Ringwald                 hfp_connection->vra_status = HFP_VRA_VOICE_RECOGNITION_OFF;
522be55a11dSMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_VOICE_RECOGNITION_STATUS, 0);
523be55a11dSMilanka Ringwald                 break;
524be55a11dSMilanka Ringwald             default:
525be55a11dSMilanka Ringwald                 break;
526be55a11dSMilanka Ringwald         }
527be55a11dSMilanka Ringwald     }
528be55a11dSMilanka Ringwald     return done;
529be55a11dSMilanka Ringwald }
530be55a11dSMilanka Ringwald 
531be55a11dSMilanka Ringwald 
532be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
533a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
534ce263fc8SMatthias Ringwald 
535332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
536332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
537ce263fc8SMatthias Ringwald 
538a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
539a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
540332ca98fSMatthias Ringwald 		return 1;
541332ca98fSMatthias Ringwald     }
542332ca98fSMatthias Ringwald 
5431cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
5441cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
545ce263fc8SMatthias Ringwald 
546a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
547fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
5481cc65c4fSMatthias Ringwald 		return 1;
5491cc65c4fSMatthias Ringwald     }
5501cc65c4fSMatthias Ringwald 
5511cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
5521cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
5531cc65c4fSMatthias Ringwald 
554a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
555a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
5561cc65c4fSMatthias Ringwald 		return 1;
5571cc65c4fSMatthias Ringwald     }
558ce263fc8SMatthias Ringwald 
559ce263fc8SMatthias Ringwald     return 0;
560ce263fc8SMatthias Ringwald }
561ce263fc8SMatthias Ringwald 
562a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
563505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
564505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
565ce263fc8SMatthias Ringwald 
56664f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
567a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
568a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
569a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
570ce263fc8SMatthias Ringwald         return 1;
571ce263fc8SMatthias Ringwald     }
572ce263fc8SMatthias Ringwald 
573a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
574ce263fc8SMatthias Ringwald 
575ce263fc8SMatthias Ringwald     // run codecs exchange
576a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
577ce263fc8SMatthias Ringwald     if (done) return 1;
578ce263fc8SMatthias Ringwald 
57938200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
58038200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
58138200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
58238200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
58338200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
58438200c1dSMilanka Ringwald         return 1;
58538200c1dSMilanka Ringwald     }
586ce263fc8SMatthias Ringwald     return 0;
587ce263fc8SMatthias Ringwald }
588ce263fc8SMatthias Ringwald 
58938200c1dSMilanka Ringwald 
590a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
591eaf2b0a1SMatthias Ringwald 
592eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
593eaf2b0a1SMatthias Ringwald 
594a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
595a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
596a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
597ce263fc8SMatthias Ringwald         return 1;
598ce263fc8SMatthias Ringwald     }
599ce263fc8SMatthias Ringwald     return 0;
600ce263fc8SMatthias Ringwald }
601ce263fc8SMatthias Ringwald 
6021c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
6037522e673SMatthias Ringwald 
60476cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
60576cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
60676cc1527SMatthias Ringwald 
60776cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
60876cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
60922387625SMatthias Ringwald 
6103721a235SMatthias Ringwald 	// assert command could be sent
6113721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
6123721a235SMatthias Ringwald 
6133721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
6143721a235SMatthias Ringwald     // WBS Disassociate
6153721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
6163721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
6173721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
6183721a235SMatthias Ringwald         return;
6193721a235SMatthias Ringwald     }
6203721a235SMatthias Ringwald     // Write Codec Config
6213721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
6223721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
6233721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
6243721a235SMatthias Ringwald         return;
6253721a235SMatthias Ringwald     }
6263721a235SMatthias Ringwald     // WBS Associate
6273721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
6283721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
6293721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
6303721a235SMatthias Ringwald         return;
6313721a235SMatthias Ringwald     }
6323721a235SMatthias Ringwald #endif
633689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
634689d4323SMatthias Ringwald     // Enable WBS
635689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
636689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
637689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
638689d4323SMatthias Ringwald         return;
639689d4323SMatthias Ringwald     }
640689d4323SMatthias Ringwald     // Write I2S/PCM params
641689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
642689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
643689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
644689d4323SMatthias Ringwald         return;
645689d4323SMatthias Ringwald     }
646689d4323SMatthias Ringwald     // Disable WBS
647689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
648689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
649689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
650689d4323SMatthias Ringwald         return;
651689d4323SMatthias Ringwald     }
652689d4323SMatthias Ringwald #endif
65348e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
65448e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
65548e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
65648e6eeeeSMatthias Ringwald         return;
65748e6eeeeSMatthias Ringwald     }
65848e6eeeeSMatthias Ringwald #endif
6593721a235SMatthias Ringwald 
660cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
661cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
662cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
6637522e673SMatthias Ringwald         // notify about codec selection if not done already
6647522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
6657522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
6667522e673SMatthias Ringwald         }
667cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
6687522e673SMatthias Ringwald         return;
6697522e673SMatthias Ringwald     }
6707522e673SMatthias Ringwald 
671d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
672d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
673d4dd47ffSMatthias Ringwald         return;
674d4dd47ffSMatthias Ringwald     }
675a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
676ce263fc8SMatthias Ringwald     if (!done){
677a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
678ce263fc8SMatthias Ringwald     }
679ce263fc8SMatthias Ringwald     if (!done){
680*af97579eSMilanka Ringwald         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
681be55a11dSMilanka Ringwald     }
682be55a11dSMilanka Ringwald     if (!done){
683a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
684ce263fc8SMatthias Ringwald     }
685ce263fc8SMatthias Ringwald     if (!done){
686a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
687ce263fc8SMatthias Ringwald     }
688ce263fc8SMatthias Ringwald 
6891016a228SMatthias Ringwald     // don't send a new command while ok still pending
6901016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6911016a228SMatthias Ringwald 
692a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
693a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
694a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
695a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
696ce263fc8SMatthias Ringwald         return;
697ce263fc8SMatthias Ringwald     }
698ce263fc8SMatthias Ringwald 
699a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
700a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
701a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
702a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
703ce263fc8SMatthias Ringwald         return;
704ce263fc8SMatthias Ringwald     }
705ce263fc8SMatthias Ringwald 
706a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
707a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
708a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
709a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
710ce263fc8SMatthias Ringwald         return;
711ce263fc8SMatthias Ringwald     }
712ce263fc8SMatthias Ringwald 
713a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
714a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
715a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
716a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
717ce263fc8SMatthias Ringwald         return;
718ce263fc8SMatthias Ringwald     }
719ce263fc8SMatthias Ringwald 
720a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
721a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
722a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
723a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
724ce263fc8SMatthias Ringwald         return;
725ce263fc8SMatthias Ringwald     }
726ce263fc8SMatthias Ringwald 
727a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
728a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
729a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
730a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
731ce263fc8SMatthias Ringwald         return;
732ce263fc8SMatthias Ringwald     }
733ce263fc8SMatthias Ringwald 
734a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
735a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
736a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
737a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
738ce263fc8SMatthias Ringwald         return;
739ce263fc8SMatthias Ringwald     }
740ce263fc8SMatthias Ringwald 
741a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
742a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
743a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
744a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
745ce263fc8SMatthias Ringwald         return;
746ce263fc8SMatthias Ringwald     }
747ce263fc8SMatthias Ringwald 
748a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
749a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
750a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
751a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
752ce263fc8SMatthias Ringwald         return;
753ce263fc8SMatthias Ringwald     }
754ce263fc8SMatthias Ringwald 
755a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
756a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
757a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
758a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
759ce263fc8SMatthias Ringwald         return;
760ce263fc8SMatthias Ringwald     }
761ce263fc8SMatthias Ringwald 
762a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
763a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
764a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
765a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
766ce263fc8SMatthias Ringwald         return;
767ce263fc8SMatthias Ringwald     }
768ce263fc8SMatthias Ringwald 
769a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
770a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
771a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
772a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
773ce263fc8SMatthias Ringwald         return;
774ce263fc8SMatthias Ringwald     }
775ce263fc8SMatthias Ringwald 
776a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
777a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
778a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
779a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
780ce263fc8SMatthias Ringwald         return;
781ce263fc8SMatthias Ringwald     }
782ce263fc8SMatthias Ringwald 
783a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
784a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
785a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
786a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
787ce263fc8SMatthias Ringwald         return;
788ce263fc8SMatthias Ringwald     }
789ce263fc8SMatthias Ringwald 
790a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
791a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
792a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
793a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
794ce263fc8SMatthias Ringwald         return;
795ce263fc8SMatthias Ringwald     }
796ce263fc8SMatthias Ringwald 
797a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
798a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
799a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
800a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
801ce263fc8SMatthias Ringwald         return;
802ce263fc8SMatthias Ringwald     }
803ce263fc8SMatthias Ringwald 
804a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
805a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
806a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
807a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
808ce263fc8SMatthias Ringwald         return;
809ce263fc8SMatthias Ringwald     }
810ce263fc8SMatthias Ringwald 
811a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
812a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
813a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
814a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
815667ec068SMatthias Ringwald         return;
816667ec068SMatthias Ringwald     }
817667ec068SMatthias Ringwald 
818a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
819a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
820a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
821a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
822a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
823ce263fc8SMatthias Ringwald         return;
824ce263fc8SMatthias Ringwald     }
825ce263fc8SMatthias Ringwald 
826a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
827a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
828a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
829a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
830ce263fc8SMatthias Ringwald         return;
831ce263fc8SMatthias Ringwald     }
832ce263fc8SMatthias Ringwald 
833a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
834a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
835a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
836a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
837667ec068SMatthias Ringwald         return;
838667ec068SMatthias Ringwald     }
839667ec068SMatthias Ringwald 
840a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
841a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
842667ec068SMatthias Ringwald         char buffer[20];
843a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
844667ec068SMatthias Ringwald             case '?':
8451599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
846ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
847ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
848a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
849667ec068SMatthias Ringwald                 return;
850667ec068SMatthias Ringwald             case '0':
851667ec068SMatthias Ringwald             case '1':
852667ec068SMatthias Ringwald             case '2':
8531599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
854ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
855ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
856ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
857a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
858667ec068SMatthias Ringwald                 return;
859667ec068SMatthias Ringwald             default:
860667ec068SMatthias Ringwald                 break;
861667ec068SMatthias Ringwald         }
862667ec068SMatthias Ringwald         return;
863667ec068SMatthias Ringwald     }
864667ec068SMatthias Ringwald 
865a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
866a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
867667ec068SMatthias Ringwald         char buffer[20];
8681599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
869ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
870ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
871a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
872667ec068SMatthias Ringwald         return;
873667ec068SMatthias Ringwald     }
874667ec068SMatthias Ringwald 
875667ec068SMatthias Ringwald     // update HF indicators
876a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
877667ec068SMatthias Ringwald         int i;
878667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
879a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
880a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
881a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
882a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
883667ec068SMatthias Ringwald                     char buffer[30];
8841599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
885ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
886ff7d6aeaSMatthias Ringwald                              hfp_indicators[i],
887ff7d6aeaSMatthias Ringwald                              (unsigned int)hfp_indicators_value[i]);
888ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
889a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
890667ec068SMatthias Ringwald                 } else {
89160ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
892667ec068SMatthias Ringwald                 }
893667ec068SMatthias Ringwald                 return;
894667ec068SMatthias Ringwald             }
895667ec068SMatthias Ringwald         }
896667ec068SMatthias Ringwald     }
897667ec068SMatthias Ringwald 
898ce263fc8SMatthias Ringwald     if (done) return;
899ce263fc8SMatthias Ringwald     // deal with disconnect
900a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
901ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
902a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
903a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
904ce263fc8SMatthias Ringwald             break;
905ce263fc8SMatthias Ringwald 
906ce263fc8SMatthias Ringwald         default:
907ce263fc8SMatthias Ringwald             break;
908ce263fc8SMatthias Ringwald     }
909ce263fc8SMatthias Ringwald }
910ce263fc8SMatthias Ringwald 
911a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
912a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
9136a7f44bdSMilanka Ringwald 
914ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
9157522e673SMatthias Ringwald 
916667ec068SMatthias Ringwald     // restore volume settings
917a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
918a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
919ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
920a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
921a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
922ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
923667ec068SMatthias Ringwald     // enable all indicators
924667ec068SMatthias Ringwald     int i;
925667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
926a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
927a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
928667ec068SMatthias Ringwald     }
929ce263fc8SMatthias Ringwald }
930ce263fc8SMatthias Ringwald 
9311cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
9321cc65c4fSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
9331cc65c4fSMatthias Ringwald 		// Codec supported, confirm
9341cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
9351cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
9361cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
9371cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
9381cc65c4fSMatthias Ringwald 
9391cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
9401cc65c4fSMatthias Ringwald 	} else {
9411cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
9421cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
9431cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
9441cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
9451cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9461cc65c4fSMatthias Ringwald 
9471cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
9481cc65c4fSMatthias Ringwald 	}
9491cc65c4fSMatthias Ringwald }
9501cc65c4fSMatthias Ringwald 
951a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
952a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
9533deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
954a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
955a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
9563deb3ec6SMatthias Ringwald                 break;
9573deb3ec6SMatthias Ringwald             }
958a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9593deb3ec6SMatthias Ringwald             break;
9603deb3ec6SMatthias Ringwald 
9613deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
962a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9633deb3ec6SMatthias Ringwald             break;
9643deb3ec6SMatthias Ringwald 
9653deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
966a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
9673deb3ec6SMatthias Ringwald             break;
9683deb3ec6SMatthias Ringwald 
9693deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
970a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
9713deb3ec6SMatthias Ringwald             break;
9723deb3ec6SMatthias Ringwald 
9733deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
974a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
975a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
9763deb3ec6SMatthias Ringwald                 break;
9773deb3ec6SMatthias Ringwald             }
978a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
979a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9803deb3ec6SMatthias Ringwald                 break;
9813deb3ec6SMatthias Ringwald             }
982a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9833deb3ec6SMatthias Ringwald             break;
9843deb3ec6SMatthias Ringwald 
9853deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
986a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
987a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9883deb3ec6SMatthias Ringwald                 break;
9893deb3ec6SMatthias Ringwald             }
990a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9913deb3ec6SMatthias Ringwald             break;
9923deb3ec6SMatthias Ringwald 
9933deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
994a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
9953deb3ec6SMatthias Ringwald             break;
9963deb3ec6SMatthias Ringwald 
9973deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
998a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9993deb3ec6SMatthias Ringwald             break;
10003deb3ec6SMatthias Ringwald 
10013deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1002a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
10033deb3ec6SMatthias Ringwald             break;
1004ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1005a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1006a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1007ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1008ce263fc8SMatthias Ringwald                 break;
1009ce263fc8SMatthias Ringwald             }
10103deb3ec6SMatthias Ringwald 
1011a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1012a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1013ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1014ce263fc8SMatthias Ringwald                 break;
10153deb3ec6SMatthias Ringwald             }
10163deb3ec6SMatthias Ringwald 
1017a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
1018ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1019a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1020ce263fc8SMatthias Ringwald                     break;
1021ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1022a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1023a473a009SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_connection);
1024ce263fc8SMatthias Ringwald                     break;
1025ce263fc8SMatthias Ringwald                 default:
1026ce263fc8SMatthias Ringwald                     break;
10273deb3ec6SMatthias Ringwald             }
1028ce263fc8SMatthias Ringwald 
1029a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
1030a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
1031ce263fc8SMatthias Ringwald                 break;
10323deb3ec6SMatthias Ringwald             }
10333deb3ec6SMatthias Ringwald 
1034a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
1035aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1036a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10373deb3ec6SMatthias Ringwald                     break;
1038ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
1039a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1040ce263fc8SMatthias Ringwald                     break;
10413deb3ec6SMatthias Ringwald                 default:
10423deb3ec6SMatthias Ringwald                     break;
10433deb3ec6SMatthias Ringwald             }
1044*af97579eSMilanka Ringwald             hfp_hf_voice_recognition_state_machine(hfp_connection);
1045be55a11dSMilanka Ringwald             break;
1046be55a11dSMilanka Ringwald         case HFP_AUDIO_CONNECTION_ESTABLISHED:
1047*af97579eSMilanka Ringwald             hfp_hf_voice_recognition_state_machine(hfp_connection);
10483deb3ec6SMatthias Ringwald             break;
10493deb3ec6SMatthias Ringwald         default:
10503deb3ec6SMatthias Ringwald             break;
10513deb3ec6SMatthias Ringwald     }
10523deb3ec6SMatthias Ringwald 
10533deb3ec6SMatthias Ringwald     // done
1054be55a11dSMilanka Ringwald     hfp_connection->ok_pending = 0;
1055a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
10563deb3ec6SMatthias Ringwald }
10573deb3ec6SMatthias Ringwald 
1058be55a11dSMilanka Ringwald 
1059b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
10604562e2a2SMatthias Ringwald     uint16_t i;
10614562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
10624562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
10634562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
10644562e2a2SMatthias Ringwald                 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
10654562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
10664562e2a2SMatthias Ringwald                 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
10674562e2a2SMatthias Ringwald                 // avoid set but not used warning
10684562e2a2SMatthias Ringwald                 (void) hfp_callheld_status;
10694562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
10704562e2a2SMatthias Ringwald                 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
10714562e2a2SMatthias Ringwald             }
10724562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1073a473a009SMatthias Ringwald             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
10744562e2a2SMatthias Ringwald             break;
10754562e2a2SMatthias Ringwald         }
10764562e2a2SMatthias Ringwald     }
10774562e2a2SMatthias Ringwald }
10784562e2a2SMatthias Ringwald 
1079426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1080186dd3d2SMatthias Ringwald     int value;
1081186dd3d2SMatthias Ringwald     int i;
1082a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1083667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1084a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1085a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1086667ec068SMatthias Ringwald             break;
1087667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1088a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1089ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1090667ec068SMatthias Ringwald             break;
1091667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1092a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1093a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1094667ec068SMatthias Ringwald             break;
1095ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1096a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10972308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1098667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1099ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1100ce263fc8SMatthias Ringwald             break;
1101ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1102a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11032308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1104667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1105ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1106ce263fc8SMatthias Ringwald             break;
1107ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1108a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1109ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1110a0ffb263SMatthias Ringwald             break;
1111a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1112a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1113a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1114a0ffb263SMatthias Ringwald             break;
1115a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1116a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1117a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1118ce263fc8SMatthias Ringwald             break;
1119ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1120a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11215a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1122a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1123ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1124ce263fc8SMatthias Ringwald             break;
1125ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1126a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1127a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1128be55a11dSMilanka Ringwald             hfp_connection->command = HFP_CMD_NONE;
112956f1adacSMilanka Ringwald 
113090244c92SMilanka Ringwald             switch (hfp_connection->state){
113190244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
113290244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
113390244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
113490244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
113590244c92SMilanka Ringwald                             return;
113690244c92SMilanka Ringwald                         default:
113790244c92SMilanka Ringwald                             break;
113890244c92SMilanka Ringwald                     }
113956f1adacSMilanka Ringwald                     break;
114056f1adacSMilanka Ringwald                 default:
114156f1adacSMilanka Ringwald                     break;
114256f1adacSMilanka Ringwald             }
114356f1adacSMilanka Ringwald 
1144be55a11dSMilanka Ringwald 
1145db3cdbd4SMilanka Ringwald             switch (hfp_connection->vra_status){
1146be55a11dSMilanka Ringwald                 case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1147db3cdbd4SMilanka Ringwald                     hfp_connection->vra_status = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
1148be55a11dSMilanka Ringwald                     break;
1149be55a11dSMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF:
1150db3cdbd4SMilanka Ringwald                     hfp_connection->vra_status = HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED;
1151be55a11dSMilanka Ringwald                     break;
1152be55a11dSMilanka Ringwald                 case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1153be55a11dSMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
1154db3cdbd4SMilanka Ringwald                     hfp_connection->vra_status = HFP_VRA_VOICE_RECOGNITION_OFF;
1155be55a11dSMilanka Ringwald                     break;
1156be55a11dSMilanka Ringwald                 default:
1157be55a11dSMilanka Ringwald                     break;
1158be55a11dSMilanka Ringwald             }
1159be55a11dSMilanka Ringwald 
1160ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1161ce263fc8SMatthias Ringwald             break;
1162ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1163a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1164ce263fc8SMatthias Ringwald             break;
1165ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
11665a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1167ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1168ce263fc8SMatthias Ringwald             break;
1169ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
11705a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11714562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1172ce263fc8SMatthias Ringwald             break;
1173c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
11745a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1175c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1176a473a009SMatthias Ringwald                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1177c741b032SMilanka Ringwald             }
1178c741b032SMilanka Ringwald             break;
11791cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
11801cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11815a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
11821cc65c4fSMatthias Ringwald 			break;
1183eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1184eac56539SMilanka 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));
1185ce263fc8SMatthias Ringwald         default:
1186ce263fc8SMatthias Ringwald             break;
11873deb3ec6SMatthias Ringwald     }
11880cef86faSMatthias Ringwald }
1189426f9988SMatthias Ringwald 
119076cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
119176cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
119276cc1527SMatthias Ringwald }
119376cc1527SMatthias Ringwald 
1194426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1195426f9988SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
1196426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1197426f9988SMatthias Ringwald     if (size < 1) return;
1198426f9988SMatthias Ringwald 
1199426f9988SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1200426f9988SMatthias Ringwald     if (!hfp_connection) return;
1201426f9988SMatthias Ringwald 
1202426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1203e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1204e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1205e43d1938SMatthias Ringwald #endif
1206426f9988SMatthias Ringwald 
1207426f9988SMatthias Ringwald     // process messages byte-wise
1208426f9988SMatthias Ringwald     int pos;
1209426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1210426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
1211426f9988SMatthias Ringwald 
12121599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1213426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1214426f9988SMatthias Ringwald 
1215426f9988SMatthias Ringwald         hfp_hf_handle_rfcomm_command(hfp_connection);
1216426f9988SMatthias Ringwald     }
12171c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
12183deb3ec6SMatthias Ringwald }
12193deb3ec6SMatthias Ringwald 
12201c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1221665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1222665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1223665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1224a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
122522387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
12261c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
12273deb3ec6SMatthias Ringwald     }
12283deb3ec6SMatthias Ringwald }
12293deb3ec6SMatthias Ringwald 
12301c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
12313deb3ec6SMatthias Ringwald     switch (packet_type){
12323deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1233426f9988SMatthias Ringwald             hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size);
12343deb3ec6SMatthias Ringwald             break;
12353deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1236d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1237d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
12381c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1239d4dd47ffSMatthias Ringwald                 return;
1240d4dd47ffSMatthias Ringwald             }
124127950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1242202c8a4cSMatthias Ringwald             break;
12433deb3ec6SMatthias Ringwald         default:
12443deb3ec6SMatthias Ringwald             break;
12453deb3ec6SMatthias Ringwald     }
12461c6a0fc0SMatthias Ringwald     hfp_hf_run();
12473deb3ec6SMatthias Ringwald }
12483deb3ec6SMatthias Ringwald 
12491c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1250405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
12511c6a0fc0SMatthias Ringwald     hfp_hf_run();
1252405014fbSMatthias Ringwald }
1253405014fbSMatthias Ringwald 
1254b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1255b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1256b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1257b4df8028SMilanka Ringwald         return status;
1258b4df8028SMilanka Ringwald     }
1259b4df8028SMilanka Ringwald 
1260520c92d5SMatthias Ringwald     hfp_init();
126120b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
126220b2edb6SMatthias Ringwald     hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
126320b2edb6SMatthias Ringwald     hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
126420b2edb6SMatthias Ringwald     hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
126520b2edb6SMatthias Ringwald     hfp_codecs_nr = 0;
126620b2edb6SMatthias Ringwald     hfp_hf_speaker_gain = 9;
126720b2edb6SMatthias Ringwald     hfp_hf_microphone_gain = 9;
126820b2edb6SMatthias Ringwald     hfp_indicators_nr = 0;
126920b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1270d63c37a1SMatthias Ringwald 
12711c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
12721c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
127327950165SMatthias Ringwald 
127427950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
12751c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1276b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
127720b2edb6SMatthias Ringwald }
127827950165SMatthias Ringwald 
127920b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
128020b2edb6SMatthias Ringwald     hfp_deinit();
128120b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
128220b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t));
128320b2edb6SMatthias Ringwald     (void) memset(phone_number, 0, sizeof(phone_number));
1284a0ffb263SMatthias Ringwald }
1285a0ffb263SMatthias Ringwald 
12867ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
128768466199SMilanka Ringwald     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
12883deb3ec6SMatthias Ringwald 
12893deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
12903deb3ec6SMatthias Ringwald     int i;
12913deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
12923deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
12933deb3ec6SMatthias Ringwald     }
12943deb3ec6SMatthias Ringwald }
12953deb3ec6SMatthias Ringwald 
1296a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
12973deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1298a0ffb263SMatthias Ringwald }
12993deb3ec6SMatthias Ringwald 
13007ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
130168466199SMilanka Ringwald     btstack_assert(hfp_indicators_nr < HFP_MAX_NUM_INDICATORS);
130268466199SMilanka Ringwald 
13033deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
13043deb3ec6SMatthias Ringwald     int i;
1305a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
13063deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
13073deb3ec6SMatthias Ringwald     }
13083deb3ec6SMatthias Ringwald }
13093deb3ec6SMatthias Ringwald 
13104eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
13114eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
13123deb3ec6SMatthias Ringwald }
13133deb3ec6SMatthias Ringwald 
1314657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
13159c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1316a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1317657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1318a33eb0c4SMilanka Ringwald     }
13191ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
13201c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1321657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
13223deb3ec6SMatthias Ringwald }
13233deb3ec6SMatthias Ringwald 
13243c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
13259c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1326a0ffb263SMatthias Ringwald     if (!hfp_connection) {
13273c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
13283deb3ec6SMatthias Ringwald     }
1329a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
13301c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13313c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
13323deb3ec6SMatthias Ringwald }
13333deb3ec6SMatthias Ringwald 
13343c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
13353c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1336ce263fc8SMatthias Ringwald }
1337ce263fc8SMatthias Ringwald 
13383c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
13393c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1340ce263fc8SMatthias Ringwald }
1341ce263fc8SMatthias Ringwald 
13423deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
13433c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
13449c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1345a0ffb263SMatthias Ringwald     if (!hfp_connection) {
13463c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
13473deb3ec6SMatthias Ringwald     }
1348a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1349a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
13501c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13513c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
13523deb3ec6SMatthias Ringwald }
13533deb3ec6SMatthias Ringwald 
13543c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
13559c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1356a0ffb263SMatthias Ringwald     if (!hfp_connection) {
13573c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
13583deb3ec6SMatthias Ringwald     }
13593c65e705SMilanka Ringwald 
1360a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1361ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1362a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1363ce263fc8SMatthias Ringwald             break;
1364ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1365a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1366ce263fc8SMatthias Ringwald             break;
1367ce263fc8SMatthias Ringwald         default:
13683c65e705SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1369ce263fc8SMatthias Ringwald     }
13701c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13713c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
13723deb3ec6SMatthias Ringwald }
13733deb3ec6SMatthias Ringwald 
13743c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
13759c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1376a0ffb263SMatthias Ringwald     if (!hfp_connection) {
13773c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
13783deb3ec6SMatthias Ringwald     }
1379a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
13801c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13813c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
13823deb3ec6SMatthias Ringwald }
13833deb3ec6SMatthias Ringwald 
1384ce263fc8SMatthias Ringwald 
13853c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
13863c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1387ce263fc8SMatthias Ringwald }
1388ce263fc8SMatthias Ringwald 
13893c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
13903c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1391ce263fc8SMatthias Ringwald }
1392ce263fc8SMatthias Ringwald 
139338200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
139438200c1dSMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
139538200c1dSMilanka Ringwald }
1396ce263fc8SMatthias Ringwald 
13973c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
13989c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1399a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14003c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1401a33eb0c4SMilanka Ringwald     }
1402ce263fc8SMatthias Ringwald 
14033c65e705SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
14043c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
14053c65e705SMilanka Ringwald     }
14063c65e705SMilanka Ringwald 
14073c65e705SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
14083c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
14093c65e705SMilanka Ringwald     }
14103deb3ec6SMatthias Ringwald 
1411f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1412a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1413aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1414aa4dd815SMatthias Ringwald                 break;
1415ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1416ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1417ec3bfc1aSMatthias Ringwald                 break;
1418aa4dd815SMatthias Ringwald             default:
14191cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
14201cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
14211cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
14221cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
142338200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1424aa4dd815SMatthias Ringwald                 break;
14253deb3ec6SMatthias Ringwald         }
1426f4412093SMatthias Ringwald     } else {
1427f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1428f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1429f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1430f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1431f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1432f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1433f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1434f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1435ce263fc8SMatthias Ringwald     }
1436ce263fc8SMatthias Ringwald 
14371c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14383c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14393deb3ec6SMatthias Ringwald }
14403deb3ec6SMatthias Ringwald 
14413c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
14429c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1443a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14443c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1445a33eb0c4SMilanka Ringwald     }
14461ffa0dd9SMilanka Ringwald     hfp_trigger_release_audio_connection(hfp_connection);
14471c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14483c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14493deb3ec6SMatthias Ringwald }
14503deb3ec6SMatthias Ringwald 
14513c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
14529c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1453a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14543c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1455a33eb0c4SMilanka Ringwald     }
1456ce263fc8SMatthias Ringwald 
1457ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1458a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
14591c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1460ce263fc8SMatthias Ringwald     } else {
1461ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
14623c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1463ce263fc8SMatthias Ringwald     }
14643c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1465ce263fc8SMatthias Ringwald }
1466ce263fc8SMatthias Ringwald 
14673c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
14689c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1469a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14703c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1471a33eb0c4SMilanka Ringwald     }
1472a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
14731c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14743c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1475ce263fc8SMatthias Ringwald }
1476ce263fc8SMatthias Ringwald 
14773c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
14789c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1479a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14803c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1481a33eb0c4SMilanka Ringwald     }
1482ce263fc8SMatthias Ringwald 
1483ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1484a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
14851c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1486ce263fc8SMatthias Ringwald     }
14873c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1488ce263fc8SMatthias Ringwald }
1489ce263fc8SMatthias Ringwald 
14903c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){
14919c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1492a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14933c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1494a33eb0c4SMilanka Ringwald     }
1495ce263fc8SMatthias Ringwald 
1496ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1497a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
14981c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1499ce263fc8SMatthias Ringwald     }
15003c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1501ce263fc8SMatthias Ringwald }
1502ce263fc8SMatthias Ringwald 
15033c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
15049c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1505a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15063c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1507a33eb0c4SMilanka Ringwald     }
1508ce263fc8SMatthias Ringwald 
1509505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1510505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1511a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
15121c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1513ce263fc8SMatthias Ringwald     }
15143c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1515ce263fc8SMatthias Ringwald }
1516ce263fc8SMatthias Ringwald 
15173c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
15189c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1519a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15203c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1521a33eb0c4SMilanka Ringwald     }
1522ce263fc8SMatthias Ringwald 
1523505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1524505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1525a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
15261c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1527ce263fc8SMatthias Ringwald     }
15283c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1529ce263fc8SMatthias Ringwald }
1530ce263fc8SMatthias Ringwald 
15313c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
15329c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1533a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15343c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1535a33eb0c4SMilanka Ringwald     }
1536ce263fc8SMatthias Ringwald 
1537505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1538505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1539a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
15401c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1541ce263fc8SMatthias Ringwald     }
15423c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1543ce263fc8SMatthias Ringwald }
1544ce263fc8SMatthias Ringwald 
15453c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
15469c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1547a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15483c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1549a33eb0c4SMilanka Ringwald     }
1550ce263fc8SMatthias Ringwald 
1551505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1552505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1553a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
15541c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1555ce263fc8SMatthias Ringwald     }
15563c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1557ce263fc8SMatthias Ringwald }
1558ce263fc8SMatthias Ringwald 
15593c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
15609c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1561a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15623c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1563a33eb0c4SMilanka Ringwald     }
1564667ec068SMatthias Ringwald 
1565505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1566505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1567a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1568a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
15691c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1570667ec068SMatthias Ringwald     }
15713c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1572667ec068SMatthias Ringwald }
1573667ec068SMatthias Ringwald 
15743c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
15759c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1576a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15773c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1578a33eb0c4SMilanka Ringwald     }
1579667ec068SMatthias Ringwald 
1580505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1581505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1582a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1583a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
15841c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1585667ec068SMatthias Ringwald     }
15863c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1587667ec068SMatthias Ringwald }
1588ce263fc8SMatthias Ringwald 
15893c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
15909c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1591a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15923c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1593a33eb0c4SMilanka Ringwald     }
1594ce263fc8SMatthias Ringwald 
1595a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1596ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
15971c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15983c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1599ce263fc8SMatthias Ringwald }
1600ce263fc8SMatthias Ringwald 
16013c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
16029c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1603a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16043c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1605a33eb0c4SMilanka Ringwald     }
1606ce263fc8SMatthias Ringwald 
1607a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1608a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1609a0ffb263SMatthias Ringwald 
16101c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16113c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1612ce263fc8SMatthias Ringwald }
1613ce263fc8SMatthias Ringwald 
16143c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
16159c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1616a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16173c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1618a33eb0c4SMilanka Ringwald     }
1619ce263fc8SMatthias Ringwald 
1620a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
16211c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16223c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1623ce263fc8SMatthias Ringwald }
1624ce263fc8SMatthias Ringwald 
16253c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
16269c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1627a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16283c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1629a33eb0c4SMilanka Ringwald     }
1630ce263fc8SMatthias Ringwald 
1631a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
16321c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16333c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1634ce263fc8SMatthias Ringwald }
1635ce263fc8SMatthias Ringwald 
1636ce263fc8SMatthias Ringwald 
16373c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
16389c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1639a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16403c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1641a33eb0c4SMilanka Ringwald     }
1642ce263fc8SMatthias Ringwald 
1643a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
16441c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16453c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1646ce263fc8SMatthias Ringwald }
1647ce263fc8SMatthias Ringwald 
1648ce263fc8SMatthias Ringwald 
16493c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
16509c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1651a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16523c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1653a33eb0c4SMilanka Ringwald     }
1654ce263fc8SMatthias Ringwald 
1655a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
16561c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16573c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1658ce263fc8SMatthias Ringwald }
1659ce263fc8SMatthias Ringwald 
16603c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
16619c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1662a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16633c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1664a33eb0c4SMilanka Ringwald     }
1665ce263fc8SMatthias Ringwald 
1666a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
16671c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16683c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1669ce263fc8SMatthias Ringwald }
1670ce263fc8SMatthias Ringwald 
1671ce263fc8SMatthias Ringwald 
16723c65e705SMilanka Ringwald uint8_t hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
16739c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1674a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16753c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1676a33eb0c4SMilanka Ringwald     }
1677ce263fc8SMatthias Ringwald 
1678a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
16791c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16803c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1681ce263fc8SMatthias Ringwald }
1682ce263fc8SMatthias Ringwald 
16833c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
16849c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1685a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16863c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1687a33eb0c4SMilanka Ringwald     }
1688ce263fc8SMatthias Ringwald 
1689a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
16901c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16913c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1692ce263fc8SMatthias Ringwald }
1693ce263fc8SMatthias Ringwald 
1694be55a11dSMilanka Ringwald static bool hfp_hf_enhanced_voice_recognition_supported(hfp_connection_t * hfp_connection){
1695be55a11dSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1696be55a11dSMilanka Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1697be55a11dSMilanka Ringwald     return hf && ag;
1698be55a11dSMilanka Ringwald }
1699be55a11dSMilanka Ringwald 
1700be55a11dSMilanka Ringwald static bool hfp_hf_voice_recognition_supported(hfp_connection_t * hfp_connection){
1701be55a11dSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION);
1702be55a11dSMilanka Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION);
1703be55a11dSMilanka Ringwald     return hf && ag;
1704be55a11dSMilanka Ringwald }
1705be55a11dSMilanka Ringwald 
1706*af97579eSMilanka Ringwald static uint8_t hfp_hf_activate_enhanced_voice_recognition_for_connection(hfp_connection_t * hfp_connection){
1707*af97579eSMilanka Ringwald     if (!hfp_hf_enhanced_voice_recognition_supported(hfp_connection)){
1708*af97579eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1709be55a11dSMilanka Ringwald     }
1710*af97579eSMilanka Ringwald     switch (hfp_connection->vra_status){
1711*af97579eSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1712*af97579eSMilanka Ringwald             hfp_connection->vra_status = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED;
1713*af97579eSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
1714*af97579eSMilanka Ringwald             break;
1715*af97579eSMilanka Ringwald         default:
1716*af97579eSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1717*af97579eSMilanka Ringwald     }
1718*af97579eSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1719*af97579eSMilanka Ringwald }
1720*af97579eSMilanka Ringwald 
1721*af97579eSMilanka Ringwald static uint8_t hfp_hf_deactivate_enhanced_voice_recognition_for_connection(hfp_connection_t * hfp_connection){
1722*af97579eSMilanka Ringwald     if (!hfp_hf_enhanced_voice_recognition_supported(hfp_connection)){
1723*af97579eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1724*af97579eSMilanka Ringwald     }
1725*af97579eSMilanka Ringwald     switch (hfp_connection->vra_status){
1726*af97579eSMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
1727*af97579eSMilanka Ringwald             hfp_connection->vra_status = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF;
1728*af97579eSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
1729*af97579eSMilanka Ringwald             break;
1730*af97579eSMilanka Ringwald         default:
1731*af97579eSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1732*af97579eSMilanka Ringwald     }
1733*af97579eSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1734*af97579eSMilanka Ringwald }
1735*af97579eSMilanka Ringwald 
1736*af97579eSMilanka Ringwald static uint8_t hfp_hf_deactivate_voice_recognition_for_connection(hfp_connection_t * hfp_connection){
1737be55a11dSMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection)){
1738be55a11dSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1739a33eb0c4SMilanka Ringwald     }
1740ce263fc8SMatthias Ringwald 
1741db3cdbd4SMilanka Ringwald     switch (hfp_connection->vra_status){
1742be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1743*af97579eSMilanka Ringwald             hfp_connection->vra_status = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
1744*af97579eSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
1745be55a11dSMilanka Ringwald             break;
1746*af97579eSMilanka Ringwald         default:
1747*af97579eSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1748*af97579eSMilanka Ringwald     }
1749*af97579eSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1750*af97579eSMilanka Ringwald }
1751*af97579eSMilanka Ringwald 
1752*af97579eSMilanka Ringwald static uint8_t hfp_hf_activate_voice_recognition_for_connection(hfp_connection_t * hfp_connection){
1753*af97579eSMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection)){
1754*af97579eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1755*af97579eSMilanka Ringwald     }
1756*af97579eSMilanka Ringwald 
1757*af97579eSMilanka Ringwald     switch (hfp_connection->vra_status){
1758be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1759db3cdbd4SMilanka Ringwald             hfp_connection->vra_status = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
17601c6a0fc0SMatthias Ringwald             hfp_hf_run_for_context(hfp_connection);
1761be55a11dSMilanka Ringwald             break;
1762be55a11dSMilanka Ringwald         default:
1763be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1764be55a11dSMilanka Ringwald     }
1765be55a11dSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1766ce263fc8SMatthias Ringwald }
1767ce263fc8SMatthias Ringwald 
1768be55a11dSMilanka Ringwald 
1769*af97579eSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1770*af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1771*af97579eSMilanka Ringwald     if (!hfp_connection) {
1772*af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1773*af97579eSMilanka Ringwald     }
1774*af97579eSMilanka Ringwald     uint8_t status = hfp_hf_activate_voice_recognition_for_connection(hfp_connection);
1775*af97579eSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
1776*af97579eSMilanka Ringwald         hfp_hf_run_for_context(hfp_connection);
1777*af97579eSMilanka Ringwald     }
1778*af97579eSMilanka Ringwald     return status;
1779*af97579eSMilanka Ringwald }
1780*af97579eSMilanka Ringwald 
1781*af97579eSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1782*af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1783*af97579eSMilanka Ringwald     if (!hfp_connection) {
1784*af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1785*af97579eSMilanka Ringwald     }
1786*af97579eSMilanka Ringwald     uint8_t status = hfp_hf_deactivate_voice_recognition_for_connection(hfp_connection);
1787*af97579eSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
1788*af97579eSMilanka Ringwald         hfp_hf_run_for_context(hfp_connection);
1789*af97579eSMilanka Ringwald     }
1790*af97579eSMilanka Ringwald     return status;
1791*af97579eSMilanka Ringwald }
1792*af97579eSMilanka Ringwald 
1793db3cdbd4SMilanka Ringwald uint8_t hfp_hf_start_enhanced_voice_recognition_session(hci_con_handle_t acl_handle){
17949c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1795a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1796be55a11dSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1797a33eb0c4SMilanka Ringwald     }
1798*af97579eSMilanka Ringwald     uint8_t status = hfp_hf_activate_enhanced_voice_recognition_for_connection(hfp_connection);
1799*af97579eSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
18001c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1801be55a11dSMilanka Ringwald     }
1802*af97579eSMilanka Ringwald     return status;
1803be55a11dSMilanka Ringwald }
1804be55a11dSMilanka Ringwald 
1805db3cdbd4SMilanka Ringwald uint8_t hfp_hf_stop_enhanced_voice_recognition_session(hci_con_handle_t acl_handle){
1806be55a11dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1807be55a11dSMilanka Ringwald     if (!hfp_connection) {
1808be55a11dSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1809be55a11dSMilanka Ringwald     }
1810*af97579eSMilanka Ringwald 
1811*af97579eSMilanka Ringwald     uint8_t status = hfp_hf_deactivate_enhanced_voice_recognition_for_connection(hfp_connection);
1812*af97579eSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
1813*af97579eSMilanka Ringwald         hfp_hf_run_for_context(hfp_connection);
1814be55a11dSMilanka Ringwald     }
1815*af97579eSMilanka Ringwald     return status;
1816ce263fc8SMatthias Ringwald }
1817ce263fc8SMatthias Ringwald 
18183c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
18199c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1820a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18213c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1822a33eb0c4SMilanka Ringwald     }
1823c8626498SMilanka Ringwald 
18243c65e705SMilanka Ringwald     if (hfp_connection->microphone_gain == gain) {
18253c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
18263c65e705SMilanka Ringwald     }
18273c65e705SMilanka Ringwald 
1828c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1829a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
18303c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1831a0ffb263SMatthias Ringwald     }
18323c65e705SMilanka Ringwald 
1833a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1834a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
18351c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18363c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1837ce263fc8SMatthias Ringwald }
1838ce263fc8SMatthias Ringwald 
18393c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
18409c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1841a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18423c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1843a33eb0c4SMilanka Ringwald     }
1844c8626498SMilanka Ringwald 
18453c65e705SMilanka Ringwald     if (hfp_connection->speaker_gain == gain){
18463c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
18473c65e705SMilanka Ringwald     }
18483c65e705SMilanka Ringwald 
1849c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1850a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
18513c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1852a0ffb263SMatthias Ringwald     }
18533c65e705SMilanka Ringwald 
1854a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1855a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
18561c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18573c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1858ce263fc8SMatthias Ringwald }
1859ce263fc8SMatthias Ringwald 
18603c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
18619c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1862a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18633c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1864a33eb0c4SMilanka Ringwald     }
1865a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
18661c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18673c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1868ce263fc8SMatthias Ringwald }
1869ce263fc8SMatthias Ringwald 
18703c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
18719c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1872a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18733c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1874a33eb0c4SMilanka Ringwald     }
1875a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
18761c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18773c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1878ce263fc8SMatthias Ringwald }
18793deb3ec6SMatthias Ringwald 
18803c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
18819c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1882a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18833c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1884a33eb0c4SMilanka Ringwald     }
1885a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
18861c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18873c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1888667ec068SMatthias Ringwald }
1889667ec068SMatthias Ringwald 
1890667ec068SMatthias Ringwald 
18913c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
18929c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1893a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18943c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1895a33eb0c4SMilanka Ringwald     }
1896a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1897a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
18981c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18993c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1900667ec068SMatthias Ringwald }
1901667ec068SMatthias Ringwald 
19023c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
19039c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1904a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19053c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1906a33eb0c4SMilanka Ringwald     }
1907a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1908a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
19091c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19103c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1911667ec068SMatthias Ringwald }
1912667ec068SMatthias Ringwald 
19133c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
19149c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1915a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19163c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1917a33eb0c4SMilanka Ringwald     }
1918a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1919a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
19201c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19213c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1922667ec068SMatthias Ringwald }
1923667ec068SMatthias Ringwald 
19243c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
19259c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1926a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19273c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1928a33eb0c4SMilanka Ringwald     }
1929a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1930a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
19311c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19323c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1933667ec068SMatthias Ringwald }
1934667ec068SMatthias Ringwald 
19353c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
19369c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1937a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19383c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1939a33eb0c4SMilanka Ringwald     }
1940a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
19411c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19423c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1943667ec068SMatthias Ringwald }
1944667ec068SMatthias Ringwald 
19453c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
19469c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1947a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19483c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1949a33eb0c4SMilanka Ringwald     }
1950667ec068SMatthias Ringwald     // find index for assigned number
1951667ec068SMatthias Ringwald     int i;
1952667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1953667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1954667ec068SMatthias Ringwald             // set value
1955667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1956667ec068SMatthias Ringwald             // mark for update
1957a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1958a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1959667ec068SMatthias Ringwald                 // send update
19601c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
1961a0ffb263SMatthias Ringwald             }
19623c65e705SMilanka Ringwald             return ERROR_CODE_SUCCESS;
1963667ec068SMatthias Ringwald         }
1964667ec068SMatthias Ringwald     }
19653c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1966667ec068SMatthias Ringwald }
1967667ec068SMatthias Ringwald 
1968d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
1969d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1970d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
1971d7f6b5cbSMatthias Ringwald         return 0;
1972d7f6b5cbSMatthias Ringwald     }
1973d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1974d7f6b5cbSMatthias Ringwald }
197576cc1527SMatthias Ringwald 
197676cc1527SMatthias 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){
197776cc1527SMatthias Ringwald 	if (!name){
197876cc1527SMatthias Ringwald 		name = default_hfp_hf_service_name;
197976cc1527SMatthias Ringwald 	}
198076cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
198176cc1527SMatthias Ringwald 
198276cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
198376cc1527SMatthias Ringwald 	//
198476cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
198576cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
198676cc1527SMatthias Ringwald 	//
198776cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
198876cc1527SMatthias Ringwald 	//
198976cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
1990ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
199176cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
199276cc1527SMatthias Ringwald 	}
1993ef3ae4ebSMilanka Ringwald 
1994ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
199556f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
1996ef3ae4ebSMilanka Ringwald     }
1997ef3ae4ebSMilanka Ringwald 
1998ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
199956f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
2000ef3ae4ebSMilanka Ringwald     }
2001ef3ae4ebSMilanka Ringwald 
200276cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
200376cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
200476cc1527SMatthias Ringwald }
200576cc1527SMatthias Ringwald 
200676cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
200768466199SMilanka Ringwald 	btstack_assert(callback != NULL);
200868466199SMilanka Ringwald 
200976cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
201076cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
201176cc1527SMatthias Ringwald }
2012