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