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