xref: /btstack/src/classic/hfp_hf.c (revision 8d5005b520634c0b372cbe0dcff50514e18ababe)
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
69aeb0f0feSMatthias Ringwald static const char hfp_hf_default_service_name[] = "Hands-Free unit";
7020b2edb6SMatthias Ringwald 
7120b2edb6SMatthias Ringwald // globals
72aeb0f0feSMatthias Ringwald 
73aeb0f0feSMatthias Ringwald // higher layer callbacks
74aeb0f0feSMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
75aeb0f0feSMatthias Ringwald 
761c6a0fc0SMatthias Ringwald static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration;
7727950165SMatthias Ringwald 
78aeb0f0feSMatthias Ringwald static uint16_t hfp_hf_supported_features;
79aeb0f0feSMatthias Ringwald static uint8_t  hfp_hf_codecs_nr;
80aeb0f0feSMatthias Ringwald static uint8_t  hfp_hf_codecs[HFP_MAX_NUM_CODECS];
813deb3ec6SMatthias Ringwald 
82aeb0f0feSMatthias Ringwald static uint8_t  hfp_hf_indicators_nr;
83aeb0f0feSMatthias Ringwald static uint8_t  hfp_hf_indicators[HFP_MAX_NUM_INDICATORS];
84aeb0f0feSMatthias Ringwald static uint32_t hfp_hf_indicators_value[HFP_MAX_NUM_INDICATORS];
85667ec068SMatthias Ringwald 
8620b2edb6SMatthias Ringwald static uint8_t  hfp_hf_speaker_gain;
8720b2edb6SMatthias Ringwald static uint8_t  hfp_hf_microphone_gain;
883deb3ec6SMatthias Ringwald 
89aeb0f0feSMatthias Ringwald static hfp_call_status_t      hfp_hf_call_status;
90aeb0f0feSMatthias Ringwald static hfp_callsetup_status_t hfp_hf_callsetup_status;
91aeb0f0feSMatthias Ringwald static hfp_callheld_status_t  hfp_hf_callheld_status;
923deb3ec6SMatthias Ringwald 
93aeb0f0feSMatthias Ringwald static char hfp_hf_phone_number[25];
94ce263fc8SMatthias Ringwald 
95ce263fc8SMatthias Ringwald 
9676cc1527SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
97aeb0f0feSMatthias Ringwald 	int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
9876cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
9976cc1527SMatthias Ringwald 	return hf && ag;
10076cc1527SMatthias Ringwald }
10176cc1527SMatthias Ringwald 
10276cc1527SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
103aeb0f0feSMatthias Ringwald 	int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_THREE_WAY_CALLING);
10476cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
10576cc1527SMatthias Ringwald 	return hf && ag;
10676cc1527SMatthias Ringwald }
10776cc1527SMatthias Ringwald 
10876cc1527SMatthias Ringwald 
10976cc1527SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
110aeb0f0feSMatthias Ringwald 	int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_HF_INDICATORS);
11176cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
11276cc1527SMatthias Ringwald 	return hf && ag;
11376cc1527SMatthias Ringwald }
11476cc1527SMatthias Ringwald 
11576cc1527SMatthias Ringwald 
1169c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
1179c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1189c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1199c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1209c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1219c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1229c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1239c9c64c1SMatthias Ringwald         return hfp_connection;
1249c9c64c1SMatthias Ringwald     }
1259c9c64c1SMatthias Ringwald     return NULL;
1269c9c64c1SMatthias Ringwald }
1279c9c64c1SMatthias Ringwald 
12876cc1527SMatthias Ringwald /* emit functinos */
1293deb3ec6SMatthias Ringwald 
130a473a009SMatthias Ringwald static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){
131a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
132d703d377SMatthias Ringwald     uint8_t event[33];
133a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
134a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
135a473a009SMatthias Ringwald     event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION;
136d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
137d703d377SMatthias Ringwald     event[5] = status;
138d703d377SMatthias Ringwald     event[6] = hfp_connection->bnip_type;
139d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 8);
140d703d377SMatthias Ringwald     strncpy((char*)&event[7], hfp_connection->bnip_number, size);
141d703d377SMatthias Ringwald     event[7 + size] = 0;
142a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
143a0ffb263SMatthias Ringwald }
144a0ffb263SMatthias Ringwald 
145a473a009SMatthias Ringwald static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){
146a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
147d703d377SMatthias Ringwald     uint8_t event[32];
148a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
149a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
150a0ffb263SMatthias Ringwald     event[2] = event_subtype;
151d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
152d703d377SMatthias Ringwald     event[5] = hfp_connection->bnip_type;
153d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 7);
154d703d377SMatthias Ringwald     strncpy((char*)&event[6], hfp_connection->bnip_number, size);
155d703d377SMatthias Ringwald     event[6 + size] = 0;
156a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
157a0ffb263SMatthias Ringwald }
158a0ffb263SMatthias Ringwald 
159a473a009SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){
160a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
161d703d377SMatthias Ringwald     uint8_t event[38];
1620aee97efSMilanka Ringwald     int pos = 0;
1630aee97efSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
1640aee97efSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
1650aee97efSMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
166d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
167d703d377SMatthias Ringwald     pos += 2;
168a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_idx;
169a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_dir;
170a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_status;
171a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mode;
172a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mpty;
173a473a009SMatthias Ringwald     event[pos++] = hfp_connection->bnip_type;
174d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - pos - 1);
175a473a009SMatthias Ringwald     strncpy((char*)&event[pos], hfp_connection->bnip_number, size);
1760aee97efSMilanka Ringwald     pos += size;
1770aee97efSMilanka Ringwald     event[pos++] = 0;
178a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
179a0ffb263SMatthias Ringwald }
180a0ffb263SMatthias Ringwald 
18176cc1527SMatthias Ringwald 
182a473a009SMatthias Ringwald static void hfp_emit_ag_indicator_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
183a473a009SMatthias Ringwald 	if (hfp_hf_callback == NULL) return;
184d703d377SMatthias Ringwald 	uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE+1];
18576cc1527SMatthias Ringwald 	int pos = 0;
18676cc1527SMatthias Ringwald 	event[pos++] = HCI_EVENT_HFP_META;
18776cc1527SMatthias Ringwald 	event[pos++] = sizeof(event) - 2;
18876cc1527SMatthias Ringwald 	event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
189d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
190d703d377SMatthias Ringwald     pos += 2;
191a473a009SMatthias Ringwald 	event[pos++] = indicator->index;
192a473a009SMatthias Ringwald 	event[pos++] = indicator->status;
193a473a009SMatthias Ringwald 	event[pos++] = indicator->min_range;
194a473a009SMatthias Ringwald 	event[pos++] = indicator->max_range;
195a473a009SMatthias Ringwald 	event[pos++] = indicator->mandatory;
196a473a009SMatthias Ringwald 	event[pos++] = indicator->enabled;
197a473a009SMatthias Ringwald 	event[pos++] = indicator->status_changed;
198a473a009SMatthias Ringwald 	strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
19976cc1527SMatthias Ringwald 	pos += HFP_MAX_INDICATOR_DESC_SIZE;
20076cc1527SMatthias Ringwald 	event[pos] = 0;
201a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2023deb3ec6SMatthias Ringwald }
2033deb3ec6SMatthias Ringwald 
204a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){
205a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
206d703d377SMatthias Ringwald 	uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
20776cc1527SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
20876cc1527SMatthias Ringwald 	event[1] = sizeof(event) - 2;
20976cc1527SMatthias Ringwald 	event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
210d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
211d703d377SMatthias Ringwald 	event[5] = hfp_connection->network_operator.mode;
212d703d377SMatthias Ringwald 	event[6] = hfp_connection->network_operator.format;
213d703d377SMatthias Ringwald 	strncpy((char*)&event[7], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
214d703d377SMatthias Ringwald 	event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
215a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2163deb3ec6SMatthias Ringwald }
2173deb3ec6SMatthias Ringwald 
218b95cac54SMilanka Ringwald 
219b95cac54SMilanka Ringwald static void hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection){
220b95cac54SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
22151aa5d5aSMilanka Ringwald     uint8_t event[HFP_MAX_VR_TEXT_SIZE + 11];
222b95cac54SMilanka Ringwald     int pos = 0;
223b95cac54SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
224b95cac54SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
225b95cac54SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE;
226b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
227b95cac54SMilanka Ringwald     pos += 2;
228b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->ag_msg.text_id);
229b95cac54SMilanka Ringwald     pos += 2;
230b95cac54SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_type;
231e83f1be7SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_operation;
232b95cac54SMilanka Ringwald 
23351aa5d5aSMilanka Ringwald     // length, zero ending is already in message
234b95cac54SMilanka Ringwald     uint8_t * value = &hfp_connection->line_buffer[0];
23551aa5d5aSMilanka Ringwald     uint16_t  value_length = hfp_connection->ag_vra_msg_length;
23651aa5d5aSMilanka Ringwald 
23751aa5d5aSMilanka Ringwald     little_endian_store_16(event, pos, value_length);
238b95cac54SMilanka Ringwald     pos += 2;
23951aa5d5aSMilanka Ringwald     memcpy(&event[pos], value, value_length);
24051aa5d5aSMilanka Ringwald     pos += value_length;
24151aa5d5aSMilanka Ringwald 
242b95cac54SMilanka Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
243b95cac54SMilanka Ringwald }
244b95cac54SMilanka Ringwald 
24576cc1527SMatthias Ringwald /* send commands */
24689425bfcSMilanka Ringwald 
24789425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2483deb3ec6SMatthias Ringwald     char buffer[20];
2491599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
25089425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
25189425bfcSMilanka Ringwald }
25289425bfcSMilanka Ringwald 
25389425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
25489425bfcSMilanka Ringwald     char buffer[20];
2551599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
25689425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
25789425bfcSMilanka Ringwald }
25889425bfcSMilanka Ringwald 
25986da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
26089425bfcSMilanka Ringwald     char buffer[40];
2611599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
2623deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2633deb3ec6SMatthias Ringwald }
2643deb3ec6SMatthias Ringwald 
2653deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2663deb3ec6SMatthias Ringwald     char buffer[30];
26789425bfcSMilanka Ringwald     const int size = sizeof(buffer);
26889425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
269aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_codecs, hfp_hf_codecs_nr);
2701599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2713deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2723deb3ec6SMatthias Ringwald }
2733deb3ec6SMatthias Ringwald 
2743deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2753deb3ec6SMatthias Ringwald     char buffer[50];
27689425bfcSMilanka Ringwald     const int size = sizeof(buffer);
27789425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
27889425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
2791599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2803deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2813deb3ec6SMatthias Ringwald }
2823deb3ec6SMatthias Ringwald 
2833deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2843deb3ec6SMatthias Ringwald     char buffer[30];
28589425bfcSMilanka Ringwald     const int size = sizeof(buffer);
28689425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
287aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_indicators, hfp_hf_indicators_nr);
2881599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2893deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2903deb3ec6SMatthias Ringwald }
2913deb3ec6SMatthias Ringwald 
29289425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2933deb3ec6SMatthias Ringwald     char buffer[20];
2941599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
295ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
296ce263fc8SMatthias Ringwald }
297ce263fc8SMatthias Ringwald 
298ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
299ce263fc8SMatthias Ringwald     char buffer[40];
300aeb0f0feSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, hfp_hf_phone_number);
301ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
302ce263fc8SMatthias Ringwald }
303ce263fc8SMatthias Ringwald 
304a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
305ce263fc8SMatthias Ringwald     char buffer[40];
3061599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
307ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
308ce263fc8SMatthias Ringwald }
309ce263fc8SMatthias Ringwald 
310f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
31189425bfcSMilanka Ringwald     char buffer[40];
3121599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
313ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
314ce263fc8SMatthias Ringwald }
315ce263fc8SMatthias Ringwald 
316ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
317ce263fc8SMatthias Ringwald     char buffer[20];
3181599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
319ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
320ce263fc8SMatthias Ringwald }
321ce263fc8SMatthias Ringwald 
32297d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
3231599fe57SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "ATA\r");
32497d2cadbSMatthias Ringwald }
32597d2cadbSMatthias Ringwald 
32689425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
327aeb0f0feSMatthias Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_hf_supported_features);
32889425bfcSMilanka Ringwald }
32989425bfcSMilanka Ringwald 
33089425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
33189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
33289425bfcSMilanka Ringwald }
33389425bfcSMilanka Ringwald 
33489425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
33589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
33689425bfcSMilanka Ringwald }
33789425bfcSMilanka Ringwald 
33889425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
33989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
34089425bfcSMilanka Ringwald }
34189425bfcSMilanka Ringwald 
34289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
34389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
34489425bfcSMilanka Ringwald }
34589425bfcSMilanka Ringwald 
34689425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
34789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
34889425bfcSMilanka Ringwald }
34989425bfcSMilanka Ringwald 
35089425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
35189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
35289425bfcSMilanka Ringwald }
35389425bfcSMilanka Ringwald 
35489425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
35589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
35689425bfcSMilanka Ringwald }
35789425bfcSMilanka Ringwald 
35889425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
35989425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
36089425bfcSMilanka Ringwald }
36189425bfcSMilanka Ringwald 
36289425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
36389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
36489425bfcSMilanka Ringwald }
36589425bfcSMilanka Ringwald 
36689425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
36789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
36889425bfcSMilanka Ringwald }
36989425bfcSMilanka Ringwald 
37089425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
37189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
37289425bfcSMilanka Ringwald }
37389425bfcSMilanka Ringwald 
37489425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
37589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
37689425bfcSMilanka Ringwald }
37789425bfcSMilanka Ringwald 
37889425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
37989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
38089425bfcSMilanka Ringwald }
38189425bfcSMilanka Ringwald 
38289425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
38389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
38489425bfcSMilanka Ringwald }
38589425bfcSMilanka Ringwald 
38689425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
38789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
38889425bfcSMilanka Ringwald }
38989425bfcSMilanka Ringwald 
39089425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
39189425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
39289425bfcSMilanka Ringwald }
39389425bfcSMilanka Ringwald 
39489425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
39589425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
39689425bfcSMilanka Ringwald }
39789425bfcSMilanka Ringwald 
398ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
39989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
400ce263fc8SMatthias Ringwald }
401ce263fc8SMatthias Ringwald 
402667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
40389425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
404667ec068SMatthias Ringwald }
405667ec068SMatthias Ringwald 
40676cc1527SMatthias Ringwald /* state machines */
4073deb3ec6SMatthias Ringwald 
408a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
409a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
410a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
411aa4dd815SMatthias Ringwald     int done = 1;
412498a8121SMilanka Ringwald     log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state);
413a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4143deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
415aeb0f0feSMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_hf_codecs, &hfp_hf_codecs_nr);
416a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
417a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4183deb3ec6SMatthias Ringwald             break;
4193deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
420a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
421a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4223deb3ec6SMatthias Ringwald             break;
4233deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
424a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
425a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4263deb3ec6SMatthias Ringwald             break;
4273deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
428a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
429a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4303deb3ec6SMatthias Ringwald             break;
4313deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
432a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
433a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4343deb3ec6SMatthias Ringwald             break;
4353deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
436a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
437a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4383deb3ec6SMatthias Ringwald             break;
4393deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
440a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
441a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4423deb3ec6SMatthias Ringwald             break;
4433deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
444a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
445a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4463deb3ec6SMatthias Ringwald             break;
4473deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
448a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
449a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4503deb3ec6SMatthias Ringwald             break;
4513deb3ec6SMatthias Ringwald         default:
452aa4dd815SMatthias Ringwald             done = 0;
4533deb3ec6SMatthias Ringwald             break;
4543deb3ec6SMatthias Ringwald     }
4553deb3ec6SMatthias Ringwald     return done;
4563deb3ec6SMatthias Ringwald }
4573deb3ec6SMatthias Ringwald 
458ce263fc8SMatthias Ringwald 
459a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
460a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
461498a8121SMilanka Ringwald     if (hfp_connection->ok_pending){
462498a8121SMilanka Ringwald         return 0;
463498a8121SMilanka Ringwald     }
464ce263fc8SMatthias Ringwald     int done = 0;
465a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
466a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
467ce263fc8SMatthias Ringwald         done = 1;
468a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
469ce263fc8SMatthias Ringwald         return done;
470ce263fc8SMatthias Ringwald     };
471a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
472a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
473ce263fc8SMatthias Ringwald         done = 1;
474a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
475a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
476a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
477ce263fc8SMatthias Ringwald         return done;
478ce263fc8SMatthias Ringwald     }
479ce263fc8SMatthias Ringwald 
480a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
481ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
482a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
483a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
484a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
485ce263fc8SMatthias Ringwald             return 1;
486ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
487a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
488a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
489a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
490ce263fc8SMatthias Ringwald             return 1;
491ce263fc8SMatthias Ringwald         default:
492ce263fc8SMatthias Ringwald             break;
493ce263fc8SMatthias Ringwald     }
494ce263fc8SMatthias Ringwald 
495a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
496a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
497ce263fc8SMatthias Ringwald         done = 1;
498a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
499ce263fc8SMatthias Ringwald         return done;
500ce263fc8SMatthias Ringwald     }
501ce263fc8SMatthias Ringwald 
502ce263fc8SMatthias Ringwald     return done;
503ce263fc8SMatthias Ringwald }
504ce263fc8SMatthias Ringwald 
505af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
506be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
507be55a11dSMilanka Ringwald         return 0;
508be55a11dSMilanka Ringwald     }
509be55a11dSMilanka Ringwald     int done = 0;
510fd4151d1SMilanka Ringwald 
5110b4debbfSMilanka Ringwald     if (hfp_connection->ok_pending == 1){
5120b4debbfSMilanka Ringwald         return 0;
5130b4debbfSMilanka Ringwald     }
5140b4debbfSMilanka Ringwald     // voice recognition activated from AG
5150b4debbfSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){
5160b4debbfSMilanka Ringwald         switch(hfp_connection->vra_state_requested){
5170b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
5180b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
519de9e0ea7SMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
5200b4debbfSMilanka Ringwald                 // ignore AG command, continue to wait for OK
5210b4debbfSMilanka Ringwald                 return 0;
522cf75be85SMilanka Ringwald 
5230b4debbfSMilanka Ringwald             default:
524b95cac54SMilanka Ringwald                 if (hfp_connection->ag_vra_msg_length > 0){
525b95cac54SMilanka Ringwald                     hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection);
526b95cac54SMilanka Ringwald                     hfp_connection->ag_vra_msg_length = 0;
527b95cac54SMilanka Ringwald                     break;
528b95cac54SMilanka Ringwald                 }
529cf75be85SMilanka Ringwald                 switch(hfp_connection->ag_vra_state){
530cf75be85SMilanka Ringwald                     case HFP_VOICE_RECOGNITION_STATE_AG_READY:
531013cc750SMilanka Ringwald                         switch (hfp_connection->ag_vra_status){
532013cc750SMilanka Ringwald                             case 0:
5330b4debbfSMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
534013cc750SMilanka Ringwald                                 break;
535013cc750SMilanka Ringwald                             case 1:
536013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
537013cc750SMilanka Ringwald                                 break;
538013cc750SMilanka Ringwald                             case 2:
539013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
540013cc750SMilanka Ringwald                                 break;
541013cc750SMilanka Ringwald                             default:
542013cc750SMilanka Ringwald                                 break;
5430b4debbfSMilanka Ringwald                         }
5440b4debbfSMilanka Ringwald                         break;
545cf75be85SMilanka Ringwald                     default:
546cf75be85SMilanka Ringwald                         // state messages from AG
547cf75be85SMilanka Ringwald                         hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
548*8d5005b5SMilanka Ringwald                         hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
549cf75be85SMilanka Ringwald                         break;
550cf75be85SMilanka Ringwald                 }
551cf75be85SMilanka Ringwald                 break;
5520b4debbfSMilanka Ringwald         }
5530b4debbfSMilanka Ringwald         hfp_connection->command = HFP_CMD_NONE;
5540b4debbfSMilanka Ringwald     }
5550b4debbfSMilanka Ringwald 
5560b4debbfSMilanka Ringwald 
557498a8121SMilanka Ringwald     switch (hfp_connection->vra_state_requested){
558fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
559fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
560fdda66c0SMilanka Ringwald             if (done != 0){
561fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
562498a8121SMilanka Ringwald                 hfp_connection->ok_pending = 1;
563498a8121SMilanka Ringwald             }
564fd4151d1SMilanka Ringwald             return 1;
565fd4151d1SMilanka Ringwald 
566fd4151d1SMilanka Ringwald 
567fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
568fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
569fdda66c0SMilanka Ringwald             if (done != 0){
570fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
571fd4151d1SMilanka Ringwald                 hfp_connection->ok_pending = 1;
572fd4151d1SMilanka Ringwald                 return 1;
5730b4debbfSMilanka Ringwald             }
5740b4debbfSMilanka Ringwald             break;
575013cc750SMilanka Ringwald 
576de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
577de9e0ea7SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
578de9e0ea7SMilanka Ringwald             if (done != 0){
579de9e0ea7SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
580de9e0ea7SMilanka Ringwald                 hfp_connection->ok_pending = 1;
581de9e0ea7SMilanka Ringwald                 return 1;
582de9e0ea7SMilanka Ringwald             }
583de9e0ea7SMilanka Ringwald             break;
584de9e0ea7SMilanka Ringwald 
585de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
586de9e0ea7SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
587de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
588de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
589de9e0ea7SMilanka Ringwald             if (hfp_connection->activate_voice_recognition){
590de9e0ea7SMilanka Ringwald                 hfp_hf_activate_voice_recognition(hfp_connection->acl_handle);
591de9e0ea7SMilanka Ringwald             } else {
592553a4a56SMilanka Ringwald                 hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
593de9e0ea7SMilanka Ringwald             }
594de9e0ea7SMilanka Ringwald             break;
595de9e0ea7SMilanka Ringwald 
596be55a11dSMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
597498a8121SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
598498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
599de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
600de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
601de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
602de9e0ea7SMilanka Ringwald             } else {
603553a4a56SMilanka Ringwald                 hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
604de9e0ea7SMilanka Ringwald             }
605be55a11dSMilanka Ringwald             break;
606be55a11dSMilanka Ringwald 
607de9e0ea7SMilanka Ringwald 
608013cc750SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
609013cc750SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
610498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
611de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
612de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
613de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
614de9e0ea7SMilanka Ringwald             } else {
615de9e0ea7SMilanka Ringwald                 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
616de9e0ea7SMilanka Ringwald             }
617be55a11dSMilanka Ringwald             break;
618fd4151d1SMilanka Ringwald 
619be55a11dSMilanka Ringwald         default:
620be55a11dSMilanka Ringwald             break;
621be55a11dSMilanka Ringwald     }
622be55a11dSMilanka Ringwald     return done;
623be55a11dSMilanka Ringwald }
624be55a11dSMilanka Ringwald 
625be55a11dSMilanka Ringwald 
626be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
627a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
628ce263fc8SMatthias Ringwald 
629332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
630332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
631ce263fc8SMatthias Ringwald 
632a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
633a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
634332ca98fSMatthias Ringwald 		return 1;
635332ca98fSMatthias Ringwald     }
636332ca98fSMatthias Ringwald 
6371cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
6381cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
639ce263fc8SMatthias Ringwald 
640a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
641fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
6421cc65c4fSMatthias Ringwald 		return 1;
6431cc65c4fSMatthias Ringwald     }
6441cc65c4fSMatthias Ringwald 
6451cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
6461cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
6471cc65c4fSMatthias Ringwald 
648a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
649a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
6501cc65c4fSMatthias Ringwald 		return 1;
6511cc65c4fSMatthias Ringwald     }
652ce263fc8SMatthias Ringwald 
653ce263fc8SMatthias Ringwald     return 0;
654ce263fc8SMatthias Ringwald }
655ce263fc8SMatthias Ringwald 
656a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
657505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
658505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
659ce263fc8SMatthias Ringwald 
66064f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
661a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
662a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
663a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
664ce263fc8SMatthias Ringwald         return 1;
665ce263fc8SMatthias Ringwald     }
666ce263fc8SMatthias Ringwald 
667a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
668ce263fc8SMatthias Ringwald 
669ce263fc8SMatthias Ringwald     // run codecs exchange
670a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
671ce263fc8SMatthias Ringwald     if (done) return 1;
672ce263fc8SMatthias Ringwald 
67338200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
67438200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
67538200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
67638200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
67738200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
67838200c1dSMilanka Ringwald         return 1;
67938200c1dSMilanka Ringwald     }
680ce263fc8SMatthias Ringwald     return 0;
681ce263fc8SMatthias Ringwald }
682ce263fc8SMatthias Ringwald 
68338200c1dSMilanka Ringwald 
684a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
685eaf2b0a1SMatthias Ringwald 
686eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
687eaf2b0a1SMatthias Ringwald 
688a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
689a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
690a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
691ce263fc8SMatthias Ringwald         return 1;
692ce263fc8SMatthias Ringwald     }
693ce263fc8SMatthias Ringwald     return 0;
694ce263fc8SMatthias Ringwald }
695ce263fc8SMatthias Ringwald 
6961c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
6977522e673SMatthias Ringwald 
69876cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
69976cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
70076cc1527SMatthias Ringwald 
70176cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
70276cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
70322387625SMatthias Ringwald 
7043721a235SMatthias Ringwald 	// assert command could be sent
7053721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
7063721a235SMatthias Ringwald 
7073721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
7083721a235SMatthias Ringwald     // WBS Disassociate
7093721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
7103721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
7113721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
7123721a235SMatthias Ringwald         return;
7133721a235SMatthias Ringwald     }
7143721a235SMatthias Ringwald     // Write Codec Config
7153721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
7163721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
7173721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
7183721a235SMatthias Ringwald         return;
7193721a235SMatthias Ringwald     }
7203721a235SMatthias Ringwald     // WBS Associate
7213721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
7223721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
7233721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
7243721a235SMatthias Ringwald         return;
7253721a235SMatthias Ringwald     }
7263721a235SMatthias Ringwald #endif
727689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
728689d4323SMatthias Ringwald     // Enable WBS
729689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
730689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
731689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
732689d4323SMatthias Ringwald         return;
733689d4323SMatthias Ringwald     }
734689d4323SMatthias Ringwald     // Write I2S/PCM params
735689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
736689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
737689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
738689d4323SMatthias Ringwald         return;
739689d4323SMatthias Ringwald     }
740689d4323SMatthias Ringwald     // Disable WBS
741689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
742689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
743689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
744689d4323SMatthias Ringwald         return;
745689d4323SMatthias Ringwald     }
746689d4323SMatthias Ringwald #endif
74748e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
74848e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
74948e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
75048e6eeeeSMatthias Ringwald         return;
75148e6eeeeSMatthias Ringwald     }
75248e6eeeeSMatthias Ringwald #endif
7533721a235SMatthias Ringwald 
754cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
755cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
756cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
7577522e673SMatthias Ringwald         // notify about codec selection if not done already
7587522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
7597522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
7607522e673SMatthias Ringwald         }
761cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
7627522e673SMatthias Ringwald         return;
7637522e673SMatthias Ringwald     }
7647522e673SMatthias Ringwald 
765d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
766d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
767d4dd47ffSMatthias Ringwald         return;
768d4dd47ffSMatthias Ringwald     }
769a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
770ce263fc8SMatthias Ringwald     if (!done){
771a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
772ce263fc8SMatthias Ringwald     }
773ce263fc8SMatthias Ringwald     if (!done){
774c95b5b3cSMilanka Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
775be55a11dSMilanka Ringwald     }
776be55a11dSMilanka Ringwald     if (!done){
777c95b5b3cSMilanka Ringwald         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
778ce263fc8SMatthias Ringwald     }
779ce263fc8SMatthias Ringwald     if (!done){
780a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
781ce263fc8SMatthias Ringwald     }
782ce263fc8SMatthias Ringwald 
783e2c3b58dSMilanka Ringwald     // don't send a new command while ok still pending or SLC is not established
784e2c3b58dSMilanka Ringwald     if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){
785e2c3b58dSMilanka Ringwald         return;
786e2c3b58dSMilanka Ringwald     }
7871016a228SMatthias Ringwald 
788a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
789a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
790a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
791a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
792ce263fc8SMatthias Ringwald         return;
793ce263fc8SMatthias Ringwald     }
794ce263fc8SMatthias Ringwald 
795a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
796a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
797a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
798a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
799ce263fc8SMatthias Ringwald         return;
800ce263fc8SMatthias Ringwald     }
801ce263fc8SMatthias Ringwald 
802a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
803a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
804a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
805a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
806ce263fc8SMatthias Ringwald         return;
807ce263fc8SMatthias Ringwald     }
808ce263fc8SMatthias Ringwald 
809a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
810a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
811a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
812a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
813ce263fc8SMatthias Ringwald         return;
814ce263fc8SMatthias Ringwald     }
815ce263fc8SMatthias Ringwald 
816a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
817a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
81899af1e28SMilanka Ringwald         hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR;
819a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
820e2c3b58dSMilanka Ringwald         hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0);
821ce263fc8SMatthias Ringwald         return;
822ce263fc8SMatthias Ringwald     }
823ce263fc8SMatthias Ringwald 
824a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
825a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
826a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
827a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
828ce263fc8SMatthias Ringwald         return;
829ce263fc8SMatthias Ringwald     }
830ce263fc8SMatthias Ringwald 
831a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
832a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
833a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
834a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
835ce263fc8SMatthias Ringwald         return;
836ce263fc8SMatthias Ringwald     }
837ce263fc8SMatthias Ringwald 
838a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
839a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
840a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
841a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
842ce263fc8SMatthias Ringwald         return;
843ce263fc8SMatthias Ringwald     }
844ce263fc8SMatthias Ringwald 
845a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
846a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
847a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
848a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
849ce263fc8SMatthias Ringwald         return;
850ce263fc8SMatthias Ringwald     }
851ce263fc8SMatthias Ringwald 
852a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
853a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
854a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
855a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
856ce263fc8SMatthias Ringwald         return;
857ce263fc8SMatthias Ringwald     }
858ce263fc8SMatthias Ringwald 
859a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
860a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
861a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
862a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
863ce263fc8SMatthias Ringwald         return;
864ce263fc8SMatthias Ringwald     }
865ce263fc8SMatthias Ringwald 
866a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
867a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
868a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
869a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
870ce263fc8SMatthias Ringwald         return;
871ce263fc8SMatthias Ringwald     }
872ce263fc8SMatthias Ringwald 
873a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
874a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
875a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
876a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
877ce263fc8SMatthias Ringwald         return;
878ce263fc8SMatthias Ringwald     }
879ce263fc8SMatthias Ringwald 
880a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
881a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
882a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
883a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
884ce263fc8SMatthias Ringwald         return;
885ce263fc8SMatthias Ringwald     }
886ce263fc8SMatthias Ringwald 
887a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
888a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
889a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
890a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
891ce263fc8SMatthias Ringwald         return;
892ce263fc8SMatthias Ringwald     }
893ce263fc8SMatthias Ringwald 
894a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
895a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
896a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
897a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
898ce263fc8SMatthias Ringwald         return;
899ce263fc8SMatthias Ringwald     }
900ce263fc8SMatthias Ringwald 
901a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
902a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
903a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
904a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
905667ec068SMatthias Ringwald         return;
906667ec068SMatthias Ringwald     }
907667ec068SMatthias Ringwald 
908a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
909a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
910a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
911a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
912a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
913ce263fc8SMatthias Ringwald         return;
914ce263fc8SMatthias Ringwald     }
915ce263fc8SMatthias Ringwald 
916a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
917a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
918a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
919a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
920ce263fc8SMatthias Ringwald         return;
921ce263fc8SMatthias Ringwald     }
922ce263fc8SMatthias Ringwald 
923a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
924a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
925a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
926a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
927667ec068SMatthias Ringwald         return;
928667ec068SMatthias Ringwald     }
929667ec068SMatthias Ringwald 
930a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
931a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
932667ec068SMatthias Ringwald         char buffer[20];
933a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
934667ec068SMatthias Ringwald             case '?':
9351599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
936ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
937ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
938a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
939667ec068SMatthias Ringwald                 return;
940667ec068SMatthias Ringwald             case '0':
941667ec068SMatthias Ringwald             case '1':
942667ec068SMatthias Ringwald             case '2':
9431599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
944ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
945ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
946ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
947a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
948667ec068SMatthias Ringwald                 return;
949667ec068SMatthias Ringwald             default:
950667ec068SMatthias Ringwald                 break;
951667ec068SMatthias Ringwald         }
952667ec068SMatthias Ringwald         return;
953667ec068SMatthias Ringwald     }
954667ec068SMatthias Ringwald 
955a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
956a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
957667ec068SMatthias Ringwald         char buffer[20];
9581599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
959ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
960ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
961a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
962667ec068SMatthias Ringwald         return;
963667ec068SMatthias Ringwald     }
964667ec068SMatthias Ringwald 
965667ec068SMatthias Ringwald     // update HF indicators
966a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
967667ec068SMatthias Ringwald         int i;
968aeb0f0feSMatthias Ringwald         for (i=0; i < hfp_hf_indicators_nr; i++){
969a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
970a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
971a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
972a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
973667ec068SMatthias Ringwald                     char buffer[30];
9741599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
975ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
976aeb0f0feSMatthias Ringwald                              hfp_hf_indicators[i],
977aeb0f0feSMatthias Ringwald                              (unsigned int)hfp_hf_indicators_value[i]);
978ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
979a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
980667ec068SMatthias Ringwald                 } else {
981aeb0f0feSMatthias Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]);
982667ec068SMatthias Ringwald                 }
983667ec068SMatthias Ringwald                 return;
984667ec068SMatthias Ringwald             }
985667ec068SMatthias Ringwald         }
986667ec068SMatthias Ringwald     }
987667ec068SMatthias Ringwald 
988ce263fc8SMatthias Ringwald     if (done) return;
989ce263fc8SMatthias Ringwald     // deal with disconnect
990a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
991ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
992a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
993a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
994ce263fc8SMatthias Ringwald             break;
995ce263fc8SMatthias Ringwald 
996ce263fc8SMatthias Ringwald         default:
997ce263fc8SMatthias Ringwald             break;
998ce263fc8SMatthias Ringwald     }
999ce263fc8SMatthias Ringwald }
1000ce263fc8SMatthias Ringwald 
1001a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
1002a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
10036a7f44bdSMilanka Ringwald 
1004ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
10057522e673SMatthias Ringwald 
1006184a03edSMilanka Ringwald     uint8_t i;
1007184a03edSMilanka Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1008184a03edSMilanka Ringwald         hfp_connection->ag_indicators[i].status_changed = 0;
1009184a03edSMilanka Ringwald         hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1010184a03edSMilanka Ringwald     }
1011667ec068SMatthias Ringwald     // restore volume settings
1012a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
1013a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1014ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
1015a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
1016a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1017ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
1018667ec068SMatthias Ringwald     // enable all indicators
1019aeb0f0feSMatthias Ringwald     for (i=0; i < hfp_hf_indicators_nr; i++){
1020aeb0f0feSMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i];
1021a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
1022667ec068SMatthias Ringwald     }
1023ce263fc8SMatthias Ringwald }
1024ce263fc8SMatthias Ringwald 
10251cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
1026aeb0f0feSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){
10271cc65c4fSMatthias Ringwald 		// Codec supported, confirm
10281cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
10291cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
10301cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
10311cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
10321cc65c4fSMatthias Ringwald 
10331cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
10341cc65c4fSMatthias Ringwald 	} else {
10351cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
10361cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
10371cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
10381cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
10391cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10401cc65c4fSMatthias Ringwald 
10411cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
10421cc65c4fSMatthias Ringwald 	}
10431cc65c4fSMatthias Ringwald }
10441cc65c4fSMatthias Ringwald 
1045e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){
1046e2c3b58dSMilanka Ringwald     bool event_emited = true;
1047e2c3b58dSMilanka Ringwald 
104899af1e28SMilanka Ringwald     switch (hfp_connection->response_pending_for_command){
1049e2c3b58dSMilanka Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1050e2c3b58dSMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status);
1051e2c3b58dSMilanka Ringwald             break;
1052e2c3b58dSMilanka Ringwald         default:
1053e2c3b58dSMilanka Ringwald             event_emited = false;
1054e2c3b58dSMilanka Ringwald 
1055a0ffb263SMatthias Ringwald             switch (hfp_connection->state){
10563deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
1057a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
1058a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_NOTIFY_ON_CODECS;
10593deb3ec6SMatthias Ringwald                         break;
10603deb3ec6SMatthias Ringwald                     }
1061a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
10623deb3ec6SMatthias Ringwald                     break;
10633deb3ec6SMatthias Ringwald 
10643deb3ec6SMatthias Ringwald                 case HFP_W4_NOTIFY_ON_CODECS:
1065a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
10663deb3ec6SMatthias Ringwald                     break;
10673deb3ec6SMatthias Ringwald 
10683deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS:
1069a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
10703deb3ec6SMatthias Ringwald                     break;
10713deb3ec6SMatthias Ringwald 
10723deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1073a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
10743deb3ec6SMatthias Ringwald                     break;
10753deb3ec6SMatthias Ringwald 
10763deb3ec6SMatthias Ringwald                 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1077a0ffb263SMatthias Ringwald                     if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1078a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
10793deb3ec6SMatthias Ringwald                         break;
10803deb3ec6SMatthias Ringwald                     }
1081a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1082a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
10833deb3ec6SMatthias Ringwald                         break;
10843deb3ec6SMatthias Ringwald                     }
1085a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
10863deb3ec6SMatthias Ringwald                     break;
10873deb3ec6SMatthias Ringwald 
10883deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1089a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1090a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
10913deb3ec6SMatthias Ringwald                         break;
10923deb3ec6SMatthias Ringwald                     }
1093a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
10943deb3ec6SMatthias Ringwald                     break;
10953deb3ec6SMatthias Ringwald 
10963deb3ec6SMatthias Ringwald                 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1097a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
10983deb3ec6SMatthias Ringwald                     break;
10993deb3ec6SMatthias Ringwald 
11003deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1101a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
11023deb3ec6SMatthias Ringwald                     break;
11033deb3ec6SMatthias Ringwald 
11043deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1105a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11063deb3ec6SMatthias Ringwald                     break;
1107ce263fc8SMatthias Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1108a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1109a0ffb263SMatthias Ringwald                         hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1110ca59be51SMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1111ce263fc8SMatthias Ringwald                         break;
1112ce263fc8SMatthias Ringwald                     }
11133deb3ec6SMatthias Ringwald 
1114a0ffb263SMatthias Ringwald                     if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1115a0ffb263SMatthias Ringwald                         hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1116ca59be51SMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1117ce263fc8SMatthias Ringwald                         break;
11183deb3ec6SMatthias Ringwald                     }
11193deb3ec6SMatthias Ringwald 
1120a0ffb263SMatthias Ringwald                     switch (hfp_connection->hf_query_operator_state){
1121ce263fc8SMatthias Ringwald                         case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1122a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1123ce263fc8SMatthias Ringwald                             break;
1124ce263fc8SMatthias Ringwald                         case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1125a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1126a473a009SMatthias Ringwald                             hfp_emit_network_operator_event(hfp_connection);
1127ce263fc8SMatthias Ringwald                             break;
1128ce263fc8SMatthias Ringwald                         default:
1129ce263fc8SMatthias Ringwald                             break;
11303deb3ec6SMatthias Ringwald                     }
1131ce263fc8SMatthias Ringwald 
1132a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_extended_audio_gateway_error_report){
1133a0ffb263SMatthias Ringwald                         hfp_connection->enable_extended_audio_gateway_error_report = 0;
1134ce263fc8SMatthias Ringwald                         break;
11353deb3ec6SMatthias Ringwald                     }
11363deb3ec6SMatthias Ringwald 
1137a0ffb263SMatthias Ringwald                     switch (hfp_connection->codecs_state){
1138aa4dd815SMatthias Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1139a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
11403deb3ec6SMatthias Ringwald                             break;
1141ce263fc8SMatthias Ringwald                         case HFP_CODECS_HF_CONFIRMED_CODEC:
1142a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1143ce263fc8SMatthias Ringwald                             break;
11443deb3ec6SMatthias Ringwald                         default:
11453deb3ec6SMatthias Ringwald                             break;
11463deb3ec6SMatthias Ringwald                     }
1147af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
1148be55a11dSMilanka Ringwald                     break;
1149be55a11dSMilanka Ringwald                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
1150af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
11513deb3ec6SMatthias Ringwald                     break;
11523deb3ec6SMatthias Ringwald                 default:
11533deb3ec6SMatthias Ringwald                     break;
11543deb3ec6SMatthias Ringwald             }
1155e2c3b58dSMilanka Ringwald             break;
1156e2c3b58dSMilanka Ringwald     }
11573deb3ec6SMatthias Ringwald 
11583deb3ec6SMatthias Ringwald     // done
115999af1e28SMilanka Ringwald     hfp_connection->response_pending_for_command = HFP_CMD_NONE;
1160be55a11dSMilanka Ringwald     hfp_connection->ok_pending = 0;
1161a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
1162e2c3b58dSMilanka Ringwald     return event_emited;
11633deb3ec6SMatthias Ringwald }
11643deb3ec6SMatthias Ringwald 
1165a03dbc20SMilanka Ringwald static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){
1166a03dbc20SMilanka Ringwald     switch (callsetup_status){
1167a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1168a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1169a03dbc20SMilanka Ringwald             return true;
1170a03dbc20SMilanka Ringwald         default:
1171a03dbc20SMilanka Ringwald             return false;
1172a03dbc20SMilanka Ringwald    }
1173a03dbc20SMilanka Ringwald }
1174be55a11dSMilanka Ringwald 
1175b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
11764562e2a2SMatthias Ringwald     uint16_t i;
1177a03dbc20SMilanka Ringwald 
11784562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
11794562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
11804562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1181a03dbc20SMilanka Ringwald                 hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1182a03dbc20SMilanka Ringwald                 bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status);
1183a03dbc20SMilanka Ringwald                 bool ringing_new = hfp_is_ringing(new_hf_callsetup_status);
1184a03dbc20SMilanka Ringwald                 if (ringing_old != ringing_new){
1185a03dbc20SMilanka Ringwald                     if (ringing_new){
1186a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING);
1187a03dbc20SMilanka Ringwald                     } else {
1188a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING);
1189a03dbc20SMilanka Ringwald                     }
1190a03dbc20SMilanka Ringwald                 }
1191a03dbc20SMilanka Ringwald                 hfp_hf_callsetup_status = new_hf_callsetup_status;
11924562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1193aeb0f0feSMatthias Ringwald                 hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
11944562e2a2SMatthias Ringwald                 // avoid set but not used warning
1195aeb0f0feSMatthias Ringwald                 (void) hfp_hf_callheld_status;
11964562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1197674ebed5SMilanka Ringwald                 hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1198674ebed5SMilanka Ringwald                 if (hfp_hf_call_status != new_hf_call_status){
1199674ebed5SMilanka Ringwald                     if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){
1200674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1201674ebed5SMilanka Ringwald                     } else {
1202674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1203674ebed5SMilanka Ringwald                     }
1204674ebed5SMilanka Ringwald                 }
1205674ebed5SMilanka Ringwald                 hfp_hf_call_status = new_hf_call_status;
12064562e2a2SMatthias Ringwald             }
12074562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1208a473a009SMatthias Ringwald             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
12094562e2a2SMatthias Ringwald             break;
12104562e2a2SMatthias Ringwald         }
12114562e2a2SMatthias Ringwald     }
12124562e2a2SMatthias Ringwald }
12134562e2a2SMatthias Ringwald 
1214426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1215186dd3d2SMatthias Ringwald     int value;
1216186dd3d2SMatthias Ringwald     int i;
1217e2c3b58dSMilanka Ringwald     bool event_emited;
1218e2c3b58dSMilanka Ringwald 
1219a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1220667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1221a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1222a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1223667ec068SMatthias Ringwald             break;
1224667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1225a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1226ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1227667ec068SMatthias Ringwald             break;
1228667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1229a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1230a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1231667ec068SMatthias Ringwald             break;
1232ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1233a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12342308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1235667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1236ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1237ce263fc8SMatthias Ringwald             break;
1238ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1239a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12402308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1241667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1242ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1243ce263fc8SMatthias Ringwald             break;
1244ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1245a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1246ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1247a0ffb263SMatthias Ringwald             break;
1248a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1249a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1250a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1251a0ffb263SMatthias Ringwald             break;
1252a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1253a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1254a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1255ce263fc8SMatthias Ringwald             break;
1256ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1257a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12585a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1259a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1260ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1261ce263fc8SMatthias Ringwald             break;
12620b4debbfSMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
12630b4debbfSMilanka Ringwald             break;
1264fdda66c0SMilanka Ringwald         case HFP_CMD_ERROR:
126590244c92SMilanka Ringwald             switch (hfp_connection->state){
126690244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
126790244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
126890244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1269fdda66c0SMilanka Ringwald                             hfp_reset_context_flags(hfp_connection);
127090244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
127190244c92SMilanka Ringwald                             return;
127290244c92SMilanka Ringwald                         default:
127390244c92SMilanka Ringwald                             break;
127490244c92SMilanka Ringwald                     }
127556f1adacSMilanka Ringwald                     break;
127656f1adacSMilanka Ringwald                 default:
127756f1adacSMilanka Ringwald                     break;
127856f1adacSMilanka Ringwald             }
1279e2c3b58dSMilanka Ringwald 
1280fdda66c0SMilanka Ringwald             // handle error response for voice activation (HF initiated)
12810b4debbfSMilanka Ringwald             switch(hfp_connection->vra_state_requested){
1282de9e0ea7SMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1283de9e0ea7SMilanka Ringwald                     hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1284be55a11dSMilanka Ringwald                     break;
1285be55a11dSMilanka Ringwald                 default:
1286e2c3b58dSMilanka Ringwald                     if (hfp_connection->vra_state_requested == hfp_connection->vra_state){
1287e2c3b58dSMilanka Ringwald                         break;
1288e2c3b58dSMilanka Ringwald                     }
12890b4debbfSMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
1290553a4a56SMilanka Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
12910b4debbfSMilanka Ringwald                     hfp_reset_context_flags(hfp_connection);
12920b4debbfSMilanka Ringwald                     return;
1293be55a11dSMilanka Ringwald             }
1294e2c3b58dSMilanka Ringwald             event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1295e2c3b58dSMilanka Ringwald             if (!event_emited){
12960b4debbfSMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1297e2c3b58dSMilanka Ringwald             }
1298fdda66c0SMilanka Ringwald             hfp_reset_context_flags(hfp_connection);
1299ce263fc8SMatthias Ringwald             break;
1300fdda66c0SMilanka Ringwald 
1301ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1302e2c3b58dSMilanka Ringwald             hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS);
1303ce263fc8SMatthias Ringwald             break;
1304ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
13055a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1306ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1307ce263fc8SMatthias Ringwald             break;
1308ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
13095a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13104562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1311ce263fc8SMatthias Ringwald             break;
1312c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
13135a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1314184a03edSMilanka Ringwald             // report status after SLC established
1315184a03edSMilanka Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
1316184a03edSMilanka Ringwald                 break;
1317184a03edSMilanka Ringwald             }
1318c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1319a473a009SMatthias Ringwald                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1320c741b032SMilanka Ringwald             }
1321c741b032SMilanka Ringwald             break;
13221cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
13231cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13245a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
13251cc65c4fSMatthias Ringwald 			break;
1326eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1327eac56539SMilanka 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));
1328ce263fc8SMatthias Ringwald         default:
1329ce263fc8SMatthias Ringwald             break;
13303deb3ec6SMatthias Ringwald     }
13310cef86faSMatthias Ringwald }
1332426f9988SMatthias Ringwald 
133376cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
133476cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
133576cc1527SMatthias Ringwald }
133676cc1527SMatthias Ringwald 
13370b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1338426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1339426f9988SMatthias Ringwald     if (size < 1) return;
1340426f9988SMatthias Ringwald 
1341426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1342e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1343e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1344e43d1938SMatthias Ringwald #endif
1345426f9988SMatthias Ringwald 
1346426f9988SMatthias Ringwald     // process messages byte-wise
1347a7ba78b0SMilanka Ringwald     uint8_t pos;
1348426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1349426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
13501599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1351426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
13520b4debbfSMilanka Ringwald         hfp_hf_handle_rfcomm_command(hfp_connection);
13533deb3ec6SMatthias Ringwald     }
1354a7ba78b0SMilanka Ringwald }
13553deb3ec6SMatthias Ringwald 
13561c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1357665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1358665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1359665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1360a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
136122387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
13621c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
13633deb3ec6SMatthias Ringwald     }
13643deb3ec6SMatthias Ringwald }
13653deb3ec6SMatthias Ringwald 
13661c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
13670b4debbfSMilanka Ringwald     hfp_connection_t * hfp_connection;
13683deb3ec6SMatthias Ringwald     switch (packet_type){
13693deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
13700b4debbfSMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
13710b4debbfSMilanka Ringwald             if (!hfp_connection) return;
13720b4debbfSMilanka Ringwald             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
13730b4debbfSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
13740b4debbfSMilanka Ringwald             return;
13753deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1376d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1377d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
13781c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1379d4dd47ffSMatthias Ringwald                 return;
1380d4dd47ffSMatthias Ringwald             }
138127950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1382202c8a4cSMatthias Ringwald             break;
13833deb3ec6SMatthias Ringwald         default:
13843deb3ec6SMatthias Ringwald             break;
13853deb3ec6SMatthias Ringwald     }
13861c6a0fc0SMatthias Ringwald     hfp_hf_run();
13873deb3ec6SMatthias Ringwald }
13883deb3ec6SMatthias Ringwald 
13891c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1390405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
13911c6a0fc0SMatthias Ringwald     hfp_hf_run();
1392405014fbSMatthias Ringwald }
1393405014fbSMatthias Ringwald 
1394aeb0f0feSMatthias Ringwald static void hfp_hf_set_defaults(void){
1395aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1396aeb0f0feSMatthias Ringwald     hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
1397aeb0f0feSMatthias Ringwald     hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1398aeb0f0feSMatthias Ringwald     hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1399aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = 0;
1400aeb0f0feSMatthias Ringwald     hfp_hf_speaker_gain = 9;
1401aeb0f0feSMatthias Ringwald     hfp_hf_microphone_gain = 9;
1402aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = 0;
1403aeb0f0feSMatthias Ringwald }
1404aeb0f0feSMatthias Ringwald 
1405b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1406b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1407b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1408b4df8028SMilanka Ringwald         return status;
1409b4df8028SMilanka Ringwald     }
1410b4df8028SMilanka Ringwald 
1411520c92d5SMatthias Ringwald     hfp_init();
1412aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1413d63c37a1SMatthias Ringwald 
14141c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
14151c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
141627950165SMatthias Ringwald 
141727950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
14181c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1419b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
142020b2edb6SMatthias Ringwald }
142127950165SMatthias Ringwald 
142220b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
142320b2edb6SMatthias Ringwald     hfp_deinit();
1424aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1425aeb0f0feSMatthias Ringwald 
1426aeb0f0feSMatthias Ringwald     hfp_hf_callback = NULL;
142720b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
1428aeb0f0feSMatthias Ringwald     (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number));
1429a0ffb263SMatthias Ringwald }
1430a0ffb263SMatthias Ringwald 
14317ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
143268466199SMilanka Ringwald     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
14333deb3ec6SMatthias Ringwald 
1434aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = codecs_nr;
14353deb3ec6SMatthias Ringwald     int i;
14363deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
1437aeb0f0feSMatthias Ringwald         hfp_hf_codecs[i] = codecs[i];
14383deb3ec6SMatthias Ringwald     }
14393deb3ec6SMatthias Ringwald }
14403deb3ec6SMatthias Ringwald 
1441a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
1442aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = supported_features;
1443a0ffb263SMatthias Ringwald }
14443deb3ec6SMatthias Ringwald 
14457ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
1446aeb0f0feSMatthias Ringwald     btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS);
144768466199SMilanka Ringwald 
1448aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = indicators_nr;
14493deb3ec6SMatthias Ringwald     int i;
1450aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
1451aeb0f0feSMatthias Ringwald         hfp_hf_indicators[i] = indicators[i];
14523deb3ec6SMatthias Ringwald     }
14533deb3ec6SMatthias Ringwald }
14543deb3ec6SMatthias Ringwald 
14554eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
14564eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
14573deb3ec6SMatthias Ringwald }
14583deb3ec6SMatthias Ringwald 
1459657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
14609c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1461a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1462657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1463a33eb0c4SMilanka Ringwald     }
14641ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
14651c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1466657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
14673deb3ec6SMatthias Ringwald }
14683deb3ec6SMatthias Ringwald 
14693c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
14709c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1471a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14723c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14733deb3ec6SMatthias Ringwald     }
1474a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
14751c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14763c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14773deb3ec6SMatthias Ringwald }
14783deb3ec6SMatthias Ringwald 
14793c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
14803c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1481ce263fc8SMatthias Ringwald }
1482ce263fc8SMatthias Ringwald 
14833c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
14843c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1485ce263fc8SMatthias Ringwald }
1486ce263fc8SMatthias Ringwald 
14873deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
14883c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
14899c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1490a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14913c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14923deb3ec6SMatthias Ringwald     }
1493a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1494a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
14951c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14963c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14973deb3ec6SMatthias Ringwald }
14983deb3ec6SMatthias Ringwald 
14993c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
15009c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1501a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15023c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15033deb3ec6SMatthias Ringwald     }
15043c65e705SMilanka Ringwald 
1505a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1506ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1507a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1508ce263fc8SMatthias Ringwald             break;
1509ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1510a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1511ce263fc8SMatthias Ringwald             break;
1512ce263fc8SMatthias Ringwald         default:
15133c65e705SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1514ce263fc8SMatthias Ringwald     }
15151c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15163c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15173deb3ec6SMatthias Ringwald }
15183deb3ec6SMatthias Ringwald 
15193c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
15209c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1521a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15223c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15233deb3ec6SMatthias Ringwald     }
1524a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
15251c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15263c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15273deb3ec6SMatthias Ringwald }
15283deb3ec6SMatthias Ringwald 
1529ce263fc8SMatthias Ringwald 
15303c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
15313c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1532ce263fc8SMatthias Ringwald }
1533ce263fc8SMatthias Ringwald 
15343c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
15353c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1536ce263fc8SMatthias Ringwald }
1537ce263fc8SMatthias Ringwald 
153838200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
1539aeb0f0feSMatthias Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4));
154038200c1dSMilanka Ringwald }
1541ce263fc8SMatthias Ringwald 
15423c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
15439c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1544a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15453c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1546a33eb0c4SMilanka Ringwald     }
1547ce263fc8SMatthias Ringwald 
15483c65e705SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
15493c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
15503c65e705SMilanka Ringwald     }
15513c65e705SMilanka Ringwald 
15523c65e705SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
15533c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
15543c65e705SMilanka Ringwald     }
1555f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1556a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1557aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1558aa4dd815SMatthias Ringwald                 break;
1559ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1560ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1561ec3bfc1aSMatthias Ringwald                 break;
1562aa4dd815SMatthias Ringwald             default:
15631cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
15641cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
15651cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
15661cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
156738200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1568aa4dd815SMatthias Ringwald                 break;
15693deb3ec6SMatthias Ringwald         }
1570f4412093SMatthias Ringwald     } else {
1571f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1572f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1573f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1574f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1575f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1576f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1577f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1578f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1579ce263fc8SMatthias Ringwald     }
1580ce263fc8SMatthias Ringwald 
15811c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15823c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15833deb3ec6SMatthias Ringwald }
15843deb3ec6SMatthias Ringwald 
15853c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
15869c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1587a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15883c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1589a33eb0c4SMilanka Ringwald     }
15900b4debbfSMilanka Ringwald     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
15910b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
15920b4debbfSMilanka Ringwald     }
15930b4debbfSMilanka Ringwald     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
15940b4debbfSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
15951c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
15960b4debbfSMilanka Ringwald     }
15973c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15983deb3ec6SMatthias Ringwald }
15993deb3ec6SMatthias Ringwald 
16003c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
16019c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1602a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16033c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1604a33eb0c4SMilanka Ringwald     }
1605ce263fc8SMatthias Ringwald 
1606aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1607a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
16081c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1609ce263fc8SMatthias Ringwald     } else {
1610aeb0f0feSMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status);
16113c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1612ce263fc8SMatthias Ringwald     }
16133c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1614ce263fc8SMatthias Ringwald }
1615ce263fc8SMatthias Ringwald 
16163c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
16179c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1618a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16193c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1620a33eb0c4SMilanka Ringwald     }
1621a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
16221c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16233c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1624ce263fc8SMatthias Ringwald }
1625ce263fc8SMatthias Ringwald 
16263c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
16279c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1628a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16293c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1630a33eb0c4SMilanka Ringwald     }
1631ce263fc8SMatthias Ringwald 
1632aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1633a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
16341c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1635ce263fc8SMatthias Ringwald     }
16363c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1637ce263fc8SMatthias Ringwald }
1638ce263fc8SMatthias Ringwald 
16393c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){
16409c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1641a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16423c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1643a33eb0c4SMilanka Ringwald     }
1644ce263fc8SMatthias Ringwald 
1645aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1646a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
16471c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1648ce263fc8SMatthias Ringwald     }
16493c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1650ce263fc8SMatthias Ringwald }
1651ce263fc8SMatthias Ringwald 
16523c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(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 
1658aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1659aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1660a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
16611c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1662ce263fc8SMatthias Ringwald     }
16633c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1664ce263fc8SMatthias Ringwald }
1665ce263fc8SMatthias Ringwald 
16663c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
16679c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1668a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16693c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1670a33eb0c4SMilanka Ringwald     }
1671ce263fc8SMatthias Ringwald 
1672aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1673aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1674a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
16751c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1676ce263fc8SMatthias Ringwald     }
16773c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1678ce263fc8SMatthias Ringwald }
1679ce263fc8SMatthias Ringwald 
16803c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
16819c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1682a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16833c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1684a33eb0c4SMilanka Ringwald     }
1685ce263fc8SMatthias Ringwald 
1686aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1687aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1688a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
16891c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1690ce263fc8SMatthias Ringwald     }
16913c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1692ce263fc8SMatthias Ringwald }
1693ce263fc8SMatthias Ringwald 
16943c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
16959c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1696a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16973c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1698a33eb0c4SMilanka Ringwald     }
1699ce263fc8SMatthias Ringwald 
1700aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1701aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1702a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
17031c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1704ce263fc8SMatthias Ringwald     }
17053c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1706ce263fc8SMatthias Ringwald }
1707ce263fc8SMatthias Ringwald 
17083c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
17099c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1710a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17113c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1712a33eb0c4SMilanka Ringwald     }
1713667ec068SMatthias Ringwald 
1714aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1715aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1716a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1717a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
17181c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1719667ec068SMatthias Ringwald     }
17203c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1721667ec068SMatthias Ringwald }
1722667ec068SMatthias Ringwald 
17233c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
17249c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1725a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17263c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1727a33eb0c4SMilanka Ringwald     }
1728667ec068SMatthias Ringwald 
1729aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1730aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1731a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1732a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
17331c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1734667ec068SMatthias Ringwald     }
17353c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1736667ec068SMatthias Ringwald }
1737ce263fc8SMatthias Ringwald 
17383c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
17399c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1740a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17413c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1742a33eb0c4SMilanka Ringwald     }
1743ce263fc8SMatthias Ringwald 
1744a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1745aeb0f0feSMatthias Ringwald     snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number);
17461c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17473c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1748ce263fc8SMatthias Ringwald }
1749ce263fc8SMatthias Ringwald 
17503c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
17519c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1752a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17533c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1754a33eb0c4SMilanka Ringwald     }
1755ce263fc8SMatthias Ringwald 
1756a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1757a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1758a0ffb263SMatthias Ringwald 
17591c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17603c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1761ce263fc8SMatthias Ringwald }
1762ce263fc8SMatthias Ringwald 
17633c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
17649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1765a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17663c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1767a33eb0c4SMilanka Ringwald     }
1768ce263fc8SMatthias Ringwald 
1769a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
17701c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17713c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1772ce263fc8SMatthias Ringwald }
1773ce263fc8SMatthias Ringwald 
17743c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
17759c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1776a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17773c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1778a33eb0c4SMilanka Ringwald     }
1779ce263fc8SMatthias Ringwald 
1780a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
17811c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17823c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1783ce263fc8SMatthias Ringwald }
1784ce263fc8SMatthias Ringwald 
1785ce263fc8SMatthias Ringwald 
17863c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
17879c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1788a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17893c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1790a33eb0c4SMilanka Ringwald     }
1791ce263fc8SMatthias Ringwald 
1792a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
17931c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17943c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1795ce263fc8SMatthias Ringwald }
1796ce263fc8SMatthias Ringwald 
1797ce263fc8SMatthias Ringwald 
17983c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(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) {
18013c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1802a33eb0c4SMilanka Ringwald     }
1803ce263fc8SMatthias Ringwald 
1804a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
18051c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18063c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1807ce263fc8SMatthias Ringwald }
1808ce263fc8SMatthias Ringwald 
18093c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
18109c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1811a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18123c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1813a33eb0c4SMilanka Ringwald     }
1814ce263fc8SMatthias Ringwald 
1815a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
18161c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18173c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1818ce263fc8SMatthias Ringwald }
1819ce263fc8SMatthias Ringwald 
18206ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){
18216ba83b5eSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION);
1822aeb0f0feSMatthias Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION);
18236ba83b5eSMilanka Ringwald     return hf && ag;
1824ce263fc8SMatthias Ringwald }
1825ce263fc8SMatthias Ringwald 
18263c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
18279c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1828a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18293c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1830a33eb0c4SMilanka Ringwald     }
18316ba83b5eSMilanka Ringwald     if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){
18326ba83b5eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
18336ba83b5eSMilanka Ringwald     }
1834ce263fc8SMatthias Ringwald 
1835a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
18361c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18373c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1838ce263fc8SMatthias Ringwald }
1839ce263fc8SMatthias Ringwald 
1840acd11d4aSMilanka Ringwald static bool hfp_hf_vra_flag_supported(hfp_connection_t * hfp_connection){
1841acd11d4aSMilanka Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION);
1842acd11d4aSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION);
1843be55a11dSMilanka Ringwald     return hf && ag;
1844be55a11dSMilanka Ringwald }
1845be55a11dSMilanka Ringwald 
1846acd11d4aSMilanka Ringwald static bool hfp_hf_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){
1847acd11d4aSMilanka Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1848acd11d4aSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1849acd11d4aSMilanka Ringwald     return hf && ag;
1850acd11d4aSMilanka Ringwald }
1851acd11d4aSMilanka Ringwald 
1852acd11d4aSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1853fdda66c0SMilanka Ringwald      hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1854fdda66c0SMilanka Ringwald     if (!hfp_connection) {
1855fdda66c0SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1856be55a11dSMilanka Ringwald     }
1857013cc750SMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1858013cc750SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1859013cc750SMilanka Ringwald     }
1860acd11d4aSMilanka Ringwald 
1861acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1862acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1863acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
1864acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1865af97579eSMilanka Ringwald     }
1866af97579eSMilanka Ringwald 
1867498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1868be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1869de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1870498a8121SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1871fd4151d1SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
1872acd11d4aSMilanka Ringwald             hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported;
1873be55a11dSMilanka Ringwald             break;
1874de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1875de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = true;
1876de9e0ea7SMilanka Ringwald             break;
1877be55a11dSMilanka Ringwald         default:
1878be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1879be55a11dSMilanka Ringwald     }
1880ce263fc8SMatthias Ringwald 
1881af97579eSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1882fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1883af97579eSMilanka Ringwald }
1884af97579eSMilanka Ringwald 
1885acd11d4aSMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
1886af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1887af97579eSMilanka Ringwald     if (!hfp_connection) {
1888af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1889af97579eSMilanka Ringwald     }
1890acd11d4aSMilanka Ringwald     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
189108a0b01cSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
189208a0b01cSMilanka Ringwald     }
1893acd11d4aSMilanka Ringwald 
1894acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1895acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported){
1896acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1897acd11d4aSMilanka Ringwald     }
1898acd11d4aSMilanka Ringwald 
1899acd11d4aSMilanka Ringwald     switch (hfp_connection->vra_state){
1900acd11d4aSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1901acd11d4aSMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1902acd11d4aSMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1903acd11d4aSMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
1904acd11d4aSMilanka Ringwald             break;
1905acd11d4aSMilanka Ringwald         default:
1906fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1907af97579eSMilanka Ringwald     }
1908013cc750SMilanka Ringwald 
1909acd11d4aSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1910acd11d4aSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1911acd11d4aSMilanka Ringwald }
1912acd11d4aSMilanka Ringwald 
1913acd11d4aSMilanka Ringwald 
1914acd11d4aSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1915acd11d4aSMilanka Ringwald     // return deactivate_voice_recognition(acl_handle, false);
1916acd11d4aSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1917acd11d4aSMilanka Ringwald     if (!hfp_connection) {
1918acd11d4aSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1919acd11d4aSMilanka Ringwald     }
1920acd11d4aSMilanka Ringwald 
1921acd11d4aSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
1922acd11d4aSMilanka Ringwald         hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1923acd11d4aSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1924acd11d4aSMilanka Ringwald     }
1925acd11d4aSMilanka Ringwald 
1926acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1927acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1928acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
1929acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1930acd11d4aSMilanka Ringwald     }
1931acd11d4aSMilanka Ringwald 
1932fdda66c0SMilanka Ringwald     switch (hfp_connection->vra_state){
1933de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
1934fdda66c0SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1935de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1936de9e0ea7SMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1937fdda66c0SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1938fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1939fdda66c0SMilanka Ringwald             break;
1940de9e0ea7SMilanka Ringwald 
1941de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1942de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1943de9e0ea7SMilanka Ringwald             hfp_connection->deactivate_voice_recognition = true;
1944de9e0ea7SMilanka Ringwald             break;
1945de9e0ea7SMilanka Ringwald 
1946de9e0ea7SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1947de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1948de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1949fdda66c0SMilanka Ringwald         default:
1950fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1951fdda66c0SMilanka Ringwald     }
1952fdda66c0SMilanka Ringwald 
1953fdda66c0SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1954fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1955af97579eSMilanka Ringwald }
1956af97579eSMilanka Ringwald 
19573c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
19589c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1959a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19603c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1961a33eb0c4SMilanka Ringwald     }
1962c8626498SMilanka Ringwald 
19633c65e705SMilanka Ringwald     if (hfp_connection->microphone_gain == gain) {
19643c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
19653c65e705SMilanka Ringwald     }
19663c65e705SMilanka Ringwald 
1967c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1968a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
19693c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1970a0ffb263SMatthias Ringwald     }
19713c65e705SMilanka Ringwald 
1972a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1973a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
19741c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19753c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1976ce263fc8SMatthias Ringwald }
1977ce263fc8SMatthias Ringwald 
19783c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
19799c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1980a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19813c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1982a33eb0c4SMilanka Ringwald     }
1983c8626498SMilanka Ringwald 
19843c65e705SMilanka Ringwald     if (hfp_connection->speaker_gain == gain){
19853c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
19863c65e705SMilanka Ringwald     }
19873c65e705SMilanka Ringwald 
1988c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1989a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
19903c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1991a0ffb263SMatthias Ringwald     }
19923c65e705SMilanka Ringwald 
1993a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1994a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
19951c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19963c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1997ce263fc8SMatthias Ringwald }
1998ce263fc8SMatthias Ringwald 
19993c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
20009c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2001a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20023c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2003a33eb0c4SMilanka Ringwald     }
2004a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
20051c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20063c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2007ce263fc8SMatthias Ringwald }
2008ce263fc8SMatthias Ringwald 
20093c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
20109c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2011a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20123c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2013a33eb0c4SMilanka Ringwald     }
2014a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
20151c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20163c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2017ce263fc8SMatthias Ringwald }
20183deb3ec6SMatthias Ringwald 
20193c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
20209c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2021a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20223c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2023a33eb0c4SMilanka Ringwald     }
2024a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
20251c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20263c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2027667ec068SMatthias Ringwald }
2028667ec068SMatthias Ringwald 
2029667ec068SMatthias Ringwald 
20303c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
20319c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2032a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20333c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2034a33eb0c4SMilanka Ringwald     }
2035a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2036a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
20371c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20383c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2039667ec068SMatthias Ringwald }
2040667ec068SMatthias Ringwald 
20413c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
20429c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2043a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20443c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2045a33eb0c4SMilanka Ringwald     }
2046a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2047a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
20481c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20493c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2050667ec068SMatthias Ringwald }
2051667ec068SMatthias Ringwald 
20523c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
20539c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2054a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20553c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2056a33eb0c4SMilanka Ringwald     }
2057a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2058a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
20591c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20603c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2061667ec068SMatthias Ringwald }
2062667ec068SMatthias Ringwald 
20633c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
20649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2065a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20663c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2067a33eb0c4SMilanka Ringwald     }
2068a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2069a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
20701c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20713c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2072667ec068SMatthias Ringwald }
2073667ec068SMatthias Ringwald 
20743c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
20759c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2076a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20773c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2078a33eb0c4SMilanka Ringwald     }
2079a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
20801c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20813c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2082667ec068SMatthias Ringwald }
2083667ec068SMatthias Ringwald 
20843c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
20859c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2086a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20873c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2088a33eb0c4SMilanka Ringwald     }
2089667ec068SMatthias Ringwald     // find index for assigned number
2090667ec068SMatthias Ringwald     int i;
2091aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
2092aeb0f0feSMatthias Ringwald         if (hfp_hf_indicators[i] == assigned_number){
2093667ec068SMatthias Ringwald             // set value
2094aeb0f0feSMatthias Ringwald             hfp_hf_indicators_value[i] = value;
2095667ec068SMatthias Ringwald             // mark for update
2096a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
2097a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
2098667ec068SMatthias Ringwald                 // send update
20991c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
2100a0ffb263SMatthias Ringwald             }
21013c65e705SMilanka Ringwald             return ERROR_CODE_SUCCESS;
2102667ec068SMatthias Ringwald         }
2103667ec068SMatthias Ringwald     }
21043c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2105667ec068SMatthias Ringwald }
2106667ec068SMatthias Ringwald 
2107d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
2108d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2109d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
2110d7f6b5cbSMatthias Ringwald         return 0;
2111d7f6b5cbSMatthias Ringwald     }
2112d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
2113d7f6b5cbSMatthias Ringwald }
211476cc1527SMatthias Ringwald 
211576cc1527SMatthias 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){
211676cc1527SMatthias Ringwald 	if (!name){
2117aeb0f0feSMatthias Ringwald 		name = hfp_hf_default_service_name;
211876cc1527SMatthias Ringwald 	}
211976cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
212076cc1527SMatthias Ringwald 
212176cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
212276cc1527SMatthias Ringwald 	//
212376cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
212476cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
212576cc1527SMatthias Ringwald 	//
212676cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
212776cc1527SMatthias Ringwald 	//
212876cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
2129ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
213076cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
213176cc1527SMatthias Ringwald 	}
2132ef3ae4ebSMilanka Ringwald 
2133ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
213456f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
2135ef3ae4ebSMilanka Ringwald     }
2136ef3ae4ebSMilanka Ringwald 
2137ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
213856f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
2139ef3ae4ebSMilanka Ringwald     }
2140ef3ae4ebSMilanka Ringwald 
214176cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
214276cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
214376cc1527SMatthias Ringwald }
214476cc1527SMatthias Ringwald 
214576cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
214668466199SMilanka Ringwald 	btstack_assert(callback != NULL);
214768466199SMilanka Ringwald 
214876cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
214976cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
215076cc1527SMatthias Ringwald }
2151