xref: /btstack/src/classic/hfp_hf.c (revision e2c3b58d3fd5175f415b313f803fc22e5bd181f7)
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
6920b2edb6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
7020b2edb6SMatthias Ringwald 
7120b2edb6SMatthias Ringwald // globals
721c6a0fc0SMatthias Ringwald static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration;
7327950165SMatthias Ringwald 
743deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
7520b2edb6SMatthias Ringwald static uint8_t hfp_codecs_nr;
763deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
773deb3ec6SMatthias Ringwald 
7820b2edb6SMatthias Ringwald static uint8_t hfp_indicators_nr;
7925789943SMilanka Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_INDICATORS];
8025789943SMilanka Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_INDICATORS];
81667ec068SMatthias Ringwald 
8220b2edb6SMatthias Ringwald static uint8_t hfp_hf_speaker_gain;
8320b2edb6SMatthias Ringwald static uint8_t hfp_hf_microphone_gain;
843deb3ec6SMatthias Ringwald 
85ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
863deb3ec6SMatthias Ringwald 
87ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
88ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
89ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
90ce263fc8SMatthias Ringwald 
91ce263fc8SMatthias Ringwald static char phone_number[25];
92ce263fc8SMatthias Ringwald 
9376cc1527SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
9476cc1527SMatthias Ringwald 	int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
9576cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
9676cc1527SMatthias Ringwald 	return hf && ag;
9776cc1527SMatthias Ringwald }
9876cc1527SMatthias Ringwald 
9976cc1527SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
10076cc1527SMatthias Ringwald 	int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
10176cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
10276cc1527SMatthias Ringwald 	return hf && ag;
10376cc1527SMatthias Ringwald }
10476cc1527SMatthias Ringwald 
10576cc1527SMatthias Ringwald 
10676cc1527SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
10776cc1527SMatthias Ringwald 	int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
10876cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
10976cc1527SMatthias Ringwald 	return hf && ag;
11076cc1527SMatthias Ringwald }
11176cc1527SMatthias Ringwald 
11276cc1527SMatthias Ringwald 
1139c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
1149c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1159c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1169c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1179c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1189c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1199c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1209c9c64c1SMatthias Ringwald         return hfp_connection;
1219c9c64c1SMatthias Ringwald     }
1229c9c64c1SMatthias Ringwald     return NULL;
1239c9c64c1SMatthias Ringwald }
1249c9c64c1SMatthias Ringwald 
12576cc1527SMatthias Ringwald /* emit functinos */
1263deb3ec6SMatthias Ringwald 
127a473a009SMatthias Ringwald static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){
128a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
129d703d377SMatthias Ringwald     uint8_t event[33];
130a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
131a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
132a473a009SMatthias Ringwald     event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION;
133d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
134d703d377SMatthias Ringwald     event[5] = status;
135d703d377SMatthias Ringwald     event[6] = hfp_connection->bnip_type;
136d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 8);
137d703d377SMatthias Ringwald     strncpy((char*)&event[7], hfp_connection->bnip_number, size);
138d703d377SMatthias Ringwald     event[7 + size] = 0;
139a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
140a0ffb263SMatthias Ringwald }
141a0ffb263SMatthias Ringwald 
142a473a009SMatthias Ringwald static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){
143a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
144d703d377SMatthias Ringwald     uint8_t event[32];
145a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
146a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
147a0ffb263SMatthias Ringwald     event[2] = event_subtype;
148d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
149d703d377SMatthias Ringwald     event[5] = hfp_connection->bnip_type;
150d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 7);
151d703d377SMatthias Ringwald     strncpy((char*)&event[6], hfp_connection->bnip_number, size);
152d703d377SMatthias Ringwald     event[6 + 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_enhanced_call_status(const hfp_connection_t * hfp_connection){
157a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
158d703d377SMatthias Ringwald     uint8_t event[38];
1590aee97efSMilanka Ringwald     int pos = 0;
1600aee97efSMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
1610aee97efSMilanka Ringwald     event[pos++] = sizeof(event) - 2;
1620aee97efSMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
163d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
164d703d377SMatthias Ringwald     pos += 2;
165a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_idx;
166a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_dir;
167a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_status;
168a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mode;
169a473a009SMatthias Ringwald     event[pos++] = hfp_connection->clcc_mpty;
170a473a009SMatthias Ringwald     event[pos++] = hfp_connection->bnip_type;
171d703d377SMatthias Ringwald     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - pos - 1);
172a473a009SMatthias Ringwald     strncpy((char*)&event[pos], hfp_connection->bnip_number, size);
1730aee97efSMilanka Ringwald     pos += size;
1740aee97efSMilanka Ringwald     event[pos++] = 0;
175a473a009SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
176a0ffb263SMatthias Ringwald }
177a0ffb263SMatthias Ringwald 
17876cc1527SMatthias Ringwald 
179a473a009SMatthias Ringwald static void hfp_emit_ag_indicator_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
180a473a009SMatthias Ringwald 	if (hfp_hf_callback == NULL) return;
181d703d377SMatthias Ringwald 	uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE+1];
18276cc1527SMatthias Ringwald 	int pos = 0;
18376cc1527SMatthias Ringwald 	event[pos++] = HCI_EVENT_HFP_META;
18476cc1527SMatthias Ringwald 	event[pos++] = sizeof(event) - 2;
18576cc1527SMatthias Ringwald 	event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
186d703d377SMatthias Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
187d703d377SMatthias Ringwald     pos += 2;
188a473a009SMatthias Ringwald 	event[pos++] = indicator->index;
189a473a009SMatthias Ringwald 	event[pos++] = indicator->status;
190a473a009SMatthias Ringwald 	event[pos++] = indicator->min_range;
191a473a009SMatthias Ringwald 	event[pos++] = indicator->max_range;
192a473a009SMatthias Ringwald 	event[pos++] = indicator->mandatory;
193a473a009SMatthias Ringwald 	event[pos++] = indicator->enabled;
194a473a009SMatthias Ringwald 	event[pos++] = indicator->status_changed;
195a473a009SMatthias Ringwald 	strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
19676cc1527SMatthias Ringwald 	pos += HFP_MAX_INDICATOR_DESC_SIZE;
19776cc1527SMatthias Ringwald 	event[pos] = 0;
198a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1993deb3ec6SMatthias Ringwald }
2003deb3ec6SMatthias Ringwald 
201a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){
202a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
203d703d377SMatthias Ringwald 	uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
20476cc1527SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
20576cc1527SMatthias Ringwald 	event[1] = sizeof(event) - 2;
20676cc1527SMatthias Ringwald 	event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
207d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
208d703d377SMatthias Ringwald 	event[5] = hfp_connection->network_operator.mode;
209d703d377SMatthias Ringwald 	event[6] = hfp_connection->network_operator.format;
210d703d377SMatthias Ringwald 	strncpy((char*)&event[7], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
211d703d377SMatthias Ringwald 	event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
212a473a009SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
2133deb3ec6SMatthias Ringwald }
2143deb3ec6SMatthias Ringwald 
215b95cac54SMilanka Ringwald 
216b95cac54SMilanka Ringwald static void hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection){
217b95cac54SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
218b95cac54SMilanka Ringwald     uint8_t event[HFP_MAX_VR_TEXT_SIZE];
219b95cac54SMilanka Ringwald     int pos = 0;
220b95cac54SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
221b95cac54SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
222b95cac54SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE;
223b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
224b95cac54SMilanka Ringwald     pos += 2;
225b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->ag_msg.text_id);
226b95cac54SMilanka Ringwald     pos += 2;
227b95cac54SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_operation;
228b95cac54SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_type;
229b95cac54SMilanka Ringwald 
230b95cac54SMilanka Ringwald     // length, zero ending
231b95cac54SMilanka Ringwald     uint16_t value_length = hfp_connection->ag_vra_msg_length;
232b95cac54SMilanka Ringwald     uint8_t * value = &hfp_connection->line_buffer[0];
233b95cac54SMilanka Ringwald     uint16_t size = btstack_min(value_length, sizeof(event) - pos - 2 - 1);
234b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, size+1);
235b95cac54SMilanka Ringwald     pos += 2;
236b95cac54SMilanka Ringwald     memcpy(&event[pos], value, size);
237b95cac54SMilanka Ringwald     event[pos + size] = 0;
238b95cac54SMilanka Ringwald     pos += size + 1;
239b95cac54SMilanka Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
240b95cac54SMilanka Ringwald }
241b95cac54SMilanka Ringwald 
24276cc1527SMatthias Ringwald /* send commands */
24389425bfcSMilanka Ringwald 
24489425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2453deb3ec6SMatthias Ringwald     char buffer[20];
2461599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
24789425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
24889425bfcSMilanka Ringwald }
24989425bfcSMilanka Ringwald 
25089425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
25189425bfcSMilanka Ringwald     char buffer[20];
2521599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
25389425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
25489425bfcSMilanka Ringwald }
25589425bfcSMilanka Ringwald 
25686da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
25789425bfcSMilanka Ringwald     char buffer[40];
2581599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
2593deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2603deb3ec6SMatthias Ringwald }
2613deb3ec6SMatthias Ringwald 
2623deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2633deb3ec6SMatthias Ringwald     char buffer[30];
26489425bfcSMilanka Ringwald     const int size = sizeof(buffer);
26589425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
26689425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
2671599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2683deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2693deb3ec6SMatthias Ringwald }
2703deb3ec6SMatthias Ringwald 
2713deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2723deb3ec6SMatthias Ringwald     char buffer[50];
27389425bfcSMilanka Ringwald     const int size = sizeof(buffer);
27489425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
27589425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
2761599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2773deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2783deb3ec6SMatthias Ringwald }
2793deb3ec6SMatthias Ringwald 
2803deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2813deb3ec6SMatthias Ringwald     char buffer[30];
28289425bfcSMilanka Ringwald     const int size = sizeof(buffer);
28389425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
28489425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
2851599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2863deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2873deb3ec6SMatthias Ringwald }
2883deb3ec6SMatthias Ringwald 
28989425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2903deb3ec6SMatthias Ringwald     char buffer[20];
2911599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
292ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
293ce263fc8SMatthias Ringwald }
294ce263fc8SMatthias Ringwald 
295ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
296ce263fc8SMatthias Ringwald     char buffer[40];
2971599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, phone_number);
298ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
299ce263fc8SMatthias Ringwald }
300ce263fc8SMatthias Ringwald 
301a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
302ce263fc8SMatthias Ringwald     char buffer[40];
3031599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
304ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
305ce263fc8SMatthias Ringwald }
306ce263fc8SMatthias Ringwald 
307f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
30889425bfcSMilanka Ringwald     char buffer[40];
3091599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
310ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
311ce263fc8SMatthias Ringwald }
312ce263fc8SMatthias Ringwald 
313ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
314ce263fc8SMatthias Ringwald     char buffer[20];
3151599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
316ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
317ce263fc8SMatthias Ringwald }
318ce263fc8SMatthias Ringwald 
31997d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
3201599fe57SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "ATA\r");
32197d2cadbSMatthias Ringwald }
32297d2cadbSMatthias Ringwald 
32389425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
32489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
32589425bfcSMilanka Ringwald }
32689425bfcSMilanka Ringwald 
32789425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
32889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
32989425bfcSMilanka Ringwald }
33089425bfcSMilanka Ringwald 
33189425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
33289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
33389425bfcSMilanka Ringwald }
33489425bfcSMilanka Ringwald 
33589425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
33689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
33789425bfcSMilanka Ringwald }
33889425bfcSMilanka Ringwald 
33989425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
34089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
34189425bfcSMilanka Ringwald }
34289425bfcSMilanka Ringwald 
34389425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
34489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
34589425bfcSMilanka Ringwald }
34689425bfcSMilanka Ringwald 
34789425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
34889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
34989425bfcSMilanka Ringwald }
35089425bfcSMilanka Ringwald 
35189425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
35289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
35389425bfcSMilanka Ringwald }
35489425bfcSMilanka Ringwald 
35589425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
35689425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
35789425bfcSMilanka Ringwald }
35889425bfcSMilanka Ringwald 
35989425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
36089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
36189425bfcSMilanka Ringwald }
36289425bfcSMilanka Ringwald 
36389425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
36489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
36589425bfcSMilanka Ringwald }
36689425bfcSMilanka Ringwald 
36789425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
36889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
36989425bfcSMilanka Ringwald }
37089425bfcSMilanka Ringwald 
37189425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
37289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
37389425bfcSMilanka Ringwald }
37489425bfcSMilanka Ringwald 
37589425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
37689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
37789425bfcSMilanka Ringwald }
37889425bfcSMilanka Ringwald 
37989425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
38089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
38189425bfcSMilanka Ringwald }
38289425bfcSMilanka Ringwald 
38389425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
38489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
38589425bfcSMilanka Ringwald }
38689425bfcSMilanka Ringwald 
38789425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
38889425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
38989425bfcSMilanka Ringwald }
39089425bfcSMilanka Ringwald 
39189425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
39289425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
39389425bfcSMilanka Ringwald }
39489425bfcSMilanka Ringwald 
395ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
39689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
397ce263fc8SMatthias Ringwald }
398ce263fc8SMatthias Ringwald 
399667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
40089425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
401667ec068SMatthias Ringwald }
402667ec068SMatthias Ringwald 
40376cc1527SMatthias Ringwald /* state machines */
4043deb3ec6SMatthias Ringwald 
405a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
406a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
407a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
408aa4dd815SMatthias Ringwald     int done = 1;
409498a8121SMilanka Ringwald     log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state);
410a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4113deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
412d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
413a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
414a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4153deb3ec6SMatthias Ringwald             break;
4163deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
417a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
418a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4193deb3ec6SMatthias Ringwald             break;
4203deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
421a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
422a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4233deb3ec6SMatthias Ringwald             break;
4243deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
425a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
426a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4273deb3ec6SMatthias Ringwald             break;
4283deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
429a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
430a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4313deb3ec6SMatthias Ringwald             break;
4323deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
433a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
434a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4353deb3ec6SMatthias Ringwald             break;
4363deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
437a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
438a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4393deb3ec6SMatthias Ringwald             break;
4403deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
441a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
442a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4433deb3ec6SMatthias Ringwald             break;
4443deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
445a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
446a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4473deb3ec6SMatthias Ringwald             break;
4483deb3ec6SMatthias Ringwald         default:
449aa4dd815SMatthias Ringwald             done = 0;
4503deb3ec6SMatthias Ringwald             break;
4513deb3ec6SMatthias Ringwald     }
4523deb3ec6SMatthias Ringwald     return done;
4533deb3ec6SMatthias Ringwald }
4543deb3ec6SMatthias Ringwald 
455ce263fc8SMatthias Ringwald 
456a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
457a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
458498a8121SMilanka Ringwald     if (hfp_connection->ok_pending){
459498a8121SMilanka Ringwald         return 0;
460498a8121SMilanka Ringwald     }
461ce263fc8SMatthias Ringwald     int done = 0;
462a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
463a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
464ce263fc8SMatthias Ringwald         done = 1;
465a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
466ce263fc8SMatthias Ringwald         return done;
467ce263fc8SMatthias Ringwald     };
468a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
469a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
470ce263fc8SMatthias Ringwald         done = 1;
471a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
472a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
473a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
474ce263fc8SMatthias Ringwald         return done;
475ce263fc8SMatthias Ringwald     }
476ce263fc8SMatthias Ringwald 
477a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
478ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
479a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
480a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
481a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
482ce263fc8SMatthias Ringwald             return 1;
483ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
484a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
485a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
486a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
487ce263fc8SMatthias Ringwald             return 1;
488ce263fc8SMatthias Ringwald         default:
489ce263fc8SMatthias Ringwald             break;
490ce263fc8SMatthias Ringwald     }
491ce263fc8SMatthias Ringwald 
492a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
493a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
494ce263fc8SMatthias Ringwald         done = 1;
495a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
496ce263fc8SMatthias Ringwald         return done;
497ce263fc8SMatthias Ringwald     }
498ce263fc8SMatthias Ringwald 
499ce263fc8SMatthias Ringwald     return done;
500ce263fc8SMatthias Ringwald }
501ce263fc8SMatthias Ringwald 
502af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
503be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
504be55a11dSMilanka Ringwald         return 0;
505be55a11dSMilanka Ringwald     }
506be55a11dSMilanka Ringwald     int done = 0;
507fd4151d1SMilanka Ringwald 
5080b4debbfSMilanka Ringwald     if (hfp_connection->ok_pending == 1){
5090b4debbfSMilanka Ringwald         return 0;
5100b4debbfSMilanka Ringwald     }
5110b4debbfSMilanka Ringwald     // voice recognition activated from AG
5120b4debbfSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){
5130b4debbfSMilanka Ringwald         switch(hfp_connection->vra_state_requested){
5140b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
5150b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
516de9e0ea7SMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
5170b4debbfSMilanka Ringwald                 // ignore AG command, continue to wait for OK
5180b4debbfSMilanka Ringwald                 return 0;
519cf75be85SMilanka Ringwald 
5200b4debbfSMilanka Ringwald             default:
521b95cac54SMilanka Ringwald                 if (hfp_connection->ag_vra_msg_length > 0){
522b95cac54SMilanka Ringwald                     hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection);
523b95cac54SMilanka Ringwald                     hfp_connection->ag_vra_msg_length = 0;
524b95cac54SMilanka Ringwald                     break;
525b95cac54SMilanka Ringwald                 }
526cf75be85SMilanka Ringwald                 switch(hfp_connection->ag_vra_state){
527cf75be85SMilanka Ringwald                     case HFP_VOICE_RECOGNITION_STATE_AG_READY:
528013cc750SMilanka Ringwald                         switch (hfp_connection->ag_vra_status){
529013cc750SMilanka Ringwald                             case 0:
5300b4debbfSMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
531013cc750SMilanka Ringwald                                 break;
532013cc750SMilanka Ringwald                             case 1:
533013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
534013cc750SMilanka Ringwald                                 break;
535013cc750SMilanka Ringwald                             case 2:
536013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
537013cc750SMilanka Ringwald                                 break;
538013cc750SMilanka Ringwald                             default:
539013cc750SMilanka Ringwald                                 break;
5400b4debbfSMilanka Ringwald                         }
5410b4debbfSMilanka Ringwald                         break;
542cf75be85SMilanka Ringwald                     default:
543cf75be85SMilanka Ringwald                         // state messages from AG
544cf75be85SMilanka Ringwald                         hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
545cf75be85SMilanka Ringwald                         break;
546cf75be85SMilanka Ringwald                 }
547cf75be85SMilanka Ringwald                 break;
5480b4debbfSMilanka Ringwald         }
5490b4debbfSMilanka Ringwald         hfp_connection->command = HFP_CMD_NONE;
5500b4debbfSMilanka Ringwald     }
5510b4debbfSMilanka Ringwald 
5520b4debbfSMilanka Ringwald 
553498a8121SMilanka Ringwald     switch (hfp_connection->vra_state_requested){
554fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
555fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
556fdda66c0SMilanka Ringwald             if (done != 0){
557fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
558498a8121SMilanka Ringwald                 hfp_connection->ok_pending = 1;
559498a8121SMilanka Ringwald             }
560fd4151d1SMilanka Ringwald             return 1;
561fd4151d1SMilanka Ringwald 
562fd4151d1SMilanka Ringwald 
563fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
564fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
565fdda66c0SMilanka Ringwald             if (done != 0){
566fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
567fd4151d1SMilanka Ringwald                 hfp_connection->ok_pending = 1;
568fd4151d1SMilanka Ringwald                 return 1;
5690b4debbfSMilanka Ringwald             }
5700b4debbfSMilanka Ringwald             break;
571013cc750SMilanka Ringwald 
572de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
573de9e0ea7SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
574de9e0ea7SMilanka Ringwald             if (done != 0){
575de9e0ea7SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
576de9e0ea7SMilanka Ringwald                 hfp_connection->ok_pending = 1;
577de9e0ea7SMilanka Ringwald                 return 1;
578de9e0ea7SMilanka Ringwald             }
579de9e0ea7SMilanka Ringwald             break;
580de9e0ea7SMilanka Ringwald 
581de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
582de9e0ea7SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
583de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
584de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
585de9e0ea7SMilanka Ringwald             if (hfp_connection->activate_voice_recognition){
586de9e0ea7SMilanka Ringwald                 hfp_hf_activate_voice_recognition(hfp_connection->acl_handle);
587de9e0ea7SMilanka Ringwald             } else {
588de9e0ea7SMilanka Ringwald                 hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
589de9e0ea7SMilanka Ringwald             }
590de9e0ea7SMilanka Ringwald             break;
591de9e0ea7SMilanka Ringwald 
592be55a11dSMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
593498a8121SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
594498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
595de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
596de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
597de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
598de9e0ea7SMilanka Ringwald             } else {
5991a26de69SMilanka Ringwald                 hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
600de9e0ea7SMilanka Ringwald             }
601be55a11dSMilanka Ringwald             break;
602be55a11dSMilanka Ringwald 
603de9e0ea7SMilanka Ringwald 
604013cc750SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
605013cc750SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
606498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
607de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
608de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
609de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
610de9e0ea7SMilanka Ringwald             } else {
611de9e0ea7SMilanka Ringwald                 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
612de9e0ea7SMilanka Ringwald             }
613be55a11dSMilanka Ringwald             break;
614fd4151d1SMilanka Ringwald 
615be55a11dSMilanka Ringwald         default:
616be55a11dSMilanka Ringwald             break;
617be55a11dSMilanka Ringwald     }
618be55a11dSMilanka Ringwald     return done;
619be55a11dSMilanka Ringwald }
620be55a11dSMilanka Ringwald 
621be55a11dSMilanka Ringwald 
622be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
623a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
624ce263fc8SMatthias Ringwald 
625332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
626332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
627ce263fc8SMatthias Ringwald 
628a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
629a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
630332ca98fSMatthias Ringwald 		return 1;
631332ca98fSMatthias Ringwald     }
632332ca98fSMatthias Ringwald 
6331cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
6341cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
635ce263fc8SMatthias Ringwald 
636a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
637fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
6381cc65c4fSMatthias Ringwald 		return 1;
6391cc65c4fSMatthias Ringwald     }
6401cc65c4fSMatthias Ringwald 
6411cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
6421cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
6431cc65c4fSMatthias Ringwald 
644a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
645a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
6461cc65c4fSMatthias Ringwald 		return 1;
6471cc65c4fSMatthias Ringwald     }
648ce263fc8SMatthias Ringwald 
649ce263fc8SMatthias Ringwald     return 0;
650ce263fc8SMatthias Ringwald }
651ce263fc8SMatthias Ringwald 
652a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
653505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
654505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
655ce263fc8SMatthias Ringwald 
65664f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
657a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
658a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
659a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
660ce263fc8SMatthias Ringwald         return 1;
661ce263fc8SMatthias Ringwald     }
662ce263fc8SMatthias Ringwald 
663a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
664ce263fc8SMatthias Ringwald 
665ce263fc8SMatthias Ringwald     // run codecs exchange
666a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
667ce263fc8SMatthias Ringwald     if (done) return 1;
668ce263fc8SMatthias Ringwald 
66938200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
67038200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
67138200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
67238200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
67338200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
67438200c1dSMilanka Ringwald         return 1;
67538200c1dSMilanka Ringwald     }
676ce263fc8SMatthias Ringwald     return 0;
677ce263fc8SMatthias Ringwald }
678ce263fc8SMatthias Ringwald 
67938200c1dSMilanka Ringwald 
680a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
681eaf2b0a1SMatthias Ringwald 
682eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
683eaf2b0a1SMatthias Ringwald 
684a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
685a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
686a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
687ce263fc8SMatthias Ringwald         return 1;
688ce263fc8SMatthias Ringwald     }
689ce263fc8SMatthias Ringwald     return 0;
690ce263fc8SMatthias Ringwald }
691ce263fc8SMatthias Ringwald 
6921c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
6937522e673SMatthias Ringwald 
69476cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
69576cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
69676cc1527SMatthias Ringwald 
69776cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
69876cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
69922387625SMatthias Ringwald 
7003721a235SMatthias Ringwald 	// assert command could be sent
7013721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
7023721a235SMatthias Ringwald 
7033721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
7043721a235SMatthias Ringwald     // WBS Disassociate
7053721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
7063721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
7073721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
7083721a235SMatthias Ringwald         return;
7093721a235SMatthias Ringwald     }
7103721a235SMatthias Ringwald     // Write Codec Config
7113721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
7123721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
7133721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
7143721a235SMatthias Ringwald         return;
7153721a235SMatthias Ringwald     }
7163721a235SMatthias Ringwald     // WBS Associate
7173721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
7183721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
7193721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
7203721a235SMatthias Ringwald         return;
7213721a235SMatthias Ringwald     }
7223721a235SMatthias Ringwald #endif
723689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
724689d4323SMatthias Ringwald     // Enable WBS
725689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
726689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
727689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
728689d4323SMatthias Ringwald         return;
729689d4323SMatthias Ringwald     }
730689d4323SMatthias Ringwald     // Write I2S/PCM params
731689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
732689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
733689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
734689d4323SMatthias Ringwald         return;
735689d4323SMatthias Ringwald     }
736689d4323SMatthias Ringwald     // Disable WBS
737689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
738689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
739689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
740689d4323SMatthias Ringwald         return;
741689d4323SMatthias Ringwald     }
742689d4323SMatthias Ringwald #endif
74348e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
74448e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
74548e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
74648e6eeeeSMatthias Ringwald         return;
74748e6eeeeSMatthias Ringwald     }
74848e6eeeeSMatthias Ringwald #endif
7493721a235SMatthias Ringwald 
750cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
751cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
752cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
7537522e673SMatthias Ringwald         // notify about codec selection if not done already
7547522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
7557522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
7567522e673SMatthias Ringwald         }
757cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
7587522e673SMatthias Ringwald         return;
7597522e673SMatthias Ringwald     }
7607522e673SMatthias Ringwald 
761d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
762d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
763d4dd47ffSMatthias Ringwald         return;
764d4dd47ffSMatthias Ringwald     }
765a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
766ce263fc8SMatthias Ringwald     if (!done){
767a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
768ce263fc8SMatthias Ringwald     }
769ce263fc8SMatthias Ringwald     if (!done){
770c95b5b3cSMilanka Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
771be55a11dSMilanka Ringwald     }
772be55a11dSMilanka Ringwald     if (!done){
773c95b5b3cSMilanka Ringwald         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
774ce263fc8SMatthias Ringwald     }
775ce263fc8SMatthias Ringwald     if (!done){
776a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
777ce263fc8SMatthias Ringwald     }
778ce263fc8SMatthias Ringwald 
779*e2c3b58dSMilanka Ringwald     // don't send a new command while ok still pending or SLC is not established
780*e2c3b58dSMilanka Ringwald     if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){
781*e2c3b58dSMilanka Ringwald         return;
782*e2c3b58dSMilanka Ringwald     }
7831016a228SMatthias Ringwald 
784a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
785a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
786a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
787a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
788ce263fc8SMatthias Ringwald         return;
789ce263fc8SMatthias Ringwald     }
790ce263fc8SMatthias Ringwald 
791a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
792a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
793a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
794a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
795ce263fc8SMatthias Ringwald         return;
796ce263fc8SMatthias Ringwald     }
797ce263fc8SMatthias Ringwald 
798a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
799a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
800a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
801a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
802ce263fc8SMatthias Ringwald         return;
803ce263fc8SMatthias Ringwald     }
804ce263fc8SMatthias Ringwald 
805a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
806a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
807a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
808a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
809ce263fc8SMatthias Ringwald         return;
810ce263fc8SMatthias Ringwald     }
811ce263fc8SMatthias Ringwald 
812a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
813a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
814*e2c3b58dSMilanka Ringwald         hfp_connection->hf_ok_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR;
815a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
816*e2c3b58dSMilanka Ringwald         hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0);
817ce263fc8SMatthias Ringwald         return;
818ce263fc8SMatthias Ringwald     }
819ce263fc8SMatthias Ringwald 
820a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
821a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
822a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
823a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
824ce263fc8SMatthias Ringwald         return;
825ce263fc8SMatthias Ringwald     }
826ce263fc8SMatthias Ringwald 
827a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
828a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
829a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
830a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
831ce263fc8SMatthias Ringwald         return;
832ce263fc8SMatthias Ringwald     }
833ce263fc8SMatthias Ringwald 
834a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
835a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
836a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
837a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
838ce263fc8SMatthias Ringwald         return;
839ce263fc8SMatthias Ringwald     }
840ce263fc8SMatthias Ringwald 
841a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
842a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
843a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
844a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
845ce263fc8SMatthias Ringwald         return;
846ce263fc8SMatthias Ringwald     }
847ce263fc8SMatthias Ringwald 
848a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
849a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
850a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
851a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
852ce263fc8SMatthias Ringwald         return;
853ce263fc8SMatthias Ringwald     }
854ce263fc8SMatthias Ringwald 
855a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
856a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
857a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
858a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
859ce263fc8SMatthias Ringwald         return;
860ce263fc8SMatthias Ringwald     }
861ce263fc8SMatthias Ringwald 
862a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
863a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
864a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
865a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
866ce263fc8SMatthias Ringwald         return;
867ce263fc8SMatthias Ringwald     }
868ce263fc8SMatthias Ringwald 
869a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
870a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
871a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
872a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
873ce263fc8SMatthias Ringwald         return;
874ce263fc8SMatthias Ringwald     }
875ce263fc8SMatthias Ringwald 
876a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
877a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
878a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
879a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
880ce263fc8SMatthias Ringwald         return;
881ce263fc8SMatthias Ringwald     }
882ce263fc8SMatthias Ringwald 
883a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
884a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
885a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
886a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
887ce263fc8SMatthias Ringwald         return;
888ce263fc8SMatthias Ringwald     }
889ce263fc8SMatthias Ringwald 
890a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
891a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
892a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
893a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
894ce263fc8SMatthias Ringwald         return;
895ce263fc8SMatthias Ringwald     }
896ce263fc8SMatthias Ringwald 
897a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
898a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
899a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
900a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
901667ec068SMatthias Ringwald         return;
902667ec068SMatthias Ringwald     }
903667ec068SMatthias Ringwald 
904a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
905a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
906a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
907a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
908a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
909ce263fc8SMatthias Ringwald         return;
910ce263fc8SMatthias Ringwald     }
911ce263fc8SMatthias Ringwald 
912a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
913a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
914a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
915a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
916ce263fc8SMatthias Ringwald         return;
917ce263fc8SMatthias Ringwald     }
918ce263fc8SMatthias Ringwald 
919a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
920a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
921a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
922a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
923667ec068SMatthias Ringwald         return;
924667ec068SMatthias Ringwald     }
925667ec068SMatthias Ringwald 
926a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
927a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
928667ec068SMatthias Ringwald         char buffer[20];
929a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
930667ec068SMatthias Ringwald             case '?':
9311599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
932ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
933ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
934a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
935667ec068SMatthias Ringwald                 return;
936667ec068SMatthias Ringwald             case '0':
937667ec068SMatthias Ringwald             case '1':
938667ec068SMatthias Ringwald             case '2':
9391599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
940ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
941ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
942ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
943a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
944667ec068SMatthias Ringwald                 return;
945667ec068SMatthias Ringwald             default:
946667ec068SMatthias Ringwald                 break;
947667ec068SMatthias Ringwald         }
948667ec068SMatthias Ringwald         return;
949667ec068SMatthias Ringwald     }
950667ec068SMatthias Ringwald 
951a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
952a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
953667ec068SMatthias Ringwald         char buffer[20];
9541599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
955ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
956ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
957a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
958667ec068SMatthias Ringwald         return;
959667ec068SMatthias Ringwald     }
960667ec068SMatthias Ringwald 
961667ec068SMatthias Ringwald     // update HF indicators
962a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
963667ec068SMatthias Ringwald         int i;
964667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
965a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
966a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
967a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
968a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
969667ec068SMatthias Ringwald                     char buffer[30];
9701599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
971ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
972ff7d6aeaSMatthias Ringwald                              hfp_indicators[i],
973ff7d6aeaSMatthias Ringwald                              (unsigned int)hfp_indicators_value[i]);
974ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
975a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
976667ec068SMatthias Ringwald                 } else {
97760ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
978667ec068SMatthias Ringwald                 }
979667ec068SMatthias Ringwald                 return;
980667ec068SMatthias Ringwald             }
981667ec068SMatthias Ringwald         }
982667ec068SMatthias Ringwald     }
983667ec068SMatthias Ringwald 
984ce263fc8SMatthias Ringwald     if (done) return;
985ce263fc8SMatthias Ringwald     // deal with disconnect
986a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
987ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
988a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
989a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
990ce263fc8SMatthias Ringwald             break;
991ce263fc8SMatthias Ringwald 
992ce263fc8SMatthias Ringwald         default:
993ce263fc8SMatthias Ringwald             break;
994ce263fc8SMatthias Ringwald     }
995ce263fc8SMatthias Ringwald }
996ce263fc8SMatthias Ringwald 
997a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
998a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
9996a7f44bdSMilanka Ringwald 
1000ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
10017522e673SMatthias Ringwald 
1002667ec068SMatthias Ringwald     // restore volume settings
1003a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
1004a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1005ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
1006a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
1007a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1008ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
1009667ec068SMatthias Ringwald     // enable all indicators
1010667ec068SMatthias Ringwald     int i;
1011667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
1012a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
1013a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
1014667ec068SMatthias Ringwald     }
1015ce263fc8SMatthias Ringwald }
1016ce263fc8SMatthias Ringwald 
10171cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
10181cc65c4fSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
10191cc65c4fSMatthias Ringwald 		// Codec supported, confirm
10201cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
10211cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
10221cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
10231cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
10241cc65c4fSMatthias Ringwald 
10251cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
10261cc65c4fSMatthias Ringwald 	} else {
10271cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
10281cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
10291cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
10301cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
10311cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10321cc65c4fSMatthias Ringwald 
10331cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
10341cc65c4fSMatthias Ringwald 	}
10351cc65c4fSMatthias Ringwald }
10361cc65c4fSMatthias Ringwald 
1037*e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){
1038*e2c3b58dSMilanka Ringwald     bool event_emited = true;
1039*e2c3b58dSMilanka Ringwald 
1040*e2c3b58dSMilanka Ringwald     switch (hfp_connection->hf_ok_pending_for_command){
1041*e2c3b58dSMilanka Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1042*e2c3b58dSMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status);
1043*e2c3b58dSMilanka Ringwald             break;
1044*e2c3b58dSMilanka Ringwald         default:
1045*e2c3b58dSMilanka Ringwald             event_emited = false;
1046*e2c3b58dSMilanka Ringwald 
1047a0ffb263SMatthias Ringwald             switch (hfp_connection->state){
10483deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
1049a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
1050a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_NOTIFY_ON_CODECS;
10513deb3ec6SMatthias Ringwald                         break;
10523deb3ec6SMatthias Ringwald                     }
1053a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
10543deb3ec6SMatthias Ringwald                     break;
10553deb3ec6SMatthias Ringwald 
10563deb3ec6SMatthias Ringwald                 case HFP_W4_NOTIFY_ON_CODECS:
1057a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
10583deb3ec6SMatthias Ringwald                     break;
10593deb3ec6SMatthias Ringwald 
10603deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS:
1061a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
10623deb3ec6SMatthias Ringwald                     break;
10633deb3ec6SMatthias Ringwald 
10643deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1065a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
10663deb3ec6SMatthias Ringwald                     break;
10673deb3ec6SMatthias Ringwald 
10683deb3ec6SMatthias Ringwald                 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1069a0ffb263SMatthias Ringwald                     if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1070a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
10713deb3ec6SMatthias Ringwald                         break;
10723deb3ec6SMatthias Ringwald                     }
1073a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1074a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
10753deb3ec6SMatthias Ringwald                         break;
10763deb3ec6SMatthias Ringwald                     }
1077a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
10783deb3ec6SMatthias Ringwald                     break;
10793deb3ec6SMatthias Ringwald 
10803deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1081a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1082a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
10833deb3ec6SMatthias Ringwald                         break;
10843deb3ec6SMatthias Ringwald                     }
1085a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
10863deb3ec6SMatthias Ringwald                     break;
10873deb3ec6SMatthias Ringwald 
10883deb3ec6SMatthias Ringwald                 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1089a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
10903deb3ec6SMatthias Ringwald                     break;
10913deb3ec6SMatthias Ringwald 
10923deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1093a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
10943deb3ec6SMatthias Ringwald                     break;
10953deb3ec6SMatthias Ringwald 
10963deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1097a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
10983deb3ec6SMatthias Ringwald                     break;
1099ce263fc8SMatthias Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1100a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1101a0ffb263SMatthias Ringwald                         hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1102ca59be51SMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1103ce263fc8SMatthias Ringwald                         break;
1104ce263fc8SMatthias Ringwald                     }
11053deb3ec6SMatthias Ringwald 
1106a0ffb263SMatthias Ringwald                     if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1107a0ffb263SMatthias Ringwald                         hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1108ca59be51SMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1109ce263fc8SMatthias Ringwald                         break;
11103deb3ec6SMatthias Ringwald                     }
11113deb3ec6SMatthias Ringwald 
1112a0ffb263SMatthias Ringwald                     switch (hfp_connection->hf_query_operator_state){
1113ce263fc8SMatthias Ringwald                         case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1114a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1115ce263fc8SMatthias Ringwald                             break;
1116ce263fc8SMatthias Ringwald                         case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1117a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1118a473a009SMatthias Ringwald                             hfp_emit_network_operator_event(hfp_connection);
1119ce263fc8SMatthias Ringwald                             break;
1120ce263fc8SMatthias Ringwald                         default:
1121ce263fc8SMatthias Ringwald                             break;
11223deb3ec6SMatthias Ringwald                     }
1123ce263fc8SMatthias Ringwald 
1124a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_extended_audio_gateway_error_report){
1125a0ffb263SMatthias Ringwald                         hfp_connection->enable_extended_audio_gateway_error_report = 0;
1126ce263fc8SMatthias Ringwald                         break;
11273deb3ec6SMatthias Ringwald                     }
11283deb3ec6SMatthias Ringwald 
1129a0ffb263SMatthias Ringwald                     switch (hfp_connection->codecs_state){
1130aa4dd815SMatthias Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1131a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
11323deb3ec6SMatthias Ringwald                             break;
1133ce263fc8SMatthias Ringwald                         case HFP_CODECS_HF_CONFIRMED_CODEC:
1134a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1135ce263fc8SMatthias Ringwald                             break;
11363deb3ec6SMatthias Ringwald                         default:
11373deb3ec6SMatthias Ringwald                             break;
11383deb3ec6SMatthias Ringwald                     }
1139af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
1140be55a11dSMilanka Ringwald                     break;
1141be55a11dSMilanka Ringwald                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
1142af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
11433deb3ec6SMatthias Ringwald                     break;
11443deb3ec6SMatthias Ringwald                 default:
11453deb3ec6SMatthias Ringwald                     break;
11463deb3ec6SMatthias Ringwald             }
1147*e2c3b58dSMilanka Ringwald             break;
1148*e2c3b58dSMilanka Ringwald     }
11493deb3ec6SMatthias Ringwald 
11503deb3ec6SMatthias Ringwald     // done
1151*e2c3b58dSMilanka Ringwald     hfp_connection->hf_ok_pending_for_command = HFP_CMD_NONE;
1152be55a11dSMilanka Ringwald     hfp_connection->ok_pending = 0;
1153a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
1154*e2c3b58dSMilanka Ringwald     return event_emited;
11553deb3ec6SMatthias Ringwald }
11563deb3ec6SMatthias Ringwald 
1157be55a11dSMilanka Ringwald 
1158b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
11594562e2a2SMatthias Ringwald     uint16_t i;
11604562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
11614562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
11624562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
11634562e2a2SMatthias Ringwald                 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
11644562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
11654562e2a2SMatthias Ringwald                 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
11664562e2a2SMatthias Ringwald                 // avoid set but not used warning
11674562e2a2SMatthias Ringwald                 (void) hfp_callheld_status;
11684562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
11694562e2a2SMatthias Ringwald                 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
11704562e2a2SMatthias Ringwald             }
11714562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1172a473a009SMatthias Ringwald             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
11734562e2a2SMatthias Ringwald             break;
11744562e2a2SMatthias Ringwald         }
11754562e2a2SMatthias Ringwald     }
11764562e2a2SMatthias Ringwald }
11774562e2a2SMatthias Ringwald 
1178426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1179186dd3d2SMatthias Ringwald     int value;
1180186dd3d2SMatthias Ringwald     int i;
1181*e2c3b58dSMilanka Ringwald     bool event_emited;
1182*e2c3b58dSMilanka Ringwald 
1183a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1184667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1185a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1186a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1187667ec068SMatthias Ringwald             break;
1188667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1189a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1190ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1191667ec068SMatthias Ringwald             break;
1192667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1193a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1194a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1195667ec068SMatthias Ringwald             break;
1196ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1197a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11982308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1199667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1200ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1201ce263fc8SMatthias Ringwald             break;
1202ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1203a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12042308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1205667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1206ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1207ce263fc8SMatthias Ringwald             break;
1208ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1209a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1210ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1211a0ffb263SMatthias Ringwald             break;
1212a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1213a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1214a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1215a0ffb263SMatthias Ringwald             break;
1216a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1217a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1218a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1219ce263fc8SMatthias Ringwald             break;
1220ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1221a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12225a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1223a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1224ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1225ce263fc8SMatthias Ringwald             break;
12260b4debbfSMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
12270b4debbfSMilanka Ringwald             break;
1228fdda66c0SMilanka Ringwald         case HFP_CMD_ERROR:
122990244c92SMilanka Ringwald             switch (hfp_connection->state){
123090244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
123190244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
123290244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1233fdda66c0SMilanka Ringwald                             hfp_reset_context_flags(hfp_connection);
123490244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
123590244c92SMilanka Ringwald                             return;
123690244c92SMilanka Ringwald                         default:
123790244c92SMilanka Ringwald                             break;
123890244c92SMilanka Ringwald                     }
123956f1adacSMilanka Ringwald                     break;
124056f1adacSMilanka Ringwald                 default:
124156f1adacSMilanka Ringwald                     break;
124256f1adacSMilanka Ringwald             }
1243*e2c3b58dSMilanka Ringwald 
1244fdda66c0SMilanka Ringwald             // handle error response for voice activation (HF initiated)
12450b4debbfSMilanka Ringwald             switch(hfp_connection->vra_state_requested){
1246de9e0ea7SMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1247de9e0ea7SMilanka Ringwald                     hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1248be55a11dSMilanka Ringwald                     break;
1249be55a11dSMilanka Ringwald                 default:
1250*e2c3b58dSMilanka Ringwald                     if (hfp_connection->vra_state_requested == hfp_connection->vra_state){
1251*e2c3b58dSMilanka Ringwald                         break;
1252*e2c3b58dSMilanka Ringwald                     }
12530b4debbfSMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
12541a26de69SMilanka Ringwald                     hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
12550b4debbfSMilanka Ringwald                     hfp_reset_context_flags(hfp_connection);
12560b4debbfSMilanka Ringwald                     return;
1257be55a11dSMilanka Ringwald             }
1258*e2c3b58dSMilanka Ringwald             event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1259*e2c3b58dSMilanka Ringwald             if (!event_emited){
12600b4debbfSMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1261*e2c3b58dSMilanka Ringwald             }
1262fdda66c0SMilanka Ringwald             hfp_reset_context_flags(hfp_connection);
1263ce263fc8SMatthias Ringwald             break;
1264fdda66c0SMilanka Ringwald 
1265ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1266*e2c3b58dSMilanka Ringwald             hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS);
1267ce263fc8SMatthias Ringwald             break;
1268ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
12695a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1270ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1271ce263fc8SMatthias Ringwald             break;
1272ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
12735a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12744562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1275ce263fc8SMatthias Ringwald             break;
1276c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
12775a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1278c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1279a473a009SMatthias Ringwald                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1280c741b032SMilanka Ringwald             }
1281c741b032SMilanka Ringwald             break;
12821cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
12831cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12845a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
12851cc65c4fSMatthias Ringwald 			break;
1286eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1287eac56539SMilanka 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));
1288ce263fc8SMatthias Ringwald         default:
1289ce263fc8SMatthias Ringwald             break;
12903deb3ec6SMatthias Ringwald     }
12910cef86faSMatthias Ringwald }
1292426f9988SMatthias Ringwald 
129376cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
129476cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
129576cc1527SMatthias Ringwald }
129676cc1527SMatthias Ringwald 
12970b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1298426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1299426f9988SMatthias Ringwald     if (size < 1) return;
1300426f9988SMatthias Ringwald 
1301426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1302e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1303e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1304e43d1938SMatthias Ringwald #endif
1305426f9988SMatthias Ringwald 
1306426f9988SMatthias Ringwald     // process messages byte-wise
1307426f9988SMatthias Ringwald     int pos;
1308426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1309426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
13101599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1311426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1312426f9988SMatthias Ringwald     }
13130b4debbfSMilanka Ringwald     hfp_hf_handle_rfcomm_command(hfp_connection);
13143deb3ec6SMatthias Ringwald }
13153deb3ec6SMatthias Ringwald 
13161c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1317665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1318665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1319665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1320a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
132122387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
13221c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
13233deb3ec6SMatthias Ringwald     }
13243deb3ec6SMatthias Ringwald }
13253deb3ec6SMatthias Ringwald 
13261c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
13270b4debbfSMilanka Ringwald     hfp_connection_t * hfp_connection;
13283deb3ec6SMatthias Ringwald     switch (packet_type){
13293deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
13300b4debbfSMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
13310b4debbfSMilanka Ringwald             if (!hfp_connection) return;
13320b4debbfSMilanka Ringwald             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
13330b4debbfSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
13340b4debbfSMilanka Ringwald             return;
13353deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1336d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1337d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
13381c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1339d4dd47ffSMatthias Ringwald                 return;
1340d4dd47ffSMatthias Ringwald             }
134127950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1342202c8a4cSMatthias Ringwald             break;
13433deb3ec6SMatthias Ringwald         default:
13443deb3ec6SMatthias Ringwald             break;
13453deb3ec6SMatthias Ringwald     }
13461c6a0fc0SMatthias Ringwald     hfp_hf_run();
13473deb3ec6SMatthias Ringwald }
13483deb3ec6SMatthias Ringwald 
13491c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1350405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
13511c6a0fc0SMatthias Ringwald     hfp_hf_run();
1352405014fbSMatthias Ringwald }
1353405014fbSMatthias Ringwald 
1354b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1355b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1356b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1357b4df8028SMilanka Ringwald         return status;
1358b4df8028SMilanka Ringwald     }
1359b4df8028SMilanka Ringwald 
1360520c92d5SMatthias Ringwald     hfp_init();
136120b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
136220b2edb6SMatthias Ringwald     hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
136320b2edb6SMatthias Ringwald     hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
136420b2edb6SMatthias Ringwald     hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
136520b2edb6SMatthias Ringwald     hfp_codecs_nr = 0;
136620b2edb6SMatthias Ringwald     hfp_hf_speaker_gain = 9;
136720b2edb6SMatthias Ringwald     hfp_hf_microphone_gain = 9;
136820b2edb6SMatthias Ringwald     hfp_indicators_nr = 0;
136920b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1370d63c37a1SMatthias Ringwald 
13711c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
13721c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
137327950165SMatthias Ringwald 
137427950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
13751c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1376b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
137720b2edb6SMatthias Ringwald }
137827950165SMatthias Ringwald 
137920b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
138020b2edb6SMatthias Ringwald     hfp_deinit();
138120b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
138220b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t));
138320b2edb6SMatthias Ringwald     (void) memset(phone_number, 0, sizeof(phone_number));
1384a0ffb263SMatthias Ringwald }
1385a0ffb263SMatthias Ringwald 
13867ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
138768466199SMilanka Ringwald     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
13883deb3ec6SMatthias Ringwald 
13893deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
13903deb3ec6SMatthias Ringwald     int i;
13913deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
13923deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
13933deb3ec6SMatthias Ringwald     }
13943deb3ec6SMatthias Ringwald }
13953deb3ec6SMatthias Ringwald 
1396a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
13973deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1398a0ffb263SMatthias Ringwald }
13993deb3ec6SMatthias Ringwald 
14007ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
140168466199SMilanka Ringwald     btstack_assert(hfp_indicators_nr < HFP_MAX_NUM_INDICATORS);
140268466199SMilanka Ringwald 
14033deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
14043deb3ec6SMatthias Ringwald     int i;
1405a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
14063deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
14073deb3ec6SMatthias Ringwald     }
14083deb3ec6SMatthias Ringwald }
14093deb3ec6SMatthias Ringwald 
14104eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
14114eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
14123deb3ec6SMatthias Ringwald }
14133deb3ec6SMatthias Ringwald 
1414657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
14159c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1416a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1417657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1418a33eb0c4SMilanka Ringwald     }
14191ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
14201c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1421657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
14223deb3ec6SMatthias Ringwald }
14233deb3ec6SMatthias Ringwald 
14243c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
14259c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1426a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14273c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14283deb3ec6SMatthias Ringwald     }
1429a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
14301c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14313c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14323deb3ec6SMatthias Ringwald }
14333deb3ec6SMatthias Ringwald 
14343c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
14353c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1436ce263fc8SMatthias Ringwald }
1437ce263fc8SMatthias Ringwald 
14383c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
14393c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1440ce263fc8SMatthias Ringwald }
1441ce263fc8SMatthias Ringwald 
14423deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
14433c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
14449c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1445a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14463c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14473deb3ec6SMatthias Ringwald     }
1448a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1449a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
14501c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14513c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14523deb3ec6SMatthias Ringwald }
14533deb3ec6SMatthias Ringwald 
14543c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
14559c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1456a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14573c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14583deb3ec6SMatthias Ringwald     }
14593c65e705SMilanka Ringwald 
1460a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1461ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1462a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1463ce263fc8SMatthias Ringwald             break;
1464ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1465a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1466ce263fc8SMatthias Ringwald             break;
1467ce263fc8SMatthias Ringwald         default:
14683c65e705SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1469ce263fc8SMatthias Ringwald     }
14701c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14713c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14723deb3ec6SMatthias Ringwald }
14733deb3ec6SMatthias Ringwald 
14743c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
14759c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1476a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14773c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14783deb3ec6SMatthias Ringwald     }
1479a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
14801c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14813c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14823deb3ec6SMatthias Ringwald }
14833deb3ec6SMatthias Ringwald 
1484ce263fc8SMatthias Ringwald 
14853c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
14863c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1487ce263fc8SMatthias Ringwald }
1488ce263fc8SMatthias Ringwald 
14893c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
14903c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1491ce263fc8SMatthias Ringwald }
1492ce263fc8SMatthias Ringwald 
149338200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
149438200c1dSMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
149538200c1dSMilanka Ringwald }
1496ce263fc8SMatthias Ringwald 
14973c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
14989c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1499a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15003c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1501a33eb0c4SMilanka Ringwald     }
1502ce263fc8SMatthias Ringwald 
15033c65e705SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
15043c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
15053c65e705SMilanka Ringwald     }
15063c65e705SMilanka Ringwald 
15073c65e705SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
15083c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
15093c65e705SMilanka Ringwald     }
1510f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1511a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1512aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1513aa4dd815SMatthias Ringwald                 break;
1514ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1515ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1516ec3bfc1aSMatthias Ringwald                 break;
1517aa4dd815SMatthias Ringwald             default:
15181cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
15191cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
15201cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
15211cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
152238200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1523aa4dd815SMatthias Ringwald                 break;
15243deb3ec6SMatthias Ringwald         }
1525f4412093SMatthias Ringwald     } else {
1526f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1527f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1528f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1529f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1530f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1531f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1532f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1533f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1534ce263fc8SMatthias Ringwald     }
1535ce263fc8SMatthias Ringwald 
15361c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15373c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15383deb3ec6SMatthias Ringwald }
15393deb3ec6SMatthias Ringwald 
15403c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
15419c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1542a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15433c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1544a33eb0c4SMilanka Ringwald     }
15450b4debbfSMilanka Ringwald     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
15460b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
15470b4debbfSMilanka Ringwald     }
15480b4debbfSMilanka Ringwald     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
15490b4debbfSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
15501c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
15510b4debbfSMilanka Ringwald     }
15523c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15533deb3ec6SMatthias Ringwald }
15543deb3ec6SMatthias Ringwald 
15553c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
15569c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1557a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15583c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1559a33eb0c4SMilanka Ringwald     }
1560ce263fc8SMatthias Ringwald 
1561ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1562a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
15631c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1564ce263fc8SMatthias Ringwald     } else {
1565ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
15663c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1567ce263fc8SMatthias Ringwald     }
15683c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1569ce263fc8SMatthias Ringwald }
1570ce263fc8SMatthias Ringwald 
15713c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(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     }
1576a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
15771c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15783c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1579ce263fc8SMatthias Ringwald }
1580ce263fc8SMatthias Ringwald 
15813c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
15829c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1583a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15843c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1585a33eb0c4SMilanka Ringwald     }
1586ce263fc8SMatthias Ringwald 
1587ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1588a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
15891c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1590ce263fc8SMatthias Ringwald     }
15913c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1592ce263fc8SMatthias Ringwald }
1593ce263fc8SMatthias Ringwald 
15943c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){
15959c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1596a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15973c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1598a33eb0c4SMilanka Ringwald     }
1599ce263fc8SMatthias Ringwald 
1600ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1601a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
16021c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1603ce263fc8SMatthias Ringwald     }
16043c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1605ce263fc8SMatthias Ringwald }
1606ce263fc8SMatthias Ringwald 
16073c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
16089c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1609a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16103c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1611a33eb0c4SMilanka Ringwald     }
1612ce263fc8SMatthias Ringwald 
1613505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1614505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1615a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
16161c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1617ce263fc8SMatthias Ringwald     }
16183c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1619ce263fc8SMatthias Ringwald }
1620ce263fc8SMatthias Ringwald 
16213c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
16229c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1623a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16243c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1625a33eb0c4SMilanka Ringwald     }
1626ce263fc8SMatthias Ringwald 
1627505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1628505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1629a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
16301c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1631ce263fc8SMatthias Ringwald     }
16323c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1633ce263fc8SMatthias Ringwald }
1634ce263fc8SMatthias Ringwald 
16353c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
16369c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1637a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16383c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1639a33eb0c4SMilanka Ringwald     }
1640ce263fc8SMatthias Ringwald 
1641505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1642505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1643a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
16441c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1645ce263fc8SMatthias Ringwald     }
16463c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1647ce263fc8SMatthias Ringwald }
1648ce263fc8SMatthias Ringwald 
16493c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
16509c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1651a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16523c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1653a33eb0c4SMilanka Ringwald     }
1654ce263fc8SMatthias Ringwald 
1655505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1656505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1657a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
16581c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1659ce263fc8SMatthias Ringwald     }
16603c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1661ce263fc8SMatthias Ringwald }
1662ce263fc8SMatthias Ringwald 
16633c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
16649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1665a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16663c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1667a33eb0c4SMilanka Ringwald     }
1668667ec068SMatthias Ringwald 
1669505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1670505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1671a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1672a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
16731c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1674667ec068SMatthias Ringwald     }
16753c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1676667ec068SMatthias Ringwald }
1677667ec068SMatthias Ringwald 
16783c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
16799c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1680a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16813c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1682a33eb0c4SMilanka Ringwald     }
1683667ec068SMatthias Ringwald 
1684505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1685505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1686a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1687a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
16881c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1689667ec068SMatthias Ringwald     }
16903c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1691667ec068SMatthias Ringwald }
1692ce263fc8SMatthias Ringwald 
16933c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
16949c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1695a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16963c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1697a33eb0c4SMilanka Ringwald     }
1698ce263fc8SMatthias Ringwald 
1699a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1700ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
17011c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17023c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1703ce263fc8SMatthias Ringwald }
1704ce263fc8SMatthias Ringwald 
17053c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
17069c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1707a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17083c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1709a33eb0c4SMilanka Ringwald     }
1710ce263fc8SMatthias Ringwald 
1711a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1712a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1713a0ffb263SMatthias Ringwald 
17141c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17153c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1716ce263fc8SMatthias Ringwald }
1717ce263fc8SMatthias Ringwald 
17183c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
17199c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1720a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17213c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1722a33eb0c4SMilanka Ringwald     }
1723ce263fc8SMatthias Ringwald 
1724a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
17251c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17263c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1727ce263fc8SMatthias Ringwald }
1728ce263fc8SMatthias Ringwald 
17293c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
17309c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1731a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17323c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1733a33eb0c4SMilanka Ringwald     }
1734ce263fc8SMatthias Ringwald 
1735a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
17361c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17373c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1738ce263fc8SMatthias Ringwald }
1739ce263fc8SMatthias Ringwald 
1740ce263fc8SMatthias Ringwald 
17413c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
17429c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1743a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17443c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1745a33eb0c4SMilanka Ringwald     }
1746ce263fc8SMatthias Ringwald 
1747a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
17481c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17493c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1750ce263fc8SMatthias Ringwald }
1751ce263fc8SMatthias Ringwald 
1752ce263fc8SMatthias Ringwald 
17533c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
17549c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1755a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17563c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1757a33eb0c4SMilanka Ringwald     }
1758ce263fc8SMatthias Ringwald 
1759a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
17601c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17613c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1762ce263fc8SMatthias Ringwald }
1763ce263fc8SMatthias Ringwald 
17643c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
17659c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1766a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17673c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1768a33eb0c4SMilanka Ringwald     }
1769ce263fc8SMatthias Ringwald 
1770a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
17711c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17723c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1773ce263fc8SMatthias Ringwald }
1774ce263fc8SMatthias Ringwald 
17756ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){
17766ba83b5eSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION);
17776ba83b5eSMilanka Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_EC_NR_FUNCTION);
17786ba83b5eSMilanka Ringwald     return hf && ag;
1779ce263fc8SMatthias Ringwald }
1780ce263fc8SMatthias Ringwald 
17813c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
17829c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1783a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17843c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1785a33eb0c4SMilanka Ringwald     }
17866ba83b5eSMilanka Ringwald     if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){
17876ba83b5eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
17886ba83b5eSMilanka Ringwald     }
1789ce263fc8SMatthias Ringwald 
1790a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
17911c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17923c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1793ce263fc8SMatthias Ringwald }
1794ce263fc8SMatthias Ringwald 
1795013cc750SMilanka Ringwald static bool hfp_hf_voice_recognition_supported(hfp_connection_t * hfp_connection, bool enhanced){
1796013cc750SMilanka Ringwald     uint8_t hf_vra_flag = HFP_HFSF_VOICE_RECOGNITION_FUNCTION;
1797013cc750SMilanka Ringwald     uint8_t ag_vra_flag = HFP_AGSF_VOICE_RECOGNITION_FUNCTION;
1798013cc750SMilanka Ringwald 
1799013cc750SMilanka Ringwald     if (enhanced){
1800013cc750SMilanka Ringwald         hf_vra_flag = HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS;
1801013cc750SMilanka Ringwald         ag_vra_flag = HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS;
1802013cc750SMilanka Ringwald     }
1803013cc750SMilanka Ringwald 
1804013cc750SMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, ag_vra_flag);
1805013cc750SMilanka Ringwald     int hf = get_bit(hfp_supported_features, hf_vra_flag);
1806be55a11dSMilanka Ringwald     return hf && ag;
1807be55a11dSMilanka Ringwald }
1808be55a11dSMilanka Ringwald 
1809013cc750SMilanka Ringwald static uint8_t activate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){
1810fdda66c0SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1811fdda66c0SMilanka Ringwald     if (!hfp_connection) {
1812fdda66c0SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1813be55a11dSMilanka Ringwald     }
1814013cc750SMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1815013cc750SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1816013cc750SMilanka Ringwald     }
1817013cc750SMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection, enhanced)){
1818af97579eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1819af97579eSMilanka Ringwald     }
1820af97579eSMilanka Ringwald 
1821498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1822be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1823de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1824498a8121SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1825fd4151d1SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
18266b8275b0SMilanka Ringwald             hfp_connection->enhanced_voice_recognition_enabled = enhanced;
1827be55a11dSMilanka Ringwald             break;
1828de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1829de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = true;
1830de9e0ea7SMilanka Ringwald             break;
1831be55a11dSMilanka Ringwald         default:
1832be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1833be55a11dSMilanka Ringwald     }
1834ce263fc8SMatthias Ringwald 
1835af97579eSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1836fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1837af97579eSMilanka Ringwald }
1838af97579eSMilanka Ringwald 
1839013cc750SMilanka Ringwald 
1840013cc750SMilanka Ringwald static uint8_t deactivate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){
1841af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1842af97579eSMilanka Ringwald     if (!hfp_connection) {
1843af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1844af97579eSMilanka Ringwald     }
1845c95b5b3cSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
184608a0b01cSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
184708a0b01cSMilanka Ringwald     }
1848013cc750SMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection, enhanced)){
1849fdda66c0SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1850af97579eSMilanka Ringwald     }
1851013cc750SMilanka Ringwald 
1852fdda66c0SMilanka Ringwald     switch (hfp_connection->vra_state){
1853de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
1854fdda66c0SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1855de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1856de9e0ea7SMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1857fdda66c0SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1858fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1859fdda66c0SMilanka Ringwald             break;
1860de9e0ea7SMilanka Ringwald 
1861de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1862de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1863de9e0ea7SMilanka Ringwald             hfp_connection->deactivate_voice_recognition = true;
1864de9e0ea7SMilanka Ringwald             break;
1865de9e0ea7SMilanka Ringwald 
1866de9e0ea7SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1867de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1868de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1869fdda66c0SMilanka Ringwald         default:
1870fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1871fdda66c0SMilanka Ringwald     }
1872fdda66c0SMilanka Ringwald 
1873fdda66c0SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1874fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1875af97579eSMilanka Ringwald }
1876af97579eSMilanka Ringwald 
1877013cc750SMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1878013cc750SMilanka Ringwald     return activate_voice_recognition(acl_handle, false);
1879013cc750SMilanka Ringwald }
1880013cc750SMilanka Ringwald 
1881e83c2025SMilanka Ringwald uint8_t hfp_hf_activate_enhanced_voice_recognition(hci_con_handle_t acl_handle){
1882013cc750SMilanka Ringwald     return activate_voice_recognition(acl_handle, true);
1883be55a11dSMilanka Ringwald }
1884fdda66c0SMilanka Ringwald 
1885de9e0ea7SMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
1886de9e0ea7SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1887de9e0ea7SMilanka Ringwald     if (!hfp_connection) {
1888de9e0ea7SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1889de9e0ea7SMilanka Ringwald     }
1890de9e0ea7SMilanka Ringwald     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
1891de9e0ea7SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1892de9e0ea7SMilanka Ringwald     }
1893de9e0ea7SMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection, true)){
1894de9e0ea7SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1895de9e0ea7SMilanka Ringwald     }
1896de9e0ea7SMilanka Ringwald 
1897de9e0ea7SMilanka Ringwald     switch (hfp_connection->vra_state){
1898de9e0ea7SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1899de9e0ea7SMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1900de9e0ea7SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1901de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
1902de9e0ea7SMilanka Ringwald             break;
1903de9e0ea7SMilanka Ringwald         default:
1904de9e0ea7SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1905de9e0ea7SMilanka Ringwald     }
1906de9e0ea7SMilanka Ringwald 
1907de9e0ea7SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1908de9e0ea7SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1909de9e0ea7SMilanka Ringwald }
1910de9e0ea7SMilanka Ringwald 
1911fdda66c0SMilanka Ringwald 
1912013cc750SMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1913013cc750SMilanka Ringwald     return deactivate_voice_recognition(acl_handle, false);
1914be55a11dSMilanka Ringwald }
1915be55a11dSMilanka Ringwald 
1916e83c2025SMilanka Ringwald uint8_t hfp_hf_deactivate_enhanced_voice_recognition(hci_con_handle_t acl_handle){
1917013cc750SMilanka Ringwald     return deactivate_voice_recognition(acl_handle, true);
1918ce263fc8SMatthias Ringwald }
1919ce263fc8SMatthias Ringwald 
1920de9e0ea7SMilanka Ringwald 
19213c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
19229c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1923a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19243c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1925a33eb0c4SMilanka Ringwald     }
1926c8626498SMilanka Ringwald 
19273c65e705SMilanka Ringwald     if (hfp_connection->microphone_gain == gain) {
19283c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
19293c65e705SMilanka Ringwald     }
19303c65e705SMilanka Ringwald 
1931c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1932a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
19333c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1934a0ffb263SMatthias Ringwald     }
19353c65e705SMilanka Ringwald 
1936a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1937a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
19381c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19393c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1940ce263fc8SMatthias Ringwald }
1941ce263fc8SMatthias Ringwald 
19423c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
19439c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1944a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19453c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1946a33eb0c4SMilanka Ringwald     }
1947c8626498SMilanka Ringwald 
19483c65e705SMilanka Ringwald     if (hfp_connection->speaker_gain == gain){
19493c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
19503c65e705SMilanka Ringwald     }
19513c65e705SMilanka Ringwald 
1952c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1953a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
19543c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1955a0ffb263SMatthias Ringwald     }
19563c65e705SMilanka Ringwald 
1957a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1958a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
19591c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19603c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1961ce263fc8SMatthias Ringwald }
1962ce263fc8SMatthias Ringwald 
19633c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
19649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1965a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19663c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1967a33eb0c4SMilanka Ringwald     }
1968a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
19691c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19703c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1971ce263fc8SMatthias Ringwald }
1972ce263fc8SMatthias Ringwald 
19733c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
19749c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1975a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19763c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1977a33eb0c4SMilanka Ringwald     }
1978a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
19791c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19803c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1981ce263fc8SMatthias Ringwald }
19823deb3ec6SMatthias Ringwald 
19833c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
19849c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1985a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19863c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1987a33eb0c4SMilanka Ringwald     }
1988a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
19891c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19903c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1991667ec068SMatthias Ringwald }
1992667ec068SMatthias Ringwald 
1993667ec068SMatthias Ringwald 
19943c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
19959c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1996a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19973c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1998a33eb0c4SMilanka Ringwald     }
1999a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2000a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
20011c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20023c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2003667ec068SMatthias Ringwald }
2004667ec068SMatthias Ringwald 
20053c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
20069c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2007a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20083c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2009a33eb0c4SMilanka Ringwald     }
2010a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2011a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
20121c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20133c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2014667ec068SMatthias Ringwald }
2015667ec068SMatthias Ringwald 
20163c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
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_rrh = 1;
2022a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
20231c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20243c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2025667ec068SMatthias Ringwald }
2026667ec068SMatthias Ringwald 
20273c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
20289c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2029a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20303c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2031a33eb0c4SMilanka Ringwald     }
2032a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2033a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
20341c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20353c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2036667ec068SMatthias Ringwald }
2037667ec068SMatthias Ringwald 
20383c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
20399c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2040a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20413c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2042a33eb0c4SMilanka Ringwald     }
2043a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
20441c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20453c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2046667ec068SMatthias Ringwald }
2047667ec068SMatthias Ringwald 
20483c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
20499c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2050a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20513c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2052a33eb0c4SMilanka Ringwald     }
2053667ec068SMatthias Ringwald     // find index for assigned number
2054667ec068SMatthias Ringwald     int i;
2055667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
2056667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
2057667ec068SMatthias Ringwald             // set value
2058667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
2059667ec068SMatthias Ringwald             // mark for update
2060a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
2061a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
2062667ec068SMatthias Ringwald                 // send update
20631c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
2064a0ffb263SMatthias Ringwald             }
20653c65e705SMilanka Ringwald             return ERROR_CODE_SUCCESS;
2066667ec068SMatthias Ringwald         }
2067667ec068SMatthias Ringwald     }
20683c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2069667ec068SMatthias Ringwald }
2070667ec068SMatthias Ringwald 
2071d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
2072d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2073d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
2074d7f6b5cbSMatthias Ringwald         return 0;
2075d7f6b5cbSMatthias Ringwald     }
2076d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
2077d7f6b5cbSMatthias Ringwald }
207876cc1527SMatthias Ringwald 
207976cc1527SMatthias 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){
208076cc1527SMatthias Ringwald 	if (!name){
208176cc1527SMatthias Ringwald 		name = default_hfp_hf_service_name;
208276cc1527SMatthias Ringwald 	}
208376cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
208476cc1527SMatthias Ringwald 
208576cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
208676cc1527SMatthias Ringwald 	//
208776cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
208876cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
208976cc1527SMatthias Ringwald 	//
209076cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
209176cc1527SMatthias Ringwald 	//
209276cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
2093ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
209476cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
209576cc1527SMatthias Ringwald 	}
2096ef3ae4ebSMilanka Ringwald 
2097ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
209856f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
2099ef3ae4ebSMilanka Ringwald     }
2100ef3ae4ebSMilanka Ringwald 
2101ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
210256f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
2103ef3ae4ebSMilanka Ringwald     }
2104ef3ae4ebSMilanka Ringwald 
210576cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
210676cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
210776cc1527SMatthias Ringwald }
210876cc1527SMatthias Ringwald 
210976cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
211068466199SMilanka Ringwald 	btstack_assert(callback != NULL);
211168466199SMilanka Ringwald 
211276cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
211376cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
211476cc1527SMatthias Ringwald }
2115