xref: /btstack/src/classic/hfp_hf.c (revision cd7fdf75adf6c89f0438401653c26547a53150db)
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 
139c10fde09SMatthias Ringwald /* emit functions */
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;
143ab2445a0SMatthias Ringwald     uint16_t bnip_number_len = btstack_min((uint16_t) strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1);
144c10fde09SMatthias Ringwald     uint8_t event[7 + sizeof(hfp_connection->bnip_number)];
145a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
146c10fde09SMatthias Ringwald     event[1] = 6 + bnip_number_len;
147a473a009SMatthias Ringwald     event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION;
148d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
149d703d377SMatthias Ringwald     event[5] = status;
150d703d377SMatthias Ringwald     event[6] = hfp_connection->bnip_type;
151c10fde09SMatthias Ringwald     memcpy(&event[7], hfp_connection->bnip_number, bnip_number_len);
152c10fde09SMatthias Ringwald     event[7 + bnip_number_len] = 0;
153c10fde09SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 8 + bnip_number_len);
154a0ffb263SMatthias Ringwald }
155a0ffb263SMatthias Ringwald 
156598d4936SMatthias Ringwald static void hfp_hf_emit_type_number_alpha(const hfp_connection_t * hfp_connection, uint8_t event_subtype){
157a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
158ab2445a0SMatthias Ringwald     uint16_t bnip_number_len = btstack_min((uint16_t) strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1);
159598d4936SMatthias Ringwald     // 10 fixed - 1 (bnip_number_len <= sizeof(hfp_connection->bnip_number)-1) + 1 (trailing \0 for line buffer)
160598d4936SMatthias Ringwald     uint8_t event[10 + sizeof(hfp_connection->bnip_number) + sizeof(hfp_connection->line_buffer)];
161ab2445a0SMatthias Ringwald     uint8_t alpha_len = hfp_connection->clip_have_alpha ? (uint16_t) strlen((const char *) hfp_connection->line_buffer) : 0;
162598d4936SMatthias Ringwald     uint8_t pos = 0;
163598d4936SMatthias Ringwald     event[pos++] = HCI_EVENT_HFP_META;
164598d4936SMatthias Ringwald     event[pos++] = 8 + bnip_number_len + alpha_len;
165598d4936SMatthias Ringwald     event[pos++] = event_subtype;
166d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
167598d4936SMatthias Ringwald     pos += 2;
168598d4936SMatthias Ringwald     event[pos++] = hfp_connection->bnip_type;
169a45da34eSMatthias Ringwald     event[pos++] = bnip_number_len + 1;
170598d4936SMatthias Ringwald     memcpy(&event[7], hfp_connection->bnip_number, bnip_number_len);
171598d4936SMatthias Ringwald     pos += bnip_number_len;
172598d4936SMatthias Ringwald     event[pos++] = 0;
173a45da34eSMatthias Ringwald     event[pos++] = alpha_len + 1;
174598d4936SMatthias Ringwald     memcpy(&event[pos], hfp_connection->line_buffer, alpha_len);
175598d4936SMatthias Ringwald     pos += alpha_len;
176598d4936SMatthias Ringwald     event[pos++] = 0;
177598d4936SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
178a0ffb263SMatthias Ringwald }
179a0ffb263SMatthias Ringwald 
180a473a009SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){
181a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
1825f5a2872SMilanka Ringwald 
183ab2445a0SMatthias Ringwald     uint16_t bnip_number_len = (uint16_t) strlen((const char *) hfp_connection->bnip_number);
1845f5a2872SMilanka Ringwald     uint8_t event[11 + HFP_BNEP_NUM_MAX_SIZE];
185c10fde09SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
1865f5a2872SMilanka Ringwald     event[1] = 10 + bnip_number_len + 1;
187c10fde09SMatthias Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
188c10fde09SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
1895f5a2872SMilanka Ringwald     event[5] = hfp_connection->clcc_idx;
1905f5a2872SMilanka Ringwald     event[6] = hfp_connection->clcc_dir;
1915f5a2872SMilanka Ringwald     event[7] = hfp_connection->clcc_status;
1925f5a2872SMilanka Ringwald     event[8] = hfp_connection->clcc_mode;
1935f5a2872SMilanka Ringwald     event[9] = hfp_connection->clcc_mpty;
1945f5a2872SMilanka Ringwald     event[10] = hfp_connection->bnip_type;
1955f5a2872SMilanka Ringwald     memcpy(&event[11], hfp_connection->bnip_number, bnip_number_len + 1);
1965f5a2872SMilanka Ringwald 
1975f5a2872SMilanka Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 11 + bnip_number_len + 1);
198a0ffb263SMatthias Ringwald }
199a0ffb263SMatthias Ringwald 
2001ac1f60fSMilanka Ringwald static void hfp_emit_ag_indicator_mapping_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
2011ac1f60fSMilanka Ringwald     if (hfp_hf_callback == NULL) return;
202c10fde09SMatthias Ringwald     uint8_t event[8 + HFP_MAX_INDICATOR_DESC_SIZE];
203ab2445a0SMatthias Ringwald     uint16_t indicator_len = btstack_min((uint16_t) strlen(indicator->name), HFP_MAX_INDICATOR_DESC_SIZE-1);
204c10fde09SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
205c10fde09SMatthias Ringwald     event[1] = 7 + indicator_len;
206c10fde09SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_MAPPING;
207c10fde09SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
208c10fde09SMatthias Ringwald     event[5] = indicator->index;
209c10fde09SMatthias Ringwald     event[6] = indicator->min_range;
210c10fde09SMatthias Ringwald     event[7] = indicator->max_range;
211c10fde09SMatthias Ringwald     memcpy(&event[8], indicator->name, indicator_len);
212c10fde09SMatthias Ringwald     event[8+indicator_len] = 0;
213c10fde09SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 9 + indicator_len);
2141ac1f60fSMilanka Ringwald }
21576cc1527SMatthias Ringwald 
216ce3797e1SMilanka Ringwald static void hfp_emit_ag_indicator_status_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
217a473a009SMatthias Ringwald 	if (hfp_hf_callback == NULL) return;
218c10fde09SMatthias Ringwald 	uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE];
219ab2445a0SMatthias Ringwald     uint16_t indicator_len = btstack_min((uint16_t) strlen(indicator->name), HFP_MAX_INDICATOR_DESC_SIZE-1);
220c10fde09SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
221c10fde09SMatthias Ringwald 	event[1] = 11 + indicator_len;
222c10fde09SMatthias Ringwald 	event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
223c10fde09SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
224c10fde09SMatthias Ringwald 	event[5] = indicator->index;
225c10fde09SMatthias Ringwald 	event[6] = indicator->status;
226c10fde09SMatthias Ringwald 	event[7] = indicator->min_range;
227c10fde09SMatthias Ringwald 	event[8] = indicator->max_range;
228c10fde09SMatthias Ringwald 	event[9] = indicator->mandatory;
229c10fde09SMatthias Ringwald 	event[10] = indicator->enabled;
230c10fde09SMatthias Ringwald 	event[11] = indicator->status_changed;
231c10fde09SMatthias Ringwald 	memcpy(&event[12], indicator->name, indicator_len);
232c10fde09SMatthias Ringwald 	event[12+indicator_len] = 0;
233c10fde09SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 13 + indicator_len);
2343deb3ec6SMatthias Ringwald }
2353deb3ec6SMatthias Ringwald 
236a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){
237a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
238ab2445a0SMatthias Ringwald     uint16_t operator_len = btstack_min((uint16_t) strlen(hfp_connection->network_operator.name), HFP_MAX_NETWORK_OPERATOR_NAME_SIZE-1);
239c10fde09SMatthias Ringwald 	uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE];
24076cc1527SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
24176cc1527SMatthias Ringwald 	event[1] = sizeof(event) - 2;
242f127324fSMilanka Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
243d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
244d703d377SMatthias Ringwald 	event[5] = hfp_connection->network_operator.mode;
245d703d377SMatthias Ringwald 	event[6] = hfp_connection->network_operator.format;
246c10fde09SMatthias Ringwald 	memcpy(&event[7], hfp_connection->network_operator.name, operator_len);
247c10fde09SMatthias Ringwald 	event[7+operator_len] = 0;
248c10fde09SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 8 + operator_len);
2493deb3ec6SMatthias Ringwald }
2503deb3ec6SMatthias Ringwald 
251b95cac54SMilanka Ringwald 
252b95cac54SMilanka Ringwald static void hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection){
253b95cac54SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
25451aa5d5aSMilanka Ringwald     uint8_t event[HFP_MAX_VR_TEXT_SIZE + 11];
255b95cac54SMilanka Ringwald     int pos = 0;
256b95cac54SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
257b95cac54SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
258b95cac54SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE;
259b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
260b95cac54SMilanka Ringwald     pos += 2;
261b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->ag_msg.text_id);
262b95cac54SMilanka Ringwald     pos += 2;
263b95cac54SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_type;
264e83f1be7SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_operation;
265b95cac54SMilanka Ringwald 
26651aa5d5aSMilanka Ringwald     // length, zero ending is already in message
267b95cac54SMilanka Ringwald     uint8_t * value = &hfp_connection->line_buffer[0];
26851aa5d5aSMilanka Ringwald     uint16_t  value_length = hfp_connection->ag_vra_msg_length;
26951aa5d5aSMilanka Ringwald 
27051aa5d5aSMilanka Ringwald     little_endian_store_16(event, pos, value_length);
271b95cac54SMilanka Ringwald     pos += 2;
27251aa5d5aSMilanka Ringwald     memcpy(&event[pos], value, value_length);
27351aa5d5aSMilanka Ringwald     pos += value_length;
27451aa5d5aSMilanka Ringwald 
275b95cac54SMilanka Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
276b95cac54SMilanka Ringwald }
277b95cac54SMilanka Ringwald 
278892f58a8SMatthias Ringwald static void hfp_hf_emit_custom_command_event(hfp_connection_t * hfp_connection){
279892f58a8SMatthias Ringwald     btstack_assert(sizeof(hfp_connection->line_buffer) < (255-5));
280892f58a8SMatthias Ringwald 
281892f58a8SMatthias Ringwald     uint16_t line_len = strlen((const char*)hfp_connection->line_buffer) + 1;
282892f58a8SMatthias Ringwald     uint8_t event[7 + sizeof(hfp_connection->line_buffer)];
283892f58a8SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
284892f58a8SMatthias Ringwald     event[1] = 5 + line_len;
285892f58a8SMatthias Ringwald     event[2] = HFP_SUBEVENT_CUSTOM_AT_COMMAND;
286892f58a8SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
287892f58a8SMatthias Ringwald     little_endian_store_16(event, 5, hfp_connection->custom_at_command_id);
288892f58a8SMatthias Ringwald     memcpy(&event[7], hfp_connection->line_buffer, line_len);
289892f58a8SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 7 + line_len);
290892f58a8SMatthias Ringwald }
291892f58a8SMatthias Ringwald 
29276cc1527SMatthias Ringwald /* send commands */
29389425bfcSMilanka Ringwald 
29489425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2953deb3ec6SMatthias Ringwald     char buffer[20];
2961599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
29789425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
29889425bfcSMilanka Ringwald }
29989425bfcSMilanka Ringwald 
30089425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
30189425bfcSMilanka Ringwald     char buffer[20];
3021599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
30389425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
30489425bfcSMilanka Ringwald }
30589425bfcSMilanka Ringwald 
30686da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
30789425bfcSMilanka Ringwald     char buffer[40];
3081599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
3093deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3103deb3ec6SMatthias Ringwald }
3113deb3ec6SMatthias Ringwald 
3123deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
3133deb3ec6SMatthias Ringwald     char buffer[30];
31489425bfcSMilanka Ringwald     const int size = sizeof(buffer);
31589425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
316aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_codecs, hfp_hf_codecs_nr);
3171599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
3183deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3193deb3ec6SMatthias Ringwald }
3203deb3ec6SMatthias Ringwald 
3213deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
3223deb3ec6SMatthias Ringwald     char buffer[50];
32389425bfcSMilanka Ringwald     const int size = sizeof(buffer);
32489425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
32589425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
3261599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
3273deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3283deb3ec6SMatthias Ringwald }
3293deb3ec6SMatthias Ringwald 
3303deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
3313deb3ec6SMatthias Ringwald     char buffer[30];
33289425bfcSMilanka Ringwald     const int size = sizeof(buffer);
33389425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
334aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_indicators, hfp_hf_indicators_nr);
3351599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
3363deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3373deb3ec6SMatthias Ringwald }
3383deb3ec6SMatthias Ringwald 
33989425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
3403deb3ec6SMatthias Ringwald     char buffer[20];
3411599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
342ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
343ce263fc8SMatthias Ringwald }
344ce263fc8SMatthias Ringwald 
345ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
346ce263fc8SMatthias Ringwald     char buffer[40];
347aeb0f0feSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, hfp_hf_phone_number);
348ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
349ce263fc8SMatthias Ringwald }
350ce263fc8SMatthias Ringwald 
351a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
352ce263fc8SMatthias Ringwald     char buffer[40];
3531599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
354ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
355ce263fc8SMatthias Ringwald }
356ce263fc8SMatthias Ringwald 
357f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
35889425bfcSMilanka Ringwald     char buffer[40];
3591599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
360ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
361ce263fc8SMatthias Ringwald }
362ce263fc8SMatthias Ringwald 
363ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
364ce263fc8SMatthias Ringwald     char buffer[20];
3651599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
366ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
367ce263fc8SMatthias Ringwald }
368ce263fc8SMatthias Ringwald 
36997d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
3701599fe57SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "ATA\r");
37197d2cadbSMatthias Ringwald }
37297d2cadbSMatthias Ringwald 
37389425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
374aeb0f0feSMatthias Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_hf_supported_features);
37589425bfcSMilanka Ringwald }
37689425bfcSMilanka Ringwald 
37789425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
37889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
37989425bfcSMilanka Ringwald }
38089425bfcSMilanka Ringwald 
38189425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
38289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
38389425bfcSMilanka Ringwald }
38489425bfcSMilanka Ringwald 
38589425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
38689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
38789425bfcSMilanka Ringwald }
38889425bfcSMilanka Ringwald 
38989425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
39089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
39189425bfcSMilanka Ringwald }
39289425bfcSMilanka Ringwald 
39389425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
39489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
39589425bfcSMilanka Ringwald }
39689425bfcSMilanka Ringwald 
39789425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
39889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
39989425bfcSMilanka Ringwald }
40089425bfcSMilanka Ringwald 
40189425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
40289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
40389425bfcSMilanka Ringwald }
40489425bfcSMilanka Ringwald 
40589425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
40689425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
40789425bfcSMilanka Ringwald }
40889425bfcSMilanka Ringwald 
40989425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
41089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
41189425bfcSMilanka Ringwald }
41289425bfcSMilanka Ringwald 
41389425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
41489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
41589425bfcSMilanka Ringwald }
41689425bfcSMilanka Ringwald 
41789425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
41889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
41989425bfcSMilanka Ringwald }
42089425bfcSMilanka Ringwald 
42189425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
42289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
42389425bfcSMilanka Ringwald }
42489425bfcSMilanka Ringwald 
42589425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
42689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
42789425bfcSMilanka Ringwald }
42889425bfcSMilanka Ringwald 
42989425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
43089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
43189425bfcSMilanka Ringwald }
43289425bfcSMilanka Ringwald 
43389425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
43489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
43589425bfcSMilanka Ringwald }
43689425bfcSMilanka Ringwald 
43789425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
43889425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
43989425bfcSMilanka Ringwald }
44089425bfcSMilanka Ringwald 
44189425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
44289425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
44389425bfcSMilanka Ringwald }
44489425bfcSMilanka Ringwald 
445ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
44689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
447ce263fc8SMatthias Ringwald }
448ce263fc8SMatthias Ringwald 
449667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
45089425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
451667ec068SMatthias Ringwald }
452667ec068SMatthias Ringwald 
45376cc1527SMatthias Ringwald /* state machines */
4543deb3ec6SMatthias Ringwald 
455a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
456a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
457a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
458aa4dd815SMatthias Ringwald     int done = 1;
459498a8121SMilanka Ringwald     log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state);
460a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4613deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
462aeb0f0feSMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_hf_codecs, &hfp_hf_codecs_nr);
463a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
464a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4653deb3ec6SMatthias Ringwald             break;
4663deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
467a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
468a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4693deb3ec6SMatthias Ringwald             break;
4703deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
471a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
472a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4733deb3ec6SMatthias Ringwald             break;
4743deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
475a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
476a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4773deb3ec6SMatthias Ringwald             break;
4783deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
479a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
480a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4813deb3ec6SMatthias Ringwald             break;
4823deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
483a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
484a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4853deb3ec6SMatthias Ringwald             break;
4863deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
487a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
488a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4893deb3ec6SMatthias Ringwald             break;
4903deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
491a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
492a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4933deb3ec6SMatthias Ringwald             break;
4943deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
495a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
496a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4973deb3ec6SMatthias Ringwald             break;
4983deb3ec6SMatthias Ringwald         default:
499aa4dd815SMatthias Ringwald             done = 0;
5003deb3ec6SMatthias Ringwald             break;
5013deb3ec6SMatthias Ringwald     }
5023deb3ec6SMatthias Ringwald     return done;
5033deb3ec6SMatthias Ringwald }
5043deb3ec6SMatthias Ringwald 
505ce263fc8SMatthias Ringwald 
506a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
507a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
508498a8121SMilanka Ringwald     if (hfp_connection->ok_pending){
509498a8121SMilanka Ringwald         return 0;
510498a8121SMilanka Ringwald     }
511ce263fc8SMatthias Ringwald     int done = 0;
512a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
513a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
514ce263fc8SMatthias Ringwald         done = 1;
515a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
516ce263fc8SMatthias Ringwald         return done;
517ce263fc8SMatthias Ringwald     };
518a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
519a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
520ce263fc8SMatthias Ringwald         done = 1;
521a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
522a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
523a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
524ce263fc8SMatthias Ringwald         return done;
525ce263fc8SMatthias Ringwald     }
526ce263fc8SMatthias Ringwald 
527a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
528ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
529a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
530a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
531a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
532ce263fc8SMatthias Ringwald             return 1;
533ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
534a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
535a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
536a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
537ce263fc8SMatthias Ringwald             return 1;
538ce263fc8SMatthias Ringwald         default:
539ce263fc8SMatthias Ringwald             break;
540ce263fc8SMatthias Ringwald     }
541ce263fc8SMatthias Ringwald 
542a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
543a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
544ce263fc8SMatthias Ringwald         done = 1;
545a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
546ce263fc8SMatthias Ringwald         return done;
547ce263fc8SMatthias Ringwald     }
548ce263fc8SMatthias Ringwald 
549ce263fc8SMatthias Ringwald     return done;
550ce263fc8SMatthias Ringwald }
551ce263fc8SMatthias Ringwald 
552af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
553be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
554be55a11dSMilanka Ringwald         return 0;
555be55a11dSMilanka Ringwald     }
556fd4151d1SMilanka Ringwald 
5570b4debbfSMilanka Ringwald     if (hfp_connection->ok_pending == 1){
5580b4debbfSMilanka Ringwald         return 0;
5590b4debbfSMilanka Ringwald     }
5600b4debbfSMilanka Ringwald     // voice recognition activated from AG
5610b4debbfSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){
5620b4debbfSMilanka Ringwald         switch(hfp_connection->vra_state_requested){
5630b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
5640b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
565de9e0ea7SMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
5660b4debbfSMilanka Ringwald                 // ignore AG command, continue to wait for OK
5670b4debbfSMilanka Ringwald                 return 0;
568cf75be85SMilanka Ringwald 
5690b4debbfSMilanka Ringwald             default:
570b95cac54SMilanka Ringwald                 if (hfp_connection->ag_vra_msg_length > 0){
571b95cac54SMilanka Ringwald                     hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection);
572b95cac54SMilanka Ringwald                     hfp_connection->ag_vra_msg_length = 0;
573b95cac54SMilanka Ringwald                     break;
574b95cac54SMilanka Ringwald                 }
575cf75be85SMilanka Ringwald                 switch(hfp_connection->ag_vra_state){
576cf75be85SMilanka Ringwald                     case HFP_VOICE_RECOGNITION_STATE_AG_READY:
577013cc750SMilanka Ringwald                         switch (hfp_connection->ag_vra_status){
578013cc750SMilanka Ringwald                             case 0:
5790b4debbfSMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
580013cc750SMilanka Ringwald                                 break;
581013cc750SMilanka Ringwald                             case 1:
582013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
583013cc750SMilanka Ringwald                                 break;
584013cc750SMilanka Ringwald                             case 2:
585013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
586013cc750SMilanka Ringwald                                 break;
587013cc750SMilanka Ringwald                             default:
588013cc750SMilanka Ringwald                                 break;
5890b4debbfSMilanka Ringwald                         }
5900b4debbfSMilanka Ringwald                         break;
591cf75be85SMilanka Ringwald                     default:
592cf75be85SMilanka Ringwald                         // state messages from AG
593cf75be85SMilanka Ringwald                         hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
5948d5005b5SMilanka Ringwald                         hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
595cf75be85SMilanka Ringwald                         break;
596cf75be85SMilanka Ringwald                 }
597cf75be85SMilanka Ringwald                 break;
5980b4debbfSMilanka Ringwald         }
5990b4debbfSMilanka Ringwald         hfp_connection->command = HFP_CMD_NONE;
6000b4debbfSMilanka Ringwald     }
6010b4debbfSMilanka Ringwald 
602498a8121SMilanka Ringwald     switch (hfp_connection->vra_state_requested){
603fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
604fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
605498a8121SMilanka Ringwald             hfp_connection->ok_pending = 1;
606*cd7fdf75SMatthias Ringwald             hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
607fd4151d1SMilanka Ringwald             return 1;
608fd4151d1SMilanka Ringwald 
609fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
610fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
611fd4151d1SMilanka Ringwald             hfp_connection->ok_pending = 1;
612*cd7fdf75SMatthias Ringwald             hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
613fd4151d1SMilanka Ringwald             return 1;
614013cc750SMilanka Ringwald 
615de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
616de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
617de9e0ea7SMilanka Ringwald             hfp_connection->ok_pending = 1;
618*cd7fdf75SMatthias Ringwald             hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
619de9e0ea7SMilanka Ringwald             return 1;
620de9e0ea7SMilanka Ringwald 
621de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
622de9e0ea7SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
623de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
624de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
625de9e0ea7SMilanka Ringwald             if (hfp_connection->activate_voice_recognition){
626fcf4ede6SMilanka Ringwald                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
627de9e0ea7SMilanka Ringwald                 hfp_hf_activate_voice_recognition(hfp_connection->acl_handle);
628de9e0ea7SMilanka Ringwald             } else {
629553a4a56SMilanka Ringwald                 hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
630de9e0ea7SMilanka Ringwald             }
631de9e0ea7SMilanka Ringwald             break;
632de9e0ea7SMilanka Ringwald 
633be55a11dSMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
634498a8121SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
635498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
636de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
637de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
638de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
639de9e0ea7SMilanka Ringwald             } else {
640fcf4ede6SMilanka Ringwald                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
64184fb9ac1SMilanka Ringwald                 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
642553a4a56SMilanka Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
64384fb9ac1SMilanka Ringwald                 } else {
64484fb9ac1SMilanka Ringwald                     // postpone VRA event to simplify application logic
64584fb9ac1SMilanka Ringwald                     hfp_connection->emit_vra_enabled_after_audio_established = true;
64684fb9ac1SMilanka Ringwald                 }
647de9e0ea7SMilanka Ringwald             }
648be55a11dSMilanka Ringwald             break;
649be55a11dSMilanka Ringwald 
650de9e0ea7SMilanka Ringwald 
651013cc750SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
652013cc750SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
653498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
654de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
655de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
656de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
657de9e0ea7SMilanka Ringwald             } else {
658de9e0ea7SMilanka Ringwald                 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
659de9e0ea7SMilanka Ringwald             }
660be55a11dSMilanka Ringwald             break;
661fd4151d1SMilanka Ringwald 
662be55a11dSMilanka Ringwald         default:
663be55a11dSMilanka Ringwald             break;
664be55a11dSMilanka Ringwald     }
665*cd7fdf75SMatthias Ringwald     return 0;
666be55a11dSMilanka Ringwald }
667be55a11dSMilanka Ringwald 
668be55a11dSMilanka Ringwald 
669be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
670a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
671ce263fc8SMatthias Ringwald 
672332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
673332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
674ce263fc8SMatthias Ringwald 
675a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
676a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
677332ca98fSMatthias Ringwald 		return 1;
678332ca98fSMatthias Ringwald     }
679332ca98fSMatthias Ringwald 
6801cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
6811cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
682ce263fc8SMatthias Ringwald 
683a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
684fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
6851cc65c4fSMatthias Ringwald 		return 1;
6861cc65c4fSMatthias Ringwald     }
6871cc65c4fSMatthias Ringwald 
6881cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
6891cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
6901cc65c4fSMatthias Ringwald 
691a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
692a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
6931cc65c4fSMatthias Ringwald 		return 1;
6941cc65c4fSMatthias Ringwald     }
695ce263fc8SMatthias Ringwald 
696ce263fc8SMatthias Ringwald     return 0;
697ce263fc8SMatthias Ringwald }
698ce263fc8SMatthias Ringwald 
699a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
700505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
701505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
702ce263fc8SMatthias Ringwald 
70364f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
704a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
705a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
706a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
707ce263fc8SMatthias Ringwald         return 1;
708ce263fc8SMatthias Ringwald     }
709ce263fc8SMatthias Ringwald 
710a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
711ce263fc8SMatthias Ringwald 
712ce263fc8SMatthias Ringwald     // run codecs exchange
713a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
714ce263fc8SMatthias Ringwald     if (done) return 1;
715ce263fc8SMatthias Ringwald 
71638200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
717447743f7SMatthias Ringwald     if (hfp_sco_setup_active()) return 0;
718d2e34ffbSMatthias Ringwald     if (hci_can_send_command_packet_now() == false) return 0;
71938200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
72038200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
72138200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
72238200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
72338200c1dSMilanka Ringwald         return 1;
72438200c1dSMilanka Ringwald     }
725ce263fc8SMatthias Ringwald     return 0;
726ce263fc8SMatthias Ringwald }
727ce263fc8SMatthias Ringwald 
72838200c1dSMilanka Ringwald 
729a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
730eaf2b0a1SMatthias Ringwald 
731eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
732eaf2b0a1SMatthias Ringwald 
733a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
734a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
735*cd7fdf75SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
736ce263fc8SMatthias Ringwald         return 1;
737ce263fc8SMatthias Ringwald     }
738ce263fc8SMatthias Ringwald     return 0;
739ce263fc8SMatthias Ringwald }
740ce263fc8SMatthias Ringwald 
7411c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
7427522e673SMatthias Ringwald 
74376cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
74476cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
74576cc1527SMatthias Ringwald 
74676cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
74776cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
74822387625SMatthias Ringwald 
74984fb9ac1SMilanka Ringwald     // emit postponed VRA event
75084fb9ac1SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){
75184fb9ac1SMilanka Ringwald         hfp_connection->emit_vra_enabled_after_audio_established = false;
75284fb9ac1SMilanka Ringwald         hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
75384fb9ac1SMilanka Ringwald     }
75484fb9ac1SMilanka Ringwald 
7553721a235SMatthias Ringwald 	// assert command could be sent
7563721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
7573721a235SMatthias Ringwald 
7583721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
7593721a235SMatthias Ringwald     // WBS Disassociate
7603721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
7613721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
7623721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
7633721a235SMatthias Ringwald         return;
7643721a235SMatthias Ringwald     }
7653721a235SMatthias Ringwald     // Write Codec Config
7663721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
7673721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
7683721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
7693721a235SMatthias Ringwald         return;
7703721a235SMatthias Ringwald     }
7713721a235SMatthias Ringwald     // WBS Associate
7723721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
7733721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
7743721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
7753721a235SMatthias Ringwald         return;
7763721a235SMatthias Ringwald     }
7773721a235SMatthias Ringwald #endif
778689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
779689d4323SMatthias Ringwald     // Enable WBS
780689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
781689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
782689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
783689d4323SMatthias Ringwald         return;
784689d4323SMatthias Ringwald     }
785689d4323SMatthias Ringwald     // Write I2S/PCM params
786689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
787689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
788689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
789689d4323SMatthias Ringwald         return;
790689d4323SMatthias Ringwald     }
791689d4323SMatthias Ringwald     // Disable WBS
792689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
793689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
794689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
795689d4323SMatthias Ringwald         return;
796689d4323SMatthias Ringwald     }
797689d4323SMatthias Ringwald #endif
7982b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS
7992b5f92fdSMatthias Ringwald     if (hfp_connection->rtk_send_sco_config){
8002b5f92fdSMatthias Ringwald         hfp_connection->rtk_send_sco_config = false;
8012b5f92fdSMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
8022b5f92fdSMatthias Ringwald             log_info("RTK SCO: 16k + mSBC");
8032b5f92fdSMatthias Ringwald             hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x00, 0x00, 0x41);
8042b5f92fdSMatthias Ringwald         } else {
8052b5f92fdSMatthias Ringwald             log_info("RTK SCO: 16k + CVSD");
8062b5f92fdSMatthias Ringwald             hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x0c, 0x00, 0x01);
8072b5f92fdSMatthias Ringwald         }
8082b5f92fdSMatthias Ringwald         return;
8092b5f92fdSMatthias Ringwald     }
8102b5f92fdSMatthias Ringwald #endif
8115fd6f360SMatthias Ringwald #ifdef ENABLE_NXP_PCM_WBS
8125fd6f360SMatthias Ringwald     if (hfp_connection->nxp_start_audio_handle != HCI_CON_HANDLE_INVALID){
8135fd6f360SMatthias Ringwald         hci_con_handle_t sco_handle = hfp_connection->nxp_start_audio_handle;
8145fd6f360SMatthias Ringwald         hfp_connection->nxp_start_audio_handle = HCI_CON_HANDLE_INVALID;
8155fd6f360SMatthias Ringwald         hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 0, 0, sco_handle, 0);
8165fd6f360SMatthias Ringwald         return;
8175fd6f360SMatthias Ringwald     }
8185fd6f360SMatthias Ringwald     if (hfp_connection->nxp_stop_audio_handle != HCI_CON_HANDLE_INVALID){
8195fd6f360SMatthias Ringwald         hci_con_handle_t sco_handle = hfp_connection->nxp_stop_audio_handle;
8205fd6f360SMatthias Ringwald         hfp_connection->nxp_stop_audio_handle = HCI_CON_HANDLE_INVALID;
8215fd6f360SMatthias Ringwald         hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 1, 0, sco_handle, 0);
8225fd6f360SMatthias Ringwald         return;
8235fd6f360SMatthias Ringwald     }
8245fd6f360SMatthias Ringwald #endif
82548e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
82648e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
82748e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
82848e6eeeeSMatthias Ringwald         return;
82948e6eeeeSMatthias Ringwald     }
83048e6eeeeSMatthias Ringwald #endif
8313721a235SMatthias Ringwald 
832447743f7SMatthias Ringwald     if (hfp_connection->accept_sco && (hfp_sco_setup_active() == false)){
833cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
8347522e673SMatthias Ringwald         // notify about codec selection if not done already
8357522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
8367522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
8377522e673SMatthias Ringwald         }
838cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
8397522e673SMatthias Ringwald         return;
8407522e673SMatthias Ringwald     }
8417522e673SMatthias Ringwald 
842d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
843d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
844d4dd47ffSMatthias Ringwald         return;
845d4dd47ffSMatthias Ringwald     }
846*cd7fdf75SMatthias Ringwald 
847*cd7fdf75SMatthias Ringwald     // we can send at least an RFCOMM packet or a HCI Command now
848*cd7fdf75SMatthias Ringwald 
849a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
850ce263fc8SMatthias Ringwald     if (!done){
851a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
852ce263fc8SMatthias Ringwald     }
853ce263fc8SMatthias Ringwald     if (!done){
854c95b5b3cSMilanka Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
855be55a11dSMilanka Ringwald     }
856be55a11dSMilanka Ringwald     if (!done){
857c95b5b3cSMilanka Ringwald         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
858ce263fc8SMatthias Ringwald     }
859ce263fc8SMatthias Ringwald     if (!done){
860a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
861ce263fc8SMatthias Ringwald     }
862ce263fc8SMatthias Ringwald 
863e2c3b58dSMilanka Ringwald     // don't send a new command while ok still pending or SLC is not established
864e2c3b58dSMilanka Ringwald     if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){
865e2c3b58dSMilanka Ringwald         return;
866e2c3b58dSMilanka Ringwald     }
8671016a228SMatthias Ringwald 
868a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
869a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
870a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
871a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
872ce263fc8SMatthias Ringwald         return;
873ce263fc8SMatthias Ringwald     }
874ce263fc8SMatthias Ringwald 
875a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
876a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
877a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
878a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
879ce263fc8SMatthias Ringwald         return;
880ce263fc8SMatthias Ringwald     }
881ce263fc8SMatthias Ringwald 
882a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
883a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
884a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
885a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
886ce263fc8SMatthias Ringwald         return;
887ce263fc8SMatthias Ringwald     }
888ce263fc8SMatthias Ringwald 
889a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
890a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
891a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
892a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
893ce263fc8SMatthias Ringwald         return;
894ce263fc8SMatthias Ringwald     }
895ce263fc8SMatthias Ringwald 
896a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
897a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
89899af1e28SMilanka Ringwald         hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR;
899a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
900e2c3b58dSMilanka Ringwald         hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0);
901ce263fc8SMatthias Ringwald         return;
902ce263fc8SMatthias Ringwald     }
903ce263fc8SMatthias Ringwald 
904a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
905a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
906a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
907a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
908ce263fc8SMatthias Ringwald         return;
909ce263fc8SMatthias Ringwald     }
910ce263fc8SMatthias Ringwald 
911a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
912a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
913a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
914a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
915ce263fc8SMatthias Ringwald         return;
916ce263fc8SMatthias Ringwald     }
917ce263fc8SMatthias Ringwald 
918a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
919a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
920a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
921a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
922ce263fc8SMatthias Ringwald         return;
923ce263fc8SMatthias Ringwald     }
924ce263fc8SMatthias Ringwald 
925a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
926a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
927a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
928a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
929ce263fc8SMatthias Ringwald         return;
930ce263fc8SMatthias Ringwald     }
931ce263fc8SMatthias Ringwald 
932a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
933a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
934a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
935a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
936ce263fc8SMatthias Ringwald         return;
937ce263fc8SMatthias Ringwald     }
938ce263fc8SMatthias Ringwald 
939a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
940a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
941a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
942a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
943ce263fc8SMatthias Ringwald         return;
944ce263fc8SMatthias Ringwald     }
945ce263fc8SMatthias Ringwald 
946a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
947a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
948a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
949a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
950ce263fc8SMatthias Ringwald         return;
951ce263fc8SMatthias Ringwald     }
952ce263fc8SMatthias Ringwald 
953a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
954a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
955a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
956a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
957ce263fc8SMatthias Ringwald         return;
958ce263fc8SMatthias Ringwald     }
959ce263fc8SMatthias Ringwald 
960a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
961a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
962a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
963a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
964ce263fc8SMatthias Ringwald         return;
965ce263fc8SMatthias Ringwald     }
966ce263fc8SMatthias Ringwald 
967a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
968a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
969a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
970a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
971ce263fc8SMatthias Ringwald         return;
972ce263fc8SMatthias Ringwald     }
973ce263fc8SMatthias Ringwald 
974a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
975a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
976a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
977a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
978ce263fc8SMatthias Ringwald         return;
979ce263fc8SMatthias Ringwald     }
980ce263fc8SMatthias Ringwald 
981a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
982a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
983a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
984a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
985667ec068SMatthias Ringwald         return;
986667ec068SMatthias Ringwald     }
987667ec068SMatthias Ringwald 
988a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
989a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
990a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
991a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
992a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
993c090e552SMatthias Ringwald         // notify client that dtmf was sent
994c090e552SMatthias Ringwald         char buffer[2];
995c090e552SMatthias Ringwald         buffer[0] = code;
996c090e552SMatthias Ringwald         buffer[1] = 0;
997c090e552SMatthias Ringwald         hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer);
998ce263fc8SMatthias Ringwald         return;
999ce263fc8SMatthias Ringwald     }
1000ce263fc8SMatthias Ringwald 
1001a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
1002a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
1003a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1004a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
1005ce263fc8SMatthias Ringwald         return;
1006ce263fc8SMatthias Ringwald     }
1007ce263fc8SMatthias Ringwald 
1008a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
1009a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
1010a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1011a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
1012667ec068SMatthias Ringwald         return;
1013667ec068SMatthias Ringwald     }
1014667ec068SMatthias Ringwald 
1015a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
1016a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
1017667ec068SMatthias Ringwald         char buffer[20];
1018a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
1019667ec068SMatthias Ringwald             case '?':
10201599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
1021ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
1022ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
1023a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1024667ec068SMatthias Ringwald                 return;
1025667ec068SMatthias Ringwald             case '0':
1026667ec068SMatthias Ringwald             case '1':
1027667ec068SMatthias Ringwald             case '2':
10281599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
1029ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
1030ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
1031ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
1032a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1033667ec068SMatthias Ringwald                 return;
1034667ec068SMatthias Ringwald             default:
1035667ec068SMatthias Ringwald                 break;
1036667ec068SMatthias Ringwald         }
1037667ec068SMatthias Ringwald         return;
1038667ec068SMatthias Ringwald     }
1039667ec068SMatthias Ringwald 
1040a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
1041a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
1042667ec068SMatthias Ringwald         char buffer[20];
10431599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
1044ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
1045ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
1046a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1047667ec068SMatthias Ringwald         return;
1048667ec068SMatthias Ringwald     }
1049667ec068SMatthias Ringwald 
1050667ec068SMatthias Ringwald     // update HF indicators
1051a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
1052667ec068SMatthias Ringwald         int i;
1053aeb0f0feSMatthias Ringwald         for (i=0; i < hfp_hf_indicators_nr; i++){
1054a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
1055a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
1056a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
1057a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
1058667ec068SMatthias Ringwald                     char buffer[30];
10591599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
1060ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
1061aeb0f0feSMatthias Ringwald                              hfp_hf_indicators[i],
1062aeb0f0feSMatthias Ringwald                              (unsigned int)hfp_hf_indicators_value[i]);
1063ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
1064a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1065667ec068SMatthias Ringwald                 } else {
1066aeb0f0feSMatthias Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]);
1067667ec068SMatthias Ringwald                 }
1068667ec068SMatthias Ringwald                 return;
1069667ec068SMatthias Ringwald             }
1070667ec068SMatthias Ringwald         }
1071667ec068SMatthias Ringwald     }
1072667ec068SMatthias Ringwald 
107351a2ebdeSMatthias Ringwald     if (hfp_connection->send_custom_message != NULL){
107451a2ebdeSMatthias Ringwald         const char * message = hfp_connection->send_custom_message;
107551a2ebdeSMatthias Ringwald         hfp_connection->send_custom_message = NULL;
107651a2ebdeSMatthias Ringwald         hfp_connection->ok_pending = 1;
107751a2ebdeSMatthias Ringwald         hfp_connection->response_pending_for_command = HFP_CMD_CUSTOM_MESSAGE;
107851a2ebdeSMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, message);
107951a2ebdeSMatthias Ringwald         return;
108051a2ebdeSMatthias Ringwald     }
108151a2ebdeSMatthias Ringwald 
1082ce263fc8SMatthias Ringwald     if (done) return;
1083ce263fc8SMatthias Ringwald     // deal with disconnect
1084a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
1085ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
1086a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1087a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
1088ce263fc8SMatthias Ringwald             break;
1089ce263fc8SMatthias Ringwald 
1090ce263fc8SMatthias Ringwald         default:
1091ce263fc8SMatthias Ringwald             break;
1092ce263fc8SMatthias Ringwald     }
1093ce263fc8SMatthias Ringwald }
1094ce263fc8SMatthias Ringwald 
1095ad862247SMatthias Ringwald static void hfp_hf_slc_established(hfp_connection_t * hfp_connection){
1096a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
10976a7f44bdSMilanka Ringwald 
10987095467fSMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection->local_role, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
10997522e673SMatthias Ringwald 
1100184a03edSMilanka Ringwald     uint8_t i;
1101184a03edSMilanka Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
11021ac1f60fSMilanka Ringwald         hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1103184a03edSMilanka Ringwald     }
1104722a85f3SMilanka Ringwald 
1105722a85f3SMilanka Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1106722a85f3SMilanka Ringwald         hfp_connection->ag_indicators[i].status_changed = 0;
1107722a85f3SMilanka Ringwald         hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1108722a85f3SMilanka Ringwald     }
1109722a85f3SMilanka Ringwald 
1110667ec068SMatthias Ringwald     // restore volume settings
1111a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
1112a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1113ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
1114a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
1115a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1116ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
1117ce263fc8SMatthias Ringwald }
1118ce263fc8SMatthias Ringwald 
11191cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
1120aeb0f0feSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){
11211cc65c4fSMatthias Ringwald 		// Codec supported, confirm
11221cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
11231cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
11241cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
11251cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
11261cc65c4fSMatthias Ringwald 
11271cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
11281cc65c4fSMatthias Ringwald 	} else {
11291cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
11301cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
11311cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
11321cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
11331cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
11341cc65c4fSMatthias Ringwald 
11351cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
11361cc65c4fSMatthias Ringwald 	}
11371cc65c4fSMatthias Ringwald }
11381cc65c4fSMatthias Ringwald 
1139e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){
1140e2c3b58dSMilanka Ringwald     bool event_emited = true;
1141e2c3b58dSMilanka Ringwald 
1142b3e7b9f5SMatthias Ringwald     // cache state and reset
1143b3e7b9f5SMatthias Ringwald     hfp_command_t response_pending_for_command = hfp_connection->response_pending_for_command;
1144b3e7b9f5SMatthias Ringwald     hfp_connection->response_pending_for_command = HFP_CMD_NONE;
1145b3e7b9f5SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
1146b3e7b9f5SMatthias Ringwald     hfp_connection->ok_pending = 0;
1147b3e7b9f5SMatthias Ringwald 
1148b3e7b9f5SMatthias Ringwald     switch (response_pending_for_command){
1149e2c3b58dSMilanka Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1150e2c3b58dSMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status);
1151e2c3b58dSMilanka Ringwald             break;
115251a2ebdeSMatthias Ringwald         case HFP_CMD_CUSTOM_MESSAGE:
11537f8f1191SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_CUSTOM_AT_MESSAGE_SENT, status);
115451a2ebdeSMatthias Ringwald             break;
1155e2c3b58dSMilanka Ringwald         default:
1156e2c3b58dSMilanka Ringwald             event_emited = false;
1157e2c3b58dSMilanka Ringwald 
1158a0ffb263SMatthias Ringwald             switch (hfp_connection->state){
11593deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
1160a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
1161a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_NOTIFY_ON_CODECS;
11623deb3ec6SMatthias Ringwald                         break;
11633deb3ec6SMatthias Ringwald                     }
1164a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
11653deb3ec6SMatthias Ringwald                     break;
11663deb3ec6SMatthias Ringwald 
11673deb3ec6SMatthias Ringwald                 case HFP_W4_NOTIFY_ON_CODECS:
1168a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
11693deb3ec6SMatthias Ringwald                     break;
11703deb3ec6SMatthias Ringwald 
11713deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS:
1172a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
11733deb3ec6SMatthias Ringwald                     break;
11743deb3ec6SMatthias Ringwald 
11753deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1176a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
11773deb3ec6SMatthias Ringwald                     break;
11783deb3ec6SMatthias Ringwald 
11793deb3ec6SMatthias Ringwald                 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1180a0ffb263SMatthias Ringwald                     if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1181a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
11823deb3ec6SMatthias Ringwald                         break;
11833deb3ec6SMatthias Ringwald                     }
1184a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1185a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
11863deb3ec6SMatthias Ringwald                         break;
11873deb3ec6SMatthias Ringwald                     }
1188ad862247SMatthias Ringwald                     hfp_hf_slc_established(hfp_connection);
11893deb3ec6SMatthias Ringwald                     break;
11903deb3ec6SMatthias Ringwald 
11913deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1192a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1193a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
11943deb3ec6SMatthias Ringwald                         break;
11953deb3ec6SMatthias Ringwald                     }
1196ad862247SMatthias Ringwald                     hfp_hf_slc_established(hfp_connection);
11973deb3ec6SMatthias Ringwald                     break;
11983deb3ec6SMatthias Ringwald 
11993deb3ec6SMatthias Ringwald                 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1200a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
12013deb3ec6SMatthias Ringwald                     break;
12023deb3ec6SMatthias Ringwald 
12033deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1204a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
12053deb3ec6SMatthias Ringwald                     break;
12063deb3ec6SMatthias Ringwald 
12073deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1208ad862247SMatthias Ringwald                     hfp_hf_slc_established(hfp_connection);
12093deb3ec6SMatthias Ringwald                     break;
1210ce263fc8SMatthias Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1211a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1212a0ffb263SMatthias Ringwald                         hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
12136cc5b34bSMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, ERROR_CODE_SUCCESS);
1214ce263fc8SMatthias Ringwald                         break;
1215ce263fc8SMatthias Ringwald                     }
12163deb3ec6SMatthias Ringwald 
1217a0ffb263SMatthias Ringwald                     if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1218a0ffb263SMatthias Ringwald                         hfp_connection->change_status_update_for_individual_ag_indicators = 0;
12196cc5b34bSMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, ERROR_CODE_SUCCESS);
1220ce263fc8SMatthias Ringwald                         break;
12213deb3ec6SMatthias Ringwald                     }
12223deb3ec6SMatthias Ringwald 
1223a0ffb263SMatthias Ringwald                     switch (hfp_connection->hf_query_operator_state){
1224ce263fc8SMatthias Ringwald                         case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1225a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1226ce263fc8SMatthias Ringwald                             break;
1227ce263fc8SMatthias Ringwald                         case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1228a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1229a473a009SMatthias Ringwald                             hfp_emit_network_operator_event(hfp_connection);
1230ce263fc8SMatthias Ringwald                             break;
1231ce263fc8SMatthias Ringwald                         default:
1232ce263fc8SMatthias Ringwald                             break;
12333deb3ec6SMatthias Ringwald                     }
1234ce263fc8SMatthias Ringwald 
1235a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_extended_audio_gateway_error_report){
1236a0ffb263SMatthias Ringwald                         hfp_connection->enable_extended_audio_gateway_error_report = 0;
1237ce263fc8SMatthias Ringwald                         break;
12383deb3ec6SMatthias Ringwald                     }
12393deb3ec6SMatthias Ringwald 
1240a0ffb263SMatthias Ringwald                     switch (hfp_connection->codecs_state){
1241aa4dd815SMatthias Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1242a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
12433deb3ec6SMatthias Ringwald                             break;
1244ce263fc8SMatthias Ringwald                         case HFP_CODECS_HF_CONFIRMED_CODEC:
1245a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1246ce263fc8SMatthias Ringwald                             break;
12473deb3ec6SMatthias Ringwald                         default:
12483deb3ec6SMatthias Ringwald                             break;
12493deb3ec6SMatthias Ringwald                     }
1250af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
1251be55a11dSMilanka Ringwald                     break;
1252be55a11dSMilanka Ringwald                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
1253af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
12543deb3ec6SMatthias Ringwald                     break;
12553deb3ec6SMatthias Ringwald                 default:
12563deb3ec6SMatthias Ringwald                     break;
12573deb3ec6SMatthias Ringwald             }
1258e2c3b58dSMilanka Ringwald             break;
1259e2c3b58dSMilanka Ringwald     }
12603deb3ec6SMatthias Ringwald 
1261e2c3b58dSMilanka Ringwald     return event_emited;
12623deb3ec6SMatthias Ringwald }
12633deb3ec6SMatthias Ringwald 
1264a03dbc20SMilanka Ringwald static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){
1265a03dbc20SMilanka Ringwald     switch (callsetup_status){
1266a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1267a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1268a03dbc20SMilanka Ringwald             return true;
1269a03dbc20SMilanka Ringwald         default:
1270a03dbc20SMilanka Ringwald             return false;
1271a03dbc20SMilanka Ringwald    }
1272a03dbc20SMilanka Ringwald }
1273be55a11dSMilanka Ringwald 
1274b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
12754562e2a2SMatthias Ringwald     uint16_t i;
1276a03dbc20SMilanka Ringwald 
12774562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
12784562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
12794562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1280a03dbc20SMilanka Ringwald                 hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1281a03dbc20SMilanka Ringwald                 bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status);
1282a03dbc20SMilanka Ringwald                 bool ringing_new = hfp_is_ringing(new_hf_callsetup_status);
1283a03dbc20SMilanka Ringwald                 if (ringing_old != ringing_new){
1284a03dbc20SMilanka Ringwald                     if (ringing_new){
1285a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING);
1286a03dbc20SMilanka Ringwald                     } else {
1287a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING);
1288a03dbc20SMilanka Ringwald                     }
1289a03dbc20SMilanka Ringwald                 }
1290a03dbc20SMilanka Ringwald                 hfp_hf_callsetup_status = new_hf_callsetup_status;
12914562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1292aeb0f0feSMatthias Ringwald                 hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
12934562e2a2SMatthias Ringwald                 // avoid set but not used warning
1294aeb0f0feSMatthias Ringwald                 (void) hfp_hf_callheld_status;
12954562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1296674ebed5SMilanka Ringwald                 hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1297674ebed5SMilanka Ringwald                 if (hfp_hf_call_status != new_hf_call_status){
1298674ebed5SMilanka Ringwald                     if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){
1299674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1300674ebed5SMilanka Ringwald                     } else {
1301674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1302674ebed5SMilanka Ringwald                     }
1303674ebed5SMilanka Ringwald                 }
1304674ebed5SMilanka Ringwald                 hfp_hf_call_status = new_hf_call_status;
13054562e2a2SMatthias Ringwald             }
13064562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1307ce3797e1SMilanka Ringwald             hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]);
13084562e2a2SMatthias Ringwald             break;
13094562e2a2SMatthias Ringwald         }
13104562e2a2SMatthias Ringwald     }
13114562e2a2SMatthias Ringwald }
13124562e2a2SMatthias Ringwald 
1313426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1314186dd3d2SMatthias Ringwald     int value;
1315186dd3d2SMatthias Ringwald     int i;
1316e2c3b58dSMilanka Ringwald     bool event_emited;
1317e2c3b58dSMilanka Ringwald 
1318125560b8SMatthias Ringwald     // last argument is still in line_buffer
1319125560b8SMatthias Ringwald 
1320a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1321667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1322a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1323a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1324667ec068SMatthias Ringwald             break;
1325667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1326a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1327ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1328667ec068SMatthias Ringwald             break;
1329667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1330a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1331a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1332667ec068SMatthias Ringwald             break;
1333ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1334a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13352308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1336667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1337ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1338ce263fc8SMatthias Ringwald             break;
1339ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1340a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13412308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1342667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1343ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1344ce263fc8SMatthias Ringwald             break;
1345ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1346a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1347ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1348a0ffb263SMatthias Ringwald             break;
1349a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1350a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1351598d4936SMatthias Ringwald             hfp_hf_emit_type_number_alpha(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1352a0ffb263SMatthias Ringwald             break;
1353a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1354a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1355598d4936SMatthias Ringwald             hfp_hf_emit_type_number_alpha(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1356ce263fc8SMatthias Ringwald             break;
1357ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1358a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13595a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1360a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1361ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1362ce263fc8SMatthias Ringwald             break;
13630b4debbfSMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
13640b4debbfSMilanka Ringwald             break;
1365fdda66c0SMilanka Ringwald         case HFP_CMD_ERROR:
136690244c92SMilanka Ringwald             switch (hfp_connection->state){
136790244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
136890244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
136990244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1370fdda66c0SMilanka Ringwald                             hfp_reset_context_flags(hfp_connection);
1371f14c5dafSMatthias Ringwald                             hfp_emit_sco_connection_established(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION,
13725e8e3664SMatthias Ringwald                                                                 hfp_connection->negotiated_codec, 0, 0);
137390244c92SMilanka Ringwald                             return;
137490244c92SMilanka Ringwald                         default:
137590244c92SMilanka Ringwald                             break;
137690244c92SMilanka Ringwald                     }
137756f1adacSMilanka Ringwald                     break;
137856f1adacSMilanka Ringwald                 default:
137956f1adacSMilanka Ringwald                     break;
138056f1adacSMilanka Ringwald             }
1381e2c3b58dSMilanka Ringwald 
1382fdda66c0SMilanka Ringwald             // handle error response for voice activation (HF initiated)
13830b4debbfSMilanka Ringwald             switch(hfp_connection->vra_state_requested){
1384de9e0ea7SMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1385de9e0ea7SMilanka Ringwald                     hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1386be55a11dSMilanka Ringwald                     break;
1387be55a11dSMilanka Ringwald                 default:
1388e2c3b58dSMilanka Ringwald                     if (hfp_connection->vra_state_requested == hfp_connection->vra_state){
1389e2c3b58dSMilanka Ringwald                         break;
1390e2c3b58dSMilanka Ringwald                     }
13910b4debbfSMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
1392553a4a56SMilanka Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
13930b4debbfSMilanka Ringwald                     hfp_reset_context_flags(hfp_connection);
13940b4debbfSMilanka Ringwald                     return;
1395be55a11dSMilanka Ringwald             }
1396e2c3b58dSMilanka Ringwald             event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1397e2c3b58dSMilanka Ringwald             if (!event_emited){
13986cc5b34bSMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, ERROR_CODE_UNSPECIFIED_ERROR);
1399e2c3b58dSMilanka Ringwald             }
1400fdda66c0SMilanka Ringwald             hfp_reset_context_flags(hfp_connection);
1401ce263fc8SMatthias Ringwald             break;
1402fdda66c0SMilanka Ringwald 
1403ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1404e2c3b58dSMilanka Ringwald             hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS);
1405ce263fc8SMatthias Ringwald             break;
1406ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
14075a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1408ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1409ce263fc8SMatthias Ringwald             break;
1410ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
14115a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
14124562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1413ce263fc8SMatthias Ringwald             break;
1414c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
14155a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1416184a03edSMilanka Ringwald             // report status after SLC established
1417184a03edSMilanka Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
1418184a03edSMilanka Ringwald                 break;
1419184a03edSMilanka Ringwald             }
1420c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
14211ac1f60fSMilanka Ringwald                 hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1422c741b032SMilanka Ringwald             }
1423c741b032SMilanka Ringwald             break;
14241cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
14251cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
14265a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
14271cc65c4fSMatthias Ringwald 			break;
1428eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1429eac56539SMilanka 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));
14305134fea9SMatthias Ringwald             break;
1431892f58a8SMatthias Ringwald         case HFP_CMD_CUSTOM_MESSAGE:
1432892f58a8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1433892f58a8SMatthias Ringwald             hfp_parser_reset_line_buffer(hfp_connection);
1434892f58a8SMatthias Ringwald             log_info("Custom AT Command ID 0x%04x", hfp_connection->custom_at_command_id);
1435892f58a8SMatthias Ringwald             hfp_hf_emit_custom_command_event(hfp_connection);
1436892f58a8SMatthias Ringwald             break;
1437ce263fc8SMatthias Ringwald         default:
1438ce263fc8SMatthias Ringwald             break;
14393deb3ec6SMatthias Ringwald     }
14400cef86faSMatthias Ringwald }
1441426f9988SMatthias Ringwald 
144276cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
144376cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
144476cc1527SMatthias Ringwald }
144576cc1527SMatthias Ringwald 
14460b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1447426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1448426f9988SMatthias Ringwald     if (size < 1) return;
1449426f9988SMatthias Ringwald 
1450426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1451e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1452e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1453e43d1938SMatthias Ringwald #endif
1454426f9988SMatthias Ringwald 
1455426f9988SMatthias Ringwald     // process messages byte-wise
1456a7ba78b0SMilanka Ringwald     uint8_t pos;
1457426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1458426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
14591599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1460426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
14610b4debbfSMilanka Ringwald         hfp_hf_handle_rfcomm_command(hfp_connection);
14623deb3ec6SMatthias Ringwald     }
1463a7ba78b0SMilanka Ringwald }
14643deb3ec6SMatthias Ringwald 
14651c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1466665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1467665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1468665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1469a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
147022387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
14711c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
14723deb3ec6SMatthias Ringwald     }
14733deb3ec6SMatthias Ringwald }
14743deb3ec6SMatthias Ringwald 
14751c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
14760b4debbfSMilanka Ringwald     hfp_connection_t * hfp_connection;
14773deb3ec6SMatthias Ringwald     switch (packet_type){
14783deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
14790b4debbfSMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
14800b4debbfSMilanka Ringwald             if (!hfp_connection) return;
14810b4debbfSMilanka Ringwald             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
14820b4debbfSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
14830b4debbfSMilanka Ringwald             return;
14843deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1485d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1486d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
14871c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1488d4dd47ffSMatthias Ringwald                 return;
1489d4dd47ffSMatthias Ringwald             }
149027950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1491202c8a4cSMatthias Ringwald             break;
14923deb3ec6SMatthias Ringwald         default:
14933deb3ec6SMatthias Ringwald             break;
14943deb3ec6SMatthias Ringwald     }
14951c6a0fc0SMatthias Ringwald     hfp_hf_run();
14963deb3ec6SMatthias Ringwald }
14973deb3ec6SMatthias Ringwald 
14981c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1499405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
15001c6a0fc0SMatthias Ringwald     hfp_hf_run();
1501405014fbSMatthias Ringwald }
1502405014fbSMatthias Ringwald 
1503aeb0f0feSMatthias Ringwald static void hfp_hf_set_defaults(void){
1504aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1505aeb0f0feSMatthias Ringwald     hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
1506aeb0f0feSMatthias Ringwald     hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1507aeb0f0feSMatthias Ringwald     hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1508aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = 0;
1509aeb0f0feSMatthias Ringwald     hfp_hf_speaker_gain = 9;
1510aeb0f0feSMatthias Ringwald     hfp_hf_microphone_gain = 9;
1511aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = 0;
1512aeb0f0feSMatthias Ringwald }
1513aeb0f0feSMatthias Ringwald 
151442aadee5SMatthias Ringwald uint8_t hfp_hf_set_default_microphone_gain(uint8_t gain){
1515455808b3SMatthias Ringwald     if (gain > 15){
151642aadee5SMatthias Ringwald         return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
151742aadee5SMatthias Ringwald     }
151842aadee5SMatthias Ringwald     hfp_hf_microphone_gain = gain;
151942aadee5SMatthias Ringwald     return ERROR_CODE_SUCCESS;
152042aadee5SMatthias Ringwald }
152142aadee5SMatthias Ringwald 
152242aadee5SMatthias Ringwald uint8_t hfp_hf_set_default_speaker_gain(uint8_t gain){
1523455808b3SMatthias Ringwald     if (gain > 15){
152442aadee5SMatthias Ringwald         return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
152542aadee5SMatthias Ringwald     }
152642aadee5SMatthias Ringwald     hfp_hf_speaker_gain = gain;
152742aadee5SMatthias Ringwald     return ERROR_CODE_SUCCESS;
152842aadee5SMatthias Ringwald }
152942aadee5SMatthias Ringwald 
1530ab2445a0SMatthias Ringwald uint8_t hfp_hf_init(uint8_t rfcomm_channel_nr){
1531b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1532b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1533b4df8028SMilanka Ringwald         return status;
1534b4df8028SMilanka Ringwald     }
1535b4df8028SMilanka Ringwald 
1536520c92d5SMatthias Ringwald     hfp_init();
1537aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1538d63c37a1SMatthias Ringwald 
15391c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
15401c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
154127950165SMatthias Ringwald 
154227950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
15431c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1544b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
154520b2edb6SMatthias Ringwald }
154627950165SMatthias Ringwald 
154720b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
154820b2edb6SMatthias Ringwald     hfp_deinit();
1549aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1550aeb0f0feSMatthias Ringwald 
1551aeb0f0feSMatthias Ringwald     hfp_hf_callback = NULL;
155220b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
1553aeb0f0feSMatthias Ringwald     (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number));
1554a0ffb263SMatthias Ringwald }
1555a0ffb263SMatthias Ringwald 
1556aa10b9cbSMatthias Ringwald void hfp_hf_init_codecs(uint8_t codecs_nr, const uint8_t * codecs){
15572be4ddd3SMatthias Ringwald     btstack_assert(codecs_nr <= HFP_MAX_NUM_CODECS);
15583deb3ec6SMatthias Ringwald 
1559aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = codecs_nr;
1560aa10b9cbSMatthias Ringwald     uint8_t i;
15613deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
1562aeb0f0feSMatthias Ringwald         hfp_hf_codecs[i] = codecs[i];
15633deb3ec6SMatthias Ringwald     }
15643deb3ec6SMatthias Ringwald }
15653deb3ec6SMatthias Ringwald 
1566a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
1567aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = supported_features;
1568a0ffb263SMatthias Ringwald }
15693deb3ec6SMatthias Ringwald 
15707ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
1571aeb0f0feSMatthias Ringwald     btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS);
157268466199SMilanka Ringwald 
1573aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = indicators_nr;
15743deb3ec6SMatthias Ringwald     int i;
1575aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
1576ab2445a0SMatthias Ringwald         hfp_hf_indicators[i] = (uint8_t) indicators[i];
15773deb3ec6SMatthias Ringwald     }
157821df969bSMatthias Ringwald 
157921df969bSMatthias Ringwald     // store copy in hfp to setup generic_status_indicators during SLC
158021df969bSMatthias Ringwald     hfp_set_hf_indicators(indicators_nr, hfp_hf_indicators);
15813deb3ec6SMatthias Ringwald }
15823deb3ec6SMatthias Ringwald 
15834eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
15844eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
15853deb3ec6SMatthias Ringwald }
15863deb3ec6SMatthias Ringwald 
1587657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
15889c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1589a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1590657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1591a33eb0c4SMilanka Ringwald     }
15921ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
15931c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1594657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
15953deb3ec6SMatthias Ringwald }
15963deb3ec6SMatthias Ringwald 
15973c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
15989c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1599a0ffb263SMatthias Ringwald     if (!hfp_connection) {
16003c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
16013deb3ec6SMatthias Ringwald     }
1602a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
16031c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16043c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16053deb3ec6SMatthias Ringwald }
16063deb3ec6SMatthias Ringwald 
16073c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
16083c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1609ce263fc8SMatthias Ringwald }
1610ce263fc8SMatthias Ringwald 
16113c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
16123c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1613ce263fc8SMatthias Ringwald }
1614ce263fc8SMatthias Ringwald 
16153deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
16163c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
16179c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1618a0ffb263SMatthias Ringwald     if (!hfp_connection) {
16193c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
16203deb3ec6SMatthias Ringwald     }
1621a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1622a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
16231c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16243c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16253deb3ec6SMatthias Ringwald }
16263deb3ec6SMatthias Ringwald 
16273c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
16289c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1629a0ffb263SMatthias Ringwald     if (!hfp_connection) {
16303c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
16313deb3ec6SMatthias Ringwald     }
16323c65e705SMilanka Ringwald 
1633a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1634ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1635a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1636ce263fc8SMatthias Ringwald             break;
1637ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1638a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1639ce263fc8SMatthias Ringwald             break;
1640ce263fc8SMatthias Ringwald         default:
16413c65e705SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1642ce263fc8SMatthias Ringwald     }
16431c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16443c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16453deb3ec6SMatthias Ringwald }
16463deb3ec6SMatthias Ringwald 
16473c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
16489c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1649a0ffb263SMatthias Ringwald     if (!hfp_connection) {
16503c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
16513deb3ec6SMatthias Ringwald     }
1652a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
16531c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16543c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16553deb3ec6SMatthias Ringwald }
16563deb3ec6SMatthias Ringwald 
1657ce263fc8SMatthias Ringwald 
16583c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
16593c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1660ce263fc8SMatthias Ringwald }
1661ce263fc8SMatthias Ringwald 
16623c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
16633c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1664ce263fc8SMatthias Ringwald }
1665ce263fc8SMatthias Ringwald 
166638200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
1667aeb0f0feSMatthias Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4));
166838200c1dSMilanka Ringwald }
1669ce263fc8SMatthias Ringwald 
16703c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
16719c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1672a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16733c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1674a33eb0c4SMilanka Ringwald     }
1675ce263fc8SMatthias Ringwald 
16763c65e705SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
16773c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16783c65e705SMilanka Ringwald     }
16793c65e705SMilanka Ringwald 
16803c65e705SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
16813c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16823c65e705SMilanka Ringwald     }
1683f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1684a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1685aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1686aa4dd815SMatthias Ringwald                 break;
1687ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1688ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1689ec3bfc1aSMatthias Ringwald                 break;
1690aa4dd815SMatthias Ringwald             default:
16911cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
16921cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
16931cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
16941cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
169538200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1696aa4dd815SMatthias Ringwald                 break;
16973deb3ec6SMatthias Ringwald         }
1698f4412093SMatthias Ringwald     } else {
1699f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1700f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1701f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1702f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1703f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1704f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1705f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1706f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1707ce263fc8SMatthias Ringwald     }
1708ce263fc8SMatthias Ringwald 
17091c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17103c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
17113deb3ec6SMatthias Ringwald }
17123deb3ec6SMatthias Ringwald 
17133c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
17149c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1715a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17163c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1717a33eb0c4SMilanka Ringwald     }
17180b4debbfSMilanka Ringwald     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
17190b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
17200b4debbfSMilanka Ringwald     }
17210b4debbfSMilanka Ringwald     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
17220b4debbfSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
17231c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
17240b4debbfSMilanka Ringwald     }
17253c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
17263deb3ec6SMatthias Ringwald }
17273deb3ec6SMatthias Ringwald 
17283c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
17299c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1730a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17313c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1732a33eb0c4SMilanka Ringwald     }
1733ce263fc8SMatthias Ringwald 
1734aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1735a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
17361c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1737ce263fc8SMatthias Ringwald     } else {
1738aeb0f0feSMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status);
17393c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1740ce263fc8SMatthias Ringwald     }
17413c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1742ce263fc8SMatthias Ringwald }
1743ce263fc8SMatthias Ringwald 
17443c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
17459c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1746a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17473c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1748a33eb0c4SMilanka Ringwald     }
1749a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
17501c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17513c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1752ce263fc8SMatthias Ringwald }
1753ce263fc8SMatthias Ringwald 
17543c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
17559c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1756a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17573c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1758a33eb0c4SMilanka Ringwald     }
1759ce263fc8SMatthias Ringwald 
1760aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1761a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 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_user_busy(hci_con_handle_t acl_handle){
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     }
1772ce263fc8SMatthias Ringwald 
1773aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1774a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
17751c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1776ce263fc8SMatthias Ringwald     }
17773c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1778ce263fc8SMatthias Ringwald }
1779ce263fc8SMatthias Ringwald 
17802be52304SMatthias Ringwald uint8_t hfp_hf_terminate_held_calls(hci_con_handle_t acl_handle){
17812be52304SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
17822be52304SMatthias Ringwald     if (!hfp_connection) {
17832be52304SMatthias Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
17842be52304SMatthias Ringwald     }
17852be52304SMatthias Ringwald 
17862be52304SMatthias Ringwald     hfp_connection->hf_send_chld_0 = 1;
17872be52304SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17882be52304SMatthias Ringwald 
17892be52304SMatthias Ringwald     return ERROR_CODE_SUCCESS;
17902be52304SMatthias Ringwald }
17912be52304SMatthias Ringwald 
17923c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
17939c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1794a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17953c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1796a33eb0c4SMilanka Ringwald     }
1797ce263fc8SMatthias Ringwald 
1798aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1799aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1800a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
18011c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1802ce263fc8SMatthias Ringwald     }
18033c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1804ce263fc8SMatthias Ringwald }
1805ce263fc8SMatthias Ringwald 
18063c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
18079c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1808a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18093c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1810a33eb0c4SMilanka Ringwald     }
1811ce263fc8SMatthias Ringwald 
1812aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1813aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1814a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
18151c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1816ce263fc8SMatthias Ringwald     }
18173c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1818ce263fc8SMatthias Ringwald }
1819ce263fc8SMatthias Ringwald 
18203c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
18219c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1822a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18233c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1824a33eb0c4SMilanka Ringwald     }
1825ce263fc8SMatthias Ringwald 
1826aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1827aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1828a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
18291c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1830ce263fc8SMatthias Ringwald     }
18313c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1832ce263fc8SMatthias Ringwald }
1833ce263fc8SMatthias Ringwald 
18343c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
18359c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1836a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18373c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1838a33eb0c4SMilanka Ringwald     }
1839ce263fc8SMatthias Ringwald 
1840aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1841aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1842a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
18431c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1844ce263fc8SMatthias Ringwald     }
18453c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1846ce263fc8SMatthias Ringwald }
1847ce263fc8SMatthias Ringwald 
18483c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
18499c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1850a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18513c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1852a33eb0c4SMilanka Ringwald     }
1853667ec068SMatthias Ringwald 
1854aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1855aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1856a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1857a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
18581c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1859667ec068SMatthias Ringwald     }
18603c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1861667ec068SMatthias Ringwald }
1862667ec068SMatthias Ringwald 
18633c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
18649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1865a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18663c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1867a33eb0c4SMilanka Ringwald     }
1868667ec068SMatthias Ringwald 
1869aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1870aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1871a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1872a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
18731c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1874667ec068SMatthias Ringwald     }
18753c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1876667ec068SMatthias Ringwald }
1877ce263fc8SMatthias Ringwald 
18783c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
18799c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1880a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18813c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1882a33eb0c4SMilanka Ringwald     }
1883ce263fc8SMatthias Ringwald 
1884a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1885aeb0f0feSMatthias Ringwald     snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number);
18861c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18873c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1888ce263fc8SMatthias Ringwald }
1889ce263fc8SMatthias Ringwald 
18903c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
18919c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1892a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18933c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1894a33eb0c4SMilanka Ringwald     }
1895ce263fc8SMatthias Ringwald 
1896a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1897a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1898a0ffb263SMatthias Ringwald 
18991c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19003c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1901ce263fc8SMatthias Ringwald }
1902ce263fc8SMatthias Ringwald 
19033c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
19049c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1905a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19063c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1907a33eb0c4SMilanka Ringwald     }
1908ce263fc8SMatthias Ringwald 
1909a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
19101c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19113c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1912ce263fc8SMatthias Ringwald }
1913ce263fc8SMatthias Ringwald 
19143c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
19159c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1916a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19173c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1918a33eb0c4SMilanka Ringwald     }
1919ce263fc8SMatthias Ringwald 
1920a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
19211c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19223c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1923ce263fc8SMatthias Ringwald }
1924ce263fc8SMatthias Ringwald 
1925ce263fc8SMatthias Ringwald 
19263c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
19279c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1928a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19293c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1930a33eb0c4SMilanka Ringwald     }
1931ce263fc8SMatthias Ringwald 
1932a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
19331c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19343c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1935ce263fc8SMatthias Ringwald }
1936ce263fc8SMatthias Ringwald 
1937ce263fc8SMatthias Ringwald 
19383c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
19399c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1940a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19413c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1942a33eb0c4SMilanka Ringwald     }
1943ce263fc8SMatthias Ringwald 
1944a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
19451c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19463c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1947ce263fc8SMatthias Ringwald }
1948ce263fc8SMatthias Ringwald 
19493c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
19509c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1951a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19523c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1953a33eb0c4SMilanka Ringwald     }
1954ce263fc8SMatthias Ringwald 
1955a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
19561c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19573c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1958ce263fc8SMatthias Ringwald }
1959ce263fc8SMatthias Ringwald 
19603c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
19619c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1962a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19633c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1964a33eb0c4SMilanka Ringwald     }
196514685fd3SMatthias Ringwald     if (get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION) == 0){
19666ba83b5eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
19676ba83b5eSMilanka Ringwald     }
1968ce263fc8SMatthias Ringwald 
1969a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
19701c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19713c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1972ce263fc8SMatthias Ringwald }
1973ce263fc8SMatthias Ringwald 
1974acd11d4aSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1975fdda66c0SMilanka Ringwald      hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1976fdda66c0SMilanka Ringwald     if (!hfp_connection) {
1977fdda66c0SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1978be55a11dSMilanka Ringwald     }
1979013cc750SMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1980013cc750SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1981013cc750SMilanka Ringwald     }
1982acd11d4aSMilanka Ringwald 
1983acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1984acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1985acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
1986acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1987af97579eSMilanka Ringwald     }
1988af97579eSMilanka Ringwald 
1989498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1990be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1991de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1992fd4151d1SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
1993acd11d4aSMilanka Ringwald             hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported;
1994be55a11dSMilanka Ringwald             break;
1995de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1996de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = true;
1997de9e0ea7SMilanka Ringwald             break;
1998be55a11dSMilanka Ringwald         default:
1999be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
2000be55a11dSMilanka Ringwald     }
2001ce263fc8SMatthias Ringwald 
2002af97579eSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
2003fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2004af97579eSMilanka Ringwald }
2005af97579eSMilanka Ringwald 
2006acd11d4aSMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
2007af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2008af97579eSMilanka Ringwald     if (!hfp_connection) {
2009af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2010af97579eSMilanka Ringwald     }
201184fb9ac1SMilanka Ringwald 
201284fb9ac1SMilanka Ringwald     if (hfp_connection->emit_vra_enabled_after_audio_established){
201384fb9ac1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
201484fb9ac1SMilanka Ringwald     }
201584fb9ac1SMilanka Ringwald 
2016acd11d4aSMilanka Ringwald     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
201708a0b01cSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
201808a0b01cSMilanka Ringwald     }
2019acd11d4aSMilanka Ringwald 
2020acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
2021acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported){
2022acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
2023acd11d4aSMilanka Ringwald     }
2024acd11d4aSMilanka Ringwald 
2025acd11d4aSMilanka Ringwald     switch (hfp_connection->vra_state){
2026acd11d4aSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
2027acd11d4aSMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
2028acd11d4aSMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
2029acd11d4aSMilanka Ringwald             break;
2030acd11d4aSMilanka Ringwald         default:
2031fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
2032af97579eSMilanka Ringwald     }
2033013cc750SMilanka Ringwald 
2034acd11d4aSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
2035acd11d4aSMilanka Ringwald     return ERROR_CODE_SUCCESS;
2036acd11d4aSMilanka Ringwald }
2037acd11d4aSMilanka Ringwald 
2038acd11d4aSMilanka Ringwald 
2039acd11d4aSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
2040acd11d4aSMilanka Ringwald     // return deactivate_voice_recognition(acl_handle, false);
2041acd11d4aSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2042acd11d4aSMilanka Ringwald     if (!hfp_connection) {
2043acd11d4aSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2044acd11d4aSMilanka Ringwald     }
2045acd11d4aSMilanka Ringwald 
204684fb9ac1SMilanka Ringwald     if (hfp_connection->emit_vra_enabled_after_audio_established){
204784fb9ac1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
204884fb9ac1SMilanka Ringwald     }
204984fb9ac1SMilanka Ringwald 
2050acd11d4aSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
2051acd11d4aSMilanka Ringwald         hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
2052acd11d4aSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2053acd11d4aSMilanka Ringwald     }
2054acd11d4aSMilanka Ringwald 
2055acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
2056acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
2057acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
2058acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
2059acd11d4aSMilanka Ringwald     }
2060acd11d4aSMilanka Ringwald 
2061fdda66c0SMilanka Ringwald     switch (hfp_connection->vra_state){
2062de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
2063fdda66c0SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
2064de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
2065de9e0ea7SMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
2066fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
2067fdda66c0SMilanka Ringwald             break;
2068de9e0ea7SMilanka Ringwald 
2069de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
2070de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
2071de9e0ea7SMilanka Ringwald             hfp_connection->deactivate_voice_recognition = true;
2072de9e0ea7SMilanka Ringwald             break;
2073de9e0ea7SMilanka Ringwald 
2074de9e0ea7SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
2075de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
2076de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
2077fdda66c0SMilanka Ringwald         default:
2078fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
2079fdda66c0SMilanka Ringwald     }
2080fdda66c0SMilanka Ringwald 
2081fdda66c0SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
2082fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2083af97579eSMilanka Ringwald }
2084af97579eSMilanka Ringwald 
20853c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
20869c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2087a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20883c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2089a33eb0c4SMilanka Ringwald     }
2090c8626498SMilanka Ringwald 
20913c65e705SMilanka Ringwald     if (hfp_connection->microphone_gain == gain) {
20923c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
20933c65e705SMilanka Ringwald     }
20943c65e705SMilanka Ringwald 
2095c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
2096a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
20973c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2098a0ffb263SMatthias Ringwald     }
20993c65e705SMilanka Ringwald 
2100a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
2101a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
21021c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21033c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2104ce263fc8SMatthias Ringwald }
2105ce263fc8SMatthias Ringwald 
21063c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
21079c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2108a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21093c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2110a33eb0c4SMilanka Ringwald     }
2111c8626498SMilanka Ringwald 
21123c65e705SMilanka Ringwald     if (hfp_connection->speaker_gain == gain){
21133c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
21143c65e705SMilanka Ringwald     }
21153c65e705SMilanka Ringwald 
2116c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
2117a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
21183c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2119a0ffb263SMatthias Ringwald     }
21203c65e705SMilanka Ringwald 
2121a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
2122a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
21231c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21243c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2125ce263fc8SMatthias Ringwald }
2126ce263fc8SMatthias Ringwald 
21273c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
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_dtmf_code = code;
21331c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21343c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2135ce263fc8SMatthias Ringwald }
2136ce263fc8SMatthias Ringwald 
21373c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
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     }
2142a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
21431c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21443c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2145ce263fc8SMatthias Ringwald }
21463deb3ec6SMatthias Ringwald 
21473c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
21489c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2149a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21503c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2151a33eb0c4SMilanka Ringwald     }
2152a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
21531c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21543c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2155667ec068SMatthias Ringwald }
2156667ec068SMatthias Ringwald 
2157667ec068SMatthias Ringwald 
21583c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
21599c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2160a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21613c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2162a33eb0c4SMilanka Ringwald     }
2163a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2164a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
21651c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21663c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2167667ec068SMatthias Ringwald }
2168667ec068SMatthias Ringwald 
21693c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
21709c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2171a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21723c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2173a33eb0c4SMilanka Ringwald     }
2174a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2175a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
21761c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21773c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2178667ec068SMatthias Ringwald }
2179667ec068SMatthias Ringwald 
21803c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
21819c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2182a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21833c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2184a33eb0c4SMilanka Ringwald     }
2185a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2186a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
21871c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21883c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2189667ec068SMatthias Ringwald }
2190667ec068SMatthias Ringwald 
21913c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
21929c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2193a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21943c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2195a33eb0c4SMilanka Ringwald     }
2196a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2197a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
21981c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21993c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2200667ec068SMatthias Ringwald }
2201667ec068SMatthias Ringwald 
22023c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
22039c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2204a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
22053c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2206a33eb0c4SMilanka Ringwald     }
2207a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
22081c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
22093c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2210667ec068SMatthias Ringwald }
2211667ec068SMatthias Ringwald 
22123c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
22139c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2214a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
22153c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2216a33eb0c4SMilanka Ringwald     }
2217667ec068SMatthias Ringwald     // find index for assigned number
2218667ec068SMatthias Ringwald     int i;
2219aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
2220aeb0f0feSMatthias Ringwald         if (hfp_hf_indicators[i] == assigned_number){
2221667ec068SMatthias Ringwald             // set value
2222aeb0f0feSMatthias Ringwald             hfp_hf_indicators_value[i] = value;
2223667ec068SMatthias Ringwald             // mark for update
2224a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
2225a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
2226667ec068SMatthias Ringwald                 // send update
22271c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
2228a0ffb263SMatthias Ringwald             }
22293c65e705SMilanka Ringwald             return ERROR_CODE_SUCCESS;
2230667ec068SMatthias Ringwald         }
2231667ec068SMatthias Ringwald     }
22323c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2233667ec068SMatthias Ringwald }
2234667ec068SMatthias Ringwald 
223551a2ebdeSMatthias Ringwald uint8_t hfp_hf_send_at_command(hci_con_handle_t acl_handle, const char * at_command){
223651a2ebdeSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
223751a2ebdeSMatthias Ringwald     if (!hfp_connection) {
223851a2ebdeSMatthias Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
223951a2ebdeSMatthias Ringwald     }
224051a2ebdeSMatthias Ringwald     if (hfp_connection->send_custom_message != NULL){
224151a2ebdeSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
224251a2ebdeSMatthias Ringwald     }
224351a2ebdeSMatthias Ringwald     hfp_connection->send_custom_message = at_command;
224451a2ebdeSMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
224551a2ebdeSMatthias Ringwald     return ERROR_CODE_SUCCESS;
224651a2ebdeSMatthias Ringwald }
224751a2ebdeSMatthias Ringwald 
2248d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
2249d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2250d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
2251d7f6b5cbSMatthias Ringwald         return 0;
2252d7f6b5cbSMatthias Ringwald     }
2253d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
2254d7f6b5cbSMatthias Ringwald }
225576cc1527SMatthias Ringwald 
2256aa10b9cbSMatthias Ringwald void hfp_hf_create_sdp_record_with_codecs(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr,
2257aa10b9cbSMatthias Ringwald                                           const char * name, uint16_t supported_features, uint8_t codecs_nr, const uint8_t * codecs){
225876cc1527SMatthias Ringwald     if (!name){
2259aeb0f0feSMatthias Ringwald         name = hfp_hf_default_service_name;
226076cc1527SMatthias Ringwald     }
226176cc1527SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
226276cc1527SMatthias Ringwald 
226376cc1527SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
226476cc1527SMatthias Ringwald     //
226576cc1527SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
226676cc1527SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
226776cc1527SMatthias Ringwald     //
2268aa10b9cbSMatthias Ringwald     // Wide band speech (bit 5) and LC3-SWB (bit 8) require Codec negotiation
226976cc1527SMatthias Ringwald     //
227076cc1527SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
2271ef3ae4ebSMilanka Ringwald 
2272ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
227356f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
2274ef3ae4ebSMilanka Ringwald     }
2275ef3ae4ebSMilanka Ringwald 
2276ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
227756f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
2278ef3ae4ebSMilanka Ringwald     }
2279ef3ae4ebSMilanka Ringwald 
2280aa10b9cbSMatthias Ringwald     // codecs
2281aa10b9cbSMatthias Ringwald     if ((supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION)) != 0){
2282aa10b9cbSMatthias Ringwald         uint8_t i;
2283aa10b9cbSMatthias Ringwald         for (i=0;i<codecs_nr;i++){
2284aa10b9cbSMatthias Ringwald             switch (codecs[i]){
2285aa10b9cbSMatthias Ringwald                 case HFP_CODEC_MSBC:
2286aa10b9cbSMatthias Ringwald                     sdp_features |= 1 << 5;
2287aa10b9cbSMatthias Ringwald                     break;
2288aa10b9cbSMatthias Ringwald                 case HFP_CODEC_LC3_SWB:
2289aa10b9cbSMatthias Ringwald                     sdp_features |= 1 << 8;
2290aa10b9cbSMatthias Ringwald                     break;
229175389f80SMatthias Ringwald                 default:
229275389f80SMatthias Ringwald                     break;
2293aa10b9cbSMatthias Ringwald             }
2294aa10b9cbSMatthias Ringwald         }
2295aa10b9cbSMatthias Ringwald     }
2296aa10b9cbSMatthias Ringwald 
2297aa10b9cbSMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SUPPORTED_FEATURES);
229876cc1527SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
229976cc1527SMatthias Ringwald }
230076cc1527SMatthias Ringwald 
2301aa10b9cbSMatthias Ringwald // @deprecated, call new API
2302aa10b9cbSMatthias 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){
2303aa10b9cbSMatthias Ringwald     uint8_t codecs_nr;
2304aa10b9cbSMatthias Ringwald     const uint8_t * codecs;
2305aa10b9cbSMatthias Ringwald     const uint8_t wide_band_codecs[] = { HFP_CODEC_MSBC };
2306aa10b9cbSMatthias Ringwald     if (wide_band_speech == 0){
2307aa10b9cbSMatthias Ringwald         codecs_nr = 0;
2308aa10b9cbSMatthias Ringwald         codecs = NULL;
2309aa10b9cbSMatthias Ringwald     } else {
2310aa10b9cbSMatthias Ringwald         codecs_nr = 1;
2311aa10b9cbSMatthias Ringwald         codecs = wide_band_codecs;
2312aa10b9cbSMatthias Ringwald     }
2313aa10b9cbSMatthias Ringwald     hfp_hf_create_sdp_record_with_codecs(service, service_record_handle, rfcomm_channel_nr, name, supported_features, codecs_nr, codecs);
2314aa10b9cbSMatthias Ringwald }
2315aa10b9cbSMatthias Ringwald 
2316077c2d92SMatthias Ringwald void hfp_hf_register_custom_at_command(hfp_custom_at_command_t * custom_at_command){
2317077c2d92SMatthias Ringwald     hfp_register_custom_hf_command(custom_at_command);
2318077c2d92SMatthias Ringwald }
2319077c2d92SMatthias Ringwald 
232076cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
232168466199SMilanka Ringwald 	btstack_assert(callback != NULL);
232268466199SMilanka Ringwald 
232376cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
232476cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
232576cc1527SMatthias Ringwald }
2326