xref: /btstack/src/classic/hfp_hf.c (revision 1ac1f60f228b5c99eed29c069da71d03f1a876b1)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp_hf.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
42fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493cfa4086SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <string.h>
513deb3ec6SMatthias Ringwald 
52235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
5359c6af15SMatthias Ringwald #include "btstack_debug.h"
54d4dd47ffSMatthias Ringwald #include "btstack_event.h"
553deb3ec6SMatthias Ringwald #include "btstack_memory.h"
5659c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5759c6af15SMatthias Ringwald #include "classic/core.h"
5859c6af15SMatthias Ringwald #include "classic/hfp.h"
5959c6af15SMatthias Ringwald #include "classic/hfp_hf.h"
60efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
61746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
62023f2764SMatthias Ringwald #include "classic/sdp_util.h"
6359c6af15SMatthias Ringwald #include "hci.h"
6459c6af15SMatthias Ringwald #include "hci_cmd.h"
6559c6af15SMatthias Ringwald #include "hci_dump.h"
6659c6af15SMatthias Ringwald #include "l2cap.h"
673deb3ec6SMatthias Ringwald 
6820b2edb6SMatthias Ringwald // const
69aeb0f0feSMatthias Ringwald static const char hfp_hf_default_service_name[] = "Hands-Free unit";
7020b2edb6SMatthias Ringwald 
7120b2edb6SMatthias Ringwald // globals
72aeb0f0feSMatthias Ringwald 
73aeb0f0feSMatthias Ringwald // higher layer callbacks
74aeb0f0feSMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
75aeb0f0feSMatthias Ringwald 
761c6a0fc0SMatthias Ringwald static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration;
7727950165SMatthias Ringwald 
78aeb0f0feSMatthias Ringwald static uint16_t hfp_hf_supported_features;
79aeb0f0feSMatthias Ringwald static uint8_t  hfp_hf_codecs_nr;
80aeb0f0feSMatthias Ringwald static uint8_t  hfp_hf_codecs[HFP_MAX_NUM_CODECS];
813deb3ec6SMatthias Ringwald 
82aeb0f0feSMatthias Ringwald static uint8_t  hfp_hf_indicators_nr;
83aeb0f0feSMatthias Ringwald static uint8_t  hfp_hf_indicators[HFP_MAX_NUM_INDICATORS];
84aeb0f0feSMatthias Ringwald static uint32_t hfp_hf_indicators_value[HFP_MAX_NUM_INDICATORS];
85667ec068SMatthias Ringwald 
8620b2edb6SMatthias Ringwald static uint8_t  hfp_hf_speaker_gain;
8720b2edb6SMatthias Ringwald static uint8_t  hfp_hf_microphone_gain;
883deb3ec6SMatthias Ringwald 
89aeb0f0feSMatthias Ringwald static hfp_call_status_t      hfp_hf_call_status;
90aeb0f0feSMatthias Ringwald static hfp_callsetup_status_t hfp_hf_callsetup_status;
91aeb0f0feSMatthias Ringwald static hfp_callheld_status_t  hfp_hf_callheld_status;
923deb3ec6SMatthias Ringwald 
93aeb0f0feSMatthias Ringwald static char hfp_hf_phone_number[25];
94ce263fc8SMatthias Ringwald 
95ce263fc8SMatthias Ringwald 
9676cc1527SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
97aeb0f0feSMatthias Ringwald 	int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
9876cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
9976cc1527SMatthias Ringwald 	return hf && ag;
10076cc1527SMatthias Ringwald }
10176cc1527SMatthias Ringwald 
10276cc1527SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
103aeb0f0feSMatthias Ringwald 	int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_THREE_WAY_CALLING);
10476cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
10576cc1527SMatthias Ringwald 	return hf && ag;
10676cc1527SMatthias Ringwald }
10776cc1527SMatthias Ringwald 
10876cc1527SMatthias Ringwald 
10976cc1527SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
110aeb0f0feSMatthias Ringwald 	int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_HF_INDICATORS);
11176cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
11276cc1527SMatthias Ringwald 	return hf && ag;
11376cc1527SMatthias Ringwald }
11476cc1527SMatthias Ringwald 
115fcf4ede6SMilanka Ringwald static bool hfp_hf_vra_flag_supported(hfp_connection_t * hfp_connection){
116fcf4ede6SMilanka Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION);
117fcf4ede6SMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION);
118fcf4ede6SMilanka Ringwald     return hf && ag;
119fcf4ede6SMilanka Ringwald }
120fcf4ede6SMilanka Ringwald 
121fcf4ede6SMilanka Ringwald static bool hfp_hf_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){
122fcf4ede6SMilanka Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS);
123fcf4ede6SMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS);
124fcf4ede6SMilanka Ringwald     return hf && ag;
125fcf4ede6SMilanka Ringwald }
12676cc1527SMatthias Ringwald 
1279c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
1289c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1299c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1309c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1319c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1329c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1339c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1349c9c64c1SMatthias Ringwald         return hfp_connection;
1359c9c64c1SMatthias Ringwald     }
1369c9c64c1SMatthias Ringwald     return NULL;
1379c9c64c1SMatthias Ringwald }
1389c9c64c1SMatthias Ringwald 
13976cc1527SMatthias Ringwald /* emit functinos */
1403deb3ec6SMatthias Ringwald 
141a473a009SMatthias Ringwald static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){
142a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
143d703d377SMatthias Ringwald     uint8_t event[33];
144a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
145a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
146a473a009SMatthias Ringwald     event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION;
147d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
148d703d377SMatthias Ringwald     event[5] = status;
149d703d377SMatthias Ringwald     event[6] = hfp_connection->bnip_type;
150d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 8);
151d703d377SMatthias Ringwald     strncpy((char*)&event[7], hfp_connection->bnip_number, size);
152d703d377SMatthias Ringwald     event[7 + size] = 0;
153a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
154a0ffb263SMatthias Ringwald }
155a0ffb263SMatthias Ringwald 
156a473a009SMatthias Ringwald static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){
157a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
158d703d377SMatthias Ringwald     uint8_t event[32];
159a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
160a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
161a0ffb263SMatthias Ringwald     event[2] = event_subtype;
162d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
163d703d377SMatthias Ringwald     event[5] = hfp_connection->bnip_type;
164d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 7);
165d703d377SMatthias Ringwald     strncpy((char*)&event[6], hfp_connection->bnip_number, size);
166d703d377SMatthias Ringwald     event[6 + size] = 0;
167a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
168a0ffb263SMatthias Ringwald }
169a0ffb263SMatthias Ringwald 
170a473a009SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){
171a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
172d703d377SMatthias Ringwald     uint8_t event[38];
1730aee97efSMilanka Ringwald     int pos = 0;
1740aee97efSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
1750aee97efSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
1760aee97efSMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
177d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
178d703d377SMatthias Ringwald     pos += 2;
179a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_idx;
180a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_dir;
181a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_status;
182a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mode;
183a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mpty;
184a473a009SMatthias Ringwald     event[pos++] = hfp_connection->bnip_type;
185d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - pos - 1);
186a473a009SMatthias Ringwald     strncpy((char*)&event[pos], hfp_connection->bnip_number, size);
1870aee97efSMilanka Ringwald     pos += size;
1880aee97efSMilanka Ringwald     event[pos++] = 0;
189a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
190a0ffb263SMatthias Ringwald }
191a0ffb263SMatthias Ringwald 
192*1ac1f60fSMilanka Ringwald static void hfp_emit_ag_indicator_mapping_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
193*1ac1f60fSMilanka Ringwald     if (hfp_hf_callback == NULL) return;
194*1ac1f60fSMilanka Ringwald     uint8_t event[8+HFP_MAX_INDICATOR_DESC_SIZE+1];
195*1ac1f60fSMilanka Ringwald     int pos = 0;
196*1ac1f60fSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
197*1ac1f60fSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
198*1ac1f60fSMilanka Ringwald     event[pos++] = HFP_SUBEVENT_AG_INDICATOR_MAPPING;
199*1ac1f60fSMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
200*1ac1f60fSMilanka Ringwald     pos += 2;
201*1ac1f60fSMilanka Ringwald     event[pos++] = indicator->index;
202*1ac1f60fSMilanka Ringwald     event[pos++] = indicator->min_range;
203*1ac1f60fSMilanka Ringwald     event[pos++] = indicator->max_range;
204*1ac1f60fSMilanka Ringwald     strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
205*1ac1f60fSMilanka Ringwald     pos += HFP_MAX_INDICATOR_DESC_SIZE;
206*1ac1f60fSMilanka Ringwald     event[pos] = 0;
207*1ac1f60fSMilanka Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
208*1ac1f60fSMilanka Ringwald }
20976cc1527SMatthias Ringwald 
210ce3797e1SMilanka Ringwald static void hfp_emit_ag_indicator_status_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
211a473a009SMatthias Ringwald 	if (hfp_hf_callback == NULL) return;
212d703d377SMatthias Ringwald 	uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE+1];
21376cc1527SMatthias Ringwald 	int pos = 0;
21476cc1527SMatthias Ringwald 	event[pos++] = HCI_EVENT_HFP_META;
21576cc1527SMatthias Ringwald 	event[pos++] = sizeof(event) - 2;
21676cc1527SMatthias Ringwald 	event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
217d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
218d703d377SMatthias Ringwald     pos += 2;
219a473a009SMatthias Ringwald 	event[pos++] = indicator->index;
220a473a009SMatthias Ringwald 	event[pos++] = indicator->status;
221a473a009SMatthias Ringwald 	event[pos++] = indicator->min_range;
222a473a009SMatthias Ringwald 	event[pos++] = indicator->max_range;
223a473a009SMatthias Ringwald 	event[pos++] = indicator->mandatory;
224a473a009SMatthias Ringwald 	event[pos++] = indicator->enabled;
225a473a009SMatthias Ringwald 	event[pos++] = indicator->status_changed;
226a473a009SMatthias Ringwald 	strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
22776cc1527SMatthias Ringwald 	pos += HFP_MAX_INDICATOR_DESC_SIZE;
22876cc1527SMatthias Ringwald 	event[pos] = 0;
229a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2303deb3ec6SMatthias Ringwald }
2313deb3ec6SMatthias Ringwald 
232a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){
233a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
234d703d377SMatthias Ringwald 	uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
23576cc1527SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
23676cc1527SMatthias Ringwald 	event[1] = sizeof(event) - 2;
23776cc1527SMatthias Ringwald 	event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
238d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
239d703d377SMatthias Ringwald 	event[5] = hfp_connection->network_operator.mode;
240d703d377SMatthias Ringwald 	event[6] = hfp_connection->network_operator.format;
241d703d377SMatthias Ringwald 	strncpy((char*)&event[7], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
242d703d377SMatthias Ringwald 	event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
243a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2443deb3ec6SMatthias Ringwald }
2453deb3ec6SMatthias Ringwald 
246b95cac54SMilanka Ringwald 
247b95cac54SMilanka Ringwald static void hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection){
248b95cac54SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
24951aa5d5aSMilanka Ringwald     uint8_t event[HFP_MAX_VR_TEXT_SIZE + 11];
250b95cac54SMilanka Ringwald     int pos = 0;
251b95cac54SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
252b95cac54SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
253b95cac54SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE;
254b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
255b95cac54SMilanka Ringwald     pos += 2;
256b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->ag_msg.text_id);
257b95cac54SMilanka Ringwald     pos += 2;
258b95cac54SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_type;
259e83f1be7SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_operation;
260b95cac54SMilanka Ringwald 
26151aa5d5aSMilanka Ringwald     // length, zero ending is already in message
262b95cac54SMilanka Ringwald     uint8_t * value = &hfp_connection->line_buffer[0];
26351aa5d5aSMilanka Ringwald     uint16_t  value_length = hfp_connection->ag_vra_msg_length;
26451aa5d5aSMilanka Ringwald 
26551aa5d5aSMilanka Ringwald     little_endian_store_16(event, pos, value_length);
266b95cac54SMilanka Ringwald     pos += 2;
26751aa5d5aSMilanka Ringwald     memcpy(&event[pos], value, value_length);
26851aa5d5aSMilanka Ringwald     pos += value_length;
26951aa5d5aSMilanka Ringwald 
270b95cac54SMilanka Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
271b95cac54SMilanka Ringwald }
272b95cac54SMilanka Ringwald 
27376cc1527SMatthias Ringwald /* send commands */
27489425bfcSMilanka Ringwald 
27589425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2763deb3ec6SMatthias Ringwald     char buffer[20];
2771599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
27889425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
27989425bfcSMilanka Ringwald }
28089425bfcSMilanka Ringwald 
28189425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
28289425bfcSMilanka Ringwald     char buffer[20];
2831599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
28489425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
28589425bfcSMilanka Ringwald }
28689425bfcSMilanka Ringwald 
28786da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
28889425bfcSMilanka Ringwald     char buffer[40];
2891599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
2903deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2913deb3ec6SMatthias Ringwald }
2923deb3ec6SMatthias Ringwald 
2933deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2943deb3ec6SMatthias Ringwald     char buffer[30];
29589425bfcSMilanka Ringwald     const int size = sizeof(buffer);
29689425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
297aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_codecs, hfp_hf_codecs_nr);
2981599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2993deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3003deb3ec6SMatthias Ringwald }
3013deb3ec6SMatthias Ringwald 
3023deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
3033deb3ec6SMatthias Ringwald     char buffer[50];
30489425bfcSMilanka Ringwald     const int size = sizeof(buffer);
30589425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
30689425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
3071599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
3083deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3093deb3ec6SMatthias Ringwald }
3103deb3ec6SMatthias Ringwald 
3113deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
3123deb3ec6SMatthias Ringwald     char buffer[30];
31389425bfcSMilanka Ringwald     const int size = sizeof(buffer);
31489425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
315aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_indicators, hfp_hf_indicators_nr);
3161599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
3173deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3183deb3ec6SMatthias Ringwald }
3193deb3ec6SMatthias Ringwald 
32089425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
3213deb3ec6SMatthias Ringwald     char buffer[20];
3221599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
323ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
324ce263fc8SMatthias Ringwald }
325ce263fc8SMatthias Ringwald 
326ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
327ce263fc8SMatthias Ringwald     char buffer[40];
328aeb0f0feSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, hfp_hf_phone_number);
329ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
330ce263fc8SMatthias Ringwald }
331ce263fc8SMatthias Ringwald 
332a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
333ce263fc8SMatthias Ringwald     char buffer[40];
3341599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
335ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
336ce263fc8SMatthias Ringwald }
337ce263fc8SMatthias Ringwald 
338f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
33989425bfcSMilanka Ringwald     char buffer[40];
3401599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
341ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
342ce263fc8SMatthias Ringwald }
343ce263fc8SMatthias Ringwald 
344ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
345ce263fc8SMatthias Ringwald     char buffer[20];
3461599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
347ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
348ce263fc8SMatthias Ringwald }
349ce263fc8SMatthias Ringwald 
35097d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
3511599fe57SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "ATA\r");
35297d2cadbSMatthias Ringwald }
35397d2cadbSMatthias Ringwald 
35489425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
355aeb0f0feSMatthias Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_hf_supported_features);
35689425bfcSMilanka Ringwald }
35789425bfcSMilanka Ringwald 
35889425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
35989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
36089425bfcSMilanka Ringwald }
36189425bfcSMilanka Ringwald 
36289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
36389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
36489425bfcSMilanka Ringwald }
36589425bfcSMilanka Ringwald 
36689425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
36789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
36889425bfcSMilanka Ringwald }
36989425bfcSMilanka Ringwald 
37089425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
37189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
37289425bfcSMilanka Ringwald }
37389425bfcSMilanka Ringwald 
37489425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
37589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
37689425bfcSMilanka Ringwald }
37789425bfcSMilanka Ringwald 
37889425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
37989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
38089425bfcSMilanka Ringwald }
38189425bfcSMilanka Ringwald 
38289425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
38389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
38489425bfcSMilanka Ringwald }
38589425bfcSMilanka Ringwald 
38689425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
38789425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
38889425bfcSMilanka Ringwald }
38989425bfcSMilanka Ringwald 
39089425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
39189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
39289425bfcSMilanka Ringwald }
39389425bfcSMilanka Ringwald 
39489425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
39589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
39689425bfcSMilanka Ringwald }
39789425bfcSMilanka Ringwald 
39889425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
39989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
40089425bfcSMilanka Ringwald }
40189425bfcSMilanka Ringwald 
40289425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
40389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
40489425bfcSMilanka Ringwald }
40589425bfcSMilanka Ringwald 
40689425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
40789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
40889425bfcSMilanka Ringwald }
40989425bfcSMilanka Ringwald 
41089425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
41189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
41289425bfcSMilanka Ringwald }
41389425bfcSMilanka Ringwald 
41489425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
41589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
41689425bfcSMilanka Ringwald }
41789425bfcSMilanka Ringwald 
41889425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
41989425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
42089425bfcSMilanka Ringwald }
42189425bfcSMilanka Ringwald 
42289425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
42389425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
42489425bfcSMilanka Ringwald }
42589425bfcSMilanka Ringwald 
426ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
42789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
428ce263fc8SMatthias Ringwald }
429ce263fc8SMatthias Ringwald 
430667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
43189425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
432667ec068SMatthias Ringwald }
433667ec068SMatthias Ringwald 
43476cc1527SMatthias Ringwald /* state machines */
4353deb3ec6SMatthias Ringwald 
436a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
437a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
438a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
439aa4dd815SMatthias Ringwald     int done = 1;
440498a8121SMilanka Ringwald     log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state);
441a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4423deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
443aeb0f0feSMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_hf_codecs, &hfp_hf_codecs_nr);
444a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
445a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4463deb3ec6SMatthias Ringwald             break;
4473deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
448a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
449a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4503deb3ec6SMatthias Ringwald             break;
4513deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
452a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
453a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4543deb3ec6SMatthias Ringwald             break;
4553deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
456a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
457a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4583deb3ec6SMatthias Ringwald             break;
4593deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
460a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
461a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4623deb3ec6SMatthias Ringwald             break;
4633deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
464a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
465a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4663deb3ec6SMatthias Ringwald             break;
4673deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
468a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
469a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4703deb3ec6SMatthias Ringwald             break;
4713deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
472a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
473a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4743deb3ec6SMatthias Ringwald             break;
4753deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
476a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
477a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4783deb3ec6SMatthias Ringwald             break;
4793deb3ec6SMatthias Ringwald         default:
480aa4dd815SMatthias Ringwald             done = 0;
4813deb3ec6SMatthias Ringwald             break;
4823deb3ec6SMatthias Ringwald     }
4833deb3ec6SMatthias Ringwald     return done;
4843deb3ec6SMatthias Ringwald }
4853deb3ec6SMatthias Ringwald 
486ce263fc8SMatthias Ringwald 
487a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
488a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
489498a8121SMilanka Ringwald     if (hfp_connection->ok_pending){
490498a8121SMilanka Ringwald         return 0;
491498a8121SMilanka Ringwald     }
492ce263fc8SMatthias Ringwald     int done = 0;
493a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
494a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
495ce263fc8SMatthias Ringwald         done = 1;
496a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
497ce263fc8SMatthias Ringwald         return done;
498ce263fc8SMatthias Ringwald     };
499a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
500a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
501ce263fc8SMatthias Ringwald         done = 1;
502a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
503a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
504a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
505ce263fc8SMatthias Ringwald         return done;
506ce263fc8SMatthias Ringwald     }
507ce263fc8SMatthias Ringwald 
508a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
509ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
510a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
511a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
512a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
513ce263fc8SMatthias Ringwald             return 1;
514ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
515a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
516a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
517a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
518ce263fc8SMatthias Ringwald             return 1;
519ce263fc8SMatthias Ringwald         default:
520ce263fc8SMatthias Ringwald             break;
521ce263fc8SMatthias Ringwald     }
522ce263fc8SMatthias Ringwald 
523a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
524a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
525ce263fc8SMatthias Ringwald         done = 1;
526a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
527ce263fc8SMatthias Ringwald         return done;
528ce263fc8SMatthias Ringwald     }
529ce263fc8SMatthias Ringwald 
530ce263fc8SMatthias Ringwald     return done;
531ce263fc8SMatthias Ringwald }
532ce263fc8SMatthias Ringwald 
533af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
534be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
535be55a11dSMilanka Ringwald         return 0;
536be55a11dSMilanka Ringwald     }
537be55a11dSMilanka Ringwald     int done = 0;
538fd4151d1SMilanka Ringwald 
5390b4debbfSMilanka Ringwald     if (hfp_connection->ok_pending == 1){
5400b4debbfSMilanka Ringwald         return 0;
5410b4debbfSMilanka Ringwald     }
5420b4debbfSMilanka Ringwald     // voice recognition activated from AG
5430b4debbfSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){
5440b4debbfSMilanka Ringwald         switch(hfp_connection->vra_state_requested){
5450b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
5460b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
547de9e0ea7SMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
5480b4debbfSMilanka Ringwald                 // ignore AG command, continue to wait for OK
5490b4debbfSMilanka Ringwald                 return 0;
550cf75be85SMilanka Ringwald 
5510b4debbfSMilanka Ringwald             default:
552b95cac54SMilanka Ringwald                 if (hfp_connection->ag_vra_msg_length > 0){
553b95cac54SMilanka Ringwald                     hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection);
554b95cac54SMilanka Ringwald                     hfp_connection->ag_vra_msg_length = 0;
555b95cac54SMilanka Ringwald                     break;
556b95cac54SMilanka Ringwald                 }
557cf75be85SMilanka Ringwald                 switch(hfp_connection->ag_vra_state){
558cf75be85SMilanka Ringwald                     case HFP_VOICE_RECOGNITION_STATE_AG_READY:
559013cc750SMilanka Ringwald                         switch (hfp_connection->ag_vra_status){
560013cc750SMilanka Ringwald                             case 0:
5610b4debbfSMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
562013cc750SMilanka Ringwald                                 break;
563013cc750SMilanka Ringwald                             case 1:
564013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
565013cc750SMilanka Ringwald                                 break;
566013cc750SMilanka Ringwald                             case 2:
567013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
568013cc750SMilanka Ringwald                                 break;
569013cc750SMilanka Ringwald                             default:
570013cc750SMilanka Ringwald                                 break;
5710b4debbfSMilanka Ringwald                         }
5720b4debbfSMilanka Ringwald                         break;
573cf75be85SMilanka Ringwald                     default:
574cf75be85SMilanka Ringwald                         // state messages from AG
575cf75be85SMilanka Ringwald                         hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
5768d5005b5SMilanka Ringwald                         hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
577cf75be85SMilanka Ringwald                         break;
578cf75be85SMilanka Ringwald                 }
579cf75be85SMilanka Ringwald                 break;
5800b4debbfSMilanka Ringwald         }
5810b4debbfSMilanka Ringwald         hfp_connection->command = HFP_CMD_NONE;
5820b4debbfSMilanka Ringwald     }
5830b4debbfSMilanka Ringwald 
5840b4debbfSMilanka Ringwald 
585498a8121SMilanka Ringwald     switch (hfp_connection->vra_state_requested){
586fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
587fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
588fdda66c0SMilanka Ringwald             if (done != 0){
589fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
590498a8121SMilanka Ringwald                 hfp_connection->ok_pending = 1;
591498a8121SMilanka Ringwald             }
592fd4151d1SMilanka Ringwald             return 1;
593fd4151d1SMilanka Ringwald 
594fd4151d1SMilanka Ringwald 
595fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
596fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
597fdda66c0SMilanka Ringwald             if (done != 0){
598fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
599fd4151d1SMilanka Ringwald                 hfp_connection->ok_pending = 1;
600fd4151d1SMilanka Ringwald                 return 1;
6010b4debbfSMilanka Ringwald             }
6020b4debbfSMilanka Ringwald             break;
603013cc750SMilanka Ringwald 
604de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
605de9e0ea7SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
606de9e0ea7SMilanka Ringwald             if (done != 0){
607de9e0ea7SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
608de9e0ea7SMilanka Ringwald                 hfp_connection->ok_pending = 1;
609de9e0ea7SMilanka Ringwald                 return 1;
610de9e0ea7SMilanka Ringwald             }
611de9e0ea7SMilanka Ringwald             break;
612de9e0ea7SMilanka Ringwald 
613de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
614de9e0ea7SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
615de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
616de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
617de9e0ea7SMilanka Ringwald             if (hfp_connection->activate_voice_recognition){
618fcf4ede6SMilanka Ringwald                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
619de9e0ea7SMilanka Ringwald                 hfp_hf_activate_voice_recognition(hfp_connection->acl_handle);
620de9e0ea7SMilanka Ringwald             } else {
621553a4a56SMilanka Ringwald                 hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
622de9e0ea7SMilanka Ringwald             }
623de9e0ea7SMilanka Ringwald             break;
624de9e0ea7SMilanka Ringwald 
625be55a11dSMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
626498a8121SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
627498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
628de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
629de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
630de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
631de9e0ea7SMilanka Ringwald             } else {
632fcf4ede6SMilanka Ringwald                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
633553a4a56SMilanka Ringwald                 hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
634de9e0ea7SMilanka Ringwald             }
635be55a11dSMilanka Ringwald             break;
636be55a11dSMilanka Ringwald 
637de9e0ea7SMilanka Ringwald 
638013cc750SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
639013cc750SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
640498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
641de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
642de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
643de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
644de9e0ea7SMilanka Ringwald             } else {
645de9e0ea7SMilanka Ringwald                 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
646de9e0ea7SMilanka Ringwald             }
647be55a11dSMilanka Ringwald             break;
648fd4151d1SMilanka Ringwald 
649be55a11dSMilanka Ringwald         default:
650be55a11dSMilanka Ringwald             break;
651be55a11dSMilanka Ringwald     }
652be55a11dSMilanka Ringwald     return done;
653be55a11dSMilanka Ringwald }
654be55a11dSMilanka Ringwald 
655be55a11dSMilanka Ringwald 
656be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
657a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
658ce263fc8SMatthias Ringwald 
659332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
660332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
661ce263fc8SMatthias Ringwald 
662a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
663a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
664332ca98fSMatthias Ringwald 		return 1;
665332ca98fSMatthias Ringwald     }
666332ca98fSMatthias Ringwald 
6671cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
6681cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
669ce263fc8SMatthias Ringwald 
670a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
671fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
6721cc65c4fSMatthias Ringwald 		return 1;
6731cc65c4fSMatthias Ringwald     }
6741cc65c4fSMatthias Ringwald 
6751cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
6761cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
6771cc65c4fSMatthias Ringwald 
678a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
679a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
6801cc65c4fSMatthias Ringwald 		return 1;
6811cc65c4fSMatthias Ringwald     }
682ce263fc8SMatthias Ringwald 
683ce263fc8SMatthias Ringwald     return 0;
684ce263fc8SMatthias Ringwald }
685ce263fc8SMatthias Ringwald 
686a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
687505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
688505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
689ce263fc8SMatthias Ringwald 
69064f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
691a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
692a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
693a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
694ce263fc8SMatthias Ringwald         return 1;
695ce263fc8SMatthias Ringwald     }
696ce263fc8SMatthias Ringwald 
697a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
698ce263fc8SMatthias Ringwald 
699ce263fc8SMatthias Ringwald     // run codecs exchange
700a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
701ce263fc8SMatthias Ringwald     if (done) return 1;
702ce263fc8SMatthias Ringwald 
70338200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
70438200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
70538200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
70638200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
70738200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
70838200c1dSMilanka Ringwald         return 1;
70938200c1dSMilanka Ringwald     }
710ce263fc8SMatthias Ringwald     return 0;
711ce263fc8SMatthias Ringwald }
712ce263fc8SMatthias Ringwald 
71338200c1dSMilanka Ringwald 
714a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
715eaf2b0a1SMatthias Ringwald 
716eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
717eaf2b0a1SMatthias Ringwald 
718a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
719a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
720a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
721ce263fc8SMatthias Ringwald         return 1;
722ce263fc8SMatthias Ringwald     }
723ce263fc8SMatthias Ringwald     return 0;
724ce263fc8SMatthias Ringwald }
725ce263fc8SMatthias Ringwald 
7261c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
7277522e673SMatthias Ringwald 
72876cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
72976cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
73076cc1527SMatthias Ringwald 
73176cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
73276cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
73322387625SMatthias Ringwald 
7343721a235SMatthias Ringwald 	// assert command could be sent
7353721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
7363721a235SMatthias Ringwald 
7373721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
7383721a235SMatthias Ringwald     // WBS Disassociate
7393721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
7403721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
7413721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
7423721a235SMatthias Ringwald         return;
7433721a235SMatthias Ringwald     }
7443721a235SMatthias Ringwald     // Write Codec Config
7453721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
7463721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
7473721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
7483721a235SMatthias Ringwald         return;
7493721a235SMatthias Ringwald     }
7503721a235SMatthias Ringwald     // WBS Associate
7513721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
7523721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
7533721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
7543721a235SMatthias Ringwald         return;
7553721a235SMatthias Ringwald     }
7563721a235SMatthias Ringwald #endif
757689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
758689d4323SMatthias Ringwald     // Enable WBS
759689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
760689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
761689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
762689d4323SMatthias Ringwald         return;
763689d4323SMatthias Ringwald     }
764689d4323SMatthias Ringwald     // Write I2S/PCM params
765689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
766689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
767689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
768689d4323SMatthias Ringwald         return;
769689d4323SMatthias Ringwald     }
770689d4323SMatthias Ringwald     // Disable WBS
771689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
772689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
773689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
774689d4323SMatthias Ringwald         return;
775689d4323SMatthias Ringwald     }
776689d4323SMatthias Ringwald #endif
77748e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
77848e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
77948e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
78048e6eeeeSMatthias Ringwald         return;
78148e6eeeeSMatthias Ringwald     }
78248e6eeeeSMatthias Ringwald #endif
7833721a235SMatthias Ringwald 
784cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
785cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
786cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
7877522e673SMatthias Ringwald         // notify about codec selection if not done already
7887522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
7897522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
7907522e673SMatthias Ringwald         }
791cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
7927522e673SMatthias Ringwald         return;
7937522e673SMatthias Ringwald     }
7947522e673SMatthias Ringwald 
795d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
796d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
797d4dd47ffSMatthias Ringwald         return;
798d4dd47ffSMatthias Ringwald     }
799a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
800ce263fc8SMatthias Ringwald     if (!done){
801a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
802ce263fc8SMatthias Ringwald     }
803ce263fc8SMatthias Ringwald     if (!done){
804c95b5b3cSMilanka Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
805be55a11dSMilanka Ringwald     }
806be55a11dSMilanka Ringwald     if (!done){
807c95b5b3cSMilanka Ringwald         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
808ce263fc8SMatthias Ringwald     }
809ce263fc8SMatthias Ringwald     if (!done){
810a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
811ce263fc8SMatthias Ringwald     }
812ce263fc8SMatthias Ringwald 
813e2c3b58dSMilanka Ringwald     // don't send a new command while ok still pending or SLC is not established
814e2c3b58dSMilanka Ringwald     if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){
815e2c3b58dSMilanka Ringwald         return;
816e2c3b58dSMilanka Ringwald     }
8171016a228SMatthias Ringwald 
818a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
819a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
820a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
821a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
822ce263fc8SMatthias Ringwald         return;
823ce263fc8SMatthias Ringwald     }
824ce263fc8SMatthias Ringwald 
825a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
826a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
827a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
828a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
829ce263fc8SMatthias Ringwald         return;
830ce263fc8SMatthias Ringwald     }
831ce263fc8SMatthias Ringwald 
832a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
833a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
834a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
835a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
836ce263fc8SMatthias Ringwald         return;
837ce263fc8SMatthias Ringwald     }
838ce263fc8SMatthias Ringwald 
839a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
840a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
841a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
842a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
843ce263fc8SMatthias Ringwald         return;
844ce263fc8SMatthias Ringwald     }
845ce263fc8SMatthias Ringwald 
846a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
847a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
84899af1e28SMilanka Ringwald         hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR;
849a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
850e2c3b58dSMilanka Ringwald         hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0);
851ce263fc8SMatthias Ringwald         return;
852ce263fc8SMatthias Ringwald     }
853ce263fc8SMatthias Ringwald 
854a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
855a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
856a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
857a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
858ce263fc8SMatthias Ringwald         return;
859ce263fc8SMatthias Ringwald     }
860ce263fc8SMatthias Ringwald 
861a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
862a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
863a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
864a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
865ce263fc8SMatthias Ringwald         return;
866ce263fc8SMatthias Ringwald     }
867ce263fc8SMatthias Ringwald 
868a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
869a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
870a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
871a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
872ce263fc8SMatthias Ringwald         return;
873ce263fc8SMatthias Ringwald     }
874ce263fc8SMatthias Ringwald 
875a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
876a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
877a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
878a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
879ce263fc8SMatthias Ringwald         return;
880ce263fc8SMatthias Ringwald     }
881ce263fc8SMatthias Ringwald 
882a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
883a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
884a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
885a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
886ce263fc8SMatthias Ringwald         return;
887ce263fc8SMatthias Ringwald     }
888ce263fc8SMatthias Ringwald 
889a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
890a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
891a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
892a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
893ce263fc8SMatthias Ringwald         return;
894ce263fc8SMatthias Ringwald     }
895ce263fc8SMatthias Ringwald 
896a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
897a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
898a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
899a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
900ce263fc8SMatthias Ringwald         return;
901ce263fc8SMatthias Ringwald     }
902ce263fc8SMatthias Ringwald 
903a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
904a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
905a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
906a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
907ce263fc8SMatthias Ringwald         return;
908ce263fc8SMatthias Ringwald     }
909ce263fc8SMatthias Ringwald 
910a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
911a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
912a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
913a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
914ce263fc8SMatthias Ringwald         return;
915ce263fc8SMatthias Ringwald     }
916ce263fc8SMatthias Ringwald 
917a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
918a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
919a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
920a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
921ce263fc8SMatthias Ringwald         return;
922ce263fc8SMatthias Ringwald     }
923ce263fc8SMatthias Ringwald 
924a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
925a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
926a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
927a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
928ce263fc8SMatthias Ringwald         return;
929ce263fc8SMatthias Ringwald     }
930ce263fc8SMatthias Ringwald 
931a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
932a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
933a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
934a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
935667ec068SMatthias Ringwald         return;
936667ec068SMatthias Ringwald     }
937667ec068SMatthias Ringwald 
938a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
939a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
940a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
941a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
942a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
943ce263fc8SMatthias Ringwald         return;
944ce263fc8SMatthias Ringwald     }
945ce263fc8SMatthias Ringwald 
946a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
947a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
948a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
949a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
950ce263fc8SMatthias Ringwald         return;
951ce263fc8SMatthias Ringwald     }
952ce263fc8SMatthias Ringwald 
953a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
954a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
955a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
956a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
957667ec068SMatthias Ringwald         return;
958667ec068SMatthias Ringwald     }
959667ec068SMatthias Ringwald 
960a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
961a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
962667ec068SMatthias Ringwald         char buffer[20];
963a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
964667ec068SMatthias Ringwald             case '?':
9651599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
966ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
967ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
968a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
969667ec068SMatthias Ringwald                 return;
970667ec068SMatthias Ringwald             case '0':
971667ec068SMatthias Ringwald             case '1':
972667ec068SMatthias Ringwald             case '2':
9731599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
974ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
975ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
976ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
977a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
978667ec068SMatthias Ringwald                 return;
979667ec068SMatthias Ringwald             default:
980667ec068SMatthias Ringwald                 break;
981667ec068SMatthias Ringwald         }
982667ec068SMatthias Ringwald         return;
983667ec068SMatthias Ringwald     }
984667ec068SMatthias Ringwald 
985a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
986a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
987667ec068SMatthias Ringwald         char buffer[20];
9881599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
989ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
990ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
991a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
992667ec068SMatthias Ringwald         return;
993667ec068SMatthias Ringwald     }
994667ec068SMatthias Ringwald 
995667ec068SMatthias Ringwald     // update HF indicators
996a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
997667ec068SMatthias Ringwald         int i;
998aeb0f0feSMatthias Ringwald         for (i=0; i < hfp_hf_indicators_nr; i++){
999a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
1000a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
1001a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
1002a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
1003667ec068SMatthias Ringwald                     char buffer[30];
10041599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
1005ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
1006aeb0f0feSMatthias Ringwald                              hfp_hf_indicators[i],
1007aeb0f0feSMatthias Ringwald                              (unsigned int)hfp_hf_indicators_value[i]);
1008ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
1009a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1010667ec068SMatthias Ringwald                 } else {
1011aeb0f0feSMatthias Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]);
1012667ec068SMatthias Ringwald                 }
1013667ec068SMatthias Ringwald                 return;
1014667ec068SMatthias Ringwald             }
1015667ec068SMatthias Ringwald         }
1016667ec068SMatthias Ringwald     }
1017667ec068SMatthias Ringwald 
1018ce263fc8SMatthias Ringwald     if (done) return;
1019ce263fc8SMatthias Ringwald     // deal with disconnect
1020a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
1021ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
1022a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1023a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
1024ce263fc8SMatthias Ringwald             break;
1025ce263fc8SMatthias Ringwald 
1026ce263fc8SMatthias Ringwald         default:
1027ce263fc8SMatthias Ringwald             break;
1028ce263fc8SMatthias Ringwald     }
1029ce263fc8SMatthias Ringwald }
1030ce263fc8SMatthias Ringwald 
1031a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
1032a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
10336a7f44bdSMilanka Ringwald 
1034ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
10357522e673SMatthias Ringwald 
1036184a03edSMilanka Ringwald     uint8_t i;
1037184a03edSMilanka Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1038*1ac1f60fSMilanka Ringwald         hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1039184a03edSMilanka Ringwald     }
1040667ec068SMatthias Ringwald     // restore volume settings
1041a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
1042a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1043ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
1044a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
1045a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1046ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
1047667ec068SMatthias Ringwald     // enable all indicators
1048aeb0f0feSMatthias Ringwald     for (i=0; i < hfp_hf_indicators_nr; i++){
1049aeb0f0feSMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i];
1050a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
1051667ec068SMatthias Ringwald     }
1052ce263fc8SMatthias Ringwald }
1053ce263fc8SMatthias Ringwald 
10541cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
1055aeb0f0feSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){
10561cc65c4fSMatthias Ringwald 		// Codec supported, confirm
10571cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
10581cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
10591cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
10601cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
10611cc65c4fSMatthias Ringwald 
10621cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
10631cc65c4fSMatthias Ringwald 	} else {
10641cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
10651cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
10661cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
10671cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
10681cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10691cc65c4fSMatthias Ringwald 
10701cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
10711cc65c4fSMatthias Ringwald 	}
10721cc65c4fSMatthias Ringwald }
10731cc65c4fSMatthias Ringwald 
1074e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){
1075e2c3b58dSMilanka Ringwald     bool event_emited = true;
1076e2c3b58dSMilanka Ringwald 
107799af1e28SMilanka Ringwald     switch (hfp_connection->response_pending_for_command){
1078e2c3b58dSMilanka Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1079e2c3b58dSMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status);
1080e2c3b58dSMilanka Ringwald             break;
1081e2c3b58dSMilanka Ringwald         default:
1082e2c3b58dSMilanka Ringwald             event_emited = false;
1083e2c3b58dSMilanka Ringwald 
1084a0ffb263SMatthias Ringwald             switch (hfp_connection->state){
10853deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
1086a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
1087a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_NOTIFY_ON_CODECS;
10883deb3ec6SMatthias Ringwald                         break;
10893deb3ec6SMatthias Ringwald                     }
1090a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
10913deb3ec6SMatthias Ringwald                     break;
10923deb3ec6SMatthias Ringwald 
10933deb3ec6SMatthias Ringwald                 case HFP_W4_NOTIFY_ON_CODECS:
1094a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
10953deb3ec6SMatthias Ringwald                     break;
10963deb3ec6SMatthias Ringwald 
10973deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS:
1098a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
10993deb3ec6SMatthias Ringwald                     break;
11003deb3ec6SMatthias Ringwald 
11013deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1102a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
11033deb3ec6SMatthias Ringwald                     break;
11043deb3ec6SMatthias Ringwald 
11053deb3ec6SMatthias Ringwald                 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1106a0ffb263SMatthias Ringwald                     if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1107a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
11083deb3ec6SMatthias Ringwald                         break;
11093deb3ec6SMatthias Ringwald                     }
1110a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1111a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
11123deb3ec6SMatthias Ringwald                         break;
11133deb3ec6SMatthias Ringwald                     }
1114a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11153deb3ec6SMatthias Ringwald                     break;
11163deb3ec6SMatthias Ringwald 
11173deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1118a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1119a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
11203deb3ec6SMatthias Ringwald                         break;
11213deb3ec6SMatthias Ringwald                     }
1122a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11233deb3ec6SMatthias Ringwald                     break;
11243deb3ec6SMatthias Ringwald 
11253deb3ec6SMatthias Ringwald                 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1126a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
11273deb3ec6SMatthias Ringwald                     break;
11283deb3ec6SMatthias Ringwald 
11293deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1130a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
11313deb3ec6SMatthias Ringwald                     break;
11323deb3ec6SMatthias Ringwald 
11333deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1134a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11353deb3ec6SMatthias Ringwald                     break;
1136ce263fc8SMatthias Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1137a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1138a0ffb263SMatthias Ringwald                         hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1139ca59be51SMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1140ce263fc8SMatthias Ringwald                         break;
1141ce263fc8SMatthias Ringwald                     }
11423deb3ec6SMatthias Ringwald 
1143a0ffb263SMatthias Ringwald                     if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1144a0ffb263SMatthias Ringwald                         hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1145ca59be51SMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1146ce263fc8SMatthias Ringwald                         break;
11473deb3ec6SMatthias Ringwald                     }
11483deb3ec6SMatthias Ringwald 
1149a0ffb263SMatthias Ringwald                     switch (hfp_connection->hf_query_operator_state){
1150ce263fc8SMatthias Ringwald                         case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1151a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1152ce263fc8SMatthias Ringwald                             break;
1153ce263fc8SMatthias Ringwald                         case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1154a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1155a473a009SMatthias Ringwald                             hfp_emit_network_operator_event(hfp_connection);
1156ce263fc8SMatthias Ringwald                             break;
1157ce263fc8SMatthias Ringwald                         default:
1158ce263fc8SMatthias Ringwald                             break;
11593deb3ec6SMatthias Ringwald                     }
1160ce263fc8SMatthias Ringwald 
1161a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_extended_audio_gateway_error_report){
1162a0ffb263SMatthias Ringwald                         hfp_connection->enable_extended_audio_gateway_error_report = 0;
1163ce263fc8SMatthias Ringwald                         break;
11643deb3ec6SMatthias Ringwald                     }
11653deb3ec6SMatthias Ringwald 
1166a0ffb263SMatthias Ringwald                     switch (hfp_connection->codecs_state){
1167aa4dd815SMatthias Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1168a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
11693deb3ec6SMatthias Ringwald                             break;
1170ce263fc8SMatthias Ringwald                         case HFP_CODECS_HF_CONFIRMED_CODEC:
1171a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1172ce263fc8SMatthias Ringwald                             break;
11733deb3ec6SMatthias Ringwald                         default:
11743deb3ec6SMatthias Ringwald                             break;
11753deb3ec6SMatthias Ringwald                     }
1176af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
1177be55a11dSMilanka Ringwald                     break;
1178be55a11dSMilanka Ringwald                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
1179af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
11803deb3ec6SMatthias Ringwald                     break;
11813deb3ec6SMatthias Ringwald                 default:
11823deb3ec6SMatthias Ringwald                     break;
11833deb3ec6SMatthias Ringwald             }
1184e2c3b58dSMilanka Ringwald             break;
1185e2c3b58dSMilanka Ringwald     }
11863deb3ec6SMatthias Ringwald 
11873deb3ec6SMatthias Ringwald     // done
118899af1e28SMilanka Ringwald     hfp_connection->response_pending_for_command = HFP_CMD_NONE;
1189be55a11dSMilanka Ringwald     hfp_connection->ok_pending = 0;
1190a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
1191e2c3b58dSMilanka Ringwald     return event_emited;
11923deb3ec6SMatthias Ringwald }
11933deb3ec6SMatthias Ringwald 
1194a03dbc20SMilanka Ringwald static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){
1195a03dbc20SMilanka Ringwald     switch (callsetup_status){
1196a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1197a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1198a03dbc20SMilanka Ringwald             return true;
1199a03dbc20SMilanka Ringwald         default:
1200a03dbc20SMilanka Ringwald             return false;
1201a03dbc20SMilanka Ringwald    }
1202a03dbc20SMilanka Ringwald }
1203be55a11dSMilanka Ringwald 
1204b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
12054562e2a2SMatthias Ringwald     uint16_t i;
1206a03dbc20SMilanka Ringwald 
12074562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
12084562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
12094562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1210a03dbc20SMilanka Ringwald                 hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1211a03dbc20SMilanka Ringwald                 bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status);
1212a03dbc20SMilanka Ringwald                 bool ringing_new = hfp_is_ringing(new_hf_callsetup_status);
1213a03dbc20SMilanka Ringwald                 if (ringing_old != ringing_new){
1214a03dbc20SMilanka Ringwald                     if (ringing_new){
1215a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING);
1216a03dbc20SMilanka Ringwald                     } else {
1217a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING);
1218a03dbc20SMilanka Ringwald                     }
1219a03dbc20SMilanka Ringwald                 }
1220a03dbc20SMilanka Ringwald                 hfp_hf_callsetup_status = new_hf_callsetup_status;
12214562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1222aeb0f0feSMatthias Ringwald                 hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
12234562e2a2SMatthias Ringwald                 // avoid set but not used warning
1224aeb0f0feSMatthias Ringwald                 (void) hfp_hf_callheld_status;
12254562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1226674ebed5SMilanka Ringwald                 hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1227674ebed5SMilanka Ringwald                 if (hfp_hf_call_status != new_hf_call_status){
1228674ebed5SMilanka Ringwald                     if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){
1229674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1230674ebed5SMilanka Ringwald                     } else {
1231674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1232674ebed5SMilanka Ringwald                     }
1233674ebed5SMilanka Ringwald                 }
1234674ebed5SMilanka Ringwald                 hfp_hf_call_status = new_hf_call_status;
12354562e2a2SMatthias Ringwald             }
12364562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1237ce3797e1SMilanka Ringwald             hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]);
12384562e2a2SMatthias Ringwald             break;
12394562e2a2SMatthias Ringwald         }
12404562e2a2SMatthias Ringwald     }
12414562e2a2SMatthias Ringwald }
12424562e2a2SMatthias Ringwald 
1243426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1244186dd3d2SMatthias Ringwald     int value;
1245186dd3d2SMatthias Ringwald     int i;
1246e2c3b58dSMilanka Ringwald     bool event_emited;
1247e2c3b58dSMilanka Ringwald 
1248a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1249667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1250a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1251a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1252667ec068SMatthias Ringwald             break;
1253667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1254a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1255ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1256667ec068SMatthias Ringwald             break;
1257667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1258a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1259a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1260667ec068SMatthias Ringwald             break;
1261ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1262a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12632308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1264667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1265ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1266ce263fc8SMatthias Ringwald             break;
1267ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1268a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12692308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1270667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1271ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1272ce263fc8SMatthias Ringwald             break;
1273ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1274a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1275ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1276a0ffb263SMatthias Ringwald             break;
1277a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1278a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1279a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1280a0ffb263SMatthias Ringwald             break;
1281a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1282a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1283a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1284ce263fc8SMatthias Ringwald             break;
1285ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1286a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12875a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1288a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1289ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1290ce263fc8SMatthias Ringwald             break;
12910b4debbfSMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
12920b4debbfSMilanka Ringwald             break;
1293fdda66c0SMilanka Ringwald         case HFP_CMD_ERROR:
129490244c92SMilanka Ringwald             switch (hfp_connection->state){
129590244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
129690244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
129790244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1298fdda66c0SMilanka Ringwald                             hfp_reset_context_flags(hfp_connection);
129990244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
130090244c92SMilanka Ringwald                             return;
130190244c92SMilanka Ringwald                         default:
130290244c92SMilanka Ringwald                             break;
130390244c92SMilanka Ringwald                     }
130456f1adacSMilanka Ringwald                     break;
130556f1adacSMilanka Ringwald                 default:
130656f1adacSMilanka Ringwald                     break;
130756f1adacSMilanka Ringwald             }
1308e2c3b58dSMilanka Ringwald 
1309fdda66c0SMilanka Ringwald             // handle error response for voice activation (HF initiated)
13100b4debbfSMilanka Ringwald             switch(hfp_connection->vra_state_requested){
1311de9e0ea7SMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1312de9e0ea7SMilanka Ringwald                     hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1313be55a11dSMilanka Ringwald                     break;
1314be55a11dSMilanka Ringwald                 default:
1315e2c3b58dSMilanka Ringwald                     if (hfp_connection->vra_state_requested == hfp_connection->vra_state){
1316e2c3b58dSMilanka Ringwald                         break;
1317e2c3b58dSMilanka Ringwald                     }
13180b4debbfSMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
1319553a4a56SMilanka Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
13200b4debbfSMilanka Ringwald                     hfp_reset_context_flags(hfp_connection);
13210b4debbfSMilanka Ringwald                     return;
1322be55a11dSMilanka Ringwald             }
1323e2c3b58dSMilanka Ringwald             event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1324e2c3b58dSMilanka Ringwald             if (!event_emited){
13250b4debbfSMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1326e2c3b58dSMilanka Ringwald             }
1327fdda66c0SMilanka Ringwald             hfp_reset_context_flags(hfp_connection);
1328ce263fc8SMatthias Ringwald             break;
1329fdda66c0SMilanka Ringwald 
1330ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1331e2c3b58dSMilanka Ringwald             hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS);
1332ce263fc8SMatthias Ringwald             break;
1333ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
13345a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1335ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1336ce263fc8SMatthias Ringwald             break;
1337ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
13385a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13394562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1340ce263fc8SMatthias Ringwald             break;
1341c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
13425a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1343184a03edSMilanka Ringwald             // report status after SLC established
1344184a03edSMilanka Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
1345184a03edSMilanka Ringwald                 break;
1346184a03edSMilanka Ringwald             }
1347c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1348*1ac1f60fSMilanka Ringwald                 hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1349c741b032SMilanka Ringwald             }
1350c741b032SMilanka Ringwald             break;
13511cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
13521cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13535a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
13541cc65c4fSMatthias Ringwald 			break;
1355eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1356eac56539SMilanka 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));
1357ce263fc8SMatthias Ringwald         default:
1358ce263fc8SMatthias Ringwald             break;
13593deb3ec6SMatthias Ringwald     }
13600cef86faSMatthias Ringwald }
1361426f9988SMatthias Ringwald 
136276cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
136376cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
136476cc1527SMatthias Ringwald }
136576cc1527SMatthias Ringwald 
13660b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1367426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1368426f9988SMatthias Ringwald     if (size < 1) return;
1369426f9988SMatthias Ringwald 
1370426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1371e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1372e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1373e43d1938SMatthias Ringwald #endif
1374426f9988SMatthias Ringwald 
1375426f9988SMatthias Ringwald     // process messages byte-wise
1376a7ba78b0SMilanka Ringwald     uint8_t pos;
1377426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1378426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
13791599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1380426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
13810b4debbfSMilanka Ringwald         hfp_hf_handle_rfcomm_command(hfp_connection);
13823deb3ec6SMatthias Ringwald     }
1383a7ba78b0SMilanka Ringwald }
13843deb3ec6SMatthias Ringwald 
13851c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1386665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1387665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1388665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1389a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
139022387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
13911c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
13923deb3ec6SMatthias Ringwald     }
13933deb3ec6SMatthias Ringwald }
13943deb3ec6SMatthias Ringwald 
13951c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
13960b4debbfSMilanka Ringwald     hfp_connection_t * hfp_connection;
13973deb3ec6SMatthias Ringwald     switch (packet_type){
13983deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
13990b4debbfSMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
14000b4debbfSMilanka Ringwald             if (!hfp_connection) return;
14010b4debbfSMilanka Ringwald             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
14020b4debbfSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
14030b4debbfSMilanka Ringwald             return;
14043deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1405d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1406d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
14071c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1408d4dd47ffSMatthias Ringwald                 return;
1409d4dd47ffSMatthias Ringwald             }
141027950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1411202c8a4cSMatthias Ringwald             break;
14123deb3ec6SMatthias Ringwald         default:
14133deb3ec6SMatthias Ringwald             break;
14143deb3ec6SMatthias Ringwald     }
14151c6a0fc0SMatthias Ringwald     hfp_hf_run();
14163deb3ec6SMatthias Ringwald }
14173deb3ec6SMatthias Ringwald 
14181c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1419405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
14201c6a0fc0SMatthias Ringwald     hfp_hf_run();
1421405014fbSMatthias Ringwald }
1422405014fbSMatthias Ringwald 
1423aeb0f0feSMatthias Ringwald static void hfp_hf_set_defaults(void){
1424aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1425aeb0f0feSMatthias Ringwald     hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
1426aeb0f0feSMatthias Ringwald     hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1427aeb0f0feSMatthias Ringwald     hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1428aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = 0;
1429aeb0f0feSMatthias Ringwald     hfp_hf_speaker_gain = 9;
1430aeb0f0feSMatthias Ringwald     hfp_hf_microphone_gain = 9;
1431aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = 0;
1432aeb0f0feSMatthias Ringwald }
1433aeb0f0feSMatthias Ringwald 
1434b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1435b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1436b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1437b4df8028SMilanka Ringwald         return status;
1438b4df8028SMilanka Ringwald     }
1439b4df8028SMilanka Ringwald 
1440520c92d5SMatthias Ringwald     hfp_init();
1441aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1442d63c37a1SMatthias Ringwald 
14431c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
14441c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
144527950165SMatthias Ringwald 
144627950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
14471c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1448b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
144920b2edb6SMatthias Ringwald }
145027950165SMatthias Ringwald 
145120b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
145220b2edb6SMatthias Ringwald     hfp_deinit();
1453aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1454aeb0f0feSMatthias Ringwald 
1455aeb0f0feSMatthias Ringwald     hfp_hf_callback = NULL;
145620b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
1457aeb0f0feSMatthias Ringwald     (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number));
1458a0ffb263SMatthias Ringwald }
1459a0ffb263SMatthias Ringwald 
14607ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
146168466199SMilanka Ringwald     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
14623deb3ec6SMatthias Ringwald 
1463aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = codecs_nr;
14643deb3ec6SMatthias Ringwald     int i;
14653deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
1466aeb0f0feSMatthias Ringwald         hfp_hf_codecs[i] = codecs[i];
14673deb3ec6SMatthias Ringwald     }
14683deb3ec6SMatthias Ringwald }
14693deb3ec6SMatthias Ringwald 
1470a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
1471aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = supported_features;
1472a0ffb263SMatthias Ringwald }
14733deb3ec6SMatthias Ringwald 
14747ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
1475aeb0f0feSMatthias Ringwald     btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS);
147668466199SMilanka Ringwald 
1477aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = indicators_nr;
14783deb3ec6SMatthias Ringwald     int i;
1479aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
1480aeb0f0feSMatthias Ringwald         hfp_hf_indicators[i] = indicators[i];
14813deb3ec6SMatthias Ringwald     }
14823deb3ec6SMatthias Ringwald }
14833deb3ec6SMatthias Ringwald 
14844eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
14854eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
14863deb3ec6SMatthias Ringwald }
14873deb3ec6SMatthias Ringwald 
1488657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
14899c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1490a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1491657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1492a33eb0c4SMilanka Ringwald     }
14931ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
14941c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1495657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
14963deb3ec6SMatthias Ringwald }
14973deb3ec6SMatthias Ringwald 
14983c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
14999c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1500a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15013c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15023deb3ec6SMatthias Ringwald     }
1503a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
15041c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15053c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15063deb3ec6SMatthias Ringwald }
15073deb3ec6SMatthias Ringwald 
15083c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
15093c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1510ce263fc8SMatthias Ringwald }
1511ce263fc8SMatthias Ringwald 
15123c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
15133c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1514ce263fc8SMatthias Ringwald }
1515ce263fc8SMatthias Ringwald 
15163deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
15173c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
15189c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1519a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15203c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15213deb3ec6SMatthias Ringwald     }
1522a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1523a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
15241c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15253c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15263deb3ec6SMatthias Ringwald }
15273deb3ec6SMatthias Ringwald 
15283c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
15299c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1530a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15313c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15323deb3ec6SMatthias Ringwald     }
15333c65e705SMilanka Ringwald 
1534a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1535ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1536a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1537ce263fc8SMatthias Ringwald             break;
1538ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1539a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1540ce263fc8SMatthias Ringwald             break;
1541ce263fc8SMatthias Ringwald         default:
15423c65e705SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1543ce263fc8SMatthias Ringwald     }
15441c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15453c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15463deb3ec6SMatthias Ringwald }
15473deb3ec6SMatthias Ringwald 
15483c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
15499c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1550a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15513c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15523deb3ec6SMatthias Ringwald     }
1553a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
15541c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15553c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15563deb3ec6SMatthias Ringwald }
15573deb3ec6SMatthias Ringwald 
1558ce263fc8SMatthias Ringwald 
15593c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
15603c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1561ce263fc8SMatthias Ringwald }
1562ce263fc8SMatthias Ringwald 
15633c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
15643c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1565ce263fc8SMatthias Ringwald }
1566ce263fc8SMatthias Ringwald 
156738200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
1568aeb0f0feSMatthias Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4));
156938200c1dSMilanka Ringwald }
1570ce263fc8SMatthias Ringwald 
15713c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
15729c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1573a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15743c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1575a33eb0c4SMilanka Ringwald     }
1576ce263fc8SMatthias Ringwald 
15773c65e705SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
15783c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
15793c65e705SMilanka Ringwald     }
15803c65e705SMilanka Ringwald 
15813c65e705SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
15823c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
15833c65e705SMilanka Ringwald     }
1584f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1585a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1586aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1587aa4dd815SMatthias Ringwald                 break;
1588ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1589ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1590ec3bfc1aSMatthias Ringwald                 break;
1591aa4dd815SMatthias Ringwald             default:
15921cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
15931cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
15941cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
15951cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
159638200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1597aa4dd815SMatthias Ringwald                 break;
15983deb3ec6SMatthias Ringwald         }
1599f4412093SMatthias Ringwald     } else {
1600f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1601f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1602f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1603f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1604f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1605f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1606f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1607f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1608ce263fc8SMatthias Ringwald     }
1609ce263fc8SMatthias Ringwald 
16101c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16113c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16123deb3ec6SMatthias Ringwald }
16133deb3ec6SMatthias Ringwald 
16143c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
16159c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1616a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16173c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1618a33eb0c4SMilanka Ringwald     }
16190b4debbfSMilanka Ringwald     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
16200b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16210b4debbfSMilanka Ringwald     }
16220b4debbfSMilanka Ringwald     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
16230b4debbfSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
16241c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
16250b4debbfSMilanka Ringwald     }
16263c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16273deb3ec6SMatthias Ringwald }
16283deb3ec6SMatthias Ringwald 
16293c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
16309c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1631a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16323c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1633a33eb0c4SMilanka Ringwald     }
1634ce263fc8SMatthias Ringwald 
1635aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1636a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
16371c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1638ce263fc8SMatthias Ringwald     } else {
1639aeb0f0feSMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status);
16403c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1641ce263fc8SMatthias Ringwald     }
16423c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1643ce263fc8SMatthias Ringwald }
1644ce263fc8SMatthias Ringwald 
16453c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
16469c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1647a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16483c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1649a33eb0c4SMilanka Ringwald     }
1650a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
16511c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16523c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1653ce263fc8SMatthias Ringwald }
1654ce263fc8SMatthias Ringwald 
16553c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
16569c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1657a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16583c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1659a33eb0c4SMilanka Ringwald     }
1660ce263fc8SMatthias Ringwald 
1661aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1662a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
16631c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1664ce263fc8SMatthias Ringwald     }
16653c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1666ce263fc8SMatthias Ringwald }
1667ce263fc8SMatthias Ringwald 
16683c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){
16699c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1670a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16713c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1672a33eb0c4SMilanka Ringwald     }
1673ce263fc8SMatthias Ringwald 
1674aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1675a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
16761c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1677ce263fc8SMatthias Ringwald     }
16783c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1679ce263fc8SMatthias Ringwald }
1680ce263fc8SMatthias Ringwald 
16813c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
16829c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1683a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16843c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1685a33eb0c4SMilanka Ringwald     }
1686ce263fc8SMatthias Ringwald 
1687aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1688aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1689a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
16901c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1691ce263fc8SMatthias Ringwald     }
16923c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1693ce263fc8SMatthias Ringwald }
1694ce263fc8SMatthias Ringwald 
16953c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
16969c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1697a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16983c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1699a33eb0c4SMilanka Ringwald     }
1700ce263fc8SMatthias Ringwald 
1701aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1702aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1703a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
17041c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1705ce263fc8SMatthias Ringwald     }
17063c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1707ce263fc8SMatthias Ringwald }
1708ce263fc8SMatthias Ringwald 
17093c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
17109c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1711a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17123c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1713a33eb0c4SMilanka Ringwald     }
1714ce263fc8SMatthias Ringwald 
1715aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1716aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1717a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
17181c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1719ce263fc8SMatthias Ringwald     }
17203c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1721ce263fc8SMatthias Ringwald }
1722ce263fc8SMatthias Ringwald 
17233c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
17249c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1725a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17263c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1727a33eb0c4SMilanka Ringwald     }
1728ce263fc8SMatthias Ringwald 
1729aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1730aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1731a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 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_release_call_with_index(hci_con_handle_t acl_handle, int index){
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     }
1742667ec068SMatthias 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_x = 1;
1746a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
17471c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1748667ec068SMatthias Ringwald     }
17493c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1750667ec068SMatthias Ringwald }
1751667ec068SMatthias Ringwald 
17523c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
17539c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1754a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17553c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1756a33eb0c4SMilanka Ringwald     }
1757667ec068SMatthias Ringwald 
1758aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1759aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1760a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1761a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
17621c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1763667ec068SMatthias Ringwald     }
17643c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1765667ec068SMatthias Ringwald }
1766ce263fc8SMatthias Ringwald 
17673c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
17689c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1769a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17703c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1771a33eb0c4SMilanka Ringwald     }
1772ce263fc8SMatthias Ringwald 
1773a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1774aeb0f0feSMatthias Ringwald     snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number);
17751c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17763c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1777ce263fc8SMatthias Ringwald }
1778ce263fc8SMatthias Ringwald 
17793c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
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 
1785a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1786a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1787a0ffb263SMatthias Ringwald 
17881c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17893c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1790ce263fc8SMatthias Ringwald }
1791ce263fc8SMatthias Ringwald 
17923c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
17939c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1794a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17953c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1796a33eb0c4SMilanka Ringwald     }
1797ce263fc8SMatthias Ringwald 
1798a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
17991c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18003c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1801ce263fc8SMatthias Ringwald }
1802ce263fc8SMatthias Ringwald 
18033c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
18049c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1805a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18063c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1807a33eb0c4SMilanka Ringwald     }
1808ce263fc8SMatthias Ringwald 
1809a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
18101c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18113c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1812ce263fc8SMatthias Ringwald }
1813ce263fc8SMatthias Ringwald 
1814ce263fc8SMatthias Ringwald 
18153c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
18169c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1817a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18183c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1819a33eb0c4SMilanka Ringwald     }
1820ce263fc8SMatthias Ringwald 
1821a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
18221c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18233c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1824ce263fc8SMatthias Ringwald }
1825ce263fc8SMatthias Ringwald 
1826ce263fc8SMatthias Ringwald 
18273c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
18289c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1829a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18303c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1831a33eb0c4SMilanka Ringwald     }
1832ce263fc8SMatthias Ringwald 
1833a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
18341c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18353c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1836ce263fc8SMatthias Ringwald }
1837ce263fc8SMatthias Ringwald 
18383c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
18399c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1840a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18413c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1842a33eb0c4SMilanka Ringwald     }
1843ce263fc8SMatthias Ringwald 
1844a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
18451c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18463c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1847ce263fc8SMatthias Ringwald }
1848ce263fc8SMatthias Ringwald 
18496ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){
18506ba83b5eSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION);
1851aeb0f0feSMatthias Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION);
18526ba83b5eSMilanka Ringwald     return hf && ag;
1853ce263fc8SMatthias Ringwald }
1854ce263fc8SMatthias Ringwald 
18553c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
18569c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1857a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18583c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1859a33eb0c4SMilanka Ringwald     }
18606ba83b5eSMilanka Ringwald     if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){
18616ba83b5eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
18626ba83b5eSMilanka Ringwald     }
1863ce263fc8SMatthias Ringwald 
1864a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
18651c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18663c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1867ce263fc8SMatthias Ringwald }
1868ce263fc8SMatthias Ringwald 
1869acd11d4aSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1870fdda66c0SMilanka Ringwald      hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1871fdda66c0SMilanka Ringwald     if (!hfp_connection) {
1872fdda66c0SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1873be55a11dSMilanka Ringwald     }
1874013cc750SMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1875013cc750SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1876013cc750SMilanka Ringwald     }
1877acd11d4aSMilanka Ringwald 
1878acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1879acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1880acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
1881acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1882af97579eSMilanka Ringwald     }
1883af97579eSMilanka Ringwald 
1884498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1885be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1886de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1887498a8121SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1888fd4151d1SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
1889acd11d4aSMilanka Ringwald             hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported;
1890be55a11dSMilanka Ringwald             break;
1891de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1892de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = true;
1893de9e0ea7SMilanka Ringwald             break;
1894be55a11dSMilanka Ringwald         default:
1895be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1896be55a11dSMilanka Ringwald     }
1897ce263fc8SMatthias Ringwald 
1898af97579eSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1899fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1900af97579eSMilanka Ringwald }
1901af97579eSMilanka Ringwald 
1902acd11d4aSMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
1903af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1904af97579eSMilanka Ringwald     if (!hfp_connection) {
1905af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1906af97579eSMilanka Ringwald     }
1907acd11d4aSMilanka Ringwald     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
190808a0b01cSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
190908a0b01cSMilanka Ringwald     }
1910acd11d4aSMilanka Ringwald 
1911acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1912acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported){
1913acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1914acd11d4aSMilanka Ringwald     }
1915acd11d4aSMilanka Ringwald 
1916acd11d4aSMilanka Ringwald     switch (hfp_connection->vra_state){
1917acd11d4aSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1918acd11d4aSMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1919acd11d4aSMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1920acd11d4aSMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
1921acd11d4aSMilanka Ringwald             break;
1922acd11d4aSMilanka Ringwald         default:
1923fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1924af97579eSMilanka Ringwald     }
1925013cc750SMilanka Ringwald 
1926acd11d4aSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1927acd11d4aSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1928acd11d4aSMilanka Ringwald }
1929acd11d4aSMilanka Ringwald 
1930acd11d4aSMilanka Ringwald 
1931acd11d4aSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1932acd11d4aSMilanka Ringwald     // return deactivate_voice_recognition(acl_handle, false);
1933acd11d4aSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1934acd11d4aSMilanka Ringwald     if (!hfp_connection) {
1935acd11d4aSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1936acd11d4aSMilanka Ringwald     }
1937acd11d4aSMilanka Ringwald 
1938acd11d4aSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
1939acd11d4aSMilanka Ringwald         hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1940acd11d4aSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1941acd11d4aSMilanka Ringwald     }
1942acd11d4aSMilanka Ringwald 
1943acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1944acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1945acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
1946acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1947acd11d4aSMilanka Ringwald     }
1948acd11d4aSMilanka Ringwald 
1949fdda66c0SMilanka Ringwald     switch (hfp_connection->vra_state){
1950de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
1951fdda66c0SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1952de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1953de9e0ea7SMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1954fdda66c0SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1955fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1956fdda66c0SMilanka Ringwald             break;
1957de9e0ea7SMilanka Ringwald 
1958de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1959de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1960de9e0ea7SMilanka Ringwald             hfp_connection->deactivate_voice_recognition = true;
1961de9e0ea7SMilanka Ringwald             break;
1962de9e0ea7SMilanka Ringwald 
1963de9e0ea7SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1964de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1965de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1966fdda66c0SMilanka Ringwald         default:
1967fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1968fdda66c0SMilanka Ringwald     }
1969fdda66c0SMilanka Ringwald 
1970fdda66c0SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1971fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1972af97579eSMilanka Ringwald }
1973af97579eSMilanka Ringwald 
19743c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
19759c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1976a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19773c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1978a33eb0c4SMilanka Ringwald     }
1979c8626498SMilanka Ringwald 
19803c65e705SMilanka Ringwald     if (hfp_connection->microphone_gain == gain) {
19813c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
19823c65e705SMilanka Ringwald     }
19833c65e705SMilanka Ringwald 
1984c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1985a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
19863c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1987a0ffb263SMatthias Ringwald     }
19883c65e705SMilanka Ringwald 
1989a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1990a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
19911c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19923c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1993ce263fc8SMatthias Ringwald }
1994ce263fc8SMatthias Ringwald 
19953c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
19969c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1997a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19983c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1999a33eb0c4SMilanka Ringwald     }
2000c8626498SMilanka Ringwald 
20013c65e705SMilanka Ringwald     if (hfp_connection->speaker_gain == gain){
20023c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
20033c65e705SMilanka Ringwald     }
20043c65e705SMilanka Ringwald 
2005c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
2006a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
20073c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2008a0ffb263SMatthias Ringwald     }
20093c65e705SMilanka Ringwald 
2010a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
2011a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
20121c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20133c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2014ce263fc8SMatthias Ringwald }
2015ce263fc8SMatthias Ringwald 
20163c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
20179c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2018a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20193c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2020a33eb0c4SMilanka Ringwald     }
2021a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
20221c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20233c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2024ce263fc8SMatthias Ringwald }
2025ce263fc8SMatthias Ringwald 
20263c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
20279c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2028a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20293c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2030a33eb0c4SMilanka Ringwald     }
2031a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
20321c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20333c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2034ce263fc8SMatthias Ringwald }
20353deb3ec6SMatthias Ringwald 
20363c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
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     }
2041a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
20421c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20433c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2044667ec068SMatthias Ringwald }
2045667ec068SMatthias Ringwald 
2046667ec068SMatthias Ringwald 
20473c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
20489c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2049a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20503c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2051a33eb0c4SMilanka Ringwald     }
2052a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2053a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
20541c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20553c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2056667ec068SMatthias Ringwald }
2057667ec068SMatthias Ringwald 
20583c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
20599c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2060a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20613c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2062a33eb0c4SMilanka Ringwald     }
2063a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2064a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
20651c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20663c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2067667ec068SMatthias Ringwald }
2068667ec068SMatthias Ringwald 
20693c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
20709c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2071a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20723c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2073a33eb0c4SMilanka Ringwald     }
2074a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2075a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
20761c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20773c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2078667ec068SMatthias Ringwald }
2079667ec068SMatthias Ringwald 
20803c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
20819c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2082a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20833c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2084a33eb0c4SMilanka Ringwald     }
2085a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2086a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
20871c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20883c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2089667ec068SMatthias Ringwald }
2090667ec068SMatthias Ringwald 
20913c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
20929c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2093a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20943c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2095a33eb0c4SMilanka Ringwald     }
2096a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
20971c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20983c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2099667ec068SMatthias Ringwald }
2100667ec068SMatthias Ringwald 
21013c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
21029c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2103a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21043c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2105a33eb0c4SMilanka Ringwald     }
2106667ec068SMatthias Ringwald     // find index for assigned number
2107667ec068SMatthias Ringwald     int i;
2108aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
2109aeb0f0feSMatthias Ringwald         if (hfp_hf_indicators[i] == assigned_number){
2110667ec068SMatthias Ringwald             // set value
2111aeb0f0feSMatthias Ringwald             hfp_hf_indicators_value[i] = value;
2112667ec068SMatthias Ringwald             // mark for update
2113a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
2114a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
2115667ec068SMatthias Ringwald                 // send update
21161c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
2117a0ffb263SMatthias Ringwald             }
21183c65e705SMilanka Ringwald             return ERROR_CODE_SUCCESS;
2119667ec068SMatthias Ringwald         }
2120667ec068SMatthias Ringwald     }
21213c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2122667ec068SMatthias Ringwald }
2123667ec068SMatthias Ringwald 
2124d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
2125d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2126d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
2127d7f6b5cbSMatthias Ringwald         return 0;
2128d7f6b5cbSMatthias Ringwald     }
2129d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
2130d7f6b5cbSMatthias Ringwald }
213176cc1527SMatthias Ringwald 
213276cc1527SMatthias 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){
213376cc1527SMatthias Ringwald 	if (!name){
2134aeb0f0feSMatthias Ringwald 		name = hfp_hf_default_service_name;
213576cc1527SMatthias Ringwald 	}
213676cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
213776cc1527SMatthias Ringwald 
213876cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
213976cc1527SMatthias Ringwald 	//
214076cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
214176cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
214276cc1527SMatthias Ringwald 	//
214376cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
214476cc1527SMatthias Ringwald 	//
214576cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
2146ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
214776cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
214876cc1527SMatthias Ringwald 	}
2149ef3ae4ebSMilanka Ringwald 
2150ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
215156f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
2152ef3ae4ebSMilanka Ringwald     }
2153ef3ae4ebSMilanka Ringwald 
2154ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
215556f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
2156ef3ae4ebSMilanka Ringwald     }
2157ef3ae4ebSMilanka Ringwald 
215876cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
215976cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
216076cc1527SMatthias Ringwald }
216176cc1527SMatthias Ringwald 
216276cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
216368466199SMilanka Ringwald 	btstack_assert(callback != NULL);
216468466199SMilanka Ringwald 
216576cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
216676cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
216776cc1527SMatthias Ringwald }
2168