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