xref: /btstack/src/classic/hfp_ag.c (revision 7522e673fc9e0653b30d7022a86522af78036f8d)
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  */
373deb3ec6SMatthias Ringwald 
383deb3ec6SMatthias Ringwald // *****************************************************************************
393deb3ec6SMatthias Ringwald //
4066a048abSMatthias Ringwald // HFP Audio Gateway (AG) unit
413deb3ec6SMatthias Ringwald //
423deb3ec6SMatthias Ringwald // *****************************************************************************
433deb3ec6SMatthias Ringwald 
447907f069SMatthias Ringwald #include "btstack_config.h"
453deb3ec6SMatthias Ringwald 
463deb3ec6SMatthias Ringwald #include <stdint.h>
473deb3ec6SMatthias Ringwald #include <stdio.h>
483deb3ec6SMatthias Ringwald #include <stdlib.h>
493deb3ec6SMatthias Ringwald #include <string.h>
503deb3ec6SMatthias Ringwald 
5156042629SMatthias Ringwald #include "hci_cmd.h"
5282636622SMatthias Ringwald #include "btstack_run_loop.h"
533deb3ec6SMatthias Ringwald 
543deb3ec6SMatthias Ringwald #include "hci.h"
553deb3ec6SMatthias Ringwald #include "btstack_memory.h"
563deb3ec6SMatthias Ringwald #include "hci_dump.h"
573deb3ec6SMatthias Ringwald #include "l2cap.h"
5816ece135SMatthias Ringwald #include "btstack_debug.h"
59e30a6a47SMatthias Ringwald #include "btstack_event.h"
6059c6af15SMatthias Ringwald #include "classic/core.h"
613edc84c5SMatthias Ringwald #include "classic/hfp.h"
623edc84c5SMatthias Ringwald #include "classic/hfp_ag.h"
63023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h"
64efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
65023f2764SMatthias Ringwald #include "classic/sdp_server.h"
66023f2764SMatthias Ringwald #include "classic/sdp_util.h"
673deb3ec6SMatthias Ringwald 
68c5b64319SMatthias Ringwald // private prototypes
69c5b64319SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
70c5b64319SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *context);
71c5b64319SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * connection);
72c5b64319SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * context);
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 
82c5b64319SMatthias Ringwald // gobals
833deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway";
84aa4dd815SMatthias Ringwald 
853deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
86aa4dd815SMatthias Ringwald 
873deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
883deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
893deb3ec6SMatthias Ringwald 
903deb3ec6SMatthias Ringwald static int  hfp_ag_indicators_nr = 0;
913deb3ec6SMatthias Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_AG_INDICATORS];
923deb3ec6SMatthias Ringwald 
93a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0;
94a0ffb263SMatthias Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_HF_INDICATORS];
95a0ffb263SMatthias Ringwald 
963deb3ec6SMatthias Ringwald static int  hfp_ag_call_hold_services_nr = 0;
973deb3ec6SMatthias Ringwald static char *hfp_ag_call_hold_services[6];
9813839019SMatthias Ringwald static btstack_packet_handler_t hfp_callback;
993deb3ec6SMatthias Ringwald 
100ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
101ce263fc8SMatthias Ringwald static int hfp_ag_response_and_hold_active = 0;
102aa4dd815SMatthias Ringwald 
103ce263fc8SMatthias Ringwald // Subcriber information entries
104ce263fc8SMatthias Ringwald static hfp_phone_number_t * subscriber_numbers = NULL;
105ce263fc8SMatthias Ringwald static int subscriber_numbers_count = 0;
106ce263fc8SMatthias Ringwald 
107c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
10899a10067SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection);
10999a10067SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
11099a10067SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
11199a10067SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection);
1123deb3ec6SMatthias Ringwald 
1133deb3ec6SMatthias Ringwald 
114a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
115a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
116a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
117a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1183deb3ec6SMatthias Ringwald     }
119a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
120a0ffb263SMatthias Ringwald }
121a0ffb263SMatthias Ringwald 
122a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
123a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
124a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
125a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
126a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
127a0ffb263SMatthias Ringwald     }
128a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1293deb3ec6SMatthias Ringwald }
1303deb3ec6SMatthias Ringwald 
131aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
132aa4dd815SMatthias Ringwald     int i;
133aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
134aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
135aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
136aa4dd815SMatthias Ringwald         }
137aa4dd815SMatthias Ringwald     }
138aa4dd815SMatthias Ringwald     return NULL;
139aa4dd815SMatthias Ringwald }
140aa4dd815SMatthias Ringwald 
141aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
142aa4dd815SMatthias Ringwald     int i;
143aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
144aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
145aa4dd815SMatthias Ringwald             return i;
146aa4dd815SMatthias Ringwald         }
147aa4dd815SMatthias Ringwald     }
148aa4dd815SMatthias Ringwald     return -1;
149aa4dd815SMatthias Ringwald }
150aa4dd815SMatthias Ringwald 
1513deb3ec6SMatthias Ringwald 
15213839019SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
1533deb3ec6SMatthias Ringwald     if (callback == NULL){
1543deb3ec6SMatthias Ringwald         log_error("hfp_ag_register_packet_handler called with NULL callback");
1553deb3ec6SMatthias Ringwald         return;
1563deb3ec6SMatthias Ringwald     }
1573deb3ec6SMatthias Ringwald     hfp_callback = callback;
158f4000eebSMatthias Ringwald     hfp_set_callback(callback);
1593deb3ec6SMatthias Ringwald }
1603deb3ec6SMatthias Ringwald 
1610cb5b971SMatthias Ringwald static int use_in_band_tone(void){
162aa4dd815SMatthias Ringwald     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1633deb3ec6SMatthias Ringwald }
1643deb3ec6SMatthias Ringwald 
165a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
166a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1673deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1683deb3ec6SMatthias Ringwald     return hf && ag;
1693deb3ec6SMatthias Ringwald }
1703deb3ec6SMatthias Ringwald 
171a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
172a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1733deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1743deb3ec6SMatthias Ringwald     return hf && ag;
1753deb3ec6SMatthias Ringwald }
1763deb3ec6SMatthias Ringwald 
177a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
178a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
1793deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
1803deb3ec6SMatthias Ringwald     return hf && ag;
1813deb3ec6SMatthias Ringwald }
1823deb3ec6SMatthias Ringwald 
1834f84bf36SMatthias 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){
1843deb3ec6SMatthias Ringwald     if (!name){
1853deb3ec6SMatthias Ringwald         name = default_hfp_ag_service_name;
1863deb3ec6SMatthias Ringwald     }
1872ef54b27SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, SDP_HandsfreeAudioGateway, rfcomm_channel_nr, name);
1883deb3ec6SMatthias Ringwald 
1893deb3ec6SMatthias Ringwald     /*
1903deb3ec6SMatthias Ringwald      * 0x01 – Ability to reject a call
1913deb3ec6SMatthias Ringwald      * 0x00 – No ability to reject a call
1923deb3ec6SMatthias Ringwald      */
193aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
194aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
195aa4dd815SMatthias Ringwald 
1964f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
1974f84bf36SMatthias Ringwald     //
1984f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1994f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
2004f84bf36SMatthias Ringwald     //
2014f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features &0x1f;
2024f84bf36SMatthias Ringwald     if (supported_features & wide_band_speech){
2034f84bf36SMatthias Ringwald         sdp_features |= 1 << 5; // Wide band speech bit
2044f84bf36SMatthias Ringwald     }
205aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
2064f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
207aa4dd815SMatthias Ringwald }
208aa4dd815SMatthias Ringwald 
209aa4dd815SMatthias Ringwald static int hfp_ag_change_in_band_ring_tone_setting_cmd(uint16_t cid){
210aa4dd815SMatthias Ringwald     char buffer[20];
211aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
212aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2133deb3ec6SMatthias Ringwald }
2143deb3ec6SMatthias Ringwald 
2153deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2163deb3ec6SMatthias Ringwald     char buffer[40];
2173deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
2183deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2193deb3ec6SMatthias Ringwald }
2203deb3ec6SMatthias Ringwald 
2213deb3ec6SMatthias Ringwald static int hfp_ag_ok(uint16_t cid){
2223deb3ec6SMatthias Ringwald     char buffer[10];
2233deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nOK\r\n");
2243deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2253deb3ec6SMatthias Ringwald }
2263deb3ec6SMatthias Ringwald 
227aa4dd815SMatthias Ringwald static int hfp_ag_ring(uint16_t cid){
228aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
229aa4dd815SMatthias Ringwald }
230aa4dd815SMatthias Ringwald 
231aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
232aa4dd815SMatthias Ringwald     char buffer[50];
233d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
234aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
235aa4dd815SMatthias Ringwald }
236aa4dd815SMatthias Ringwald 
237ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
238ce263fc8SMatthias Ringwald     char buffer[50];
239ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
240ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
241ce263fc8SMatthias Ringwald }
242ce263fc8SMatthias Ringwald 
243c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
244aa4dd815SMatthias Ringwald     char buffer[50];
245d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
246aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
247aa4dd815SMatthias Ringwald }
248aa4dd815SMatthias Ringwald 
249aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
250aa4dd815SMatthias Ringwald     char buffer[50];
251a0ffb263SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
252aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
253aa4dd815SMatthias Ringwald }
254aa4dd815SMatthias Ringwald 
2553deb3ec6SMatthias Ringwald static int hfp_ag_error(uint16_t cid){
2563deb3ec6SMatthias Ringwald     char buffer[10];
2573deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nERROR\r\n");
2583deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2593deb3ec6SMatthias Ringwald }
2603deb3ec6SMatthias Ringwald 
2613deb3ec6SMatthias Ringwald static int hfp_ag_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2623deb3ec6SMatthias Ringwald     char buffer[20];
2633deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2643deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2653deb3ec6SMatthias Ringwald }
2663deb3ec6SMatthias Ringwald 
267ad902e3dSMatthias Ringwald // fast & small implementation for fixed int size
268ad902e3dSMatthias Ringwald static int string_len_for_uint32(uint32_t i){
269ad902e3dSMatthias Ringwald     if (i <         10) return 1;
270ad902e3dSMatthias Ringwald     if (i <        100) return 2;
271ad902e3dSMatthias Ringwald     if (i <       1000) return 3;
272ad902e3dSMatthias Ringwald     if (i <      10000) return 4;
27374386ee0SMatthias Ringwald     if (i <     100000) return 5;
274ad902e3dSMatthias Ringwald     if (i <    1000000) return 6;
275ad902e3dSMatthias Ringwald     if (i <   10000000) return 7;
276ad902e3dSMatthias Ringwald     if (i <  100000000) return 8;
277ad902e3dSMatthias Ringwald     if (i < 1000000000) return 9;
278ad902e3dSMatthias Ringwald     return 10;
279ad902e3dSMatthias Ringwald }
280ad902e3dSMatthias Ringwald 
281ad902e3dSMatthias Ringwald // get size for indicator string
282a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
283ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
284a0ffb263SMatthias Ringwald     return 8 + strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
285a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
286a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
287ad902e3dSMatthias Ringwald }
288ad902e3dSMatthias Ringwald 
289ad902e3dSMatthias Ringwald // store indicator
290a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
291ad902e3dSMatthias Ringwald     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
292a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
293a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
294a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2953deb3ec6SMatthias Ringwald }
296ad902e3dSMatthias Ringwald 
297ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
298a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
299a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
300ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
301ad902e3dSMatthias Ringwald     return 3 + (num_indicators-1) * 2;
3023deb3ec6SMatthias Ringwald }
303ad902e3dSMatthias Ringwald 
304ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
305a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
306ad902e3dSMatthias Ringwald     if (index == 0) {
307ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
308ad902e3dSMatthias Ringwald     }
309ad902e3dSMatthias Ringwald     index--;
310a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
311ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
312ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
313a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
314ad902e3dSMatthias Ringwald     }
315ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators - 1){
316ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
317ad902e3dSMatthias Ringwald     }
318ad902e3dSMatthias Ringwald     return 1; // comma
319ad902e3dSMatthias Ringwald }
320ad902e3dSMatthias Ringwald 
321a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
322ad902e3dSMatthias Ringwald     if (index == 0){
323ad902e3dSMatthias Ringwald         *buffer++ = '\r';
32474386ee0SMatthias Ringwald         *buffer++ = '\n';
325ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
326ad902e3dSMatthias Ringwald         memcpy(buffer, HFP_INDICATOR, len);
327ad902e3dSMatthias Ringwald         buffer += len;
328ad902e3dSMatthias Ringwald         *buffer++ = ':';
329ad902e3dSMatthias Ringwald         return;
330ad902e3dSMatthias Ringwald     }
331ad902e3dSMatthias Ringwald     index--;
332a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
333ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
334ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
335a0ffb263SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
336ad902e3dSMatthias Ringwald         return;
337ad902e3dSMatthias Ringwald     }
338ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators-1){
339ad902e3dSMatthias Ringwald         memcpy(buffer, "\r\n\r\nOK\r\n", 8);
340ad902e3dSMatthias Ringwald         return;
341ad902e3dSMatthias Ringwald     }
342ad902e3dSMatthias Ringwald     *buffer = ',';
3433deb3ec6SMatthias Ringwald }
3443deb3ec6SMatthias Ringwald 
3453deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
3463deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3473deb3ec6SMatthias Ringwald     int i;
3483deb3ec6SMatthias Ringwald     int offset = 0;
349a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr-1; i++) {
350a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3513deb3ec6SMatthias Ringwald     }
352a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
353a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3543deb3ec6SMatthias Ringwald     }
3553deb3ec6SMatthias Ringwald     return offset;
3563deb3ec6SMatthias Ringwald }
3573deb3ec6SMatthias Ringwald 
3583deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
359a0ffb263SMatthias Ringwald     if (buffer_size < hfp_generic_status_indicators_nr * 3) return 0;
3603deb3ec6SMatthias Ringwald     int i;
3613deb3ec6SMatthias Ringwald     int offset = 0;
362a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
363a0ffb263SMatthias 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);
3643deb3ec6SMatthias Ringwald     }
3653deb3ec6SMatthias Ringwald     return offset;
3663deb3ec6SMatthias Ringwald }
3673deb3ec6SMatthias Ringwald 
3683deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
3693deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3703deb3ec6SMatthias Ringwald     int i;
3713deb3ec6SMatthias Ringwald     int offset = 0;
3723deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr-1; i++) {
3733deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3743deb3ec6SMatthias Ringwald     }
3753deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3763deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3773deb3ec6SMatthias Ringwald     }
3783deb3ec6SMatthias Ringwald     return offset;
3793deb3ec6SMatthias Ringwald }
3803deb3ec6SMatthias Ringwald 
3813deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
3823deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0;
3833deb3ec6SMatthias Ringwald     int i;
3843deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
3853deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) {
3863deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3873deb3ec6SMatthias Ringwald     }
3883deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3893deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3903deb3ec6SMatthias Ringwald     }
3913deb3ec6SMatthias Ringwald     return offset;
3923deb3ec6SMatthias Ringwald }
3933deb3ec6SMatthias Ringwald 
394a0ffb263SMatthias Ringwald static int hfp_ag_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
395ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
396a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
397a0ffb263SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
3983deb3ec6SMatthias Ringwald 
399ad902e3dSMatthias Ringwald     // assumes: can send now == true
400ad902e3dSMatthias Ringwald     // assumes: num segments > 0
401aba39421SMatthias Ringwald     // assumes: individual segments are smaller than MTU
402ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
403ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
404ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
405ad902e3dSMatthias Ringwald     int offset = 0;
406ad902e3dSMatthias Ringwald     int segment = start_segment;
407ad902e3dSMatthias Ringwald     while (segment < num_segments){
408a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
409aba39421SMatthias Ringwald         if (offset + segment_len > mtu) break;
410aba39421SMatthias Ringwald         // append segement
411a0ffb263SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset);
412ad902e3dSMatthias Ringwald         offset += segment_len;
413ad902e3dSMatthias Ringwald         segment++;
414ad902e3dSMatthias Ringwald     }
415ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
416ad902e3dSMatthias Ringwald     return segment;
417ad902e3dSMatthias Ringwald }
4183deb3ec6SMatthias Ringwald 
419ad902e3dSMatthias Ringwald // returns next segment to store
420a0ffb263SMatthias Ringwald static int hfp_ag_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
421a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
422a0ffb263SMatthias Ringwald     return hfp_ag_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
423ad902e3dSMatthias Ringwald         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
4243deb3ec6SMatthias Ringwald }
4253deb3ec6SMatthias Ringwald 
4263deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_indicators_status_cmd(uint16_t cid){
4273deb3ec6SMatthias Ringwald     char buffer[40];
4283deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_INDICATOR);
4293deb3ec6SMatthias Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, sizeof(buffer)-offset);
4303deb3ec6SMatthias Ringwald 
4313deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4323deb3ec6SMatthias Ringwald 
4333deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4343deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4353deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4363deb3ec6SMatthias Ringwald }
4373deb3ec6SMatthias Ringwald 
4383deb3ec6SMatthias Ringwald static int hfp_ag_set_indicator_status_update_cmd(uint16_t cid, uint8_t activate){
4393deb3ec6SMatthias Ringwald     // AT\r\n%s:3,0,0,%d\r\n
4403deb3ec6SMatthias Ringwald     return hfp_ag_ok(cid);
4413deb3ec6SMatthias Ringwald }
4423deb3ec6SMatthias Ringwald 
4433deb3ec6SMatthias Ringwald 
4443deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_can_hold_call_cmd(uint16_t cid){
445ad902e3dSMatthias Ringwald     char buffer[40];
4463deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
4473deb3ec6SMatthias Ringwald     offset += hfp_ag_call_services_join(buffer+offset, sizeof(buffer)-offset);
4483deb3ec6SMatthias Ringwald 
4493deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4503deb3ec6SMatthias Ringwald 
4513deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4523deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4533deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4543deb3ec6SMatthias Ringwald }
4553deb3ec6SMatthias Ringwald 
4563deb3ec6SMatthias Ringwald 
4573deb3ec6SMatthias Ringwald static int hfp_ag_list_supported_generic_status_indicators_cmd(uint16_t cid){
4583deb3ec6SMatthias Ringwald     return hfp_ag_ok(cid);
4593deb3ec6SMatthias Ringwald }
4603deb3ec6SMatthias Ringwald 
4613deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4623deb3ec6SMatthias Ringwald     char buffer[40];
463aa4dd815SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
4643deb3ec6SMatthias Ringwald     offset += hfp_hf_indicators_join(buffer+offset, sizeof(buffer)-offset);
4653deb3ec6SMatthias Ringwald 
4663deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4673deb3ec6SMatthias Ringwald 
468aa4dd815SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, ")\r\n\r\nOK\r\n");
4693deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4703deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4713deb3ec6SMatthias Ringwald }
4723deb3ec6SMatthias Ringwald 
4733deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4743deb3ec6SMatthias Ringwald     char buffer[40];
4753deb3ec6SMatthias Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer));
4763deb3ec6SMatthias Ringwald 
4773deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4783deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4793deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4803deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4813deb3ec6SMatthias Ringwald }
4823deb3ec6SMatthias Ringwald 
483aa4dd815SMatthias Ringwald static int hfp_ag_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4843deb3ec6SMatthias Ringwald     char buffer[20];
485aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
4863deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4873deb3ec6SMatthias Ringwald }
4883deb3ec6SMatthias Ringwald 
4893deb3ec6SMatthias Ringwald static int hfp_ag_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4903deb3ec6SMatthias Ringwald     char buffer[40];
4913deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
4923deb3ec6SMatthias Ringwald         sprintf(buffer, "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4933deb3ec6SMatthias Ringwald     } else {
4943deb3ec6SMatthias Ringwald         sprintf(buffer, "\r\n%s:%d,%d,%s\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format, op.name);
4953deb3ec6SMatthias Ringwald     }
4963deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4973deb3ec6SMatthias Ringwald }
4983deb3ec6SMatthias Ringwald 
4993deb3ec6SMatthias Ringwald 
5003deb3ec6SMatthias Ringwald static int hfp_ag_cmd_suggest_codec(uint16_t cid, uint8_t codec){
5013deb3ec6SMatthias Ringwald     char buffer[30];
5023deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec);
5033deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
5043deb3ec6SMatthias Ringwald }
5053deb3ec6SMatthias Ringwald 
506aa4dd815SMatthias Ringwald static int hfp_ag_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
507aa4dd815SMatthias Ringwald     char buffer[30];
508aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
509aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
510aa4dd815SMatthias Ringwald }
511aa4dd815SMatthias Ringwald 
512aa4dd815SMatthias Ringwald static int hfp_ag_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
513aa4dd815SMatthias Ringwald     char buffer[30];
514aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_SPEAKER_GAIN, gain);
515aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
516aa4dd815SMatthias Ringwald }
517aa4dd815SMatthias Ringwald 
518aa4dd815SMatthias Ringwald static int hfp_ag_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
519aa4dd815SMatthias Ringwald     char buffer[30];
520aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_MICROPHONE_GAIN, gain);
521aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
522aa4dd815SMatthias Ringwald }
523aa4dd815SMatthias Ringwald 
524ce263fc8SMatthias Ringwald static int hfp_ag_set_response_and_hold(uint16_t cid, int state){
525ce263fc8SMatthias Ringwald     char buffer[30];
526ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: %d\r\n", HFP_RESPONSE_AND_HOLD, state);
527ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
528ce263fc8SMatthias Ringwald }
529ce263fc8SMatthias Ringwald 
530a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
5316a7f44bdSMilanka Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){
5326a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
533df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
5343deb3ec6SMatthias Ringwald         }
5353deb3ec6SMatthias Ringwald     }
536df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
5373deb3ec6SMatthias Ringwald }
5383deb3ec6SMatthias Ringwald 
539*7522e673SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){
540*7522e673SMatthias Ringwald     // determine highest possible link setting
541*7522e673SMatthias Ringwald     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
542*7522e673SMatthias Ringwald     switch (hfp_connection->negotiated_codec){
543*7522e673SMatthias Ringwald         default:
544*7522e673SMatthias Ringwald         case HFP_CODEC_CVSD:
545*7522e673SMatthias Ringwald             if (hci_remote_esco_supported(hfp_connection->acl_handle)){
546*7522e673SMatthias Ringwald                 hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
547*7522e673SMatthias Ringwald                 if ((hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4))
548*7522e673SMatthias Ringwald                 &&  (hfp_supported_features             & (1<<HFP_AGSF_ESCO_S4))){
549*7522e673SMatthias Ringwald                     hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
550*7522e673SMatthias Ringwald                 }
551*7522e673SMatthias Ringwald             }
552*7522e673SMatthias Ringwald             break;
553*7522e673SMatthias Ringwald         case HFP_CODEC_MSBC:
554*7522e673SMatthias Ringwald             if (hci_remote_esco_supported(hfp_connection->acl_handle)){
555*7522e673SMatthias Ringwald                 hfp_connection->link_setting = HFP_LINK_SETTINGS_T2;
556*7522e673SMatthias Ringwald             }
557*7522e673SMatthias Ringwald             break;
558*7522e673SMatthias Ringwald     }
559*7522e673SMatthias Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
560*7522e673SMatthias Ringwald }
561*7522e673SMatthias Ringwald 
562a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
563aa4dd815SMatthias Ringwald     /* events ( == commands):
564aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
565aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
566aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
567aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
568aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
569aa4dd815SMatthias Ringwald     */
570aa4dd815SMatthias Ringwald 
571a0ffb263SMatthias Ringwald      switch (hfp_connection->codecs_state){
572aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
573a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
574aa4dd815SMatthias Ringwald             break;
575aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
576a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
577aa4dd815SMatthias Ringwald             break;
578aa4dd815SMatthias Ringwald         default:
579aa4dd815SMatthias Ringwald             break;
580aa4dd815SMatthias Ringwald     }
581aa4dd815SMatthias Ringwald 
582aa4dd815SMatthias Ringwald     // printf(" -> State machine: CC\n");
583aa4dd815SMatthias Ringwald 
584a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
585aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
586a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
587a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
588a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
589aa4dd815SMatthias Ringwald                 return 1;
590aa4dd815SMatthias Ringwald             }
591aa4dd815SMatthias Ringwald 
592a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
593aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
594aa4dd815SMatthias Ringwald                 case HFP_CODECS_EXCHANGED:
595a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
596aa4dd815SMatthias Ringwald                     break;
597aa4dd815SMatthias Ringwald                 default:
598aa4dd815SMatthias Ringwald                     break;
599aa4dd815SMatthias Ringwald             }
600a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
601aa4dd815SMatthias Ringwald             return 1;
602aa4dd815SMatthias Ringwald 
603aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
604a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
605a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
606aa4dd815SMatthias Ringwald             return 1;
607aa4dd815SMatthias Ringwald 
608aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
609a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
610a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
611a0ffb263SMatthias Ringwald             hfp_ag_cmd_suggest_codec(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
612aa4dd815SMatthias Ringwald             return 1;
613aa4dd815SMatthias Ringwald 
614aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
615a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
616a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
617a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
618aa4dd815SMatthias Ringwald                 return 1;
619aa4dd815SMatthias Ringwald             }
620a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
621a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
62203645b44SMatthias Ringwald             log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
6236a7f44bdSMilanka Ringwald             hfp_emit_codec_event(hfp_callback, 0, hfp_connection->negotiated_codec);
624a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
625*7522e673SMatthias Ringwald             // now, pick link settings
626*7522e673SMatthias Ringwald             hfp_init_link_settings(hfp_connection);
627aa4dd815SMatthias Ringwald             return 1;
628aa4dd815SMatthias Ringwald         default:
629aa4dd815SMatthias Ringwald             break;
630aa4dd815SMatthias Ringwald     }
631aa4dd815SMatthias Ringwald     return 0;
632aa4dd815SMatthias Ringwald }
633aa4dd815SMatthias Ringwald 
634a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
635a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
6364f84bf36SMatthias Ringwald     hfp_emit_connection_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
637aa4dd815SMatthias Ringwald 
638a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
639d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
640a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
641aa4dd815SMatthias Ringwald     }
642ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
643d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS &&
644d0c20769SMatthias Ringwald         hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
645a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
646ce263fc8SMatthias Ringwald     }
647aa4dd815SMatthias Ringwald }
6483deb3ec6SMatthias Ringwald 
649a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
650473ac565SMatthias Ringwald     log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
651a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
6523deb3ec6SMatthias Ringwald     int done = 0;
653a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
6543deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
655a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
6563deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
657aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
658a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
659a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
660aa4dd815SMatthias Ringwald                     } else {
661a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
662aa4dd815SMatthias Ringwald                     }
663a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
664aa4dd815SMatthias Ringwald                     return 1;
6653deb3ec6SMatthias Ringwald                 default:
6663deb3ec6SMatthias Ringwald                     break;
6673deb3ec6SMatthias Ringwald             }
6683deb3ec6SMatthias Ringwald             break;
6693deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
670a0ffb263SMatthias Ringwald             done = codecs_exchange_state_machine(hfp_connection);
6713deb3ec6SMatthias Ringwald 
672a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
673a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6743deb3ec6SMatthias Ringwald             }
675aa4dd815SMatthias Ringwald             return done;
676aa4dd815SMatthias Ringwald 
677aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
678a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
679473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
680473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
681473ac565SMatthias Ringwald                 // continue below in state switch
682ad902e3dSMatthias Ringwald             }
683ad902e3dSMatthias Ringwald             break;
684aa4dd815SMatthias Ringwald 
685aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
686a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
687a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
688a0ffb263SMatthias Ringwald             hfp_ag_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
689aa4dd815SMatthias Ringwald             return 1;
690aa4dd815SMatthias Ringwald 
6913deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
692a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
693a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
694a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
695a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
696a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
697aa4dd815SMatthias Ringwald             } else {
698a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
6993deb3ec6SMatthias Ringwald             }
700a0ffb263SMatthias Ringwald             hfp_ag_set_indicator_status_update_cmd(hfp_connection->rfcomm_cid, 1);
701aa4dd815SMatthias Ringwald             return 1;
7023deb3ec6SMatthias Ringwald 
703aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
704a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
705a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
706a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
707aa4dd815SMatthias Ringwald             }
708a0ffb263SMatthias Ringwald             hfp_ag_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
709a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
710a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
711ce263fc8SMatthias Ringwald             }
712aa4dd815SMatthias Ringwald             return 1;
713aa4dd815SMatthias Ringwald 
714aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
715a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
716a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
717a0ffb263SMatthias Ringwald             hfp_ag_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
718aa4dd815SMatthias Ringwald             return 1;
719aa4dd815SMatthias Ringwald 
720aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
721a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
722a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
723a0ffb263SMatthias Ringwald             hfp_ag_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
724aa4dd815SMatthias Ringwald             return 1;
725aa4dd815SMatthias Ringwald 
726aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
727a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
728a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
729a0ffb263SMatthias Ringwald             hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
730aa4dd815SMatthias Ringwald             return 1;
7313deb3ec6SMatthias Ringwald         default:
7323deb3ec6SMatthias Ringwald             break;
7333deb3ec6SMatthias Ringwald     }
734473ac565SMatthias Ringwald 
735473ac565SMatthias Ringwald     switch (hfp_connection->state){
736473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
737473ac565SMatthias Ringwald             int next_segment = hfp_ag_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
738473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
739473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
740473ac565SMatthias Ringwald             if (next_segment < num_segments){
741473ac565SMatthias Ringwald                 // prepare sending of next segment
742473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
743473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
744473ac565SMatthias Ringwald             } else {
745473ac565SMatthias Ringwald                 // done, go to next state
746473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
747473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
748473ac565SMatthias Ringwald             }
749473ac565SMatthias Ringwald             return 1;
750473ac565SMatthias Ringwald         }
751473ac565SMatthias Ringwald         default:
752473ac565SMatthias Ringwald             break;
753473ac565SMatthias Ringwald     }
754473ac565SMatthias Ringwald 
7553deb3ec6SMatthias Ringwald     return done;
7563deb3ec6SMatthias Ringwald }
7573deb3ec6SMatthias Ringwald 
758a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
7593deb3ec6SMatthias Ringwald 
760a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
761aa4dd815SMatthias Ringwald     if (done) return done;
762aa4dd815SMatthias Ringwald 
763a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
764aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
765a0ffb263SMatthias Ringwald             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
766a0ffb263SMatthias Ringwald             hfp_ag_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
767aa4dd815SMatthias Ringwald             return 1;
768aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
769aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
770a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
771a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
772a0ffb263SMatthias Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
773aa4dd815SMatthias Ringwald             } else {
774a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
775aa4dd815SMatthias Ringwald             }
776aa4dd815SMatthias Ringwald             return 1;
777aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
778a0ffb263SMatthias Ringwald             hfp_ag_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
779aa4dd815SMatthias Ringwald             return 1;
780aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
781a0ffb263SMatthias Ringwald             hfp_ag_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
782aa4dd815SMatthias Ringwald             return 1;
783aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
784a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
785a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
786aa4dd815SMatthias Ringwald             } else {
787a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
7883deb3ec6SMatthias Ringwald             }
789aa4dd815SMatthias Ringwald             return 1;
790aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
791a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
792aa4dd815SMatthias Ringwald             return 1;
7933deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
794a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
795a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
796a0ffb263SMatthias Ringwald                 hfp_ag_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
797aa4dd815SMatthias Ringwald                 return 1;
7983deb3ec6SMatthias Ringwald             }
7993deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
800a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
801ce263fc8SMatthias Ringwald             return 1;
8023deb3ec6SMatthias Ringwald         default:
8033deb3ec6SMatthias Ringwald             break;
8043deb3ec6SMatthias Ringwald     }
805aa4dd815SMatthias Ringwald     return 0;
8063deb3ec6SMatthias Ringwald }
8073deb3ec6SMatthias Ringwald 
808a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
809a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
810a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
8113deb3ec6SMatthias Ringwald 
812aa4dd815SMatthias Ringwald 
813a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
814a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
815a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
816a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
817aa4dd815SMatthias Ringwald         return 1;
818aa4dd815SMatthias Ringwald     }
819aa4dd815SMatthias Ringwald 
820a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
821aa4dd815SMatthias Ringwald 
822aa4dd815SMatthias Ringwald     // run codecs exchange
823a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
824aa4dd815SMatthias Ringwald     if (done) return done;
825aa4dd815SMatthias Ringwald 
826a0ffb263SMatthias Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return done;
827a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
828a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
829a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
830eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
831aa4dd815SMatthias Ringwald         return 1;
832aa4dd815SMatthias Ringwald     }
833aa4dd815SMatthias Ringwald     return 0;
834aa4dd815SMatthias Ringwald }
835aa4dd815SMatthias Ringwald 
836ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
837665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
838665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
839aa4dd815SMatthias Ringwald 
840665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
841a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
842a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
843a0ffb263SMatthias Ringwald             return hfp_connection;
844aa4dd815SMatthias Ringwald         }
845aa4dd815SMatthias Ringwald     }
846aa4dd815SMatthias Ringwald     return NULL;
847aa4dd815SMatthias Ringwald }
848aa4dd815SMatthias Ringwald 
849ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
850d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
851d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
852aa4dd815SMatthias Ringwald 
853d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
854a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
855a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
856aa4dd815SMatthias Ringwald 
857a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
858a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
859aa4dd815SMatthias Ringwald 
860a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
861aa4dd815SMatthias Ringwald }
862aa4dd815SMatthias Ringwald 
863a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
864a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
865a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
866a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
867a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
868aa4dd815SMatthias Ringwald }
869aa4dd815SMatthias Ringwald 
870a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
871a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
872a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
873aa4dd815SMatthias Ringwald }
874aa4dd815SMatthias Ringwald 
875aa4dd815SMatthias Ringwald //
876aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
877aa4dd815SMatthias Ringwald //
878aa4dd815SMatthias Ringwald 
879a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
880aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
881a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
882d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
883aa4dd815SMatthias Ringwald     } else {
884a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
885a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
886a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
887a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
888a0ffb263SMatthias Ringwald         hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG);
889aa4dd815SMatthias Ringwald     }
890aa4dd815SMatthias Ringwald }
891aa4dd815SMatthias Ringwald 
892a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
893a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
894a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
895a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
896a0ffb263SMatthias Ringwald     hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_STOP_RINGINIG);
897aa4dd815SMatthias Ringwald }
898aa4dd815SMatthias Ringwald 
899aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
900aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
901aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
902aa4dd815SMatthias Ringwald 
903665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
904665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
905665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
906a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
907a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
908a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
909a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
910d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
911aa4dd815SMatthias Ringwald         }
912a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
913a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
914aa4dd815SMatthias Ringwald         }
915a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
916aa4dd815SMatthias Ringwald     }
917aa4dd815SMatthias Ringwald }
918aa4dd815SMatthias Ringwald 
919aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
920aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
921aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
922aa4dd815SMatthias Ringwald 
923665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
924665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
925665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
926a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
927a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
928a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
929a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
930aa4dd815SMatthias Ringwald     }
931aa4dd815SMatthias Ringwald }
932aa4dd815SMatthias Ringwald 
933aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
934aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("call");
935aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
936aa4dd815SMatthias Ringwald 
937665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
938665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
939665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
940a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
941a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
942a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
943a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
944aa4dd815SMatthias Ringwald     }
945aa4dd815SMatthias Ringwald }
946aa4dd815SMatthias Ringwald 
947aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
948aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callheld");
949aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
950aa4dd815SMatthias Ringwald 
951665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
952665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
953665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
954a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
955a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
956a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
957a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
958aa4dd815SMatthias Ringwald     }
959aa4dd815SMatthias Ringwald }
960aa4dd815SMatthias Ringwald 
961aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
962aa4dd815SMatthias Ringwald 
963aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
964aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
965aa4dd815SMatthias Ringwald 
966665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
967665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
968665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
969a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
970a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
971a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
972aa4dd815SMatthias Ringwald 
973a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
974a0ffb263SMatthias Ringwald         if (hfp_connection == source){
975a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
976aa4dd815SMatthias Ringwald 
977aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
978a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
979aa4dd815SMatthias Ringwald             } else {
980a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
981d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
982aa4dd815SMatthias Ringwald             }
983aa4dd815SMatthias Ringwald 
984a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
985a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
986aa4dd815SMatthias Ringwald 
987aa4dd815SMatthias Ringwald         } else {
988a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
989aa4dd815SMatthias Ringwald         }
990a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
991aa4dd815SMatthias Ringwald     }
992aa4dd815SMatthias Ringwald }
993aa4dd815SMatthias Ringwald 
994aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
995aa4dd815SMatthias Ringwald 
996aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
997aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
998aa4dd815SMatthias Ringwald 
999665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1000665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1001665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1002a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1003a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
1004aa4dd815SMatthias Ringwald 
1005a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1006a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
1007aa4dd815SMatthias Ringwald 
1008a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1009a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1010aa4dd815SMatthias Ringwald 
1011a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1012aa4dd815SMatthias Ringwald         break;  // only single
1013aa4dd815SMatthias Ringwald     }
1014aa4dd815SMatthias Ringwald }
1015aa4dd815SMatthias Ringwald 
1016aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
1017aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1018665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1019665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1020665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1021665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1022aa4dd815SMatthias Ringwald         if (connection->call_state != HFP_CALL_RINGING &&
1023aa4dd815SMatthias Ringwald             connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1024aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
1025aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1026aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
1027aa4dd815SMatthias Ringwald         hfp_run_for_context(connection);
1028aa4dd815SMatthias Ringwald     }
1029aa4dd815SMatthias Ringwald }
1030aa4dd815SMatthias Ringwald 
1031aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1032aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1033aa4dd815SMatthias Ringwald 
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);
1038d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1039a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1040a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1041a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1042a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 1;
1043a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1044aa4dd815SMatthias Ringwald     }
1045a0ffb263SMatthias Ringwald     hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_TERMINATED);
1046aa4dd815SMatthias Ringwald }
1047aa4dd815SMatthias Ringwald 
10480cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
1049aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1050aa4dd815SMatthias Ringwald     if (!indicator){
1051d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
1052aa4dd815SMatthias Ringwald     };
1053d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1054aa4dd815SMatthias Ringwald }
1055aa4dd815SMatthias Ringwald 
10560cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1057d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1058d210d9c4SMatthias Ringwald     if (!indicator){
1059d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
1060d210d9c4SMatthias Ringwald     };
1061d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1062d210d9c4SMatthias Ringwald }
1063d210d9c4SMatthias Ringwald 
10640cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1065aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1066aa4dd815SMatthias Ringwald     if (!indicator){
1067aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
1068aa4dd815SMatthias Ringwald     };
1069d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1070aa4dd815SMatthias Ringwald }
1071aa4dd815SMatthias Ringwald 
1072aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1073665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1074665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1075665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1076a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1077a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
1078a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1079a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1080aa4dd815SMatthias Ringwald     }
1081aa4dd815SMatthias Ringwald }
1082aa4dd815SMatthias Ringwald 
1083aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1084665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1085665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1086665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1087a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1088a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1089aa4dd815SMatthias Ringwald     }
1090aa4dd815SMatthias Ringwald     return NULL;
1091aa4dd815SMatthias Ringwald }
1092aa4dd815SMatthias Ringwald 
1093a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1094665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1095665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1096665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1097a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1098a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1099ce263fc8SMatthias Ringwald     }
1100ce263fc8SMatthias Ringwald }
1101ce263fc8SMatthias Ringwald 
1102a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1103aa4dd815SMatthias Ringwald     int indicator_index;
1104a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1105aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1106a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1107a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1108a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1109a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1110a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1111a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1112a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1113a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG);
1114aa4dd815SMatthias Ringwald             break;
1115aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1116a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1117a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1118a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1119aa4dd815SMatthias Ringwald             break;
1120aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1121a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1122a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1123aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1124a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1125aa4dd815SMatthias Ringwald             break;
1126aa4dd815SMatthias Ringwald         default:
1127aa4dd815SMatthias Ringwald             break;
1128aa4dd815SMatthias Ringwald     }
1129aa4dd815SMatthias Ringwald     return 0;
1130aa4dd815SMatthias Ringwald }
1131a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1132a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1133aa4dd815SMatthias Ringwald     int indicator_index;
1134d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1135d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1136d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
113774386ee0SMatthias Ringwald 
1138d210d9c4SMatthias Ringwald     //printf("hfp_ag_call_sm event %d \n", event);
1139aa4dd815SMatthias Ringwald     switch (event){
1140aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1141d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1142aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1143d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1144aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1145d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1146d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1147aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
1148aa4dd815SMatthias Ringwald                             printf("AG rings\n");
1149aa4dd815SMatthias Ringwald                             break;
1150aa4dd815SMatthias Ringwald                         default:
11513deb3ec6SMatthias Ringwald                             break;
11523deb3ec6SMatthias Ringwald                     }
11533deb3ec6SMatthias Ringwald                     break;
1154aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1155d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1156aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1157d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1158d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1159aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
1160aa4dd815SMatthias Ringwald                             printf("AG call waiting\n");
1161aa4dd815SMatthias Ringwald                             break;
1162aa4dd815SMatthias Ringwald                         default:
11633deb3ec6SMatthias Ringwald                             break;
11643deb3ec6SMatthias Ringwald                     }
11653deb3ec6SMatthias Ringwald                     break;
1166aa4dd815SMatthias Ringwald             }
1167aa4dd815SMatthias Ringwald             break;
1168aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1169d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1170aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1171d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1172aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1173d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1174d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1175d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1176aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
1177aa4dd815SMatthias Ringwald                             printf("AG answers call, accept call by GSM\n");
1178aa4dd815SMatthias Ringwald                             break;
1179aa4dd815SMatthias Ringwald                         default:
11803deb3ec6SMatthias Ringwald                             break;
11813deb3ec6SMatthias Ringwald                     }
1182aa4dd815SMatthias Ringwald                     break;
1183aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1184d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1185aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1186aa4dd815SMatthias Ringwald                             printf("AG: current call is placed on hold, incoming call gets active\n");
1187d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1188d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1189d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1190ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1191ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1192aa4dd815SMatthias Ringwald                             break;
1193aa4dd815SMatthias Ringwald                         default:
1194aa4dd815SMatthias Ringwald                             break;
1195aa4dd815SMatthias Ringwald                     }
1196aa4dd815SMatthias Ringwald                     break;
1197aa4dd815SMatthias Ringwald             }
1198aa4dd815SMatthias Ringwald             break;
1199ce263fc8SMatthias Ringwald 
1200ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1201d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1202ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1203d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1204ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
1205ce263fc8SMatthias Ringwald                             printf("AG: joining held call with active call\n");
1206d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG);
1207d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1208ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1209a0ffb263SMatthias Ringwald                             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL);
1210ce263fc8SMatthias Ringwald                             break;
1211ce263fc8SMatthias Ringwald                         default:
1212ce263fc8SMatthias Ringwald                             break;
1213ce263fc8SMatthias Ringwald                     }
1214ce263fc8SMatthias Ringwald                     break;
1215ce263fc8SMatthias Ringwald                 default:
1216ce263fc8SMatthias Ringwald                     break;
1217ce263fc8SMatthias Ringwald             }
1218ce263fc8SMatthias Ringwald             break;
1219ce263fc8SMatthias Ringwald 
1220aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1221d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1222aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1223d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1224aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1225d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF);
1226d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1227d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1228a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
1229aa4dd815SMatthias Ringwald                             printf("HF answers call, accept call by GSM\n");
1230a0ffb263SMatthias Ringwald                             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_ANSWERED);
1231aa4dd815SMatthias Ringwald                             break;
1232aa4dd815SMatthias Ringwald                         default:
1233aa4dd815SMatthias Ringwald                             break;
1234aa4dd815SMatthias Ringwald                     }
12353deb3ec6SMatthias Ringwald                     break;
12363deb3ec6SMatthias Ringwald                 default:
12373deb3ec6SMatthias Ringwald                     break;
12383deb3ec6SMatthias Ringwald             }
12393deb3ec6SMatthias Ringwald             break;
12403deb3ec6SMatthias Ringwald 
1241ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1242d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1243ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1244d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1245ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1246d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG);
1247ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1248ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1249a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1250ce263fc8SMatthias Ringwald                             // as with regualr call
1251d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1252d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1253ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
1254ce263fc8SMatthias Ringwald                             printf("AG response and hold - hold by AG\n");
1255ce263fc8SMatthias Ringwald                             break;
1256ce263fc8SMatthias Ringwald                         default:
1257ce263fc8SMatthias Ringwald                             break;
1258ce263fc8SMatthias Ringwald                     }
1259ce263fc8SMatthias Ringwald                     break;
1260ce263fc8SMatthias Ringwald                 default:
1261ce263fc8SMatthias Ringwald                     break;
1262ce263fc8SMatthias Ringwald             }
1263ce263fc8SMatthias Ringwald             break;
1264ce263fc8SMatthias Ringwald 
1265ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1266d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1267ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1268d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1269ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1270d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF);
1271ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1272ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1273a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1274ce263fc8SMatthias Ringwald                             // as with regualr call
1275d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1276d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1277a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
1278ce263fc8SMatthias Ringwald                             printf("AG response and hold - hold by HF\n");
1279ce263fc8SMatthias Ringwald                             break;
1280ce263fc8SMatthias Ringwald                         default:
1281ce263fc8SMatthias Ringwald                             break;
1282ce263fc8SMatthias Ringwald                     }
1283ce263fc8SMatthias Ringwald                     break;
1284ce263fc8SMatthias Ringwald                 default:
1285ce263fc8SMatthias Ringwald                     break;
1286ce263fc8SMatthias Ringwald             }
1287ce263fc8SMatthias Ringwald             break;
1288ce263fc8SMatthias Ringwald 
1289ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1290ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1291ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1292ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1293d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG);
1294a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1295ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1296a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1297ce263fc8SMatthias Ringwald             printf("Held Call accepted and active\n");
1298ce263fc8SMatthias Ringwald             break;
1299ce263fc8SMatthias Ringwald 
1300ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1301ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1302ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1303ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1304d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG);
1305a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1306ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1307a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1308ce263fc8SMatthias Ringwald             // from terminate by ag
1309d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1310ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1311ce263fc8SMatthias Ringwald             break;
1312ce263fc8SMatthias Ringwald 
1313aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1314d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1315aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1316d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1317aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1318d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1319d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1320aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1321aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
1322aa4dd815SMatthias Ringwald                             printf("HF Rejected Incoming call, AG terminate call\n");
1323aa4dd815SMatthias Ringwald                             break;
1324aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1325aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1326d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1327d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1328aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1329aa4dd815SMatthias Ringwald                             printf("AG terminate outgoing call process\n");
1330aa4dd815SMatthias Ringwald                         default:
1331aa4dd815SMatthias Ringwald                             break;
1332aa4dd815SMatthias Ringwald                     }
1333aa4dd815SMatthias Ringwald                     break;
1334aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1335d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1336d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1337aa4dd815SMatthias Ringwald                     hfp_ag_transfer_call_state();
1338a0ffb263SMatthias Ringwald                     hfp_connection->call_state = HFP_CALL_IDLE;
1339aa4dd815SMatthias Ringwald                     printf("AG terminate call\n");
1340aa4dd815SMatthias Ringwald                     break;
1341aa4dd815SMatthias Ringwald             }
1342aa4dd815SMatthias Ringwald             break;
13433deb3ec6SMatthias Ringwald 
1344aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
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:
1349d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1350d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1351aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
1352aa4dd815SMatthias Ringwald                             printf("AG Rejected Incoming call, AG terminate call\n");
1353aa4dd815SMatthias Ringwald                             break;
1354aa4dd815SMatthias Ringwald                         default:
1355aa4dd815SMatthias Ringwald                             break;
13563deb3ec6SMatthias Ringwald                     }
1357aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1358d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1359d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1360d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1361aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1362aa4dd815SMatthias Ringwald                     printf("AG terminate call\n");
1363aa4dd815SMatthias Ringwald                     break;
1364aa4dd815SMatthias Ringwald                 default:
13653deb3ec6SMatthias Ringwald                     break;
13663deb3ec6SMatthias Ringwald             }
13673deb3ec6SMatthias Ringwald             break;
1368aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1369d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1370aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1371d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1372aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1373aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
1374aa4dd815SMatthias Ringwald                             printf("Incoming call interrupted\n");
1375aa4dd815SMatthias Ringwald                             break;
1376aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1377aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1378aa4dd815SMatthias Ringwald                             printf("Outgoing call interrupted\n");
1379aa4dd815SMatthias Ringwald                             printf("AG notify call dropped\n");
1380aa4dd815SMatthias Ringwald                             break;
1381aa4dd815SMatthias Ringwald                         default:
13823deb3ec6SMatthias Ringwald                             break;
13833deb3ec6SMatthias Ringwald                     }
1384d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1385d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1386aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
1387aa4dd815SMatthias Ringwald                     break;
1388aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1389ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1390d210d9c4SMatthias Ringwald                         hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1391ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1392a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1393ce263fc8SMatthias Ringwald                     }
1394d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1395d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1396d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1397aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1398aa4dd815SMatthias Ringwald                     printf("AG notify call dropped\n");
1399aa4dd815SMatthias Ringwald                     break;
1400aa4dd815SMatthias Ringwald                 default:
1401aa4dd815SMatthias Ringwald                     break;
1402aa4dd815SMatthias Ringwald             }
1403aa4dd815SMatthias Ringwald             break;
1404aa4dd815SMatthias Ringwald 
1405aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1406d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1407d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1408a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1409a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1410d210d9c4SMatthias Ringwald                 break;
1411d210d9c4SMatthias Ringwald             }
1412a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]);
14139cae807eSMatthias Ringwald 
1414a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
141574386ee0SMatthias Ringwald 
1416a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1417aa4dd815SMatthias Ringwald             break;
1418aa4dd815SMatthias Ringwald 
14199cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1420d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1421d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1422a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1423a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1424d210d9c4SMatthias Ringwald                 break;
1425d210d9c4SMatthias Ringwald             }
1426d210d9c4SMatthias Ringwald 
142774386ee0SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED);
1428a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
142974386ee0SMatthias Ringwald 
14309cae807eSMatthias Ringwald             printf("\nRedial last number");
14319cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1432aa4dd815SMatthias Ringwald 
14339cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
14349cae807eSMatthias Ringwald                 printf("\nLast number exists: accept call");
14359cae807eSMatthias Ringwald                 hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
14369cae807eSMatthias Ringwald             } else {
14379cae807eSMatthias Ringwald                 printf("\nLast number missing: reject call");
14389cae807eSMatthias Ringwald                 hfp_ag_outgoing_call_rejected();
14399cae807eSMatthias Ringwald             }
14409cae807eSMatthias Ringwald             break;
14419cae807eSMatthias Ringwald         }
1442aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
1443a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1444a0ffb263SMatthias Ringwald             if (!hfp_connection){
1445a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1446aa4dd815SMatthias Ringwald                 break;
1447aa4dd815SMatthias Ringwald             }
1448d210d9c4SMatthias Ringwald 
1449d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED);
1450a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1451a0ffb263SMatthias Ringwald             hfp_connection->send_error = 1;
1452a0ffb263SMatthias Ringwald             hfp_run_for_context(hfp_connection);
1453aa4dd815SMatthias Ringwald             break;
1454aa4dd815SMatthias Ringwald 
1455d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1456a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1457a0ffb263SMatthias Ringwald             if (!hfp_connection){
1458a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1459aa4dd815SMatthias Ringwald                 break;
1460aa4dd815SMatthias Ringwald             }
1461aa4dd815SMatthias Ringwald 
1462a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1463a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1464aa4dd815SMatthias Ringwald 
1465aa4dd815SMatthias Ringwald             // trigger callsetup to be
1466d0c20769SMatthias Ringwald             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1467d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED);
1468d210d9c4SMatthias Ringwald 
1469d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1470aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1471a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1472aa4dd815SMatthias Ringwald 
1473aa4dd815SMatthias Ringwald             // put current call on hold if active
1474d210d9c4SMatthias Ringwald             if (put_call_on_hold){
1475aa4dd815SMatthias Ringwald                 printf("AG putting current call on hold for new outgoing call\n");
1476d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1477aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1478a0ffb263SMatthias Ringwald                 hfp_ag_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1479aa4dd815SMatthias Ringwald             }
1480aa4dd815SMatthias Ringwald 
1481aa4dd815SMatthias Ringwald             // start audio if needed
1482d97d752dSMilanka Ringwald             hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1483aa4dd815SMatthias Ringwald             break;
1484d210d9c4SMatthias Ringwald         }
1485aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1486a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1487a0ffb263SMatthias Ringwald             if (!hfp_connection){
1488a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1489aa4dd815SMatthias Ringwald                 break;
1490aa4dd815SMatthias Ringwald             }
1491d210d9c4SMatthias Ringwald 
1492d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING);
1493a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1494d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1495aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1496aa4dd815SMatthias Ringwald             break;
1497aa4dd815SMatthias Ringwald 
1498d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1499aa4dd815SMatthias Ringwald             // get outgoing call
1500a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1501a0ffb263SMatthias Ringwald             if (!hfp_connection){
1502a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1503aa4dd815SMatthias Ringwald             }
1504a0ffb263SMatthias Ringwald             if (!hfp_connection){
1505a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1506aa4dd815SMatthias Ringwald                 break;
1507aa4dd815SMatthias Ringwald             }
1508d210d9c4SMatthias Ringwald 
1509d0c20769SMatthias 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;
1510d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED);
1511a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1512d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1513d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1514aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1515aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1516d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1517d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1518aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
15193deb3ec6SMatthias Ringwald             }
15203deb3ec6SMatthias Ringwald             break;
1521d210d9c4SMatthias Ringwald         }
1522d210d9c4SMatthias Ringwald 
1523d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1524d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY);
1525d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1526a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1527a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1528d210d9c4SMatthias Ringwald             printf("AG: Call Waiting, User Busy\n");
1529d210d9c4SMatthias Ringwald             break;
1530d210d9c4SMatthias Ringwald 
1531d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1532d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1533d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1534d210d9c4SMatthias Ringwald 
1535d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1536d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1537a0ffb263SMatthias Ringwald                 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1538d0c20769SMatthias Ringwald 
1539d0c20769SMatthias Ringwald             }
1540d0c20769SMatthias Ringwald 
1541d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
1542d210d9c4SMatthias Ringwald                 printf("AG: Call Dropped, Accept new call\n");
1543d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1544a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1545d210d9c4SMatthias Ringwald             } else {
1546d210d9c4SMatthias Ringwald                 printf("AG: Call Dropped, Resume held call\n");
1547d210d9c4SMatthias Ringwald             }
1548d210d9c4SMatthias Ringwald             if (call_held){
1549d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1550a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1551d210d9c4SMatthias Ringwald             }
1552d0c20769SMatthias Ringwald 
1553a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1554d210d9c4SMatthias Ringwald             break;
1555d210d9c4SMatthias Ringwald         }
1556d0c20769SMatthias Ringwald 
1557d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1558d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1559d210d9c4SMatthias Ringwald             // only update if callsetup changed
1560d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1561a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1562d0c20769SMatthias Ringwald 
1563d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
1564d210d9c4SMatthias Ringwald                 printf("AG: Call on Hold, Accept new call\n");
1565d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1566a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1567d210d9c4SMatthias Ringwald             } else {
1568d210d9c4SMatthias Ringwald                 printf("AG: Swap calls\n");
1569d210d9c4SMatthias Ringwald             }
1570d0c20769SMatthias Ringwald 
1571d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1572d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1573a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1574a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1575d210d9c4SMatthias Ringwald             break;
1576d210d9c4SMatthias Ringwald         }
1577d0c20769SMatthias Ringwald 
1578d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1579d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1580d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
1581d210d9c4SMatthias Ringwald                 printf("AG: Join 3-way-call\n");
1582d210d9c4SMatthias Ringwald                 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL);
1583d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1584a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1585a0ffb263SMatthias Ringwald                 hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL);
1586d210d9c4SMatthias Ringwald             }
1587a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1588d210d9c4SMatthias Ringwald             break;
1589d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1590d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1591d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS);
1592d210d9c4SMatthias Ringwald             printf("AG: Transfer call -> Connect two calls and disconnect\n");
1593d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1594d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1595a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1596a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1597a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1598d210d9c4SMatthias Ringwald             break;
1599ce263fc8SMatthias Ringwald 
16003deb3ec6SMatthias Ringwald         default:
16013deb3ec6SMatthias Ringwald             break;
16023deb3ec6SMatthias Ringwald     }
1603d210d9c4SMatthias Ringwald 
1604d0c20769SMatthias Ringwald 
16053deb3ec6SMatthias Ringwald }
16063deb3ec6SMatthias Ringwald 
16079cae807eSMatthias Ringwald 
1608a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
16099cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
16109cae807eSMatthias Ringwald     if (!active_call) return;
16119cae807eSMatthias Ringwald 
16129cae807eSMatthias Ringwald     int idx = active_call->index;
16139cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
16149cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
16159cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
16169cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
16179cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
16189cae807eSMatthias Ringwald     char * number = active_call->clip_number;
16199cae807eSMatthias Ringwald 
16209cae807eSMatthias Ringwald     char buffer[100];
16219cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
16229cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
16239cae807eSMatthias Ringwald     if (number){
16249cae807eSMatthias Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset, ", \"%s\",%u", number, type);
16259cae807eSMatthias Ringwald     }
16269cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
16279cae807eSMatthias Ringwald     printf("hfp_ag_send_current_call_status 000 index %d, dir %d, status %d, mode %d, mpty %d, type %d, number %s\n", idx, dir, status,
16289cae807eSMatthias Ringwald        mode, mpty, type, number);
1629a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
16309cae807eSMatthias Ringwald }
16319cae807eSMatthias Ringwald 
1632a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection){
1633473ac565SMatthias Ringwald 
1634473ac565SMatthias Ringwald     log_info("hfp_run_for_context %p", hfp_connection);
1635473ac565SMatthias Ringwald 
1636a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1637473ac565SMatthias Ringwald 
1638724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
1639473ac565SMatthias Ringwald 
1640e30a6a47SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1641473ac565SMatthias Ringwald         log_info("hfp_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1642e30a6a47SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1643e30a6a47SMatthias Ringwald         return;
1644e30a6a47SMatthias Ringwald     }
16453deb3ec6SMatthias Ringwald 
1646a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1647a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1648a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1649a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1650a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
16519cae807eSMatthias Ringwald         } else {
1652a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1653a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1654a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16559cae807eSMatthias Ringwald         }
1656ce263fc8SMatthias Ringwald         return;
1657ce263fc8SMatthias Ringwald     }
1658ce263fc8SMatthias Ringwald 
1659a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1660a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1661a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1662aa4dd815SMatthias Ringwald         return;
1663aa4dd815SMatthias Ringwald     }
1664aa4dd815SMatthias Ringwald 
1665a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1666a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1667a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1668a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1669a0ffb263SMatthias Ringwald         hfp_ag_error(hfp_connection->rfcomm_cid);
1670a0ffb263SMatthias Ringwald         return;
1671a0ffb263SMatthias Ringwald     }
1672a0ffb263SMatthias Ringwald 
1673a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1674a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1675a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1676a0ffb263SMatthias Ringwald         hfp_ag_error(hfp_connection->rfcomm_cid);
1677aa4dd815SMatthias Ringwald         return;
1678aa4dd815SMatthias Ringwald     }
1679aa4dd815SMatthias Ringwald 
1680ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1681a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1682a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1683a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1684a0ffb263SMatthias Ringwald         hfp_ag_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1685ce263fc8SMatthias Ringwald         return;
1686ce263fc8SMatthias Ringwald     }
1687ce263fc8SMatthias Ringwald 
1688a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1689a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1690a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1691a0ffb263SMatthias Ringwald         hfp_ag_ok(hfp_connection->rfcomm_cid);
1692ce263fc8SMatthias Ringwald         return;
1693ce263fc8SMatthias Ringwald     }
1694ce263fc8SMatthias Ringwald 
1695aa4dd815SMatthias Ringwald     // update AG indicators
1696a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1697aa4dd815SMatthias Ringwald         int i;
1698a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1699a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1700a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1701a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1702ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1703ce263fc8SMatthias Ringwald                     break;
1704ce263fc8SMatthias Ringwald                 }
1705a0ffb263SMatthias Ringwald                 hfp_ag_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1706aa4dd815SMatthias Ringwald                 return;
1707aa4dd815SMatthias Ringwald             }
1708aa4dd815SMatthias Ringwald         }
1709aa4dd815SMatthias Ringwald     }
1710aa4dd815SMatthias Ringwald 
1711a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1712a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1713a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1714a0ffb263SMatthias Ringwald         hfp_ag_ring(hfp_connection->rfcomm_cid);
1715aa4dd815SMatthias Ringwald         return;
1716aa4dd815SMatthias Ringwald     }
1717aa4dd815SMatthias Ringwald 
1718a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1719a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1720a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1721a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1722aa4dd815SMatthias Ringwald         return;
1723aa4dd815SMatthias Ringwald     }
1724aa4dd815SMatthias Ringwald 
1725a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1726a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1727a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1728a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1729a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1730aa4dd815SMatthias Ringwald         return;
1731aa4dd815SMatthias Ringwald     }
1732aa4dd815SMatthias Ringwald 
1733a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1734a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1735a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1736a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1737ce263fc8SMatthias Ringwald         } else {
1738a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1739a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1740a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
1741ce263fc8SMatthias Ringwald         }
1742a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1743ce263fc8SMatthias Ringwald     }
1744ce263fc8SMatthias Ringwald 
1745a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1746a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1747a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1748a0ffb263SMatthias Ringwald         hfp_ag_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1749aa4dd815SMatthias Ringwald         return;
1750aa4dd815SMatthias Ringwald     }
1751aa4dd815SMatthias Ringwald 
1752a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1753a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1754a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1755a0ffb263SMatthias Ringwald         hfp_ag_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
17563deb3ec6SMatthias Ringwald         return;
17573deb3ec6SMatthias Ringwald     }
17583deb3ec6SMatthias Ringwald 
1759a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1760a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1761a0ffb263SMatthias Ringwald         hfp_ag_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1762ce263fc8SMatthias Ringwald         return;
1763ce263fc8SMatthias Ringwald     }
1764ce263fc8SMatthias Ringwald 
1765a0ffb263SMatthias Ringwald     int done = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1766aa4dd815SMatthias Ringwald     if (!done){
1767a0ffb263SMatthias Ringwald         done = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
17683deb3ec6SMatthias Ringwald     }
17693deb3ec6SMatthias Ringwald 
1770aa4dd815SMatthias Ringwald     if (!done){
1771a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
1772aa4dd815SMatthias Ringwald     }
1773aa4dd815SMatthias Ringwald 
1774aa4dd815SMatthias Ringwald     if (!done){
1775a0ffb263SMatthias Ringwald         done = hfp_ag_run_for_audio_connection(hfp_connection);
1776aa4dd815SMatthias Ringwald     }
17773deb3ec6SMatthias Ringwald 
1778a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_NONE && !done){
1779a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1780a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
17813deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1782a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1783a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
17843deb3ec6SMatthias Ringwald                 break;
17853deb3ec6SMatthias Ringwald             default:
17863deb3ec6SMatthias Ringwald                 break;
17873deb3ec6SMatthias Ringwald         }
17883deb3ec6SMatthias Ringwald     }
17893deb3ec6SMatthias Ringwald     if (done){
1790a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
17913deb3ec6SMatthias Ringwald     }
1792473ac565SMatthias Ringwald     //
1793473ac565SMatthias Ringwald     if (done) {
1794473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1795473ac565SMatthias Ringwald     }
17963deb3ec6SMatthias Ringwald }
1797d68dcce1SMatthias Ringwald 
1798ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1799ce263fc8SMatthias Ringwald     int i;
1800a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1801a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1802ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1803ce263fc8SMatthias Ringwald             return indicator;
1804ce263fc8SMatthias Ringwald         }
1805ce263fc8SMatthias Ringwald     }
1806ce263fc8SMatthias Ringwald     return NULL;
1807ce263fc8SMatthias Ringwald }
18083deb3ec6SMatthias Ringwald 
1809aa4dd815SMatthias Ringwald static void hfp_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1810a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1811a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
18121e35c04dSMatthias Ringwald 
18131e35c04dSMatthias Ringwald     char last_char = packet[size-1];
18141e35c04dSMatthias Ringwald     packet[size-1] = 0;
18151e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
18161e35c04dSMatthias Ringwald     packet[size-1] = last_char;
18171e35c04dSMatthias Ringwald 
18183deb3ec6SMatthias Ringwald     int pos;
18193deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1820a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1821aa4dd815SMatthias Ringwald     }
1822ce263fc8SMatthias Ringwald     hfp_generic_status_indicator_t * indicator;
1823ce263fc8SMatthias Ringwald     int value;
1824a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
1825ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1826ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_active){
1827a0ffb263SMatthias Ringwald                 hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1828ce263fc8SMatthias Ringwald             }
1829a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1830ce263fc8SMatthias Ringwald             break;
1831ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
1832a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1833ce263fc8SMatthias Ringwald             printf("HF Response and Hold: %u\n", value);
1834ce263fc8SMatthias Ringwald             switch(value){
1835ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1836a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1837ce263fc8SMatthias Ringwald                     break;
1838ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1839a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1840ce263fc8SMatthias Ringwald                     break;
1841ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1842a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1843ce263fc8SMatthias Ringwald                     break;
1844ce263fc8SMatthias Ringwald                 default:
1845ce263fc8SMatthias Ringwald                     break;
1846ce263fc8SMatthias Ringwald             }
1847a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1848ce263fc8SMatthias Ringwald             break;
1849ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
1850a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1851ce263fc8SMatthias Ringwald             // find indicator by assigned number
1852a0ffb263SMatthias Ringwald             indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1853ce263fc8SMatthias Ringwald             if (!indicator){
1854a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1855ce263fc8SMatthias Ringwald                 break;
1856ce263fc8SMatthias Ringwald             }
1857a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1858ce263fc8SMatthias Ringwald             switch (indicator->uuid){
1859ce263fc8SMatthias Ringwald                 case 1: // enhanced security
1860ce263fc8SMatthias Ringwald                     if (value > 1) {
1861a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1862ce263fc8SMatthias Ringwald                         return;
1863ce263fc8SMatthias Ringwald                     }
1864ce263fc8SMatthias Ringwald                     printf("HF Indicator 'enhanced security' set to %u\n", value);
1865ce263fc8SMatthias Ringwald                     break;
1866ce263fc8SMatthias Ringwald                 case 2: // battery level
1867ce263fc8SMatthias Ringwald                     if (value > 100){
1868a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1869ce263fc8SMatthias Ringwald                         return;
1870ce263fc8SMatthias Ringwald                     }
1871ce263fc8SMatthias Ringwald                     printf("HF Indicator 'battery' set to %u\n", value);
1872ce263fc8SMatthias Ringwald                     break;
1873ce263fc8SMatthias Ringwald                 default:
1874ce263fc8SMatthias Ringwald                     printf("HF Indicator unknown set to %u\n", value);
1875ce263fc8SMatthias Ringwald                     break;
1876ce263fc8SMatthias Ringwald             }
1877a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1878ce263fc8SMatthias Ringwald             break;
1879ce263fc8SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1880ce263fc8SMatthias Ringwald             // expected by SLC state machine
1881a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1882a0ffb263SMatthias Ringwald             hfp_connection->send_ag_indicators_segment = 0;
1883a0ffb263SMatthias Ringwald             hfp_connection->send_ag_status_indicators = 1;
1884ce263fc8SMatthias Ringwald             break;
1885ce263fc8SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1886a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1887a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1888a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 1;
1889ce263fc8SMatthias Ringwald             break;
1890ce263fc8SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1891ce263fc8SMatthias Ringwald             if (subscriber_numbers_count == 0){
1892a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
1893ce263fc8SMatthias Ringwald                 break;
1894ce263fc8SMatthias Ringwald             }
1895a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1896a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 1;
1897ce263fc8SMatthias Ringwald             break;
1898c1797c7dSMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
1899a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1900a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]);
1901c1797c7dSMatthias Ringwald             break;
1902aa4dd815SMatthias Ringwald         case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1903a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1904a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1905aa4dd815SMatthias Ringwald             break;
1906aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1907a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1908aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1909a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1910a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
1911a0ffb263SMatthias Ringwald                 printf("AG: EC/NR = %u\n", hfp_connection->ag_echo_and_noise_reduction);
1912aa4dd815SMatthias Ringwald             } else {
1913a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1914aa4dd815SMatthias Ringwald             }
1915aa4dd815SMatthias Ringwald             break;
1916aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_ANSWERED:
1917a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1918aa4dd815SMatthias Ringwald             printf("HFP: ATA\n");
1919a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1920aa4dd815SMatthias Ringwald             break;
1921aa4dd815SMatthias Ringwald         case HFP_CMD_HANG_UP_CALL:
1922a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1923a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1924a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1925aa4dd815SMatthias Ringwald             break;
1926aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_HOLD: {
1927aa4dd815SMatthias Ringwald             // TODO: fully implement this
1928a0ffb263SMatthias Ringwald             log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]);
1929a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1930a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1931a0ffb263SMatthias Ringwald             hfp_connection->call_index = 0;
1932d0c20769SMatthias Ringwald 
1933a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
1934a0ffb263SMatthias Ringwald                 hfp_connection->call_index = atoi((char *)&hfp_connection->line_buffer[1]);
1935d0c20769SMatthias Ringwald             }
1936d210d9c4SMatthias Ringwald 
1937a0ffb263SMatthias Ringwald             switch (hfp_connection->line_buffer[0]){
1938aa4dd815SMatthias Ringwald                 case '0':
1939d0c20769SMatthias Ringwald                     // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1940a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1941aa4dd815SMatthias Ringwald                     break;
1942aa4dd815SMatthias Ringwald                 case '1':
1943d0c20769SMatthias Ringwald                     // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1944d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1945d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1946a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1947aa4dd815SMatthias Ringwald                     break;
1948aa4dd815SMatthias Ringwald                 case '2':
1949d0c20769SMatthias Ringwald                     // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1950d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1951d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1952a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1953aa4dd815SMatthias Ringwald                     break;
1954aa4dd815SMatthias Ringwald                 case '3':
1955d0c20769SMatthias Ringwald                     // Adds a held call to the conversation.
1956a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1957aa4dd815SMatthias Ringwald                     break;
1958aa4dd815SMatthias Ringwald                 case '4':
1959d0c20769SMatthias Ringwald                     // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1960a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1961aa4dd815SMatthias Ringwald                     break;
1962aa4dd815SMatthias Ringwald                 default:
1963aa4dd815SMatthias Ringwald                     break;
1964aa4dd815SMatthias Ringwald             }
1965aa4dd815SMatthias Ringwald             break;
1966aa4dd815SMatthias Ringwald         }
1967aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_PHONE_NUMBER:
1968a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1969a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
1970aa4dd815SMatthias Ringwald             break;
1971aa4dd815SMatthias Ringwald         case HFP_CMD_REDIAL_LAST_NUMBER:
1972a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1973a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
1974aa4dd815SMatthias Ringwald             break;
1975aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
1976a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1977a0ffb263SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0';
1978a0ffb263SMatthias Ringwald             log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
1979a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1980aa4dd815SMatthias Ringwald             break;
1981aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1982a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1983a0ffb263SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0';
1984a0ffb263SMatthias Ringwald             log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
1985a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1986aa4dd815SMatthias Ringwald             break;
1987aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1988a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1989a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1990a0ffb263SMatthias Ringwald             printf("HF speaker gain = %u\n", hfp_connection->speaker_gain);
1991aa4dd815SMatthias Ringwald             break;
1992aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1993a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1994a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1995a0ffb263SMatthias Ringwald             printf("HF microphone gain = %u\n", hfp_connection->microphone_gain);
1996aa4dd815SMatthias Ringwald             break;
1997aa4dd815SMatthias Ringwald         default:
1998aa4dd815SMatthias Ringwald             break;
19993deb3ec6SMatthias Ringwald     }
20003deb3ec6SMatthias Ringwald }
20013deb3ec6SMatthias Ringwald 
2002aa4dd815SMatthias Ringwald static void hfp_run(void){
2003d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
2004d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2005d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2006d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2007a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
20083deb3ec6SMatthias Ringwald     }
20093deb3ec6SMatthias Ringwald }
20103deb3ec6SMatthias Ringwald 
2011ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
20123deb3ec6SMatthias Ringwald     switch (packet_type){
20133deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
2014aa4dd815SMatthias Ringwald             hfp_handle_rfcomm_data(packet_type, channel, packet, size);
20153deb3ec6SMatthias Ringwald             break;
20163deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2017e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2018e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2019e30a6a47SMatthias Ringwald                 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2020e30a6a47SMatthias Ringwald                 return;
2021e30a6a47SMatthias Ringwald             }
2022e30a6a47SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size);
2023aa4dd815SMatthias Ringwald             break;
20243deb3ec6SMatthias Ringwald         default:
20253deb3ec6SMatthias Ringwald             break;
20263deb3ec6SMatthias Ringwald     }
20273deb3ec6SMatthias Ringwald 
20283deb3ec6SMatthias Ringwald     hfp_run();
20293deb3ec6SMatthias Ringwald }
20303deb3ec6SMatthias Ringwald 
20311e35c04dSMatthias Ringwald 
2032a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
20333deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
20343deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
20353deb3ec6SMatthias Ringwald         return;
20363deb3ec6SMatthias Ringwald     }
20373deb3ec6SMatthias Ringwald     int i;
2038a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
20393deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
20403deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
20413deb3ec6SMatthias Ringwald     }
2042a0ffb263SMatthias Ringwald }
20433deb3ec6SMatthias Ringwald 
2044a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2045a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2046a0ffb263SMatthias Ringwald }
20473deb3ec6SMatthias Ringwald 
2048a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2049a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
2050a0ffb263SMatthias Ringwald     memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2051a0ffb263SMatthias Ringwald }
20523deb3ec6SMatthias Ringwald 
2053a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
2054a0ffb263SMatthias Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return;
2055a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
2056a0ffb263SMatthias Ringwald     memcpy(hfp_generic_status_indicators, hf_indicators, hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2057a0ffb263SMatthias Ringwald }
2058a0ffb263SMatthias Ringwald 
2059a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
20603deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
20613deb3ec6SMatthias Ringwald     memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *));
2062a0ffb263SMatthias Ringwald }
2063a0ffb263SMatthias Ringwald 
2064a0ffb263SMatthias Ringwald 
2065a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
2066d63c37a1SMatthias Ringwald     // register for HCI events
2067d63c37a1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
2068d63c37a1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
2069d63c37a1SMatthias Ringwald 
2070d68dcce1SMatthias Ringwald     rfcomm_register_service(&packet_handler, rfcomm_channel_nr, 0xffff);
2071aa4dd815SMatthias Ringwald 
2072d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2073d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2074d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2075d210d9c4SMatthias Ringwald 
2076d68dcce1SMatthias Ringwald     hfp_set_packet_handler_for_rfcomm_connections(&packet_handler);
2077d68dcce1SMatthias Ringwald 
2078d210d9c4SMatthias Ringwald     hfp_gsm_init();
20793deb3ec6SMatthias Ringwald }
20803deb3ec6SMatthias Ringwald 
20813deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
20823deb3ec6SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, SDP_Handsfree);
20833deb3ec6SMatthias Ringwald }
20843deb3ec6SMatthias Ringwald 
2085d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
2086d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2087a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2088a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2089a33eb0c4SMilanka Ringwald         return;
2090a33eb0c4SMilanka Ringwald     }
2091a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2092a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20933deb3ec6SMatthias Ringwald }
20943deb3ec6SMatthias Ringwald 
2095d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
2096d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2097a0ffb263SMatthias Ringwald     if (!hfp_connection){
2098a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
20993deb3ec6SMatthias Ringwald         return;
21003deb3ec6SMatthias Ringwald     }
2101a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2102a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
21033deb3ec6SMatthias Ringwald         return;
21043deb3ec6SMatthias Ringwald     }
2105a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2106a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21073deb3ec6SMatthias Ringwald }
21083deb3ec6SMatthias Ringwald 
2109a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
2110a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2111a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
21123deb3ec6SMatthias Ringwald 
2113a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2114aa4dd815SMatthias Ringwald 
2115a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2116d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2117d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2118a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
2119d6ff09e1SMatthias Ringwald         hfp_emit_codec_event(hfp_callback, 0, hfp_connection->negotiated_codec);
2120*7522e673SMatthias Ringwald         // now, pick link settings
2121*7522e673SMatthias Ringwald         hfp_init_link_settings(hfp_connection);
21223deb3ec6SMatthias Ringwald     }
2123aa4dd815SMatthias Ringwald 
2124a0ffb263SMatthias Ringwald     switch (hfp_connection->codecs_state){
2125aa4dd815SMatthias Ringwald         case HFP_CODECS_IDLE:
2126aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_LIST:
2127aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
2128aa4dd815SMatthias Ringwald         case HFP_CODECS_ERROR:
2129a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
2130aa4dd815SMatthias Ringwald             break;
2131aa4dd815SMatthias Ringwald         default:
2132aa4dd815SMatthias Ringwald             break;
2133aa4dd815SMatthias Ringwald     }
2134aa4dd815SMatthias Ringwald }
2135aa4dd815SMatthias Ringwald 
2136d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
2137d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2138a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2139a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2140a33eb0c4SMilanka Ringwald         return;
2141a33eb0c4SMilanka Ringwald     }
2142a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2143a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2144a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21453deb3ec6SMatthias Ringwald }
21463deb3ec6SMatthias Ringwald 
2147d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
2148d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2149a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2150a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2151a33eb0c4SMilanka Ringwald         return;
2152a33eb0c4SMilanka Ringwald     }
2153a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2154a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21553deb3ec6SMatthias Ringwald }
2156aa4dd815SMatthias Ringwald 
2157aa4dd815SMatthias Ringwald /**
2158aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2159aa4dd815SMatthias Ringwald  */
2160aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2161aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2162aa4dd815SMatthias Ringwald         return;
2163aa4dd815SMatthias Ringwald     }
2164aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2165aa4dd815SMatthias Ringwald 
2166665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2167665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2168665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2169a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2170a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2171a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2172aa4dd815SMatthias Ringwald     }
2173aa4dd815SMatthias Ringwald }
2174aa4dd815SMatthias Ringwald 
2175aa4dd815SMatthias Ringwald /**
2176aa4dd815SMatthias Ringwald  * @brief Called from GSM
2177aa4dd815SMatthias Ringwald  */
2178aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2179aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2180aa4dd815SMatthias Ringwald }
2181aa4dd815SMatthias Ringwald 
2182aa4dd815SMatthias Ringwald /**
2183aa4dd815SMatthias Ringwald  * @brief number is stored.
2184aa4dd815SMatthias Ringwald  */
2185aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2186d0c20769SMatthias Ringwald     hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number);
2187aa4dd815SMatthias Ringwald }
2188aa4dd815SMatthias Ringwald 
2189aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2190aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2191aa4dd815SMatthias Ringwald }
2192aa4dd815SMatthias Ringwald 
2193aa4dd815SMatthias Ringwald // call from AG UI
2194aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2195aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2196aa4dd815SMatthias Ringwald }
2197aa4dd815SMatthias Ringwald 
2198ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2199ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2200ce263fc8SMatthias Ringwald }
2201ce263fc8SMatthias Ringwald 
2202aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2203aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2204aa4dd815SMatthias Ringwald }
2205aa4dd815SMatthias Ringwald 
2206aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2207aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2208aa4dd815SMatthias Ringwald }
2209aa4dd815SMatthias Ringwald 
2210aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2211aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2212aa4dd815SMatthias Ringwald }
2213aa4dd815SMatthias Ringwald 
2214aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2215aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2216aa4dd815SMatthias Ringwald }
2217aa4dd815SMatthias Ringwald 
2218aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2219aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2220aa4dd815SMatthias Ringwald }
2221ce263fc8SMatthias Ringwald 
2222ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2223ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2224ce263fc8SMatthias Ringwald }
2225ce263fc8SMatthias Ringwald 
2226ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2227ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2228ce263fc8SMatthias Ringwald }
2229ce263fc8SMatthias Ringwald 
2230ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2231ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2232ce263fc8SMatthias Ringwald }
2233ce263fc8SMatthias Ringwald 
2234aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2235aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2236aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2237aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2238aa4dd815SMatthias Ringwald 
2239ce263fc8SMatthias Ringwald 
2240665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2241665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2242665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2243a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2244a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2245ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2246ce263fc8SMatthias Ringwald             continue;
2247ce263fc8SMatthias Ringwald         }
2248ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2249a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2250a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2251aa4dd815SMatthias Ringwald     }
2252aa4dd815SMatthias Ringwald }
2253aa4dd815SMatthias Ringwald 
2254aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2255aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2256aa4dd815SMatthias Ringwald }
2257aa4dd815SMatthias Ringwald 
2258aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2259aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2260aa4dd815SMatthias Ringwald }
2261aa4dd815SMatthias Ringwald 
2262aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2263aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2264aa4dd815SMatthias Ringwald }
2265aa4dd815SMatthias Ringwald 
2266aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2267aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2268aa4dd815SMatthias Ringwald }
2269aa4dd815SMatthias Ringwald 
2270d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){
2271aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
2272d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2273a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2274a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2275a33eb0c4SMilanka Ringwald         return;
2276a33eb0c4SMilanka Ringwald     }
2277aa4dd815SMatthias Ringwald 
2278a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
2279aa4dd815SMatthias Ringwald         printf("AG cannot acivate voice recognition - not supported by HF\n");
2280aa4dd815SMatthias Ringwald         return;
2281aa4dd815SMatthias Ringwald     }
2282aa4dd815SMatthias Ringwald 
2283aa4dd815SMatthias Ringwald     if (activate){
2284d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(acl_handle);
2285aa4dd815SMatthias Ringwald     }
2286aa4dd815SMatthias Ringwald 
2287a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2288a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2289a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2290aa4dd815SMatthias Ringwald }
2291aa4dd815SMatthias Ringwald 
2292d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
2293d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2294a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2295a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2296a33eb0c4SMilanka Ringwald         return;
2297a33eb0c4SMilanka Ringwald     }
2298a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2299a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2300a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2301a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2302aa4dd815SMatthias Ringwald     }
2303a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2304aa4dd815SMatthias Ringwald }
2305aa4dd815SMatthias Ringwald 
2306d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
2307d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2308a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2309a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2310a33eb0c4SMilanka Ringwald         return;
2311a33eb0c4SMilanka Ringwald     }
2312a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2313a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2314a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2315aa4dd815SMatthias Ringwald     }
2316a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2317aa4dd815SMatthias Ringwald }
2318aa4dd815SMatthias Ringwald 
2319d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
2320d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2321a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2322a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2323a33eb0c4SMilanka Ringwald         return;
2324a33eb0c4SMilanka Ringwald     }
2325aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2326a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2327aa4dd815SMatthias Ringwald }
2328aa4dd815SMatthias Ringwald 
2329d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
2330d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2331a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2332a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2333a33eb0c4SMilanka Ringwald         return;
2334a33eb0c4SMilanka Ringwald     }
2335a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2336aa4dd815SMatthias Ringwald }
2337aa4dd815SMatthias Ringwald 
2338d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
2339d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2340a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2341a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2342a33eb0c4SMilanka Ringwald         return;
2343a33eb0c4SMilanka Ringwald     }
2344a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2345c1797c7dSMatthias Ringwald }
2346aa4dd815SMatthias Ringwald 
2347ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2348ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2349ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2350ce263fc8SMatthias Ringwald }
2351ce263fc8SMatthias Ringwald 
23529cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
23539cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2354ce263fc8SMatthias Ringwald }
2355ce263fc8SMatthias Ringwald 
2356d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
2357d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2358a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2359a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2360a33eb0c4SMilanka Ringwald         return;
2361a33eb0c4SMilanka Ringwald     }
2362a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2363a0ffb263SMatthias Ringwald 
2364a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2365a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2366a0ffb263SMatthias Ringwald }
2367ce263fc8SMatthias Ringwald 
2368