xref: /btstack/src/classic/hfp_hf.c (revision cf75be853470858fd5258f077fae9b5179159329)
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 
21576cc1527SMatthias Ringwald /* send commands */
21689425bfcSMilanka Ringwald 
21789425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2183deb3ec6SMatthias Ringwald     char buffer[20];
2191599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
22089425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
22189425bfcSMilanka Ringwald }
22289425bfcSMilanka Ringwald 
22389425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
22489425bfcSMilanka Ringwald     char buffer[20];
2251599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
22689425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
22789425bfcSMilanka Ringwald }
22889425bfcSMilanka Ringwald 
22986da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
23089425bfcSMilanka Ringwald     char buffer[40];
2311599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
2323deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2333deb3ec6SMatthias Ringwald }
2343deb3ec6SMatthias Ringwald 
2353deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2363deb3ec6SMatthias Ringwald     char buffer[30];
23789425bfcSMilanka Ringwald     const int size = sizeof(buffer);
23889425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
23989425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
2401599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2413deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2423deb3ec6SMatthias Ringwald }
2433deb3ec6SMatthias Ringwald 
2443deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2453deb3ec6SMatthias Ringwald     char buffer[50];
24689425bfcSMilanka Ringwald     const int size = sizeof(buffer);
24789425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
24889425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
2491599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2503deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2513deb3ec6SMatthias Ringwald }
2523deb3ec6SMatthias Ringwald 
2533deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2543deb3ec6SMatthias Ringwald     char buffer[30];
25589425bfcSMilanka Ringwald     const int size = sizeof(buffer);
25689425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
25789425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
2581599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2593deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2603deb3ec6SMatthias Ringwald }
2613deb3ec6SMatthias Ringwald 
26289425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2633deb3ec6SMatthias Ringwald     char buffer[20];
2641599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
265ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
266ce263fc8SMatthias Ringwald }
267ce263fc8SMatthias Ringwald 
268ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
269ce263fc8SMatthias Ringwald     char buffer[40];
2701599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, phone_number);
271ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
272ce263fc8SMatthias Ringwald }
273ce263fc8SMatthias Ringwald 
274a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
275ce263fc8SMatthias Ringwald     char buffer[40];
2761599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
277ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
278ce263fc8SMatthias Ringwald }
279ce263fc8SMatthias Ringwald 
280f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
28189425bfcSMilanka Ringwald     char buffer[40];
2821599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
283ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
284ce263fc8SMatthias Ringwald }
285ce263fc8SMatthias Ringwald 
286ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
287ce263fc8SMatthias Ringwald     char buffer[20];
2881599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
289ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
290ce263fc8SMatthias Ringwald }
291ce263fc8SMatthias Ringwald 
29297d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
2931599fe57SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "ATA\r");
29497d2cadbSMatthias Ringwald }
29597d2cadbSMatthias Ringwald 
29689425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
29789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
29889425bfcSMilanka Ringwald }
29989425bfcSMilanka Ringwald 
30089425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
30189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
30289425bfcSMilanka Ringwald }
30389425bfcSMilanka Ringwald 
30489425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
30589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
30689425bfcSMilanka Ringwald }
30789425bfcSMilanka Ringwald 
30889425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
30989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
31089425bfcSMilanka Ringwald }
31189425bfcSMilanka Ringwald 
31289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
31389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
31489425bfcSMilanka Ringwald }
31589425bfcSMilanka Ringwald 
31689425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
31789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
31889425bfcSMilanka Ringwald }
31989425bfcSMilanka Ringwald 
32089425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
32189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
32289425bfcSMilanka Ringwald }
32389425bfcSMilanka Ringwald 
32489425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
32589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
32689425bfcSMilanka Ringwald }
32789425bfcSMilanka Ringwald 
32889425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
32989425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
33089425bfcSMilanka Ringwald }
33189425bfcSMilanka Ringwald 
33289425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
33389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
33489425bfcSMilanka Ringwald }
33589425bfcSMilanka Ringwald 
33689425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
33789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
33889425bfcSMilanka Ringwald }
33989425bfcSMilanka Ringwald 
34089425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
34189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
34289425bfcSMilanka Ringwald }
34389425bfcSMilanka Ringwald 
34489425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
34589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
34689425bfcSMilanka Ringwald }
34789425bfcSMilanka Ringwald 
34889425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
34989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
35089425bfcSMilanka Ringwald }
35189425bfcSMilanka Ringwald 
35289425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
35389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
35489425bfcSMilanka Ringwald }
35589425bfcSMilanka Ringwald 
35689425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
35789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
35889425bfcSMilanka Ringwald }
35989425bfcSMilanka Ringwald 
36089425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
36189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
36289425bfcSMilanka Ringwald }
36389425bfcSMilanka Ringwald 
36489425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
36589425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
36689425bfcSMilanka Ringwald }
36789425bfcSMilanka Ringwald 
36889425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
36989425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
37089425bfcSMilanka Ringwald }
37189425bfcSMilanka Ringwald 
372ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
37389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
374ce263fc8SMatthias Ringwald }
375ce263fc8SMatthias Ringwald 
376667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
37789425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
378667ec068SMatthias Ringwald }
379667ec068SMatthias Ringwald 
38076cc1527SMatthias Ringwald /* state machines */
3813deb3ec6SMatthias Ringwald 
382a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
383a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
384a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
385aa4dd815SMatthias Ringwald     int done = 1;
386498a8121SMilanka Ringwald     log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state);
387a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
3883deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
389d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
390a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
391a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
3923deb3ec6SMatthias Ringwald             break;
3933deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
394a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
395a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
3963deb3ec6SMatthias Ringwald             break;
3973deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
398a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
399a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4003deb3ec6SMatthias Ringwald             break;
4013deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
402a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
403a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4043deb3ec6SMatthias Ringwald             break;
4053deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
406a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
407a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4083deb3ec6SMatthias Ringwald             break;
4093deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
410a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
411a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4123deb3ec6SMatthias Ringwald             break;
4133deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
414a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
415a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4163deb3ec6SMatthias Ringwald             break;
4173deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
418a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
419a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4203deb3ec6SMatthias Ringwald             break;
4213deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
422a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
423a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4243deb3ec6SMatthias Ringwald             break;
4253deb3ec6SMatthias Ringwald         default:
426aa4dd815SMatthias Ringwald             done = 0;
4273deb3ec6SMatthias Ringwald             break;
4283deb3ec6SMatthias Ringwald     }
4293deb3ec6SMatthias Ringwald     return done;
4303deb3ec6SMatthias Ringwald }
4313deb3ec6SMatthias Ringwald 
432ce263fc8SMatthias Ringwald 
433a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
434a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
435498a8121SMilanka Ringwald     if (hfp_connection->ok_pending){
436498a8121SMilanka Ringwald         return 0;
437498a8121SMilanka Ringwald     }
438ce263fc8SMatthias Ringwald     int done = 0;
439a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
440a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
441ce263fc8SMatthias Ringwald         done = 1;
442a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
443ce263fc8SMatthias Ringwald         return done;
444ce263fc8SMatthias Ringwald     };
445a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
446a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
447ce263fc8SMatthias Ringwald         done = 1;
448a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
449a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
450a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
451ce263fc8SMatthias Ringwald         return done;
452ce263fc8SMatthias Ringwald     }
453ce263fc8SMatthias Ringwald 
454a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
455ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
456a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
457a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
458a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
459ce263fc8SMatthias Ringwald             return 1;
460ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
461a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
462a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
463a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
464ce263fc8SMatthias Ringwald             return 1;
465ce263fc8SMatthias Ringwald         default:
466ce263fc8SMatthias Ringwald             break;
467ce263fc8SMatthias Ringwald     }
468ce263fc8SMatthias Ringwald 
469a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
470a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
471ce263fc8SMatthias Ringwald         done = 1;
472a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
473ce263fc8SMatthias Ringwald         return done;
474ce263fc8SMatthias Ringwald     }
475ce263fc8SMatthias Ringwald 
476ce263fc8SMatthias Ringwald     return done;
477ce263fc8SMatthias Ringwald }
478ce263fc8SMatthias Ringwald 
479af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
480be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
481be55a11dSMilanka Ringwald         return 0;
482be55a11dSMilanka Ringwald     }
483be55a11dSMilanka Ringwald     int done = 0;
484fd4151d1SMilanka Ringwald 
4850b4debbfSMilanka Ringwald     if (hfp_connection->ok_pending == 1){
4860b4debbfSMilanka Ringwald         return 0;
4870b4debbfSMilanka Ringwald     }
4880b4debbfSMilanka Ringwald     // voice recognition activated from AG
4890b4debbfSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){
4900b4debbfSMilanka Ringwald         switch(hfp_connection->vra_state_requested){
4910b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
4920b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
493de9e0ea7SMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
4940b4debbfSMilanka Ringwald                 // ignore AG command, continue to wait for OK
4950b4debbfSMilanka Ringwald                 return 0;
496*cf75be85SMilanka Ringwald 
4970b4debbfSMilanka Ringwald             default:
498*cf75be85SMilanka Ringwald                 printf("status %d state %d\n", hfp_connection->ag_vra_status, hfp_connection->ag_vra_state);
499*cf75be85SMilanka Ringwald                 switch(hfp_connection->ag_vra_state){
500*cf75be85SMilanka Ringwald                     case HFP_VOICE_RECOGNITION_STATE_AG_READY:
501013cc750SMilanka Ringwald                         switch (hfp_connection->ag_vra_status){
502013cc750SMilanka Ringwald                             case 0:
5030b4debbfSMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
504013cc750SMilanka Ringwald                                 break;
505013cc750SMilanka Ringwald                             case 1:
506013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
507013cc750SMilanka Ringwald                                 break;
508013cc750SMilanka Ringwald                             case 2:
509013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
510013cc750SMilanka Ringwald                                 break;
511013cc750SMilanka Ringwald                             default:
512013cc750SMilanka Ringwald                                 break;
5130b4debbfSMilanka Ringwald                         }
5140b4debbfSMilanka Ringwald                         break;
515*cf75be85SMilanka Ringwald                     default:
516*cf75be85SMilanka Ringwald                         // state messages from AG
517*cf75be85SMilanka Ringwald                         hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
518*cf75be85SMilanka Ringwald                         break;
519*cf75be85SMilanka Ringwald                 }
520*cf75be85SMilanka Ringwald                 break;
5210b4debbfSMilanka Ringwald         }
5220b4debbfSMilanka Ringwald         hfp_connection->command = HFP_CMD_NONE;
5230b4debbfSMilanka Ringwald     }
5240b4debbfSMilanka Ringwald 
5250b4debbfSMilanka Ringwald 
526498a8121SMilanka Ringwald     switch (hfp_connection->vra_state_requested){
527fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
528fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
529fdda66c0SMilanka Ringwald             if (done != 0){
530fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
531498a8121SMilanka Ringwald                 hfp_connection->ok_pending = 1;
532498a8121SMilanka Ringwald             }
533fd4151d1SMilanka Ringwald             return 1;
534fd4151d1SMilanka Ringwald 
535fd4151d1SMilanka Ringwald 
536fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
537fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
538fdda66c0SMilanka Ringwald             if (done != 0){
539fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
540fd4151d1SMilanka Ringwald                 hfp_connection->ok_pending = 1;
541fd4151d1SMilanka Ringwald                 return 1;
5420b4debbfSMilanka Ringwald             }
5430b4debbfSMilanka Ringwald             break;
544013cc750SMilanka Ringwald 
545de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
546de9e0ea7SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
547de9e0ea7SMilanka Ringwald             if (done != 0){
548de9e0ea7SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
549de9e0ea7SMilanka Ringwald                 hfp_connection->ok_pending = 1;
550de9e0ea7SMilanka Ringwald                 return 1;
551de9e0ea7SMilanka Ringwald             }
552de9e0ea7SMilanka Ringwald             break;
553de9e0ea7SMilanka Ringwald 
554de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
555de9e0ea7SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
556de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
557de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
558de9e0ea7SMilanka Ringwald             if (hfp_connection->activate_voice_recognition){
559de9e0ea7SMilanka Ringwald                 hfp_hf_activate_voice_recognition(hfp_connection->acl_handle);
560de9e0ea7SMilanka Ringwald             } else {
561de9e0ea7SMilanka Ringwald                 hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
562de9e0ea7SMilanka Ringwald             }
563de9e0ea7SMilanka Ringwald             break;
564de9e0ea7SMilanka Ringwald 
565be55a11dSMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
566498a8121SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
567498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
568de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
569de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
570de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
571de9e0ea7SMilanka Ringwald             } else {
5721a26de69SMilanka Ringwald                 hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
573de9e0ea7SMilanka Ringwald             }
574be55a11dSMilanka Ringwald             break;
575be55a11dSMilanka Ringwald 
576de9e0ea7SMilanka Ringwald 
577013cc750SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
578013cc750SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
579498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
580de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
581de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
582de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
583de9e0ea7SMilanka Ringwald             } else {
584de9e0ea7SMilanka Ringwald                 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
585de9e0ea7SMilanka Ringwald             }
586be55a11dSMilanka Ringwald             break;
587fd4151d1SMilanka Ringwald 
588be55a11dSMilanka Ringwald         default:
589be55a11dSMilanka Ringwald             break;
590be55a11dSMilanka Ringwald     }
591be55a11dSMilanka Ringwald     return done;
592be55a11dSMilanka Ringwald }
593be55a11dSMilanka Ringwald 
594be55a11dSMilanka Ringwald 
595be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
596a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
597ce263fc8SMatthias Ringwald 
598332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
599332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
600ce263fc8SMatthias Ringwald 
601a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
602a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
603332ca98fSMatthias Ringwald 		return 1;
604332ca98fSMatthias Ringwald     }
605332ca98fSMatthias Ringwald 
6061cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
6071cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
608ce263fc8SMatthias Ringwald 
609a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
610fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
6111cc65c4fSMatthias Ringwald 		return 1;
6121cc65c4fSMatthias Ringwald     }
6131cc65c4fSMatthias Ringwald 
6141cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
6151cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
6161cc65c4fSMatthias Ringwald 
617a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
618a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
6191cc65c4fSMatthias Ringwald 		return 1;
6201cc65c4fSMatthias Ringwald     }
621ce263fc8SMatthias Ringwald 
622ce263fc8SMatthias Ringwald     return 0;
623ce263fc8SMatthias Ringwald }
624ce263fc8SMatthias Ringwald 
625a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
626505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
627505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
628ce263fc8SMatthias Ringwald 
62964f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
630a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
631a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
632a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
633ce263fc8SMatthias Ringwald         return 1;
634ce263fc8SMatthias Ringwald     }
635ce263fc8SMatthias Ringwald 
636a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
637ce263fc8SMatthias Ringwald 
638ce263fc8SMatthias Ringwald     // run codecs exchange
639a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
640ce263fc8SMatthias Ringwald     if (done) return 1;
641ce263fc8SMatthias Ringwald 
64238200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
64338200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
64438200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
64538200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
64638200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
64738200c1dSMilanka Ringwald         return 1;
64838200c1dSMilanka Ringwald     }
649ce263fc8SMatthias Ringwald     return 0;
650ce263fc8SMatthias Ringwald }
651ce263fc8SMatthias Ringwald 
65238200c1dSMilanka Ringwald 
653a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
654eaf2b0a1SMatthias Ringwald 
655eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
656eaf2b0a1SMatthias Ringwald 
657a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
658a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
659a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
660ce263fc8SMatthias Ringwald         return 1;
661ce263fc8SMatthias Ringwald     }
662ce263fc8SMatthias Ringwald     return 0;
663ce263fc8SMatthias Ringwald }
664ce263fc8SMatthias Ringwald 
6651c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
6667522e673SMatthias Ringwald 
66776cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
66876cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
66976cc1527SMatthias Ringwald 
67076cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
67176cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
67222387625SMatthias Ringwald 
6733721a235SMatthias Ringwald 	// assert command could be sent
6743721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
6753721a235SMatthias Ringwald 
6763721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
6773721a235SMatthias Ringwald     // WBS Disassociate
6783721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
6793721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
6803721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
6813721a235SMatthias Ringwald         return;
6823721a235SMatthias Ringwald     }
6833721a235SMatthias Ringwald     // Write Codec Config
6843721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
6853721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
6863721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
6873721a235SMatthias Ringwald         return;
6883721a235SMatthias Ringwald     }
6893721a235SMatthias Ringwald     // WBS Associate
6903721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
6913721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
6923721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
6933721a235SMatthias Ringwald         return;
6943721a235SMatthias Ringwald     }
6953721a235SMatthias Ringwald #endif
696689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
697689d4323SMatthias Ringwald     // Enable WBS
698689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
699689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
700689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
701689d4323SMatthias Ringwald         return;
702689d4323SMatthias Ringwald     }
703689d4323SMatthias Ringwald     // Write I2S/PCM params
704689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
705689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
706689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
707689d4323SMatthias Ringwald         return;
708689d4323SMatthias Ringwald     }
709689d4323SMatthias Ringwald     // Disable WBS
710689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
711689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
712689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
713689d4323SMatthias Ringwald         return;
714689d4323SMatthias Ringwald     }
715689d4323SMatthias Ringwald #endif
71648e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
71748e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
71848e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
71948e6eeeeSMatthias Ringwald         return;
72048e6eeeeSMatthias Ringwald     }
72148e6eeeeSMatthias Ringwald #endif
7223721a235SMatthias Ringwald 
723cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
724cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
725cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
7267522e673SMatthias Ringwald         // notify about codec selection if not done already
7277522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
7287522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
7297522e673SMatthias Ringwald         }
730cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
7317522e673SMatthias Ringwald         return;
7327522e673SMatthias Ringwald     }
7337522e673SMatthias Ringwald 
734d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
735d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
736d4dd47ffSMatthias Ringwald         return;
737d4dd47ffSMatthias Ringwald     }
738a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
739ce263fc8SMatthias Ringwald     if (!done){
740a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
741ce263fc8SMatthias Ringwald     }
742ce263fc8SMatthias Ringwald     if (!done){
743af97579eSMilanka Ringwald         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
744be55a11dSMilanka Ringwald     }
745be55a11dSMilanka Ringwald     if (!done){
746a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
747ce263fc8SMatthias Ringwald     }
748ce263fc8SMatthias Ringwald     if (!done){
749a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
750ce263fc8SMatthias Ringwald     }
751ce263fc8SMatthias Ringwald 
7521016a228SMatthias Ringwald     // don't send a new command while ok still pending
7531016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
7541016a228SMatthias Ringwald 
755a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
756a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
757a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
758a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
759ce263fc8SMatthias Ringwald         return;
760ce263fc8SMatthias Ringwald     }
761ce263fc8SMatthias Ringwald 
762a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
763a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
764a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
765a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
766ce263fc8SMatthias Ringwald         return;
767ce263fc8SMatthias Ringwald     }
768ce263fc8SMatthias Ringwald 
769a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
770a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
771a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
772a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
773ce263fc8SMatthias Ringwald         return;
774ce263fc8SMatthias Ringwald     }
775ce263fc8SMatthias Ringwald 
776a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
777a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
778a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
779a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
780ce263fc8SMatthias Ringwald         return;
781ce263fc8SMatthias Ringwald     }
782ce263fc8SMatthias Ringwald 
783a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
784a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
785a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
786a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
787ce263fc8SMatthias Ringwald         return;
788ce263fc8SMatthias Ringwald     }
789ce263fc8SMatthias Ringwald 
790a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
791a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
792a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
793a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
794ce263fc8SMatthias Ringwald         return;
795ce263fc8SMatthias Ringwald     }
796ce263fc8SMatthias Ringwald 
797a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
798a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
799a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
800a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
801ce263fc8SMatthias Ringwald         return;
802ce263fc8SMatthias Ringwald     }
803ce263fc8SMatthias Ringwald 
804a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
805a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
806a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
807a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
808ce263fc8SMatthias Ringwald         return;
809ce263fc8SMatthias Ringwald     }
810ce263fc8SMatthias Ringwald 
811a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
812a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
813a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
814a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
815ce263fc8SMatthias Ringwald         return;
816ce263fc8SMatthias Ringwald     }
817ce263fc8SMatthias Ringwald 
818a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
819a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
820a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
821a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
822ce263fc8SMatthias Ringwald         return;
823ce263fc8SMatthias Ringwald     }
824ce263fc8SMatthias Ringwald 
825a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
826a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
827a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
828a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
829ce263fc8SMatthias Ringwald         return;
830ce263fc8SMatthias Ringwald     }
831ce263fc8SMatthias Ringwald 
832a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
833a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
834a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
835a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
836ce263fc8SMatthias Ringwald         return;
837ce263fc8SMatthias Ringwald     }
838ce263fc8SMatthias Ringwald 
839a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
840a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
841a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
842a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
843ce263fc8SMatthias Ringwald         return;
844ce263fc8SMatthias Ringwald     }
845ce263fc8SMatthias Ringwald 
846a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
847a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
848a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
849a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
850ce263fc8SMatthias Ringwald         return;
851ce263fc8SMatthias Ringwald     }
852ce263fc8SMatthias Ringwald 
853a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
854a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
855a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
856a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
857ce263fc8SMatthias Ringwald         return;
858ce263fc8SMatthias Ringwald     }
859ce263fc8SMatthias Ringwald 
860a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
861a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
862a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
863a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
864ce263fc8SMatthias Ringwald         return;
865ce263fc8SMatthias Ringwald     }
866ce263fc8SMatthias Ringwald 
867a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
868a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
869a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
870a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
871ce263fc8SMatthias Ringwald         return;
872ce263fc8SMatthias Ringwald     }
873ce263fc8SMatthias Ringwald 
874a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
875a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
876a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
877a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
878667ec068SMatthias Ringwald         return;
879667ec068SMatthias Ringwald     }
880667ec068SMatthias Ringwald 
881a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
882a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
883a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
884a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
885a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
886ce263fc8SMatthias Ringwald         return;
887ce263fc8SMatthias Ringwald     }
888ce263fc8SMatthias Ringwald 
889a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
890a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
891a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
892a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
893ce263fc8SMatthias Ringwald         return;
894ce263fc8SMatthias Ringwald     }
895ce263fc8SMatthias Ringwald 
896a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
897a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
898a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
899a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
900667ec068SMatthias Ringwald         return;
901667ec068SMatthias Ringwald     }
902667ec068SMatthias Ringwald 
903a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
904a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
905667ec068SMatthias Ringwald         char buffer[20];
906a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
907667ec068SMatthias Ringwald             case '?':
9081599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
909ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
910ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
911a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
912667ec068SMatthias Ringwald                 return;
913667ec068SMatthias Ringwald             case '0':
914667ec068SMatthias Ringwald             case '1':
915667ec068SMatthias Ringwald             case '2':
9161599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
917ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
918ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
919ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
920a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
921667ec068SMatthias Ringwald                 return;
922667ec068SMatthias Ringwald             default:
923667ec068SMatthias Ringwald                 break;
924667ec068SMatthias Ringwald         }
925667ec068SMatthias Ringwald         return;
926667ec068SMatthias Ringwald     }
927667ec068SMatthias Ringwald 
928a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
929a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
930667ec068SMatthias Ringwald         char buffer[20];
9311599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
932ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
933ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
934a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
935667ec068SMatthias Ringwald         return;
936667ec068SMatthias Ringwald     }
937667ec068SMatthias Ringwald 
938667ec068SMatthias Ringwald     // update HF indicators
939a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
940667ec068SMatthias Ringwald         int i;
941667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
942a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
943a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
944a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
945a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
946667ec068SMatthias Ringwald                     char buffer[30];
9471599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
948ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
949ff7d6aeaSMatthias Ringwald                              hfp_indicators[i],
950ff7d6aeaSMatthias Ringwald                              (unsigned int)hfp_indicators_value[i]);
951ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
952a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
953667ec068SMatthias Ringwald                 } else {
95460ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
955667ec068SMatthias Ringwald                 }
956667ec068SMatthias Ringwald                 return;
957667ec068SMatthias Ringwald             }
958667ec068SMatthias Ringwald         }
959667ec068SMatthias Ringwald     }
960667ec068SMatthias Ringwald 
961ce263fc8SMatthias Ringwald     if (done) return;
962ce263fc8SMatthias Ringwald     // deal with disconnect
963a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
964ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
965a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
966a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
967ce263fc8SMatthias Ringwald             break;
968ce263fc8SMatthias Ringwald 
969ce263fc8SMatthias Ringwald         default:
970ce263fc8SMatthias Ringwald             break;
971ce263fc8SMatthias Ringwald     }
972ce263fc8SMatthias Ringwald }
973ce263fc8SMatthias Ringwald 
974a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
975a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
9766a7f44bdSMilanka Ringwald 
977ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
9787522e673SMatthias Ringwald 
979667ec068SMatthias Ringwald     // restore volume settings
980a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
981a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
982ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
983a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
984a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
985ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
986667ec068SMatthias Ringwald     // enable all indicators
987667ec068SMatthias Ringwald     int i;
988667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
989a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
990a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
991667ec068SMatthias Ringwald     }
992ce263fc8SMatthias Ringwald }
993ce263fc8SMatthias Ringwald 
9941cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
9951cc65c4fSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
9961cc65c4fSMatthias Ringwald 		// Codec supported, confirm
9971cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
9981cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
9991cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
10001cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
10011cc65c4fSMatthias Ringwald 
10021cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
10031cc65c4fSMatthias Ringwald 	} else {
10041cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
10051cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
10061cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
10071cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
10081cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10091cc65c4fSMatthias Ringwald 
10101cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
10111cc65c4fSMatthias Ringwald 	}
10121cc65c4fSMatthias Ringwald }
10131cc65c4fSMatthias Ringwald 
1014a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
1015a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
10163deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
1017a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
1018a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
10193deb3ec6SMatthias Ringwald                 break;
10203deb3ec6SMatthias Ringwald             }
1021a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
10223deb3ec6SMatthias Ringwald             break;
10233deb3ec6SMatthias Ringwald 
10243deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
1025a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
10263deb3ec6SMatthias Ringwald             break;
10273deb3ec6SMatthias Ringwald 
10283deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
1029a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
10303deb3ec6SMatthias Ringwald             break;
10313deb3ec6SMatthias Ringwald 
10323deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1033a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
10343deb3ec6SMatthias Ringwald             break;
10353deb3ec6SMatthias Ringwald 
10363deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1037a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1038a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
10393deb3ec6SMatthias Ringwald                 break;
10403deb3ec6SMatthias Ringwald             }
1041a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
1042a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
10433deb3ec6SMatthias Ringwald                 break;
10443deb3ec6SMatthias Ringwald             }
1045a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
10463deb3ec6SMatthias Ringwald             break;
10473deb3ec6SMatthias Ringwald 
10483deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1049a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
1050a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
10513deb3ec6SMatthias Ringwald                 break;
10523deb3ec6SMatthias Ringwald             }
1053a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
10543deb3ec6SMatthias Ringwald             break;
10553deb3ec6SMatthias Ringwald 
10563deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1057a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
10583deb3ec6SMatthias Ringwald             break;
10593deb3ec6SMatthias Ringwald 
10603deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1061a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
10623deb3ec6SMatthias Ringwald             break;
10633deb3ec6SMatthias Ringwald 
10643deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1065a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
10663deb3ec6SMatthias Ringwald             break;
1067ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1068a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1069a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1070ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1071ce263fc8SMatthias Ringwald                 break;
1072ce263fc8SMatthias Ringwald             }
10733deb3ec6SMatthias Ringwald 
1074a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1075a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1076ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1077ce263fc8SMatthias Ringwald                 break;
10783deb3ec6SMatthias Ringwald             }
10793deb3ec6SMatthias Ringwald 
1080a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
1081ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1082a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1083ce263fc8SMatthias Ringwald                     break;
1084ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1085a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1086a473a009SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_connection);
1087ce263fc8SMatthias Ringwald                     break;
1088ce263fc8SMatthias Ringwald                 default:
1089ce263fc8SMatthias Ringwald                     break;
10903deb3ec6SMatthias Ringwald             }
1091ce263fc8SMatthias Ringwald 
1092a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
1093a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
1094ce263fc8SMatthias Ringwald                 break;
10953deb3ec6SMatthias Ringwald             }
10963deb3ec6SMatthias Ringwald 
1097a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
1098aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1099a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
11003deb3ec6SMatthias Ringwald                     break;
1101ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
1102a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1103ce263fc8SMatthias Ringwald                     break;
11043deb3ec6SMatthias Ringwald                 default:
11053deb3ec6SMatthias Ringwald                     break;
11063deb3ec6SMatthias Ringwald             }
1107af97579eSMilanka Ringwald             hfp_hf_voice_recognition_state_machine(hfp_connection);
1108be55a11dSMilanka Ringwald             break;
1109be55a11dSMilanka Ringwald         case HFP_AUDIO_CONNECTION_ESTABLISHED:
1110af97579eSMilanka Ringwald             hfp_hf_voice_recognition_state_machine(hfp_connection);
11113deb3ec6SMatthias Ringwald             break;
11123deb3ec6SMatthias Ringwald         default:
11133deb3ec6SMatthias Ringwald             break;
11143deb3ec6SMatthias Ringwald     }
11153deb3ec6SMatthias Ringwald 
11163deb3ec6SMatthias Ringwald     // done
1117be55a11dSMilanka Ringwald     hfp_connection->ok_pending = 0;
1118a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
11193deb3ec6SMatthias Ringwald }
11203deb3ec6SMatthias Ringwald 
1121be55a11dSMilanka Ringwald 
1122b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
11234562e2a2SMatthias Ringwald     uint16_t i;
11244562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
11254562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
11264562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
11274562e2a2SMatthias Ringwald                 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
11284562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
11294562e2a2SMatthias Ringwald                 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
11304562e2a2SMatthias Ringwald                 // avoid set but not used warning
11314562e2a2SMatthias Ringwald                 (void) hfp_callheld_status;
11324562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
11334562e2a2SMatthias Ringwald                 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
11344562e2a2SMatthias Ringwald             }
11354562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1136a473a009SMatthias Ringwald             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
11374562e2a2SMatthias Ringwald             break;
11384562e2a2SMatthias Ringwald         }
11394562e2a2SMatthias Ringwald     }
11404562e2a2SMatthias Ringwald }
11414562e2a2SMatthias Ringwald 
1142426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1143186dd3d2SMatthias Ringwald     int value;
1144186dd3d2SMatthias Ringwald     int i;
1145a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1146667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1147a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1148a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1149667ec068SMatthias Ringwald             break;
1150667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1151a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1152ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1153667ec068SMatthias Ringwald             break;
1154667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1155a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1156a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1157667ec068SMatthias Ringwald             break;
1158ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1159a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11602308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1161667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1162ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1163ce263fc8SMatthias Ringwald             break;
1164ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1165a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11662308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1167667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1168ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1169ce263fc8SMatthias Ringwald             break;
1170ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1171a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1172ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1173a0ffb263SMatthias Ringwald             break;
1174a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1175a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1176a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1177a0ffb263SMatthias Ringwald             break;
1178a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1179a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1180a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1181ce263fc8SMatthias Ringwald             break;
1182ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1183a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11845a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1185a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1186ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1187ce263fc8SMatthias Ringwald             break;
11880b4debbfSMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
11890b4debbfSMilanka Ringwald             break;
1190fdda66c0SMilanka Ringwald         case HFP_CMD_ERROR:
119190244c92SMilanka Ringwald             switch (hfp_connection->state){
119290244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
119390244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
119490244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1195fdda66c0SMilanka Ringwald                             hfp_reset_context_flags(hfp_connection);
119690244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
119790244c92SMilanka Ringwald                             return;
119890244c92SMilanka Ringwald                         default:
119990244c92SMilanka Ringwald                             break;
120090244c92SMilanka Ringwald                     }
120156f1adacSMilanka Ringwald                     break;
120256f1adacSMilanka Ringwald                 default:
120356f1adacSMilanka Ringwald                     break;
120456f1adacSMilanka Ringwald             }
1205fdda66c0SMilanka Ringwald             // handle error response for voice activation (HF initiated)
12060b4debbfSMilanka Ringwald             switch(hfp_connection->vra_state_requested){
1207de9e0ea7SMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1208de9e0ea7SMilanka Ringwald                     hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1209be55a11dSMilanka Ringwald                     break;
1210be55a11dSMilanka Ringwald                 default:
12110b4debbfSMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
12121a26de69SMilanka Ringwald                     hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
12130b4debbfSMilanka Ringwald                     hfp_reset_context_flags(hfp_connection);
12140b4debbfSMilanka Ringwald                     return;
1215be55a11dSMilanka Ringwald             }
12160b4debbfSMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1217fdda66c0SMilanka Ringwald             hfp_reset_context_flags(hfp_connection);
1218ce263fc8SMatthias Ringwald             break;
1219fdda66c0SMilanka Ringwald 
1220ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1221a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1222ce263fc8SMatthias Ringwald             break;
1223ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
12245a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1225ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1226ce263fc8SMatthias Ringwald             break;
1227ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
12285a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12294562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1230ce263fc8SMatthias Ringwald             break;
1231c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
12325a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1233c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1234a473a009SMatthias Ringwald                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1235c741b032SMilanka Ringwald             }
1236c741b032SMilanka Ringwald             break;
12371cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
12381cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12395a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
12401cc65c4fSMatthias Ringwald 			break;
1241eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1242eac56539SMilanka 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));
1243ce263fc8SMatthias Ringwald         default:
1244ce263fc8SMatthias Ringwald             break;
12453deb3ec6SMatthias Ringwald     }
12460cef86faSMatthias Ringwald }
1247426f9988SMatthias Ringwald 
124876cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
124976cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
125076cc1527SMatthias Ringwald }
125176cc1527SMatthias Ringwald 
12520b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1253426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1254426f9988SMatthias Ringwald     if (size < 1) return;
1255426f9988SMatthias Ringwald 
1256426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1257e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1258e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1259e43d1938SMatthias Ringwald #endif
1260426f9988SMatthias Ringwald 
1261426f9988SMatthias Ringwald     // process messages byte-wise
1262426f9988SMatthias Ringwald     int pos;
1263426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1264426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
12651599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1266426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1267426f9988SMatthias Ringwald     }
12680b4debbfSMilanka Ringwald     hfp_hf_handle_rfcomm_command(hfp_connection);
12693deb3ec6SMatthias Ringwald }
12703deb3ec6SMatthias Ringwald 
12711c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1272665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1273665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1274665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1275a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
127622387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
12771c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
12783deb3ec6SMatthias Ringwald     }
12793deb3ec6SMatthias Ringwald }
12803deb3ec6SMatthias Ringwald 
12811c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
12820b4debbfSMilanka Ringwald     hfp_connection_t * hfp_connection;
12833deb3ec6SMatthias Ringwald     switch (packet_type){
12843deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
12850b4debbfSMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
12860b4debbfSMilanka Ringwald             if (!hfp_connection) return;
12870b4debbfSMilanka Ringwald             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
12880b4debbfSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
12890b4debbfSMilanka Ringwald             return;
12903deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1291d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1292d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
12931c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1294d4dd47ffSMatthias Ringwald                 return;
1295d4dd47ffSMatthias Ringwald             }
129627950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1297202c8a4cSMatthias Ringwald             break;
12983deb3ec6SMatthias Ringwald         default:
12993deb3ec6SMatthias Ringwald             break;
13003deb3ec6SMatthias Ringwald     }
13011c6a0fc0SMatthias Ringwald     hfp_hf_run();
13023deb3ec6SMatthias Ringwald }
13033deb3ec6SMatthias Ringwald 
13041c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1305405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
13061c6a0fc0SMatthias Ringwald     hfp_hf_run();
1307405014fbSMatthias Ringwald }
1308405014fbSMatthias Ringwald 
1309b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1310b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1311b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1312b4df8028SMilanka Ringwald         return status;
1313b4df8028SMilanka Ringwald     }
1314b4df8028SMilanka Ringwald 
1315520c92d5SMatthias Ringwald     hfp_init();
131620b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
131720b2edb6SMatthias Ringwald     hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
131820b2edb6SMatthias Ringwald     hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
131920b2edb6SMatthias Ringwald     hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
132020b2edb6SMatthias Ringwald     hfp_codecs_nr = 0;
132120b2edb6SMatthias Ringwald     hfp_hf_speaker_gain = 9;
132220b2edb6SMatthias Ringwald     hfp_hf_microphone_gain = 9;
132320b2edb6SMatthias Ringwald     hfp_indicators_nr = 0;
132420b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1325d63c37a1SMatthias Ringwald 
13261c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
13271c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
132827950165SMatthias Ringwald 
132927950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
13301c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1331b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
133220b2edb6SMatthias Ringwald }
133327950165SMatthias Ringwald 
133420b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
133520b2edb6SMatthias Ringwald     hfp_deinit();
133620b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
133720b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t));
133820b2edb6SMatthias Ringwald     (void) memset(phone_number, 0, sizeof(phone_number));
1339a0ffb263SMatthias Ringwald }
1340a0ffb263SMatthias Ringwald 
13417ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
134268466199SMilanka Ringwald     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
13433deb3ec6SMatthias Ringwald 
13443deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
13453deb3ec6SMatthias Ringwald     int i;
13463deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
13473deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
13483deb3ec6SMatthias Ringwald     }
13493deb3ec6SMatthias Ringwald }
13503deb3ec6SMatthias Ringwald 
1351a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
13523deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1353a0ffb263SMatthias Ringwald }
13543deb3ec6SMatthias Ringwald 
13557ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
135668466199SMilanka Ringwald     btstack_assert(hfp_indicators_nr < HFP_MAX_NUM_INDICATORS);
135768466199SMilanka Ringwald 
13583deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
13593deb3ec6SMatthias Ringwald     int i;
1360a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
13613deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
13623deb3ec6SMatthias Ringwald     }
13633deb3ec6SMatthias Ringwald }
13643deb3ec6SMatthias Ringwald 
13654eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
13664eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
13673deb3ec6SMatthias Ringwald }
13683deb3ec6SMatthias Ringwald 
1369657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
13709c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1371a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1372657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1373a33eb0c4SMilanka Ringwald     }
13741ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
13751c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1376657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
13773deb3ec6SMatthias Ringwald }
13783deb3ec6SMatthias Ringwald 
13793c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
13809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1381a0ffb263SMatthias Ringwald     if (!hfp_connection) {
13823c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
13833deb3ec6SMatthias Ringwald     }
1384a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
13851c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13863c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
13873deb3ec6SMatthias Ringwald }
13883deb3ec6SMatthias Ringwald 
13893c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
13903c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1391ce263fc8SMatthias Ringwald }
1392ce263fc8SMatthias Ringwald 
13933c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
13943c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1395ce263fc8SMatthias Ringwald }
1396ce263fc8SMatthias Ringwald 
13973deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
13983c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
13999c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1400a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14013c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14023deb3ec6SMatthias Ringwald     }
1403a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1404a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
14051c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14063c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14073deb3ec6SMatthias Ringwald }
14083deb3ec6SMatthias Ringwald 
14093c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
14109c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1411a0ffb263SMatthias Ringwald     if (!hfp_connection) {
14123c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
14133deb3ec6SMatthias Ringwald     }
14143c65e705SMilanka Ringwald 
1415a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1416ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1417a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1418ce263fc8SMatthias Ringwald             break;
1419ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1420a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1421ce263fc8SMatthias Ringwald             break;
1422ce263fc8SMatthias Ringwald         default:
14233c65e705SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1424ce263fc8SMatthias Ringwald     }
14251c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14263c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14273deb3ec6SMatthias Ringwald }
14283deb3ec6SMatthias Ringwald 
14293c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
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->enable_extended_audio_gateway_error_report = enable;
14351c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14363c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14373deb3ec6SMatthias Ringwald }
14383deb3ec6SMatthias Ringwald 
1439ce263fc8SMatthias Ringwald 
14403c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
14413c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1442ce263fc8SMatthias Ringwald }
1443ce263fc8SMatthias Ringwald 
14443c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
14453c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1446ce263fc8SMatthias Ringwald }
1447ce263fc8SMatthias Ringwald 
144838200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
144938200c1dSMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
145038200c1dSMilanka Ringwald }
1451ce263fc8SMatthias Ringwald 
14523c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
14539c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1454a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14553c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1456a33eb0c4SMilanka Ringwald     }
1457ce263fc8SMatthias Ringwald 
14583c65e705SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
14593c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
14603c65e705SMilanka Ringwald     }
14613c65e705SMilanka Ringwald 
14623c65e705SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
14633c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
14643c65e705SMilanka Ringwald     }
1465f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1466a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1467aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1468aa4dd815SMatthias Ringwald                 break;
1469ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1470ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1471ec3bfc1aSMatthias Ringwald                 break;
1472aa4dd815SMatthias Ringwald             default:
14731cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
14741cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
14751cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
14761cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
147738200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1478aa4dd815SMatthias Ringwald                 break;
14793deb3ec6SMatthias Ringwald         }
1480f4412093SMatthias Ringwald     } else {
1481f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1482f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1483f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1484f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1485f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1486f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1487f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1488f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1489ce263fc8SMatthias Ringwald     }
1490ce263fc8SMatthias Ringwald 
14911c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14923c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
14933deb3ec6SMatthias Ringwald }
14943deb3ec6SMatthias Ringwald 
14953c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
14969c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1497a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
14983c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1499a33eb0c4SMilanka Ringwald     }
15000b4debbfSMilanka Ringwald     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
15010b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
15020b4debbfSMilanka Ringwald     }
15030b4debbfSMilanka Ringwald     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
15040b4debbfSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
15051c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
15060b4debbfSMilanka Ringwald     }
15073c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15083deb3ec6SMatthias Ringwald }
15093deb3ec6SMatthias Ringwald 
15103c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
15119c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1512a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15133c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1514a33eb0c4SMilanka Ringwald     }
1515ce263fc8SMatthias Ringwald 
1516ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1517a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
15181c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1519ce263fc8SMatthias Ringwald     } else {
1520ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
15213c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1522ce263fc8SMatthias Ringwald     }
15233c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1524ce263fc8SMatthias Ringwald }
1525ce263fc8SMatthias Ringwald 
15263c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(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     }
1531a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
15321c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15333c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1534ce263fc8SMatthias Ringwald }
1535ce263fc8SMatthias Ringwald 
15363c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
15379c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1538a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15393c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1540a33eb0c4SMilanka Ringwald     }
1541ce263fc8SMatthias Ringwald 
1542ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1543a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
15441c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1545ce263fc8SMatthias Ringwald     }
15463c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1547ce263fc8SMatthias Ringwald }
1548ce263fc8SMatthias Ringwald 
15493c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){
15509c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1551a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15523c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1553a33eb0c4SMilanka Ringwald     }
1554ce263fc8SMatthias Ringwald 
1555ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1556a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
15571c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1558ce263fc8SMatthias Ringwald     }
15593c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1560ce263fc8SMatthias Ringwald }
1561ce263fc8SMatthias Ringwald 
15623c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
15639c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1564a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15653c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1566a33eb0c4SMilanka Ringwald     }
1567ce263fc8SMatthias Ringwald 
1568505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1569505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1570a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
15711c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1572ce263fc8SMatthias Ringwald     }
15733c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1574ce263fc8SMatthias Ringwald }
1575ce263fc8SMatthias Ringwald 
15763c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
15779c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1578a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15793c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1580a33eb0c4SMilanka Ringwald     }
1581ce263fc8SMatthias Ringwald 
1582505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1583505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1584a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
15851c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1586ce263fc8SMatthias Ringwald     }
15873c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1588ce263fc8SMatthias Ringwald }
1589ce263fc8SMatthias Ringwald 
15903c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
15919c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1592a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15933c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1594a33eb0c4SMilanka Ringwald     }
1595ce263fc8SMatthias Ringwald 
1596505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1597505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1598a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
15991c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1600ce263fc8SMatthias Ringwald     }
16013c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1602ce263fc8SMatthias Ringwald }
1603ce263fc8SMatthias Ringwald 
16043c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
16059c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1606a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16073c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1608a33eb0c4SMilanka Ringwald     }
1609ce263fc8SMatthias Ringwald 
1610505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1611505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1612a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
16131c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1614ce263fc8SMatthias Ringwald     }
16153c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1616ce263fc8SMatthias Ringwald }
1617ce263fc8SMatthias Ringwald 
16183c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
16199c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1620a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16213c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1622a33eb0c4SMilanka Ringwald     }
1623667ec068SMatthias Ringwald 
1624505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1625505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1626a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1627a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
16281c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1629667ec068SMatthias Ringwald     }
16303c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1631667ec068SMatthias Ringwald }
1632667ec068SMatthias Ringwald 
16333c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
16349c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1635a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16363c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1637a33eb0c4SMilanka Ringwald     }
1638667ec068SMatthias Ringwald 
1639505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1640505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1641a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1642a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
16431c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1644667ec068SMatthias Ringwald     }
16453c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1646667ec068SMatthias Ringwald }
1647ce263fc8SMatthias Ringwald 
16483c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
16499c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1650a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16513c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1652a33eb0c4SMilanka Ringwald     }
1653ce263fc8SMatthias Ringwald 
1654a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1655ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
16561c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16573c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1658ce263fc8SMatthias Ringwald }
1659ce263fc8SMatthias Ringwald 
16603c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
16619c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1662a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16633c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1664a33eb0c4SMilanka Ringwald     }
1665ce263fc8SMatthias Ringwald 
1666a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1667a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1668a0ffb263SMatthias Ringwald 
16691c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16703c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1671ce263fc8SMatthias Ringwald }
1672ce263fc8SMatthias Ringwald 
16733c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
16749c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1675a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16763c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1677a33eb0c4SMilanka Ringwald     }
1678ce263fc8SMatthias Ringwald 
1679a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
16801c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16813c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1682ce263fc8SMatthias Ringwald }
1683ce263fc8SMatthias Ringwald 
16843c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
16859c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1686a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16873c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1688a33eb0c4SMilanka Ringwald     }
1689ce263fc8SMatthias Ringwald 
1690a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
16911c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16923c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1693ce263fc8SMatthias Ringwald }
1694ce263fc8SMatthias Ringwald 
1695ce263fc8SMatthias Ringwald 
16963c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
16979c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1698a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16993c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1700a33eb0c4SMilanka Ringwald     }
1701ce263fc8SMatthias Ringwald 
1702a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
17031c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17043c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1705ce263fc8SMatthias Ringwald }
1706ce263fc8SMatthias Ringwald 
1707ce263fc8SMatthias Ringwald 
17083c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
17099c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1710a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17113c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1712a33eb0c4SMilanka Ringwald     }
1713ce263fc8SMatthias Ringwald 
1714a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
17151c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17163c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1717ce263fc8SMatthias Ringwald }
1718ce263fc8SMatthias Ringwald 
17193c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
17209c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1721a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17223c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1723a33eb0c4SMilanka Ringwald     }
1724ce263fc8SMatthias Ringwald 
1725a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
17261c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17273c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1728ce263fc8SMatthias Ringwald }
1729ce263fc8SMatthias Ringwald 
1730ce263fc8SMatthias Ringwald 
17313c65e705SMilanka Ringwald uint8_t hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
17329c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1733a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17343c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1735a33eb0c4SMilanka Ringwald     }
1736ce263fc8SMatthias Ringwald 
1737a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
17381c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17393c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1740ce263fc8SMatthias Ringwald }
1741ce263fc8SMatthias Ringwald 
17423c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
17439c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1744a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17453c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1746a33eb0c4SMilanka Ringwald     }
1747ce263fc8SMatthias Ringwald 
1748a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
17491c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
17503c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1751ce263fc8SMatthias Ringwald }
1752ce263fc8SMatthias Ringwald 
1753013cc750SMilanka Ringwald static bool hfp_hf_voice_recognition_supported(hfp_connection_t * hfp_connection, bool enhanced){
1754013cc750SMilanka Ringwald     uint8_t hf_vra_flag = HFP_HFSF_VOICE_RECOGNITION_FUNCTION;
1755013cc750SMilanka Ringwald     uint8_t ag_vra_flag = HFP_AGSF_VOICE_RECOGNITION_FUNCTION;
1756013cc750SMilanka Ringwald 
1757013cc750SMilanka Ringwald     if (enhanced){
1758013cc750SMilanka Ringwald         hf_vra_flag = HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS;
1759013cc750SMilanka Ringwald         ag_vra_flag = HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS;
1760013cc750SMilanka Ringwald     }
1761013cc750SMilanka Ringwald 
1762013cc750SMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, ag_vra_flag);
1763013cc750SMilanka Ringwald     int hf = get_bit(hfp_supported_features, hf_vra_flag);
1764be55a11dSMilanka Ringwald     return hf && ag;
1765be55a11dSMilanka Ringwald }
1766be55a11dSMilanka Ringwald 
1767013cc750SMilanka Ringwald static uint8_t activate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){
1768fdda66c0SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1769fdda66c0SMilanka Ringwald     if (!hfp_connection) {
1770fdda66c0SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1771be55a11dSMilanka Ringwald     }
1772013cc750SMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1773013cc750SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1774013cc750SMilanka Ringwald     }
1775013cc750SMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection, enhanced)){
1776af97579eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1777af97579eSMilanka Ringwald     }
1778af97579eSMilanka Ringwald 
1779498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1780be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1781de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1782498a8121SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1783fd4151d1SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
17846b8275b0SMilanka Ringwald             hfp_connection->enhanced_voice_recognition_enabled = enhanced;
1785be55a11dSMilanka Ringwald             break;
1786de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1787de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = true;
1788de9e0ea7SMilanka Ringwald             break;
1789be55a11dSMilanka Ringwald         default:
1790be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1791be55a11dSMilanka Ringwald     }
1792ce263fc8SMatthias Ringwald 
1793af97579eSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1794fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1795af97579eSMilanka Ringwald }
1796af97579eSMilanka Ringwald 
1797013cc750SMilanka Ringwald 
1798013cc750SMilanka Ringwald static uint8_t deactivate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){
1799af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1800af97579eSMilanka Ringwald     if (!hfp_connection) {
1801af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1802af97579eSMilanka Ringwald     }
1803de9e0ea7SMilanka Ringwald     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
180408a0b01cSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
180508a0b01cSMilanka Ringwald     }
1806013cc750SMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection, enhanced)){
1807fdda66c0SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1808af97579eSMilanka Ringwald     }
1809013cc750SMilanka Ringwald 
1810fdda66c0SMilanka Ringwald     switch (hfp_connection->vra_state){
1811de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
1812fdda66c0SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1813de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1814de9e0ea7SMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1815fdda66c0SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1816fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1817fdda66c0SMilanka Ringwald             break;
1818de9e0ea7SMilanka Ringwald 
1819de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1820de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1821de9e0ea7SMilanka Ringwald             hfp_connection->deactivate_voice_recognition = true;
1822de9e0ea7SMilanka Ringwald             break;
1823de9e0ea7SMilanka Ringwald 
1824de9e0ea7SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1825de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1826de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1827fdda66c0SMilanka Ringwald         default:
1828fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1829fdda66c0SMilanka Ringwald     }
1830fdda66c0SMilanka Ringwald 
1831fdda66c0SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1832fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1833af97579eSMilanka Ringwald }
1834af97579eSMilanka Ringwald 
1835013cc750SMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1836013cc750SMilanka Ringwald     return activate_voice_recognition(acl_handle, false);
1837013cc750SMilanka Ringwald }
1838013cc750SMilanka Ringwald 
1839e83c2025SMilanka Ringwald uint8_t hfp_hf_activate_enhanced_voice_recognition(hci_con_handle_t acl_handle){
1840013cc750SMilanka Ringwald     return activate_voice_recognition(acl_handle, true);
1841be55a11dSMilanka Ringwald }
1842fdda66c0SMilanka Ringwald 
1843de9e0ea7SMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
1844de9e0ea7SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1845de9e0ea7SMilanka Ringwald     if (!hfp_connection) {
1846de9e0ea7SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1847de9e0ea7SMilanka Ringwald     }
1848de9e0ea7SMilanka Ringwald     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
1849de9e0ea7SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1850de9e0ea7SMilanka Ringwald     }
1851de9e0ea7SMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection, true)){
1852de9e0ea7SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1853de9e0ea7SMilanka Ringwald     }
1854de9e0ea7SMilanka Ringwald 
1855de9e0ea7SMilanka Ringwald     switch (hfp_connection->vra_state){
1856de9e0ea7SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1857de9e0ea7SMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1858de9e0ea7SMilanka Ringwald             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1859de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
1860de9e0ea7SMilanka Ringwald             break;
1861de9e0ea7SMilanka Ringwald         default:
1862de9e0ea7SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1863de9e0ea7SMilanka Ringwald     }
1864de9e0ea7SMilanka Ringwald 
1865de9e0ea7SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1866de9e0ea7SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1867de9e0ea7SMilanka Ringwald }
1868de9e0ea7SMilanka Ringwald 
1869fdda66c0SMilanka Ringwald 
1870013cc750SMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1871013cc750SMilanka Ringwald     return deactivate_voice_recognition(acl_handle, false);
1872be55a11dSMilanka Ringwald }
1873be55a11dSMilanka Ringwald 
1874e83c2025SMilanka Ringwald uint8_t hfp_hf_deactivate_enhanced_voice_recognition(hci_con_handle_t acl_handle){
1875013cc750SMilanka Ringwald     return deactivate_voice_recognition(acl_handle, true);
1876ce263fc8SMatthias Ringwald }
1877ce263fc8SMatthias Ringwald 
1878de9e0ea7SMilanka Ringwald 
18793c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
18809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1881a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18823c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1883a33eb0c4SMilanka Ringwald     }
1884c8626498SMilanka Ringwald 
18853c65e705SMilanka Ringwald     if (hfp_connection->microphone_gain == gain) {
18863c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
18873c65e705SMilanka Ringwald     }
18883c65e705SMilanka Ringwald 
1889c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1890a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
18913c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1892a0ffb263SMatthias Ringwald     }
18933c65e705SMilanka Ringwald 
1894a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1895a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
18961c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18973c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1898ce263fc8SMatthias Ringwald }
1899ce263fc8SMatthias Ringwald 
19003c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
19019c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1902a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19033c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1904a33eb0c4SMilanka Ringwald     }
1905c8626498SMilanka Ringwald 
19063c65e705SMilanka Ringwald     if (hfp_connection->speaker_gain == gain){
19073c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
19083c65e705SMilanka Ringwald     }
19093c65e705SMilanka Ringwald 
1910c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1911a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
19123c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1913a0ffb263SMatthias Ringwald     }
19143c65e705SMilanka Ringwald 
1915a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1916a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
19171c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19183c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1919ce263fc8SMatthias Ringwald }
1920ce263fc8SMatthias Ringwald 
19213c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
19229c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1923a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19243c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1925a33eb0c4SMilanka Ringwald     }
1926a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
19271c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19283c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1929ce263fc8SMatthias Ringwald }
1930ce263fc8SMatthias Ringwald 
19313c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
19329c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1933a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19343c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1935a33eb0c4SMilanka Ringwald     }
1936a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
19371c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19383c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1939ce263fc8SMatthias Ringwald }
19403deb3ec6SMatthias Ringwald 
19413c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
19429c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1943a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19443c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1945a33eb0c4SMilanka Ringwald     }
1946a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
19471c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19483c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1949667ec068SMatthias Ringwald }
1950667ec068SMatthias Ringwald 
1951667ec068SMatthias Ringwald 
19523c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
19539c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1954a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19553c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1956a33eb0c4SMilanka Ringwald     }
1957a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1958a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
19591c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19603c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1961667ec068SMatthias Ringwald }
1962667ec068SMatthias Ringwald 
19633c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
19649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1965a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19663c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1967a33eb0c4SMilanka Ringwald     }
1968a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1969a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
19701c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19713c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1972667ec068SMatthias Ringwald }
1973667ec068SMatthias Ringwald 
19743c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
19759c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1976a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19773c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1978a33eb0c4SMilanka Ringwald     }
1979a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1980a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
19811c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19823c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1983667ec068SMatthias Ringwald }
1984667ec068SMatthias Ringwald 
19853c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
19869c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1987a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19883c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1989a33eb0c4SMilanka Ringwald     }
1990a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1991a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
19921c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
19933c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1994667ec068SMatthias Ringwald }
1995667ec068SMatthias Ringwald 
19963c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
19979c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1998a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
19993c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2000a33eb0c4SMilanka Ringwald     }
2001a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
20021c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20033c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2004667ec068SMatthias Ringwald }
2005667ec068SMatthias Ringwald 
20063c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
20079c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2008a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20093c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2010a33eb0c4SMilanka Ringwald     }
2011667ec068SMatthias Ringwald     // find index for assigned number
2012667ec068SMatthias Ringwald     int i;
2013667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
2014667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
2015667ec068SMatthias Ringwald             // set value
2016667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
2017667ec068SMatthias Ringwald             // mark for update
2018a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
2019a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
2020667ec068SMatthias Ringwald                 // send update
20211c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
2022a0ffb263SMatthias Ringwald             }
20233c65e705SMilanka Ringwald             return ERROR_CODE_SUCCESS;
2024667ec068SMatthias Ringwald         }
2025667ec068SMatthias Ringwald     }
20263c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2027667ec068SMatthias Ringwald }
2028667ec068SMatthias Ringwald 
2029d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
2030d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2031d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
2032d7f6b5cbSMatthias Ringwald         return 0;
2033d7f6b5cbSMatthias Ringwald     }
2034d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
2035d7f6b5cbSMatthias Ringwald }
203676cc1527SMatthias Ringwald 
203776cc1527SMatthias 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){
203876cc1527SMatthias Ringwald 	if (!name){
203976cc1527SMatthias Ringwald 		name = default_hfp_hf_service_name;
204076cc1527SMatthias Ringwald 	}
204176cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
204276cc1527SMatthias Ringwald 
204376cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
204476cc1527SMatthias Ringwald 	//
204576cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
204676cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
204776cc1527SMatthias Ringwald 	//
204876cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
204976cc1527SMatthias Ringwald 	//
205076cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
2051ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
205276cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
205376cc1527SMatthias Ringwald 	}
2054ef3ae4ebSMilanka Ringwald 
2055ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
205656f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
2057ef3ae4ebSMilanka Ringwald     }
2058ef3ae4ebSMilanka Ringwald 
2059ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
206056f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
2061ef3ae4ebSMilanka Ringwald     }
2062ef3ae4ebSMilanka Ringwald 
206376cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
206476cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
206576cc1527SMatthias Ringwald }
206676cc1527SMatthias Ringwald 
206776cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
206868466199SMilanka Ringwald 	btstack_assert(callback != NULL);
206968466199SMilanka Ringwald 
207076cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
207176cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
207276cc1527SMatthias Ringwald }
2073