xref: /btstack/src/classic/hfp_hf.c (revision 013cc750cf6346288ea6beee2f41b1f1f37056d5)
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;
386498a8121SMilanka Ringwald     log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state);
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;
435498a8121SMilanka Ringwald     if (hfp_connection->ok_pending){
436498a8121SMilanka Ringwald         return 0;
437498a8121SMilanka Ringwald     }
438ce263fc8SMatthias Ringwald     int done = 0;
439a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
440a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
441ce263fc8SMatthias Ringwald         done = 1;
442a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
443ce263fc8SMatthias Ringwald         return done;
444ce263fc8SMatthias Ringwald     };
445a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
446a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
447ce263fc8SMatthias Ringwald         done = 1;
448a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
449a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
450a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
451ce263fc8SMatthias Ringwald         return done;
452ce263fc8SMatthias Ringwald     }
453ce263fc8SMatthias Ringwald 
454a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
455ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
456a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
457a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
458a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
459ce263fc8SMatthias Ringwald             return 1;
460ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
461a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
462a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
463a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
464ce263fc8SMatthias Ringwald             return 1;
465ce263fc8SMatthias Ringwald         default:
466ce263fc8SMatthias Ringwald             break;
467ce263fc8SMatthias Ringwald     }
468ce263fc8SMatthias Ringwald 
469a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
470a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
471ce263fc8SMatthias Ringwald         done = 1;
472a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
473ce263fc8SMatthias Ringwald         return done;
474ce263fc8SMatthias Ringwald     }
475ce263fc8SMatthias Ringwald 
476ce263fc8SMatthias Ringwald     return done;
477ce263fc8SMatthias Ringwald }
478ce263fc8SMatthias Ringwald 
479af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
480be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
481be55a11dSMilanka Ringwald         return 0;
482be55a11dSMilanka Ringwald     }
483be55a11dSMilanka Ringwald     int done = 0;
484fd4151d1SMilanka Ringwald 
4850b4debbfSMilanka Ringwald     if (hfp_connection->ok_pending == 1){
4860b4debbfSMilanka Ringwald         return 0;
4870b4debbfSMilanka Ringwald     }
4880b4debbfSMilanka Ringwald     // voice recognition activated from AG
4890b4debbfSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){
4900b4debbfSMilanka Ringwald         switch(hfp_connection->vra_state_requested){
4910b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
4920b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
493*013cc750SMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF:
4940b4debbfSMilanka Ringwald                 // ignore AG command, continue to wait for OK
4950b4debbfSMilanka Ringwald                 return 0;
4960b4debbfSMilanka Ringwald             default:
497*013cc750SMilanka Ringwald                 switch (hfp_connection->ag_vra_status){
498*013cc750SMilanka Ringwald                     case 0:
4990b4debbfSMilanka Ringwald                         hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
500*013cc750SMilanka Ringwald                         break;
501*013cc750SMilanka Ringwald                     case 1:
502*013cc750SMilanka Ringwald                         hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
503*013cc750SMilanka Ringwald                         break;
504*013cc750SMilanka Ringwald                     case 2:
505*013cc750SMilanka Ringwald                         hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
506*013cc750SMilanka Ringwald                         break;
507*013cc750SMilanka Ringwald                     default:
508*013cc750SMilanka Ringwald                         break;
5090b4debbfSMilanka Ringwald                 }
5100b4debbfSMilanka Ringwald                 break;
5110b4debbfSMilanka Ringwald         }
5120b4debbfSMilanka Ringwald         hfp_connection->command = HFP_CMD_NONE;
5130b4debbfSMilanka Ringwald     }
5140b4debbfSMilanka Ringwald 
5150b4debbfSMilanka Ringwald 
516498a8121SMilanka Ringwald     switch (hfp_connection->vra_state_requested){
517fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
518fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
519fdda66c0SMilanka Ringwald             if (done != 0){
520fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
521498a8121SMilanka Ringwald                 hfp_connection->ok_pending = 1;
522498a8121SMilanka Ringwald             }
523fd4151d1SMilanka Ringwald             return 1;
524fd4151d1SMilanka Ringwald 
525fdda66c0SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
526fdda66c0SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
527fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
5280b4debbfSMilanka Ringwald             hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS, hfp_connection->vra_state);
529fdda66c0SMilanka Ringwald             break;
530fd4151d1SMilanka Ringwald 
531fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
532fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
533fdda66c0SMilanka Ringwald             if (done != 0){
534fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
535fd4151d1SMilanka Ringwald                 hfp_connection->ok_pending = 1;
536fd4151d1SMilanka Ringwald                 return 1;
5370b4debbfSMilanka Ringwald             }
5380b4debbfSMilanka Ringwald             break;
539*013cc750SMilanka Ringwald 
540be55a11dSMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
541498a8121SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
542498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
5430b4debbfSMilanka Ringwald             hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS, hfp_connection->vra_state);
544be55a11dSMilanka Ringwald             break;
545be55a11dSMilanka Ringwald 
546*013cc750SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
547fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
548fdda66c0SMilanka Ringwald             if (done != 0){
549*013cc750SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
550fdda66c0SMilanka Ringwald                 hfp_connection->ok_pending = 1;
551fdda66c0SMilanka Ringwald             }
552fdda66c0SMilanka Ringwald             return 1;
553fdda66c0SMilanka Ringwald 
554*013cc750SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
555*013cc750SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
556498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
5570b4debbfSMilanka Ringwald             hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS, hfp_connection->vra_state);
558be55a11dSMilanka Ringwald             break;
559fd4151d1SMilanka Ringwald 
560be55a11dSMilanka Ringwald         default:
5610b4debbfSMilanka Ringwald 
562be55a11dSMilanka Ringwald             break;
563be55a11dSMilanka Ringwald     }
564be55a11dSMilanka Ringwald     return done;
565be55a11dSMilanka Ringwald }
566be55a11dSMilanka Ringwald 
567be55a11dSMilanka Ringwald 
568be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
569a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
570ce263fc8SMatthias Ringwald 
571332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
572332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
573ce263fc8SMatthias Ringwald 
574a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
575a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
576332ca98fSMatthias Ringwald 		return 1;
577332ca98fSMatthias Ringwald     }
578332ca98fSMatthias Ringwald 
5791cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
5801cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
581ce263fc8SMatthias Ringwald 
582a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
583fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
5841cc65c4fSMatthias Ringwald 		return 1;
5851cc65c4fSMatthias Ringwald     }
5861cc65c4fSMatthias Ringwald 
5871cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
5881cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
5891cc65c4fSMatthias Ringwald 
590a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
591a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
5921cc65c4fSMatthias Ringwald 		return 1;
5931cc65c4fSMatthias Ringwald     }
594ce263fc8SMatthias Ringwald 
595ce263fc8SMatthias Ringwald     return 0;
596ce263fc8SMatthias Ringwald }
597ce263fc8SMatthias Ringwald 
598a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
599505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
600505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
601ce263fc8SMatthias Ringwald 
60264f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
603a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
604a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
605a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
606ce263fc8SMatthias Ringwald         return 1;
607ce263fc8SMatthias Ringwald     }
608ce263fc8SMatthias Ringwald 
609a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
610ce263fc8SMatthias Ringwald 
611ce263fc8SMatthias Ringwald     // run codecs exchange
612a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
613ce263fc8SMatthias Ringwald     if (done) return 1;
614ce263fc8SMatthias Ringwald 
61538200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
61638200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
61738200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
61838200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
61938200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
62038200c1dSMilanka Ringwald         return 1;
62138200c1dSMilanka Ringwald     }
622ce263fc8SMatthias Ringwald     return 0;
623ce263fc8SMatthias Ringwald }
624ce263fc8SMatthias Ringwald 
62538200c1dSMilanka Ringwald 
626a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
627eaf2b0a1SMatthias Ringwald 
628eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
629eaf2b0a1SMatthias Ringwald 
630a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
631a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
632a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
633ce263fc8SMatthias Ringwald         return 1;
634ce263fc8SMatthias Ringwald     }
635ce263fc8SMatthias Ringwald     return 0;
636ce263fc8SMatthias Ringwald }
637ce263fc8SMatthias Ringwald 
6381c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
6397522e673SMatthias Ringwald 
64076cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
64176cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
64276cc1527SMatthias Ringwald 
64376cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
64476cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
64522387625SMatthias Ringwald 
6463721a235SMatthias Ringwald 	// assert command could be sent
6473721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
6483721a235SMatthias Ringwald 
6493721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
6503721a235SMatthias Ringwald     // WBS Disassociate
6513721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
6523721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
6533721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
6543721a235SMatthias Ringwald         return;
6553721a235SMatthias Ringwald     }
6563721a235SMatthias Ringwald     // Write Codec Config
6573721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
6583721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
6593721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
6603721a235SMatthias Ringwald         return;
6613721a235SMatthias Ringwald     }
6623721a235SMatthias Ringwald     // WBS Associate
6633721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
6643721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
6653721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
6663721a235SMatthias Ringwald         return;
6673721a235SMatthias Ringwald     }
6683721a235SMatthias Ringwald #endif
669689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
670689d4323SMatthias Ringwald     // Enable WBS
671689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
672689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
673689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
674689d4323SMatthias Ringwald         return;
675689d4323SMatthias Ringwald     }
676689d4323SMatthias Ringwald     // Write I2S/PCM params
677689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
678689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
679689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
680689d4323SMatthias Ringwald         return;
681689d4323SMatthias Ringwald     }
682689d4323SMatthias Ringwald     // Disable WBS
683689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
684689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
685689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
686689d4323SMatthias Ringwald         return;
687689d4323SMatthias Ringwald     }
688689d4323SMatthias Ringwald #endif
68948e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
69048e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
69148e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
69248e6eeeeSMatthias Ringwald         return;
69348e6eeeeSMatthias Ringwald     }
69448e6eeeeSMatthias Ringwald #endif
6953721a235SMatthias Ringwald 
696cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
697cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
698cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
6997522e673SMatthias Ringwald         // notify about codec selection if not done already
7007522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
7017522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
7027522e673SMatthias Ringwald         }
703cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
7047522e673SMatthias Ringwald         return;
7057522e673SMatthias Ringwald     }
7067522e673SMatthias Ringwald 
707d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
708d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
709d4dd47ffSMatthias Ringwald         return;
710d4dd47ffSMatthias Ringwald     }
711a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
712ce263fc8SMatthias Ringwald     if (!done){
713a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
714ce263fc8SMatthias Ringwald     }
715ce263fc8SMatthias Ringwald     if (!done){
716af97579eSMilanka Ringwald         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
717be55a11dSMilanka Ringwald     }
718be55a11dSMilanka Ringwald     if (!done){
719a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
720ce263fc8SMatthias Ringwald     }
721ce263fc8SMatthias Ringwald     if (!done){
722a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
723ce263fc8SMatthias Ringwald     }
724ce263fc8SMatthias Ringwald 
7251016a228SMatthias Ringwald     // don't send a new command while ok still pending
7261016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
7271016a228SMatthias Ringwald 
728a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
729a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
730a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
731a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
732ce263fc8SMatthias Ringwald         return;
733ce263fc8SMatthias Ringwald     }
734ce263fc8SMatthias Ringwald 
735a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
736a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
737a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
738a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
739ce263fc8SMatthias Ringwald         return;
740ce263fc8SMatthias Ringwald     }
741ce263fc8SMatthias Ringwald 
742a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
743a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
744a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
745a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
746ce263fc8SMatthias Ringwald         return;
747ce263fc8SMatthias Ringwald     }
748ce263fc8SMatthias Ringwald 
749a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
750a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
751a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
752a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
753ce263fc8SMatthias Ringwald         return;
754ce263fc8SMatthias Ringwald     }
755ce263fc8SMatthias Ringwald 
756a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
757a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
758a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
759a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
760ce263fc8SMatthias Ringwald         return;
761ce263fc8SMatthias Ringwald     }
762ce263fc8SMatthias Ringwald 
763a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
764a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
765a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
766a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
767ce263fc8SMatthias Ringwald         return;
768ce263fc8SMatthias Ringwald     }
769ce263fc8SMatthias Ringwald 
770a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
771a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
772a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
773a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
774ce263fc8SMatthias Ringwald         return;
775ce263fc8SMatthias Ringwald     }
776ce263fc8SMatthias Ringwald 
777a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
778a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
779a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
780a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
781ce263fc8SMatthias Ringwald         return;
782ce263fc8SMatthias Ringwald     }
783ce263fc8SMatthias Ringwald 
784a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
785a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
786a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
787a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
788ce263fc8SMatthias Ringwald         return;
789ce263fc8SMatthias Ringwald     }
790ce263fc8SMatthias Ringwald 
791a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
792a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
793a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
794a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
795ce263fc8SMatthias Ringwald         return;
796ce263fc8SMatthias Ringwald     }
797ce263fc8SMatthias Ringwald 
798a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
799a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
800a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
801a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
802ce263fc8SMatthias Ringwald         return;
803ce263fc8SMatthias Ringwald     }
804ce263fc8SMatthias Ringwald 
805a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
806a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
807a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
808a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
809ce263fc8SMatthias Ringwald         return;
810ce263fc8SMatthias Ringwald     }
811ce263fc8SMatthias Ringwald 
812a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
813a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
814a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
815a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
816ce263fc8SMatthias Ringwald         return;
817ce263fc8SMatthias Ringwald     }
818ce263fc8SMatthias Ringwald 
819a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
820a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
821a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
822a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
823ce263fc8SMatthias Ringwald         return;
824ce263fc8SMatthias Ringwald     }
825ce263fc8SMatthias Ringwald 
826a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
827a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
828a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
829a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
830ce263fc8SMatthias Ringwald         return;
831ce263fc8SMatthias Ringwald     }
832ce263fc8SMatthias Ringwald 
833a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
834a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
835a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
836a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
837ce263fc8SMatthias Ringwald         return;
838ce263fc8SMatthias Ringwald     }
839ce263fc8SMatthias Ringwald 
840a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
841a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
842a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
843a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
844ce263fc8SMatthias Ringwald         return;
845ce263fc8SMatthias Ringwald     }
846ce263fc8SMatthias Ringwald 
847a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
848a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
849a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
850a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
851667ec068SMatthias Ringwald         return;
852667ec068SMatthias Ringwald     }
853667ec068SMatthias Ringwald 
854a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
855a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
856a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
857a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
858a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
859ce263fc8SMatthias Ringwald         return;
860ce263fc8SMatthias Ringwald     }
861ce263fc8SMatthias Ringwald 
862a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
863a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
864a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
865a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
866ce263fc8SMatthias Ringwald         return;
867ce263fc8SMatthias Ringwald     }
868ce263fc8SMatthias Ringwald 
869a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
870a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
871a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
872a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
873667ec068SMatthias Ringwald         return;
874667ec068SMatthias Ringwald     }
875667ec068SMatthias Ringwald 
876a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
877a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
878667ec068SMatthias Ringwald         char buffer[20];
879a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
880667ec068SMatthias Ringwald             case '?':
8811599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
882ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
883ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
884a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
885667ec068SMatthias Ringwald                 return;
886667ec068SMatthias Ringwald             case '0':
887667ec068SMatthias Ringwald             case '1':
888667ec068SMatthias Ringwald             case '2':
8891599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
890ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
891ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
892ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
893a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
894667ec068SMatthias Ringwald                 return;
895667ec068SMatthias Ringwald             default:
896667ec068SMatthias Ringwald                 break;
897667ec068SMatthias Ringwald         }
898667ec068SMatthias Ringwald         return;
899667ec068SMatthias Ringwald     }
900667ec068SMatthias Ringwald 
901a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
902a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
903667ec068SMatthias Ringwald         char buffer[20];
9041599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
905ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
906ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
907a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
908667ec068SMatthias Ringwald         return;
909667ec068SMatthias Ringwald     }
910667ec068SMatthias Ringwald 
911667ec068SMatthias Ringwald     // update HF indicators
912a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
913667ec068SMatthias Ringwald         int i;
914667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
915a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
916a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
917a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
918a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
919667ec068SMatthias Ringwald                     char buffer[30];
9201599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
921ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
922ff7d6aeaSMatthias Ringwald                              hfp_indicators[i],
923ff7d6aeaSMatthias Ringwald                              (unsigned int)hfp_indicators_value[i]);
924ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
925a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
926667ec068SMatthias Ringwald                 } else {
92760ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
928667ec068SMatthias Ringwald                 }
929667ec068SMatthias Ringwald                 return;
930667ec068SMatthias Ringwald             }
931667ec068SMatthias Ringwald         }
932667ec068SMatthias Ringwald     }
933667ec068SMatthias Ringwald 
934ce263fc8SMatthias Ringwald     if (done) return;
935ce263fc8SMatthias Ringwald     // deal with disconnect
936a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
937ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
938a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
939a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
940ce263fc8SMatthias Ringwald             break;
941ce263fc8SMatthias Ringwald 
942ce263fc8SMatthias Ringwald         default:
943ce263fc8SMatthias Ringwald             break;
944ce263fc8SMatthias Ringwald     }
945ce263fc8SMatthias Ringwald }
946ce263fc8SMatthias Ringwald 
947a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
948a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
9496a7f44bdSMilanka Ringwald 
950ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
9517522e673SMatthias Ringwald 
952667ec068SMatthias Ringwald     // restore volume settings
953a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
954a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
955ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
956a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
957a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
958ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
959667ec068SMatthias Ringwald     // enable all indicators
960667ec068SMatthias Ringwald     int i;
961667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
962a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
963a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
964667ec068SMatthias Ringwald     }
965ce263fc8SMatthias Ringwald }
966ce263fc8SMatthias Ringwald 
9671cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
9681cc65c4fSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
9691cc65c4fSMatthias Ringwald 		// Codec supported, confirm
9701cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
9711cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
9721cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
9731cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
9741cc65c4fSMatthias Ringwald 
9751cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
9761cc65c4fSMatthias Ringwald 	} else {
9771cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
9781cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
9791cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
9801cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
9811cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9821cc65c4fSMatthias Ringwald 
9831cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
9841cc65c4fSMatthias Ringwald 	}
9851cc65c4fSMatthias Ringwald }
9861cc65c4fSMatthias Ringwald 
987a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
988a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
9893deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
990a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
991a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
9923deb3ec6SMatthias Ringwald                 break;
9933deb3ec6SMatthias Ringwald             }
994a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9953deb3ec6SMatthias Ringwald             break;
9963deb3ec6SMatthias Ringwald 
9973deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
998a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9993deb3ec6SMatthias Ringwald             break;
10003deb3ec6SMatthias Ringwald 
10013deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
1002a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
10033deb3ec6SMatthias Ringwald             break;
10043deb3ec6SMatthias Ringwald 
10053deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1006a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
10073deb3ec6SMatthias Ringwald             break;
10083deb3ec6SMatthias Ringwald 
10093deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1010a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1011a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
10123deb3ec6SMatthias Ringwald                 break;
10133deb3ec6SMatthias Ringwald             }
1014a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
1015a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
10163deb3ec6SMatthias Ringwald                 break;
10173deb3ec6SMatthias Ringwald             }
1018a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
10193deb3ec6SMatthias Ringwald             break;
10203deb3ec6SMatthias Ringwald 
10213deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1022a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
1023a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
10243deb3ec6SMatthias Ringwald                 break;
10253deb3ec6SMatthias Ringwald             }
1026a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
10273deb3ec6SMatthias Ringwald             break;
10283deb3ec6SMatthias Ringwald 
10293deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1030a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
10313deb3ec6SMatthias Ringwald             break;
10323deb3ec6SMatthias Ringwald 
10333deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1034a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
10353deb3ec6SMatthias Ringwald             break;
10363deb3ec6SMatthias Ringwald 
10373deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1038a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
10393deb3ec6SMatthias Ringwald             break;
1040ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1041a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1042a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1043ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1044ce263fc8SMatthias Ringwald                 break;
1045ce263fc8SMatthias Ringwald             }
10463deb3ec6SMatthias Ringwald 
1047a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1048a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1049ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1050ce263fc8SMatthias Ringwald                 break;
10513deb3ec6SMatthias Ringwald             }
10523deb3ec6SMatthias Ringwald 
1053a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
1054ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1055a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1056ce263fc8SMatthias Ringwald                     break;
1057ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1058a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1059a473a009SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_connection);
1060ce263fc8SMatthias Ringwald                     break;
1061ce263fc8SMatthias Ringwald                 default:
1062ce263fc8SMatthias Ringwald                     break;
10633deb3ec6SMatthias Ringwald             }
1064ce263fc8SMatthias Ringwald 
1065a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
1066a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
1067ce263fc8SMatthias Ringwald                 break;
10683deb3ec6SMatthias Ringwald             }
10693deb3ec6SMatthias Ringwald 
1070a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
1071aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1072a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10733deb3ec6SMatthias Ringwald                     break;
1074ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
1075a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1076ce263fc8SMatthias Ringwald                     break;
10773deb3ec6SMatthias Ringwald                 default:
10783deb3ec6SMatthias Ringwald                     break;
10793deb3ec6SMatthias Ringwald             }
1080af97579eSMilanka Ringwald             hfp_hf_voice_recognition_state_machine(hfp_connection);
1081be55a11dSMilanka Ringwald             break;
1082be55a11dSMilanka Ringwald         case HFP_AUDIO_CONNECTION_ESTABLISHED:
1083af97579eSMilanka Ringwald             hfp_hf_voice_recognition_state_machine(hfp_connection);
10843deb3ec6SMatthias Ringwald             break;
10853deb3ec6SMatthias Ringwald         default:
10863deb3ec6SMatthias Ringwald             break;
10873deb3ec6SMatthias Ringwald     }
10883deb3ec6SMatthias Ringwald 
10893deb3ec6SMatthias Ringwald     // done
1090be55a11dSMilanka Ringwald     hfp_connection->ok_pending = 0;
1091a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
10923deb3ec6SMatthias Ringwald }
10933deb3ec6SMatthias Ringwald 
1094be55a11dSMilanka Ringwald 
1095b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
10964562e2a2SMatthias Ringwald     uint16_t i;
10974562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
10984562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
10994562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
11004562e2a2SMatthias Ringwald                 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
11014562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
11024562e2a2SMatthias Ringwald                 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
11034562e2a2SMatthias Ringwald                 // avoid set but not used warning
11044562e2a2SMatthias Ringwald                 (void) hfp_callheld_status;
11054562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
11064562e2a2SMatthias Ringwald                 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
11074562e2a2SMatthias Ringwald             }
11084562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1109a473a009SMatthias Ringwald             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
11104562e2a2SMatthias Ringwald             break;
11114562e2a2SMatthias Ringwald         }
11124562e2a2SMatthias Ringwald     }
11134562e2a2SMatthias Ringwald }
11144562e2a2SMatthias Ringwald 
1115426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1116186dd3d2SMatthias Ringwald     int value;
1117186dd3d2SMatthias Ringwald     int i;
1118a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1119667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1120a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1121a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1122667ec068SMatthias Ringwald             break;
1123667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1124a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1125ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1126667ec068SMatthias Ringwald             break;
1127667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1128a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1129a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1130667ec068SMatthias Ringwald             break;
1131ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1132a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11332308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1134667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1135ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1136ce263fc8SMatthias Ringwald             break;
1137ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1138a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11392308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1140667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1141ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1142ce263fc8SMatthias Ringwald             break;
1143ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1144a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1145ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1146a0ffb263SMatthias Ringwald             break;
1147a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1148a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1149a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1150a0ffb263SMatthias Ringwald             break;
1151a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1152a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1153a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1154ce263fc8SMatthias Ringwald             break;
1155ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1156a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11575a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1158a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1159ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1160ce263fc8SMatthias Ringwald             break;
11610b4debbfSMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
11620b4debbfSMilanka Ringwald             break;
1163fdda66c0SMilanka Ringwald         case HFP_CMD_ERROR:
116490244c92SMilanka Ringwald             switch (hfp_connection->state){
116590244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
116690244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
116790244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1168fdda66c0SMilanka Ringwald                             hfp_reset_context_flags(hfp_connection);
116990244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
117090244c92SMilanka Ringwald                             return;
117190244c92SMilanka Ringwald                         default:
117290244c92SMilanka Ringwald                             break;
117390244c92SMilanka Ringwald                     }
117456f1adacSMilanka Ringwald                     break;
117556f1adacSMilanka Ringwald                 default:
117656f1adacSMilanka Ringwald                     break;
117756f1adacSMilanka Ringwald             }
1178fdda66c0SMilanka Ringwald             // handle error response for voice activation (HF initiated)
11790b4debbfSMilanka Ringwald             switch(hfp_connection->vra_state_requested){
11800b4debbfSMilanka Ringwald                 case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
11810b4debbfSMilanka Ringwald                 case HFP_VRA_VOICE_RECOGNITION_OFF:
1182be55a11dSMilanka Ringwald                     break;
1183be55a11dSMilanka Ringwald                 default:
11840b4debbfSMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
11850b4debbfSMilanka Ringwald                     hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR, hfp_connection->vra_state);
11860b4debbfSMilanka Ringwald                     hfp_reset_context_flags(hfp_connection);
11870b4debbfSMilanka Ringwald                     return;
1188be55a11dSMilanka Ringwald             }
11890b4debbfSMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1190fdda66c0SMilanka Ringwald             hfp_reset_context_flags(hfp_connection);
1191ce263fc8SMatthias Ringwald             break;
1192fdda66c0SMilanka Ringwald 
1193ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1194a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1195ce263fc8SMatthias Ringwald             break;
1196ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
11975a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1198ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1199ce263fc8SMatthias Ringwald             break;
1200ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
12015a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12024562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1203ce263fc8SMatthias Ringwald             break;
1204c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
12055a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1206c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1207a473a009SMatthias Ringwald                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1208c741b032SMilanka Ringwald             }
1209c741b032SMilanka Ringwald             break;
12101cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
12111cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12125a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
12131cc65c4fSMatthias Ringwald 			break;
1214eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1215eac56539SMilanka 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));
1216ce263fc8SMatthias Ringwald         default:
1217ce263fc8SMatthias Ringwald             break;
12183deb3ec6SMatthias Ringwald     }
12190cef86faSMatthias Ringwald }
1220426f9988SMatthias Ringwald 
122176cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
122276cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
122376cc1527SMatthias Ringwald }
122476cc1527SMatthias Ringwald 
12250b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1226426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1227426f9988SMatthias Ringwald     if (size < 1) return;
1228426f9988SMatthias Ringwald 
1229426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1230e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1231e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1232e43d1938SMatthias Ringwald #endif
1233426f9988SMatthias Ringwald 
1234426f9988SMatthias Ringwald     // process messages byte-wise
1235426f9988SMatthias Ringwald     int pos;
1236426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1237426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
12381599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1239426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1240426f9988SMatthias Ringwald     }
12410b4debbfSMilanka Ringwald     hfp_hf_handle_rfcomm_command(hfp_connection);
12423deb3ec6SMatthias Ringwald }
12433deb3ec6SMatthias Ringwald 
12441c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1245665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1246665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1247665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1248a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
124922387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
12501c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
12513deb3ec6SMatthias Ringwald     }
12523deb3ec6SMatthias Ringwald }
12533deb3ec6SMatthias Ringwald 
12541c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
12550b4debbfSMilanka Ringwald     hfp_connection_t * hfp_connection;
12563deb3ec6SMatthias Ringwald     switch (packet_type){
12573deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
12580b4debbfSMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
12590b4debbfSMilanka Ringwald             if (!hfp_connection) return;
12600b4debbfSMilanka Ringwald             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
12610b4debbfSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
12620b4debbfSMilanka Ringwald             return;
12633deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1264d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1265d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
12661c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1267d4dd47ffSMatthias Ringwald                 return;
1268d4dd47ffSMatthias Ringwald             }
126927950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1270202c8a4cSMatthias Ringwald             break;
12713deb3ec6SMatthias Ringwald         default:
12723deb3ec6SMatthias Ringwald             break;
12733deb3ec6SMatthias Ringwald     }
12741c6a0fc0SMatthias Ringwald     hfp_hf_run();
12753deb3ec6SMatthias Ringwald }
12763deb3ec6SMatthias Ringwald 
12771c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1278405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
12791c6a0fc0SMatthias Ringwald     hfp_hf_run();
1280405014fbSMatthias Ringwald }
1281405014fbSMatthias Ringwald 
1282b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1283b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1284b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1285b4df8028SMilanka Ringwald         return status;
1286b4df8028SMilanka Ringwald     }
1287b4df8028SMilanka Ringwald 
1288520c92d5SMatthias Ringwald     hfp_init();
128920b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
129020b2edb6SMatthias Ringwald     hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
129120b2edb6SMatthias Ringwald     hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
129220b2edb6SMatthias Ringwald     hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
129320b2edb6SMatthias Ringwald     hfp_codecs_nr = 0;
129420b2edb6SMatthias Ringwald     hfp_hf_speaker_gain = 9;
129520b2edb6SMatthias Ringwald     hfp_hf_microphone_gain = 9;
129620b2edb6SMatthias Ringwald     hfp_indicators_nr = 0;
129720b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1298d63c37a1SMatthias Ringwald 
12991c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
13001c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
130127950165SMatthias Ringwald 
130227950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
13031c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1304b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
130520b2edb6SMatthias Ringwald }
130627950165SMatthias Ringwald 
130720b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
130820b2edb6SMatthias Ringwald     hfp_deinit();
130920b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
131020b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t));
131120b2edb6SMatthias Ringwald     (void) memset(phone_number, 0, sizeof(phone_number));
1312a0ffb263SMatthias Ringwald }
1313a0ffb263SMatthias Ringwald 
13147ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
131568466199SMilanka Ringwald     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
13163deb3ec6SMatthias Ringwald 
13173deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
13183deb3ec6SMatthias Ringwald     int i;
13193deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
13203deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
13213deb3ec6SMatthias Ringwald     }
13223deb3ec6SMatthias Ringwald }
13233deb3ec6SMatthias Ringwald 
1324a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
13253deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1326a0ffb263SMatthias Ringwald }
13273deb3ec6SMatthias Ringwald 
13287ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
132968466199SMilanka Ringwald     btstack_assert(hfp_indicators_nr < HFP_MAX_NUM_INDICATORS);
133068466199SMilanka Ringwald 
13313deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
13323deb3ec6SMatthias Ringwald     int i;
1333a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
13343deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
13353deb3ec6SMatthias Ringwald     }
13363deb3ec6SMatthias Ringwald }
13373deb3ec6SMatthias Ringwald 
13384eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
13394eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
13403deb3ec6SMatthias Ringwald }
13413deb3ec6SMatthias Ringwald 
1342657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
13439c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1344a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1345657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1346a33eb0c4SMilanka Ringwald     }
13471ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
13481c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1349657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
13503deb3ec6SMatthias Ringwald }
13513deb3ec6SMatthias Ringwald 
13523c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
13539c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1354a0ffb263SMatthias Ringwald     if (!hfp_connection) {
13553c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
13563deb3ec6SMatthias Ringwald     }
1357a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
13581c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13593c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
13603deb3ec6SMatthias Ringwald }
13613deb3ec6SMatthias Ringwald 
13623c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
13633c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1364ce263fc8SMatthias Ringwald }
1365ce263fc8SMatthias Ringwald 
13663c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
13673c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1368ce263fc8SMatthias Ringwald }
1369ce263fc8SMatthias Ringwald 
13703deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
13713c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
13729c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1373a0ffb263SMatthias Ringwald     if (!hfp_connection) {
13743c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
13753deb3ec6SMatthias Ringwald     }
1376a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1377a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
13781c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13793c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
13803deb3ec6SMatthias Ringwald }
13813deb3ec6SMatthias Ringwald 
13823c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
13839c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1384a0ffb263SMatthias Ringwald     if (!hfp_connection) {
13853c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
13863deb3ec6SMatthias Ringwald     }
13873c65e705SMilanka Ringwald 
1388a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1389ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1390a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1391ce263fc8SMatthias Ringwald             break;
1392ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1393a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1394ce263fc8SMatthias Ringwald             break;
1395ce263fc8SMatthias Ringwald         default:
13963c65e705SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1397ce263fc8SMatthias Ringwald     }
13981c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13993c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14003deb3ec6SMatthias Ringwald }
14013deb3ec6SMatthias Ringwald 
14023c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
14039c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1404a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14053c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14063deb3ec6SMatthias Ringwald     }
1407a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
14081c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14093c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14103deb3ec6SMatthias Ringwald }
14113deb3ec6SMatthias Ringwald 
1412ce263fc8SMatthias Ringwald 
14133c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
14143c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1415ce263fc8SMatthias Ringwald }
1416ce263fc8SMatthias Ringwald 
14173c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
14183c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1419ce263fc8SMatthias Ringwald }
1420ce263fc8SMatthias Ringwald 
142138200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
142238200c1dSMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
142338200c1dSMilanka Ringwald }
1424ce263fc8SMatthias Ringwald 
14253c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
14269c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1427a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14283c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1429a33eb0c4SMilanka Ringwald     }
1430ce263fc8SMatthias Ringwald 
14313c65e705SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
14323c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
14333c65e705SMilanka Ringwald     }
14343c65e705SMilanka Ringwald 
14353c65e705SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
14363c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
14373c65e705SMilanka Ringwald     }
1438f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1439a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1440aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1441aa4dd815SMatthias Ringwald                 break;
1442ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1443ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1444ec3bfc1aSMatthias Ringwald                 break;
1445aa4dd815SMatthias Ringwald             default:
14461cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
14471cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
14481cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
14491cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
145038200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1451aa4dd815SMatthias Ringwald                 break;
14523deb3ec6SMatthias Ringwald         }
1453f4412093SMatthias Ringwald     } else {
1454f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1455f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1456f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1457f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1458f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1459f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1460f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1461f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1462ce263fc8SMatthias Ringwald     }
1463ce263fc8SMatthias Ringwald 
14641c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14653c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14663deb3ec6SMatthias Ringwald }
14673deb3ec6SMatthias Ringwald 
14683c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
14699c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1470a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14713c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1472a33eb0c4SMilanka Ringwald     }
14730b4debbfSMilanka Ringwald     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
14740b4debbfSMilanka Ringwald         if (!hfp_connection->ag_audio_connection_opened_after_vra){
14750b4debbfSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
14760b4debbfSMilanka Ringwald         }
14770b4debbfSMilanka Ringwald     }
14780b4debbfSMilanka Ringwald     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
14790b4debbfSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
14801c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
14810b4debbfSMilanka Ringwald     }
14823c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14833deb3ec6SMatthias Ringwald }
14843deb3ec6SMatthias Ringwald 
14853c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
14869c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1487a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14883c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1489a33eb0c4SMilanka Ringwald     }
1490ce263fc8SMatthias Ringwald 
1491ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1492a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
14931c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1494ce263fc8SMatthias Ringwald     } else {
1495ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
14963c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1497ce263fc8SMatthias Ringwald     }
14983c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1499ce263fc8SMatthias Ringwald }
1500ce263fc8SMatthias Ringwald 
15013c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
15029c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1503a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15043c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1505a33eb0c4SMilanka Ringwald     }
1506a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
15071c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15083c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1509ce263fc8SMatthias Ringwald }
1510ce263fc8SMatthias Ringwald 
15113c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
15129c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1513a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15143c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1515a33eb0c4SMilanka Ringwald     }
1516ce263fc8SMatthias Ringwald 
1517ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1518a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
15191c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1520ce263fc8SMatthias Ringwald     }
15213c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1522ce263fc8SMatthias Ringwald }
1523ce263fc8SMatthias Ringwald 
15243c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){
15259c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1526a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15273c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1528a33eb0c4SMilanka Ringwald     }
1529ce263fc8SMatthias Ringwald 
1530ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1531a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
15321c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1533ce263fc8SMatthias Ringwald     }
15343c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1535ce263fc8SMatthias Ringwald }
1536ce263fc8SMatthias Ringwald 
15373c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
15389c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1539a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15403c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1541a33eb0c4SMilanka Ringwald     }
1542ce263fc8SMatthias Ringwald 
1543505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1544505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1545a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
15461c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1547ce263fc8SMatthias Ringwald     }
15483c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1549ce263fc8SMatthias Ringwald }
1550ce263fc8SMatthias Ringwald 
15513c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
15529c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1553a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15543c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1555a33eb0c4SMilanka Ringwald     }
1556ce263fc8SMatthias Ringwald 
1557505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1558505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1559a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
15601c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1561ce263fc8SMatthias Ringwald     }
15623c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1563ce263fc8SMatthias Ringwald }
1564ce263fc8SMatthias Ringwald 
15653c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
15669c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1567a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15683c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1569a33eb0c4SMilanka Ringwald     }
1570ce263fc8SMatthias Ringwald 
1571505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1572505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1573a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
15741c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1575ce263fc8SMatthias Ringwald     }
15763c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1577ce263fc8SMatthias Ringwald }
1578ce263fc8SMatthias Ringwald 
15793c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
15809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1581a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15823c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1583a33eb0c4SMilanka Ringwald     }
1584ce263fc8SMatthias Ringwald 
1585505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1586505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1587a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
15881c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1589ce263fc8SMatthias Ringwald     }
15903c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1591ce263fc8SMatthias Ringwald }
1592ce263fc8SMatthias Ringwald 
15933c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
15949c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1595a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15963c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1597a33eb0c4SMilanka Ringwald     }
1598667ec068SMatthias Ringwald 
1599505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1600505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1601a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1602a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
16031c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1604667ec068SMatthias Ringwald     }
16053c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1606667ec068SMatthias Ringwald }
1607667ec068SMatthias Ringwald 
16083c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
16099c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1610a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16113c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1612a33eb0c4SMilanka Ringwald     }
1613667ec068SMatthias Ringwald 
1614505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1615505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1616a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1617a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
16181c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1619667ec068SMatthias Ringwald     }
16203c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1621667ec068SMatthias Ringwald }
1622ce263fc8SMatthias Ringwald 
16233c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
16249c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1625a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16263c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1627a33eb0c4SMilanka Ringwald     }
1628ce263fc8SMatthias Ringwald 
1629a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1630ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
16311c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16323c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1633ce263fc8SMatthias Ringwald }
1634ce263fc8SMatthias Ringwald 
16353c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
16369c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1637a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16383c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1639a33eb0c4SMilanka Ringwald     }
1640ce263fc8SMatthias Ringwald 
1641a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1642a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1643a0ffb263SMatthias Ringwald 
16441c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16453c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1646ce263fc8SMatthias Ringwald }
1647ce263fc8SMatthias Ringwald 
16483c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
16499c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1650a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16513c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1652a33eb0c4SMilanka Ringwald     }
1653ce263fc8SMatthias Ringwald 
1654a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
16551c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16563c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1657ce263fc8SMatthias Ringwald }
1658ce263fc8SMatthias Ringwald 
16593c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
16609c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1661a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16623c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1663a33eb0c4SMilanka Ringwald     }
1664ce263fc8SMatthias Ringwald 
1665a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
16661c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16673c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1668ce263fc8SMatthias Ringwald }
1669ce263fc8SMatthias Ringwald 
1670ce263fc8SMatthias Ringwald 
16713c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
16729c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1673a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16743c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1675a33eb0c4SMilanka Ringwald     }
1676ce263fc8SMatthias Ringwald 
1677a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
16781c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16793c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1680ce263fc8SMatthias Ringwald }
1681ce263fc8SMatthias Ringwald 
1682ce263fc8SMatthias Ringwald 
16833c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(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_activate_calling_line_notification = 1;
16901c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16913c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1692ce263fc8SMatthias Ringwald }
1693ce263fc8SMatthias Ringwald 
16943c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
16959c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1696a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16973c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1698a33eb0c4SMilanka Ringwald     }
1699ce263fc8SMatthias Ringwald 
1700a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
17011c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17023c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1703ce263fc8SMatthias Ringwald }
1704ce263fc8SMatthias Ringwald 
1705ce263fc8SMatthias Ringwald 
17063c65e705SMilanka Ringwald uint8_t hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
17079c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1708a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17093c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1710a33eb0c4SMilanka Ringwald     }
1711ce263fc8SMatthias Ringwald 
1712a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
17131c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17143c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1715ce263fc8SMatthias Ringwald }
1716ce263fc8SMatthias Ringwald 
17173c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
17189c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1719a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17203c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1721a33eb0c4SMilanka Ringwald     }
1722ce263fc8SMatthias Ringwald 
1723a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
17241c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17253c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1726ce263fc8SMatthias Ringwald }
1727ce263fc8SMatthias Ringwald 
1728*013cc750SMilanka Ringwald static bool hfp_hf_voice_recognition_supported(hfp_connection_t * hfp_connection, bool enhanced){
1729*013cc750SMilanka Ringwald     uint8_t hf_vra_flag = HFP_HFSF_VOICE_RECOGNITION_FUNCTION;
1730*013cc750SMilanka Ringwald     uint8_t ag_vra_flag = HFP_AGSF_VOICE_RECOGNITION_FUNCTION;
1731*013cc750SMilanka Ringwald 
1732*013cc750SMilanka Ringwald     if (enhanced){
1733*013cc750SMilanka Ringwald         hf_vra_flag = HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS;
1734*013cc750SMilanka Ringwald         ag_vra_flag = HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS;
1735*013cc750SMilanka Ringwald     }
1736*013cc750SMilanka Ringwald 
1737*013cc750SMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, ag_vra_flag);
1738*013cc750SMilanka Ringwald     int hf = get_bit(hfp_supported_features, hf_vra_flag);
1739be55a11dSMilanka Ringwald     return hf && ag;
1740be55a11dSMilanka Ringwald }
1741be55a11dSMilanka Ringwald 
1742*013cc750SMilanka Ringwald static uint8_t activate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){
1743fdda66c0SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1744fdda66c0SMilanka Ringwald     if (!hfp_connection) {
1745fdda66c0SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1746be55a11dSMilanka Ringwald     }
1747*013cc750SMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1748*013cc750SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1749*013cc750SMilanka Ringwald     }
1750*013cc750SMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection, enhanced)){
1751af97579eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1752af97579eSMilanka Ringwald     }
1753af97579eSMilanka Ringwald 
1754498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1755be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1756498a8121SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1757fd4151d1SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
1758*013cc750SMilanka Ringwald             hfp_connection->enhanced_voice_recognition_enabled = false;
1759be55a11dSMilanka Ringwald             break;
1760be55a11dSMilanka Ringwald         default:
1761be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1762be55a11dSMilanka Ringwald     }
1763ce263fc8SMatthias Ringwald 
1764af97579eSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1765fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1766af97579eSMilanka Ringwald }
1767af97579eSMilanka Ringwald 
1768*013cc750SMilanka Ringwald 
1769*013cc750SMilanka Ringwald static uint8_t deactivate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){
1770af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1771af97579eSMilanka Ringwald     if (!hfp_connection) {
1772af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1773af97579eSMilanka Ringwald     }
177408a0b01cSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
177508a0b01cSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
177608a0b01cSMilanka Ringwald     }
1777*013cc750SMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection, enhanced)){
1778fdda66c0SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1779af97579eSMilanka Ringwald     }
1780*013cc750SMilanka Ringwald 
1781fdda66c0SMilanka Ringwald     switch (hfp_connection->vra_state){
1782fdda66c0SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1783fdda66c0SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1784fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1785fdda66c0SMilanka Ringwald             break;
1786fdda66c0SMilanka Ringwald         default:
1787fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1788fdda66c0SMilanka Ringwald     }
1789fdda66c0SMilanka Ringwald 
1790fdda66c0SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1791fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1792af97579eSMilanka Ringwald }
1793af97579eSMilanka Ringwald 
1794*013cc750SMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1795*013cc750SMilanka Ringwald     return activate_voice_recognition(acl_handle, false);
1796*013cc750SMilanka Ringwald }
1797*013cc750SMilanka Ringwald 
1798e83c2025SMilanka Ringwald uint8_t hfp_hf_activate_enhanced_voice_recognition(hci_con_handle_t acl_handle){
1799*013cc750SMilanka Ringwald     return activate_voice_recognition(acl_handle, true);
1800be55a11dSMilanka Ringwald }
1801fdda66c0SMilanka Ringwald 
1802fdda66c0SMilanka Ringwald 
1803*013cc750SMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1804*013cc750SMilanka Ringwald     return deactivate_voice_recognition(acl_handle, false);
1805be55a11dSMilanka Ringwald }
1806be55a11dSMilanka Ringwald 
1807e83c2025SMilanka Ringwald uint8_t hfp_hf_deactivate_enhanced_voice_recognition(hci_con_handle_t acl_handle){
1808*013cc750SMilanka Ringwald     return deactivate_voice_recognition(acl_handle, true);
1809ce263fc8SMatthias Ringwald }
1810ce263fc8SMatthias Ringwald 
18113c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
18129c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1813a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18143c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1815a33eb0c4SMilanka Ringwald     }
1816c8626498SMilanka Ringwald 
18173c65e705SMilanka Ringwald     if (hfp_connection->microphone_gain == gain) {
18183c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
18193c65e705SMilanka Ringwald     }
18203c65e705SMilanka Ringwald 
1821c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1822a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
18233c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1824a0ffb263SMatthias Ringwald     }
18253c65e705SMilanka Ringwald 
1826a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1827a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
18281c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18293c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1830ce263fc8SMatthias Ringwald }
1831ce263fc8SMatthias Ringwald 
18323c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
18339c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1834a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18353c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1836a33eb0c4SMilanka Ringwald     }
1837c8626498SMilanka Ringwald 
18383c65e705SMilanka Ringwald     if (hfp_connection->speaker_gain == gain){
18393c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
18403c65e705SMilanka Ringwald     }
18413c65e705SMilanka Ringwald 
1842c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1843a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
18443c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1845a0ffb263SMatthias Ringwald     }
18463c65e705SMilanka Ringwald 
1847a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1848a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
18491c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18503c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1851ce263fc8SMatthias Ringwald }
1852ce263fc8SMatthias Ringwald 
18533c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
18549c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1855a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18563c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1857a33eb0c4SMilanka Ringwald     }
1858a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
18591c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18603c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1861ce263fc8SMatthias Ringwald }
1862ce263fc8SMatthias Ringwald 
18633c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
18649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1865a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18663c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1867a33eb0c4SMilanka Ringwald     }
1868a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
18691c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18703c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1871ce263fc8SMatthias Ringwald }
18723deb3ec6SMatthias Ringwald 
18733c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
18749c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1875a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18763c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1877a33eb0c4SMilanka Ringwald     }
1878a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
18791c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18803c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1881667ec068SMatthias Ringwald }
1882667ec068SMatthias Ringwald 
1883667ec068SMatthias Ringwald 
18843c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
18859c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1886a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18873c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1888a33eb0c4SMilanka Ringwald     }
1889a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1890a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
18911c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18923c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1893667ec068SMatthias Ringwald }
1894667ec068SMatthias Ringwald 
18953c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
18969c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1897a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18983c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1899a33eb0c4SMilanka Ringwald     }
1900a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1901a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
19021c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19033c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1904667ec068SMatthias Ringwald }
1905667ec068SMatthias Ringwald 
19063c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
19079c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1908a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19093c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1910a33eb0c4SMilanka Ringwald     }
1911a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1912a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
19131c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19143c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1915667ec068SMatthias Ringwald }
1916667ec068SMatthias Ringwald 
19173c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
19189c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1919a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19203c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1921a33eb0c4SMilanka Ringwald     }
1922a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1923a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
19241c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19253c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1926667ec068SMatthias Ringwald }
1927667ec068SMatthias Ringwald 
19283c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
19299c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1930a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19313c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1932a33eb0c4SMilanka Ringwald     }
1933a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
19341c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19353c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1936667ec068SMatthias Ringwald }
1937667ec068SMatthias Ringwald 
19383c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
19399c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1940a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19413c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1942a33eb0c4SMilanka Ringwald     }
1943667ec068SMatthias Ringwald     // find index for assigned number
1944667ec068SMatthias Ringwald     int i;
1945667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1946667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1947667ec068SMatthias Ringwald             // set value
1948667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1949667ec068SMatthias Ringwald             // mark for update
1950a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1951a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1952667ec068SMatthias Ringwald                 // send update
19531c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
1954a0ffb263SMatthias Ringwald             }
19553c65e705SMilanka Ringwald             return ERROR_CODE_SUCCESS;
1956667ec068SMatthias Ringwald         }
1957667ec068SMatthias Ringwald     }
19583c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1959667ec068SMatthias Ringwald }
1960667ec068SMatthias Ringwald 
1961d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
1962d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1963d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
1964d7f6b5cbSMatthias Ringwald         return 0;
1965d7f6b5cbSMatthias Ringwald     }
1966d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1967d7f6b5cbSMatthias Ringwald }
196876cc1527SMatthias Ringwald 
196976cc1527SMatthias 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){
197076cc1527SMatthias Ringwald 	if (!name){
197176cc1527SMatthias Ringwald 		name = default_hfp_hf_service_name;
197276cc1527SMatthias Ringwald 	}
197376cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
197476cc1527SMatthias Ringwald 
197576cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
197676cc1527SMatthias Ringwald 	//
197776cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
197876cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
197976cc1527SMatthias Ringwald 	//
198076cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
198176cc1527SMatthias Ringwald 	//
198276cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
1983ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
198476cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
198576cc1527SMatthias Ringwald 	}
1986ef3ae4ebSMilanka Ringwald 
1987ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
198856f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
1989ef3ae4ebSMilanka Ringwald     }
1990ef3ae4ebSMilanka Ringwald 
1991ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
199256f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
1993ef3ae4ebSMilanka Ringwald     }
1994ef3ae4ebSMilanka Ringwald 
199576cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
199676cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
199776cc1527SMatthias Ringwald }
199876cc1527SMatthias Ringwald 
199976cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
200068466199SMilanka Ringwald 	btstack_assert(callback != NULL);
200168466199SMilanka Ringwald 
200276cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
200376cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
200476cc1527SMatthias Ringwald }
2005