xref: /btstack/src/classic/hfp_hf.c (revision 657bc59fff90f6ac233c249277400d543b34942b)
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;
3863deb3ec6SMatthias Ringwald 
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;
435a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
436ce263fc8SMatthias Ringwald 
437ce263fc8SMatthias Ringwald     int done = 0;
438a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
439a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
440ce263fc8SMatthias Ringwald         done = 1;
441a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
442ce263fc8SMatthias Ringwald         return done;
443ce263fc8SMatthias Ringwald     };
444a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
445a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
446ce263fc8SMatthias Ringwald         done = 1;
447a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
448a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
449a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
450ce263fc8SMatthias Ringwald         return done;
451ce263fc8SMatthias Ringwald     }
452ce263fc8SMatthias Ringwald 
453a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
454ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
455a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
456a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
457a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
458ce263fc8SMatthias Ringwald             return 1;
459ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
460a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
461a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
462a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
463ce263fc8SMatthias Ringwald             return 1;
464ce263fc8SMatthias Ringwald         default:
465ce263fc8SMatthias Ringwald             break;
466ce263fc8SMatthias Ringwald     }
467ce263fc8SMatthias Ringwald 
468a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
469a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
470ce263fc8SMatthias Ringwald         done = 1;
471a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
472ce263fc8SMatthias Ringwald         return done;
473ce263fc8SMatthias Ringwald     }
474ce263fc8SMatthias Ringwald 
475ce263fc8SMatthias Ringwald     return done;
476ce263fc8SMatthias Ringwald }
477ce263fc8SMatthias Ringwald 
478be55a11dSMilanka Ringwald static int voice_recognition_state_machine(hfp_connection_t * hfp_connection){
479be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
480be55a11dSMilanka Ringwald         return 0;
481be55a11dSMilanka Ringwald     }
482be55a11dSMilanka Ringwald     int done = 0;
483be55a11dSMilanka Ringwald     if (hfp_connection->ok_pending == 0) {
484db3cdbd4SMilanka Ringwald         switch (hfp_connection->vra_status){
485be55a11dSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
486be55a11dSMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF:
487be55a11dSMilanka Ringwald                 hfp_connection->ok_pending = 1;
488be55a11dSMilanka Ringwald                 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
489be55a11dSMilanka Ringwald                 break;
490be55a11dSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
491be55a11dSMilanka Ringwald                 hfp_connection->ok_pending = 1;
492be55a11dSMilanka Ringwald                 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
493be55a11dSMilanka Ringwald                 break;
494ce263fc8SMatthias Ringwald 
495be55a11dSMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
496be55a11dSMilanka Ringwald                 if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
497be55a11dSMilanka Ringwald                     return 0;
498be55a11dSMilanka Ringwald                 }
499be55a11dSMilanka Ringwald                 hfp_connection->ok_pending = 1;
500be55a11dSMilanka Ringwald                 hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
501be55a11dSMilanka Ringwald                 break;
502be55a11dSMilanka Ringwald 
503be55a11dSMilanka Ringwald             default:
504be55a11dSMilanka Ringwald                 break;
505be55a11dSMilanka Ringwald         }
506be55a11dSMilanka Ringwald     } else {
507db3cdbd4SMilanka Ringwald         switch (hfp_connection->vra_status){
508be55a11dSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
509db3cdbd4SMilanka Ringwald                 hfp_connection->vra_status = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
510be55a11dSMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_VOICE_RECOGNITION_STATUS, 1);
511be55a11dSMilanka Ringwald                 break;
512be55a11dSMilanka Ringwald 
513be55a11dSMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
514db3cdbd4SMilanka Ringwald                 hfp_connection->vra_status = HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED;
515be55a11dSMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_VOICE_RECOGNITION_STATUS, 2);
516be55a11dSMilanka Ringwald                 break;
517be55a11dSMilanka Ringwald 
518be55a11dSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
519be55a11dSMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF:
520db3cdbd4SMilanka Ringwald                 hfp_connection->vra_status = HFP_VRA_VOICE_RECOGNITION_OFF;
521be55a11dSMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_VOICE_RECOGNITION_STATUS, 0);
522be55a11dSMilanka Ringwald                 break;
523be55a11dSMilanka Ringwald             default:
524be55a11dSMilanka Ringwald                 break;
525be55a11dSMilanka Ringwald         }
526be55a11dSMilanka Ringwald     }
527be55a11dSMilanka Ringwald     return done;
528be55a11dSMilanka Ringwald }
529be55a11dSMilanka Ringwald 
530be55a11dSMilanka Ringwald 
531be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
532a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
533ce263fc8SMatthias Ringwald 
534332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
535332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
536ce263fc8SMatthias Ringwald 
537a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
538a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
539332ca98fSMatthias Ringwald 		return 1;
540332ca98fSMatthias Ringwald     }
541332ca98fSMatthias Ringwald 
5421cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
5431cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
544ce263fc8SMatthias Ringwald 
545a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
546fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
5471cc65c4fSMatthias Ringwald 		return 1;
5481cc65c4fSMatthias Ringwald     }
5491cc65c4fSMatthias Ringwald 
5501cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
5511cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
5521cc65c4fSMatthias Ringwald 
553a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
554a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
5551cc65c4fSMatthias Ringwald 		return 1;
5561cc65c4fSMatthias Ringwald     }
557ce263fc8SMatthias Ringwald 
558ce263fc8SMatthias Ringwald     return 0;
559ce263fc8SMatthias Ringwald }
560ce263fc8SMatthias Ringwald 
561a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
562505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
563505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
564ce263fc8SMatthias Ringwald 
56564f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
566a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
567a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
568a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
569ce263fc8SMatthias Ringwald         return 1;
570ce263fc8SMatthias Ringwald     }
571ce263fc8SMatthias Ringwald 
572a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
573ce263fc8SMatthias Ringwald 
574ce263fc8SMatthias Ringwald     // run codecs exchange
575a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
576ce263fc8SMatthias Ringwald     if (done) return 1;
577ce263fc8SMatthias Ringwald 
57838200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
57938200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
58038200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
58138200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
58238200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
58338200c1dSMilanka Ringwald         return 1;
58438200c1dSMilanka Ringwald     }
585ce263fc8SMatthias Ringwald     return 0;
586ce263fc8SMatthias Ringwald }
587ce263fc8SMatthias Ringwald 
58838200c1dSMilanka Ringwald 
589a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
590eaf2b0a1SMatthias Ringwald 
591eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
592eaf2b0a1SMatthias Ringwald 
593a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
594a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
595a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
596ce263fc8SMatthias Ringwald         return 1;
597ce263fc8SMatthias Ringwald     }
598ce263fc8SMatthias Ringwald     return 0;
599ce263fc8SMatthias Ringwald }
600ce263fc8SMatthias Ringwald 
6011c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
6027522e673SMatthias Ringwald 
60376cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
60476cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
60576cc1527SMatthias Ringwald 
60676cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
60776cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
60822387625SMatthias Ringwald 
6093721a235SMatthias Ringwald 	// assert command could be sent
6103721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
6113721a235SMatthias Ringwald 
6123721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
6133721a235SMatthias Ringwald     // WBS Disassociate
6143721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
6153721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
6163721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
6173721a235SMatthias Ringwald         return;
6183721a235SMatthias Ringwald     }
6193721a235SMatthias Ringwald     // Write Codec Config
6203721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
6213721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
6223721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
6233721a235SMatthias Ringwald         return;
6243721a235SMatthias Ringwald     }
6253721a235SMatthias Ringwald     // WBS Associate
6263721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
6273721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
6283721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
6293721a235SMatthias Ringwald         return;
6303721a235SMatthias Ringwald     }
6313721a235SMatthias Ringwald #endif
632689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
633689d4323SMatthias Ringwald     // Enable WBS
634689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
635689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
636689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
637689d4323SMatthias Ringwald         return;
638689d4323SMatthias Ringwald     }
639689d4323SMatthias Ringwald     // Write I2S/PCM params
640689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
641689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
642689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
643689d4323SMatthias Ringwald         return;
644689d4323SMatthias Ringwald     }
645689d4323SMatthias Ringwald     // Disable WBS
646689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
647689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
648689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
649689d4323SMatthias Ringwald         return;
650689d4323SMatthias Ringwald     }
651689d4323SMatthias Ringwald #endif
65248e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
65348e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
65448e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
65548e6eeeeSMatthias Ringwald         return;
65648e6eeeeSMatthias Ringwald     }
65748e6eeeeSMatthias Ringwald #endif
6583721a235SMatthias Ringwald 
659cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
660cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
661cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
6627522e673SMatthias Ringwald         // notify about codec selection if not done already
6637522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
6647522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
6657522e673SMatthias Ringwald         }
666cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
6677522e673SMatthias Ringwald         return;
6687522e673SMatthias Ringwald     }
6697522e673SMatthias Ringwald 
670d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
671d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
672d4dd47ffSMatthias Ringwald         return;
673d4dd47ffSMatthias Ringwald     }
674a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
675ce263fc8SMatthias Ringwald     if (!done){
676a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
677ce263fc8SMatthias Ringwald     }
678ce263fc8SMatthias Ringwald     if (!done){
679be55a11dSMilanka Ringwald         done = voice_recognition_state_machine(hfp_connection);
680be55a11dSMilanka Ringwald     }
681be55a11dSMilanka Ringwald     if (!done){
682a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
683ce263fc8SMatthias Ringwald     }
684ce263fc8SMatthias Ringwald     if (!done){
685a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
686ce263fc8SMatthias Ringwald     }
687ce263fc8SMatthias Ringwald 
6881016a228SMatthias Ringwald     // don't send a new command while ok still pending
6891016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6901016a228SMatthias Ringwald 
691a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
692a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
693a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
694a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
695ce263fc8SMatthias Ringwald         return;
696ce263fc8SMatthias Ringwald     }
697ce263fc8SMatthias Ringwald 
698a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
699a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
700a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
701a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
702ce263fc8SMatthias Ringwald         return;
703ce263fc8SMatthias Ringwald     }
704ce263fc8SMatthias Ringwald 
705a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
706a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
707a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
708a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
709ce263fc8SMatthias Ringwald         return;
710ce263fc8SMatthias Ringwald     }
711ce263fc8SMatthias Ringwald 
712a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
713a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
714a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
715a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
716ce263fc8SMatthias Ringwald         return;
717ce263fc8SMatthias Ringwald     }
718ce263fc8SMatthias Ringwald 
719a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
720a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
721a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
722a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
723ce263fc8SMatthias Ringwald         return;
724ce263fc8SMatthias Ringwald     }
725ce263fc8SMatthias Ringwald 
726a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
727a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
728a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
729a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
730ce263fc8SMatthias Ringwald         return;
731ce263fc8SMatthias Ringwald     }
732ce263fc8SMatthias Ringwald 
733a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
734a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
735a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
736a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
737ce263fc8SMatthias Ringwald         return;
738ce263fc8SMatthias Ringwald     }
739ce263fc8SMatthias Ringwald 
740a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
741a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
742a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
743a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
744ce263fc8SMatthias Ringwald         return;
745ce263fc8SMatthias Ringwald     }
746ce263fc8SMatthias Ringwald 
747a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
748a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
749a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
750a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
751ce263fc8SMatthias Ringwald         return;
752ce263fc8SMatthias Ringwald     }
753ce263fc8SMatthias Ringwald 
754a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
755a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
756a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
757a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
758ce263fc8SMatthias Ringwald         return;
759ce263fc8SMatthias Ringwald     }
760ce263fc8SMatthias Ringwald 
761a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
762a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
763a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
764a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
765ce263fc8SMatthias Ringwald         return;
766ce263fc8SMatthias Ringwald     }
767ce263fc8SMatthias Ringwald 
768a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
769a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
770a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
771a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
772ce263fc8SMatthias Ringwald         return;
773ce263fc8SMatthias Ringwald     }
774ce263fc8SMatthias Ringwald 
775a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
776a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
777a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
778a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
779ce263fc8SMatthias Ringwald         return;
780ce263fc8SMatthias Ringwald     }
781ce263fc8SMatthias Ringwald 
782a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
783a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
784a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
785a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
786ce263fc8SMatthias Ringwald         return;
787ce263fc8SMatthias Ringwald     }
788ce263fc8SMatthias Ringwald 
789a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
790a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
791a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
792a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
793ce263fc8SMatthias Ringwald         return;
794ce263fc8SMatthias Ringwald     }
795ce263fc8SMatthias Ringwald 
796a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
797a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
798a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
799a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
800ce263fc8SMatthias Ringwald         return;
801ce263fc8SMatthias Ringwald     }
802ce263fc8SMatthias Ringwald 
803a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
804a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
805a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
806a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
807ce263fc8SMatthias Ringwald         return;
808ce263fc8SMatthias Ringwald     }
809ce263fc8SMatthias Ringwald 
810a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
811a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
812a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
813a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
814667ec068SMatthias Ringwald         return;
815667ec068SMatthias Ringwald     }
816667ec068SMatthias Ringwald 
817a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
818a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
819a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
820a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
821a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
822ce263fc8SMatthias Ringwald         return;
823ce263fc8SMatthias Ringwald     }
824ce263fc8SMatthias Ringwald 
825a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
826a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
827a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
828a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
829ce263fc8SMatthias Ringwald         return;
830ce263fc8SMatthias Ringwald     }
831ce263fc8SMatthias Ringwald 
832a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
833a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
834a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
835a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
836667ec068SMatthias Ringwald         return;
837667ec068SMatthias Ringwald     }
838667ec068SMatthias Ringwald 
839a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
840a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
841667ec068SMatthias Ringwald         char buffer[20];
842a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
843667ec068SMatthias Ringwald             case '?':
8441599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
845ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
846ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
847a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
848667ec068SMatthias Ringwald                 return;
849667ec068SMatthias Ringwald             case '0':
850667ec068SMatthias Ringwald             case '1':
851667ec068SMatthias Ringwald             case '2':
8521599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
853ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
854ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
855ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
856a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
857667ec068SMatthias Ringwald                 return;
858667ec068SMatthias Ringwald             default:
859667ec068SMatthias Ringwald                 break;
860667ec068SMatthias Ringwald         }
861667ec068SMatthias Ringwald         return;
862667ec068SMatthias Ringwald     }
863667ec068SMatthias Ringwald 
864a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
865a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
866667ec068SMatthias Ringwald         char buffer[20];
8671599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
868ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
869ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
870a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
871667ec068SMatthias Ringwald         return;
872667ec068SMatthias Ringwald     }
873667ec068SMatthias Ringwald 
874667ec068SMatthias Ringwald     // update HF indicators
875a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
876667ec068SMatthias Ringwald         int i;
877667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
878a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
879a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
880a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
881a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
882667ec068SMatthias Ringwald                     char buffer[30];
8831599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
884ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
885ff7d6aeaSMatthias Ringwald                              hfp_indicators[i],
886ff7d6aeaSMatthias Ringwald                              (unsigned int)hfp_indicators_value[i]);
887ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
888a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
889667ec068SMatthias Ringwald                 } else {
89060ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
891667ec068SMatthias Ringwald                 }
892667ec068SMatthias Ringwald                 return;
893667ec068SMatthias Ringwald             }
894667ec068SMatthias Ringwald         }
895667ec068SMatthias Ringwald     }
896667ec068SMatthias Ringwald 
897ce263fc8SMatthias Ringwald     if (done) return;
898ce263fc8SMatthias Ringwald     // deal with disconnect
899a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
900ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
901a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
902a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
903ce263fc8SMatthias Ringwald             break;
904ce263fc8SMatthias Ringwald 
905ce263fc8SMatthias Ringwald         default:
906ce263fc8SMatthias Ringwald             break;
907ce263fc8SMatthias Ringwald     }
908ce263fc8SMatthias Ringwald }
909ce263fc8SMatthias Ringwald 
910a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
911a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
9126a7f44bdSMilanka Ringwald 
913ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
9147522e673SMatthias Ringwald 
915667ec068SMatthias Ringwald     // restore volume settings
916a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
917a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
918ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
919a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
920a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
921ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
922667ec068SMatthias Ringwald     // enable all indicators
923667ec068SMatthias Ringwald     int i;
924667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
925a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
926a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
927667ec068SMatthias Ringwald     }
928ce263fc8SMatthias Ringwald }
929ce263fc8SMatthias Ringwald 
9301cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
9311cc65c4fSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
9321cc65c4fSMatthias Ringwald 		// Codec supported, confirm
9331cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
9341cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
9351cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
9361cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
9371cc65c4fSMatthias Ringwald 
9381cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
9391cc65c4fSMatthias Ringwald 	} else {
9401cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
9411cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
9421cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
9431cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
9441cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9451cc65c4fSMatthias Ringwald 
9461cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
9471cc65c4fSMatthias Ringwald 	}
9481cc65c4fSMatthias Ringwald }
9491cc65c4fSMatthias Ringwald 
950a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
951a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
9523deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
953a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
954a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
9553deb3ec6SMatthias Ringwald                 break;
9563deb3ec6SMatthias Ringwald             }
957a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9583deb3ec6SMatthias Ringwald             break;
9593deb3ec6SMatthias Ringwald 
9603deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
961a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
9623deb3ec6SMatthias Ringwald             break;
9633deb3ec6SMatthias Ringwald 
9643deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
965a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
9663deb3ec6SMatthias Ringwald             break;
9673deb3ec6SMatthias Ringwald 
9683deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
969a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
9703deb3ec6SMatthias Ringwald             break;
9713deb3ec6SMatthias Ringwald 
9723deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
973a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
974a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
9753deb3ec6SMatthias Ringwald                 break;
9763deb3ec6SMatthias Ringwald             }
977a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
978a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9793deb3ec6SMatthias Ringwald                 break;
9803deb3ec6SMatthias Ringwald             }
981a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9823deb3ec6SMatthias Ringwald             break;
9833deb3ec6SMatthias Ringwald 
9843deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
985a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
986a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9873deb3ec6SMatthias Ringwald                 break;
9883deb3ec6SMatthias Ringwald             }
989a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9903deb3ec6SMatthias Ringwald             break;
9913deb3ec6SMatthias Ringwald 
9923deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
993a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
9943deb3ec6SMatthias Ringwald             break;
9953deb3ec6SMatthias Ringwald 
9963deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
997a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9983deb3ec6SMatthias Ringwald             break;
9993deb3ec6SMatthias Ringwald 
10003deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1001a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
10023deb3ec6SMatthias Ringwald             break;
1003ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1004a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1005a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1006ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1007ce263fc8SMatthias Ringwald                 break;
1008ce263fc8SMatthias Ringwald             }
10093deb3ec6SMatthias Ringwald 
1010a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1011a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1012ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1013ce263fc8SMatthias Ringwald                 break;
10143deb3ec6SMatthias Ringwald             }
10153deb3ec6SMatthias Ringwald 
1016a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
1017ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1018a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1019ce263fc8SMatthias Ringwald                     break;
1020ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1021a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1022a473a009SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_connection);
1023ce263fc8SMatthias Ringwald                     break;
1024ce263fc8SMatthias Ringwald                 default:
1025ce263fc8SMatthias Ringwald                     break;
10263deb3ec6SMatthias Ringwald             }
1027ce263fc8SMatthias Ringwald 
1028a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
1029a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
1030ce263fc8SMatthias Ringwald                 break;
10313deb3ec6SMatthias Ringwald             }
10323deb3ec6SMatthias Ringwald 
1033a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
1034aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1035a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10363deb3ec6SMatthias Ringwald                     break;
1037ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
1038a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1039ce263fc8SMatthias Ringwald                     break;
10403deb3ec6SMatthias Ringwald                 default:
10413deb3ec6SMatthias Ringwald                     break;
10423deb3ec6SMatthias Ringwald             }
1043be55a11dSMilanka Ringwald             voice_recognition_state_machine(hfp_connection);
1044be55a11dSMilanka Ringwald             break;
1045be55a11dSMilanka Ringwald         case HFP_AUDIO_CONNECTION_ESTABLISHED:
1046be55a11dSMilanka Ringwald             voice_recognition_state_machine(hfp_connection);
10473deb3ec6SMatthias Ringwald             break;
10483deb3ec6SMatthias Ringwald         default:
10493deb3ec6SMatthias Ringwald             break;
10503deb3ec6SMatthias Ringwald     }
10513deb3ec6SMatthias Ringwald 
10523deb3ec6SMatthias Ringwald     // done
1053be55a11dSMilanka Ringwald     hfp_connection->ok_pending = 0;
1054a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
10553deb3ec6SMatthias Ringwald }
10563deb3ec6SMatthias Ringwald 
1057be55a11dSMilanka Ringwald 
1058b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
10594562e2a2SMatthias Ringwald     uint16_t i;
10604562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
10614562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
10624562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
10634562e2a2SMatthias Ringwald                 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
10644562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
10654562e2a2SMatthias Ringwald                 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
10664562e2a2SMatthias Ringwald                 // avoid set but not used warning
10674562e2a2SMatthias Ringwald                 (void) hfp_callheld_status;
10684562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
10694562e2a2SMatthias Ringwald                 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
10704562e2a2SMatthias Ringwald             }
10714562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1072a473a009SMatthias Ringwald             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
10734562e2a2SMatthias Ringwald             break;
10744562e2a2SMatthias Ringwald         }
10754562e2a2SMatthias Ringwald     }
10764562e2a2SMatthias Ringwald }
10774562e2a2SMatthias Ringwald 
1078426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1079186dd3d2SMatthias Ringwald     int value;
1080186dd3d2SMatthias Ringwald     int i;
1081a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1082667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1083a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1084a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1085667ec068SMatthias Ringwald             break;
1086667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1087a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1088ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1089667ec068SMatthias Ringwald             break;
1090667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1091a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1092a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1093667ec068SMatthias Ringwald             break;
1094ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1095a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10962308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1097667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1098ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1099ce263fc8SMatthias Ringwald             break;
1100ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1101a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11022308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1103667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1104ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1105ce263fc8SMatthias Ringwald             break;
1106ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1107a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1108ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1109a0ffb263SMatthias Ringwald             break;
1110a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1111a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1112a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1113a0ffb263SMatthias Ringwald             break;
1114a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1115a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1116a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1117ce263fc8SMatthias Ringwald             break;
1118ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1119a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11205a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1121a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1122ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1123ce263fc8SMatthias Ringwald             break;
1124ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1125a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1126a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1127be55a11dSMilanka Ringwald             hfp_connection->command = HFP_CMD_NONE;
112856f1adacSMilanka Ringwald 
112990244c92SMilanka Ringwald             switch (hfp_connection->state){
113090244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
113190244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
113290244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
113390244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
113490244c92SMilanka Ringwald                             return;
113590244c92SMilanka Ringwald                         default:
113690244c92SMilanka Ringwald                             break;
113790244c92SMilanka Ringwald                     }
113856f1adacSMilanka Ringwald                     break;
113956f1adacSMilanka Ringwald                 default:
114056f1adacSMilanka Ringwald                     break;
114156f1adacSMilanka Ringwald             }
114256f1adacSMilanka Ringwald 
1143be55a11dSMilanka Ringwald 
1144db3cdbd4SMilanka Ringwald             switch (hfp_connection->vra_status){
1145be55a11dSMilanka Ringwald                 case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1146db3cdbd4SMilanka Ringwald                     hfp_connection->vra_status = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
1147be55a11dSMilanka Ringwald                     break;
1148be55a11dSMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF:
1149db3cdbd4SMilanka Ringwald                     hfp_connection->vra_status = HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED;
1150be55a11dSMilanka Ringwald                     break;
1151be55a11dSMilanka Ringwald                 case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1152be55a11dSMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
1153db3cdbd4SMilanka Ringwald                     hfp_connection->vra_status = HFP_VRA_VOICE_RECOGNITION_OFF;
1154be55a11dSMilanka Ringwald                     break;
1155be55a11dSMilanka Ringwald                 default:
1156be55a11dSMilanka Ringwald                     break;
1157be55a11dSMilanka Ringwald             }
1158be55a11dSMilanka Ringwald 
1159ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1160ce263fc8SMatthias Ringwald             break;
1161ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1162a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1163ce263fc8SMatthias Ringwald             break;
1164ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
11655a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1166ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1167ce263fc8SMatthias Ringwald             break;
1168ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
11695a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11704562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1171ce263fc8SMatthias Ringwald             break;
1172c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
11735a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1174c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1175a473a009SMatthias Ringwald                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1176c741b032SMilanka Ringwald             }
1177c741b032SMilanka Ringwald             break;
11781cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
11791cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
11805a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
11811cc65c4fSMatthias Ringwald 			break;
1182eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1183eac56539SMilanka 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));
1184ce263fc8SMatthias Ringwald         default:
1185ce263fc8SMatthias Ringwald             break;
11863deb3ec6SMatthias Ringwald     }
11870cef86faSMatthias Ringwald }
1188426f9988SMatthias Ringwald 
118976cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
119076cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
119176cc1527SMatthias Ringwald }
119276cc1527SMatthias Ringwald 
1193426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1194426f9988SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
1195426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1196426f9988SMatthias Ringwald     if (size < 1) return;
1197426f9988SMatthias Ringwald 
1198426f9988SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1199426f9988SMatthias Ringwald     if (!hfp_connection) return;
1200426f9988SMatthias Ringwald 
1201426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1202e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1203e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1204e43d1938SMatthias Ringwald #endif
1205426f9988SMatthias Ringwald 
1206426f9988SMatthias Ringwald     // process messages byte-wise
1207426f9988SMatthias Ringwald     int pos;
1208426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1209426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
1210426f9988SMatthias Ringwald 
12111599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1212426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1213426f9988SMatthias Ringwald 
1214426f9988SMatthias Ringwald         hfp_hf_handle_rfcomm_command(hfp_connection);
1215426f9988SMatthias Ringwald     }
12161c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
12173deb3ec6SMatthias Ringwald }
12183deb3ec6SMatthias Ringwald 
12191c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1220665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1221665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1222665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1223a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
122422387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
12251c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
12263deb3ec6SMatthias Ringwald     }
12273deb3ec6SMatthias Ringwald }
12283deb3ec6SMatthias Ringwald 
12291c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
12303deb3ec6SMatthias Ringwald     switch (packet_type){
12313deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1232426f9988SMatthias Ringwald             hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size);
12333deb3ec6SMatthias Ringwald             break;
12343deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1235d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1236d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
12371c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1238d4dd47ffSMatthias Ringwald                 return;
1239d4dd47ffSMatthias Ringwald             }
124027950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1241202c8a4cSMatthias Ringwald             break;
12423deb3ec6SMatthias Ringwald         default:
12433deb3ec6SMatthias Ringwald             break;
12443deb3ec6SMatthias Ringwald     }
12451c6a0fc0SMatthias Ringwald     hfp_hf_run();
12463deb3ec6SMatthias Ringwald }
12473deb3ec6SMatthias Ringwald 
12481c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1249405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
12501c6a0fc0SMatthias Ringwald     hfp_hf_run();
1251405014fbSMatthias Ringwald }
1252405014fbSMatthias Ringwald 
1253b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1254b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1255b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1256b4df8028SMilanka Ringwald         return status;
1257b4df8028SMilanka Ringwald     }
1258b4df8028SMilanka Ringwald 
1259520c92d5SMatthias Ringwald     hfp_init();
126020b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
126120b2edb6SMatthias Ringwald     hfp_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
126220b2edb6SMatthias Ringwald     hfp_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
126320b2edb6SMatthias Ringwald     hfp_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
126420b2edb6SMatthias Ringwald     hfp_codecs_nr = 0;
126520b2edb6SMatthias Ringwald     hfp_hf_speaker_gain = 9;
126620b2edb6SMatthias Ringwald     hfp_hf_microphone_gain = 9;
126720b2edb6SMatthias Ringwald     hfp_indicators_nr = 0;
126820b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1269d63c37a1SMatthias Ringwald 
12701c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
12711c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
127227950165SMatthias Ringwald 
127327950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
12741c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1275b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
127620b2edb6SMatthias Ringwald }
127727950165SMatthias Ringwald 
127820b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
127920b2edb6SMatthias Ringwald     hfp_deinit();
128020b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
128120b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_callback, 0, sizeof(btstack_packet_handler_t));
128220b2edb6SMatthias Ringwald     (void) memset(phone_number, 0, sizeof(phone_number));
1283a0ffb263SMatthias Ringwald }
1284a0ffb263SMatthias Ringwald 
12857ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
128668466199SMilanka Ringwald     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
12873deb3ec6SMatthias Ringwald 
12883deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
12893deb3ec6SMatthias Ringwald     int i;
12903deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
12913deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
12923deb3ec6SMatthias Ringwald     }
12933deb3ec6SMatthias Ringwald }
12943deb3ec6SMatthias Ringwald 
1295a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
12963deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1297a0ffb263SMatthias Ringwald }
12983deb3ec6SMatthias Ringwald 
12997ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
130068466199SMilanka Ringwald     btstack_assert(hfp_indicators_nr < HFP_MAX_NUM_INDICATORS);
130168466199SMilanka Ringwald 
13023deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
13033deb3ec6SMatthias Ringwald     int i;
1304a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
13053deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
13063deb3ec6SMatthias Ringwald     }
13073deb3ec6SMatthias Ringwald }
13083deb3ec6SMatthias Ringwald 
13094eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
13104eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
13113deb3ec6SMatthias Ringwald }
13123deb3ec6SMatthias Ringwald 
1313*657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
13149c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1315a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1316*657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1317a33eb0c4SMilanka Ringwald     }
13181ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
13191c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1320*657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
13213deb3ec6SMatthias Ringwald }
13223deb3ec6SMatthias Ringwald 
1323c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
13249c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1325a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1326a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
13273deb3ec6SMatthias Ringwald         return;
13283deb3ec6SMatthias Ringwald     }
1329a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
13301c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13313deb3ec6SMatthias Ringwald }
13323deb3ec6SMatthias Ringwald 
1333c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1334c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1335ce263fc8SMatthias Ringwald }
1336ce263fc8SMatthias Ringwald 
1337c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1338c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1339ce263fc8SMatthias Ringwald }
1340ce263fc8SMatthias Ringwald 
13413deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1342c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
13439c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1344a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1345a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
13463deb3ec6SMatthias Ringwald         return;
13473deb3ec6SMatthias Ringwald     }
1348a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1349a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
13501c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13513deb3ec6SMatthias Ringwald }
13523deb3ec6SMatthias Ringwald 
1353c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
13549c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1355a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1356a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
13573deb3ec6SMatthias Ringwald         return;
13583deb3ec6SMatthias Ringwald     }
1359a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1360ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1361a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1362ce263fc8SMatthias Ringwald             break;
1363ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1364a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1365ce263fc8SMatthias Ringwald             break;
1366ce263fc8SMatthias Ringwald         default:
1367ce263fc8SMatthias Ringwald             break;
1368ce263fc8SMatthias Ringwald     }
13691c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13703deb3ec6SMatthias Ringwald }
13713deb3ec6SMatthias Ringwald 
1372c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
13739c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1374a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1375a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
13763deb3ec6SMatthias Ringwald         return;
13773deb3ec6SMatthias Ringwald     }
1378a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
13791c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
13803deb3ec6SMatthias Ringwald }
13813deb3ec6SMatthias Ringwald 
1382ce263fc8SMatthias Ringwald 
1383c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1384c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1385ce263fc8SMatthias Ringwald }
1386ce263fc8SMatthias Ringwald 
1387c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1388c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1389ce263fc8SMatthias Ringwald }
1390ce263fc8SMatthias Ringwald 
139138200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
139238200c1dSMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
139338200c1dSMilanka Ringwald }
1394ce263fc8SMatthias Ringwald 
1395c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
13969c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1397a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1398a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1399a33eb0c4SMilanka Ringwald         return;
1400a33eb0c4SMilanka Ringwald     }
1401ce263fc8SMatthias Ringwald 
1402a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1403a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
14043deb3ec6SMatthias Ringwald 
1405f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1406a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1407aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1408aa4dd815SMatthias Ringwald                 break;
1409ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1410ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1411ec3bfc1aSMatthias Ringwald                 break;
1412aa4dd815SMatthias Ringwald             default:
14131cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
14141cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
14151cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
14161cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
141738200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1418aa4dd815SMatthias Ringwald                 break;
14193deb3ec6SMatthias Ringwald         }
1420f4412093SMatthias Ringwald     } else {
1421f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1422f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1423f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1424f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1425f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1426f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1427f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1428f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1429ce263fc8SMatthias Ringwald     }
1430ce263fc8SMatthias Ringwald 
14311c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14323deb3ec6SMatthias Ringwald }
14333deb3ec6SMatthias Ringwald 
1434c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
14359c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1436a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1437a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1438a33eb0c4SMilanka Ringwald         return;
1439a33eb0c4SMilanka Ringwald     }
14401ffa0dd9SMilanka Ringwald     hfp_trigger_release_audio_connection(hfp_connection);
14411c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
14423deb3ec6SMatthias Ringwald }
14433deb3ec6SMatthias Ringwald 
1444c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
14459c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1446a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1447a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1448a33eb0c4SMilanka Ringwald         return;
1449a33eb0c4SMilanka Ringwald     }
1450ce263fc8SMatthias Ringwald 
1451ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1452a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
14531c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1454ce263fc8SMatthias Ringwald     } else {
1455ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1456ce263fc8SMatthias Ringwald     }
1457ce263fc8SMatthias Ringwald }
1458ce263fc8SMatthias Ringwald 
1459c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
14609c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1461a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1462a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1463a33eb0c4SMilanka Ringwald         return;
1464a33eb0c4SMilanka Ringwald     }
1465a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
14661c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1467ce263fc8SMatthias Ringwald }
1468ce263fc8SMatthias Ringwald 
1469c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
14709c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1471a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1472a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1473a33eb0c4SMilanka Ringwald         return;
1474a33eb0c4SMilanka Ringwald     }
1475ce263fc8SMatthias Ringwald 
1476ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1477a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
14781c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1479ce263fc8SMatthias Ringwald     }
1480ce263fc8SMatthias Ringwald }
1481ce263fc8SMatthias Ringwald 
1482c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){
14839c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1484a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1485a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1486a33eb0c4SMilanka Ringwald         return;
1487a33eb0c4SMilanka Ringwald     }
1488ce263fc8SMatthias Ringwald 
1489ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1490a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
14911c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1492ce263fc8SMatthias Ringwald     }
1493ce263fc8SMatthias Ringwald }
1494ce263fc8SMatthias Ringwald 
1495c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(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) {
1498a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1499a33eb0c4SMilanka Ringwald         return;
1500a33eb0c4SMilanka Ringwald     }
1501ce263fc8SMatthias Ringwald 
1502505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1503505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1504a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
15051c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1506ce263fc8SMatthias Ringwald     }
1507ce263fc8SMatthias Ringwald }
1508ce263fc8SMatthias Ringwald 
1509c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){
15109c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1511a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1512a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1513a33eb0c4SMilanka Ringwald         return;
1514a33eb0c4SMilanka Ringwald     }
1515ce263fc8SMatthias Ringwald 
1516505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1517505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1518a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
15191c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1520ce263fc8SMatthias Ringwald     }
1521ce263fc8SMatthias Ringwald }
1522ce263fc8SMatthias Ringwald 
1523c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
15249c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1525a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1526a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1527a33eb0c4SMilanka Ringwald         return;
1528a33eb0c4SMilanka Ringwald     }
1529ce263fc8SMatthias Ringwald 
1530505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1531505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1532a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
15331c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1534ce263fc8SMatthias Ringwald     }
1535ce263fc8SMatthias Ringwald }
1536ce263fc8SMatthias Ringwald 
1537c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
15389c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1539a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1540a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1541a33eb0c4SMilanka Ringwald         return;
1542a33eb0c4SMilanka Ringwald     }
1543ce263fc8SMatthias Ringwald 
1544505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1545505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1546a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
15471c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1548ce263fc8SMatthias Ringwald     }
1549ce263fc8SMatthias Ringwald }
1550ce263fc8SMatthias Ringwald 
1551c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
15529c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1553a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1554a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1555a33eb0c4SMilanka Ringwald         return;
1556a33eb0c4SMilanka Ringwald     }
1557667ec068SMatthias Ringwald 
1558505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1559505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1560a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1561a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
15621c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1563667ec068SMatthias Ringwald     }
1564667ec068SMatthias Ringwald }
1565667ec068SMatthias Ringwald 
1566c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
15679c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1568a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1569a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1570a33eb0c4SMilanka Ringwald         return;
1571a33eb0c4SMilanka Ringwald     }
1572667ec068SMatthias Ringwald 
1573505f1c30SMatthias Ringwald     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1574505f1c30SMatthias Ringwald         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1575a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1576a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
15771c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1578667ec068SMatthias Ringwald     }
1579667ec068SMatthias Ringwald }
1580ce263fc8SMatthias Ringwald 
1581c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
15829c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1583a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1584a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1585a33eb0c4SMilanka Ringwald         return;
1586a33eb0c4SMilanka Ringwald     }
1587ce263fc8SMatthias Ringwald 
1588a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1589ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
15901c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1591ce263fc8SMatthias Ringwald }
1592ce263fc8SMatthias Ringwald 
1593c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
15949c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1595a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1596a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1597a33eb0c4SMilanka Ringwald         return;
1598a33eb0c4SMilanka Ringwald     }
1599ce263fc8SMatthias Ringwald 
1600a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1601a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1602a0ffb263SMatthias Ringwald 
16031c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1604ce263fc8SMatthias Ringwald }
1605ce263fc8SMatthias Ringwald 
1606c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
16079c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1608a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1609a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1610a33eb0c4SMilanka Ringwald         return;
1611a33eb0c4SMilanka Ringwald     }
1612ce263fc8SMatthias Ringwald 
1613a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
16141c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1615ce263fc8SMatthias Ringwald }
1616ce263fc8SMatthias Ringwald 
1617c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
16189c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1619a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1620a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1621a33eb0c4SMilanka Ringwald         return;
1622a33eb0c4SMilanka Ringwald     }
1623ce263fc8SMatthias Ringwald 
1624a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
16251c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1626ce263fc8SMatthias Ringwald }
1627ce263fc8SMatthias Ringwald 
1628ce263fc8SMatthias Ringwald 
1629c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
16309c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1631a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1632a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1633a33eb0c4SMilanka Ringwald         return;
1634a33eb0c4SMilanka Ringwald     }
1635ce263fc8SMatthias Ringwald 
1636a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
16371c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1638ce263fc8SMatthias Ringwald }
1639ce263fc8SMatthias Ringwald 
1640ce263fc8SMatthias Ringwald 
1641c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
16429c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1643a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1644a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1645a33eb0c4SMilanka Ringwald         return;
1646a33eb0c4SMilanka Ringwald     }
1647ce263fc8SMatthias Ringwald 
1648a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
16491c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1650ce263fc8SMatthias Ringwald }
1651ce263fc8SMatthias Ringwald 
1652c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
16539c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1654a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1655a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1656a33eb0c4SMilanka Ringwald         return;
1657a33eb0c4SMilanka Ringwald     }
1658ce263fc8SMatthias Ringwald 
1659a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
16601c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1661ce263fc8SMatthias Ringwald }
1662ce263fc8SMatthias Ringwald 
1663ce263fc8SMatthias Ringwald 
1664c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
16659c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1666a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1667a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1668a33eb0c4SMilanka Ringwald         return;
1669a33eb0c4SMilanka Ringwald     }
1670ce263fc8SMatthias Ringwald 
1671a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
16721c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1673ce263fc8SMatthias Ringwald }
1674ce263fc8SMatthias Ringwald 
1675c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
16769c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1677a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1678a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1679a33eb0c4SMilanka Ringwald         return;
1680a33eb0c4SMilanka Ringwald     }
1681ce263fc8SMatthias Ringwald 
1682a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
16831c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1684ce263fc8SMatthias Ringwald }
1685ce263fc8SMatthias Ringwald 
1686be55a11dSMilanka Ringwald static bool hfp_hf_enhanced_voice_recognition_supported(hfp_connection_t * hfp_connection){
1687be55a11dSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1688be55a11dSMilanka Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1689be55a11dSMilanka Ringwald     return hf && ag;
1690be55a11dSMilanka Ringwald }
1691be55a11dSMilanka Ringwald 
1692be55a11dSMilanka Ringwald static bool hfp_hf_voice_recognition_supported(hfp_connection_t * hfp_connection){
1693be55a11dSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION);
1694be55a11dSMilanka Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION);
1695be55a11dSMilanka Ringwald     return hf && ag;
1696be55a11dSMilanka Ringwald }
1697be55a11dSMilanka Ringwald 
1698be55a11dSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
16999c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1700a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1701a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1702be55a11dSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1703be55a11dSMilanka Ringwald     }
1704be55a11dSMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection)){
1705be55a11dSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1706a33eb0c4SMilanka Ringwald     }
1707ce263fc8SMatthias Ringwald 
1708db3cdbd4SMilanka Ringwald     switch (hfp_connection->vra_status){
1709be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
171056f1adacSMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_VOICE_RECOGNITION_STATUS, 1);
1711be55a11dSMilanka Ringwald             break;
1712be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1713db3cdbd4SMilanka Ringwald             hfp_connection->vra_status = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
17141c6a0fc0SMatthias Ringwald             hfp_hf_run_for_context(hfp_connection);
1715be55a11dSMilanka Ringwald             break;
1716be55a11dSMilanka Ringwald         default:
1717be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1718be55a11dSMilanka Ringwald     }
1719be55a11dSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1720ce263fc8SMatthias Ringwald }
1721ce263fc8SMatthias Ringwald 
1722be55a11dSMilanka Ringwald 
1723db3cdbd4SMilanka Ringwald uint8_t hfp_hf_start_enhanced_voice_recognition_session(hci_con_handle_t acl_handle){
17249c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1725a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1726a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1727be55a11dSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1728a33eb0c4SMilanka Ringwald     }
1729ce263fc8SMatthias Ringwald 
1730be55a11dSMilanka Ringwald     if (!hfp_hf_enhanced_voice_recognition_supported(hfp_connection)){
1731be55a11dSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
173256f1adacSMilanka Ringwald     }
173356f1adacSMilanka Ringwald 
1734db3cdbd4SMilanka Ringwald     switch (hfp_connection->vra_status){
1735be55a11dSMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
1736be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1737db3cdbd4SMilanka Ringwald             hfp_connection->vra_status = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_ACTIVATED;
17381c6a0fc0SMatthias Ringwald             hfp_hf_run_for_context(hfp_connection);
1739be55a11dSMilanka Ringwald             break;
1740be55a11dSMilanka Ringwald         default:
1741be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1742be55a11dSMilanka Ringwald     }
1743be55a11dSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1744be55a11dSMilanka Ringwald }
1745be55a11dSMilanka Ringwald 
1746be55a11dSMilanka Ringwald static uint8_t hfp_hf_deactivate_voice_recognition(hfp_connection_t * hfp_connection){
1747db3cdbd4SMilanka Ringwald     switch (hfp_connection->vra_status){
1748be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1749be55a11dSMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_VOICE_RECOGNITION_STATUS, 0);
1750be55a11dSMilanka Ringwald             break;
1751be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1752db3cdbd4SMilanka Ringwald             hfp_connection->vra_status = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
1753be55a11dSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
1754be55a11dSMilanka Ringwald             break;
1755be55a11dSMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_ACTIVATED:
1756db3cdbd4SMilanka Ringwald             hfp_connection->vra_status = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_OFF;
1757be55a11dSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
1758be55a11dSMilanka Ringwald             break;
1759be55a11dSMilanka Ringwald         default:
1760be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1761be55a11dSMilanka Ringwald     }
1762be55a11dSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1763be55a11dSMilanka Ringwald }
1764be55a11dSMilanka Ringwald 
1765be55a11dSMilanka Ringwald 
1766be55a11dSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
1767be55a11dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1768be55a11dSMilanka Ringwald     if (!hfp_connection) {
1769be55a11dSMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1770be55a11dSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1771be55a11dSMilanka Ringwald     }
1772be55a11dSMilanka Ringwald     if (!hfp_hf_voice_recognition_supported(hfp_connection)){
1773be55a11dSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1774be55a11dSMilanka Ringwald     }
1775be55a11dSMilanka Ringwald     return hfp_hf_deactivate_voice_recognition(hfp_connection);
1776be55a11dSMilanka Ringwald }
1777be55a11dSMilanka Ringwald 
1778be55a11dSMilanka Ringwald 
1779db3cdbd4SMilanka Ringwald uint8_t hfp_hf_stop_enhanced_voice_recognition_session(hci_con_handle_t acl_handle){
1780be55a11dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1781be55a11dSMilanka Ringwald     if (!hfp_connection) {
1782be55a11dSMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1783be55a11dSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1784be55a11dSMilanka Ringwald     }
1785be55a11dSMilanka Ringwald     if (!hfp_hf_enhanced_voice_recognition_supported(hfp_connection)){
1786be55a11dSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1787be55a11dSMilanka Ringwald     }
1788be55a11dSMilanka Ringwald     return hfp_hf_deactivate_voice_recognition(hfp_connection);
1789ce263fc8SMatthias Ringwald }
1790ce263fc8SMatthias Ringwald 
1791c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
17929c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1793a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1794a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1795a33eb0c4SMilanka Ringwald         return;
1796a33eb0c4SMilanka Ringwald     }
1797c8626498SMilanka Ringwald 
1798a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1799c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1800a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1801a0ffb263SMatthias Ringwald         return;
1802a0ffb263SMatthias Ringwald     }
1803a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1804a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
18051c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1806ce263fc8SMatthias Ringwald }
1807ce263fc8SMatthias Ringwald 
1808c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
18099c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1810a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1811a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1812a33eb0c4SMilanka Ringwald         return;
1813a33eb0c4SMilanka Ringwald     }
1814c8626498SMilanka Ringwald 
1815a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1816c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
1817a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1818a0ffb263SMatthias Ringwald         return;
1819a0ffb263SMatthias Ringwald     }
1820a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1821a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
18221c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1823ce263fc8SMatthias Ringwald }
1824ce263fc8SMatthias Ringwald 
1825c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
18269c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1827a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1828a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1829a33eb0c4SMilanka Ringwald         return;
1830a33eb0c4SMilanka Ringwald     }
1831a33eb0c4SMilanka Ringwald 
1832a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
18331c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1834ce263fc8SMatthias Ringwald }
1835ce263fc8SMatthias Ringwald 
1836c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
18379c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1838a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1839a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1840a33eb0c4SMilanka Ringwald         return;
1841a33eb0c4SMilanka Ringwald     }
1842a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
18431c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1844ce263fc8SMatthias Ringwald }
18453deb3ec6SMatthias Ringwald 
1846c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
18479c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1848a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1849a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1850a33eb0c4SMilanka Ringwald         return;
1851a33eb0c4SMilanka Ringwald     }
1852a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
18531c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1854667ec068SMatthias Ringwald }
1855667ec068SMatthias Ringwald 
1856667ec068SMatthias Ringwald 
1857c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
18589c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1859a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1860a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1861a33eb0c4SMilanka Ringwald         return;
1862a33eb0c4SMilanka Ringwald     }
1863a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1864a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
18651c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1866667ec068SMatthias Ringwald }
1867667ec068SMatthias Ringwald 
1868c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
18699c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1870a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1871a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1872a33eb0c4SMilanka Ringwald         return;
1873a33eb0c4SMilanka Ringwald     }
1874a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1875a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
18761c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1877667ec068SMatthias Ringwald }
1878667ec068SMatthias Ringwald 
1879c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
18809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1881a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1882a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1883a33eb0c4SMilanka Ringwald         return;
1884a33eb0c4SMilanka Ringwald     }
1885a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1886a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
18871c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1888667ec068SMatthias Ringwald }
1889667ec068SMatthias Ringwald 
1890c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
18919c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1892a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1893a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1894a33eb0c4SMilanka Ringwald         return;
1895a33eb0c4SMilanka Ringwald     }
1896a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1897a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
18981c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1899667ec068SMatthias Ringwald }
1900667ec068SMatthias Ringwald 
1901c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
19029c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1903a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1904a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1905a33eb0c4SMilanka Ringwald         return;
1906a33eb0c4SMilanka Ringwald     }
1907a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
19081c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1909667ec068SMatthias Ringwald }
1910667ec068SMatthias Ringwald 
1911c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
19129c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1913a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1914a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1915a33eb0c4SMilanka Ringwald         return;
1916a33eb0c4SMilanka Ringwald     }
1917667ec068SMatthias Ringwald     // find index for assigned number
1918667ec068SMatthias Ringwald     int i;
1919667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1920667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1921667ec068SMatthias Ringwald             // set value
1922667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1923667ec068SMatthias Ringwald             // mark for update
1924a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1925a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1926667ec068SMatthias Ringwald                 // send update
19271c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
1928a0ffb263SMatthias Ringwald             }
1929667ec068SMatthias Ringwald             return;
1930667ec068SMatthias Ringwald         }
1931667ec068SMatthias Ringwald     }
1932667ec068SMatthias Ringwald }
1933667ec068SMatthias Ringwald 
1934d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
1935d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1936d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
1937d7f6b5cbSMatthias Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1938d7f6b5cbSMatthias Ringwald         return 0;
1939d7f6b5cbSMatthias Ringwald     }
1940d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1941d7f6b5cbSMatthias Ringwald }
194276cc1527SMatthias Ringwald 
194376cc1527SMatthias 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){
194476cc1527SMatthias Ringwald 	if (!name){
194576cc1527SMatthias Ringwald 		name = default_hfp_hf_service_name;
194676cc1527SMatthias Ringwald 	}
194776cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
194876cc1527SMatthias Ringwald 
194976cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
195076cc1527SMatthias Ringwald 	//
195176cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
195276cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
195376cc1527SMatthias Ringwald 	//
195476cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
195576cc1527SMatthias Ringwald 	//
195676cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
1957ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
195876cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
195976cc1527SMatthias Ringwald 	}
1960ef3ae4ebSMilanka Ringwald 
1961ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
196256f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
1963ef3ae4ebSMilanka Ringwald     }
1964ef3ae4ebSMilanka Ringwald 
1965ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
196656f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
1967ef3ae4ebSMilanka Ringwald     }
1968ef3ae4ebSMilanka Ringwald 
196976cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
197076cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
197176cc1527SMatthias Ringwald }
197276cc1527SMatthias Ringwald 
197376cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
197468466199SMilanka Ringwald 	btstack_assert(callback != NULL);
197568466199SMilanka Ringwald 
197676cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
197776cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
197876cc1527SMatthias Ringwald }
1979