xref: /btstack/src/classic/hfp_hf.c (revision cfd54eb73cd29e7bf738f261fb454a84a1bb66b0)
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 <string.h>
50 
51 #include "bluetooth_sdp.h"
52 #include "btstack_debug.h"
53 #include "btstack_event.h"
54 #include "btstack_memory.h"
55 #include "btstack_run_loop.h"
56 #include "classic/core.h"
57 #include "classic/hfp.h"
58 #include "classic/hfp_hf.h"
59 #include "classic/sdp_client_rfcomm.h"
60 #include "classic/sdp_server.h"
61 #include "classic/sdp_util.h"
62 #include "hci.h"
63 #include "hci_cmd.h"
64 #include "hci_dump.h"
65 #include "l2cap.h"
66 
67 static btstack_packet_callback_registration_t hci_event_callback_registration;
68 
69 static const char default_hfp_hf_service_name[] = "Hands-Free unit";
70 static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
71 static uint8_t hfp_codecs_nr = 0;
72 static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
73 
74 static uint8_t hfp_indicators_nr = 0;
75 static uint8_t hfp_indicators[HFP_MAX_NUM_INDICATORS];
76 static uint32_t hfp_indicators_value[HFP_MAX_NUM_INDICATORS];
77 
78 static uint8_t hfp_hf_speaker_gain = 9;
79 static uint8_t hfp_hf_microphone_gain = 9;
80 
81 static btstack_packet_handler_t hfp_hf_callback;
82 
83 static hfp_call_status_t hfp_call_status;
84 static hfp_callsetup_status_t hfp_callsetup_status;
85 static hfp_callheld_status_t hfp_callheld_status;
86 
87 static char phone_number[25];
88 
89 static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
90     btstack_linked_list_iterator_t it;
91     btstack_linked_list_iterator_init(&it, hfp_get_connections());
92     while (btstack_linked_list_iterator_has_next(&it)){
93         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
94         if (hfp_connection->acl_handle != handle)      continue;
95         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
96         return hfp_connection;
97     }
98     return NULL;
99 }
100 
101 void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
102     if (callback == NULL){
103         log_error("hfp_hf_register_packet_handler called with NULL callback");
104         return;
105     }
106     hfp_hf_callback = callback;
107     hfp_set_hf_callback(callback);
108 }
109 
110 static void hfp_hf_emit_subscriber_information(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t status, uint8_t bnip_type, const char * bnip_number){
111     if (!callback) return;
112     uint8_t event[31];
113     event[0] = HCI_EVENT_HFP_META;
114     event[1] = sizeof(event) - 2;
115     event[2] = event_subtype;
116     event[3] = status;
117     event[4] = bnip_type;
118     uint16_t size = btstack_min(strlen(bnip_number), sizeof(event) - 6);
119     strncpy((char*)&event[5], bnip_number, size);
120     event[5 + size] = 0;
121     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
122 }
123 
124 static void hfp_hf_emit_type_and_number(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t bnip_type, const char * bnip_number){
125     if (!callback) return;
126     uint8_t event[30];
127     event[0] = HCI_EVENT_HFP_META;
128     event[1] = sizeof(event) - 2;
129     event[2] = event_subtype;
130     event[3] = bnip_type;
131     uint16_t size = btstack_min(strlen(bnip_number), sizeof(event) - 5);
132     strncpy((char*)&event[4], bnip_number, size);
133     event[4 + size] = 0;
134     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
135 }
136 
137 static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, hfp_connection_t * connection){
138     if (!callback) return;
139     uint8_t event[36];
140     int pos = 0;
141     event[pos++] = HCI_EVENT_HFP_META;
142     event[pos++] = sizeof(event) - 2;
143     event[pos++] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
144     event[pos++] = connection->clcc_idx;
145     event[pos++] = connection->clcc_dir;
146     event[pos++] = connection->clcc_status;
147     event[pos++] = connection->clcc_mode;
148     event[pos++] = connection->clcc_mpty;
149     event[pos++] = connection->bnip_type;
150     uint16_t size = btstack_min(strlen(connection->bnip_number), sizeof(event) - pos);
151     strncpy((char*)&event[pos], connection->bnip_number, size);
152     pos += size;
153     event[pos++] = 0;
154     (*callback)(HCI_EVENT_PACKET, 0, event, pos);
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, (char *) "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[10+HFP_MAX_INDICATOR_DESC_SIZE+1];
364     int pos = 0;
365     event[pos++] = HCI_EVENT_HFP_META;
366     event[pos++] = sizeof(event) - 2;
367     event[pos++] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
368     event[pos++] = indicator.index;
369     event[pos++] = indicator.status;
370     event[pos++] = indicator.min_range;
371     event[pos++] = indicator.max_range;
372     event[pos++] = indicator.mandatory;
373     event[pos++] = indicator.enabled;
374     event[pos++] = indicator.status_changed;
375     strncpy((char*)&event[pos], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
376     pos += HFP_MAX_INDICATOR_DESC_SIZE;
377     event[pos] = 0;
378     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
379 }
380 
381 static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){
382     if (!callback) return;
383     uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
384     event[0] = HCI_EVENT_HFP_META;
385     event[1] = sizeof(event) - 2;
386     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
387     event[3] = network_operator.mode;
388     event[4] = network_operator.format;
389     strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
390     event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
391     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
392 }
393 
394 static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
395     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
396     if (hfp_connection->ok_pending) return 0;
397     int done = 1;
398 
399     switch (hfp_connection->state){
400         case HFP_EXCHANGE_SUPPORTED_FEATURES:
401             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
402             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
403             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
404             break;
405         case HFP_NOTIFY_ON_CODECS:
406             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
407             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
408             break;
409         case HFP_RETRIEVE_INDICATORS:
410             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
411             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
412             break;
413         case HFP_RETRIEVE_INDICATORS_STATUS:
414             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
415             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
416             break;
417         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
418             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
419             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
420             break;
421         case HFP_RETRIEVE_CAN_HOLD_CALL:
422             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
423             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
424             break;
425         case HFP_LIST_GENERIC_STATUS_INDICATORS:
426             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
427             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
428             break;
429         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
430             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
431             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
432             break;
433         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
434             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
435             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
436             break;
437         default:
438             done = 0;
439             break;
440     }
441     return done;
442 }
443 
444 
445 static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
446     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
447     if (hfp_connection->ok_pending) return 0;
448 
449     int done = 0;
450     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
451         hfp_connection->ok_pending = 1;
452         done = 1;
453         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
454         return done;
455     };
456     if (hfp_connection->change_status_update_for_individual_ag_indicators){
457         hfp_connection->ok_pending = 1;
458         done = 1;
459         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
460                 hfp_connection->ag_indicators_status_update_bitmap,
461                 hfp_connection->ag_indicators_nr);
462         return done;
463     }
464 
465     switch (hfp_connection->hf_query_operator_state){
466         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
467             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
468             hfp_connection->ok_pending = 1;
469             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
470             return 1;
471         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
472             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
473             hfp_connection->ok_pending = 1;
474             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
475             return 1;
476         default:
477             break;
478     }
479 
480     if (hfp_connection->enable_extended_audio_gateway_error_report){
481         hfp_connection->ok_pending = 1;
482         done = 1;
483         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
484         return done;
485     }
486 
487     return done;
488 }
489 
490 static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
491     /* events ( == commands):
492         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
493         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
494             hf_trigger_codec_connection_setup == received BCC
495             ag_trigger_codec_connection_setup == received from AG to send BCS
496         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
497     */
498 
499     if (hfp_connection->ok_pending) return 0;
500 
501     switch (hfp_connection->command){
502         case HFP_CMD_AVAILABLE_CODECS:
503             if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
504 
505             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
506             hfp_connection->ok_pending = 1;
507             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
508             return 1;
509         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
510             hfp_connection->codec_confirmed = 0;
511             hfp_connection->suggested_codec = 0;
512             hfp_connection->negotiated_codec = 0;
513 
514             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
515             hfp_connection->ok_pending = 1;
516             hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
517             break;
518 
519          case HFP_CMD_AG_SUGGESTED_CODEC:{
520             if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
521                 hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
522                 hfp_connection->ok_pending = 1;
523                 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
524                 hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
525                 log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
526                 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
527             } else {
528                 hfp_connection->codec_confirmed = 0;
529                 hfp_connection->suggested_codec = 0;
530                 hfp_connection->negotiated_codec = 0;
531                 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
532                 hfp_connection->ok_pending = 1;
533                 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
534 
535             }
536             break;
537         }
538         default:
539             break;
540     }
541     return 0;
542 }
543 
544 static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
545     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
546         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
547 
548     if (hfp_connection->release_audio_connection){
549         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
550         hfp_connection->release_audio_connection = 0;
551         gap_disconnect(hfp_connection->sco_handle);
552         return 1;
553     }
554 
555     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
556 
557     // run codecs exchange
558     int done = codecs_exchange_state_machine(hfp_connection);
559     if (done) return 1;
560 
561     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
562     if (hfp_connection->establish_audio_connection){
563         hfp_connection->state = HFP_W4_SCO_CONNECTED;
564         hfp_connection->establish_audio_connection = 0;
565         hfp_setup_synchronous_connection(hfp_connection);
566         return 1;
567     }
568 
569     return 0;
570 }
571 
572 
573 static int call_setup_state_machine(hfp_connection_t * hfp_connection){
574     if (hfp_connection->hf_answer_incoming_call){
575         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
576         hfp_connection->hf_answer_incoming_call = 0;
577         return 1;
578     }
579     return 0;
580 }
581 
582 static void hfp_run_for_context(hfp_connection_t * hfp_connection){
583     if (!hfp_connection) return;
584     if (!hfp_connection->rfcomm_cid) return;
585 
586     if (hfp_connection->local_role != HFP_ROLE_HF) {
587         log_info("HFP HF%p, wrong role %u", hfp_connection, hfp_connection->local_role);
588         return;
589     }
590 
591     if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){
592 
593         bool eSCO = hfp_connection->hf_accept_sco == 2;
594         hfp_connection->hf_accept_sco = 0;
595 
596         // notify about codec selection if not done already
597         if (hfp_connection->negotiated_codec == 0){
598             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
599         }
600 
601         // remote supported feature eSCO is set if link type is eSCO
602         // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
603         uint16_t max_latency;
604         uint8_t  retransmission_effort;
605         uint16_t packet_types;
606 
607         if (eSCO && hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
608             max_latency = 0x000c;
609             retransmission_effort = 0x02;
610             // eSCO: EV3 and 2-EV3
611             packet_types = 0x0048;
612         } else {
613             max_latency = 0xffff;
614             retransmission_effort = 0xff;
615             // sco: HV1 and HV3
616             packet_types = 0x005;
617         }
618 
619         // mSBC only allows for transparent data
620         uint16_t sco_voice_setting = hci_get_sco_voice_setting();
621         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
622             sco_voice_setting = 0x0043; // Transparent data
623         }
624 
625         // filter packet types
626         packet_types &= hfp_get_sco_packet_types();
627 
628         // bits 6-9 are 'don't allow'
629         packet_types ^= 0x3c0;
630 
631         log_info("HFP: sending hci_accept_connection_request, packet types 0x%04x, sco_voice_setting 0x%02x", packet_types, sco_voice_setting);
632         hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
633                         sco_voice_setting, retransmission_effort, packet_types);
634         return;
635     }
636 
637     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
638         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
639         return;
640     }
641     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
642     if (!done){
643         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
644     }
645     if (!done){
646         done = hfp_hf_run_for_audio_connection(hfp_connection);
647     }
648     if (!done){
649         done = call_setup_state_machine(hfp_connection);
650     }
651 
652     // don't send a new command while ok still pending
653     if (hfp_connection->ok_pending) return;
654 
655     if (hfp_connection->send_microphone_gain){
656         hfp_connection->send_microphone_gain = 0;
657         hfp_connection->ok_pending = 1;
658         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
659         return;
660     }
661 
662     if (hfp_connection->send_speaker_gain){
663         hfp_connection->send_speaker_gain = 0;
664         hfp_connection->ok_pending = 1;
665         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
666         return;
667     }
668 
669     if (hfp_connection->hf_deactivate_calling_line_notification){
670         hfp_connection->hf_deactivate_calling_line_notification = 0;
671         hfp_connection->ok_pending = 1;
672         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
673         return;
674     }
675 
676     if (hfp_connection->hf_activate_calling_line_notification){
677         hfp_connection->hf_activate_calling_line_notification = 0;
678         hfp_connection->ok_pending = 1;
679         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
680         return;
681     }
682 
683     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
684         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
685         hfp_connection->ok_pending = 1;
686         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
687         return;
688     }
689 
690     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
691         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
692         hfp_connection->ok_pending = 1;
693         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
694         return;
695     }
696 
697     if (hfp_connection->hf_deactivate_voice_recognition_notification){
698         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
699         hfp_connection->ok_pending = 1;
700         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
701         return;
702     }
703 
704     if (hfp_connection->hf_activate_voice_recognition_notification){
705         hfp_connection->hf_activate_voice_recognition_notification = 0;
706         hfp_connection->ok_pending = 1;
707         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
708         return;
709     }
710 
711 
712     if (hfp_connection->hf_deactivate_call_waiting_notification){
713         hfp_connection->hf_deactivate_call_waiting_notification = 0;
714         hfp_connection->ok_pending = 1;
715         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
716         return;
717     }
718 
719     if (hfp_connection->hf_activate_call_waiting_notification){
720         hfp_connection->hf_activate_call_waiting_notification = 0;
721         hfp_connection->ok_pending = 1;
722         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
723         return;
724     }
725 
726     if (hfp_connection->hf_initiate_outgoing_call){
727         hfp_connection->hf_initiate_outgoing_call = 0;
728         hfp_connection->ok_pending = 1;
729         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
730         return;
731     }
732 
733     if (hfp_connection->hf_initiate_memory_dialing){
734         hfp_connection->hf_initiate_memory_dialing = 0;
735         hfp_connection->ok_pending = 1;
736         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
737         return;
738     }
739 
740     if (hfp_connection->hf_initiate_redial_last_number){
741         hfp_connection->hf_initiate_redial_last_number = 0;
742         hfp_connection->ok_pending = 1;
743         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
744         return;
745     }
746 
747     if (hfp_connection->hf_send_chup){
748         hfp_connection->hf_send_chup = 0;
749         hfp_connection->ok_pending = 1;
750         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
751         return;
752     }
753 
754     if (hfp_connection->hf_send_chld_0){
755         hfp_connection->hf_send_chld_0 = 0;
756         hfp_connection->ok_pending = 1;
757         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
758         return;
759     }
760 
761     if (hfp_connection->hf_send_chld_1){
762         hfp_connection->hf_send_chld_1 = 0;
763         hfp_connection->ok_pending = 1;
764         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
765         return;
766     }
767 
768     if (hfp_connection->hf_send_chld_2){
769         hfp_connection->hf_send_chld_2 = 0;
770         hfp_connection->ok_pending = 1;
771         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
772         return;
773     }
774 
775     if (hfp_connection->hf_send_chld_3){
776         hfp_connection->hf_send_chld_3 = 0;
777         hfp_connection->ok_pending = 1;
778         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
779         return;
780     }
781 
782     if (hfp_connection->hf_send_chld_4){
783         hfp_connection->hf_send_chld_4 = 0;
784         hfp_connection->ok_pending = 1;
785         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
786         return;
787     }
788 
789     if (hfp_connection->hf_send_chld_x){
790         hfp_connection->hf_send_chld_x = 0;
791         hfp_connection->ok_pending = 1;
792         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
793         return;
794     }
795 
796     if (hfp_connection->hf_send_dtmf_code){
797         char code = hfp_connection->hf_send_dtmf_code;
798         hfp_connection->hf_send_dtmf_code = 0;
799         hfp_connection->ok_pending = 1;
800         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
801         return;
802     }
803 
804     if (hfp_connection->hf_send_binp){
805         hfp_connection->hf_send_binp = 0;
806         hfp_connection->ok_pending = 1;
807         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
808         return;
809     }
810 
811     if (hfp_connection->hf_send_clcc){
812         hfp_connection->hf_send_clcc = 0;
813         hfp_connection->ok_pending = 1;
814         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
815         return;
816     }
817 
818     if (hfp_connection->hf_send_rrh){
819         hfp_connection->hf_send_rrh = 0;
820         char buffer[20];
821         switch (hfp_connection->hf_send_rrh_command){
822             case '?':
823                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
824                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
825                 return;
826             case '0':
827             case '1':
828             case '2':
829                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
830                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
831                 return;
832             default:
833                 break;
834         }
835         return;
836     }
837 
838     if (hfp_connection->hf_send_cnum){
839         hfp_connection->hf_send_cnum = 0;
840         char buffer[20];
841         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
842         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
843         return;
844     }
845 
846     // update HF indicators
847     if (hfp_connection->generic_status_update_bitmap){
848         int i;
849         for (i=0;i<hfp_indicators_nr;i++){
850             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
851                 if (hfp_connection->generic_status_indicators[i].state){
852                     hfp_connection->ok_pending = 1;
853                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
854                     char buffer[30];
855                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]);
856                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
857                 } else {
858                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
859                 }
860                 return;
861             }
862         }
863     }
864 
865     if (done) return;
866     // deal with disconnect
867     switch (hfp_connection->state){
868         case HFP_W2_DISCONNECT_RFCOMM:
869             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
870             rfcomm_disconnect(hfp_connection->rfcomm_cid);
871             break;
872 
873         default:
874             break;
875     }
876 }
877 
878 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
879     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
880 
881     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
882 
883     // restore volume settings
884     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
885     hfp_connection->send_speaker_gain = 1;
886     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
887     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
888     hfp_connection->send_microphone_gain = 1;
889     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
890     // enable all indicators
891     int i;
892     for (i=0;i<hfp_indicators_nr;i++){
893         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
894         hfp_connection->generic_status_indicators[i].state = 1;
895     }
896 }
897 
898 static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
899     hfp_connection->ok_pending = 0;
900     switch (hfp_connection->state){
901         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
902             if (has_codec_negotiation_feature(hfp_connection)){
903                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
904                 break;
905             }
906             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
907             break;
908 
909         case HFP_W4_NOTIFY_ON_CODECS:
910             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
911             break;
912 
913         case HFP_W4_RETRIEVE_INDICATORS:
914             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
915             break;
916 
917         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
918             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
919             break;
920 
921         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
922             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
923                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
924                 break;
925             }
926             if (has_hf_indicators_feature(hfp_connection)){
927                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
928                 break;
929             }
930             hfp_ag_slc_established(hfp_connection);
931             break;
932 
933         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
934             if (has_hf_indicators_feature(hfp_connection)){
935                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
936                 break;
937             }
938             hfp_ag_slc_established(hfp_connection);
939             break;
940 
941         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
942             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
943             break;
944 
945         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
946             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
947             break;
948 
949         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
950             hfp_ag_slc_established(hfp_connection);
951             break;
952         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
953             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
954                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
955                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
956                 break;
957             }
958 
959             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
960                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
961                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
962                 break;
963             }
964 
965             switch (hfp_connection->hf_query_operator_state){
966                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
967                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
968                     break;
969                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
970                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
971                     hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator);
972                     break;
973                 default:
974                     break;
975             }
976 
977             if (hfp_connection->enable_extended_audio_gateway_error_report){
978                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
979                 break;
980             }
981 
982             switch (hfp_connection->codecs_state){
983                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
984                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
985                     break;
986                 case HFP_CODECS_HF_CONFIRMED_CODEC:
987                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
988                     break;
989                 default:
990                     break;
991             }
992             break;
993         default:
994             break;
995     }
996 
997     // done
998     hfp_connection->command = HFP_CMD_NONE;
999 }
1000 
1001 static int hfp_parser_is_end_of_line(uint8_t byte){
1002     return (byte == '\n') || (byte == '\r');
1003 }
1004 
1005 static void hfp_hf_handle_transfer_ag_indicator_status(hfp_connection_t * hfp_connection) {
1006     uint16_t i;
1007     for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1008         if (hfp_connection->ag_indicators[i].status_changed) {
1009             if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1010                 hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1011             } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1012                 hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1013                 // avoid set but not used warning
1014                 (void) hfp_callheld_status;
1015             } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1016                 hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1017             }
1018             hfp_connection->ag_indicators[i].status_changed = 0;
1019             hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]);
1020             break;
1021         }
1022     }
1023 }
1024 
1025 static void hfp_hf_handle_rfcomm_command(hfp_connection_t * hfp_connection){
1026     int value;
1027     int i;
1028     switch (hfp_connection->command){
1029         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1030             hfp_connection->command = HFP_CMD_NONE;
1031             hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
1032             break;
1033         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1034             hfp_connection->command = HFP_CMD_NONE;
1035             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1036             break;
1037         case HFP_CMD_LIST_CURRENT_CALLS:
1038             hfp_connection->command = HFP_CMD_NONE;
1039             hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection);
1040             break;
1041         case HFP_CMD_SET_SPEAKER_GAIN:
1042             hfp_connection->command = HFP_CMD_NONE;
1043             value = btstack_atoi((char*)hfp_connection->line_buffer);
1044             hfp_hf_speaker_gain = value;
1045             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1046             break;
1047         case HFP_CMD_SET_MICROPHONE_GAIN:
1048             hfp_connection->command = HFP_CMD_NONE;
1049             value = btstack_atoi((char*)hfp_connection->line_buffer);
1050             hfp_hf_microphone_gain = value;
1051             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1052             break;
1053         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1054             hfp_connection->command = HFP_CMD_NONE;
1055             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1056             break;
1057         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1058             hfp_connection->command = HFP_CMD_NONE;
1059             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1060             break;
1061         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1062             hfp_connection->command = HFP_CMD_NONE;
1063             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1064             break;
1065         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1066             hfp_connection->ok_pending = 0;
1067             hfp_connection->command = HFP_CMD_NONE;
1068             hfp_connection->extended_audio_gateway_error = 0;
1069             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1070             break;
1071         case HFP_CMD_ERROR:
1072             hfp_connection->ok_pending = 0;
1073             hfp_reset_context_flags(hfp_connection);
1074             hfp_connection->command = HFP_CMD_NONE;
1075 
1076             switch (hfp_connection->state){
1077                 case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
1078                     switch (hfp_connection->codecs_state){
1079                         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
1080                             hfp_emit_sco_event(hfp_connection, HFP_REMOTE_REJECTS_AUDIO_CONNECTION, 0, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
1081                             return;
1082                         default:
1083                             break;
1084                     }
1085                 default:
1086                     break;
1087             }
1088             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1089             break;
1090         case HFP_CMD_OK:
1091             hfp_hf_switch_on_ok(hfp_connection);
1092             break;
1093         case HFP_CMD_RING:
1094             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1095             break;
1096         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1097             hfp_hf_handle_transfer_ag_indicator_status(hfp_connection);
1098             break;
1099         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1100             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1101                 hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]);
1102             }
1103             hfp_connection->command = HFP_CMD_NONE;
1104             break;
1105         default:
1106             break;
1107     }
1108 }
1109 
1110 static void hfp_hf_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1111     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
1112     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1113     if (size < 1) return;
1114 
1115     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1116     if (!hfp_connection) return;
1117 
1118     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
1119 
1120     // process messages byte-wise
1121     int pos;
1122     for (pos = 0; pos < size; pos++){
1123         hfp_parse(hfp_connection, packet[pos], 1);
1124 
1125         // parse until end of line "\r\n"
1126         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1127 
1128         hfp_hf_handle_rfcomm_command(hfp_connection);
1129     }
1130     hfp_run_for_context(hfp_connection);
1131 }
1132 
1133 static void hfp_run(void){
1134     btstack_linked_list_iterator_t it;
1135     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1136     while (btstack_linked_list_iterator_has_next(&it)){
1137         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1138         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1139         hfp_run_for_context(hfp_connection);
1140     }
1141 }
1142 
1143 static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1144     switch (packet_type){
1145         case RFCOMM_DATA_PACKET:
1146             hfp_hf_handle_rfcomm_data(packet_type, channel, packet, size);
1147             break;
1148         case HCI_EVENT_PACKET:
1149             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1150                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
1151                 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1152                 return;
1153             }
1154             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1155             break;
1156         default:
1157             break;
1158     }
1159     hfp_run();
1160 }
1161 
1162 static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1163     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1164 
1165     // allow for sco established -> ring transition and sco retry
1166     if (packet_type != HCI_EVENT_PACKET) return;
1167     if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return;
1168     hfp_run();
1169 }
1170 
1171 void hfp_hf_init(uint16_t rfcomm_channel_nr){
1172     hfp_init();
1173 
1174     hci_event_callback_registration.callback = &hci_event_packet_handler;
1175     hci_add_event_handler(&hci_event_callback_registration);
1176 
1177     rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
1178 
1179     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
1180     hfp_set_hf_rfcomm_packet_handler(&rfcomm_packet_handler);
1181 
1182     hfp_set_hf_run_for_context(hfp_run_for_context);
1183 
1184     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1185     hfp_codecs_nr = 0;
1186     hfp_indicators_nr = 0;
1187     hfp_hf_speaker_gain = 9;
1188     hfp_hf_microphone_gain = 9;
1189 }
1190 
1191 void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
1192     if (codecs_nr > HFP_MAX_NUM_CODECS){
1193         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
1194         return;
1195     }
1196 
1197     hfp_codecs_nr = codecs_nr;
1198     int i;
1199     for (i=0; i<codecs_nr; i++){
1200         hfp_codecs[i] = codecs[i];
1201     }
1202 }
1203 
1204 void hfp_hf_init_supported_features(uint32_t supported_features){
1205     hfp_supported_features = supported_features;
1206 }
1207 
1208 void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
1209     hfp_indicators_nr = indicators_nr;
1210     int i;
1211     for (i = 0; i < hfp_indicators_nr ; i++){
1212         hfp_indicators[i] = indicators[i];
1213     }
1214 }
1215 
1216 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1217     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
1218 }
1219 
1220 void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
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_release_service_level_connection(hfp_connection);
1227     hfp_run_for_context(hfp_connection);
1228 }
1229 
1230 static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
1231     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1232     if (!hfp_connection) {
1233         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1234         return;
1235     }
1236     hfp_connection->enable_status_update_for_ag_indicators = enable;
1237     hfp_run_for_context(hfp_connection);
1238 }
1239 
1240 void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1241     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1242 }
1243 
1244 void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1245     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1246 }
1247 
1248 // TODO: returned ERROR - wrong format
1249 void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
1250     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1251     if (!hfp_connection) {
1252         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1253         return;
1254     }
1255     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1256     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1257     hfp_run_for_context(hfp_connection);
1258 }
1259 
1260 void hfp_hf_query_operator_selection(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     switch (hfp_connection->hf_query_operator_state){
1267         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1268             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1269             break;
1270         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1271             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1272             break;
1273         default:
1274             break;
1275     }
1276     hfp_run_for_context(hfp_connection);
1277 }
1278 
1279 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
1280     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1281     if (!hfp_connection) {
1282         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1283         return;
1284     }
1285     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1286     hfp_run_for_context(hfp_connection);
1287 }
1288 
1289 
1290 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1291     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1292 }
1293 
1294 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1295     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1296 }
1297 
1298 static uint8_t hfp_hf_esco_s4_supported(hfp_connection_t * hfp_connection){
1299     return (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_HFSF_ESCO_S4));
1300 }
1301 
1302 void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
1303     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1304     if (!hfp_connection) {
1305         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1306         return;
1307     }
1308     hfp_connection->establish_audio_connection = 0;
1309 
1310     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1311     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
1312 
1313     hfp_connection->trigger_codec_exchange = 0;
1314     hfp_connection->establish_audio_connection = 1;
1315     if (!has_codec_negotiation_feature(hfp_connection)){
1316         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1317         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1318         hfp_connection->suggested_codec = HFP_CODEC_CVSD;
1319         hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
1320         hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
1321         hfp_init_link_settings(hfp_connection, hfp_hf_esco_s4_supported(hfp_connection));
1322         hfp_connection->trigger_codec_exchange = 0;
1323         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1324     } else {
1325         switch (hfp_connection->codecs_state){
1326             case HFP_CODECS_W4_AG_COMMON_CODEC:
1327                 break;
1328             default:
1329                 hfp_connection->trigger_codec_exchange = 1;
1330                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1331                 break;
1332         }
1333     }
1334 
1335     hfp_run_for_context(hfp_connection);
1336 }
1337 
1338 void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
1339     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1340     if (!hfp_connection) {
1341         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1342         return;
1343     }
1344     hfp_release_audio_connection(hfp_connection);
1345     hfp_run_for_context(hfp_connection);
1346 }
1347 
1348 void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
1349     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1350     if (!hfp_connection) {
1351         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1352         return;
1353     }
1354 
1355     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1356         hfp_connection->hf_answer_incoming_call = 1;
1357         hfp_run_for_context(hfp_connection);
1358     } else {
1359         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1360     }
1361 }
1362 
1363 void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
1364     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1365     if (!hfp_connection) {
1366         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1367         return;
1368     }
1369     hfp_connection->hf_send_chup = 1;
1370     hfp_run_for_context(hfp_connection);
1371 }
1372 
1373 void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
1374     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1375     if (!hfp_connection) {
1376         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1377         return;
1378     }
1379 
1380     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1381         hfp_connection->hf_send_chup = 1;
1382         hfp_run_for_context(hfp_connection);
1383     }
1384 }
1385 
1386 void hfp_hf_user_busy(hci_con_handle_t acl_handle){
1387     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1388     if (!hfp_connection) {
1389         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1390         return;
1391     }
1392 
1393     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1394         hfp_connection->hf_send_chld_0 = 1;
1395         hfp_run_for_context(hfp_connection);
1396     }
1397 }
1398 
1399 void hfp_hf_end_active_and_accept_other(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_1 = 1;
1409         hfp_run_for_context(hfp_connection);
1410     }
1411 }
1412 
1413 void hfp_hf_swap_calls(hci_con_handle_t acl_handle){
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_2 = 1;
1423         hfp_run_for_context(hfp_connection);
1424     }
1425 }
1426 
1427 void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
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     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1435         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1436         hfp_connection->hf_send_chld_3 = 1;
1437         hfp_run_for_context(hfp_connection);
1438     }
1439 }
1440 
1441 void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
1442     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1443     if (!hfp_connection) {
1444         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1445         return;
1446     }
1447 
1448     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1449         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1450         hfp_connection->hf_send_chld_4 = 1;
1451         hfp_run_for_context(hfp_connection);
1452     }
1453 }
1454 
1455 void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
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     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1463         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1464         hfp_connection->hf_send_chld_x = 1;
1465         hfp_connection->hf_send_chld_x_index = 10 + index;
1466         hfp_run_for_context(hfp_connection);
1467     }
1468 }
1469 
1470 void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
1471     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1472     if (!hfp_connection) {
1473         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1474         return;
1475     }
1476 
1477     if ((hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS) ||
1478         (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
1479         hfp_connection->hf_send_chld_x = 1;
1480         hfp_connection->hf_send_chld_x_index = 20 + index;
1481         hfp_run_for_context(hfp_connection);
1482     }
1483 }
1484 
1485 void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
1486     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1487     if (!hfp_connection) {
1488         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1489         return;
1490     }
1491 
1492     hfp_connection->hf_initiate_outgoing_call = 1;
1493     snprintf(phone_number, sizeof(phone_number), "%s", number);
1494     hfp_run_for_context(hfp_connection);
1495 }
1496 
1497 void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
1498     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1499     if (!hfp_connection) {
1500         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1501         return;
1502     }
1503 
1504     hfp_connection->hf_initiate_memory_dialing = 1;
1505     hfp_connection->memory_id = memory_id;
1506 
1507     hfp_run_for_context(hfp_connection);
1508 }
1509 
1510 void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
1511     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1512     if (!hfp_connection) {
1513         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1514         return;
1515     }
1516 
1517     hfp_connection->hf_initiate_redial_last_number = 1;
1518     hfp_run_for_context(hfp_connection);
1519 }
1520 
1521 void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
1522     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1523     if (!hfp_connection) {
1524         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1525         return;
1526     }
1527 
1528     hfp_connection->hf_activate_call_waiting_notification = 1;
1529     hfp_run_for_context(hfp_connection);
1530 }
1531 
1532 
1533 void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
1534     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1535     if (!hfp_connection) {
1536         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1537         return;
1538     }
1539 
1540     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1541     hfp_run_for_context(hfp_connection);
1542 }
1543 
1544 
1545 void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
1546     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1547     if (!hfp_connection) {
1548         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1549         return;
1550     }
1551 
1552     hfp_connection->hf_activate_calling_line_notification = 1;
1553     hfp_run_for_context(hfp_connection);
1554 }
1555 
1556 void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
1557     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1558     if (!hfp_connection) {
1559         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1560         return;
1561     }
1562 
1563     hfp_connection->hf_deactivate_calling_line_notification = 1;
1564     hfp_run_for_context(hfp_connection);
1565 }
1566 
1567 
1568 void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1569     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1570     if (!hfp_connection) {
1571         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1572         return;
1573     }
1574 
1575     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1576     hfp_run_for_context(hfp_connection);
1577 }
1578 
1579 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1580     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1581     if (!hfp_connection) {
1582         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1583         return;
1584     }
1585 
1586     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1587     hfp_run_for_context(hfp_connection);
1588 }
1589 
1590 void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
1591     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1592     if (!hfp_connection) {
1593         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1594         return;
1595     }
1596 
1597     hfp_connection->hf_activate_voice_recognition_notification = 1;
1598     hfp_run_for_context(hfp_connection);
1599 }
1600 
1601 void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
1602     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1603     if (!hfp_connection) {
1604         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1605         return;
1606     }
1607 
1608     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1609     hfp_run_for_context(hfp_connection);
1610 }
1611 
1612 void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
1613     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1614     if (!hfp_connection) {
1615         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1616         return;
1617     }
1618 
1619     if (hfp_connection->microphone_gain == gain) return;
1620     if ((gain < 0) || (gain > 15)){
1621         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1622         return;
1623     }
1624     hfp_connection->microphone_gain = gain;
1625     hfp_connection->send_microphone_gain = 1;
1626     hfp_run_for_context(hfp_connection);
1627 }
1628 
1629 void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
1630     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1631     if (!hfp_connection) {
1632         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1633         return;
1634     }
1635 
1636     if (hfp_connection->speaker_gain == gain) return;
1637     if ((gain < 0) || (gain > 15)){
1638         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1639         return;
1640     }
1641     hfp_connection->speaker_gain = gain;
1642     hfp_connection->send_speaker_gain = 1;
1643     hfp_run_for_context(hfp_connection);
1644 }
1645 
1646 void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
1647     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1648     if (!hfp_connection) {
1649         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1650         return;
1651     }
1652 
1653     hfp_connection->hf_send_dtmf_code = code;
1654     hfp_run_for_context(hfp_connection);
1655 }
1656 
1657 void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
1658     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1659     if (!hfp_connection) {
1660         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1661         return;
1662     }
1663     hfp_connection->hf_send_binp = 1;
1664     hfp_run_for_context(hfp_connection);
1665 }
1666 
1667 void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
1668     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1669     if (!hfp_connection) {
1670         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1671         return;
1672     }
1673     hfp_connection->hf_send_clcc = 1;
1674     hfp_run_for_context(hfp_connection);
1675 }
1676 
1677 
1678 void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
1679     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1680     if (!hfp_connection) {
1681         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1682         return;
1683     }
1684     hfp_connection->hf_send_rrh = 1;
1685     hfp_connection->hf_send_rrh_command = '?';
1686     hfp_run_for_context(hfp_connection);
1687 }
1688 
1689 void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
1690     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1691     if (!hfp_connection) {
1692         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1693         return;
1694     }
1695     hfp_connection->hf_send_rrh = 1;
1696     hfp_connection->hf_send_rrh_command = '0';
1697     hfp_run_for_context(hfp_connection);
1698 }
1699 
1700 void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
1701     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1702     if (!hfp_connection) {
1703         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1704         return;
1705     }
1706     hfp_connection->hf_send_rrh = 1;
1707     hfp_connection->hf_send_rrh_command = '1';
1708     hfp_run_for_context(hfp_connection);
1709 }
1710 
1711 void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
1712     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1713     if (!hfp_connection) {
1714         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1715         return;
1716     }
1717     hfp_connection->hf_send_rrh = 1;
1718     hfp_connection->hf_send_rrh_command = '2';
1719     hfp_run_for_context(hfp_connection);
1720 }
1721 
1722 void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
1723     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1724     if (!hfp_connection) {
1725         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1726         return;
1727     }
1728     hfp_connection->hf_send_cnum = 1;
1729     hfp_run_for_context(hfp_connection);
1730 }
1731 
1732 void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
1733     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1734     if (!hfp_connection) {
1735         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1736         return;
1737     }
1738     // find index for assigned number
1739     int i;
1740     for (i = 0; i < hfp_indicators_nr ; i++){
1741         if (hfp_indicators[i] == assigned_number){
1742             // set value
1743             hfp_indicators_value[i] = value;
1744             // mark for update
1745             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1746                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1747                 // send update
1748                 hfp_run_for_context(hfp_connection);
1749             }
1750             return;
1751         }
1752     }
1753 }
1754 
1755 int hfp_hf_in_band_ringtone_active(hci_con_handle_t acl_handle){
1756     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1757     if (!hfp_connection) {
1758         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1759         return 0;
1760     }
1761     return get_bit(hfp_connection->remote_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1762 }
1763