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