xref: /btstack/src/classic/hfp_hf.c (revision 498a8121ec1220e45c2cefc447f581e06c8d343b)
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;
386*498a8121SMilanka 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;
435*498a8121SMilanka Ringwald     if (hfp_connection->ok_pending){
436ce263fc8SMatthias Ringwald 
437*498a8121SMilanka Ringwald         return 0;
438*498a8121SMilanka Ringwald     }
439*498a8121SMilanka Ringwald     log_info("ok not pending\n");
440ce263fc8SMatthias Ringwald     int done = 0;
441a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
442a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
443ce263fc8SMatthias Ringwald         done = 1;
444a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
445ce263fc8SMatthias Ringwald         return done;
446ce263fc8SMatthias Ringwald     };
447a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
448a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
449ce263fc8SMatthias Ringwald         done = 1;
450a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
451a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
452a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
453ce263fc8SMatthias Ringwald         return done;
454ce263fc8SMatthias Ringwald     }
455ce263fc8SMatthias Ringwald 
456a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
457ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
458a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
459a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
460a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
461ce263fc8SMatthias Ringwald             return 1;
462ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
463a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
464a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
465a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
466ce263fc8SMatthias Ringwald             return 1;
467ce263fc8SMatthias Ringwald         default:
468ce263fc8SMatthias Ringwald             break;
469ce263fc8SMatthias Ringwald     }
470ce263fc8SMatthias Ringwald 
471a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
472a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
473ce263fc8SMatthias Ringwald         done = 1;
474a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
475ce263fc8SMatthias Ringwald         return done;
476ce263fc8SMatthias Ringwald     }
477ce263fc8SMatthias Ringwald 
478ce263fc8SMatthias Ringwald     return done;
479ce263fc8SMatthias Ringwald }
480ce263fc8SMatthias Ringwald 
481af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
482be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
483be55a11dSMilanka Ringwald         return 0;
484be55a11dSMilanka Ringwald     }
485be55a11dSMilanka Ringwald     int done = 0;
486*498a8121SMilanka Ringwald     uint8_t status = 0;
487be55a11dSMilanka Ringwald     if (hfp_connection->ok_pending == 0){
488*498a8121SMilanka Ringwald         switch (hfp_connection->vra_state_requested){
489be55a11dSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
490be55a11dSMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF:
491*498a8121SMilanka Ringwald                 status = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
492be55a11dSMilanka Ringwald                 break;
493*498a8121SMilanka Ringwald 
494be55a11dSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
495*498a8121SMilanka Ringwald                 status = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
496be55a11dSMilanka Ringwald                 break;
497ce263fc8SMatthias Ringwald 
498be55a11dSMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
499be55a11dSMilanka Ringwald                 if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
500be55a11dSMilanka Ringwald                     return 0;
501be55a11dSMilanka Ringwald                 }
502*498a8121SMilanka Ringwald                 status = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
503be55a11dSMilanka Ringwald                 break;
504be55a11dSMilanka Ringwald 
505be55a11dSMilanka Ringwald             default:
506be55a11dSMilanka Ringwald                 break;
507be55a11dSMilanka Ringwald         }
508*498a8121SMilanka Ringwald         if (status == 1){
509*498a8121SMilanka Ringwald             hfp_connection->ok_pending = 1;
510*498a8121SMilanka Ringwald         }
511be55a11dSMilanka Ringwald     } else {
512*498a8121SMilanka Ringwald         switch (hfp_connection->vra_state_requested){
513be55a11dSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
514*498a8121SMilanka Ringwald                 hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
515*498a8121SMilanka Ringwald                 hfp_connection->vra_state_requested = hfp_connection->vra_state;
516*498a8121SMilanka Ringwald                 hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS, 1);
517be55a11dSMilanka Ringwald                 break;
518be55a11dSMilanka Ringwald 
519be55a11dSMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
520*498a8121SMilanka Ringwald                 hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED;
521*498a8121SMilanka Ringwald                 hfp_connection->vra_state_requested = hfp_connection->vra_state;
522*498a8121SMilanka Ringwald                 hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS, 2);
523be55a11dSMilanka Ringwald                 break;
524be55a11dSMilanka Ringwald 
525be55a11dSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
526be55a11dSMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF:
527*498a8121SMilanka Ringwald                 hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
528*498a8121SMilanka Ringwald                 hfp_connection->vra_state_requested = hfp_connection->vra_state;
529*498a8121SMilanka Ringwald                 hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS, 0);
530be55a11dSMilanka Ringwald                 break;
531be55a11dSMilanka Ringwald             default:
532be55a11dSMilanka Ringwald                 break;
533be55a11dSMilanka Ringwald         }
534be55a11dSMilanka Ringwald     }
535be55a11dSMilanka Ringwald     return done;
536be55a11dSMilanka Ringwald }
537be55a11dSMilanka Ringwald 
538be55a11dSMilanka Ringwald 
539be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
540a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
541ce263fc8SMatthias Ringwald 
542332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
543332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
544ce263fc8SMatthias Ringwald 
545a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
546a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
547332ca98fSMatthias Ringwald 		return 1;
548332ca98fSMatthias Ringwald     }
549332ca98fSMatthias Ringwald 
5501cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
5511cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
552ce263fc8SMatthias Ringwald 
553a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
554fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
5551cc65c4fSMatthias Ringwald 		return 1;
5561cc65c4fSMatthias Ringwald     }
5571cc65c4fSMatthias Ringwald 
5581cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
5591cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
5601cc65c4fSMatthias Ringwald 
561a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
562a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
5631cc65c4fSMatthias Ringwald 		return 1;
5641cc65c4fSMatthias Ringwald     }
565ce263fc8SMatthias Ringwald 
566ce263fc8SMatthias Ringwald     return 0;
567ce263fc8SMatthias Ringwald }
568ce263fc8SMatthias Ringwald 
569a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
570505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
571505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
572ce263fc8SMatthias Ringwald 
57364f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
574a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
575a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
576a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
577ce263fc8SMatthias Ringwald         return 1;
578ce263fc8SMatthias Ringwald     }
579ce263fc8SMatthias Ringwald 
580a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
581ce263fc8SMatthias Ringwald 
582ce263fc8SMatthias Ringwald     // run codecs exchange
583a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
584ce263fc8SMatthias Ringwald     if (done) return 1;
585ce263fc8SMatthias Ringwald 
58638200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
58738200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
58838200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
58938200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
59038200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
59138200c1dSMilanka Ringwald         return 1;
59238200c1dSMilanka Ringwald     }
593ce263fc8SMatthias Ringwald     return 0;
594ce263fc8SMatthias Ringwald }
595ce263fc8SMatthias Ringwald 
59638200c1dSMilanka Ringwald 
597a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
598eaf2b0a1SMatthias Ringwald 
599eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
600eaf2b0a1SMatthias Ringwald 
601a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
602a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
603a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
604ce263fc8SMatthias Ringwald         return 1;
605ce263fc8SMatthias Ringwald     }
606ce263fc8SMatthias Ringwald     return 0;
607ce263fc8SMatthias Ringwald }
608ce263fc8SMatthias Ringwald 
6091c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
6107522e673SMatthias Ringwald 
61176cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
61276cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
61376cc1527SMatthias Ringwald 
61476cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
61576cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
61622387625SMatthias Ringwald 
6173721a235SMatthias Ringwald 	// assert command could be sent
6183721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
6193721a235SMatthias Ringwald 
6203721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
6213721a235SMatthias Ringwald     // WBS Disassociate
6223721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
6233721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
6243721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
6253721a235SMatthias Ringwald         return;
6263721a235SMatthias Ringwald     }
6273721a235SMatthias Ringwald     // Write Codec Config
6283721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
6293721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
6303721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
6313721a235SMatthias Ringwald         return;
6323721a235SMatthias Ringwald     }
6333721a235SMatthias Ringwald     // WBS Associate
6343721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
6353721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
6363721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
6373721a235SMatthias Ringwald         return;
6383721a235SMatthias Ringwald     }
6393721a235SMatthias Ringwald #endif
640689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
641689d4323SMatthias Ringwald     // Enable WBS
642689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
643689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
644689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
645689d4323SMatthias Ringwald         return;
646689d4323SMatthias Ringwald     }
647689d4323SMatthias Ringwald     // Write I2S/PCM params
648689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
649689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
650689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
651689d4323SMatthias Ringwald         return;
652689d4323SMatthias Ringwald     }
653689d4323SMatthias Ringwald     // Disable WBS
654689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
655689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
656689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
657689d4323SMatthias Ringwald         return;
658689d4323SMatthias Ringwald     }
659689d4323SMatthias Ringwald #endif
66048e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
66148e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
66248e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
66348e6eeeeSMatthias Ringwald         return;
66448e6eeeeSMatthias Ringwald     }
66548e6eeeeSMatthias Ringwald #endif
6663721a235SMatthias Ringwald 
667cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
668cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
669cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
6707522e673SMatthias Ringwald         // notify about codec selection if not done already
6717522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
6727522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
6737522e673SMatthias Ringwald         }
674cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
6757522e673SMatthias Ringwald         return;
6767522e673SMatthias Ringwald     }
6777522e673SMatthias Ringwald 
678d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
679d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
680d4dd47ffSMatthias Ringwald         return;
681d4dd47ffSMatthias Ringwald     }
682a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
683ce263fc8SMatthias Ringwald     if (!done){
684a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
685ce263fc8SMatthias Ringwald     }
686ce263fc8SMatthias Ringwald     if (!done){
687af97579eSMilanka Ringwald         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
688be55a11dSMilanka Ringwald     }
689be55a11dSMilanka Ringwald     if (!done){
690a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
691ce263fc8SMatthias Ringwald     }
692ce263fc8SMatthias Ringwald     if (!done){
693a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
694ce263fc8SMatthias Ringwald     }
695ce263fc8SMatthias Ringwald 
6961016a228SMatthias Ringwald     // don't send a new command while ok still pending
6971016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6981016a228SMatthias Ringwald 
699a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
700a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
701a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
702a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
703ce263fc8SMatthias Ringwald         return;
704ce263fc8SMatthias Ringwald     }
705ce263fc8SMatthias Ringwald 
706a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
707a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
708a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
709a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
710ce263fc8SMatthias Ringwald         return;
711ce263fc8SMatthias Ringwald     }
712ce263fc8SMatthias Ringwald 
713a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
714a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
715a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
716a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
717ce263fc8SMatthias Ringwald         return;
718ce263fc8SMatthias Ringwald     }
719ce263fc8SMatthias Ringwald 
720a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
721a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
722a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
723a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
724ce263fc8SMatthias Ringwald         return;
725ce263fc8SMatthias Ringwald     }
726ce263fc8SMatthias Ringwald 
727a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
728a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
729a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
730a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
731ce263fc8SMatthias Ringwald         return;
732ce263fc8SMatthias Ringwald     }
733ce263fc8SMatthias Ringwald 
734a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
735a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
736a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
737a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
738ce263fc8SMatthias Ringwald         return;
739ce263fc8SMatthias Ringwald     }
740ce263fc8SMatthias Ringwald 
741a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
742a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
743a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
744a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
745ce263fc8SMatthias Ringwald         return;
746ce263fc8SMatthias Ringwald     }
747ce263fc8SMatthias Ringwald 
748a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
749a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
750a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
751a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
752ce263fc8SMatthias Ringwald         return;
753ce263fc8SMatthias Ringwald     }
754ce263fc8SMatthias Ringwald 
755a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
756a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
757a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
758a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
759ce263fc8SMatthias Ringwald         return;
760ce263fc8SMatthias Ringwald     }
761ce263fc8SMatthias Ringwald 
762a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
763a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
764a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
765a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
766ce263fc8SMatthias Ringwald         return;
767ce263fc8SMatthias Ringwald     }
768ce263fc8SMatthias Ringwald 
769a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
770a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
771a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
772a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
773ce263fc8SMatthias Ringwald         return;
774ce263fc8SMatthias Ringwald     }
775ce263fc8SMatthias Ringwald 
776a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
777a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
778a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
779a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
780ce263fc8SMatthias Ringwald         return;
781ce263fc8SMatthias Ringwald     }
782ce263fc8SMatthias Ringwald 
783a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
784a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
785a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
786a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
787ce263fc8SMatthias Ringwald         return;
788ce263fc8SMatthias Ringwald     }
789ce263fc8SMatthias Ringwald 
790a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
791a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
792a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
793a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
794ce263fc8SMatthias Ringwald         return;
795ce263fc8SMatthias Ringwald     }
796ce263fc8SMatthias Ringwald 
797a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
798a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
799a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
800a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
801ce263fc8SMatthias Ringwald         return;
802ce263fc8SMatthias Ringwald     }
803ce263fc8SMatthias Ringwald 
804a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
805a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
806a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
807a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
808ce263fc8SMatthias Ringwald         return;
809ce263fc8SMatthias Ringwald     }
810ce263fc8SMatthias Ringwald 
811a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
812a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
813a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
814a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
815ce263fc8SMatthias Ringwald         return;
816ce263fc8SMatthias Ringwald     }
817ce263fc8SMatthias Ringwald 
818a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
819a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
820a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
821a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
822667ec068SMatthias Ringwald         return;
823667ec068SMatthias Ringwald     }
824667ec068SMatthias Ringwald 
825a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
826a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
827a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
828a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
829a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
830ce263fc8SMatthias Ringwald         return;
831ce263fc8SMatthias Ringwald     }
832ce263fc8SMatthias Ringwald 
833a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
834a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
835a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
836a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
837ce263fc8SMatthias Ringwald         return;
838ce263fc8SMatthias Ringwald     }
839ce263fc8SMatthias Ringwald 
840a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
841a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
842a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
843a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
844667ec068SMatthias Ringwald         return;
845667ec068SMatthias Ringwald     }
846667ec068SMatthias Ringwald 
847a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
848a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
849667ec068SMatthias Ringwald         char buffer[20];
850a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
851667ec068SMatthias Ringwald             case '?':
8521599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
853ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
854ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
855a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
856667ec068SMatthias Ringwald                 return;
857667ec068SMatthias Ringwald             case '0':
858667ec068SMatthias Ringwald             case '1':
859667ec068SMatthias Ringwald             case '2':
8601599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
861ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
862ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
863ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
864a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
865667ec068SMatthias Ringwald                 return;
866667ec068SMatthias Ringwald             default:
867667ec068SMatthias Ringwald                 break;
868667ec068SMatthias Ringwald         }
869667ec068SMatthias Ringwald         return;
870667ec068SMatthias Ringwald     }
871667ec068SMatthias Ringwald 
872a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
873a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
874667ec068SMatthias Ringwald         char buffer[20];
8751599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
876ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
877ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
878a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
879667ec068SMatthias Ringwald         return;
880667ec068SMatthias Ringwald     }
881667ec068SMatthias Ringwald 
882667ec068SMatthias Ringwald     // update HF indicators
883a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
884667ec068SMatthias Ringwald         int i;
885667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
886a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
887a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
888a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
889a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
890667ec068SMatthias Ringwald                     char buffer[30];
8911599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
892ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
893ff7d6aeaSMatthias Ringwald                              hfp_indicators[i],
894ff7d6aeaSMatthias Ringwald                              (unsigned int)hfp_indicators_value[i]);
895ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
896a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
897667ec068SMatthias Ringwald                 } else {
89860ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
899667ec068SMatthias Ringwald                 }
900667ec068SMatthias Ringwald                 return;
901667ec068SMatthias Ringwald             }
902667ec068SMatthias Ringwald         }
903667ec068SMatthias Ringwald     }
904667ec068SMatthias Ringwald 
905ce263fc8SMatthias Ringwald     if (done) return;
906ce263fc8SMatthias Ringwald     // deal with disconnect
907a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
908ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
909a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
910a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
911ce263fc8SMatthias Ringwald             break;
912ce263fc8SMatthias Ringwald 
913ce263fc8SMatthias Ringwald         default:
914ce263fc8SMatthias Ringwald             break;
915ce263fc8SMatthias Ringwald     }
916ce263fc8SMatthias Ringwald }
917ce263fc8SMatthias Ringwald 
918a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
919a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
9206a7f44bdSMilanka Ringwald 
921ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
9227522e673SMatthias Ringwald 
923667ec068SMatthias Ringwald     // restore volume settings
924a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
925a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
926ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
927a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
928a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
929ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
930667ec068SMatthias Ringwald     // enable all indicators
931667ec068SMatthias Ringwald     int i;
932667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
933a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
934a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
935667ec068SMatthias Ringwald     }
936ce263fc8SMatthias Ringwald }
937ce263fc8SMatthias Ringwald 
9381cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
9391cc65c4fSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
9401cc65c4fSMatthias Ringwald 		// Codec supported, confirm
9411cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
9421cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
9431cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
9441cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
9451cc65c4fSMatthias Ringwald 
9461cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
9471cc65c4fSMatthias Ringwald 	} else {
9481cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
9491cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
9501cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
9511cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
9521cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9531cc65c4fSMatthias Ringwald 
9541cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
9551cc65c4fSMatthias Ringwald 	}
9561cc65c4fSMatthias Ringwald }
9571cc65c4fSMatthias Ringwald 
958a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
959a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
9603deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
961a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
962a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
9633deb3ec6SMatthias Ringwald                 break;
9643deb3ec6SMatthias Ringwald             }
965a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9663deb3ec6SMatthias Ringwald             break;
9673deb3ec6SMatthias Ringwald 
9683deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
969a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9703deb3ec6SMatthias Ringwald             break;
9713deb3ec6SMatthias Ringwald 
9723deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
973a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
9743deb3ec6SMatthias Ringwald             break;
9753deb3ec6SMatthias Ringwald 
9763deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
977a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
9783deb3ec6SMatthias Ringwald             break;
9793deb3ec6SMatthias Ringwald 
9803deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
981a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
982a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
9833deb3ec6SMatthias Ringwald                 break;
9843deb3ec6SMatthias Ringwald             }
985a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
986a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9873deb3ec6SMatthias Ringwald                 break;
9883deb3ec6SMatthias Ringwald             }
989a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9903deb3ec6SMatthias Ringwald             break;
9913deb3ec6SMatthias Ringwald 
9923deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
993a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
994a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9953deb3ec6SMatthias Ringwald                 break;
9963deb3ec6SMatthias Ringwald             }
997a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9983deb3ec6SMatthias Ringwald             break;
9993deb3ec6SMatthias Ringwald 
10003deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1001a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
10023deb3ec6SMatthias Ringwald             break;
10033deb3ec6SMatthias Ringwald 
10043deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1005a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
10063deb3ec6SMatthias Ringwald             break;
10073deb3ec6SMatthias Ringwald 
10083deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1009a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
10103deb3ec6SMatthias Ringwald             break;
1011ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1012a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1013a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1014ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1015ce263fc8SMatthias Ringwald                 break;
1016ce263fc8SMatthias Ringwald             }
10173deb3ec6SMatthias Ringwald 
1018a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1019a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1020ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1021ce263fc8SMatthias Ringwald                 break;
10223deb3ec6SMatthias Ringwald             }
10233deb3ec6SMatthias Ringwald 
1024a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
1025ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1026a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1027ce263fc8SMatthias Ringwald                     break;
1028ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1029a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1030a473a009SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_connection);
1031ce263fc8SMatthias Ringwald                     break;
1032ce263fc8SMatthias Ringwald                 default:
1033ce263fc8SMatthias Ringwald                     break;
10343deb3ec6SMatthias Ringwald             }
1035ce263fc8SMatthias Ringwald 
1036a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
1037a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
1038ce263fc8SMatthias Ringwald                 break;
10393deb3ec6SMatthias Ringwald             }
10403deb3ec6SMatthias Ringwald 
1041a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
1042aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1043a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10443deb3ec6SMatthias Ringwald                     break;
1045ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
1046a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1047ce263fc8SMatthias Ringwald                     break;
10483deb3ec6SMatthias Ringwald                 default:
10493deb3ec6SMatthias Ringwald                     break;
10503deb3ec6SMatthias Ringwald             }
1051af97579eSMilanka Ringwald             hfp_hf_voice_recognition_state_machine(hfp_connection);
1052be55a11dSMilanka Ringwald             break;
1053be55a11dSMilanka Ringwald         case HFP_AUDIO_CONNECTION_ESTABLISHED:
1054af97579eSMilanka Ringwald             hfp_hf_voice_recognition_state_machine(hfp_connection);
10553deb3ec6SMatthias Ringwald             break;
10563deb3ec6SMatthias Ringwald         default:
10573deb3ec6SMatthias Ringwald             break;
10583deb3ec6SMatthias Ringwald     }
10593deb3ec6SMatthias Ringwald 
10603deb3ec6SMatthias Ringwald     // done
1061be55a11dSMilanka Ringwald     hfp_connection->ok_pending = 0;
1062a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
10633deb3ec6SMatthias Ringwald }
10643deb3ec6SMatthias Ringwald 
1065be55a11dSMilanka Ringwald 
1066b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
10674562e2a2SMatthias Ringwald     uint16_t i;
10684562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
10694562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
10704562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
10714562e2a2SMatthias Ringwald                 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
10724562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
10734562e2a2SMatthias Ringwald                 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
10744562e2a2SMatthias Ringwald                 // avoid set but not used warning
10754562e2a2SMatthias Ringwald                 (void) hfp_callheld_status;
10764562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
10774562e2a2SMatthias Ringwald                 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
10784562e2a2SMatthias Ringwald             }
10794562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1080a473a009SMatthias Ringwald             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
10814562e2a2SMatthias Ringwald             break;
10824562e2a2SMatthias Ringwald         }
10834562e2a2SMatthias Ringwald     }
10844562e2a2SMatthias Ringwald }
10854562e2a2SMatthias Ringwald 
1086426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1087186dd3d2SMatthias Ringwald     int value;
1088186dd3d2SMatthias Ringwald     int i;
1089a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1090667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1091a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1092a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1093667ec068SMatthias Ringwald             break;
1094667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1095a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1096ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1097667ec068SMatthias Ringwald             break;
1098667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1099a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1100a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1101667ec068SMatthias Ringwald             break;
1102ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1103a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11042308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1105667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1106ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1107ce263fc8SMatthias Ringwald             break;
1108ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1109a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11102308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1111667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1112ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1113ce263fc8SMatthias Ringwald             break;
1114ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1115a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1116ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1117a0ffb263SMatthias Ringwald             break;
1118a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1119a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1120a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1121a0ffb263SMatthias Ringwald             break;
1122a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1123a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1124a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1125ce263fc8SMatthias Ringwald             break;
1126ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1127a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11285a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1129a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1130ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1131ce263fc8SMatthias Ringwald             break;
1132ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1133a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1134a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1135be55a11dSMilanka Ringwald             hfp_connection->command = HFP_CMD_NONE;
113656f1adacSMilanka Ringwald 
113790244c92SMilanka Ringwald             switch (hfp_connection->state){
113890244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
113990244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
114090244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
114190244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
114290244c92SMilanka Ringwald                             return;
114390244c92SMilanka Ringwald                         default:
114490244c92SMilanka Ringwald                             break;
114590244c92SMilanka Ringwald                     }
114656f1adacSMilanka Ringwald                     break;
114756f1adacSMilanka Ringwald                 default:
114856f1adacSMilanka Ringwald                     break;
114956f1adacSMilanka Ringwald             }
115056f1adacSMilanka Ringwald 
1151*498a8121SMilanka Ringwald             // TODO
1152*498a8121SMilanka Ringwald             switch (hfp_connection->vra_state){
1153be55a11dSMilanka Ringwald                 case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1154be55a11dSMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF:
1155be55a11dSMilanka Ringwald                 case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1156be55a11dSMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
1157*498a8121SMilanka Ringwald                     hfp_connection->vra_state = hfp_connection->vra_state_requested;
1158be55a11dSMilanka Ringwald                     break;
1159be55a11dSMilanka Ringwald                 default:
1160be55a11dSMilanka Ringwald                     break;
1161be55a11dSMilanka Ringwald             }
1162be55a11dSMilanka Ringwald 
1163ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1164ce263fc8SMatthias Ringwald             break;
1165ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1166a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1167ce263fc8SMatthias Ringwald             break;
1168ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
11695a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1170ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1171ce263fc8SMatthias Ringwald             break;
1172ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
11735a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11744562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1175ce263fc8SMatthias Ringwald             break;
1176c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
11775a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1178c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1179a473a009SMatthias Ringwald                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1180c741b032SMilanka Ringwald             }
1181c741b032SMilanka Ringwald             break;
11821cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
11831cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11845a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
11851cc65c4fSMatthias Ringwald 			break;
1186eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1187eac56539SMilanka 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));
1188ce263fc8SMatthias Ringwald         default:
1189ce263fc8SMatthias Ringwald             break;
11903deb3ec6SMatthias Ringwald     }
11910cef86faSMatthias Ringwald }
1192426f9988SMatthias Ringwald 
119376cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
119476cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
119576cc1527SMatthias Ringwald }
119676cc1527SMatthias Ringwald 
1197426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1198426f9988SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
1199426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1200426f9988SMatthias Ringwald     if (size < 1) return;
1201426f9988SMatthias Ringwald 
1202426f9988SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1203426f9988SMatthias Ringwald     if (!hfp_connection) return;
1204426f9988SMatthias Ringwald 
1205426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1206e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1207e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1208e43d1938SMatthias Ringwald #endif
1209426f9988SMatthias Ringwald 
1210426f9988SMatthias Ringwald     // process messages byte-wise
1211426f9988SMatthias Ringwald     int pos;
1212426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1213426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
1214426f9988SMatthias Ringwald 
12151599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1216426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1217426f9988SMatthias Ringwald 
1218426f9988SMatthias Ringwald         hfp_hf_handle_rfcomm_command(hfp_connection);
1219426f9988SMatthias Ringwald     }
12201c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
12213deb3ec6SMatthias Ringwald }
12223deb3ec6SMatthias Ringwald 
12231c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1224665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1225665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1226665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1227a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
122822387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
12291c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
12303deb3ec6SMatthias Ringwald     }
12313deb3ec6SMatthias Ringwald }
12323deb3ec6SMatthias Ringwald 
12331c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
12343deb3ec6SMatthias Ringwald     switch (packet_type){
12353deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1236426f9988SMatthias Ringwald             hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size);
12373deb3ec6SMatthias Ringwald             break;
12383deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1239d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1240d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
12411c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1242d4dd47ffSMatthias Ringwald                 return;
1243d4dd47ffSMatthias Ringwald             }
124427950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1245202c8a4cSMatthias Ringwald             break;
12463deb3ec6SMatthias Ringwald         default:
12473deb3ec6SMatthias Ringwald             break;
12483deb3ec6SMatthias Ringwald     }
12491c6a0fc0SMatthias Ringwald     hfp_hf_run();
12503deb3ec6SMatthias Ringwald }
12513deb3ec6SMatthias Ringwald 
12521c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1253405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
12541c6a0fc0SMatthias Ringwald     hfp_hf_run();
1255405014fbSMatthias Ringwald }
1256405014fbSMatthias Ringwald 
1257b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1258b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1259b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1260b4df8028SMilanka Ringwald         return status;
1261b4df8028SMilanka Ringwald     }
1262b4df8028SMilanka Ringwald 
1263520c92d5SMatthias Ringwald     hfp_init();
126420b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
126520b2edb6SMatthias Ringwald     hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
126620b2edb6SMatthias Ringwald     hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
126720b2edb6SMatthias Ringwald     hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
126820b2edb6SMatthias Ringwald     hfp_codecs_nr = 0;
126920b2edb6SMatthias Ringwald     hfp_hf_speaker_gain = 9;
127020b2edb6SMatthias Ringwald     hfp_hf_microphone_gain = 9;
127120b2edb6SMatthias Ringwald     hfp_indicators_nr = 0;
127220b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1273d63c37a1SMatthias Ringwald 
12741c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
12751c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
127627950165SMatthias Ringwald 
127727950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
12781c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1279b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
128020b2edb6SMatthias Ringwald }
128127950165SMatthias Ringwald 
128220b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
128320b2edb6SMatthias Ringwald     hfp_deinit();
128420b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
128520b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t));
128620b2edb6SMatthias Ringwald     (void) memset(phone_number, 0, sizeof(phone_number));
1287a0ffb263SMatthias Ringwald }
1288a0ffb263SMatthias Ringwald 
12897ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
129068466199SMilanka Ringwald     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
12913deb3ec6SMatthias Ringwald 
12923deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
12933deb3ec6SMatthias Ringwald     int i;
12943deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
12953deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
12963deb3ec6SMatthias Ringwald     }
12973deb3ec6SMatthias Ringwald }
12983deb3ec6SMatthias Ringwald 
1299a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
13003deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1301a0ffb263SMatthias Ringwald }
13023deb3ec6SMatthias Ringwald 
13037ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
130468466199SMilanka Ringwald     btstack_assert(hfp_indicators_nr < HFP_MAX_NUM_INDICATORS);
130568466199SMilanka Ringwald 
13063deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
13073deb3ec6SMatthias Ringwald     int i;
1308a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
13093deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
13103deb3ec6SMatthias Ringwald     }
13113deb3ec6SMatthias Ringwald }
13123deb3ec6SMatthias Ringwald 
13134eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
13144eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
13153deb3ec6SMatthias Ringwald }
13163deb3ec6SMatthias Ringwald 
1317657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
13189c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1319a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1320657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1321a33eb0c4SMilanka Ringwald     }
13221ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
13231c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1324657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
13253deb3ec6SMatthias Ringwald }
13263deb3ec6SMatthias Ringwald 
13273c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
13289c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1329a0ffb263SMatthias Ringwald     if (!hfp_connection) {
13303c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
13313deb3ec6SMatthias Ringwald     }
1332a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
13331c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13343c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
13353deb3ec6SMatthias Ringwald }
13363deb3ec6SMatthias Ringwald 
13373c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
13383c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1339ce263fc8SMatthias Ringwald }
1340ce263fc8SMatthias Ringwald 
13413c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
13423c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1343ce263fc8SMatthias Ringwald }
1344ce263fc8SMatthias Ringwald 
13453deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
13463c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
13479c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1348a0ffb263SMatthias Ringwald     if (!hfp_connection) {
13493c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
13503deb3ec6SMatthias Ringwald     }
1351a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1352a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
13531c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13543c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
13553deb3ec6SMatthias Ringwald }
13563deb3ec6SMatthias Ringwald 
13573c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
13589c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1359a0ffb263SMatthias Ringwald     if (!hfp_connection) {
13603c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
13613deb3ec6SMatthias Ringwald     }
13623c65e705SMilanka Ringwald 
1363a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1364ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1365a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1366ce263fc8SMatthias Ringwald             break;
1367ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1368a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1369ce263fc8SMatthias Ringwald             break;
1370ce263fc8SMatthias Ringwald         default:
13713c65e705SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1372ce263fc8SMatthias Ringwald     }
13731c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13743c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
13753deb3ec6SMatthias Ringwald }
13763deb3ec6SMatthias Ringwald 
13773c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
13789c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1379a0ffb263SMatthias Ringwald     if (!hfp_connection) {
13803c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
13813deb3ec6SMatthias Ringwald     }
1382a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
13831c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13843c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
13853deb3ec6SMatthias Ringwald }
13863deb3ec6SMatthias Ringwald 
1387ce263fc8SMatthias Ringwald 
13883c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
13893c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1390ce263fc8SMatthias Ringwald }
1391ce263fc8SMatthias Ringwald 
13923c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
13933c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1394ce263fc8SMatthias Ringwald }
1395ce263fc8SMatthias Ringwald 
139638200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
139738200c1dSMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
139838200c1dSMilanka Ringwald }
1399ce263fc8SMatthias Ringwald 
14003c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
14019c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1402a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14033c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1404a33eb0c4SMilanka Ringwald     }
1405ce263fc8SMatthias Ringwald 
14063c65e705SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
14073c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
14083c65e705SMilanka Ringwald     }
14093c65e705SMilanka Ringwald 
14103c65e705SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
14113c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
14123c65e705SMilanka Ringwald     }
14133deb3ec6SMatthias Ringwald 
1414f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1415a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1416aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1417aa4dd815SMatthias Ringwald                 break;
1418ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1419ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1420ec3bfc1aSMatthias Ringwald                 break;
1421aa4dd815SMatthias Ringwald             default:
14221cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
14231cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
14241cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
14251cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
142638200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1427aa4dd815SMatthias Ringwald                 break;
14283deb3ec6SMatthias Ringwald         }
1429f4412093SMatthias Ringwald     } else {
1430f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1431f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1432f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1433f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1434f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1435f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1436f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1437f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1438ce263fc8SMatthias Ringwald     }
1439ce263fc8SMatthias Ringwald 
14401c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14413c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14423deb3ec6SMatthias Ringwald }
14433deb3ec6SMatthias Ringwald 
14443c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
14459c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1446a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14473c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1448a33eb0c4SMilanka Ringwald     }
14491ffa0dd9SMilanka Ringwald     hfp_trigger_release_audio_connection(hfp_connection);
14501c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14513c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14523deb3ec6SMatthias Ringwald }
14533deb3ec6SMatthias Ringwald 
14543c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
14559c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1456a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14573c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1458a33eb0c4SMilanka Ringwald     }
1459ce263fc8SMatthias Ringwald 
1460ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1461a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
14621c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1463ce263fc8SMatthias Ringwald     } else {
1464ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
14653c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1466ce263fc8SMatthias Ringwald     }
14673c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1468ce263fc8SMatthias Ringwald }
1469ce263fc8SMatthias Ringwald 
14703c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
14719c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1472a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14733c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1474a33eb0c4SMilanka Ringwald     }
1475a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
14761c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14773c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1478ce263fc8SMatthias Ringwald }
1479ce263fc8SMatthias Ringwald 
14803c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
14819c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1482a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14833c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1484a33eb0c4SMilanka Ringwald     }
1485ce263fc8SMatthias Ringwald 
1486ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1487a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
14881c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1489ce263fc8SMatthias Ringwald     }
14903c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1491ce263fc8SMatthias Ringwald }
1492ce263fc8SMatthias Ringwald 
14933c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){
14949c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1495a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14963c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1497a33eb0c4SMilanka Ringwald     }
1498ce263fc8SMatthias Ringwald 
1499ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1500a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
15011c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1502ce263fc8SMatthias Ringwald     }
15033c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1504ce263fc8SMatthias Ringwald }
1505ce263fc8SMatthias Ringwald 
15063c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
15079c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1508a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15093c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1510a33eb0c4SMilanka Ringwald     }
1511ce263fc8SMatthias Ringwald 
1512505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1513505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1514a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
15151c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1516ce263fc8SMatthias Ringwald     }
15173c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1518ce263fc8SMatthias Ringwald }
1519ce263fc8SMatthias Ringwald 
15203c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
15219c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1522a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15233c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1524a33eb0c4SMilanka Ringwald     }
1525ce263fc8SMatthias Ringwald 
1526505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1527505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1528a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
15291c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1530ce263fc8SMatthias Ringwald     }
15313c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1532ce263fc8SMatthias Ringwald }
1533ce263fc8SMatthias Ringwald 
15343c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
15359c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1536a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15373c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1538a33eb0c4SMilanka Ringwald     }
1539ce263fc8SMatthias Ringwald 
1540505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1541505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1542a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
15431c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1544ce263fc8SMatthias Ringwald     }
15453c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1546ce263fc8SMatthias Ringwald }
1547ce263fc8SMatthias Ringwald 
15483c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
15499c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1550a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15513c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1552a33eb0c4SMilanka Ringwald     }
1553ce263fc8SMatthias Ringwald 
1554505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1555505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1556a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
15571c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1558ce263fc8SMatthias Ringwald     }
15593c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1560ce263fc8SMatthias Ringwald }
1561ce263fc8SMatthias Ringwald 
15623c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
15639c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1564a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15653c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1566a33eb0c4SMilanka Ringwald     }
1567667ec068SMatthias Ringwald 
1568505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1569505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1570a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1571a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
15721c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1573667ec068SMatthias Ringwald     }
15743c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1575667ec068SMatthias Ringwald }
1576667ec068SMatthias Ringwald 
15773c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
15789c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1579a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15803c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1581a33eb0c4SMilanka Ringwald     }
1582667ec068SMatthias Ringwald 
1583505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1584505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1585a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1586a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
15871c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1588667ec068SMatthias Ringwald     }
15893c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1590667ec068SMatthias Ringwald }
1591ce263fc8SMatthias Ringwald 
15923c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
15939c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1594a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15953c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1596a33eb0c4SMilanka Ringwald     }
1597ce263fc8SMatthias Ringwald 
1598a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1599ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
16001c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16013c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1602ce263fc8SMatthias Ringwald }
1603ce263fc8SMatthias Ringwald 
16043c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
16059c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1606a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16073c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1608a33eb0c4SMilanka Ringwald     }
1609ce263fc8SMatthias Ringwald 
1610a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1611a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1612a0ffb263SMatthias Ringwald 
16131c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16143c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1615ce263fc8SMatthias Ringwald }
1616ce263fc8SMatthias Ringwald 
16173c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
16189c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1619a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16203c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1621a33eb0c4SMilanka Ringwald     }
1622ce263fc8SMatthias Ringwald 
1623a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
16241c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16253c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1626ce263fc8SMatthias Ringwald }
1627ce263fc8SMatthias Ringwald 
16283c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
16299c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1630a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16313c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1632a33eb0c4SMilanka Ringwald     }
1633ce263fc8SMatthias Ringwald 
1634a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
16351c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16363c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1637ce263fc8SMatthias Ringwald }
1638ce263fc8SMatthias Ringwald 
1639ce263fc8SMatthias Ringwald 
16403c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
16419c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1642a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16433c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1644a33eb0c4SMilanka Ringwald     }
1645ce263fc8SMatthias Ringwald 
1646a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
16471c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16483c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1649ce263fc8SMatthias Ringwald }
1650ce263fc8SMatthias Ringwald 
1651ce263fc8SMatthias Ringwald 
16523c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
16539c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1654a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16553c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1656a33eb0c4SMilanka Ringwald     }
1657ce263fc8SMatthias Ringwald 
1658a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
16591c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16603c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1661ce263fc8SMatthias Ringwald }
1662ce263fc8SMatthias Ringwald 
16633c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
16649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1665a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16663c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1667a33eb0c4SMilanka Ringwald     }
1668ce263fc8SMatthias Ringwald 
1669a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
16701c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16713c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1672ce263fc8SMatthias Ringwald }
1673ce263fc8SMatthias Ringwald 
1674ce263fc8SMatthias Ringwald 
16753c65e705SMilanka Ringwald uint8_t hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
16769c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1677a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16783c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1679a33eb0c4SMilanka Ringwald     }
1680ce263fc8SMatthias Ringwald 
1681a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
16821c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16833c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1684ce263fc8SMatthias Ringwald }
1685ce263fc8SMatthias Ringwald 
16863c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
16879c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1688a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16893c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1690a33eb0c4SMilanka Ringwald     }
1691ce263fc8SMatthias Ringwald 
1692a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
16931c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16943c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1695ce263fc8SMatthias Ringwald }
1696ce263fc8SMatthias Ringwald 
1697be55a11dSMilanka Ringwald static bool hfp_hf_enhanced_voice_recognition_supported(hfp_connection_t * hfp_connection){
1698be55a11dSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1699be55a11dSMilanka Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1700be55a11dSMilanka Ringwald     return hf && ag;
1701be55a11dSMilanka Ringwald }
1702be55a11dSMilanka Ringwald 
1703be55a11dSMilanka Ringwald static bool hfp_hf_voice_recognition_supported(hfp_connection_t * hfp_connection){
1704be55a11dSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION);
1705be55a11dSMilanka Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION);
1706be55a11dSMilanka Ringwald     return hf && ag;
1707be55a11dSMilanka Ringwald }
1708be55a11dSMilanka Ringwald 
1709af97579eSMilanka Ringwald static uint8_t hfp_hf_activate_enhanced_voice_recognition_for_connection(hfp_connection_t * hfp_connection){
1710af97579eSMilanka Ringwald     if (!hfp_hf_enhanced_voice_recognition_supported(hfp_connection)){
1711af97579eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1712be55a11dSMilanka Ringwald     }
1713*498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1714af97579eSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1715*498a8121SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED;
1716af97579eSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
1717af97579eSMilanka Ringwald             break;
1718af97579eSMilanka Ringwald         default:
1719af97579eSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1720af97579eSMilanka Ringwald     }
1721af97579eSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1722af97579eSMilanka Ringwald }
1723af97579eSMilanka Ringwald 
1724af97579eSMilanka Ringwald static uint8_t hfp_hf_deactivate_enhanced_voice_recognition_for_connection(hfp_connection_t * hfp_connection){
1725af97579eSMilanka Ringwald     if (!hfp_hf_enhanced_voice_recognition_supported(hfp_connection)){
1726af97579eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1727af97579eSMilanka Ringwald     }
1728*498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1729af97579eSMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
1730*498a8121SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF;
1731af97579eSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
1732af97579eSMilanka Ringwald             break;
1733af97579eSMilanka Ringwald         default:
1734af97579eSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1735af97579eSMilanka Ringwald     }
1736af97579eSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1737af97579eSMilanka Ringwald }
1738af97579eSMilanka Ringwald 
1739af97579eSMilanka Ringwald static uint8_t hfp_hf_deactivate_voice_recognition_for_connection(hfp_connection_t * hfp_connection){
1740be55a11dSMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection)){
1741be55a11dSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1742a33eb0c4SMilanka Ringwald     }
1743ce263fc8SMatthias Ringwald 
1744*498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1745be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1746*498a8121SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1747*498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
1748af97579eSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
1749be55a11dSMilanka Ringwald             break;
1750af97579eSMilanka Ringwald         default:
1751af97579eSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1752af97579eSMilanka Ringwald     }
1753af97579eSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1754af97579eSMilanka Ringwald }
1755af97579eSMilanka Ringwald 
1756af97579eSMilanka Ringwald static uint8_t hfp_hf_activate_voice_recognition_for_connection(hfp_connection_t * hfp_connection){
1757af97579eSMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection)){
1758af97579eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1759af97579eSMilanka Ringwald     }
1760af97579eSMilanka Ringwald 
1761*498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1762be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1763*498a8121SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1764*498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
17651c6a0fc0SMatthias Ringwald             hfp_hf_run_for_context(hfp_connection);
1766be55a11dSMilanka Ringwald             break;
1767be55a11dSMilanka Ringwald         default:
1768be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1769be55a11dSMilanka Ringwald     }
1770be55a11dSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1771ce263fc8SMatthias Ringwald }
1772ce263fc8SMatthias Ringwald 
1773be55a11dSMilanka Ringwald 
1774af97579eSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1775af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1776af97579eSMilanka Ringwald     if (!hfp_connection) {
1777af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1778af97579eSMilanka Ringwald     }
1779af97579eSMilanka Ringwald     uint8_t status = hfp_hf_activate_voice_recognition_for_connection(hfp_connection);
1780af97579eSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
1781af97579eSMilanka Ringwald         hfp_hf_run_for_context(hfp_connection);
1782af97579eSMilanka Ringwald     }
1783af97579eSMilanka Ringwald     return status;
1784af97579eSMilanka Ringwald }
1785af97579eSMilanka Ringwald 
1786af97579eSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1787af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1788af97579eSMilanka Ringwald     if (!hfp_connection) {
1789af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1790af97579eSMilanka Ringwald     }
1791af97579eSMilanka Ringwald     uint8_t status = hfp_hf_deactivate_voice_recognition_for_connection(hfp_connection);
1792af97579eSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
1793af97579eSMilanka Ringwald         hfp_hf_run_for_context(hfp_connection);
1794af97579eSMilanka Ringwald     }
1795af97579eSMilanka Ringwald     return status;
1796af97579eSMilanka Ringwald }
1797af97579eSMilanka Ringwald 
1798db3cdbd4SMilanka Ringwald uint8_t hfp_hf_start_enhanced_voice_recognition_session(hci_con_handle_t acl_handle){
17999c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1800a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1801be55a11dSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1802a33eb0c4SMilanka Ringwald     }
1803af97579eSMilanka Ringwald     uint8_t status = hfp_hf_activate_enhanced_voice_recognition_for_connection(hfp_connection);
1804af97579eSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
18051c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1806be55a11dSMilanka Ringwald     }
1807af97579eSMilanka Ringwald     return status;
1808be55a11dSMilanka Ringwald }
1809be55a11dSMilanka Ringwald 
1810db3cdbd4SMilanka Ringwald uint8_t hfp_hf_stop_enhanced_voice_recognition_session(hci_con_handle_t acl_handle){
1811be55a11dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1812be55a11dSMilanka Ringwald     if (!hfp_connection) {
1813be55a11dSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1814be55a11dSMilanka Ringwald     }
1815af97579eSMilanka Ringwald 
1816af97579eSMilanka Ringwald     uint8_t status = hfp_hf_deactivate_enhanced_voice_recognition_for_connection(hfp_connection);
1817af97579eSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
1818af97579eSMilanka Ringwald         hfp_hf_run_for_context(hfp_connection);
1819be55a11dSMilanka Ringwald     }
1820af97579eSMilanka Ringwald     return status;
1821ce263fc8SMatthias Ringwald }
1822ce263fc8SMatthias Ringwald 
18233c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
18249c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1825a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18263c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1827a33eb0c4SMilanka Ringwald     }
1828c8626498SMilanka Ringwald 
18293c65e705SMilanka Ringwald     if (hfp_connection->microphone_gain == gain) {
18303c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
18313c65e705SMilanka Ringwald     }
18323c65e705SMilanka Ringwald 
1833c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1834a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
18353c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1836a0ffb263SMatthias Ringwald     }
18373c65e705SMilanka Ringwald 
1838a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1839a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
18401c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18413c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1842ce263fc8SMatthias Ringwald }
1843ce263fc8SMatthias Ringwald 
18443c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
18459c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1846a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18473c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1848a33eb0c4SMilanka Ringwald     }
1849c8626498SMilanka Ringwald 
18503c65e705SMilanka Ringwald     if (hfp_connection->speaker_gain == gain){
18513c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
18523c65e705SMilanka Ringwald     }
18533c65e705SMilanka Ringwald 
1854c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1855a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
18563c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1857a0ffb263SMatthias Ringwald     }
18583c65e705SMilanka Ringwald 
1859a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1860a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
18611c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18623c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1863ce263fc8SMatthias Ringwald }
1864ce263fc8SMatthias Ringwald 
18653c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
18669c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1867a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18683c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1869a33eb0c4SMilanka Ringwald     }
1870a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
18711c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18723c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1873ce263fc8SMatthias Ringwald }
1874ce263fc8SMatthias Ringwald 
18753c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
18769c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1877a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18783c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1879a33eb0c4SMilanka Ringwald     }
1880a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
18811c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18823c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1883ce263fc8SMatthias Ringwald }
18843deb3ec6SMatthias Ringwald 
18853c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
18869c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1887a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18883c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1889a33eb0c4SMilanka Ringwald     }
1890a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
18911c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18923c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1893667ec068SMatthias Ringwald }
1894667ec068SMatthias Ringwald 
1895667ec068SMatthias Ringwald 
18963c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
18979c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1898a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18993c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1900a33eb0c4SMilanka Ringwald     }
1901a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1902a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
19031c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19043c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1905667ec068SMatthias Ringwald }
1906667ec068SMatthias Ringwald 
19073c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
19089c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1909a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19103c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1911a33eb0c4SMilanka Ringwald     }
1912a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1913a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
19141c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19153c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1916667ec068SMatthias Ringwald }
1917667ec068SMatthias Ringwald 
19183c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
19199c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1920a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19213c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1922a33eb0c4SMilanka Ringwald     }
1923a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1924a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
19251c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19263c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1927667ec068SMatthias Ringwald }
1928667ec068SMatthias Ringwald 
19293c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
19309c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1931a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19323c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1933a33eb0c4SMilanka Ringwald     }
1934a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1935a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
19361c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19373c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1938667ec068SMatthias Ringwald }
1939667ec068SMatthias Ringwald 
19403c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
19419c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1942a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19433c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1944a33eb0c4SMilanka Ringwald     }
1945a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
19461c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19473c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1948667ec068SMatthias Ringwald }
1949667ec068SMatthias Ringwald 
19503c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
19519c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1952a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19533c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1954a33eb0c4SMilanka Ringwald     }
1955667ec068SMatthias Ringwald     // find index for assigned number
1956667ec068SMatthias Ringwald     int i;
1957667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1958667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1959667ec068SMatthias Ringwald             // set value
1960667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1961667ec068SMatthias Ringwald             // mark for update
1962a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1963a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1964667ec068SMatthias Ringwald                 // send update
19651c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
1966a0ffb263SMatthias Ringwald             }
19673c65e705SMilanka Ringwald             return ERROR_CODE_SUCCESS;
1968667ec068SMatthias Ringwald         }
1969667ec068SMatthias Ringwald     }
19703c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1971667ec068SMatthias Ringwald }
1972667ec068SMatthias Ringwald 
1973d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
1974d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1975d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
1976d7f6b5cbSMatthias Ringwald         return 0;
1977d7f6b5cbSMatthias Ringwald     }
1978d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1979d7f6b5cbSMatthias Ringwald }
198076cc1527SMatthias Ringwald 
198176cc1527SMatthias 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){
198276cc1527SMatthias Ringwald 	if (!name){
198376cc1527SMatthias Ringwald 		name = default_hfp_hf_service_name;
198476cc1527SMatthias Ringwald 	}
198576cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
198676cc1527SMatthias Ringwald 
198776cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
198876cc1527SMatthias Ringwald 	//
198976cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
199076cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
199176cc1527SMatthias Ringwald 	//
199276cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
199376cc1527SMatthias Ringwald 	//
199476cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
1995ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
199676cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
199776cc1527SMatthias Ringwald 	}
1998ef3ae4ebSMilanka Ringwald 
1999ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
200056f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
2001ef3ae4ebSMilanka Ringwald     }
2002ef3ae4ebSMilanka Ringwald 
2003ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
200456f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
2005ef3ae4ebSMilanka Ringwald     }
2006ef3ae4ebSMilanka Ringwald 
200776cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
200876cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
200976cc1527SMatthias Ringwald }
201076cc1527SMatthias Ringwald 
201176cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
201268466199SMilanka Ringwald 	btstack_assert(callback != NULL);
201368466199SMilanka Ringwald 
201476cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
201576cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
201676cc1527SMatthias Ringwald }
2017