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