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