xref: /btstack/src/classic/hfp_hf.c (revision 2b5f92fd17cf7c0407f08b551caaa8f93b415f83)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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 
1921ac1f60fSMilanka Ringwald static void hfp_emit_ag_indicator_mapping_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
1931ac1f60fSMilanka Ringwald     if (hfp_hf_callback == NULL) return;
1941ac1f60fSMilanka Ringwald     uint8_t event[8+HFP_MAX_INDICATOR_DESC_SIZE+1];
1951ac1f60fSMilanka Ringwald     int pos = 0;
1961ac1f60fSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
1971ac1f60fSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
1981ac1f60fSMilanka Ringwald     event[pos++] = HFP_SUBEVENT_AG_INDICATOR_MAPPING;
1991ac1f60fSMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
2001ac1f60fSMilanka Ringwald     pos += 2;
2011ac1f60fSMilanka Ringwald     event[pos++] = indicator->index;
2021ac1f60fSMilanka Ringwald     event[pos++] = indicator->min_range;
2031ac1f60fSMilanka Ringwald     event[pos++] = indicator->max_range;
2041ac1f60fSMilanka Ringwald     strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
2051ac1f60fSMilanka Ringwald     pos += HFP_MAX_INDICATOR_DESC_SIZE;
2061ac1f60fSMilanka Ringwald     event[pos] = 0;
2071ac1f60fSMilanka Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2081ac1f60fSMilanka Ringwald }
20976cc1527SMatthias Ringwald 
210ce3797e1SMilanka Ringwald static void hfp_emit_ag_indicator_status_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
211a473a009SMatthias Ringwald 	if (hfp_hf_callback == NULL) return;
212d703d377SMatthias Ringwald 	uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE+1];
21376cc1527SMatthias Ringwald 	int pos = 0;
21476cc1527SMatthias Ringwald 	event[pos++] = HCI_EVENT_HFP_META;
21576cc1527SMatthias Ringwald 	event[pos++] = sizeof(event) - 2;
21676cc1527SMatthias Ringwald 	event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
217d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
218d703d377SMatthias Ringwald     pos += 2;
219a473a009SMatthias Ringwald 	event[pos++] = indicator->index;
220a473a009SMatthias Ringwald 	event[pos++] = indicator->status;
221a473a009SMatthias Ringwald 	event[pos++] = indicator->min_range;
222a473a009SMatthias Ringwald 	event[pos++] = indicator->max_range;
223a473a009SMatthias Ringwald 	event[pos++] = indicator->mandatory;
224a473a009SMatthias Ringwald 	event[pos++] = indicator->enabled;
225a473a009SMatthias Ringwald 	event[pos++] = indicator->status_changed;
226a473a009SMatthias Ringwald 	strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
22776cc1527SMatthias Ringwald 	pos += HFP_MAX_INDICATOR_DESC_SIZE;
22876cc1527SMatthias Ringwald 	event[pos] = 0;
229a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2303deb3ec6SMatthias Ringwald }
2313deb3ec6SMatthias Ringwald 
232a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){
233a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
234d703d377SMatthias Ringwald 	uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
23576cc1527SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
23676cc1527SMatthias Ringwald 	event[1] = sizeof(event) - 2;
23776cc1527SMatthias Ringwald 	event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
238d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
239d703d377SMatthias Ringwald 	event[5] = hfp_connection->network_operator.mode;
240d703d377SMatthias Ringwald 	event[6] = hfp_connection->network_operator.format;
241d703d377SMatthias Ringwald 	strncpy((char*)&event[7], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
242d703d377SMatthias Ringwald 	event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
243a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2443deb3ec6SMatthias Ringwald }
2453deb3ec6SMatthias Ringwald 
246b95cac54SMilanka Ringwald 
247b95cac54SMilanka Ringwald static void hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection){
248b95cac54SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
24951aa5d5aSMilanka Ringwald     uint8_t event[HFP_MAX_VR_TEXT_SIZE + 11];
250b95cac54SMilanka Ringwald     int pos = 0;
251b95cac54SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
252b95cac54SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
253b95cac54SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE;
254b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
255b95cac54SMilanka Ringwald     pos += 2;
256b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->ag_msg.text_id);
257b95cac54SMilanka Ringwald     pos += 2;
258b95cac54SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_type;
259e83f1be7SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_operation;
260b95cac54SMilanka Ringwald 
26151aa5d5aSMilanka Ringwald     // length, zero ending is already in message
262b95cac54SMilanka Ringwald     uint8_t * value = &hfp_connection->line_buffer[0];
26351aa5d5aSMilanka Ringwald     uint16_t  value_length = hfp_connection->ag_vra_msg_length;
26451aa5d5aSMilanka Ringwald 
26551aa5d5aSMilanka Ringwald     little_endian_store_16(event, pos, value_length);
266b95cac54SMilanka Ringwald     pos += 2;
26751aa5d5aSMilanka Ringwald     memcpy(&event[pos], value, value_length);
26851aa5d5aSMilanka Ringwald     pos += value_length;
26951aa5d5aSMilanka Ringwald 
270b95cac54SMilanka Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
271b95cac54SMilanka Ringwald }
272b95cac54SMilanka Ringwald 
27376cc1527SMatthias Ringwald /* send commands */
27489425bfcSMilanka Ringwald 
27589425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2763deb3ec6SMatthias Ringwald     char buffer[20];
2771599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
27889425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
27989425bfcSMilanka Ringwald }
28089425bfcSMilanka Ringwald 
28189425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
28289425bfcSMilanka Ringwald     char buffer[20];
2831599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
28489425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
28589425bfcSMilanka Ringwald }
28689425bfcSMilanka Ringwald 
28786da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
28889425bfcSMilanka Ringwald     char buffer[40];
2891599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
2903deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2913deb3ec6SMatthias Ringwald }
2923deb3ec6SMatthias Ringwald 
2933deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2943deb3ec6SMatthias Ringwald     char buffer[30];
29589425bfcSMilanka Ringwald     const int size = sizeof(buffer);
29689425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
297aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_codecs, hfp_hf_codecs_nr);
2981599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2993deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3003deb3ec6SMatthias Ringwald }
3013deb3ec6SMatthias Ringwald 
3023deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
3033deb3ec6SMatthias Ringwald     char buffer[50];
30489425bfcSMilanka Ringwald     const int size = sizeof(buffer);
30589425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
30689425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
3071599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
3083deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3093deb3ec6SMatthias Ringwald }
3103deb3ec6SMatthias Ringwald 
3113deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
3123deb3ec6SMatthias Ringwald     char buffer[30];
31389425bfcSMilanka Ringwald     const int size = sizeof(buffer);
31489425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
315aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_indicators, hfp_hf_indicators_nr);
3161599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
3173deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3183deb3ec6SMatthias Ringwald }
3193deb3ec6SMatthias Ringwald 
32089425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
3213deb3ec6SMatthias Ringwald     char buffer[20];
3221599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
323ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
324ce263fc8SMatthias Ringwald }
325ce263fc8SMatthias Ringwald 
326ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
327ce263fc8SMatthias Ringwald     char buffer[40];
328aeb0f0feSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, hfp_hf_phone_number);
329ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
330ce263fc8SMatthias Ringwald }
331ce263fc8SMatthias Ringwald 
332a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
333ce263fc8SMatthias Ringwald     char buffer[40];
3341599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
335ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
336ce263fc8SMatthias Ringwald }
337ce263fc8SMatthias Ringwald 
338f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
33989425bfcSMilanka Ringwald     char buffer[40];
3401599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
341ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
342ce263fc8SMatthias Ringwald }
343ce263fc8SMatthias Ringwald 
344ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
345ce263fc8SMatthias Ringwald     char buffer[20];
3461599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
347ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
348ce263fc8SMatthias Ringwald }
349ce263fc8SMatthias Ringwald 
35097d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
3511599fe57SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "ATA\r");
35297d2cadbSMatthias Ringwald }
35397d2cadbSMatthias Ringwald 
35489425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
355aeb0f0feSMatthias Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_hf_supported_features);
35689425bfcSMilanka Ringwald }
35789425bfcSMilanka Ringwald 
35889425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
35989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
36089425bfcSMilanka Ringwald }
36189425bfcSMilanka Ringwald 
36289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
36389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
36489425bfcSMilanka Ringwald }
36589425bfcSMilanka Ringwald 
36689425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
36789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
36889425bfcSMilanka Ringwald }
36989425bfcSMilanka Ringwald 
37089425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
37189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
37289425bfcSMilanka Ringwald }
37389425bfcSMilanka Ringwald 
37489425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
37589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
37689425bfcSMilanka Ringwald }
37789425bfcSMilanka Ringwald 
37889425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
37989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
38089425bfcSMilanka Ringwald }
38189425bfcSMilanka Ringwald 
38289425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
38389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
38489425bfcSMilanka Ringwald }
38589425bfcSMilanka Ringwald 
38689425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
38789425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
38889425bfcSMilanka Ringwald }
38989425bfcSMilanka Ringwald 
39089425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
39189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
39289425bfcSMilanka Ringwald }
39389425bfcSMilanka Ringwald 
39489425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
39589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
39689425bfcSMilanka Ringwald }
39789425bfcSMilanka Ringwald 
39889425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
39989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
40089425bfcSMilanka Ringwald }
40189425bfcSMilanka Ringwald 
40289425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
40389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
40489425bfcSMilanka Ringwald }
40589425bfcSMilanka Ringwald 
40689425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
40789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
40889425bfcSMilanka Ringwald }
40989425bfcSMilanka Ringwald 
41089425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
41189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
41289425bfcSMilanka Ringwald }
41389425bfcSMilanka Ringwald 
41489425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
41589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
41689425bfcSMilanka Ringwald }
41789425bfcSMilanka Ringwald 
41889425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
41989425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
42089425bfcSMilanka Ringwald }
42189425bfcSMilanka Ringwald 
42289425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
42389425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
42489425bfcSMilanka Ringwald }
42589425bfcSMilanka Ringwald 
426ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
42789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
428ce263fc8SMatthias Ringwald }
429ce263fc8SMatthias Ringwald 
430667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
43189425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
432667ec068SMatthias Ringwald }
433667ec068SMatthias Ringwald 
43476cc1527SMatthias Ringwald /* state machines */
4353deb3ec6SMatthias Ringwald 
436a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
437a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
438a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
439aa4dd815SMatthias Ringwald     int done = 1;
440498a8121SMilanka Ringwald     log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state);
441a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4423deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
443aeb0f0feSMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_hf_codecs, &hfp_hf_codecs_nr);
444a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
445a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4463deb3ec6SMatthias Ringwald             break;
4473deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
448a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
449a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4503deb3ec6SMatthias Ringwald             break;
4513deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
452a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
453a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4543deb3ec6SMatthias Ringwald             break;
4553deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
456a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
457a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4583deb3ec6SMatthias Ringwald             break;
4593deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
460a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
461a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4623deb3ec6SMatthias Ringwald             break;
4633deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
464a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
465a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4663deb3ec6SMatthias Ringwald             break;
4673deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
468a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
469a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4703deb3ec6SMatthias Ringwald             break;
4713deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
472a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
473a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4743deb3ec6SMatthias Ringwald             break;
4753deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
476a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
477a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4783deb3ec6SMatthias Ringwald             break;
4793deb3ec6SMatthias Ringwald         default:
480aa4dd815SMatthias Ringwald             done = 0;
4813deb3ec6SMatthias Ringwald             break;
4823deb3ec6SMatthias Ringwald     }
4833deb3ec6SMatthias Ringwald     return done;
4843deb3ec6SMatthias Ringwald }
4853deb3ec6SMatthias Ringwald 
486ce263fc8SMatthias Ringwald 
487a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
488a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
489498a8121SMilanka Ringwald     if (hfp_connection->ok_pending){
490498a8121SMilanka Ringwald         return 0;
491498a8121SMilanka Ringwald     }
492ce263fc8SMatthias Ringwald     int done = 0;
493a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
494a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
495ce263fc8SMatthias Ringwald         done = 1;
496a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
497ce263fc8SMatthias Ringwald         return done;
498ce263fc8SMatthias Ringwald     };
499a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
500a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
501ce263fc8SMatthias Ringwald         done = 1;
502a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
503a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
504a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
505ce263fc8SMatthias Ringwald         return done;
506ce263fc8SMatthias Ringwald     }
507ce263fc8SMatthias Ringwald 
508a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
509ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
510a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
511a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
512a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
513ce263fc8SMatthias Ringwald             return 1;
514ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
515a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
516a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
517a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
518ce263fc8SMatthias Ringwald             return 1;
519ce263fc8SMatthias Ringwald         default:
520ce263fc8SMatthias Ringwald             break;
521ce263fc8SMatthias Ringwald     }
522ce263fc8SMatthias Ringwald 
523a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
524a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
525ce263fc8SMatthias Ringwald         done = 1;
526a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
527ce263fc8SMatthias Ringwald         return done;
528ce263fc8SMatthias Ringwald     }
529ce263fc8SMatthias Ringwald 
530ce263fc8SMatthias Ringwald     return done;
531ce263fc8SMatthias Ringwald }
532ce263fc8SMatthias Ringwald 
533af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
534be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
535be55a11dSMilanka Ringwald         return 0;
536be55a11dSMilanka Ringwald     }
537be55a11dSMilanka Ringwald     int done = 0;
538fd4151d1SMilanka Ringwald 
5390b4debbfSMilanka Ringwald     if (hfp_connection->ok_pending == 1){
5400b4debbfSMilanka Ringwald         return 0;
5410b4debbfSMilanka Ringwald     }
5420b4debbfSMilanka Ringwald     // voice recognition activated from AG
5430b4debbfSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){
5440b4debbfSMilanka Ringwald         switch(hfp_connection->vra_state_requested){
5450b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
5460b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
547de9e0ea7SMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
5480b4debbfSMilanka Ringwald                 // ignore AG command, continue to wait for OK
5490b4debbfSMilanka Ringwald                 return 0;
550cf75be85SMilanka Ringwald 
5510b4debbfSMilanka Ringwald             default:
552b95cac54SMilanka Ringwald                 if (hfp_connection->ag_vra_msg_length > 0){
553b95cac54SMilanka Ringwald                     hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection);
554b95cac54SMilanka Ringwald                     hfp_connection->ag_vra_msg_length = 0;
555b95cac54SMilanka Ringwald                     break;
556b95cac54SMilanka Ringwald                 }
557cf75be85SMilanka Ringwald                 switch(hfp_connection->ag_vra_state){
558cf75be85SMilanka Ringwald                     case HFP_VOICE_RECOGNITION_STATE_AG_READY:
559013cc750SMilanka Ringwald                         switch (hfp_connection->ag_vra_status){
560013cc750SMilanka Ringwald                             case 0:
5610b4debbfSMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
562013cc750SMilanka Ringwald                                 break;
563013cc750SMilanka Ringwald                             case 1:
564013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
565013cc750SMilanka Ringwald                                 break;
566013cc750SMilanka Ringwald                             case 2:
567013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
568013cc750SMilanka Ringwald                                 break;
569013cc750SMilanka Ringwald                             default:
570013cc750SMilanka Ringwald                                 break;
5710b4debbfSMilanka Ringwald                         }
5720b4debbfSMilanka Ringwald                         break;
573cf75be85SMilanka Ringwald                     default:
574cf75be85SMilanka Ringwald                         // state messages from AG
575cf75be85SMilanka Ringwald                         hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
5768d5005b5SMilanka Ringwald                         hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
577cf75be85SMilanka Ringwald                         break;
578cf75be85SMilanka Ringwald                 }
579cf75be85SMilanka Ringwald                 break;
5800b4debbfSMilanka Ringwald         }
5810b4debbfSMilanka Ringwald         hfp_connection->command = HFP_CMD_NONE;
5820b4debbfSMilanka Ringwald     }
5830b4debbfSMilanka Ringwald 
5840b4debbfSMilanka Ringwald 
585498a8121SMilanka Ringwald     switch (hfp_connection->vra_state_requested){
586fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
587fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
588fdda66c0SMilanka Ringwald             if (done != 0){
589fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
590498a8121SMilanka Ringwald                 hfp_connection->ok_pending = 1;
591498a8121SMilanka Ringwald             }
592fd4151d1SMilanka Ringwald             return 1;
593fd4151d1SMilanka Ringwald 
594fd4151d1SMilanka Ringwald 
595fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
596fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
597fdda66c0SMilanka Ringwald             if (done != 0){
598fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
599fd4151d1SMilanka Ringwald                 hfp_connection->ok_pending = 1;
600fd4151d1SMilanka Ringwald                 return 1;
6010b4debbfSMilanka Ringwald             }
6020b4debbfSMilanka Ringwald             break;
603013cc750SMilanka Ringwald 
604de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
605de9e0ea7SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
606de9e0ea7SMilanka Ringwald             if (done != 0){
607de9e0ea7SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
608de9e0ea7SMilanka Ringwald                 hfp_connection->ok_pending = 1;
609de9e0ea7SMilanka Ringwald                 return 1;
610de9e0ea7SMilanka Ringwald             }
611de9e0ea7SMilanka Ringwald             break;
612de9e0ea7SMilanka Ringwald 
613de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
614de9e0ea7SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
615de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
616de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
617de9e0ea7SMilanka Ringwald             if (hfp_connection->activate_voice_recognition){
618fcf4ede6SMilanka Ringwald                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
619de9e0ea7SMilanka Ringwald                 hfp_hf_activate_voice_recognition(hfp_connection->acl_handle);
620de9e0ea7SMilanka Ringwald             } else {
621553a4a56SMilanka Ringwald                 hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
622de9e0ea7SMilanka Ringwald             }
623de9e0ea7SMilanka Ringwald             break;
624de9e0ea7SMilanka Ringwald 
625be55a11dSMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
626498a8121SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
627498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
628de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
629de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
630de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
631de9e0ea7SMilanka Ringwald             } else {
632fcf4ede6SMilanka Ringwald                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
63384fb9ac1SMilanka Ringwald                 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
634553a4a56SMilanka Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
63584fb9ac1SMilanka Ringwald                 } else {
63684fb9ac1SMilanka Ringwald                     // postpone VRA event to simplify application logic
63784fb9ac1SMilanka Ringwald                     hfp_connection->emit_vra_enabled_after_audio_established = true;
63884fb9ac1SMilanka Ringwald                 }
639de9e0ea7SMilanka Ringwald             }
640be55a11dSMilanka Ringwald             break;
641be55a11dSMilanka Ringwald 
642de9e0ea7SMilanka Ringwald 
643013cc750SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
644013cc750SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
645498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
646de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
647de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
648de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
649de9e0ea7SMilanka Ringwald             } else {
650de9e0ea7SMilanka Ringwald                 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
651de9e0ea7SMilanka Ringwald             }
652be55a11dSMilanka Ringwald             break;
653fd4151d1SMilanka Ringwald 
654be55a11dSMilanka Ringwald         default:
655be55a11dSMilanka Ringwald             break;
656be55a11dSMilanka Ringwald     }
657be55a11dSMilanka Ringwald     return done;
658be55a11dSMilanka Ringwald }
659be55a11dSMilanka Ringwald 
660be55a11dSMilanka Ringwald 
661be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
662a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
663ce263fc8SMatthias Ringwald 
664332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
665332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
666ce263fc8SMatthias Ringwald 
667a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
668a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
669332ca98fSMatthias Ringwald 		return 1;
670332ca98fSMatthias Ringwald     }
671332ca98fSMatthias Ringwald 
6721cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
6731cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
674ce263fc8SMatthias Ringwald 
675a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
676fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
6771cc65c4fSMatthias Ringwald 		return 1;
6781cc65c4fSMatthias Ringwald     }
6791cc65c4fSMatthias Ringwald 
6801cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
6811cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
6821cc65c4fSMatthias Ringwald 
683a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
684a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
6851cc65c4fSMatthias Ringwald 		return 1;
6861cc65c4fSMatthias Ringwald     }
687ce263fc8SMatthias Ringwald 
688ce263fc8SMatthias Ringwald     return 0;
689ce263fc8SMatthias Ringwald }
690ce263fc8SMatthias Ringwald 
691a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
692505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
693505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
694ce263fc8SMatthias Ringwald 
69564f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
696a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
697a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
698a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
699ce263fc8SMatthias Ringwald         return 1;
700ce263fc8SMatthias Ringwald     }
701ce263fc8SMatthias Ringwald 
702a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
703ce263fc8SMatthias Ringwald 
704ce263fc8SMatthias Ringwald     // run codecs exchange
705a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
706ce263fc8SMatthias Ringwald     if (done) return 1;
707ce263fc8SMatthias Ringwald 
70838200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
70938200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
71038200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
71138200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
71238200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
71338200c1dSMilanka Ringwald         return 1;
71438200c1dSMilanka Ringwald     }
715ce263fc8SMatthias Ringwald     return 0;
716ce263fc8SMatthias Ringwald }
717ce263fc8SMatthias Ringwald 
71838200c1dSMilanka Ringwald 
719a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
720eaf2b0a1SMatthias Ringwald 
721eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
722eaf2b0a1SMatthias Ringwald 
723a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
724a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
725a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
726ce263fc8SMatthias Ringwald         return 1;
727ce263fc8SMatthias Ringwald     }
728ce263fc8SMatthias Ringwald     return 0;
729ce263fc8SMatthias Ringwald }
730ce263fc8SMatthias Ringwald 
7311c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
7327522e673SMatthias Ringwald 
73376cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
73476cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
73576cc1527SMatthias Ringwald 
73676cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
73776cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
73822387625SMatthias Ringwald 
73984fb9ac1SMilanka Ringwald     // emit postponed VRA event
74084fb9ac1SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){
74184fb9ac1SMilanka Ringwald         hfp_connection->emit_vra_enabled_after_audio_established = false;
74284fb9ac1SMilanka Ringwald         hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
74384fb9ac1SMilanka Ringwald     }
74484fb9ac1SMilanka Ringwald 
7453721a235SMatthias Ringwald 	// assert command could be sent
7463721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
7473721a235SMatthias Ringwald 
7483721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
7493721a235SMatthias Ringwald     // WBS Disassociate
7503721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
7513721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
7523721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
7533721a235SMatthias Ringwald         return;
7543721a235SMatthias Ringwald     }
7553721a235SMatthias Ringwald     // Write Codec Config
7563721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
7573721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
7583721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
7593721a235SMatthias Ringwald         return;
7603721a235SMatthias Ringwald     }
7613721a235SMatthias Ringwald     // WBS Associate
7623721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
7633721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
7643721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
7653721a235SMatthias Ringwald         return;
7663721a235SMatthias Ringwald     }
7673721a235SMatthias Ringwald #endif
768689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
769689d4323SMatthias Ringwald     // Enable WBS
770689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
771689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
772689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
773689d4323SMatthias Ringwald         return;
774689d4323SMatthias Ringwald     }
775689d4323SMatthias Ringwald     // Write I2S/PCM params
776689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
777689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
778689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
779689d4323SMatthias Ringwald         return;
780689d4323SMatthias Ringwald     }
781689d4323SMatthias Ringwald     // Disable WBS
782689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
783689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
784689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
785689d4323SMatthias Ringwald         return;
786689d4323SMatthias Ringwald     }
787689d4323SMatthias Ringwald #endif
788*2b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS
789*2b5f92fdSMatthias Ringwald     if (hfp_connection->rtk_send_sco_config){
790*2b5f92fdSMatthias Ringwald         hfp_connection->rtk_send_sco_config = false;
791*2b5f92fdSMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
792*2b5f92fdSMatthias Ringwald             log_info("RTK SCO: 16k + mSBC");
793*2b5f92fdSMatthias Ringwald             hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x00, 0x00, 0x41);
794*2b5f92fdSMatthias Ringwald         } else {
795*2b5f92fdSMatthias Ringwald             log_info("RTK SCO: 16k + CVSD");
796*2b5f92fdSMatthias Ringwald             hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x0c, 0x00, 0x01);
797*2b5f92fdSMatthias Ringwald         }
798*2b5f92fdSMatthias Ringwald         return;
799*2b5f92fdSMatthias Ringwald     }
800*2b5f92fdSMatthias Ringwald #endif
80148e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
80248e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
80348e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
80448e6eeeeSMatthias Ringwald         return;
80548e6eeeeSMatthias Ringwald     }
80648e6eeeeSMatthias Ringwald #endif
8073721a235SMatthias Ringwald 
808cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
809cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
810cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
8117522e673SMatthias Ringwald         // notify about codec selection if not done already
8127522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
8137522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
8147522e673SMatthias Ringwald         }
815cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
8167522e673SMatthias Ringwald         return;
8177522e673SMatthias Ringwald     }
8187522e673SMatthias Ringwald 
819d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
820d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
821d4dd47ffSMatthias Ringwald         return;
822d4dd47ffSMatthias Ringwald     }
823a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
824ce263fc8SMatthias Ringwald     if (!done){
825a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
826ce263fc8SMatthias Ringwald     }
827ce263fc8SMatthias Ringwald     if (!done){
828c95b5b3cSMilanka Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
829be55a11dSMilanka Ringwald     }
830be55a11dSMilanka Ringwald     if (!done){
831c95b5b3cSMilanka Ringwald         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
832ce263fc8SMatthias Ringwald     }
833ce263fc8SMatthias Ringwald     if (!done){
834a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
835ce263fc8SMatthias Ringwald     }
836ce263fc8SMatthias Ringwald 
837e2c3b58dSMilanka Ringwald     // don't send a new command while ok still pending or SLC is not established
838e2c3b58dSMilanka Ringwald     if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){
839e2c3b58dSMilanka Ringwald         return;
840e2c3b58dSMilanka Ringwald     }
8411016a228SMatthias Ringwald 
842a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
843a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
844a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
845a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
846ce263fc8SMatthias Ringwald         return;
847ce263fc8SMatthias Ringwald     }
848ce263fc8SMatthias Ringwald 
849a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
850a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
851a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
852a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
853ce263fc8SMatthias Ringwald         return;
854ce263fc8SMatthias Ringwald     }
855ce263fc8SMatthias Ringwald 
856a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
857a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
858a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
859a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
860ce263fc8SMatthias Ringwald         return;
861ce263fc8SMatthias Ringwald     }
862ce263fc8SMatthias Ringwald 
863a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
864a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
865a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
866a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
867ce263fc8SMatthias Ringwald         return;
868ce263fc8SMatthias Ringwald     }
869ce263fc8SMatthias Ringwald 
870a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
871a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
87299af1e28SMilanka Ringwald         hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR;
873a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
874e2c3b58dSMilanka Ringwald         hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0);
875ce263fc8SMatthias Ringwald         return;
876ce263fc8SMatthias Ringwald     }
877ce263fc8SMatthias Ringwald 
878a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
879a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
880a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
881a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
882ce263fc8SMatthias Ringwald         return;
883ce263fc8SMatthias Ringwald     }
884ce263fc8SMatthias Ringwald 
885a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
886a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
887a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
888a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
889ce263fc8SMatthias Ringwald         return;
890ce263fc8SMatthias Ringwald     }
891ce263fc8SMatthias Ringwald 
892a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
893a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
894a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
895a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
896ce263fc8SMatthias Ringwald         return;
897ce263fc8SMatthias Ringwald     }
898ce263fc8SMatthias Ringwald 
899a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
900a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
901a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
902a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
903ce263fc8SMatthias Ringwald         return;
904ce263fc8SMatthias Ringwald     }
905ce263fc8SMatthias Ringwald 
906a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
907a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
908a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
909a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
910ce263fc8SMatthias Ringwald         return;
911ce263fc8SMatthias Ringwald     }
912ce263fc8SMatthias Ringwald 
913a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
914a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
915a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
916a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
917ce263fc8SMatthias Ringwald         return;
918ce263fc8SMatthias Ringwald     }
919ce263fc8SMatthias Ringwald 
920a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
921a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
922a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
923a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
924ce263fc8SMatthias Ringwald         return;
925ce263fc8SMatthias Ringwald     }
926ce263fc8SMatthias Ringwald 
927a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
928a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
929a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
930a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
931ce263fc8SMatthias Ringwald         return;
932ce263fc8SMatthias Ringwald     }
933ce263fc8SMatthias Ringwald 
934a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
935a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
936a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
937a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
938ce263fc8SMatthias Ringwald         return;
939ce263fc8SMatthias Ringwald     }
940ce263fc8SMatthias Ringwald 
941a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
942a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
943a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
944a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
945ce263fc8SMatthias Ringwald         return;
946ce263fc8SMatthias Ringwald     }
947ce263fc8SMatthias Ringwald 
948a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
949a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
950a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
951a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
952ce263fc8SMatthias Ringwald         return;
953ce263fc8SMatthias Ringwald     }
954ce263fc8SMatthias Ringwald 
955a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
956a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
957a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
958a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
959667ec068SMatthias Ringwald         return;
960667ec068SMatthias Ringwald     }
961667ec068SMatthias Ringwald 
962a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
963a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
964a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
965a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
966a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
967ce263fc8SMatthias Ringwald         return;
968ce263fc8SMatthias Ringwald     }
969ce263fc8SMatthias Ringwald 
970a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
971a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
972a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
973a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
974ce263fc8SMatthias Ringwald         return;
975ce263fc8SMatthias Ringwald     }
976ce263fc8SMatthias Ringwald 
977a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
978a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
979a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
980a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
981667ec068SMatthias Ringwald         return;
982667ec068SMatthias Ringwald     }
983667ec068SMatthias Ringwald 
984a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
985a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
986667ec068SMatthias Ringwald         char buffer[20];
987a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
988667ec068SMatthias Ringwald             case '?':
9891599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
990ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
991ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
992a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
993667ec068SMatthias Ringwald                 return;
994667ec068SMatthias Ringwald             case '0':
995667ec068SMatthias Ringwald             case '1':
996667ec068SMatthias Ringwald             case '2':
9971599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
998ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
999ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
1000ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
1001a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1002667ec068SMatthias Ringwald                 return;
1003667ec068SMatthias Ringwald             default:
1004667ec068SMatthias Ringwald                 break;
1005667ec068SMatthias Ringwald         }
1006667ec068SMatthias Ringwald         return;
1007667ec068SMatthias Ringwald     }
1008667ec068SMatthias Ringwald 
1009a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
1010a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
1011667ec068SMatthias Ringwald         char buffer[20];
10121599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
1013ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
1014ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
1015a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1016667ec068SMatthias Ringwald         return;
1017667ec068SMatthias Ringwald     }
1018667ec068SMatthias Ringwald 
1019667ec068SMatthias Ringwald     // update HF indicators
1020a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
1021667ec068SMatthias Ringwald         int i;
1022aeb0f0feSMatthias Ringwald         for (i=0; i < hfp_hf_indicators_nr; i++){
1023a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
1024a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
1025a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
1026a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
1027667ec068SMatthias Ringwald                     char buffer[30];
10281599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
1029ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
1030aeb0f0feSMatthias Ringwald                              hfp_hf_indicators[i],
1031aeb0f0feSMatthias Ringwald                              (unsigned int)hfp_hf_indicators_value[i]);
1032ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
1033a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1034667ec068SMatthias Ringwald                 } else {
1035aeb0f0feSMatthias Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]);
1036667ec068SMatthias Ringwald                 }
1037667ec068SMatthias Ringwald                 return;
1038667ec068SMatthias Ringwald             }
1039667ec068SMatthias Ringwald         }
1040667ec068SMatthias Ringwald     }
1041667ec068SMatthias Ringwald 
1042ce263fc8SMatthias Ringwald     if (done) return;
1043ce263fc8SMatthias Ringwald     // deal with disconnect
1044a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
1045ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
1046a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1047a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
1048ce263fc8SMatthias Ringwald             break;
1049ce263fc8SMatthias Ringwald 
1050ce263fc8SMatthias Ringwald         default:
1051ce263fc8SMatthias Ringwald             break;
1052ce263fc8SMatthias Ringwald     }
1053ce263fc8SMatthias Ringwald }
1054ce263fc8SMatthias Ringwald 
1055a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
1056a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
10576a7f44bdSMilanka Ringwald 
1058ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
10597522e673SMatthias Ringwald 
1060184a03edSMilanka Ringwald     uint8_t i;
1061184a03edSMilanka Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
10621ac1f60fSMilanka Ringwald         hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1063184a03edSMilanka Ringwald     }
1064722a85f3SMilanka Ringwald 
1065722a85f3SMilanka Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1066722a85f3SMilanka Ringwald         hfp_connection->ag_indicators[i].status_changed = 0;
1067722a85f3SMilanka Ringwald         hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1068722a85f3SMilanka Ringwald     }
1069722a85f3SMilanka Ringwald 
1070667ec068SMatthias Ringwald     // restore volume settings
1071a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
1072a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1073ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
1074a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
1075a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1076ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
1077667ec068SMatthias Ringwald     // enable all indicators
1078aeb0f0feSMatthias Ringwald     for (i=0; i < hfp_hf_indicators_nr; i++){
1079aeb0f0feSMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i];
1080a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
1081667ec068SMatthias Ringwald     }
1082ce263fc8SMatthias Ringwald }
1083ce263fc8SMatthias Ringwald 
10841cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
1085aeb0f0feSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){
10861cc65c4fSMatthias Ringwald 		// Codec supported, confirm
10871cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
10881cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
10891cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
10901cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
10911cc65c4fSMatthias Ringwald 
10921cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
10931cc65c4fSMatthias Ringwald 	} else {
10941cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
10951cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
10961cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
10971cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
10981cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10991cc65c4fSMatthias Ringwald 
11001cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
11011cc65c4fSMatthias Ringwald 	}
11021cc65c4fSMatthias Ringwald }
11031cc65c4fSMatthias Ringwald 
1104e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){
1105e2c3b58dSMilanka Ringwald     bool event_emited = true;
1106e2c3b58dSMilanka Ringwald 
110799af1e28SMilanka Ringwald     switch (hfp_connection->response_pending_for_command){
1108e2c3b58dSMilanka Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1109e2c3b58dSMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status);
1110e2c3b58dSMilanka Ringwald             break;
1111e2c3b58dSMilanka Ringwald         default:
1112e2c3b58dSMilanka Ringwald             event_emited = false;
1113e2c3b58dSMilanka Ringwald 
1114a0ffb263SMatthias Ringwald             switch (hfp_connection->state){
11153deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
1116a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
1117a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_NOTIFY_ON_CODECS;
11183deb3ec6SMatthias Ringwald                         break;
11193deb3ec6SMatthias Ringwald                     }
1120a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
11213deb3ec6SMatthias Ringwald                     break;
11223deb3ec6SMatthias Ringwald 
11233deb3ec6SMatthias Ringwald                 case HFP_W4_NOTIFY_ON_CODECS:
1124a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
11253deb3ec6SMatthias Ringwald                     break;
11263deb3ec6SMatthias Ringwald 
11273deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS:
1128a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
11293deb3ec6SMatthias Ringwald                     break;
11303deb3ec6SMatthias Ringwald 
11313deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1132a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
11333deb3ec6SMatthias Ringwald                     break;
11343deb3ec6SMatthias Ringwald 
11353deb3ec6SMatthias Ringwald                 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1136a0ffb263SMatthias Ringwald                     if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1137a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
11383deb3ec6SMatthias Ringwald                         break;
11393deb3ec6SMatthias Ringwald                     }
1140a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1141a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
11423deb3ec6SMatthias Ringwald                         break;
11433deb3ec6SMatthias Ringwald                     }
1144a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11453deb3ec6SMatthias Ringwald                     break;
11463deb3ec6SMatthias Ringwald 
11473deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1148a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1149a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
11503deb3ec6SMatthias Ringwald                         break;
11513deb3ec6SMatthias Ringwald                     }
1152a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11533deb3ec6SMatthias Ringwald                     break;
11543deb3ec6SMatthias Ringwald 
11553deb3ec6SMatthias Ringwald                 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1156a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
11573deb3ec6SMatthias Ringwald                     break;
11583deb3ec6SMatthias Ringwald 
11593deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1160a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
11613deb3ec6SMatthias Ringwald                     break;
11623deb3ec6SMatthias Ringwald 
11633deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1164a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11653deb3ec6SMatthias Ringwald                     break;
1166ce263fc8SMatthias Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1167a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1168a0ffb263SMatthias Ringwald                         hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1169ca59be51SMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1170ce263fc8SMatthias Ringwald                         break;
1171ce263fc8SMatthias Ringwald                     }
11723deb3ec6SMatthias Ringwald 
1173a0ffb263SMatthias Ringwald                     if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1174a0ffb263SMatthias Ringwald                         hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1175ca59be51SMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1176ce263fc8SMatthias Ringwald                         break;
11773deb3ec6SMatthias Ringwald                     }
11783deb3ec6SMatthias Ringwald 
1179a0ffb263SMatthias Ringwald                     switch (hfp_connection->hf_query_operator_state){
1180ce263fc8SMatthias Ringwald                         case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1181a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1182ce263fc8SMatthias Ringwald                             break;
1183ce263fc8SMatthias Ringwald                         case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1184a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1185a473a009SMatthias Ringwald                             hfp_emit_network_operator_event(hfp_connection);
1186ce263fc8SMatthias Ringwald                             break;
1187ce263fc8SMatthias Ringwald                         default:
1188ce263fc8SMatthias Ringwald                             break;
11893deb3ec6SMatthias Ringwald                     }
1190ce263fc8SMatthias Ringwald 
1191a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_extended_audio_gateway_error_report){
1192a0ffb263SMatthias Ringwald                         hfp_connection->enable_extended_audio_gateway_error_report = 0;
1193ce263fc8SMatthias Ringwald                         break;
11943deb3ec6SMatthias Ringwald                     }
11953deb3ec6SMatthias Ringwald 
1196a0ffb263SMatthias Ringwald                     switch (hfp_connection->codecs_state){
1197aa4dd815SMatthias Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1198a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
11993deb3ec6SMatthias Ringwald                             break;
1200ce263fc8SMatthias Ringwald                         case HFP_CODECS_HF_CONFIRMED_CODEC:
1201a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1202ce263fc8SMatthias Ringwald                             break;
12033deb3ec6SMatthias Ringwald                         default:
12043deb3ec6SMatthias Ringwald                             break;
12053deb3ec6SMatthias Ringwald                     }
1206af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
1207be55a11dSMilanka Ringwald                     break;
1208be55a11dSMilanka Ringwald                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
1209af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
12103deb3ec6SMatthias Ringwald                     break;
12113deb3ec6SMatthias Ringwald                 default:
12123deb3ec6SMatthias Ringwald                     break;
12133deb3ec6SMatthias Ringwald             }
1214e2c3b58dSMilanka Ringwald             break;
1215e2c3b58dSMilanka Ringwald     }
12163deb3ec6SMatthias Ringwald 
12173deb3ec6SMatthias Ringwald     // done
121899af1e28SMilanka Ringwald     hfp_connection->response_pending_for_command = HFP_CMD_NONE;
1219be55a11dSMilanka Ringwald     hfp_connection->ok_pending = 0;
1220a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
1221e2c3b58dSMilanka Ringwald     return event_emited;
12223deb3ec6SMatthias Ringwald }
12233deb3ec6SMatthias Ringwald 
1224a03dbc20SMilanka Ringwald static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){
1225a03dbc20SMilanka Ringwald     switch (callsetup_status){
1226a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1227a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1228a03dbc20SMilanka Ringwald             return true;
1229a03dbc20SMilanka Ringwald         default:
1230a03dbc20SMilanka Ringwald             return false;
1231a03dbc20SMilanka Ringwald    }
1232a03dbc20SMilanka Ringwald }
1233be55a11dSMilanka Ringwald 
1234b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
12354562e2a2SMatthias Ringwald     uint16_t i;
1236a03dbc20SMilanka Ringwald 
12374562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
12384562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
12394562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1240a03dbc20SMilanka Ringwald                 hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1241a03dbc20SMilanka Ringwald                 bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status);
1242a03dbc20SMilanka Ringwald                 bool ringing_new = hfp_is_ringing(new_hf_callsetup_status);
1243a03dbc20SMilanka Ringwald                 if (ringing_old != ringing_new){
1244a03dbc20SMilanka Ringwald                     if (ringing_new){
1245a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING);
1246a03dbc20SMilanka Ringwald                     } else {
1247a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING);
1248a03dbc20SMilanka Ringwald                     }
1249a03dbc20SMilanka Ringwald                 }
1250a03dbc20SMilanka Ringwald                 hfp_hf_callsetup_status = new_hf_callsetup_status;
12514562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1252aeb0f0feSMatthias Ringwald                 hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
12534562e2a2SMatthias Ringwald                 // avoid set but not used warning
1254aeb0f0feSMatthias Ringwald                 (void) hfp_hf_callheld_status;
12554562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1256674ebed5SMilanka Ringwald                 hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1257674ebed5SMilanka Ringwald                 if (hfp_hf_call_status != new_hf_call_status){
1258674ebed5SMilanka Ringwald                     if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){
1259674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1260674ebed5SMilanka Ringwald                     } else {
1261674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1262674ebed5SMilanka Ringwald                     }
1263674ebed5SMilanka Ringwald                 }
1264674ebed5SMilanka Ringwald                 hfp_hf_call_status = new_hf_call_status;
12654562e2a2SMatthias Ringwald             }
12664562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1267ce3797e1SMilanka Ringwald             hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]);
12684562e2a2SMatthias Ringwald             break;
12694562e2a2SMatthias Ringwald         }
12704562e2a2SMatthias Ringwald     }
12714562e2a2SMatthias Ringwald }
12724562e2a2SMatthias Ringwald 
1273426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1274186dd3d2SMatthias Ringwald     int value;
1275186dd3d2SMatthias Ringwald     int i;
1276e2c3b58dSMilanka Ringwald     bool event_emited;
1277e2c3b58dSMilanka Ringwald 
1278a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1279667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1280a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1281a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1282667ec068SMatthias Ringwald             break;
1283667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1284a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1285ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1286667ec068SMatthias Ringwald             break;
1287667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1288a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1289a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1290667ec068SMatthias Ringwald             break;
1291ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1292a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12932308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1294667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1295ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1296ce263fc8SMatthias Ringwald             break;
1297ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1298a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12992308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1300667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1301ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1302ce263fc8SMatthias Ringwald             break;
1303ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1304a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1305ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1306a0ffb263SMatthias Ringwald             break;
1307a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1308a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1309a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1310a0ffb263SMatthias Ringwald             break;
1311a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1312a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1313a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1314ce263fc8SMatthias Ringwald             break;
1315ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1316a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13175a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1318a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1319ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1320ce263fc8SMatthias Ringwald             break;
13210b4debbfSMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
13220b4debbfSMilanka Ringwald             break;
1323fdda66c0SMilanka Ringwald         case HFP_CMD_ERROR:
132490244c92SMilanka Ringwald             switch (hfp_connection->state){
132590244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
132690244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
132790244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1328fdda66c0SMilanka Ringwald                             hfp_reset_context_flags(hfp_connection);
132990244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
133090244c92SMilanka Ringwald                             return;
133190244c92SMilanka Ringwald                         default:
133290244c92SMilanka Ringwald                             break;
133390244c92SMilanka Ringwald                     }
133456f1adacSMilanka Ringwald                     break;
133556f1adacSMilanka Ringwald                 default:
133656f1adacSMilanka Ringwald                     break;
133756f1adacSMilanka Ringwald             }
1338e2c3b58dSMilanka Ringwald 
1339fdda66c0SMilanka Ringwald             // handle error response for voice activation (HF initiated)
13400b4debbfSMilanka Ringwald             switch(hfp_connection->vra_state_requested){
1341de9e0ea7SMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1342de9e0ea7SMilanka Ringwald                     hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1343be55a11dSMilanka Ringwald                     break;
1344be55a11dSMilanka Ringwald                 default:
1345e2c3b58dSMilanka Ringwald                     if (hfp_connection->vra_state_requested == hfp_connection->vra_state){
1346e2c3b58dSMilanka Ringwald                         break;
1347e2c3b58dSMilanka Ringwald                     }
13480b4debbfSMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
1349553a4a56SMilanka Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
13500b4debbfSMilanka Ringwald                     hfp_reset_context_flags(hfp_connection);
13510b4debbfSMilanka Ringwald                     return;
1352be55a11dSMilanka Ringwald             }
1353e2c3b58dSMilanka Ringwald             event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1354e2c3b58dSMilanka Ringwald             if (!event_emited){
13550b4debbfSMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1356e2c3b58dSMilanka Ringwald             }
1357fdda66c0SMilanka Ringwald             hfp_reset_context_flags(hfp_connection);
1358ce263fc8SMatthias Ringwald             break;
1359fdda66c0SMilanka Ringwald 
1360ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1361e2c3b58dSMilanka Ringwald             hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS);
1362ce263fc8SMatthias Ringwald             break;
1363ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
13645a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1365ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1366ce263fc8SMatthias Ringwald             break;
1367ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
13685a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13694562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1370ce263fc8SMatthias Ringwald             break;
1371c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
13725a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1373184a03edSMilanka Ringwald             // report status after SLC established
1374184a03edSMilanka Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
1375184a03edSMilanka Ringwald                 break;
1376184a03edSMilanka Ringwald             }
1377c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
13781ac1f60fSMilanka Ringwald                 hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1379c741b032SMilanka Ringwald             }
1380c741b032SMilanka Ringwald             break;
13811cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
13821cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13835a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
13841cc65c4fSMatthias Ringwald 			break;
1385eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1386eac56539SMilanka 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));
1387ce263fc8SMatthias Ringwald         default:
1388ce263fc8SMatthias Ringwald             break;
13893deb3ec6SMatthias Ringwald     }
13900cef86faSMatthias Ringwald }
1391426f9988SMatthias Ringwald 
139276cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
139376cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
139476cc1527SMatthias Ringwald }
139576cc1527SMatthias Ringwald 
13960b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1397426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1398426f9988SMatthias Ringwald     if (size < 1) return;
1399426f9988SMatthias Ringwald 
1400426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1401e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1402e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1403e43d1938SMatthias Ringwald #endif
1404426f9988SMatthias Ringwald 
1405426f9988SMatthias Ringwald     // process messages byte-wise
1406a7ba78b0SMilanka Ringwald     uint8_t pos;
1407426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1408426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
14091599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1410426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
14110b4debbfSMilanka Ringwald         hfp_hf_handle_rfcomm_command(hfp_connection);
14123deb3ec6SMatthias Ringwald     }
1413a7ba78b0SMilanka Ringwald }
14143deb3ec6SMatthias Ringwald 
14151c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1416665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1417665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1418665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1419a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
142022387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
14211c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
14223deb3ec6SMatthias Ringwald     }
14233deb3ec6SMatthias Ringwald }
14243deb3ec6SMatthias Ringwald 
14251c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
14260b4debbfSMilanka Ringwald     hfp_connection_t * hfp_connection;
14273deb3ec6SMatthias Ringwald     switch (packet_type){
14283deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
14290b4debbfSMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
14300b4debbfSMilanka Ringwald             if (!hfp_connection) return;
14310b4debbfSMilanka Ringwald             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
14320b4debbfSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
14330b4debbfSMilanka Ringwald             return;
14343deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1435d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1436d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
14371c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1438d4dd47ffSMatthias Ringwald                 return;
1439d4dd47ffSMatthias Ringwald             }
144027950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1441202c8a4cSMatthias Ringwald             break;
14423deb3ec6SMatthias Ringwald         default:
14433deb3ec6SMatthias Ringwald             break;
14443deb3ec6SMatthias Ringwald     }
14451c6a0fc0SMatthias Ringwald     hfp_hf_run();
14463deb3ec6SMatthias Ringwald }
14473deb3ec6SMatthias Ringwald 
14481c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1449405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
14501c6a0fc0SMatthias Ringwald     hfp_hf_run();
1451405014fbSMatthias Ringwald }
1452405014fbSMatthias Ringwald 
1453aeb0f0feSMatthias Ringwald static void hfp_hf_set_defaults(void){
1454aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1455aeb0f0feSMatthias Ringwald     hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
1456aeb0f0feSMatthias Ringwald     hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1457aeb0f0feSMatthias Ringwald     hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1458aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = 0;
1459aeb0f0feSMatthias Ringwald     hfp_hf_speaker_gain = 9;
1460aeb0f0feSMatthias Ringwald     hfp_hf_microphone_gain = 9;
1461aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = 0;
1462aeb0f0feSMatthias Ringwald }
1463aeb0f0feSMatthias Ringwald 
1464b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1465b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1466b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1467b4df8028SMilanka Ringwald         return status;
1468b4df8028SMilanka Ringwald     }
1469b4df8028SMilanka Ringwald 
1470520c92d5SMatthias Ringwald     hfp_init();
1471aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1472d63c37a1SMatthias Ringwald 
14731c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
14741c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
147527950165SMatthias Ringwald 
147627950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
14771c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1478b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
147920b2edb6SMatthias Ringwald }
148027950165SMatthias Ringwald 
148120b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
148220b2edb6SMatthias Ringwald     hfp_deinit();
1483aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1484aeb0f0feSMatthias Ringwald 
1485aeb0f0feSMatthias Ringwald     hfp_hf_callback = NULL;
148620b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
1487aeb0f0feSMatthias Ringwald     (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number));
1488a0ffb263SMatthias Ringwald }
1489a0ffb263SMatthias Ringwald 
14907ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
149168466199SMilanka Ringwald     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
14923deb3ec6SMatthias Ringwald 
1493aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = codecs_nr;
14943deb3ec6SMatthias Ringwald     int i;
14953deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
1496aeb0f0feSMatthias Ringwald         hfp_hf_codecs[i] = codecs[i];
14973deb3ec6SMatthias Ringwald     }
14983deb3ec6SMatthias Ringwald }
14993deb3ec6SMatthias Ringwald 
1500a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
1501aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = supported_features;
1502a0ffb263SMatthias Ringwald }
15033deb3ec6SMatthias Ringwald 
15047ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
1505aeb0f0feSMatthias Ringwald     btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS);
150668466199SMilanka Ringwald 
1507aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = indicators_nr;
15083deb3ec6SMatthias Ringwald     int i;
1509aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
1510aeb0f0feSMatthias Ringwald         hfp_hf_indicators[i] = indicators[i];
15113deb3ec6SMatthias Ringwald     }
15123deb3ec6SMatthias Ringwald }
15133deb3ec6SMatthias Ringwald 
15144eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
15154eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
15163deb3ec6SMatthias Ringwald }
15173deb3ec6SMatthias Ringwald 
1518657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
15199c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1520a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1521657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1522a33eb0c4SMilanka Ringwald     }
15231ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
15241c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1525657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
15263deb3ec6SMatthias Ringwald }
15273deb3ec6SMatthias Ringwald 
15283c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
15299c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1530a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15313c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15323deb3ec6SMatthias Ringwald     }
1533a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
15341c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15353c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15363deb3ec6SMatthias Ringwald }
15373deb3ec6SMatthias Ringwald 
15383c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
15393c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1540ce263fc8SMatthias Ringwald }
1541ce263fc8SMatthias Ringwald 
15423c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
15433c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1544ce263fc8SMatthias Ringwald }
1545ce263fc8SMatthias Ringwald 
15463deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
15473c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
15489c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1549a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15503c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15513deb3ec6SMatthias Ringwald     }
1552a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1553a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
15541c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15553c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15563deb3ec6SMatthias Ringwald }
15573deb3ec6SMatthias Ringwald 
15583c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
15599c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1560a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15613c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15623deb3ec6SMatthias Ringwald     }
15633c65e705SMilanka Ringwald 
1564a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1565ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1566a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1567ce263fc8SMatthias Ringwald             break;
1568ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1569a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1570ce263fc8SMatthias Ringwald             break;
1571ce263fc8SMatthias Ringwald         default:
15723c65e705SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1573ce263fc8SMatthias Ringwald     }
15741c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15753c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15763deb3ec6SMatthias Ringwald }
15773deb3ec6SMatthias Ringwald 
15783c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
15799c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1580a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15813c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15823deb3ec6SMatthias Ringwald     }
1583a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
15841c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15853c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15863deb3ec6SMatthias Ringwald }
15873deb3ec6SMatthias Ringwald 
1588ce263fc8SMatthias Ringwald 
15893c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
15903c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1591ce263fc8SMatthias Ringwald }
1592ce263fc8SMatthias Ringwald 
15933c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
15943c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1595ce263fc8SMatthias Ringwald }
1596ce263fc8SMatthias Ringwald 
159738200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
1598aeb0f0feSMatthias Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4));
159938200c1dSMilanka Ringwald }
1600ce263fc8SMatthias Ringwald 
16013c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
16029c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1603a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16043c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1605a33eb0c4SMilanka Ringwald     }
1606ce263fc8SMatthias Ringwald 
16073c65e705SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
16083c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16093c65e705SMilanka Ringwald     }
16103c65e705SMilanka Ringwald 
16113c65e705SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
16123c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16133c65e705SMilanka Ringwald     }
1614f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1615a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1616aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1617aa4dd815SMatthias Ringwald                 break;
1618ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1619ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1620ec3bfc1aSMatthias Ringwald                 break;
1621aa4dd815SMatthias Ringwald             default:
16221cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
16231cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
16241cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
16251cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
162638200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1627aa4dd815SMatthias Ringwald                 break;
16283deb3ec6SMatthias Ringwald         }
1629f4412093SMatthias Ringwald     } else {
1630f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1631f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1632f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1633f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1634f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1635f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1636f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1637f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1638ce263fc8SMatthias Ringwald     }
1639ce263fc8SMatthias Ringwald 
16401c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16413c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16423deb3ec6SMatthias Ringwald }
16433deb3ec6SMatthias Ringwald 
16443c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
16459c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1646a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16473c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1648a33eb0c4SMilanka Ringwald     }
16490b4debbfSMilanka Ringwald     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
16500b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16510b4debbfSMilanka Ringwald     }
16520b4debbfSMilanka Ringwald     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
16530b4debbfSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
16541c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
16550b4debbfSMilanka Ringwald     }
16563c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16573deb3ec6SMatthias Ringwald }
16583deb3ec6SMatthias Ringwald 
16593c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
16609c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1661a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16623c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1663a33eb0c4SMilanka Ringwald     }
1664ce263fc8SMatthias Ringwald 
1665aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1666a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
16671c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1668ce263fc8SMatthias Ringwald     } else {
1669aeb0f0feSMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status);
16703c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1671ce263fc8SMatthias Ringwald     }
16723c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1673ce263fc8SMatthias Ringwald }
1674ce263fc8SMatthias Ringwald 
16753c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
16769c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1677a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16783c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1679a33eb0c4SMilanka Ringwald     }
1680a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
16811c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16823c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1683ce263fc8SMatthias Ringwald }
1684ce263fc8SMatthias Ringwald 
16853c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
16869c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1687a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16883c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1689a33eb0c4SMilanka Ringwald     }
1690ce263fc8SMatthias Ringwald 
1691aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1692a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
16931c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1694ce263fc8SMatthias Ringwald     }
16953c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1696ce263fc8SMatthias Ringwald }
1697ce263fc8SMatthias Ringwald 
16983c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){
16999c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1700a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17013c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1702a33eb0c4SMilanka Ringwald     }
1703ce263fc8SMatthias Ringwald 
1704aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1705a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
17061c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1707ce263fc8SMatthias Ringwald     }
17083c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1709ce263fc8SMatthias Ringwald }
1710ce263fc8SMatthias Ringwald 
17113c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
17129c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1713a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17143c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1715a33eb0c4SMilanka Ringwald     }
1716ce263fc8SMatthias Ringwald 
1717aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1718aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1719a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
17201c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1721ce263fc8SMatthias Ringwald     }
17223c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1723ce263fc8SMatthias Ringwald }
1724ce263fc8SMatthias Ringwald 
17253c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
17269c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1727a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17283c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1729a33eb0c4SMilanka Ringwald     }
1730ce263fc8SMatthias Ringwald 
1731aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1732aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1733a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
17341c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1735ce263fc8SMatthias Ringwald     }
17363c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1737ce263fc8SMatthias Ringwald }
1738ce263fc8SMatthias Ringwald 
17393c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
17409c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1741a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17423c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1743a33eb0c4SMilanka Ringwald     }
1744ce263fc8SMatthias Ringwald 
1745aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1746aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1747a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
17481c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1749ce263fc8SMatthias Ringwald     }
17503c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1751ce263fc8SMatthias Ringwald }
1752ce263fc8SMatthias Ringwald 
17533c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
17549c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1755a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17563c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1757a33eb0c4SMilanka Ringwald     }
1758ce263fc8SMatthias Ringwald 
1759aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1760aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1761a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
17621c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1763ce263fc8SMatthias Ringwald     }
17643c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1765ce263fc8SMatthias Ringwald }
1766ce263fc8SMatthias Ringwald 
17673c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
17689c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1769a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17703c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1771a33eb0c4SMilanka Ringwald     }
1772667ec068SMatthias Ringwald 
1773aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1774aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1775a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1776a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
17771c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1778667ec068SMatthias Ringwald     }
17793c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1780667ec068SMatthias Ringwald }
1781667ec068SMatthias Ringwald 
17823c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
17839c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1784a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17853c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1786a33eb0c4SMilanka Ringwald     }
1787667ec068SMatthias Ringwald 
1788aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1789aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1790a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1791a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
17921c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1793667ec068SMatthias Ringwald     }
17943c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1795667ec068SMatthias Ringwald }
1796ce263fc8SMatthias Ringwald 
17973c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
17989c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1799a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18003c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1801a33eb0c4SMilanka Ringwald     }
1802ce263fc8SMatthias Ringwald 
1803a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1804aeb0f0feSMatthias Ringwald     snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number);
18051c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18063c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1807ce263fc8SMatthias Ringwald }
1808ce263fc8SMatthias Ringwald 
18093c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
18109c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1811a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18123c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1813a33eb0c4SMilanka Ringwald     }
1814ce263fc8SMatthias Ringwald 
1815a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1816a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1817a0ffb263SMatthias Ringwald 
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_redial_last_number(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_initiate_redial_last_number = 1;
18291c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18303c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1831ce263fc8SMatthias Ringwald }
1832ce263fc8SMatthias Ringwald 
18333c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
18349c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1835a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18363c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1837a33eb0c4SMilanka Ringwald     }
1838ce263fc8SMatthias Ringwald 
1839a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
18401c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18413c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1842ce263fc8SMatthias Ringwald }
1843ce263fc8SMatthias Ringwald 
1844ce263fc8SMatthias Ringwald 
18453c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
18469c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1847a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18483c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1849a33eb0c4SMilanka Ringwald     }
1850ce263fc8SMatthias Ringwald 
1851a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
18521c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18533c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1854ce263fc8SMatthias Ringwald }
1855ce263fc8SMatthias Ringwald 
1856ce263fc8SMatthias Ringwald 
18573c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
18589c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1859a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18603c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1861a33eb0c4SMilanka Ringwald     }
1862ce263fc8SMatthias Ringwald 
1863a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
18641c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18653c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1866ce263fc8SMatthias Ringwald }
1867ce263fc8SMatthias Ringwald 
18683c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
18699c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1870a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18713c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1872a33eb0c4SMilanka Ringwald     }
1873ce263fc8SMatthias Ringwald 
1874a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
18751c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18763c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1877ce263fc8SMatthias Ringwald }
1878ce263fc8SMatthias Ringwald 
18796ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){
18806ba83b5eSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION);
1881aeb0f0feSMatthias Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION);
18826ba83b5eSMilanka Ringwald     return hf && ag;
1883ce263fc8SMatthias Ringwald }
1884ce263fc8SMatthias Ringwald 
18853c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
18869c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1887a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18883c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1889a33eb0c4SMilanka Ringwald     }
18906ba83b5eSMilanka Ringwald     if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){
18916ba83b5eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
18926ba83b5eSMilanka Ringwald     }
1893ce263fc8SMatthias Ringwald 
1894a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
18951c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18963c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1897ce263fc8SMatthias Ringwald }
1898ce263fc8SMatthias Ringwald 
1899acd11d4aSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1900fdda66c0SMilanka Ringwald      hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1901fdda66c0SMilanka Ringwald     if (!hfp_connection) {
1902fdda66c0SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1903be55a11dSMilanka Ringwald     }
1904013cc750SMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1905013cc750SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1906013cc750SMilanka Ringwald     }
1907acd11d4aSMilanka Ringwald 
1908acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1909acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1910acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
1911acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1912af97579eSMilanka Ringwald     }
1913af97579eSMilanka Ringwald 
1914498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1915be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1916de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1917fd4151d1SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
1918acd11d4aSMilanka Ringwald             hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported;
1919be55a11dSMilanka Ringwald             break;
1920de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1921de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = true;
1922de9e0ea7SMilanka Ringwald             break;
1923be55a11dSMilanka Ringwald         default:
1924be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1925be55a11dSMilanka Ringwald     }
1926ce263fc8SMatthias Ringwald 
1927af97579eSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1928fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1929af97579eSMilanka Ringwald }
1930af97579eSMilanka Ringwald 
1931acd11d4aSMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
1932af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1933af97579eSMilanka Ringwald     if (!hfp_connection) {
1934af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1935af97579eSMilanka Ringwald     }
193684fb9ac1SMilanka Ringwald 
193784fb9ac1SMilanka Ringwald     if (hfp_connection->emit_vra_enabled_after_audio_established){
193884fb9ac1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
193984fb9ac1SMilanka Ringwald     }
194084fb9ac1SMilanka Ringwald 
1941acd11d4aSMilanka Ringwald     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
194208a0b01cSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
194308a0b01cSMilanka Ringwald     }
1944acd11d4aSMilanka Ringwald 
1945acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1946acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported){
1947acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1948acd11d4aSMilanka Ringwald     }
1949acd11d4aSMilanka Ringwald 
1950acd11d4aSMilanka Ringwald     switch (hfp_connection->vra_state){
1951acd11d4aSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1952acd11d4aSMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1953acd11d4aSMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
1954acd11d4aSMilanka Ringwald             break;
1955acd11d4aSMilanka Ringwald         default:
1956fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1957af97579eSMilanka Ringwald     }
1958013cc750SMilanka Ringwald 
1959acd11d4aSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1960acd11d4aSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1961acd11d4aSMilanka Ringwald }
1962acd11d4aSMilanka Ringwald 
1963acd11d4aSMilanka Ringwald 
1964acd11d4aSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1965acd11d4aSMilanka Ringwald     // return deactivate_voice_recognition(acl_handle, false);
1966acd11d4aSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1967acd11d4aSMilanka Ringwald     if (!hfp_connection) {
1968acd11d4aSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1969acd11d4aSMilanka Ringwald     }
1970acd11d4aSMilanka Ringwald 
197184fb9ac1SMilanka Ringwald     if (hfp_connection->emit_vra_enabled_after_audio_established){
197284fb9ac1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
197384fb9ac1SMilanka Ringwald     }
197484fb9ac1SMilanka Ringwald 
1975acd11d4aSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
1976acd11d4aSMilanka Ringwald         hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1977acd11d4aSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1978acd11d4aSMilanka Ringwald     }
1979acd11d4aSMilanka Ringwald 
1980acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1981acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1982acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
1983acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1984acd11d4aSMilanka Ringwald     }
1985acd11d4aSMilanka Ringwald 
1986fdda66c0SMilanka Ringwald     switch (hfp_connection->vra_state){
1987de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
1988fdda66c0SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1989de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1990de9e0ea7SMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1991fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1992fdda66c0SMilanka Ringwald             break;
1993de9e0ea7SMilanka Ringwald 
1994de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1995de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1996de9e0ea7SMilanka Ringwald             hfp_connection->deactivate_voice_recognition = true;
1997de9e0ea7SMilanka Ringwald             break;
1998de9e0ea7SMilanka Ringwald 
1999de9e0ea7SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
2000de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
2001de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
2002fdda66c0SMilanka Ringwald         default:
2003fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
2004fdda66c0SMilanka Ringwald     }
2005fdda66c0SMilanka Ringwald 
2006fdda66c0SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
2007fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2008af97579eSMilanka Ringwald }
2009af97579eSMilanka Ringwald 
20103c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
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     }
2015c8626498SMilanka Ringwald 
20163c65e705SMilanka Ringwald     if (hfp_connection->microphone_gain == gain) {
20173c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
20183c65e705SMilanka Ringwald     }
20193c65e705SMilanka Ringwald 
2020c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
2021a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
20223c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2023a0ffb263SMatthias Ringwald     }
20243c65e705SMilanka Ringwald 
2025a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
2026a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
20271c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20283c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2029ce263fc8SMatthias Ringwald }
2030ce263fc8SMatthias Ringwald 
20313c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
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     }
2036c8626498SMilanka Ringwald 
20373c65e705SMilanka Ringwald     if (hfp_connection->speaker_gain == gain){
20383c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
20393c65e705SMilanka Ringwald     }
20403c65e705SMilanka Ringwald 
2041c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
2042a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
20433c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2044a0ffb263SMatthias Ringwald     }
20453c65e705SMilanka Ringwald 
2046a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
2047a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
20481c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20493c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2050ce263fc8SMatthias Ringwald }
2051ce263fc8SMatthias Ringwald 
20523c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
20539c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2054a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20553c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2056a33eb0c4SMilanka Ringwald     }
2057a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
20581c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20593c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2060ce263fc8SMatthias Ringwald }
2061ce263fc8SMatthias Ringwald 
20623c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
20639c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2064a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20653c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2066a33eb0c4SMilanka Ringwald     }
2067a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
20681c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20693c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2070ce263fc8SMatthias Ringwald }
20713deb3ec6SMatthias Ringwald 
20723c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
20739c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2074a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20753c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2076a33eb0c4SMilanka Ringwald     }
2077a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
20781c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20793c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2080667ec068SMatthias Ringwald }
2081667ec068SMatthias Ringwald 
2082667ec068SMatthias Ringwald 
20833c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
20849c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2085a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20863c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2087a33eb0c4SMilanka Ringwald     }
2088a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2089a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
20901c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20913c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2092667ec068SMatthias Ringwald }
2093667ec068SMatthias Ringwald 
20943c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
20959c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2096a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20973c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2098a33eb0c4SMilanka Ringwald     }
2099a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2100a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
21011c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21023c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2103667ec068SMatthias Ringwald }
2104667ec068SMatthias Ringwald 
21053c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
21069c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2107a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21083c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2109a33eb0c4SMilanka Ringwald     }
2110a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2111a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
21121c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21133c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2114667ec068SMatthias Ringwald }
2115667ec068SMatthias Ringwald 
21163c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
21179c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2118a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21193c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2120a33eb0c4SMilanka Ringwald     }
2121a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2122a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
21231c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21243c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2125667ec068SMatthias Ringwald }
2126667ec068SMatthias Ringwald 
21273c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
21289c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2129a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21303c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2131a33eb0c4SMilanka Ringwald     }
2132a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
21331c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21343c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2135667ec068SMatthias Ringwald }
2136667ec068SMatthias Ringwald 
21373c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
21389c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2139a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21403c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2141a33eb0c4SMilanka Ringwald     }
2142667ec068SMatthias Ringwald     // find index for assigned number
2143667ec068SMatthias Ringwald     int i;
2144aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
2145aeb0f0feSMatthias Ringwald         if (hfp_hf_indicators[i] == assigned_number){
2146667ec068SMatthias Ringwald             // set value
2147aeb0f0feSMatthias Ringwald             hfp_hf_indicators_value[i] = value;
2148667ec068SMatthias Ringwald             // mark for update
2149a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
2150a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
2151667ec068SMatthias Ringwald                 // send update
21521c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
2153a0ffb263SMatthias Ringwald             }
21543c65e705SMilanka Ringwald             return ERROR_CODE_SUCCESS;
2155667ec068SMatthias Ringwald         }
2156667ec068SMatthias Ringwald     }
21573c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2158667ec068SMatthias Ringwald }
2159667ec068SMatthias Ringwald 
2160d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
2161d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2162d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
2163d7f6b5cbSMatthias Ringwald         return 0;
2164d7f6b5cbSMatthias Ringwald     }
2165d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
2166d7f6b5cbSMatthias Ringwald }
216776cc1527SMatthias Ringwald 
216876cc1527SMatthias 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){
216976cc1527SMatthias Ringwald 	if (!name){
2170aeb0f0feSMatthias Ringwald 		name = hfp_hf_default_service_name;
217176cc1527SMatthias Ringwald 	}
217276cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
217376cc1527SMatthias Ringwald 
217476cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
217576cc1527SMatthias Ringwald 	//
217676cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
217776cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
217876cc1527SMatthias Ringwald 	//
217976cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
218076cc1527SMatthias Ringwald 	//
218176cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
2182ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
218376cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
218476cc1527SMatthias Ringwald 	}
2185ef3ae4ebSMilanka Ringwald 
2186ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
218756f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
2188ef3ae4ebSMilanka Ringwald     }
2189ef3ae4ebSMilanka Ringwald 
2190ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
219156f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
2192ef3ae4ebSMilanka Ringwald     }
2193ef3ae4ebSMilanka Ringwald 
219476cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
219576cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
219676cc1527SMatthias Ringwald }
219776cc1527SMatthias Ringwald 
219876cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
219968466199SMilanka Ringwald 	btstack_assert(callback != NULL);
220068466199SMilanka Ringwald 
220176cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
220276cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
220376cc1527SMatthias Ringwald }
2204