xref: /btstack/src/classic/hfp_ag.c (revision 06e1ef76482a4c9e6c01926f0f9636fff00eff9b)
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 #define __BTSTACK_FILE__ "hfp_ag.c"
39 
40 // *****************************************************************************
41 //
42 // HFP Audio Gateway (AG) unit
43 //
44 // *****************************************************************************
45 
46 #include "btstack_config.h"
47 
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "hci_cmd.h"
54 #include "btstack_run_loop.h"
55 
56 #include "bluetooth_sdp.h"
57 #include "hci.h"
58 #include "btstack_memory.h"
59 #include "hci_dump.h"
60 #include "l2cap.h"
61 #include "btstack_debug.h"
62 #include "btstack_event.h"
63 #include "classic/core.h"
64 #include "classic/hfp.h"
65 #include "classic/hfp_ag.h"
66 #include "classic/hfp_gsm_model.h"
67 #include "classic/sdp_client_rfcomm.h"
68 #include "classic/sdp_server.h"
69 #include "classic/sdp_util.h"
70 
71 // private prototypes
72 static void hfp_run_for_context(hfp_connection_t *hfp_connection);
73 static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
74 static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
75 
76 // public prototypes
77 hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void);
78 int get_hfp_generic_status_indicators_nr(void);
79 void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr);
80 void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr);
81 int get_hfp_ag_indicators_nr(hfp_connection_t * context);
82 hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context);
83 
84 // gobals
85 static const char default_hfp_ag_service_name[] = "Voice gateway";
86 
87 static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
88 
89 static uint8_t hfp_codecs_nr = 0;
90 static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
91 
92 static int  hfp_ag_indicators_nr = 0;
93 static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_AG_INDICATORS];
94 
95 static int hfp_generic_status_indicators_nr = 0;
96 static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_HF_INDICATORS];
97 
98 static int  hfp_ag_call_hold_services_nr = 0;
99 static char *hfp_ag_call_hold_services[6];
100 static btstack_packet_handler_t hfp_ag_callback;
101 
102 static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
103 static int hfp_ag_response_and_hold_active = 0;
104 
105 // Subcriber information entries
106 static hfp_phone_number_t * subscriber_numbers = NULL;
107 static int subscriber_numbers_count = 0;
108 
109 hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection);
110 
111 
112 static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
113     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
114         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
115         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
116     }
117     return hfp_connection->ag_indicators_nr;
118 }
119 
120 hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
121     // TODO: save only value, and value changed in the hfp_connection?
122     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
123         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
124         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
125     }
126     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
127 }
128 
129 static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
130     int i;
131     for (i = 0; i < hfp_ag_indicators_nr; i++){
132         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
133             return &hfp_ag_indicators[i];
134         }
135     }
136     return NULL;
137 }
138 
139 static int get_ag_indicator_index_for_name(const char * name){
140     int i;
141     for (i = 0; i < hfp_ag_indicators_nr; i++){
142         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
143             return i;
144         }
145     }
146     return -1;
147 }
148 
149 static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){
150     btstack_linked_list_iterator_t it;
151     btstack_linked_list_iterator_init(&it, hfp_get_connections());
152     while (btstack_linked_list_iterator_has_next(&it)){
153         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
154         if (hfp_connection->acl_handle != handle)      continue;
155         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
156         return hfp_connection;
157     }
158     return NULL;
159 }
160 
161 void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
162     if (callback == NULL){
163         log_error("hfp_ag_register_packet_handler called with NULL callback");
164         return;
165     }
166     hfp_ag_callback = callback;
167     hfp_set_ag_callback(callback);
168 }
169 
170 static int use_in_band_tone(void){
171     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
172 }
173 
174 static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
175     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
176     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
177     return hf && ag;
178 }
179 
180 static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
181     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
182     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
183     return hf && ag;
184 }
185 
186 static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
187     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
188     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
189     return hf && ag;
190 }
191 
192 void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint8_t ability_to_reject_call, uint16_t supported_features, int wide_band_speech){
193     if (!name){
194         name = default_hfp_ag_service_name;
195     }
196     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
197 
198     /*
199      * 0x01 – Ability to reject a call
200      * 0x00 – No ability to reject a call
201      */
202     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
203     de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
204 
205     // Construct SupportedFeatures for SDP bitmap:
206     //
207     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
208     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
209     //
210     uint16_t sdp_features = supported_features &0x1f;
211     if (supported_features & wide_band_speech){
212         sdp_features |= 1 << 5; // Wide band speech bit
213     }
214     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
215     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
216 }
217 
218 static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){
219     char buffer[20];
220     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
221     return send_str_over_rfcomm(cid, buffer);
222 }
223 
224 static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
225     char buffer[40];
226     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
227     return send_str_over_rfcomm(cid, buffer);
228 }
229 
230 static int hfp_ag_send_ok(uint16_t cid){
231     char buffer[10];
232     sprintf(buffer, "\r\nOK\r\n");
233     return send_str_over_rfcomm(cid, buffer);
234 }
235 
236 static int hfp_ag_send_ring(uint16_t cid){
237     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
238 }
239 
240 static int hfp_ag_send_clip(uint16_t cid){
241     char buffer[50];
242     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
243     return send_str_over_rfcomm(cid, buffer);
244 }
245 
246 static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
247     char buffer[50];
248     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
249     return send_str_over_rfcomm(cid, buffer);
250 }
251 
252 static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
253     char buffer[50];
254     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
255     return send_str_over_rfcomm(cid, buffer);
256 }
257 
258 static int hfp_ag_send_call_waiting_notification(uint16_t cid){
259     char buffer[50];
260     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
261     return send_str_over_rfcomm(cid, buffer);
262 }
263 
264 static int hfp_ag_send_error(uint16_t cid){
265     char buffer[10];
266     sprintf(buffer, "\r\nERROR\r\n");
267     return send_str_over_rfcomm(cid, buffer);
268 }
269 
270 static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
271     char buffer[20];
272     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
273     return send_str_over_rfcomm(cid, buffer);
274 }
275 
276 // get size for indicator string
277 static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
278     // template: ("$NAME",($MIN,$MAX))
279     return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
280          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
281          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
282 }
283 
284 // store indicator
285 static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
286     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
287             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
288             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
289             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
290 }
291 
292 // structure: header [indicator [comma indicator]] footer
293 static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
294     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
295     if (!num_indicators) return 2;
296     return 3 + (num_indicators-1) * 2;
297 }
298 
299 // get size of individual segment for hfp_ag_retrieve_indicators_cmd
300 static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
301     if (index == 0) {
302         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
303     }
304     index--;
305     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
306     int indicator_index = index >> 1;
307     if ((index & 1) == 0){
308         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
309     }
310     if (indicator_index == num_indicators - 1){
311         return 8; // "\r\n\r\nOK\r\n"
312     }
313     return 1; // comma
314 }
315 
316 static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
317     if (index == 0){
318         *buffer++ = '\r';
319         *buffer++ = '\n';
320         int len = strlen(HFP_INDICATOR);
321         memcpy(buffer, HFP_INDICATOR, len);
322         buffer += len;
323         *buffer++ = ':';
324         return;
325     }
326     index--;
327     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
328     int indicator_index = index >> 1;
329     if ((index & 1) == 0){
330         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
331         return;
332     }
333     if (indicator_index == num_indicators-1){
334         memcpy(buffer, "\r\n\r\nOK\r\n", 8);
335         return;
336     }
337     *buffer = ',';
338 }
339 
340 static int hfp_hf_indicators_join(char * buffer, int buffer_size){
341     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
342     int i;
343     int offset = 0;
344     for (i = 0; i < hfp_generic_status_indicators_nr-1; i++) {
345         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
346     }
347     if (i < hfp_generic_status_indicators_nr){
348         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
349     }
350     return offset;
351 }
352 
353 static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
354     if (buffer_size < hfp_generic_status_indicators_nr * 3) return 0;
355     int i;
356     int offset = 0;
357     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
358         offset += snprintf(buffer+offset, buffer_size-offset, "\r\n%s:%d,%d\r\n", HFP_GENERIC_STATUS_INDICATOR, hfp_generic_status_indicators[i].uuid, hfp_generic_status_indicators[i].state);
359     }
360     return offset;
361 }
362 
363 static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
364     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
365     int i;
366     int offset = 0;
367     for (i = 0; i < hfp_ag_indicators_nr-1; i++) {
368         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
369     }
370     if (i<hfp_ag_indicators_nr){
371         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
372     }
373     return offset;
374 }
375 
376 static int hfp_ag_call_services_join(char * buffer, int buffer_size){
377     if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0;
378     int i;
379     int offset = snprintf(buffer, buffer_size, "(");
380     for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) {
381         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
382     }
383     if (i<hfp_ag_call_hold_services_nr){
384         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
385     }
386     return offset;
387 }
388 
389 static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
390     int start_segment, int num_segments,
391     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
392     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
393 
394     // assumes: can send now == true
395     // assumes: num segments > 0
396     // assumes: individual segments are smaller than MTU
397     rfcomm_reserve_packet_buffer();
398     int mtu = rfcomm_get_max_frame_size(cid);
399     uint8_t * data = rfcomm_get_outgoing_buffer();
400     int offset = 0;
401     int segment = start_segment;
402     while (segment < num_segments){
403         int segment_len = get_segment_len(hfp_connection, segment);
404         if (offset + segment_len > mtu) break;
405         // append segement
406         store_segment(hfp_connection, segment, data+offset);
407         offset += segment_len;
408         segment++;
409     }
410     rfcomm_send_prepared(cid, offset);
411     return segment;
412 }
413 
414 // returns next segment to store
415 static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
416     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
417     return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
418         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
419 }
420 
421 static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){
422     char buffer[40];
423     const int size = sizeof(buffer);
424     int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR);
425     offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9);
426     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
427     return send_str_over_rfcomm(cid, buffer);
428 }
429 
430 static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){
431     char buffer[40];
432     const int size = sizeof(buffer);
433     int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
434     offset += hfp_ag_call_services_join(buffer+offset, size-offset-9);
435     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
436     return send_str_over_rfcomm(cid, buffer);
437 }
438 
439 
440 static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){
441     return hfp_ag_send_ok(cid);
442 }
443 
444 static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
445     char buffer[40];
446     const int size = sizeof(buffer);
447     int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
448     offset += hfp_hf_indicators_join(buffer+offset, size-offset-10);
449     offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n");
450     return send_str_over_rfcomm(cid, buffer);
451 }
452 
453 static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
454     char buffer[40];
455     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7);
456     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
457     return send_str_over_rfcomm(cid, buffer);
458 }
459 
460 static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
461     char buffer[20];
462     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
463     return send_str_over_rfcomm(cid, buffer);
464 }
465 
466 static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
467     char buffer[41];
468     if (strlen(op.name) == 0){
469         snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
470     } else {
471         int offset = snprintf(buffer,  sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format);
472         offset += snprintf(buffer+offset, 16, "%s", op.name);
473         snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
474     }
475     return send_str_over_rfcomm(cid, buffer);
476 }
477 
478 static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
479     char buffer[30];
480     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value);
481     return send_str_over_rfcomm(cid, buffer);
482 }
483 
484 static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){
485     return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
486 }
487 
488 static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
489     return hfp_ag_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
490 }
491 
492 static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
493     return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
494 }
495 
496 static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
497     return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
498 }
499 
500 static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){
501     return hfp_ag_send_cmd_with_int(cid, HFP_RESPONSE_AND_HOLD, state);
502 }
503 
504 static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
505     if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
506 
507     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){
508         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
509             return HFP_CODEC_MSBC;
510         }
511     }
512     return HFP_CODEC_CVSD;
513 }
514 
515 static void hfp_init_link_settings(hfp_connection_t * hfp_connection){
516     // determine highest possible link setting
517     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
518     // anything else requires eSCO support on both sides
519     if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
520         switch (hfp_connection->negotiated_codec){
521             case HFP_CODEC_CVSD:
522                 hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
523                 if ((hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4))
524                 &&  (hfp_supported_features                    & (1<<HFP_AGSF_ESCO_S4))){
525                     hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
526                 }
527                 break;
528             case HFP_CODEC_MSBC:
529                 hfp_connection->link_setting = HFP_LINK_SETTINGS_T2;
530                 break;
531             default:
532                 break;
533         }
534     }
535     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
536 }
537 
538 static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
539     /* events ( == commands):
540         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
541         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
542             hf_trigger_codec_connection_setup == received BCC
543             ag_trigger_codec_connection_setup == received from AG to send BCS
544         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
545     */
546 
547      switch (hfp_connection->codecs_state){
548         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
549             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
550             break;
551         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
552             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
553             break;
554         default:
555             break;
556     }
557 
558     // printf(" -> State machine: CC\n");
559 
560     switch (hfp_connection->command){
561         case HFP_CMD_AVAILABLE_CODECS:
562             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
563                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
564                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
565                 return 1;
566             }
567 
568             switch (hfp_connection->codecs_state){
569                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
570                 case HFP_CODECS_EXCHANGED:
571                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
572                     break;
573                 default:
574                     break;
575             }
576             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
577             return 1;
578 
579         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
580             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
581             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
582             return 1;
583 
584         case HFP_CMD_AG_SEND_COMMON_CODEC:
585             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
586             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
587             hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
588             return 1;
589 
590         case HFP_CMD_HF_CONFIRMED_CODEC:
591             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
592                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
593                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
594                 return 1;
595             }
596             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
597             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
598             log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
599             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
600             // now, pick link settings
601             hfp_init_link_settings(hfp_connection);
602             return 1;
603         default:
604             break;
605     }
606     return 0;
607 }
608 
609 static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
610     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
611     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
612 
613     // if active call exist, set per-hfp_connection state active, too (when audio is on)
614     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
615         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
616     }
617     // if AG is ringing, also start ringing on the HF
618     if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS &&
619         hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
620         hfp_ag_hf_start_ringing(hfp_connection);
621     }
622 }
623 
624 static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
625     // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
626     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
627     int sent = 0;
628     switch(hfp_connection->command){
629         case HFP_CMD_SUPPORTED_FEATURES:
630             switch(hfp_connection->state){
631                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
632                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
633                     hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
634                     if (has_codec_negotiation_feature(hfp_connection)){
635                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
636                     } else {
637                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
638                     }
639                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
640                     return 1;
641                 default:
642                     break;
643             }
644             break;
645         case HFP_CMD_AVAILABLE_CODECS:
646             sent = codecs_exchange_state_machine(hfp_connection);
647             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
648                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
649             }
650             return sent;
651 
652         case HFP_CMD_RETRIEVE_AG_INDICATORS:
653             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
654                 // HF requested AG Indicators and we did expect it
655                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
656                 // continue below in state switch
657             }
658             break;
659 
660         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
661             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
662             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
663             hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
664             return 1;
665 
666         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
667             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
668             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
669                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
670             } else if (has_hf_indicators_feature(hfp_connection)){
671                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
672             } else {
673                 hfp_ag_slc_established(hfp_connection);
674             }
675             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
676             return 1;
677 
678         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
679             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
680             if (has_hf_indicators_feature(hfp_connection)){
681                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
682             }
683             hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
684             if (!has_hf_indicators_feature(hfp_connection)){
685                 hfp_ag_slc_established(hfp_connection);
686             }
687             return 1;
688 
689         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
690             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
691             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
692             hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
693             return 1;
694 
695         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
696             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
697             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
698             hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
699             return 1;
700 
701         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
702             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
703             hfp_ag_slc_established(hfp_connection);
704             hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
705             return 1;
706         default:
707             break;
708     }
709 
710     switch (hfp_connection->state){
711         case HFP_RETRIEVE_INDICATORS: {
712             int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
713             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
714             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
715             if (next_segment < num_segments){
716                 // prepare sending of next segment
717                 hfp_connection->send_ag_indicators_segment = next_segment;
718                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
719             } else {
720                 // done, go to next state
721                 hfp_connection->send_ag_indicators_segment = 0;
722                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
723             }
724             return 1;
725         }
726         default:
727             break;
728     }
729     return 0;
730 }
731 
732 static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
733     int sent = codecs_exchange_state_machine(hfp_connection);
734     if (sent) return 1;
735 
736     switch(hfp_connection->command){
737         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
738             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
739             hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
740             return 1;
741         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
742             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
743                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
744                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
745                 hfp_ag_setup_audio_connection(hfp_connection);
746             } else {
747                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
748             }
749             return 1;
750         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
751             hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
752             return 1;
753         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
754             hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
755             return 1;
756         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
757             if (hfp_connection->network_operator.format != 0){
758                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
759             } else {
760                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
761             }
762             return 1;
763         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
764             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
765             return 1;
766         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
767             if (hfp_connection->extended_audio_gateway_error){
768                 hfp_connection->extended_audio_gateway_error = 0;
769                 hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
770                 return 1;
771             }
772             break;
773         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
774             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
775             return 1;
776         default:
777             break;
778     }
779     return 0;
780 }
781 
782 static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
783     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
784         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
785 
786 
787     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
788         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
789         hfp_connection->release_audio_connection = 0;
790         gap_disconnect(hfp_connection->sco_handle);
791         return 1;
792     }
793 
794     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
795 
796     // run codecs exchange
797     int sent = codecs_exchange_state_machine(hfp_connection);
798     if (sent) return 1;
799 
800     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
801     if (hfp_connection->establish_audio_connection){
802         hfp_connection->state = HFP_W4_SCO_CONNECTED;
803         hfp_connection->establish_audio_connection = 0;
804         hfp_setup_synchronous_connection(hfp_connection);
805         return 1;
806     }
807     return 0;
808 }
809 
810 static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
811     btstack_linked_list_iterator_t it;
812     btstack_linked_list_iterator_init(&it, hfp_get_connections());
813 
814     while (btstack_linked_list_iterator_has_next(&it)){
815         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
816         if ( & hfp_connection->hfp_timeout == ts) {
817             return hfp_connection;
818         }
819     }
820     return NULL;
821 }
822 
823 static void hfp_timeout_handler(btstack_timer_source_t * timer){
824     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
825     if (!hfp_connection) return;
826 
827     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
828     hfp_connection->ag_ring = 1;
829     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
830 
831     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
832     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
833 
834     hfp_run_for_context(hfp_connection);
835 }
836 
837 static void hfp_timeout_start(hfp_connection_t * hfp_connection){
838     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
839     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
840     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
841     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
842 }
843 
844 static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
845     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
846     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
847 }
848 
849 //
850 // transitition implementations for hfp_ag_call_state_machine
851 //
852 
853 static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
854     if (use_in_band_tone()){
855         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
856         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
857     } else {
858         hfp_timeout_start(hfp_connection);
859         hfp_connection->ag_ring = 1;
860         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
861         hfp_connection->call_state = HFP_CALL_RINGING;
862         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
863     }
864 }
865 
866 static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
867     hfp_connection->ag_ring = 0;
868     hfp_connection->ag_send_clip = 0;
869     hfp_timeout_stop(hfp_connection);
870     hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGINIG);
871 }
872 
873 static void hfp_ag_trigger_incoming_call(void){
874     int indicator_index = get_ag_indicator_index_for_name("callsetup");
875     if (indicator_index < 0) return;
876 
877     btstack_linked_list_iterator_t it;
878     btstack_linked_list_iterator_init(&it, hfp_get_connections());
879     while (btstack_linked_list_iterator_has_next(&it)){
880         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
881         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
882         if (hfp_connection->call_state == HFP_CALL_IDLE){
883             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
884             hfp_ag_hf_start_ringing(hfp_connection);
885         }
886         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
887             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
888         }
889         hfp_run_for_context(hfp_connection);
890     }
891 }
892 
893 static void hfp_ag_transfer_callsetup_state(void){
894     int indicator_index = get_ag_indicator_index_for_name("callsetup");
895     if (indicator_index < 0) return;
896 
897     btstack_linked_list_iterator_t it;
898     btstack_linked_list_iterator_init(&it, hfp_get_connections());
899     while (btstack_linked_list_iterator_has_next(&it)){
900         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
901         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
902         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
903         hfp_run_for_context(hfp_connection);
904     }
905 }
906 
907 static void hfp_ag_transfer_call_state(void){
908     int indicator_index = get_ag_indicator_index_for_name("call");
909     if (indicator_index < 0) return;
910 
911     btstack_linked_list_iterator_t it;
912     btstack_linked_list_iterator_init(&it, hfp_get_connections());
913     while (btstack_linked_list_iterator_has_next(&it)){
914         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
915         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
916         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
917         hfp_run_for_context(hfp_connection);
918     }
919 }
920 
921 static void hfp_ag_transfer_callheld_state(void){
922     int indicator_index = get_ag_indicator_index_for_name("callheld");
923     if (indicator_index < 0) return;
924 
925     btstack_linked_list_iterator_t it;
926     btstack_linked_list_iterator_init(&it, hfp_get_connections());
927     while (btstack_linked_list_iterator_has_next(&it)){
928         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
929         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
930         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
931         hfp_run_for_context(hfp_connection);
932     }
933 }
934 
935 static void hfp_ag_hf_accept_call(hfp_connection_t * source){
936 
937     int call_indicator_index = get_ag_indicator_index_for_name("call");
938     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
939 
940     btstack_linked_list_iterator_t it;
941     btstack_linked_list_iterator_init(&it, hfp_get_connections());
942     while (btstack_linked_list_iterator_has_next(&it)){
943         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
944         if (hfp_connection->call_state != HFP_CALL_RINGING &&
945             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
946 
947         hfp_ag_hf_stop_ringing(hfp_connection);
948         if (hfp_connection == source){
949             hfp_connection->ok_pending = 1;
950 
951             if (use_in_band_tone()){
952                 hfp_connection->call_state = HFP_CALL_ACTIVE;
953             } else {
954                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
955                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
956             }
957 
958             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
959             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
960 
961         } else {
962             hfp_connection->call_state = HFP_CALL_IDLE;
963         }
964         hfp_run_for_context(hfp_connection);
965     }
966 }
967 
968 static void hfp_ag_ag_accept_call(void){
969 
970     int call_indicator_index = get_ag_indicator_index_for_name("call");
971     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
972 
973     btstack_linked_list_iterator_t it;
974     btstack_linked_list_iterator_init(&it, hfp_get_connections());
975     while (btstack_linked_list_iterator_has_next(&it)){
976         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
977         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
978 
979         hfp_ag_hf_stop_ringing(hfp_connection);
980         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
981 
982         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
983         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
984 
985         hfp_run_for_context(hfp_connection);
986         break;  // only single
987     }
988 }
989 
990 static void hfp_ag_trigger_reject_call(void){
991     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
992     btstack_linked_list_iterator_t it;
993     btstack_linked_list_iterator_init(&it, hfp_get_connections());
994     while (btstack_linked_list_iterator_has_next(&it)){
995         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
996         if (connection->call_state != HFP_CALL_RINGING &&
997             connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
998         hfp_ag_hf_stop_ringing(connection);
999         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1000         connection->call_state = HFP_CALL_IDLE;
1001         hfp_run_for_context(connection);
1002     }
1003 }
1004 
1005 static void hfp_ag_emit_simple_event(uint8_t event_subtype){
1006     uint8_t event[3];
1007     event[0] = HCI_EVENT_HFP_META;
1008     event[1] = sizeof(event) - 2;
1009     event[2] = event_subtype;
1010     if (!hfp_ag_callback) return;
1011     (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1012 }
1013 
1014 static void hfp_ag_trigger_terminate_call(void){
1015     int call_indicator_index = get_ag_indicator_index_for_name("call");
1016 
1017     btstack_linked_list_iterator_t it;
1018     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1019     while (btstack_linked_list_iterator_has_next(&it)){
1020         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1021         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1022         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1023         hfp_connection->call_state = HFP_CALL_IDLE;
1024         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1025         hfp_connection->release_audio_connection = 1;
1026         hfp_run_for_context(hfp_connection);
1027     }
1028     hfp_ag_emit_simple_event(HFP_SUBEVENT_CALL_TERMINATED);
1029 }
1030 
1031 static void hfp_ag_set_callsetup_indicator(void){
1032     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1033     if (!indicator){
1034         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
1035         return;
1036     };
1037     indicator->status = hfp_gsm_callsetup_status();
1038 }
1039 
1040 static void hfp_ag_set_callheld_indicator(void){
1041     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1042     if (!indicator){
1043         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
1044         return;
1045     };
1046     indicator->status = hfp_gsm_callheld_status();
1047 }
1048 
1049 static void hfp_ag_set_call_indicator(void){
1050     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1051     if (!indicator){
1052         log_error("hfp_ag_set_call_state: call indicator is missing");
1053         return;
1054     };
1055     indicator->status = hfp_gsm_call_status();
1056 }
1057 
1058 static void hfp_ag_stop_ringing(void){
1059     btstack_linked_list_iterator_t it;
1060     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1061     while (btstack_linked_list_iterator_has_next(&it)){
1062         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1063         if (hfp_connection->call_state != HFP_CALL_RINGING &&
1064             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1065         hfp_ag_hf_stop_ringing(hfp_connection);
1066     }
1067 }
1068 
1069 static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1070     btstack_linked_list_iterator_t it;
1071     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1072     while (btstack_linked_list_iterator_has_next(&it)){
1073         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1074         if (hfp_connection->call_state == call_state) return hfp_connection;
1075     }
1076     return NULL;
1077 }
1078 
1079 static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1080     btstack_linked_list_iterator_t it;
1081     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1082     while (btstack_linked_list_iterator_has_next(&it)){
1083         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1084         hfp_connection->send_response_and_hold_status = state + 1;
1085     }
1086 }
1087 
1088 static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1089     int indicator_index;
1090     switch (hfp_connection->call_state){
1091         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1092             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1093             // we got event: audio hfp_connection established
1094             hfp_timeout_start(hfp_connection);
1095             hfp_connection->ag_ring = 1;
1096             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1097             hfp_connection->call_state = HFP_CALL_RINGING;
1098             hfp_connection->call_state = HFP_CALL_RINGING;
1099             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
1100             break;
1101         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1102             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1103             // we got event: audio hfp_connection established
1104             hfp_connection->call_state = HFP_CALL_ACTIVE;
1105             break;
1106         case HFP_CALL_W2_SEND_CALL_WAITING:
1107             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1108             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1109             indicator_index = get_ag_indicator_index_for_name("callsetup");
1110             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1111             break;
1112         default:
1113             break;
1114     }
1115     return 0;
1116 }
1117 // hfp_connection is used to identify originating HF
1118 static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1119     int indicator_index;
1120     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1121     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1122     int call_indicator_index = get_ag_indicator_index_for_name("call");
1123 
1124     //printf("hfp_ag_call_sm event %d \n", event);
1125     switch (event){
1126         case HFP_AG_INCOMING_CALL:
1127             switch (hfp_gsm_call_status()){
1128                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1129                     switch (hfp_gsm_callsetup_status()){
1130                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1131                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1132                             hfp_ag_set_callsetup_indicator();
1133                             hfp_ag_trigger_incoming_call();
1134                             log_info("AG rings");
1135                             break;
1136                         default:
1137                             break;
1138                     }
1139                     break;
1140                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1141                     switch (hfp_gsm_callsetup_status()){
1142                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1143                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1144                             hfp_ag_set_callsetup_indicator();
1145                             hfp_ag_trigger_incoming_call();
1146                             log_info("AG call waiting");
1147                             break;
1148                         default:
1149                             break;
1150                     }
1151                     break;
1152             }
1153             break;
1154         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1155             switch (hfp_gsm_call_status()){
1156                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1157                     switch (hfp_gsm_callsetup_status()){
1158                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1159                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1160                             hfp_ag_set_call_indicator();
1161                             hfp_ag_set_callsetup_indicator();
1162                             hfp_ag_ag_accept_call();
1163                             log_info("AG answers call, accept call by GSM");
1164                             break;
1165                         default:
1166                             break;
1167                     }
1168                     break;
1169                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1170                     switch (hfp_gsm_callsetup_status()){
1171                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1172                             log_info("AG: current call is placed on hold, incoming call gets active");
1173                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1174                             hfp_ag_set_callsetup_indicator();
1175                             hfp_ag_set_callheld_indicator();
1176                             hfp_ag_transfer_callsetup_state();
1177                             hfp_ag_transfer_callheld_state();
1178                             break;
1179                         default:
1180                             break;
1181                     }
1182                     break;
1183             }
1184             break;
1185 
1186         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1187             switch (hfp_gsm_call_status()){
1188                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1189                     switch (hfp_gsm_callheld_status()){
1190                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
1191                             log_info("AG: joining held call with active call");
1192                             hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG);
1193                             hfp_ag_set_callheld_indicator();
1194                             hfp_ag_transfer_callheld_state();
1195                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1196                             break;
1197                         default:
1198                             break;
1199                     }
1200                     break;
1201                 default:
1202                     break;
1203             }
1204             break;
1205 
1206         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1207             switch (hfp_gsm_call_status()){
1208                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1209                     switch (hfp_gsm_callsetup_status()){
1210                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1211                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF);
1212                             hfp_ag_set_callsetup_indicator();
1213                             hfp_ag_set_call_indicator();
1214                             hfp_ag_hf_accept_call(hfp_connection);
1215                             log_info("HF answers call, accept call by GSM");
1216                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1217                             break;
1218                         default:
1219                             break;
1220                     }
1221                     break;
1222                 default:
1223                     break;
1224             }
1225             break;
1226 
1227         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1228             switch (hfp_gsm_call_status()){
1229                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1230                     switch (hfp_gsm_callsetup_status()){
1231                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1232                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG);
1233                             hfp_ag_response_and_hold_active = 1;
1234                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1235                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1236                             // as with regualr call
1237                             hfp_ag_set_call_indicator();
1238                             hfp_ag_set_callsetup_indicator();
1239                             hfp_ag_ag_accept_call();
1240                             log_info("AG response and hold - hold by AG");
1241                             break;
1242                         default:
1243                             break;
1244                     }
1245                     break;
1246                 default:
1247                     break;
1248             }
1249             break;
1250 
1251         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1252             switch (hfp_gsm_call_status()){
1253                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1254                     switch (hfp_gsm_callsetup_status()){
1255                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1256                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF);
1257                             hfp_ag_response_and_hold_active = 1;
1258                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1259                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1260                             // as with regualr call
1261                             hfp_ag_set_call_indicator();
1262                             hfp_ag_set_callsetup_indicator();
1263                             hfp_ag_hf_accept_call(hfp_connection);
1264                             log_info("AG response and hold - hold by HF");
1265                             break;
1266                         default:
1267                             break;
1268                     }
1269                     break;
1270                 default:
1271                     break;
1272             }
1273             break;
1274 
1275         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1276         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1277             if (!hfp_ag_response_and_hold_active) break;
1278             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1279             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG);
1280             hfp_ag_response_and_hold_active = 0;
1281             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1282             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1283             log_info("Held Call accepted and active");
1284             break;
1285 
1286         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1287         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1288             if (!hfp_ag_response_and_hold_active) break;
1289             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1290             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG);
1291             hfp_ag_response_and_hold_active = 0;
1292             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1293             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1294             // from terminate by ag
1295             hfp_ag_set_call_indicator();
1296             hfp_ag_trigger_terminate_call();
1297             break;
1298 
1299         case HFP_AG_TERMINATE_CALL_BY_HF:
1300             switch (hfp_gsm_call_status()){
1301                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1302                     switch (hfp_gsm_callsetup_status()){
1303                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1304                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1305                             hfp_ag_set_callsetup_indicator();
1306                             hfp_ag_transfer_callsetup_state();
1307                             hfp_ag_trigger_reject_call();
1308                             log_info("HF Rejected Incoming call, AG terminate call");
1309                             break;
1310                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1311                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1312                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1313                             hfp_ag_set_callsetup_indicator();
1314                             hfp_ag_transfer_callsetup_state();
1315                             log_info("AG terminate outgoing call process");
1316                             break;
1317                         default:
1318                             break;
1319                     }
1320                     break;
1321                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1322                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1323                     hfp_ag_set_call_indicator();
1324                     hfp_ag_transfer_call_state();
1325                     hfp_connection->call_state = HFP_CALL_IDLE;
1326                     log_info("AG terminate call");
1327                     break;
1328             }
1329             break;
1330 
1331         case HFP_AG_TERMINATE_CALL_BY_AG:
1332             switch (hfp_gsm_call_status()){
1333                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1334                     switch (hfp_gsm_callsetup_status()){
1335                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1336                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1337                             hfp_ag_set_callsetup_indicator();
1338                             hfp_ag_trigger_reject_call();
1339                             log_info("AG Rejected Incoming call, AG terminate call");
1340                             break;
1341                         default:
1342                             break;
1343                     }
1344                     break;
1345                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1346                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1347                     hfp_ag_set_callsetup_indicator();
1348                     hfp_ag_set_call_indicator();
1349                     hfp_ag_trigger_terminate_call();
1350                     log_info("AG terminate call");
1351                     break;
1352                 default:
1353                     break;
1354             }
1355             break;
1356         case HFP_AG_CALL_DROPPED:
1357             switch (hfp_gsm_call_status()){
1358                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1359                     switch (hfp_gsm_callsetup_status()){
1360                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1361                             hfp_ag_stop_ringing();
1362                             log_info("Incoming call interrupted");
1363                             break;
1364                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1365                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1366                             log_info("Outgoing call interrupted\n");
1367                             log_info("AG notify call dropped\n");
1368                             break;
1369                         default:
1370                             break;
1371                     }
1372                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1373                     hfp_ag_set_callsetup_indicator();
1374                     hfp_ag_transfer_callsetup_state();
1375                     break;
1376                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1377                     if (hfp_ag_response_and_hold_active) {
1378                         hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1379                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1380                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1381                     }
1382                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1383                     hfp_ag_set_callsetup_indicator();
1384                     hfp_ag_set_call_indicator();
1385                     hfp_ag_trigger_terminate_call();
1386                     log_info("AG notify call dropped");
1387                     break;
1388                 default:
1389                     break;
1390             }
1391             break;
1392 
1393         case HFP_AG_OUTGOING_CALL_INITIATED:
1394             // directly reject call if number of free slots is exceeded
1395             if (!hfp_gsm_call_possible()){
1396                 hfp_connection->send_error = 1;
1397                 hfp_run_for_context(hfp_connection);
1398                 break;
1399             }
1400             hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]);
1401 
1402             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
1403 
1404             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1405             break;
1406 
1407         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1408             // directly reject call if number of free slots is exceeded
1409             if (!hfp_gsm_call_possible()){
1410                 hfp_connection->send_error = 1;
1411                 hfp_run_for_context(hfp_connection);
1412                 break;
1413             }
1414 
1415             hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED);
1416             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
1417 
1418             log_info("Redial last number");
1419             char * last_dialed_number = hfp_gsm_last_dialed_number();
1420 
1421             if (strlen(last_dialed_number) > 0){
1422                 log_info("Last number exists: accept call");
1423                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
1424             } else {
1425                 log_info("log_infoLast number missing: reject call");
1426                 hfp_ag_outgoing_call_rejected();
1427             }
1428             break;
1429         }
1430         case HFP_AG_OUTGOING_CALL_REJECTED:
1431             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1432             if (!hfp_connection){
1433                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1434                 break;
1435             }
1436 
1437             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED);
1438             hfp_connection->call_state = HFP_CALL_IDLE;
1439             hfp_connection->send_error = 1;
1440             hfp_run_for_context(hfp_connection);
1441             break;
1442 
1443         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1444             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1445             if (!hfp_connection){
1446                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1447                 break;
1448             }
1449 
1450             hfp_connection->ok_pending = 1;
1451             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1452 
1453             // trigger callsetup to be
1454             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1455             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED);
1456 
1457             hfp_ag_set_callsetup_indicator();
1458             indicator_index = get_ag_indicator_index_for_name("callsetup");
1459             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1460 
1461             // put current call on hold if active
1462             if (put_call_on_hold){
1463                 log_info("AG putting current call on hold for new outgoing calllog_info");
1464                 hfp_ag_set_callheld_indicator();
1465                 indicator_index = get_ag_indicator_index_for_name("callheld");
1466                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1467             }
1468 
1469             // start audio if needed
1470             hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1471             break;
1472         }
1473         case HFP_AG_OUTGOING_CALL_RINGING:
1474             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1475             if (!hfp_connection){
1476                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1477                 break;
1478             }
1479 
1480             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING);
1481             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1482             hfp_ag_set_callsetup_indicator();
1483             hfp_ag_transfer_callsetup_state();
1484             break;
1485 
1486         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1487             // get outgoing call
1488             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1489             if (!hfp_connection){
1490                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1491             }
1492             if (!hfp_connection){
1493                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1494                 break;
1495             }
1496 
1497             int CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS = hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS;
1498             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED);
1499             hfp_connection->call_state = HFP_CALL_ACTIVE;
1500             hfp_ag_set_callsetup_indicator();
1501             hfp_ag_set_call_indicator();
1502             hfp_ag_transfer_call_state();
1503             hfp_ag_transfer_callsetup_state();
1504             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1505                 hfp_ag_set_callheld_indicator();
1506                 hfp_ag_transfer_callheld_state();
1507             }
1508             break;
1509         }
1510 
1511         case HFP_AG_CALL_HOLD_USER_BUSY:
1512             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY);
1513             hfp_ag_set_callsetup_indicator();
1514             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1515             hfp_connection->call_state = HFP_CALL_ACTIVE;
1516             log_info("AG: Call Waiting, User Busy");
1517             break;
1518 
1519         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1520             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1521             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1522 
1523             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1524             if (call_held || call_setup_in_progress){
1525                 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1526 
1527             }
1528 
1529             if (call_setup_in_progress){
1530                 log_info("AG: Call Dropped, Accept new call");
1531                 hfp_ag_set_callsetup_indicator();
1532                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1533             } else {
1534                 log_info("AG: Call Dropped, Resume held call");
1535             }
1536             if (call_held){
1537                 hfp_ag_set_callheld_indicator();
1538                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1539             }
1540 
1541             hfp_connection->call_state = HFP_CALL_ACTIVE;
1542             break;
1543         }
1544 
1545         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1546             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1547             // only update if callsetup changed
1548             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1549             hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1550 
1551             if (call_setup_in_progress){
1552                 log_info("AG: Call on Hold, Accept new call");
1553                 hfp_ag_set_callsetup_indicator();
1554                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1555             } else {
1556                 log_info("AG: Swap calls");
1557             }
1558 
1559             hfp_ag_set_callheld_indicator();
1560             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1561             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1562             hfp_connection->call_state = HFP_CALL_ACTIVE;
1563             break;
1564         }
1565 
1566         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1567             // Adds a held call to the conversation.
1568             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
1569                 log_info("AG: Join 3-way-call");
1570                 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL);
1571                 hfp_ag_set_callheld_indicator();
1572                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1573                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1574             }
1575             hfp_connection->call_state = HFP_CALL_ACTIVE;
1576             break;
1577         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1578             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1579             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS);
1580             log_info("AG: Transfer call -> Connect two calls and disconnect");
1581             hfp_ag_set_call_indicator();
1582             hfp_ag_set_callheld_indicator();
1583             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1584             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1585             hfp_connection->call_state = HFP_CALL_IDLE;
1586             break;
1587 
1588         default:
1589             break;
1590     }
1591 
1592 
1593 }
1594 
1595 
1596 static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
1597     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
1598     if (!active_call) return;
1599 
1600     int idx = active_call->index;
1601     hfp_enhanced_call_dir_t dir = active_call->direction;
1602     hfp_enhanced_call_status_t status = active_call->enhanced_status;
1603     hfp_enhanced_call_mode_t mode = active_call->mode;
1604     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
1605     uint8_t type = active_call->clip_type;
1606     char * number = active_call->clip_number;
1607 
1608     char buffer[100];
1609     // TODO: check length of a buffer, to fit the MTU
1610     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
1611     if (number){
1612         offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
1613     }
1614     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
1615     log_info("hfp_ag_send_current_call_status 000 index %d, dir %d, status %d, mode %d, mpty %d, type %d, number %s", idx, dir, status,
1616        mode, mpty, type, number);
1617     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
1618 }
1619 
1620 static void hfp_run_for_context(hfp_connection_t *hfp_connection){
1621     if (!hfp_connection) return;
1622     if (!hfp_connection->rfcomm_cid) return;
1623 
1624     if (hfp_connection->local_role != HFP_ROLE_AG) {
1625         log_info("HFP AG%p, wrong role %u", hfp_connection, hfp_connection->local_role);
1626         return;
1627     }
1628 
1629     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1630         log_info("hfp_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1631         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1632         return;
1633     }
1634     if (hfp_connection->send_status_of_current_calls){
1635         hfp_connection->ok_pending = 0;
1636         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1637             hfp_connection->next_call_index++;
1638             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
1639         } else {
1640             hfp_connection->next_call_index = 0;
1641             hfp_connection->ok_pending = 1;
1642             hfp_connection->send_status_of_current_calls = 0;
1643         }
1644         return;
1645     }
1646 
1647     if (hfp_connection->ag_notify_incoming_call_waiting){
1648         hfp_connection->ag_notify_incoming_call_waiting = 0;
1649         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1650         return;
1651     }
1652 
1653     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1654         hfp_connection->ok_pending = 0;
1655         hfp_connection->send_error = 0;
1656         hfp_connection->command = HFP_CMD_NONE;
1657         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1658         return;
1659     }
1660 
1661     if (hfp_connection->send_error){
1662         hfp_connection->send_error = 0;
1663         hfp_connection->command = HFP_CMD_NONE;
1664         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1665         return;
1666     }
1667 
1668     // note: before update AG indicators and ok_pending
1669     if (hfp_connection->send_response_and_hold_status){
1670         int status = hfp_connection->send_response_and_hold_status - 1;
1671         hfp_connection->send_response_and_hold_status = 0;
1672         hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1673         return;
1674     }
1675 
1676     if (hfp_connection->ok_pending){
1677         hfp_connection->ok_pending = 0;
1678         hfp_connection->command = HFP_CMD_NONE;
1679         hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1680         return;
1681     }
1682 
1683     // update AG indicators
1684     if (hfp_connection->ag_indicators_status_update_bitmap){
1685         int i;
1686         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1687             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1688                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1689                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1690                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1691                     break;
1692                 }
1693                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1694                 return;
1695             }
1696         }
1697     }
1698 
1699     if (hfp_connection->ag_ring){
1700         hfp_connection->ag_ring = 0;
1701         hfp_connection->command = HFP_CMD_NONE;
1702         hfp_ag_send_ring(hfp_connection->rfcomm_cid);
1703         return;
1704     }
1705 
1706     if (hfp_connection->ag_send_clip){
1707         hfp_connection->ag_send_clip = 0;
1708         hfp_connection->command = HFP_CMD_NONE;
1709         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1710         return;
1711     }
1712 
1713     if (hfp_connection->send_phone_number_for_voice_tag){
1714         hfp_connection->send_phone_number_for_voice_tag = 0;
1715         hfp_connection->command = HFP_CMD_NONE;
1716         hfp_connection->ok_pending = 1;
1717         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1718         return;
1719     }
1720 
1721     if (hfp_connection->send_subscriber_number){
1722         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1723             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1724             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1725         } else {
1726             hfp_connection->send_subscriber_number = 0;
1727             hfp_connection->next_subscriber_number_to_send = 0;
1728             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1729         }
1730         hfp_connection->command = HFP_CMD_NONE;
1731 
1732     }
1733 
1734     if (hfp_connection->send_microphone_gain){
1735         hfp_connection->send_microphone_gain = 0;
1736         hfp_connection->command = HFP_CMD_NONE;
1737         hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1738         return;
1739     }
1740 
1741     if (hfp_connection->send_speaker_gain){
1742         hfp_connection->send_speaker_gain = 0;
1743         hfp_connection->command = HFP_CMD_NONE;
1744         hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
1745         return;
1746     }
1747 
1748     if (hfp_connection->send_ag_status_indicators){
1749         hfp_connection->send_ag_status_indicators = 0;
1750         hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1751         return;
1752     }
1753 
1754     // trigger codec exchange if requested by app
1755     if (hfp_connection->trigger_codec_exchange){
1756         log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state);
1757     }
1758     if (hfp_connection->trigger_codec_exchange && hfp_connection->command == HFP_CMD_NONE){
1759         switch (hfp_connection->codecs_state){
1760             case HFP_CODECS_IDLE:
1761             case HFP_CODECS_RECEIVED_LIST:
1762             case HFP_CODECS_AG_RESEND_COMMON_CODEC:
1763             case HFP_CODECS_ERROR:
1764                 hfp_connection->trigger_codec_exchange = 0;
1765                 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
1766                 break;
1767             default:
1768                 break;
1769         }
1770     }
1771 
1772     int cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1773     if (!cmd_sent){
1774         cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
1775     }
1776 
1777     if (!cmd_sent){
1778         cmd_sent = call_setup_state_machine(hfp_connection);
1779     }
1780 
1781     if (!cmd_sent){
1782         cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
1783     }
1784 
1785     if (hfp_connection->command == HFP_CMD_NONE && !cmd_sent){
1786         // log_info("hfp_connection->command == HFP_CMD_NONE");
1787         switch(hfp_connection->state){
1788             case HFP_W2_DISCONNECT_RFCOMM:
1789                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1790                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
1791                 break;
1792             default:
1793                 break;
1794         }
1795     }
1796 
1797     if (cmd_sent){
1798         hfp_connection->command = HFP_CMD_NONE;
1799         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1800     }
1801 }
1802 
1803 static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1804     int i;
1805     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1806         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1807         if (indicator->uuid == number){
1808             return indicator;
1809         }
1810     }
1811     return NULL;
1812 }
1813 
1814 static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1815     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
1816 
1817     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
1818     if (size < 1) return;
1819 
1820     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1821     if (!hfp_connection) return;
1822 
1823     // temp overwrite last byte (most likely \n for log_info)
1824     char last_char = packet[size-1];
1825     packet[size-1] = 0;
1826     log_info("HFP_RX %s", packet);
1827     packet[size-1] = last_char;
1828 
1829     // process messages byte-wise
1830     int pos;
1831     for (pos = 0; pos < size ; pos++){
1832         hfp_parse(hfp_connection, packet[pos], 0);
1833     }
1834     hfp_generic_status_indicator_t * indicator;
1835     int value;
1836     switch(hfp_connection->command){
1837         case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1838             if (hfp_ag_response_and_hold_active){
1839                 hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1840             }
1841             hfp_connection->ok_pending = 1;
1842             break;
1843         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
1844             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1845             log_info("HF Response and Hold: %u", value);
1846             switch(value){
1847                 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1848                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1849                     break;
1850                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1851                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1852                     break;
1853                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1854                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1855                     break;
1856                 default:
1857                     break;
1858             }
1859             hfp_connection->ok_pending = 1;
1860             break;
1861         case HFP_CMD_HF_INDICATOR_STATUS:
1862             hfp_connection->command = HFP_CMD_NONE;
1863             // find indicator by assigned number
1864             indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1865             if (!indicator){
1866                 hfp_connection->send_error = 1;
1867                 break;
1868             }
1869             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1870             switch (indicator->uuid){
1871                 case 1: // enhanced security
1872                     if (value > 1) {
1873                         hfp_connection->send_error = 1;
1874                         return;
1875                     }
1876                     log_info("HF Indicator 'enhanced security' set to %u", value);
1877                     break;
1878                 case 2: // battery level
1879                     if (value > 100){
1880                         hfp_connection->send_error = 1;
1881                         return;
1882                     }
1883                     log_info("HF Indicator 'battery' set to %u", value);
1884                     break;
1885                 default:
1886                     log_info("HF Indicator unknown set to %u", value);
1887                     break;
1888             }
1889             hfp_connection->ok_pending = 1;
1890             break;
1891         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1892             // expected by SLC state machine
1893             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1894             hfp_connection->send_ag_indicators_segment = 0;
1895             hfp_connection->send_ag_status_indicators = 1;
1896             break;
1897         case HFP_CMD_LIST_CURRENT_CALLS:
1898             hfp_connection->command = HFP_CMD_NONE;
1899             hfp_connection->next_call_index = 0;
1900             hfp_connection->send_status_of_current_calls = 1;
1901             break;
1902         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1903             if (subscriber_numbers_count == 0){
1904                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1905                 break;
1906             }
1907             hfp_connection->next_subscriber_number_to_send = 0;
1908             hfp_connection->send_subscriber_number = 1;
1909             break;
1910         case HFP_CMD_TRANSMIT_DTMF_CODES:
1911             hfp_connection->command = HFP_CMD_NONE;
1912             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]);
1913             break;
1914         case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1915             hfp_connection->command = HFP_CMD_NONE;
1916             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1917             break;
1918         case HFP_CMD_TURN_OFF_EC_AND_NR:
1919             hfp_connection->command = HFP_CMD_NONE;
1920             if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1921                 hfp_connection->ok_pending = 1;
1922                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
1923                 log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction);
1924             } else {
1925                 hfp_connection->send_error = 1;
1926             }
1927             break;
1928         case HFP_CMD_CALL_ANSWERED:
1929             hfp_connection->command = HFP_CMD_NONE;
1930             log_info("HFP: ATA");
1931             hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1932             break;
1933         case HFP_CMD_HANG_UP_CALL:
1934             hfp_connection->command = HFP_CMD_NONE;
1935             hfp_connection->ok_pending = 1;
1936             hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1937             break;
1938         case HFP_CMD_CALL_HOLD: {
1939             // TODO: fully implement this
1940             log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]);
1941             hfp_connection->command = HFP_CMD_NONE;
1942             hfp_connection->ok_pending = 1;
1943             hfp_connection->call_index = 0;
1944 
1945             if (hfp_connection->line_buffer[1] != '\0'){
1946                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
1947             }
1948 
1949             switch (hfp_connection->line_buffer[0]){
1950                 case '0':
1951                     // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1952                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1953                     break;
1954                 case '1':
1955                     // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1956                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1957                     // waiting call (i.e., not to the held call) in conflicting situation.
1958                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1959                     break;
1960                 case '2':
1961                     // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1962                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1963                     // waiting call (i.e., not to the held call) in conflicting situation.
1964                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1965                     break;
1966                 case '3':
1967                     // Adds a held call to the conversation.
1968                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1969                     break;
1970                 case '4':
1971                     // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1972                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1973                     break;
1974                 default:
1975                     break;
1976             }
1977             break;
1978         }
1979         case HFP_CMD_CALL_PHONE_NUMBER:
1980             hfp_connection->command = HFP_CMD_NONE;
1981             hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
1982             break;
1983         case HFP_CMD_REDIAL_LAST_NUMBER:
1984             hfp_connection->command = HFP_CMD_NONE;
1985             hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
1986             break;
1987         case HFP_CMD_ENABLE_CLIP:
1988             hfp_connection->command = HFP_CMD_NONE;
1989             hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0';
1990             log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
1991             hfp_connection->ok_pending = 1;
1992             break;
1993         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1994             hfp_connection->command = HFP_CMD_NONE;
1995             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0';
1996             log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
1997             hfp_connection->ok_pending = 1;
1998             break;
1999         case HFP_CMD_SET_SPEAKER_GAIN:
2000             hfp_connection->command = HFP_CMD_NONE;
2001             hfp_connection->ok_pending = 1;
2002             log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
2003             break;
2004         case HFP_CMD_SET_MICROPHONE_GAIN:
2005             hfp_connection->command = HFP_CMD_NONE;
2006             hfp_connection->ok_pending = 1;
2007             log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
2008             break;
2009         default:
2010             break;
2011     }
2012 }
2013 
2014 static void hfp_run(void){
2015     btstack_linked_list_iterator_t it;
2016     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2017     while (btstack_linked_list_iterator_has_next(&it)){
2018         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2019         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2020         hfp_run_for_context(hfp_connection);
2021     }
2022 }
2023 
2024 static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2025     switch (packet_type){
2026         case RFCOMM_DATA_PACKET:
2027             hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size);
2028             break;
2029         case HCI_EVENT_PACKET:
2030             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2031                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2032                 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2033                 return;
2034             }
2035             hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2036             break;
2037         default:
2038             break;
2039     }
2040 
2041     hfp_run();
2042 }
2043 
2044 void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
2045     if (codecs_nr > HFP_MAX_NUM_CODECS){
2046         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
2047         return;
2048     }
2049     int i;
2050     hfp_codecs_nr = codecs_nr;
2051     for (i=0; i < codecs_nr; i++){
2052         hfp_codecs[i] = codecs[i];
2053     }
2054 }
2055 
2056 void hfp_ag_init_supported_features(uint32_t supported_features){
2057     hfp_supported_features = supported_features;
2058 }
2059 
2060 void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2061     hfp_ag_indicators_nr = ag_indicators_nr;
2062     memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2063 }
2064 
2065 void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
2066     if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return;
2067     hfp_generic_status_indicators_nr = hf_indicators_nr;
2068     memcpy(hfp_generic_status_indicators, hf_indicators, hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2069 }
2070 
2071 void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
2072     hfp_ag_call_hold_services_nr = call_hold_services_nr;
2073     memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *));
2074 }
2075 
2076 
2077 void hfp_ag_init(uint16_t rfcomm_channel_nr){
2078     hfp_init();
2079 
2080     rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
2081     hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler);
2082 
2083     hfp_ag_response_and_hold_active = 0;
2084     subscriber_numbers = NULL;
2085     subscriber_numbers_count = 0;
2086 
2087     hfp_gsm_init();
2088 }
2089 
2090 void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
2091     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
2092 }
2093 
2094 void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
2095     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2096     if (!hfp_connection){
2097         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2098         return;
2099     }
2100     hfp_release_service_level_connection(hfp_connection);
2101     hfp_run_for_context(hfp_connection);
2102 }
2103 
2104 void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
2105     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2106     if (!hfp_connection){
2107         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2108         return;
2109     }
2110     hfp_connection->extended_audio_gateway_error = 0;
2111     if (!hfp_connection->enable_extended_audio_gateway_error_report){
2112         return;
2113     }
2114     hfp_connection->extended_audio_gateway_error = error;
2115     hfp_run_for_context(hfp_connection);
2116 }
2117 
2118 static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
2119     log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state);
2120     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2121     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
2122 
2123     hfp_connection->establish_audio_connection = 1;
2124     if (!has_codec_negotiation_feature(hfp_connection)){
2125         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2126         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2127         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
2128         // now, pick link settings
2129         hfp_init_link_settings(hfp_connection);
2130         return;
2131     }
2132 
2133     hfp_connection->trigger_codec_exchange = 1;
2134 }
2135 
2136 void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
2137     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2138     if (!hfp_connection){
2139         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2140         return;
2141     }
2142     hfp_connection->trigger_codec_exchange = 0;
2143     hfp_connection->establish_audio_connection = 0;
2144     hfp_ag_setup_audio_connection(hfp_connection);
2145     hfp_run_for_context(hfp_connection);
2146 }
2147 
2148 void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
2149     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2150     if (!hfp_connection){
2151         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2152         return;
2153     }
2154     hfp_release_audio_connection(hfp_connection);
2155     hfp_run_for_context(hfp_connection);
2156 }
2157 
2158 /**
2159  * @brief Enable in-band ring tone
2160  */
2161 void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2162     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2163         return;
2164     }
2165     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2166 
2167     btstack_linked_list_iterator_t it;
2168     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2169     while (btstack_linked_list_iterator_has_next(&it)){
2170         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2171         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2172         hfp_run_for_context(hfp_connection);
2173     }
2174 }
2175 
2176 /**
2177  * @brief Called from GSM
2178  */
2179 void hfp_ag_incoming_call(void){
2180     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2181 }
2182 
2183 /**
2184  * @brief number is stored.
2185  */
2186 void hfp_ag_set_clip(uint8_t type, const char * number){
2187     hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number);
2188 }
2189 
2190 void hfp_ag_call_dropped(void){
2191     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2192 }
2193 
2194 // call from AG UI
2195 void hfp_ag_answer_incoming_call(void){
2196     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2197 }
2198 
2199 void hfp_ag_join_held_call(void){
2200     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2201 }
2202 
2203 void hfp_ag_terminate_call(void){
2204     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2205 }
2206 
2207 void hfp_ag_outgoing_call_ringing(void){
2208     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2209 }
2210 
2211 void hfp_ag_outgoing_call_established(void){
2212     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2213 }
2214 
2215 void hfp_ag_outgoing_call_rejected(void){
2216     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2217 }
2218 
2219 void hfp_ag_outgoing_call_accepted(void){
2220     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2221 }
2222 
2223 void hfp_ag_hold_incoming_call(void){
2224     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2225 }
2226 
2227 void hfp_ag_accept_held_incoming_call(void) {
2228     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2229 }
2230 
2231 void hfp_ag_reject_held_incoming_call(void){
2232     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2233 }
2234 
2235 static void hfp_ag_set_ag_indicator(const char * name, int value){
2236     int indicator_index = get_ag_indicator_index_for_name(name);
2237     if (indicator_index < 0) return;
2238     hfp_ag_indicators[indicator_index].status = value;
2239 
2240 
2241     btstack_linked_list_iterator_t it;
2242     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2243     while (btstack_linked_list_iterator_has_next(&it)){
2244         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2245         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2246             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2247             continue;
2248         }
2249         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2250         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2251         hfp_run_for_context(hfp_connection);
2252     }
2253 }
2254 
2255 void hfp_ag_set_registration_status(int status){
2256     hfp_ag_set_ag_indicator("service", status);
2257 }
2258 
2259 void hfp_ag_set_signal_strength(int strength){
2260     hfp_ag_set_ag_indicator("signal", strength);
2261 }
2262 
2263 void hfp_ag_set_roaming_status(int status){
2264     hfp_ag_set_ag_indicator("roam", status);
2265 }
2266 
2267 void hfp_ag_set_battery_level(int level){
2268     hfp_ag_set_ag_indicator("battchg", level);
2269 }
2270 
2271 void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){
2272     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
2273     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2274     if (!hfp_connection){
2275         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2276         return;
2277     }
2278 
2279     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
2280         log_info("AG cannot acivate voice recognition - not supported by HF");
2281         return;
2282     }
2283 
2284     if (activate){
2285         hfp_ag_establish_audio_connection(acl_handle);
2286     }
2287 
2288     hfp_connection->ag_activate_voice_recognition = activate;
2289     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2290     hfp_run_for_context(hfp_connection);
2291 }
2292 
2293 void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
2294     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2295     if (!hfp_connection){
2296         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2297         return;
2298     }
2299     if (hfp_connection->microphone_gain != gain){
2300         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2301         hfp_connection->microphone_gain = gain;
2302         hfp_connection->send_microphone_gain = 1;
2303     }
2304     hfp_run_for_context(hfp_connection);
2305 }
2306 
2307 void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
2308     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2309     if (!hfp_connection){
2310         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2311         return;
2312     }
2313     if (hfp_connection->speaker_gain != gain){
2314         hfp_connection->speaker_gain = gain;
2315         hfp_connection->send_speaker_gain = 1;
2316     }
2317     hfp_run_for_context(hfp_connection);
2318 }
2319 
2320 void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
2321     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2322     if (!hfp_connection){
2323         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2324         return;
2325     }
2326     hfp_ag_set_clip(0, number);
2327     hfp_connection->send_phone_number_for_voice_tag = 1;
2328 }
2329 
2330 void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
2331     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2332     if (!hfp_connection){
2333         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2334         return;
2335     }
2336     hfp_connection->send_error = 1;
2337 }
2338 
2339 void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
2340     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2341     if (!hfp_connection){
2342         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2343         return;
2344     }
2345     hfp_connection->ok_pending = 1;
2346 }
2347 
2348 void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2349     subscriber_numbers = numbers;
2350     subscriber_numbers_count = numbers_count;
2351 }
2352 
2353 void hfp_ag_clear_last_dialed_number(void){
2354     hfp_gsm_clear_last_dialed_number();
2355 }
2356 
2357 void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
2358     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2359     if (!hfp_connection){
2360         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2361         return;
2362     }
2363     if (!hfp_connection->call_waiting_notification_enabled) return;
2364 
2365     hfp_connection->ag_notify_incoming_call_waiting = 1;
2366     hfp_run_for_context(hfp_connection);
2367 }
2368 
2369