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