xref: /btstack/src/classic/hfp_hf.c (revision 2c455dad9a589f886e28f6a5d10bb735305e4f1b)
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 "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_client_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     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(){
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, 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     l2cap_init();
1087 
1088     rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff);
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