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