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