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