xref: /btstack/src/classic/hfp_ag.c (revision 1ec112deb972d04575b0fada678da234fad428a7)
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 
821c6a0fc0SMatthias Ringwald static btstack_packet_callback_registration_t hfp_ag_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 
8534a2e058fSMatthias Ringwald static void hfp_ag_transfer_indicator(const char * name){
8544a2e058fSMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
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 
8684a2e058fSMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
8694a2e058fSMatthias Ringwald     hfp_ag_transfer_indicator("callsetup");
8704a2e058fSMatthias Ringwald }
8714a2e058fSMatthias Ringwald 
872aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
8734a2e058fSMatthias Ringwald     hfp_ag_transfer_indicator("call");
874aa4dd815SMatthias Ringwald }
875aa4dd815SMatthias Ringwald 
876aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
8774a2e058fSMatthias Ringwald     hfp_ag_transfer_indicator("callheld");
878aa4dd815SMatthias Ringwald }
879aa4dd815SMatthias Ringwald 
880aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
881aa4dd815SMatthias Ringwald 
882aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
883aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
884aa4dd815SMatthias Ringwald 
885665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
886665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
887665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
888a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
88966c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
890505f1c30SMatthias Ringwald         if ((hfp_connection->call_state != HFP_CALL_RINGING) &&
891505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
892aa4dd815SMatthias Ringwald 
893a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
894a0ffb263SMatthias Ringwald         if (hfp_connection == source){
895aa4dd815SMatthias Ringwald 
896aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
897a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
898aa4dd815SMatthias Ringwald             } else {
899a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
900d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
901aa4dd815SMatthias Ringwald             }
902aa4dd815SMatthias Ringwald 
903a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
904a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
905aa4dd815SMatthias Ringwald 
906aa4dd815SMatthias Ringwald         } else {
907a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
908aa4dd815SMatthias Ringwald         }
909f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
910aa4dd815SMatthias Ringwald     }
911aa4dd815SMatthias Ringwald }
912aa4dd815SMatthias Ringwald 
913aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
914aa4dd815SMatthias Ringwald 
915aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
916aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
917aa4dd815SMatthias Ringwald 
918665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
919665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
920665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
921a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
92266c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
923a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
924aa4dd815SMatthias Ringwald 
925a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
926a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
927aa4dd815SMatthias Ringwald 
928a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
929a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
930aa4dd815SMatthias Ringwald 
931f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
932aa4dd815SMatthias Ringwald         break;  // only single
933aa4dd815SMatthias Ringwald     }
934aa4dd815SMatthias Ringwald }
935aa4dd815SMatthias Ringwald 
936aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
937aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
938665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
939665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
940665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
941665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
94266c5995fSMatthias Ringwald         if (connection->local_role != HFP_ROLE_AG) continue;
943505f1c30SMatthias Ringwald         if ((connection->call_state != HFP_CALL_RINGING) &&
944505f1c30SMatthias Ringwald             (connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
945aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
946aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
947aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
948f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(connection);
949aa4dd815SMatthias Ringwald     }
950aa4dd815SMatthias Ringwald }
951aa4dd815SMatthias Ringwald 
952aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
953aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
954aa4dd815SMatthias Ringwald 
955665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
956665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
957665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
958a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
95966c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
960d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
961a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
962a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
963a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
9646d78145cSMatthias Ringwald         if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
965a0ffb263SMatthias Ringwald             hfp_connection->release_audio_connection = 1;
9666d78145cSMatthias Ringwald         }
967f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
968aa4dd815SMatthias Ringwald     }
969ca59be51SMatthias Ringwald     hfp_ag_emit_simple_event(HFP_SUBEVENT_CALL_TERMINATED);
970aa4dd815SMatthias Ringwald }
971aa4dd815SMatthias Ringwald 
9720cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
973aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
974aa4dd815SMatthias Ringwald     if (!indicator){
975d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
97645718b6fSMatthias Ringwald         return;
977aa4dd815SMatthias Ringwald     };
978d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
979aa4dd815SMatthias Ringwald }
980aa4dd815SMatthias Ringwald 
9810cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
982d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
983d210d9c4SMatthias Ringwald     if (!indicator){
984d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
98545718b6fSMatthias Ringwald         return;
986d210d9c4SMatthias Ringwald     };
987d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
988d210d9c4SMatthias Ringwald }
989d210d9c4SMatthias Ringwald 
9900cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
991aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
992aa4dd815SMatthias Ringwald     if (!indicator){
993aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
99445718b6fSMatthias Ringwald         return;
995aa4dd815SMatthias Ringwald     };
996d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
997aa4dd815SMatthias Ringwald }
998aa4dd815SMatthias Ringwald 
999aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1000665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1001665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1002665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1003a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
100466c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1005505f1c30SMatthias Ringwald         if ((hfp_connection->call_state != HFP_CALL_RINGING) &&
1006505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1007a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1008aa4dd815SMatthias Ringwald     }
1009aa4dd815SMatthias Ringwald }
1010aa4dd815SMatthias Ringwald 
1011aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1012665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1013665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1014665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1015a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
101666c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1017a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1018aa4dd815SMatthias Ringwald     }
1019aa4dd815SMatthias Ringwald     return NULL;
1020aa4dd815SMatthias Ringwald }
1021aa4dd815SMatthias Ringwald 
1022a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1023665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1024665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1025665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1026a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
102766c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1028a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1029ce263fc8SMatthias Ringwald     }
1030ce263fc8SMatthias Ringwald }
1031ce263fc8SMatthias Ringwald 
1032a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1033aa4dd815SMatthias Ringwald     int indicator_index;
1034a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1035aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1036a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1037a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1038a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1039a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1040a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1041a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1042ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
1043aa4dd815SMatthias Ringwald             break;
1044aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1045a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1046a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1047a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1048aa4dd815SMatthias Ringwald             break;
1049aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1050a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1051a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1052aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1053a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1054aa4dd815SMatthias Ringwald             break;
1055aa4dd815SMatthias Ringwald         default:
1056aa4dd815SMatthias Ringwald             break;
1057aa4dd815SMatthias Ringwald     }
1058aa4dd815SMatthias Ringwald     return 0;
1059aa4dd815SMatthias Ringwald }
106039426febSMatthias Ringwald 
106139426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below
106239426febSMatthias Ringwald static void hfp_ag_handle_reject_incoming_call(void){
106339426febSMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
106439426febSMatthias Ringwald     if (!hfp_connection){
106539426febSMatthias Ringwald         log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
106639426febSMatthias Ringwald         return;
106739426febSMatthias Ringwald     }
106839426febSMatthias Ringwald 
1069f8737b81SMatthias Ringwald     hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL);
107039426febSMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
107139426febSMatthias Ringwald     hfp_connection->send_error = 1;
107239426febSMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
107339426febSMatthias Ringwald }
107439426febSMatthias Ringwald 
1075a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1076a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1077aa4dd815SMatthias Ringwald     int indicator_index;
1078d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1079d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1080d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
108174386ee0SMatthias Ringwald 
1082aa4dd815SMatthias Ringwald     switch (event){
1083aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1084d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1085aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1086d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
108712cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1088f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL);
1089d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1090aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
109160ebb071SMilanka Ringwald                             log_info("AG rings");
1092aa4dd815SMatthias Ringwald                             break;
1093aa4dd815SMatthias Ringwald                         default:
10943deb3ec6SMatthias Ringwald                             break;
10953deb3ec6SMatthias Ringwald                     }
10963deb3ec6SMatthias Ringwald                     break;
1097aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1098d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
109912cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1100f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL);
1101d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1102aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
110360ebb071SMilanka Ringwald                             log_info("AG call waiting");
1104aa4dd815SMatthias Ringwald                             break;
1105aa4dd815SMatthias Ringwald                         default:
11063deb3ec6SMatthias Ringwald                             break;
11073deb3ec6SMatthias Ringwald                     }
11083deb3ec6SMatthias Ringwald                     break;
11097bbeb3adSMilanka Ringwald                 default:
11107bbeb3adSMilanka Ringwald                     break;
1111aa4dd815SMatthias Ringwald             }
1112aa4dd815SMatthias Ringwald             break;
1113aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1114d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1115aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1116d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
111712cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1118f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL);
1119d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1120d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1121aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
112260ebb071SMilanka Ringwald                             log_info("AG answers call, accept call by GSM");
1123aa4dd815SMatthias Ringwald                             break;
1124aa4dd815SMatthias Ringwald                         default:
11253deb3ec6SMatthias Ringwald                             break;
11263deb3ec6SMatthias Ringwald                     }
1127aa4dd815SMatthias Ringwald                     break;
1128aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1129d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1130aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
113160ebb071SMilanka Ringwald                             log_info("AG: current call is placed on hold, incoming call gets active");
1132f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL);
1133d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1134d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1135ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1136ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1137aa4dd815SMatthias Ringwald                             break;
1138aa4dd815SMatthias Ringwald                         default:
1139aa4dd815SMatthias Ringwald                             break;
1140aa4dd815SMatthias Ringwald                     }
1141aa4dd815SMatthias Ringwald                     break;
11427bbeb3adSMilanka Ringwald                 default:
11437bbeb3adSMilanka Ringwald                     break;
1144aa4dd815SMatthias Ringwald             }
1145aa4dd815SMatthias Ringwald             break;
1146ce263fc8SMatthias Ringwald 
1147ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1148d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1149ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1150d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1151ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
115260ebb071SMilanka Ringwald                             log_info("AG: joining held call with active call");
1153f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL);
1154d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1155ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1156ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1157ce263fc8SMatthias Ringwald                             break;
1158ce263fc8SMatthias Ringwald                         default:
1159ce263fc8SMatthias Ringwald                             break;
1160ce263fc8SMatthias Ringwald                     }
1161ce263fc8SMatthias Ringwald                     break;
1162ce263fc8SMatthias Ringwald                 default:
1163ce263fc8SMatthias Ringwald                     break;
1164ce263fc8SMatthias Ringwald             }
1165ce263fc8SMatthias Ringwald             break;
1166ce263fc8SMatthias Ringwald 
1167aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1168d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1169aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1170d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
117112cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1172f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL);
1173d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1174d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1175a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
11766323f788SMatthias Ringwald                             hfp_connection->ok_pending = 1;
117760ebb071SMilanka Ringwald                             log_info("HF answers call, accept call by GSM");
1178ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1179aa4dd815SMatthias Ringwald                             break;
1180aa4dd815SMatthias Ringwald                         default:
1181aa4dd815SMatthias Ringwald                             break;
1182aa4dd815SMatthias Ringwald                     }
11833deb3ec6SMatthias Ringwald                     break;
11843deb3ec6SMatthias Ringwald                 default:
11853deb3ec6SMatthias Ringwald                     break;
11863deb3ec6SMatthias Ringwald             }
11873deb3ec6SMatthias Ringwald             break;
11883deb3ec6SMatthias Ringwald 
1189ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1190d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1191ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1192d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
119312cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1194f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL);
1195ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1196ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1197a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1198ce263fc8SMatthias Ringwald                             // as with regualr call
1199d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1200d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1201ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
120260ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by AG");
1203ce263fc8SMatthias Ringwald                             break;
1204ce263fc8SMatthias Ringwald                         default:
1205ce263fc8SMatthias Ringwald                             break;
1206ce263fc8SMatthias Ringwald                     }
1207ce263fc8SMatthias Ringwald                     break;
1208ce263fc8SMatthias Ringwald                 default:
1209ce263fc8SMatthias Ringwald                     break;
1210ce263fc8SMatthias Ringwald             }
1211ce263fc8SMatthias Ringwald             break;
1212ce263fc8SMatthias Ringwald 
1213ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1214d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1215ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1216d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
121712cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1218f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL);
1219ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1220ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1221a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1222ce263fc8SMatthias Ringwald                             // as with regualr call
1223d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1224d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1225a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
122660ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by HF");
1227ce263fc8SMatthias Ringwald                             break;
1228ce263fc8SMatthias Ringwald                         default:
1229ce263fc8SMatthias Ringwald                             break;
1230ce263fc8SMatthias Ringwald                     }
1231ce263fc8SMatthias Ringwald                     break;
1232ce263fc8SMatthias Ringwald                 default:
1233ce263fc8SMatthias Ringwald                     break;
1234ce263fc8SMatthias Ringwald             }
1235ce263fc8SMatthias Ringwald             break;
1236ce263fc8SMatthias Ringwald 
1237ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1238ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1239ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1240ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1241f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL);
1242a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1243ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1244a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
124560ebb071SMilanka Ringwald             log_info("Held Call accepted and active");
1246ce263fc8SMatthias Ringwald             break;
1247ce263fc8SMatthias Ringwald 
1248ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1249ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1250ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1251ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1252f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL);
1253a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1254ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1255a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1256ce263fc8SMatthias Ringwald             // from terminate by ag
1257d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1258ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1259ce263fc8SMatthias Ringwald             break;
1260ce263fc8SMatthias Ringwald 
1261aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1262d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1263aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1264d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
126512cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1266f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
1267d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1268aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1269aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
127060ebb071SMilanka Ringwald                             log_info("HF Rejected Incoming call, AG terminate call");
1271aa4dd815SMatthias Ringwald                             break;
1272aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
127312cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1274f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
1275d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1276aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
127760ebb071SMilanka Ringwald                             log_info("AG terminate outgoing call process");
1278202c8a4cSMatthias Ringwald                             break;
1279aa4dd815SMatthias Ringwald                         default:
1280aa4dd815SMatthias Ringwald                             break;
1281aa4dd815SMatthias Ringwald                     }
1282aa4dd815SMatthias Ringwald                     break;
128312cbbeeeSMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1284f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
1285*1ec112deSMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1286d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1287*1ec112deSMatthias Ringwald                     hfp_ag_trigger_terminate_call();
128860ebb071SMilanka Ringwald                     log_info("AG terminate call");
1289aa4dd815SMatthias Ringwald                     break;
12907bbeb3adSMilanka Ringwald                 default:
12917bbeb3adSMilanka Ringwald                     break;
1292aa4dd815SMatthias Ringwald             }
12930c1b8529SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1294aa4dd815SMatthias Ringwald             break;
12953deb3ec6SMatthias Ringwald 
1296aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1297d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1298aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1299d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
130012cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1301f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL);
1302d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1303aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
130460ebb071SMilanka Ringwald                             log_info("AG Rejected Incoming call, AG terminate call");
1305aa4dd815SMatthias Ringwald                             break;
1306aa4dd815SMatthias Ringwald                         default:
1307aa4dd815SMatthias Ringwald                             break;
13083deb3ec6SMatthias Ringwald                     }
1309202c8a4cSMatthias Ringwald                     break;
131012cbbeeeSMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1311f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL);
1312d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1313d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1314aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
131560ebb071SMilanka Ringwald                     log_info("AG terminate call");
1316aa4dd815SMatthias Ringwald                     break;
1317aa4dd815SMatthias Ringwald                 default:
13183deb3ec6SMatthias Ringwald                     break;
13193deb3ec6SMatthias Ringwald             }
13203deb3ec6SMatthias Ringwald             break;
1321aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1322d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1323aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1324d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1325aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1326aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
132760ebb071SMilanka Ringwald                             log_info("Incoming call interrupted");
1328aa4dd815SMatthias Ringwald                             break;
1329aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1330aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
133160ebb071SMilanka Ringwald                             log_info("Outgoing call interrupted\n");
133260ebb071SMilanka Ringwald                             log_info("AG notify call dropped\n");
1333aa4dd815SMatthias Ringwald                             break;
1334aa4dd815SMatthias Ringwald                         default:
13353deb3ec6SMatthias Ringwald                             break;
13363deb3ec6SMatthias Ringwald                     }
1337f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1338d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1339aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
134010c236bfSMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1341aa4dd815SMatthias Ringwald                     break;
1342aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1343ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1344f8737b81SMatthias Ringwald                         hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1345ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1346a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1347ce263fc8SMatthias Ringwald                     }
1348f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1349d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1350d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1351aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
135260ebb071SMilanka Ringwald                     log_info("AG notify call dropped");
1353aa4dd815SMatthias Ringwald                     break;
1354aa4dd815SMatthias Ringwald                 default:
1355aa4dd815SMatthias Ringwald                     break;
1356aa4dd815SMatthias Ringwald             }
1357aa4dd815SMatthias Ringwald             break;
1358aa4dd815SMatthias Ringwald 
1359aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1360d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1361d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1362a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1363f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1364d210d9c4SMatthias Ringwald                 break;
1365d210d9c4SMatthias Ringwald             }
1366f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED, 0, 0, (const char *) &hfp_connection->line_buffer[3]);
13679cae807eSMatthias Ringwald 
1368a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
136974386ee0SMatthias Ringwald 
1370ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1371aa4dd815SMatthias Ringwald             break;
1372aa4dd815SMatthias Ringwald 
13739cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1374d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1375d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1376a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1377f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1378d210d9c4SMatthias Ringwald                 break;
1379d210d9c4SMatthias Ringwald             }
1380d210d9c4SMatthias Ringwald 
1381f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL);
1382a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
138374386ee0SMatthias Ringwald 
138460ebb071SMilanka Ringwald             log_info("Redial last number");
13859cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1386aa4dd815SMatthias Ringwald 
13879cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
138860ebb071SMilanka Ringwald                 log_info("Last number exists: accept call");
1389ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
13909cae807eSMatthias Ringwald             } else {
139160ebb071SMilanka Ringwald                 log_info("log_infoLast number missing: reject call");
139239426febSMatthias Ringwald                 hfp_ag_handle_reject_incoming_call();
13939cae807eSMatthias Ringwald             }
13949cae807eSMatthias Ringwald             break;
13959cae807eSMatthias Ringwald         }
1396aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
139739426febSMatthias Ringwald             hfp_ag_handle_reject_incoming_call();
1398aa4dd815SMatthias Ringwald             break;
1399aa4dd815SMatthias Ringwald 
1400d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1401a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1402a0ffb263SMatthias Ringwald             if (!hfp_connection){
1403a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1404aa4dd815SMatthias Ringwald                 break;
1405aa4dd815SMatthias Ringwald             }
1406aa4dd815SMatthias Ringwald 
1407a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1408a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1409aa4dd815SMatthias Ringwald 
1410aa4dd815SMatthias Ringwald             // trigger callsetup to be
1411d0c20769SMatthias Ringwald             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1412f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL);
1413d210d9c4SMatthias Ringwald 
1414d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1415aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1416a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1417aa4dd815SMatthias Ringwald 
1418aa4dd815SMatthias Ringwald             // put current call on hold if active
1419d210d9c4SMatthias Ringwald             if (put_call_on_hold){
142060ebb071SMilanka Ringwald                 log_info("AG putting current call on hold for new outgoing calllog_info");
1421d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1422aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1423485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1424aa4dd815SMatthias Ringwald             }
1425aa4dd815SMatthias Ringwald 
1426aa4dd815SMatthias Ringwald             // start audio if needed
1427d97d752dSMilanka Ringwald             hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1428aa4dd815SMatthias Ringwald             break;
1429d210d9c4SMatthias Ringwald         }
1430aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1431a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1432a0ffb263SMatthias Ringwald             if (!hfp_connection){
1433a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1434aa4dd815SMatthias Ringwald                 break;
1435aa4dd815SMatthias Ringwald             }
1436d210d9c4SMatthias Ringwald 
1437f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL);
1438a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1439d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1440aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1441aa4dd815SMatthias Ringwald             break;
1442aa4dd815SMatthias Ringwald 
1443d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1444aa4dd815SMatthias Ringwald             // get outgoing call
1445a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1446a0ffb263SMatthias Ringwald             if (!hfp_connection){
1447a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1448aa4dd815SMatthias Ringwald             }
1449a0ffb263SMatthias Ringwald             if (!hfp_connection){
1450a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1451aa4dd815SMatthias Ringwald                 break;
1452aa4dd815SMatthias Ringwald             }
1453d210d9c4SMatthias Ringwald 
1454d0c20769SMatthias 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;
1455f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL);
1456a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1457d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1458d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1459aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1460aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1461d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1462d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1463aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
14643deb3ec6SMatthias Ringwald             }
14653deb3ec6SMatthias Ringwald             break;
1466d210d9c4SMatthias Ringwald         }
1467d210d9c4SMatthias Ringwald 
146812cbbeeeSMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1469f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL);
1470d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1471a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1472a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
147360ebb071SMilanka Ringwald             log_info("AG: Call Waiting, User Busy");
1474d210d9c4SMatthias Ringwald             break;
1475d210d9c4SMatthias Ringwald 
1476d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1477d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1478d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1479d210d9c4SMatthias Ringwald 
1480d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1481d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1482f8737b81SMatthias Ringwald                 hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index,
1483f8737b81SMatthias Ringwald                                 0, NULL);
1484d0c20769SMatthias Ringwald 
1485d0c20769SMatthias Ringwald             }
1486d0c20769SMatthias Ringwald 
1487d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
148860ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Accept new call");
1489d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1490a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1491d210d9c4SMatthias Ringwald             } else {
149260ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Resume held call");
1493d210d9c4SMatthias Ringwald             }
1494d210d9c4SMatthias Ringwald             if (call_held){
1495d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1496a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1497d210d9c4SMatthias Ringwald             }
1498d0c20769SMatthias Ringwald 
1499a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1500d210d9c4SMatthias Ringwald             break;
1501d210d9c4SMatthias Ringwald         }
1502d0c20769SMatthias Ringwald 
1503d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1504d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1505d210d9c4SMatthias Ringwald             // only update if callsetup changed
1506d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1507f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0,
1508f8737b81SMatthias Ringwald                             NULL);
1509d0c20769SMatthias Ringwald 
1510d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
151160ebb071SMilanka Ringwald                 log_info("AG: Call on Hold, Accept new call");
1512d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1513a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1514d210d9c4SMatthias Ringwald             } else {
151560ebb071SMilanka Ringwald                 log_info("AG: Swap calls");
1516d210d9c4SMatthias Ringwald             }
1517d0c20769SMatthias Ringwald 
1518d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1519d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1520a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1521a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1522d210d9c4SMatthias Ringwald             break;
1523d210d9c4SMatthias Ringwald         }
1524d0c20769SMatthias Ringwald 
1525d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1526d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1527d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
152860ebb071SMilanka Ringwald                 log_info("AG: Join 3-way-call");
1529f8737b81SMatthias Ringwald                 hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL);
1530d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1531a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1532ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1533d210d9c4SMatthias Ringwald             }
1534a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1535d210d9c4SMatthias Ringwald             break;
1536d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1537d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1538f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL);
153960ebb071SMilanka Ringwald             log_info("AG: Transfer call -> Connect two calls and disconnect");
1540d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1541d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1542a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1543a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1544a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1545d210d9c4SMatthias Ringwald             break;
1546ce263fc8SMatthias Ringwald 
15473deb3ec6SMatthias Ringwald         default:
15483deb3ec6SMatthias Ringwald             break;
15493deb3ec6SMatthias Ringwald     }
1550d210d9c4SMatthias Ringwald 
1551d0c20769SMatthias Ringwald 
15523deb3ec6SMatthias Ringwald }
15533deb3ec6SMatthias Ringwald 
15549cae807eSMatthias Ringwald 
1555a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
15569cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
15579cae807eSMatthias Ringwald     if (!active_call) return;
15589cae807eSMatthias Ringwald 
15599cae807eSMatthias Ringwald     int idx = active_call->index;
15609cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
15619cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
15629cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
15639cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
15649cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
15659cae807eSMatthias Ringwald     char * number = active_call->clip_number;
15669cae807eSMatthias Ringwald 
15679cae807eSMatthias Ringwald     char buffer[100];
15689cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
15699cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
15709cae807eSMatthias Ringwald     if (number){
15711cc1d9e9SMilanka Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
15729cae807eSMatthias Ringwald     }
15739cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
157460ebb071SMilanka 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,
15759cae807eSMatthias Ringwald        mode, mpty, type, number);
1576a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
15779cae807eSMatthias Ringwald }
15789cae807eSMatthias Ringwald 
1579473ac565SMatthias Ringwald 
1580febc14f5SMatthias Ringwald // sends pending command, returns if command was sent
1581febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){
158222387625SMatthias Ringwald 
1583a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1584a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1585a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1586a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1587a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
1588febc14f5SMatthias Ringwald             return 1;
15899cae807eSMatthias Ringwald         } else {
1590febc14f5SMatthias 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
1591a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1592a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1593a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
15949cae807eSMatthias Ringwald         }
1595ce263fc8SMatthias Ringwald     }
1596ce263fc8SMatthias Ringwald 
1597a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1598a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1599a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1600febc14f5SMatthias Ringwald         return 1;
1601aa4dd815SMatthias Ringwald     }
1602aa4dd815SMatthias Ringwald 
1603a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1604a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1605a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1606a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1607485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1608febc14f5SMatthias Ringwald         return 1;
1609a0ffb263SMatthias Ringwald     }
1610a0ffb263SMatthias Ringwald 
1611a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1612a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1613a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1614485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1615febc14f5SMatthias Ringwald         return 1;
1616aa4dd815SMatthias Ringwald     }
1617aa4dd815SMatthias Ringwald 
1618ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1619a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1620a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1621a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1622485ac19eSMilanka Ringwald         hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1623febc14f5SMatthias Ringwald         return 1;
1624ce263fc8SMatthias Ringwald     }
1625ce263fc8SMatthias Ringwald 
1626a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1627a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1628a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1629485ac19eSMilanka Ringwald         hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1630febc14f5SMatthias Ringwald         return 1;
1631ce263fc8SMatthias Ringwald     }
1632ce263fc8SMatthias Ringwald 
1633aa4dd815SMatthias Ringwald     // update AG indicators
1634a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1635aa4dd815SMatthias Ringwald         int i;
1636a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1637a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1638a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1639a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1640ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1641ce263fc8SMatthias Ringwald                     break;
1642ce263fc8SMatthias Ringwald                 }
1643485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1644febc14f5SMatthias Ringwald                 return 1;
1645aa4dd815SMatthias Ringwald             }
1646aa4dd815SMatthias Ringwald         }
1647aa4dd815SMatthias Ringwald     }
1648aa4dd815SMatthias Ringwald 
1649a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1650a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1651a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1652485ac19eSMilanka Ringwald         hfp_ag_send_ring(hfp_connection->rfcomm_cid);
1653febc14f5SMatthias Ringwald         return 1;
1654aa4dd815SMatthias Ringwald     }
1655aa4dd815SMatthias Ringwald 
1656a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1657a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1658a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1659a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1660febc14f5SMatthias Ringwald         return 1;
1661aa4dd815SMatthias Ringwald     }
1662aa4dd815SMatthias Ringwald 
1663a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1664a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1665a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1666a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1667a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1668febc14f5SMatthias Ringwald         return 1;
1669aa4dd815SMatthias Ringwald     }
1670aa4dd815SMatthias Ringwald 
1671a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1672a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1673a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1674a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1675ce263fc8SMatthias Ringwald         } else {
1676a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1677a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1678485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1679ce263fc8SMatthias Ringwald         }
1680a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1681febc14f5SMatthias Ringwald         return 1;
1682ce263fc8SMatthias Ringwald     }
1683ce263fc8SMatthias Ringwald 
1684a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1685a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1686a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1687485ac19eSMilanka Ringwald         hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1688febc14f5SMatthias Ringwald         return 1;
1689aa4dd815SMatthias Ringwald     }
1690aa4dd815SMatthias Ringwald 
1691a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1692a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1693a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1694485ac19eSMilanka Ringwald         hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
1695febc14f5SMatthias Ringwald         return 1;
16963deb3ec6SMatthias Ringwald     }
16973deb3ec6SMatthias Ringwald 
1698a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1699a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1700485ac19eSMilanka Ringwald         hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1701febc14f5SMatthias Ringwald         return 1;
1702febc14f5SMatthias Ringwald     }
1703febc14f5SMatthias Ringwald 
1704febc14f5SMatthias Ringwald     return 0;
1705febc14f5SMatthias Ringwald }
1706febc14f5SMatthias Ringwald 
1707febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){
1708febc14f5SMatthias Ringwald 
170976cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
171076cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_AG);
1711febc14f5SMatthias Ringwald 
171276cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
171376cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
1714ce263fc8SMatthias Ringwald 
1715febc14f5SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1716febc14f5SMatthias Ringwald         log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1717febc14f5SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1718febc14f5SMatthias Ringwald         return;
1719febc14f5SMatthias Ringwald     }
1720febc14f5SMatthias Ringwald 
1721febc14f5SMatthias Ringwald     int cmd_sent = hfp_ag_send_commands(hfp_connection);
1722febc14f5SMatthias Ringwald 
17232ee45eb2SMilanka Ringwald     // trigger codec exchange if requested by app
17242ee45eb2SMilanka Ringwald     if (hfp_connection->trigger_codec_exchange){
17252ee45eb2SMilanka Ringwald         log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state);
17262ee45eb2SMilanka Ringwald     }
1727febc14f5SMatthias Ringwald 
1728c1ab6cc1SMatthias Ringwald     if (hfp_connection->trigger_codec_exchange && (hfp_connection->command == HFP_CMD_NONE)){
17292ee45eb2SMilanka Ringwald         switch (hfp_connection->codecs_state){
17302ee45eb2SMilanka Ringwald             case HFP_CODECS_IDLE:
17312ee45eb2SMilanka Ringwald             case HFP_CODECS_RECEIVED_LIST:
17322ee45eb2SMilanka Ringwald             case HFP_CODECS_AG_RESEND_COMMON_CODEC:
17332ee45eb2SMilanka Ringwald             case HFP_CODECS_ERROR:
17342ee45eb2SMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 0;
17352ee45eb2SMilanka Ringwald                 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
17362ee45eb2SMilanka Ringwald                 break;
17372ee45eb2SMilanka Ringwald             default:
17382ee45eb2SMilanka Ringwald                 break;
17392ee45eb2SMilanka Ringwald         }
17402ee45eb2SMilanka Ringwald     }
17412ee45eb2SMilanka Ringwald 
1742febc14f5SMatthias Ringwald     if (!cmd_sent){
1743febc14f5SMatthias Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1744febc14f5SMatthias Ringwald     }
1745febc14f5SMatthias Ringwald 
1746c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1747c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
17483deb3ec6SMatthias Ringwald     }
17493deb3ec6SMatthias Ringwald 
1750c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1751c04cf7ffSMilanka Ringwald         cmd_sent = call_setup_state_machine(hfp_connection);
1752aa4dd815SMatthias Ringwald     }
1753aa4dd815SMatthias Ringwald 
1754c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1755c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
1756aa4dd815SMatthias Ringwald     }
17573deb3ec6SMatthias Ringwald 
1758c1ab6cc1SMatthias Ringwald     if ((hfp_connection->command == HFP_CMD_NONE) && !cmd_sent){
1759a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1760a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
17613deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1762a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1763a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
17643deb3ec6SMatthias Ringwald                 break;
17653deb3ec6SMatthias Ringwald             default:
17663deb3ec6SMatthias Ringwald                 break;
17673deb3ec6SMatthias Ringwald         }
17683deb3ec6SMatthias Ringwald     }
1769c04cf7ffSMilanka Ringwald 
1770c04cf7ffSMilanka Ringwald     if (cmd_sent){
1771a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1772473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1773473ac565SMatthias Ringwald     }
17743deb3ec6SMatthias Ringwald }
1775d68dcce1SMatthias Ringwald 
1776ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1777ce263fc8SMatthias Ringwald     int i;
1778a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1779a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1780ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1781ce263fc8SMatthias Ringwald             return indicator;
1782ce263fc8SMatthias Ringwald         }
1783ce263fc8SMatthias Ringwald     }
1784ce263fc8SMatthias Ringwald     return NULL;
1785ce263fc8SMatthias Ringwald }
17863deb3ec6SMatthias Ringwald 
1787e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1788c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
1789e0d09929SMatthias Ringwald }
1790e0d09929SMatthias Ringwald 
1791e9c22d4eSMatthias Ringwald static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
17928a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
17938a46ec40SMatthias Ringwald 
17948a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
17958a46ec40SMatthias Ringwald     if (size < 1) return;
17969ec2630cSMatthias Ringwald 
1797a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1798a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
17991e35c04dSMatthias Ringwald 
1800186dd3d2SMatthias Ringwald     hfp_log_rfcomm_message("HFP_AG_RX", packet, size);
18011e35c04dSMatthias Ringwald 
18028a46ec40SMatthias Ringwald     // process messages byte-wise
18033deb3ec6SMatthias Ringwald     int pos;
18043deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1805a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1806186dd3d2SMatthias Ringwald 
1807e0d09929SMatthias Ringwald         // parse until end of line
1808e0d09929SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1809e0d09929SMatthias Ringwald 
1810186dd3d2SMatthias Ringwald         hfp_generic_status_indicator_t * indicator;
1811a0ffb263SMatthias Ringwald         switch(hfp_connection->command){
1812ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1813ce263fc8SMatthias Ringwald                 if (hfp_ag_response_and_hold_active){
1814a0ffb263SMatthias Ringwald                     hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1815ce263fc8SMatthias Ringwald                 }
1816a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1817ce263fc8SMatthias Ringwald                 break;
1818ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
18190222a807SMatthias Ringwald                 switch(hfp_connection->ag_response_and_hold_action){
1820ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1821a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1822ce263fc8SMatthias Ringwald                         break;
1823ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1824a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1825ce263fc8SMatthias Ringwald                         break;
1826ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1827a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1828ce263fc8SMatthias Ringwald                         break;
1829ce263fc8SMatthias Ringwald                     default:
1830ce263fc8SMatthias Ringwald                         break;
1831ce263fc8SMatthias Ringwald                 }
1832a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1833ce263fc8SMatthias Ringwald                 break;
1834ce263fc8SMatthias Ringwald             case HFP_CMD_HF_INDICATOR_STATUS:
1835a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1836ce263fc8SMatthias Ringwald                 // find indicator by assigned number
1837a0ffb263SMatthias Ringwald                 indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1838ce263fc8SMatthias Ringwald                 if (!indicator){
1839a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
1840ce263fc8SMatthias Ringwald                     break;
1841ce263fc8SMatthias Ringwald                 }
1842ce263fc8SMatthias Ringwald                 switch (indicator->uuid){
1843ce263fc8SMatthias Ringwald                     case 1: // enhanced security
18440222a807SMatthias Ringwald                         if (hfp_connection->parser_indicator_value > 1) {
1845a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1846ce263fc8SMatthias Ringwald                             return;
1847ce263fc8SMatthias Ringwald                         }
1848e883851fSMatthias Ringwald                         log_info("HF Indicator 'enhanced security' set to %u", (unsigned int) hfp_connection->parser_indicator_value);
1849ce263fc8SMatthias Ringwald                         break;
1850ce263fc8SMatthias Ringwald                     case 2: // battery level
18510222a807SMatthias Ringwald                         if (hfp_connection->parser_indicator_value > 100){
1852a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1853ce263fc8SMatthias Ringwald                             return;
1854ce263fc8SMatthias Ringwald                         }
1855e883851fSMatthias Ringwald                         log_info("HF Indicator 'battery' set to %u", (unsigned int) hfp_connection->parser_indicator_value);
1856ce263fc8SMatthias Ringwald                         break;
1857ce263fc8SMatthias Ringwald                     default:
1858e883851fSMatthias Ringwald                         log_info("HF Indicator unknown set to %u", (unsigned int) hfp_connection->parser_indicator_value);
1859ce263fc8SMatthias Ringwald                         break;
1860ce263fc8SMatthias Ringwald                 }
1861a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1862ce263fc8SMatthias Ringwald                 break;
1863ce263fc8SMatthias Ringwald             case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1864ce263fc8SMatthias Ringwald                 // expected by SLC state machine
1865a0ffb263SMatthias Ringwald                 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1866a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
1867a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_status_indicators = 1;
1868ce263fc8SMatthias Ringwald                 break;
1869ce263fc8SMatthias Ringwald             case HFP_CMD_LIST_CURRENT_CALLS:
1870a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1871a0ffb263SMatthias Ringwald                 hfp_connection->next_call_index = 0;
1872a0ffb263SMatthias Ringwald                 hfp_connection->send_status_of_current_calls = 1;
1873ce263fc8SMatthias Ringwald                 break;
1874ce263fc8SMatthias Ringwald             case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1875ce263fc8SMatthias Ringwald                 if (subscriber_numbers_count == 0){
1876485ac19eSMilanka Ringwald                     hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1877ce263fc8SMatthias Ringwald                     break;
1878ce263fc8SMatthias Ringwald                 }
1879a0ffb263SMatthias Ringwald                 hfp_connection->next_subscriber_number_to_send = 0;
1880a0ffb263SMatthias Ringwald                 hfp_connection->send_subscriber_number = 1;
1881ce263fc8SMatthias Ringwald                 break;
1882c1797c7dSMatthias Ringwald             case HFP_CMD_TRANSMIT_DTMF_CODES:
18830222a807SMatthias Ringwald             {
1884a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
18850222a807SMatthias Ringwald                 char buffer[2];
18860222a807SMatthias Ringwald                 buffer[0] = (char) hfp_connection->ag_dtmf_code;
18870222a807SMatthias Ringwald                 buffer[1] = 0;
18880222a807SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer);
1889c1797c7dSMatthias Ringwald                 break;
18900222a807SMatthias Ringwald             }
1891aa4dd815SMatthias Ringwald             case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1892a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1893ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1894aa4dd815SMatthias Ringwald                 break;
1895aa4dd815SMatthias Ringwald             case HFP_CMD_TURN_OFF_EC_AND_NR:
1896a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1897aa4dd815SMatthias Ringwald                 if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1898a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
1899a0ffb263SMatthias Ringwald                     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
190060ebb071SMilanka Ringwald                     log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction);
1901aa4dd815SMatthias Ringwald                 } else {
1902a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
1903aa4dd815SMatthias Ringwald                 }
1904aa4dd815SMatthias Ringwald                 break;
1905aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_ANSWERED:
1906a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
190760ebb071SMilanka Ringwald                 log_info("HFP: ATA");
1908a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1909aa4dd815SMatthias Ringwald                 break;
1910aa4dd815SMatthias Ringwald             case HFP_CMD_HANG_UP_CALL:
1911a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1912a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1913a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1914aa4dd815SMatthias Ringwald                 break;
1915aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_HOLD: {
1916a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1917a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1918d0c20769SMatthias Ringwald 
19190222a807SMatthias Ringwald                 switch (hfp_connection->ag_call_hold_action){
19200222a807SMatthias Ringwald                     case 0:
1921d0c20769SMatthias Ringwald                         // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1922a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1923aa4dd815SMatthias Ringwald                         break;
19240222a807SMatthias Ringwald                     case 1:
1925d0c20769SMatthias Ringwald                         // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1926d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
1927d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
1928a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1929aa4dd815SMatthias Ringwald                         break;
19300222a807SMatthias Ringwald                     case 2:
1931d0c20769SMatthias Ringwald                         // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1932d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
1933d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
1934a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1935aa4dd815SMatthias Ringwald                         break;
19360222a807SMatthias Ringwald                     case 3:
1937d0c20769SMatthias Ringwald                         // Adds a held call to the conversation.
1938a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1939aa4dd815SMatthias Ringwald                         break;
19400222a807SMatthias Ringwald                     case 4:
1941d0c20769SMatthias Ringwald                         // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1942a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1943aa4dd815SMatthias Ringwald                         break;
1944aa4dd815SMatthias Ringwald                     default:
1945aa4dd815SMatthias Ringwald                         break;
1946aa4dd815SMatthias Ringwald                 }
19470222a807SMatthias Ringwald 				hfp_connection->call_index = 0;
194835e92150SMatthias Ringwald                 break;
1949aa4dd815SMatthias Ringwald             }
1950aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_PHONE_NUMBER:
1951a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1952a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
1953aa4dd815SMatthias Ringwald                 break;
1954aa4dd815SMatthias Ringwald             case HFP_CMD_REDIAL_LAST_NUMBER:
1955a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1956a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
1957aa4dd815SMatthias Ringwald                 break;
1958aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CLIP:
1959a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1960a0ffb263SMatthias Ringwald                 log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
1961a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1962aa4dd815SMatthias Ringwald                 break;
1963aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1964a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1965a0ffb263SMatthias Ringwald                 log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
1966a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1967aa4dd815SMatthias Ringwald                 break;
1968aa4dd815SMatthias Ringwald             case HFP_CMD_SET_SPEAKER_GAIN:
1969a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1970a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
197160ebb071SMilanka Ringwald                 log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
1972aa4dd815SMatthias Ringwald                 break;
1973aa4dd815SMatthias Ringwald             case HFP_CMD_SET_MICROPHONE_GAIN:
1974a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1975a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
197660ebb071SMilanka Ringwald                 log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
1977aa4dd815SMatthias Ringwald                 break;
1978aa4dd815SMatthias Ringwald             default:
1979aa4dd815SMatthias Ringwald                 break;
19803deb3ec6SMatthias Ringwald         }
19813deb3ec6SMatthias Ringwald     }
19820cef86faSMatthias Ringwald }
19833deb3ec6SMatthias Ringwald 
19841c6a0fc0SMatthias Ringwald static void hfp_ag_run(void){
1985d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1986d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1987d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1988d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
198922387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1990f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
19913deb3ec6SMatthias Ringwald     }
19923deb3ec6SMatthias Ringwald }
19933deb3ec6SMatthias Ringwald 
19941c6a0fc0SMatthias Ringwald static void hfp_ag_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
19953deb3ec6SMatthias Ringwald     switch (packet_type){
19963deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1997e9c22d4eSMatthias Ringwald             hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size);
19983deb3ec6SMatthias Ringwald             break;
19993deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2000e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2001e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2002f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2003e30a6a47SMatthias Ringwald                 return;
2004e30a6a47SMatthias Ringwald             }
200527950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2006aa4dd815SMatthias Ringwald             break;
20073deb3ec6SMatthias Ringwald         default:
20083deb3ec6SMatthias Ringwald             break;
20093deb3ec6SMatthias Ringwald     }
20103deb3ec6SMatthias Ringwald 
20111c6a0fc0SMatthias Ringwald     hfp_ag_run();
20123deb3ec6SMatthias Ringwald }
20133deb3ec6SMatthias Ringwald 
20141c6a0fc0SMatthias Ringwald static void hfp_ag_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2015405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
201605bed9f6SMatthias Ringwald 
201705bed9f6SMatthias Ringwald     // allow for sco established -> ring transition
201805bed9f6SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
201905bed9f6SMatthias Ringwald     if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return;
20201c6a0fc0SMatthias Ringwald     hfp_ag_run();
2021405014fbSMatthias Ringwald }
2022405014fbSMatthias Ringwald 
2023a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
20243deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
20253deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
20263deb3ec6SMatthias Ringwald         return;
20273deb3ec6SMatthias Ringwald     }
20283deb3ec6SMatthias Ringwald     int i;
2029a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
20303deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
20313deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
20323deb3ec6SMatthias Ringwald     }
2033a0ffb263SMatthias Ringwald }
20343deb3ec6SMatthias Ringwald 
2035a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2036a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2037a0ffb263SMatthias Ringwald }
20383deb3ec6SMatthias Ringwald 
2039a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2040a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
20416535961aSMatthias Ringwald     (void)memcpy(hfp_ag_indicators, ag_indicators,
20426535961aSMatthias Ringwald                  ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2043a0ffb263SMatthias Ringwald }
20443deb3ec6SMatthias Ringwald 
2045a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
204625789943SMilanka Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return;
2047a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
20486535961aSMatthias Ringwald     (void)memcpy(hfp_generic_status_indicators, hf_indicators,
20496535961aSMatthias Ringwald                  hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2050a0ffb263SMatthias Ringwald }
2051a0ffb263SMatthias Ringwald 
2052a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
20533deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
20546535961aSMatthias Ringwald     (void)memcpy(hfp_ag_call_hold_services, call_hold_services,
20556535961aSMatthias Ringwald                  call_hold_services_nr * sizeof(char *));
2056a0ffb263SMatthias Ringwald }
2057a0ffb263SMatthias Ringwald 
2058a0ffb263SMatthias Ringwald 
2059a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
206027950165SMatthias Ringwald 
2061520c92d5SMatthias Ringwald     hfp_init();
2062d63c37a1SMatthias Ringwald 
20631c6a0fc0SMatthias Ringwald     hfp_ag_hci_event_callback_registration.callback = &hfp_ag_hci_event_packet_handler;
20641c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_ag_hci_event_callback_registration);
206527950165SMatthias Ringwald 
20661c6a0fc0SMatthias Ringwald     rfcomm_register_service(&hfp_ag_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
206727950165SMatthias Ringwald 
206827950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
20691c6a0fc0SMatthias Ringwald     hfp_set_ag_rfcomm_packet_handler(&hfp_ag_rfcomm_packet_handler);
2070aa4dd815SMatthias Ringwald 
2071d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2072d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2073d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2074d210d9c4SMatthias Ringwald 
2075d210d9c4SMatthias Ringwald     hfp_gsm_init();
20763deb3ec6SMatthias Ringwald }
20773deb3ec6SMatthias Ringwald 
20783deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
2079323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
20803deb3ec6SMatthias Ringwald }
20813deb3ec6SMatthias Ringwald 
2082d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
20839c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2084a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2085a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2086a33eb0c4SMilanka Ringwald         return;
2087a33eb0c4SMilanka Ringwald     }
2088a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2089f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
20903deb3ec6SMatthias Ringwald }
20913deb3ec6SMatthias Ringwald 
2092d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
20939c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2094a0ffb263SMatthias Ringwald     if (!hfp_connection){
2095a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
20963deb3ec6SMatthias Ringwald         return;
20973deb3ec6SMatthias Ringwald     }
2098a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2099a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
21003deb3ec6SMatthias Ringwald         return;
21013deb3ec6SMatthias Ringwald     }
2102a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2103f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21043deb3ec6SMatthias Ringwald }
21053deb3ec6SMatthias Ringwald 
2106a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
21072ee45eb2SMilanka Ringwald     log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state);
2108a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2109a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
21103deb3ec6SMatthias Ringwald 
2111a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2112a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2113d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2114d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2115a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
21167522e673SMatthias Ringwald         // now, pick link settings
2117afac2a04SMilanka Ringwald         hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
21182ee45eb2SMilanka Ringwald         return;
21193deb3ec6SMatthias Ringwald     }
2120aa4dd815SMatthias Ringwald 
21212ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 1;
2122aa4dd815SMatthias Ringwald }
2123aa4dd815SMatthias Ringwald 
2124d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
21259c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2126a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2127a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2128a33eb0c4SMilanka Ringwald         return;
2129a33eb0c4SMilanka Ringwald     }
21302ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 0;
2131a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2132a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2133f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21343deb3ec6SMatthias Ringwald }
21353deb3ec6SMatthias Ringwald 
2136d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
21379c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2138a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2139a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2140a33eb0c4SMilanka Ringwald         return;
2141a33eb0c4SMilanka Ringwald     }
2142a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2143f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21443deb3ec6SMatthias Ringwald }
2145aa4dd815SMatthias Ringwald 
2146aa4dd815SMatthias Ringwald /**
2147aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2148aa4dd815SMatthias Ringwald  */
2149aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2150aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2151aa4dd815SMatthias Ringwald         return;
2152aa4dd815SMatthias Ringwald     }
2153aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2154aa4dd815SMatthias Ringwald 
2155665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2156665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2157665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2158a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
215966c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2160a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2161f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2162aa4dd815SMatthias Ringwald     }
2163aa4dd815SMatthias Ringwald }
2164aa4dd815SMatthias Ringwald 
2165aa4dd815SMatthias Ringwald /**
2166aa4dd815SMatthias Ringwald  * @brief Called from GSM
2167aa4dd815SMatthias Ringwald  */
2168aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2169aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2170aa4dd815SMatthias Ringwald }
2171aa4dd815SMatthias Ringwald 
2172aa4dd815SMatthias Ringwald /**
2173aa4dd815SMatthias Ringwald  * @brief number is stored.
2174aa4dd815SMatthias Ringwald  */
2175aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2176f8737b81SMatthias Ringwald     hfp_gsm_handler(HFP_AG_SET_CLIP, 0, type, number);
2177aa4dd815SMatthias Ringwald }
2178aa4dd815SMatthias Ringwald 
2179aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2180aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2181aa4dd815SMatthias Ringwald }
2182aa4dd815SMatthias Ringwald 
2183aa4dd815SMatthias Ringwald // call from AG UI
2184aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2185aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2186aa4dd815SMatthias Ringwald }
2187aa4dd815SMatthias Ringwald 
2188ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2189ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2190ce263fc8SMatthias Ringwald }
2191ce263fc8SMatthias Ringwald 
2192aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2193aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2194aa4dd815SMatthias Ringwald }
2195aa4dd815SMatthias Ringwald 
2196aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2197aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2198aa4dd815SMatthias Ringwald }
2199aa4dd815SMatthias Ringwald 
2200aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2201aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2202aa4dd815SMatthias Ringwald }
2203aa4dd815SMatthias Ringwald 
2204aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2205aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2206aa4dd815SMatthias Ringwald }
2207aa4dd815SMatthias Ringwald 
2208aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2209aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2210aa4dd815SMatthias Ringwald }
2211ce263fc8SMatthias Ringwald 
2212ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2213ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2214ce263fc8SMatthias Ringwald }
2215ce263fc8SMatthias Ringwald 
2216ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2217ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2218ce263fc8SMatthias Ringwald }
2219ce263fc8SMatthias Ringwald 
2220ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2221ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2222ce263fc8SMatthias Ringwald }
2223ce263fc8SMatthias Ringwald 
2224aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2225aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2226aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2227aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2228aa4dd815SMatthias Ringwald 
2229ce263fc8SMatthias Ringwald 
2230665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2231665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2232665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2233a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
223466c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2235a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2236ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2237ce263fc8SMatthias Ringwald             continue;
2238ce263fc8SMatthias Ringwald         }
2239ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2240a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2241f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2242aa4dd815SMatthias Ringwald     }
2243aa4dd815SMatthias Ringwald }
2244aa4dd815SMatthias Ringwald 
2245aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2246aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2247aa4dd815SMatthias Ringwald }
2248aa4dd815SMatthias Ringwald 
2249aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2250aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2251aa4dd815SMatthias Ringwald }
2252aa4dd815SMatthias Ringwald 
2253aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2254aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2255aa4dd815SMatthias Ringwald }
2256aa4dd815SMatthias Ringwald 
2257aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2258aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2259aa4dd815SMatthias Ringwald }
2260aa4dd815SMatthias Ringwald 
2261d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){
2262aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
22639c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2264a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2265a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2266a33eb0c4SMilanka Ringwald         return;
2267a33eb0c4SMilanka Ringwald     }
2268aa4dd815SMatthias Ringwald 
2269a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
227060ebb071SMilanka Ringwald         log_info("AG cannot acivate voice recognition - not supported by HF");
2271aa4dd815SMatthias Ringwald         return;
2272aa4dd815SMatthias Ringwald     }
2273aa4dd815SMatthias Ringwald 
2274aa4dd815SMatthias Ringwald     if (activate){
2275d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(acl_handle);
2276aa4dd815SMatthias Ringwald     }
2277aa4dd815SMatthias Ringwald 
2278a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2279a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2280f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2281aa4dd815SMatthias Ringwald }
2282aa4dd815SMatthias Ringwald 
2283d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
22849c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2285a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2286a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2287a33eb0c4SMilanka Ringwald         return;
2288a33eb0c4SMilanka Ringwald     }
2289a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2290a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2291a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2292a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2293aa4dd815SMatthias Ringwald     }
2294f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2295aa4dd815SMatthias Ringwald }
2296aa4dd815SMatthias Ringwald 
2297d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
22989c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2299a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2300a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2301a33eb0c4SMilanka Ringwald         return;
2302a33eb0c4SMilanka Ringwald     }
2303a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2304a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2305a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2306aa4dd815SMatthias Ringwald     }
2307f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2308aa4dd815SMatthias Ringwald }
2309aa4dd815SMatthias Ringwald 
2310d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
23119c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2312a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2313a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2314a33eb0c4SMilanka Ringwald         return;
2315a33eb0c4SMilanka Ringwald     }
2316aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2317a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2318aa4dd815SMatthias Ringwald }
2319aa4dd815SMatthias Ringwald 
2320d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
23219c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2322a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2323a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2324a33eb0c4SMilanka Ringwald         return;
2325a33eb0c4SMilanka Ringwald     }
2326a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2327aa4dd815SMatthias Ringwald }
2328aa4dd815SMatthias Ringwald 
2329d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
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     hfp_connection->ok_pending = 1;
2336c1797c7dSMatthias Ringwald }
2337aa4dd815SMatthias Ringwald 
2338ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2339ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2340ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2341ce263fc8SMatthias Ringwald }
2342ce263fc8SMatthias Ringwald 
23439cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
23449cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2345ce263fc8SMatthias Ringwald }
2346ce263fc8SMatthias Ringwald 
2347d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
23489c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2349a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2350a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2351a33eb0c4SMilanka Ringwald         return;
2352a33eb0c4SMilanka Ringwald     }
2353a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2354a0ffb263SMatthias Ringwald 
2355a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2356f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2357a0ffb263SMatthias Ringwald }
2358ce263fc8SMatthias Ringwald 
235976cc1527SMatthias 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){
236076cc1527SMatthias Ringwald 	if (!name){
236176cc1527SMatthias Ringwald 		name = default_hfp_ag_service_name;
236276cc1527SMatthias Ringwald 	}
236376cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
236476cc1527SMatthias Ringwald 
236576cc1527SMatthias Ringwald 	/*
236676cc1527SMatthias Ringwald 	 * 0x01 – Ability to reject a call
236776cc1527SMatthias Ringwald 	 * 0x00 – No ability to reject a call
236876cc1527SMatthias Ringwald 	 */
236976cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
237076cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
237176cc1527SMatthias Ringwald 
237276cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
237376cc1527SMatthias Ringwald 	//
237476cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
237576cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
237676cc1527SMatthias Ringwald 	//
237776cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
237876cc1527SMatthias Ringwald 	//
237976cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
238076cc1527SMatthias Ringwald 	if (wide_band_speech && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){
238176cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
238276cc1527SMatthias Ringwald 	}
238376cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
238476cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
238576cc1527SMatthias Ringwald }
238676cc1527SMatthias Ringwald 
238776cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
238876cc1527SMatthias Ringwald 	if (callback == NULL){
238976cc1527SMatthias Ringwald 		log_error("hfp_ag_register_packet_handler called with NULL callback");
239076cc1527SMatthias Ringwald 		return;
239176cc1527SMatthias Ringwald 	}
239276cc1527SMatthias Ringwald 	hfp_ag_callback = callback;
239376cc1527SMatthias Ringwald 	hfp_set_ag_callback(callback);
239476cc1527SMatthias Ringwald }
2395