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