xref: /btstack/src/classic/hfp_hf.c (revision ce3797e1bd4ddc17a76309d2aeb2deb2d7474952)
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 
115fcf4ede6SMilanka Ringwald static bool hfp_hf_vra_flag_supported(hfp_connection_t * hfp_connection){
116fcf4ede6SMilanka Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION);
117fcf4ede6SMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION);
118fcf4ede6SMilanka Ringwald     return hf && ag;
119fcf4ede6SMilanka Ringwald }
120fcf4ede6SMilanka Ringwald 
121fcf4ede6SMilanka Ringwald static bool hfp_hf_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){
122fcf4ede6SMilanka Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS);
123fcf4ede6SMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS);
124fcf4ede6SMilanka Ringwald     return hf && ag;
125fcf4ede6SMilanka Ringwald }
12676cc1527SMatthias Ringwald 
1279c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
1289c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1299c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1309c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1319c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1329c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1339c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1349c9c64c1SMatthias Ringwald         return hfp_connection;
1359c9c64c1SMatthias Ringwald     }
1369c9c64c1SMatthias Ringwald     return NULL;
1379c9c64c1SMatthias Ringwald }
1389c9c64c1SMatthias Ringwald 
13976cc1527SMatthias Ringwald /* emit functinos */
1403deb3ec6SMatthias Ringwald 
141a473a009SMatthias Ringwald static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){
142a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
143d703d377SMatthias Ringwald     uint8_t event[33];
144a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
145a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
146a473a009SMatthias Ringwald     event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION;
147d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
148d703d377SMatthias Ringwald     event[5] = status;
149d703d377SMatthias Ringwald     event[6] = hfp_connection->bnip_type;
150d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 8);
151d703d377SMatthias Ringwald     strncpy((char*)&event[7], hfp_connection->bnip_number, size);
152d703d377SMatthias Ringwald     event[7 + size] = 0;
153a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
154a0ffb263SMatthias Ringwald }
155a0ffb263SMatthias Ringwald 
156a473a009SMatthias Ringwald static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){
157a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
158d703d377SMatthias Ringwald     uint8_t event[32];
159a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
160a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
161a0ffb263SMatthias Ringwald     event[2] = event_subtype;
162d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
163d703d377SMatthias Ringwald     event[5] = hfp_connection->bnip_type;
164d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 7);
165d703d377SMatthias Ringwald     strncpy((char*)&event[6], hfp_connection->bnip_number, size);
166d703d377SMatthias Ringwald     event[6 + size] = 0;
167a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
168a0ffb263SMatthias Ringwald }
169a0ffb263SMatthias Ringwald 
170a473a009SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){
171a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
172d703d377SMatthias Ringwald     uint8_t event[38];
1730aee97efSMilanka Ringwald     int pos = 0;
1740aee97efSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
1750aee97efSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
1760aee97efSMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
177d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
178d703d377SMatthias Ringwald     pos += 2;
179a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_idx;
180a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_dir;
181a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_status;
182a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mode;
183a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mpty;
184a473a009SMatthias Ringwald     event[pos++] = hfp_connection->bnip_type;
185d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - pos - 1);
186a473a009SMatthias Ringwald     strncpy((char*)&event[pos], hfp_connection->bnip_number, size);
1870aee97efSMilanka Ringwald     pos += size;
1880aee97efSMilanka Ringwald     event[pos++] = 0;
189a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
190a0ffb263SMatthias Ringwald }
191a0ffb263SMatthias Ringwald 
19276cc1527SMatthias Ringwald 
193*ce3797e1SMilanka Ringwald static void hfp_emit_ag_indicator_status_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
194a473a009SMatthias Ringwald 	if (hfp_hf_callback == NULL) return;
195d703d377SMatthias Ringwald 	uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE+1];
19676cc1527SMatthias Ringwald 	int pos = 0;
19776cc1527SMatthias Ringwald 	event[pos++] = HCI_EVENT_HFP_META;
19876cc1527SMatthias Ringwald 	event[pos++] = sizeof(event) - 2;
19976cc1527SMatthias Ringwald 	event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
200d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
201d703d377SMatthias Ringwald     pos += 2;
202a473a009SMatthias Ringwald 	event[pos++] = indicator->index;
203a473a009SMatthias Ringwald 	event[pos++] = indicator->status;
204a473a009SMatthias Ringwald 	event[pos++] = indicator->min_range;
205a473a009SMatthias Ringwald 	event[pos++] = indicator->max_range;
206a473a009SMatthias Ringwald 	event[pos++] = indicator->mandatory;
207a473a009SMatthias Ringwald 	event[pos++] = indicator->enabled;
208a473a009SMatthias Ringwald 	event[pos++] = indicator->status_changed;
209a473a009SMatthias Ringwald 	strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
21076cc1527SMatthias Ringwald 	pos += HFP_MAX_INDICATOR_DESC_SIZE;
21176cc1527SMatthias Ringwald 	event[pos] = 0;
212a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2133deb3ec6SMatthias Ringwald }
2143deb3ec6SMatthias Ringwald 
215a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){
216a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
217d703d377SMatthias Ringwald 	uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
21876cc1527SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
21976cc1527SMatthias Ringwald 	event[1] = sizeof(event) - 2;
22076cc1527SMatthias Ringwald 	event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
221d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
222d703d377SMatthias Ringwald 	event[5] = hfp_connection->network_operator.mode;
223d703d377SMatthias Ringwald 	event[6] = hfp_connection->network_operator.format;
224d703d377SMatthias Ringwald 	strncpy((char*)&event[7], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
225d703d377SMatthias Ringwald 	event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
226a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2273deb3ec6SMatthias Ringwald }
2283deb3ec6SMatthias Ringwald 
229b95cac54SMilanka Ringwald 
230b95cac54SMilanka Ringwald static void hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection){
231b95cac54SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
23251aa5d5aSMilanka Ringwald     uint8_t event[HFP_MAX_VR_TEXT_SIZE + 11];
233b95cac54SMilanka Ringwald     int pos = 0;
234b95cac54SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
235b95cac54SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
236b95cac54SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE;
237b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
238b95cac54SMilanka Ringwald     pos += 2;
239b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->ag_msg.text_id);
240b95cac54SMilanka Ringwald     pos += 2;
241b95cac54SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_type;
242e83f1be7SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_operation;
243b95cac54SMilanka Ringwald 
24451aa5d5aSMilanka Ringwald     // length, zero ending is already in message
245b95cac54SMilanka Ringwald     uint8_t * value = &hfp_connection->line_buffer[0];
24651aa5d5aSMilanka Ringwald     uint16_t  value_length = hfp_connection->ag_vra_msg_length;
24751aa5d5aSMilanka Ringwald 
24851aa5d5aSMilanka Ringwald     little_endian_store_16(event, pos, value_length);
249b95cac54SMilanka Ringwald     pos += 2;
25051aa5d5aSMilanka Ringwald     memcpy(&event[pos], value, value_length);
25151aa5d5aSMilanka Ringwald     pos += value_length;
25251aa5d5aSMilanka Ringwald 
253b95cac54SMilanka Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
254b95cac54SMilanka Ringwald }
255b95cac54SMilanka Ringwald 
25676cc1527SMatthias Ringwald /* send commands */
25789425bfcSMilanka Ringwald 
25889425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2593deb3ec6SMatthias Ringwald     char buffer[20];
2601599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
26189425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
26289425bfcSMilanka Ringwald }
26389425bfcSMilanka Ringwald 
26489425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
26589425bfcSMilanka Ringwald     char buffer[20];
2661599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
26789425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
26889425bfcSMilanka Ringwald }
26989425bfcSMilanka Ringwald 
27086da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
27189425bfcSMilanka Ringwald     char buffer[40];
2721599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
2733deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2743deb3ec6SMatthias Ringwald }
2753deb3ec6SMatthias Ringwald 
2763deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2773deb3ec6SMatthias Ringwald     char buffer[30];
27889425bfcSMilanka Ringwald     const int size = sizeof(buffer);
27989425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
280aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_codecs, hfp_hf_codecs_nr);
2811599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2823deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2833deb3ec6SMatthias Ringwald }
2843deb3ec6SMatthias Ringwald 
2853deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2863deb3ec6SMatthias Ringwald     char buffer[50];
28789425bfcSMilanka Ringwald     const int size = sizeof(buffer);
28889425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
28989425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
2901599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2913deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2923deb3ec6SMatthias Ringwald }
2933deb3ec6SMatthias Ringwald 
2943deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2953deb3ec6SMatthias Ringwald     char buffer[30];
29689425bfcSMilanka Ringwald     const int size = sizeof(buffer);
29789425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
298aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_indicators, hfp_hf_indicators_nr);
2991599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
3003deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3013deb3ec6SMatthias Ringwald }
3023deb3ec6SMatthias Ringwald 
30389425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
3043deb3ec6SMatthias Ringwald     char buffer[20];
3051599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
306ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
307ce263fc8SMatthias Ringwald }
308ce263fc8SMatthias Ringwald 
309ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
310ce263fc8SMatthias Ringwald     char buffer[40];
311aeb0f0feSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, hfp_hf_phone_number);
312ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
313ce263fc8SMatthias Ringwald }
314ce263fc8SMatthias Ringwald 
315a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
316ce263fc8SMatthias Ringwald     char buffer[40];
3171599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
318ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
319ce263fc8SMatthias Ringwald }
320ce263fc8SMatthias Ringwald 
321f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
32289425bfcSMilanka Ringwald     char buffer[40];
3231599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
324ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
325ce263fc8SMatthias Ringwald }
326ce263fc8SMatthias Ringwald 
327ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
328ce263fc8SMatthias Ringwald     char buffer[20];
3291599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
330ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
331ce263fc8SMatthias Ringwald }
332ce263fc8SMatthias Ringwald 
33397d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
3341599fe57SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "ATA\r");
33597d2cadbSMatthias Ringwald }
33697d2cadbSMatthias Ringwald 
33789425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
338aeb0f0feSMatthias Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_hf_supported_features);
33989425bfcSMilanka Ringwald }
34089425bfcSMilanka Ringwald 
34189425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
34289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
34389425bfcSMilanka Ringwald }
34489425bfcSMilanka Ringwald 
34589425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
34689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
34789425bfcSMilanka Ringwald }
34889425bfcSMilanka Ringwald 
34989425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
35089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
35189425bfcSMilanka Ringwald }
35289425bfcSMilanka Ringwald 
35389425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
35489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
35589425bfcSMilanka Ringwald }
35689425bfcSMilanka Ringwald 
35789425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
35889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
35989425bfcSMilanka Ringwald }
36089425bfcSMilanka Ringwald 
36189425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
36289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
36389425bfcSMilanka Ringwald }
36489425bfcSMilanka Ringwald 
36589425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
36689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
36789425bfcSMilanka Ringwald }
36889425bfcSMilanka Ringwald 
36989425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
37089425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
37189425bfcSMilanka Ringwald }
37289425bfcSMilanka Ringwald 
37389425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
37489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
37589425bfcSMilanka Ringwald }
37689425bfcSMilanka Ringwald 
37789425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
37889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
37989425bfcSMilanka Ringwald }
38089425bfcSMilanka Ringwald 
38189425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
38289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
38389425bfcSMilanka Ringwald }
38489425bfcSMilanka Ringwald 
38589425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
38689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
38789425bfcSMilanka Ringwald }
38889425bfcSMilanka Ringwald 
38989425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
39089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
39189425bfcSMilanka Ringwald }
39289425bfcSMilanka Ringwald 
39389425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
39489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
39589425bfcSMilanka Ringwald }
39689425bfcSMilanka Ringwald 
39789425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
39889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
39989425bfcSMilanka Ringwald }
40089425bfcSMilanka Ringwald 
40189425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
40289425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
40389425bfcSMilanka Ringwald }
40489425bfcSMilanka Ringwald 
40589425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
40689425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
40789425bfcSMilanka Ringwald }
40889425bfcSMilanka Ringwald 
409ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
41089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
411ce263fc8SMatthias Ringwald }
412ce263fc8SMatthias Ringwald 
413667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
41489425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
415667ec068SMatthias Ringwald }
416667ec068SMatthias Ringwald 
41776cc1527SMatthias Ringwald /* state machines */
4183deb3ec6SMatthias Ringwald 
419a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
420a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
421a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
422aa4dd815SMatthias Ringwald     int done = 1;
423498a8121SMilanka Ringwald     log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state);
424a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4253deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
426aeb0f0feSMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_hf_codecs, &hfp_hf_codecs_nr);
427a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
428a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4293deb3ec6SMatthias Ringwald             break;
4303deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
431a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
432a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4333deb3ec6SMatthias Ringwald             break;
4343deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
435a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
436a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4373deb3ec6SMatthias Ringwald             break;
4383deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
439a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
440a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4413deb3ec6SMatthias Ringwald             break;
4423deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
443a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
444a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4453deb3ec6SMatthias Ringwald             break;
4463deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
447a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
448a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4493deb3ec6SMatthias Ringwald             break;
4503deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
451a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
452a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4533deb3ec6SMatthias Ringwald             break;
4543deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
455a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
456a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4573deb3ec6SMatthias Ringwald             break;
4583deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
459a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
460a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4613deb3ec6SMatthias Ringwald             break;
4623deb3ec6SMatthias Ringwald         default:
463aa4dd815SMatthias Ringwald             done = 0;
4643deb3ec6SMatthias Ringwald             break;
4653deb3ec6SMatthias Ringwald     }
4663deb3ec6SMatthias Ringwald     return done;
4673deb3ec6SMatthias Ringwald }
4683deb3ec6SMatthias Ringwald 
469ce263fc8SMatthias Ringwald 
470a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
471a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
472498a8121SMilanka Ringwald     if (hfp_connection->ok_pending){
473498a8121SMilanka Ringwald         return 0;
474498a8121SMilanka Ringwald     }
475ce263fc8SMatthias Ringwald     int done = 0;
476a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
477a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
478ce263fc8SMatthias Ringwald         done = 1;
479a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
480ce263fc8SMatthias Ringwald         return done;
481ce263fc8SMatthias Ringwald     };
482a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
483a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
484ce263fc8SMatthias Ringwald         done = 1;
485a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
486a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
487a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
488ce263fc8SMatthias Ringwald         return done;
489ce263fc8SMatthias Ringwald     }
490ce263fc8SMatthias Ringwald 
491a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
492ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
493a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
494a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
495a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
496ce263fc8SMatthias Ringwald             return 1;
497ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
498a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
499a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
500a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
501ce263fc8SMatthias Ringwald             return 1;
502ce263fc8SMatthias Ringwald         default:
503ce263fc8SMatthias Ringwald             break;
504ce263fc8SMatthias Ringwald     }
505ce263fc8SMatthias Ringwald 
506a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
507a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
508ce263fc8SMatthias Ringwald         done = 1;
509a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
510ce263fc8SMatthias Ringwald         return done;
511ce263fc8SMatthias Ringwald     }
512ce263fc8SMatthias Ringwald 
513ce263fc8SMatthias Ringwald     return done;
514ce263fc8SMatthias Ringwald }
515ce263fc8SMatthias Ringwald 
516af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
517be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
518be55a11dSMilanka Ringwald         return 0;
519be55a11dSMilanka Ringwald     }
520be55a11dSMilanka Ringwald     int done = 0;
521fd4151d1SMilanka Ringwald 
5220b4debbfSMilanka Ringwald     if (hfp_connection->ok_pending == 1){
5230b4debbfSMilanka Ringwald         return 0;
5240b4debbfSMilanka Ringwald     }
5250b4debbfSMilanka Ringwald     // voice recognition activated from AG
5260b4debbfSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){
5270b4debbfSMilanka Ringwald         switch(hfp_connection->vra_state_requested){
5280b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
5290b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
530de9e0ea7SMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
5310b4debbfSMilanka Ringwald                 // ignore AG command, continue to wait for OK
5320b4debbfSMilanka Ringwald                 return 0;
533cf75be85SMilanka Ringwald 
5340b4debbfSMilanka Ringwald             default:
535b95cac54SMilanka Ringwald                 if (hfp_connection->ag_vra_msg_length > 0){
536b95cac54SMilanka Ringwald                     hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection);
537b95cac54SMilanka Ringwald                     hfp_connection->ag_vra_msg_length = 0;
538b95cac54SMilanka Ringwald                     break;
539b95cac54SMilanka Ringwald                 }
540cf75be85SMilanka Ringwald                 switch(hfp_connection->ag_vra_state){
541cf75be85SMilanka Ringwald                     case HFP_VOICE_RECOGNITION_STATE_AG_READY:
542013cc750SMilanka Ringwald                         switch (hfp_connection->ag_vra_status){
543013cc750SMilanka Ringwald                             case 0:
5440b4debbfSMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
545013cc750SMilanka Ringwald                                 break;
546013cc750SMilanka Ringwald                             case 1:
547013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
548013cc750SMilanka Ringwald                                 break;
549013cc750SMilanka Ringwald                             case 2:
550013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
551013cc750SMilanka Ringwald                                 break;
552013cc750SMilanka Ringwald                             default:
553013cc750SMilanka Ringwald                                 break;
5540b4debbfSMilanka Ringwald                         }
5550b4debbfSMilanka Ringwald                         break;
556cf75be85SMilanka Ringwald                     default:
557cf75be85SMilanka Ringwald                         // state messages from AG
558cf75be85SMilanka Ringwald                         hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
5598d5005b5SMilanka Ringwald                         hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
560cf75be85SMilanka Ringwald                         break;
561cf75be85SMilanka Ringwald                 }
562cf75be85SMilanka Ringwald                 break;
5630b4debbfSMilanka Ringwald         }
5640b4debbfSMilanka Ringwald         hfp_connection->command = HFP_CMD_NONE;
5650b4debbfSMilanka Ringwald     }
5660b4debbfSMilanka Ringwald 
5670b4debbfSMilanka Ringwald 
568498a8121SMilanka Ringwald     switch (hfp_connection->vra_state_requested){
569fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
570fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
571fdda66c0SMilanka Ringwald             if (done != 0){
572fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
573498a8121SMilanka Ringwald                 hfp_connection->ok_pending = 1;
574498a8121SMilanka Ringwald             }
575fd4151d1SMilanka Ringwald             return 1;
576fd4151d1SMilanka Ringwald 
577fd4151d1SMilanka Ringwald 
578fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
579fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
580fdda66c0SMilanka Ringwald             if (done != 0){
581fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
582fd4151d1SMilanka Ringwald                 hfp_connection->ok_pending = 1;
583fd4151d1SMilanka Ringwald                 return 1;
5840b4debbfSMilanka Ringwald             }
5850b4debbfSMilanka Ringwald             break;
586013cc750SMilanka Ringwald 
587de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
588de9e0ea7SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
589de9e0ea7SMilanka Ringwald             if (done != 0){
590de9e0ea7SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
591de9e0ea7SMilanka Ringwald                 hfp_connection->ok_pending = 1;
592de9e0ea7SMilanka Ringwald                 return 1;
593de9e0ea7SMilanka Ringwald             }
594de9e0ea7SMilanka Ringwald             break;
595de9e0ea7SMilanka Ringwald 
596de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
597de9e0ea7SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
598de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
599de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
600de9e0ea7SMilanka Ringwald             if (hfp_connection->activate_voice_recognition){
601fcf4ede6SMilanka Ringwald                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
602de9e0ea7SMilanka Ringwald                 hfp_hf_activate_voice_recognition(hfp_connection->acl_handle);
603de9e0ea7SMilanka Ringwald             } else {
604553a4a56SMilanka Ringwald                 hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
605de9e0ea7SMilanka Ringwald             }
606de9e0ea7SMilanka Ringwald             break;
607de9e0ea7SMilanka Ringwald 
608be55a11dSMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
609498a8121SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
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 {
615fcf4ede6SMilanka Ringwald                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
616553a4a56SMilanka Ringwald                 hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
617de9e0ea7SMilanka Ringwald             }
618be55a11dSMilanka Ringwald             break;
619be55a11dSMilanka Ringwald 
620de9e0ea7SMilanka Ringwald 
621013cc750SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
622013cc750SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
623498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
624de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
625de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
626de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
627de9e0ea7SMilanka Ringwald             } else {
628de9e0ea7SMilanka Ringwald                 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
629de9e0ea7SMilanka Ringwald             }
630be55a11dSMilanka Ringwald             break;
631fd4151d1SMilanka Ringwald 
632be55a11dSMilanka Ringwald         default:
633be55a11dSMilanka Ringwald             break;
634be55a11dSMilanka Ringwald     }
635be55a11dSMilanka Ringwald     return done;
636be55a11dSMilanka Ringwald }
637be55a11dSMilanka Ringwald 
638be55a11dSMilanka Ringwald 
639be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
640a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
641ce263fc8SMatthias Ringwald 
642332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
643332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
644ce263fc8SMatthias Ringwald 
645a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
646a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
647332ca98fSMatthias Ringwald 		return 1;
648332ca98fSMatthias Ringwald     }
649332ca98fSMatthias Ringwald 
6501cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
6511cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
652ce263fc8SMatthias Ringwald 
653a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
654fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
6551cc65c4fSMatthias Ringwald 		return 1;
6561cc65c4fSMatthias Ringwald     }
6571cc65c4fSMatthias Ringwald 
6581cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
6591cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
6601cc65c4fSMatthias Ringwald 
661a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
662a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
6631cc65c4fSMatthias Ringwald 		return 1;
6641cc65c4fSMatthias Ringwald     }
665ce263fc8SMatthias Ringwald 
666ce263fc8SMatthias Ringwald     return 0;
667ce263fc8SMatthias Ringwald }
668ce263fc8SMatthias Ringwald 
669a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
670505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
671505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
672ce263fc8SMatthias Ringwald 
67364f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
674a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
675a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
676a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
677ce263fc8SMatthias Ringwald         return 1;
678ce263fc8SMatthias Ringwald     }
679ce263fc8SMatthias Ringwald 
680a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
681ce263fc8SMatthias Ringwald 
682ce263fc8SMatthias Ringwald     // run codecs exchange
683a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
684ce263fc8SMatthias Ringwald     if (done) return 1;
685ce263fc8SMatthias Ringwald 
68638200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
68738200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
68838200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
68938200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
69038200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
69138200c1dSMilanka Ringwald         return 1;
69238200c1dSMilanka Ringwald     }
693ce263fc8SMatthias Ringwald     return 0;
694ce263fc8SMatthias Ringwald }
695ce263fc8SMatthias Ringwald 
69638200c1dSMilanka Ringwald 
697a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
698eaf2b0a1SMatthias Ringwald 
699eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
700eaf2b0a1SMatthias Ringwald 
701a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
702a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
703a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
704ce263fc8SMatthias Ringwald         return 1;
705ce263fc8SMatthias Ringwald     }
706ce263fc8SMatthias Ringwald     return 0;
707ce263fc8SMatthias Ringwald }
708ce263fc8SMatthias Ringwald 
7091c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
7107522e673SMatthias Ringwald 
71176cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
71276cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
71376cc1527SMatthias Ringwald 
71476cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
71576cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
71622387625SMatthias Ringwald 
7173721a235SMatthias Ringwald 	// assert command could be sent
7183721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
7193721a235SMatthias Ringwald 
7203721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
7213721a235SMatthias Ringwald     // WBS Disassociate
7223721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
7233721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
7243721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
7253721a235SMatthias Ringwald         return;
7263721a235SMatthias Ringwald     }
7273721a235SMatthias Ringwald     // Write Codec Config
7283721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
7293721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
7303721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
7313721a235SMatthias Ringwald         return;
7323721a235SMatthias Ringwald     }
7333721a235SMatthias Ringwald     // WBS Associate
7343721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
7353721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
7363721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
7373721a235SMatthias Ringwald         return;
7383721a235SMatthias Ringwald     }
7393721a235SMatthias Ringwald #endif
740689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
741689d4323SMatthias Ringwald     // Enable WBS
742689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
743689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
744689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
745689d4323SMatthias Ringwald         return;
746689d4323SMatthias Ringwald     }
747689d4323SMatthias Ringwald     // Write I2S/PCM params
748689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
749689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
750689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
751689d4323SMatthias Ringwald         return;
752689d4323SMatthias Ringwald     }
753689d4323SMatthias Ringwald     // Disable WBS
754689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
755689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
756689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
757689d4323SMatthias Ringwald         return;
758689d4323SMatthias Ringwald     }
759689d4323SMatthias Ringwald #endif
76048e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
76148e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
76248e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
76348e6eeeeSMatthias Ringwald         return;
76448e6eeeeSMatthias Ringwald     }
76548e6eeeeSMatthias Ringwald #endif
7663721a235SMatthias Ringwald 
767cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
768cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
769cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
7707522e673SMatthias Ringwald         // notify about codec selection if not done already
7717522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
7727522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
7737522e673SMatthias Ringwald         }
774cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
7757522e673SMatthias Ringwald         return;
7767522e673SMatthias Ringwald     }
7777522e673SMatthias Ringwald 
778d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
779d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
780d4dd47ffSMatthias Ringwald         return;
781d4dd47ffSMatthias Ringwald     }
782a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
783ce263fc8SMatthias Ringwald     if (!done){
784a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
785ce263fc8SMatthias Ringwald     }
786ce263fc8SMatthias Ringwald     if (!done){
787c95b5b3cSMilanka Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
788be55a11dSMilanka Ringwald     }
789be55a11dSMilanka Ringwald     if (!done){
790c95b5b3cSMilanka Ringwald         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
791ce263fc8SMatthias Ringwald     }
792ce263fc8SMatthias Ringwald     if (!done){
793a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
794ce263fc8SMatthias Ringwald     }
795ce263fc8SMatthias Ringwald 
796e2c3b58dSMilanka Ringwald     // don't send a new command while ok still pending or SLC is not established
797e2c3b58dSMilanka Ringwald     if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){
798e2c3b58dSMilanka Ringwald         return;
799e2c3b58dSMilanka Ringwald     }
8001016a228SMatthias Ringwald 
801a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
802a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
803a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
804a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
805ce263fc8SMatthias Ringwald         return;
806ce263fc8SMatthias Ringwald     }
807ce263fc8SMatthias Ringwald 
808a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
809a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
810a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
811a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
812ce263fc8SMatthias Ringwald         return;
813ce263fc8SMatthias Ringwald     }
814ce263fc8SMatthias Ringwald 
815a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
816a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
817a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
818a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
819ce263fc8SMatthias Ringwald         return;
820ce263fc8SMatthias Ringwald     }
821ce263fc8SMatthias Ringwald 
822a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
823a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
824a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
825a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
826ce263fc8SMatthias Ringwald         return;
827ce263fc8SMatthias Ringwald     }
828ce263fc8SMatthias Ringwald 
829a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
830a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
83199af1e28SMilanka Ringwald         hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR;
832a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
833e2c3b58dSMilanka Ringwald         hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0);
834ce263fc8SMatthias Ringwald         return;
835ce263fc8SMatthias Ringwald     }
836ce263fc8SMatthias Ringwald 
837a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
838a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
839a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
840a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
841ce263fc8SMatthias Ringwald         return;
842ce263fc8SMatthias Ringwald     }
843ce263fc8SMatthias Ringwald 
844a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
845a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
846a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
847a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
848ce263fc8SMatthias Ringwald         return;
849ce263fc8SMatthias Ringwald     }
850ce263fc8SMatthias Ringwald 
851a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
852a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
853a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
854a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
855ce263fc8SMatthias Ringwald         return;
856ce263fc8SMatthias Ringwald     }
857ce263fc8SMatthias Ringwald 
858a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
859a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
860a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
861a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
862ce263fc8SMatthias Ringwald         return;
863ce263fc8SMatthias Ringwald     }
864ce263fc8SMatthias Ringwald 
865a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
866a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
867a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
868a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
869ce263fc8SMatthias Ringwald         return;
870ce263fc8SMatthias Ringwald     }
871ce263fc8SMatthias Ringwald 
872a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
873a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
874a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
875a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
876ce263fc8SMatthias Ringwald         return;
877ce263fc8SMatthias Ringwald     }
878ce263fc8SMatthias Ringwald 
879a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
880a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
881a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
882a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
883ce263fc8SMatthias Ringwald         return;
884ce263fc8SMatthias Ringwald     }
885ce263fc8SMatthias Ringwald 
886a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
887a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
888a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
889a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
890ce263fc8SMatthias Ringwald         return;
891ce263fc8SMatthias Ringwald     }
892ce263fc8SMatthias Ringwald 
893a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
894a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
895a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
896a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
897ce263fc8SMatthias Ringwald         return;
898ce263fc8SMatthias Ringwald     }
899ce263fc8SMatthias Ringwald 
900a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
901a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
902a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
903a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
904ce263fc8SMatthias Ringwald         return;
905ce263fc8SMatthias Ringwald     }
906ce263fc8SMatthias Ringwald 
907a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
908a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
909a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
910a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
911ce263fc8SMatthias Ringwald         return;
912ce263fc8SMatthias Ringwald     }
913ce263fc8SMatthias Ringwald 
914a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
915a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
916a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
917a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
918667ec068SMatthias Ringwald         return;
919667ec068SMatthias Ringwald     }
920667ec068SMatthias Ringwald 
921a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
922a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
923a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
924a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
925a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
926ce263fc8SMatthias Ringwald         return;
927ce263fc8SMatthias Ringwald     }
928ce263fc8SMatthias Ringwald 
929a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
930a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
931a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
932a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
933ce263fc8SMatthias Ringwald         return;
934ce263fc8SMatthias Ringwald     }
935ce263fc8SMatthias Ringwald 
936a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
937a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
938a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
939a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
940667ec068SMatthias Ringwald         return;
941667ec068SMatthias Ringwald     }
942667ec068SMatthias Ringwald 
943a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
944a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
945667ec068SMatthias Ringwald         char buffer[20];
946a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
947667ec068SMatthias Ringwald             case '?':
9481599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
949ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
950ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
951a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
952667ec068SMatthias Ringwald                 return;
953667ec068SMatthias Ringwald             case '0':
954667ec068SMatthias Ringwald             case '1':
955667ec068SMatthias Ringwald             case '2':
9561599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
957ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
958ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
959ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
960a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
961667ec068SMatthias Ringwald                 return;
962667ec068SMatthias Ringwald             default:
963667ec068SMatthias Ringwald                 break;
964667ec068SMatthias Ringwald         }
965667ec068SMatthias Ringwald         return;
966667ec068SMatthias Ringwald     }
967667ec068SMatthias Ringwald 
968a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
969a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
970667ec068SMatthias Ringwald         char buffer[20];
9711599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
972ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
973ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
974a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
975667ec068SMatthias Ringwald         return;
976667ec068SMatthias Ringwald     }
977667ec068SMatthias Ringwald 
978667ec068SMatthias Ringwald     // update HF indicators
979a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
980667ec068SMatthias Ringwald         int i;
981aeb0f0feSMatthias Ringwald         for (i=0; i < hfp_hf_indicators_nr; i++){
982a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
983a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
984a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
985a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
986667ec068SMatthias Ringwald                     char buffer[30];
9871599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
988ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
989aeb0f0feSMatthias Ringwald                              hfp_hf_indicators[i],
990aeb0f0feSMatthias Ringwald                              (unsigned int)hfp_hf_indicators_value[i]);
991ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
992a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
993667ec068SMatthias Ringwald                 } else {
994aeb0f0feSMatthias Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]);
995667ec068SMatthias Ringwald                 }
996667ec068SMatthias Ringwald                 return;
997667ec068SMatthias Ringwald             }
998667ec068SMatthias Ringwald         }
999667ec068SMatthias Ringwald     }
1000667ec068SMatthias Ringwald 
1001ce263fc8SMatthias Ringwald     if (done) return;
1002ce263fc8SMatthias Ringwald     // deal with disconnect
1003a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
1004ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
1005a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1006a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
1007ce263fc8SMatthias Ringwald             break;
1008ce263fc8SMatthias Ringwald 
1009ce263fc8SMatthias Ringwald         default:
1010ce263fc8SMatthias Ringwald             break;
1011ce263fc8SMatthias Ringwald     }
1012ce263fc8SMatthias Ringwald }
1013ce263fc8SMatthias Ringwald 
1014a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
1015a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
10166a7f44bdSMilanka Ringwald 
1017ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
10187522e673SMatthias Ringwald 
1019184a03edSMilanka Ringwald     uint8_t i;
1020184a03edSMilanka Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1021184a03edSMilanka Ringwald         hfp_connection->ag_indicators[i].status_changed = 0;
1022*ce3797e1SMilanka Ringwald         hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1023184a03edSMilanka Ringwald     }
1024667ec068SMatthias Ringwald     // restore volume settings
1025a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
1026a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1027ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
1028a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
1029a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1030ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
1031667ec068SMatthias Ringwald     // enable all indicators
1032aeb0f0feSMatthias Ringwald     for (i=0; i < hfp_hf_indicators_nr; i++){
1033aeb0f0feSMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i];
1034a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
1035667ec068SMatthias Ringwald     }
1036ce263fc8SMatthias Ringwald }
1037ce263fc8SMatthias Ringwald 
10381cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
1039aeb0f0feSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){
10401cc65c4fSMatthias Ringwald 		// Codec supported, confirm
10411cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
10421cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
10431cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
10441cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
10451cc65c4fSMatthias Ringwald 
10461cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
10471cc65c4fSMatthias Ringwald 	} else {
10481cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
10491cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
10501cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
10511cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
10521cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10531cc65c4fSMatthias Ringwald 
10541cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
10551cc65c4fSMatthias Ringwald 	}
10561cc65c4fSMatthias Ringwald }
10571cc65c4fSMatthias Ringwald 
1058e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){
1059e2c3b58dSMilanka Ringwald     bool event_emited = true;
1060e2c3b58dSMilanka Ringwald 
106199af1e28SMilanka Ringwald     switch (hfp_connection->response_pending_for_command){
1062e2c3b58dSMilanka Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1063e2c3b58dSMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status);
1064e2c3b58dSMilanka Ringwald             break;
1065e2c3b58dSMilanka Ringwald         default:
1066e2c3b58dSMilanka Ringwald             event_emited = false;
1067e2c3b58dSMilanka Ringwald 
1068a0ffb263SMatthias Ringwald             switch (hfp_connection->state){
10693deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
1070a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
1071a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_NOTIFY_ON_CODECS;
10723deb3ec6SMatthias Ringwald                         break;
10733deb3ec6SMatthias Ringwald                     }
1074a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
10753deb3ec6SMatthias Ringwald                     break;
10763deb3ec6SMatthias Ringwald 
10773deb3ec6SMatthias Ringwald                 case HFP_W4_NOTIFY_ON_CODECS:
1078a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
10793deb3ec6SMatthias Ringwald                     break;
10803deb3ec6SMatthias Ringwald 
10813deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS:
1082a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
10833deb3ec6SMatthias Ringwald                     break;
10843deb3ec6SMatthias Ringwald 
10853deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1086a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
10873deb3ec6SMatthias Ringwald                     break;
10883deb3ec6SMatthias Ringwald 
10893deb3ec6SMatthias Ringwald                 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1090a0ffb263SMatthias Ringwald                     if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1091a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
10923deb3ec6SMatthias Ringwald                         break;
10933deb3ec6SMatthias Ringwald                     }
1094a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1095a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
10963deb3ec6SMatthias Ringwald                         break;
10973deb3ec6SMatthias Ringwald                     }
1098a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
10993deb3ec6SMatthias Ringwald                     break;
11003deb3ec6SMatthias Ringwald 
11013deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1102a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1103a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
11043deb3ec6SMatthias Ringwald                         break;
11053deb3ec6SMatthias Ringwald                     }
1106a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11073deb3ec6SMatthias Ringwald                     break;
11083deb3ec6SMatthias Ringwald 
11093deb3ec6SMatthias Ringwald                 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1110a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
11113deb3ec6SMatthias Ringwald                     break;
11123deb3ec6SMatthias Ringwald 
11133deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1114a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
11153deb3ec6SMatthias Ringwald                     break;
11163deb3ec6SMatthias Ringwald 
11173deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1118a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11193deb3ec6SMatthias Ringwald                     break;
1120ce263fc8SMatthias Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1121a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1122a0ffb263SMatthias Ringwald                         hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1123ca59be51SMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1124ce263fc8SMatthias Ringwald                         break;
1125ce263fc8SMatthias Ringwald                     }
11263deb3ec6SMatthias Ringwald 
1127a0ffb263SMatthias Ringwald                     if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1128a0ffb263SMatthias Ringwald                         hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1129ca59be51SMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1130ce263fc8SMatthias Ringwald                         break;
11313deb3ec6SMatthias Ringwald                     }
11323deb3ec6SMatthias Ringwald 
1133a0ffb263SMatthias Ringwald                     switch (hfp_connection->hf_query_operator_state){
1134ce263fc8SMatthias Ringwald                         case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1135a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1136ce263fc8SMatthias Ringwald                             break;
1137ce263fc8SMatthias Ringwald                         case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1138a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1139a473a009SMatthias Ringwald                             hfp_emit_network_operator_event(hfp_connection);
1140ce263fc8SMatthias Ringwald                             break;
1141ce263fc8SMatthias Ringwald                         default:
1142ce263fc8SMatthias Ringwald                             break;
11433deb3ec6SMatthias Ringwald                     }
1144ce263fc8SMatthias Ringwald 
1145a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_extended_audio_gateway_error_report){
1146a0ffb263SMatthias Ringwald                         hfp_connection->enable_extended_audio_gateway_error_report = 0;
1147ce263fc8SMatthias Ringwald                         break;
11483deb3ec6SMatthias Ringwald                     }
11493deb3ec6SMatthias Ringwald 
1150a0ffb263SMatthias Ringwald                     switch (hfp_connection->codecs_state){
1151aa4dd815SMatthias Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1152a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
11533deb3ec6SMatthias Ringwald                             break;
1154ce263fc8SMatthias Ringwald                         case HFP_CODECS_HF_CONFIRMED_CODEC:
1155a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1156ce263fc8SMatthias Ringwald                             break;
11573deb3ec6SMatthias Ringwald                         default:
11583deb3ec6SMatthias Ringwald                             break;
11593deb3ec6SMatthias Ringwald                     }
1160af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
1161be55a11dSMilanka Ringwald                     break;
1162be55a11dSMilanka Ringwald                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
1163af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
11643deb3ec6SMatthias Ringwald                     break;
11653deb3ec6SMatthias Ringwald                 default:
11663deb3ec6SMatthias Ringwald                     break;
11673deb3ec6SMatthias Ringwald             }
1168e2c3b58dSMilanka Ringwald             break;
1169e2c3b58dSMilanka Ringwald     }
11703deb3ec6SMatthias Ringwald 
11713deb3ec6SMatthias Ringwald     // done
117299af1e28SMilanka Ringwald     hfp_connection->response_pending_for_command = HFP_CMD_NONE;
1173be55a11dSMilanka Ringwald     hfp_connection->ok_pending = 0;
1174a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
1175e2c3b58dSMilanka Ringwald     return event_emited;
11763deb3ec6SMatthias Ringwald }
11773deb3ec6SMatthias Ringwald 
1178a03dbc20SMilanka Ringwald static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){
1179a03dbc20SMilanka Ringwald     switch (callsetup_status){
1180a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1181a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1182a03dbc20SMilanka Ringwald             return true;
1183a03dbc20SMilanka Ringwald         default:
1184a03dbc20SMilanka Ringwald             return false;
1185a03dbc20SMilanka Ringwald    }
1186a03dbc20SMilanka Ringwald }
1187be55a11dSMilanka Ringwald 
1188b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
11894562e2a2SMatthias Ringwald     uint16_t i;
1190a03dbc20SMilanka Ringwald 
11914562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
11924562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
11934562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1194a03dbc20SMilanka Ringwald                 hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1195a03dbc20SMilanka Ringwald                 bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status);
1196a03dbc20SMilanka Ringwald                 bool ringing_new = hfp_is_ringing(new_hf_callsetup_status);
1197a03dbc20SMilanka Ringwald                 if (ringing_old != ringing_new){
1198a03dbc20SMilanka Ringwald                     if (ringing_new){
1199a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING);
1200a03dbc20SMilanka Ringwald                     } else {
1201a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING);
1202a03dbc20SMilanka Ringwald                     }
1203a03dbc20SMilanka Ringwald                 }
1204a03dbc20SMilanka Ringwald                 hfp_hf_callsetup_status = new_hf_callsetup_status;
12054562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1206aeb0f0feSMatthias Ringwald                 hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
12074562e2a2SMatthias Ringwald                 // avoid set but not used warning
1208aeb0f0feSMatthias Ringwald                 (void) hfp_hf_callheld_status;
12094562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1210674ebed5SMilanka Ringwald                 hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1211674ebed5SMilanka Ringwald                 if (hfp_hf_call_status != new_hf_call_status){
1212674ebed5SMilanka Ringwald                     if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){
1213674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1214674ebed5SMilanka Ringwald                     } else {
1215674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1216674ebed5SMilanka Ringwald                     }
1217674ebed5SMilanka Ringwald                 }
1218674ebed5SMilanka Ringwald                 hfp_hf_call_status = new_hf_call_status;
12194562e2a2SMatthias Ringwald             }
12204562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1221*ce3797e1SMilanka Ringwald             hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]);
12224562e2a2SMatthias Ringwald             break;
12234562e2a2SMatthias Ringwald         }
12244562e2a2SMatthias Ringwald     }
12254562e2a2SMatthias Ringwald }
12264562e2a2SMatthias Ringwald 
1227426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1228186dd3d2SMatthias Ringwald     int value;
1229186dd3d2SMatthias Ringwald     int i;
1230e2c3b58dSMilanka Ringwald     bool event_emited;
1231e2c3b58dSMilanka Ringwald 
1232a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1233667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1234a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1235a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1236667ec068SMatthias Ringwald             break;
1237667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1238a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1239ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1240667ec068SMatthias Ringwald             break;
1241667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1242a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1243a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1244667ec068SMatthias Ringwald             break;
1245ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1246a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12472308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1248667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1249ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1250ce263fc8SMatthias Ringwald             break;
1251ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1252a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12532308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1254667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1255ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1256ce263fc8SMatthias Ringwald             break;
1257ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1258a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1259ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1260a0ffb263SMatthias Ringwald             break;
1261a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1262a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1263a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1264a0ffb263SMatthias Ringwald             break;
1265a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1266a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1267a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1268ce263fc8SMatthias Ringwald             break;
1269ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1270a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12715a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1272a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1273ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1274ce263fc8SMatthias Ringwald             break;
12750b4debbfSMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
12760b4debbfSMilanka Ringwald             break;
1277fdda66c0SMilanka Ringwald         case HFP_CMD_ERROR:
127890244c92SMilanka Ringwald             switch (hfp_connection->state){
127990244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
128090244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
128190244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1282fdda66c0SMilanka Ringwald                             hfp_reset_context_flags(hfp_connection);
128390244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
128490244c92SMilanka Ringwald                             return;
128590244c92SMilanka Ringwald                         default:
128690244c92SMilanka Ringwald                             break;
128790244c92SMilanka Ringwald                     }
128856f1adacSMilanka Ringwald                     break;
128956f1adacSMilanka Ringwald                 default:
129056f1adacSMilanka Ringwald                     break;
129156f1adacSMilanka Ringwald             }
1292e2c3b58dSMilanka Ringwald 
1293fdda66c0SMilanka Ringwald             // handle error response for voice activation (HF initiated)
12940b4debbfSMilanka Ringwald             switch(hfp_connection->vra_state_requested){
1295de9e0ea7SMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1296de9e0ea7SMilanka Ringwald                     hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1297be55a11dSMilanka Ringwald                     break;
1298be55a11dSMilanka Ringwald                 default:
1299e2c3b58dSMilanka Ringwald                     if (hfp_connection->vra_state_requested == hfp_connection->vra_state){
1300e2c3b58dSMilanka Ringwald                         break;
1301e2c3b58dSMilanka Ringwald                     }
13020b4debbfSMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
1303553a4a56SMilanka Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
13040b4debbfSMilanka Ringwald                     hfp_reset_context_flags(hfp_connection);
13050b4debbfSMilanka Ringwald                     return;
1306be55a11dSMilanka Ringwald             }
1307e2c3b58dSMilanka Ringwald             event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1308e2c3b58dSMilanka Ringwald             if (!event_emited){
13090b4debbfSMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1310e2c3b58dSMilanka Ringwald             }
1311fdda66c0SMilanka Ringwald             hfp_reset_context_flags(hfp_connection);
1312ce263fc8SMatthias Ringwald             break;
1313fdda66c0SMilanka Ringwald 
1314ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1315e2c3b58dSMilanka Ringwald             hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS);
1316ce263fc8SMatthias Ringwald             break;
1317ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
13185a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1319ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1320ce263fc8SMatthias Ringwald             break;
1321ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
13225a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13234562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1324ce263fc8SMatthias Ringwald             break;
1325c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
13265a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1327184a03edSMilanka Ringwald             // report status after SLC established
1328184a03edSMilanka Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
1329184a03edSMilanka Ringwald                 break;
1330184a03edSMilanka Ringwald             }
1331c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1332*ce3797e1SMilanka Ringwald                 hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1333c741b032SMilanka Ringwald             }
1334c741b032SMilanka Ringwald             break;
13351cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
13361cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13375a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
13381cc65c4fSMatthias Ringwald 			break;
1339eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1340eac56539SMilanka 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));
1341ce263fc8SMatthias Ringwald         default:
1342ce263fc8SMatthias Ringwald             break;
13433deb3ec6SMatthias Ringwald     }
13440cef86faSMatthias Ringwald }
1345426f9988SMatthias Ringwald 
134676cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
134776cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
134876cc1527SMatthias Ringwald }
134976cc1527SMatthias Ringwald 
13500b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1351426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1352426f9988SMatthias Ringwald     if (size < 1) return;
1353426f9988SMatthias Ringwald 
1354426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1355e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1356e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1357e43d1938SMatthias Ringwald #endif
1358426f9988SMatthias Ringwald 
1359426f9988SMatthias Ringwald     // process messages byte-wise
1360a7ba78b0SMilanka Ringwald     uint8_t pos;
1361426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1362426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
13631599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1364426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
13650b4debbfSMilanka Ringwald         hfp_hf_handle_rfcomm_command(hfp_connection);
13663deb3ec6SMatthias Ringwald     }
1367a7ba78b0SMilanka Ringwald }
13683deb3ec6SMatthias Ringwald 
13691c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1370665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1371665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1372665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1373a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
137422387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
13751c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
13763deb3ec6SMatthias Ringwald     }
13773deb3ec6SMatthias Ringwald }
13783deb3ec6SMatthias Ringwald 
13791c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
13800b4debbfSMilanka Ringwald     hfp_connection_t * hfp_connection;
13813deb3ec6SMatthias Ringwald     switch (packet_type){
13823deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
13830b4debbfSMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
13840b4debbfSMilanka Ringwald             if (!hfp_connection) return;
13850b4debbfSMilanka Ringwald             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
13860b4debbfSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
13870b4debbfSMilanka Ringwald             return;
13883deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1389d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1390d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
13911c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1392d4dd47ffSMatthias Ringwald                 return;
1393d4dd47ffSMatthias Ringwald             }
139427950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1395202c8a4cSMatthias Ringwald             break;
13963deb3ec6SMatthias Ringwald         default:
13973deb3ec6SMatthias Ringwald             break;
13983deb3ec6SMatthias Ringwald     }
13991c6a0fc0SMatthias Ringwald     hfp_hf_run();
14003deb3ec6SMatthias Ringwald }
14013deb3ec6SMatthias Ringwald 
14021c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1403405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
14041c6a0fc0SMatthias Ringwald     hfp_hf_run();
1405405014fbSMatthias Ringwald }
1406405014fbSMatthias Ringwald 
1407aeb0f0feSMatthias Ringwald static void hfp_hf_set_defaults(void){
1408aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1409aeb0f0feSMatthias Ringwald     hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
1410aeb0f0feSMatthias Ringwald     hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1411aeb0f0feSMatthias Ringwald     hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1412aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = 0;
1413aeb0f0feSMatthias Ringwald     hfp_hf_speaker_gain = 9;
1414aeb0f0feSMatthias Ringwald     hfp_hf_microphone_gain = 9;
1415aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = 0;
1416aeb0f0feSMatthias Ringwald }
1417aeb0f0feSMatthias Ringwald 
1418b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1419b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1420b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1421b4df8028SMilanka Ringwald         return status;
1422b4df8028SMilanka Ringwald     }
1423b4df8028SMilanka Ringwald 
1424520c92d5SMatthias Ringwald     hfp_init();
1425aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1426d63c37a1SMatthias Ringwald 
14271c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
14281c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
142927950165SMatthias Ringwald 
143027950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
14311c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1432b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
143320b2edb6SMatthias Ringwald }
143427950165SMatthias Ringwald 
143520b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
143620b2edb6SMatthias Ringwald     hfp_deinit();
1437aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1438aeb0f0feSMatthias Ringwald 
1439aeb0f0feSMatthias Ringwald     hfp_hf_callback = NULL;
144020b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
1441aeb0f0feSMatthias Ringwald     (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number));
1442a0ffb263SMatthias Ringwald }
1443a0ffb263SMatthias Ringwald 
14447ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
144568466199SMilanka Ringwald     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
14463deb3ec6SMatthias Ringwald 
1447aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = codecs_nr;
14483deb3ec6SMatthias Ringwald     int i;
14493deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
1450aeb0f0feSMatthias Ringwald         hfp_hf_codecs[i] = codecs[i];
14513deb3ec6SMatthias Ringwald     }
14523deb3ec6SMatthias Ringwald }
14533deb3ec6SMatthias Ringwald 
1454a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
1455aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = supported_features;
1456a0ffb263SMatthias Ringwald }
14573deb3ec6SMatthias Ringwald 
14587ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
1459aeb0f0feSMatthias Ringwald     btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS);
146068466199SMilanka Ringwald 
1461aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = indicators_nr;
14623deb3ec6SMatthias Ringwald     int i;
1463aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
1464aeb0f0feSMatthias Ringwald         hfp_hf_indicators[i] = indicators[i];
14653deb3ec6SMatthias Ringwald     }
14663deb3ec6SMatthias Ringwald }
14673deb3ec6SMatthias Ringwald 
14684eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
14694eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
14703deb3ec6SMatthias Ringwald }
14713deb3ec6SMatthias Ringwald 
1472657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
14739c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1474a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1475657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1476a33eb0c4SMilanka Ringwald     }
14771ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
14781c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1479657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
14803deb3ec6SMatthias Ringwald }
14813deb3ec6SMatthias Ringwald 
14823c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
14839c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1484a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14853c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14863deb3ec6SMatthias Ringwald     }
1487a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
14881c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14893c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14903deb3ec6SMatthias Ringwald }
14913deb3ec6SMatthias Ringwald 
14923c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
14933c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1494ce263fc8SMatthias Ringwald }
1495ce263fc8SMatthias Ringwald 
14963c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
14973c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1498ce263fc8SMatthias Ringwald }
1499ce263fc8SMatthias Ringwald 
15003deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
15013c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
15029c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1503a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15043c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15053deb3ec6SMatthias Ringwald     }
1506a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1507a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
15081c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15093c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15103deb3ec6SMatthias Ringwald }
15113deb3ec6SMatthias Ringwald 
15123c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
15139c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1514a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15153c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15163deb3ec6SMatthias Ringwald     }
15173c65e705SMilanka Ringwald 
1518a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1519ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1520a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1521ce263fc8SMatthias Ringwald             break;
1522ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1523a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1524ce263fc8SMatthias Ringwald             break;
1525ce263fc8SMatthias Ringwald         default:
15263c65e705SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1527ce263fc8SMatthias Ringwald     }
15281c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15293c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15303deb3ec6SMatthias Ringwald }
15313deb3ec6SMatthias Ringwald 
15323c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
15339c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1534a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15353c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15363deb3ec6SMatthias Ringwald     }
1537a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
15381c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15393c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15403deb3ec6SMatthias Ringwald }
15413deb3ec6SMatthias Ringwald 
1542ce263fc8SMatthias Ringwald 
15433c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
15443c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1545ce263fc8SMatthias Ringwald }
1546ce263fc8SMatthias Ringwald 
15473c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
15483c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1549ce263fc8SMatthias Ringwald }
1550ce263fc8SMatthias Ringwald 
155138200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
1552aeb0f0feSMatthias Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4));
155338200c1dSMilanka Ringwald }
1554ce263fc8SMatthias Ringwald 
15553c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
15569c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1557a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15583c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1559a33eb0c4SMilanka Ringwald     }
1560ce263fc8SMatthias Ringwald 
15613c65e705SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
15623c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
15633c65e705SMilanka Ringwald     }
15643c65e705SMilanka Ringwald 
15653c65e705SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
15663c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
15673c65e705SMilanka Ringwald     }
1568f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1569a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1570aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1571aa4dd815SMatthias Ringwald                 break;
1572ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1573ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1574ec3bfc1aSMatthias Ringwald                 break;
1575aa4dd815SMatthias Ringwald             default:
15761cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
15771cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
15781cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
15791cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
158038200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1581aa4dd815SMatthias Ringwald                 break;
15823deb3ec6SMatthias Ringwald         }
1583f4412093SMatthias Ringwald     } else {
1584f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1585f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1586f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1587f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1588f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1589f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1590f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1591f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1592ce263fc8SMatthias Ringwald     }
1593ce263fc8SMatthias Ringwald 
15941c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15953c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15963deb3ec6SMatthias Ringwald }
15973deb3ec6SMatthias Ringwald 
15983c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
15999c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1600a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16013c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1602a33eb0c4SMilanka Ringwald     }
16030b4debbfSMilanka Ringwald     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
16040b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16050b4debbfSMilanka Ringwald     }
16060b4debbfSMilanka Ringwald     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
16070b4debbfSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
16081c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
16090b4debbfSMilanka Ringwald     }
16103c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16113deb3ec6SMatthias Ringwald }
16123deb3ec6SMatthias Ringwald 
16133c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
16149c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1615a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16163c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1617a33eb0c4SMilanka Ringwald     }
1618ce263fc8SMatthias Ringwald 
1619aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1620a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
16211c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1622ce263fc8SMatthias Ringwald     } else {
1623aeb0f0feSMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status);
16243c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1625ce263fc8SMatthias Ringwald     }
16263c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1627ce263fc8SMatthias Ringwald }
1628ce263fc8SMatthias Ringwald 
16293c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
16309c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1631a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16323c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1633a33eb0c4SMilanka Ringwald     }
1634a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
16351c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16363c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1637ce263fc8SMatthias Ringwald }
1638ce263fc8SMatthias Ringwald 
16393c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(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_chup = 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_user_busy(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){
1659a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
16601c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1661ce263fc8SMatthias Ringwald     }
16623c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1663ce263fc8SMatthias Ringwald }
1664ce263fc8SMatthias Ringwald 
16653c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
16669c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1667a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16683c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1669a33eb0c4SMilanka Ringwald     }
1670ce263fc8SMatthias Ringwald 
1671aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1672aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1673a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
16741c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1675ce263fc8SMatthias Ringwald     }
16763c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1677ce263fc8SMatthias Ringwald }
1678ce263fc8SMatthias Ringwald 
16793c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
16809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1681a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16823c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1683a33eb0c4SMilanka Ringwald     }
1684ce263fc8SMatthias Ringwald 
1685aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1686aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1687a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
16881c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1689ce263fc8SMatthias Ringwald     }
16903c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1691ce263fc8SMatthias Ringwald }
1692ce263fc8SMatthias Ringwald 
16933c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
16949c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1695a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16963c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1697a33eb0c4SMilanka Ringwald     }
1698ce263fc8SMatthias Ringwald 
1699aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1700aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1701a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
17021c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1703ce263fc8SMatthias Ringwald     }
17043c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1705ce263fc8SMatthias Ringwald }
1706ce263fc8SMatthias Ringwald 
17073c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
17089c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1709a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17103c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1711a33eb0c4SMilanka Ringwald     }
1712ce263fc8SMatthias Ringwald 
1713aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1714aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1715a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
17161c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1717ce263fc8SMatthias Ringwald     }
17183c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1719ce263fc8SMatthias Ringwald }
1720ce263fc8SMatthias Ringwald 
17213c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
17229c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1723a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17243c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1725a33eb0c4SMilanka Ringwald     }
1726667ec068SMatthias Ringwald 
1727aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1728aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1729a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1730a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
17311c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1732667ec068SMatthias Ringwald     }
17333c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1734667ec068SMatthias Ringwald }
1735667ec068SMatthias Ringwald 
17363c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
17379c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1738a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17393c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1740a33eb0c4SMilanka Ringwald     }
1741667ec068SMatthias Ringwald 
1742aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1743aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1744a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1745a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
17461c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1747667ec068SMatthias Ringwald     }
17483c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1749667ec068SMatthias Ringwald }
1750ce263fc8SMatthias Ringwald 
17513c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
17529c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1753a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17543c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1755a33eb0c4SMilanka Ringwald     }
1756ce263fc8SMatthias Ringwald 
1757a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1758aeb0f0feSMatthias Ringwald     snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number);
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_dial_memory(hci_con_handle_t acl_handle, int memory_id){
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_memory_dialing = 1;
1770a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1771a0ffb263SMatthias Ringwald 
17721c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17733c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1774ce263fc8SMatthias Ringwald }
1775ce263fc8SMatthias Ringwald 
17763c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
17779c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1778a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17793c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1780a33eb0c4SMilanka Ringwald     }
1781ce263fc8SMatthias Ringwald 
1782a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
17831c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17843c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1785ce263fc8SMatthias Ringwald }
1786ce263fc8SMatthias Ringwald 
17873c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
17889c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1789a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17903c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1791a33eb0c4SMilanka Ringwald     }
1792ce263fc8SMatthias Ringwald 
1793a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
17941c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17953c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1796ce263fc8SMatthias Ringwald }
1797ce263fc8SMatthias Ringwald 
1798ce263fc8SMatthias Ringwald 
17993c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
18009c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1801a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18023c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1803a33eb0c4SMilanka Ringwald     }
1804ce263fc8SMatthias Ringwald 
1805a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
18061c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18073c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1808ce263fc8SMatthias Ringwald }
1809ce263fc8SMatthias Ringwald 
1810ce263fc8SMatthias Ringwald 
18113c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
18129c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1813a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18143c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1815a33eb0c4SMilanka Ringwald     }
1816ce263fc8SMatthias Ringwald 
1817a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
18181c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18193c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1820ce263fc8SMatthias Ringwald }
1821ce263fc8SMatthias Ringwald 
18223c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
18239c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1824a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18253c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1826a33eb0c4SMilanka Ringwald     }
1827ce263fc8SMatthias Ringwald 
1828a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
18291c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18303c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1831ce263fc8SMatthias Ringwald }
1832ce263fc8SMatthias Ringwald 
18336ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){
18346ba83b5eSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION);
1835aeb0f0feSMatthias Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION);
18366ba83b5eSMilanka Ringwald     return hf && ag;
1837ce263fc8SMatthias Ringwald }
1838ce263fc8SMatthias Ringwald 
18393c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
18409c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1841a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18423c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1843a33eb0c4SMilanka Ringwald     }
18446ba83b5eSMilanka Ringwald     if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){
18456ba83b5eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
18466ba83b5eSMilanka Ringwald     }
1847ce263fc8SMatthias Ringwald 
1848a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
18491c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18503c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1851ce263fc8SMatthias Ringwald }
1852ce263fc8SMatthias Ringwald 
1853acd11d4aSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1854fdda66c0SMilanka Ringwald      hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1855fdda66c0SMilanka Ringwald     if (!hfp_connection) {
1856fdda66c0SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1857be55a11dSMilanka Ringwald     }
1858013cc750SMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1859013cc750SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1860013cc750SMilanka Ringwald     }
1861acd11d4aSMilanka Ringwald 
1862acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1863acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1864acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
1865acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1866af97579eSMilanka Ringwald     }
1867af97579eSMilanka Ringwald 
1868498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1869be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1870de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1871498a8121SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1872fd4151d1SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
1873acd11d4aSMilanka Ringwald             hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported;
1874be55a11dSMilanka Ringwald             break;
1875de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1876de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = true;
1877de9e0ea7SMilanka Ringwald             break;
1878be55a11dSMilanka Ringwald         default:
1879be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1880be55a11dSMilanka Ringwald     }
1881ce263fc8SMatthias Ringwald 
1882af97579eSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1883fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1884af97579eSMilanka Ringwald }
1885af97579eSMilanka Ringwald 
1886acd11d4aSMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
1887af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1888af97579eSMilanka Ringwald     if (!hfp_connection) {
1889af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1890af97579eSMilanka Ringwald     }
1891acd11d4aSMilanka Ringwald     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
189208a0b01cSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
189308a0b01cSMilanka Ringwald     }
1894acd11d4aSMilanka Ringwald 
1895acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1896acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported){
1897acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1898acd11d4aSMilanka Ringwald     }
1899acd11d4aSMilanka Ringwald 
1900acd11d4aSMilanka Ringwald     switch (hfp_connection->vra_state){
1901acd11d4aSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1902acd11d4aSMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1903acd11d4aSMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1904acd11d4aSMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
1905acd11d4aSMilanka Ringwald             break;
1906acd11d4aSMilanka Ringwald         default:
1907fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1908af97579eSMilanka Ringwald     }
1909013cc750SMilanka Ringwald 
1910acd11d4aSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1911acd11d4aSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1912acd11d4aSMilanka Ringwald }
1913acd11d4aSMilanka Ringwald 
1914acd11d4aSMilanka Ringwald 
1915acd11d4aSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1916acd11d4aSMilanka Ringwald     // return deactivate_voice_recognition(acl_handle, false);
1917acd11d4aSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1918acd11d4aSMilanka Ringwald     if (!hfp_connection) {
1919acd11d4aSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1920acd11d4aSMilanka Ringwald     }
1921acd11d4aSMilanka Ringwald 
1922acd11d4aSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
1923acd11d4aSMilanka Ringwald         hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1924acd11d4aSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1925acd11d4aSMilanka Ringwald     }
1926acd11d4aSMilanka Ringwald 
1927acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1928acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1929acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
1930acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1931acd11d4aSMilanka Ringwald     }
1932acd11d4aSMilanka Ringwald 
1933fdda66c0SMilanka Ringwald     switch (hfp_connection->vra_state){
1934de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
1935fdda66c0SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1936de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1937de9e0ea7SMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1938fdda66c0SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1939fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1940fdda66c0SMilanka Ringwald             break;
1941de9e0ea7SMilanka Ringwald 
1942de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1943de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1944de9e0ea7SMilanka Ringwald             hfp_connection->deactivate_voice_recognition = true;
1945de9e0ea7SMilanka Ringwald             break;
1946de9e0ea7SMilanka Ringwald 
1947de9e0ea7SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1948de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1949de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1950fdda66c0SMilanka Ringwald         default:
1951fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1952fdda66c0SMilanka Ringwald     }
1953fdda66c0SMilanka Ringwald 
1954fdda66c0SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1955fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1956af97579eSMilanka Ringwald }
1957af97579eSMilanka Ringwald 
19583c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
19599c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1960a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19613c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1962a33eb0c4SMilanka Ringwald     }
1963c8626498SMilanka Ringwald 
19643c65e705SMilanka Ringwald     if (hfp_connection->microphone_gain == gain) {
19653c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
19663c65e705SMilanka Ringwald     }
19673c65e705SMilanka Ringwald 
1968c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1969a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
19703c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1971a0ffb263SMatthias Ringwald     }
19723c65e705SMilanka Ringwald 
1973a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1974a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
19751c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19763c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1977ce263fc8SMatthias Ringwald }
1978ce263fc8SMatthias Ringwald 
19793c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
19809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1981a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19823c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1983a33eb0c4SMilanka Ringwald     }
1984c8626498SMilanka Ringwald 
19853c65e705SMilanka Ringwald     if (hfp_connection->speaker_gain == gain){
19863c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
19873c65e705SMilanka Ringwald     }
19883c65e705SMilanka Ringwald 
1989c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1990a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
19913c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1992a0ffb263SMatthias Ringwald     }
19933c65e705SMilanka Ringwald 
1994a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1995a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
19961c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19973c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1998ce263fc8SMatthias Ringwald }
1999ce263fc8SMatthias Ringwald 
20003c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
20019c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2002a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20033c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2004a33eb0c4SMilanka Ringwald     }
2005a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
20061c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20073c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2008ce263fc8SMatthias Ringwald }
2009ce263fc8SMatthias Ringwald 
20103c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
20119c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2012a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20133c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2014a33eb0c4SMilanka Ringwald     }
2015a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
20161c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20173c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2018ce263fc8SMatthias Ringwald }
20193deb3ec6SMatthias Ringwald 
20203c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
20219c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2022a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20233c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2024a33eb0c4SMilanka Ringwald     }
2025a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
20261c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20273c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2028667ec068SMatthias Ringwald }
2029667ec068SMatthias Ringwald 
2030667ec068SMatthias Ringwald 
20313c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
20329c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2033a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20343c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2035a33eb0c4SMilanka Ringwald     }
2036a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2037a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
20381c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20393c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2040667ec068SMatthias Ringwald }
2041667ec068SMatthias Ringwald 
20423c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
20439c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2044a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20453c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2046a33eb0c4SMilanka Ringwald     }
2047a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2048a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
20491c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20503c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2051667ec068SMatthias Ringwald }
2052667ec068SMatthias Ringwald 
20533c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
20549c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2055a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20563c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2057a33eb0c4SMilanka Ringwald     }
2058a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2059a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
20601c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20613c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2062667ec068SMatthias Ringwald }
2063667ec068SMatthias Ringwald 
20643c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
20659c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2066a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20673c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2068a33eb0c4SMilanka Ringwald     }
2069a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2070a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
20711c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20723c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2073667ec068SMatthias Ringwald }
2074667ec068SMatthias Ringwald 
20753c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
20769c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2077a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20783c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2079a33eb0c4SMilanka Ringwald     }
2080a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
20811c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20823c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2083667ec068SMatthias Ringwald }
2084667ec068SMatthias Ringwald 
20853c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
20869c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2087a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20883c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2089a33eb0c4SMilanka Ringwald     }
2090667ec068SMatthias Ringwald     // find index for assigned number
2091667ec068SMatthias Ringwald     int i;
2092aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
2093aeb0f0feSMatthias Ringwald         if (hfp_hf_indicators[i] == assigned_number){
2094667ec068SMatthias Ringwald             // set value
2095aeb0f0feSMatthias Ringwald             hfp_hf_indicators_value[i] = value;
2096667ec068SMatthias Ringwald             // mark for update
2097a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
2098a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
2099667ec068SMatthias Ringwald                 // send update
21001c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
2101a0ffb263SMatthias Ringwald             }
21023c65e705SMilanka Ringwald             return ERROR_CODE_SUCCESS;
2103667ec068SMatthias Ringwald         }
2104667ec068SMatthias Ringwald     }
21053c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2106667ec068SMatthias Ringwald }
2107667ec068SMatthias Ringwald 
2108d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
2109d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2110d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
2111d7f6b5cbSMatthias Ringwald         return 0;
2112d7f6b5cbSMatthias Ringwald     }
2113d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
2114d7f6b5cbSMatthias Ringwald }
211576cc1527SMatthias Ringwald 
211676cc1527SMatthias 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){
211776cc1527SMatthias Ringwald 	if (!name){
2118aeb0f0feSMatthias Ringwald 		name = hfp_hf_default_service_name;
211976cc1527SMatthias Ringwald 	}
212076cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
212176cc1527SMatthias Ringwald 
212276cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
212376cc1527SMatthias Ringwald 	//
212476cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
212576cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
212676cc1527SMatthias Ringwald 	//
212776cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
212876cc1527SMatthias Ringwald 	//
212976cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
2130ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
213176cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
213276cc1527SMatthias Ringwald 	}
2133ef3ae4ebSMilanka Ringwald 
2134ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
213556f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
2136ef3ae4ebSMilanka Ringwald     }
2137ef3ae4ebSMilanka Ringwald 
2138ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
213956f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
2140ef3ae4ebSMilanka Ringwald     }
2141ef3ae4ebSMilanka Ringwald 
214276cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
214376cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
214476cc1527SMatthias Ringwald }
214576cc1527SMatthias Ringwald 
214676cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
214768466199SMilanka Ringwald 	btstack_assert(callback != NULL);
214868466199SMilanka Ringwald 
214976cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
215076cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
215176cc1527SMatthias Ringwald }
2152