xref: /btstack/src/classic/hfp_hf.c (revision a484130c09e8d2a02a11deb14ecadc15f0217953)
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     char last_char = packet[size-1];
923     packet[size-1] = 0;
924     log_info("HFP_RX %s", packet);
925     packet[size-1] = last_char;
926 
927     int pos, i, value;
928     for (pos = 0; pos < size ; pos++){
929         hfp_parse(context, packet[pos], 1);
930     }
931 
932     switch (context->command){
933         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
934             context->command = HFP_CMD_NONE;
935             printf("Subscriber Number: number %s, type %u\n", context->bnip_number, context->bnip_type);
936             break;
937         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
938             context->command = HFP_CMD_NONE;
939             printf("Response and Hold status: %s\n", context->line_buffer);
940             break;
941         case HFP_CMD_LIST_CURRENT_CALLS:
942             context->command = HFP_CMD_NONE;
943             printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
944                 context->clcc_idx, context->clcc_dir, context->clcc_status, context->clcc_mpty,
945                 context->bnip_number, context->bnip_type);
946             break;
947         case HFP_CMD_SET_SPEAKER_GAIN:
948             context->command = HFP_CMD_NONE;
949             value = atoi((char*)context->line_buffer);
950             hfp_hf_speaker_gain = value;
951             hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value);
952             break;
953         case HFP_CMD_SET_MICROPHONE_GAIN:
954             context->command = HFP_CMD_NONE;
955             value = atoi((char*)context->line_buffer);
956             hfp_hf_microphone_gain = value;
957             hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
958             break;
959         case HFP_CMD_AG_SENT_PHONE_NUMBER:
960             context->command = HFP_CMD_NONE;
961             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, context->bnip_number);
962             break;
963         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
964             context->ok_pending = 0;
965             context->extended_audio_gateway_error = 0;
966             context->command = HFP_CMD_NONE;
967             hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, context->extended_audio_gateway_error);
968             break;
969         case HFP_CMD_ERROR:
970             context->ok_pending = 0;
971             hfp_reset_context_flags(context);
972             context->command = HFP_CMD_NONE;
973             hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1);
974             break;
975         case HFP_CMD_OK:
976             hfp_hf_switch_on_ok(context);
977             break;
978         case HFP_CMD_RING:
979             hfp_emit_event(hfp_callback, HFP_SUBEVENT_RING, 0);
980             break;
981         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
982             for (i = 0; i < context->ag_indicators_nr; i++){
983                 if (context->ag_indicators[i].status_changed) {
984                     if (strcmp(context->ag_indicators[i].name, "callsetup") == 0){
985                         hfp_callsetup_status = (hfp_callsetup_status_t) context->ag_indicators[i].status;
986                     } else if (strcmp(context->ag_indicators[i].name, "callheld") == 0){
987                         hfp_callheld_status = (hfp_callheld_status_t) context->ag_indicators[i].status;
988                     } else if (strcmp(context->ag_indicators[i].name, "call") == 0){
989                         hfp_call_status = (hfp_call_status_t) context->ag_indicators[i].status;
990                     }
991                     context->ag_indicators[i].status_changed = 0;
992                     hfp_emit_ag_indicator_event(hfp_callback, 0, context->ag_indicators[i]);
993                     break;
994                 }
995             }
996             break;
997         default:
998             break;
999     }
1000     hfp_run_for_context(context);
1001 }
1002 
1003 static void hfp_run(){
1004     linked_list_iterator_t it;
1005     linked_list_iterator_init(&it, hfp_get_connections());
1006     while (linked_list_iterator_has_next(&it)){
1007         hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it);
1008         hfp_run_for_context(connection);
1009     }
1010 }
1011 
1012 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1013     switch (packet_type){
1014         case RFCOMM_DATA_PACKET:
1015             hfp_handle_rfcomm_event(packet_type, channel, packet, size);
1016             break;
1017         case HCI_EVENT_PACKET:
1018             hfp_handle_hci_event(hfp_callback, packet_type, packet, size);
1019         default:
1020             break;
1021     }
1022     hfp_run();
1023 }
1024 
1025 void hfp_hf_set_codecs(uint8_t * codecs, int codecs_nr){
1026     if (codecs_nr > HFP_MAX_NUM_CODECS){
1027         log_error("hfp_hf_set_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
1028         return;
1029     }
1030 
1031     hfp_codecs_nr = codecs_nr;
1032     int i;
1033     for (i=0; i<codecs_nr; i++){
1034         hfp_codecs[i] = codecs[i];
1035     }
1036 
1037     char buffer[30];
1038     int offset = join(buffer, sizeof(buffer), hfp_codecs, hfp_codecs_nr);
1039     buffer[offset] = 0;
1040     linked_list_iterator_t it;
1041     linked_list_iterator_init(&it, hfp_get_connections());
1042     while (linked_list_iterator_has_next(&it)){
1043         hfp_connection_t * connection = (hfp_connection_t *)linked_list_iterator_next(&it);
1044         if (!connection) continue;
1045         connection->command = HFP_CMD_AVAILABLE_CODECS;
1046         hfp_run_for_context(connection);
1047     }
1048 }
1049 
1050 void hfp_hf_init(uint16_t rfcomm_channel_nr, uint32_t supported_features, uint16_t * indicators, int indicators_nr, uint32_t indicators_status){
1051     l2cap_init();
1052     l2cap_register_packet_handler(packet_handler);
1053     rfcomm_register_packet_handler(packet_handler);
1054     hfp_init(rfcomm_channel_nr);
1055 
1056     hfp_supported_features = supported_features;
1057 
1058     hfp_indicators_nr = indicators_nr;
1059     hfp_indicators_status = indicators_status;
1060     int i;
1061     for (i=0; i<indicators_nr; i++){
1062         hfp_indicators[i] = indicators[i];
1063     }
1064 }
1065 
1066 void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1067     hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway);
1068 }
1069 
1070 void hfp_hf_release_service_level_connection(bd_addr_t bd_addr){
1071     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1072     hfp_release_service_level_connection(connection);
1073     hfp_run_for_context(connection);
1074 }
1075 
1076 static void hfp_hf_set_status_update_for_all_ag_indicators(bd_addr_t bd_addr, uint8_t enable){
1077     hfp_hf_establish_service_level_connection(bd_addr);
1078     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1079     if (!connection){
1080         log_error("HFP HF: connection doesn't exist.");
1081         return;
1082     }
1083     connection->enable_status_update_for_ag_indicators = enable;
1084     hfp_run_for_context(connection);
1085 }
1086 
1087 void hfp_hf_enable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1088     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 1);
1089 }
1090 
1091 void hfp_hf_disable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1092     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 0);
1093 }
1094 
1095 // TODO: returned ERROR - wrong format
1096 void hfp_hf_set_status_update_for_individual_ag_indicators(bd_addr_t bd_addr, uint32_t indicators_status_bitmap){
1097     hfp_hf_establish_service_level_connection(bd_addr);
1098     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1099     if (!connection){
1100         log_error("HFP HF: connection doesn't exist.");
1101         return;
1102     }
1103     connection->change_status_update_for_individual_ag_indicators = 1;
1104     connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1105     hfp_run_for_context(connection);
1106 }
1107 
1108 void hfp_hf_query_operator_selection(bd_addr_t bd_addr){
1109     hfp_hf_establish_service_level_connection(bd_addr);
1110     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1111     if (!connection){
1112         log_error("HFP HF: connection doesn't exist.");
1113         return;
1114     }
1115     switch (connection->hf_query_operator_state){
1116         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1117             connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1118             break;
1119         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1120             connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1121             break;
1122         default:
1123             break;
1124     }
1125     hfp_run_for_context(connection);
1126 }
1127 
1128 static void hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, uint8_t enable){
1129     hfp_hf_establish_service_level_connection(bd_addr);
1130     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1131     if (!connection){
1132         log_error("HFP HF: connection doesn't exist.");
1133         return;
1134     }
1135     connection->enable_extended_audio_gateway_error_report = enable;
1136     hfp_run_for_context(connection);
1137 }
1138 
1139 
1140 void hfp_hf_enable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1141     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 1);
1142 }
1143 
1144 void hfp_hf_disable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1145     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 0);
1146 }
1147 
1148 
1149 void hfp_hf_establish_audio_connection(bd_addr_t bd_addr){
1150     hfp_hf_establish_service_level_connection(bd_addr);
1151     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1152     connection->establish_audio_connection = 0;
1153 
1154     if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1155     if (connection->state >= HFP_W2_DISCONNECT_SCO) return;
1156 
1157     if (!has_codec_negotiation_feature(connection)){
1158         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1159         connection->codecs_state = HFP_CODECS_EXCHANGED;
1160         connection->establish_audio_connection = 1;
1161     } else {
1162         switch (connection->codecs_state){
1163             case HFP_CODECS_W4_AG_COMMON_CODEC:
1164                 break;
1165             default:
1166                 connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1167                 break;
1168         }
1169     }
1170 
1171     hfp_run_for_context(connection);
1172 }
1173 
1174 void hfp_hf_release_audio_connection(bd_addr_t bd_addr){
1175     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1176     hfp_release_audio_connection(connection);
1177     hfp_run_for_context(connection);
1178 }
1179 
1180 void hfp_hf_answer_incoming_call(bd_addr_t bd_addr){
1181     hfp_hf_establish_service_level_connection(bd_addr);
1182     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1183 
1184     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1185         connection->hf_answer_incoming_call = 1;
1186         hfp_run_for_context(connection);
1187     } else {
1188         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1189     }
1190 }
1191 
1192 void hfp_hf_terminate_call(bd_addr_t bd_addr){
1193     hfp_hf_establish_service_level_connection(bd_addr);
1194     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1195 
1196     // if (hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1197     connection->hf_send_chup = 1;
1198     hfp_run_for_context(connection);
1199     // } else {
1200     //     log_error("HFP HF: terminating incoming call with wrong call status %u", hfp_call_status);
1201     // }
1202 }
1203 
1204 void hfp_hf_reject_call(bd_addr_t bd_addr){
1205     hfp_hf_establish_service_level_connection(bd_addr);
1206     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1207 
1208     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1209         connection->hf_send_chup = 1;
1210         hfp_run_for_context(connection);
1211     }
1212 }
1213 
1214 void hfp_hf_user_busy(bd_addr_t addr){
1215     hfp_hf_establish_service_level_connection(addr);
1216     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1217 
1218     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1219         connection->hf_send_chld_0 = 1;
1220         hfp_run_for_context(connection);
1221     }
1222 }
1223 
1224 void hfp_hf_end_active_and_accept_other(bd_addr_t addr){
1225     hfp_hf_establish_service_level_connection(addr);
1226     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1227 
1228     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1229         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1230         connection->hf_send_chld_1 = 1;
1231         hfp_run_for_context(connection);
1232     }
1233 }
1234 
1235 void hfp_hf_swap_calls(bd_addr_t addr){
1236     hfp_hf_establish_service_level_connection(addr);
1237     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1238 
1239     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1240         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1241         connection->hf_send_chld_2 = 1;
1242         hfp_run_for_context(connection);
1243     }
1244 }
1245 
1246 void hfp_hf_join_held_call(bd_addr_t addr){
1247     hfp_hf_establish_service_level_connection(addr);
1248     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1249 
1250     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1251         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1252         connection->hf_send_chld_3 = 1;
1253         hfp_run_for_context(connection);
1254     }
1255 }
1256 
1257 void hfp_hf_connect_calls(bd_addr_t addr){
1258     hfp_hf_establish_service_level_connection(addr);
1259     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1260 
1261     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1262         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1263         connection->hf_send_chld_4 = 1;
1264         hfp_run_for_context(connection);
1265     }
1266 }
1267 
1268 void hfp_hf_release_call_with_index(bd_addr_t addr, int index){
1269     hfp_hf_establish_service_level_connection(addr);
1270     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1271 
1272     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1273         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1274         connection->hf_send_chld_x = 1;
1275         connection->hf_send_chld_x_index = 10 + index;
1276         hfp_run_for_context(connection);
1277     }
1278 }
1279 
1280 void hfp_hf_private_consultation_with_call(bd_addr_t addr, int index){
1281     hfp_hf_establish_service_level_connection(addr);
1282     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1283 
1284     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1285         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1286         connection->hf_send_chld_x = 1;
1287         connection->hf_send_chld_x_index = 20 + index;
1288         hfp_run_for_context(connection);
1289     }
1290 }
1291 
1292 void hfp_hf_dial_number(bd_addr_t bd_addr, char * number){
1293     hfp_hf_establish_service_level_connection(bd_addr);
1294     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1295 
1296     connection->hf_initiate_outgoing_call = 1;
1297     snprintf(phone_number, sizeof(phone_number), "%s", number);
1298     hfp_run_for_context(connection);
1299 }
1300 
1301 void hfp_hf_dial_memory(bd_addr_t bd_addr, char * number){
1302     hfp_hf_establish_service_level_connection(bd_addr);
1303     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1304 
1305     connection->hf_initiate_memory_dialing = 1;
1306     snprintf(phone_number, sizeof(phone_number), "%s", number);
1307     hfp_run_for_context(connection);
1308 }
1309 
1310 void hfp_hf_redial_last_number(bd_addr_t bd_addr){
1311     hfp_hf_establish_service_level_connection(bd_addr);
1312     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1313 
1314     connection->hf_initiate_redial_last_number = 1;
1315     hfp_run_for_context(connection);
1316 }
1317 
1318 void hfp_hf_activate_call_waiting_notification(bd_addr_t bd_addr){
1319     hfp_hf_establish_service_level_connection(bd_addr);
1320     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1321 
1322     connection->hf_activate_call_waiting_notification = 1;
1323     hfp_run_for_context(connection);
1324 }
1325 
1326 
1327 void hfp_hf_deactivate_call_waiting_notification(bd_addr_t bd_addr){
1328     hfp_hf_establish_service_level_connection(bd_addr);
1329     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1330 
1331     connection->hf_deactivate_call_waiting_notification = 1;
1332     hfp_run_for_context(connection);
1333 }
1334 
1335 
1336 void hfp_hf_activate_calling_line_notification(bd_addr_t bd_addr){
1337     hfp_hf_establish_service_level_connection(bd_addr);
1338     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1339 
1340     connection->hf_activate_calling_line_notification = 1;
1341     hfp_run_for_context(connection);
1342 }
1343 
1344 /*
1345  * @brief
1346  */
1347 void hfp_hf_deactivate_calling_line_notification(bd_addr_t bd_addr){
1348     hfp_hf_establish_service_level_connection(bd_addr);
1349     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1350 
1351     connection->hf_deactivate_calling_line_notification = 1;
1352     hfp_run_for_context(connection);
1353 }
1354 
1355 
1356 /*
1357  * @brief
1358  */
1359 void hfp_hf_activate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1360     hfp_hf_establish_service_level_connection(bd_addr);
1361     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1362 
1363     connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1364     hfp_run_for_context(connection);
1365 }
1366 
1367 /*
1368  * @brief
1369  */
1370 void hfp_hf_deactivate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1371     hfp_hf_establish_service_level_connection(bd_addr);
1372     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1373 
1374     connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1375     hfp_run_for_context(connection);
1376 }
1377 
1378 /*
1379  * @brief
1380  */
1381 void hfp_hf_activate_voice_recognition_notification(bd_addr_t bd_addr){
1382     hfp_hf_establish_service_level_connection(bd_addr);
1383     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1384 
1385     connection->hf_activate_voice_recognition_notification = 1;
1386     hfp_run_for_context(connection);
1387 }
1388 
1389 /*
1390  * @brief
1391  */
1392 void hfp_hf_deactivate_voice_recognition_notification(bd_addr_t bd_addr){
1393     hfp_hf_establish_service_level_connection(bd_addr);
1394     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1395 
1396     connection->hf_deactivate_voice_recognition_notification = 1;
1397     hfp_run_for_context(connection);
1398 }
1399 
1400 /*
1401  * @brief
1402  */
1403 void hfp_hf_set_microphone_gain(bd_addr_t bd_addr, int gain){
1404     hfp_hf_establish_service_level_connection(bd_addr);
1405     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1406     if (connection->microphone_gain == gain) return;
1407 
1408     connection->microphone_gain = gain;
1409     connection->send_microphone_gain = 1;
1410     hfp_run_for_context(connection);
1411 }
1412 
1413 /*
1414  * @brief
1415  */
1416 void hfp_hf_set_speaker_gain(bd_addr_t bd_addr, int gain){
1417     hfp_hf_establish_service_level_connection(bd_addr);
1418     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1419     if (connection->speaker_gain == gain) return;
1420 
1421     connection->speaker_gain = gain;
1422     connection->send_speaker_gain = 1;
1423     hfp_run_for_context(connection);
1424 }
1425 
1426 /*
1427  * @brief
1428  */
1429 void hfp_hf_send_dtmf_code(bd_addr_t addr, char code){
1430     hfp_hf_establish_service_level_connection(addr);
1431     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1432     connection->hf_send_dtmf_code = code;
1433     hfp_run_for_context(connection);
1434 }
1435 
1436 /*
1437  * @brief
1438  */
1439 void hfp_hf_request_phone_number_for_voice_tag(bd_addr_t addr){
1440     hfp_hf_establish_service_level_connection(addr);
1441     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1442     connection->hf_send_binp = 1;
1443     hfp_run_for_context(connection);
1444 }
1445 
1446 /*
1447  * @brief
1448  */
1449 void hfp_hf_query_current_call_status(bd_addr_t addr){
1450     hfp_hf_establish_service_level_connection(addr);
1451     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1452     connection->hf_send_clcc = 1;
1453     hfp_run_for_context(connection);
1454 }
1455 
1456 
1457 /*
1458  * @brief
1459  */
1460 void hfp_hf_rrh_query_status(bd_addr_t addr){
1461     hfp_hf_establish_service_level_connection(addr);
1462     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1463     connection->hf_send_rrh = 1;
1464     connection->hf_send_rrh_command = '?';
1465     hfp_run_for_context(connection);
1466 }
1467 
1468 /*
1469  * @brief
1470  */
1471 void hfp_hf_rrh_hold_call(bd_addr_t addr){
1472     hfp_hf_establish_service_level_connection(addr);
1473     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1474     connection->hf_send_rrh = 1;
1475     connection->hf_send_rrh_command = '0';
1476     hfp_run_for_context(connection);
1477 }
1478 
1479 /*
1480  * @brief
1481  */
1482 void hfp_hf_rrh_accept_held_call(bd_addr_t addr){
1483     hfp_hf_establish_service_level_connection(addr);
1484     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1485     connection->hf_send_rrh = 1;
1486     connection->hf_send_rrh_command = '1';
1487     hfp_run_for_context(connection);
1488 }
1489 
1490 /*
1491  * @brief
1492  */
1493 void hfp_hf_rrh_reject_held_call(bd_addr_t addr)
1494 {
1495     hfp_hf_establish_service_level_connection(addr);
1496     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1497     connection->hf_send_rrh = 1;
1498     connection->hf_send_rrh_command = '2';
1499     hfp_run_for_context(connection);
1500 }
1501 
1502 /*
1503  * @brief
1504  */
1505 void hfp_hf_query_subscriber_number(bd_addr_t addr)
1506 {
1507     hfp_hf_establish_service_level_connection(addr);
1508     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1509     connection->hf_send_cnum = 1;
1510     hfp_run_for_context(connection);
1511 }
1512 
1513 /*
1514  * @brief
1515  */
1516 void hfp_hf_set_hf_indicator(bd_addr_t addr, int assigned_number, int value){
1517     hfp_hf_establish_service_level_connection(addr);
1518     hfp_connection_t * connection = get_hfp_connection_context_for_bd_addr(addr);
1519     // find index for assigned number
1520     int i;
1521     for (i = 0; i < hfp_indicators_nr ; i++){
1522         if (hfp_indicators[i] == assigned_number){
1523             // set value
1524             hfp_indicators_value[i] = value;
1525             // mark for update
1526             connection->generic_status_update_bitmap |= (1<<i);
1527             // send update
1528             hfp_run_for_context(connection);
1529             return;
1530         }
1531     }
1532 }
1533 
1534