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