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