xref: /btstack/src/classic/hfp_ag.c (revision 7bbeb3ad8cec0c1816689843bf9383cf4c644ef8)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp_ag.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
4266a048abSMatthias Ringwald // HFP Audio Gateway (AG) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493deb3ec6SMatthias Ringwald #include <string.h>
503deb3ec6SMatthias Ringwald 
5156042629SMatthias Ringwald #include "hci_cmd.h"
5282636622SMatthias Ringwald #include "btstack_run_loop.h"
533deb3ec6SMatthias Ringwald 
54235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
553deb3ec6SMatthias Ringwald #include "hci.h"
563deb3ec6SMatthias Ringwald #include "btstack_memory.h"
573deb3ec6SMatthias Ringwald #include "hci_dump.h"
583deb3ec6SMatthias Ringwald #include "l2cap.h"
5916ece135SMatthias Ringwald #include "btstack_debug.h"
60e30a6a47SMatthias Ringwald #include "btstack_event.h"
6159c6af15SMatthias Ringwald #include "classic/core.h"
623edc84c5SMatthias Ringwald #include "classic/hfp.h"
633edc84c5SMatthias Ringwald #include "classic/hfp_ag.h"
64023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h"
65efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
66023f2764SMatthias Ringwald #include "classic/sdp_server.h"
67023f2764SMatthias Ringwald #include "classic/sdp_util.h"
683deb3ec6SMatthias Ringwald 
69c5b64319SMatthias Ringwald // private prototypes
70f0aeb307SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection);
71adaba9f3SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
72adaba9f3SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
73c5b64319SMatthias Ringwald 
74c5b64319SMatthias Ringwald // public prototypes
7535833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void);
7635833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void);
77c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr);
78c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr);
79c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context);
80c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context);
81c5b64319SMatthias Ringwald 
8227950165SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
8327950165SMatthias Ringwald 
84c5b64319SMatthias Ringwald // gobals
853deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway";
86aa4dd815SMatthias Ringwald 
873deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
88aa4dd815SMatthias Ringwald 
893deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
903deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
913deb3ec6SMatthias Ringwald 
923deb3ec6SMatthias Ringwald static int  hfp_ag_indicators_nr = 0;
9325789943SMilanka Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS];
943deb3ec6SMatthias Ringwald 
95a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0;
9625789943SMilanka Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_INDICATORS];
97a0ffb263SMatthias Ringwald 
983deb3ec6SMatthias Ringwald static int  hfp_ag_call_hold_services_nr = 0;
993deb3ec6SMatthias Ringwald static char *hfp_ag_call_hold_services[6];
100ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback;
1013deb3ec6SMatthias Ringwald 
102ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
103ce263fc8SMatthias Ringwald static int hfp_ag_response_and_hold_active = 0;
104aa4dd815SMatthias Ringwald 
105ce263fc8SMatthias Ringwald // Subcriber information entries
106ce263fc8SMatthias Ringwald static hfp_phone_number_t * subscriber_numbers = NULL;
107ce263fc8SMatthias Ringwald static int subscriber_numbers_count = 0;
108ce263fc8SMatthias Ringwald 
10999a10067SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection);
1103deb3ec6SMatthias Ringwald 
1113deb3ec6SMatthias Ringwald 
11276cc1527SMatthias Ringwald static void hfp_ag_emit_simple_event(uint8_t event_subtype){
11376cc1527SMatthias Ringwald 	uint8_t event[3];
11476cc1527SMatthias Ringwald 	event[0] = HCI_EVENT_HFP_META;
11576cc1527SMatthias Ringwald 	event[1] = sizeof(event) - 2;
11676cc1527SMatthias Ringwald 	event[2] = event_subtype;
11776cc1527SMatthias Ringwald 	if (!hfp_ag_callback) return;
11876cc1527SMatthias Ringwald 	(*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
11976cc1527SMatthias Ringwald }
12076cc1527SMatthias Ringwald 
121a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
122a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
123a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1246535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1256535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1263deb3ec6SMatthias Ringwald     }
127a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
128a0ffb263SMatthias Ringwald }
129a0ffb263SMatthias Ringwald 
130a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
131a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
132a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
133a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1346535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1356535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
136a0ffb263SMatthias Ringwald     }
137a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1383deb3ec6SMatthias Ringwald }
1393deb3ec6SMatthias Ringwald 
140aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
141aa4dd815SMatthias Ringwald     int i;
142aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
143aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
144aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
145aa4dd815SMatthias Ringwald         }
146aa4dd815SMatthias Ringwald     }
147aa4dd815SMatthias Ringwald     return NULL;
148aa4dd815SMatthias Ringwald }
149aa4dd815SMatthias Ringwald 
150aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
151aa4dd815SMatthias Ringwald     int i;
152aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
153aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
154aa4dd815SMatthias Ringwald             return i;
155aa4dd815SMatthias Ringwald         }
156aa4dd815SMatthias Ringwald     }
157aa4dd815SMatthias Ringwald     return -1;
158aa4dd815SMatthias Ringwald }
159aa4dd815SMatthias Ringwald 
1609c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){
1619c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1629c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1639c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1649c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1659c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1669c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1679c9c64c1SMatthias Ringwald         return hfp_connection;
1689c9c64c1SMatthias Ringwald     }
1699c9c64c1SMatthias Ringwald     return NULL;
1709c9c64c1SMatthias Ringwald }
1713deb3ec6SMatthias Ringwald 
1720cb5b971SMatthias Ringwald static int use_in_band_tone(void){
173aa4dd815SMatthias Ringwald     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1743deb3ec6SMatthias Ringwald }
1753deb3ec6SMatthias Ringwald 
176a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
177a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1783deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1793deb3ec6SMatthias Ringwald     return hf && ag;
1803deb3ec6SMatthias Ringwald }
1813deb3ec6SMatthias Ringwald 
182a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
183a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1843deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1853deb3ec6SMatthias Ringwald     return hf && ag;
1863deb3ec6SMatthias Ringwald }
1873deb3ec6SMatthias Ringwald 
188a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
189a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
1903deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
1913deb3ec6SMatthias Ringwald     return hf && ag;
1923deb3ec6SMatthias Ringwald }
1933deb3ec6SMatthias Ringwald 
19476cc1527SMatthias Ringwald /* unsolicited responses */
195aa4dd815SMatthias Ringwald 
196485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){
197aa4dd815SMatthias Ringwald     char buffer[20];
198aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
199aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2003deb3ec6SMatthias Ringwald }
2013deb3ec6SMatthias Ringwald 
2023deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2033deb3ec6SMatthias Ringwald     char buffer[40];
2043deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
2053deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2063deb3ec6SMatthias Ringwald }
2073deb3ec6SMatthias Ringwald 
208485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){
2093deb3ec6SMatthias Ringwald     char buffer[10];
2103deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nOK\r\n");
2113deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2123deb3ec6SMatthias Ringwald }
2133deb3ec6SMatthias Ringwald 
214485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){
215aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
216aa4dd815SMatthias Ringwald }
217aa4dd815SMatthias Ringwald 
218aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
219aa4dd815SMatthias Ringwald     char buffer[50];
220d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
221aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
222aa4dd815SMatthias Ringwald }
223aa4dd815SMatthias Ringwald 
224ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
225ce263fc8SMatthias Ringwald     char buffer[50];
226ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
227ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
228ce263fc8SMatthias Ringwald }
229ce263fc8SMatthias Ringwald 
230c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
231aa4dd815SMatthias Ringwald     char buffer[50];
232d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
233aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
234aa4dd815SMatthias Ringwald }
235aa4dd815SMatthias Ringwald 
236aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
237aa4dd815SMatthias Ringwald     char buffer[50];
238a0ffb263SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
239aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
240aa4dd815SMatthias Ringwald }
241aa4dd815SMatthias Ringwald 
242485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){
2433deb3ec6SMatthias Ringwald     char buffer[10];
2443deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nERROR\r\n");
2453deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2463deb3ec6SMatthias Ringwald }
2473deb3ec6SMatthias Ringwald 
248485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2493deb3ec6SMatthias Ringwald     char buffer[20];
2503deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2513deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2523deb3ec6SMatthias Ringwald }
2533deb3ec6SMatthias Ringwald 
254ad902e3dSMatthias Ringwald // get size for indicator string
255a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
256ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
25745718b6fSMatthias Ringwald     return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
258a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
259a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
260ad902e3dSMatthias Ringwald }
261ad902e3dSMatthias Ringwald 
262ad902e3dSMatthias Ringwald // store indicator
263a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
264ad902e3dSMatthias Ringwald     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
265a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
266a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
267a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2683deb3ec6SMatthias Ringwald }
269ad902e3dSMatthias Ringwald 
270ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
271a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
272a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
273ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
274c1ab6cc1SMatthias Ringwald     return 3 + ((num_indicators-1) * 2);
2753deb3ec6SMatthias Ringwald }
276ad902e3dSMatthias Ringwald 
277ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
278a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
279ad902e3dSMatthias Ringwald     if (index == 0) {
280ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
281ad902e3dSMatthias Ringwald     }
282ad902e3dSMatthias Ringwald     index--;
283a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
284ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
285ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
286a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
287ad902e3dSMatthias Ringwald     }
288c1ab6cc1SMatthias Ringwald     if (indicator_index == (num_indicators - 1)){
289ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
290ad902e3dSMatthias Ringwald     }
291ad902e3dSMatthias Ringwald     return 1; // comma
292ad902e3dSMatthias Ringwald }
293ad902e3dSMatthias Ringwald 
294a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
295ad902e3dSMatthias Ringwald     if (index == 0){
296ad902e3dSMatthias Ringwald         *buffer++ = '\r';
29774386ee0SMatthias Ringwald         *buffer++ = '\n';
298ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
2996535961aSMatthias Ringwald         (void)memcpy(buffer, HFP_INDICATOR, len);
300ad902e3dSMatthias Ringwald         buffer += len;
301ad902e3dSMatthias Ringwald         *buffer++ = ':';
302ad902e3dSMatthias Ringwald         return;
303ad902e3dSMatthias Ringwald     }
304ad902e3dSMatthias Ringwald     index--;
305a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
306ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
307ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
308a0ffb263SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
309ad902e3dSMatthias Ringwald         return;
310ad902e3dSMatthias Ringwald     }
311c1ab6cc1SMatthias Ringwald     if (indicator_index == (num_indicators-1)){
3126535961aSMatthias Ringwald         (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8);
313ad902e3dSMatthias Ringwald         return;
314ad902e3dSMatthias Ringwald     }
315ad902e3dSMatthias Ringwald     *buffer = ',';
3163deb3ec6SMatthias Ringwald }
3173deb3ec6SMatthias Ringwald 
3183deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
319c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3203deb3ec6SMatthias Ringwald     int i;
3213deb3ec6SMatthias Ringwald     int offset = 0;
322c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_generic_status_indicators_nr-1); i++) {
323a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3243deb3ec6SMatthias Ringwald     }
325a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
326a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3273deb3ec6SMatthias Ringwald     }
3283deb3ec6SMatthias Ringwald     return offset;
3293deb3ec6SMatthias Ringwald }
3303deb3ec6SMatthias Ringwald 
3313deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
332c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_generic_status_indicators_nr * 3)) return 0;
3333deb3ec6SMatthias Ringwald     int i;
3343deb3ec6SMatthias Ringwald     int offset = 0;
335a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
336a0ffb263SMatthias 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);
3373deb3ec6SMatthias Ringwald     }
3383deb3ec6SMatthias Ringwald     return offset;
3393deb3ec6SMatthias Ringwald }
3403deb3ec6SMatthias Ringwald 
3413deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
342c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3433deb3ec6SMatthias Ringwald     int i;
3443deb3ec6SMatthias Ringwald     int offset = 0;
345c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_ag_indicators_nr-1); i++) {
3463deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3473deb3ec6SMatthias Ringwald     }
3483deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3493deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3503deb3ec6SMatthias Ringwald     }
3513deb3ec6SMatthias Ringwald     return offset;
3523deb3ec6SMatthias Ringwald }
3533deb3ec6SMatthias Ringwald 
3543deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
355c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0;
3563deb3ec6SMatthias Ringwald     int i;
3573deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
358c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) {
3593deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3603deb3ec6SMatthias Ringwald     }
3613deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3623deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3633deb3ec6SMatthias Ringwald     }
3643deb3ec6SMatthias Ringwald     return offset;
3653deb3ec6SMatthias Ringwald }
3663deb3ec6SMatthias Ringwald 
367485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
368ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
369a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
370a0ffb263SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
3713deb3ec6SMatthias Ringwald 
372ad902e3dSMatthias Ringwald     // assumes: can send now == true
373ad902e3dSMatthias Ringwald     // assumes: num segments > 0
374aba39421SMatthias Ringwald     // assumes: individual segments are smaller than MTU
375ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
376ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
377ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
378ad902e3dSMatthias Ringwald     int offset = 0;
379ad902e3dSMatthias Ringwald     int segment = start_segment;
380ad902e3dSMatthias Ringwald     while (segment < num_segments){
381a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
382c1ab6cc1SMatthias Ringwald         if ((offset + segment_len) > mtu) break;
383aba39421SMatthias Ringwald         // append segement
384a0ffb263SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset);
385ad902e3dSMatthias Ringwald         offset += segment_len;
386ad902e3dSMatthias Ringwald         segment++;
387ad902e3dSMatthias Ringwald     }
388ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
389ad902e3dSMatthias Ringwald     return segment;
390ad902e3dSMatthias Ringwald }
3913deb3ec6SMatthias Ringwald 
392ad902e3dSMatthias Ringwald // returns next segment to store
393485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
394a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
395485ac19eSMilanka Ringwald     return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
396ad902e3dSMatthias Ringwald         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
3973deb3ec6SMatthias Ringwald }
3983deb3ec6SMatthias Ringwald 
399485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){
4003deb3ec6SMatthias Ringwald     char buffer[40];
40189425bfcSMilanka Ringwald     const int size = sizeof(buffer);
40289425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR);
40389425bfcSMilanka Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9);
40489425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4053deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4063deb3ec6SMatthias Ringwald }
4073deb3ec6SMatthias Ringwald 
408485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){
409ad902e3dSMatthias Ringwald     char buffer[40];
41089425bfcSMilanka Ringwald     const int size = sizeof(buffer);
41189425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
41289425bfcSMilanka Ringwald     offset += hfp_ag_call_services_join(buffer+offset, size-offset-9);
41389425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4143deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4153deb3ec6SMatthias Ringwald }
4163deb3ec6SMatthias Ringwald 
4173deb3ec6SMatthias Ringwald 
418485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){
419485ac19eSMilanka Ringwald     return hfp_ag_send_ok(cid);
4203deb3ec6SMatthias Ringwald }
4213deb3ec6SMatthias Ringwald 
422485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4233deb3ec6SMatthias Ringwald     char buffer[40];
42489425bfcSMilanka Ringwald     const int size = sizeof(buffer);
42589425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
42689425bfcSMilanka Ringwald     offset += hfp_hf_indicators_join(buffer+offset, size-offset-10);
42789425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n");
4283deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4293deb3ec6SMatthias Ringwald }
4303deb3ec6SMatthias Ringwald 
431485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4323deb3ec6SMatthias Ringwald     char buffer[40];
4331cc1d9e9SMilanka Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7);
4341cc1d9e9SMilanka Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4353deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4363deb3ec6SMatthias Ringwald }
4373deb3ec6SMatthias Ringwald 
438485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4393deb3ec6SMatthias Ringwald     char buffer[20];
440aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
4413deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4423deb3ec6SMatthias Ringwald }
4433deb3ec6SMatthias Ringwald 
444485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4455e71456cSMilanka Ringwald     char buffer[41];
4463deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
44789425bfcSMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4483deb3ec6SMatthias Ringwald     } else {
44989425bfcSMilanka Ringwald         int offset = snprintf(buffer,  sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format);
4505e71456cSMilanka Ringwald         offset += snprintf(buffer+offset, 16, "%s", op.name);
45189425bfcSMilanka Ringwald         snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4523deb3ec6SMatthias Ringwald     }
4533deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4543deb3ec6SMatthias Ringwald }
4553deb3ec6SMatthias Ringwald 
4561cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
4571cc1d9e9SMilanka Ringwald     char buffer[30];
45889425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value);
4591cc1d9e9SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
4601cc1d9e9SMilanka Ringwald }
4613deb3ec6SMatthias Ringwald 
462485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){
4631cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
4643deb3ec6SMatthias Ringwald }
4653deb3ec6SMatthias Ringwald 
466485ac19eSMilanka Ringwald static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
4671cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
468aa4dd815SMatthias Ringwald }
469aa4dd815SMatthias Ringwald 
470485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
4711cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
472aa4dd815SMatthias Ringwald }
473aa4dd815SMatthias Ringwald 
474485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
4751cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
476aa4dd815SMatthias Ringwald }
477aa4dd815SMatthias Ringwald 
478485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){
4791cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_RESPONSE_AND_HOLD, state);
480ce263fc8SMatthias Ringwald }
481ce263fc8SMatthias Ringwald 
482a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
483bc1b1537SMilanka Ringwald     if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
484bc1b1537SMilanka Ringwald 
4856a7f44bdSMilanka Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){
4866a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
487df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
4883deb3ec6SMatthias Ringwald         }
4893deb3ec6SMatthias Ringwald     }
490df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
4913deb3ec6SMatthias Ringwald }
4923deb3ec6SMatthias Ringwald 
49376cc1527SMatthias Ringwald /* state machines */
49476cc1527SMatthias Ringwald 
495afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){
496afac2a04SMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_AGSF_ESCO_S4));
4977522e673SMatthias Ringwald }
4987522e673SMatthias Ringwald 
499a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
500aa4dd815SMatthias Ringwald     /* events ( == commands):
501aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
502aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
503aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
504aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
505aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
506aa4dd815SMatthias Ringwald     */
507aa4dd815SMatthias Ringwald 
508a0ffb263SMatthias Ringwald      switch (hfp_connection->codecs_state){
509aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
510a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
511aa4dd815SMatthias Ringwald             break;
512aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
513a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
514aa4dd815SMatthias Ringwald             break;
515aa4dd815SMatthias Ringwald         default:
516aa4dd815SMatthias Ringwald             break;
517aa4dd815SMatthias Ringwald     }
518aa4dd815SMatthias Ringwald 
519aa4dd815SMatthias Ringwald     // printf(" -> State machine: CC\n");
520aa4dd815SMatthias Ringwald 
521a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
522aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
523a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
524a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
525485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
526aa4dd815SMatthias Ringwald                 return 1;
527aa4dd815SMatthias Ringwald             }
528aa4dd815SMatthias Ringwald 
529a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
530aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
531aa4dd815SMatthias Ringwald                 case HFP_CODECS_EXCHANGED:
532a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
533aa4dd815SMatthias Ringwald                     break;
534aa4dd815SMatthias Ringwald                 default:
535aa4dd815SMatthias Ringwald                     break;
536aa4dd815SMatthias Ringwald             }
537485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
538aa4dd815SMatthias Ringwald             return 1;
539aa4dd815SMatthias Ringwald 
540aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
541a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
542485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
543aa4dd815SMatthias Ringwald             return 1;
544aa4dd815SMatthias Ringwald 
545aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
546a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
547a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
548485ac19eSMilanka Ringwald             hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
549aa4dd815SMatthias Ringwald             return 1;
550aa4dd815SMatthias Ringwald 
551aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
552a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
553a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
554485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
555aa4dd815SMatthias Ringwald                 return 1;
556aa4dd815SMatthias Ringwald             }
557a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
558a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
559c1ab6cc1SMatthias Ringwald             log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
560485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
5617522e673SMatthias Ringwald             // now, pick link settings
562afac2a04SMilanka Ringwald 
563afac2a04SMilanka Ringwald             hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
564aa4dd815SMatthias Ringwald             return 1;
565aa4dd815SMatthias Ringwald         default:
566aa4dd815SMatthias Ringwald             break;
567aa4dd815SMatthias Ringwald     }
568aa4dd815SMatthias Ringwald     return 0;
569aa4dd815SMatthias Ringwald }
570aa4dd815SMatthias Ringwald 
571a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
572a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
573ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
574aa4dd815SMatthias Ringwald 
575a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
576d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
577a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
578aa4dd815SMatthias Ringwald     }
579ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
580c1ab6cc1SMatthias Ringwald     if ((hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) &&
581505f1c30SMatthias Ringwald         (hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS)){
582a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
583ce263fc8SMatthias Ringwald     }
584aa4dd815SMatthias Ringwald }
5853deb3ec6SMatthias Ringwald 
586a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
587c79b4cabSMatthias Ringwald     // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
588a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
589c04cf7ffSMilanka Ringwald     int sent = 0;
590a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
5913deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
592a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
5933deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
594aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
595d715cf51SMatthias Ringwald                     hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
596a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
597a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
598aa4dd815SMatthias Ringwald                     } else {
599a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
600aa4dd815SMatthias Ringwald                     }
601a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
602aa4dd815SMatthias Ringwald                     return 1;
6033deb3ec6SMatthias Ringwald                 default:
6043deb3ec6SMatthias Ringwald                     break;
6053deb3ec6SMatthias Ringwald             }
6063deb3ec6SMatthias Ringwald             break;
6073deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
608c04cf7ffSMilanka Ringwald             sent = codecs_exchange_state_machine(hfp_connection);
609a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
610a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6113deb3ec6SMatthias Ringwald             }
612c04cf7ffSMilanka Ringwald             return sent;
613aa4dd815SMatthias Ringwald 
614aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
615a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
616473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
61784a0c24eSMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
618473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
619473ac565SMatthias Ringwald                 // continue below in state switch
620ad902e3dSMatthias Ringwald             }
621ad902e3dSMatthias Ringwald             break;
622aa4dd815SMatthias Ringwald 
623aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
624a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
625a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
626485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
627aa4dd815SMatthias Ringwald             return 1;
628aa4dd815SMatthias Ringwald 
6293deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
630a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
631a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
632a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
633a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
634a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
635aa4dd815SMatthias Ringwald             } else {
636a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
6373deb3ec6SMatthias Ringwald             }
6388a46ec40SMatthias Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
639aa4dd815SMatthias Ringwald             return 1;
6403deb3ec6SMatthias Ringwald 
641aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
642a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
643a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
644a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
645aa4dd815SMatthias Ringwald             }
646485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
647a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
648a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
649ce263fc8SMatthias Ringwald             }
650aa4dd815SMatthias Ringwald             return 1;
651aa4dd815SMatthias Ringwald 
652aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
653a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
654a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
655485ac19eSMilanka Ringwald             hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
656aa4dd815SMatthias Ringwald             return 1;
657aa4dd815SMatthias Ringwald 
658aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
659a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
660a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
661485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
662aa4dd815SMatthias Ringwald             return 1;
663aa4dd815SMatthias Ringwald 
664aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
665a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
666a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
667485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
668aa4dd815SMatthias Ringwald             return 1;
6693deb3ec6SMatthias Ringwald         default:
6703deb3ec6SMatthias Ringwald             break;
6713deb3ec6SMatthias Ringwald     }
672473ac565SMatthias Ringwald 
673473ac565SMatthias Ringwald     switch (hfp_connection->state){
674473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
675485ac19eSMilanka Ringwald             int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
676473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
677473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
678473ac565SMatthias Ringwald             if (next_segment < num_segments){
679473ac565SMatthias Ringwald                 // prepare sending of next segment
680473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
681473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
682473ac565SMatthias Ringwald             } else {
683473ac565SMatthias Ringwald                 // done, go to next state
684473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
685473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
686473ac565SMatthias Ringwald             }
687473ac565SMatthias Ringwald             return 1;
688473ac565SMatthias Ringwald         }
689473ac565SMatthias Ringwald         default:
690473ac565SMatthias Ringwald             break;
691473ac565SMatthias Ringwald     }
692c04cf7ffSMilanka Ringwald     return 0;
6933deb3ec6SMatthias Ringwald }
6943deb3ec6SMatthias Ringwald 
695a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
696c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
697c04cf7ffSMilanka Ringwald     if (sent) return 1;
698aa4dd815SMatthias Ringwald 
699a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
700aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
701a0ffb263SMatthias Ringwald             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
702485ac19eSMilanka Ringwald             hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
703aa4dd815SMatthias Ringwald             return 1;
704aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
705aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
706a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
707485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
708a0ffb263SMatthias Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
709aa4dd815SMatthias Ringwald             } else {
710485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
711aa4dd815SMatthias Ringwald             }
712aa4dd815SMatthias Ringwald             return 1;
713aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
714485ac19eSMilanka Ringwald             hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
715aa4dd815SMatthias Ringwald             return 1;
716aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
717485ac19eSMilanka Ringwald             hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
718aa4dd815SMatthias Ringwald             return 1;
719aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
720a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
721485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
722aa4dd815SMatthias Ringwald             } else {
723485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
7243deb3ec6SMatthias Ringwald             }
725aa4dd815SMatthias Ringwald             return 1;
726aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
727485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
728aa4dd815SMatthias Ringwald             return 1;
7293deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
730a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
731a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
732485ac19eSMilanka Ringwald                 hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
733aa4dd815SMatthias Ringwald                 return 1;
7343deb3ec6SMatthias Ringwald             }
735202c8a4cSMatthias Ringwald             break;
7363deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
737485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
738ce263fc8SMatthias Ringwald             return 1;
7393deb3ec6SMatthias Ringwald         default:
7403deb3ec6SMatthias Ringwald             break;
7413deb3ec6SMatthias Ringwald     }
742aa4dd815SMatthias Ringwald     return 0;
7433deb3ec6SMatthias Ringwald }
7443deb3ec6SMatthias Ringwald 
745a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
746505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
747505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
7483deb3ec6SMatthias Ringwald 
749aa4dd815SMatthias Ringwald 
750505f1c30SMatthias Ringwald     if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){
751a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
752a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
753a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
754aa4dd815SMatthias Ringwald         return 1;
755aa4dd815SMatthias Ringwald     }
756aa4dd815SMatthias Ringwald 
757a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
758aa4dd815SMatthias Ringwald 
759aa4dd815SMatthias Ringwald     // run codecs exchange
760c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
761c04cf7ffSMilanka Ringwald     if (sent) return 1;
762aa4dd815SMatthias Ringwald 
763c04cf7ffSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
764a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
765a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
766a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
767eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
768aa4dd815SMatthias Ringwald         return 1;
769aa4dd815SMatthias Ringwald     }
770aa4dd815SMatthias Ringwald     return 0;
771aa4dd815SMatthias Ringwald }
772aa4dd815SMatthias Ringwald 
773ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
774665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
775665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
776aa4dd815SMatthias Ringwald 
777665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
778a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
779a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
780a0ffb263SMatthias Ringwald             return hfp_connection;
781aa4dd815SMatthias Ringwald         }
782aa4dd815SMatthias Ringwald     }
783aa4dd815SMatthias Ringwald     return NULL;
784aa4dd815SMatthias Ringwald }
785aa4dd815SMatthias Ringwald 
786ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
787d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
788d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
789aa4dd815SMatthias Ringwald 
790d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
791a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
792a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
793aa4dd815SMatthias Ringwald 
794a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
795a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
796aa4dd815SMatthias Ringwald 
797f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
798aa4dd815SMatthias Ringwald }
799aa4dd815SMatthias Ringwald 
800a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
801a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
802a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
803a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
804a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
805aa4dd815SMatthias Ringwald }
806aa4dd815SMatthias Ringwald 
807a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
808a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
809a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
810aa4dd815SMatthias Ringwald }
811aa4dd815SMatthias Ringwald 
812aa4dd815SMatthias Ringwald //
813aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
814aa4dd815SMatthias Ringwald //
815aa4dd815SMatthias Ringwald 
816a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
817aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
818a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
819d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
820aa4dd815SMatthias Ringwald     } else {
821a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
822a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
823a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
824a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
825ca59be51SMatthias Ringwald         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
826aa4dd815SMatthias Ringwald     }
827aa4dd815SMatthias Ringwald }
828aa4dd815SMatthias Ringwald 
829a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
830a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
831a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
832a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
833ca59be51SMatthias Ringwald     hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGINIG);
834aa4dd815SMatthias Ringwald }
835aa4dd815SMatthias Ringwald 
836aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
837aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
838aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
839aa4dd815SMatthias Ringwald 
840665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
841665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
842665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
843a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
84466c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
845a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
846a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
847a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
848d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
849aa4dd815SMatthias Ringwald         }
850a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
851a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
852aa4dd815SMatthias Ringwald         }
853f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
854aa4dd815SMatthias Ringwald     }
855aa4dd815SMatthias Ringwald }
856aa4dd815SMatthias Ringwald 
857aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
858aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
859aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
860aa4dd815SMatthias Ringwald 
861665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
862665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
863665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
864a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
86566c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
866a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
867a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
868f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
869aa4dd815SMatthias Ringwald     }
870aa4dd815SMatthias Ringwald }
871aa4dd815SMatthias Ringwald 
872aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
873aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("call");
874aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
875aa4dd815SMatthias Ringwald 
876665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
877665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
878665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
879a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
88066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
881a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
882a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
883f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
884aa4dd815SMatthias Ringwald     }
885aa4dd815SMatthias Ringwald }
886aa4dd815SMatthias Ringwald 
887aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
888aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callheld");
889aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
890aa4dd815SMatthias Ringwald 
891665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
892665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
893665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
894a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
89566c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
896a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
897a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
898f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
899aa4dd815SMatthias Ringwald     }
900aa4dd815SMatthias Ringwald }
901aa4dd815SMatthias Ringwald 
902aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
903aa4dd815SMatthias Ringwald 
904aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
905aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
906aa4dd815SMatthias Ringwald 
907665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
908665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
909665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
910a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
91166c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
912505f1c30SMatthias Ringwald         if ((hfp_connection->call_state != HFP_CALL_RINGING) &&
913505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
914aa4dd815SMatthias Ringwald 
915a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
916a0ffb263SMatthias Ringwald         if (hfp_connection == source){
917aa4dd815SMatthias Ringwald 
918aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
919a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
920aa4dd815SMatthias Ringwald             } else {
921a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
922d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
923aa4dd815SMatthias Ringwald             }
924aa4dd815SMatthias Ringwald 
925a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
926a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
927aa4dd815SMatthias Ringwald 
928aa4dd815SMatthias Ringwald         } else {
929a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
930aa4dd815SMatthias Ringwald         }
931f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
932aa4dd815SMatthias Ringwald     }
933aa4dd815SMatthias Ringwald }
934aa4dd815SMatthias Ringwald 
935aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
936aa4dd815SMatthias Ringwald 
937aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
938aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
939aa4dd815SMatthias Ringwald 
940665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
941665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
942665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
943a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
94466c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
945a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
946aa4dd815SMatthias Ringwald 
947a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
948a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
949aa4dd815SMatthias Ringwald 
950a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
951a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
952aa4dd815SMatthias Ringwald 
953f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
954aa4dd815SMatthias Ringwald         break;  // only single
955aa4dd815SMatthias Ringwald     }
956aa4dd815SMatthias Ringwald }
957aa4dd815SMatthias Ringwald 
958aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
959aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
960665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
961665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
962665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
963665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
96466c5995fSMatthias Ringwald         if (connection->local_role != HFP_ROLE_AG) continue;
965505f1c30SMatthias Ringwald         if ((connection->call_state != HFP_CALL_RINGING) &&
966505f1c30SMatthias Ringwald             (connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
967aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
968aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
969aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
970f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(connection);
971aa4dd815SMatthias Ringwald     }
972aa4dd815SMatthias Ringwald }
973aa4dd815SMatthias Ringwald 
974aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
975aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
976aa4dd815SMatthias Ringwald 
977665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
978665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
979665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
980a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
98166c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
982d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
983a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
984a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
985a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
9866d78145cSMatthias Ringwald         if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
987a0ffb263SMatthias Ringwald             hfp_connection->release_audio_connection = 1;
9886d78145cSMatthias Ringwald         }
989f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
990aa4dd815SMatthias Ringwald     }
991ca59be51SMatthias Ringwald     hfp_ag_emit_simple_event(HFP_SUBEVENT_CALL_TERMINATED);
992aa4dd815SMatthias Ringwald }
993aa4dd815SMatthias Ringwald 
9940cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
995aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
996aa4dd815SMatthias Ringwald     if (!indicator){
997d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
99845718b6fSMatthias Ringwald         return;
999aa4dd815SMatthias Ringwald     };
1000d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1001aa4dd815SMatthias Ringwald }
1002aa4dd815SMatthias Ringwald 
10030cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1004d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1005d210d9c4SMatthias Ringwald     if (!indicator){
1006d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
100745718b6fSMatthias Ringwald         return;
1008d210d9c4SMatthias Ringwald     };
1009d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1010d210d9c4SMatthias Ringwald }
1011d210d9c4SMatthias Ringwald 
10120cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1013aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1014aa4dd815SMatthias Ringwald     if (!indicator){
1015aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
101645718b6fSMatthias Ringwald         return;
1017aa4dd815SMatthias Ringwald     };
1018d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1019aa4dd815SMatthias Ringwald }
1020aa4dd815SMatthias Ringwald 
1021aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1022665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1023665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1024665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1025a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
102666c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1027505f1c30SMatthias Ringwald         if ((hfp_connection->call_state != HFP_CALL_RINGING) &&
1028505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1029a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1030aa4dd815SMatthias Ringwald     }
1031aa4dd815SMatthias Ringwald }
1032aa4dd815SMatthias Ringwald 
1033aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1034665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1035665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1036665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1037a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
103866c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1039a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1040aa4dd815SMatthias Ringwald     }
1041aa4dd815SMatthias Ringwald     return NULL;
1042aa4dd815SMatthias Ringwald }
1043aa4dd815SMatthias Ringwald 
1044a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1045665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1046665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1047665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1048a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
104966c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1050a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1051ce263fc8SMatthias Ringwald     }
1052ce263fc8SMatthias Ringwald }
1053ce263fc8SMatthias Ringwald 
1054a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1055aa4dd815SMatthias Ringwald     int indicator_index;
1056a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1057aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1058a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1059a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1060a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1061a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1062a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1063a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1064ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
1065aa4dd815SMatthias Ringwald             break;
1066aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1067a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1068a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1069a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1070aa4dd815SMatthias Ringwald             break;
1071aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1072a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1073a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1074aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1075a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1076aa4dd815SMatthias Ringwald             break;
1077aa4dd815SMatthias Ringwald         default:
1078aa4dd815SMatthias Ringwald             break;
1079aa4dd815SMatthias Ringwald     }
1080aa4dd815SMatthias Ringwald     return 0;
1081aa4dd815SMatthias Ringwald }
108239426febSMatthias Ringwald 
108339426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below
108439426febSMatthias Ringwald static void hfp_ag_handle_reject_incoming_call(void){
108539426febSMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
108639426febSMatthias Ringwald     if (!hfp_connection){
108739426febSMatthias Ringwald         log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
108839426febSMatthias Ringwald         return;
108939426febSMatthias Ringwald     }
109039426febSMatthias Ringwald 
109139426febSMatthias Ringwald     hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED);
109239426febSMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
109339426febSMatthias Ringwald     hfp_connection->send_error = 1;
109439426febSMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
109539426febSMatthias Ringwald }
109639426febSMatthias Ringwald 
1097a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1098a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1099aa4dd815SMatthias Ringwald     int indicator_index;
1100d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1101d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1102d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
110374386ee0SMatthias Ringwald 
1104d210d9c4SMatthias Ringwald     //printf("hfp_ag_call_sm event %d \n", event);
1105aa4dd815SMatthias Ringwald     switch (event){
1106aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1107d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1108aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1109d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1110aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1111d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1112d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1113aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
111460ebb071SMilanka Ringwald                             log_info("AG rings");
1115aa4dd815SMatthias Ringwald                             break;
1116aa4dd815SMatthias Ringwald                         default:
11173deb3ec6SMatthias Ringwald                             break;
11183deb3ec6SMatthias Ringwald                     }
11193deb3ec6SMatthias Ringwald                     break;
1120aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1121d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1122aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1123d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1124d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1125aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
112660ebb071SMilanka Ringwald                             log_info("AG call waiting");
1127aa4dd815SMatthias Ringwald                             break;
1128aa4dd815SMatthias Ringwald                         default:
11293deb3ec6SMatthias Ringwald                             break;
11303deb3ec6SMatthias Ringwald                     }
11313deb3ec6SMatthias Ringwald                     break;
1132*7bbeb3adSMilanka Ringwald                 default:
1133*7bbeb3adSMilanka Ringwald                     break;
1134aa4dd815SMatthias Ringwald             }
1135aa4dd815SMatthias Ringwald             break;
1136aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1137d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1138aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1139d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1140aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1141d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1142d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1143d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1144aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
114560ebb071SMilanka Ringwald                             log_info("AG answers call, accept call by GSM");
1146aa4dd815SMatthias Ringwald                             break;
1147aa4dd815SMatthias Ringwald                         default:
11483deb3ec6SMatthias Ringwald                             break;
11493deb3ec6SMatthias Ringwald                     }
1150aa4dd815SMatthias Ringwald                     break;
1151aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1152d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1153aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
115460ebb071SMilanka Ringwald                             log_info("AG: current call is placed on hold, incoming call gets active");
1155d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1156d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1157d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1158ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1159ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1160aa4dd815SMatthias Ringwald                             break;
1161aa4dd815SMatthias Ringwald                         default:
1162aa4dd815SMatthias Ringwald                             break;
1163aa4dd815SMatthias Ringwald                     }
1164aa4dd815SMatthias Ringwald                     break;
1165*7bbeb3adSMilanka Ringwald                 default:
1166*7bbeb3adSMilanka Ringwald                     break;
1167aa4dd815SMatthias Ringwald             }
1168aa4dd815SMatthias Ringwald             break;
1169ce263fc8SMatthias Ringwald 
1170ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1171d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1172ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1173d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1174ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
117560ebb071SMilanka Ringwald                             log_info("AG: joining held call with active call");
1176d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG);
1177d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1178ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1179ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1180ce263fc8SMatthias Ringwald                             break;
1181ce263fc8SMatthias Ringwald                         default:
1182ce263fc8SMatthias Ringwald                             break;
1183ce263fc8SMatthias Ringwald                     }
1184ce263fc8SMatthias Ringwald                     break;
1185ce263fc8SMatthias Ringwald                 default:
1186ce263fc8SMatthias Ringwald                     break;
1187ce263fc8SMatthias Ringwald             }
1188ce263fc8SMatthias Ringwald             break;
1189ce263fc8SMatthias Ringwald 
1190aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1191d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1192aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1193d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1194aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1195d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF);
1196d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1197d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1198a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
11996323f788SMatthias Ringwald                             hfp_connection->ok_pending = 1;
120060ebb071SMilanka Ringwald                             log_info("HF answers call, accept call by GSM");
1201ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1202aa4dd815SMatthias Ringwald                             break;
1203aa4dd815SMatthias Ringwald                         default:
1204aa4dd815SMatthias Ringwald                             break;
1205aa4dd815SMatthias Ringwald                     }
12063deb3ec6SMatthias Ringwald                     break;
12073deb3ec6SMatthias Ringwald                 default:
12083deb3ec6SMatthias Ringwald                     break;
12093deb3ec6SMatthias Ringwald             }
12103deb3ec6SMatthias Ringwald             break;
12113deb3ec6SMatthias Ringwald 
1212ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1213d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1214ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1215d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1216ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1217d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG);
1218ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1219ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1220a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1221ce263fc8SMatthias Ringwald                             // as with regualr call
1222d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1223d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1224ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
122560ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by AG");
1226ce263fc8SMatthias Ringwald                             break;
1227ce263fc8SMatthias Ringwald                         default:
1228ce263fc8SMatthias Ringwald                             break;
1229ce263fc8SMatthias Ringwald                     }
1230ce263fc8SMatthias Ringwald                     break;
1231ce263fc8SMatthias Ringwald                 default:
1232ce263fc8SMatthias Ringwald                     break;
1233ce263fc8SMatthias Ringwald             }
1234ce263fc8SMatthias Ringwald             break;
1235ce263fc8SMatthias Ringwald 
1236ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1237d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1238ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1239d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1240ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1241d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF);
1242ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1243ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1244a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1245ce263fc8SMatthias Ringwald                             // as with regualr call
1246d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1247d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1248a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
124960ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by HF");
1250ce263fc8SMatthias Ringwald                             break;
1251ce263fc8SMatthias Ringwald                         default:
1252ce263fc8SMatthias Ringwald                             break;
1253ce263fc8SMatthias Ringwald                     }
1254ce263fc8SMatthias Ringwald                     break;
1255ce263fc8SMatthias Ringwald                 default:
1256ce263fc8SMatthias Ringwald                     break;
1257ce263fc8SMatthias Ringwald             }
1258ce263fc8SMatthias Ringwald             break;
1259ce263fc8SMatthias Ringwald 
1260ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1261ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1262ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1263ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1264d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG);
1265a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1266ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1267a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
126860ebb071SMilanka Ringwald             log_info("Held Call accepted and active");
1269ce263fc8SMatthias Ringwald             break;
1270ce263fc8SMatthias Ringwald 
1271ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1272ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1273ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1274ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1275d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG);
1276a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1277ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1278a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1279ce263fc8SMatthias Ringwald             // from terminate by ag
1280d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1281ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1282ce263fc8SMatthias Ringwald             break;
1283ce263fc8SMatthias Ringwald 
1284aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1285d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1286aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1287d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1288aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1289d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1290d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1291aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1292aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
129360ebb071SMilanka Ringwald                             log_info("HF Rejected Incoming call, AG terminate call");
1294aa4dd815SMatthias Ringwald                             break;
1295aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1296aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1297d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1298d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1299aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
130060ebb071SMilanka Ringwald                             log_info("AG terminate outgoing call process");
1301202c8a4cSMatthias Ringwald                             break;
1302aa4dd815SMatthias Ringwald                         default:
1303aa4dd815SMatthias Ringwald                             break;
1304aa4dd815SMatthias Ringwald                     }
1305aa4dd815SMatthias Ringwald                     break;
1306aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1307d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1308d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1309aa4dd815SMatthias Ringwald                     hfp_ag_transfer_call_state();
1310a0ffb263SMatthias Ringwald                     hfp_connection->call_state = HFP_CALL_IDLE;
131160ebb071SMilanka Ringwald                     log_info("AG terminate call");
1312aa4dd815SMatthias Ringwald                     break;
1313*7bbeb3adSMilanka Ringwald                 default:
1314*7bbeb3adSMilanka Ringwald                     break;
1315aa4dd815SMatthias Ringwald             }
13160c1b8529SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1317aa4dd815SMatthias Ringwald             break;
13183deb3ec6SMatthias Ringwald 
1319aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1320d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1321aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1322d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1323aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1324d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1325d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1326aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
132760ebb071SMilanka Ringwald                             log_info("AG Rejected Incoming call, AG terminate call");
1328aa4dd815SMatthias Ringwald                             break;
1329aa4dd815SMatthias Ringwald                         default:
1330aa4dd815SMatthias Ringwald                             break;
13313deb3ec6SMatthias Ringwald                     }
1332202c8a4cSMatthias Ringwald                     break;
1333aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1334d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1335d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1336d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1337aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
133860ebb071SMilanka Ringwald                     log_info("AG terminate call");
1339aa4dd815SMatthias Ringwald                     break;
1340aa4dd815SMatthias Ringwald                 default:
13413deb3ec6SMatthias Ringwald                     break;
13423deb3ec6SMatthias Ringwald             }
13433deb3ec6SMatthias Ringwald             break;
1344aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1345d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1346aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1347d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1348aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1349aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
135060ebb071SMilanka Ringwald                             log_info("Incoming call interrupted");
1351aa4dd815SMatthias Ringwald                             break;
1352aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1353aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
135460ebb071SMilanka Ringwald                             log_info("Outgoing call interrupted\n");
135560ebb071SMilanka Ringwald                             log_info("AG notify call dropped\n");
1356aa4dd815SMatthias Ringwald                             break;
1357aa4dd815SMatthias Ringwald                         default:
13583deb3ec6SMatthias Ringwald                             break;
13593deb3ec6SMatthias Ringwald                     }
1360d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1361d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1362aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
136310c236bfSMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1364aa4dd815SMatthias Ringwald                     break;
1365aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1366ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1367d210d9c4SMatthias Ringwald                         hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1368ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1369a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1370ce263fc8SMatthias Ringwald                     }
1371d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1372d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1373d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1374aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
137560ebb071SMilanka Ringwald                     log_info("AG notify call dropped");
1376aa4dd815SMatthias Ringwald                     break;
1377aa4dd815SMatthias Ringwald                 default:
1378aa4dd815SMatthias Ringwald                     break;
1379aa4dd815SMatthias Ringwald             }
1380aa4dd815SMatthias Ringwald             break;
1381aa4dd815SMatthias Ringwald 
1382aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1383d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1384d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1385a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1386f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1387d210d9c4SMatthias Ringwald                 break;
1388d210d9c4SMatthias Ringwald             }
1389a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]);
13909cae807eSMatthias Ringwald 
1391a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
139274386ee0SMatthias Ringwald 
1393ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1394aa4dd815SMatthias Ringwald             break;
1395aa4dd815SMatthias Ringwald 
13969cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1397d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1398d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1399a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1400f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1401d210d9c4SMatthias Ringwald                 break;
1402d210d9c4SMatthias Ringwald             }
1403d210d9c4SMatthias Ringwald 
140474386ee0SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED);
1405a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
140674386ee0SMatthias Ringwald 
140760ebb071SMilanka Ringwald             log_info("Redial last number");
14089cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1409aa4dd815SMatthias Ringwald 
14109cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
141160ebb071SMilanka Ringwald                 log_info("Last number exists: accept call");
1412ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
14139cae807eSMatthias Ringwald             } else {
141460ebb071SMilanka Ringwald                 log_info("log_infoLast number missing: reject call");
141539426febSMatthias Ringwald                 hfp_ag_handle_reject_incoming_call();
14169cae807eSMatthias Ringwald             }
14179cae807eSMatthias Ringwald             break;
14189cae807eSMatthias Ringwald         }
1419aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
142039426febSMatthias Ringwald             hfp_ag_handle_reject_incoming_call();
1421aa4dd815SMatthias Ringwald             break;
1422aa4dd815SMatthias Ringwald 
1423d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1424a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1425a0ffb263SMatthias Ringwald             if (!hfp_connection){
1426a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1427aa4dd815SMatthias Ringwald                 break;
1428aa4dd815SMatthias Ringwald             }
1429aa4dd815SMatthias Ringwald 
1430a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1431a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1432aa4dd815SMatthias Ringwald 
1433aa4dd815SMatthias Ringwald             // trigger callsetup to be
1434d0c20769SMatthias Ringwald             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1435d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED);
1436d210d9c4SMatthias Ringwald 
1437d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1438aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1439a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1440aa4dd815SMatthias Ringwald 
1441aa4dd815SMatthias Ringwald             // put current call on hold if active
1442d210d9c4SMatthias Ringwald             if (put_call_on_hold){
144360ebb071SMilanka Ringwald                 log_info("AG putting current call on hold for new outgoing calllog_info");
1444d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1445aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1446485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1447aa4dd815SMatthias Ringwald             }
1448aa4dd815SMatthias Ringwald 
1449aa4dd815SMatthias Ringwald             // start audio if needed
1450d97d752dSMilanka Ringwald             hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1451aa4dd815SMatthias Ringwald             break;
1452d210d9c4SMatthias Ringwald         }
1453aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1454a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1455a0ffb263SMatthias Ringwald             if (!hfp_connection){
1456a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1457aa4dd815SMatthias Ringwald                 break;
1458aa4dd815SMatthias Ringwald             }
1459d210d9c4SMatthias Ringwald 
1460d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING);
1461a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1462d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1463aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1464aa4dd815SMatthias Ringwald             break;
1465aa4dd815SMatthias Ringwald 
1466d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1467aa4dd815SMatthias Ringwald             // get outgoing call
1468a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1469a0ffb263SMatthias Ringwald             if (!hfp_connection){
1470a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1471aa4dd815SMatthias Ringwald             }
1472a0ffb263SMatthias Ringwald             if (!hfp_connection){
1473a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1474aa4dd815SMatthias Ringwald                 break;
1475aa4dd815SMatthias Ringwald             }
1476d210d9c4SMatthias Ringwald 
1477d0c20769SMatthias 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;
1478d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED);
1479a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1480d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1481d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1482aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1483aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1484d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1485d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1486aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
14873deb3ec6SMatthias Ringwald             }
14883deb3ec6SMatthias Ringwald             break;
1489d210d9c4SMatthias Ringwald         }
1490d210d9c4SMatthias Ringwald 
1491d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1492d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY);
1493d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1494a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1495a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
149660ebb071SMilanka Ringwald             log_info("AG: Call Waiting, User Busy");
1497d210d9c4SMatthias Ringwald             break;
1498d210d9c4SMatthias Ringwald 
1499d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1500d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1501d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1502d210d9c4SMatthias Ringwald 
1503d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1504d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1505a0ffb263SMatthias Ringwald                 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1506d0c20769SMatthias Ringwald 
1507d0c20769SMatthias Ringwald             }
1508d0c20769SMatthias Ringwald 
1509d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
151060ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Accept new call");
1511d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1512a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1513d210d9c4SMatthias Ringwald             } else {
151460ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Resume held call");
1515d210d9c4SMatthias Ringwald             }
1516d210d9c4SMatthias Ringwald             if (call_held){
1517d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1518a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1519d210d9c4SMatthias Ringwald             }
1520d0c20769SMatthias Ringwald 
1521a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1522d210d9c4SMatthias Ringwald             break;
1523d210d9c4SMatthias Ringwald         }
1524d0c20769SMatthias Ringwald 
1525d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1526d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1527d210d9c4SMatthias Ringwald             // only update if callsetup changed
1528d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1529a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1530d0c20769SMatthias Ringwald 
1531d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
153260ebb071SMilanka Ringwald                 log_info("AG: Call on Hold, Accept new call");
1533d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1534a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1535d210d9c4SMatthias Ringwald             } else {
153660ebb071SMilanka Ringwald                 log_info("AG: Swap calls");
1537d210d9c4SMatthias Ringwald             }
1538d0c20769SMatthias Ringwald 
1539d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1540d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1541a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1542a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1543d210d9c4SMatthias Ringwald             break;
1544d210d9c4SMatthias Ringwald         }
1545d0c20769SMatthias Ringwald 
1546d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1547d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1548d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
154960ebb071SMilanka Ringwald                 log_info("AG: Join 3-way-call");
1550d210d9c4SMatthias Ringwald                 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL);
1551d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1552a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1553ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1554d210d9c4SMatthias Ringwald             }
1555a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1556d210d9c4SMatthias Ringwald             break;
1557d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1558d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1559d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS);
156060ebb071SMilanka Ringwald             log_info("AG: Transfer call -> Connect two calls and disconnect");
1561d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1562d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1563a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1564a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1565a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1566d210d9c4SMatthias Ringwald             break;
1567ce263fc8SMatthias Ringwald 
15683deb3ec6SMatthias Ringwald         default:
15693deb3ec6SMatthias Ringwald             break;
15703deb3ec6SMatthias Ringwald     }
1571d210d9c4SMatthias Ringwald 
1572d0c20769SMatthias Ringwald 
15733deb3ec6SMatthias Ringwald }
15743deb3ec6SMatthias Ringwald 
15759cae807eSMatthias Ringwald 
1576a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
15779cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
15789cae807eSMatthias Ringwald     if (!active_call) return;
15799cae807eSMatthias Ringwald 
15809cae807eSMatthias Ringwald     int idx = active_call->index;
15819cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
15829cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
15839cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
15849cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
15859cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
15869cae807eSMatthias Ringwald     char * number = active_call->clip_number;
15879cae807eSMatthias Ringwald 
15889cae807eSMatthias Ringwald     char buffer[100];
15899cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
15909cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
15919cae807eSMatthias Ringwald     if (number){
15921cc1d9e9SMilanka Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
15939cae807eSMatthias Ringwald     }
15949cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
159560ebb071SMilanka 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,
15969cae807eSMatthias Ringwald        mode, mpty, type, number);
1597a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
15989cae807eSMatthias Ringwald }
15999cae807eSMatthias Ringwald 
1600473ac565SMatthias Ringwald 
1601febc14f5SMatthias Ringwald // sends pending command, returns if command was sent
1602febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){
160322387625SMatthias Ringwald 
1604a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1605a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1606a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1607a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1608a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
1609febc14f5SMatthias Ringwald             return 1;
16109cae807eSMatthias Ringwald         } else {
1611febc14f5SMatthias 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
1612a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1613a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1614a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16159cae807eSMatthias Ringwald         }
1616ce263fc8SMatthias Ringwald     }
1617ce263fc8SMatthias Ringwald 
1618a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1619a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1620a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1621febc14f5SMatthias Ringwald         return 1;
1622aa4dd815SMatthias Ringwald     }
1623aa4dd815SMatthias Ringwald 
1624a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1625a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1626a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1627a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1628485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1629febc14f5SMatthias Ringwald         return 1;
1630a0ffb263SMatthias Ringwald     }
1631a0ffb263SMatthias Ringwald 
1632a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1633a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1634a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1635485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1636febc14f5SMatthias Ringwald         return 1;
1637aa4dd815SMatthias Ringwald     }
1638aa4dd815SMatthias Ringwald 
1639ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1640a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1641a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1642a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1643485ac19eSMilanka Ringwald         hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1644febc14f5SMatthias Ringwald         return 1;
1645ce263fc8SMatthias Ringwald     }
1646ce263fc8SMatthias Ringwald 
1647a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1648a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1649a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1650485ac19eSMilanka Ringwald         hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1651febc14f5SMatthias Ringwald         return 1;
1652ce263fc8SMatthias Ringwald     }
1653ce263fc8SMatthias Ringwald 
1654aa4dd815SMatthias Ringwald     // update AG indicators
1655a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1656aa4dd815SMatthias Ringwald         int i;
1657a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1658a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1659a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1660a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1661ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1662ce263fc8SMatthias Ringwald                     break;
1663ce263fc8SMatthias Ringwald                 }
1664485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1665febc14f5SMatthias Ringwald                 return 1;
1666aa4dd815SMatthias Ringwald             }
1667aa4dd815SMatthias Ringwald         }
1668aa4dd815SMatthias Ringwald     }
1669aa4dd815SMatthias Ringwald 
1670a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1671a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1672a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1673485ac19eSMilanka Ringwald         hfp_ag_send_ring(hfp_connection->rfcomm_cid);
1674febc14f5SMatthias Ringwald         return 1;
1675aa4dd815SMatthias Ringwald     }
1676aa4dd815SMatthias Ringwald 
1677a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1678a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1679a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1680a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1681febc14f5SMatthias Ringwald         return 1;
1682aa4dd815SMatthias Ringwald     }
1683aa4dd815SMatthias Ringwald 
1684a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1685a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1686a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1687a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1688a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1689febc14f5SMatthias Ringwald         return 1;
1690aa4dd815SMatthias Ringwald     }
1691aa4dd815SMatthias Ringwald 
1692a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1693a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1694a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1695a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1696ce263fc8SMatthias Ringwald         } else {
1697a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1698a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1699485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1700ce263fc8SMatthias Ringwald         }
1701a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1702febc14f5SMatthias Ringwald         return 1;
1703ce263fc8SMatthias Ringwald     }
1704ce263fc8SMatthias Ringwald 
1705a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1706a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1707a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1708485ac19eSMilanka Ringwald         hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1709febc14f5SMatthias Ringwald         return 1;
1710aa4dd815SMatthias Ringwald     }
1711aa4dd815SMatthias Ringwald 
1712a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1713a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1714a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1715485ac19eSMilanka Ringwald         hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
1716febc14f5SMatthias Ringwald         return 1;
17173deb3ec6SMatthias Ringwald     }
17183deb3ec6SMatthias Ringwald 
1719a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1720a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1721485ac19eSMilanka Ringwald         hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1722febc14f5SMatthias Ringwald         return 1;
1723febc14f5SMatthias Ringwald     }
1724febc14f5SMatthias Ringwald 
1725febc14f5SMatthias Ringwald     return 0;
1726febc14f5SMatthias Ringwald }
1727febc14f5SMatthias Ringwald 
1728febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){
1729febc14f5SMatthias Ringwald 
173076cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
173176cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_AG);
1732febc14f5SMatthias Ringwald 
173376cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
173476cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
1735ce263fc8SMatthias Ringwald 
1736febc14f5SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1737febc14f5SMatthias Ringwald         log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1738febc14f5SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1739febc14f5SMatthias Ringwald         return;
1740febc14f5SMatthias Ringwald     }
1741febc14f5SMatthias Ringwald 
1742febc14f5SMatthias Ringwald     int cmd_sent = hfp_ag_send_commands(hfp_connection);
1743febc14f5SMatthias Ringwald 
17442ee45eb2SMilanka Ringwald     // trigger codec exchange if requested by app
17452ee45eb2SMilanka Ringwald     if (hfp_connection->trigger_codec_exchange){
17462ee45eb2SMilanka Ringwald         log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state);
17472ee45eb2SMilanka Ringwald     }
1748febc14f5SMatthias Ringwald 
1749c1ab6cc1SMatthias Ringwald     if (hfp_connection->trigger_codec_exchange && (hfp_connection->command == HFP_CMD_NONE)){
17502ee45eb2SMilanka Ringwald         switch (hfp_connection->codecs_state){
17512ee45eb2SMilanka Ringwald             case HFP_CODECS_IDLE:
17522ee45eb2SMilanka Ringwald             case HFP_CODECS_RECEIVED_LIST:
17532ee45eb2SMilanka Ringwald             case HFP_CODECS_AG_RESEND_COMMON_CODEC:
17542ee45eb2SMilanka Ringwald             case HFP_CODECS_ERROR:
17552ee45eb2SMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 0;
17562ee45eb2SMilanka Ringwald                 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
17572ee45eb2SMilanka Ringwald                 break;
17582ee45eb2SMilanka Ringwald             default:
17592ee45eb2SMilanka Ringwald                 break;
17602ee45eb2SMilanka Ringwald         }
17612ee45eb2SMilanka Ringwald     }
17622ee45eb2SMilanka Ringwald 
1763febc14f5SMatthias Ringwald     if (!cmd_sent){
1764febc14f5SMatthias Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1765febc14f5SMatthias Ringwald     }
1766febc14f5SMatthias Ringwald 
1767c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1768c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
17693deb3ec6SMatthias Ringwald     }
17703deb3ec6SMatthias Ringwald 
1771c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1772c04cf7ffSMilanka Ringwald         cmd_sent = call_setup_state_machine(hfp_connection);
1773aa4dd815SMatthias Ringwald     }
1774aa4dd815SMatthias Ringwald 
1775c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1776c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
1777aa4dd815SMatthias Ringwald     }
17783deb3ec6SMatthias Ringwald 
1779c1ab6cc1SMatthias Ringwald     if ((hfp_connection->command == HFP_CMD_NONE) && !cmd_sent){
1780a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1781a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
17823deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1783a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1784a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
17853deb3ec6SMatthias Ringwald                 break;
17863deb3ec6SMatthias Ringwald             default:
17873deb3ec6SMatthias Ringwald                 break;
17883deb3ec6SMatthias Ringwald         }
17893deb3ec6SMatthias Ringwald     }
1790c04cf7ffSMilanka Ringwald 
1791c04cf7ffSMilanka Ringwald     if (cmd_sent){
1792a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1793473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1794473ac565SMatthias Ringwald     }
17953deb3ec6SMatthias Ringwald }
1796d68dcce1SMatthias Ringwald 
1797ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1798ce263fc8SMatthias Ringwald     int i;
1799a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1800a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1801ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1802ce263fc8SMatthias Ringwald             return indicator;
1803ce263fc8SMatthias Ringwald         }
1804ce263fc8SMatthias Ringwald     }
1805ce263fc8SMatthias Ringwald     return NULL;
1806ce263fc8SMatthias Ringwald }
18073deb3ec6SMatthias Ringwald 
1808e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1809c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
1810e0d09929SMatthias Ringwald }
1811e0d09929SMatthias Ringwald 
1812e9c22d4eSMatthias Ringwald static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
18138a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
18148a46ec40SMatthias Ringwald 
18158a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
18168a46ec40SMatthias Ringwald     if (size < 1) return;
18179ec2630cSMatthias Ringwald 
1818a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1819a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
18201e35c04dSMatthias Ringwald 
1821186dd3d2SMatthias Ringwald     hfp_log_rfcomm_message("HFP_AG_RX", packet, size);
18221e35c04dSMatthias Ringwald 
18238a46ec40SMatthias Ringwald     // process messages byte-wise
18243deb3ec6SMatthias Ringwald     int pos;
18253deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1826a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1827186dd3d2SMatthias Ringwald 
1828e0d09929SMatthias Ringwald         // parse until end of line
1829e0d09929SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1830e0d09929SMatthias Ringwald 
1831186dd3d2SMatthias Ringwald         hfp_generic_status_indicator_t * indicator;
1832a0ffb263SMatthias Ringwald         switch(hfp_connection->command){
1833ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1834ce263fc8SMatthias Ringwald                 if (hfp_ag_response_and_hold_active){
1835a0ffb263SMatthias Ringwald                     hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1836ce263fc8SMatthias Ringwald                 }
1837a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1838ce263fc8SMatthias Ringwald                 break;
1839ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
18400222a807SMatthias Ringwald                 switch(hfp_connection->ag_response_and_hold_action){
1841ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1842a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1843ce263fc8SMatthias Ringwald                         break;
1844ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1845a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1846ce263fc8SMatthias Ringwald                         break;
1847ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1848a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1849ce263fc8SMatthias Ringwald                         break;
1850ce263fc8SMatthias Ringwald                     default:
1851ce263fc8SMatthias Ringwald                         break;
1852ce263fc8SMatthias Ringwald                 }
1853a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1854ce263fc8SMatthias Ringwald                 break;
1855ce263fc8SMatthias Ringwald             case HFP_CMD_HF_INDICATOR_STATUS:
1856a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1857ce263fc8SMatthias Ringwald                 // find indicator by assigned number
1858a0ffb263SMatthias Ringwald                 indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1859ce263fc8SMatthias Ringwald                 if (!indicator){
1860a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
1861ce263fc8SMatthias Ringwald                     break;
1862ce263fc8SMatthias Ringwald                 }
1863ce263fc8SMatthias Ringwald                 switch (indicator->uuid){
1864ce263fc8SMatthias Ringwald                     case 1: // enhanced security
18650222a807SMatthias Ringwald                         if (hfp_connection->parser_indicator_value > 1) {
1866a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1867ce263fc8SMatthias Ringwald                             return;
1868ce263fc8SMatthias Ringwald                         }
18690222a807SMatthias Ringwald                         log_info("HF Indicator 'enhanced security' set to %u", hfp_connection->parser_indicator_value);
1870ce263fc8SMatthias Ringwald                         break;
1871ce263fc8SMatthias Ringwald                     case 2: // battery level
18720222a807SMatthias Ringwald                         if (hfp_connection->parser_indicator_value > 100){
1873a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1874ce263fc8SMatthias Ringwald                             return;
1875ce263fc8SMatthias Ringwald                         }
18760222a807SMatthias Ringwald                         log_info("HF Indicator 'battery' set to %u", hfp_connection->parser_indicator_value);
1877ce263fc8SMatthias Ringwald                         break;
1878ce263fc8SMatthias Ringwald                     default:
18790222a807SMatthias Ringwald                         log_info("HF Indicator unknown set to %u", hfp_connection->parser_indicator_value);
1880ce263fc8SMatthias Ringwald                         break;
1881ce263fc8SMatthias Ringwald                 }
1882a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1883ce263fc8SMatthias Ringwald                 break;
1884ce263fc8SMatthias Ringwald             case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1885ce263fc8SMatthias Ringwald                 // expected by SLC state machine
1886a0ffb263SMatthias Ringwald                 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1887a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
1888a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_status_indicators = 1;
1889ce263fc8SMatthias Ringwald                 break;
1890ce263fc8SMatthias Ringwald             case HFP_CMD_LIST_CURRENT_CALLS:
1891a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1892a0ffb263SMatthias Ringwald                 hfp_connection->next_call_index = 0;
1893a0ffb263SMatthias Ringwald                 hfp_connection->send_status_of_current_calls = 1;
1894ce263fc8SMatthias Ringwald                 break;
1895ce263fc8SMatthias Ringwald             case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1896ce263fc8SMatthias Ringwald                 if (subscriber_numbers_count == 0){
1897485ac19eSMilanka Ringwald                     hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1898ce263fc8SMatthias Ringwald                     break;
1899ce263fc8SMatthias Ringwald                 }
1900a0ffb263SMatthias Ringwald                 hfp_connection->next_subscriber_number_to_send = 0;
1901a0ffb263SMatthias Ringwald                 hfp_connection->send_subscriber_number = 1;
1902ce263fc8SMatthias Ringwald                 break;
1903c1797c7dSMatthias Ringwald             case HFP_CMD_TRANSMIT_DTMF_CODES:
19040222a807SMatthias Ringwald             {
1905a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
19060222a807SMatthias Ringwald                 char buffer[2];
19070222a807SMatthias Ringwald                 buffer[0] = (char) hfp_connection->ag_dtmf_code;
19080222a807SMatthias Ringwald                 buffer[1] = 0;
19090222a807SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer);
1910c1797c7dSMatthias Ringwald                 break;
19110222a807SMatthias Ringwald             }
1912aa4dd815SMatthias Ringwald             case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1913a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1914ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1915aa4dd815SMatthias Ringwald                 break;
1916aa4dd815SMatthias Ringwald             case HFP_CMD_TURN_OFF_EC_AND_NR:
1917a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1918aa4dd815SMatthias Ringwald                 if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1919a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
1920a0ffb263SMatthias Ringwald                     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
192160ebb071SMilanka Ringwald                     log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction);
1922aa4dd815SMatthias Ringwald                 } else {
1923a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
1924aa4dd815SMatthias Ringwald                 }
1925aa4dd815SMatthias Ringwald                 break;
1926aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_ANSWERED:
1927a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
192860ebb071SMilanka Ringwald                 log_info("HFP: ATA");
1929a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1930aa4dd815SMatthias Ringwald                 break;
1931aa4dd815SMatthias Ringwald             case HFP_CMD_HANG_UP_CALL:
1932a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1933a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1934a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1935aa4dd815SMatthias Ringwald                 break;
1936aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_HOLD: {
1937a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1938a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1939d0c20769SMatthias Ringwald 
19400222a807SMatthias Ringwald                 switch (hfp_connection->ag_call_hold_action){
19410222a807SMatthias Ringwald                     case 0:
1942d0c20769SMatthias Ringwald                         // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1943a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1944aa4dd815SMatthias Ringwald                         break;
19450222a807SMatthias Ringwald                     case 1:
1946d0c20769SMatthias Ringwald                         // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1947d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
1948d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
1949a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1950aa4dd815SMatthias Ringwald                         break;
19510222a807SMatthias Ringwald                     case 2:
1952d0c20769SMatthias Ringwald                         // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1953d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
1954d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
1955a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1956aa4dd815SMatthias Ringwald                         break;
19570222a807SMatthias Ringwald                     case 3:
1958d0c20769SMatthias Ringwald                         // Adds a held call to the conversation.
1959a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1960aa4dd815SMatthias Ringwald                         break;
19610222a807SMatthias Ringwald                     case 4:
1962d0c20769SMatthias Ringwald                         // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1963a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1964aa4dd815SMatthias Ringwald                         break;
1965aa4dd815SMatthias Ringwald                     default:
1966aa4dd815SMatthias Ringwald                         break;
1967aa4dd815SMatthias Ringwald                 }
19680222a807SMatthias Ringwald 				hfp_connection->call_index = 0;
196935e92150SMatthias Ringwald                 break;
1970aa4dd815SMatthias Ringwald             }
1971aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_PHONE_NUMBER:
1972a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1973a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
1974aa4dd815SMatthias Ringwald                 break;
1975aa4dd815SMatthias Ringwald             case HFP_CMD_REDIAL_LAST_NUMBER:
1976a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1977a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
1978aa4dd815SMatthias Ringwald                 break;
1979aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CLIP:
1980a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1981a0ffb263SMatthias Ringwald                 log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
1982a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1983aa4dd815SMatthias Ringwald                 break;
1984aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1985a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1986a0ffb263SMatthias Ringwald                 log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
1987a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1988aa4dd815SMatthias Ringwald                 break;
1989aa4dd815SMatthias Ringwald             case HFP_CMD_SET_SPEAKER_GAIN:
1990a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1991a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
199260ebb071SMilanka Ringwald                 log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
1993aa4dd815SMatthias Ringwald                 break;
1994aa4dd815SMatthias Ringwald             case HFP_CMD_SET_MICROPHONE_GAIN:
1995a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1996a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
199760ebb071SMilanka Ringwald                 log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
1998aa4dd815SMatthias Ringwald                 break;
1999aa4dd815SMatthias Ringwald             default:
2000aa4dd815SMatthias Ringwald                 break;
20013deb3ec6SMatthias Ringwald         }
20023deb3ec6SMatthias Ringwald     }
20030cef86faSMatthias Ringwald }
20043deb3ec6SMatthias Ringwald 
2005aa4dd815SMatthias Ringwald static void hfp_run(void){
2006d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
2007d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2008d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2009d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
201022387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2011f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
20123deb3ec6SMatthias Ringwald     }
20133deb3ec6SMatthias Ringwald }
20143deb3ec6SMatthias Ringwald 
2015e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
20163deb3ec6SMatthias Ringwald     switch (packet_type){
20173deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
2018e9c22d4eSMatthias Ringwald             hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size);
20193deb3ec6SMatthias Ringwald             break;
20203deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2021e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2022e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2023f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2024e30a6a47SMatthias Ringwald                 return;
2025e30a6a47SMatthias Ringwald             }
202627950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2027aa4dd815SMatthias Ringwald             break;
20283deb3ec6SMatthias Ringwald         default:
20293deb3ec6SMatthias Ringwald             break;
20303deb3ec6SMatthias Ringwald     }
20313deb3ec6SMatthias Ringwald 
20323deb3ec6SMatthias Ringwald     hfp_run();
20333deb3ec6SMatthias Ringwald }
20343deb3ec6SMatthias Ringwald 
2035405014fbSMatthias Ringwald static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2036405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
203705bed9f6SMatthias Ringwald 
203805bed9f6SMatthias Ringwald     // allow for sco established -> ring transition
203905bed9f6SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
204005bed9f6SMatthias Ringwald     if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return;
204105bed9f6SMatthias Ringwald     hfp_run();
2042405014fbSMatthias Ringwald }
2043405014fbSMatthias Ringwald 
2044a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
20453deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
20463deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
20473deb3ec6SMatthias Ringwald         return;
20483deb3ec6SMatthias Ringwald     }
20493deb3ec6SMatthias Ringwald     int i;
2050a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
20513deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
20523deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
20533deb3ec6SMatthias Ringwald     }
2054a0ffb263SMatthias Ringwald }
20553deb3ec6SMatthias Ringwald 
2056a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2057a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2058a0ffb263SMatthias Ringwald }
20593deb3ec6SMatthias Ringwald 
2060a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2061a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
20626535961aSMatthias Ringwald     (void)memcpy(hfp_ag_indicators, ag_indicators,
20636535961aSMatthias Ringwald                  ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2064a0ffb263SMatthias Ringwald }
20653deb3ec6SMatthias Ringwald 
2066a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
206725789943SMilanka Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return;
2068a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
20696535961aSMatthias Ringwald     (void)memcpy(hfp_generic_status_indicators, hf_indicators,
20706535961aSMatthias Ringwald                  hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2071a0ffb263SMatthias Ringwald }
2072a0ffb263SMatthias Ringwald 
2073a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
20743deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
20756535961aSMatthias Ringwald     (void)memcpy(hfp_ag_call_hold_services, call_hold_services,
20766535961aSMatthias Ringwald                  call_hold_services_nr * sizeof(char *));
2077a0ffb263SMatthias Ringwald }
2078a0ffb263SMatthias Ringwald 
2079a0ffb263SMatthias Ringwald 
2080a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
208127950165SMatthias Ringwald 
2082520c92d5SMatthias Ringwald     hfp_init();
2083d63c37a1SMatthias Ringwald 
2084405014fbSMatthias Ringwald     hci_event_callback_registration.callback = &hci_event_packet_handler;
208527950165SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
208627950165SMatthias Ringwald 
2087e9c22d4eSMatthias Ringwald     rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
208827950165SMatthias Ringwald 
208927950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
2090e9c22d4eSMatthias Ringwald     hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler);
2091aa4dd815SMatthias Ringwald 
2092d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2093d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2094d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2095d210d9c4SMatthias Ringwald 
2096d210d9c4SMatthias Ringwald     hfp_gsm_init();
20973deb3ec6SMatthias Ringwald }
20983deb3ec6SMatthias Ringwald 
20993deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
2100323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
21013deb3ec6SMatthias Ringwald }
21023deb3ec6SMatthias Ringwald 
2103d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
21049c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2105a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2106a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2107a33eb0c4SMilanka Ringwald         return;
2108a33eb0c4SMilanka Ringwald     }
2109a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2110f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21113deb3ec6SMatthias Ringwald }
21123deb3ec6SMatthias Ringwald 
2113d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
21149c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2115a0ffb263SMatthias Ringwald     if (!hfp_connection){
2116a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
21173deb3ec6SMatthias Ringwald         return;
21183deb3ec6SMatthias Ringwald     }
2119a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2120a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
21213deb3ec6SMatthias Ringwald         return;
21223deb3ec6SMatthias Ringwald     }
2123a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2124f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21253deb3ec6SMatthias Ringwald }
21263deb3ec6SMatthias Ringwald 
2127a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
21282ee45eb2SMilanka Ringwald     log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state);
2129a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2130a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
21313deb3ec6SMatthias Ringwald 
2132a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2133a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2134d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2135d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2136a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
21377522e673SMatthias Ringwald         // now, pick link settings
2138afac2a04SMilanka Ringwald         hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
21392ee45eb2SMilanka Ringwald         return;
21403deb3ec6SMatthias Ringwald     }
2141aa4dd815SMatthias Ringwald 
21422ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 1;
2143aa4dd815SMatthias Ringwald }
2144aa4dd815SMatthias Ringwald 
2145d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
21469c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2147a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2148a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2149a33eb0c4SMilanka Ringwald         return;
2150a33eb0c4SMilanka Ringwald     }
21512ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 0;
2152a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2153a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2154f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21553deb3ec6SMatthias Ringwald }
21563deb3ec6SMatthias Ringwald 
2157d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
21589c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2159a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2160a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2161a33eb0c4SMilanka Ringwald         return;
2162a33eb0c4SMilanka Ringwald     }
2163a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2164f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21653deb3ec6SMatthias Ringwald }
2166aa4dd815SMatthias Ringwald 
2167aa4dd815SMatthias Ringwald /**
2168aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2169aa4dd815SMatthias Ringwald  */
2170aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2171aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2172aa4dd815SMatthias Ringwald         return;
2173aa4dd815SMatthias Ringwald     }
2174aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2175aa4dd815SMatthias Ringwald 
2176665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2177665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2178665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2179a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
218066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2181a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2182f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2183aa4dd815SMatthias Ringwald     }
2184aa4dd815SMatthias Ringwald }
2185aa4dd815SMatthias Ringwald 
2186aa4dd815SMatthias Ringwald /**
2187aa4dd815SMatthias Ringwald  * @brief Called from GSM
2188aa4dd815SMatthias Ringwald  */
2189aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2190aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2191aa4dd815SMatthias Ringwald }
2192aa4dd815SMatthias Ringwald 
2193aa4dd815SMatthias Ringwald /**
2194aa4dd815SMatthias Ringwald  * @brief number is stored.
2195aa4dd815SMatthias Ringwald  */
2196aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2197d0c20769SMatthias Ringwald     hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number);
2198aa4dd815SMatthias Ringwald }
2199aa4dd815SMatthias Ringwald 
2200aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2201aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2202aa4dd815SMatthias Ringwald }
2203aa4dd815SMatthias Ringwald 
2204aa4dd815SMatthias Ringwald // call from AG UI
2205aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2206aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2207aa4dd815SMatthias Ringwald }
2208aa4dd815SMatthias Ringwald 
2209ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2210ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2211ce263fc8SMatthias Ringwald }
2212ce263fc8SMatthias Ringwald 
2213aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2214aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2215aa4dd815SMatthias Ringwald }
2216aa4dd815SMatthias Ringwald 
2217aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2218aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2219aa4dd815SMatthias Ringwald }
2220aa4dd815SMatthias Ringwald 
2221aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2222aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2223aa4dd815SMatthias Ringwald }
2224aa4dd815SMatthias Ringwald 
2225aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2226aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2227aa4dd815SMatthias Ringwald }
2228aa4dd815SMatthias Ringwald 
2229aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2230aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2231aa4dd815SMatthias Ringwald }
2232ce263fc8SMatthias Ringwald 
2233ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2234ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2235ce263fc8SMatthias Ringwald }
2236ce263fc8SMatthias Ringwald 
2237ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2238ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2239ce263fc8SMatthias Ringwald }
2240ce263fc8SMatthias Ringwald 
2241ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2242ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2243ce263fc8SMatthias Ringwald }
2244ce263fc8SMatthias Ringwald 
2245aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2246aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2247aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2248aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2249aa4dd815SMatthias Ringwald 
2250ce263fc8SMatthias Ringwald 
2251665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2252665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2253665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2254a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
225566c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2256a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2257ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2258ce263fc8SMatthias Ringwald             continue;
2259ce263fc8SMatthias Ringwald         }
2260ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2261a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2262f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2263aa4dd815SMatthias Ringwald     }
2264aa4dd815SMatthias Ringwald }
2265aa4dd815SMatthias Ringwald 
2266aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2267aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2268aa4dd815SMatthias Ringwald }
2269aa4dd815SMatthias Ringwald 
2270aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2271aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2272aa4dd815SMatthias Ringwald }
2273aa4dd815SMatthias Ringwald 
2274aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2275aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2276aa4dd815SMatthias Ringwald }
2277aa4dd815SMatthias Ringwald 
2278aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2279aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2280aa4dd815SMatthias Ringwald }
2281aa4dd815SMatthias Ringwald 
2282d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){
2283aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
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     }
2289aa4dd815SMatthias Ringwald 
2290a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
229160ebb071SMilanka Ringwald         log_info("AG cannot acivate voice recognition - not supported by HF");
2292aa4dd815SMatthias Ringwald         return;
2293aa4dd815SMatthias Ringwald     }
2294aa4dd815SMatthias Ringwald 
2295aa4dd815SMatthias Ringwald     if (activate){
2296d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(acl_handle);
2297aa4dd815SMatthias Ringwald     }
2298aa4dd815SMatthias Ringwald 
2299a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2300a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2301f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2302aa4dd815SMatthias Ringwald }
2303aa4dd815SMatthias Ringwald 
2304d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
23059c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2306a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2307a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2308a33eb0c4SMilanka Ringwald         return;
2309a33eb0c4SMilanka Ringwald     }
2310a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2311a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2312a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2313a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2314aa4dd815SMatthias Ringwald     }
2315f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2316aa4dd815SMatthias Ringwald }
2317aa4dd815SMatthias Ringwald 
2318d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
23199c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2320a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2321a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2322a33eb0c4SMilanka Ringwald         return;
2323a33eb0c4SMilanka Ringwald     }
2324a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2325a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2326a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2327aa4dd815SMatthias Ringwald     }
2328f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2329aa4dd815SMatthias Ringwald }
2330aa4dd815SMatthias Ringwald 
2331d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
23329c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2333a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2334a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2335a33eb0c4SMilanka Ringwald         return;
2336a33eb0c4SMilanka Ringwald     }
2337aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2338a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2339aa4dd815SMatthias Ringwald }
2340aa4dd815SMatthias Ringwald 
2341d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
23429c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2343a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2344a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2345a33eb0c4SMilanka Ringwald         return;
2346a33eb0c4SMilanka Ringwald     }
2347a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2348aa4dd815SMatthias Ringwald }
2349aa4dd815SMatthias Ringwald 
2350d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
23519c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2352a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2353a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2354a33eb0c4SMilanka Ringwald         return;
2355a33eb0c4SMilanka Ringwald     }
2356a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2357c1797c7dSMatthias Ringwald }
2358aa4dd815SMatthias Ringwald 
2359ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2360ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2361ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2362ce263fc8SMatthias Ringwald }
2363ce263fc8SMatthias Ringwald 
23649cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
23659cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2366ce263fc8SMatthias Ringwald }
2367ce263fc8SMatthias Ringwald 
2368d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
23699c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2370a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2371a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2372a33eb0c4SMilanka Ringwald         return;
2373a33eb0c4SMilanka Ringwald     }
2374a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2375a0ffb263SMatthias Ringwald 
2376a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2377f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2378a0ffb263SMatthias Ringwald }
2379ce263fc8SMatthias Ringwald 
238076cc1527SMatthias 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){
238176cc1527SMatthias Ringwald 	if (!name){
238276cc1527SMatthias Ringwald 		name = default_hfp_ag_service_name;
238376cc1527SMatthias Ringwald 	}
238476cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
238576cc1527SMatthias Ringwald 
238676cc1527SMatthias Ringwald 	/*
238776cc1527SMatthias Ringwald 	 * 0x01 – Ability to reject a call
238876cc1527SMatthias Ringwald 	 * 0x00 – No ability to reject a call
238976cc1527SMatthias Ringwald 	 */
239076cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
239176cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
239276cc1527SMatthias Ringwald 
239376cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
239476cc1527SMatthias Ringwald 	//
239576cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
239676cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
239776cc1527SMatthias Ringwald 	//
239876cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
239976cc1527SMatthias Ringwald 	//
240076cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
240176cc1527SMatthias Ringwald 	if (wide_band_speech && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){
240276cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
240376cc1527SMatthias Ringwald 	}
240476cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
240576cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
240676cc1527SMatthias Ringwald }
240776cc1527SMatthias Ringwald 
240876cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
240976cc1527SMatthias Ringwald 	if (callback == NULL){
241076cc1527SMatthias Ringwald 		log_error("hfp_ag_register_packet_handler called with NULL callback");
241176cc1527SMatthias Ringwald 		return;
241276cc1527SMatthias Ringwald 	}
241376cc1527SMatthias Ringwald 	hfp_ag_callback = callback;
241476cc1527SMatthias Ringwald 	hfp_set_ag_callback(callback);
241576cc1527SMatthias Ringwald }
2416