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