xref: /btstack/src/classic/hfp_hf.c (revision b321b6092d74051a4503b9e2674803ff9ce83061)
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 + 11];
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_type;
231     event[pos++] = hfp_connection->ag_msg.text_operation;
232 
233     // length, zero ending is already in message
234     uint8_t * value = &hfp_connection->line_buffer[0];
235     uint16_t  value_length = hfp_connection->ag_vra_msg_length;
236 
237     little_endian_store_16(event, pos, value_length);
238     pos += 2;
239     memcpy(&event[pos], value, value_length);
240     pos += value_length;
241 
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                         hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
549                         break;
550                 }
551                 break;
552         }
553         hfp_connection->command = HFP_CMD_NONE;
554     }
555 
556 
557     switch (hfp_connection->vra_state_requested){
558         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
559             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
560             if (done != 0){
561                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_OFF;
562                 hfp_connection->ok_pending = 1;
563             }
564             return 1;
565 
566 
567         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
568             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
569             if (done != 0){
570                 hfp_connection->vra_state_requested = HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED;
571                 hfp_connection->ok_pending = 1;
572                 return 1;
573             }
574             break;
575 
576         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
577             done = hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 2);
578             if (done != 0){
579                 hfp_connection->vra_state_requested = HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
580                 hfp_connection->ok_pending = 1;
581                 return 1;
582             }
583             break;
584 
585         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
586             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
587             hfp_connection->vra_state_requested = hfp_connection->vra_state;
588             hfp_connection->activate_voice_recognition = false;
589             if (hfp_connection->activate_voice_recognition){
590                 hfp_hf_activate_voice_recognition(hfp_connection->acl_handle);
591             } else {
592                 hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
593             }
594             break;
595 
596         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
597             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
598             hfp_connection->vra_state_requested = hfp_connection->vra_state;
599             hfp_connection->activate_voice_recognition = false;
600             if (hfp_connection->deactivate_voice_recognition){
601                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
602             } else {
603                 hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
604             }
605             break;
606 
607 
608         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
609             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
610             hfp_connection->vra_state_requested = hfp_connection->vra_state;
611             hfp_connection->activate_voice_recognition = false;
612             if (hfp_connection->deactivate_voice_recognition){
613                 hfp_hf_deactivate_voice_recognition(hfp_connection->acl_handle);
614             } else {
615                 hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
616             }
617             break;
618 
619         default:
620             break;
621     }
622     return done;
623 }
624 
625 
626 static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
627     if (hfp_connection->ok_pending) return 0;
628 
629     if (hfp_connection->trigger_codec_exchange){
630 		hfp_connection->trigger_codec_exchange = 0;
631 
632 		hfp_connection->ok_pending = 1;
633 		hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
634 		return 1;
635     }
636 
637     if (hfp_connection->hf_send_codec_confirm){
638 		hfp_connection->hf_send_codec_confirm = false;
639 
640 		hfp_connection->ok_pending = 1;
641 		hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
642 		return 1;
643     }
644 
645     if (hfp_connection->hf_send_supported_codecs){
646 		hfp_connection->hf_send_supported_codecs = false;
647 
648 		hfp_connection->ok_pending = 1;
649 		hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
650 		return 1;
651     }
652 
653     return 0;
654 }
655 
656 static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
657     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
658         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
659 
660     if (hfp_connection->release_audio_connection){
661         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
662         hfp_connection->release_audio_connection = 0;
663         gap_disconnect(hfp_connection->sco_handle);
664         return 1;
665     }
666 
667     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
668 
669     // run codecs exchange
670     int done = codecs_exchange_state_machine(hfp_connection);
671     if (done) return 1;
672 
673     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
674     if (hfp_connection->establish_audio_connection){
675         hfp_connection->state = HFP_W4_SCO_CONNECTED;
676         hfp_connection->establish_audio_connection = 0;
677         hfp_setup_synchronous_connection(hfp_connection);
678         return 1;
679     }
680     return 0;
681 }
682 
683 
684 static int call_setup_state_machine(hfp_connection_t * hfp_connection){
685 
686 	if (hfp_connection->ok_pending) return 0;
687 
688     if (hfp_connection->hf_answer_incoming_call){
689         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
690         hfp_connection->hf_answer_incoming_call = 0;
691         return 1;
692     }
693     return 0;
694 }
695 
696 static void hfp_hf_run_for_context(hfp_connection_t * hfp_connection){
697 
698 	btstack_assert(hfp_connection != NULL);
699 	btstack_assert(hfp_connection->local_role == HFP_ROLE_HF);
700 
701 	// during SDP query, RFCOMM CID is not set
702 	if (hfp_connection->rfcomm_cid == 0) return;
703 
704 	// assert command could be sent
705 	if (hci_can_send_command_packet_now() == 0) return;
706 
707 #ifdef ENABLE_CC256X_ASSISTED_HFP
708     // WBS Disassociate
709     if (hfp_connection->cc256x_send_wbs_disassociate){
710         hfp_connection->cc256x_send_wbs_disassociate = false;
711         hci_send_cmd(&hci_ti_wbs_disassociate);
712         return;
713     }
714     // Write Codec Config
715     if (hfp_connection->cc256x_send_write_codec_config){
716         hfp_connection->cc256x_send_write_codec_config = false;
717         hfp_cc256x_write_codec_config(hfp_connection);
718         return;
719     }
720     // WBS Associate
721     if (hfp_connection->cc256x_send_wbs_associate){
722         hfp_connection->cc256x_send_wbs_associate = false;
723         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
724         return;
725     }
726 #endif
727 #ifdef ENABLE_BCM_PCM_WBS
728     // Enable WBS
729     if (hfp_connection->bcm_send_enable_wbs){
730         hfp_connection->bcm_send_enable_wbs = false;
731         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
732         return;
733     }
734     // Write I2S/PCM params
735     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
736         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
737         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
738         return;
739     }
740     // Disable WBS
741     if (hfp_connection->bcm_send_disable_wbs){
742         hfp_connection->bcm_send_disable_wbs = false;
743         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
744         return;
745     }
746 #endif
747 #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
748     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
749         hfp_finalize_connection_context(hfp_connection);
750         return;
751     }
752 #endif
753 
754     if (hfp_connection->accept_sco){
755         bool incoming_eSCO = hfp_connection->accept_sco == 2;
756         hfp_connection->accept_sco = 0;
757         // notify about codec selection if not done already
758         if (hfp_connection->negotiated_codec == 0){
759             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
760         }
761         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
762         return;
763     }
764 
765     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
766         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
767         return;
768     }
769     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
770     if (!done){
771         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
772     }
773     if (!done){
774         done = hfp_hf_run_for_audio_connection(hfp_connection);
775     }
776     if (!done){
777         done = hfp_hf_voice_recognition_state_machine(hfp_connection);
778     }
779     if (!done){
780         done = call_setup_state_machine(hfp_connection);
781     }
782 
783     // don't send a new command while ok still pending or SLC is not established
784     if (hfp_connection->ok_pending || (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED)){
785         return;
786     }
787 
788     if (hfp_connection->send_microphone_gain){
789         hfp_connection->send_microphone_gain = 0;
790         hfp_connection->ok_pending = 1;
791         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
792         return;
793     }
794 
795     if (hfp_connection->send_speaker_gain){
796         hfp_connection->send_speaker_gain = 0;
797         hfp_connection->ok_pending = 1;
798         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
799         return;
800     }
801 
802     if (hfp_connection->hf_deactivate_calling_line_notification){
803         hfp_connection->hf_deactivate_calling_line_notification = 0;
804         hfp_connection->ok_pending = 1;
805         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
806         return;
807     }
808 
809     if (hfp_connection->hf_activate_calling_line_notification){
810         hfp_connection->hf_activate_calling_line_notification = 0;
811         hfp_connection->ok_pending = 1;
812         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
813         return;
814     }
815 
816     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
817         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
818         hfp_connection->response_pending_for_command = HFP_CMD_TURN_OFF_EC_AND_NR;
819         hfp_connection->ok_pending = 1;
820         hfp_hf_send_cmd_with_int(hfp_connection->rfcomm_cid, HFP_TURN_OFF_EC_AND_NR, 0);
821         return;
822     }
823 
824     if (hfp_connection->hf_deactivate_call_waiting_notification){
825         hfp_connection->hf_deactivate_call_waiting_notification = 0;
826         hfp_connection->ok_pending = 1;
827         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
828         return;
829     }
830 
831     if (hfp_connection->hf_activate_call_waiting_notification){
832         hfp_connection->hf_activate_call_waiting_notification = 0;
833         hfp_connection->ok_pending = 1;
834         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
835         return;
836     }
837 
838     if (hfp_connection->hf_initiate_outgoing_call){
839         hfp_connection->hf_initiate_outgoing_call = 0;
840         hfp_connection->ok_pending = 1;
841         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
842         return;
843     }
844 
845     if (hfp_connection->hf_initiate_memory_dialing){
846         hfp_connection->hf_initiate_memory_dialing = 0;
847         hfp_connection->ok_pending = 1;
848         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
849         return;
850     }
851 
852     if (hfp_connection->hf_initiate_redial_last_number){
853         hfp_connection->hf_initiate_redial_last_number = 0;
854         hfp_connection->ok_pending = 1;
855         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
856         return;
857     }
858 
859     if (hfp_connection->hf_send_chup){
860         hfp_connection->hf_send_chup = 0;
861         hfp_connection->ok_pending = 1;
862         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
863         return;
864     }
865 
866     if (hfp_connection->hf_send_chld_0){
867         hfp_connection->hf_send_chld_0 = 0;
868         hfp_connection->ok_pending = 1;
869         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
870         return;
871     }
872 
873     if (hfp_connection->hf_send_chld_1){
874         hfp_connection->hf_send_chld_1 = 0;
875         hfp_connection->ok_pending = 1;
876         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
877         return;
878     }
879 
880     if (hfp_connection->hf_send_chld_2){
881         hfp_connection->hf_send_chld_2 = 0;
882         hfp_connection->ok_pending = 1;
883         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
884         return;
885     }
886 
887     if (hfp_connection->hf_send_chld_3){
888         hfp_connection->hf_send_chld_3 = 0;
889         hfp_connection->ok_pending = 1;
890         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
891         return;
892     }
893 
894     if (hfp_connection->hf_send_chld_4){
895         hfp_connection->hf_send_chld_4 = 0;
896         hfp_connection->ok_pending = 1;
897         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
898         return;
899     }
900 
901     if (hfp_connection->hf_send_chld_x){
902         hfp_connection->hf_send_chld_x = 0;
903         hfp_connection->ok_pending = 1;
904         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
905         return;
906     }
907 
908     if (hfp_connection->hf_send_dtmf_code){
909         char code = hfp_connection->hf_send_dtmf_code;
910         hfp_connection->hf_send_dtmf_code = 0;
911         hfp_connection->ok_pending = 1;
912         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
913         return;
914     }
915 
916     if (hfp_connection->hf_send_binp){
917         hfp_connection->hf_send_binp = 0;
918         hfp_connection->ok_pending = 1;
919         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
920         return;
921     }
922 
923     if (hfp_connection->hf_send_clcc){
924         hfp_connection->hf_send_clcc = 0;
925         hfp_connection->ok_pending = 1;
926         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
927         return;
928     }
929 
930     if (hfp_connection->hf_send_rrh){
931         hfp_connection->hf_send_rrh = 0;
932         char buffer[20];
933         switch (hfp_connection->hf_send_rrh_command){
934             case '?':
935                 snprintf(buffer, sizeof(buffer), "AT%s?\r",
936                          HFP_RESPONSE_AND_HOLD);
937                 buffer[sizeof(buffer) - 1] = 0;
938                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
939                 return;
940             case '0':
941             case '1':
942             case '2':
943                 snprintf(buffer, sizeof(buffer), "AT%s=%c\r",
944                          HFP_RESPONSE_AND_HOLD,
945                          hfp_connection->hf_send_rrh_command);
946                 buffer[sizeof(buffer) - 1] = 0;
947                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
948                 return;
949             default:
950                 break;
951         }
952         return;
953     }
954 
955     if (hfp_connection->hf_send_cnum){
956         hfp_connection->hf_send_cnum = 0;
957         char buffer[20];
958         snprintf(buffer, sizeof(buffer), "AT%s\r",
959                  HFP_SUBSCRIBER_NUMBER_INFORMATION);
960         buffer[sizeof(buffer) - 1] = 0;
961         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
962         return;
963     }
964 
965     // update HF indicators
966     if (hfp_connection->generic_status_update_bitmap){
967         int i;
968         for (i=0; i < hfp_hf_indicators_nr; i++){
969             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
970                 if (hfp_connection->generic_status_indicators[i].state){
971                     hfp_connection->ok_pending = 1;
972                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
973                     char buffer[30];
974                     snprintf(buffer, sizeof(buffer), "AT%s=%u,%u\r",
975                              HFP_TRANSFER_HF_INDICATOR_STATUS,
976                              hfp_hf_indicators[i],
977                              (unsigned int)hfp_hf_indicators_value[i]);
978                     buffer[sizeof(buffer) - 1] = 0;
979                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
980                 } else {
981                     log_info("Not sending HF indicator %u as it is disabled", hfp_hf_indicators[i]);
982                 }
983                 return;
984             }
985         }
986     }
987 
988     if (done) return;
989     // deal with disconnect
990     switch (hfp_connection->state){
991         case HFP_W2_DISCONNECT_RFCOMM:
992             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
993             rfcomm_disconnect(hfp_connection->rfcomm_cid);
994             break;
995 
996         default:
997             break;
998     }
999 }
1000 
1001 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
1002     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
1003 
1004     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
1005 
1006     uint8_t i;
1007     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1008         hfp_connection->ag_indicators[i].status_changed = 0;
1009         hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1010     }
1011     // restore volume settings
1012     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
1013     hfp_connection->send_speaker_gain = 1;
1014     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
1015     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
1016     hfp_connection->send_microphone_gain = 1;
1017     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
1018     // enable all indicators
1019     for (i=0; i < hfp_hf_indicators_nr; i++){
1020         hfp_connection->generic_status_indicators[i].uuid = hfp_hf_indicators[i];
1021         hfp_connection->generic_status_indicators[i].state = 1;
1022     }
1023 }
1024 
1025 static void hfp_hf_handle_suggested_codec(hfp_connection_t * hfp_connection){
1026 	if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_hf_codecs_nr, hfp_hf_codecs)){
1027 		// Codec supported, confirm
1028 		hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1029 		hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1030 		log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
1031 		hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
1032 
1033 		hfp_connection->hf_send_codec_confirm = true;
1034 	} else {
1035 		// Codec not supported, send supported codecs
1036 		hfp_connection->codec_confirmed = 0;
1037 		hfp_connection->suggested_codec = 0;
1038 		hfp_connection->negotiated_codec = 0;
1039 		hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
1040 
1041 		hfp_connection->hf_send_supported_codecs = true;
1042 	}
1043 }
1044 
1045 static bool hfp_hf_switch_on_ok_pending(hfp_connection_t *hfp_connection, uint8_t status){
1046     bool event_emited = true;
1047 
1048     switch (hfp_connection->response_pending_for_command){
1049         case HFP_CMD_TURN_OFF_EC_AND_NR:
1050             hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status);
1051             break;
1052         default:
1053             event_emited = false;
1054 
1055             switch (hfp_connection->state){
1056                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
1057                     if (has_codec_negotiation_feature(hfp_connection)){
1058                         hfp_connection->state = HFP_NOTIFY_ON_CODECS;
1059                         break;
1060                     }
1061                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
1062                     break;
1063 
1064                 case HFP_W4_NOTIFY_ON_CODECS:
1065                     hfp_connection->state = HFP_RETRIEVE_INDICATORS;
1066                     break;
1067 
1068                 case HFP_W4_RETRIEVE_INDICATORS:
1069                     hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
1070                     break;
1071 
1072                 case HFP_W4_RETRIEVE_INDICATORS_STATUS:
1073                     hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
1074                     break;
1075 
1076                 case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
1077                     if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
1078                         hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
1079                         break;
1080                     }
1081                     if (has_hf_indicators_feature(hfp_connection)){
1082                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
1083                         break;
1084                     }
1085                     hfp_ag_slc_established(hfp_connection);
1086                     break;
1087 
1088                 case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
1089                     if (has_hf_indicators_feature(hfp_connection)){
1090                         hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
1091                         break;
1092                     }
1093                     hfp_ag_slc_established(hfp_connection);
1094                     break;
1095 
1096                 case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
1097                     hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
1098                     break;
1099 
1100                 case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
1101                     hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
1102                     break;
1103 
1104                 case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
1105                     hfp_ag_slc_established(hfp_connection);
1106                     break;
1107                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1108                     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
1109                         hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
1110                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1111                         break;
1112                     }
1113 
1114                     if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
1115                         hfp_connection->change_status_update_for_individual_ag_indicators = 0;
1116                         hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
1117                         break;
1118                     }
1119 
1120                     switch (hfp_connection->hf_query_operator_state){
1121                         case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
1122                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1123                             break;
1124                         case HPF_HF_QUERY_OPERATOR_W4_RESULT:
1125                             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
1126                             hfp_emit_network_operator_event(hfp_connection);
1127                             break;
1128                         default:
1129                             break;
1130                     }
1131 
1132                     if (hfp_connection->enable_extended_audio_gateway_error_report){
1133                         hfp_connection->enable_extended_audio_gateway_error_report = 0;
1134                         break;
1135                     }
1136 
1137                     switch (hfp_connection->codecs_state){
1138                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1139                             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
1140                             break;
1141                         case HFP_CODECS_HF_CONFIRMED_CODEC:
1142                             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1143                             break;
1144                         default:
1145                             break;
1146                     }
1147                     hfp_hf_voice_recognition_state_machine(hfp_connection);
1148                     break;
1149                 case HFP_AUDIO_CONNECTION_ESTABLISHED:
1150                     hfp_hf_voice_recognition_state_machine(hfp_connection);
1151                     break;
1152                 default:
1153                     break;
1154             }
1155             break;
1156     }
1157 
1158     // done
1159     hfp_connection->response_pending_for_command = HFP_CMD_NONE;
1160     hfp_connection->ok_pending = 0;
1161     hfp_connection->command = HFP_CMD_NONE;
1162     return event_emited;
1163 }
1164 
1165 static bool hfp_is_ringing(hfp_callsetup_status_t callsetup_status){
1166     switch (callsetup_status){
1167         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1168         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1169             return true;
1170         default:
1171             return false;
1172    }
1173 }
1174 
1175 static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
1176     uint16_t i;
1177 
1178     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1179         if (hfp_connection->ag_indicators[i].status_changed) {
1180             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1181                 hfp_callsetup_status_t new_hf_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1182                 bool ringing_old = hfp_is_ringing(hfp_hf_callsetup_status);
1183                 bool ringing_new = hfp_is_ringing(new_hf_callsetup_status);
1184                 if (ringing_old != ringing_new){
1185                     if (ringing_new){
1186                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGING);
1187                     } else {
1188                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGING);
1189                     }
1190                 }
1191                 hfp_hf_callsetup_status = new_hf_callsetup_status;
1192             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1193                 hfp_hf_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1194                 // avoid set but not used warning
1195                 (void) hfp_hf_callheld_status;
1196             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1197                 hfp_call_status_t new_hf_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1198                 if (hfp_hf_call_status != new_hf_call_status){
1199                     if (new_hf_call_status == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){
1200                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1201                     } else {
1202                         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1203                     }
1204                 }
1205                 hfp_hf_call_status = new_hf_call_status;
1206             }
1207             hfp_connection->ag_indicators[i].status_changed = 0;
1208             hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1209             break;
1210         }
1211     }
1212 }
1213 
1214 static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1215     int value;
1216     int i;
1217     bool event_emited;
1218 
1219     switch (hfp_connection->command){
1220         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1221             hfp_connection->command = HFP_CMD_NONE;
1222             hfp_hf_emit_subscriber_information(hfp_connection, 0);
1223             break;
1224         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1225             hfp_connection->command = HFP_CMD_NONE;
1226             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1227             break;
1228         case HFP_CMD_LIST_CURRENT_CALLS:
1229             hfp_connection->command = HFP_CMD_NONE;
1230             hfp_hf_emit_enhanced_call_status(hfp_connection);
1231             break;
1232         case HFP_CMD_SET_SPEAKER_GAIN:
1233             hfp_connection->command = HFP_CMD_NONE;
1234             value = btstack_atoi((char*)hfp_connection->line_buffer);
1235             hfp_hf_speaker_gain = value;
1236             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1237             break;
1238         case HFP_CMD_SET_MICROPHONE_GAIN:
1239             hfp_connection->command = HFP_CMD_NONE;
1240             value = btstack_atoi((char*)hfp_connection->line_buffer);
1241             hfp_hf_microphone_gain = value;
1242             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1243             break;
1244         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1245             hfp_connection->command = HFP_CMD_NONE;
1246             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1247             break;
1248         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1249             hfp_connection->command = HFP_CMD_NONE;
1250             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION);
1251             break;
1252         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1253             hfp_connection->command = HFP_CMD_NONE;
1254             hfp_hf_emit_type_and_number(hfp_connection, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION);
1255             break;
1256         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1257             hfp_connection->command = HFP_CMD_NONE;
1258             hfp_connection->ok_pending = 0;
1259             hfp_connection->extended_audio_gateway_error = 0;
1260             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1261             break;
1262         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
1263             break;
1264         case HFP_CMD_ERROR:
1265             switch (hfp_connection->state){
1266                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1267                     switch (hfp_connection->codecs_state){
1268                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1269                             hfp_reset_context_flags(hfp_connection);
1270                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
1271                             return;
1272                         default:
1273                             break;
1274                     }
1275                     break;
1276                 default:
1277                     break;
1278             }
1279 
1280             // handle error response for voice activation (HF initiated)
1281             switch(hfp_connection->vra_state_requested){
1282                 case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1283                     hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1284                     break;
1285                 default:
1286                     if (hfp_connection->vra_state_requested == hfp_connection->vra_state){
1287                         break;
1288                     }
1289                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
1290                     hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1291                     hfp_reset_context_flags(hfp_connection);
1292                     return;
1293             }
1294             event_emited = hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
1295             if (!event_emited){
1296                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1297             }
1298             hfp_reset_context_flags(hfp_connection);
1299             break;
1300 
1301         case HFP_CMD_OK:
1302             hfp_hf_switch_on_ok_pending(hfp_connection, ERROR_CODE_SUCCESS);
1303             break;
1304         case HFP_CMD_RING:
1305             hfp_connection->command = HFP_CMD_NONE;
1306             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1307             break;
1308         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1309             hfp_connection->command = HFP_CMD_NONE;
1310             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1311             break;
1312         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1313             hfp_connection->command = HFP_CMD_NONE;
1314             // report status after SLC established
1315             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
1316                 break;
1317             }
1318             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1319                 hfp_emit_ag_indicator_event(hfp_connection, &hfp_connection->ag_indicators[i]);
1320             }
1321             break;
1322     	case HFP_CMD_AG_SUGGESTED_CODEC:
1323             hfp_connection->command = HFP_CMD_NONE;
1324     		hfp_hf_handle_suggested_codec(hfp_connection);
1325 			break;
1326         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1327             hfp_emit_event(hfp_connection, HFP_SUBEVENT_IN_BAND_RING_TONE, get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE));
1328         default:
1329             break;
1330     }
1331 }
1332 
1333 static int hfp_parser_is_end_of_line(uint8_t byte){
1334 	return (byte == '\n') || (byte == '\r');
1335 }
1336 
1337 static void hfp_hf_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
1338     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1339     if (size < 1) return;
1340 
1341     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1342 #ifdef ENABLE_HFP_AT_MESSAGES
1343     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1344 #endif
1345 
1346     // process messages byte-wise
1347     uint8_t pos;
1348     for (pos = 0; pos < size; pos++){
1349         hfp_parse(hfp_connection, packet[pos], 1);
1350         // parse until end of line "\r" or "\n"
1351         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1352         hfp_hf_handle_rfcomm_command(hfp_connection);
1353     }
1354 }
1355 
1356 static void hfp_hf_run(void){
1357     btstack_linked_list_iterator_t it;
1358     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1359     while (btstack_linked_list_iterator_has_next(&it)){
1360         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1361         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1362         hfp_hf_run_for_context(hfp_connection);
1363     }
1364 }
1365 
1366 static void hfp_hf_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1367     hfp_connection_t * hfp_connection;
1368     switch (packet_type){
1369         case RFCOMM_DATA_PACKET:
1370             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1371             if (!hfp_connection) return;
1372             hfp_hf_handle_rfcomm_data(hfp_connection, packet, size);
1373             hfp_hf_run_for_context(hfp_connection);
1374             return;
1375         case HCI_EVENT_PACKET:
1376             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1377                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
1378                 hfp_hf_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1379                 return;
1380             }
1381             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1382             break;
1383         default:
1384             break;
1385     }
1386     hfp_hf_run();
1387 }
1388 
1389 static void hfp_hf_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1390     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1391     hfp_hf_run();
1392 }
1393 
1394 static void hfp_hf_set_defaults(void){
1395     hfp_hf_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1396     hfp_hf_call_status = HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS;
1397     hfp_hf_callsetup_status = HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1398     hfp_hf_callheld_status= HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1399     hfp_hf_codecs_nr = 0;
1400     hfp_hf_speaker_gain = 9;
1401     hfp_hf_microphone_gain = 9;
1402     hfp_hf_indicators_nr = 0;
1403 }
1404 
1405 uint8_t hfp_hf_init(uint16_t rfcomm_channel_nr){
1406     uint8_t status = rfcomm_register_service(hfp_hf_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1407     if (status != ERROR_CODE_SUCCESS){
1408         return status;
1409     }
1410 
1411     hfp_init();
1412     hfp_hf_set_defaults();
1413 
1414     hfp_hf_hci_event_callback_registration.callback = &hfp_hf_hci_event_packet_handler;
1415     hci_add_event_handler(&hfp_hf_hci_event_callback_registration);
1416 
1417     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
1418     hfp_set_hf_rfcomm_packet_handler(&hfp_hf_rfcomm_packet_handler);
1419     return ERROR_CODE_SUCCESS;
1420 }
1421 
1422 void hfp_hf_deinit(void){
1423     hfp_deinit();
1424     hfp_hf_set_defaults();
1425 
1426     hfp_hf_callback = NULL;
1427     (void) memset(&hfp_hf_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
1428     (void) memset(hfp_hf_phone_number, 0, sizeof(hfp_hf_phone_number));
1429 }
1430 
1431 void hfp_hf_init_codecs(int codecs_nr, const uint8_t * codecs){
1432     btstack_assert(codecs_nr < HFP_MAX_NUM_CODECS);
1433 
1434     hfp_hf_codecs_nr = codecs_nr;
1435     int i;
1436     for (i=0; i<codecs_nr; i++){
1437         hfp_hf_codecs[i] = codecs[i];
1438     }
1439 }
1440 
1441 void hfp_hf_init_supported_features(uint32_t supported_features){
1442     hfp_hf_supported_features = supported_features;
1443 }
1444 
1445 void hfp_hf_init_hf_indicators(int indicators_nr, const uint16_t * indicators){
1446     btstack_assert(hfp_hf_indicators_nr < HFP_MAX_NUM_INDICATORS);
1447 
1448     hfp_hf_indicators_nr = indicators_nr;
1449     int i;
1450     for (i = 0; i < hfp_hf_indicators_nr ; i++){
1451         hfp_hf_indicators[i] = indicators[i];
1452     }
1453 }
1454 
1455 uint8_t hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1456     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
1457 }
1458 
1459 uint8_t hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
1460     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1461     if (!hfp_connection){
1462         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1463     }
1464     hfp_trigger_release_service_level_connection(hfp_connection);
1465     hfp_hf_run_for_context(hfp_connection);
1466     return ERROR_CODE_SUCCESS;
1467 }
1468 
1469 static uint8_t hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
1470     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1471     if (!hfp_connection) {
1472         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1473     }
1474     hfp_connection->enable_status_update_for_ag_indicators = enable;
1475     hfp_hf_run_for_context(hfp_connection);
1476     return ERROR_CODE_SUCCESS;
1477 }
1478 
1479 uint8_t hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1480     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1481 }
1482 
1483 uint8_t hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1484     return hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1485 }
1486 
1487 // TODO: returned ERROR - wrong format
1488 uint8_t hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
1489     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1490     if (!hfp_connection) {
1491         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1492     }
1493     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1494     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1495     hfp_hf_run_for_context(hfp_connection);
1496     return ERROR_CODE_SUCCESS;
1497 }
1498 
1499 uint8_t hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
1500     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1501     if (!hfp_connection) {
1502         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1503     }
1504 
1505     switch (hfp_connection->hf_query_operator_state){
1506         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1507             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1508             break;
1509         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1510             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1511             break;
1512         default:
1513             return ERROR_CODE_COMMAND_DISALLOWED;
1514     }
1515     hfp_hf_run_for_context(hfp_connection);
1516     return ERROR_CODE_SUCCESS;
1517 }
1518 
1519 static uint8_t hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
1520     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1521     if (!hfp_connection) {
1522         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1523     }
1524     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1525     hfp_hf_run_for_context(hfp_connection);
1526     return ERROR_CODE_SUCCESS;
1527 }
1528 
1529 
1530 uint8_t hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1531     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1532 }
1533 
1534 uint8_t hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1535     return hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1536 }
1537 
1538 static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
1539     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_hf_supported_features & (1 << HFP_HFSF_ESCO_S4));
1540 }
1541 
1542 uint8_t hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
1543     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1544     if (!hfp_connection) {
1545         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1546     }
1547 
1548     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
1549         return ERROR_CODE_COMMAND_DISALLOWED;
1550     }
1551 
1552     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
1553         return ERROR_CODE_COMMAND_DISALLOWED;
1554     }
1555     if (has_codec_negotiation_feature(hfp_connection)) {
1556         switch (hfp_connection->codecs_state) {
1557             case HFP_CODECS_W4_AG_COMMON_CODEC:
1558                 break;
1559             case HFP_CODECS_EXCHANGED:
1560                 hfp_connection->trigger_codec_exchange = 1;
1561                 break;
1562             default:
1563                 hfp_connection->codec_confirmed = 0;
1564                 hfp_connection->suggested_codec = 0;
1565                 hfp_connection->negotiated_codec = 0;
1566                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
1567                 hfp_connection->trigger_codec_exchange = 1;
1568                 break;
1569         }
1570     } else {
1571         log_info("no codec negotiation feature, use CVSD");
1572         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1573         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1574         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1575         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1576         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1577         hfp_connection->establish_audio_connection = 1;
1578         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1579     }
1580 
1581     hfp_hf_run_for_context(hfp_connection);
1582     return ERROR_CODE_SUCCESS;
1583 }
1584 
1585 uint8_t hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
1586     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1587     if (!hfp_connection) {
1588         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1589     }
1590     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
1591         return ERROR_CODE_COMMAND_DISALLOWED;
1592     }
1593     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
1594     if (status == ERROR_CODE_SUCCESS){
1595         hfp_hf_run_for_context(hfp_connection);
1596     }
1597     return ERROR_CODE_SUCCESS;
1598 }
1599 
1600 uint8_t hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
1601     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1602     if (!hfp_connection) {
1603         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1604     }
1605 
1606     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1607         hfp_connection->hf_answer_incoming_call = 1;
1608         hfp_hf_run_for_context(hfp_connection);
1609     } else {
1610         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_hf_callsetup_status);
1611         return ERROR_CODE_COMMAND_DISALLOWED;
1612     }
1613     return ERROR_CODE_SUCCESS;
1614 }
1615 
1616 uint8_t hfp_hf_terminate_call(hci_con_handle_t acl_handle){
1617     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1618     if (!hfp_connection) {
1619         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1620     }
1621     hfp_connection->hf_send_chup = 1;
1622     hfp_hf_run_for_context(hfp_connection);
1623     return ERROR_CODE_SUCCESS;
1624 }
1625 
1626 uint8_t hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
1627     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1628     if (!hfp_connection) {
1629         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1630     }
1631 
1632     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1633         hfp_connection->hf_send_chup = 1;
1634         hfp_hf_run_for_context(hfp_connection);
1635     }
1636     return ERROR_CODE_SUCCESS;
1637 }
1638 
1639 uint8_t hfp_hf_user_busy(hci_con_handle_t acl_handle){
1640     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1641     if (!hfp_connection) {
1642         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1643     }
1644 
1645     if (hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1646         hfp_connection->hf_send_chld_0 = 1;
1647         hfp_hf_run_for_context(hfp_connection);
1648     }
1649     return ERROR_CODE_SUCCESS;
1650 }
1651 
1652 uint8_t hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
1653     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1654     if (!hfp_connection) {
1655         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1656     }
1657 
1658     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1659         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1660         hfp_connection->hf_send_chld_1 = 1;
1661         hfp_hf_run_for_context(hfp_connection);
1662     }
1663     return ERROR_CODE_SUCCESS;
1664 }
1665 
1666 uint8_t hfp_hf_swap_calls(hci_con_handle_t acl_handle){
1667     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1668     if (!hfp_connection) {
1669         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1670     }
1671 
1672     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1673         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1674         hfp_connection->hf_send_chld_2 = 1;
1675         hfp_hf_run_for_context(hfp_connection);
1676     }
1677     return ERROR_CODE_SUCCESS;
1678 }
1679 
1680 uint8_t hfp_hf_join_held_call(hci_con_handle_t acl_handle){
1681     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1682     if (!hfp_connection) {
1683         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1684     }
1685 
1686     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1687         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1688         hfp_connection->hf_send_chld_3 = 1;
1689         hfp_hf_run_for_context(hfp_connection);
1690     }
1691     return ERROR_CODE_SUCCESS;
1692 }
1693 
1694 uint8_t hfp_hf_connect_calls(hci_con_handle_t acl_handle){
1695     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1696     if (!hfp_connection) {
1697         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1698     }
1699 
1700     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1701         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1702         hfp_connection->hf_send_chld_4 = 1;
1703         hfp_hf_run_for_context(hfp_connection);
1704     }
1705     return ERROR_CODE_SUCCESS;
1706 }
1707 
1708 uint8_t hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
1709     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1710     if (!hfp_connection) {
1711         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1712     }
1713 
1714     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1715         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1716         hfp_connection->hf_send_chld_x = 1;
1717         hfp_connection->hf_send_chld_x_index = 10 + index;
1718         hfp_hf_run_for_context(hfp_connection);
1719     }
1720     return ERROR_CODE_SUCCESS;
1721 }
1722 
1723 uint8_t hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
1724     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1725     if (!hfp_connection) {
1726         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1727     }
1728 
1729     if ((hfp_hf_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1730         (hfp_hf_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1731         hfp_connection->hf_send_chld_x = 1;
1732         hfp_connection->hf_send_chld_x_index = 20 + index;
1733         hfp_hf_run_for_context(hfp_connection);
1734     }
1735     return ERROR_CODE_SUCCESS;
1736 }
1737 
1738 uint8_t hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
1739     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1740     if (!hfp_connection) {
1741         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1742     }
1743 
1744     hfp_connection->hf_initiate_outgoing_call = 1;
1745     snprintf(hfp_hf_phone_number, sizeof(hfp_hf_phone_number), "%s", number);
1746     hfp_hf_run_for_context(hfp_connection);
1747     return ERROR_CODE_SUCCESS;
1748 }
1749 
1750 uint8_t hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
1751     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1752     if (!hfp_connection) {
1753         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1754     }
1755 
1756     hfp_connection->hf_initiate_memory_dialing = 1;
1757     hfp_connection->memory_id = memory_id;
1758 
1759     hfp_hf_run_for_context(hfp_connection);
1760     return ERROR_CODE_SUCCESS;
1761 }
1762 
1763 uint8_t hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
1764     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1765     if (!hfp_connection) {
1766         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1767     }
1768 
1769     hfp_connection->hf_initiate_redial_last_number = 1;
1770     hfp_hf_run_for_context(hfp_connection);
1771     return ERROR_CODE_SUCCESS;
1772 }
1773 
1774 uint8_t hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
1775     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1776     if (!hfp_connection) {
1777         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1778     }
1779 
1780     hfp_connection->hf_activate_call_waiting_notification = 1;
1781     hfp_hf_run_for_context(hfp_connection);
1782     return ERROR_CODE_SUCCESS;
1783 }
1784 
1785 
1786 uint8_t hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
1787     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1788     if (!hfp_connection) {
1789         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1790     }
1791 
1792     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1793     hfp_hf_run_for_context(hfp_connection);
1794     return ERROR_CODE_SUCCESS;
1795 }
1796 
1797 
1798 uint8_t hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
1799     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1800     if (!hfp_connection) {
1801         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1802     }
1803 
1804     hfp_connection->hf_activate_calling_line_notification = 1;
1805     hfp_hf_run_for_context(hfp_connection);
1806     return ERROR_CODE_SUCCESS;
1807 }
1808 
1809 uint8_t hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
1810     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1811     if (!hfp_connection) {
1812         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1813     }
1814 
1815     hfp_connection->hf_deactivate_calling_line_notification = 1;
1816     hfp_hf_run_for_context(hfp_connection);
1817     return ERROR_CODE_SUCCESS;
1818 }
1819 
1820 static bool hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection_t * hfp_connection){
1821     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_EC_NR_FUNCTION);
1822     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_EC_NR_FUNCTION);
1823     return hf && ag;
1824 }
1825 
1826 uint8_t hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1827     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1828     if (!hfp_connection) {
1829         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1830     }
1831     if (!hfp_hf_echo_canceling_and_noise_reduction_supported(hfp_connection)){
1832         return ERROR_CODE_COMMAND_DISALLOWED;
1833     }
1834 
1835     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1836     hfp_hf_run_for_context(hfp_connection);
1837     return ERROR_CODE_SUCCESS;
1838 }
1839 
1840 static bool hfp_hf_vra_flag_supported(hfp_connection_t * hfp_connection){
1841     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION);
1842     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION);
1843     return hf && ag;
1844 }
1845 
1846 static bool hfp_hf_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){
1847     int hf = get_bit(hfp_hf_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1848     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS);
1849     return hf && ag;
1850 }
1851 
1852 uint8_t hfp_hf_activate_voice_recognition(hci_con_handle_t acl_handle){
1853      hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1854     if (!hfp_connection) {
1855         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1856     }
1857     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1858         return ERROR_CODE_COMMAND_DISALLOWED;
1859     }
1860 
1861     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1862     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1863     if (!enhanced_vra_supported && !legacy_vra_supported){
1864         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1865     }
1866 
1867     switch (hfp_connection->vra_state){
1868         case HFP_VRA_VOICE_RECOGNITION_OFF:
1869         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1870             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1871             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
1872             hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported;
1873             break;
1874         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1875             hfp_connection->activate_voice_recognition = true;
1876             break;
1877         default:
1878             return ERROR_CODE_COMMAND_DISALLOWED;
1879     }
1880 
1881     hfp_hf_run_for_context(hfp_connection);
1882     return ERROR_CODE_SUCCESS;
1883 }
1884 
1885 uint8_t hfp_hf_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
1886     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1887     if (!hfp_connection) {
1888         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1889     }
1890     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
1891         return ERROR_CODE_COMMAND_DISALLOWED;
1892     }
1893 
1894     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1895     if (!enhanced_vra_supported){
1896         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1897     }
1898 
1899     switch (hfp_connection->vra_state){
1900         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1901         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1902             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1903             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
1904             break;
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 
1914 uint8_t hfp_hf_deactivate_voice_recognition(hci_con_handle_t acl_handle){
1915     // return deactivate_voice_recognition(acl_handle, false);
1916     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1917     if (!hfp_connection) {
1918         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1919     }
1920 
1921     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
1922         hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
1923         return ERROR_CODE_COMMAND_DISALLOWED;
1924     }
1925 
1926     bool enhanced_vra_supported = hfp_hf_enhanced_vra_flag_supported(hfp_connection);
1927     bool legacy_vra_supported   = hfp_hf_vra_flag_supported(hfp_connection);
1928     if (!enhanced_vra_supported && !legacy_vra_supported){
1929         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
1930     }
1931 
1932     switch (hfp_connection->vra_state){
1933         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
1934         case HFP_VRA_VOICE_RECOGNITION_ACTIVATED:
1935         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1936         case HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1937             hfp_connection->command = HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION;
1938             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1939             break;
1940 
1941         case HFP_VRA_W4_VOICE_RECOGNITION_ACTIVATED:
1942         case HFP_VRA_W4_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
1943             hfp_connection->deactivate_voice_recognition = true;
1944             break;
1945 
1946         case HFP_VRA_VOICE_RECOGNITION_OFF:
1947         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
1948         case HFP_VRA_W4_VOICE_RECOGNITION_OFF:
1949         default:
1950             return ERROR_CODE_COMMAND_DISALLOWED;
1951     }
1952 
1953     hfp_hf_run_for_context(hfp_connection);
1954     return ERROR_CODE_SUCCESS;
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