xref: /btstack/src/classic/hfp_hf.c (revision df25739fc3ea5a0a90f0f5925e6461d653697d2e)
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 hfp_callback_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(hfp_callback_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(hfp_callback_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)(event, sizeof(event));
109 }
110 
111 static void hfp_hf_emit_type_and_number(hfp_callback_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)(event, sizeof(event));
122 }
123 
124 static void hfp_hf_emit_enhanced_call_status(hfp_callback_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)(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(hfp_callback_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)(event, sizeof(event));
387 }
388 
389 static void hfp_emit_network_operator_event(hfp_callback_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)(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->acl_handle, hfp_connection->link_setting);
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     l2cap_init();
1087 
1088     rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff);
1089 
1090     hfp_set_packet_handler_for_rfcomm_connections(&packet_handler);
1091 
1092     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1093     hfp_codecs_nr = 0;
1094     hfp_indicators_nr = 0;
1095     hfp_hf_speaker_gain = 9;
1096     hfp_hf_microphone_gain = 9;
1097 }
1098 
1099 void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
1100     if (codecs_nr > HFP_MAX_NUM_CODECS){
1101         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
1102         return;
1103     }
1104 
1105     hfp_codecs_nr = codecs_nr;
1106     int i;
1107     for (i=0; i<codecs_nr; i++){
1108         hfp_codecs[i] = codecs[i];
1109     }
1110 
1111     char buffer[30];
1112     int offset = join(buffer, sizeof(buffer), hfp_codecs, hfp_codecs_nr);
1113     buffer[offset] = 0;
1114     btstack_linked_list_iterator_t it;
1115     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1116     while (btstack_linked_list_iterator_has_next(&it)){
1117         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1118         if (! hfp_connection) continue;
1119         hfp_connection->command = HFP_CMD_AVAILABLE_CODECS;
1120         hfp_run_for_context(hfp_connection);
1121     }
1122 }
1123 
1124 void hfp_hf_init_supported_features(uint32_t supported_features){
1125     hfp_supported_features = supported_features;
1126 }
1127 
1128 void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
1129     hfp_indicators_nr = indicators_nr;
1130     int i;
1131     for (i = 0; i < hfp_indicators_nr ; i++){
1132         hfp_indicators[i] = indicators[i];
1133     }
1134 }
1135 
1136 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1137     hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway);
1138 }
1139 
1140 void hfp_hf_release_service_level_connection(bd_addr_t bd_addr){
1141     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1142     hfp_release_service_level_connection(hfp_connection);
1143     hfp_run_for_context(hfp_connection);
1144 }
1145 
1146 static void hfp_hf_set_status_update_for_all_ag_indicators(bd_addr_t bd_addr, uint8_t enable){
1147     hfp_hf_establish_service_level_connection(bd_addr);
1148     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1149     if (!hfp_connection){
1150         log_error("HFP HF: hfp_connection doesn't exist.");
1151         return;
1152     }
1153     hfp_connection->enable_status_update_for_ag_indicators = enable;
1154     hfp_run_for_context(hfp_connection);
1155 }
1156 
1157 void hfp_hf_enable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1158     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 1);
1159 }
1160 
1161 void hfp_hf_disable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1162     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 0);
1163 }
1164 
1165 // TODO: returned ERROR - wrong format
1166 void hfp_hf_set_status_update_for_individual_ag_indicators(bd_addr_t bd_addr, uint32_t indicators_status_bitmap){
1167     hfp_hf_establish_service_level_connection(bd_addr);
1168     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1169     if (!hfp_connection){
1170         log_error("HFP HF: hfp_connection doesn't exist.");
1171         return;
1172     }
1173     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1174     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1175     hfp_run_for_context(hfp_connection);
1176 }
1177 
1178 void hfp_hf_query_operator_selection(bd_addr_t bd_addr){
1179     hfp_hf_establish_service_level_connection(bd_addr);
1180     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1181     if (!hfp_connection){
1182         log_error("HFP HF: hfp_connection doesn't exist.");
1183         return;
1184     }
1185     switch (hfp_connection->hf_query_operator_state){
1186         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1187             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1188             break;
1189         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1190             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1191             break;
1192         default:
1193             break;
1194     }
1195     hfp_run_for_context(hfp_connection);
1196 }
1197 
1198 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, uint8_t enable){
1199     hfp_hf_establish_service_level_connection(bd_addr);
1200     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1201     if (!hfp_connection){
1202         log_error("HFP HF: hfp_connection doesn't exist.");
1203         return;
1204     }
1205     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1206     hfp_run_for_context(hfp_connection);
1207 }
1208 
1209 
1210 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1211     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 1);
1212 }
1213 
1214 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1215     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 0);
1216 }
1217 
1218 
1219 void hfp_hf_establish_audio_connection(bd_addr_t bd_addr){
1220     hfp_hf_establish_service_level_connection(bd_addr);
1221     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1222     hfp_connection->establish_audio_connection = 0;
1223 
1224     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1225     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
1226 
1227     if (!has_codec_negotiation_feature(hfp_connection)){
1228         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1229         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1230         hfp_connection->establish_audio_connection = 1;
1231     } else {
1232         switch (hfp_connection->codecs_state){
1233             case HFP_CODECS_W4_AG_COMMON_CODEC:
1234                 break;
1235             default:
1236                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1237                 break;
1238         }
1239     }
1240 
1241     hfp_run_for_context(hfp_connection);
1242 }
1243 
1244 void hfp_hf_release_audio_connection(bd_addr_t bd_addr){
1245     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1246     hfp_release_audio_connection(hfp_connection);
1247     hfp_run_for_context(hfp_connection);
1248 }
1249 
1250 void hfp_hf_answer_incoming_call(bd_addr_t bd_addr){
1251     hfp_hf_establish_service_level_connection(bd_addr);
1252     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1253 
1254     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1255         hfp_connection->hf_answer_incoming_call = 1;
1256         hfp_run_for_context(hfp_connection);
1257     } else {
1258         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1259     }
1260 }
1261 
1262 void hfp_hf_terminate_call(bd_addr_t bd_addr){
1263     hfp_hf_establish_service_level_connection(bd_addr);
1264     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1265 
1266     hfp_connection->hf_send_chup = 1;
1267     hfp_run_for_context(hfp_connection);
1268 }
1269 
1270 void hfp_hf_reject_incoming_call(bd_addr_t bd_addr){
1271     hfp_hf_establish_service_level_connection(bd_addr);
1272     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1273 
1274     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1275         hfp_connection->hf_send_chup = 1;
1276         hfp_run_for_context(hfp_connection);
1277     }
1278 }
1279 
1280 void hfp_hf_user_busy(bd_addr_t addr){
1281     hfp_hf_establish_service_level_connection(addr);
1282     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1283 
1284     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1285         hfp_connection->hf_send_chld_0 = 1;
1286         hfp_run_for_context(hfp_connection);
1287     }
1288 }
1289 
1290 void hfp_hf_end_active_and_accept_other(bd_addr_t addr){
1291     hfp_hf_establish_service_level_connection(addr);
1292     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1293 
1294     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1295         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1296         hfp_connection->hf_send_chld_1 = 1;
1297         hfp_run_for_context(hfp_connection);
1298     }
1299 }
1300 
1301 void hfp_hf_swap_calls(bd_addr_t addr){
1302     hfp_hf_establish_service_level_connection(addr);
1303     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1304 
1305     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1306         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1307         hfp_connection->hf_send_chld_2 = 1;
1308         hfp_run_for_context(hfp_connection);
1309     }
1310 }
1311 
1312 void hfp_hf_join_held_call(bd_addr_t addr){
1313     hfp_hf_establish_service_level_connection(addr);
1314     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1315 
1316     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1317         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1318         hfp_connection->hf_send_chld_3 = 1;
1319         hfp_run_for_context(hfp_connection);
1320     }
1321 }
1322 
1323 void hfp_hf_connect_calls(bd_addr_t addr){
1324     hfp_hf_establish_service_level_connection(addr);
1325     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1326 
1327     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1328         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1329         hfp_connection->hf_send_chld_4 = 1;
1330         hfp_run_for_context(hfp_connection);
1331     }
1332 }
1333 
1334 void hfp_hf_release_call_with_index(bd_addr_t addr, int index){
1335     hfp_hf_establish_service_level_connection(addr);
1336     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1337 
1338     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1339         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1340         hfp_connection->hf_send_chld_x = 1;
1341         hfp_connection->hf_send_chld_x_index = 10 + index;
1342         hfp_run_for_context(hfp_connection);
1343     }
1344 }
1345 
1346 void hfp_hf_private_consultation_with_call(bd_addr_t addr, int index){
1347     hfp_hf_establish_service_level_connection(addr);
1348     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1349 
1350     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1351         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1352         hfp_connection->hf_send_chld_x = 1;
1353         hfp_connection->hf_send_chld_x_index = 20 + index;
1354         hfp_run_for_context(hfp_connection);
1355     }
1356 }
1357 
1358 void hfp_hf_dial_number(bd_addr_t bd_addr, char * number){
1359     hfp_hf_establish_service_level_connection(bd_addr);
1360     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1361 
1362     hfp_connection->hf_initiate_outgoing_call = 1;
1363     snprintf(phone_number, sizeof(phone_number), "%s", number);
1364     hfp_run_for_context(hfp_connection);
1365 }
1366 
1367 void hfp_hf_dial_memory(bd_addr_t bd_addr, int memory_id){
1368     hfp_hf_establish_service_level_connection(bd_addr);
1369     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1370 
1371     hfp_connection->hf_initiate_memory_dialing = 1;
1372     hfp_connection->memory_id = memory_id;
1373 
1374     hfp_run_for_context(hfp_connection);
1375 }
1376 
1377 void hfp_hf_redial_last_number(bd_addr_t bd_addr){
1378     hfp_hf_establish_service_level_connection(bd_addr);
1379     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1380 
1381     hfp_connection->hf_initiate_redial_last_number = 1;
1382     hfp_run_for_context(hfp_connection);
1383 }
1384 
1385 void hfp_hf_activate_call_waiting_notification(bd_addr_t bd_addr){
1386     hfp_hf_establish_service_level_connection(bd_addr);
1387     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1388 
1389     hfp_connection->hf_activate_call_waiting_notification = 1;
1390     hfp_run_for_context(hfp_connection);
1391 }
1392 
1393 
1394 void hfp_hf_deactivate_call_waiting_notification(bd_addr_t bd_addr){
1395     hfp_hf_establish_service_level_connection(bd_addr);
1396     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1397 
1398     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1399     hfp_run_for_context(hfp_connection);
1400 }
1401 
1402 
1403 void hfp_hf_activate_calling_line_notification(bd_addr_t bd_addr){
1404     hfp_hf_establish_service_level_connection(bd_addr);
1405     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1406 
1407     hfp_connection->hf_activate_calling_line_notification = 1;
1408     hfp_run_for_context(hfp_connection);
1409 }
1410 
1411 void hfp_hf_deactivate_calling_line_notification(bd_addr_t bd_addr){
1412     hfp_hf_establish_service_level_connection(bd_addr);
1413     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1414 
1415     hfp_connection->hf_deactivate_calling_line_notification = 1;
1416     hfp_run_for_context(hfp_connection);
1417 }
1418 
1419 
1420 void hfp_hf_activate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1421     hfp_hf_establish_service_level_connection(bd_addr);
1422     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1423 
1424     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1425     hfp_run_for_context(hfp_connection);
1426 }
1427 
1428 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1429     hfp_hf_establish_service_level_connection(bd_addr);
1430     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1431 
1432     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1433     hfp_run_for_context(hfp_connection);
1434 }
1435 
1436 void hfp_hf_activate_voice_recognition_notification(bd_addr_t bd_addr){
1437     hfp_hf_establish_service_level_connection(bd_addr);
1438     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1439 
1440     hfp_connection->hf_activate_voice_recognition_notification = 1;
1441     hfp_run_for_context(hfp_connection);
1442 }
1443 
1444 void hfp_hf_deactivate_voice_recognition_notification(bd_addr_t bd_addr){
1445     hfp_hf_establish_service_level_connection(bd_addr);
1446     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1447 
1448     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1449     hfp_run_for_context(hfp_connection);
1450 }
1451 
1452 void hfp_hf_set_microphone_gain(bd_addr_t bd_addr, int gain){
1453     hfp_hf_establish_service_level_connection(bd_addr);
1454     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1455     if (hfp_connection->microphone_gain == gain) return;
1456     if (gain < 0 || gain > 15){
1457         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1458         return;
1459     }
1460     hfp_connection->microphone_gain = gain;
1461     hfp_connection->send_microphone_gain = 1;
1462     hfp_run_for_context(hfp_connection);
1463 }
1464 
1465 void hfp_hf_set_speaker_gain(bd_addr_t bd_addr, int gain){
1466     hfp_hf_establish_service_level_connection(bd_addr);
1467     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1468     if (hfp_connection->speaker_gain == gain) return;
1469     if (gain < 0 || gain > 15){
1470         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1471         return;
1472     }
1473     hfp_connection->speaker_gain = gain;
1474     hfp_connection->send_speaker_gain = 1;
1475     hfp_run_for_context(hfp_connection);
1476 }
1477 
1478 void hfp_hf_send_dtmf_code(bd_addr_t addr, char code){
1479     hfp_hf_establish_service_level_connection(addr);
1480     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1481     hfp_connection->hf_send_dtmf_code = code;
1482     hfp_run_for_context(hfp_connection);
1483 }
1484 
1485 void hfp_hf_request_phone_number_for_voice_tag(bd_addr_t addr){
1486     hfp_hf_establish_service_level_connection(addr);
1487     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1488     hfp_connection->hf_send_binp = 1;
1489     hfp_run_for_context(hfp_connection);
1490 }
1491 
1492 void hfp_hf_query_current_call_status(bd_addr_t addr){
1493     hfp_hf_establish_service_level_connection(addr);
1494     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1495     hfp_connection->hf_send_clcc = 1;
1496     hfp_run_for_context(hfp_connection);
1497 }
1498 
1499 
1500 void hfp_hf_rrh_query_status(bd_addr_t addr){
1501     hfp_hf_establish_service_level_connection(addr);
1502     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1503     hfp_connection->hf_send_rrh = 1;
1504     hfp_connection->hf_send_rrh_command = '?';
1505     hfp_run_for_context(hfp_connection);
1506 }
1507 
1508 void hfp_hf_rrh_hold_call(bd_addr_t addr){
1509     hfp_hf_establish_service_level_connection(addr);
1510     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1511     hfp_connection->hf_send_rrh = 1;
1512     hfp_connection->hf_send_rrh_command = '0';
1513     hfp_run_for_context(hfp_connection);
1514 }
1515 
1516 void hfp_hf_rrh_accept_held_call(bd_addr_t addr){
1517     hfp_hf_establish_service_level_connection(addr);
1518     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1519     hfp_connection->hf_send_rrh = 1;
1520     hfp_connection->hf_send_rrh_command = '1';
1521     hfp_run_for_context(hfp_connection);
1522 }
1523 
1524 void hfp_hf_rrh_reject_held_call(bd_addr_t addr){
1525     hfp_hf_establish_service_level_connection(addr);
1526     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1527     hfp_connection->hf_send_rrh = 1;
1528     hfp_connection->hf_send_rrh_command = '2';
1529     hfp_run_for_context(hfp_connection);
1530 }
1531 
1532 void hfp_hf_query_subscriber_number(bd_addr_t addr){
1533     hfp_hf_establish_service_level_connection(addr);
1534     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1535     hfp_connection->hf_send_cnum = 1;
1536     hfp_run_for_context(hfp_connection);
1537 }
1538 
1539 void hfp_hf_set_hf_indicator(bd_addr_t addr, int assigned_number, int value){
1540     hfp_hf_establish_service_level_connection(addr);
1541     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1542     // find index for assigned number
1543     int i;
1544     for (i = 0; i < hfp_indicators_nr ; i++){
1545         if (hfp_indicators[i] == assigned_number){
1546             // set value
1547             hfp_indicators_value[i] = value;
1548             // mark for update
1549             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1550                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1551                 // send update
1552                 hfp_run_for_context(hfp_connection);
1553             }
1554             return;
1555         }
1556     }
1557 }
1558 
1559