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