xref: /btstack/src/classic/hfp_hf.c (revision 51a2ebde0031fdd3ad6bb64362108b25da7cb703)
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 
27876cc1527SMatthias Ringwald /* send commands */
27989425bfcSMilanka Ringwald 
28089425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2813deb3ec6SMatthias Ringwald     char buffer[20];
2821599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
28389425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
28489425bfcSMilanka Ringwald }
28589425bfcSMilanka Ringwald 
28689425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
28789425bfcSMilanka Ringwald     char buffer[20];
2881599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
28989425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
29089425bfcSMilanka Ringwald }
29189425bfcSMilanka Ringwald 
29286da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
29389425bfcSMilanka Ringwald     char buffer[40];
2941599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
2953deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2963deb3ec6SMatthias Ringwald }
2973deb3ec6SMatthias Ringwald 
2983deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2993deb3ec6SMatthias Ringwald     char buffer[30];
30089425bfcSMilanka Ringwald     const int size = sizeof(buffer);
30189425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
302aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_codecs, hfp_hf_codecs_nr);
3031599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
3043deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3053deb3ec6SMatthias Ringwald }
3063deb3ec6SMatthias Ringwald 
3073deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
3083deb3ec6SMatthias Ringwald     char buffer[50];
30989425bfcSMilanka Ringwald     const int size = sizeof(buffer);
31089425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
31189425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
3121599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
3133deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3143deb3ec6SMatthias Ringwald }
3153deb3ec6SMatthias Ringwald 
3163deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
3173deb3ec6SMatthias Ringwald     char buffer[30];
31889425bfcSMilanka Ringwald     const int size = sizeof(buffer);
31989425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
320aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_indicators, hfp_hf_indicators_nr);
3211599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
3223deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3233deb3ec6SMatthias Ringwald }
3243deb3ec6SMatthias Ringwald 
32589425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
3263deb3ec6SMatthias Ringwald     char buffer[20];
3271599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
328ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
329ce263fc8SMatthias Ringwald }
330ce263fc8SMatthias Ringwald 
331ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
332ce263fc8SMatthias Ringwald     char buffer[40];
333aeb0f0feSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, hfp_hf_phone_number);
334ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
335ce263fc8SMatthias Ringwald }
336ce263fc8SMatthias Ringwald 
337a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
338ce263fc8SMatthias Ringwald     char buffer[40];
3391599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
340ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
341ce263fc8SMatthias Ringwald }
342ce263fc8SMatthias Ringwald 
343f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
34489425bfcSMilanka Ringwald     char buffer[40];
3451599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
346ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
347ce263fc8SMatthias Ringwald }
348ce263fc8SMatthias Ringwald 
349ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
350ce263fc8SMatthias Ringwald     char buffer[20];
3511599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
352ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
353ce263fc8SMatthias Ringwald }
354ce263fc8SMatthias Ringwald 
35597d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
3561599fe57SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "ATA\r");
35797d2cadbSMatthias Ringwald }
35897d2cadbSMatthias Ringwald 
35989425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
360aeb0f0feSMatthias Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_hf_supported_features);
36189425bfcSMilanka Ringwald }
36289425bfcSMilanka Ringwald 
36389425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
36489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
36589425bfcSMilanka Ringwald }
36689425bfcSMilanka Ringwald 
36789425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
36889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
36989425bfcSMilanka Ringwald }
37089425bfcSMilanka Ringwald 
37189425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
37289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
37389425bfcSMilanka Ringwald }
37489425bfcSMilanka Ringwald 
37589425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
37689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
37789425bfcSMilanka Ringwald }
37889425bfcSMilanka Ringwald 
37989425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
38089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
38189425bfcSMilanka Ringwald }
38289425bfcSMilanka Ringwald 
38389425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
38489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
38589425bfcSMilanka Ringwald }
38689425bfcSMilanka Ringwald 
38789425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
38889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
38989425bfcSMilanka Ringwald }
39089425bfcSMilanka Ringwald 
39189425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
39289425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
39389425bfcSMilanka Ringwald }
39489425bfcSMilanka Ringwald 
39589425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
39689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
39789425bfcSMilanka Ringwald }
39889425bfcSMilanka Ringwald 
39989425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
40089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
40189425bfcSMilanka Ringwald }
40289425bfcSMilanka Ringwald 
40389425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
40489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
40589425bfcSMilanka Ringwald }
40689425bfcSMilanka Ringwald 
40789425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
40889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
40989425bfcSMilanka Ringwald }
41089425bfcSMilanka Ringwald 
41189425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
41289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
41389425bfcSMilanka Ringwald }
41489425bfcSMilanka Ringwald 
41589425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
41689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
41789425bfcSMilanka Ringwald }
41889425bfcSMilanka Ringwald 
41989425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
42089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
42189425bfcSMilanka Ringwald }
42289425bfcSMilanka Ringwald 
42389425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
42489425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
42589425bfcSMilanka Ringwald }
42689425bfcSMilanka Ringwald 
42789425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
42889425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
42989425bfcSMilanka Ringwald }
43089425bfcSMilanka Ringwald 
431ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
43289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
433ce263fc8SMatthias Ringwald }
434ce263fc8SMatthias Ringwald 
435667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
43689425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
437667ec068SMatthias Ringwald }
438667ec068SMatthias Ringwald 
43976cc1527SMatthias Ringwald /* state machines */
4403deb3ec6SMatthias Ringwald 
441a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
442a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
443a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
444aa4dd815SMatthias Ringwald     int done = 1;
445498a8121SMilanka Ringwald     log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state);
446a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4473deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
448aeb0f0feSMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_hf_codecs, &hfp_hf_codecs_nr);
449a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
450a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4513deb3ec6SMatthias Ringwald             break;
4523deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
453a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
454a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4553deb3ec6SMatthias Ringwald             break;
4563deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
457a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
458a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4593deb3ec6SMatthias Ringwald             break;
4603deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
461a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
462a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4633deb3ec6SMatthias Ringwald             break;
4643deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
465a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
466a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4673deb3ec6SMatthias Ringwald             break;
4683deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
469a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
470a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4713deb3ec6SMatthias Ringwald             break;
4723deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
473a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
474a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4753deb3ec6SMatthias Ringwald             break;
4763deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
477a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
478a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4793deb3ec6SMatthias Ringwald             break;
4803deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
481a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
482a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4833deb3ec6SMatthias Ringwald             break;
4843deb3ec6SMatthias Ringwald         default:
485aa4dd815SMatthias Ringwald             done = 0;
4863deb3ec6SMatthias Ringwald             break;
4873deb3ec6SMatthias Ringwald     }
4883deb3ec6SMatthias Ringwald     return done;
4893deb3ec6SMatthias Ringwald }
4903deb3ec6SMatthias Ringwald 
491ce263fc8SMatthias Ringwald 
492a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
493a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
494498a8121SMilanka Ringwald     if (hfp_connection->ok_pending){
495498a8121SMilanka Ringwald         return 0;
496498a8121SMilanka Ringwald     }
497ce263fc8SMatthias Ringwald     int done = 0;
498a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
499a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
500ce263fc8SMatthias Ringwald         done = 1;
501a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
502ce263fc8SMatthias Ringwald         return done;
503ce263fc8SMatthias Ringwald     };
504a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
505a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
506ce263fc8SMatthias Ringwald         done = 1;
507a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
508a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
509a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
510ce263fc8SMatthias Ringwald         return done;
511ce263fc8SMatthias Ringwald     }
512ce263fc8SMatthias Ringwald 
513a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
514ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
515a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
516a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
517a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
518ce263fc8SMatthias Ringwald             return 1;
519ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
520a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
521a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
522a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
523ce263fc8SMatthias Ringwald             return 1;
524ce263fc8SMatthias Ringwald         default:
525ce263fc8SMatthias Ringwald             break;
526ce263fc8SMatthias Ringwald     }
527ce263fc8SMatthias Ringwald 
528a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
529a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
530ce263fc8SMatthias Ringwald         done = 1;
531a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
532ce263fc8SMatthias Ringwald         return done;
533ce263fc8SMatthias Ringwald     }
534ce263fc8SMatthias Ringwald 
535ce263fc8SMatthias Ringwald     return done;
536ce263fc8SMatthias Ringwald }
537ce263fc8SMatthias Ringwald 
538af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
539be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
540be55a11dSMilanka Ringwald         return 0;
541be55a11dSMilanka Ringwald     }
542be55a11dSMilanka Ringwald     int done = 0;
543fd4151d1SMilanka Ringwald 
5440b4debbfSMilanka Ringwald     if (hfp_connection->ok_pending == 1){
5450b4debbfSMilanka Ringwald         return 0;
5460b4debbfSMilanka Ringwald     }
5470b4debbfSMilanka Ringwald     // voice recognition activated from AG
5480b4debbfSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){
5490b4debbfSMilanka Ringwald         switch(hfp_connection->vra_state_requested){
5500b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
5510b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
552de9e0ea7SMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
5530b4debbfSMilanka Ringwald                 // ignore AG command, continue to wait for OK
5540b4debbfSMilanka Ringwald                 return 0;
555cf75be85SMilanka Ringwald 
5560b4debbfSMilanka Ringwald             default:
557b95cac54SMilanka Ringwald                 if (hfp_connection->ag_vra_msg_length > 0){
558b95cac54SMilanka Ringwald                     hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection);
559b95cac54SMilanka Ringwald                     hfp_connection->ag_vra_msg_length = 0;
560b95cac54SMilanka Ringwald                     break;
561b95cac54SMilanka Ringwald                 }
562cf75be85SMilanka Ringwald                 switch(hfp_connection->ag_vra_state){
563cf75be85SMilanka Ringwald                     case HFP_VOICE_RECOGNITION_STATE_AG_READY:
564013cc750SMilanka Ringwald                         switch (hfp_connection->ag_vra_status){
565013cc750SMilanka Ringwald                             case 0:
5660b4debbfSMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
567013cc750SMilanka Ringwald                                 break;
568013cc750SMilanka Ringwald                             case 1:
569013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
570013cc750SMilanka Ringwald                                 break;
571013cc750SMilanka Ringwald                             case 2:
572013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
573013cc750SMilanka Ringwald                                 break;
574013cc750SMilanka Ringwald                             default:
575013cc750SMilanka Ringwald                                 break;
5760b4debbfSMilanka Ringwald                         }
5770b4debbfSMilanka Ringwald                         break;
578cf75be85SMilanka Ringwald                     default:
579cf75be85SMilanka Ringwald                         // state messages from AG
580cf75be85SMilanka Ringwald                         hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
5818d5005b5SMilanka Ringwald                         hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
582cf75be85SMilanka Ringwald                         break;
583cf75be85SMilanka Ringwald                 }
584cf75be85SMilanka Ringwald                 break;
5850b4debbfSMilanka Ringwald         }
5860b4debbfSMilanka Ringwald         hfp_connection->command = HFP_CMD_NONE;
5870b4debbfSMilanka Ringwald     }
5880b4debbfSMilanka Ringwald 
5890b4debbfSMilanka Ringwald 
590498a8121SMilanka Ringwald     switch (hfp_connection->vra_state_requested){
591fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
592fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
593fdda66c0SMilanka Ringwald             if (done != 0){
594fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
595498a8121SMilanka Ringwald                 hfp_connection->ok_pending = 1;
596498a8121SMilanka Ringwald             }
597fd4151d1SMilanka Ringwald             return 1;
598fd4151d1SMilanka Ringwald 
599fd4151d1SMilanka Ringwald 
600fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
601fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
602fdda66c0SMilanka Ringwald             if (done != 0){
603fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
604fd4151d1SMilanka Ringwald                 hfp_connection->ok_pending = 1;
605fd4151d1SMilanka Ringwald                 return 1;
6060b4debbfSMilanka Ringwald             }
6070b4debbfSMilanka Ringwald             break;
608013cc750SMilanka Ringwald 
609de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
610de9e0ea7SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
611de9e0ea7SMilanka Ringwald             if (done != 0){
612de9e0ea7SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
613de9e0ea7SMilanka Ringwald                 hfp_connection->ok_pending = 1;
614de9e0ea7SMilanka Ringwald                 return 1;
615de9e0ea7SMilanka Ringwald             }
616de9e0ea7SMilanka Ringwald             break;
617de9e0ea7SMilanka Ringwald 
618de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
619de9e0ea7SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
620de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
621de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
622de9e0ea7SMilanka Ringwald             if (hfp_connection->activate_voice_recognition){
623fcf4ede6SMilanka Ringwald                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
624de9e0ea7SMilanka Ringwald                 hfp_hf_activate_voice_recognition(hfp_connection->acl_handle);
625de9e0ea7SMilanka Ringwald             } else {
626553a4a56SMilanka Ringwald                 hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
627de9e0ea7SMilanka Ringwald             }
628de9e0ea7SMilanka Ringwald             break;
629de9e0ea7SMilanka Ringwald 
630be55a11dSMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
631498a8121SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
632498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
633de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
634de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
635de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
636de9e0ea7SMilanka Ringwald             } else {
637fcf4ede6SMilanka Ringwald                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
63884fb9ac1SMilanka Ringwald                 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
639553a4a56SMilanka Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
64084fb9ac1SMilanka Ringwald                 } else {
64184fb9ac1SMilanka Ringwald                     // postpone VRA event to simplify application logic
64284fb9ac1SMilanka Ringwald                     hfp_connection->emit_vra_enabled_after_audio_established = true;
64384fb9ac1SMilanka Ringwald                 }
644de9e0ea7SMilanka Ringwald             }
645be55a11dSMilanka Ringwald             break;
646be55a11dSMilanka Ringwald 
647de9e0ea7SMilanka Ringwald 
648013cc750SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
649013cc750SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
650498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
651de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
652de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
653de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
654de9e0ea7SMilanka Ringwald             } else {
655de9e0ea7SMilanka Ringwald                 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
656de9e0ea7SMilanka Ringwald             }
657be55a11dSMilanka Ringwald             break;
658fd4151d1SMilanka Ringwald 
659be55a11dSMilanka Ringwald         default:
660be55a11dSMilanka Ringwald             break;
661be55a11dSMilanka Ringwald     }
662be55a11dSMilanka Ringwald     return done;
663be55a11dSMilanka Ringwald }
664be55a11dSMilanka Ringwald 
665be55a11dSMilanka Ringwald 
666be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
667a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
668ce263fc8SMatthias Ringwald 
669332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
670332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
671ce263fc8SMatthias Ringwald 
672a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
673a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
674332ca98fSMatthias Ringwald 		return 1;
675332ca98fSMatthias Ringwald     }
676332ca98fSMatthias Ringwald 
6771cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
6781cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
679ce263fc8SMatthias Ringwald 
680a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
681fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
6821cc65c4fSMatthias Ringwald 		return 1;
6831cc65c4fSMatthias Ringwald     }
6841cc65c4fSMatthias Ringwald 
6851cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
6861cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
6871cc65c4fSMatthias Ringwald 
688a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
689a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
6901cc65c4fSMatthias Ringwald 		return 1;
6911cc65c4fSMatthias Ringwald     }
692ce263fc8SMatthias Ringwald 
693ce263fc8SMatthias Ringwald     return 0;
694ce263fc8SMatthias Ringwald }
695ce263fc8SMatthias Ringwald 
696a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
697505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
698505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
699ce263fc8SMatthias Ringwald 
70064f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
701a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
702a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
703a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
704ce263fc8SMatthias Ringwald         return 1;
705ce263fc8SMatthias Ringwald     }
706ce263fc8SMatthias Ringwald 
707a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
708ce263fc8SMatthias Ringwald 
709ce263fc8SMatthias Ringwald     // run codecs exchange
710a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
711ce263fc8SMatthias Ringwald     if (done) return 1;
712ce263fc8SMatthias Ringwald 
71338200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
71438200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
71538200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
71638200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
71738200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
71838200c1dSMilanka Ringwald         return 1;
71938200c1dSMilanka Ringwald     }
720ce263fc8SMatthias Ringwald     return 0;
721ce263fc8SMatthias Ringwald }
722ce263fc8SMatthias Ringwald 
72338200c1dSMilanka Ringwald 
724a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
725eaf2b0a1SMatthias Ringwald 
726eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
727eaf2b0a1SMatthias Ringwald 
728a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
729a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
730a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
731ce263fc8SMatthias Ringwald         return 1;
732ce263fc8SMatthias Ringwald     }
733ce263fc8SMatthias Ringwald     return 0;
734ce263fc8SMatthias Ringwald }
735ce263fc8SMatthias Ringwald 
7361c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
7377522e673SMatthias Ringwald 
73876cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
73976cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
74076cc1527SMatthias Ringwald 
74176cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
74276cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
74322387625SMatthias Ringwald 
74484fb9ac1SMilanka Ringwald     // emit postponed VRA event
74584fb9ac1SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){
74684fb9ac1SMilanka Ringwald         hfp_connection->emit_vra_enabled_after_audio_established = false;
74784fb9ac1SMilanka Ringwald         hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
74884fb9ac1SMilanka Ringwald     }
74984fb9ac1SMilanka Ringwald 
7503721a235SMatthias Ringwald 	// assert command could be sent
7513721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
7523721a235SMatthias Ringwald 
7533721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
7543721a235SMatthias Ringwald     // WBS Disassociate
7553721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
7563721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
7573721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
7583721a235SMatthias Ringwald         return;
7593721a235SMatthias Ringwald     }
7603721a235SMatthias Ringwald     // Write Codec Config
7613721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
7623721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
7633721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
7643721a235SMatthias Ringwald         return;
7653721a235SMatthias Ringwald     }
7663721a235SMatthias Ringwald     // WBS Associate
7673721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
7683721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
7693721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
7703721a235SMatthias Ringwald         return;
7713721a235SMatthias Ringwald     }
7723721a235SMatthias Ringwald #endif
773689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
774689d4323SMatthias Ringwald     // Enable WBS
775689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
776689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
777689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
778689d4323SMatthias Ringwald         return;
779689d4323SMatthias Ringwald     }
780689d4323SMatthias Ringwald     // Write I2S/PCM params
781689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
782689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
783689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
784689d4323SMatthias Ringwald         return;
785689d4323SMatthias Ringwald     }
786689d4323SMatthias Ringwald     // Disable WBS
787689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
788689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
789689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
790689d4323SMatthias Ringwald         return;
791689d4323SMatthias Ringwald     }
792689d4323SMatthias Ringwald #endif
7932b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS
7942b5f92fdSMatthias Ringwald     if (hfp_connection->rtk_send_sco_config){
7952b5f92fdSMatthias Ringwald         hfp_connection->rtk_send_sco_config = false;
7962b5f92fdSMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
7972b5f92fdSMatthias Ringwald             log_info("RTK SCO: 16k + mSBC");
7982b5f92fdSMatthias Ringwald             hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x00, 0x00, 0x41);
7992b5f92fdSMatthias Ringwald         } else {
8002b5f92fdSMatthias Ringwald             log_info("RTK SCO: 16k + CVSD");
8012b5f92fdSMatthias Ringwald             hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x0c, 0x00, 0x01);
8022b5f92fdSMatthias Ringwald         }
8032b5f92fdSMatthias Ringwald         return;
8042b5f92fdSMatthias Ringwald     }
8052b5f92fdSMatthias Ringwald #endif
80648e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
80748e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
80848e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
80948e6eeeeSMatthias Ringwald         return;
81048e6eeeeSMatthias Ringwald     }
81148e6eeeeSMatthias Ringwald #endif
8123721a235SMatthias Ringwald 
813cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
814cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
815cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
8167522e673SMatthias Ringwald         // notify about codec selection if not done already
8177522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
8187522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
8197522e673SMatthias Ringwald         }
820cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
8217522e673SMatthias Ringwald         return;
8227522e673SMatthias Ringwald     }
8237522e673SMatthias Ringwald 
824d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
825d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
826d4dd47ffSMatthias Ringwald         return;
827d4dd47ffSMatthias Ringwald     }
828a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
829ce263fc8SMatthias Ringwald     if (!done){
830a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
831ce263fc8SMatthias Ringwald     }
832ce263fc8SMatthias Ringwald     if (!done){
833c95b5b3cSMilanka Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
834be55a11dSMilanka Ringwald     }
835be55a11dSMilanka Ringwald     if (!done){
836c95b5b3cSMilanka Ringwald         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
837ce263fc8SMatthias Ringwald     }
838ce263fc8SMatthias Ringwald     if (!done){
839a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
840ce263fc8SMatthias Ringwald     }
841ce263fc8SMatthias Ringwald 
842e2c3b58dSMilanka Ringwald     // don't send a new command while ok still pending or SLC is not established
843e2c3b58dSMilanka Ringwald     if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){
844e2c3b58dSMilanka Ringwald         return;
845e2c3b58dSMilanka Ringwald     }
8461016a228SMatthias Ringwald 
847a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
848a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
849a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
850a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
851ce263fc8SMatthias Ringwald         return;
852ce263fc8SMatthias Ringwald     }
853ce263fc8SMatthias Ringwald 
854a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
855a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
856a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
857a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
858ce263fc8SMatthias Ringwald         return;
859ce263fc8SMatthias Ringwald     }
860ce263fc8SMatthias Ringwald 
861a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
862a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
863a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
864a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
865ce263fc8SMatthias Ringwald         return;
866ce263fc8SMatthias Ringwald     }
867ce263fc8SMatthias Ringwald 
868a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
869a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
870a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
871a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
872ce263fc8SMatthias Ringwald         return;
873ce263fc8SMatthias Ringwald     }
874ce263fc8SMatthias Ringwald 
875a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
876a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
87799af1e28SMilanka Ringwald         hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR;
878a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
879e2c3b58dSMilanka Ringwald         hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0);
880ce263fc8SMatthias Ringwald         return;
881ce263fc8SMatthias Ringwald     }
882ce263fc8SMatthias Ringwald 
883a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
884a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
885a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
886a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
887ce263fc8SMatthias Ringwald         return;
888ce263fc8SMatthias Ringwald     }
889ce263fc8SMatthias Ringwald 
890a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
891a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
892a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
893a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
894ce263fc8SMatthias Ringwald         return;
895ce263fc8SMatthias Ringwald     }
896ce263fc8SMatthias Ringwald 
897a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
898a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
899a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
900a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
901ce263fc8SMatthias Ringwald         return;
902ce263fc8SMatthias Ringwald     }
903ce263fc8SMatthias Ringwald 
904a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
905a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
906a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
907a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
908ce263fc8SMatthias Ringwald         return;
909ce263fc8SMatthias Ringwald     }
910ce263fc8SMatthias Ringwald 
911a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
912a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
913a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
914a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
915ce263fc8SMatthias Ringwald         return;
916ce263fc8SMatthias Ringwald     }
917ce263fc8SMatthias Ringwald 
918a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
919a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
920a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
921a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
922ce263fc8SMatthias Ringwald         return;
923ce263fc8SMatthias Ringwald     }
924ce263fc8SMatthias Ringwald 
925a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
926a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
927a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
928a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
929ce263fc8SMatthias Ringwald         return;
930ce263fc8SMatthias Ringwald     }
931ce263fc8SMatthias Ringwald 
932a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
933a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
934a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
935a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
936ce263fc8SMatthias Ringwald         return;
937ce263fc8SMatthias Ringwald     }
938ce263fc8SMatthias Ringwald 
939a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
940a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
941a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
942a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
943ce263fc8SMatthias Ringwald         return;
944ce263fc8SMatthias Ringwald     }
945ce263fc8SMatthias Ringwald 
946a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
947a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
948a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
949a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
950ce263fc8SMatthias Ringwald         return;
951ce263fc8SMatthias Ringwald     }
952ce263fc8SMatthias Ringwald 
953a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
954a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
955a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
956a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
957ce263fc8SMatthias Ringwald         return;
958ce263fc8SMatthias Ringwald     }
959ce263fc8SMatthias Ringwald 
960a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
961a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
962a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
963a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
964667ec068SMatthias Ringwald         return;
965667ec068SMatthias Ringwald     }
966667ec068SMatthias Ringwald 
967a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
968a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
969a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
970a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
971a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
972c090e552SMatthias Ringwald         // notify client that dtmf was sent
973c090e552SMatthias Ringwald         char buffer[2];
974c090e552SMatthias Ringwald         buffer[0] = code;
975c090e552SMatthias Ringwald         buffer[1] = 0;
976c090e552SMatthias Ringwald         hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer);
977ce263fc8SMatthias Ringwald         return;
978ce263fc8SMatthias Ringwald     }
979ce263fc8SMatthias Ringwald 
980a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
981a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
982a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
983a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
984ce263fc8SMatthias Ringwald         return;
985ce263fc8SMatthias Ringwald     }
986ce263fc8SMatthias Ringwald 
987a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
988a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
989a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
990a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
991667ec068SMatthias Ringwald         return;
992667ec068SMatthias Ringwald     }
993667ec068SMatthias Ringwald 
994a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
995a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
996667ec068SMatthias Ringwald         char buffer[20];
997a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
998667ec068SMatthias Ringwald             case '?':
9991599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
1000ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
1001ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
1002a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1003667ec068SMatthias Ringwald                 return;
1004667ec068SMatthias Ringwald             case '0':
1005667ec068SMatthias Ringwald             case '1':
1006667ec068SMatthias Ringwald             case '2':
10071599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
1008ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
1009ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
1010ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
1011a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1012667ec068SMatthias Ringwald                 return;
1013667ec068SMatthias Ringwald             default:
1014667ec068SMatthias Ringwald                 break;
1015667ec068SMatthias Ringwald         }
1016667ec068SMatthias Ringwald         return;
1017667ec068SMatthias Ringwald     }
1018667ec068SMatthias Ringwald 
1019a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
1020a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
1021667ec068SMatthias Ringwald         char buffer[20];
10221599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
1023ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
1024ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
1025a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1026667ec068SMatthias Ringwald         return;
1027667ec068SMatthias Ringwald     }
1028667ec068SMatthias Ringwald 
1029667ec068SMatthias Ringwald     // update HF indicators
1030a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
1031667ec068SMatthias Ringwald         int i;
1032aeb0f0feSMatthias Ringwald         for (i=0; i < hfp_hf_indicators_nr; i++){
1033a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
1034a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
1035a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
1036a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
1037667ec068SMatthias Ringwald                     char buffer[30];
10381599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
1039ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
1040aeb0f0feSMatthias Ringwald                              hfp_hf_indicators[i],
1041aeb0f0feSMatthias Ringwald                              (unsigned int)hfp_hf_indicators_value[i]);
1042ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
1043a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1044667ec068SMatthias Ringwald                 } else {
1045aeb0f0feSMatthias Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]);
1046667ec068SMatthias Ringwald                 }
1047667ec068SMatthias Ringwald                 return;
1048667ec068SMatthias Ringwald             }
1049667ec068SMatthias Ringwald         }
1050667ec068SMatthias Ringwald     }
1051667ec068SMatthias Ringwald 
1052*51a2ebdeSMatthias Ringwald     if (hfp_connection->send_custom_message != NULL){
1053*51a2ebdeSMatthias Ringwald         const char * message = hfp_connection->send_custom_message;
1054*51a2ebdeSMatthias Ringwald         hfp_connection->send_custom_message = NULL;
1055*51a2ebdeSMatthias Ringwald         hfp_connection->ok_pending = 1;
1056*51a2ebdeSMatthias Ringwald         hfp_connection->response_pending_for_command = HFP_CMD_CUSTOM_MESSAGE;
1057*51a2ebdeSMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, message);
1058*51a2ebdeSMatthias Ringwald         return;
1059*51a2ebdeSMatthias Ringwald     }
1060*51a2ebdeSMatthias Ringwald 
1061ce263fc8SMatthias Ringwald     if (done) return;
1062ce263fc8SMatthias Ringwald     // deal with disconnect
1063a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
1064ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
1065a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1066a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
1067ce263fc8SMatthias Ringwald             break;
1068ce263fc8SMatthias Ringwald 
1069ce263fc8SMatthias Ringwald         default:
1070ce263fc8SMatthias Ringwald             break;
1071ce263fc8SMatthias Ringwald     }
1072ce263fc8SMatthias Ringwald }
1073ce263fc8SMatthias Ringwald 
1074a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
1075a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
10766a7f44bdSMilanka Ringwald 
10777095467fSMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection->local_role, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
10787522e673SMatthias Ringwald 
1079184a03edSMilanka Ringwald     uint8_t i;
1080184a03edSMilanka Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
10811ac1f60fSMilanka Ringwald         hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1082184a03edSMilanka Ringwald     }
1083722a85f3SMilanka Ringwald 
1084722a85f3SMilanka Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1085722a85f3SMilanka Ringwald         hfp_connection->ag_indicators[i].status_changed = 0;
1086722a85f3SMilanka Ringwald         hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1087722a85f3SMilanka Ringwald     }
1088722a85f3SMilanka Ringwald 
1089667ec068SMatthias Ringwald     // restore volume settings
1090a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
1091a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1092ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
1093a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
1094a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1095ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
1096667ec068SMatthias Ringwald     // enable all indicators
1097aeb0f0feSMatthias Ringwald     for (i=0; i < hfp_hf_indicators_nr; i++){
1098aeb0f0feSMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i];
1099a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
1100667ec068SMatthias Ringwald     }
1101ce263fc8SMatthias Ringwald }
1102ce263fc8SMatthias Ringwald 
11031cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
1104aeb0f0feSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){
11051cc65c4fSMatthias Ringwald 		// Codec supported, confirm
11061cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
11071cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
11081cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
11091cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
11101cc65c4fSMatthias Ringwald 
11111cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
11121cc65c4fSMatthias Ringwald 	} else {
11131cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
11141cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
11151cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
11161cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
11171cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
11181cc65c4fSMatthias Ringwald 
11191cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
11201cc65c4fSMatthias Ringwald 	}
11211cc65c4fSMatthias Ringwald }
11221cc65c4fSMatthias Ringwald 
1123e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){
1124e2c3b58dSMilanka Ringwald     bool event_emited = true;
1125e2c3b58dSMilanka Ringwald 
1126b3e7b9f5SMatthias Ringwald     // cache state and reset
1127b3e7b9f5SMatthias Ringwald     hfp_command_t response_pending_for_command = hfp_connection->response_pending_for_command;
1128b3e7b9f5SMatthias Ringwald     hfp_connection->response_pending_for_command = HFP_CMD_NONE;
1129b3e7b9f5SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
1130b3e7b9f5SMatthias Ringwald     hfp_connection->ok_pending = 0;
1131b3e7b9f5SMatthias Ringwald 
1132b3e7b9f5SMatthias Ringwald     switch (response_pending_for_command){
1133e2c3b58dSMilanka Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1134e2c3b58dSMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status);
1135e2c3b58dSMilanka Ringwald             break;
1136*51a2ebdeSMatthias Ringwald         case HFP_CMD_CUSTOM_MESSAGE:
1137*51a2ebdeSMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, status);
1138*51a2ebdeSMatthias Ringwald             break;
1139e2c3b58dSMilanka Ringwald         default:
1140e2c3b58dSMilanka Ringwald             event_emited = false;
1141e2c3b58dSMilanka Ringwald 
1142a0ffb263SMatthias Ringwald             switch (hfp_connection->state){
11433deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
1144a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
1145a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_NOTIFY_ON_CODECS;
11463deb3ec6SMatthias Ringwald                         break;
11473deb3ec6SMatthias Ringwald                     }
1148a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
11493deb3ec6SMatthias Ringwald                     break;
11503deb3ec6SMatthias Ringwald 
11513deb3ec6SMatthias Ringwald                 case HFP_W4_NOTIFY_ON_CODECS:
1152a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
11533deb3ec6SMatthias Ringwald                     break;
11543deb3ec6SMatthias Ringwald 
11553deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS:
1156a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
11573deb3ec6SMatthias Ringwald                     break;
11583deb3ec6SMatthias Ringwald 
11593deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1160a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
11613deb3ec6SMatthias Ringwald                     break;
11623deb3ec6SMatthias Ringwald 
11633deb3ec6SMatthias Ringwald                 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1164a0ffb263SMatthias Ringwald                     if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1165a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
11663deb3ec6SMatthias Ringwald                         break;
11673deb3ec6SMatthias Ringwald                     }
1168a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1169a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
11703deb3ec6SMatthias Ringwald                         break;
11713deb3ec6SMatthias Ringwald                     }
1172a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11733deb3ec6SMatthias Ringwald                     break;
11743deb3ec6SMatthias Ringwald 
11753deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1176a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1177a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
11783deb3ec6SMatthias Ringwald                         break;
11793deb3ec6SMatthias Ringwald                     }
1180a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11813deb3ec6SMatthias Ringwald                     break;
11823deb3ec6SMatthias Ringwald 
11833deb3ec6SMatthias Ringwald                 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1184a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
11853deb3ec6SMatthias Ringwald                     break;
11863deb3ec6SMatthias Ringwald 
11873deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1188a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
11893deb3ec6SMatthias Ringwald                     break;
11903deb3ec6SMatthias Ringwald 
11913deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1192a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11933deb3ec6SMatthias Ringwald                     break;
1194ce263fc8SMatthias Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1195a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1196a0ffb263SMatthias Ringwald                         hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
11976cc5b34bSMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, ERROR_CODE_SUCCESS);
1198ce263fc8SMatthias Ringwald                         break;
1199ce263fc8SMatthias Ringwald                     }
12003deb3ec6SMatthias Ringwald 
1201a0ffb263SMatthias Ringwald                     if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1202a0ffb263SMatthias Ringwald                         hfp_connection->change_status_update_for_individual_ag_indicators = 0;
12036cc5b34bSMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, ERROR_CODE_SUCCESS);
1204ce263fc8SMatthias Ringwald                         break;
12053deb3ec6SMatthias Ringwald                     }
12063deb3ec6SMatthias Ringwald 
1207a0ffb263SMatthias Ringwald                     switch (hfp_connection->hf_query_operator_state){
1208ce263fc8SMatthias Ringwald                         case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1209a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1210ce263fc8SMatthias Ringwald                             break;
1211ce263fc8SMatthias Ringwald                         case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1212a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1213a473a009SMatthias Ringwald                             hfp_emit_network_operator_event(hfp_connection);
1214ce263fc8SMatthias Ringwald                             break;
1215ce263fc8SMatthias Ringwald                         default:
1216ce263fc8SMatthias Ringwald                             break;
12173deb3ec6SMatthias Ringwald                     }
1218ce263fc8SMatthias Ringwald 
1219a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_extended_audio_gateway_error_report){
1220a0ffb263SMatthias Ringwald                         hfp_connection->enable_extended_audio_gateway_error_report = 0;
1221ce263fc8SMatthias Ringwald                         break;
12223deb3ec6SMatthias Ringwald                     }
12233deb3ec6SMatthias Ringwald 
1224a0ffb263SMatthias Ringwald                     switch (hfp_connection->codecs_state){
1225aa4dd815SMatthias Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1226a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
12273deb3ec6SMatthias Ringwald                             break;
1228ce263fc8SMatthias Ringwald                         case HFP_CODECS_HF_CONFIRMED_CODEC:
1229a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1230ce263fc8SMatthias Ringwald                             break;
12313deb3ec6SMatthias Ringwald                         default:
12323deb3ec6SMatthias Ringwald                             break;
12333deb3ec6SMatthias Ringwald                     }
1234af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
1235be55a11dSMilanka Ringwald                     break;
1236be55a11dSMilanka Ringwald                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
1237af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
12383deb3ec6SMatthias Ringwald                     break;
12393deb3ec6SMatthias Ringwald                 default:
12403deb3ec6SMatthias Ringwald                     break;
12413deb3ec6SMatthias Ringwald             }
1242e2c3b58dSMilanka Ringwald             break;
1243e2c3b58dSMilanka Ringwald     }
12443deb3ec6SMatthias Ringwald 
1245e2c3b58dSMilanka Ringwald     return event_emited;
12463deb3ec6SMatthias Ringwald }
12473deb3ec6SMatthias Ringwald 
1248a03dbc20SMilanka Ringwald static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){
1249a03dbc20SMilanka Ringwald     switch (callsetup_status){
1250a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1251a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1252a03dbc20SMilanka Ringwald             return true;
1253a03dbc20SMilanka Ringwald         default:
1254a03dbc20SMilanka Ringwald             return false;
1255a03dbc20SMilanka Ringwald    }
1256a03dbc20SMilanka Ringwald }
1257be55a11dSMilanka Ringwald 
1258b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
12594562e2a2SMatthias Ringwald     uint16_t i;
1260a03dbc20SMilanka Ringwald 
12614562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
12624562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
12634562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1264a03dbc20SMilanka Ringwald                 hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1265a03dbc20SMilanka Ringwald                 bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status);
1266a03dbc20SMilanka Ringwald                 bool ringing_new = hfp_is_ringing(new_hf_callsetup_status);
1267a03dbc20SMilanka Ringwald                 if (ringing_old != ringing_new){
1268a03dbc20SMilanka Ringwald                     if (ringing_new){
1269a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING);
1270a03dbc20SMilanka Ringwald                     } else {
1271a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING);
1272a03dbc20SMilanka Ringwald                     }
1273a03dbc20SMilanka Ringwald                 }
1274a03dbc20SMilanka Ringwald                 hfp_hf_callsetup_status = new_hf_callsetup_status;
12754562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1276aeb0f0feSMatthias Ringwald                 hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
12774562e2a2SMatthias Ringwald                 // avoid set but not used warning
1278aeb0f0feSMatthias Ringwald                 (void) hfp_hf_callheld_status;
12794562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1280674ebed5SMilanka Ringwald                 hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1281674ebed5SMilanka Ringwald                 if (hfp_hf_call_status != new_hf_call_status){
1282674ebed5SMilanka Ringwald                     if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){
1283674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1284674ebed5SMilanka Ringwald                     } else {
1285674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1286674ebed5SMilanka Ringwald                     }
1287674ebed5SMilanka Ringwald                 }
1288674ebed5SMilanka Ringwald                 hfp_hf_call_status = new_hf_call_status;
12894562e2a2SMatthias Ringwald             }
12904562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1291ce3797e1SMilanka Ringwald             hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]);
12924562e2a2SMatthias Ringwald             break;
12934562e2a2SMatthias Ringwald         }
12944562e2a2SMatthias Ringwald     }
12954562e2a2SMatthias Ringwald }
12964562e2a2SMatthias Ringwald 
1297426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1298186dd3d2SMatthias Ringwald     int value;
1299186dd3d2SMatthias Ringwald     int i;
1300e2c3b58dSMilanka Ringwald     bool event_emited;
1301e2c3b58dSMilanka Ringwald 
1302125560b8SMatthias Ringwald     // last argument is still in line_buffer
1303125560b8SMatthias Ringwald 
1304a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1305667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1306a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1307a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1308667ec068SMatthias Ringwald             break;
1309667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1310a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1311ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1312667ec068SMatthias Ringwald             break;
1313667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1314a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1315a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1316667ec068SMatthias Ringwald             break;
1317ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1318a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13192308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1320667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1321ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1322ce263fc8SMatthias Ringwald             break;
1323ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1324a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13252308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1326667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1327ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1328ce263fc8SMatthias Ringwald             break;
1329ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1330a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1331ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1332a0ffb263SMatthias Ringwald             break;
1333a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1334a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1335598d4936SMatthias Ringwald             hfp_hf_emit_type_number_alpha(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1336a0ffb263SMatthias Ringwald             break;
1337a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1338a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1339598d4936SMatthias Ringwald             hfp_hf_emit_type_number_alpha(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1340ce263fc8SMatthias Ringwald             break;
1341ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1342a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13435a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1344a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1345ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1346ce263fc8SMatthias Ringwald             break;
13470b4debbfSMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
13480b4debbfSMilanka Ringwald             break;
1349fdda66c0SMilanka Ringwald         case HFP_CMD_ERROR:
135090244c92SMilanka Ringwald             switch (hfp_connection->state){
135190244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
135290244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
135390244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1354fdda66c0SMilanka Ringwald                             hfp_reset_context_flags(hfp_connection);
135590244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
135690244c92SMilanka Ringwald                             return;
135790244c92SMilanka Ringwald                         default:
135890244c92SMilanka Ringwald                             break;
135990244c92SMilanka Ringwald                     }
136056f1adacSMilanka Ringwald                     break;
136156f1adacSMilanka Ringwald                 default:
136256f1adacSMilanka Ringwald                     break;
136356f1adacSMilanka Ringwald             }
1364e2c3b58dSMilanka Ringwald 
1365fdda66c0SMilanka Ringwald             // handle error response for voice activation (HF initiated)
13660b4debbfSMilanka Ringwald             switch(hfp_connection->vra_state_requested){
1367de9e0ea7SMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1368de9e0ea7SMilanka Ringwald                     hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1369be55a11dSMilanka Ringwald                     break;
1370be55a11dSMilanka Ringwald                 default:
1371e2c3b58dSMilanka Ringwald                     if (hfp_connection->vra_state_requested == hfp_connection->vra_state){
1372e2c3b58dSMilanka Ringwald                         break;
1373e2c3b58dSMilanka Ringwald                     }
13740b4debbfSMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
1375553a4a56SMilanka Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
13760b4debbfSMilanka Ringwald                     hfp_reset_context_flags(hfp_connection);
13770b4debbfSMilanka Ringwald                     return;
1378be55a11dSMilanka Ringwald             }
1379e2c3b58dSMilanka Ringwald             event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1380e2c3b58dSMilanka Ringwald             if (!event_emited){
13816cc5b34bSMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, ERROR_CODE_UNSPECIFIED_ERROR);
1382e2c3b58dSMilanka Ringwald             }
1383fdda66c0SMilanka Ringwald             hfp_reset_context_flags(hfp_connection);
1384ce263fc8SMatthias Ringwald             break;
1385fdda66c0SMilanka Ringwald 
1386ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1387e2c3b58dSMilanka Ringwald             hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS);
1388ce263fc8SMatthias Ringwald             break;
1389ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
13905a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1391ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1392ce263fc8SMatthias Ringwald             break;
1393ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
13945a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13954562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1396ce263fc8SMatthias Ringwald             break;
1397c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
13985a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1399184a03edSMilanka Ringwald             // report status after SLC established
1400184a03edSMilanka Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
1401184a03edSMilanka Ringwald                 break;
1402184a03edSMilanka Ringwald             }
1403c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
14041ac1f60fSMilanka Ringwald                 hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1405c741b032SMilanka Ringwald             }
1406c741b032SMilanka Ringwald             break;
14071cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
14081cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
14095a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
14101cc65c4fSMatthias Ringwald 			break;
1411eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1412eac56539SMilanka 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));
1413ce263fc8SMatthias Ringwald         default:
1414ce263fc8SMatthias Ringwald             break;
14153deb3ec6SMatthias Ringwald     }
14160cef86faSMatthias Ringwald }
1417426f9988SMatthias Ringwald 
141876cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
141976cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
142076cc1527SMatthias Ringwald }
142176cc1527SMatthias Ringwald 
14220b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1423426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1424426f9988SMatthias Ringwald     if (size < 1) return;
1425426f9988SMatthias Ringwald 
1426426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1427e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1428e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1429e43d1938SMatthias Ringwald #endif
1430426f9988SMatthias Ringwald 
1431426f9988SMatthias Ringwald     // process messages byte-wise
1432a7ba78b0SMilanka Ringwald     uint8_t pos;
1433426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1434426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
14351599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1436426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
14370b4debbfSMilanka Ringwald         hfp_hf_handle_rfcomm_command(hfp_connection);
14383deb3ec6SMatthias Ringwald     }
1439a7ba78b0SMilanka Ringwald }
14403deb3ec6SMatthias Ringwald 
14411c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1442665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1443665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1444665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1445a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
144622387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
14471c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
14483deb3ec6SMatthias Ringwald     }
14493deb3ec6SMatthias Ringwald }
14503deb3ec6SMatthias Ringwald 
14511c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
14520b4debbfSMilanka Ringwald     hfp_connection_t * hfp_connection;
14533deb3ec6SMatthias Ringwald     switch (packet_type){
14543deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
14550b4debbfSMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
14560b4debbfSMilanka Ringwald             if (!hfp_connection) return;
14570b4debbfSMilanka Ringwald             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
14580b4debbfSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
14590b4debbfSMilanka Ringwald             return;
14603deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1461d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1462d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
14631c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1464d4dd47ffSMatthias Ringwald                 return;
1465d4dd47ffSMatthias Ringwald             }
146627950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1467202c8a4cSMatthias Ringwald             break;
14683deb3ec6SMatthias Ringwald         default:
14693deb3ec6SMatthias Ringwald             break;
14703deb3ec6SMatthias Ringwald     }
14711c6a0fc0SMatthias Ringwald     hfp_hf_run();
14723deb3ec6SMatthias Ringwald }
14733deb3ec6SMatthias Ringwald 
14741c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1475405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
14761c6a0fc0SMatthias Ringwald     hfp_hf_run();
1477405014fbSMatthias Ringwald }
1478405014fbSMatthias Ringwald 
1479aeb0f0feSMatthias Ringwald static void hfp_hf_set_defaults(void){
1480aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1481aeb0f0feSMatthias Ringwald     hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
1482aeb0f0feSMatthias Ringwald     hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1483aeb0f0feSMatthias Ringwald     hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1484aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = 0;
1485aeb0f0feSMatthias Ringwald     hfp_hf_speaker_gain = 9;
1486aeb0f0feSMatthias Ringwald     hfp_hf_microphone_gain = 9;
1487aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = 0;
1488aeb0f0feSMatthias Ringwald }
1489aeb0f0feSMatthias Ringwald 
1490ab2445a0SMatthias Ringwald uint8_t hfp_hf_init(uint8_t rfcomm_channel_nr){
1491b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1492b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1493b4df8028SMilanka Ringwald         return status;
1494b4df8028SMilanka Ringwald     }
1495b4df8028SMilanka Ringwald 
1496520c92d5SMatthias Ringwald     hfp_init();
1497aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1498d63c37a1SMatthias Ringwald 
14991c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
15001c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
150127950165SMatthias Ringwald 
150227950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
15031c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1504b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
150520b2edb6SMatthias Ringwald }
150627950165SMatthias Ringwald 
150720b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
150820b2edb6SMatthias Ringwald     hfp_deinit();
1509aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1510aeb0f0feSMatthias Ringwald 
1511aeb0f0feSMatthias Ringwald     hfp_hf_callback = NULL;
151220b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
1513aeb0f0feSMatthias Ringwald     (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number));
1514a0ffb263SMatthias Ringwald }
1515a0ffb263SMatthias Ringwald 
15167ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
151768466199SMilanka Ringwald     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
15183deb3ec6SMatthias Ringwald 
1519aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = codecs_nr;
15203deb3ec6SMatthias Ringwald     int i;
15213deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
1522aeb0f0feSMatthias Ringwald         hfp_hf_codecs[i] = codecs[i];
15233deb3ec6SMatthias Ringwald     }
15243deb3ec6SMatthias Ringwald }
15253deb3ec6SMatthias Ringwald 
1526a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
1527aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = supported_features;
1528a0ffb263SMatthias Ringwald }
15293deb3ec6SMatthias Ringwald 
15307ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
1531aeb0f0feSMatthias Ringwald     btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS);
153268466199SMilanka Ringwald 
1533aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = indicators_nr;
15343deb3ec6SMatthias Ringwald     int i;
1535aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
1536ab2445a0SMatthias Ringwald         hfp_hf_indicators[i] = (uint8_t) indicators[i];
15373deb3ec6SMatthias Ringwald     }
15383deb3ec6SMatthias Ringwald }
15393deb3ec6SMatthias Ringwald 
15404eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
15414eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
15423deb3ec6SMatthias Ringwald }
15433deb3ec6SMatthias Ringwald 
1544657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
15459c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1546a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1547657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1548a33eb0c4SMilanka Ringwald     }
15491ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
15501c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1551657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
15523deb3ec6SMatthias Ringwald }
15533deb3ec6SMatthias Ringwald 
15543c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
15559c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1556a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15573c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15583deb3ec6SMatthias Ringwald     }
1559a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
15601c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15613c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15623deb3ec6SMatthias Ringwald }
15633deb3ec6SMatthias Ringwald 
15643c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
15653c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1566ce263fc8SMatthias Ringwald }
1567ce263fc8SMatthias Ringwald 
15683c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
15693c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1570ce263fc8SMatthias Ringwald }
1571ce263fc8SMatthias Ringwald 
15723deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
15733c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
15749c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1575a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15763c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15773deb3ec6SMatthias Ringwald     }
1578a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1579a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
15801c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15813c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15823deb3ec6SMatthias Ringwald }
15833deb3ec6SMatthias Ringwald 
15843c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
15859c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1586a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15873c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15883deb3ec6SMatthias Ringwald     }
15893c65e705SMilanka Ringwald 
1590a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1591ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1592a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1593ce263fc8SMatthias Ringwald             break;
1594ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1595a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1596ce263fc8SMatthias Ringwald             break;
1597ce263fc8SMatthias Ringwald         default:
15983c65e705SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1599ce263fc8SMatthias Ringwald     }
16001c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16013c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16023deb3ec6SMatthias Ringwald }
16033deb3ec6SMatthias Ringwald 
16043c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
16059c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1606a0ffb263SMatthias Ringwald     if (!hfp_connection) {
16073c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
16083deb3ec6SMatthias Ringwald     }
1609a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
16101c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16113c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16123deb3ec6SMatthias Ringwald }
16133deb3ec6SMatthias Ringwald 
1614ce263fc8SMatthias Ringwald 
16153c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
16163c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1617ce263fc8SMatthias Ringwald }
1618ce263fc8SMatthias Ringwald 
16193c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
16203c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1621ce263fc8SMatthias Ringwald }
1622ce263fc8SMatthias Ringwald 
162338200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
1624aeb0f0feSMatthias Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4));
162538200c1dSMilanka Ringwald }
1626ce263fc8SMatthias Ringwald 
16273c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
16289c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1629a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16303c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1631a33eb0c4SMilanka Ringwald     }
1632ce263fc8SMatthias Ringwald 
16333c65e705SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
16343c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16353c65e705SMilanka Ringwald     }
16363c65e705SMilanka Ringwald 
16373c65e705SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
16383c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16393c65e705SMilanka Ringwald     }
1640f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1641a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1642aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1643aa4dd815SMatthias Ringwald                 break;
1644ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1645ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1646ec3bfc1aSMatthias Ringwald                 break;
1647aa4dd815SMatthias Ringwald             default:
16481cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
16491cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
16501cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
16511cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
165238200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1653aa4dd815SMatthias Ringwald                 break;
16543deb3ec6SMatthias Ringwald         }
1655f4412093SMatthias Ringwald     } else {
1656f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1657f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1658f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1659f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1660f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1661f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1662f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1663f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1664ce263fc8SMatthias Ringwald     }
1665ce263fc8SMatthias Ringwald 
16661c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16673c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16683deb3ec6SMatthias Ringwald }
16693deb3ec6SMatthias Ringwald 
16703c65e705SMilanka Ringwald uint8_t hfp_hf_release_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     }
16750b4debbfSMilanka Ringwald     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
16760b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16770b4debbfSMilanka Ringwald     }
16780b4debbfSMilanka Ringwald     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
16790b4debbfSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
16801c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
16810b4debbfSMilanka Ringwald     }
16823c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16833deb3ec6SMatthias Ringwald }
16843deb3ec6SMatthias Ringwald 
16853c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
16869c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1687a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16883c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1689a33eb0c4SMilanka Ringwald     }
1690ce263fc8SMatthias Ringwald 
1691aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1692a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
16931c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1694ce263fc8SMatthias Ringwald     } else {
1695aeb0f0feSMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status);
16963c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1697ce263fc8SMatthias Ringwald     }
16983c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1699ce263fc8SMatthias Ringwald }
1700ce263fc8SMatthias Ringwald 
17013c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
17029c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1703a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17043c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1705a33eb0c4SMilanka Ringwald     }
1706a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
17071c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17083c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1709ce263fc8SMatthias Ringwald }
1710ce263fc8SMatthias Ringwald 
17113c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
17129c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1713a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17143c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1715a33eb0c4SMilanka Ringwald     }
1716ce263fc8SMatthias Ringwald 
1717aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1718a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
17191c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1720ce263fc8SMatthias Ringwald     }
17213c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1722ce263fc8SMatthias Ringwald }
1723ce263fc8SMatthias Ringwald 
17243c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){
17259c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1726a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17273c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1728a33eb0c4SMilanka Ringwald     }
1729ce263fc8SMatthias Ringwald 
1730aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1731a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
17321c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1733ce263fc8SMatthias Ringwald     }
17343c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1735ce263fc8SMatthias Ringwald }
1736ce263fc8SMatthias Ringwald 
17373c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
17389c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1739a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17403c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1741a33eb0c4SMilanka Ringwald     }
1742ce263fc8SMatthias Ringwald 
1743aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1744aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1745a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
17461c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1747ce263fc8SMatthias Ringwald     }
17483c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1749ce263fc8SMatthias Ringwald }
1750ce263fc8SMatthias Ringwald 
17513c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
17529c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1753a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17543c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1755a33eb0c4SMilanka Ringwald     }
1756ce263fc8SMatthias Ringwald 
1757aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1758aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1759a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
17601c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1761ce263fc8SMatthias Ringwald     }
17623c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1763ce263fc8SMatthias Ringwald }
1764ce263fc8SMatthias Ringwald 
17653c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
17669c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1767a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17683c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1769a33eb0c4SMilanka Ringwald     }
1770ce263fc8SMatthias Ringwald 
1771aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1772aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1773a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
17741c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1775ce263fc8SMatthias Ringwald     }
17763c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1777ce263fc8SMatthias Ringwald }
1778ce263fc8SMatthias Ringwald 
17793c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
17809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1781a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17823c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1783a33eb0c4SMilanka Ringwald     }
1784ce263fc8SMatthias Ringwald 
1785aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1786aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1787a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
17881c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1789ce263fc8SMatthias Ringwald     }
17903c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1791ce263fc8SMatthias Ringwald }
1792ce263fc8SMatthias Ringwald 
17933c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
17949c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1795a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17963c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1797a33eb0c4SMilanka Ringwald     }
1798667ec068SMatthias Ringwald 
1799aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1800aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1801a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1802a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
18031c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1804667ec068SMatthias Ringwald     }
18053c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1806667ec068SMatthias Ringwald }
1807667ec068SMatthias Ringwald 
18083c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
18099c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1810a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18113c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1812a33eb0c4SMilanka Ringwald     }
1813667ec068SMatthias Ringwald 
1814aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1815aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1816a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1817a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
18181c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1819667ec068SMatthias Ringwald     }
18203c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1821667ec068SMatthias Ringwald }
1822ce263fc8SMatthias Ringwald 
18233c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
18249c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1825a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18263c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1827a33eb0c4SMilanka Ringwald     }
1828ce263fc8SMatthias Ringwald 
1829a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1830aeb0f0feSMatthias Ringwald     snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number);
18311c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18323c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1833ce263fc8SMatthias Ringwald }
1834ce263fc8SMatthias Ringwald 
18353c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
18369c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1837a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18383c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1839a33eb0c4SMilanka Ringwald     }
1840ce263fc8SMatthias Ringwald 
1841a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1842a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1843a0ffb263SMatthias Ringwald 
18441c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18453c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1846ce263fc8SMatthias Ringwald }
1847ce263fc8SMatthias Ringwald 
18483c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
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     }
1853ce263fc8SMatthias Ringwald 
1854a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
18551c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18563c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1857ce263fc8SMatthias Ringwald }
1858ce263fc8SMatthias Ringwald 
18593c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
18609c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1861a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18623c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1863a33eb0c4SMilanka Ringwald     }
1864ce263fc8SMatthias Ringwald 
1865a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
18661c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18673c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1868ce263fc8SMatthias Ringwald }
1869ce263fc8SMatthias Ringwald 
1870ce263fc8SMatthias Ringwald 
18713c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
18729c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1873a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18743c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1875a33eb0c4SMilanka Ringwald     }
1876ce263fc8SMatthias Ringwald 
1877a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
18781c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18793c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1880ce263fc8SMatthias Ringwald }
1881ce263fc8SMatthias Ringwald 
1882ce263fc8SMatthias Ringwald 
18833c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
18849c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1885a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18863c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1887a33eb0c4SMilanka Ringwald     }
1888ce263fc8SMatthias Ringwald 
1889a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
18901c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18913c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1892ce263fc8SMatthias Ringwald }
1893ce263fc8SMatthias Ringwald 
18943c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
18959c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1896a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18973c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1898a33eb0c4SMilanka Ringwald     }
1899ce263fc8SMatthias Ringwald 
1900a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
19011c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19023c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1903ce263fc8SMatthias Ringwald }
1904ce263fc8SMatthias Ringwald 
19056ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){
19066ba83b5eSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION);
1907aeb0f0feSMatthias Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION);
19086ba83b5eSMilanka Ringwald     return hf && ag;
1909ce263fc8SMatthias Ringwald }
1910ce263fc8SMatthias Ringwald 
19113c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
19129c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1913a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19143c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1915a33eb0c4SMilanka Ringwald     }
19166ba83b5eSMilanka Ringwald     if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){
19176ba83b5eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
19186ba83b5eSMilanka Ringwald     }
1919ce263fc8SMatthias Ringwald 
1920a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
19211c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19223c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1923ce263fc8SMatthias Ringwald }
1924ce263fc8SMatthias Ringwald 
1925acd11d4aSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1926fdda66c0SMilanka Ringwald      hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1927fdda66c0SMilanka Ringwald     if (!hfp_connection) {
1928fdda66c0SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1929be55a11dSMilanka Ringwald     }
1930013cc750SMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1931013cc750SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1932013cc750SMilanka Ringwald     }
1933acd11d4aSMilanka Ringwald 
1934acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1935acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1936acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
1937acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1938af97579eSMilanka Ringwald     }
1939af97579eSMilanka Ringwald 
1940498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1941be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1942de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1943fd4151d1SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
1944acd11d4aSMilanka Ringwald             hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported;
1945be55a11dSMilanka Ringwald             break;
1946de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1947de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = true;
1948de9e0ea7SMilanka Ringwald             break;
1949be55a11dSMilanka Ringwald         default:
1950be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1951be55a11dSMilanka Ringwald     }
1952ce263fc8SMatthias Ringwald 
1953af97579eSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1954fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1955af97579eSMilanka Ringwald }
1956af97579eSMilanka Ringwald 
1957acd11d4aSMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
1958af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1959af97579eSMilanka Ringwald     if (!hfp_connection) {
1960af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1961af97579eSMilanka Ringwald     }
196284fb9ac1SMilanka Ringwald 
196384fb9ac1SMilanka Ringwald     if (hfp_connection->emit_vra_enabled_after_audio_established){
196484fb9ac1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
196584fb9ac1SMilanka Ringwald     }
196684fb9ac1SMilanka Ringwald 
1967acd11d4aSMilanka Ringwald     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
196808a0b01cSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
196908a0b01cSMilanka Ringwald     }
1970acd11d4aSMilanka Ringwald 
1971acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1972acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported){
1973acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1974acd11d4aSMilanka Ringwald     }
1975acd11d4aSMilanka Ringwald 
1976acd11d4aSMilanka Ringwald     switch (hfp_connection->vra_state){
1977acd11d4aSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1978acd11d4aSMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1979acd11d4aSMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
1980acd11d4aSMilanka Ringwald             break;
1981acd11d4aSMilanka Ringwald         default:
1982fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1983af97579eSMilanka Ringwald     }
1984013cc750SMilanka Ringwald 
1985acd11d4aSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1986acd11d4aSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1987acd11d4aSMilanka Ringwald }
1988acd11d4aSMilanka Ringwald 
1989acd11d4aSMilanka Ringwald 
1990acd11d4aSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1991acd11d4aSMilanka Ringwald     // return deactivate_voice_recognition(acl_handle, false);
1992acd11d4aSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1993acd11d4aSMilanka Ringwald     if (!hfp_connection) {
1994acd11d4aSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1995acd11d4aSMilanka Ringwald     }
1996acd11d4aSMilanka Ringwald 
199784fb9ac1SMilanka Ringwald     if (hfp_connection->emit_vra_enabled_after_audio_established){
199884fb9ac1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
199984fb9ac1SMilanka Ringwald     }
200084fb9ac1SMilanka Ringwald 
2001acd11d4aSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
2002acd11d4aSMilanka Ringwald         hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
2003acd11d4aSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2004acd11d4aSMilanka Ringwald     }
2005acd11d4aSMilanka Ringwald 
2006acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
2007acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
2008acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
2009acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
2010acd11d4aSMilanka Ringwald     }
2011acd11d4aSMilanka Ringwald 
2012fdda66c0SMilanka Ringwald     switch (hfp_connection->vra_state){
2013de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
2014fdda66c0SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
2015de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
2016de9e0ea7SMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
2017fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
2018fdda66c0SMilanka Ringwald             break;
2019de9e0ea7SMilanka Ringwald 
2020de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
2021de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
2022de9e0ea7SMilanka Ringwald             hfp_connection->deactivate_voice_recognition = true;
2023de9e0ea7SMilanka Ringwald             break;
2024de9e0ea7SMilanka Ringwald 
2025de9e0ea7SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
2026de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
2027de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
2028fdda66c0SMilanka Ringwald         default:
2029fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
2030fdda66c0SMilanka Ringwald     }
2031fdda66c0SMilanka Ringwald 
2032fdda66c0SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
2033fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2034af97579eSMilanka Ringwald }
2035af97579eSMilanka Ringwald 
20363c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
20379c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2038a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20393c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2040a33eb0c4SMilanka Ringwald     }
2041c8626498SMilanka Ringwald 
20423c65e705SMilanka Ringwald     if (hfp_connection->microphone_gain == gain) {
20433c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
20443c65e705SMilanka Ringwald     }
20453c65e705SMilanka Ringwald 
2046c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
2047a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
20483c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2049a0ffb263SMatthias Ringwald     }
20503c65e705SMilanka Ringwald 
2051a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
2052a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
20531c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20543c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2055ce263fc8SMatthias Ringwald }
2056ce263fc8SMatthias Ringwald 
20573c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
20589c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2059a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20603c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2061a33eb0c4SMilanka Ringwald     }
2062c8626498SMilanka Ringwald 
20633c65e705SMilanka Ringwald     if (hfp_connection->speaker_gain == gain){
20643c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
20653c65e705SMilanka Ringwald     }
20663c65e705SMilanka Ringwald 
2067c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
2068a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
20693c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2070a0ffb263SMatthias Ringwald     }
20713c65e705SMilanka Ringwald 
2072a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
2073a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
20741c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20753c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2076ce263fc8SMatthias Ringwald }
2077ce263fc8SMatthias Ringwald 
20783c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
20799c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2080a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20813c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2082a33eb0c4SMilanka Ringwald     }
2083a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
20841c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20853c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2086ce263fc8SMatthias Ringwald }
2087ce263fc8SMatthias Ringwald 
20883c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
20899c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2090a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20913c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2092a33eb0c4SMilanka Ringwald     }
2093a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
20941c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20953c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2096ce263fc8SMatthias Ringwald }
20973deb3ec6SMatthias Ringwald 
20983c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
20999c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2100a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21013c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2102a33eb0c4SMilanka Ringwald     }
2103a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
21041c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21053c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2106667ec068SMatthias Ringwald }
2107667ec068SMatthias Ringwald 
2108667ec068SMatthias Ringwald 
21093c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
21109c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2111a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21123c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2113a33eb0c4SMilanka Ringwald     }
2114a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2115a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
21161c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21173c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2118667ec068SMatthias Ringwald }
2119667ec068SMatthias Ringwald 
21203c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
21219c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2122a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21233c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2124a33eb0c4SMilanka Ringwald     }
2125a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2126a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
21271c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21283c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2129667ec068SMatthias Ringwald }
2130667ec068SMatthias Ringwald 
21313c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
21329c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2133a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21343c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2135a33eb0c4SMilanka Ringwald     }
2136a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2137a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
21381c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21393c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2140667ec068SMatthias Ringwald }
2141667ec068SMatthias Ringwald 
21423c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
21439c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2144a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21453c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2146a33eb0c4SMilanka Ringwald     }
2147a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2148a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
21491c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21503c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2151667ec068SMatthias Ringwald }
2152667ec068SMatthias Ringwald 
21533c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
21549c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2155a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21563c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2157a33eb0c4SMilanka Ringwald     }
2158a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
21591c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21603c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2161667ec068SMatthias Ringwald }
2162667ec068SMatthias Ringwald 
21633c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
21649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2165a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21663c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2167a33eb0c4SMilanka Ringwald     }
2168667ec068SMatthias Ringwald     // find index for assigned number
2169667ec068SMatthias Ringwald     int i;
2170aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
2171aeb0f0feSMatthias Ringwald         if (hfp_hf_indicators[i] == assigned_number){
2172667ec068SMatthias Ringwald             // set value
2173aeb0f0feSMatthias Ringwald             hfp_hf_indicators_value[i] = value;
2174667ec068SMatthias Ringwald             // mark for update
2175a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
2176a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
2177667ec068SMatthias Ringwald                 // send update
21781c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
2179a0ffb263SMatthias Ringwald             }
21803c65e705SMilanka Ringwald             return ERROR_CODE_SUCCESS;
2181667ec068SMatthias Ringwald         }
2182667ec068SMatthias Ringwald     }
21833c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2184667ec068SMatthias Ringwald }
2185667ec068SMatthias Ringwald 
2186*51a2ebdeSMatthias Ringwald uint8_t hfp_hf_send_at_command(hci_con_handle_t acl_handle, const char * at_command){
2187*51a2ebdeSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2188*51a2ebdeSMatthias Ringwald     if (!hfp_connection) {
2189*51a2ebdeSMatthias Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2190*51a2ebdeSMatthias Ringwald     }
2191*51a2ebdeSMatthias Ringwald     if (hfp_connection->send_custom_message != NULL){
2192*51a2ebdeSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2193*51a2ebdeSMatthias Ringwald     }
2194*51a2ebdeSMatthias Ringwald     hfp_connection->send_custom_message = at_command;
2195*51a2ebdeSMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
2196*51a2ebdeSMatthias Ringwald     return ERROR_CODE_SUCCESS;
2197*51a2ebdeSMatthias Ringwald }
2198*51a2ebdeSMatthias Ringwald 
2199d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
2200d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2201d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
2202d7f6b5cbSMatthias Ringwald         return 0;
2203d7f6b5cbSMatthias Ringwald     }
2204d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
2205d7f6b5cbSMatthias Ringwald }
220676cc1527SMatthias Ringwald 
220776cc1527SMatthias 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){
220876cc1527SMatthias Ringwald 	if (!name){
2209aeb0f0feSMatthias Ringwald 		name = hfp_hf_default_service_name;
221076cc1527SMatthias Ringwald 	}
221176cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
221276cc1527SMatthias Ringwald 
221376cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
221476cc1527SMatthias Ringwald 	//
221576cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
221676cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
221776cc1527SMatthias Ringwald 	//
221876cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
221976cc1527SMatthias Ringwald 	//
222076cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
2221ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
222276cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
222376cc1527SMatthias Ringwald 	}
2224ef3ae4ebSMilanka Ringwald 
2225ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
222656f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
2227ef3ae4ebSMilanka Ringwald     }
2228ef3ae4ebSMilanka Ringwald 
2229ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
223056f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
2231ef3ae4ebSMilanka Ringwald     }
2232ef3ae4ebSMilanka Ringwald 
223376cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
223476cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
223576cc1527SMatthias Ringwald }
223676cc1527SMatthias Ringwald 
223776cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
223868466199SMilanka Ringwald 	btstack_assert(callback != NULL);
223968466199SMilanka Ringwald 
224076cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
224176cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
224276cc1527SMatthias Ringwald }
2243