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