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