xref: /btstack/src/classic/hfp_ag.c (revision f8737b81dad32e65ed9cf59fa78d273cbfe3b2dc)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp_ag.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
4266a048abSMatthias Ringwald // HFP Audio Gateway (AG) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493deb3ec6SMatthias Ringwald #include <string.h>
503deb3ec6SMatthias Ringwald 
5156042629SMatthias Ringwald #include "hci_cmd.h"
5282636622SMatthias Ringwald #include "btstack_run_loop.h"
533deb3ec6SMatthias Ringwald 
54235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
553deb3ec6SMatthias Ringwald #include "hci.h"
563deb3ec6SMatthias Ringwald #include "btstack_memory.h"
573deb3ec6SMatthias Ringwald #include "hci_dump.h"
583deb3ec6SMatthias Ringwald #include "l2cap.h"
5916ece135SMatthias Ringwald #include "btstack_debug.h"
60e30a6a47SMatthias Ringwald #include "btstack_event.h"
6159c6af15SMatthias Ringwald #include "classic/core.h"
623edc84c5SMatthias Ringwald #include "classic/hfp.h"
633edc84c5SMatthias Ringwald #include "classic/hfp_ag.h"
64023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h"
65efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
66023f2764SMatthias Ringwald #include "classic/sdp_server.h"
67023f2764SMatthias Ringwald #include "classic/sdp_util.h"
683deb3ec6SMatthias Ringwald 
69c5b64319SMatthias Ringwald // private prototypes
70f0aeb307SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection);
71adaba9f3SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
72adaba9f3SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
73c5b64319SMatthias Ringwald 
74c5b64319SMatthias Ringwald // public prototypes
7535833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void);
7635833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void);
77c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr);
78c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr);
79c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context);
80c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context);
81c5b64319SMatthias Ringwald 
8227950165SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
8327950165SMatthias Ringwald 
84c5b64319SMatthias Ringwald // gobals
853deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway";
86aa4dd815SMatthias Ringwald 
873deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
88aa4dd815SMatthias Ringwald 
893deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
903deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
913deb3ec6SMatthias Ringwald 
923deb3ec6SMatthias Ringwald static int  hfp_ag_indicators_nr = 0;
9325789943SMilanka Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS];
943deb3ec6SMatthias Ringwald 
95a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0;
9625789943SMilanka Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_INDICATORS];
97a0ffb263SMatthias Ringwald 
983deb3ec6SMatthias Ringwald static int  hfp_ag_call_hold_services_nr = 0;
993deb3ec6SMatthias Ringwald static char *hfp_ag_call_hold_services[6];
100ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback;
1013deb3ec6SMatthias Ringwald 
102ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
103ce263fc8SMatthias Ringwald static int hfp_ag_response_and_hold_active = 0;
104aa4dd815SMatthias Ringwald 
105ce263fc8SMatthias Ringwald // Subcriber information entries
106ce263fc8SMatthias Ringwald static hfp_phone_number_t * subscriber_numbers = NULL;
107ce263fc8SMatthias Ringwald static int subscriber_numbers_count = 0;
108ce263fc8SMatthias Ringwald 
1093deb3ec6SMatthias Ringwald 
11076cc1527SMatthias Ringwald static void hfp_ag_emit_simple_event(uint8_t event_subtype){
11176cc1527SMatthias Ringwald 	uint8_t event[3];
11276cc1527SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
11376cc1527SMatthias Ringwald 	event[1] = sizeof(event) - 2;
11476cc1527SMatthias Ringwald 	event[2] = event_subtype;
11576cc1527SMatthias Ringwald 	if (!hfp_ag_callback) return;
11676cc1527SMatthias Ringwald 	(*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
11776cc1527SMatthias Ringwald }
11876cc1527SMatthias Ringwald 
119a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
120a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
121a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1226535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1236535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1243deb3ec6SMatthias Ringwald     }
125a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
126a0ffb263SMatthias Ringwald }
127a0ffb263SMatthias Ringwald 
128a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
129a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
130a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
131a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1326535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1336535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
134a0ffb263SMatthias Ringwald     }
135a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1363deb3ec6SMatthias Ringwald }
1373deb3ec6SMatthias Ringwald 
138aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
139aa4dd815SMatthias Ringwald     int i;
140aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
141aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
142aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
143aa4dd815SMatthias Ringwald         }
144aa4dd815SMatthias Ringwald     }
145aa4dd815SMatthias Ringwald     return NULL;
146aa4dd815SMatthias Ringwald }
147aa4dd815SMatthias Ringwald 
148aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
149aa4dd815SMatthias Ringwald     int i;
150aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
151aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
152aa4dd815SMatthias Ringwald             return i;
153aa4dd815SMatthias Ringwald         }
154aa4dd815SMatthias Ringwald     }
155aa4dd815SMatthias Ringwald     return -1;
156aa4dd815SMatthias Ringwald }
157aa4dd815SMatthias Ringwald 
1589c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){
1599c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1609c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1619c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1629c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1639c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1649c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1659c9c64c1SMatthias Ringwald         return hfp_connection;
1669c9c64c1SMatthias Ringwald     }
1679c9c64c1SMatthias Ringwald     return NULL;
1689c9c64c1SMatthias Ringwald }
1693deb3ec6SMatthias Ringwald 
1700cb5b971SMatthias Ringwald static int use_in_band_tone(void){
171aa4dd815SMatthias Ringwald     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1723deb3ec6SMatthias Ringwald }
1733deb3ec6SMatthias Ringwald 
174a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
175a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1763deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1773deb3ec6SMatthias Ringwald     return hf && ag;
1783deb3ec6SMatthias Ringwald }
1793deb3ec6SMatthias Ringwald 
180a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
181a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1823deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1833deb3ec6SMatthias Ringwald     return hf && ag;
1843deb3ec6SMatthias Ringwald }
1853deb3ec6SMatthias Ringwald 
186a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
187a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
1883deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
1893deb3ec6SMatthias Ringwald     return hf && ag;
1903deb3ec6SMatthias Ringwald }
1913deb3ec6SMatthias Ringwald 
19276cc1527SMatthias Ringwald /* unsolicited responses */
193aa4dd815SMatthias Ringwald 
194485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){
195aa4dd815SMatthias Ringwald     char buffer[20];
196aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
197aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1983deb3ec6SMatthias Ringwald }
1993deb3ec6SMatthias Ringwald 
2003deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2013deb3ec6SMatthias Ringwald     char buffer[40];
2023deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
2033deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2043deb3ec6SMatthias Ringwald }
2053deb3ec6SMatthias Ringwald 
206485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){
2073deb3ec6SMatthias Ringwald     char buffer[10];
2083deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nOK\r\n");
2093deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2103deb3ec6SMatthias Ringwald }
2113deb3ec6SMatthias Ringwald 
212485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){
213aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
214aa4dd815SMatthias Ringwald }
215aa4dd815SMatthias Ringwald 
216aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
217aa4dd815SMatthias Ringwald     char buffer[50];
218d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
219aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
220aa4dd815SMatthias Ringwald }
221aa4dd815SMatthias Ringwald 
222ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
223ce263fc8SMatthias Ringwald     char buffer[50];
224ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
225ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
226ce263fc8SMatthias Ringwald }
227ce263fc8SMatthias Ringwald 
228c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
229aa4dd815SMatthias Ringwald     char buffer[50];
230d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
231aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
232aa4dd815SMatthias Ringwald }
233aa4dd815SMatthias Ringwald 
234aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
235aa4dd815SMatthias Ringwald     char buffer[50];
236a0ffb263SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
237aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
238aa4dd815SMatthias Ringwald }
239aa4dd815SMatthias Ringwald 
240485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){
2413deb3ec6SMatthias Ringwald     char buffer[10];
2423deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nERROR\r\n");
2433deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2443deb3ec6SMatthias Ringwald }
2453deb3ec6SMatthias Ringwald 
246485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2473deb3ec6SMatthias Ringwald     char buffer[20];
2483deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2493deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2503deb3ec6SMatthias Ringwald }
2513deb3ec6SMatthias Ringwald 
252ad902e3dSMatthias Ringwald // get size for indicator string
253a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
254ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
25545718b6fSMatthias Ringwald     return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
256a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
257a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
258ad902e3dSMatthias Ringwald }
259ad902e3dSMatthias Ringwald 
260ad902e3dSMatthias Ringwald // store indicator
261a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
262ad902e3dSMatthias Ringwald     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
263a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
264a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
265a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2663deb3ec6SMatthias Ringwald }
267ad902e3dSMatthias Ringwald 
268ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
269a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
270a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
271ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
272c1ab6cc1SMatthias Ringwald     return 3 + ((num_indicators-1) * 2);
2733deb3ec6SMatthias Ringwald }
274ad902e3dSMatthias Ringwald 
275ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
276a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
277ad902e3dSMatthias Ringwald     if (index == 0) {
278ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
279ad902e3dSMatthias Ringwald     }
280ad902e3dSMatthias Ringwald     index--;
281a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
282ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
283ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
284a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
285ad902e3dSMatthias Ringwald     }
286c1ab6cc1SMatthias Ringwald     if (indicator_index == (num_indicators - 1)){
287ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
288ad902e3dSMatthias Ringwald     }
289ad902e3dSMatthias Ringwald     return 1; // comma
290ad902e3dSMatthias Ringwald }
291ad902e3dSMatthias Ringwald 
292a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
293ad902e3dSMatthias Ringwald     if (index == 0){
294ad902e3dSMatthias Ringwald         *buffer++ = '\r';
29574386ee0SMatthias Ringwald         *buffer++ = '\n';
296ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
2976535961aSMatthias Ringwald         (void)memcpy(buffer, HFP_INDICATOR, len);
298ad902e3dSMatthias Ringwald         buffer += len;
299ad902e3dSMatthias Ringwald         *buffer++ = ':';
300ad902e3dSMatthias Ringwald         return;
301ad902e3dSMatthias Ringwald     }
302ad902e3dSMatthias Ringwald     index--;
303a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
304ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
305ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
306a0ffb263SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
307ad902e3dSMatthias Ringwald         return;
308ad902e3dSMatthias Ringwald     }
309c1ab6cc1SMatthias Ringwald     if (indicator_index == (num_indicators-1)){
3106535961aSMatthias Ringwald         (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8);
311ad902e3dSMatthias Ringwald         return;
312ad902e3dSMatthias Ringwald     }
313ad902e3dSMatthias Ringwald     *buffer = ',';
3143deb3ec6SMatthias Ringwald }
3153deb3ec6SMatthias Ringwald 
3163deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
317c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3183deb3ec6SMatthias Ringwald     int i;
3193deb3ec6SMatthias Ringwald     int offset = 0;
320c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_generic_status_indicators_nr-1); i++) {
321a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3223deb3ec6SMatthias Ringwald     }
323a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
324a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3253deb3ec6SMatthias Ringwald     }
3263deb3ec6SMatthias Ringwald     return offset;
3273deb3ec6SMatthias Ringwald }
3283deb3ec6SMatthias Ringwald 
3293deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
330c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_generic_status_indicators_nr * 3)) return 0;
3313deb3ec6SMatthias Ringwald     int i;
3323deb3ec6SMatthias Ringwald     int offset = 0;
333a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
334a0ffb263SMatthias Ringwald         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);
3353deb3ec6SMatthias Ringwald     }
3363deb3ec6SMatthias Ringwald     return offset;
3373deb3ec6SMatthias Ringwald }
3383deb3ec6SMatthias Ringwald 
3393deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
340c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3413deb3ec6SMatthias Ringwald     int i;
3423deb3ec6SMatthias Ringwald     int offset = 0;
343c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_ag_indicators_nr-1); i++) {
3443deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3453deb3ec6SMatthias Ringwald     }
3463deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3473deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3483deb3ec6SMatthias Ringwald     }
3493deb3ec6SMatthias Ringwald     return offset;
3503deb3ec6SMatthias Ringwald }
3513deb3ec6SMatthias Ringwald 
3523deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
353c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0;
3543deb3ec6SMatthias Ringwald     int i;
3553deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
356c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) {
3573deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3583deb3ec6SMatthias Ringwald     }
3593deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3603deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3613deb3ec6SMatthias Ringwald     }
3623deb3ec6SMatthias Ringwald     return offset;
3633deb3ec6SMatthias Ringwald }
3643deb3ec6SMatthias Ringwald 
365485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
366ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
367a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
368a0ffb263SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
3693deb3ec6SMatthias Ringwald 
370ad902e3dSMatthias Ringwald     // assumes: can send now == true
371ad902e3dSMatthias Ringwald     // assumes: num segments > 0
372aba39421SMatthias Ringwald     // assumes: individual segments are smaller than MTU
373ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
374ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
375ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
376ad902e3dSMatthias Ringwald     int offset = 0;
377ad902e3dSMatthias Ringwald     int segment = start_segment;
378ad902e3dSMatthias Ringwald     while (segment < num_segments){
379a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
380c1ab6cc1SMatthias Ringwald         if ((offset + segment_len) > mtu) break;
381aba39421SMatthias Ringwald         // append segement
382a0ffb263SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset);
383ad902e3dSMatthias Ringwald         offset += segment_len;
384ad902e3dSMatthias Ringwald         segment++;
385ad902e3dSMatthias Ringwald     }
386ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
387ad902e3dSMatthias Ringwald     return segment;
388ad902e3dSMatthias Ringwald }
3893deb3ec6SMatthias Ringwald 
390ad902e3dSMatthias Ringwald // returns next segment to store
391485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
392a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
393485ac19eSMilanka Ringwald     return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
394ad902e3dSMatthias Ringwald         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
3953deb3ec6SMatthias Ringwald }
3963deb3ec6SMatthias Ringwald 
397485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){
3983deb3ec6SMatthias Ringwald     char buffer[40];
39989425bfcSMilanka Ringwald     const int size = sizeof(buffer);
40089425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR);
40189425bfcSMilanka Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9);
40289425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4033deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4043deb3ec6SMatthias Ringwald }
4053deb3ec6SMatthias Ringwald 
406485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){
407ad902e3dSMatthias Ringwald     char buffer[40];
40889425bfcSMilanka Ringwald     const int size = sizeof(buffer);
40989425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
41089425bfcSMilanka Ringwald     offset += hfp_ag_call_services_join(buffer+offset, size-offset-9);
41189425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4123deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4133deb3ec6SMatthias Ringwald }
4143deb3ec6SMatthias Ringwald 
4153deb3ec6SMatthias Ringwald 
416485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){
417485ac19eSMilanka Ringwald     return hfp_ag_send_ok(cid);
4183deb3ec6SMatthias Ringwald }
4193deb3ec6SMatthias Ringwald 
420485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4213deb3ec6SMatthias Ringwald     char buffer[40];
42289425bfcSMilanka Ringwald     const int size = sizeof(buffer);
42389425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
42489425bfcSMilanka Ringwald     offset += hfp_hf_indicators_join(buffer+offset, size-offset-10);
42589425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n");
4263deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4273deb3ec6SMatthias Ringwald }
4283deb3ec6SMatthias Ringwald 
429485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4303deb3ec6SMatthias Ringwald     char buffer[40];
4311cc1d9e9SMilanka Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7);
4321cc1d9e9SMilanka Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4333deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4343deb3ec6SMatthias Ringwald }
4353deb3ec6SMatthias Ringwald 
436485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4373deb3ec6SMatthias Ringwald     char buffer[20];
438aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
4393deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4403deb3ec6SMatthias Ringwald }
4413deb3ec6SMatthias Ringwald 
442485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4435e71456cSMilanka Ringwald     char buffer[41];
4443deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
44589425bfcSMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4463deb3ec6SMatthias Ringwald     } else {
44789425bfcSMilanka Ringwald         int offset = snprintf(buffer,  sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format);
4485e71456cSMilanka Ringwald         offset += snprintf(buffer+offset, 16, "%s", op.name);
44989425bfcSMilanka Ringwald         snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4503deb3ec6SMatthias Ringwald     }
4513deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4523deb3ec6SMatthias Ringwald }
4533deb3ec6SMatthias Ringwald 
4541cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
4551cc1d9e9SMilanka Ringwald     char buffer[30];
45689425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value);
4571cc1d9e9SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
4581cc1d9e9SMilanka Ringwald }
4593deb3ec6SMatthias Ringwald 
460485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){
4611cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
4623deb3ec6SMatthias Ringwald }
4633deb3ec6SMatthias Ringwald 
464485ac19eSMilanka Ringwald static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
4651cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
466aa4dd815SMatthias Ringwald }
467aa4dd815SMatthias Ringwald 
468485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
4691cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
470aa4dd815SMatthias Ringwald }
471aa4dd815SMatthias Ringwald 
472485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
4731cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
474aa4dd815SMatthias Ringwald }
475aa4dd815SMatthias Ringwald 
476485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){
4771cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_RESPONSE_AND_HOLD, state);
478ce263fc8SMatthias Ringwald }
479ce263fc8SMatthias Ringwald 
480a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
481bc1b1537SMilanka Ringwald     if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
482bc1b1537SMilanka Ringwald 
4836a7f44bdSMilanka Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){
4846a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
485df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
4863deb3ec6SMatthias Ringwald         }
4873deb3ec6SMatthias Ringwald     }
488df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
4893deb3ec6SMatthias Ringwald }
4903deb3ec6SMatthias Ringwald 
49176cc1527SMatthias Ringwald /* state machines */
49276cc1527SMatthias Ringwald 
493afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){
494afac2a04SMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_AGSF_ESCO_S4));
4957522e673SMatthias Ringwald }
4967522e673SMatthias Ringwald 
497a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
498aa4dd815SMatthias Ringwald     /* events ( == commands):
499aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
500aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
501aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
502aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
503aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
504aa4dd815SMatthias Ringwald     */
505aa4dd815SMatthias Ringwald 
506a0ffb263SMatthias Ringwald      switch (hfp_connection->codecs_state){
507aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
508a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
509aa4dd815SMatthias Ringwald             break;
510aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
511a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
512aa4dd815SMatthias Ringwald             break;
513aa4dd815SMatthias Ringwald         default:
514aa4dd815SMatthias Ringwald             break;
515aa4dd815SMatthias Ringwald     }
516aa4dd815SMatthias Ringwald 
517a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
518aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
519a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
520a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
521485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
522aa4dd815SMatthias Ringwald                 return 1;
523aa4dd815SMatthias Ringwald             }
524aa4dd815SMatthias Ringwald 
525a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
526aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
527aa4dd815SMatthias Ringwald                 case HFP_CODECS_EXCHANGED:
528a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
529aa4dd815SMatthias Ringwald                     break;
530aa4dd815SMatthias Ringwald                 default:
531aa4dd815SMatthias Ringwald                     break;
532aa4dd815SMatthias Ringwald             }
533485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
534aa4dd815SMatthias Ringwald             return 1;
535aa4dd815SMatthias Ringwald 
536aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
537a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
538485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
539aa4dd815SMatthias Ringwald             return 1;
540aa4dd815SMatthias Ringwald 
541aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
542a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
543a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
544485ac19eSMilanka Ringwald             hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
545aa4dd815SMatthias Ringwald             return 1;
546aa4dd815SMatthias Ringwald 
547aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
548a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
549a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
550485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
551aa4dd815SMatthias Ringwald                 return 1;
552aa4dd815SMatthias Ringwald             }
553a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
554a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
555c1ab6cc1SMatthias Ringwald             log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
556485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
5577522e673SMatthias Ringwald             // now, pick link settings
558afac2a04SMilanka Ringwald 
559afac2a04SMilanka Ringwald             hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
560aa4dd815SMatthias Ringwald             return 1;
561aa4dd815SMatthias Ringwald         default:
562aa4dd815SMatthias Ringwald             break;
563aa4dd815SMatthias Ringwald     }
564aa4dd815SMatthias Ringwald     return 0;
565aa4dd815SMatthias Ringwald }
566aa4dd815SMatthias Ringwald 
567a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
568a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
569ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
570aa4dd815SMatthias Ringwald 
571a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
572d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
573a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
574aa4dd815SMatthias Ringwald     }
575ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
576c1ab6cc1SMatthias Ringwald     if ((hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) &&
577505f1c30SMatthias Ringwald         (hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS)){
578a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
579ce263fc8SMatthias Ringwald     }
580aa4dd815SMatthias Ringwald }
5813deb3ec6SMatthias Ringwald 
582a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
583c79b4cabSMatthias Ringwald     // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
584a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
585c04cf7ffSMilanka Ringwald     int sent = 0;
586a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
5873deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
588a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
5893deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
590aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
591d715cf51SMatthias Ringwald                     hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
592a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
593a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
594aa4dd815SMatthias Ringwald                     } else {
595a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
596aa4dd815SMatthias Ringwald                     }
597a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
598aa4dd815SMatthias Ringwald                     return 1;
5993deb3ec6SMatthias Ringwald                 default:
6003deb3ec6SMatthias Ringwald                     break;
6013deb3ec6SMatthias Ringwald             }
6023deb3ec6SMatthias Ringwald             break;
6033deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
604c04cf7ffSMilanka Ringwald             sent = codecs_exchange_state_machine(hfp_connection);
605a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
606a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6073deb3ec6SMatthias Ringwald             }
608c04cf7ffSMilanka Ringwald             return sent;
609aa4dd815SMatthias Ringwald 
610aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
611a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
612473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
61384a0c24eSMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
614473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
615473ac565SMatthias Ringwald                 // continue below in state switch
616ad902e3dSMatthias Ringwald             }
617ad902e3dSMatthias Ringwald             break;
618aa4dd815SMatthias Ringwald 
619aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
620a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
621a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
622485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
623aa4dd815SMatthias Ringwald             return 1;
624aa4dd815SMatthias Ringwald 
6253deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
626a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
627a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
628a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
629a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
630a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
631aa4dd815SMatthias Ringwald             } else {
632a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
6333deb3ec6SMatthias Ringwald             }
6348a46ec40SMatthias Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
635aa4dd815SMatthias Ringwald             return 1;
6363deb3ec6SMatthias Ringwald 
637aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
638a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
639a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
640a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
641aa4dd815SMatthias Ringwald             }
642485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
643a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
644a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
645ce263fc8SMatthias Ringwald             }
646aa4dd815SMatthias Ringwald             return 1;
647aa4dd815SMatthias Ringwald 
648aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
649a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
650a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
651485ac19eSMilanka Ringwald             hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
652aa4dd815SMatthias Ringwald             return 1;
653aa4dd815SMatthias Ringwald 
654aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
655a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
656a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
657485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
658aa4dd815SMatthias Ringwald             return 1;
659aa4dd815SMatthias Ringwald 
660aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
661a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
662a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
663485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
664aa4dd815SMatthias Ringwald             return 1;
6653deb3ec6SMatthias Ringwald         default:
6663deb3ec6SMatthias Ringwald             break;
6673deb3ec6SMatthias Ringwald     }
668473ac565SMatthias Ringwald 
669473ac565SMatthias Ringwald     switch (hfp_connection->state){
670473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
671485ac19eSMilanka Ringwald             int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
672473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
673473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
674473ac565SMatthias Ringwald             if (next_segment < num_segments){
675473ac565SMatthias Ringwald                 // prepare sending of next segment
676473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
677473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
678473ac565SMatthias Ringwald             } else {
679473ac565SMatthias Ringwald                 // done, go to next state
680473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
681473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
682473ac565SMatthias Ringwald             }
683473ac565SMatthias Ringwald             return 1;
684473ac565SMatthias Ringwald         }
685473ac565SMatthias Ringwald         default:
686473ac565SMatthias Ringwald             break;
687473ac565SMatthias Ringwald     }
688c04cf7ffSMilanka Ringwald     return 0;
6893deb3ec6SMatthias Ringwald }
6903deb3ec6SMatthias Ringwald 
691a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
692c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
693c04cf7ffSMilanka Ringwald     if (sent) return 1;
694aa4dd815SMatthias Ringwald 
695a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
696aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
697a0ffb263SMatthias Ringwald             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
698485ac19eSMilanka Ringwald             hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
699aa4dd815SMatthias Ringwald             return 1;
700aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
701aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
702a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
703485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
704a0ffb263SMatthias Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
705aa4dd815SMatthias Ringwald             } else {
706485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
707aa4dd815SMatthias Ringwald             }
708aa4dd815SMatthias Ringwald             return 1;
709aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
710485ac19eSMilanka Ringwald             hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
711aa4dd815SMatthias Ringwald             return 1;
712aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
713485ac19eSMilanka Ringwald             hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
714aa4dd815SMatthias Ringwald             return 1;
715aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
716a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
717485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
718aa4dd815SMatthias Ringwald             } else {
719485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
7203deb3ec6SMatthias Ringwald             }
721aa4dd815SMatthias Ringwald             return 1;
722aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
723485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
724aa4dd815SMatthias Ringwald             return 1;
7253deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
726a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
727a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
728485ac19eSMilanka Ringwald                 hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
729aa4dd815SMatthias Ringwald                 return 1;
7303deb3ec6SMatthias Ringwald             }
731202c8a4cSMatthias Ringwald             break;
7323deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
733485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
734ce263fc8SMatthias Ringwald             return 1;
7353deb3ec6SMatthias Ringwald         default:
7363deb3ec6SMatthias Ringwald             break;
7373deb3ec6SMatthias Ringwald     }
738aa4dd815SMatthias Ringwald     return 0;
7393deb3ec6SMatthias Ringwald }
7403deb3ec6SMatthias Ringwald 
741a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
742505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
743505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
7443deb3ec6SMatthias Ringwald 
745aa4dd815SMatthias Ringwald 
746505f1c30SMatthias Ringwald     if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){
747a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
748a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
749a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
750aa4dd815SMatthias Ringwald         return 1;
751aa4dd815SMatthias Ringwald     }
752aa4dd815SMatthias Ringwald 
753a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
754aa4dd815SMatthias Ringwald 
755aa4dd815SMatthias Ringwald     // run codecs exchange
756c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
757c04cf7ffSMilanka Ringwald     if (sent) return 1;
758aa4dd815SMatthias Ringwald 
759c04cf7ffSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
760a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
761a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
762a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
763eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
764aa4dd815SMatthias Ringwald         return 1;
765aa4dd815SMatthias Ringwald     }
766aa4dd815SMatthias Ringwald     return 0;
767aa4dd815SMatthias Ringwald }
768aa4dd815SMatthias Ringwald 
769ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
770665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
771665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
772aa4dd815SMatthias Ringwald 
773665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
774a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
775a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
776a0ffb263SMatthias Ringwald             return hfp_connection;
777aa4dd815SMatthias Ringwald         }
778aa4dd815SMatthias Ringwald     }
779aa4dd815SMatthias Ringwald     return NULL;
780aa4dd815SMatthias Ringwald }
781aa4dd815SMatthias Ringwald 
782ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
783d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
784d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
785aa4dd815SMatthias Ringwald 
786d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
787a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
788a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
789aa4dd815SMatthias Ringwald 
790a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
791a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
792aa4dd815SMatthias Ringwald 
793f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
794aa4dd815SMatthias Ringwald }
795aa4dd815SMatthias Ringwald 
796a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
797a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
798a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
799a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
800a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
801aa4dd815SMatthias Ringwald }
802aa4dd815SMatthias Ringwald 
803a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
804a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
805a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
806aa4dd815SMatthias Ringwald }
807aa4dd815SMatthias Ringwald 
808aa4dd815SMatthias Ringwald //
809aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
810aa4dd815SMatthias Ringwald //
811aa4dd815SMatthias Ringwald 
812a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
813aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
814a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
815d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
816aa4dd815SMatthias Ringwald     } else {
817a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
818a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
819a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
820a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
821ca59be51SMatthias Ringwald         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
822aa4dd815SMatthias Ringwald     }
823aa4dd815SMatthias Ringwald }
824aa4dd815SMatthias Ringwald 
825a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
826a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
827a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
828a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
829ca59be51SMatthias Ringwald     hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGINIG);
830aa4dd815SMatthias Ringwald }
831aa4dd815SMatthias Ringwald 
832aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
833aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
834aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
835aa4dd815SMatthias Ringwald 
836665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
837665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
838665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
839a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
84066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
841a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
842a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
843a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
844d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
845aa4dd815SMatthias Ringwald         }
846a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
847a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
848aa4dd815SMatthias Ringwald         }
849f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
850aa4dd815SMatthias Ringwald     }
851aa4dd815SMatthias Ringwald }
852aa4dd815SMatthias Ringwald 
853aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
854aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
855aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
856aa4dd815SMatthias Ringwald 
857665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
858665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
859665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
860a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
86166c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
862a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
863a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
864f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
865aa4dd815SMatthias Ringwald     }
866aa4dd815SMatthias Ringwald }
867aa4dd815SMatthias Ringwald 
868aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
869aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("call");
870aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
871aa4dd815SMatthias Ringwald 
872665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
873665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
874665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
875a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
87666c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
877a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
878a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
879f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
880aa4dd815SMatthias Ringwald     }
881aa4dd815SMatthias Ringwald }
882aa4dd815SMatthias Ringwald 
883aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
884aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callheld");
885aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
886aa4dd815SMatthias Ringwald 
887665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
888665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
889665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
890a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
89166c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
892a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
893a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
894f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
895aa4dd815SMatthias Ringwald     }
896aa4dd815SMatthias Ringwald }
897aa4dd815SMatthias Ringwald 
898aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
899aa4dd815SMatthias Ringwald 
900aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
901aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
902aa4dd815SMatthias Ringwald 
903665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
904665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
905665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
906a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
90766c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
908505f1c30SMatthias Ringwald         if ((hfp_connection->call_state != HFP_CALL_RINGING) &&
909505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
910aa4dd815SMatthias Ringwald 
911a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
912a0ffb263SMatthias Ringwald         if (hfp_connection == source){
913aa4dd815SMatthias Ringwald 
914aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
915a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
916aa4dd815SMatthias Ringwald             } else {
917a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
918d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
919aa4dd815SMatthias Ringwald             }
920aa4dd815SMatthias Ringwald 
921a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
922a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
923aa4dd815SMatthias Ringwald 
924aa4dd815SMatthias Ringwald         } else {
925a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
926aa4dd815SMatthias Ringwald         }
927f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
928aa4dd815SMatthias Ringwald     }
929aa4dd815SMatthias Ringwald }
930aa4dd815SMatthias Ringwald 
931aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
932aa4dd815SMatthias Ringwald 
933aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
934aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
935aa4dd815SMatthias Ringwald 
936665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
937665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
938665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
939a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
94066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
941a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
942aa4dd815SMatthias Ringwald 
943a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
944a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
945aa4dd815SMatthias Ringwald 
946a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
947a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
948aa4dd815SMatthias Ringwald 
949f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
950aa4dd815SMatthias Ringwald         break;  // only single
951aa4dd815SMatthias Ringwald     }
952aa4dd815SMatthias Ringwald }
953aa4dd815SMatthias Ringwald 
954aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
955aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
956665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
957665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
958665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
959665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
96066c5995fSMatthias Ringwald         if (connection->local_role != HFP_ROLE_AG) continue;
961505f1c30SMatthias Ringwald         if ((connection->call_state != HFP_CALL_RINGING) &&
962505f1c30SMatthias Ringwald             (connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
963aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
964aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
965aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
966f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(connection);
967aa4dd815SMatthias Ringwald     }
968aa4dd815SMatthias Ringwald }
969aa4dd815SMatthias Ringwald 
970aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
971aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
972aa4dd815SMatthias Ringwald 
973665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
974665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
975665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
976a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
97766c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
978d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
979a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
980a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
981a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
9826d78145cSMatthias Ringwald         if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
983a0ffb263SMatthias Ringwald             hfp_connection->release_audio_connection = 1;
9846d78145cSMatthias Ringwald         }
985f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
986aa4dd815SMatthias Ringwald     }
987ca59be51SMatthias Ringwald     hfp_ag_emit_simple_event(HFP_SUBEVENT_CALL_TERMINATED);
988aa4dd815SMatthias Ringwald }
989aa4dd815SMatthias Ringwald 
9900cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
991aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
992aa4dd815SMatthias Ringwald     if (!indicator){
993d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
99445718b6fSMatthias Ringwald         return;
995aa4dd815SMatthias Ringwald     };
996d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
997aa4dd815SMatthias Ringwald }
998aa4dd815SMatthias Ringwald 
9990cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1000d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1001d210d9c4SMatthias Ringwald     if (!indicator){
1002d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
100345718b6fSMatthias Ringwald         return;
1004d210d9c4SMatthias Ringwald     };
1005d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1006d210d9c4SMatthias Ringwald }
1007d210d9c4SMatthias Ringwald 
10080cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1009aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1010aa4dd815SMatthias Ringwald     if (!indicator){
1011aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
101245718b6fSMatthias Ringwald         return;
1013aa4dd815SMatthias Ringwald     };
1014d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1015aa4dd815SMatthias Ringwald }
1016aa4dd815SMatthias Ringwald 
1017aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1018665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1019665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1020665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1021a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
102266c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1023505f1c30SMatthias Ringwald         if ((hfp_connection->call_state != HFP_CALL_RINGING) &&
1024505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1025a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1026aa4dd815SMatthias Ringwald     }
1027aa4dd815SMatthias Ringwald }
1028aa4dd815SMatthias Ringwald 
1029aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1030665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1031665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1032665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1033a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
103466c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1035a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1036aa4dd815SMatthias Ringwald     }
1037aa4dd815SMatthias Ringwald     return NULL;
1038aa4dd815SMatthias Ringwald }
1039aa4dd815SMatthias Ringwald 
1040a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1041665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1042665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1043665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1044a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
104566c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1046a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1047ce263fc8SMatthias Ringwald     }
1048ce263fc8SMatthias Ringwald }
1049ce263fc8SMatthias Ringwald 
1050a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1051aa4dd815SMatthias Ringwald     int indicator_index;
1052a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1053aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1054a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1055a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1056a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1057a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1058a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1059a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1060ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
1061aa4dd815SMatthias Ringwald             break;
1062aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1063a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1064a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1065a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1066aa4dd815SMatthias Ringwald             break;
1067aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1068a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1069a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1070aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1071a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1072aa4dd815SMatthias Ringwald             break;
1073aa4dd815SMatthias Ringwald         default:
1074aa4dd815SMatthias Ringwald             break;
1075aa4dd815SMatthias Ringwald     }
1076aa4dd815SMatthias Ringwald     return 0;
1077aa4dd815SMatthias Ringwald }
107839426febSMatthias Ringwald 
107939426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below
108039426febSMatthias Ringwald static void hfp_ag_handle_reject_incoming_call(void){
108139426febSMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
108239426febSMatthias Ringwald     if (!hfp_connection){
108339426febSMatthias Ringwald         log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
108439426febSMatthias Ringwald         return;
108539426febSMatthias Ringwald     }
108639426febSMatthias Ringwald 
1087*f8737b81SMatthias Ringwald     hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL);
108839426febSMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
108939426febSMatthias Ringwald     hfp_connection->send_error = 1;
109039426febSMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
109139426febSMatthias Ringwald }
109239426febSMatthias Ringwald 
1093a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1094a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1095aa4dd815SMatthias Ringwald     int indicator_index;
1096d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1097d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1098d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
109974386ee0SMatthias Ringwald 
1100aa4dd815SMatthias Ringwald     switch (event){
1101aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1102d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1103aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1104d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1105*f8737b81SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: {
1106*f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL);
1107*f8737b81SMatthias Ringwald                         }
1108d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1109aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
111060ebb071SMilanka Ringwald                             log_info("AG rings");
1111aa4dd815SMatthias Ringwald                             break;
1112aa4dd815SMatthias Ringwald                         default:
11133deb3ec6SMatthias Ringwald                             break;
11143deb3ec6SMatthias Ringwald                     }
11153deb3ec6SMatthias Ringwald                     break;
1116aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1117d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1118*f8737b81SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS: {
1119*f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL);
1120*f8737b81SMatthias Ringwald                         }
1121d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1122aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
112360ebb071SMilanka Ringwald                             log_info("AG call waiting");
1124aa4dd815SMatthias Ringwald                             break;
1125aa4dd815SMatthias Ringwald                         default:
11263deb3ec6SMatthias Ringwald                             break;
11273deb3ec6SMatthias Ringwald                     }
11283deb3ec6SMatthias Ringwald                     break;
11297bbeb3adSMilanka Ringwald                 default:
11307bbeb3adSMilanka Ringwald                     break;
1131aa4dd815SMatthias Ringwald             }
1132aa4dd815SMatthias Ringwald             break;
1133aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1134d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1135aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1136d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1137*f8737b81SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: {
1138*f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL);
1139*f8737b81SMatthias Ringwald                         }
1140d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1141d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1142aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
114360ebb071SMilanka Ringwald                             log_info("AG answers call, accept call by GSM");
1144aa4dd815SMatthias Ringwald                             break;
1145aa4dd815SMatthias Ringwald                         default:
11463deb3ec6SMatthias Ringwald                             break;
11473deb3ec6SMatthias Ringwald                     }
1148aa4dd815SMatthias Ringwald                     break;
1149aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1150d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1151aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
115260ebb071SMilanka Ringwald                             log_info("AG: current call is placed on hold, incoming call gets active");
1153*f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL);
1154d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1155d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1156ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1157ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1158aa4dd815SMatthias Ringwald                             break;
1159aa4dd815SMatthias Ringwald                         default:
1160aa4dd815SMatthias Ringwald                             break;
1161aa4dd815SMatthias Ringwald                     }
1162aa4dd815SMatthias Ringwald                     break;
11637bbeb3adSMilanka Ringwald                 default:
11647bbeb3adSMilanka Ringwald                     break;
1165aa4dd815SMatthias Ringwald             }
1166aa4dd815SMatthias Ringwald             break;
1167ce263fc8SMatthias Ringwald 
1168ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1169d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1170ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1171d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1172ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
117360ebb071SMilanka Ringwald                             log_info("AG: joining held call with active call");
1174*f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL);
1175d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1176ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1177ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1178ce263fc8SMatthias Ringwald                             break;
1179ce263fc8SMatthias Ringwald                         default:
1180ce263fc8SMatthias Ringwald                             break;
1181ce263fc8SMatthias Ringwald                     }
1182ce263fc8SMatthias Ringwald                     break;
1183ce263fc8SMatthias Ringwald                 default:
1184ce263fc8SMatthias Ringwald                     break;
1185ce263fc8SMatthias Ringwald             }
1186ce263fc8SMatthias Ringwald             break;
1187ce263fc8SMatthias Ringwald 
1188aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1189d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1190aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1191d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1192*f8737b81SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: {
1193*f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL);
1194*f8737b81SMatthias Ringwald                         }
1195d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1196d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1197a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
11986323f788SMatthias Ringwald                             hfp_connection->ok_pending = 1;
119960ebb071SMilanka Ringwald                             log_info("HF answers call, accept call by GSM");
1200ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1201aa4dd815SMatthias Ringwald                             break;
1202aa4dd815SMatthias Ringwald                         default:
1203aa4dd815SMatthias Ringwald                             break;
1204aa4dd815SMatthias Ringwald                     }
12053deb3ec6SMatthias Ringwald                     break;
12063deb3ec6SMatthias Ringwald                 default:
12073deb3ec6SMatthias Ringwald                     break;
12083deb3ec6SMatthias Ringwald             }
12093deb3ec6SMatthias Ringwald             break;
12103deb3ec6SMatthias Ringwald 
1211ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1212d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1213ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1214d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1215*f8737b81SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: {
1216*f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL);
1217*f8737b81SMatthias Ringwald                         }
1218ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1219ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1220a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1221ce263fc8SMatthias Ringwald                             // as with regualr call
1222d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1223d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1224ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
122560ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by AG");
1226ce263fc8SMatthias Ringwald                             break;
1227ce263fc8SMatthias Ringwald                         default:
1228ce263fc8SMatthias Ringwald                             break;
1229ce263fc8SMatthias Ringwald                     }
1230ce263fc8SMatthias Ringwald                     break;
1231ce263fc8SMatthias Ringwald                 default:
1232ce263fc8SMatthias Ringwald                     break;
1233ce263fc8SMatthias Ringwald             }
1234ce263fc8SMatthias Ringwald             break;
1235ce263fc8SMatthias Ringwald 
1236ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1237d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1238ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1239d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1240*f8737b81SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: {
1241*f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL);
1242*f8737b81SMatthias Ringwald                         }
1243ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1244ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1245a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1246ce263fc8SMatthias Ringwald                             // as with regualr call
1247d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1248d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1249a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
125060ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by HF");
1251ce263fc8SMatthias Ringwald                             break;
1252ce263fc8SMatthias Ringwald                         default:
1253ce263fc8SMatthias Ringwald                             break;
1254ce263fc8SMatthias Ringwald                     }
1255ce263fc8SMatthias Ringwald                     break;
1256ce263fc8SMatthias Ringwald                 default:
1257ce263fc8SMatthias Ringwald                     break;
1258ce263fc8SMatthias Ringwald             }
1259ce263fc8SMatthias Ringwald             break;
1260ce263fc8SMatthias Ringwald 
1261ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1262ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1263ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1264ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1265*f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL);
1266a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1267ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1268a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
126960ebb071SMilanka Ringwald             log_info("Held Call accepted and active");
1270ce263fc8SMatthias Ringwald             break;
1271ce263fc8SMatthias Ringwald 
1272ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1273ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1274ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1275ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1276*f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL);
1277a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1278ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1279a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1280ce263fc8SMatthias Ringwald             // from terminate by ag
1281d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1282ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1283ce263fc8SMatthias Ringwald             break;
1284ce263fc8SMatthias Ringwald 
1285aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1286d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1287aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1288d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1289*f8737b81SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: {
1290*f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
1291*f8737b81SMatthias Ringwald                         }
1292d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1293aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1294aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
129560ebb071SMilanka Ringwald                             log_info("HF Rejected Incoming call, AG terminate call");
1296aa4dd815SMatthias Ringwald                             break;
1297aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1298*f8737b81SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE: {
1299*f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
1300*f8737b81SMatthias Ringwald                         }
1301d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1302aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
130360ebb071SMilanka Ringwald                             log_info("AG terminate outgoing call process");
1304202c8a4cSMatthias Ringwald                             break;
1305aa4dd815SMatthias Ringwald                         default:
1306aa4dd815SMatthias Ringwald                             break;
1307aa4dd815SMatthias Ringwald                     }
1308aa4dd815SMatthias Ringwald                     break;
1309*f8737b81SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: {
1310*f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
1311*f8737b81SMatthias Ringwald                 }
1312d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1313aa4dd815SMatthias Ringwald                     hfp_ag_transfer_call_state();
1314a0ffb263SMatthias Ringwald                     hfp_connection->call_state = HFP_CALL_IDLE;
131560ebb071SMilanka Ringwald                     log_info("AG terminate call");
1316aa4dd815SMatthias Ringwald                     break;
13177bbeb3adSMilanka Ringwald                 default:
13187bbeb3adSMilanka Ringwald                     break;
1319aa4dd815SMatthias Ringwald             }
13200c1b8529SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1321aa4dd815SMatthias Ringwald             break;
13223deb3ec6SMatthias Ringwald 
1323aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1324d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1325aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1326d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1327*f8737b81SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS: {
1328*f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL);
1329*f8737b81SMatthias Ringwald                         }
1330d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1331aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
133260ebb071SMilanka Ringwald                             log_info("AG Rejected Incoming call, AG terminate call");
1333aa4dd815SMatthias Ringwald                             break;
1334aa4dd815SMatthias Ringwald                         default:
1335aa4dd815SMatthias Ringwald                             break;
13363deb3ec6SMatthias Ringwald                     }
1337202c8a4cSMatthias Ringwald                     break;
1338*f8737b81SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT: {
1339*f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL);
1340*f8737b81SMatthias Ringwald                 }
1341d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1342d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1343aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
134460ebb071SMilanka Ringwald                     log_info("AG terminate call");
1345aa4dd815SMatthias Ringwald                     break;
1346aa4dd815SMatthias Ringwald                 default:
13473deb3ec6SMatthias Ringwald                     break;
13483deb3ec6SMatthias Ringwald             }
13493deb3ec6SMatthias Ringwald             break;
1350aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1351d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1352aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1353d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1354aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1355aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
135660ebb071SMilanka Ringwald                             log_info("Incoming call interrupted");
1357aa4dd815SMatthias Ringwald                             break;
1358aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1359aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
136060ebb071SMilanka Ringwald                             log_info("Outgoing call interrupted\n");
136160ebb071SMilanka Ringwald                             log_info("AG notify call dropped\n");
1362aa4dd815SMatthias Ringwald                             break;
1363aa4dd815SMatthias Ringwald                         default:
13643deb3ec6SMatthias Ringwald                             break;
13653deb3ec6SMatthias Ringwald                     }
1366*f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1367d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1368aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
136910c236bfSMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1370aa4dd815SMatthias Ringwald                     break;
1371aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1372ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1373*f8737b81SMatthias Ringwald                         hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1374ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1375a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1376ce263fc8SMatthias Ringwald                     }
1377*f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1378d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1379d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1380aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
138160ebb071SMilanka Ringwald                     log_info("AG notify call dropped");
1382aa4dd815SMatthias Ringwald                     break;
1383aa4dd815SMatthias Ringwald                 default:
1384aa4dd815SMatthias Ringwald                     break;
1385aa4dd815SMatthias Ringwald             }
1386aa4dd815SMatthias Ringwald             break;
1387aa4dd815SMatthias Ringwald 
1388aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1389d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1390d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1391a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1392f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1393d210d9c4SMatthias Ringwald                 break;
1394d210d9c4SMatthias Ringwald             }
1395*f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED, 0, 0, (const char *) &hfp_connection->line_buffer[3]);
13969cae807eSMatthias Ringwald 
1397a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
139874386ee0SMatthias Ringwald 
1399ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1400aa4dd815SMatthias Ringwald             break;
1401aa4dd815SMatthias Ringwald 
14029cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1403d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1404d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1405a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1406f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1407d210d9c4SMatthias Ringwald                 break;
1408d210d9c4SMatthias Ringwald             }
1409d210d9c4SMatthias Ringwald 
1410*f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL);
1411a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
141274386ee0SMatthias Ringwald 
141360ebb071SMilanka Ringwald             log_info("Redial last number");
14149cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1415aa4dd815SMatthias Ringwald 
14169cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
141760ebb071SMilanka Ringwald                 log_info("Last number exists: accept call");
1418ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
14199cae807eSMatthias Ringwald             } else {
142060ebb071SMilanka Ringwald                 log_info("log_infoLast number missing: reject call");
142139426febSMatthias Ringwald                 hfp_ag_handle_reject_incoming_call();
14229cae807eSMatthias Ringwald             }
14239cae807eSMatthias Ringwald             break;
14249cae807eSMatthias Ringwald         }
1425aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
142639426febSMatthias Ringwald             hfp_ag_handle_reject_incoming_call();
1427aa4dd815SMatthias Ringwald             break;
1428aa4dd815SMatthias Ringwald 
1429d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1430a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1431a0ffb263SMatthias Ringwald             if (!hfp_connection){
1432a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1433aa4dd815SMatthias Ringwald                 break;
1434aa4dd815SMatthias Ringwald             }
1435aa4dd815SMatthias Ringwald 
1436a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1437a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1438aa4dd815SMatthias Ringwald 
1439aa4dd815SMatthias Ringwald             // trigger callsetup to be
1440d0c20769SMatthias Ringwald             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1441*f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL);
1442d210d9c4SMatthias Ringwald 
1443d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1444aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1445a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1446aa4dd815SMatthias Ringwald 
1447aa4dd815SMatthias Ringwald             // put current call on hold if active
1448d210d9c4SMatthias Ringwald             if (put_call_on_hold){
144960ebb071SMilanka Ringwald                 log_info("AG putting current call on hold for new outgoing calllog_info");
1450d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1451aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1452485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1453aa4dd815SMatthias Ringwald             }
1454aa4dd815SMatthias Ringwald 
1455aa4dd815SMatthias Ringwald             // start audio if needed
1456d97d752dSMilanka Ringwald             hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1457aa4dd815SMatthias Ringwald             break;
1458d210d9c4SMatthias Ringwald         }
1459aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1460a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1461a0ffb263SMatthias Ringwald             if (!hfp_connection){
1462a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1463aa4dd815SMatthias Ringwald                 break;
1464aa4dd815SMatthias Ringwald             }
1465d210d9c4SMatthias Ringwald 
1466*f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL);
1467a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1468d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1469aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1470aa4dd815SMatthias Ringwald             break;
1471aa4dd815SMatthias Ringwald 
1472d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1473aa4dd815SMatthias Ringwald             // get outgoing call
1474a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1475a0ffb263SMatthias Ringwald             if (!hfp_connection){
1476a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1477aa4dd815SMatthias Ringwald             }
1478a0ffb263SMatthias Ringwald             if (!hfp_connection){
1479a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1480aa4dd815SMatthias Ringwald                 break;
1481aa4dd815SMatthias Ringwald             }
1482d210d9c4SMatthias Ringwald 
1483d0c20769SMatthias Ringwald             int CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS = hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS;
1484*f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL);
1485a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1486d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1487d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1488aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1489aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1490d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1491d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1492aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
14933deb3ec6SMatthias Ringwald             }
14943deb3ec6SMatthias Ringwald             break;
1495d210d9c4SMatthias Ringwald         }
1496d210d9c4SMatthias Ringwald 
1497*f8737b81SMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY: {
1498*f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL);
1499*f8737b81SMatthias Ringwald         }
1500d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1501a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1502a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
150360ebb071SMilanka Ringwald             log_info("AG: Call Waiting, User Busy");
1504d210d9c4SMatthias Ringwald             break;
1505d210d9c4SMatthias Ringwald 
1506d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1507d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1508d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1509d210d9c4SMatthias Ringwald 
1510d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1511d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1512*f8737b81SMatthias Ringwald                 hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index,
1513*f8737b81SMatthias Ringwald                                 0, NULL);
1514d0c20769SMatthias Ringwald 
1515d0c20769SMatthias Ringwald             }
1516d0c20769SMatthias Ringwald 
1517d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
151860ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Accept new call");
1519d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1520a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1521d210d9c4SMatthias Ringwald             } else {
152260ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Resume held call");
1523d210d9c4SMatthias Ringwald             }
1524d210d9c4SMatthias Ringwald             if (call_held){
1525d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1526a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1527d210d9c4SMatthias Ringwald             }
1528d0c20769SMatthias Ringwald 
1529a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1530d210d9c4SMatthias Ringwald             break;
1531d210d9c4SMatthias Ringwald         }
1532d0c20769SMatthias Ringwald 
1533d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1534d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1535d210d9c4SMatthias Ringwald             // only update if callsetup changed
1536d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1537*f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0,
1538*f8737b81SMatthias Ringwald                             NULL);
1539d0c20769SMatthias Ringwald 
1540d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
154160ebb071SMilanka Ringwald                 log_info("AG: Call on Hold, Accept new call");
1542d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1543a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1544d210d9c4SMatthias Ringwald             } else {
154560ebb071SMilanka Ringwald                 log_info("AG: Swap calls");
1546d210d9c4SMatthias Ringwald             }
1547d0c20769SMatthias Ringwald 
1548d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1549d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1550a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1551a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1552d210d9c4SMatthias Ringwald             break;
1553d210d9c4SMatthias Ringwald         }
1554d0c20769SMatthias Ringwald 
1555d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1556d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1557d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
155860ebb071SMilanka Ringwald                 log_info("AG: Join 3-way-call");
1559*f8737b81SMatthias Ringwald                 hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL);
1560d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1561a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1562ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1563d210d9c4SMatthias Ringwald             }
1564a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1565d210d9c4SMatthias Ringwald             break;
1566d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1567d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1568*f8737b81SMatthias Ringwald         {
1569*f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL);
1570*f8737b81SMatthias Ringwald         }
157160ebb071SMilanka Ringwald             log_info("AG: Transfer call -> Connect two calls and disconnect");
1572d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1573d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1574a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1575a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1576a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1577d210d9c4SMatthias Ringwald             break;
1578ce263fc8SMatthias Ringwald 
15793deb3ec6SMatthias Ringwald         default:
15803deb3ec6SMatthias Ringwald             break;
15813deb3ec6SMatthias Ringwald     }
1582d210d9c4SMatthias Ringwald 
1583d0c20769SMatthias Ringwald 
15843deb3ec6SMatthias Ringwald }
15853deb3ec6SMatthias Ringwald 
15869cae807eSMatthias Ringwald 
1587a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
15889cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
15899cae807eSMatthias Ringwald     if (!active_call) return;
15909cae807eSMatthias Ringwald 
15919cae807eSMatthias Ringwald     int idx = active_call->index;
15929cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
15939cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
15949cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
15959cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
15969cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
15979cae807eSMatthias Ringwald     char * number = active_call->clip_number;
15989cae807eSMatthias Ringwald 
15999cae807eSMatthias Ringwald     char buffer[100];
16009cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
16019cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
16029cae807eSMatthias Ringwald     if (number){
16031cc1d9e9SMilanka Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
16049cae807eSMatthias Ringwald     }
16059cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
160660ebb071SMilanka Ringwald     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,
16079cae807eSMatthias Ringwald        mode, mpty, type, number);
1608a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
16099cae807eSMatthias Ringwald }
16109cae807eSMatthias Ringwald 
1611473ac565SMatthias Ringwald 
1612febc14f5SMatthias Ringwald // sends pending command, returns if command was sent
1613febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){
161422387625SMatthias Ringwald 
1615a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1616a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1617a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1618a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1619a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
1620febc14f5SMatthias Ringwald             return 1;
16219cae807eSMatthias Ringwald         } else {
1622febc14f5SMatthias Ringwald             // 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
1623a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1624a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1625a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16269cae807eSMatthias Ringwald         }
1627ce263fc8SMatthias Ringwald     }
1628ce263fc8SMatthias Ringwald 
1629a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1630a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1631a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1632febc14f5SMatthias Ringwald         return 1;
1633aa4dd815SMatthias Ringwald     }
1634aa4dd815SMatthias Ringwald 
1635a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1636a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1637a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1638a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1639485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1640febc14f5SMatthias Ringwald         return 1;
1641a0ffb263SMatthias Ringwald     }
1642a0ffb263SMatthias Ringwald 
1643a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1644a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1645a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1646485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1647febc14f5SMatthias Ringwald         return 1;
1648aa4dd815SMatthias Ringwald     }
1649aa4dd815SMatthias Ringwald 
1650ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1651a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1652a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1653a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1654485ac19eSMilanka Ringwald         hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1655febc14f5SMatthias Ringwald         return 1;
1656ce263fc8SMatthias Ringwald     }
1657ce263fc8SMatthias Ringwald 
1658a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1659a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1660a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1661485ac19eSMilanka Ringwald         hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1662febc14f5SMatthias Ringwald         return 1;
1663ce263fc8SMatthias Ringwald     }
1664ce263fc8SMatthias Ringwald 
1665aa4dd815SMatthias Ringwald     // update AG indicators
1666a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1667aa4dd815SMatthias Ringwald         int i;
1668a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1669a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1670a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1671a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1672ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1673ce263fc8SMatthias Ringwald                     break;
1674ce263fc8SMatthias Ringwald                 }
1675485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1676febc14f5SMatthias Ringwald                 return 1;
1677aa4dd815SMatthias Ringwald             }
1678aa4dd815SMatthias Ringwald         }
1679aa4dd815SMatthias Ringwald     }
1680aa4dd815SMatthias Ringwald 
1681a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1682a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1683a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1684485ac19eSMilanka Ringwald         hfp_ag_send_ring(hfp_connection->rfcomm_cid);
1685febc14f5SMatthias Ringwald         return 1;
1686aa4dd815SMatthias Ringwald     }
1687aa4dd815SMatthias Ringwald 
1688a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1689a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1690a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1691a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1692febc14f5SMatthias Ringwald         return 1;
1693aa4dd815SMatthias Ringwald     }
1694aa4dd815SMatthias Ringwald 
1695a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1696a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1697a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1698a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1699a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1700febc14f5SMatthias Ringwald         return 1;
1701aa4dd815SMatthias Ringwald     }
1702aa4dd815SMatthias Ringwald 
1703a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1704a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1705a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1706a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1707ce263fc8SMatthias Ringwald         } else {
1708a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1709a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1710485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1711ce263fc8SMatthias Ringwald         }
1712a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1713febc14f5SMatthias Ringwald         return 1;
1714ce263fc8SMatthias Ringwald     }
1715ce263fc8SMatthias Ringwald 
1716a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1717a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1718a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1719485ac19eSMilanka Ringwald         hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1720febc14f5SMatthias Ringwald         return 1;
1721aa4dd815SMatthias Ringwald     }
1722aa4dd815SMatthias Ringwald 
1723a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1724a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1725a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1726485ac19eSMilanka Ringwald         hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
1727febc14f5SMatthias Ringwald         return 1;
17283deb3ec6SMatthias Ringwald     }
17293deb3ec6SMatthias Ringwald 
1730a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1731a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1732485ac19eSMilanka Ringwald         hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1733febc14f5SMatthias Ringwald         return 1;
1734febc14f5SMatthias Ringwald     }
1735febc14f5SMatthias Ringwald 
1736febc14f5SMatthias Ringwald     return 0;
1737febc14f5SMatthias Ringwald }
1738febc14f5SMatthias Ringwald 
1739febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){
1740febc14f5SMatthias Ringwald 
174176cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
174276cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_AG);
1743febc14f5SMatthias Ringwald 
174476cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
174576cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
1746ce263fc8SMatthias Ringwald 
1747febc14f5SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1748febc14f5SMatthias Ringwald         log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1749febc14f5SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1750febc14f5SMatthias Ringwald         return;
1751febc14f5SMatthias Ringwald     }
1752febc14f5SMatthias Ringwald 
1753febc14f5SMatthias Ringwald     int cmd_sent = hfp_ag_send_commands(hfp_connection);
1754febc14f5SMatthias Ringwald 
17552ee45eb2SMilanka Ringwald     // trigger codec exchange if requested by app
17562ee45eb2SMilanka Ringwald     if (hfp_connection->trigger_codec_exchange){
17572ee45eb2SMilanka Ringwald         log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state);
17582ee45eb2SMilanka Ringwald     }
1759febc14f5SMatthias Ringwald 
1760c1ab6cc1SMatthias Ringwald     if (hfp_connection->trigger_codec_exchange && (hfp_connection->command == HFP_CMD_NONE)){
17612ee45eb2SMilanka Ringwald         switch (hfp_connection->codecs_state){
17622ee45eb2SMilanka Ringwald             case HFP_CODECS_IDLE:
17632ee45eb2SMilanka Ringwald             case HFP_CODECS_RECEIVED_LIST:
17642ee45eb2SMilanka Ringwald             case HFP_CODECS_AG_RESEND_COMMON_CODEC:
17652ee45eb2SMilanka Ringwald             case HFP_CODECS_ERROR:
17662ee45eb2SMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 0;
17672ee45eb2SMilanka Ringwald                 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
17682ee45eb2SMilanka Ringwald                 break;
17692ee45eb2SMilanka Ringwald             default:
17702ee45eb2SMilanka Ringwald                 break;
17712ee45eb2SMilanka Ringwald         }
17722ee45eb2SMilanka Ringwald     }
17732ee45eb2SMilanka Ringwald 
1774febc14f5SMatthias Ringwald     if (!cmd_sent){
1775febc14f5SMatthias Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1776febc14f5SMatthias Ringwald     }
1777febc14f5SMatthias Ringwald 
1778c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1779c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
17803deb3ec6SMatthias Ringwald     }
17813deb3ec6SMatthias Ringwald 
1782c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1783c04cf7ffSMilanka Ringwald         cmd_sent = call_setup_state_machine(hfp_connection);
1784aa4dd815SMatthias Ringwald     }
1785aa4dd815SMatthias Ringwald 
1786c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1787c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
1788aa4dd815SMatthias Ringwald     }
17893deb3ec6SMatthias Ringwald 
1790c1ab6cc1SMatthias Ringwald     if ((hfp_connection->command == HFP_CMD_NONE) && !cmd_sent){
1791a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1792a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
17933deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1794a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1795a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
17963deb3ec6SMatthias Ringwald                 break;
17973deb3ec6SMatthias Ringwald             default:
17983deb3ec6SMatthias Ringwald                 break;
17993deb3ec6SMatthias Ringwald         }
18003deb3ec6SMatthias Ringwald     }
1801c04cf7ffSMilanka Ringwald 
1802c04cf7ffSMilanka Ringwald     if (cmd_sent){
1803a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1804473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1805473ac565SMatthias Ringwald     }
18063deb3ec6SMatthias Ringwald }
1807d68dcce1SMatthias Ringwald 
1808ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1809ce263fc8SMatthias Ringwald     int i;
1810a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1811a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1812ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1813ce263fc8SMatthias Ringwald             return indicator;
1814ce263fc8SMatthias Ringwald         }
1815ce263fc8SMatthias Ringwald     }
1816ce263fc8SMatthias Ringwald     return NULL;
1817ce263fc8SMatthias Ringwald }
18183deb3ec6SMatthias Ringwald 
1819e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1820c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
1821e0d09929SMatthias Ringwald }
1822e0d09929SMatthias Ringwald 
1823e9c22d4eSMatthias Ringwald static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
18248a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
18258a46ec40SMatthias Ringwald 
18268a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
18278a46ec40SMatthias Ringwald     if (size < 1) return;
18289ec2630cSMatthias Ringwald 
1829a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1830a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
18311e35c04dSMatthias Ringwald 
1832186dd3d2SMatthias Ringwald     hfp_log_rfcomm_message("HFP_AG_RX", packet, size);
18331e35c04dSMatthias Ringwald 
18348a46ec40SMatthias Ringwald     // process messages byte-wise
18353deb3ec6SMatthias Ringwald     int pos;
18363deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1837a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1838186dd3d2SMatthias Ringwald 
1839e0d09929SMatthias Ringwald         // parse until end of line
1840e0d09929SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1841e0d09929SMatthias Ringwald 
1842186dd3d2SMatthias Ringwald         hfp_generic_status_indicator_t * indicator;
1843a0ffb263SMatthias Ringwald         switch(hfp_connection->command){
1844ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1845ce263fc8SMatthias Ringwald                 if (hfp_ag_response_and_hold_active){
1846a0ffb263SMatthias Ringwald                     hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1847ce263fc8SMatthias Ringwald                 }
1848a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1849ce263fc8SMatthias Ringwald                 break;
1850ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
18510222a807SMatthias Ringwald                 switch(hfp_connection->ag_response_and_hold_action){
1852ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1853a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1854ce263fc8SMatthias Ringwald                         break;
1855ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1856a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1857ce263fc8SMatthias Ringwald                         break;
1858ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1859a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1860ce263fc8SMatthias Ringwald                         break;
1861ce263fc8SMatthias Ringwald                     default:
1862ce263fc8SMatthias Ringwald                         break;
1863ce263fc8SMatthias Ringwald                 }
1864a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1865ce263fc8SMatthias Ringwald                 break;
1866ce263fc8SMatthias Ringwald             case HFP_CMD_HF_INDICATOR_STATUS:
1867a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1868ce263fc8SMatthias Ringwald                 // find indicator by assigned number
1869a0ffb263SMatthias Ringwald                 indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1870ce263fc8SMatthias Ringwald                 if (!indicator){
1871a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
1872ce263fc8SMatthias Ringwald                     break;
1873ce263fc8SMatthias Ringwald                 }
1874ce263fc8SMatthias Ringwald                 switch (indicator->uuid){
1875ce263fc8SMatthias Ringwald                     case 1: // enhanced security
18760222a807SMatthias Ringwald                         if (hfp_connection->parser_indicator_value > 1) {
1877a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1878ce263fc8SMatthias Ringwald                             return;
1879ce263fc8SMatthias Ringwald                         }
1880e883851fSMatthias Ringwald                         log_info("HF Indicator 'enhanced security' set to %u", (unsigned int) hfp_connection->parser_indicator_value);
1881ce263fc8SMatthias Ringwald                         break;
1882ce263fc8SMatthias Ringwald                     case 2: // battery level
18830222a807SMatthias Ringwald                         if (hfp_connection->parser_indicator_value > 100){
1884a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1885ce263fc8SMatthias Ringwald                             return;
1886ce263fc8SMatthias Ringwald                         }
1887e883851fSMatthias Ringwald                         log_info("HF Indicator 'battery' set to %u", (unsigned int) hfp_connection->parser_indicator_value);
1888ce263fc8SMatthias Ringwald                         break;
1889ce263fc8SMatthias Ringwald                     default:
1890e883851fSMatthias Ringwald                         log_info("HF Indicator unknown set to %u", (unsigned int) hfp_connection->parser_indicator_value);
1891ce263fc8SMatthias Ringwald                         break;
1892ce263fc8SMatthias Ringwald                 }
1893a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1894ce263fc8SMatthias Ringwald                 break;
1895ce263fc8SMatthias Ringwald             case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1896ce263fc8SMatthias Ringwald                 // expected by SLC state machine
1897a0ffb263SMatthias Ringwald                 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1898a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
1899a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_status_indicators = 1;
1900ce263fc8SMatthias Ringwald                 break;
1901ce263fc8SMatthias Ringwald             case HFP_CMD_LIST_CURRENT_CALLS:
1902a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1903a0ffb263SMatthias Ringwald                 hfp_connection->next_call_index = 0;
1904a0ffb263SMatthias Ringwald                 hfp_connection->send_status_of_current_calls = 1;
1905ce263fc8SMatthias Ringwald                 break;
1906ce263fc8SMatthias Ringwald             case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1907ce263fc8SMatthias Ringwald                 if (subscriber_numbers_count == 0){
1908485ac19eSMilanka Ringwald                     hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1909ce263fc8SMatthias Ringwald                     break;
1910ce263fc8SMatthias Ringwald                 }
1911a0ffb263SMatthias Ringwald                 hfp_connection->next_subscriber_number_to_send = 0;
1912a0ffb263SMatthias Ringwald                 hfp_connection->send_subscriber_number = 1;
1913ce263fc8SMatthias Ringwald                 break;
1914c1797c7dSMatthias Ringwald             case HFP_CMD_TRANSMIT_DTMF_CODES:
19150222a807SMatthias Ringwald             {
1916a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
19170222a807SMatthias Ringwald                 char buffer[2];
19180222a807SMatthias Ringwald                 buffer[0] = (char) hfp_connection->ag_dtmf_code;
19190222a807SMatthias Ringwald                 buffer[1] = 0;
19200222a807SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer);
1921c1797c7dSMatthias Ringwald                 break;
19220222a807SMatthias Ringwald             }
1923aa4dd815SMatthias Ringwald             case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1924a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1925ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1926aa4dd815SMatthias Ringwald                 break;
1927aa4dd815SMatthias Ringwald             case HFP_CMD_TURN_OFF_EC_AND_NR:
1928a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1929aa4dd815SMatthias Ringwald                 if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1930a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
1931a0ffb263SMatthias Ringwald                     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
193260ebb071SMilanka Ringwald                     log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction);
1933aa4dd815SMatthias Ringwald                 } else {
1934a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
1935aa4dd815SMatthias Ringwald                 }
1936aa4dd815SMatthias Ringwald                 break;
1937aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_ANSWERED:
1938a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
193960ebb071SMilanka Ringwald                 log_info("HFP: ATA");
1940a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1941aa4dd815SMatthias Ringwald                 break;
1942aa4dd815SMatthias Ringwald             case HFP_CMD_HANG_UP_CALL:
1943a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1944a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1945a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1946aa4dd815SMatthias Ringwald                 break;
1947aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_HOLD: {
1948a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1949a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1950d0c20769SMatthias Ringwald 
19510222a807SMatthias Ringwald                 switch (hfp_connection->ag_call_hold_action){
19520222a807SMatthias Ringwald                     case 0:
1953d0c20769SMatthias Ringwald                         // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1954a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1955aa4dd815SMatthias Ringwald                         break;
19560222a807SMatthias Ringwald                     case 1:
1957d0c20769SMatthias Ringwald                         // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1958d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
1959d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
1960a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1961aa4dd815SMatthias Ringwald                         break;
19620222a807SMatthias Ringwald                     case 2:
1963d0c20769SMatthias Ringwald                         // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1964d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
1965d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
1966a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1967aa4dd815SMatthias Ringwald                         break;
19680222a807SMatthias Ringwald                     case 3:
1969d0c20769SMatthias Ringwald                         // Adds a held call to the conversation.
1970a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1971aa4dd815SMatthias Ringwald                         break;
19720222a807SMatthias Ringwald                     case 4:
1973d0c20769SMatthias Ringwald                         // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1974a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1975aa4dd815SMatthias Ringwald                         break;
1976aa4dd815SMatthias Ringwald                     default:
1977aa4dd815SMatthias Ringwald                         break;
1978aa4dd815SMatthias Ringwald                 }
19790222a807SMatthias Ringwald 				hfp_connection->call_index = 0;
198035e92150SMatthias Ringwald                 break;
1981aa4dd815SMatthias Ringwald             }
1982aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_PHONE_NUMBER:
1983a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1984a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
1985aa4dd815SMatthias Ringwald                 break;
1986aa4dd815SMatthias Ringwald             case HFP_CMD_REDIAL_LAST_NUMBER:
1987a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1988a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
1989aa4dd815SMatthias Ringwald                 break;
1990aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CLIP:
1991a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1992a0ffb263SMatthias Ringwald                 log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
1993a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1994aa4dd815SMatthias Ringwald                 break;
1995aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1996a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1997a0ffb263SMatthias Ringwald                 log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
1998a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1999aa4dd815SMatthias Ringwald                 break;
2000aa4dd815SMatthias Ringwald             case HFP_CMD_SET_SPEAKER_GAIN:
2001a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2002a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
200360ebb071SMilanka Ringwald                 log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
2004aa4dd815SMatthias Ringwald                 break;
2005aa4dd815SMatthias Ringwald             case HFP_CMD_SET_MICROPHONE_GAIN:
2006a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2007a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
200860ebb071SMilanka Ringwald                 log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
2009aa4dd815SMatthias Ringwald                 break;
2010aa4dd815SMatthias Ringwald             default:
2011aa4dd815SMatthias Ringwald                 break;
20123deb3ec6SMatthias Ringwald         }
20133deb3ec6SMatthias Ringwald     }
20140cef86faSMatthias Ringwald }
20153deb3ec6SMatthias Ringwald 
2016aa4dd815SMatthias Ringwald static void hfp_run(void){
2017d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
2018d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2019d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2020d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
202122387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2022f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
20233deb3ec6SMatthias Ringwald     }
20243deb3ec6SMatthias Ringwald }
20253deb3ec6SMatthias Ringwald 
2026e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
20273deb3ec6SMatthias Ringwald     switch (packet_type){
20283deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
2029e9c22d4eSMatthias Ringwald             hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size);
20303deb3ec6SMatthias Ringwald             break;
20313deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2032e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2033e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2034f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2035e30a6a47SMatthias Ringwald                 return;
2036e30a6a47SMatthias Ringwald             }
203727950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2038aa4dd815SMatthias Ringwald             break;
20393deb3ec6SMatthias Ringwald         default:
20403deb3ec6SMatthias Ringwald             break;
20413deb3ec6SMatthias Ringwald     }
20423deb3ec6SMatthias Ringwald 
20433deb3ec6SMatthias Ringwald     hfp_run();
20443deb3ec6SMatthias Ringwald }
20453deb3ec6SMatthias Ringwald 
2046405014fbSMatthias Ringwald static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2047405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
204805bed9f6SMatthias Ringwald 
204905bed9f6SMatthias Ringwald     // allow for sco established -> ring transition
205005bed9f6SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
205105bed9f6SMatthias Ringwald     if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return;
205205bed9f6SMatthias Ringwald     hfp_run();
2053405014fbSMatthias Ringwald }
2054405014fbSMatthias Ringwald 
2055a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
20563deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
20573deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
20583deb3ec6SMatthias Ringwald         return;
20593deb3ec6SMatthias Ringwald     }
20603deb3ec6SMatthias Ringwald     int i;
2061a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
20623deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
20633deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
20643deb3ec6SMatthias Ringwald     }
2065a0ffb263SMatthias Ringwald }
20663deb3ec6SMatthias Ringwald 
2067a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2068a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2069a0ffb263SMatthias Ringwald }
20703deb3ec6SMatthias Ringwald 
2071a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2072a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
20736535961aSMatthias Ringwald     (void)memcpy(hfp_ag_indicators, ag_indicators,
20746535961aSMatthias Ringwald                  ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2075a0ffb263SMatthias Ringwald }
20763deb3ec6SMatthias Ringwald 
2077a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
207825789943SMilanka Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return;
2079a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
20806535961aSMatthias Ringwald     (void)memcpy(hfp_generic_status_indicators, hf_indicators,
20816535961aSMatthias Ringwald                  hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2082a0ffb263SMatthias Ringwald }
2083a0ffb263SMatthias Ringwald 
2084a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
20853deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
20866535961aSMatthias Ringwald     (void)memcpy(hfp_ag_call_hold_services, call_hold_services,
20876535961aSMatthias Ringwald                  call_hold_services_nr * sizeof(char *));
2088a0ffb263SMatthias Ringwald }
2089a0ffb263SMatthias Ringwald 
2090a0ffb263SMatthias Ringwald 
2091a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
209227950165SMatthias Ringwald 
2093520c92d5SMatthias Ringwald     hfp_init();
2094d63c37a1SMatthias Ringwald 
2095405014fbSMatthias Ringwald     hci_event_callback_registration.callback = &hci_event_packet_handler;
209627950165SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
209727950165SMatthias Ringwald 
2098e9c22d4eSMatthias Ringwald     rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
209927950165SMatthias Ringwald 
210027950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
2101e9c22d4eSMatthias Ringwald     hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler);
2102aa4dd815SMatthias Ringwald 
2103d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2104d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2105d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2106d210d9c4SMatthias Ringwald 
2107d210d9c4SMatthias Ringwald     hfp_gsm_init();
21083deb3ec6SMatthias Ringwald }
21093deb3ec6SMatthias Ringwald 
21103deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
2111323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
21123deb3ec6SMatthias Ringwald }
21133deb3ec6SMatthias Ringwald 
2114d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
21159c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2116a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2117a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2118a33eb0c4SMilanka Ringwald         return;
2119a33eb0c4SMilanka Ringwald     }
2120a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2121f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21223deb3ec6SMatthias Ringwald }
21233deb3ec6SMatthias Ringwald 
2124d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
21259c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2126a0ffb263SMatthias Ringwald     if (!hfp_connection){
2127a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
21283deb3ec6SMatthias Ringwald         return;
21293deb3ec6SMatthias Ringwald     }
2130a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2131a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
21323deb3ec6SMatthias Ringwald         return;
21333deb3ec6SMatthias Ringwald     }
2134a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2135f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21363deb3ec6SMatthias Ringwald }
21373deb3ec6SMatthias Ringwald 
2138a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
21392ee45eb2SMilanka Ringwald     log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state);
2140a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2141a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
21423deb3ec6SMatthias Ringwald 
2143a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2144a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2145d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2146d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2147a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
21487522e673SMatthias Ringwald         // now, pick link settings
2149afac2a04SMilanka Ringwald         hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
21502ee45eb2SMilanka Ringwald         return;
21513deb3ec6SMatthias Ringwald     }
2152aa4dd815SMatthias Ringwald 
21532ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 1;
2154aa4dd815SMatthias Ringwald }
2155aa4dd815SMatthias Ringwald 
2156d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
21579c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2158a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2159a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2160a33eb0c4SMilanka Ringwald         return;
2161a33eb0c4SMilanka Ringwald     }
21622ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 0;
2163a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2164a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2165f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21663deb3ec6SMatthias Ringwald }
21673deb3ec6SMatthias Ringwald 
2168d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
21699c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2170a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2171a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2172a33eb0c4SMilanka Ringwald         return;
2173a33eb0c4SMilanka Ringwald     }
2174a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2175f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21763deb3ec6SMatthias Ringwald }
2177aa4dd815SMatthias Ringwald 
2178aa4dd815SMatthias Ringwald /**
2179aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2180aa4dd815SMatthias Ringwald  */
2181aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2182aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2183aa4dd815SMatthias Ringwald         return;
2184aa4dd815SMatthias Ringwald     }
2185aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2186aa4dd815SMatthias Ringwald 
2187665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2188665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2189665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2190a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
219166c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2192a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2193f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2194aa4dd815SMatthias Ringwald     }
2195aa4dd815SMatthias Ringwald }
2196aa4dd815SMatthias Ringwald 
2197aa4dd815SMatthias Ringwald /**
2198aa4dd815SMatthias Ringwald  * @brief Called from GSM
2199aa4dd815SMatthias Ringwald  */
2200aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2201aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2202aa4dd815SMatthias Ringwald }
2203aa4dd815SMatthias Ringwald 
2204aa4dd815SMatthias Ringwald /**
2205aa4dd815SMatthias Ringwald  * @brief number is stored.
2206aa4dd815SMatthias Ringwald  */
2207aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2208*f8737b81SMatthias Ringwald     hfp_gsm_handler(HFP_AG_SET_CLIP, 0, type, number);
2209aa4dd815SMatthias Ringwald }
2210aa4dd815SMatthias Ringwald 
2211aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2212aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2213aa4dd815SMatthias Ringwald }
2214aa4dd815SMatthias Ringwald 
2215aa4dd815SMatthias Ringwald // call from AG UI
2216aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2217aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2218aa4dd815SMatthias Ringwald }
2219aa4dd815SMatthias Ringwald 
2220ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2221ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2222ce263fc8SMatthias Ringwald }
2223ce263fc8SMatthias Ringwald 
2224aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2225aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2226aa4dd815SMatthias Ringwald }
2227aa4dd815SMatthias Ringwald 
2228aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2229aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2230aa4dd815SMatthias Ringwald }
2231aa4dd815SMatthias Ringwald 
2232aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2233aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2234aa4dd815SMatthias Ringwald }
2235aa4dd815SMatthias Ringwald 
2236aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2237aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2238aa4dd815SMatthias Ringwald }
2239aa4dd815SMatthias Ringwald 
2240aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2241aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2242aa4dd815SMatthias Ringwald }
2243ce263fc8SMatthias Ringwald 
2244ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2245ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2246ce263fc8SMatthias Ringwald }
2247ce263fc8SMatthias Ringwald 
2248ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2249ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2250ce263fc8SMatthias Ringwald }
2251ce263fc8SMatthias Ringwald 
2252ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2253ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2254ce263fc8SMatthias Ringwald }
2255ce263fc8SMatthias Ringwald 
2256aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2257aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2258aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2259aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2260aa4dd815SMatthias Ringwald 
2261ce263fc8SMatthias Ringwald 
2262665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2263665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2264665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2265a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
226666c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2267a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2268ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2269ce263fc8SMatthias Ringwald             continue;
2270ce263fc8SMatthias Ringwald         }
2271ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2272a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2273f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2274aa4dd815SMatthias Ringwald     }
2275aa4dd815SMatthias Ringwald }
2276aa4dd815SMatthias Ringwald 
2277aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2278aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2279aa4dd815SMatthias Ringwald }
2280aa4dd815SMatthias Ringwald 
2281aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2282aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2283aa4dd815SMatthias Ringwald }
2284aa4dd815SMatthias Ringwald 
2285aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2286aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2287aa4dd815SMatthias Ringwald }
2288aa4dd815SMatthias Ringwald 
2289aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2290aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2291aa4dd815SMatthias Ringwald }
2292aa4dd815SMatthias Ringwald 
2293d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){
2294aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
22959c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2296a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2297a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2298a33eb0c4SMilanka Ringwald         return;
2299a33eb0c4SMilanka Ringwald     }
2300aa4dd815SMatthias Ringwald 
2301a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
230260ebb071SMilanka Ringwald         log_info("AG cannot acivate voice recognition - not supported by HF");
2303aa4dd815SMatthias Ringwald         return;
2304aa4dd815SMatthias Ringwald     }
2305aa4dd815SMatthias Ringwald 
2306aa4dd815SMatthias Ringwald     if (activate){
2307d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(acl_handle);
2308aa4dd815SMatthias Ringwald     }
2309aa4dd815SMatthias Ringwald 
2310a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2311a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2312f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2313aa4dd815SMatthias Ringwald }
2314aa4dd815SMatthias Ringwald 
2315d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
23169c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2317a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2318a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2319a33eb0c4SMilanka Ringwald         return;
2320a33eb0c4SMilanka Ringwald     }
2321a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2322a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2323a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2324a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2325aa4dd815SMatthias Ringwald     }
2326f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2327aa4dd815SMatthias Ringwald }
2328aa4dd815SMatthias Ringwald 
2329d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
23309c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2331a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2332a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2333a33eb0c4SMilanka Ringwald         return;
2334a33eb0c4SMilanka Ringwald     }
2335a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2336a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2337a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2338aa4dd815SMatthias Ringwald     }
2339f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2340aa4dd815SMatthias Ringwald }
2341aa4dd815SMatthias Ringwald 
2342d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
23439c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2344a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2345a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2346a33eb0c4SMilanka Ringwald         return;
2347a33eb0c4SMilanka Ringwald     }
2348aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2349a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2350aa4dd815SMatthias Ringwald }
2351aa4dd815SMatthias Ringwald 
2352d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
23539c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2354a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2355a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2356a33eb0c4SMilanka Ringwald         return;
2357a33eb0c4SMilanka Ringwald     }
2358a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2359aa4dd815SMatthias Ringwald }
2360aa4dd815SMatthias Ringwald 
2361d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
23629c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2363a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2364a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2365a33eb0c4SMilanka Ringwald         return;
2366a33eb0c4SMilanka Ringwald     }
2367a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2368c1797c7dSMatthias Ringwald }
2369aa4dd815SMatthias Ringwald 
2370ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2371ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2372ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2373ce263fc8SMatthias Ringwald }
2374ce263fc8SMatthias Ringwald 
23759cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
23769cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2377ce263fc8SMatthias Ringwald }
2378ce263fc8SMatthias Ringwald 
2379d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
23809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2381a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2382a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2383a33eb0c4SMilanka Ringwald         return;
2384a33eb0c4SMilanka Ringwald     }
2385a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2386a0ffb263SMatthias Ringwald 
2387a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2388f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2389a0ffb263SMatthias Ringwald }
2390ce263fc8SMatthias Ringwald 
239176cc1527SMatthias Ringwald 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){
239276cc1527SMatthias Ringwald 	if (!name){
239376cc1527SMatthias Ringwald 		name = default_hfp_ag_service_name;
239476cc1527SMatthias Ringwald 	}
239576cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
239676cc1527SMatthias Ringwald 
239776cc1527SMatthias Ringwald 	/*
239876cc1527SMatthias Ringwald 	 * 0x01 – Ability to reject a call
239976cc1527SMatthias Ringwald 	 * 0x00 – No ability to reject a call
240076cc1527SMatthias Ringwald 	 */
240176cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
240276cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
240376cc1527SMatthias Ringwald 
240476cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
240576cc1527SMatthias Ringwald 	//
240676cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
240776cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
240876cc1527SMatthias Ringwald 	//
240976cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
241076cc1527SMatthias Ringwald 	//
241176cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
241276cc1527SMatthias Ringwald 	if (wide_band_speech && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){
241376cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
241476cc1527SMatthias Ringwald 	}
241576cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
241676cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
241776cc1527SMatthias Ringwald }
241876cc1527SMatthias Ringwald 
241976cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
242076cc1527SMatthias Ringwald 	if (callback == NULL){
242176cc1527SMatthias Ringwald 		log_error("hfp_ag_register_packet_handler called with NULL callback");
242276cc1527SMatthias Ringwald 		return;
242376cc1527SMatthias Ringwald 	}
242476cc1527SMatthias Ringwald 	hfp_ag_callback = callback;
242576cc1527SMatthias Ringwald 	hfp_set_ag_callback(callback);
242676cc1527SMatthias Ringwald }
2427