xref: /btstack/src/classic/hfp_hf.c (revision 6ba83b5e65b7df1008f59d6cd59fdde205cd3987)
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_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
37289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
37389425bfcSMilanka Ringwald }
37489425bfcSMilanka Ringwald 
37589425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
37689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
37789425bfcSMilanka Ringwald }
37889425bfcSMilanka Ringwald 
37989425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
38089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
38189425bfcSMilanka Ringwald }
38289425bfcSMilanka Ringwald 
38389425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
38489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
38589425bfcSMilanka Ringwald }
38689425bfcSMilanka Ringwald 
38789425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
38889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
38989425bfcSMilanka Ringwald }
39089425bfcSMilanka Ringwald 
39189425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
39289425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
39389425bfcSMilanka Ringwald }
39489425bfcSMilanka Ringwald 
39589425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
39689425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
39789425bfcSMilanka Ringwald }
39889425bfcSMilanka Ringwald 
399ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
40089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
401ce263fc8SMatthias Ringwald }
402ce263fc8SMatthias Ringwald 
403667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
40489425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
405667ec068SMatthias Ringwald }
406667ec068SMatthias Ringwald 
40776cc1527SMatthias Ringwald /* state machines */
4083deb3ec6SMatthias Ringwald 
409a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
410a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
411a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
412aa4dd815SMatthias Ringwald     int done = 1;
413498a8121SMilanka Ringwald     log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state);
414a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4153deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
416d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
417a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
418a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4193deb3ec6SMatthias Ringwald             break;
4203deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
421a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
422a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4233deb3ec6SMatthias Ringwald             break;
4243deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
425a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
426a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4273deb3ec6SMatthias Ringwald             break;
4283deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
429a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
430a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4313deb3ec6SMatthias Ringwald             break;
4323deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
433a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
434a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4353deb3ec6SMatthias Ringwald             break;
4363deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
437a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
438a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4393deb3ec6SMatthias Ringwald             break;
4403deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
441a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
442a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4433deb3ec6SMatthias Ringwald             break;
4443deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
445a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
446a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4473deb3ec6SMatthias Ringwald             break;
4483deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
449a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
450a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4513deb3ec6SMatthias Ringwald             break;
4523deb3ec6SMatthias Ringwald         default:
453aa4dd815SMatthias Ringwald             done = 0;
4543deb3ec6SMatthias Ringwald             break;
4553deb3ec6SMatthias Ringwald     }
4563deb3ec6SMatthias Ringwald     return done;
4573deb3ec6SMatthias Ringwald }
4583deb3ec6SMatthias Ringwald 
459ce263fc8SMatthias Ringwald 
460a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
461a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
462498a8121SMilanka Ringwald     if (hfp_connection->ok_pending){
463498a8121SMilanka Ringwald         return 0;
464498a8121SMilanka Ringwald     }
465ce263fc8SMatthias Ringwald     int done = 0;
466a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
467a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
468ce263fc8SMatthias Ringwald         done = 1;
469a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
470ce263fc8SMatthias Ringwald         return done;
471ce263fc8SMatthias Ringwald     };
472a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
473a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
474ce263fc8SMatthias Ringwald         done = 1;
475a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
476a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
477a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
478ce263fc8SMatthias Ringwald         return done;
479ce263fc8SMatthias Ringwald     }
480ce263fc8SMatthias Ringwald 
481a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
482ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
483a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
484a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
485a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
486ce263fc8SMatthias Ringwald             return 1;
487ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
488a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
489a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
490a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
491ce263fc8SMatthias Ringwald             return 1;
492ce263fc8SMatthias Ringwald         default:
493ce263fc8SMatthias Ringwald             break;
494ce263fc8SMatthias Ringwald     }
495ce263fc8SMatthias Ringwald 
496a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
497a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
498ce263fc8SMatthias Ringwald         done = 1;
499a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
500ce263fc8SMatthias Ringwald         return done;
501ce263fc8SMatthias Ringwald     }
502ce263fc8SMatthias Ringwald 
503ce263fc8SMatthias Ringwald     return done;
504ce263fc8SMatthias Ringwald }
505ce263fc8SMatthias Ringwald 
506af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
507be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
508be55a11dSMilanka Ringwald         return 0;
509be55a11dSMilanka Ringwald     }
510be55a11dSMilanka Ringwald     int done = 0;
511fd4151d1SMilanka Ringwald 
5120b4debbfSMilanka Ringwald     if (hfp_connection->ok_pending == 1){
5130b4debbfSMilanka Ringwald         return 0;
5140b4debbfSMilanka Ringwald     }
5150b4debbfSMilanka Ringwald     // voice recognition activated from AG
5160b4debbfSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){
5170b4debbfSMilanka Ringwald         switch(hfp_connection->vra_state_requested){
5180b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
5190b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
520de9e0ea7SMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
5210b4debbfSMilanka Ringwald                 // ignore AG command, continue to wait for OK
5220b4debbfSMilanka Ringwald                 return 0;
523cf75be85SMilanka Ringwald 
5240b4debbfSMilanka Ringwald             default:
525b95cac54SMilanka Ringwald                 if (hfp_connection->ag_vra_msg_length > 0){
526b95cac54SMilanka Ringwald                     hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection);
527b95cac54SMilanka Ringwald                     hfp_connection->ag_vra_msg_length = 0;
528b95cac54SMilanka Ringwald                     break;
529b95cac54SMilanka Ringwald                 }
530cf75be85SMilanka Ringwald                 switch(hfp_connection->ag_vra_state){
531cf75be85SMilanka Ringwald                     case HFP_VOICE_RECOGNITION_STATE_AG_READY:
532013cc750SMilanka Ringwald                         switch (hfp_connection->ag_vra_status){
533013cc750SMilanka Ringwald                             case 0:
5340b4debbfSMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
535013cc750SMilanka Ringwald                                 break;
536013cc750SMilanka Ringwald                             case 1:
537013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
538013cc750SMilanka Ringwald                                 break;
539013cc750SMilanka Ringwald                             case 2:
540013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
541013cc750SMilanka Ringwald                                 break;
542013cc750SMilanka Ringwald                             default:
543013cc750SMilanka Ringwald                                 break;
5440b4debbfSMilanka Ringwald                         }
5450b4debbfSMilanka Ringwald                         break;
546cf75be85SMilanka Ringwald                     default:
547cf75be85SMilanka Ringwald                         // state messages from AG
548cf75be85SMilanka Ringwald                         hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
549cf75be85SMilanka Ringwald                         break;
550cf75be85SMilanka Ringwald                 }
551cf75be85SMilanka Ringwald                 break;
5520b4debbfSMilanka Ringwald         }
5530b4debbfSMilanka Ringwald         hfp_connection->command = HFP_CMD_NONE;
5540b4debbfSMilanka Ringwald     }
5550b4debbfSMilanka Ringwald 
5560b4debbfSMilanka Ringwald 
557498a8121SMilanka Ringwald     switch (hfp_connection->vra_state_requested){
558fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
559fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
560fdda66c0SMilanka Ringwald             if (done != 0){
561fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
562498a8121SMilanka Ringwald                 hfp_connection->ok_pending = 1;
563498a8121SMilanka Ringwald             }
564fd4151d1SMilanka Ringwald             return 1;
565fd4151d1SMilanka Ringwald 
566fd4151d1SMilanka Ringwald 
567fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
568fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
569fdda66c0SMilanka Ringwald             if (done != 0){
570fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
571fd4151d1SMilanka Ringwald                 hfp_connection->ok_pending = 1;
572fd4151d1SMilanka Ringwald                 return 1;
5730b4debbfSMilanka Ringwald             }
5740b4debbfSMilanka Ringwald             break;
575013cc750SMilanka Ringwald 
576de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
577de9e0ea7SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
578de9e0ea7SMilanka Ringwald             if (done != 0){
579de9e0ea7SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
580de9e0ea7SMilanka Ringwald                 hfp_connection->ok_pending = 1;
581de9e0ea7SMilanka Ringwald                 return 1;
582de9e0ea7SMilanka Ringwald             }
583de9e0ea7SMilanka Ringwald             break;
584de9e0ea7SMilanka Ringwald 
585de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
586de9e0ea7SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
587de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
588de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
589de9e0ea7SMilanka Ringwald             if (hfp_connection->activate_voice_recognition){
590de9e0ea7SMilanka Ringwald                 hfp_hf_activate_voice_recognition(hfp_connection->acl_handle);
591de9e0ea7SMilanka Ringwald             } else {
592de9e0ea7SMilanka Ringwald                 hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
593de9e0ea7SMilanka Ringwald             }
594de9e0ea7SMilanka Ringwald             break;
595de9e0ea7SMilanka Ringwald 
596be55a11dSMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
597498a8121SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
598498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
599de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
600de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
601de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
602de9e0ea7SMilanka Ringwald             } else {
6031a26de69SMilanka Ringwald                 hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
604de9e0ea7SMilanka Ringwald             }
605be55a11dSMilanka Ringwald             break;
606be55a11dSMilanka Ringwald 
607de9e0ea7SMilanka Ringwald 
608013cc750SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
609013cc750SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
610498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
611de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
612de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
613de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
614de9e0ea7SMilanka Ringwald             } else {
615de9e0ea7SMilanka Ringwald                 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
616de9e0ea7SMilanka Ringwald             }
617be55a11dSMilanka Ringwald             break;
618fd4151d1SMilanka Ringwald 
619be55a11dSMilanka Ringwald         default:
620be55a11dSMilanka Ringwald             break;
621be55a11dSMilanka Ringwald     }
622be55a11dSMilanka Ringwald     return done;
623be55a11dSMilanka Ringwald }
624be55a11dSMilanka Ringwald 
625be55a11dSMilanka Ringwald 
626be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
627a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
628ce263fc8SMatthias Ringwald 
629332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
630332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
631ce263fc8SMatthias Ringwald 
632a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
633a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
634332ca98fSMatthias Ringwald 		return 1;
635332ca98fSMatthias Ringwald     }
636332ca98fSMatthias Ringwald 
6371cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
6381cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
639ce263fc8SMatthias Ringwald 
640a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
641fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
6421cc65c4fSMatthias Ringwald 		return 1;
6431cc65c4fSMatthias Ringwald     }
6441cc65c4fSMatthias Ringwald 
6451cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
6461cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
6471cc65c4fSMatthias Ringwald 
648a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
649a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
6501cc65c4fSMatthias Ringwald 		return 1;
6511cc65c4fSMatthias Ringwald     }
652ce263fc8SMatthias Ringwald 
653ce263fc8SMatthias Ringwald     return 0;
654ce263fc8SMatthias Ringwald }
655ce263fc8SMatthias Ringwald 
656a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
657505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
658505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
659ce263fc8SMatthias Ringwald 
66064f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
661a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
662a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
663a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
664ce263fc8SMatthias Ringwald         return 1;
665ce263fc8SMatthias Ringwald     }
666ce263fc8SMatthias Ringwald 
667a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
668ce263fc8SMatthias Ringwald 
669ce263fc8SMatthias Ringwald     // run codecs exchange
670a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
671ce263fc8SMatthias Ringwald     if (done) return 1;
672ce263fc8SMatthias Ringwald 
67338200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
67438200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
67538200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
67638200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
67738200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
67838200c1dSMilanka Ringwald         return 1;
67938200c1dSMilanka Ringwald     }
680ce263fc8SMatthias Ringwald     return 0;
681ce263fc8SMatthias Ringwald }
682ce263fc8SMatthias Ringwald 
68338200c1dSMilanka Ringwald 
684a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
685eaf2b0a1SMatthias Ringwald 
686eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
687eaf2b0a1SMatthias Ringwald 
688a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
689a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
690a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
691ce263fc8SMatthias Ringwald         return 1;
692ce263fc8SMatthias Ringwald     }
693ce263fc8SMatthias Ringwald     return 0;
694ce263fc8SMatthias Ringwald }
695ce263fc8SMatthias Ringwald 
6961c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
6977522e673SMatthias Ringwald 
69876cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
69976cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
70076cc1527SMatthias Ringwald 
70176cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
70276cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
70322387625SMatthias Ringwald 
7043721a235SMatthias Ringwald 	// assert command could be sent
7053721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
7063721a235SMatthias Ringwald 
7073721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
7083721a235SMatthias Ringwald     // WBS Disassociate
7093721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
7103721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
7113721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
7123721a235SMatthias Ringwald         return;
7133721a235SMatthias Ringwald     }
7143721a235SMatthias Ringwald     // Write Codec Config
7153721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
7163721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
7173721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
7183721a235SMatthias Ringwald         return;
7193721a235SMatthias Ringwald     }
7203721a235SMatthias Ringwald     // WBS Associate
7213721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
7223721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
7233721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
7243721a235SMatthias Ringwald         return;
7253721a235SMatthias Ringwald     }
7263721a235SMatthias Ringwald #endif
727689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
728689d4323SMatthias Ringwald     // Enable WBS
729689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
730689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
731689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
732689d4323SMatthias Ringwald         return;
733689d4323SMatthias Ringwald     }
734689d4323SMatthias Ringwald     // Write I2S/PCM params
735689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
736689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
737689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
738689d4323SMatthias Ringwald         return;
739689d4323SMatthias Ringwald     }
740689d4323SMatthias Ringwald     // Disable WBS
741689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
742689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
743689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
744689d4323SMatthias Ringwald         return;
745689d4323SMatthias Ringwald     }
746689d4323SMatthias Ringwald #endif
74748e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
74848e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
74948e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
75048e6eeeeSMatthias Ringwald         return;
75148e6eeeeSMatthias Ringwald     }
75248e6eeeeSMatthias Ringwald #endif
7533721a235SMatthias Ringwald 
754cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
755cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
756cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
7577522e673SMatthias Ringwald         // notify about codec selection if not done already
7587522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
7597522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
7607522e673SMatthias Ringwald         }
761cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
7627522e673SMatthias Ringwald         return;
7637522e673SMatthias Ringwald     }
7647522e673SMatthias Ringwald 
765d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
766d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
767d4dd47ffSMatthias Ringwald         return;
768d4dd47ffSMatthias Ringwald     }
769a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
770ce263fc8SMatthias Ringwald     if (!done){
771a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
772ce263fc8SMatthias Ringwald     }
773ce263fc8SMatthias Ringwald     if (!done){
774c95b5b3cSMilanka Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
775be55a11dSMilanka Ringwald     }
776be55a11dSMilanka Ringwald     if (!done){
777c95b5b3cSMilanka Ringwald         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
778ce263fc8SMatthias Ringwald     }
779ce263fc8SMatthias Ringwald     if (!done){
780a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
781ce263fc8SMatthias Ringwald     }
782ce263fc8SMatthias Ringwald 
7831016a228SMatthias Ringwald     // don't send a new command while ok still pending
7841016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
7851016a228SMatthias Ringwald 
786a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
787a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
788a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
789a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
790ce263fc8SMatthias Ringwald         return;
791ce263fc8SMatthias Ringwald     }
792ce263fc8SMatthias Ringwald 
793a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
794a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
795a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
796a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
797ce263fc8SMatthias Ringwald         return;
798ce263fc8SMatthias Ringwald     }
799ce263fc8SMatthias Ringwald 
800a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
801a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
802a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
803a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
804ce263fc8SMatthias Ringwald         return;
805ce263fc8SMatthias Ringwald     }
806ce263fc8SMatthias Ringwald 
807a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
808a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
809a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
810a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
811ce263fc8SMatthias Ringwald         return;
812ce263fc8SMatthias Ringwald     }
813ce263fc8SMatthias Ringwald 
814a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
815a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
816a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
817a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
818ce263fc8SMatthias Ringwald         return;
819ce263fc8SMatthias Ringwald     }
820ce263fc8SMatthias Ringwald 
821a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
822a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
823a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
824a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
825ce263fc8SMatthias Ringwald         return;
826ce263fc8SMatthias Ringwald     }
827ce263fc8SMatthias Ringwald 
828a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
829a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
830a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
831a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
832ce263fc8SMatthias Ringwald         return;
833ce263fc8SMatthias Ringwald     }
834ce263fc8SMatthias Ringwald 
835a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
836a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
837a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
838a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
839ce263fc8SMatthias Ringwald         return;
840ce263fc8SMatthias Ringwald     }
841ce263fc8SMatthias Ringwald 
842a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
843a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
844a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
845a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
846ce263fc8SMatthias Ringwald         return;
847ce263fc8SMatthias Ringwald     }
848ce263fc8SMatthias Ringwald 
849a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
850a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
851a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
852a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
853ce263fc8SMatthias Ringwald         return;
854ce263fc8SMatthias Ringwald     }
855ce263fc8SMatthias Ringwald 
856a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
857a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
858a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
859a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
860ce263fc8SMatthias Ringwald         return;
861ce263fc8SMatthias Ringwald     }
862ce263fc8SMatthias Ringwald 
863a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
864a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
865a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
866a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
867ce263fc8SMatthias Ringwald         return;
868ce263fc8SMatthias Ringwald     }
869ce263fc8SMatthias Ringwald 
870a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
871a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
872a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
873a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
874ce263fc8SMatthias Ringwald         return;
875ce263fc8SMatthias Ringwald     }
876ce263fc8SMatthias Ringwald 
877a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
878a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
879a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
880a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
881ce263fc8SMatthias Ringwald         return;
882ce263fc8SMatthias Ringwald     }
883ce263fc8SMatthias Ringwald 
884a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
885a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
886a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
887a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
888ce263fc8SMatthias Ringwald         return;
889ce263fc8SMatthias Ringwald     }
890ce263fc8SMatthias Ringwald 
891a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
892a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
893a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
894a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
895ce263fc8SMatthias Ringwald         return;
896ce263fc8SMatthias Ringwald     }
897ce263fc8SMatthias Ringwald 
898a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
899a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
900a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
901a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
902ce263fc8SMatthias Ringwald         return;
903ce263fc8SMatthias Ringwald     }
904ce263fc8SMatthias Ringwald 
905a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
906a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
907a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
908a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
909667ec068SMatthias Ringwald         return;
910667ec068SMatthias Ringwald     }
911667ec068SMatthias Ringwald 
912a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
913a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
914a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
915a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
916a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
917ce263fc8SMatthias Ringwald         return;
918ce263fc8SMatthias Ringwald     }
919ce263fc8SMatthias Ringwald 
920a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
921a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
922a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
923a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
924ce263fc8SMatthias Ringwald         return;
925ce263fc8SMatthias Ringwald     }
926ce263fc8SMatthias Ringwald 
927a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
928a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
929a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
930a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
931667ec068SMatthias Ringwald         return;
932667ec068SMatthias Ringwald     }
933667ec068SMatthias Ringwald 
934a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
935a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
936667ec068SMatthias Ringwald         char buffer[20];
937a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
938667ec068SMatthias Ringwald             case '?':
9391599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
940ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
941ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
942a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
943667ec068SMatthias Ringwald                 return;
944667ec068SMatthias Ringwald             case '0':
945667ec068SMatthias Ringwald             case '1':
946667ec068SMatthias Ringwald             case '2':
9471599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
948ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
949ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
950ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
951a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
952667ec068SMatthias Ringwald                 return;
953667ec068SMatthias Ringwald             default:
954667ec068SMatthias Ringwald                 break;
955667ec068SMatthias Ringwald         }
956667ec068SMatthias Ringwald         return;
957667ec068SMatthias Ringwald     }
958667ec068SMatthias Ringwald 
959a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
960a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
961667ec068SMatthias Ringwald         char buffer[20];
9621599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
963ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
964ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
965a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
966667ec068SMatthias Ringwald         return;
967667ec068SMatthias Ringwald     }
968667ec068SMatthias Ringwald 
969667ec068SMatthias Ringwald     // update HF indicators
970a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
971667ec068SMatthias Ringwald         int i;
972667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
973a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
974a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
975a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
976a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
977667ec068SMatthias Ringwald                     char buffer[30];
9781599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
979ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
980ff7d6aeaSMatthias Ringwald                              hfp_indicators[i],
981ff7d6aeaSMatthias Ringwald                              (unsigned int)hfp_indicators_value[i]);
982ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
983a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
984667ec068SMatthias Ringwald                 } else {
98560ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
986667ec068SMatthias Ringwald                 }
987667ec068SMatthias Ringwald                 return;
988667ec068SMatthias Ringwald             }
989667ec068SMatthias Ringwald         }
990667ec068SMatthias Ringwald     }
991667ec068SMatthias Ringwald 
992ce263fc8SMatthias Ringwald     if (done) return;
993ce263fc8SMatthias Ringwald     // deal with disconnect
994a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
995ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
996a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
997a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
998ce263fc8SMatthias Ringwald             break;
999ce263fc8SMatthias Ringwald 
1000ce263fc8SMatthias Ringwald         default:
1001ce263fc8SMatthias Ringwald             break;
1002ce263fc8SMatthias Ringwald     }
1003ce263fc8SMatthias Ringwald }
1004ce263fc8SMatthias Ringwald 
1005a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
1006a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
10076a7f44bdSMilanka Ringwald 
1008ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
10097522e673SMatthias Ringwald 
1010667ec068SMatthias Ringwald     // restore volume settings
1011a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
1012a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1013ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
1014a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
1015a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1016ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
1017667ec068SMatthias Ringwald     // enable all indicators
1018667ec068SMatthias Ringwald     int i;
1019667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
1020a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
1021a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
1022667ec068SMatthias Ringwald     }
1023ce263fc8SMatthias Ringwald }
1024ce263fc8SMatthias Ringwald 
10251cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
10261cc65c4fSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
10271cc65c4fSMatthias Ringwald 		// Codec supported, confirm
10281cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
10291cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
10301cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
10311cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
10321cc65c4fSMatthias Ringwald 
10331cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
10341cc65c4fSMatthias Ringwald 	} else {
10351cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
10361cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
10371cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
10381cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
10391cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10401cc65c4fSMatthias Ringwald 
10411cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
10421cc65c4fSMatthias Ringwald 	}
10431cc65c4fSMatthias Ringwald }
10441cc65c4fSMatthias Ringwald 
1045a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
1046a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
10473deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
1048a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
1049a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
10503deb3ec6SMatthias Ringwald                 break;
10513deb3ec6SMatthias Ringwald             }
1052a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
10533deb3ec6SMatthias Ringwald             break;
10543deb3ec6SMatthias Ringwald 
10553deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
1056a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
10573deb3ec6SMatthias Ringwald             break;
10583deb3ec6SMatthias Ringwald 
10593deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
1060a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
10613deb3ec6SMatthias Ringwald             break;
10623deb3ec6SMatthias Ringwald 
10633deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1064a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
10653deb3ec6SMatthias Ringwald             break;
10663deb3ec6SMatthias Ringwald 
10673deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1068a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1069a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
10703deb3ec6SMatthias Ringwald                 break;
10713deb3ec6SMatthias Ringwald             }
1072a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
1073a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
10743deb3ec6SMatthias Ringwald                 break;
10753deb3ec6SMatthias Ringwald             }
1076a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
10773deb3ec6SMatthias Ringwald             break;
10783deb3ec6SMatthias Ringwald 
10793deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1080a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
1081a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
10823deb3ec6SMatthias Ringwald                 break;
10833deb3ec6SMatthias Ringwald             }
1084a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
10853deb3ec6SMatthias Ringwald             break;
10863deb3ec6SMatthias Ringwald 
10873deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1088a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
10893deb3ec6SMatthias Ringwald             break;
10903deb3ec6SMatthias Ringwald 
10913deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1092a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
10933deb3ec6SMatthias Ringwald             break;
10943deb3ec6SMatthias Ringwald 
10953deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1096a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
10973deb3ec6SMatthias Ringwald             break;
1098ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1099a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1100a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1101ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1102ce263fc8SMatthias Ringwald                 break;
1103ce263fc8SMatthias Ringwald             }
11043deb3ec6SMatthias Ringwald 
1105a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1106a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1107ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1108ce263fc8SMatthias Ringwald                 break;
11093deb3ec6SMatthias Ringwald             }
11103deb3ec6SMatthias Ringwald 
1111a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
1112ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1113a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1114ce263fc8SMatthias Ringwald                     break;
1115ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1116a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1117a473a009SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_connection);
1118ce263fc8SMatthias Ringwald                     break;
1119ce263fc8SMatthias Ringwald                 default:
1120ce263fc8SMatthias Ringwald                     break;
11213deb3ec6SMatthias Ringwald             }
1122ce263fc8SMatthias Ringwald 
1123a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
1124a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
1125ce263fc8SMatthias Ringwald                 break;
11263deb3ec6SMatthias Ringwald             }
11273deb3ec6SMatthias Ringwald 
1128a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
1129aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1130a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
11313deb3ec6SMatthias Ringwald                     break;
1132ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
1133a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1134ce263fc8SMatthias Ringwald                     break;
11353deb3ec6SMatthias Ringwald                 default:
11363deb3ec6SMatthias Ringwald                     break;
11373deb3ec6SMatthias Ringwald             }
1138af97579eSMilanka Ringwald             hfp_hf_voice_recognition_state_machine(hfp_connection);
1139be55a11dSMilanka Ringwald             break;
1140be55a11dSMilanka Ringwald         case HFP_AUDIO_CONNECTION_ESTABLISHED:
1141af97579eSMilanka Ringwald             hfp_hf_voice_recognition_state_machine(hfp_connection);
11423deb3ec6SMatthias Ringwald             break;
11433deb3ec6SMatthias Ringwald         default:
11443deb3ec6SMatthias Ringwald             break;
11453deb3ec6SMatthias Ringwald     }
11463deb3ec6SMatthias Ringwald 
11473deb3ec6SMatthias Ringwald     // done
1148be55a11dSMilanka Ringwald     hfp_connection->ok_pending = 0;
1149a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
11503deb3ec6SMatthias Ringwald }
11513deb3ec6SMatthias Ringwald 
1152be55a11dSMilanka Ringwald 
1153b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
11544562e2a2SMatthias Ringwald     uint16_t i;
11554562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
11564562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
11574562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
11584562e2a2SMatthias Ringwald                 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
11594562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
11604562e2a2SMatthias Ringwald                 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
11614562e2a2SMatthias Ringwald                 // avoid set but not used warning
11624562e2a2SMatthias Ringwald                 (void) hfp_callheld_status;
11634562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
11644562e2a2SMatthias Ringwald                 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
11654562e2a2SMatthias Ringwald             }
11664562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1167a473a009SMatthias Ringwald             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
11684562e2a2SMatthias Ringwald             break;
11694562e2a2SMatthias Ringwald         }
11704562e2a2SMatthias Ringwald     }
11714562e2a2SMatthias Ringwald }
11724562e2a2SMatthias Ringwald 
1173426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1174186dd3d2SMatthias Ringwald     int value;
1175186dd3d2SMatthias Ringwald     int i;
1176a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1177667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1178a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1179a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1180667ec068SMatthias Ringwald             break;
1181667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1182a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1183ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1184667ec068SMatthias Ringwald             break;
1185667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1186a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1187a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1188667ec068SMatthias Ringwald             break;
1189ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1190a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11912308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1192667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1193ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1194ce263fc8SMatthias Ringwald             break;
1195ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1196a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11972308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1198667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1199ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1200ce263fc8SMatthias Ringwald             break;
1201ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1202a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1203ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1204a0ffb263SMatthias Ringwald             break;
1205a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1206a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1207a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1208a0ffb263SMatthias Ringwald             break;
1209a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1210a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1211a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1212ce263fc8SMatthias Ringwald             break;
1213ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1214a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12155a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1216a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1217ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1218ce263fc8SMatthias Ringwald             break;
12190b4debbfSMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
12200b4debbfSMilanka Ringwald             break;
1221fdda66c0SMilanka Ringwald         case HFP_CMD_ERROR:
122290244c92SMilanka Ringwald             switch (hfp_connection->state){
122390244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
122490244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
122590244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1226fdda66c0SMilanka Ringwald                             hfp_reset_context_flags(hfp_connection);
122790244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
122890244c92SMilanka Ringwald                             return;
122990244c92SMilanka Ringwald                         default:
123090244c92SMilanka Ringwald                             break;
123190244c92SMilanka Ringwald                     }
123256f1adacSMilanka Ringwald                     break;
123356f1adacSMilanka Ringwald                 default:
123456f1adacSMilanka Ringwald                     break;
123556f1adacSMilanka Ringwald             }
1236fdda66c0SMilanka Ringwald             // handle error response for voice activation (HF initiated)
12370b4debbfSMilanka Ringwald             switch(hfp_connection->vra_state_requested){
1238de9e0ea7SMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1239de9e0ea7SMilanka Ringwald                     hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1240be55a11dSMilanka Ringwald                     break;
1241be55a11dSMilanka Ringwald                 default:
12420b4debbfSMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
12431a26de69SMilanka Ringwald                     hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
12440b4debbfSMilanka Ringwald                     hfp_reset_context_flags(hfp_connection);
12450b4debbfSMilanka Ringwald                     return;
1246be55a11dSMilanka Ringwald             }
12470b4debbfSMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1248fdda66c0SMilanka Ringwald             hfp_reset_context_flags(hfp_connection);
1249ce263fc8SMatthias Ringwald             break;
1250fdda66c0SMilanka Ringwald 
1251ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1252a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1253ce263fc8SMatthias Ringwald             break;
1254ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
12555a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1256ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1257ce263fc8SMatthias Ringwald             break;
1258ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
12595a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12604562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1261ce263fc8SMatthias Ringwald             break;
1262c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
12635a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1264c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1265a473a009SMatthias Ringwald                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1266c741b032SMilanka Ringwald             }
1267c741b032SMilanka Ringwald             break;
12681cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
12691cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12705a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
12711cc65c4fSMatthias Ringwald 			break;
1272eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1273eac56539SMilanka 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));
1274ce263fc8SMatthias Ringwald         default:
1275ce263fc8SMatthias Ringwald             break;
12763deb3ec6SMatthias Ringwald     }
12770cef86faSMatthias Ringwald }
1278426f9988SMatthias Ringwald 
127976cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
128076cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
128176cc1527SMatthias Ringwald }
128276cc1527SMatthias Ringwald 
12830b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1284426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1285426f9988SMatthias Ringwald     if (size < 1) return;
1286426f9988SMatthias Ringwald 
1287426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1288e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1289e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1290e43d1938SMatthias Ringwald #endif
1291426f9988SMatthias Ringwald 
1292426f9988SMatthias Ringwald     // process messages byte-wise
1293426f9988SMatthias Ringwald     int pos;
1294426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1295426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
12961599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1297426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1298426f9988SMatthias Ringwald     }
12990b4debbfSMilanka Ringwald     hfp_hf_handle_rfcomm_command(hfp_connection);
13003deb3ec6SMatthias Ringwald }
13013deb3ec6SMatthias Ringwald 
13021c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1303665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1304665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1305665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1306a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
130722387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
13081c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
13093deb3ec6SMatthias Ringwald     }
13103deb3ec6SMatthias Ringwald }
13113deb3ec6SMatthias Ringwald 
13121c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
13130b4debbfSMilanka Ringwald     hfp_connection_t * hfp_connection;
13143deb3ec6SMatthias Ringwald     switch (packet_type){
13153deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
13160b4debbfSMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
13170b4debbfSMilanka Ringwald             if (!hfp_connection) return;
13180b4debbfSMilanka Ringwald             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
13190b4debbfSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
13200b4debbfSMilanka Ringwald             return;
13213deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1322d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1323d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
13241c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1325d4dd47ffSMatthias Ringwald                 return;
1326d4dd47ffSMatthias Ringwald             }
132727950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1328202c8a4cSMatthias Ringwald             break;
13293deb3ec6SMatthias Ringwald         default:
13303deb3ec6SMatthias Ringwald             break;
13313deb3ec6SMatthias Ringwald     }
13321c6a0fc0SMatthias Ringwald     hfp_hf_run();
13333deb3ec6SMatthias Ringwald }
13343deb3ec6SMatthias Ringwald 
13351c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1336405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
13371c6a0fc0SMatthias Ringwald     hfp_hf_run();
1338405014fbSMatthias Ringwald }
1339405014fbSMatthias Ringwald 
1340b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1341b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1342b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1343b4df8028SMilanka Ringwald         return status;
1344b4df8028SMilanka Ringwald     }
1345b4df8028SMilanka Ringwald 
1346520c92d5SMatthias Ringwald     hfp_init();
134720b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
134820b2edb6SMatthias Ringwald     hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
134920b2edb6SMatthias Ringwald     hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
135020b2edb6SMatthias Ringwald     hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
135120b2edb6SMatthias Ringwald     hfp_codecs_nr = 0;
135220b2edb6SMatthias Ringwald     hfp_hf_speaker_gain = 9;
135320b2edb6SMatthias Ringwald     hfp_hf_microphone_gain = 9;
135420b2edb6SMatthias Ringwald     hfp_indicators_nr = 0;
135520b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1356d63c37a1SMatthias Ringwald 
13571c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
13581c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
135927950165SMatthias Ringwald 
136027950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
13611c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1362b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
136320b2edb6SMatthias Ringwald }
136427950165SMatthias Ringwald 
136520b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
136620b2edb6SMatthias Ringwald     hfp_deinit();
136720b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
136820b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t));
136920b2edb6SMatthias Ringwald     (void) memset(phone_number, 0, sizeof(phone_number));
1370a0ffb263SMatthias Ringwald }
1371a0ffb263SMatthias Ringwald 
13727ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
137368466199SMilanka Ringwald     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
13743deb3ec6SMatthias Ringwald 
13753deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
13763deb3ec6SMatthias Ringwald     int i;
13773deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
13783deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
13793deb3ec6SMatthias Ringwald     }
13803deb3ec6SMatthias Ringwald }
13813deb3ec6SMatthias Ringwald 
1382a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
13833deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1384a0ffb263SMatthias Ringwald }
13853deb3ec6SMatthias Ringwald 
13867ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
138768466199SMilanka Ringwald     btstack_assert(hfp_indicators_nr < HFP_MAX_NUM_INDICATORS);
138868466199SMilanka Ringwald 
13893deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
13903deb3ec6SMatthias Ringwald     int i;
1391a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
13923deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
13933deb3ec6SMatthias Ringwald     }
13943deb3ec6SMatthias Ringwald }
13953deb3ec6SMatthias Ringwald 
13964eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
13974eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
13983deb3ec6SMatthias Ringwald }
13993deb3ec6SMatthias Ringwald 
1400657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
14019c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1402a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1403657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1404a33eb0c4SMilanka Ringwald     }
14051ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
14061c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1407657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
14083deb3ec6SMatthias Ringwald }
14093deb3ec6SMatthias Ringwald 
14103c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
14119c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1412a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14133c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14143deb3ec6SMatthias Ringwald     }
1415a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
14161c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14173c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14183deb3ec6SMatthias Ringwald }
14193deb3ec6SMatthias Ringwald 
14203c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
14213c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1422ce263fc8SMatthias Ringwald }
1423ce263fc8SMatthias Ringwald 
14243c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
14253c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1426ce263fc8SMatthias Ringwald }
1427ce263fc8SMatthias Ringwald 
14283deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
14293c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
14309c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1431a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14323c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14333deb3ec6SMatthias Ringwald     }
1434a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1435a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
14361c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14373c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14383deb3ec6SMatthias Ringwald }
14393deb3ec6SMatthias Ringwald 
14403c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
14419c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1442a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14433c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14443deb3ec6SMatthias Ringwald     }
14453c65e705SMilanka Ringwald 
1446a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1447ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1448a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1449ce263fc8SMatthias Ringwald             break;
1450ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1451a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1452ce263fc8SMatthias Ringwald             break;
1453ce263fc8SMatthias Ringwald         default:
14543c65e705SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1455ce263fc8SMatthias Ringwald     }
14561c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14573c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14583deb3ec6SMatthias Ringwald }
14593deb3ec6SMatthias Ringwald 
14603c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
14619c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1462a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14633c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14643deb3ec6SMatthias Ringwald     }
1465a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
14661c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14673c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14683deb3ec6SMatthias Ringwald }
14693deb3ec6SMatthias Ringwald 
1470ce263fc8SMatthias Ringwald 
14713c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
14723c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1473ce263fc8SMatthias Ringwald }
1474ce263fc8SMatthias Ringwald 
14753c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
14763c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1477ce263fc8SMatthias Ringwald }
1478ce263fc8SMatthias Ringwald 
147938200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
148038200c1dSMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
148138200c1dSMilanka Ringwald }
1482ce263fc8SMatthias Ringwald 
14833c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
14849c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1485a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14863c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1487a33eb0c4SMilanka Ringwald     }
1488ce263fc8SMatthias Ringwald 
14893c65e705SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
14903c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
14913c65e705SMilanka Ringwald     }
14923c65e705SMilanka Ringwald 
14933c65e705SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
14943c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
14953c65e705SMilanka Ringwald     }
1496f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1497a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1498aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1499aa4dd815SMatthias Ringwald                 break;
1500ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1501ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1502ec3bfc1aSMatthias Ringwald                 break;
1503aa4dd815SMatthias Ringwald             default:
15041cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
15051cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
15061cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
15071cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
150838200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1509aa4dd815SMatthias Ringwald                 break;
15103deb3ec6SMatthias Ringwald         }
1511f4412093SMatthias Ringwald     } else {
1512f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1513f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1514f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1515f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1516f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1517f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1518f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1519f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1520ce263fc8SMatthias Ringwald     }
1521ce263fc8SMatthias Ringwald 
15221c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15233c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15243deb3ec6SMatthias Ringwald }
15253deb3ec6SMatthias Ringwald 
15263c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
15279c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1528a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15293c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1530a33eb0c4SMilanka Ringwald     }
15310b4debbfSMilanka Ringwald     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
15320b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
15330b4debbfSMilanka Ringwald     }
15340b4debbfSMilanka Ringwald     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
15350b4debbfSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
15361c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
15370b4debbfSMilanka Ringwald     }
15383c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15393deb3ec6SMatthias Ringwald }
15403deb3ec6SMatthias Ringwald 
15413c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
15429c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1543a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15443c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1545a33eb0c4SMilanka Ringwald     }
1546ce263fc8SMatthias Ringwald 
1547ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1548a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
15491c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1550ce263fc8SMatthias Ringwald     } else {
1551ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
15523c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1553ce263fc8SMatthias Ringwald     }
15543c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1555ce263fc8SMatthias Ringwald }
1556ce263fc8SMatthias Ringwald 
15573c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
15589c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1559a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15603c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1561a33eb0c4SMilanka Ringwald     }
1562a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
15631c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15643c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1565ce263fc8SMatthias Ringwald }
1566ce263fc8SMatthias Ringwald 
15673c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
15689c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1569a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15703c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1571a33eb0c4SMilanka Ringwald     }
1572ce263fc8SMatthias Ringwald 
1573ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1574a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
15751c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1576ce263fc8SMatthias Ringwald     }
15773c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1578ce263fc8SMatthias Ringwald }
1579ce263fc8SMatthias Ringwald 
15803c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){
15819c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1582a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15833c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1584a33eb0c4SMilanka Ringwald     }
1585ce263fc8SMatthias Ringwald 
1586ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1587a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
15881c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1589ce263fc8SMatthias Ringwald     }
15903c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1591ce263fc8SMatthias Ringwald }
1592ce263fc8SMatthias Ringwald 
15933c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
15949c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1595a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15963c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1597a33eb0c4SMilanka Ringwald     }
1598ce263fc8SMatthias Ringwald 
1599505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1600505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1601a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 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_swap_calls(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_2 = 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_join_held_call(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_3 = 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_connect_calls(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_4 = 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_release_call_with_index(hci_con_handle_t acl_handle, int index){
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     }
1654667ec068SMatthias 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_x = 1;
1658a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
16591c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1660667ec068SMatthias Ringwald     }
16613c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1662667ec068SMatthias Ringwald }
1663667ec068SMatthias Ringwald 
16643c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
16659c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1666a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16673c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1668a33eb0c4SMilanka Ringwald     }
1669667ec068SMatthias Ringwald 
1670505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1671505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1672a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1673a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
16741c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1675667ec068SMatthias Ringwald     }
16763c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1677667ec068SMatthias Ringwald }
1678ce263fc8SMatthias Ringwald 
16793c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
16809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1681a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16823c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1683a33eb0c4SMilanka Ringwald     }
1684ce263fc8SMatthias Ringwald 
1685a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1686ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
16871c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16883c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1689ce263fc8SMatthias Ringwald }
1690ce263fc8SMatthias Ringwald 
16913c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
16929c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1693a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16943c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1695a33eb0c4SMilanka Ringwald     }
1696ce263fc8SMatthias Ringwald 
1697a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1698a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1699a0ffb263SMatthias Ringwald 
17001c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17013c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1702ce263fc8SMatthias Ringwald }
1703ce263fc8SMatthias Ringwald 
17043c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
17059c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1706a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17073c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1708a33eb0c4SMilanka Ringwald     }
1709ce263fc8SMatthias Ringwald 
1710a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
17111c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17123c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1713ce263fc8SMatthias Ringwald }
1714ce263fc8SMatthias Ringwald 
17153c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
17169c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1717a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17183c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1719a33eb0c4SMilanka Ringwald     }
1720ce263fc8SMatthias Ringwald 
1721a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
17221c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17233c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1724ce263fc8SMatthias Ringwald }
1725ce263fc8SMatthias Ringwald 
1726ce263fc8SMatthias Ringwald 
17273c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
17289c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1729a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17303c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1731a33eb0c4SMilanka Ringwald     }
1732ce263fc8SMatthias Ringwald 
1733a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
17341c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17353c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1736ce263fc8SMatthias Ringwald }
1737ce263fc8SMatthias Ringwald 
1738ce263fc8SMatthias Ringwald 
17393c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
17409c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1741a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17423c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1743a33eb0c4SMilanka Ringwald     }
1744ce263fc8SMatthias Ringwald 
1745a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
17461c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17473c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1748ce263fc8SMatthias Ringwald }
1749ce263fc8SMatthias Ringwald 
17503c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
17519c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1752a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17533c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1754a33eb0c4SMilanka Ringwald     }
1755ce263fc8SMatthias Ringwald 
1756a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
17571c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17583c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1759ce263fc8SMatthias Ringwald }
1760ce263fc8SMatthias Ringwald 
1761*6ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){
1762*6ba83b5eSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION);
1763*6ba83b5eSMilanka Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_EC_NR_FUNCTION);
1764*6ba83b5eSMilanka Ringwald     return hf && ag;
1765ce263fc8SMatthias Ringwald }
1766ce263fc8SMatthias Ringwald 
17673c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
17689c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1769a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17703c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1771a33eb0c4SMilanka Ringwald     }
1772*6ba83b5eSMilanka Ringwald     if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){
1773*6ba83b5eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1774*6ba83b5eSMilanka Ringwald     }
1775ce263fc8SMatthias Ringwald 
1776a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
17771c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17783c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1779ce263fc8SMatthias Ringwald }
1780ce263fc8SMatthias Ringwald 
1781013cc750SMilanka Ringwald static bool hfp_hf_voice_recognition_supported(hfp_connection_t * hfp_connection, bool enhanced){
1782013cc750SMilanka Ringwald     uint8_t hf_vra_flag = HFP_HFSF_VOICE_RECOGNITION_FUNCTION;
1783013cc750SMilanka Ringwald     uint8_t ag_vra_flag = HFP_AGSF_VOICE_RECOGNITION_FUNCTION;
1784013cc750SMilanka Ringwald 
1785013cc750SMilanka Ringwald     if (enhanced){
1786013cc750SMilanka Ringwald         hf_vra_flag = HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS;
1787013cc750SMilanka Ringwald         ag_vra_flag = HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS;
1788013cc750SMilanka Ringwald     }
1789013cc750SMilanka Ringwald 
1790013cc750SMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, ag_vra_flag);
1791013cc750SMilanka Ringwald     int hf = get_bit(hfp_supported_features, hf_vra_flag);
1792be55a11dSMilanka Ringwald     return hf && ag;
1793be55a11dSMilanka Ringwald }
1794be55a11dSMilanka Ringwald 
1795013cc750SMilanka Ringwald static uint8_t activate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){
1796fdda66c0SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1797fdda66c0SMilanka Ringwald     if (!hfp_connection) {
1798fdda66c0SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1799be55a11dSMilanka Ringwald     }
1800013cc750SMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1801013cc750SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1802013cc750SMilanka Ringwald     }
1803013cc750SMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection, enhanced)){
1804af97579eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1805af97579eSMilanka Ringwald     }
1806af97579eSMilanka Ringwald 
1807498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1808be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1809de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1810498a8121SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1811fd4151d1SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
18126b8275b0SMilanka Ringwald             hfp_connection->enhanced_voice_recognition_enabled = enhanced;
1813be55a11dSMilanka Ringwald             break;
1814de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1815de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = true;
1816de9e0ea7SMilanka Ringwald             break;
1817be55a11dSMilanka Ringwald         default:
1818be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1819be55a11dSMilanka Ringwald     }
1820ce263fc8SMatthias Ringwald 
1821af97579eSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1822fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1823af97579eSMilanka Ringwald }
1824af97579eSMilanka Ringwald 
1825013cc750SMilanka Ringwald 
1826013cc750SMilanka Ringwald static uint8_t deactivate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){
1827af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1828af97579eSMilanka Ringwald     if (!hfp_connection) {
1829af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1830af97579eSMilanka Ringwald     }
1831c95b5b3cSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
183208a0b01cSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
183308a0b01cSMilanka Ringwald     }
1834013cc750SMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection, enhanced)){
1835fdda66c0SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1836af97579eSMilanka Ringwald     }
1837013cc750SMilanka Ringwald 
1838fdda66c0SMilanka Ringwald     switch (hfp_connection->vra_state){
1839de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
1840fdda66c0SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1841de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1842de9e0ea7SMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1843fdda66c0SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1844fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1845fdda66c0SMilanka Ringwald             break;
1846de9e0ea7SMilanka Ringwald 
1847de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1848de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1849de9e0ea7SMilanka Ringwald             hfp_connection->deactivate_voice_recognition = true;
1850de9e0ea7SMilanka Ringwald             break;
1851de9e0ea7SMilanka Ringwald 
1852de9e0ea7SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1853de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1854de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1855fdda66c0SMilanka Ringwald         default:
1856fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1857fdda66c0SMilanka Ringwald     }
1858fdda66c0SMilanka Ringwald 
1859fdda66c0SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1860fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1861af97579eSMilanka Ringwald }
1862af97579eSMilanka Ringwald 
1863013cc750SMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1864013cc750SMilanka Ringwald     return activate_voice_recognition(acl_handle, false);
1865013cc750SMilanka Ringwald }
1866013cc750SMilanka Ringwald 
1867e83c2025SMilanka Ringwald uint8_t hfp_hf_activate_enhanced_voice_recognition(hci_con_handle_t acl_handle){
1868013cc750SMilanka Ringwald     return activate_voice_recognition(acl_handle, true);
1869be55a11dSMilanka Ringwald }
1870fdda66c0SMilanka Ringwald 
1871de9e0ea7SMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
1872de9e0ea7SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1873de9e0ea7SMilanka Ringwald     if (!hfp_connection) {
1874de9e0ea7SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1875de9e0ea7SMilanka Ringwald     }
1876de9e0ea7SMilanka Ringwald     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
1877de9e0ea7SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1878de9e0ea7SMilanka Ringwald     }
1879de9e0ea7SMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection, true)){
1880de9e0ea7SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1881de9e0ea7SMilanka Ringwald     }
1882de9e0ea7SMilanka Ringwald 
1883de9e0ea7SMilanka Ringwald     switch (hfp_connection->vra_state){
1884de9e0ea7SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1885de9e0ea7SMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1886de9e0ea7SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1887de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
1888de9e0ea7SMilanka Ringwald             break;
1889de9e0ea7SMilanka Ringwald         default:
1890de9e0ea7SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1891de9e0ea7SMilanka Ringwald     }
1892de9e0ea7SMilanka Ringwald 
1893de9e0ea7SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1894de9e0ea7SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1895de9e0ea7SMilanka Ringwald }
1896de9e0ea7SMilanka Ringwald 
1897fdda66c0SMilanka Ringwald 
1898013cc750SMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1899013cc750SMilanka Ringwald     return deactivate_voice_recognition(acl_handle, false);
1900be55a11dSMilanka Ringwald }
1901be55a11dSMilanka Ringwald 
1902e83c2025SMilanka Ringwald uint8_t hfp_hf_deactivate_enhanced_voice_recognition(hci_con_handle_t acl_handle){
1903013cc750SMilanka Ringwald     return deactivate_voice_recognition(acl_handle, true);
1904ce263fc8SMatthias Ringwald }
1905ce263fc8SMatthias Ringwald 
1906de9e0ea7SMilanka Ringwald 
19073c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
19089c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1909a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19103c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1911a33eb0c4SMilanka Ringwald     }
1912c8626498SMilanka Ringwald 
19133c65e705SMilanka Ringwald     if (hfp_connection->microphone_gain == gain) {
19143c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
19153c65e705SMilanka Ringwald     }
19163c65e705SMilanka Ringwald 
1917c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1918a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
19193c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1920a0ffb263SMatthias Ringwald     }
19213c65e705SMilanka Ringwald 
1922a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1923a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
19241c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19253c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1926ce263fc8SMatthias Ringwald }
1927ce263fc8SMatthias Ringwald 
19283c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
19299c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1930a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19313c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1932a33eb0c4SMilanka Ringwald     }
1933c8626498SMilanka Ringwald 
19343c65e705SMilanka Ringwald     if (hfp_connection->speaker_gain == gain){
19353c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
19363c65e705SMilanka Ringwald     }
19373c65e705SMilanka Ringwald 
1938c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1939a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
19403c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1941a0ffb263SMatthias Ringwald     }
19423c65e705SMilanka Ringwald 
1943a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1944a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
19451c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19463c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1947ce263fc8SMatthias Ringwald }
1948ce263fc8SMatthias Ringwald 
19493c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
19509c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1951a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19523c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1953a33eb0c4SMilanka Ringwald     }
1954a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
19551c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19563c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1957ce263fc8SMatthias Ringwald }
1958ce263fc8SMatthias Ringwald 
19593c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
19609c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1961a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19623c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1963a33eb0c4SMilanka Ringwald     }
1964a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
19651c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19663c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1967ce263fc8SMatthias Ringwald }
19683deb3ec6SMatthias Ringwald 
19693c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
19709c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1971a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19723c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1973a33eb0c4SMilanka Ringwald     }
1974a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
19751c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19763c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1977667ec068SMatthias Ringwald }
1978667ec068SMatthias Ringwald 
1979667ec068SMatthias Ringwald 
19803c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
19819c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1982a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19833c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1984a33eb0c4SMilanka Ringwald     }
1985a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1986a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
19871c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19883c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1989667ec068SMatthias Ringwald }
1990667ec068SMatthias Ringwald 
19913c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
19929c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1993a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19943c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1995a33eb0c4SMilanka Ringwald     }
1996a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1997a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
19981c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19993c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2000667ec068SMatthias Ringwald }
2001667ec068SMatthias Ringwald 
20023c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
20039c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2004a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20053c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2006a33eb0c4SMilanka Ringwald     }
2007a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2008a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
20091c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20103c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2011667ec068SMatthias Ringwald }
2012667ec068SMatthias Ringwald 
20133c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
20149c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2015a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20163c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2017a33eb0c4SMilanka Ringwald     }
2018a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2019a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
20201c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20213c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2022667ec068SMatthias Ringwald }
2023667ec068SMatthias Ringwald 
20243c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
20259c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2026a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20273c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2028a33eb0c4SMilanka Ringwald     }
2029a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
20301c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20313c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2032667ec068SMatthias Ringwald }
2033667ec068SMatthias Ringwald 
20343c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
20359c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2036a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20373c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2038a33eb0c4SMilanka Ringwald     }
2039667ec068SMatthias Ringwald     // find index for assigned number
2040667ec068SMatthias Ringwald     int i;
2041667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
2042667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
2043667ec068SMatthias Ringwald             // set value
2044667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
2045667ec068SMatthias Ringwald             // mark for update
2046a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
2047a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
2048667ec068SMatthias Ringwald                 // send update
20491c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
2050a0ffb263SMatthias Ringwald             }
20513c65e705SMilanka Ringwald             return ERROR_CODE_SUCCESS;
2052667ec068SMatthias Ringwald         }
2053667ec068SMatthias Ringwald     }
20543c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2055667ec068SMatthias Ringwald }
2056667ec068SMatthias Ringwald 
2057d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
2058d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2059d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
2060d7f6b5cbSMatthias Ringwald         return 0;
2061d7f6b5cbSMatthias Ringwald     }
2062d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
2063d7f6b5cbSMatthias Ringwald }
206476cc1527SMatthias Ringwald 
206576cc1527SMatthias 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){
206676cc1527SMatthias Ringwald 	if (!name){
206776cc1527SMatthias Ringwald 		name = default_hfp_hf_service_name;
206876cc1527SMatthias Ringwald 	}
206976cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
207076cc1527SMatthias Ringwald 
207176cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
207276cc1527SMatthias Ringwald 	//
207376cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
207476cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
207576cc1527SMatthias Ringwald 	//
207676cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
207776cc1527SMatthias Ringwald 	//
207876cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
2079ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
208076cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
208176cc1527SMatthias Ringwald 	}
2082ef3ae4ebSMilanka Ringwald 
2083ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
208456f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
2085ef3ae4ebSMilanka Ringwald     }
2086ef3ae4ebSMilanka Ringwald 
2087ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
208856f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
2089ef3ae4ebSMilanka Ringwald     }
2090ef3ae4ebSMilanka Ringwald 
209176cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
209276cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
209376cc1527SMatthias Ringwald }
209476cc1527SMatthias Ringwald 
209576cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
209668466199SMilanka Ringwald 	btstack_assert(callback != NULL);
209768466199SMilanka Ringwald 
209876cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
209976cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
210076cc1527SMatthias Ringwald }
2101