xref: /btstack/src/classic/hfp_hf.c (revision ca93b47d3c0a65b91d7a5dc8586f4c82a3b3e1c2)
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     if (hfp_connection->send_microphone_gain){
640         hfp_connection->send_microphone_gain = 0;
641         hfp_connection->ok_pending = 1;
642         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
643         return;
644     }
645 
646     if (hfp_connection->send_speaker_gain){
647         hfp_connection->send_speaker_gain = 0;
648         hfp_connection->ok_pending = 1;
649         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
650         return;
651     }
652 
653     if (hfp_connection->hf_deactivate_calling_line_notification){
654         hfp_connection->hf_deactivate_calling_line_notification = 0;
655         hfp_connection->ok_pending = 1;
656         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
657         return;
658     }
659 
660     if (hfp_connection->hf_activate_calling_line_notification){
661         hfp_connection->hf_activate_calling_line_notification = 0;
662         hfp_connection->ok_pending = 1;
663         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
664         return;
665     }
666 
667     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
668         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
669         hfp_connection->ok_pending = 1;
670         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
671         return;
672     }
673 
674     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
675         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
676         hfp_connection->ok_pending = 1;
677         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
678         return;
679     }
680 
681     if (hfp_connection->hf_deactivate_voice_recognition_notification){
682         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
683         hfp_connection->ok_pending = 1;
684         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
685         return;
686     }
687 
688     if (hfp_connection->hf_activate_voice_recognition_notification){
689         hfp_connection->hf_activate_voice_recognition_notification = 0;
690         hfp_connection->ok_pending = 1;
691         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
692         return;
693     }
694 
695 
696     if (hfp_connection->hf_deactivate_call_waiting_notification){
697         hfp_connection->hf_deactivate_call_waiting_notification = 0;
698         hfp_connection->ok_pending = 1;
699         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
700         return;
701     }
702 
703     if (hfp_connection->hf_activate_call_waiting_notification){
704         hfp_connection->hf_activate_call_waiting_notification = 0;
705         hfp_connection->ok_pending = 1;
706         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
707         return;
708     }
709 
710     if (hfp_connection->hf_initiate_outgoing_call){
711         hfp_connection->hf_initiate_outgoing_call = 0;
712         hfp_connection->ok_pending = 1;
713         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
714         return;
715     }
716 
717     if (hfp_connection->hf_initiate_memory_dialing){
718         hfp_connection->hf_initiate_memory_dialing = 0;
719         hfp_connection->ok_pending = 1;
720         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
721         return;
722     }
723 
724     if (hfp_connection->hf_initiate_redial_last_number){
725         hfp_connection->hf_initiate_redial_last_number = 0;
726         hfp_connection->ok_pending = 1;
727         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
728         return;
729     }
730 
731     if (hfp_connection->hf_send_chup){
732         hfp_connection->hf_send_chup = 0;
733         hfp_connection->ok_pending = 1;
734         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
735         return;
736     }
737 
738     if (hfp_connection->hf_send_chld_0){
739         hfp_connection->hf_send_chld_0 = 0;
740         hfp_connection->ok_pending = 1;
741         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
742         return;
743     }
744 
745     if (hfp_connection->hf_send_chld_1){
746         hfp_connection->hf_send_chld_1 = 0;
747         hfp_connection->ok_pending = 1;
748         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
749         return;
750     }
751 
752     if (hfp_connection->hf_send_chld_2){
753         hfp_connection->hf_send_chld_2 = 0;
754         hfp_connection->ok_pending = 1;
755         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
756         return;
757     }
758 
759     if (hfp_connection->hf_send_chld_3){
760         hfp_connection->hf_send_chld_3 = 0;
761         hfp_connection->ok_pending = 1;
762         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
763         return;
764     }
765 
766     if (hfp_connection->hf_send_chld_4){
767         hfp_connection->hf_send_chld_4 = 0;
768         hfp_connection->ok_pending = 1;
769         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
770         return;
771     }
772 
773     if (hfp_connection->hf_send_chld_x){
774         hfp_connection->hf_send_chld_x = 0;
775         hfp_connection->ok_pending = 1;
776         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
777         return;
778     }
779 
780     if (hfp_connection->hf_send_dtmf_code){
781         char code = hfp_connection->hf_send_dtmf_code;
782         hfp_connection->hf_send_dtmf_code = 0;
783         hfp_connection->ok_pending = 1;
784         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
785         return;
786     }
787 
788     if (hfp_connection->hf_send_binp){
789         hfp_connection->hf_send_binp = 0;
790         hfp_connection->ok_pending = 1;
791         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
792         return;
793     }
794 
795     if (hfp_connection->hf_send_clcc){
796         hfp_connection->hf_send_clcc = 0;
797         hfp_connection->ok_pending = 1;
798         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
799         return;
800     }
801 
802     if (hfp_connection->hf_send_rrh){
803         hfp_connection->hf_send_rrh = 0;
804         char buffer[20];
805         switch (hfp_connection->hf_send_rrh_command){
806             case '?':
807                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
808                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
809                 return;
810             case '0':
811             case '1':
812             case '2':
813                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
814                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
815                 return;
816             default:
817                 break;
818         }
819         return;
820     }
821 
822     if (hfp_connection->hf_send_cnum){
823         hfp_connection->hf_send_cnum = 0;
824         char buffer[20];
825         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
826         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
827         return;
828     }
829 
830     // update HF indicators
831     if (hfp_connection->generic_status_update_bitmap){
832         int i;
833         for (i=0;i<hfp_indicators_nr;i++){
834             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
835                 if (hfp_connection->generic_status_indicators[i].state){
836                     hfp_connection->ok_pending = 1;
837                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
838                     char buffer[30];
839                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]);
840                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
841                 } else {
842                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
843                 }
844                 return;
845             }
846         }
847     }
848 
849     if (done) return;
850     // deal with disconnect
851     switch (hfp_connection->state){
852         case HFP_W2_DISCONNECT_RFCOMM:
853             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
854             rfcomm_disconnect(hfp_connection->rfcomm_cid);
855             break;
856 
857         default:
858             break;
859     }
860 }
861 
862 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
863     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
864 
865     hfp_emit_slc_connection_event(hfp_callback, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
866 
867     // restore volume settings
868     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
869     hfp_connection->send_speaker_gain = 1;
870     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
871     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
872     hfp_connection->send_microphone_gain = 1;
873     hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
874     // enable all indicators
875     int i;
876     for (i=0;i<hfp_indicators_nr;i++){
877         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
878         hfp_connection->generic_status_indicators[i].state = 1;
879     }
880 }
881 
882 static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
883     hfp_connection->ok_pending = 0;
884     switch (hfp_connection->state){
885         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
886             if (has_codec_negotiation_feature(hfp_connection)){
887                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
888                 break;
889             }
890             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
891             break;
892 
893         case HFP_W4_NOTIFY_ON_CODECS:
894             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
895             break;
896 
897         case HFP_W4_RETRIEVE_INDICATORS:
898             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
899             break;
900 
901         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
902             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
903             break;
904 
905         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
906             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
907                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
908                 break;
909             }
910             if (has_hf_indicators_feature(hfp_connection)){
911                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
912                 break;
913             }
914             hfp_ag_slc_established(hfp_connection);
915             break;
916 
917         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
918             if (has_hf_indicators_feature(hfp_connection)){
919                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
920                 break;
921             }
922             hfp_ag_slc_established(hfp_connection);
923             break;
924 
925         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
926             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
927             break;
928 
929         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
930             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
931             break;
932 
933         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
934             hfp_ag_slc_established(hfp_connection);
935             break;
936         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
937             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
938                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
939                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
940                 break;
941             }
942 
943             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
944                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
945                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
946                 break;
947             }
948 
949             switch (hfp_connection->hf_query_operator_state){
950                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
951                     // printf("Format set, querying name\n");
952                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
953                     break;
954                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
955                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
956                     hfp_emit_network_operator_event(hfp_callback, hfp_connection->network_operator);
957                     break;
958                 default:
959                     break;
960             }
961 
962             if (hfp_connection->enable_extended_audio_gateway_error_report){
963                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
964                 break;
965             }
966 
967             switch (hfp_connection->codecs_state){
968                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
969                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
970                     break;
971                 case HFP_CODECS_HF_CONFIRMED_CODEC:
972                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
973                     break;
974                 default:
975                     break;
976             }
977             break;
978         default:
979             break;
980     }
981 
982     // done
983     hfp_connection->command = HFP_CMD_NONE;
984 }
985 
986 
987 static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
988     UNUSED(packet_type);
989 
990     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
991     if (!hfp_connection) return;
992 
993     char last_char = packet[size-1];
994     packet[size-1] = 0;
995     log_info("HFP_RX %s", packet);
996     packet[size-1] = last_char;
997 
998     int pos, i, value;
999     for (pos = 0; pos < size ; pos++){
1000         hfp_parse(hfp_connection, packet[pos], 1);
1001     }
1002 
1003     switch (hfp_connection->command){
1004         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1005             hfp_connection->command = HFP_CMD_NONE;
1006             // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type);
1007             hfp_hf_emit_subscriber_information(hfp_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
1008             break;
1009         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1010             hfp_connection->command = HFP_CMD_NONE;
1011             // printf("Response and Hold status: %s\n", hfp_connection->line_buffer);
1012             hfp_emit_event(hfp_callback, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1013             break;
1014         case HFP_CMD_LIST_CURRENT_CALLS:
1015             hfp_connection->command = HFP_CMD_NONE;
1016             // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
1017             //      hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
1018             //      hfp_connection->bnip_number, hfp_connection->bnip_type);
1019             hfp_hf_emit_enhanced_call_status(hfp_callback, hfp_connection->clcc_idx,
1020                 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
1021                 hfp_connection->bnip_type, hfp_connection->bnip_number);
1022             break;
1023         case HFP_CMD_SET_SPEAKER_GAIN:
1024             hfp_connection->command = HFP_CMD_NONE;
1025             value = btstack_atoi((char*)hfp_connection->line_buffer);
1026             hfp_hf_speaker_gain = value;
1027             hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1028             break;
1029         case HFP_CMD_SET_MICROPHONE_GAIN:
1030             hfp_connection->command = HFP_CMD_NONE;
1031             value = btstack_atoi((char*)hfp_connection->line_buffer);
1032             hfp_hf_microphone_gain = value;
1033             hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1034             break;
1035         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1036             hfp_connection->command = HFP_CMD_NONE;
1037             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1038             break;
1039         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1040             hfp_connection->command = HFP_CMD_NONE;
1041             hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1042             break;
1043         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1044             hfp_connection->command = HFP_CMD_NONE;
1045             hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALLING_LINE_INDETIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1046             break;
1047         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1048             hfp_connection->ok_pending = 0;
1049             hfp_connection->command = HFP_CMD_NONE;
1050             hfp_connection->extended_audio_gateway_error = 0;
1051             hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1052             break;
1053         case HFP_CMD_ERROR:
1054             hfp_connection->ok_pending = 0;
1055             hfp_reset_context_flags(hfp_connection);
1056             hfp_connection->command = HFP_CMD_NONE;
1057             hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1);
1058             break;
1059         case HFP_CMD_OK:
1060             hfp_hf_switch_on_ok(hfp_connection);
1061             break;
1062         case HFP_CMD_RING:
1063             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_RING);
1064             break;
1065         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1066             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1067                 if (hfp_connection->ag_indicators[i].status_changed) {
1068                     if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1069                         hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1070                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1071                         hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1072                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1073                         hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1074                     }
1075                     hfp_connection->ag_indicators[i].status_changed = 0;
1076                     hfp_emit_ag_indicator_event(hfp_callback, hfp_connection->ag_indicators[i]);
1077                     break;
1078                 }
1079             }
1080             break;
1081         default:
1082             break;
1083     }
1084     hfp_run_for_context(hfp_connection);
1085 }
1086 
1087 static void hfp_run(void){
1088     btstack_linked_list_iterator_t it;
1089     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1090     while (btstack_linked_list_iterator_has_next(&it)){
1091         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1092         hfp_run_for_context(hfp_connection);
1093     }
1094 }
1095 
1096 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1097     switch (packet_type){
1098         case RFCOMM_DATA_PACKET:
1099             hfp_handle_rfcomm_event(packet_type, channel, packet, size);
1100             break;
1101         case HCI_EVENT_PACKET:
1102             hfp_handle_hci_event(packet_type, channel, packet, size);
1103             break;
1104         default:
1105             break;
1106     }
1107     hfp_run();
1108 }
1109 
1110 void hfp_hf_init(uint16_t rfcomm_channel_nr){
1111     // register for HCI events
1112     hci_event_callback_registration.callback = &packet_handler;
1113     hci_add_event_handler(&hci_event_callback_registration);
1114 
1115     rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff);
1116 
1117     hfp_set_packet_handler_for_rfcomm_connections(&packet_handler);
1118 
1119     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1120     hfp_codecs_nr = 0;
1121     hfp_indicators_nr = 0;
1122     hfp_hf_speaker_gain = 9;
1123     hfp_hf_microphone_gain = 9;
1124 }
1125 
1126 void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
1127     if (codecs_nr > HFP_MAX_NUM_CODECS){
1128         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
1129         return;
1130     }
1131 
1132     hfp_codecs_nr = codecs_nr;
1133     int i;
1134     for (i=0; i<codecs_nr; i++){
1135         hfp_codecs[i] = codecs[i];
1136     }
1137 }
1138 
1139 void hfp_hf_init_supported_features(uint32_t supported_features){
1140     hfp_supported_features = supported_features;
1141 }
1142 
1143 void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
1144     hfp_indicators_nr = indicators_nr;
1145     int i;
1146     for (i = 0; i < hfp_indicators_nr ; i++){
1147         hfp_indicators[i] = indicators[i];
1148     }
1149 }
1150 
1151 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1152     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY);
1153 }
1154 
1155 void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
1156     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1157     if (!hfp_connection) {
1158         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1159         return;
1160     }
1161     hfp_release_service_level_connection(hfp_connection);
1162     hfp_run_for_context(hfp_connection);
1163 }
1164 
1165 static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
1166     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1167     if (!hfp_connection) {
1168         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1169         return;
1170     }
1171     hfp_connection->enable_status_update_for_ag_indicators = enable;
1172     hfp_run_for_context(hfp_connection);
1173 }
1174 
1175 void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1176     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1177 }
1178 
1179 void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1180     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1181 }
1182 
1183 // TODO: returned ERROR - wrong format
1184 void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
1185     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1186     if (!hfp_connection) {
1187         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1188         return;
1189     }
1190     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1191     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1192     hfp_run_for_context(hfp_connection);
1193 }
1194 
1195 void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
1196     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1197     if (!hfp_connection) {
1198         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1199         return;
1200     }
1201     switch (hfp_connection->hf_query_operator_state){
1202         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1203             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1204             break;
1205         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1206             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1207             break;
1208         default:
1209             break;
1210     }
1211     hfp_run_for_context(hfp_connection);
1212 }
1213 
1214 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
1215     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1216     if (!hfp_connection) {
1217         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1218         return;
1219     }
1220     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1221     hfp_run_for_context(hfp_connection);
1222 }
1223 
1224 
1225 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1226     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1227 }
1228 
1229 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1230     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1231 }
1232 
1233 
1234 void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
1235     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1236     if (!hfp_connection) {
1237         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1238         return;
1239     }
1240     hfp_connection->establish_audio_connection = 0;
1241 
1242     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1243     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
1244 
1245     if (!has_codec_negotiation_feature(hfp_connection)){
1246         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1247         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1248         hfp_connection->establish_audio_connection = 1;
1249     } else {
1250         switch (hfp_connection->codecs_state){
1251             case HFP_CODECS_W4_AG_COMMON_CODEC:
1252                 break;
1253             default:
1254                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1255                 break;
1256         }
1257     }
1258 
1259     hfp_run_for_context(hfp_connection);
1260 }
1261 
1262 void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
1263     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1264     if (!hfp_connection) {
1265         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1266         return;
1267     }
1268     hfp_release_audio_connection(hfp_connection);
1269     hfp_run_for_context(hfp_connection);
1270 }
1271 
1272 void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
1273     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1274     if (!hfp_connection) {
1275         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1276         return;
1277     }
1278 
1279     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1280         hfp_connection->hf_answer_incoming_call = 1;
1281         hfp_run_for_context(hfp_connection);
1282     } else {
1283         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1284     }
1285 }
1286 
1287 void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
1288     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1289     if (!hfp_connection) {
1290         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1291         return;
1292     }
1293     hfp_connection->hf_send_chup = 1;
1294     hfp_run_for_context(hfp_connection);
1295 }
1296 
1297 void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
1298     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1299     if (!hfp_connection) {
1300         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1301         return;
1302     }
1303 
1304     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1305         hfp_connection->hf_send_chup = 1;
1306         hfp_run_for_context(hfp_connection);
1307     }
1308 }
1309 
1310 void hfp_hf_user_busy(hci_con_handle_t acl_handle){
1311     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1312     if (!hfp_connection) {
1313         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1314         return;
1315     }
1316 
1317     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1318         hfp_connection->hf_send_chld_0 = 1;
1319         hfp_run_for_context(hfp_connection);
1320     }
1321 }
1322 
1323 void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
1324     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1325     if (!hfp_connection) {
1326         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1327         return;
1328     }
1329 
1330     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1331         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1332         hfp_connection->hf_send_chld_1 = 1;
1333         hfp_run_for_context(hfp_connection);
1334     }
1335 }
1336 
1337 void hfp_hf_swap_calls(hci_con_handle_t acl_handle){
1338     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1339     if (!hfp_connection) {
1340         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1341         return;
1342     }
1343 
1344     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1345         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1346         hfp_connection->hf_send_chld_2 = 1;
1347         hfp_run_for_context(hfp_connection);
1348     }
1349 }
1350 
1351 void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
1352     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1353     if (!hfp_connection) {
1354         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1355         return;
1356     }
1357 
1358     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1359         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1360         hfp_connection->hf_send_chld_3 = 1;
1361         hfp_run_for_context(hfp_connection);
1362     }
1363 }
1364 
1365 void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
1366     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1367     if (!hfp_connection) {
1368         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1369         return;
1370     }
1371 
1372     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1373         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1374         hfp_connection->hf_send_chld_4 = 1;
1375         hfp_run_for_context(hfp_connection);
1376     }
1377 }
1378 
1379 void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
1380     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1381     if (!hfp_connection) {
1382         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1383         return;
1384     }
1385 
1386     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1387         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1388         hfp_connection->hf_send_chld_x = 1;
1389         hfp_connection->hf_send_chld_x_index = 10 + index;
1390         hfp_run_for_context(hfp_connection);
1391     }
1392 }
1393 
1394 void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
1395     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1396     if (!hfp_connection) {
1397         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1398         return;
1399     }
1400 
1401     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1402         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1403         hfp_connection->hf_send_chld_x = 1;
1404         hfp_connection->hf_send_chld_x_index = 20 + index;
1405         hfp_run_for_context(hfp_connection);
1406     }
1407 }
1408 
1409 void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
1410     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1411     if (!hfp_connection) {
1412         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1413         return;
1414     }
1415 
1416     hfp_connection->hf_initiate_outgoing_call = 1;
1417     snprintf(phone_number, sizeof(phone_number), "%s", number);
1418     hfp_run_for_context(hfp_connection);
1419 }
1420 
1421 void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
1422     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1423     if (!hfp_connection) {
1424         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1425         return;
1426     }
1427 
1428     hfp_connection->hf_initiate_memory_dialing = 1;
1429     hfp_connection->memory_id = memory_id;
1430 
1431     hfp_run_for_context(hfp_connection);
1432 }
1433 
1434 void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
1435     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1436     if (!hfp_connection) {
1437         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1438         return;
1439     }
1440 
1441     hfp_connection->hf_initiate_redial_last_number = 1;
1442     hfp_run_for_context(hfp_connection);
1443 }
1444 
1445 void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
1446     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1447     if (!hfp_connection) {
1448         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1449         return;
1450     }
1451 
1452     hfp_connection->hf_activate_call_waiting_notification = 1;
1453     hfp_run_for_context(hfp_connection);
1454 }
1455 
1456 
1457 void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
1458     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1459     if (!hfp_connection) {
1460         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1461         return;
1462     }
1463 
1464     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1465     hfp_run_for_context(hfp_connection);
1466 }
1467 
1468 
1469 void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
1470     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1471     if (!hfp_connection) {
1472         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1473         return;
1474     }
1475 
1476     hfp_connection->hf_activate_calling_line_notification = 1;
1477     hfp_run_for_context(hfp_connection);
1478 }
1479 
1480 void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
1481     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1482     if (!hfp_connection) {
1483         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1484         return;
1485     }
1486 
1487     hfp_connection->hf_deactivate_calling_line_notification = 1;
1488     hfp_run_for_context(hfp_connection);
1489 }
1490 
1491 
1492 void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1493     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1494     if (!hfp_connection) {
1495         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1496         return;
1497     }
1498 
1499     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1500     hfp_run_for_context(hfp_connection);
1501 }
1502 
1503 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
1504     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1505     if (!hfp_connection) {
1506         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1507         return;
1508     }
1509 
1510     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1511     hfp_run_for_context(hfp_connection);
1512 }
1513 
1514 void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
1515     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1516     if (!hfp_connection) {
1517         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1518         return;
1519     }
1520 
1521     hfp_connection->hf_activate_voice_recognition_notification = 1;
1522     hfp_run_for_context(hfp_connection);
1523 }
1524 
1525 void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
1526     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1527     if (!hfp_connection) {
1528         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1529         return;
1530     }
1531 
1532     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1533     hfp_run_for_context(hfp_connection);
1534 }
1535 
1536 void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
1537     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1538     if (!hfp_connection) {
1539         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1540         return;
1541     }
1542 
1543     if (hfp_connection->microphone_gain == gain) return;
1544     if (gain < 0 || gain > 15){
1545         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1546         return;
1547     }
1548     hfp_connection->microphone_gain = gain;
1549     hfp_connection->send_microphone_gain = 1;
1550     hfp_run_for_context(hfp_connection);
1551 }
1552 
1553 void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
1554     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1555     if (!hfp_connection) {
1556         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1557         return;
1558     }
1559 
1560     if (hfp_connection->speaker_gain == gain) return;
1561     if (gain < 0 || gain > 15){
1562         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1563         return;
1564     }
1565     hfp_connection->speaker_gain = gain;
1566     hfp_connection->send_speaker_gain = 1;
1567     hfp_run_for_context(hfp_connection);
1568 }
1569 
1570 void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
1571     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1572     if (!hfp_connection) {
1573         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1574         return;
1575     }
1576 
1577     hfp_connection->hf_send_dtmf_code = code;
1578     hfp_run_for_context(hfp_connection);
1579 }
1580 
1581 void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
1582     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1583     if (!hfp_connection) {
1584         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1585         return;
1586     }
1587     hfp_connection->hf_send_binp = 1;
1588     hfp_run_for_context(hfp_connection);
1589 }
1590 
1591 void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
1592     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1593     if (!hfp_connection) {
1594         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1595         return;
1596     }
1597     hfp_connection->hf_send_clcc = 1;
1598     hfp_run_for_context(hfp_connection);
1599 }
1600 
1601 
1602 void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
1603     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1604     if (!hfp_connection) {
1605         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1606         return;
1607     }
1608     hfp_connection->hf_send_rrh = 1;
1609     hfp_connection->hf_send_rrh_command = '?';
1610     hfp_run_for_context(hfp_connection);
1611 }
1612 
1613 void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
1614     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1615     if (!hfp_connection) {
1616         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1617         return;
1618     }
1619     hfp_connection->hf_send_rrh = 1;
1620     hfp_connection->hf_send_rrh_command = '0';
1621     hfp_run_for_context(hfp_connection);
1622 }
1623 
1624 void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
1625     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1626     if (!hfp_connection) {
1627         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1628         return;
1629     }
1630     hfp_connection->hf_send_rrh = 1;
1631     hfp_connection->hf_send_rrh_command = '1';
1632     hfp_run_for_context(hfp_connection);
1633 }
1634 
1635 void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
1636     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1637     if (!hfp_connection) {
1638         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1639         return;
1640     }
1641     hfp_connection->hf_send_rrh = 1;
1642     hfp_connection->hf_send_rrh_command = '2';
1643     hfp_run_for_context(hfp_connection);
1644 }
1645 
1646 void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
1647     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1648     if (!hfp_connection) {
1649         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1650         return;
1651     }
1652     hfp_connection->hf_send_cnum = 1;
1653     hfp_run_for_context(hfp_connection);
1654 }
1655 
1656 void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
1657     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
1658     if (!hfp_connection) {
1659         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1660         return;
1661     }
1662     // find index for assigned number
1663     int i;
1664     for (i = 0; i < hfp_indicators_nr ; i++){
1665         if (hfp_indicators[i] == assigned_number){
1666             // set value
1667             hfp_indicators_value[i] = value;
1668             // mark for update
1669             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1670                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1671                 // send update
1672                 hfp_run_for_context(hfp_connection);
1673             }
1674             return;
1675         }
1676     }
1677 }
1678 
1679