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