xref: /btstack/src/classic/hfp_hf.c (revision c10fde09c0e6adff9bc266486a6e8c926b9c96a4)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp_hf.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
42fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493cfa4086SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <string.h>
513deb3ec6SMatthias Ringwald 
52235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
5359c6af15SMatthias Ringwald #include "btstack_debug.h"
54d4dd47ffSMatthias Ringwald #include "btstack_event.h"
553deb3ec6SMatthias Ringwald #include "btstack_memory.h"
5659c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5759c6af15SMatthias Ringwald #include "classic/core.h"
5859c6af15SMatthias Ringwald #include "classic/hfp.h"
5959c6af15SMatthias Ringwald #include "classic/hfp_hf.h"
60efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
61746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
62023f2764SMatthias Ringwald #include "classic/sdp_util.h"
6359c6af15SMatthias Ringwald #include "hci.h"
6459c6af15SMatthias Ringwald #include "hci_cmd.h"
6559c6af15SMatthias Ringwald #include "hci_dump.h"
6659c6af15SMatthias Ringwald #include "l2cap.h"
673deb3ec6SMatthias Ringwald 
6820b2edb6SMatthias Ringwald // const
69aeb0f0feSMatthias Ringwald static const char hfp_hf_default_service_name[] = "Hands-Free unit";
7020b2edb6SMatthias Ringwald 
7120b2edb6SMatthias Ringwald // globals
72aeb0f0feSMatthias Ringwald 
73aeb0f0feSMatthias Ringwald // higher layer callbacks
74aeb0f0feSMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
75aeb0f0feSMatthias Ringwald 
761c6a0fc0SMatthias Ringwald static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration;
7727950165SMatthias Ringwald 
78aeb0f0feSMatthias Ringwald static uint16_t hfp_hf_supported_features;
79aeb0f0feSMatthias Ringwald static uint8_t  hfp_hf_codecs_nr;
80aeb0f0feSMatthias Ringwald static uint8_t  hfp_hf_codecs[HFP_MAX_NUM_CODECS];
813deb3ec6SMatthias Ringwald 
82aeb0f0feSMatthias Ringwald static uint8_t  hfp_hf_indicators_nr;
83aeb0f0feSMatthias Ringwald static uint8_t  hfp_hf_indicators[HFP_MAX_NUM_INDICATORS];
84aeb0f0feSMatthias Ringwald static uint32_t hfp_hf_indicators_value[HFP_MAX_NUM_INDICATORS];
85667ec068SMatthias Ringwald 
8620b2edb6SMatthias Ringwald static uint8_t  hfp_hf_speaker_gain;
8720b2edb6SMatthias Ringwald static uint8_t  hfp_hf_microphone_gain;
883deb3ec6SMatthias Ringwald 
89aeb0f0feSMatthias Ringwald static hfp_call_status_t      hfp_hf_call_status;
90aeb0f0feSMatthias Ringwald static hfp_callsetup_status_t hfp_hf_callsetup_status;
91aeb0f0feSMatthias Ringwald static hfp_callheld_status_t  hfp_hf_callheld_status;
923deb3ec6SMatthias Ringwald 
93aeb0f0feSMatthias Ringwald static char hfp_hf_phone_number[25];
94ce263fc8SMatthias Ringwald 
95ce263fc8SMatthias Ringwald 
9676cc1527SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
97aeb0f0feSMatthias Ringwald 	int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
9876cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
9976cc1527SMatthias Ringwald 	return hf && ag;
10076cc1527SMatthias Ringwald }
10176cc1527SMatthias Ringwald 
10276cc1527SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
103aeb0f0feSMatthias Ringwald 	int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_THREE_WAY_CALLING);
10476cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
10576cc1527SMatthias Ringwald 	return hf && ag;
10676cc1527SMatthias Ringwald }
10776cc1527SMatthias Ringwald 
10876cc1527SMatthias Ringwald 
10976cc1527SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
110aeb0f0feSMatthias Ringwald 	int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_HF_INDICATORS);
11176cc1527SMatthias Ringwald 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
11276cc1527SMatthias Ringwald 	return hf && ag;
11376cc1527SMatthias Ringwald }
11476cc1527SMatthias Ringwald 
115fcf4ede6SMilanka Ringwald static bool hfp_hf_vra_flag_supported(hfp_connection_t * hfp_connection){
116fcf4ede6SMilanka Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION);
117fcf4ede6SMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION);
118fcf4ede6SMilanka Ringwald     return hf && ag;
119fcf4ede6SMilanka Ringwald }
120fcf4ede6SMilanka Ringwald 
121fcf4ede6SMilanka Ringwald static bool hfp_hf_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){
122fcf4ede6SMilanka Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS);
123fcf4ede6SMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS);
124fcf4ede6SMilanka Ringwald     return hf && ag;
125fcf4ede6SMilanka Ringwald }
12676cc1527SMatthias Ringwald 
1279c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
1289c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1299c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1309c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1319c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1329c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1339c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1349c9c64c1SMatthias Ringwald         return hfp_connection;
1359c9c64c1SMatthias Ringwald     }
1369c9c64c1SMatthias Ringwald     return NULL;
1379c9c64c1SMatthias Ringwald }
1389c9c64c1SMatthias Ringwald 
139*c10fde09SMatthias Ringwald /* emit functions */
1403deb3ec6SMatthias Ringwald 
141a473a009SMatthias Ringwald static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){
142a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
143*c10fde09SMatthias Ringwald     uint16_t bnip_number_len = btstack_min(strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1);
144*c10fde09SMatthias Ringwald     uint8_t event[7 + sizeof(hfp_connection->bnip_number)];
145a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
146*c10fde09SMatthias Ringwald     event[1] = 6 + bnip_number_len;
147a473a009SMatthias Ringwald     event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION;
148d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
149d703d377SMatthias Ringwald     event[5] = status;
150d703d377SMatthias Ringwald     event[6] = hfp_connection->bnip_type;
151*c10fde09SMatthias Ringwald     memcpy(&event[7], hfp_connection->bnip_number, bnip_number_len);
152*c10fde09SMatthias Ringwald     event[7 + bnip_number_len] = 0;
153*c10fde09SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 8 + bnip_number_len);
154a0ffb263SMatthias Ringwald }
155a0ffb263SMatthias Ringwald 
156a473a009SMatthias Ringwald static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){
157a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
158*c10fde09SMatthias Ringwald     uint16_t bnip_number_len = btstack_min(strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1);
159*c10fde09SMatthias Ringwald     uint8_t event[6 + sizeof(hfp_connection->bnip_number)];
160a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
161*c10fde09SMatthias Ringwald     event[1] = 5 + bnip_number_len;
162a0ffb263SMatthias Ringwald     event[2] = event_subtype;
163d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
164d703d377SMatthias Ringwald     event[5] = hfp_connection->bnip_type;
165*c10fde09SMatthias Ringwald     memcpy(&event[6], hfp_connection->bnip_number, bnip_number_len);
166*c10fde09SMatthias Ringwald     event[6 + bnip_number_len] = 0;
167*c10fde09SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 7 + bnip_number_len);
168a0ffb263SMatthias Ringwald }
169a0ffb263SMatthias Ringwald 
170a473a009SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){
171a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
172*c10fde09SMatthias Ringwald     uint16_t bnip_number_len = btstack_min(strlen(hfp_connection->bnip_number), sizeof(hfp_connection->bnip_number)-1);
173*c10fde09SMatthias Ringwald     uint8_t event[11 + sizeof(hfp_connection->bnip_number)];
174*c10fde09SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
175*c10fde09SMatthias Ringwald     event[1] = 10 + bnip_number_len;
176*c10fde09SMatthias Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
177*c10fde09SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
178*c10fde09SMatthias Ringwald     event[4] = hfp_connection->clcc_idx;
179*c10fde09SMatthias Ringwald     event[5] = hfp_connection->clcc_dir;
180*c10fde09SMatthias Ringwald     event[6] = hfp_connection->clcc_status;
181*c10fde09SMatthias Ringwald     event[7] = hfp_connection->clcc_mode;
182*c10fde09SMatthias Ringwald     event[8] = hfp_connection->clcc_mpty;
183*c10fde09SMatthias Ringwald     event[9] = hfp_connection->bnip_type;
184*c10fde09SMatthias Ringwald     memcpy(&event[10], hfp_connection->bnip_number, bnip_number_len);
185*c10fde09SMatthias Ringwald     event[11+bnip_number_len] = 0;
186*c10fde09SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 12+bnip_number_len);
187a0ffb263SMatthias Ringwald }
188a0ffb263SMatthias Ringwald 
1891ac1f60fSMilanka Ringwald static void hfp_emit_ag_indicator_mapping_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
1901ac1f60fSMilanka Ringwald     if (hfp_hf_callback == NULL) return;
191*c10fde09SMatthias Ringwald     uint8_t event[8 + HFP_MAX_INDICATOR_DESC_SIZE];
192*c10fde09SMatthias Ringwald     uint16_t indicator_len = btstack_min(strlen(indicator->name), HFP_MAX_INDICATOR_DESC_SIZE-1);
193*c10fde09SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
194*c10fde09SMatthias Ringwald     event[1] = 7 + indicator_len;
195*c10fde09SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_MAPPING;
196*c10fde09SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
197*c10fde09SMatthias Ringwald     event[5] = indicator->index;
198*c10fde09SMatthias Ringwald     event[6] = indicator->min_range;
199*c10fde09SMatthias Ringwald     event[7] = indicator->max_range;
200*c10fde09SMatthias Ringwald     memcpy(&event[8], indicator->name, indicator_len);
201*c10fde09SMatthias Ringwald     event[8+indicator_len] = 0;
202*c10fde09SMatthias Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 9 + indicator_len);
2031ac1f60fSMilanka Ringwald }
20476cc1527SMatthias Ringwald 
205ce3797e1SMilanka Ringwald static void hfp_emit_ag_indicator_status_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
206a473a009SMatthias Ringwald 	if (hfp_hf_callback == NULL) return;
207*c10fde09SMatthias Ringwald 	uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE];
208*c10fde09SMatthias Ringwald     uint16_t indicator_len = btstack_min(strlen(indicator->name), HFP_MAX_INDICATOR_DESC_SIZE-1);
209*c10fde09SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
210*c10fde09SMatthias Ringwald 	event[1] = 11 + indicator_len;
211*c10fde09SMatthias Ringwald 	event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
212*c10fde09SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
213*c10fde09SMatthias Ringwald 	event[5] = indicator->index;
214*c10fde09SMatthias Ringwald 	event[6] = indicator->status;
215*c10fde09SMatthias Ringwald 	event[7] = indicator->min_range;
216*c10fde09SMatthias Ringwald 	event[8] = indicator->max_range;
217*c10fde09SMatthias Ringwald 	event[9] = indicator->mandatory;
218*c10fde09SMatthias Ringwald 	event[10] = indicator->enabled;
219*c10fde09SMatthias Ringwald 	event[11] = indicator->status_changed;
220*c10fde09SMatthias Ringwald 	memcpy(&event[12], indicator->name, indicator_len);
221*c10fde09SMatthias Ringwald 	event[12+indicator_len] = 0;
222*c10fde09SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 13 + indicator_len);
2233deb3ec6SMatthias Ringwald }
2243deb3ec6SMatthias Ringwald 
225a473a009SMatthias Ringwald static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){
226a473a009SMatthias Ringwald     if (hfp_hf_callback == NULL) return;
227*c10fde09SMatthias Ringwald     uint16_t operator_len = btstack_min(strlen(hfp_connection->network_operator.name), HFP_MAX_NETWORK_OPERATOR_NAME_SIZE-1);
228*c10fde09SMatthias Ringwald 	uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE];
22976cc1527SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
23076cc1527SMatthias Ringwald 	event[1] = sizeof(event) - 2;
231*c10fde09SMatthias Ringwald 	event[2] = 6 + operator_len;
232d703d377SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
233d703d377SMatthias Ringwald 	event[5] = hfp_connection->network_operator.mode;
234d703d377SMatthias Ringwald 	event[6] = hfp_connection->network_operator.format;
235*c10fde09SMatthias Ringwald 	memcpy(&event[7], hfp_connection->network_operator.name, operator_len);
236*c10fde09SMatthias Ringwald 	event[7+operator_len] = 0;
237*c10fde09SMatthias Ringwald 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, 8 + operator_len);
2383deb3ec6SMatthias Ringwald }
2393deb3ec6SMatthias Ringwald 
240b95cac54SMilanka Ringwald 
241b95cac54SMilanka Ringwald static void hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection){
242b95cac54SMilanka Ringwald     btstack_assert(hfp_connection != NULL);
24351aa5d5aSMilanka Ringwald     uint8_t event[HFP_MAX_VR_TEXT_SIZE + 11];
244b95cac54SMilanka Ringwald     int pos = 0;
245b95cac54SMilanka Ringwald     event[pos++] = HCI_EVENT_HFP_META;
246b95cac54SMilanka Ringwald     event[pos++] = sizeof(event) - 2;
247b95cac54SMilanka Ringwald     event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE;
248b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->acl_handle);
249b95cac54SMilanka Ringwald     pos += 2;
250b95cac54SMilanka Ringwald     little_endian_store_16(event, pos, hfp_connection->ag_msg.text_id);
251b95cac54SMilanka Ringwald     pos += 2;
252b95cac54SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_type;
253e83f1be7SMilanka Ringwald     event[pos++] = hfp_connection->ag_msg.text_operation;
254b95cac54SMilanka Ringwald 
25551aa5d5aSMilanka Ringwald     // length, zero ending is already in message
256b95cac54SMilanka Ringwald     uint8_t * value = &hfp_connection->line_buffer[0];
25751aa5d5aSMilanka Ringwald     uint16_t  value_length = hfp_connection->ag_vra_msg_length;
25851aa5d5aSMilanka Ringwald 
25951aa5d5aSMilanka Ringwald     little_endian_store_16(event, pos, value_length);
260b95cac54SMilanka Ringwald     pos += 2;
26151aa5d5aSMilanka Ringwald     memcpy(&event[pos], value, value_length);
26251aa5d5aSMilanka Ringwald     pos += value_length;
26351aa5d5aSMilanka Ringwald 
264b95cac54SMilanka Ringwald     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
265b95cac54SMilanka Ringwald }
266b95cac54SMilanka Ringwald 
26776cc1527SMatthias Ringwald /* send commands */
26889425bfcSMilanka Ringwald 
26989425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
2703deb3ec6SMatthias Ringwald     char buffer[20];
2711599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
27289425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
27389425bfcSMilanka Ringwald }
27489425bfcSMilanka Ringwald 
27589425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
27689425bfcSMilanka Ringwald     char buffer[20];
2771599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
27889425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
27989425bfcSMilanka Ringwald }
28089425bfcSMilanka Ringwald 
28186da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
28289425bfcSMilanka Ringwald     char buffer[40];
2831599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
2843deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2853deb3ec6SMatthias Ringwald }
2863deb3ec6SMatthias Ringwald 
2873deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2883deb3ec6SMatthias Ringwald     char buffer[30];
28989425bfcSMilanka Ringwald     const int size = sizeof(buffer);
29089425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
291aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_codecs, hfp_hf_codecs_nr);
2921599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
2933deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2943deb3ec6SMatthias Ringwald }
2953deb3ec6SMatthias Ringwald 
2963deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2973deb3ec6SMatthias Ringwald     char buffer[50];
29889425bfcSMilanka Ringwald     const int size = sizeof(buffer);
29989425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
30089425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
3011599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
3023deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3033deb3ec6SMatthias Ringwald }
3043deb3ec6SMatthias Ringwald 
3053deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
3063deb3ec6SMatthias Ringwald     char buffer[30];
30789425bfcSMilanka Ringwald     const int size = sizeof(buffer);
30889425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
309aeb0f0feSMatthias Ringwald     offset += join(buffer+offset, size-offset, hfp_hf_indicators, hfp_hf_indicators_nr);
3101599fe57SMatthias Ringwald     offset += snprintf(buffer+offset, size-offset, "\r");
3113deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
3123deb3ec6SMatthias Ringwald }
3133deb3ec6SMatthias Ringwald 
31489425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
3153deb3ec6SMatthias Ringwald     char buffer[20];
3161599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
317ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
318ce263fc8SMatthias Ringwald }
319ce263fc8SMatthias Ringwald 
320ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
321ce263fc8SMatthias Ringwald     char buffer[40];
322aeb0f0feSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, hfp_hf_phone_number);
323ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
324ce263fc8SMatthias Ringwald }
325ce263fc8SMatthias Ringwald 
326a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
327ce263fc8SMatthias Ringwald     char buffer[40];
3281599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
329ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
330ce263fc8SMatthias Ringwald }
331ce263fc8SMatthias Ringwald 
332f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
33389425bfcSMilanka Ringwald     char buffer[40];
3341599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
335ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
336ce263fc8SMatthias Ringwald }
337ce263fc8SMatthias Ringwald 
338ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
339ce263fc8SMatthias Ringwald     char buffer[20];
3401599fe57SMatthias Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
341ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
342ce263fc8SMatthias Ringwald }
343ce263fc8SMatthias Ringwald 
34497d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
3451599fe57SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "ATA\r");
34697d2cadbSMatthias Ringwald }
34797d2cadbSMatthias Ringwald 
34889425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
349aeb0f0feSMatthias Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_hf_supported_features);
35089425bfcSMilanka Ringwald }
35189425bfcSMilanka Ringwald 
35289425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
35389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
35489425bfcSMilanka Ringwald }
35589425bfcSMilanka Ringwald 
35689425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
35789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
35889425bfcSMilanka Ringwald }
35989425bfcSMilanka Ringwald 
36089425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
36189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
36289425bfcSMilanka Ringwald }
36389425bfcSMilanka Ringwald 
36489425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
36589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
36689425bfcSMilanka Ringwald }
36789425bfcSMilanka Ringwald 
36889425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
36989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
37089425bfcSMilanka Ringwald }
37189425bfcSMilanka Ringwald 
37289425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
37389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
37489425bfcSMilanka Ringwald }
37589425bfcSMilanka Ringwald 
37689425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
37789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
37889425bfcSMilanka Ringwald }
37989425bfcSMilanka Ringwald 
38089425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
38189425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
38289425bfcSMilanka Ringwald }
38389425bfcSMilanka Ringwald 
38489425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
38589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
38689425bfcSMilanka Ringwald }
38789425bfcSMilanka Ringwald 
38889425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
38989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
39089425bfcSMilanka Ringwald }
39189425bfcSMilanka Ringwald 
39289425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
39389425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
39489425bfcSMilanka Ringwald }
39589425bfcSMilanka Ringwald 
39689425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
39789425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
39889425bfcSMilanka Ringwald }
39989425bfcSMilanka Ringwald 
40089425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
40189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
40289425bfcSMilanka Ringwald }
40389425bfcSMilanka Ringwald 
40489425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
40589425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
40689425bfcSMilanka Ringwald }
40789425bfcSMilanka Ringwald 
40889425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
40989425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
41089425bfcSMilanka Ringwald }
41189425bfcSMilanka Ringwald 
41289425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
41389425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
41489425bfcSMilanka Ringwald }
41589425bfcSMilanka Ringwald 
41689425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
41789425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
41889425bfcSMilanka Ringwald }
41989425bfcSMilanka Ringwald 
420ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
42189425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
422ce263fc8SMatthias Ringwald }
423ce263fc8SMatthias Ringwald 
424667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
42589425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
426667ec068SMatthias Ringwald }
427667ec068SMatthias Ringwald 
42876cc1527SMatthias Ringwald /* state machines */
4293deb3ec6SMatthias Ringwald 
430a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
431a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
432a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
433aa4dd815SMatthias Ringwald     int done = 1;
434498a8121SMilanka Ringwald     log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state);
435a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4363deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
437aeb0f0feSMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_hf_codecs, &hfp_hf_codecs_nr);
438a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
439a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4403deb3ec6SMatthias Ringwald             break;
4413deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
442a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
443a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4443deb3ec6SMatthias Ringwald             break;
4453deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
446a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
447a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4483deb3ec6SMatthias Ringwald             break;
4493deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
450a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
451a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4523deb3ec6SMatthias Ringwald             break;
4533deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
454a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
455a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4563deb3ec6SMatthias Ringwald             break;
4573deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
458a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
459a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4603deb3ec6SMatthias Ringwald             break;
4613deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
462a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
463a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4643deb3ec6SMatthias Ringwald             break;
4653deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
466a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
467a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4683deb3ec6SMatthias Ringwald             break;
4693deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
470a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
471a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4723deb3ec6SMatthias Ringwald             break;
4733deb3ec6SMatthias Ringwald         default:
474aa4dd815SMatthias Ringwald             done = 0;
4753deb3ec6SMatthias Ringwald             break;
4763deb3ec6SMatthias Ringwald     }
4773deb3ec6SMatthias Ringwald     return done;
4783deb3ec6SMatthias Ringwald }
4793deb3ec6SMatthias Ringwald 
480ce263fc8SMatthias Ringwald 
481a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
482a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
483498a8121SMilanka Ringwald     if (hfp_connection->ok_pending){
484498a8121SMilanka Ringwald         return 0;
485498a8121SMilanka Ringwald     }
486ce263fc8SMatthias Ringwald     int done = 0;
487a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
488a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
489ce263fc8SMatthias Ringwald         done = 1;
490a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
491ce263fc8SMatthias Ringwald         return done;
492ce263fc8SMatthias Ringwald     };
493a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
494a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
495ce263fc8SMatthias Ringwald         done = 1;
496a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
497a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
498a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
499ce263fc8SMatthias Ringwald         return done;
500ce263fc8SMatthias Ringwald     }
501ce263fc8SMatthias Ringwald 
502a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
503ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
504a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
505a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
506a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
507ce263fc8SMatthias Ringwald             return 1;
508ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
509a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
510a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
511a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
512ce263fc8SMatthias Ringwald             return 1;
513ce263fc8SMatthias Ringwald         default:
514ce263fc8SMatthias Ringwald             break;
515ce263fc8SMatthias Ringwald     }
516ce263fc8SMatthias Ringwald 
517a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
518a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
519ce263fc8SMatthias Ringwald         done = 1;
520a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
521ce263fc8SMatthias Ringwald         return done;
522ce263fc8SMatthias Ringwald     }
523ce263fc8SMatthias Ringwald 
524ce263fc8SMatthias Ringwald     return done;
525ce263fc8SMatthias Ringwald }
526ce263fc8SMatthias Ringwald 
527af97579eSMilanka Ringwald static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
528be55a11dSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
529be55a11dSMilanka Ringwald         return 0;
530be55a11dSMilanka Ringwald     }
531be55a11dSMilanka Ringwald     int done = 0;
532fd4151d1SMilanka Ringwald 
5330b4debbfSMilanka Ringwald     if (hfp_connection->ok_pending == 1){
5340b4debbfSMilanka Ringwald         return 0;
5350b4debbfSMilanka Ringwald     }
5360b4debbfSMilanka Ringwald     // voice recognition activated from AG
5370b4debbfSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){
5380b4debbfSMilanka Ringwald         switch(hfp_connection->vra_state_requested){
5390b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
5400b4debbfSMilanka Ringwald             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
541de9e0ea7SMilanka Ringwald             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
5420b4debbfSMilanka Ringwald                 // ignore AG command, continue to wait for OK
5430b4debbfSMilanka Ringwald                 return 0;
544cf75be85SMilanka Ringwald 
5450b4debbfSMilanka Ringwald             default:
546b95cac54SMilanka Ringwald                 if (hfp_connection->ag_vra_msg_length > 0){
547b95cac54SMilanka Ringwald                     hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection);
548b95cac54SMilanka Ringwald                     hfp_connection->ag_vra_msg_length = 0;
549b95cac54SMilanka Ringwald                     break;
550b95cac54SMilanka Ringwald                 }
551cf75be85SMilanka Ringwald                 switch(hfp_connection->ag_vra_state){
552cf75be85SMilanka Ringwald                     case HFP_VOICE_RECOGNITION_STATE_AG_READY:
553013cc750SMilanka Ringwald                         switch (hfp_connection->ag_vra_status){
554013cc750SMilanka Ringwald                             case 0:
5550b4debbfSMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
556013cc750SMilanka Ringwald                                 break;
557013cc750SMilanka Ringwald                             case 1:
558013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
559013cc750SMilanka Ringwald                                 break;
560013cc750SMilanka Ringwald                             case 2:
561013cc750SMilanka Ringwald                                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
562013cc750SMilanka Ringwald                                 break;
563013cc750SMilanka Ringwald                             default:
564013cc750SMilanka Ringwald                                 break;
5650b4debbfSMilanka Ringwald                         }
5660b4debbfSMilanka Ringwald                         break;
567cf75be85SMilanka Ringwald                     default:
568cf75be85SMilanka Ringwald                         // state messages from AG
569cf75be85SMilanka Ringwald                         hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
5708d5005b5SMilanka Ringwald                         hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
571cf75be85SMilanka Ringwald                         break;
572cf75be85SMilanka Ringwald                 }
573cf75be85SMilanka Ringwald                 break;
5740b4debbfSMilanka Ringwald         }
5750b4debbfSMilanka Ringwald         hfp_connection->command = HFP_CMD_NONE;
5760b4debbfSMilanka Ringwald     }
5770b4debbfSMilanka Ringwald 
5780b4debbfSMilanka Ringwald 
579498a8121SMilanka Ringwald     switch (hfp_connection->vra_state_requested){
580fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
581fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
582fdda66c0SMilanka Ringwald             if (done != 0){
583fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
584498a8121SMilanka Ringwald                 hfp_connection->ok_pending = 1;
585498a8121SMilanka Ringwald             }
586fd4151d1SMilanka Ringwald             return 1;
587fd4151d1SMilanka Ringwald 
588fd4151d1SMilanka Ringwald 
589fdda66c0SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
590fdda66c0SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
591fdda66c0SMilanka Ringwald             if (done != 0){
592fdda66c0SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
593fd4151d1SMilanka Ringwald                 hfp_connection->ok_pending = 1;
594fd4151d1SMilanka Ringwald                 return 1;
5950b4debbfSMilanka Ringwald             }
5960b4debbfSMilanka Ringwald             break;
597013cc750SMilanka Ringwald 
598de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
599de9e0ea7SMilanka Ringwald             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
600de9e0ea7SMilanka Ringwald             if (done != 0){
601de9e0ea7SMilanka Ringwald                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
602de9e0ea7SMilanka Ringwald                 hfp_connection->ok_pending = 1;
603de9e0ea7SMilanka Ringwald                 return 1;
604de9e0ea7SMilanka Ringwald             }
605de9e0ea7SMilanka Ringwald             break;
606de9e0ea7SMilanka Ringwald 
607de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
608de9e0ea7SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
609de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
610de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
611de9e0ea7SMilanka Ringwald             if (hfp_connection->activate_voice_recognition){
612fcf4ede6SMilanka Ringwald                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
613de9e0ea7SMilanka Ringwald                 hfp_hf_activate_voice_recognition(hfp_connection->acl_handle);
614de9e0ea7SMilanka Ringwald             } else {
615553a4a56SMilanka Ringwald                 hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
616de9e0ea7SMilanka Ringwald             }
617de9e0ea7SMilanka Ringwald             break;
618de9e0ea7SMilanka Ringwald 
619be55a11dSMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
620498a8121SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
621498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
622de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
623de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
624de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
625de9e0ea7SMilanka Ringwald             } else {
626fcf4ede6SMilanka Ringwald                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
62784fb9ac1SMilanka Ringwald                 if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
628553a4a56SMilanka Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
62984fb9ac1SMilanka Ringwald                 } else {
63084fb9ac1SMilanka Ringwald                     // postpone VRA event to simplify application logic
63184fb9ac1SMilanka Ringwald                     hfp_connection->emit_vra_enabled_after_audio_established = true;
63284fb9ac1SMilanka Ringwald                 }
633de9e0ea7SMilanka Ringwald             }
634be55a11dSMilanka Ringwald             break;
635be55a11dSMilanka Ringwald 
636de9e0ea7SMilanka Ringwald 
637013cc750SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
638013cc750SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
639498a8121SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
640de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = false;
641de9e0ea7SMilanka Ringwald             if (hfp_connection->deactivate_voice_recognition){
642de9e0ea7SMilanka Ringwald                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
643de9e0ea7SMilanka Ringwald             } else {
644de9e0ea7SMilanka Ringwald                 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
645de9e0ea7SMilanka Ringwald             }
646be55a11dSMilanka Ringwald             break;
647fd4151d1SMilanka Ringwald 
648be55a11dSMilanka Ringwald         default:
649be55a11dSMilanka Ringwald             break;
650be55a11dSMilanka Ringwald     }
651be55a11dSMilanka Ringwald     return done;
652be55a11dSMilanka Ringwald }
653be55a11dSMilanka Ringwald 
654be55a11dSMilanka Ringwald 
655be55a11dSMilanka Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
656a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
657ce263fc8SMatthias Ringwald 
658332ca98fSMatthias Ringwald     if (hfp_connection->trigger_codec_exchange){
659332ca98fSMatthias Ringwald 		hfp_connection->trigger_codec_exchange = 0;
660ce263fc8SMatthias Ringwald 
661a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
662a0ffb263SMatthias Ringwald 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
663332ca98fSMatthias Ringwald 		return 1;
664332ca98fSMatthias Ringwald     }
665332ca98fSMatthias Ringwald 
6661cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_codec_confirm){
6671cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = false;
668ce263fc8SMatthias Ringwald 
669a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
670fcb08cdbSMilanka Ringwald 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
6711cc65c4fSMatthias Ringwald 		return 1;
6721cc65c4fSMatthias Ringwald     }
6731cc65c4fSMatthias Ringwald 
6741cc65c4fSMatthias Ringwald     if (hfp_connection->hf_send_supported_codecs){
6751cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = false;
6761cc65c4fSMatthias Ringwald 
677a0ffb263SMatthias Ringwald 		hfp_connection->ok_pending = 1;
678a0ffb263SMatthias Ringwald 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
6791cc65c4fSMatthias Ringwald 		return 1;
6801cc65c4fSMatthias Ringwald     }
681ce263fc8SMatthias Ringwald 
682ce263fc8SMatthias Ringwald     return 0;
683ce263fc8SMatthias Ringwald }
684ce263fc8SMatthias Ringwald 
685a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
686505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
687505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
688ce263fc8SMatthias Ringwald 
68964f19dedSMilanka Ringwald     if (hfp_connection->release_audio_connection){
690a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
691a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
692a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
693ce263fc8SMatthias Ringwald         return 1;
694ce263fc8SMatthias Ringwald     }
695ce263fc8SMatthias Ringwald 
696a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
697ce263fc8SMatthias Ringwald 
698ce263fc8SMatthias Ringwald     // run codecs exchange
699a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
700ce263fc8SMatthias Ringwald     if (done) return 1;
701ce263fc8SMatthias Ringwald 
70238200c1dSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
70338200c1dSMilanka Ringwald     if (hfp_connection->establish_audio_connection){
70438200c1dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
70538200c1dSMilanka Ringwald         hfp_connection->establish_audio_connection = 0;
70638200c1dSMilanka Ringwald         hfp_setup_synchronous_connection(hfp_connection);
70738200c1dSMilanka Ringwald         return 1;
70838200c1dSMilanka Ringwald     }
709ce263fc8SMatthias Ringwald     return 0;
710ce263fc8SMatthias Ringwald }
711ce263fc8SMatthias Ringwald 
71238200c1dSMilanka Ringwald 
713a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
714eaf2b0a1SMatthias Ringwald 
715eaf2b0a1SMatthias Ringwald 	if (hfp_connection->ok_pending) return 0;
716eaf2b0a1SMatthias Ringwald 
717a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
718a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
719a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
720ce263fc8SMatthias Ringwald         return 1;
721ce263fc8SMatthias Ringwald     }
722ce263fc8SMatthias Ringwald     return 0;
723ce263fc8SMatthias Ringwald }
724ce263fc8SMatthias Ringwald 
7251c6a0fc0SMatthias Ringwald static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
7267522e673SMatthias Ringwald 
72776cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
72876cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
72976cc1527SMatthias Ringwald 
73076cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
73176cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
73222387625SMatthias Ringwald 
73384fb9ac1SMilanka Ringwald     // emit postponed VRA event
73484fb9ac1SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){
73584fb9ac1SMilanka Ringwald         hfp_connection->emit_vra_enabled_after_audio_established = false;
73684fb9ac1SMilanka Ringwald         hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
73784fb9ac1SMilanka Ringwald     }
73884fb9ac1SMilanka Ringwald 
7393721a235SMatthias Ringwald 	// assert command could be sent
7403721a235SMatthias Ringwald 	if (hci_can_send_command_packet_now() == 0) return;
7413721a235SMatthias Ringwald 
7423721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
7433721a235SMatthias Ringwald     // WBS Disassociate
7443721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
7453721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
7463721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
7473721a235SMatthias Ringwald         return;
7483721a235SMatthias Ringwald     }
7493721a235SMatthias Ringwald     // Write Codec Config
7503721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
7513721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
7523721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
7533721a235SMatthias Ringwald         return;
7543721a235SMatthias Ringwald     }
7553721a235SMatthias Ringwald     // WBS Associate
7563721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
7573721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
7583721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
7593721a235SMatthias Ringwald         return;
7603721a235SMatthias Ringwald     }
7613721a235SMatthias Ringwald #endif
762689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
763689d4323SMatthias Ringwald     // Enable WBS
764689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
765689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
766689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
767689d4323SMatthias Ringwald         return;
768689d4323SMatthias Ringwald     }
769689d4323SMatthias Ringwald     // Write I2S/PCM params
770689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
771689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
772689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
773689d4323SMatthias Ringwald         return;
774689d4323SMatthias Ringwald     }
775689d4323SMatthias Ringwald     // Disable WBS
776689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
777689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
778689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
779689d4323SMatthias Ringwald         return;
780689d4323SMatthias Ringwald     }
781689d4323SMatthias Ringwald #endif
7822b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS
7832b5f92fdSMatthias Ringwald     if (hfp_connection->rtk_send_sco_config){
7842b5f92fdSMatthias Ringwald         hfp_connection->rtk_send_sco_config = false;
7852b5f92fdSMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
7862b5f92fdSMatthias Ringwald             log_info("RTK SCO: 16k + mSBC");
7872b5f92fdSMatthias Ringwald             hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x00, 0x00, 0x41);
7882b5f92fdSMatthias Ringwald         } else {
7892b5f92fdSMatthias Ringwald             log_info("RTK SCO: 16k + CVSD");
7902b5f92fdSMatthias Ringwald             hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x0c, 0x00, 0x01);
7912b5f92fdSMatthias Ringwald         }
7922b5f92fdSMatthias Ringwald         return;
7932b5f92fdSMatthias Ringwald     }
7942b5f92fdSMatthias Ringwald #endif
79548e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
79648e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
79748e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
79848e6eeeeSMatthias Ringwald         return;
79948e6eeeeSMatthias Ringwald     }
80048e6eeeeSMatthias Ringwald #endif
8013721a235SMatthias Ringwald 
802cb81d35dSMatthias Ringwald     if (hfp_connection->accept_sco){
803cb81d35dSMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
804cb81d35dSMatthias Ringwald         hfp_connection->accept_sco = 0;
8057522e673SMatthias Ringwald         // notify about codec selection if not done already
8067522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
8077522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
8087522e673SMatthias Ringwald         }
809cb81d35dSMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
8107522e673SMatthias Ringwald         return;
8117522e673SMatthias Ringwald     }
8127522e673SMatthias Ringwald 
813d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
814d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
815d4dd47ffSMatthias Ringwald         return;
816d4dd47ffSMatthias Ringwald     }
817a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
818ce263fc8SMatthias Ringwald     if (!done){
819a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
820ce263fc8SMatthias Ringwald     }
821ce263fc8SMatthias Ringwald     if (!done){
822c95b5b3cSMilanka Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
823be55a11dSMilanka Ringwald     }
824be55a11dSMilanka Ringwald     if (!done){
825c95b5b3cSMilanka Ringwald         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
826ce263fc8SMatthias Ringwald     }
827ce263fc8SMatthias Ringwald     if (!done){
828a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
829ce263fc8SMatthias Ringwald     }
830ce263fc8SMatthias Ringwald 
831e2c3b58dSMilanka Ringwald     // don't send a new command while ok still pending or SLC is not established
832e2c3b58dSMilanka Ringwald     if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){
833e2c3b58dSMilanka Ringwald         return;
834e2c3b58dSMilanka Ringwald     }
8351016a228SMatthias Ringwald 
836a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
837a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
838a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
839a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
840ce263fc8SMatthias Ringwald         return;
841ce263fc8SMatthias Ringwald     }
842ce263fc8SMatthias Ringwald 
843a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
844a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
845a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
846a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
847ce263fc8SMatthias Ringwald         return;
848ce263fc8SMatthias Ringwald     }
849ce263fc8SMatthias Ringwald 
850a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
851a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
852a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
853a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
854ce263fc8SMatthias Ringwald         return;
855ce263fc8SMatthias Ringwald     }
856ce263fc8SMatthias Ringwald 
857a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
858a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
859a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
860a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
861ce263fc8SMatthias Ringwald         return;
862ce263fc8SMatthias Ringwald     }
863ce263fc8SMatthias Ringwald 
864a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
865a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
86699af1e28SMilanka Ringwald         hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR;
867a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
868e2c3b58dSMilanka Ringwald         hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0);
869ce263fc8SMatthias Ringwald         return;
870ce263fc8SMatthias Ringwald     }
871ce263fc8SMatthias Ringwald 
872a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
873a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
874a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
875a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
876ce263fc8SMatthias Ringwald         return;
877ce263fc8SMatthias Ringwald     }
878ce263fc8SMatthias Ringwald 
879a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
880a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
881a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
882a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
883ce263fc8SMatthias Ringwald         return;
884ce263fc8SMatthias Ringwald     }
885ce263fc8SMatthias Ringwald 
886a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
887a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
888a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
889a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
890ce263fc8SMatthias Ringwald         return;
891ce263fc8SMatthias Ringwald     }
892ce263fc8SMatthias Ringwald 
893a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
894a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
895a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
896a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
897ce263fc8SMatthias Ringwald         return;
898ce263fc8SMatthias Ringwald     }
899ce263fc8SMatthias Ringwald 
900a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
901a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
902a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
903a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
904ce263fc8SMatthias Ringwald         return;
905ce263fc8SMatthias Ringwald     }
906ce263fc8SMatthias Ringwald 
907a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
908a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
909a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
910a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
911ce263fc8SMatthias Ringwald         return;
912ce263fc8SMatthias Ringwald     }
913ce263fc8SMatthias Ringwald 
914a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
915a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
916a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
917a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
918ce263fc8SMatthias Ringwald         return;
919ce263fc8SMatthias Ringwald     }
920ce263fc8SMatthias Ringwald 
921a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
922a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
923a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
924a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
925ce263fc8SMatthias Ringwald         return;
926ce263fc8SMatthias Ringwald     }
927ce263fc8SMatthias Ringwald 
928a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
929a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
930a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
931a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
932ce263fc8SMatthias Ringwald         return;
933ce263fc8SMatthias Ringwald     }
934ce263fc8SMatthias Ringwald 
935a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
936a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
937a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
938a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
939ce263fc8SMatthias Ringwald         return;
940ce263fc8SMatthias Ringwald     }
941ce263fc8SMatthias Ringwald 
942a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
943a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
944a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
945a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
946ce263fc8SMatthias Ringwald         return;
947ce263fc8SMatthias Ringwald     }
948ce263fc8SMatthias Ringwald 
949a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
950a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
951a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
952a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
953667ec068SMatthias Ringwald         return;
954667ec068SMatthias Ringwald     }
955667ec068SMatthias Ringwald 
956a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
957a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
958a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
959a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
960a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
961ce263fc8SMatthias Ringwald         return;
962ce263fc8SMatthias Ringwald     }
963ce263fc8SMatthias Ringwald 
964a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
965a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
966a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
967a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
968ce263fc8SMatthias Ringwald         return;
969ce263fc8SMatthias Ringwald     }
970ce263fc8SMatthias Ringwald 
971a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
972a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
973a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
974a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
975667ec068SMatthias Ringwald         return;
976667ec068SMatthias Ringwald     }
977667ec068SMatthias Ringwald 
978a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
979a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
980667ec068SMatthias Ringwald         char buffer[20];
981a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
982667ec068SMatthias Ringwald             case '?':
9831599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
984ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD);
985ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
986a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
987667ec068SMatthias Ringwald                 return;
988667ec068SMatthias Ringwald             case '0':
989667ec068SMatthias Ringwald             case '1':
990667ec068SMatthias Ringwald             case '2':
9911599fe57SMatthias Ringwald                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
992ff7d6aeaSMatthias Ringwald                          HFP_RESPONSE_AND_HOLD,
993ff7d6aeaSMatthias Ringwald                          hfp_connection->hf_send_rrh_command);
994ff7d6aeaSMatthias Ringwald                 buffer[sizeof(buffer) - 1] = 0;
995a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
996667ec068SMatthias Ringwald                 return;
997667ec068SMatthias Ringwald             default:
998667ec068SMatthias Ringwald                 break;
999667ec068SMatthias Ringwald         }
1000667ec068SMatthias Ringwald         return;
1001667ec068SMatthias Ringwald     }
1002667ec068SMatthias Ringwald 
1003a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
1004a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
1005667ec068SMatthias Ringwald         char buffer[20];
10061599fe57SMatthias Ringwald         snprintf(buffer, sizeof(buffer), "AT%s\r",
1007ff7d6aeaSMatthias Ringwald                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
1008ff7d6aeaSMatthias Ringwald         buffer[sizeof(buffer) - 1] = 0;
1009a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1010667ec068SMatthias Ringwald         return;
1011667ec068SMatthias Ringwald     }
1012667ec068SMatthias Ringwald 
1013667ec068SMatthias Ringwald     // update HF indicators
1014a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
1015667ec068SMatthias Ringwald         int i;
1016aeb0f0feSMatthias Ringwald         for (i=0; i < hfp_hf_indicators_nr; i++){
1017a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
1018a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
1019a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
1020a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
1021667ec068SMatthias Ringwald                     char buffer[30];
10221599fe57SMatthias Ringwald                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
1023ff7d6aeaSMatthias Ringwald                              HFP_TRANSFER_HF_INDICATOR_STATUS,
1024aeb0f0feSMatthias Ringwald                              hfp_hf_indicators[i],
1025aeb0f0feSMatthias Ringwald                              (unsigned int)hfp_hf_indicators_value[i]);
1026ff7d6aeaSMatthias Ringwald                     buffer[sizeof(buffer) - 1] = 0;
1027a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1028667ec068SMatthias Ringwald                 } else {
1029aeb0f0feSMatthias Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]);
1030667ec068SMatthias Ringwald                 }
1031667ec068SMatthias Ringwald                 return;
1032667ec068SMatthias Ringwald             }
1033667ec068SMatthias Ringwald         }
1034667ec068SMatthias Ringwald     }
1035667ec068SMatthias Ringwald 
1036ce263fc8SMatthias Ringwald     if (done) return;
1037ce263fc8SMatthias Ringwald     // deal with disconnect
1038a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
1039ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
1040a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1041a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
1042ce263fc8SMatthias Ringwald             break;
1043ce263fc8SMatthias Ringwald 
1044ce263fc8SMatthias Ringwald         default:
1045ce263fc8SMatthias Ringwald             break;
1046ce263fc8SMatthias Ringwald     }
1047ce263fc8SMatthias Ringwald }
1048ce263fc8SMatthias Ringwald 
1049a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
1050a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
10516a7f44bdSMilanka Ringwald 
1052ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
10537522e673SMatthias Ringwald 
1054184a03edSMilanka Ringwald     uint8_t i;
1055184a03edSMilanka Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
10561ac1f60fSMilanka Ringwald         hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1057184a03edSMilanka Ringwald     }
1058722a85f3SMilanka Ringwald 
1059722a85f3SMilanka Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1060722a85f3SMilanka Ringwald         hfp_connection->ag_indicators[i].status_changed = 0;
1061722a85f3SMilanka Ringwald         hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1062722a85f3SMilanka Ringwald     }
1063722a85f3SMilanka Ringwald 
1064667ec068SMatthias Ringwald     // restore volume settings
1065a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
1066a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1067ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
1068a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
1069a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1070ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
1071667ec068SMatthias Ringwald     // enable all indicators
1072aeb0f0feSMatthias Ringwald     for (i=0; i < hfp_hf_indicators_nr; i++){
1073aeb0f0feSMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i];
1074a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
1075667ec068SMatthias Ringwald     }
1076ce263fc8SMatthias Ringwald }
1077ce263fc8SMatthias Ringwald 
10781cc65c4fSMatthias Ringwald static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
1079aeb0f0feSMatthias Ringwald 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){
10801cc65c4fSMatthias Ringwald 		// Codec supported, confirm
10811cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
10821cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
10831cc65c4fSMatthias Ringwald 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
10841cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
10851cc65c4fSMatthias Ringwald 
10861cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_codec_confirm = true;
10871cc65c4fSMatthias Ringwald 	} else {
10881cc65c4fSMatthias Ringwald 		// Codec not supported, send supported codecs
10891cc65c4fSMatthias Ringwald 		hfp_connection->codec_confirmed = 0;
10901cc65c4fSMatthias Ringwald 		hfp_connection->suggested_codec = 0;
10911cc65c4fSMatthias Ringwald 		hfp_connection->negotiated_codec = 0;
10921cc65c4fSMatthias Ringwald 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
10931cc65c4fSMatthias Ringwald 
10941cc65c4fSMatthias Ringwald 		hfp_connection->hf_send_supported_codecs = true;
10951cc65c4fSMatthias Ringwald 	}
10961cc65c4fSMatthias Ringwald }
10971cc65c4fSMatthias Ringwald 
1098e2c3b58dSMilanka Ringwald static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){
1099e2c3b58dSMilanka Ringwald     bool event_emited = true;
1100e2c3b58dSMilanka Ringwald 
110199af1e28SMilanka Ringwald     switch (hfp_connection->response_pending_for_command){
1102e2c3b58dSMilanka Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1103e2c3b58dSMilanka Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status);
1104e2c3b58dSMilanka Ringwald             break;
1105e2c3b58dSMilanka Ringwald         default:
1106e2c3b58dSMilanka Ringwald             event_emited = false;
1107e2c3b58dSMilanka Ringwald 
1108a0ffb263SMatthias Ringwald             switch (hfp_connection->state){
11093deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
1110a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
1111a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_NOTIFY_ON_CODECS;
11123deb3ec6SMatthias Ringwald                         break;
11133deb3ec6SMatthias Ringwald                     }
1114a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
11153deb3ec6SMatthias Ringwald                     break;
11163deb3ec6SMatthias Ringwald 
11173deb3ec6SMatthias Ringwald                 case HFP_W4_NOTIFY_ON_CODECS:
1118a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
11193deb3ec6SMatthias Ringwald                     break;
11203deb3ec6SMatthias Ringwald 
11213deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS:
1122a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
11233deb3ec6SMatthias Ringwald                     break;
11243deb3ec6SMatthias Ringwald 
11253deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1126a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
11273deb3ec6SMatthias Ringwald                     break;
11283deb3ec6SMatthias Ringwald 
11293deb3ec6SMatthias Ringwald                 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1130a0ffb263SMatthias Ringwald                     if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1131a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
11323deb3ec6SMatthias Ringwald                         break;
11333deb3ec6SMatthias Ringwald                     }
1134a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1135a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
11363deb3ec6SMatthias Ringwald                         break;
11373deb3ec6SMatthias Ringwald                     }
1138a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11393deb3ec6SMatthias Ringwald                     break;
11403deb3ec6SMatthias Ringwald 
11413deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1142a0ffb263SMatthias Ringwald                     if (has_hf_indicators_feature(hfp_connection)){
1143a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
11443deb3ec6SMatthias Ringwald                         break;
11453deb3ec6SMatthias Ringwald                     }
1146a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11473deb3ec6SMatthias Ringwald                     break;
11483deb3ec6SMatthias Ringwald 
11493deb3ec6SMatthias Ringwald                 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1150a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
11513deb3ec6SMatthias Ringwald                     break;
11523deb3ec6SMatthias Ringwald 
11533deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1154a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
11553deb3ec6SMatthias Ringwald                     break;
11563deb3ec6SMatthias Ringwald 
11573deb3ec6SMatthias Ringwald                 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1158a0ffb263SMatthias Ringwald                     hfp_ag_slc_established(hfp_connection);
11593deb3ec6SMatthias Ringwald                     break;
1160ce263fc8SMatthias Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1161a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1162a0ffb263SMatthias Ringwald                         hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1163ca59be51SMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1164ce263fc8SMatthias Ringwald                         break;
1165ce263fc8SMatthias Ringwald                     }
11663deb3ec6SMatthias Ringwald 
1167a0ffb263SMatthias Ringwald                     if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1168a0ffb263SMatthias Ringwald                         hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1169ca59be51SMatthias Ringwald                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1170ce263fc8SMatthias Ringwald                         break;
11713deb3ec6SMatthias Ringwald                     }
11723deb3ec6SMatthias Ringwald 
1173a0ffb263SMatthias Ringwald                     switch (hfp_connection->hf_query_operator_state){
1174ce263fc8SMatthias Ringwald                         case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1175a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1176ce263fc8SMatthias Ringwald                             break;
1177ce263fc8SMatthias Ringwald                         case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1178a0ffb263SMatthias Ringwald                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1179a473a009SMatthias Ringwald                             hfp_emit_network_operator_event(hfp_connection);
1180ce263fc8SMatthias Ringwald                             break;
1181ce263fc8SMatthias Ringwald                         default:
1182ce263fc8SMatthias Ringwald                             break;
11833deb3ec6SMatthias Ringwald                     }
1184ce263fc8SMatthias Ringwald 
1185a0ffb263SMatthias Ringwald                     if (hfp_connection->enable_extended_audio_gateway_error_report){
1186a0ffb263SMatthias Ringwald                         hfp_connection->enable_extended_audio_gateway_error_report = 0;
1187ce263fc8SMatthias Ringwald                         break;
11883deb3ec6SMatthias Ringwald                     }
11893deb3ec6SMatthias Ringwald 
1190a0ffb263SMatthias Ringwald                     switch (hfp_connection->codecs_state){
1191aa4dd815SMatthias Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1192a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
11933deb3ec6SMatthias Ringwald                             break;
1194ce263fc8SMatthias Ringwald                         case HFP_CODECS_HF_CONFIRMED_CODEC:
1195a0ffb263SMatthias Ringwald                             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1196ce263fc8SMatthias Ringwald                             break;
11973deb3ec6SMatthias Ringwald                         default:
11983deb3ec6SMatthias Ringwald                             break;
11993deb3ec6SMatthias Ringwald                     }
1200af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
1201be55a11dSMilanka Ringwald                     break;
1202be55a11dSMilanka Ringwald                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
1203af97579eSMilanka Ringwald                     hfp_hf_voice_recognition_state_machine(hfp_connection);
12043deb3ec6SMatthias Ringwald                     break;
12053deb3ec6SMatthias Ringwald                 default:
12063deb3ec6SMatthias Ringwald                     break;
12073deb3ec6SMatthias Ringwald             }
1208e2c3b58dSMilanka Ringwald             break;
1209e2c3b58dSMilanka Ringwald     }
12103deb3ec6SMatthias Ringwald 
12113deb3ec6SMatthias Ringwald     // done
121299af1e28SMilanka Ringwald     hfp_connection->response_pending_for_command = HFP_CMD_NONE;
1213be55a11dSMilanka Ringwald     hfp_connection->ok_pending = 0;
1214a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
1215e2c3b58dSMilanka Ringwald     return event_emited;
12163deb3ec6SMatthias Ringwald }
12173deb3ec6SMatthias Ringwald 
1218a03dbc20SMilanka Ringwald static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){
1219a03dbc20SMilanka Ringwald     switch (callsetup_status){
1220a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1221a03dbc20SMilanka Ringwald         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1222a03dbc20SMilanka Ringwald             return true;
1223a03dbc20SMilanka Ringwald         default:
1224a03dbc20SMilanka Ringwald             return false;
1225a03dbc20SMilanka Ringwald    }
1226a03dbc20SMilanka Ringwald }
1227be55a11dSMilanka Ringwald 
1228b08371a9SMilanka Ringwald static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
12294562e2a2SMatthias Ringwald     uint16_t i;
1230a03dbc20SMilanka Ringwald 
12314562e2a2SMatthias Ringwald     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
12324562e2a2SMatthias Ringwald         if (hfp_connection->ag_indicators[i].status_changed) {
12334562e2a2SMatthias Ringwald             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1234a03dbc20SMilanka Ringwald                 hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1235a03dbc20SMilanka Ringwald                 bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status);
1236a03dbc20SMilanka Ringwald                 bool ringing_new = hfp_is_ringing(new_hf_callsetup_status);
1237a03dbc20SMilanka Ringwald                 if (ringing_old != ringing_new){
1238a03dbc20SMilanka Ringwald                     if (ringing_new){
1239a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING);
1240a03dbc20SMilanka Ringwald                     } else {
1241a03dbc20SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING);
1242a03dbc20SMilanka Ringwald                     }
1243a03dbc20SMilanka Ringwald                 }
1244a03dbc20SMilanka Ringwald                 hfp_hf_callsetup_status = new_hf_callsetup_status;
12454562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1246aeb0f0feSMatthias Ringwald                 hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
12474562e2a2SMatthias Ringwald                 // avoid set but not used warning
1248aeb0f0feSMatthias Ringwald                 (void) hfp_hf_callheld_status;
12494562e2a2SMatthias Ringwald             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1250674ebed5SMilanka Ringwald                 hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1251674ebed5SMilanka Ringwald                 if (hfp_hf_call_status != new_hf_call_status){
1252674ebed5SMilanka Ringwald                     if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){
1253674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1254674ebed5SMilanka Ringwald                     } else {
1255674ebed5SMilanka Ringwald                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1256674ebed5SMilanka Ringwald                     }
1257674ebed5SMilanka Ringwald                 }
1258674ebed5SMilanka Ringwald                 hfp_hf_call_status = new_hf_call_status;
12594562e2a2SMatthias Ringwald             }
12604562e2a2SMatthias Ringwald             hfp_connection->ag_indicators[i].status_changed = 0;
1261ce3797e1SMilanka Ringwald             hfp_emit_ag_indicator_status_event(hfp_connection, &hfp_connection->ag_indicators[i]);
12624562e2a2SMatthias Ringwald             break;
12634562e2a2SMatthias Ringwald         }
12644562e2a2SMatthias Ringwald     }
12654562e2a2SMatthias Ringwald }
12664562e2a2SMatthias Ringwald 
1267426f9988SMatthias Ringwald static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1268186dd3d2SMatthias Ringwald     int value;
1269186dd3d2SMatthias Ringwald     int i;
1270e2c3b58dSMilanka Ringwald     bool event_emited;
1271e2c3b58dSMilanka Ringwald 
1272a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
1273667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1274a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1275a473a009SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1276667ec068SMatthias Ringwald             break;
1277667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1278a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1279ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1280667ec068SMatthias Ringwald             break;
1281667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1282a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1283a473a009SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_connection);
1284667ec068SMatthias Ringwald             break;
1285ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1286a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12872308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1288667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1289ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1290ce263fc8SMatthias Ringwald             break;
1291ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1292a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
12932308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1294667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1295ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1296ce263fc8SMatthias Ringwald             break;
1297ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1298a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1299ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1300a0ffb263SMatthias Ringwald             break;
1301a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1302a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1303a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1304a0ffb263SMatthias Ringwald             break;
1305a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1306a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1307a473a009SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1308ce263fc8SMatthias Ringwald             break;
1309ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1310a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13115a4785c8SMatthias Ringwald             hfp_connection->ok_pending = 0;
1312a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1313ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1314ce263fc8SMatthias Ringwald             break;
13150b4debbfSMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
13160b4debbfSMilanka Ringwald             break;
1317fdda66c0SMilanka Ringwald         case HFP_CMD_ERROR:
131890244c92SMilanka Ringwald             switch (hfp_connection->state){
131990244c92SMilanka Ringwald                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
132090244c92SMilanka Ringwald                     switch (hfp_connection->codecs_state){
132190244c92SMilanka Ringwald                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1322fdda66c0SMilanka Ringwald                             hfp_reset_context_flags(hfp_connection);
132390244c92SMilanka Ringwald                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
132490244c92SMilanka Ringwald                             return;
132590244c92SMilanka Ringwald                         default:
132690244c92SMilanka Ringwald                             break;
132790244c92SMilanka Ringwald                     }
132856f1adacSMilanka Ringwald                     break;
132956f1adacSMilanka Ringwald                 default:
133056f1adacSMilanka Ringwald                     break;
133156f1adacSMilanka Ringwald             }
1332e2c3b58dSMilanka Ringwald 
1333fdda66c0SMilanka Ringwald             // handle error response for voice activation (HF initiated)
13340b4debbfSMilanka Ringwald             switch(hfp_connection->vra_state_requested){
1335de9e0ea7SMilanka Ringwald                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1336de9e0ea7SMilanka Ringwald                     hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1337be55a11dSMilanka Ringwald                     break;
1338be55a11dSMilanka Ringwald                 default:
1339e2c3b58dSMilanka Ringwald                     if (hfp_connection->vra_state_requested == hfp_connection->vra_state){
1340e2c3b58dSMilanka Ringwald                         break;
1341e2c3b58dSMilanka Ringwald                     }
13420b4debbfSMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
1343553a4a56SMilanka Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
13440b4debbfSMilanka Ringwald                     hfp_reset_context_flags(hfp_connection);
13450b4debbfSMilanka Ringwald                     return;
1346be55a11dSMilanka Ringwald             }
1347e2c3b58dSMilanka Ringwald             event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1348e2c3b58dSMilanka Ringwald             if (!event_emited){
13490b4debbfSMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1350e2c3b58dSMilanka Ringwald             }
1351fdda66c0SMilanka Ringwald             hfp_reset_context_flags(hfp_connection);
1352ce263fc8SMatthias Ringwald             break;
1353fdda66c0SMilanka Ringwald 
1354ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1355e2c3b58dSMilanka Ringwald             hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS);
1356ce263fc8SMatthias Ringwald             break;
1357ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
13585a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1359ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1360ce263fc8SMatthias Ringwald             break;
1361ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
13625a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13634562e2a2SMatthias Ringwald             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1364ce263fc8SMatthias Ringwald             break;
1365c741b032SMilanka Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
13665a4785c8SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1367184a03edSMilanka Ringwald             // report status after SLC established
1368184a03edSMilanka Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
1369184a03edSMilanka Ringwald                 break;
1370184a03edSMilanka Ringwald             }
1371c741b032SMilanka Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
13721ac1f60fSMilanka Ringwald                 hfp_emit_ag_indicator_mapping_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1373c741b032SMilanka Ringwald             }
1374c741b032SMilanka Ringwald             break;
13751cc65c4fSMatthias Ringwald     	case HFP_CMD_AG_SUGGESTED_CODEC:
13761cc65c4fSMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
13775a4785c8SMatthias Ringwald     		hfp_hf_handle_suggested_codec(hfp_connection);
13781cc65c4fSMatthias Ringwald 			break;
1379eac56539SMilanka Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1380eac56539SMilanka 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));
1381ce263fc8SMatthias Ringwald         default:
1382ce263fc8SMatthias Ringwald             break;
13833deb3ec6SMatthias Ringwald     }
13840cef86faSMatthias Ringwald }
1385426f9988SMatthias Ringwald 
138676cc1527SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
138776cc1527SMatthias Ringwald 	return (byte == '\n') || (byte == '\r');
138876cc1527SMatthias Ringwald }
138976cc1527SMatthias Ringwald 
13900b4debbfSMilanka Ringwald static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1391426f9988SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1392426f9988SMatthias Ringwald     if (size < 1) return;
1393426f9988SMatthias Ringwald 
1394426f9988SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1395e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1396e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1397e43d1938SMatthias Ringwald #endif
1398426f9988SMatthias Ringwald 
1399426f9988SMatthias Ringwald     // process messages byte-wise
1400a7ba78b0SMilanka Ringwald     uint8_t pos;
1401426f9988SMatthias Ringwald     for (pos = 0; pos < size; pos++){
1402426f9988SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
14031599fe57SMatthias Ringwald         // parse until end of line "\r" or "\n"
1404426f9988SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
14050b4debbfSMilanka Ringwald         hfp_hf_handle_rfcomm_command(hfp_connection);
14063deb3ec6SMatthias Ringwald     }
1407a7ba78b0SMilanka Ringwald }
14083deb3ec6SMatthias Ringwald 
14091c6a0fc0SMatthias Ringwald static void hfp_hf_run(void){
1410665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1411665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1412665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1413a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
141422387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
14151c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
14163deb3ec6SMatthias Ringwald     }
14173deb3ec6SMatthias Ringwald }
14183deb3ec6SMatthias Ringwald 
14191c6a0fc0SMatthias Ringwald static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
14200b4debbfSMilanka Ringwald     hfp_connection_t * hfp_connection;
14213deb3ec6SMatthias Ringwald     switch (packet_type){
14223deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
14230b4debbfSMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
14240b4debbfSMilanka Ringwald             if (!hfp_connection) return;
14250b4debbfSMilanka Ringwald             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
14260b4debbfSMilanka Ringwald             hfp_hf_run_for_context(hfp_connection);
14270b4debbfSMilanka Ringwald             return;
14283deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1429d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1430d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
14311c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1432d4dd47ffSMatthias Ringwald                 return;
1433d4dd47ffSMatthias Ringwald             }
143427950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1435202c8a4cSMatthias Ringwald             break;
14363deb3ec6SMatthias Ringwald         default:
14373deb3ec6SMatthias Ringwald             break;
14383deb3ec6SMatthias Ringwald     }
14391c6a0fc0SMatthias Ringwald     hfp_hf_run();
14403deb3ec6SMatthias Ringwald }
14413deb3ec6SMatthias Ringwald 
14421c6a0fc0SMatthias Ringwald static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1443405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
14441c6a0fc0SMatthias Ringwald     hfp_hf_run();
1445405014fbSMatthias Ringwald }
1446405014fbSMatthias Ringwald 
1447aeb0f0feSMatthias Ringwald static void hfp_hf_set_defaults(void){
1448aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1449aeb0f0feSMatthias Ringwald     hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
1450aeb0f0feSMatthias Ringwald     hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1451aeb0f0feSMatthias Ringwald     hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1452aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = 0;
1453aeb0f0feSMatthias Ringwald     hfp_hf_speaker_gain = 9;
1454aeb0f0feSMatthias Ringwald     hfp_hf_microphone_gain = 9;
1455aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = 0;
1456aeb0f0feSMatthias Ringwald }
1457aeb0f0feSMatthias Ringwald 
1458b4df8028SMilanka Ringwald uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1459b4df8028SMilanka Ringwald     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1460b4df8028SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
1461b4df8028SMilanka Ringwald         return status;
1462b4df8028SMilanka Ringwald     }
1463b4df8028SMilanka Ringwald 
1464520c92d5SMatthias Ringwald     hfp_init();
1465aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1466d63c37a1SMatthias Ringwald 
14671c6a0fc0SMatthias Ringwald     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
14681c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
146927950165SMatthias Ringwald 
147027950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
14711c6a0fc0SMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1472b4df8028SMilanka Ringwald     return ERROR_CODE_SUCCESS;
147320b2edb6SMatthias Ringwald }
147427950165SMatthias Ringwald 
147520b2edb6SMatthias Ringwald void hfp_hf_deinit(void){
147620b2edb6SMatthias Ringwald     hfp_deinit();
1477aeb0f0feSMatthias Ringwald     hfp_hf_set_defaults();
1478aeb0f0feSMatthias Ringwald 
1479aeb0f0feSMatthias Ringwald     hfp_hf_callback = NULL;
148020b2edb6SMatthias Ringwald     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
1481aeb0f0feSMatthias Ringwald     (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number));
1482a0ffb263SMatthias Ringwald }
1483a0ffb263SMatthias Ringwald 
14847ca89cabSMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
148568466199SMilanka Ringwald     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
14863deb3ec6SMatthias Ringwald 
1487aeb0f0feSMatthias Ringwald     hfp_hf_codecs_nr = codecs_nr;
14883deb3ec6SMatthias Ringwald     int i;
14893deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
1490aeb0f0feSMatthias Ringwald         hfp_hf_codecs[i] = codecs[i];
14913deb3ec6SMatthias Ringwald     }
14923deb3ec6SMatthias Ringwald }
14933deb3ec6SMatthias Ringwald 
1494a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
1495aeb0f0feSMatthias Ringwald     hfp_hf_supported_features = supported_features;
1496a0ffb263SMatthias Ringwald }
14973deb3ec6SMatthias Ringwald 
14987ca89cabSMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
1499aeb0f0feSMatthias Ringwald     btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS);
150068466199SMilanka Ringwald 
1501aeb0f0feSMatthias Ringwald     hfp_hf_indicators_nr = indicators_nr;
15023deb3ec6SMatthias Ringwald     int i;
1503aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
1504aeb0f0feSMatthias Ringwald         hfp_hf_indicators[i] = indicators[i];
15053deb3ec6SMatthias Ringwald     }
15063deb3ec6SMatthias Ringwald }
15073deb3ec6SMatthias Ringwald 
15084eb3f1d8SMilanka Ringwald uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
15094eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
15103deb3ec6SMatthias Ringwald }
15113deb3ec6SMatthias Ringwald 
1512657bc59fSMilanka Ringwald uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
15139c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1514a33eb0c4SMilanka Ringwald     if (!hfp_connection){
1515657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1516a33eb0c4SMilanka Ringwald     }
15171ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
15181c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
1519657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
15203deb3ec6SMatthias Ringwald }
15213deb3ec6SMatthias Ringwald 
15223c65e705SMilanka Ringwald static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
15239c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1524a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15253c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15263deb3ec6SMatthias Ringwald     }
1527a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
15281c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15293c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15303deb3ec6SMatthias Ringwald }
15313deb3ec6SMatthias Ringwald 
15323c65e705SMilanka Ringwald uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
15333c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1534ce263fc8SMatthias Ringwald }
1535ce263fc8SMatthias Ringwald 
15363c65e705SMilanka Ringwald uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
15373c65e705SMilanka Ringwald     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1538ce263fc8SMatthias Ringwald }
1539ce263fc8SMatthias Ringwald 
15403deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
15413c65e705SMilanka Ringwald uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
15429c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1543a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15443c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15453deb3ec6SMatthias Ringwald     }
1546a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1547a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
15481c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15493c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15503deb3ec6SMatthias Ringwald }
15513deb3ec6SMatthias Ringwald 
15523c65e705SMilanka Ringwald uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
15539c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1554a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15553c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15563deb3ec6SMatthias Ringwald     }
15573c65e705SMilanka Ringwald 
1558a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1559ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1560a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1561ce263fc8SMatthias Ringwald             break;
1562ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1563a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1564ce263fc8SMatthias Ringwald             break;
1565ce263fc8SMatthias Ringwald         default:
15663c65e705SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1567ce263fc8SMatthias Ringwald     }
15681c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15693c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15703deb3ec6SMatthias Ringwald }
15713deb3ec6SMatthias Ringwald 
15723c65e705SMilanka Ringwald static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
15739c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1574a0ffb263SMatthias Ringwald     if (!hfp_connection) {
15753c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
15763deb3ec6SMatthias Ringwald     }
1577a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
15781c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
15793c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
15803deb3ec6SMatthias Ringwald }
15813deb3ec6SMatthias Ringwald 
1582ce263fc8SMatthias Ringwald 
15833c65e705SMilanka Ringwald uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
15843c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1585ce263fc8SMatthias Ringwald }
1586ce263fc8SMatthias Ringwald 
15873c65e705SMilanka Ringwald uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
15883c65e705SMilanka Ringwald     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1589ce263fc8SMatthias Ringwald }
1590ce263fc8SMatthias Ringwald 
159138200c1dSMilanka Ringwald static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
1592aeb0f0feSMatthias Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4));
159338200c1dSMilanka Ringwald }
1594ce263fc8SMatthias Ringwald 
15953c65e705SMilanka Ringwald uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
15969c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1597a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
15983c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1599a33eb0c4SMilanka Ringwald     }
1600ce263fc8SMatthias Ringwald 
16013c65e705SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
16023c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16033c65e705SMilanka Ringwald     }
16043c65e705SMilanka Ringwald 
16053c65e705SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
16063c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16073c65e705SMilanka Ringwald     }
1608f4412093SMatthias Ringwald     if (has_codec_negotiation_feature(hfp_connection)) {
1609a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state) {
1610aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1611aa4dd815SMatthias Ringwald                 break;
1612ec3bfc1aSMatthias Ringwald             case HFP_CODECS_EXCHANGED:
1613ec3bfc1aSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1614ec3bfc1aSMatthias Ringwald                 break;
1615aa4dd815SMatthias Ringwald             default:
16161cc65c4fSMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
16171cc65c4fSMatthias Ringwald                 hfp_connection->suggested_codec = 0;
16181cc65c4fSMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
16191cc65c4fSMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
162038200c1dSMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 1;
1621aa4dd815SMatthias Ringwald                 break;
16223deb3ec6SMatthias Ringwald         }
1623f4412093SMatthias Ringwald     } else {
1624f4412093SMatthias Ringwald         log_info("no codec negotiation feature, use CVSD");
1625f4412093SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1626f4412093SMatthias Ringwald         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1627f4412093SMatthias Ringwald         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1628f4412093SMatthias Ringwald         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1629f4412093SMatthias Ringwald         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1630f4412093SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1631f4412093SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1632ce263fc8SMatthias Ringwald     }
1633ce263fc8SMatthias Ringwald 
16341c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16353c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16363deb3ec6SMatthias Ringwald }
16373deb3ec6SMatthias Ringwald 
16383c65e705SMilanka Ringwald uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
16399c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1640a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16413c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1642a33eb0c4SMilanka Ringwald     }
16430b4debbfSMilanka Ringwald     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
16440b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
16450b4debbfSMilanka Ringwald     }
16460b4debbfSMilanka Ringwald     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
16470b4debbfSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
16481c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
16490b4debbfSMilanka Ringwald     }
16503c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
16513deb3ec6SMatthias Ringwald }
16523deb3ec6SMatthias Ringwald 
16533c65e705SMilanka Ringwald uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
16549c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1655a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16563c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1657a33eb0c4SMilanka Ringwald     }
1658ce263fc8SMatthias Ringwald 
1659aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1660a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
16611c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1662ce263fc8SMatthias Ringwald     } else {
1663aeb0f0feSMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status);
16643c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1665ce263fc8SMatthias Ringwald     }
16663c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1667ce263fc8SMatthias Ringwald }
1668ce263fc8SMatthias Ringwald 
16693c65e705SMilanka Ringwald uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
16709c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1671a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16723c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1673a33eb0c4SMilanka Ringwald     }
1674a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
16751c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
16763c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1677ce263fc8SMatthias Ringwald }
1678ce263fc8SMatthias Ringwald 
16793c65e705SMilanka Ringwald uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
16809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1681a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16823c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1683a33eb0c4SMilanka Ringwald     }
1684ce263fc8SMatthias Ringwald 
1685aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1686a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
16871c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1688ce263fc8SMatthias Ringwald     }
16893c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1690ce263fc8SMatthias Ringwald }
1691ce263fc8SMatthias Ringwald 
16923c65e705SMilanka Ringwald uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){
16939c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1694a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
16953c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1696a33eb0c4SMilanka Ringwald     }
1697ce263fc8SMatthias Ringwald 
1698aeb0f0feSMatthias Ringwald     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1699a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
17001c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1701ce263fc8SMatthias Ringwald     }
17023c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1703ce263fc8SMatthias Ringwald }
1704ce263fc8SMatthias Ringwald 
17053c65e705SMilanka Ringwald uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
17069c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1707a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17083c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1709a33eb0c4SMilanka Ringwald     }
1710ce263fc8SMatthias Ringwald 
1711aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1712aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1713a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
17141c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1715ce263fc8SMatthias Ringwald     }
17163c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1717ce263fc8SMatthias Ringwald }
1718ce263fc8SMatthias Ringwald 
17193c65e705SMilanka Ringwald uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
17209c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1721a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17223c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1723a33eb0c4SMilanka Ringwald     }
1724ce263fc8SMatthias Ringwald 
1725aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1726aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1727a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
17281c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1729ce263fc8SMatthias Ringwald     }
17303c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1731ce263fc8SMatthias Ringwald }
1732ce263fc8SMatthias Ringwald 
17333c65e705SMilanka Ringwald uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
17349c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1735a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17363c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1737a33eb0c4SMilanka Ringwald     }
1738ce263fc8SMatthias Ringwald 
1739aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1740aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1741a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
17421c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1743ce263fc8SMatthias Ringwald     }
17443c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1745ce263fc8SMatthias Ringwald }
1746ce263fc8SMatthias Ringwald 
17473c65e705SMilanka Ringwald uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
17489c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1749a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17503c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1751a33eb0c4SMilanka Ringwald     }
1752ce263fc8SMatthias Ringwald 
1753aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1754aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1755a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
17561c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1757ce263fc8SMatthias Ringwald     }
17583c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1759ce263fc8SMatthias Ringwald }
1760ce263fc8SMatthias Ringwald 
17613c65e705SMilanka Ringwald uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
17629c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1763a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17643c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1765a33eb0c4SMilanka Ringwald     }
1766667ec068SMatthias Ringwald 
1767aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1768aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1769a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1770a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
17711c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1772667ec068SMatthias Ringwald     }
17733c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1774667ec068SMatthias Ringwald }
1775667ec068SMatthias Ringwald 
17763c65e705SMilanka Ringwald uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
17779c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1778a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17793c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1780a33eb0c4SMilanka Ringwald     }
1781667ec068SMatthias Ringwald 
1782aeb0f0feSMatthias Ringwald     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1783aeb0f0feSMatthias Ringwald         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1784a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1785a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
17861c6a0fc0SMatthias Ringwald         hfp_hf_run_for_context(hfp_connection);
1787667ec068SMatthias Ringwald     }
17883c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1789667ec068SMatthias Ringwald }
1790ce263fc8SMatthias Ringwald 
17913c65e705SMilanka Ringwald uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
17929c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1793a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
17943c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1795a33eb0c4SMilanka Ringwald     }
1796ce263fc8SMatthias Ringwald 
1797a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1798aeb0f0feSMatthias Ringwald     snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number);
17991c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18003c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1801ce263fc8SMatthias Ringwald }
1802ce263fc8SMatthias Ringwald 
18033c65e705SMilanka Ringwald uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
18049c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1805a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18063c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1807a33eb0c4SMilanka Ringwald     }
1808ce263fc8SMatthias Ringwald 
1809a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1810a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1811a0ffb263SMatthias Ringwald 
18121c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18133c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1814ce263fc8SMatthias Ringwald }
1815ce263fc8SMatthias Ringwald 
18163c65e705SMilanka Ringwald uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
18179c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1818a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18193c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1820a33eb0c4SMilanka Ringwald     }
1821ce263fc8SMatthias Ringwald 
1822a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
18231c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18243c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1825ce263fc8SMatthias Ringwald }
1826ce263fc8SMatthias Ringwald 
18273c65e705SMilanka Ringwald uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
18289c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1829a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18303c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1831a33eb0c4SMilanka Ringwald     }
1832ce263fc8SMatthias Ringwald 
1833a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
18341c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18353c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1836ce263fc8SMatthias Ringwald }
1837ce263fc8SMatthias Ringwald 
1838ce263fc8SMatthias Ringwald 
18393c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
18409c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1841a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18423c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1843a33eb0c4SMilanka Ringwald     }
1844ce263fc8SMatthias Ringwald 
1845a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
18461c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18473c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1848ce263fc8SMatthias Ringwald }
1849ce263fc8SMatthias Ringwald 
1850ce263fc8SMatthias Ringwald 
18513c65e705SMilanka Ringwald uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
18529c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1853a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18543c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1855a33eb0c4SMilanka Ringwald     }
1856ce263fc8SMatthias Ringwald 
1857a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
18581c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18593c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1860ce263fc8SMatthias Ringwald }
1861ce263fc8SMatthias Ringwald 
18623c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
18639c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1864a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
18653c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1866a33eb0c4SMilanka Ringwald     }
1867ce263fc8SMatthias Ringwald 
1868a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
18691c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18703c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1871ce263fc8SMatthias Ringwald }
1872ce263fc8SMatthias Ringwald 
18736ba83b5eSMilanka Ringwald static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){
18746ba83b5eSMilanka Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION);
1875aeb0f0feSMatthias Ringwald     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION);
18766ba83b5eSMilanka Ringwald     return hf && ag;
1877ce263fc8SMatthias Ringwald }
1878ce263fc8SMatthias Ringwald 
18793c65e705SMilanka Ringwald uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(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) {
18823c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1883a33eb0c4SMilanka Ringwald     }
18846ba83b5eSMilanka Ringwald     if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){
18856ba83b5eSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
18866ba83b5eSMilanka Ringwald     }
1887ce263fc8SMatthias Ringwald 
1888a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
18891c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
18903c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1891ce263fc8SMatthias Ringwald }
1892ce263fc8SMatthias Ringwald 
1893acd11d4aSMilanka Ringwald uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1894fdda66c0SMilanka Ringwald      hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1895fdda66c0SMilanka Ringwald     if (!hfp_connection) {
1896fdda66c0SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1897be55a11dSMilanka Ringwald     }
1898013cc750SMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1899013cc750SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1900013cc750SMilanka Ringwald     }
1901acd11d4aSMilanka Ringwald 
1902acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1903acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1904acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
1905acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1906af97579eSMilanka Ringwald     }
1907af97579eSMilanka Ringwald 
1908498a8121SMilanka Ringwald     switch (hfp_connection->vra_state){
1909be55a11dSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1910de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1911fd4151d1SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
1912acd11d4aSMilanka Ringwald             hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported;
1913be55a11dSMilanka Ringwald             break;
1914de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1915de9e0ea7SMilanka Ringwald             hfp_connection->activate_voice_recognition = true;
1916de9e0ea7SMilanka Ringwald             break;
1917be55a11dSMilanka Ringwald         default:
1918be55a11dSMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1919be55a11dSMilanka Ringwald     }
1920ce263fc8SMatthias Ringwald 
1921af97579eSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1922fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
1923af97579eSMilanka Ringwald }
1924af97579eSMilanka Ringwald 
1925acd11d4aSMilanka Ringwald uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
1926af97579eSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1927af97579eSMilanka Ringwald     if (!hfp_connection) {
1928af97579eSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1929af97579eSMilanka Ringwald     }
193084fb9ac1SMilanka Ringwald 
193184fb9ac1SMilanka Ringwald     if (hfp_connection->emit_vra_enabled_after_audio_established){
193284fb9ac1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
193384fb9ac1SMilanka Ringwald     }
193484fb9ac1SMilanka Ringwald 
1935acd11d4aSMilanka Ringwald     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
193608a0b01cSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
193708a0b01cSMilanka Ringwald     }
1938acd11d4aSMilanka Ringwald 
1939acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1940acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported){
1941acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1942acd11d4aSMilanka Ringwald     }
1943acd11d4aSMilanka Ringwald 
1944acd11d4aSMilanka Ringwald     switch (hfp_connection->vra_state){
1945acd11d4aSMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1946acd11d4aSMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1947acd11d4aSMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
1948acd11d4aSMilanka Ringwald             break;
1949acd11d4aSMilanka Ringwald         default:
1950fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1951af97579eSMilanka Ringwald     }
1952013cc750SMilanka Ringwald 
1953acd11d4aSMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
1954acd11d4aSMilanka Ringwald     return ERROR_CODE_SUCCESS;
1955acd11d4aSMilanka Ringwald }
1956acd11d4aSMilanka Ringwald 
1957acd11d4aSMilanka Ringwald 
1958acd11d4aSMilanka Ringwald uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1959acd11d4aSMilanka Ringwald     // return deactivate_voice_recognition(acl_handle, false);
1960acd11d4aSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1961acd11d4aSMilanka Ringwald     if (!hfp_connection) {
1962acd11d4aSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1963acd11d4aSMilanka Ringwald     }
1964acd11d4aSMilanka Ringwald 
196584fb9ac1SMilanka Ringwald     if (hfp_connection->emit_vra_enabled_after_audio_established){
196684fb9ac1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
196784fb9ac1SMilanka Ringwald     }
196884fb9ac1SMilanka Ringwald 
1969acd11d4aSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
1970acd11d4aSMilanka Ringwald         hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1971acd11d4aSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1972acd11d4aSMilanka Ringwald     }
1973acd11d4aSMilanka Ringwald 
1974acd11d4aSMilanka Ringwald     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1975acd11d4aSMilanka Ringwald     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1976acd11d4aSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
1977acd11d4aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1978acd11d4aSMilanka Ringwald     }
1979acd11d4aSMilanka Ringwald 
1980fdda66c0SMilanka Ringwald     switch (hfp_connection->vra_state){
1981de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
1982fdda66c0SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1983de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1984de9e0ea7SMilanka Ringwald         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1985fdda66c0SMilanka Ringwald             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1986fdda66c0SMilanka Ringwald             break;
1987de9e0ea7SMilanka Ringwald 
1988de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1989de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1990de9e0ea7SMilanka Ringwald             hfp_connection->deactivate_voice_recognition = true;
1991de9e0ea7SMilanka Ringwald             break;
1992de9e0ea7SMilanka Ringwald 
1993de9e0ea7SMilanka Ringwald         case HFP_VRA_VOICE_RECOGNITION_OFF:
1994de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1995de9e0ea7SMilanka Ringwald         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1996fdda66c0SMilanka Ringwald         default:
1997fdda66c0SMilanka Ringwald             return ERROR_CODE_COMMAND_DISALLOWED;
1998fdda66c0SMilanka Ringwald     }
1999fdda66c0SMilanka Ringwald 
2000fdda66c0SMilanka Ringwald     hfp_hf_run_for_context(hfp_connection);
2001fdda66c0SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2002af97579eSMilanka Ringwald }
2003af97579eSMilanka Ringwald 
20043c65e705SMilanka Ringwald uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
20059c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2006a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20073c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2008a33eb0c4SMilanka Ringwald     }
2009c8626498SMilanka Ringwald 
20103c65e705SMilanka Ringwald     if (hfp_connection->microphone_gain == gain) {
20113c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
20123c65e705SMilanka Ringwald     }
20133c65e705SMilanka Ringwald 
2014c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
2015a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
20163c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2017a0ffb263SMatthias Ringwald     }
20183c65e705SMilanka Ringwald 
2019a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
2020a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
20211c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20223c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2023ce263fc8SMatthias Ringwald }
2024ce263fc8SMatthias Ringwald 
20253c65e705SMilanka Ringwald uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
20269c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2027a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20283c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2029a33eb0c4SMilanka Ringwald     }
2030c8626498SMilanka Ringwald 
20313c65e705SMilanka Ringwald     if (hfp_connection->speaker_gain == gain){
20323c65e705SMilanka Ringwald         return ERROR_CODE_SUCCESS;
20333c65e705SMilanka Ringwald     }
20343c65e705SMilanka Ringwald 
2035c1ab6cc1SMatthias Ringwald     if ((gain < 0) || (gain > 15)){
2036a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
20373c65e705SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2038a0ffb263SMatthias Ringwald     }
20393c65e705SMilanka Ringwald 
2040a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
2041a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
20421c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20433c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2044ce263fc8SMatthias Ringwald }
2045ce263fc8SMatthias Ringwald 
20463c65e705SMilanka Ringwald uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
20479c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2048a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20493c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2050a33eb0c4SMilanka Ringwald     }
2051a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
20521c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20533c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2054ce263fc8SMatthias Ringwald }
2055ce263fc8SMatthias Ringwald 
20563c65e705SMilanka Ringwald uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
20579c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2058a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20593c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2060a33eb0c4SMilanka Ringwald     }
2061a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
20621c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20633c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2064ce263fc8SMatthias Ringwald }
20653deb3ec6SMatthias Ringwald 
20663c65e705SMilanka Ringwald uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
20679c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2068a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20693c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2070a33eb0c4SMilanka Ringwald     }
2071a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
20721c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20733c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2074667ec068SMatthias Ringwald }
2075667ec068SMatthias Ringwald 
2076667ec068SMatthias Ringwald 
20773c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
20789c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2079a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20803c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2081a33eb0c4SMilanka Ringwald     }
2082a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2083a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
20841c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20853c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2086667ec068SMatthias Ringwald }
2087667ec068SMatthias Ringwald 
20883c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
20899c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2090a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
20913c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2092a33eb0c4SMilanka Ringwald     }
2093a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2094a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
20951c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
20963c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2097667ec068SMatthias Ringwald }
2098667ec068SMatthias Ringwald 
20993c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
21009c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2101a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21023c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2103a33eb0c4SMilanka Ringwald     }
2104a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2105a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
21061c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21073c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2108667ec068SMatthias Ringwald }
2109667ec068SMatthias Ringwald 
21103c65e705SMilanka Ringwald uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
21119c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2112a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21133c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2114a33eb0c4SMilanka Ringwald     }
2115a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
2116a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
21171c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21183c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2119667ec068SMatthias Ringwald }
2120667ec068SMatthias Ringwald 
21213c65e705SMilanka Ringwald uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
21229c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2123a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21243c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2125a33eb0c4SMilanka Ringwald     }
2126a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
21271c6a0fc0SMatthias Ringwald     hfp_hf_run_for_context(hfp_connection);
21283c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2129667ec068SMatthias Ringwald }
2130667ec068SMatthias Ringwald 
21313c65e705SMilanka Ringwald uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
21329c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2133a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
21343c65e705SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2135a33eb0c4SMilanka Ringwald     }
2136667ec068SMatthias Ringwald     // find index for assigned number
2137667ec068SMatthias Ringwald     int i;
2138aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_hf_indicators_nr ; i++){
2139aeb0f0feSMatthias Ringwald         if (hfp_hf_indicators[i] == assigned_number){
2140667ec068SMatthias Ringwald             // set value
2141aeb0f0feSMatthias Ringwald             hfp_hf_indicators_value[i] = value;
2142667ec068SMatthias Ringwald             // mark for update
2143a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
2144a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
2145667ec068SMatthias Ringwald                 // send update
21461c6a0fc0SMatthias Ringwald                 hfp_hf_run_for_context(hfp_connection);
2147a0ffb263SMatthias Ringwald             }
21483c65e705SMilanka Ringwald             return ERROR_CODE_SUCCESS;
2149667ec068SMatthias Ringwald         }
2150667ec068SMatthias Ringwald     }
21513c65e705SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2152667ec068SMatthias Ringwald }
2153667ec068SMatthias Ringwald 
2154d7f6b5cbSMatthias Ringwald int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
2155d7f6b5cbSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2156d7f6b5cbSMatthias Ringwald     if (!hfp_connection) {
2157d7f6b5cbSMatthias Ringwald         return 0;
2158d7f6b5cbSMatthias Ringwald     }
2159d7f6b5cbSMatthias Ringwald     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
2160d7f6b5cbSMatthias Ringwald }
216176cc1527SMatthias Ringwald 
216276cc1527SMatthias 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){
216376cc1527SMatthias Ringwald 	if (!name){
2164aeb0f0feSMatthias Ringwald 		name = hfp_hf_default_service_name;
216576cc1527SMatthias Ringwald 	}
216676cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
216776cc1527SMatthias Ringwald 
216876cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
216976cc1527SMatthias Ringwald 	//
217076cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
217176cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
217276cc1527SMatthias Ringwald 	//
217376cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
217476cc1527SMatthias Ringwald 	//
217576cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
2176ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
217776cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
217876cc1527SMatthias Ringwald 	}
2179ef3ae4ebSMilanka Ringwald 
2180ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
218156f1adacSMilanka Ringwald         sdp_features |= 1 << 6;
2182ef3ae4ebSMilanka Ringwald     }
2183ef3ae4ebSMilanka Ringwald 
2184ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
218556f1adacSMilanka Ringwald         sdp_features |= 1 << 7;
2186ef3ae4ebSMilanka Ringwald     }
2187ef3ae4ebSMilanka Ringwald 
218876cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
218976cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
219076cc1527SMatthias Ringwald }
219176cc1527SMatthias Ringwald 
219276cc1527SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
219368466199SMilanka Ringwald 	btstack_assert(callback != NULL);
219468466199SMilanka Ringwald 
219576cc1527SMatthias Ringwald 	hfp_hf_callback = callback;
219676cc1527SMatthias Ringwald 	hfp_set_hf_callback(callback);
219776cc1527SMatthias Ringwald }
2198