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