xref: /btstack/src/classic/hfp_hf.c (revision 15b7a1173e37f598e7c568d159468e77de545cae)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "hfp_hf.c"
39 
40 // *****************************************************************************
41 //
42 // HFP Hands-Free (HF) unit
43 //
44 // *****************************************************************************
45 
46 #include "btstack_config.h"
47 
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <string.h>
51 
52 #include "bluetooth_sdp.h"
53 #include "btstack_debug.h"
54 #include "btstack_event.h"
55 #include "btstack_memory.h"
56 #include "btstack_run_loop.h"
57 #include "classic/core.h"
58 #include "classic/hfp.h"
59 #include "classic/hfp_hf.h"
60 #include "classic/sdp_client_rfcomm.h"
61 #include "classic/sdp_server.h"
62 #include "classic/sdp_util.h"
63 #include "hci.h"
64 #include "hci_cmd.h"
65 #include "hci_dump.h"
66 #include "l2cap.h"
67 
68 // const
69 static const char hfp_hf_default_service_name[] = "Hands-Free unit";
70 
71 // globals
72 
73 // higher layer callbacks
74 static btstack_packet_handler_t hfp_hf_callback;
75 
76 static btstack_packet_callback_registration_t hfp_hf_hci_event_callback_registration;
77 
78 static uint16_t hfp_hf_supported_features;
79 static uint8_t  hfp_hf_codecs_nr;
80 static uint8_t  hfp_hf_codecs[HFP_MAX_NUM_CODECS];
81 
82 static uint8_t  hfp_hf_indicators_nr;
83 static uint8_t  hfp_hf_indicators[HFP_MAX_NUM_INDICATORS];
84 static uint32_t hfp_hf_indicators_value[HFP_MAX_NUM_INDICATORS];
85 
86 static uint8_t  hfp_hf_speaker_gain;
87 static uint8_t  hfp_hf_microphone_gain;
88 
89 static hfp_call_status_t      hfp_hf_call_status;
90 static hfp_callsetup_status_t hfp_hf_callsetup_status;
91 static hfp_callheld_status_t  hfp_hf_callheld_status;
92 
93 static char hfp_hf_phone_number[25];
94 
95 
96 static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
97 	int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
98 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
99 	return hf && ag;
100 }
101 
102 static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
103 	int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_THREE_WAY_CALLING);
104 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
105 	return hf && ag;
106 }
107 
108 
109 static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
110 	int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_HF_INDICATORS);
111 	int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
112 	return hf && ag;
113 }
114 
115 static bool hfp_hf_vra_flag_supported(hfp_connection_t * hfp_connection){
116     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION);
117     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION);
118     return hf && ag;
119 }
120 
121 static bool hfp_hf_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){
122     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS);
123     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS);
124     return hf && ag;
125 }
126 
127 static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
128     btstack_linked_list_iterator_t it;
129     btstack_linked_list_iterator_init(&it, hfp_get_connections());
130     while (btstack_linked_list_iterator_has_next(&it)){
131         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
132         if (hfp_connection->acl_handle != handle)      continue;
133         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
134         return hfp_connection;
135     }
136     return NULL;
137 }
138 
139 /* emit functinos */
140 
141 static void hfp_hf_emit_subscriber_information(const hfp_connection_t * hfp_connection, uint8_t status){
142     if (hfp_hf_callback == NULL) return;
143     uint8_t event[33];
144     event[0] = HCI_EVENT_HFP_META;
145     event[1] = sizeof(event) - 2;
146     event[2] = HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION;
147     little_endian_store_16(event, 3, hfp_connection->acl_handle);
148     event[5] = status;
149     event[6] = hfp_connection->bnip_type;
150     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 8);
151     strncpy((char*)&event[7], hfp_connection->bnip_number, size);
152     event[7 + size] = 0;
153     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
154 }
155 
156 static void hfp_hf_emit_type_and_number(const hfp_connection_t * hfp_connection, uint8_t event_subtype){
157     if (hfp_hf_callback == NULL) return;
158     uint8_t event[32];
159     event[0] = HCI_EVENT_HFP_META;
160     event[1] = sizeof(event) - 2;
161     event[2] = event_subtype;
162     little_endian_store_16(event, 3, hfp_connection->acl_handle);
163     event[5] = hfp_connection->bnip_type;
164     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - 7);
165     strncpy((char*)&event[6], hfp_connection->bnip_number, size);
166     event[6 + size] = 0;
167     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
168 }
169 
170 static void hfp_hf_emit_enhanced_call_status(const hfp_connection_t * hfp_connection){
171     if (hfp_hf_callback == NULL) return;
172     uint8_t event[38];
173     int pos = 0;
174     event[pos++] = HCI_EVENT_HFP_META;
175     event[pos++] = sizeof(event) - 2;
176     event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
177     little_endian_store_16(event, pos, hfp_connection->acl_handle);
178     pos += 2;
179     event[pos++] = hfp_connection->clcc_idx;
180     event[pos++] = hfp_connection->clcc_dir;
181     event[pos++] = hfp_connection->clcc_status;
182     event[pos++] = hfp_connection->clcc_mode;
183     event[pos++] = hfp_connection->clcc_mpty;
184     event[pos++] = hfp_connection->bnip_type;
185     uint16_t size = btstack_min(strlen(hfp_connection->bnip_number), sizeof(event) - pos - 1);
186     strncpy((char*)&event[pos], hfp_connection->bnip_number, size);
187     pos += size;
188     event[pos++] = 0;
189     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
190 }
191 
192 
193 static void hfp_emit_ag_indicator_event(const hfp_connection_t * hfp_connection, const hfp_ag_indicator_t * indicator){
194 	if (hfp_hf_callback == NULL) return;
195 	uint8_t event[12+HFP_MAX_INDICATOR_DESC_SIZE+1];
196 	int pos = 0;
197 	event[pos++] = HCI_EVENT_HFP_META;
198 	event[pos++] = sizeof(event) - 2;
199 	event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
200     little_endian_store_16(event, pos, hfp_connection->acl_handle);
201     pos += 2;
202 	event[pos++] = indicator->index;
203 	event[pos++] = indicator->status;
204 	event[pos++] = indicator->min_range;
205 	event[pos++] = indicator->max_range;
206 	event[pos++] = indicator->mandatory;
207 	event[pos++] = indicator->enabled;
208 	event[pos++] = indicator->status_changed;
209 	strncpy((char*)&event[pos], indicator->name, HFP_MAX_INDICATOR_DESC_SIZE);
210 	pos += HFP_MAX_INDICATOR_DESC_SIZE;
211 	event[pos] = 0;
212 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
213 }
214 
215 static void hfp_emit_network_operator_event(const hfp_connection_t * hfp_connection){
216     if (hfp_hf_callback == NULL) return;
217 	uint8_t event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
218 	event[0] = HCI_EVENT_HFP_META;
219 	event[1] = sizeof(event) - 2;
220 	event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
221     little_endian_store_16(event, 3, hfp_connection->acl_handle);
222 	event[5] = hfp_connection->network_operator.mode;
223 	event[6] = hfp_connection->network_operator.format;
224 	strncpy((char*)&event[7], hfp_connection->network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
225 	event[7+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
226 	(*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
227 }
228 
229 
230 static void hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection_t * hfp_connection){
231     btstack_assert(hfp_connection != NULL);
232     uint8_t event[HFP_MAX_VR_TEXT_SIZE + 11];
233     int pos = 0;
234     event[pos++] = HCI_EVENT_HFP_META;
235     event[pos++] = sizeof(event) - 2;
236     event[pos++] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE;
237     little_endian_store_16(event, pos, hfp_connection->acl_handle);
238     pos += 2;
239     little_endian_store_16(event, pos, hfp_connection->ag_msg.text_id);
240     pos += 2;
241     event[pos++] = hfp_connection->ag_msg.text_type;
242     event[pos++] = hfp_connection->ag_msg.text_operation;
243 
244     // length, zero ending is already in message
245     uint8_t * value = &hfp_connection->line_buffer[0];
246     uint16_t  value_length = hfp_connection->ag_vra_msg_length;
247 
248     little_endian_store_16(event, pos, value_length);
249     pos += 2;
250     memcpy(&event[pos], value, value_length);
251     pos += value_length;
252 
253     (*hfp_hf_callback)(HCI_EVENT_PACKET, 0, event, pos);
254 }
255 
256 /* send commands */
257 
258 static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
259     char buffer[20];
260     snprintf(buffer, sizeof(buffer), "AT%s\r", cmd);
261     return send_str_over_rfcomm(cid, buffer);
262 }
263 
264 static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
265     char buffer[20];
266     snprintf(buffer, sizeof(buffer), "AT%s%s\r", cmd, mark);
267     return send_str_over_rfcomm(cid, buffer);
268 }
269 
270 static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
271     char buffer[40];
272     snprintf(buffer, sizeof(buffer), "AT%s=%d\r", cmd, value);
273     return send_str_over_rfcomm(cid, buffer);
274 }
275 
276 static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
277     char buffer[30];
278     const int size = sizeof(buffer);
279     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
280     offset += join(buffer+offset, size-offset, hfp_hf_codecs, hfp_hf_codecs_nr);
281     offset += snprintf(buffer+offset, size-offset, "\r");
282     return send_str_over_rfcomm(cid, buffer);
283 }
284 
285 static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
286     char buffer[50];
287     const int size = sizeof(buffer);
288     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
289     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
290     offset += snprintf(buffer+offset, size-offset, "\r");
291     return send_str_over_rfcomm(cid, buffer);
292 }
293 
294 static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
295     char buffer[30];
296     const int size = sizeof(buffer);
297     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
298     offset += join(buffer+offset, size-offset, hfp_hf_indicators, hfp_hf_indicators_nr);
299     offset += snprintf(buffer+offset, size-offset, "\r");
300     return send_str_over_rfcomm(cid, buffer);
301 }
302 
303 static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
304     char buffer[20];
305     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
306     return send_str_over_rfcomm(cid, buffer);
307 }
308 
309 static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
310     char buffer[40];
311     snprintf(buffer, sizeof(buffer), "%s%s;\r", HFP_CALL_PHONE_NUMBER, hfp_hf_phone_number);
312     return send_str_over_rfcomm(cid, buffer);
313 }
314 
315 static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
316     char buffer[40];
317     snprintf(buffer, sizeof(buffer), "%s>%d;\r", HFP_CALL_PHONE_NUMBER, memory_id);
318     return send_str_over_rfcomm(cid, buffer);
319 }
320 
321 static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
322     char buffer[40];
323     snprintf(buffer, sizeof(buffer), "AT%s=%u\r", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
324     return send_str_over_rfcomm(cid, buffer);
325 }
326 
327 static int hfp_hf_send_dtmf(uint16_t cid, char code){
328     char buffer[20];
329     snprintf(buffer, sizeof(buffer), "AT%s=%c\r", HFP_TRANSMIT_DTMF_CODES, code);
330     return send_str_over_rfcomm(cid, buffer);
331 }
332 
333 static int hfp_hf_cmd_ata(uint16_t cid){
334     return send_str_over_rfcomm(cid, (char *) "ATA\r");
335 }
336 
337 static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
338     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_hf_supported_features);
339 }
340 
341 static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
342     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
343 }
344 
345 static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
346     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
347 }
348 
349 static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
350     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
351 }
352 
353 static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
354     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
355 }
356 
357 static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
358     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
359 }
360 
361 static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
362     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
363 }
364 
365 static int hfp_hf_cmd_query_operator_name(uint16_t cid){
366     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
367 }
368 
369 static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
370     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
371 }
372 
373 static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
374     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
375 }
376 
377 static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
378     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
379 }
380 
381 static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
382     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
383 }
384 
385 static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
386     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
387 }
388 
389 static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
390     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
391 }
392 
393 static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
394     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
395 }
396 
397 static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
398     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
399 }
400 
401 static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
402     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
403 }
404 
405 static int hfp_hf_send_chup(uint16_t cid){
406     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
407 }
408 
409 static int hfp_hf_send_binp(uint16_t cid){
410     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
411 }
412 
413 static int hfp_hf_send_clcc(uint16_t cid){
414     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
415 }
416 
417 /* state machines */
418 
419 static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
420     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
421     if (hfp_connection->ok_pending) return 0;
422     int done = 1;
423     log_info("hfp_hf_run_for_context_service_level_connection state %d\n", hfp_connection->state);
424     switch (hfp_connection->state){
425         case HFP_EXCHANGE_SUPPORTED_FEATURES:
426             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_hf_codecs, &hfp_hf_codecs_nr);
427             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
428             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
429             break;
430         case HFP_NOTIFY_ON_CODECS:
431             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
432             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
433             break;
434         case HFP_RETRIEVE_INDICATORS:
435             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
436             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
437             break;
438         case HFP_RETRIEVE_INDICATORS_STATUS:
439             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
440             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
441             break;
442         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
443             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
444             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
445             break;
446         case HFP_RETRIEVE_CAN_HOLD_CALL:
447             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
448             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
449             break;
450         case HFP_LIST_GENERIC_STATUS_INDICATORS:
451             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
452             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
453             break;
454         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
455             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
456             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
457             break;
458         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
459             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
460             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
461             break;
462         default:
463             done = 0;
464             break;
465     }
466     return done;
467 }
468 
469 
470 static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
471     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
472     if (hfp_connection->ok_pending){
473         return 0;
474     }
475     int done = 0;
476     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
477         hfp_connection->ok_pending = 1;
478         done = 1;
479         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
480         return done;
481     };
482     if (hfp_connection->change_status_update_for_individual_ag_indicators){
483         hfp_connection->ok_pending = 1;
484         done = 1;
485         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
486                 hfp_connection->ag_indicators_status_update_bitmap,
487                 hfp_connection->ag_indicators_nr);
488         return done;
489     }
490 
491     switch (hfp_connection->hf_query_operator_state){
492         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
493             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
494             hfp_connection->ok_pending = 1;
495             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
496             return 1;
497         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
498             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
499             hfp_connection->ok_pending = 1;
500             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
501             return 1;
502         default:
503             break;
504     }
505 
506     if (hfp_connection->enable_extended_audio_gateway_error_report){
507         hfp_connection->ok_pending = 1;
508         done = 1;
509         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
510         return done;
511     }
512 
513     return done;
514 }
515 
516 static int hfp_hf_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
517     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
518         return 0;
519     }
520     int done = 0;
521 
522     if (hfp_connection->ok_pending == 1){
523         return 0;
524     }
525     // voice recognition activated from AG
526     if (hfp_connection->command == HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION){
527         switch(hfp_connection->vra_state_requested){
528             case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
529             case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
530             case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
531                 // ignore AG command, continue to wait for OK
532                 return 0;
533 
534             default:
535                 if (hfp_connection->ag_vra_msg_length > 0){
536                     hfp_hf_emit_enhanced_voice_recognition_text(hfp_connection);
537                     hfp_connection->ag_vra_msg_length = 0;
538                     break;
539                 }
540                 switch(hfp_connection->ag_vra_state){
541                     case HFP_VOICE_RECOGNITION_STATE_AG_READY:
542                         switch (hfp_connection->ag_vra_status){
543                             case 0:
544                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
545                                 break;
546                             case 1:
547                                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
548                                 break;
549                             case 2:
550                                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
551                                 break;
552                             default:
553                                 break;
554                         }
555                         break;
556                     default:
557                         // state messages from AG
558                         hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
559                         hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
560                         break;
561                 }
562                 break;
563         }
564         hfp_connection->command = HFP_CMD_NONE;
565     }
566 
567 
568     switch (hfp_connection->vra_state_requested){
569         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
570             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
571             if (done != 0){
572                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
573                 hfp_connection->ok_pending = 1;
574             }
575             return 1;
576 
577 
578         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
579             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
580             if (done != 0){
581                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
582                 hfp_connection->ok_pending = 1;
583                 return 1;
584             }
585             break;
586 
587         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
588             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
589             if (done != 0){
590                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
591                 hfp_connection->ok_pending = 1;
592                 return 1;
593             }
594             break;
595 
596         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
597             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
598             hfp_connection->vra_state_requested = hfp_connection->vra_state;
599             hfp_connection->activate_voice_recognition = false;
600             if (hfp_connection->activate_voice_recognition){
601                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
602                 hfp_hf_activate_voice_recognition(hfp_connection->acl_handle);
603             } else {
604                 hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
605             }
606             break;
607 
608         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
609             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
610             hfp_connection->vra_state_requested = hfp_connection->vra_state;
611             hfp_connection->activate_voice_recognition = false;
612             if (hfp_connection->deactivate_voice_recognition){
613                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
614             } else {
615                 hfp_connection->enhanced_voice_recognition_enabled = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
616                 hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
617             }
618             break;
619 
620 
621         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
622             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
623             hfp_connection->vra_state_requested = hfp_connection->vra_state;
624             hfp_connection->activate_voice_recognition = false;
625             if (hfp_connection->deactivate_voice_recognition){
626                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
627             } else {
628                 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
629             }
630             break;
631 
632         default:
633             break;
634     }
635     return done;
636 }
637 
638 
639 static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
640     if (hfp_connection->ok_pending) return 0;
641 
642     if (hfp_connection->trigger_codec_exchange){
643 		hfp_connection->trigger_codec_exchange = 0;
644 
645 		hfp_connection->ok_pending = 1;
646 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
647 		return 1;
648     }
649 
650     if (hfp_connection->hf_send_codec_confirm){
651 		hfp_connection->hf_send_codec_confirm = false;
652 
653 		hfp_connection->ok_pending = 1;
654 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
655 		return 1;
656     }
657 
658     if (hfp_connection->hf_send_supported_codecs){
659 		hfp_connection->hf_send_supported_codecs = false;
660 
661 		hfp_connection->ok_pending = 1;
662 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
663 		return 1;
664     }
665 
666     return 0;
667 }
668 
669 static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
670     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
671         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
672 
673     if (hfp_connection->release_audio_connection){
674         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
675         hfp_connection->release_audio_connection = 0;
676         gap_disconnect(hfp_connection->sco_handle);
677         return 1;
678     }
679 
680     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
681 
682     // run codecs exchange
683     int done = codecs_exchange_state_machine(hfp_connection);
684     if (done) return 1;
685 
686     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
687     if (hfp_connection->establish_audio_connection){
688         hfp_connection->state = HFP_W4_SCO_CONNECTED;
689         hfp_connection->establish_audio_connection = 0;
690         hfp_setup_synchronous_connection(hfp_connection);
691         return 1;
692     }
693     return 0;
694 }
695 
696 
697 static int call_setup_state_machine(hfp_connection_t * hfp_connection){
698 
699 	if (hfp_connection->ok_pending) return 0;
700 
701     if (hfp_connection->hf_answer_incoming_call){
702         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
703         hfp_connection->hf_answer_incoming_call = 0;
704         return 1;
705     }
706     return 0;
707 }
708 
709 static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
710 
711 	btstack_assert(hfp_connection != NULL);
712 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
713 
714 	// during SDP query, RFCOMM CID is not set
715 	if (hfp_connection->rfcomm_cid == 0) return;
716 
717 	// assert command could be sent
718 	if (hci_can_send_command_packet_now() == 0) return;
719 
720 #ifdef ENABLE_CC256X_ASSISTED_HFP
721     // WBS Disassociate
722     if (hfp_connection->cc256x_send_wbs_disassociate){
723         hfp_connection->cc256x_send_wbs_disassociate = false;
724         hci_send_cmd(&hci_ti_wbs_disassociate);
725         return;
726     }
727     // Write Codec Config
728     if (hfp_connection->cc256x_send_write_codec_config){
729         hfp_connection->cc256x_send_write_codec_config = false;
730         hfp_cc256x_write_codec_config(hfp_connection);
731         return;
732     }
733     // WBS Associate
734     if (hfp_connection->cc256x_send_wbs_associate){
735         hfp_connection->cc256x_send_wbs_associate = false;
736         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
737         return;
738     }
739 #endif
740 #ifdef ENABLE_BCM_PCM_WBS
741     // Enable WBS
742     if (hfp_connection->bcm_send_enable_wbs){
743         hfp_connection->bcm_send_enable_wbs = false;
744         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
745         return;
746     }
747     // Write I2S/PCM params
748     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
749         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
750         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
751         return;
752     }
753     // Disable WBS
754     if (hfp_connection->bcm_send_disable_wbs){
755         hfp_connection->bcm_send_disable_wbs = false;
756         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
757         return;
758     }
759 #endif
760 #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
761     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
762         hfp_finalize_connection_context(hfp_connection);
763         return;
764     }
765 #endif
766 
767     if (hfp_connection->accept_sco){
768         bool incoming_eSCO = hfp_connection->accept_sco == 2;
769         hfp_connection->accept_sco = 0;
770         // notify about codec selection if not done already
771         if (hfp_connection->negotiated_codec == 0){
772             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
773         }
774         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
775         return;
776     }
777 
778     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
779         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
780         return;
781     }
782     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
783     if (!done){
784         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
785     }
786     if (!done){
787         done = hfp_hf_run_for_audio_connection(hfp_connection);
788     }
789     if (!done){
790         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
791     }
792     if (!done){
793         done = call_setup_state_machine(hfp_connection);
794     }
795 
796     // don't send a new command while ok still pending or SLC is not established
797     if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){
798         return;
799     }
800 
801     if (hfp_connection->send_microphone_gain){
802         hfp_connection->send_microphone_gain = 0;
803         hfp_connection->ok_pending = 1;
804         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
805         return;
806     }
807 
808     if (hfp_connection->send_speaker_gain){
809         hfp_connection->send_speaker_gain = 0;
810         hfp_connection->ok_pending = 1;
811         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
812         return;
813     }
814 
815     if (hfp_connection->hf_deactivate_calling_line_notification){
816         hfp_connection->hf_deactivate_calling_line_notification = 0;
817         hfp_connection->ok_pending = 1;
818         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
819         return;
820     }
821 
822     if (hfp_connection->hf_activate_calling_line_notification){
823         hfp_connection->hf_activate_calling_line_notification = 0;
824         hfp_connection->ok_pending = 1;
825         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
826         return;
827     }
828 
829     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
830         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
831         hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR;
832         hfp_connection->ok_pending = 1;
833         hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0);
834         return;
835     }
836 
837     if (hfp_connection->hf_deactivate_call_waiting_notification){
838         hfp_connection->hf_deactivate_call_waiting_notification = 0;
839         hfp_connection->ok_pending = 1;
840         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
841         return;
842     }
843 
844     if (hfp_connection->hf_activate_call_waiting_notification){
845         hfp_connection->hf_activate_call_waiting_notification = 0;
846         hfp_connection->ok_pending = 1;
847         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
848         return;
849     }
850 
851     if (hfp_connection->hf_initiate_outgoing_call){
852         hfp_connection->hf_initiate_outgoing_call = 0;
853         hfp_connection->ok_pending = 1;
854         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
855         return;
856     }
857 
858     if (hfp_connection->hf_initiate_memory_dialing){
859         hfp_connection->hf_initiate_memory_dialing = 0;
860         hfp_connection->ok_pending = 1;
861         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
862         return;
863     }
864 
865     if (hfp_connection->hf_initiate_redial_last_number){
866         hfp_connection->hf_initiate_redial_last_number = 0;
867         hfp_connection->ok_pending = 1;
868         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
869         return;
870     }
871 
872     if (hfp_connection->hf_send_chup){
873         hfp_connection->hf_send_chup = 0;
874         hfp_connection->ok_pending = 1;
875         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
876         return;
877     }
878 
879     if (hfp_connection->hf_send_chld_0){
880         hfp_connection->hf_send_chld_0 = 0;
881         hfp_connection->ok_pending = 1;
882         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
883         return;
884     }
885 
886     if (hfp_connection->hf_send_chld_1){
887         hfp_connection->hf_send_chld_1 = 0;
888         hfp_connection->ok_pending = 1;
889         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
890         return;
891     }
892 
893     if (hfp_connection->hf_send_chld_2){
894         hfp_connection->hf_send_chld_2 = 0;
895         hfp_connection->ok_pending = 1;
896         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
897         return;
898     }
899 
900     if (hfp_connection->hf_send_chld_3){
901         hfp_connection->hf_send_chld_3 = 0;
902         hfp_connection->ok_pending = 1;
903         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
904         return;
905     }
906 
907     if (hfp_connection->hf_send_chld_4){
908         hfp_connection->hf_send_chld_4 = 0;
909         hfp_connection->ok_pending = 1;
910         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
911         return;
912     }
913 
914     if (hfp_connection->hf_send_chld_x){
915         hfp_connection->hf_send_chld_x = 0;
916         hfp_connection->ok_pending = 1;
917         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
918         return;
919     }
920 
921     if (hfp_connection->hf_send_dtmf_code){
922         char code = hfp_connection->hf_send_dtmf_code;
923         hfp_connection->hf_send_dtmf_code = 0;
924         hfp_connection->ok_pending = 1;
925         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
926         return;
927     }
928 
929     if (hfp_connection->hf_send_binp){
930         hfp_connection->hf_send_binp = 0;
931         hfp_connection->ok_pending = 1;
932         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
933         return;
934     }
935 
936     if (hfp_connection->hf_send_clcc){
937         hfp_connection->hf_send_clcc = 0;
938         hfp_connection->ok_pending = 1;
939         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
940         return;
941     }
942 
943     if (hfp_connection->hf_send_rrh){
944         hfp_connection->hf_send_rrh = 0;
945         char buffer[20];
946         switch (hfp_connection->hf_send_rrh_command){
947             case '?':
948                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
949                          HFP_RESPONSE_AND_HOLD);
950                 buffer[sizeof(buffer) - 1] = 0;
951                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
952                 return;
953             case '0':
954             case '1':
955             case '2':
956                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
957                          HFP_RESPONSE_AND_HOLD,
958                          hfp_connection->hf_send_rrh_command);
959                 buffer[sizeof(buffer) - 1] = 0;
960                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
961                 return;
962             default:
963                 break;
964         }
965         return;
966     }
967 
968     if (hfp_connection->hf_send_cnum){
969         hfp_connection->hf_send_cnum = 0;
970         char buffer[20];
971         snprintf(buffer, sizeof(buffer), "AT%s\r",
972                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
973         buffer[sizeof(buffer) - 1] = 0;
974         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
975         return;
976     }
977 
978     // update HF indicators
979     if (hfp_connection->generic_status_update_bitmap){
980         int i;
981         for (i=0; i < hfp_hf_indicators_nr; i++){
982             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
983                 if (hfp_connection->generic_status_indicators[i].state){
984                     hfp_connection->ok_pending = 1;
985                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
986                     char buffer[30];
987                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
988                              HFP_TRANSFER_HF_INDICATOR_STATUS,
989                              hfp_hf_indicators[i],
990                              (unsigned int)hfp_hf_indicators_value[i]);
991                     buffer[sizeof(buffer) - 1] = 0;
992                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
993                 } else {
994                     log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]);
995                 }
996                 return;
997             }
998         }
999     }
1000 
1001     if (done) return;
1002     // deal with disconnect
1003     switch (hfp_connection->state){
1004         case HFP_W2_DISCONNECT_RFCOMM:
1005             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1006             rfcomm_disconnect(hfp_connection->rfcomm_cid);
1007             break;
1008 
1009         default:
1010             break;
1011     }
1012 }
1013 
1014 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
1015     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
1016 
1017     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
1018 
1019     uint8_t i;
1020     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1021         hfp_connection->ag_indicators[i].status_changed = 0;
1022         hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1023     }
1024     // restore volume settings
1025     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
1026     hfp_connection->send_speaker_gain = 1;
1027     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
1028     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
1029     hfp_connection->send_microphone_gain = 1;
1030     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
1031     // enable all indicators
1032     for (i=0; i < hfp_hf_indicators_nr; i++){
1033         hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i];
1034         hfp_connection->generic_status_indicators[i].state = 1;
1035     }
1036 }
1037 
1038 static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
1039 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){
1040 		// Codec supported, confirm
1041 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1042 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1043 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
1044 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
1045 
1046 		hfp_connection->hf_send_codec_confirm = true;
1047 	} else {
1048 		// Codec not supported, send supported codecs
1049 		hfp_connection->codec_confirmed = 0;
1050 		hfp_connection->suggested_codec = 0;
1051 		hfp_connection->negotiated_codec = 0;
1052 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
1053 
1054 		hfp_connection->hf_send_supported_codecs = true;
1055 	}
1056 }
1057 
1058 static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){
1059     bool event_emited = true;
1060 
1061     switch (hfp_connection->response_pending_for_command){
1062         case HFP_CMD_TURN_OFF_EC_AND_NR:
1063             hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status);
1064             break;
1065         default:
1066             event_emited = false;
1067 
1068             switch (hfp_connection->state){
1069                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
1070                     if (has_codec_negotiation_feature(hfp_connection)){
1071                         hfp_connection->state = HFP_NOTIFY_ON_CODECS;
1072                         break;
1073                     }
1074                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
1075                     break;
1076 
1077                 case HFP_W4_NOTIFY_ON_CODECS:
1078                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
1079                     break;
1080 
1081                 case HFP_W4_RETRIEVE_INDICATORS:
1082                     hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
1083                     break;
1084 
1085                 case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1086                     hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
1087                     break;
1088 
1089                 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1090                     if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1091                         hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
1092                         break;
1093                     }
1094                     if (has_hf_indicators_feature(hfp_connection)){
1095                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
1096                         break;
1097                     }
1098                     hfp_ag_slc_established(hfp_connection);
1099                     break;
1100 
1101                 case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1102                     if (has_hf_indicators_feature(hfp_connection)){
1103                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
1104                         break;
1105                     }
1106                     hfp_ag_slc_established(hfp_connection);
1107                     break;
1108 
1109                 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1110                     hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
1111                     break;
1112 
1113                 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1114                     hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
1115                     break;
1116 
1117                 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1118                     hfp_ag_slc_established(hfp_connection);
1119                     break;
1120                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1121                     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1122                         hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1123                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1124                         break;
1125                     }
1126 
1127                     if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1128                         hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1129                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1130                         break;
1131                     }
1132 
1133                     switch (hfp_connection->hf_query_operator_state){
1134                         case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1135                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1136                             break;
1137                         case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1138                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1139                             hfp_emit_network_operator_event(hfp_connection);
1140                             break;
1141                         default:
1142                             break;
1143                     }
1144 
1145                     if (hfp_connection->enable_extended_audio_gateway_error_report){
1146                         hfp_connection->enable_extended_audio_gateway_error_report = 0;
1147                         break;
1148                     }
1149 
1150                     switch (hfp_connection->codecs_state){
1151                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1152                             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
1153                             break;
1154                         case HFP_CODECS_HF_CONFIRMED_CODEC:
1155                             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1156                             break;
1157                         default:
1158                             break;
1159                     }
1160                     hfp_hf_voice_recognition_state_machine(hfp_connection);
1161                     break;
1162                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
1163                     hfp_hf_voice_recognition_state_machine(hfp_connection);
1164                     break;
1165                 default:
1166                     break;
1167             }
1168             break;
1169     }
1170 
1171     // done
1172     hfp_connection->response_pending_for_command = HFP_CMD_NONE;
1173     hfp_connection->ok_pending = 0;
1174     hfp_connection->command = HFP_CMD_NONE;
1175     return event_emited;
1176 }
1177 
1178 static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){
1179     switch (callsetup_status){
1180         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1181         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1182             return true;
1183         default:
1184             return false;
1185    }
1186 }
1187 
1188 static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
1189     uint16_t i;
1190 
1191     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1192         if (hfp_connection->ag_indicators[i].status_changed) {
1193             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1194                 hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1195                 bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status);
1196                 bool ringing_new = hfp_is_ringing(new_hf_callsetup_status);
1197                 if (ringing_old != ringing_new){
1198                     if (ringing_new){
1199                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING);
1200                     } else {
1201                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING);
1202                     }
1203                 }
1204                 hfp_hf_callsetup_status = new_hf_callsetup_status;
1205             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1206                 hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1207                 // avoid set but not used warning
1208                 (void) hfp_hf_callheld_status;
1209             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1210                 hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1211                 if (hfp_hf_call_status != new_hf_call_status){
1212                     if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){
1213                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1214                     } else {
1215                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1216                     }
1217                 }
1218                 hfp_hf_call_status = new_hf_call_status;
1219             }
1220             hfp_connection->ag_indicators[i].status_changed = 0;
1221             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1222             break;
1223         }
1224     }
1225 }
1226 
1227 static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1228     int value;
1229     int i;
1230     bool event_emited;
1231 
1232     switch (hfp_connection->command){
1233         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1234             hfp_connection->command = HFP_CMD_NONE;
1235             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1236             break;
1237         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1238             hfp_connection->command = HFP_CMD_NONE;
1239             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1240             break;
1241         case HFP_CMD_LIST_CURRENT_CALLS:
1242             hfp_connection->command = HFP_CMD_NONE;
1243             hfp_hf_emit_enhanced_call_status(hfp_connection);
1244             break;
1245         case HFP_CMD_SET_SPEAKER_GAIN:
1246             hfp_connection->command = HFP_CMD_NONE;
1247             value = btstack_atoi((char*)hfp_connection->line_buffer);
1248             hfp_hf_speaker_gain = value;
1249             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1250             break;
1251         case HFP_CMD_SET_MICROPHONE_GAIN:
1252             hfp_connection->command = HFP_CMD_NONE;
1253             value = btstack_atoi((char*)hfp_connection->line_buffer);
1254             hfp_hf_microphone_gain = value;
1255             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1256             break;
1257         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1258             hfp_connection->command = HFP_CMD_NONE;
1259             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1260             break;
1261         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1262             hfp_connection->command = HFP_CMD_NONE;
1263             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1264             break;
1265         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1266             hfp_connection->command = HFP_CMD_NONE;
1267             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1268             break;
1269         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1270             hfp_connection->command = HFP_CMD_NONE;
1271             hfp_connection->ok_pending = 0;
1272             hfp_connection->extended_audio_gateway_error = 0;
1273             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1274             break;
1275         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
1276             break;
1277         case HFP_CMD_ERROR:
1278             switch (hfp_connection->state){
1279                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1280                     switch (hfp_connection->codecs_state){
1281                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1282                             hfp_reset_context_flags(hfp_connection);
1283                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
1284                             return;
1285                         default:
1286                             break;
1287                     }
1288                     break;
1289                 default:
1290                     break;
1291             }
1292 
1293             // handle error response for voice activation (HF initiated)
1294             switch(hfp_connection->vra_state_requested){
1295                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1296                     hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1297                     break;
1298                 default:
1299                     if (hfp_connection->vra_state_requested == hfp_connection->vra_state){
1300                         break;
1301                     }
1302                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
1303                     hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1304                     hfp_reset_context_flags(hfp_connection);
1305                     return;
1306             }
1307             event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1308             if (!event_emited){
1309                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1310             }
1311             hfp_reset_context_flags(hfp_connection);
1312             break;
1313 
1314         case HFP_CMD_OK:
1315             hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS);
1316             break;
1317         case HFP_CMD_RING:
1318             hfp_connection->command = HFP_CMD_NONE;
1319             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1320             break;
1321         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1322             hfp_connection->command = HFP_CMD_NONE;
1323             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1324             break;
1325         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1326             hfp_connection->command = HFP_CMD_NONE;
1327             // report status after SLC established
1328             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
1329                 break;
1330             }
1331             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1332                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1333             }
1334             break;
1335     	case HFP_CMD_AG_SUGGESTED_CODEC:
1336             hfp_connection->command = HFP_CMD_NONE;
1337     		hfp_hf_handle_suggested_codec(hfp_connection);
1338 			break;
1339         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1340             hfp_emit_event(hfp_connection, HFP_SUBEVENT_IN_BAND_RING_TONE, get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE));
1341         default:
1342             break;
1343     }
1344 }
1345 
1346 static int hfp_parser_is_end_of_line(uint8_t byte){
1347 	return (byte == '\n') || (byte == '\r');
1348 }
1349 
1350 static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1351     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1352     if (size < 1) return;
1353 
1354     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1355 #ifdef ENABLE_HFP_AT_MESSAGES
1356     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1357 #endif
1358 
1359     // process messages byte-wise
1360     uint8_t pos;
1361     for (pos = 0; pos < size; pos++){
1362         hfp_parse(hfp_connection, packet[pos], 1);
1363         // parse until end of line "\r" or "\n"
1364         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1365         hfp_hf_handle_rfcomm_command(hfp_connection);
1366     }
1367 }
1368 
1369 static void hfp_hf_run(void){
1370     btstack_linked_list_iterator_t it;
1371     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1372     while (btstack_linked_list_iterator_has_next(&it)){
1373         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1374         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1375         hfp_hf_run_for_context(hfp_connection);
1376     }
1377 }
1378 
1379 static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1380     hfp_connection_t * hfp_connection;
1381     switch (packet_type){
1382         case RFCOMM_DATA_PACKET:
1383             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1384             if (!hfp_connection) return;
1385             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
1386             hfp_hf_run_for_context(hfp_connection);
1387             return;
1388         case HCI_EVENT_PACKET:
1389             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1390                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
1391                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1392                 return;
1393             }
1394             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1395             break;
1396         default:
1397             break;
1398     }
1399     hfp_hf_run();
1400 }
1401 
1402 static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1403     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1404     hfp_hf_run();
1405 }
1406 
1407 static void hfp_hf_set_defaults(void){
1408     hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1409     hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
1410     hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1411     hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1412     hfp_hf_codecs_nr = 0;
1413     hfp_hf_speaker_gain = 9;
1414     hfp_hf_microphone_gain = 9;
1415     hfp_hf_indicators_nr = 0;
1416 }
1417 
1418 uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1419     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1420     if (status != ERROR_CODE_SUCCESS){
1421         return status;
1422     }
1423 
1424     hfp_init();
1425     hfp_hf_set_defaults();
1426 
1427     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
1428     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
1429 
1430     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
1431     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1432     return ERROR_CODE_SUCCESS;
1433 }
1434 
1435 void hfp_hf_deinit(void){
1436     hfp_deinit();
1437     hfp_hf_set_defaults();
1438 
1439     hfp_hf_callback = NULL;
1440     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
1441     (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number));
1442 }
1443 
1444 void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
1445     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
1446 
1447     hfp_hf_codecs_nr = codecs_nr;
1448     int i;
1449     for (i=0; i<codecs_nr; i++){
1450         hfp_hf_codecs[i] = codecs[i];
1451     }
1452 }
1453 
1454 void hfp_hf_init_supported_features(uint32_t supported_features){
1455     hfp_hf_supported_features = supported_features;
1456 }
1457 
1458 void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
1459     btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS);
1460 
1461     hfp_hf_indicators_nr = indicators_nr;
1462     int i;
1463     for (i = 0; i < hfp_hf_indicators_nr ; i++){
1464         hfp_hf_indicators[i] = indicators[i];
1465     }
1466 }
1467 
1468 uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1469     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
1470 }
1471 
1472 uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
1473     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1474     if (!hfp_connection){
1475         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1476     }
1477     hfp_trigger_release_service_level_connection(hfp_connection);
1478     hfp_hf_run_for_context(hfp_connection);
1479     return ERROR_CODE_SUCCESS;
1480 }
1481 
1482 static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
1483     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1484     if (!hfp_connection) {
1485         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1486     }
1487     hfp_connection->enable_status_update_for_ag_indicators = enable;
1488     hfp_hf_run_for_context(hfp_connection);
1489     return ERROR_CODE_SUCCESS;
1490 }
1491 
1492 uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1493     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1494 }
1495 
1496 uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1497     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1498 }
1499 
1500 // TODO: returned ERROR - wrong format
1501 uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
1502     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1503     if (!hfp_connection) {
1504         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1505     }
1506     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1507     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1508     hfp_hf_run_for_context(hfp_connection);
1509     return ERROR_CODE_SUCCESS;
1510 }
1511 
1512 uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
1513     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1514     if (!hfp_connection) {
1515         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1516     }
1517 
1518     switch (hfp_connection->hf_query_operator_state){
1519         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1520             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1521             break;
1522         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1523             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1524             break;
1525         default:
1526             return ERROR_CODE_COMMAND_DISALLOWED;
1527     }
1528     hfp_hf_run_for_context(hfp_connection);
1529     return ERROR_CODE_SUCCESS;
1530 }
1531 
1532 static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
1533     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1534     if (!hfp_connection) {
1535         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1536     }
1537     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1538     hfp_hf_run_for_context(hfp_connection);
1539     return ERROR_CODE_SUCCESS;
1540 }
1541 
1542 
1543 uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1544     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1545 }
1546 
1547 uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1548     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1549 }
1550 
1551 static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
1552     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4));
1553 }
1554 
1555 uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
1556     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1557     if (!hfp_connection) {
1558         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1559     }
1560 
1561     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
1562         return ERROR_CODE_COMMAND_DISALLOWED;
1563     }
1564 
1565     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
1566         return ERROR_CODE_COMMAND_DISALLOWED;
1567     }
1568     if (has_codec_negotiation_feature(hfp_connection)) {
1569         switch (hfp_connection->codecs_state) {
1570             case HFP_CODECS_W4_AG_COMMON_CODEC:
1571                 break;
1572             case HFP_CODECS_EXCHANGED:
1573                 hfp_connection->trigger_codec_exchange = 1;
1574                 break;
1575             default:
1576                 hfp_connection->codec_confirmed = 0;
1577                 hfp_connection->suggested_codec = 0;
1578                 hfp_connection->negotiated_codec = 0;
1579                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
1580                 hfp_connection->trigger_codec_exchange = 1;
1581                 break;
1582         }
1583     } else {
1584         log_info("no codec negotiation feature, use CVSD");
1585         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1586         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1587         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1588         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1589         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1590         hfp_connection->establish_audio_connection = 1;
1591         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1592     }
1593 
1594     hfp_hf_run_for_context(hfp_connection);
1595     return ERROR_CODE_SUCCESS;
1596 }
1597 
1598 uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
1599     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1600     if (!hfp_connection) {
1601         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1602     }
1603     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
1604         return ERROR_CODE_COMMAND_DISALLOWED;
1605     }
1606     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
1607     if (status == ERROR_CODE_SUCCESS){
1608         hfp_hf_run_for_context(hfp_connection);
1609     }
1610     return ERROR_CODE_SUCCESS;
1611 }
1612 
1613 uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
1614     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1615     if (!hfp_connection) {
1616         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1617     }
1618 
1619     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1620         hfp_connection->hf_answer_incoming_call = 1;
1621         hfp_hf_run_for_context(hfp_connection);
1622     } else {
1623         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status);
1624         return ERROR_CODE_COMMAND_DISALLOWED;
1625     }
1626     return ERROR_CODE_SUCCESS;
1627 }
1628 
1629 uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
1630     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1631     if (!hfp_connection) {
1632         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1633     }
1634     hfp_connection->hf_send_chup = 1;
1635     hfp_hf_run_for_context(hfp_connection);
1636     return ERROR_CODE_SUCCESS;
1637 }
1638 
1639 uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
1640     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1641     if (!hfp_connection) {
1642         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1643     }
1644 
1645     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1646         hfp_connection->hf_send_chup = 1;
1647         hfp_hf_run_for_context(hfp_connection);
1648     }
1649     return ERROR_CODE_SUCCESS;
1650 }
1651 
1652 uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){
1653     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1654     if (!hfp_connection) {
1655         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1656     }
1657 
1658     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1659         hfp_connection->hf_send_chld_0 = 1;
1660         hfp_hf_run_for_context(hfp_connection);
1661     }
1662     return ERROR_CODE_SUCCESS;
1663 }
1664 
1665 uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
1666     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1667     if (!hfp_connection) {
1668         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1669     }
1670 
1671     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1672         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1673         hfp_connection->hf_send_chld_1 = 1;
1674         hfp_hf_run_for_context(hfp_connection);
1675     }
1676     return ERROR_CODE_SUCCESS;
1677 }
1678 
1679 uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
1680     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1681     if (!hfp_connection) {
1682         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1683     }
1684 
1685     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1686         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1687         hfp_connection->hf_send_chld_2 = 1;
1688         hfp_hf_run_for_context(hfp_connection);
1689     }
1690     return ERROR_CODE_SUCCESS;
1691 }
1692 
1693 uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
1694     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1695     if (!hfp_connection) {
1696         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1697     }
1698 
1699     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1700         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1701         hfp_connection->hf_send_chld_3 = 1;
1702         hfp_hf_run_for_context(hfp_connection);
1703     }
1704     return ERROR_CODE_SUCCESS;
1705 }
1706 
1707 uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
1708     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1709     if (!hfp_connection) {
1710         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1711     }
1712 
1713     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1714         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1715         hfp_connection->hf_send_chld_4 = 1;
1716         hfp_hf_run_for_context(hfp_connection);
1717     }
1718     return ERROR_CODE_SUCCESS;
1719 }
1720 
1721 uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
1722     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1723     if (!hfp_connection) {
1724         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1725     }
1726 
1727     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1728         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1729         hfp_connection->hf_send_chld_x = 1;
1730         hfp_connection->hf_send_chld_x_index = 10 + index;
1731         hfp_hf_run_for_context(hfp_connection);
1732     }
1733     return ERROR_CODE_SUCCESS;
1734 }
1735 
1736 uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
1737     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1738     if (!hfp_connection) {
1739         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1740     }
1741 
1742     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1743         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1744         hfp_connection->hf_send_chld_x = 1;
1745         hfp_connection->hf_send_chld_x_index = 20 + index;
1746         hfp_hf_run_for_context(hfp_connection);
1747     }
1748     return ERROR_CODE_SUCCESS;
1749 }
1750 
1751 uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
1752     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1753     if (!hfp_connection) {
1754         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1755     }
1756 
1757     hfp_connection->hf_initiate_outgoing_call = 1;
1758     snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number);
1759     hfp_hf_run_for_context(hfp_connection);
1760     return ERROR_CODE_SUCCESS;
1761 }
1762 
1763 uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
1764     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1765     if (!hfp_connection) {
1766         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1767     }
1768 
1769     hfp_connection->hf_initiate_memory_dialing = 1;
1770     hfp_connection->memory_id = memory_id;
1771 
1772     hfp_hf_run_for_context(hfp_connection);
1773     return ERROR_CODE_SUCCESS;
1774 }
1775 
1776 uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
1777     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1778     if (!hfp_connection) {
1779         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1780     }
1781 
1782     hfp_connection->hf_initiate_redial_last_number = 1;
1783     hfp_hf_run_for_context(hfp_connection);
1784     return ERROR_CODE_SUCCESS;
1785 }
1786 
1787 uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
1788     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1789     if (!hfp_connection) {
1790         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1791     }
1792 
1793     hfp_connection->hf_activate_call_waiting_notification = 1;
1794     hfp_hf_run_for_context(hfp_connection);
1795     return ERROR_CODE_SUCCESS;
1796 }
1797 
1798 
1799 uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
1800     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1801     if (!hfp_connection) {
1802         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1803     }
1804 
1805     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1806     hfp_hf_run_for_context(hfp_connection);
1807     return ERROR_CODE_SUCCESS;
1808 }
1809 
1810 
1811 uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
1812     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1813     if (!hfp_connection) {
1814         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1815     }
1816 
1817     hfp_connection->hf_activate_calling_line_notification = 1;
1818     hfp_hf_run_for_context(hfp_connection);
1819     return ERROR_CODE_SUCCESS;
1820 }
1821 
1822 uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
1823     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1824     if (!hfp_connection) {
1825         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1826     }
1827 
1828     hfp_connection->hf_deactivate_calling_line_notification = 1;
1829     hfp_hf_run_for_context(hfp_connection);
1830     return ERROR_CODE_SUCCESS;
1831 }
1832 
1833 static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){
1834     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION);
1835     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION);
1836     return hf && ag;
1837 }
1838 
1839 uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1840     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1841     if (!hfp_connection) {
1842         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1843     }
1844     if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){
1845         return ERROR_CODE_COMMAND_DISALLOWED;
1846     }
1847 
1848     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1849     hfp_hf_run_for_context(hfp_connection);
1850     return ERROR_CODE_SUCCESS;
1851 }
1852 
1853 uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1854      hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1855     if (!hfp_connection) {
1856         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1857     }
1858     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1859         return ERROR_CODE_COMMAND_DISALLOWED;
1860     }
1861 
1862     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1863     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1864     if (!enhanced_vra_supported && !legacy_vra_supported){
1865         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1866     }
1867 
1868     switch (hfp_connection->vra_state){
1869         case HFP_VRA_VOICE_RECOGNITION_OFF:
1870         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1871             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1872             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
1873             hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported;
1874             break;
1875         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1876             hfp_connection->activate_voice_recognition = true;
1877             break;
1878         default:
1879             return ERROR_CODE_COMMAND_DISALLOWED;
1880     }
1881 
1882     hfp_hf_run_for_context(hfp_connection);
1883     return ERROR_CODE_SUCCESS;
1884 }
1885 
1886 uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
1887     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1888     if (!hfp_connection) {
1889         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1890     }
1891     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
1892         return ERROR_CODE_COMMAND_DISALLOWED;
1893     }
1894 
1895     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1896     if (!enhanced_vra_supported){
1897         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1898     }
1899 
1900     switch (hfp_connection->vra_state){
1901         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1902         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1903             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1904             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
1905             break;
1906         default:
1907             return ERROR_CODE_COMMAND_DISALLOWED;
1908     }
1909 
1910     hfp_hf_run_for_context(hfp_connection);
1911     return ERROR_CODE_SUCCESS;
1912 }
1913 
1914 
1915 uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1916     // return deactivate_voice_recognition(acl_handle, false);
1917     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1918     if (!hfp_connection) {
1919         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1920     }
1921 
1922     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
1923         hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1924         return ERROR_CODE_COMMAND_DISALLOWED;
1925     }
1926 
1927     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1928     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1929     if (!enhanced_vra_supported && !legacy_vra_supported){
1930         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1931     }
1932 
1933     switch (hfp_connection->vra_state){
1934         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
1935         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1936         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1937         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1938             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1939             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1940             break;
1941 
1942         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1943         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1944             hfp_connection->deactivate_voice_recognition = true;
1945             break;
1946 
1947         case HFP_VRA_VOICE_RECOGNITION_OFF:
1948         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1949         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1950         default:
1951             return ERROR_CODE_COMMAND_DISALLOWED;
1952     }
1953 
1954     hfp_hf_run_for_context(hfp_connection);
1955     return ERROR_CODE_SUCCESS;
1956 }
1957 
1958 uint8_t hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
1959     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1960     if (!hfp_connection) {
1961         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1962     }
1963 
1964     if (hfp_connection->microphone_gain == gain) {
1965         return ERROR_CODE_SUCCESS;
1966     }
1967 
1968     if ((gain < 0) || (gain > 15)){
1969         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1970         return ERROR_CODE_COMMAND_DISALLOWED;
1971     }
1972 
1973     hfp_connection->microphone_gain = gain;
1974     hfp_connection->send_microphone_gain = 1;
1975     hfp_hf_run_for_context(hfp_connection);
1976     return ERROR_CODE_SUCCESS;
1977 }
1978 
1979 uint8_t hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
1980     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1981     if (!hfp_connection) {
1982         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1983     }
1984 
1985     if (hfp_connection->speaker_gain == gain){
1986         return ERROR_CODE_SUCCESS;
1987     }
1988 
1989     if ((gain < 0) || (gain > 15)){
1990         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1991         return ERROR_CODE_COMMAND_DISALLOWED;
1992     }
1993 
1994     hfp_connection->speaker_gain = gain;
1995     hfp_connection->send_speaker_gain = 1;
1996     hfp_hf_run_for_context(hfp_connection);
1997     return ERROR_CODE_SUCCESS;
1998 }
1999 
2000 uint8_t hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
2001     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2002     if (!hfp_connection) {
2003         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2004     }
2005     hfp_connection->hf_send_dtmf_code = code;
2006     hfp_hf_run_for_context(hfp_connection);
2007     return ERROR_CODE_SUCCESS;
2008 }
2009 
2010 uint8_t hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
2011     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2012     if (!hfp_connection) {
2013         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2014     }
2015     hfp_connection->hf_send_binp = 1;
2016     hfp_hf_run_for_context(hfp_connection);
2017     return ERROR_CODE_SUCCESS;
2018 }
2019 
2020 uint8_t hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
2021     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2022     if (!hfp_connection) {
2023         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2024     }
2025     hfp_connection->hf_send_clcc = 1;
2026     hfp_hf_run_for_context(hfp_connection);
2027     return ERROR_CODE_SUCCESS;
2028 }
2029 
2030 
2031 uint8_t hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
2032     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2033     if (!hfp_connection) {
2034         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2035     }
2036     hfp_connection->hf_send_rrh = 1;
2037     hfp_connection->hf_send_rrh_command = '?';
2038     hfp_hf_run_for_context(hfp_connection);
2039     return ERROR_CODE_SUCCESS;
2040 }
2041 
2042 uint8_t hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
2043     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2044     if (!hfp_connection) {
2045         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2046     }
2047     hfp_connection->hf_send_rrh = 1;
2048     hfp_connection->hf_send_rrh_command = '0';
2049     hfp_hf_run_for_context(hfp_connection);
2050     return ERROR_CODE_SUCCESS;
2051 }
2052 
2053 uint8_t hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
2054     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2055     if (!hfp_connection) {
2056         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2057     }
2058     hfp_connection->hf_send_rrh = 1;
2059     hfp_connection->hf_send_rrh_command = '1';
2060     hfp_hf_run_for_context(hfp_connection);
2061     return ERROR_CODE_SUCCESS;
2062 }
2063 
2064 uint8_t hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
2065     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2066     if (!hfp_connection) {
2067         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2068     }
2069     hfp_connection->hf_send_rrh = 1;
2070     hfp_connection->hf_send_rrh_command = '2';
2071     hfp_hf_run_for_context(hfp_connection);
2072     return ERROR_CODE_SUCCESS;
2073 }
2074 
2075 uint8_t hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
2076     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2077     if (!hfp_connection) {
2078         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2079     }
2080     hfp_connection->hf_send_cnum = 1;
2081     hfp_hf_run_for_context(hfp_connection);
2082     return ERROR_CODE_SUCCESS;
2083 }
2084 
2085 uint8_t hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
2086     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2087     if (!hfp_connection) {
2088         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2089     }
2090     // find index for assigned number
2091     int i;
2092     for (i = 0; i < hfp_hf_indicators_nr ; i++){
2093         if (hfp_hf_indicators[i] == assigned_number){
2094             // set value
2095             hfp_hf_indicators_value[i] = value;
2096             // mark for update
2097             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
2098                 hfp_connection->generic_status_update_bitmap |= (1<<i);
2099                 // send update
2100                 hfp_hf_run_for_context(hfp_connection);
2101             }
2102             return ERROR_CODE_SUCCESS;
2103         }
2104     }
2105     return ERROR_CODE_SUCCESS;
2106 }
2107 
2108 int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
2109     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
2110     if (!hfp_connection) {
2111         return 0;
2112     }
2113     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
2114 }
2115 
2116 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){
2117 	if (!name){
2118 		name = hfp_hf_default_service_name;
2119 	}
2120 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
2121 
2122 	// Construct SupportedFeatures for SDP bitmap:
2123 	//
2124 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
2125 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
2126 	//
2127 	// Wide band speech (bit 5) requires Codec negotiation
2128 	//
2129 	uint16_t sdp_features = supported_features & 0x1f;
2130 	if ( (wide_band_speech != 0) && (supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION))){
2131 		sdp_features |= 1 << 5;
2132 	}
2133 
2134     if (supported_features & (1 << HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
2135         sdp_features |= 1 << 6;
2136     }
2137 
2138     if (supported_features & (1 << HFP_HFSF_VOICE_RECOGNITION_TEXT)){
2139         sdp_features |= 1 << 7;
2140     }
2141 
2142 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
2143 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
2144 }
2145 
2146 void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
2147 	btstack_assert(callback != NULL);
2148 
2149 	hfp_hf_callback = callback;
2150 	hfp_set_hf_callback(callback);
2151 }
2152