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