xref: /btstack/src/classic/hfp_ag.c (revision 99a1006758b1c3699c20f18ef38ff35741fe6eb2)
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"
593edc84c5SMatthias Ringwald #include "classic/hfp.h"
603edc84c5SMatthias Ringwald #include "classic/hfp_ag.h"
61023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h"
62efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
63023f2764SMatthias Ringwald #include "classic/sdp_server.h"
64023f2764SMatthias Ringwald #include "classic/sdp_util.h"
653deb3ec6SMatthias Ringwald 
66c5b64319SMatthias Ringwald // private prototypes
67c5b64319SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
68c5b64319SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *context);
69c5b64319SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * connection);
70c5b64319SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * context);
71c5b64319SMatthias Ringwald 
72c5b64319SMatthias Ringwald // public prototypes
73c5b64319SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators();
74c5b64319SMatthias Ringwald int get_hfp_generic_status_indicators_nr();
75c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr);
76c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr);
77c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context);
78c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context);
79c5b64319SMatthias Ringwald 
80c5b64319SMatthias Ringwald // gobals
813deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway";
82aa4dd815SMatthias Ringwald 
833deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
84aa4dd815SMatthias Ringwald 
853deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
863deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
873deb3ec6SMatthias Ringwald 
883deb3ec6SMatthias Ringwald static int  hfp_ag_indicators_nr = 0;
893deb3ec6SMatthias Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_AG_INDICATORS];
903deb3ec6SMatthias Ringwald 
91a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0;
92a0ffb263SMatthias Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_HF_INDICATORS];
93a0ffb263SMatthias Ringwald 
943deb3ec6SMatthias Ringwald static int  hfp_ag_call_hold_services_nr = 0;
953deb3ec6SMatthias Ringwald static char *hfp_ag_call_hold_services[6];
963deb3ec6SMatthias Ringwald static hfp_callback_t hfp_callback;
973deb3ec6SMatthias Ringwald 
98ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
99ce263fc8SMatthias Ringwald static int hfp_ag_response_and_hold_active = 0;
100aa4dd815SMatthias Ringwald 
101ce263fc8SMatthias Ringwald // Subcriber information entries
102ce263fc8SMatthias Ringwald static hfp_phone_number_t * subscriber_numbers = NULL;
103ce263fc8SMatthias Ringwald static int subscriber_numbers_count = 0;
104ce263fc8SMatthias Ringwald 
105c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
106*99a10067SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection);
107*99a10067SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
108*99a10067SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
109*99a10067SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection);
1103deb3ec6SMatthias Ringwald 
1113deb3ec6SMatthias Ringwald 
112a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
113a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
114a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
115a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1163deb3ec6SMatthias Ringwald     }
117a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
118a0ffb263SMatthias Ringwald }
119a0ffb263SMatthias Ringwald 
120a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
121a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
122a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
123a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
124a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
125a0ffb263SMatthias Ringwald     }
126a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1273deb3ec6SMatthias Ringwald }
1283deb3ec6SMatthias Ringwald 
129aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
130aa4dd815SMatthias Ringwald     int i;
131aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
132aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
133aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
134aa4dd815SMatthias Ringwald         }
135aa4dd815SMatthias Ringwald     }
136aa4dd815SMatthias Ringwald     return NULL;
137aa4dd815SMatthias Ringwald }
138aa4dd815SMatthias Ringwald 
139aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
140aa4dd815SMatthias Ringwald     int i;
141aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
142aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
143aa4dd815SMatthias Ringwald             return i;
144aa4dd815SMatthias Ringwald         }
145aa4dd815SMatthias Ringwald     }
146aa4dd815SMatthias Ringwald     return -1;
147aa4dd815SMatthias Ringwald }
148aa4dd815SMatthias Ringwald 
1493deb3ec6SMatthias Ringwald 
1503deb3ec6SMatthias Ringwald void hfp_ag_register_packet_handler(hfp_callback_t callback){
1513deb3ec6SMatthias Ringwald     if (callback == NULL){
1523deb3ec6SMatthias Ringwald         log_error("hfp_ag_register_packet_handler called with NULL callback");
1533deb3ec6SMatthias Ringwald         return;
1543deb3ec6SMatthias Ringwald     }
1553deb3ec6SMatthias Ringwald     hfp_callback = callback;
156f4000eebSMatthias Ringwald     hfp_set_callback(callback);
1573deb3ec6SMatthias Ringwald }
1583deb3ec6SMatthias Ringwald 
159aa4dd815SMatthias Ringwald static int use_in_band_tone(){
160aa4dd815SMatthias Ringwald     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1613deb3ec6SMatthias Ringwald }
1623deb3ec6SMatthias Ringwald 
163a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
164a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1653deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1663deb3ec6SMatthias Ringwald     return hf && ag;
1673deb3ec6SMatthias Ringwald }
1683deb3ec6SMatthias Ringwald 
169a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
170a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1713deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1723deb3ec6SMatthias Ringwald     return hf && ag;
1733deb3ec6SMatthias Ringwald }
1743deb3ec6SMatthias Ringwald 
175a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
176a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
1773deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
1783deb3ec6SMatthias Ringwald     return hf && ag;
1793deb3ec6SMatthias Ringwald }
1803deb3ec6SMatthias Ringwald 
1819b1c3b4dSMatthias 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){
1823deb3ec6SMatthias Ringwald     if (!name){
1833deb3ec6SMatthias Ringwald         name = default_hfp_ag_service_name;
1843deb3ec6SMatthias Ringwald     }
1852ef54b27SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, SDP_HandsfreeAudioGateway, rfcomm_channel_nr, name);
1863deb3ec6SMatthias Ringwald 
1873deb3ec6SMatthias Ringwald     /*
1883deb3ec6SMatthias Ringwald      * 0x01 – Ability to reject a call
1893deb3ec6SMatthias Ringwald      * 0x00 – No ability to reject a call
1903deb3ec6SMatthias Ringwald      */
191aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
192aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
193aa4dd815SMatthias Ringwald 
194aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
195aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
196aa4dd815SMatthias Ringwald }
197aa4dd815SMatthias Ringwald 
198aa4dd815SMatthias Ringwald static int hfp_ag_change_in_band_ring_tone_setting_cmd(uint16_t cid){
199aa4dd815SMatthias Ringwald     char buffer[20];
200aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
201aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2023deb3ec6SMatthias Ringwald }
2033deb3ec6SMatthias Ringwald 
2043deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2053deb3ec6SMatthias Ringwald     char buffer[40];
2063deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
2073deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2083deb3ec6SMatthias Ringwald }
2093deb3ec6SMatthias Ringwald 
2103deb3ec6SMatthias Ringwald static int hfp_ag_ok(uint16_t cid){
2113deb3ec6SMatthias Ringwald     char buffer[10];
2123deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nOK\r\n");
2133deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2143deb3ec6SMatthias Ringwald }
2153deb3ec6SMatthias Ringwald 
216aa4dd815SMatthias Ringwald static int hfp_ag_ring(uint16_t cid){
217aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
218aa4dd815SMatthias Ringwald }
219aa4dd815SMatthias Ringwald 
220aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
221aa4dd815SMatthias Ringwald     char buffer[50];
222d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
223aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
224aa4dd815SMatthias Ringwald }
225aa4dd815SMatthias Ringwald 
226ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
227ce263fc8SMatthias Ringwald     char buffer[50];
228ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
229ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
230ce263fc8SMatthias Ringwald }
231ce263fc8SMatthias Ringwald 
232c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
233aa4dd815SMatthias Ringwald     char buffer[50];
234d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
235aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
236aa4dd815SMatthias Ringwald }
237aa4dd815SMatthias Ringwald 
238aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
239aa4dd815SMatthias Ringwald     char buffer[50];
240a0ffb263SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
241aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
242aa4dd815SMatthias Ringwald }
243aa4dd815SMatthias Ringwald 
2443deb3ec6SMatthias Ringwald static int hfp_ag_error(uint16_t cid){
2453deb3ec6SMatthias Ringwald     char buffer[10];
2463deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nERROR\r\n");
2473deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2483deb3ec6SMatthias Ringwald }
2493deb3ec6SMatthias Ringwald 
2503deb3ec6SMatthias Ringwald static int hfp_ag_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2513deb3ec6SMatthias Ringwald     char buffer[20];
2523deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2533deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2543deb3ec6SMatthias Ringwald }
2553deb3ec6SMatthias Ringwald 
256ad902e3dSMatthias Ringwald // fast & small implementation for fixed int size
257ad902e3dSMatthias Ringwald static int string_len_for_uint32(uint32_t i){
258ad902e3dSMatthias Ringwald     if (i <         10) return 1;
259ad902e3dSMatthias Ringwald     if (i <        100) return 2;
260ad902e3dSMatthias Ringwald     if (i <       1000) return 3;
261ad902e3dSMatthias Ringwald     if (i <      10000) return 4;
26274386ee0SMatthias Ringwald     if (i <     100000) return 5;
263ad902e3dSMatthias Ringwald     if (i <    1000000) return 6;
264ad902e3dSMatthias Ringwald     if (i <   10000000) return 7;
265ad902e3dSMatthias Ringwald     if (i <  100000000) return 8;
266ad902e3dSMatthias Ringwald     if (i < 1000000000) return 9;
267ad902e3dSMatthias Ringwald     return 10;
268ad902e3dSMatthias Ringwald }
269ad902e3dSMatthias Ringwald 
270ad902e3dSMatthias Ringwald // get size for indicator string
271a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
272ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
273a0ffb263SMatthias Ringwald     return 8 + strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
274a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
275a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
276ad902e3dSMatthias Ringwald }
277ad902e3dSMatthias Ringwald 
278ad902e3dSMatthias Ringwald // store indicator
279a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
280ad902e3dSMatthias Ringwald     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
281a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
282a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
283a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2843deb3ec6SMatthias Ringwald }
285ad902e3dSMatthias Ringwald 
286ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
287a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
288a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
289ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
290ad902e3dSMatthias Ringwald     return 3 + (num_indicators-1) * 2;
2913deb3ec6SMatthias Ringwald }
292ad902e3dSMatthias Ringwald 
293ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
294a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
295ad902e3dSMatthias Ringwald     if (index == 0) {
296ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
297ad902e3dSMatthias Ringwald     }
298ad902e3dSMatthias Ringwald     index--;
299a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
300ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
301ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
302a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
303ad902e3dSMatthias Ringwald     }
304ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators - 1){
305ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
306ad902e3dSMatthias Ringwald     }
307ad902e3dSMatthias Ringwald     return 1; // comma
308ad902e3dSMatthias Ringwald }
309ad902e3dSMatthias Ringwald 
310a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
311ad902e3dSMatthias Ringwald     if (index == 0){
312ad902e3dSMatthias Ringwald         *buffer++ = '\r';
31374386ee0SMatthias Ringwald         *buffer++ = '\n';
314ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
315ad902e3dSMatthias Ringwald         memcpy(buffer, HFP_INDICATOR, len);
316ad902e3dSMatthias Ringwald         buffer += len;
317ad902e3dSMatthias Ringwald         *buffer++ = ':';
318ad902e3dSMatthias Ringwald         return;
319ad902e3dSMatthias Ringwald     }
320ad902e3dSMatthias Ringwald     index--;
321a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
322ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
323ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
324a0ffb263SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
325ad902e3dSMatthias Ringwald         return;
326ad902e3dSMatthias Ringwald     }
327ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators-1){
328ad902e3dSMatthias Ringwald         memcpy(buffer, "\r\n\r\nOK\r\n", 8);
329ad902e3dSMatthias Ringwald         return;
330ad902e3dSMatthias Ringwald     }
331ad902e3dSMatthias Ringwald     *buffer = ',';
3323deb3ec6SMatthias Ringwald }
3333deb3ec6SMatthias Ringwald 
3343deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
3353deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3363deb3ec6SMatthias Ringwald     int i;
3373deb3ec6SMatthias Ringwald     int offset = 0;
338a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr-1; i++) {
339a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3403deb3ec6SMatthias Ringwald     }
341a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
342a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3433deb3ec6SMatthias Ringwald     }
3443deb3ec6SMatthias Ringwald     return offset;
3453deb3ec6SMatthias Ringwald }
3463deb3ec6SMatthias Ringwald 
3473deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
348a0ffb263SMatthias Ringwald     if (buffer_size < hfp_generic_status_indicators_nr * 3) return 0;
3493deb3ec6SMatthias Ringwald     int i;
3503deb3ec6SMatthias Ringwald     int offset = 0;
351a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
352a0ffb263SMatthias 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);
3533deb3ec6SMatthias Ringwald     }
3543deb3ec6SMatthias Ringwald     return offset;
3553deb3ec6SMatthias Ringwald }
3563deb3ec6SMatthias Ringwald 
3573deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
3583deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3593deb3ec6SMatthias Ringwald     int i;
3603deb3ec6SMatthias Ringwald     int offset = 0;
3613deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr-1; i++) {
3623deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3633deb3ec6SMatthias Ringwald     }
3643deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3653deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3663deb3ec6SMatthias Ringwald     }
3673deb3ec6SMatthias Ringwald     return offset;
3683deb3ec6SMatthias Ringwald }
3693deb3ec6SMatthias Ringwald 
3703deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
3713deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0;
3723deb3ec6SMatthias Ringwald     int i;
3733deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
3743deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) {
3753deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3763deb3ec6SMatthias Ringwald     }
3773deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3783deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3793deb3ec6SMatthias Ringwald     }
3803deb3ec6SMatthias Ringwald     return offset;
3813deb3ec6SMatthias Ringwald }
3823deb3ec6SMatthias Ringwald 
383a0ffb263SMatthias Ringwald static int hfp_ag_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
384ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
385a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
386a0ffb263SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
3873deb3ec6SMatthias Ringwald 
388ad902e3dSMatthias Ringwald     // assumes: can send now == true
389ad902e3dSMatthias Ringwald     // assumes: num segments > 0
390ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
391ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
392ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
393ad902e3dSMatthias Ringwald     int offset = 0;
394ad902e3dSMatthias Ringwald     int segment = start_segment;
395ad902e3dSMatthias Ringwald     while (segment < num_segments){
396a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
397ad902e3dSMatthias Ringwald         if (offset + segment_len <= mtu){
398a0ffb263SMatthias Ringwald             store_segment(hfp_connection, segment, data+offset);
399ad902e3dSMatthias Ringwald             offset += segment_len;
400ad902e3dSMatthias Ringwald             segment++;
401ad902e3dSMatthias Ringwald         }
402ad902e3dSMatthias Ringwald     }
403ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
404ad902e3dSMatthias Ringwald     return segment;
405ad902e3dSMatthias Ringwald }
4063deb3ec6SMatthias Ringwald 
407ad902e3dSMatthias Ringwald // returns next segment to store
408a0ffb263SMatthias Ringwald static int hfp_ag_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
409a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
410a0ffb263SMatthias Ringwald     return hfp_ag_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
411ad902e3dSMatthias Ringwald         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
4123deb3ec6SMatthias Ringwald }
4133deb3ec6SMatthias Ringwald 
4143deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_indicators_status_cmd(uint16_t cid){
4153deb3ec6SMatthias Ringwald     char buffer[40];
4163deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_INDICATOR);
4173deb3ec6SMatthias Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, sizeof(buffer)-offset);
4183deb3ec6SMatthias Ringwald 
4193deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4203deb3ec6SMatthias Ringwald 
4213deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4223deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4233deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4243deb3ec6SMatthias Ringwald }
4253deb3ec6SMatthias Ringwald 
4263deb3ec6SMatthias Ringwald static int hfp_ag_set_indicator_status_update_cmd(uint16_t cid, uint8_t activate){
4273deb3ec6SMatthias Ringwald     // AT\r\n%s:3,0,0,%d\r\n
4283deb3ec6SMatthias Ringwald     return hfp_ag_ok(cid);
4293deb3ec6SMatthias Ringwald }
4303deb3ec6SMatthias Ringwald 
4313deb3ec6SMatthias Ringwald 
4323deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_can_hold_call_cmd(uint16_t cid){
433ad902e3dSMatthias Ringwald     char buffer[40];
4343deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
4353deb3ec6SMatthias Ringwald     offset += hfp_ag_call_services_join(buffer+offset, sizeof(buffer)-offset);
4363deb3ec6SMatthias Ringwald 
4373deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4383deb3ec6SMatthias Ringwald 
4393deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4403deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4413deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4423deb3ec6SMatthias Ringwald }
4433deb3ec6SMatthias Ringwald 
4443deb3ec6SMatthias Ringwald 
4453deb3ec6SMatthias Ringwald static int hfp_ag_list_supported_generic_status_indicators_cmd(uint16_t cid){
4463deb3ec6SMatthias Ringwald     return hfp_ag_ok(cid);
4473deb3ec6SMatthias Ringwald }
4483deb3ec6SMatthias Ringwald 
4493deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4503deb3ec6SMatthias Ringwald     char buffer[40];
451aa4dd815SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
4523deb3ec6SMatthias Ringwald     offset += hfp_hf_indicators_join(buffer+offset, sizeof(buffer)-offset);
4533deb3ec6SMatthias Ringwald 
4543deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4553deb3ec6SMatthias Ringwald 
456aa4dd815SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, ")\r\n\r\nOK\r\n");
4573deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4583deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4593deb3ec6SMatthias Ringwald }
4603deb3ec6SMatthias Ringwald 
4613deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4623deb3ec6SMatthias Ringwald     char buffer[40];
4633deb3ec6SMatthias Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer));
4643deb3ec6SMatthias Ringwald 
4653deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4663deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4673deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4683deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4693deb3ec6SMatthias Ringwald }
4703deb3ec6SMatthias Ringwald 
471aa4dd815SMatthias Ringwald static int hfp_ag_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4723deb3ec6SMatthias Ringwald     char buffer[20];
473aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
4743deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4753deb3ec6SMatthias Ringwald }
4763deb3ec6SMatthias Ringwald 
4773deb3ec6SMatthias Ringwald static int hfp_ag_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4783deb3ec6SMatthias Ringwald     char buffer[40];
4793deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
4803deb3ec6SMatthias Ringwald         sprintf(buffer, "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4813deb3ec6SMatthias Ringwald     } else {
4823deb3ec6SMatthias 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);
4833deb3ec6SMatthias Ringwald     }
4843deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4853deb3ec6SMatthias Ringwald }
4863deb3ec6SMatthias Ringwald 
4873deb3ec6SMatthias Ringwald 
4883deb3ec6SMatthias Ringwald static int hfp_ag_cmd_suggest_codec(uint16_t cid, uint8_t codec){
4893deb3ec6SMatthias Ringwald     char buffer[30];
4903deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec);
4913deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4923deb3ec6SMatthias Ringwald }
4933deb3ec6SMatthias Ringwald 
494aa4dd815SMatthias Ringwald static int hfp_ag_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
495aa4dd815SMatthias Ringwald     char buffer[30];
496aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
497aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
498aa4dd815SMatthias Ringwald }
499aa4dd815SMatthias Ringwald 
500aa4dd815SMatthias Ringwald static int hfp_ag_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
501aa4dd815SMatthias Ringwald     char buffer[30];
502aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_SPEAKER_GAIN, gain);
503aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
504aa4dd815SMatthias Ringwald }
505aa4dd815SMatthias Ringwald 
506aa4dd815SMatthias Ringwald static int hfp_ag_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
507aa4dd815SMatthias Ringwald     char buffer[30];
508aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_MICROPHONE_GAIN, gain);
509aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
510aa4dd815SMatthias Ringwald }
511aa4dd815SMatthias Ringwald 
512ce263fc8SMatthias Ringwald static int hfp_ag_set_response_and_hold(uint16_t cid, int state){
513ce263fc8SMatthias Ringwald     char buffer[30];
514ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: %d\r\n", HFP_RESPONSE_AND_HOLD, state);
515ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
516ce263fc8SMatthias Ringwald }
517ce263fc8SMatthias Ringwald 
518aa4dd815SMatthias Ringwald 
519a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
5203deb3ec6SMatthias Ringwald     int i,j;
521aa4dd815SMatthias Ringwald     uint8_t codec = HFP_CODEC_CVSD;
5223deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_codecs_nr; i++){
523a0ffb263SMatthias Ringwald         for (j = 0; j < hfp_connection->remote_codecs_nr; j++){
524a0ffb263SMatthias Ringwald             if (hfp_connection->remote_codecs[j] == hfp_codecs[i]){
525a0ffb263SMatthias Ringwald                 codec = hfp_connection->remote_codecs[j];
5263deb3ec6SMatthias Ringwald                 continue;
5273deb3ec6SMatthias Ringwald             }
5283deb3ec6SMatthias Ringwald         }
5293deb3ec6SMatthias Ringwald     }
5303deb3ec6SMatthias Ringwald     return codec;
5313deb3ec6SMatthias Ringwald }
5323deb3ec6SMatthias Ringwald 
533a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
534aa4dd815SMatthias Ringwald     /* events ( == commands):
535aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
536aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
537aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
538aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
539aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
540aa4dd815SMatthias Ringwald     */
541aa4dd815SMatthias Ringwald 
542a0ffb263SMatthias Ringwald      switch (hfp_connection->codecs_state){
543aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
544a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
545aa4dd815SMatthias Ringwald             break;
546aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
547a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
548aa4dd815SMatthias Ringwald             break;
549aa4dd815SMatthias Ringwald         default:
550aa4dd815SMatthias Ringwald             break;
551aa4dd815SMatthias Ringwald     }
552aa4dd815SMatthias Ringwald 
553aa4dd815SMatthias Ringwald     // printf(" -> State machine: CC\n");
554aa4dd815SMatthias Ringwald 
555a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
556aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
557aa4dd815SMatthias Ringwald             //printf("HFP_CODECS_RECEIVED_LIST \n");
558a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
559a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
560a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
561aa4dd815SMatthias Ringwald                 return 1;
562aa4dd815SMatthias Ringwald             }
563aa4dd815SMatthias Ringwald 
564a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
565aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
566aa4dd815SMatthias Ringwald                 case HFP_CODECS_EXCHANGED:
567a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
568aa4dd815SMatthias Ringwald                     break;
569aa4dd815SMatthias Ringwald                 default:
570aa4dd815SMatthias Ringwald                     break;
571aa4dd815SMatthias Ringwald             }
572a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
573aa4dd815SMatthias Ringwald             return 1;
574aa4dd815SMatthias Ringwald 
575aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
576aa4dd815SMatthias Ringwald             //printf(" HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP \n");
577a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
578a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
579aa4dd815SMatthias Ringwald             return 1;
580aa4dd815SMatthias Ringwald 
581aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
582aa4dd815SMatthias Ringwald             //printf(" HFP_CMD_AG_SEND_COMMON_CODEC \n");
583a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
584a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
585a0ffb263SMatthias Ringwald             hfp_ag_cmd_suggest_codec(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
586aa4dd815SMatthias Ringwald             return 1;
587aa4dd815SMatthias Ringwald 
588aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
589aa4dd815SMatthias Ringwald             //printf("HFP_CMD_HF_CONFIRMED_CODEC \n");
590a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
591a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
592a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
593aa4dd815SMatthias Ringwald                 return 1;
594aa4dd815SMatthias Ringwald             }
595a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
596a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
597aa4dd815SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_CODECS_CONNECTION_COMPLETE, 0);
598a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
599aa4dd815SMatthias Ringwald             return 1;
600aa4dd815SMatthias Ringwald         default:
601aa4dd815SMatthias Ringwald             break;
602aa4dd815SMatthias Ringwald     }
603aa4dd815SMatthias Ringwald     return 0;
604aa4dd815SMatthias Ringwald }
605aa4dd815SMatthias Ringwald 
606a0ffb263SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){
607ce263fc8SMatthias Ringwald     // determine highest possible link setting
608a0ffb263SMatthias Ringwald     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
609a0ffb263SMatthias Ringwald     if (hci_remote_esco_supported(hfp_connection->acl_handle)){
610a0ffb263SMatthias Ringwald         hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
611a0ffb263SMatthias Ringwald         if ((hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4))
612ce263fc8SMatthias Ringwald         &&  (hfp_supported_features             & (1<<HFP_AGSF_ESCO_S4))){
613a0ffb263SMatthias Ringwald             hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
614ce263fc8SMatthias Ringwald         }
615ce263fc8SMatthias Ringwald     }
616ce263fc8SMatthias Ringwald }
617ce263fc8SMatthias Ringwald 
618a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
619a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
620aa4dd815SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0);
621aa4dd815SMatthias Ringwald 
622a0ffb263SMatthias Ringwald     hfp_init_link_settings(hfp_connection);
623ce263fc8SMatthias Ringwald 
624a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
625d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
626a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
627aa4dd815SMatthias Ringwald     }
628ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
629d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS &&
630d0c20769SMatthias Ringwald         hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
631a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
632ce263fc8SMatthias Ringwald     }
633aa4dd815SMatthias Ringwald }
6343deb3ec6SMatthias Ringwald 
635a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
636a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
6373deb3ec6SMatthias Ringwald     int done = 0;
638aa4dd815SMatthias Ringwald     // printf(" -> State machine: SLC\n");
639a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
6403deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
641a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
6423deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
643aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
644a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
645a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
646aa4dd815SMatthias Ringwald                     } else {
647a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
648aa4dd815SMatthias Ringwald                     }
649a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
650aa4dd815SMatthias Ringwald                     return 1;
6513deb3ec6SMatthias Ringwald                 default:
6523deb3ec6SMatthias Ringwald                     break;
6533deb3ec6SMatthias Ringwald             }
6543deb3ec6SMatthias Ringwald             break;
6553deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
656a0ffb263SMatthias Ringwald             done = codecs_exchange_state_machine(hfp_connection);
6573deb3ec6SMatthias Ringwald 
658a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
659a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6603deb3ec6SMatthias Ringwald             }
661aa4dd815SMatthias Ringwald             return done;
662aa4dd815SMatthias Ringwald 
663aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
664a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
665a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;  // prevent reentrance
666a0ffb263SMatthias Ringwald                 int next_segment = hfp_ag_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
667a0ffb263SMatthias Ringwald                 if (next_segment < hfp_ag_indicators_cmd_generator_num_segments(hfp_connection)){
668ad902e3dSMatthias Ringwald                     // prepare sending of next segment
669a0ffb263SMatthias Ringwald                     hfp_connection->send_ag_indicators_segment = next_segment;
670a0ffb263SMatthias Ringwald                     hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
671ad902e3dSMatthias Ringwald                 } else {
672ad902e3dSMatthias Ringwald                     // done, go to next state
673a0ffb263SMatthias Ringwald                     hfp_connection->send_ag_indicators_segment = 0;
674a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
675ad902e3dSMatthias Ringwald                 }
676aa4dd815SMatthias Ringwald                 return 1;
677ad902e3dSMatthias Ringwald             }
678ad902e3dSMatthias Ringwald             break;
679aa4dd815SMatthias Ringwald 
680aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
681a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
682a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
683a0ffb263SMatthias Ringwald             hfp_ag_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
684aa4dd815SMatthias Ringwald             return 1;
685aa4dd815SMatthias Ringwald 
6863deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
687a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
688a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
689a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
690a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
691a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
692aa4dd815SMatthias Ringwald             } else {
693a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
6943deb3ec6SMatthias Ringwald             }
695a0ffb263SMatthias Ringwald             hfp_ag_set_indicator_status_update_cmd(hfp_connection->rfcomm_cid, 1);
696aa4dd815SMatthias Ringwald             return 1;
6973deb3ec6SMatthias Ringwald 
698aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
699a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
700a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
701a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
702aa4dd815SMatthias Ringwald             }
703a0ffb263SMatthias Ringwald             hfp_ag_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
704a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
705a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
706ce263fc8SMatthias Ringwald             }
707aa4dd815SMatthias Ringwald             return 1;
708aa4dd815SMatthias Ringwald 
709aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
710a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
711a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
712a0ffb263SMatthias Ringwald             hfp_ag_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
713aa4dd815SMatthias Ringwald             return 1;
714aa4dd815SMatthias Ringwald 
715aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
716a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
717a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
718a0ffb263SMatthias Ringwald             hfp_ag_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
719aa4dd815SMatthias Ringwald             return 1;
720aa4dd815SMatthias Ringwald 
721aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
722a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
723a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
724a0ffb263SMatthias Ringwald             hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
725aa4dd815SMatthias Ringwald             return 1;
7263deb3ec6SMatthias Ringwald         default:
7273deb3ec6SMatthias Ringwald             break;
7283deb3ec6SMatthias Ringwald     }
7293deb3ec6SMatthias Ringwald     return done;
7303deb3ec6SMatthias Ringwald }
7313deb3ec6SMatthias Ringwald 
732a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
733a0ffb263SMatthias Ringwald     // if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
7343deb3ec6SMatthias Ringwald 
735a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
736aa4dd815SMatthias Ringwald     if (done) return done;
737aa4dd815SMatthias Ringwald 
738aa4dd815SMatthias Ringwald     // printf(" -> State machine: SLC Queries\n");
739a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
740aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
741a0ffb263SMatthias Ringwald             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
742a0ffb263SMatthias Ringwald             hfp_ag_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
743aa4dd815SMatthias Ringwald             return 1;
744aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
745aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
746a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
747a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
748a0ffb263SMatthias Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
749aa4dd815SMatthias Ringwald             } else {
750a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
751aa4dd815SMatthias Ringwald             }
752aa4dd815SMatthias Ringwald             return 1;
753aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
754a0ffb263SMatthias Ringwald             hfp_ag_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
755aa4dd815SMatthias Ringwald             return 1;
756aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
757a0ffb263SMatthias Ringwald             hfp_ag_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
758aa4dd815SMatthias Ringwald             return 1;
759aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
760a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
761a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
762aa4dd815SMatthias Ringwald             } else {
763a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
7643deb3ec6SMatthias Ringwald             }
765aa4dd815SMatthias Ringwald             return 1;
766aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
767a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
768aa4dd815SMatthias Ringwald             return 1;
7693deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
770a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
771a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
772a0ffb263SMatthias Ringwald                 hfp_ag_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
773aa4dd815SMatthias Ringwald                 return 1;
7743deb3ec6SMatthias Ringwald             }
7753deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
776a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
777ce263fc8SMatthias Ringwald             return 1;
7783deb3ec6SMatthias Ringwald         default:
7793deb3ec6SMatthias Ringwald             break;
7803deb3ec6SMatthias Ringwald     }
781aa4dd815SMatthias Ringwald     return 0;
7823deb3ec6SMatthias Ringwald }
7833deb3ec6SMatthias Ringwald 
784a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
785a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
786a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
7873deb3ec6SMatthias Ringwald 
788aa4dd815SMatthias Ringwald 
789a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
790a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
791a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
792a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
793aa4dd815SMatthias Ringwald         return 1;
794aa4dd815SMatthias Ringwald     }
795aa4dd815SMatthias Ringwald 
796a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
797aa4dd815SMatthias Ringwald 
798aa4dd815SMatthias Ringwald     // run codecs exchange
799a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
800aa4dd815SMatthias Ringwald     if (done) return done;
801a0ffb263SMatthias Ringwald     // printf(" -> State machine: Audio hfp_connection\n");
802aa4dd815SMatthias Ringwald 
803a0ffb263SMatthias Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return done;
804a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
805a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
806a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
807d63c37a1SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection->acl_handle, hfp_connection->link_setting);
808aa4dd815SMatthias Ringwald         return 1;
809aa4dd815SMatthias Ringwald     }
810aa4dd815SMatthias Ringwald     return 0;
811aa4dd815SMatthias Ringwald }
812aa4dd815SMatthias Ringwald 
813ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
814665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
815665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
816aa4dd815SMatthias Ringwald 
817665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
818a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
819a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
820a0ffb263SMatthias Ringwald             return hfp_connection;
821aa4dd815SMatthias Ringwald         }
822aa4dd815SMatthias Ringwald     }
823aa4dd815SMatthias Ringwald     return NULL;
824aa4dd815SMatthias Ringwald }
825aa4dd815SMatthias Ringwald 
826ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
827d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
828d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
829aa4dd815SMatthias Ringwald 
830d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
831a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
832a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
833aa4dd815SMatthias Ringwald 
834a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
835a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
836aa4dd815SMatthias Ringwald 
837a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
838aa4dd815SMatthias Ringwald }
839aa4dd815SMatthias Ringwald 
840a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
841a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
842a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
843a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
844a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
845aa4dd815SMatthias Ringwald }
846aa4dd815SMatthias Ringwald 
847a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
848a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
849a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
850aa4dd815SMatthias Ringwald }
851aa4dd815SMatthias Ringwald 
852aa4dd815SMatthias Ringwald //
853aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
854aa4dd815SMatthias Ringwald //
855aa4dd815SMatthias Ringwald 
856a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
857aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
858a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
859a0ffb263SMatthias Ringwald         hfp_ag_establish_audio_connection(hfp_connection->remote_addr);
860aa4dd815SMatthias Ringwald     } else {
861a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
862a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
863a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
864a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
865a0ffb263SMatthias Ringwald         hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG);
866aa4dd815SMatthias Ringwald     }
867aa4dd815SMatthias Ringwald }
868aa4dd815SMatthias Ringwald 
869a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
870a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
871a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
872a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
873a0ffb263SMatthias Ringwald     hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_STOP_RINGINIG);
874aa4dd815SMatthias Ringwald }
875aa4dd815SMatthias Ringwald 
876aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
877aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
878aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
879aa4dd815SMatthias Ringwald 
880665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
881665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
882665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
883a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
884a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
885a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
886a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
887d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
888aa4dd815SMatthias Ringwald         }
889a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
890a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
891aa4dd815SMatthias Ringwald         }
892a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
893aa4dd815SMatthias Ringwald     }
894aa4dd815SMatthias Ringwald }
895aa4dd815SMatthias Ringwald 
896aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
897aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
898aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
899aa4dd815SMatthias Ringwald 
900665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
901665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
902665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
903a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
904a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
905a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
906a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
907aa4dd815SMatthias Ringwald     }
908aa4dd815SMatthias Ringwald }
909aa4dd815SMatthias Ringwald 
910aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
911aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("call");
912aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
913aa4dd815SMatthias Ringwald 
914665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
915665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
916665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
917a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
918a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
919a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
920a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
921aa4dd815SMatthias Ringwald     }
922aa4dd815SMatthias Ringwald }
923aa4dd815SMatthias Ringwald 
924aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
925aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callheld");
926aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
927aa4dd815SMatthias Ringwald 
928665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
929665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
930665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
931a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
932a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
933a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
934a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
935aa4dd815SMatthias Ringwald     }
936aa4dd815SMatthias Ringwald }
937aa4dd815SMatthias Ringwald 
938aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
939aa4dd815SMatthias Ringwald 
940aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
941aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
942aa4dd815SMatthias Ringwald 
943665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
944665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
945665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
946a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
947a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
948a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
949aa4dd815SMatthias Ringwald 
950a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
951a0ffb263SMatthias Ringwald         if (hfp_connection == source){
952a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
953aa4dd815SMatthias Ringwald 
954aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
955a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
956aa4dd815SMatthias Ringwald             } else {
957a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
958a0ffb263SMatthias Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->remote_addr);
959aa4dd815SMatthias Ringwald             }
960aa4dd815SMatthias Ringwald 
961a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
962a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
963aa4dd815SMatthias Ringwald 
964aa4dd815SMatthias Ringwald         } else {
965a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
966aa4dd815SMatthias Ringwald         }
967a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
968aa4dd815SMatthias Ringwald     }
969aa4dd815SMatthias Ringwald }
970aa4dd815SMatthias Ringwald 
971aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
972aa4dd815SMatthias Ringwald 
973aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
974aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
975aa4dd815SMatthias Ringwald 
976665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
977665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
978665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
979a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
980a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
981aa4dd815SMatthias Ringwald 
982a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
983a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
984aa4dd815SMatthias Ringwald 
985a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
986a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
987aa4dd815SMatthias Ringwald 
988a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
989aa4dd815SMatthias Ringwald         break;  // only single
990aa4dd815SMatthias Ringwald     }
991aa4dd815SMatthias Ringwald }
992aa4dd815SMatthias Ringwald 
993aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
994aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
995665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
996665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
997665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
998665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
999aa4dd815SMatthias Ringwald         if (connection->call_state != HFP_CALL_RINGING &&
1000aa4dd815SMatthias Ringwald             connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1001aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
1002aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1003aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
1004aa4dd815SMatthias Ringwald         hfp_run_for_context(connection);
1005aa4dd815SMatthias Ringwald     }
1006aa4dd815SMatthias Ringwald }
1007aa4dd815SMatthias Ringwald 
1008aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1009aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1010aa4dd815SMatthias Ringwald 
1011665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1012665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1013665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1014a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1015d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1016a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1017a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1018a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1019a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 1;
1020a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1021aa4dd815SMatthias Ringwald     }
1022a0ffb263SMatthias Ringwald     hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_TERMINATED);
1023aa4dd815SMatthias Ringwald }
1024aa4dd815SMatthias Ringwald 
1025d0c20769SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(){
1026aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1027aa4dd815SMatthias Ringwald     if (!indicator){
1028d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
1029aa4dd815SMatthias Ringwald     };
1030d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1031aa4dd815SMatthias Ringwald }
1032aa4dd815SMatthias Ringwald 
1033d210d9c4SMatthias Ringwald static void hfp_ag_set_callheld_indicator(){
1034d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1035d210d9c4SMatthias Ringwald     if (!indicator){
1036d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
1037d210d9c4SMatthias Ringwald     };
1038d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1039d210d9c4SMatthias Ringwald }
1040d210d9c4SMatthias Ringwald 
1041d210d9c4SMatthias Ringwald static void hfp_ag_set_call_indicator(){
1042aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1043aa4dd815SMatthias Ringwald     if (!indicator){
1044aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
1045aa4dd815SMatthias Ringwald     };
1046d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1047aa4dd815SMatthias Ringwald }
1048aa4dd815SMatthias Ringwald 
1049aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1050665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1051665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1052665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1053a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1054a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
1055a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1056a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1057aa4dd815SMatthias Ringwald     }
1058aa4dd815SMatthias Ringwald }
1059aa4dd815SMatthias Ringwald 
1060aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1061665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1062665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1063665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1064a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1065a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1066aa4dd815SMatthias Ringwald     }
1067aa4dd815SMatthias Ringwald     return NULL;
1068aa4dd815SMatthias Ringwald }
1069aa4dd815SMatthias Ringwald 
1070a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1071665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1072665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1073665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1074a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1075a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1076ce263fc8SMatthias Ringwald     }
1077ce263fc8SMatthias Ringwald }
1078ce263fc8SMatthias Ringwald 
1079a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1080aa4dd815SMatthias Ringwald     int indicator_index;
1081a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1082aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1083a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1084a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1085a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1086a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1087a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1088a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1089a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1090a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG);
1091aa4dd815SMatthias Ringwald             break;
1092aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1093a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1094a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1095a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1096aa4dd815SMatthias Ringwald             break;
1097aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1098a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1099a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1100aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1101a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1102aa4dd815SMatthias Ringwald             break;
1103aa4dd815SMatthias Ringwald         default:
1104aa4dd815SMatthias Ringwald             break;
1105aa4dd815SMatthias Ringwald     }
1106aa4dd815SMatthias Ringwald     return 0;
1107aa4dd815SMatthias Ringwald }
1108a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1109a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1110aa4dd815SMatthias Ringwald     int indicator_index;
1111d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1112d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1113d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
111474386ee0SMatthias Ringwald 
1115d210d9c4SMatthias Ringwald     //printf("hfp_ag_call_sm event %d \n", event);
1116aa4dd815SMatthias Ringwald     switch (event){
1117aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1118d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1119aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1120d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1121aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1122d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1123d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1124aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
1125aa4dd815SMatthias Ringwald                             printf("AG rings\n");
1126aa4dd815SMatthias Ringwald                             break;
1127aa4dd815SMatthias Ringwald                         default:
11283deb3ec6SMatthias Ringwald                             break;
11293deb3ec6SMatthias Ringwald                     }
11303deb3ec6SMatthias Ringwald                     break;
1131aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1132d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1133aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1134d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1135d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1136aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
1137aa4dd815SMatthias Ringwald                             printf("AG call waiting\n");
1138aa4dd815SMatthias Ringwald                             break;
1139aa4dd815SMatthias Ringwald                         default:
11403deb3ec6SMatthias Ringwald                             break;
11413deb3ec6SMatthias Ringwald                     }
11423deb3ec6SMatthias Ringwald                     break;
1143aa4dd815SMatthias Ringwald             }
1144aa4dd815SMatthias Ringwald             break;
1145aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1146d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1147aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1148d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1149aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1150d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1151d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1152d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1153aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
1154aa4dd815SMatthias Ringwald                             printf("AG answers call, accept call by GSM\n");
1155aa4dd815SMatthias Ringwald                             break;
1156aa4dd815SMatthias Ringwald                         default:
11573deb3ec6SMatthias Ringwald                             break;
11583deb3ec6SMatthias Ringwald                     }
1159aa4dd815SMatthias Ringwald                     break;
1160aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1161d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1162aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1163aa4dd815SMatthias Ringwald                             printf("AG: current call is placed on hold, incoming call gets active\n");
1164d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1165d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1166d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1167ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1168ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1169aa4dd815SMatthias Ringwald                             break;
1170aa4dd815SMatthias Ringwald                         default:
1171aa4dd815SMatthias Ringwald                             break;
1172aa4dd815SMatthias Ringwald                     }
1173aa4dd815SMatthias Ringwald                     break;
1174aa4dd815SMatthias Ringwald             }
1175aa4dd815SMatthias Ringwald             break;
1176ce263fc8SMatthias Ringwald 
1177ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1178d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1179ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1180d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1181ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
1182ce263fc8SMatthias Ringwald                             printf("AG: joining held call with active call\n");
1183d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG);
1184d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1185ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1186a0ffb263SMatthias Ringwald                             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL);
1187ce263fc8SMatthias Ringwald                             break;
1188ce263fc8SMatthias Ringwald                         default:
1189ce263fc8SMatthias Ringwald                             break;
1190ce263fc8SMatthias Ringwald                     }
1191ce263fc8SMatthias Ringwald                     break;
1192ce263fc8SMatthias Ringwald                 default:
1193ce263fc8SMatthias Ringwald                     break;
1194ce263fc8SMatthias Ringwald             }
1195ce263fc8SMatthias Ringwald             break;
1196ce263fc8SMatthias Ringwald 
1197aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1198d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1199aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1200d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1201aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1202d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF);
1203d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1204d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1205a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
1206aa4dd815SMatthias Ringwald                             printf("HF answers call, accept call by GSM\n");
1207a0ffb263SMatthias Ringwald                             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_ANSWERED);
1208aa4dd815SMatthias Ringwald                             break;
1209aa4dd815SMatthias Ringwald                         default:
1210aa4dd815SMatthias Ringwald                             break;
1211aa4dd815SMatthias Ringwald                     }
12123deb3ec6SMatthias Ringwald                     break;
12133deb3ec6SMatthias Ringwald                 default:
12143deb3ec6SMatthias Ringwald                     break;
12153deb3ec6SMatthias Ringwald             }
12163deb3ec6SMatthias Ringwald             break;
12173deb3ec6SMatthias Ringwald 
1218ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1219d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1220ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1221d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1222ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1223d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG);
1224ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1225ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1226a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1227ce263fc8SMatthias Ringwald                             // as with regualr call
1228d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1229d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1230ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
1231ce263fc8SMatthias Ringwald                             printf("AG response and hold - hold by AG\n");
1232ce263fc8SMatthias Ringwald                             break;
1233ce263fc8SMatthias Ringwald                         default:
1234ce263fc8SMatthias Ringwald                             break;
1235ce263fc8SMatthias Ringwald                     }
1236ce263fc8SMatthias Ringwald                     break;
1237ce263fc8SMatthias Ringwald                 default:
1238ce263fc8SMatthias Ringwald                     break;
1239ce263fc8SMatthias Ringwald             }
1240ce263fc8SMatthias Ringwald             break;
1241ce263fc8SMatthias Ringwald 
1242ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1243d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1244ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1245d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1246ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1247d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF);
1248ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1249ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1250a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1251ce263fc8SMatthias Ringwald                             // as with regualr call
1252d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1253d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1254a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
1255ce263fc8SMatthias Ringwald                             printf("AG response and hold - hold by HF\n");
1256ce263fc8SMatthias Ringwald                             break;
1257ce263fc8SMatthias Ringwald                         default:
1258ce263fc8SMatthias Ringwald                             break;
1259ce263fc8SMatthias Ringwald                     }
1260ce263fc8SMatthias Ringwald                     break;
1261ce263fc8SMatthias Ringwald                 default:
1262ce263fc8SMatthias Ringwald                     break;
1263ce263fc8SMatthias Ringwald             }
1264ce263fc8SMatthias Ringwald             break;
1265ce263fc8SMatthias Ringwald 
1266ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1267ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1268ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1269ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1270d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG);
1271a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1272ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1273a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1274ce263fc8SMatthias Ringwald             printf("Held Call accepted and active\n");
1275ce263fc8SMatthias Ringwald             break;
1276ce263fc8SMatthias Ringwald 
1277ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1278ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1279ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1280ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1281d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG);
1282a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1283ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1284a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1285ce263fc8SMatthias Ringwald             // from terminate by ag
1286d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1287ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1288ce263fc8SMatthias Ringwald             break;
1289ce263fc8SMatthias Ringwald 
1290aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1291d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1292aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1293d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1294aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1295d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1296d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1297aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1298aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
1299aa4dd815SMatthias Ringwald                             printf("HF Rejected Incoming call, AG terminate call\n");
1300aa4dd815SMatthias Ringwald                             break;
1301aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1302aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1303d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1304d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1305aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1306aa4dd815SMatthias Ringwald                             printf("AG terminate outgoing call process\n");
1307aa4dd815SMatthias Ringwald                         default:
1308aa4dd815SMatthias Ringwald                             break;
1309aa4dd815SMatthias Ringwald                     }
1310aa4dd815SMatthias Ringwald                     break;
1311aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1312d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1313d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1314aa4dd815SMatthias Ringwald                     hfp_ag_transfer_call_state();
1315a0ffb263SMatthias Ringwald                     hfp_connection->call_state = HFP_CALL_IDLE;
1316aa4dd815SMatthias Ringwald                     printf("AG terminate call\n");
1317aa4dd815SMatthias Ringwald                     break;
1318aa4dd815SMatthias Ringwald             }
1319aa4dd815SMatthias Ringwald             break;
13203deb3ec6SMatthias Ringwald 
1321aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1322d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1323aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1324d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1325aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1326d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1327d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1328aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
1329aa4dd815SMatthias Ringwald                             printf("AG Rejected Incoming call, AG terminate call\n");
1330aa4dd815SMatthias Ringwald                             break;
1331aa4dd815SMatthias Ringwald                         default:
1332aa4dd815SMatthias Ringwald                             break;
13333deb3ec6SMatthias Ringwald                     }
1334aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1335d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1336d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1337d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1338aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1339aa4dd815SMatthias Ringwald                     printf("AG terminate call\n");
1340aa4dd815SMatthias Ringwald                     break;
1341aa4dd815SMatthias Ringwald                 default:
13423deb3ec6SMatthias Ringwald                     break;
13433deb3ec6SMatthias Ringwald             }
13443deb3ec6SMatthias Ringwald             break;
1345aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1346d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1347aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1348d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1349aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1350aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
1351aa4dd815SMatthias Ringwald                             printf("Incoming call interrupted\n");
1352aa4dd815SMatthias Ringwald                             break;
1353aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1354aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1355aa4dd815SMatthias Ringwald                             printf("Outgoing call interrupted\n");
1356aa4dd815SMatthias Ringwald                             printf("AG notify call dropped\n");
1357aa4dd815SMatthias Ringwald                             break;
1358aa4dd815SMatthias Ringwald                         default:
13593deb3ec6SMatthias Ringwald                             break;
13603deb3ec6SMatthias Ringwald                     }
1361d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1362d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1363aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
1364aa4dd815SMatthias Ringwald                     break;
1365aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1366ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1367d210d9c4SMatthias Ringwald                         hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1368ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1369a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1370ce263fc8SMatthias Ringwald                     }
1371d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1372d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1373d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1374aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1375aa4dd815SMatthias Ringwald                     printf("AG notify call dropped\n");
1376aa4dd815SMatthias Ringwald                     break;
1377aa4dd815SMatthias Ringwald                 default:
1378aa4dd815SMatthias Ringwald                     break;
1379aa4dd815SMatthias Ringwald             }
1380aa4dd815SMatthias Ringwald             break;
1381aa4dd815SMatthias Ringwald 
1382aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1383d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1384d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1385a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1386a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1387d210d9c4SMatthias Ringwald                 break;
1388d210d9c4SMatthias Ringwald             }
1389a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]);
13909cae807eSMatthias Ringwald 
1391a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
139274386ee0SMatthias Ringwald 
1393a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1394aa4dd815SMatthias Ringwald             break;
1395aa4dd815SMatthias Ringwald 
13969cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1397d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1398d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1399a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1400a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1401d210d9c4SMatthias Ringwald                 break;
1402d210d9c4SMatthias Ringwald             }
1403d210d9c4SMatthias Ringwald 
140474386ee0SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED);
1405a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
140674386ee0SMatthias Ringwald 
14079cae807eSMatthias Ringwald             printf("\nRedial last number");
14089cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1409aa4dd815SMatthias Ringwald 
14109cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
14119cae807eSMatthias Ringwald                 printf("\nLast number exists: accept call");
14129cae807eSMatthias Ringwald                 hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
14139cae807eSMatthias Ringwald             } else {
14149cae807eSMatthias Ringwald                 printf("\nLast number missing: reject call");
14159cae807eSMatthias Ringwald                 hfp_ag_outgoing_call_rejected();
14169cae807eSMatthias Ringwald             }
14179cae807eSMatthias Ringwald             break;
14189cae807eSMatthias Ringwald         }
1419aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
1420a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1421a0ffb263SMatthias Ringwald             if (!hfp_connection){
1422a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1423aa4dd815SMatthias Ringwald                 break;
1424aa4dd815SMatthias Ringwald             }
1425d210d9c4SMatthias Ringwald 
1426d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED);
1427a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1428a0ffb263SMatthias Ringwald             hfp_connection->send_error = 1;
1429a0ffb263SMatthias Ringwald             hfp_run_for_context(hfp_connection);
1430aa4dd815SMatthias Ringwald             break;
1431aa4dd815SMatthias Ringwald 
1432d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1433a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1434a0ffb263SMatthias Ringwald             if (!hfp_connection){
1435a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1436aa4dd815SMatthias Ringwald                 break;
1437aa4dd815SMatthias Ringwald             }
1438aa4dd815SMatthias Ringwald 
1439a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1440a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1441aa4dd815SMatthias Ringwald 
1442aa4dd815SMatthias Ringwald             // trigger callsetup to be
1443d0c20769SMatthias Ringwald             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1444d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED);
1445d210d9c4SMatthias Ringwald 
1446d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1447aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1448a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1449aa4dd815SMatthias Ringwald 
1450aa4dd815SMatthias Ringwald             // put current call on hold if active
1451d210d9c4SMatthias Ringwald             if (put_call_on_hold){
1452aa4dd815SMatthias Ringwald                 printf("AG putting current call on hold for new outgoing call\n");
1453d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1454aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1455a0ffb263SMatthias Ringwald                 hfp_ag_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1456aa4dd815SMatthias Ringwald             }
1457aa4dd815SMatthias Ringwald 
1458aa4dd815SMatthias Ringwald             // start audio if needed
1459a0ffb263SMatthias Ringwald             hfp_ag_establish_audio_connection(hfp_connection->remote_addr);
1460aa4dd815SMatthias Ringwald             break;
1461d210d9c4SMatthias Ringwald         }
1462aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1463a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1464a0ffb263SMatthias Ringwald             if (!hfp_connection){
1465a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1466aa4dd815SMatthias Ringwald                 break;
1467aa4dd815SMatthias Ringwald             }
1468d210d9c4SMatthias Ringwald 
1469d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING);
1470a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1471d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1472aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1473aa4dd815SMatthias Ringwald             break;
1474aa4dd815SMatthias Ringwald 
1475d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1476aa4dd815SMatthias Ringwald             // get outgoing call
1477a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1478a0ffb263SMatthias Ringwald             if (!hfp_connection){
1479a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1480aa4dd815SMatthias Ringwald             }
1481a0ffb263SMatthias Ringwald             if (!hfp_connection){
1482a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1483aa4dd815SMatthias Ringwald                 break;
1484aa4dd815SMatthias Ringwald             }
1485d210d9c4SMatthias Ringwald 
1486d0c20769SMatthias 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;
1487d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED);
1488a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1489d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1490d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1491aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1492aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1493d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1494d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1495aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
14963deb3ec6SMatthias Ringwald             }
14973deb3ec6SMatthias Ringwald             break;
1498d210d9c4SMatthias Ringwald         }
1499d210d9c4SMatthias Ringwald 
1500d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1501d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY);
1502d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1503a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1504a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1505d210d9c4SMatthias Ringwald             printf("AG: Call Waiting, User Busy\n");
1506d210d9c4SMatthias Ringwald             break;
1507d210d9c4SMatthias Ringwald 
1508d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1509d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1510d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1511d210d9c4SMatthias Ringwald 
1512d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1513d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1514a0ffb263SMatthias Ringwald                 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1515d0c20769SMatthias Ringwald 
1516d0c20769SMatthias Ringwald             }
1517d0c20769SMatthias Ringwald 
1518d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
1519d210d9c4SMatthias Ringwald                 printf("AG: Call Dropped, Accept new call\n");
1520d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1521a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1522d210d9c4SMatthias Ringwald             } else {
1523d210d9c4SMatthias Ringwald                 printf("AG: Call Dropped, Resume held call\n");
1524d210d9c4SMatthias Ringwald             }
1525d210d9c4SMatthias Ringwald             if (call_held){
1526d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1527a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1528d210d9c4SMatthias Ringwald             }
1529d0c20769SMatthias Ringwald 
1530a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1531d210d9c4SMatthias Ringwald             break;
1532d210d9c4SMatthias Ringwald         }
1533d0c20769SMatthias Ringwald 
1534d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1535d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1536d210d9c4SMatthias Ringwald             // only update if callsetup changed
1537d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1538a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1539d0c20769SMatthias Ringwald 
1540d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
1541d210d9c4SMatthias Ringwald                 printf("AG: Call on Hold, Accept new call\n");
1542d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1543a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1544d210d9c4SMatthias Ringwald             } else {
1545d210d9c4SMatthias Ringwald                 printf("AG: Swap calls\n");
1546d210d9c4SMatthias Ringwald             }
1547d0c20769SMatthias Ringwald 
1548d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1549d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1550a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1551a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1552d210d9c4SMatthias Ringwald             break;
1553d210d9c4SMatthias Ringwald         }
1554d0c20769SMatthias Ringwald 
1555d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1556d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1557d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
1558d210d9c4SMatthias Ringwald                 printf("AG: Join 3-way-call\n");
1559d210d9c4SMatthias Ringwald                 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL);
1560d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1561a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1562a0ffb263SMatthias Ringwald                 hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL);
1563d210d9c4SMatthias Ringwald             }
1564a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1565d210d9c4SMatthias Ringwald             break;
1566d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1567d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1568d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS);
1569d210d9c4SMatthias Ringwald             printf("AG: Transfer call -> Connect two calls and disconnect\n");
1570d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1571d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1572a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
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_IDLE;
1575d210d9c4SMatthias Ringwald             break;
1576ce263fc8SMatthias Ringwald 
15773deb3ec6SMatthias Ringwald         default:
15783deb3ec6SMatthias Ringwald             break;
15793deb3ec6SMatthias Ringwald     }
1580d210d9c4SMatthias Ringwald 
1581d0c20769SMatthias Ringwald 
15823deb3ec6SMatthias Ringwald }
15833deb3ec6SMatthias Ringwald 
15849cae807eSMatthias Ringwald 
1585a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
15869cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
15879cae807eSMatthias Ringwald     if (!active_call) return;
15889cae807eSMatthias Ringwald 
15899cae807eSMatthias Ringwald     int idx = active_call->index;
15909cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
15919cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
15929cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
15939cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
15949cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
15959cae807eSMatthias Ringwald     char * number = active_call->clip_number;
15969cae807eSMatthias Ringwald 
15979cae807eSMatthias Ringwald     char buffer[100];
15989cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
15999cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
16009cae807eSMatthias Ringwald     if (number){
16019cae807eSMatthias Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset, ", \"%s\",%u", number, type);
16029cae807eSMatthias Ringwald     }
16039cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
16049cae807eSMatthias 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,
16059cae807eSMatthias Ringwald        mode, mpty, type, number);
1606a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
16079cae807eSMatthias Ringwald }
16089cae807eSMatthias Ringwald 
1609a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection){
1610a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1611a0ffb263SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return;
16123deb3ec6SMatthias Ringwald 
1613a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1614a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1615a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1616a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1617a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
16189cae807eSMatthias Ringwald         } else {
1619a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1620a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1621a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16229cae807eSMatthias Ringwald         }
1623ce263fc8SMatthias Ringwald         return;
1624ce263fc8SMatthias Ringwald     }
1625ce263fc8SMatthias Ringwald 
1626a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1627a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1628a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1629aa4dd815SMatthias Ringwald         return;
1630aa4dd815SMatthias Ringwald     }
1631aa4dd815SMatthias Ringwald 
1632a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1633a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1634a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1635a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1636a0ffb263SMatthias Ringwald         hfp_ag_error(hfp_connection->rfcomm_cid);
1637a0ffb263SMatthias Ringwald         return;
1638a0ffb263SMatthias Ringwald     }
1639a0ffb263SMatthias Ringwald 
1640a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1641a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1642a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1643a0ffb263SMatthias Ringwald         hfp_ag_error(hfp_connection->rfcomm_cid);
1644aa4dd815SMatthias Ringwald         return;
1645aa4dd815SMatthias Ringwald     }
1646aa4dd815SMatthias Ringwald 
1647ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1648a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1649a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1650a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1651a0ffb263SMatthias Ringwald         hfp_ag_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1652ce263fc8SMatthias Ringwald         return;
1653ce263fc8SMatthias Ringwald     }
1654ce263fc8SMatthias Ringwald 
1655a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1656a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1657a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1658a0ffb263SMatthias Ringwald         hfp_ag_ok(hfp_connection->rfcomm_cid);
1659ce263fc8SMatthias Ringwald         return;
1660ce263fc8SMatthias Ringwald     }
1661ce263fc8SMatthias Ringwald 
1662aa4dd815SMatthias Ringwald     // update AG indicators
1663a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1664aa4dd815SMatthias Ringwald         int i;
1665a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1666a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1667a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1668a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1669ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1670ce263fc8SMatthias Ringwald                     break;
1671ce263fc8SMatthias Ringwald                 }
1672a0ffb263SMatthias Ringwald                 hfp_ag_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1673aa4dd815SMatthias Ringwald                 return;
1674aa4dd815SMatthias Ringwald             }
1675aa4dd815SMatthias Ringwald         }
1676aa4dd815SMatthias Ringwald     }
1677aa4dd815SMatthias Ringwald 
1678a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1679a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1680a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1681a0ffb263SMatthias Ringwald         hfp_ag_ring(hfp_connection->rfcomm_cid);
1682aa4dd815SMatthias Ringwald         return;
1683aa4dd815SMatthias Ringwald     }
1684aa4dd815SMatthias Ringwald 
1685a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1686a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1687a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1688a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1689aa4dd815SMatthias Ringwald         return;
1690aa4dd815SMatthias Ringwald     }
1691aa4dd815SMatthias Ringwald 
1692a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1693a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1694a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1695a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1696a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1697aa4dd815SMatthias Ringwald         return;
1698aa4dd815SMatthias Ringwald     }
1699aa4dd815SMatthias Ringwald 
1700a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1701a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1702a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1703a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1704ce263fc8SMatthias Ringwald         } else {
1705a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1706a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1707a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
1708ce263fc8SMatthias Ringwald         }
1709a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1710ce263fc8SMatthias Ringwald     }
1711ce263fc8SMatthias Ringwald 
1712a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1713a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1714a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1715a0ffb263SMatthias Ringwald         hfp_ag_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1716aa4dd815SMatthias Ringwald         return;
1717aa4dd815SMatthias Ringwald     }
1718aa4dd815SMatthias Ringwald 
1719a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1720a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1721a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1722a0ffb263SMatthias Ringwald         hfp_ag_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
17233deb3ec6SMatthias Ringwald         return;
17243deb3ec6SMatthias Ringwald     }
17253deb3ec6SMatthias Ringwald 
1726a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1727a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1728a0ffb263SMatthias Ringwald         hfp_ag_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1729ce263fc8SMatthias Ringwald         return;
1730ce263fc8SMatthias Ringwald     }
1731ce263fc8SMatthias Ringwald 
1732a0ffb263SMatthias Ringwald     int done = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1733aa4dd815SMatthias Ringwald     if (!done){
1734a0ffb263SMatthias Ringwald         done = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
17353deb3ec6SMatthias Ringwald     }
17363deb3ec6SMatthias Ringwald 
1737aa4dd815SMatthias Ringwald     if (!done){
1738a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
1739aa4dd815SMatthias Ringwald     }
1740aa4dd815SMatthias Ringwald 
1741aa4dd815SMatthias Ringwald     if (!done){
1742a0ffb263SMatthias Ringwald         done = hfp_ag_run_for_audio_connection(hfp_connection);
1743aa4dd815SMatthias Ringwald     }
17443deb3ec6SMatthias Ringwald 
1745a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_NONE && !done){
1746a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1747a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
17483deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1749a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1750a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
17513deb3ec6SMatthias Ringwald                 break;
17523deb3ec6SMatthias Ringwald             default:
17533deb3ec6SMatthias Ringwald                 break;
17543deb3ec6SMatthias Ringwald         }
17553deb3ec6SMatthias Ringwald     }
17563deb3ec6SMatthias Ringwald     if (done){
1757a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
17583deb3ec6SMatthias Ringwald     }
17593deb3ec6SMatthias Ringwald }
1760ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1761ce263fc8SMatthias Ringwald     int i;
1762a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1763a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1764ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1765ce263fc8SMatthias Ringwald             return indicator;
1766ce263fc8SMatthias Ringwald         }
1767ce263fc8SMatthias Ringwald     }
1768ce263fc8SMatthias Ringwald     return NULL;
1769ce263fc8SMatthias Ringwald }
17703deb3ec6SMatthias Ringwald 
1771aa4dd815SMatthias Ringwald static void hfp_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1772a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1773a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
17741e35c04dSMatthias Ringwald 
17751e35c04dSMatthias Ringwald     char last_char = packet[size-1];
17761e35c04dSMatthias Ringwald     packet[size-1] = 0;
17771e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
17781e35c04dSMatthias Ringwald     packet[size-1] = last_char;
17791e35c04dSMatthias Ringwald 
17803deb3ec6SMatthias Ringwald     int pos;
17813deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1782a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1783aa4dd815SMatthias Ringwald     }
1784ce263fc8SMatthias Ringwald     hfp_generic_status_indicator_t * indicator;
1785ce263fc8SMatthias Ringwald     int value;
1786a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
1787ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1788ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_active){
1789a0ffb263SMatthias Ringwald                 hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1790ce263fc8SMatthias Ringwald             }
1791a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1792ce263fc8SMatthias Ringwald             break;
1793ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
1794a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1795ce263fc8SMatthias Ringwald             printf("HF Response and Hold: %u\n", value);
1796ce263fc8SMatthias Ringwald             switch(value){
1797ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1798a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1799ce263fc8SMatthias Ringwald                     break;
1800ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1801a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1802ce263fc8SMatthias Ringwald                     break;
1803ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1804a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1805ce263fc8SMatthias Ringwald                     break;
1806ce263fc8SMatthias Ringwald                 default:
1807ce263fc8SMatthias Ringwald                     break;
1808ce263fc8SMatthias Ringwald             }
1809a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1810ce263fc8SMatthias Ringwald             break;
1811ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
1812a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1813ce263fc8SMatthias Ringwald             // find indicator by assigned number
1814a0ffb263SMatthias Ringwald             indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1815ce263fc8SMatthias Ringwald             if (!indicator){
1816a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1817ce263fc8SMatthias Ringwald                 break;
1818ce263fc8SMatthias Ringwald             }
1819a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1820ce263fc8SMatthias Ringwald             switch (indicator->uuid){
1821ce263fc8SMatthias Ringwald                 case 1: // enhanced security
1822ce263fc8SMatthias Ringwald                     if (value > 1) {
1823a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1824ce263fc8SMatthias Ringwald                         return;
1825ce263fc8SMatthias Ringwald                     }
1826ce263fc8SMatthias Ringwald                     printf("HF Indicator 'enhanced security' set to %u\n", value);
1827ce263fc8SMatthias Ringwald                     break;
1828ce263fc8SMatthias Ringwald                 case 2: // battery level
1829ce263fc8SMatthias Ringwald                     if (value > 100){
1830a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1831ce263fc8SMatthias Ringwald                         return;
1832ce263fc8SMatthias Ringwald                     }
1833ce263fc8SMatthias Ringwald                     printf("HF Indicator 'battery' set to %u\n", value);
1834ce263fc8SMatthias Ringwald                     break;
1835ce263fc8SMatthias Ringwald                 default:
1836ce263fc8SMatthias Ringwald                     printf("HF Indicator unknown set to %u\n", value);
1837ce263fc8SMatthias Ringwald                     break;
1838ce263fc8SMatthias Ringwald             }
1839a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1840ce263fc8SMatthias Ringwald             break;
1841ce263fc8SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1842ce263fc8SMatthias Ringwald             // expected by SLC state machine
1843a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1844a0ffb263SMatthias Ringwald             hfp_connection->send_ag_indicators_segment = 0;
1845a0ffb263SMatthias Ringwald             hfp_connection->send_ag_status_indicators = 1;
1846ce263fc8SMatthias Ringwald             break;
1847ce263fc8SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1848a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1849a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1850a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 1;
1851ce263fc8SMatthias Ringwald             break;
1852ce263fc8SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1853ce263fc8SMatthias Ringwald             if (subscriber_numbers_count == 0){
1854a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
1855ce263fc8SMatthias Ringwald                 break;
1856ce263fc8SMatthias Ringwald             }
1857a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1858a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 1;
1859ce263fc8SMatthias Ringwald             break;
1860c1797c7dSMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
1861a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1862a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]);
1863c1797c7dSMatthias Ringwald             break;
1864aa4dd815SMatthias Ringwald         case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1865a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1866a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1867aa4dd815SMatthias Ringwald             break;
1868aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1869a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1870aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1871a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1872a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
1873a0ffb263SMatthias Ringwald                 printf("AG: EC/NR = %u\n", hfp_connection->ag_echo_and_noise_reduction);
1874aa4dd815SMatthias Ringwald             } else {
1875a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1876aa4dd815SMatthias Ringwald             }
1877aa4dd815SMatthias Ringwald             break;
1878aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_ANSWERED:
1879a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1880aa4dd815SMatthias Ringwald             printf("HFP: ATA\n");
1881a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1882aa4dd815SMatthias Ringwald             break;
1883aa4dd815SMatthias Ringwald         case HFP_CMD_HANG_UP_CALL:
1884a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1885a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1886a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1887aa4dd815SMatthias Ringwald             break;
1888aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_HOLD: {
1889aa4dd815SMatthias Ringwald             // TODO: fully implement this
1890a0ffb263SMatthias Ringwald             log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]);
1891a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1892a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1893a0ffb263SMatthias Ringwald             hfp_connection->call_index = 0;
1894d0c20769SMatthias Ringwald 
1895a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
1896a0ffb263SMatthias Ringwald                 hfp_connection->call_index = atoi((char *)&hfp_connection->line_buffer[1]);
1897d0c20769SMatthias Ringwald             }
1898d210d9c4SMatthias Ringwald 
1899a0ffb263SMatthias Ringwald             switch (hfp_connection->line_buffer[0]){
1900aa4dd815SMatthias Ringwald                 case '0':
1901d0c20769SMatthias Ringwald                     // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1902a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1903aa4dd815SMatthias Ringwald                     break;
1904aa4dd815SMatthias Ringwald                 case '1':
1905d0c20769SMatthias Ringwald                     // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1906d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1907d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1908a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1909aa4dd815SMatthias Ringwald                     break;
1910aa4dd815SMatthias Ringwald                 case '2':
1911d0c20769SMatthias Ringwald                     // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1912d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1913d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1914a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1915aa4dd815SMatthias Ringwald                     break;
1916aa4dd815SMatthias Ringwald                 case '3':
1917d0c20769SMatthias Ringwald                     // Adds a held call to the conversation.
1918a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1919aa4dd815SMatthias Ringwald                     break;
1920aa4dd815SMatthias Ringwald                 case '4':
1921d0c20769SMatthias Ringwald                     // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1922a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1923aa4dd815SMatthias Ringwald                     break;
1924aa4dd815SMatthias Ringwald                 default:
1925aa4dd815SMatthias Ringwald                     break;
1926aa4dd815SMatthias Ringwald             }
1927aa4dd815SMatthias Ringwald             break;
1928aa4dd815SMatthias Ringwald         }
1929aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_PHONE_NUMBER:
1930a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1931a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
1932aa4dd815SMatthias Ringwald             break;
1933aa4dd815SMatthias Ringwald         case HFP_CMD_REDIAL_LAST_NUMBER:
1934a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1935a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
1936aa4dd815SMatthias Ringwald             break;
1937aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
1938a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1939a0ffb263SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0';
1940a0ffb263SMatthias Ringwald             log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
1941a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1942aa4dd815SMatthias Ringwald             break;
1943aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1944a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1945a0ffb263SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0';
1946a0ffb263SMatthias Ringwald             log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
1947a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1948aa4dd815SMatthias Ringwald             break;
1949aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1950a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1951a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1952a0ffb263SMatthias Ringwald             printf("HF speaker gain = %u\n", hfp_connection->speaker_gain);
1953aa4dd815SMatthias Ringwald             break;
1954aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1955a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1956a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1957a0ffb263SMatthias Ringwald             printf("HF microphone gain = %u\n", hfp_connection->microphone_gain);
1958aa4dd815SMatthias Ringwald             break;
1959aa4dd815SMatthias Ringwald         default:
1960aa4dd815SMatthias Ringwald             break;
19613deb3ec6SMatthias Ringwald     }
19623deb3ec6SMatthias Ringwald }
19633deb3ec6SMatthias Ringwald 
1964aa4dd815SMatthias Ringwald static void hfp_run(void){
1965d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1966d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1967d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1968d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1969a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
19703deb3ec6SMatthias Ringwald     }
19713deb3ec6SMatthias Ringwald }
19723deb3ec6SMatthias Ringwald 
1973ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
19743deb3ec6SMatthias Ringwald     switch (packet_type){
19753deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1976aa4dd815SMatthias Ringwald             hfp_handle_rfcomm_data(packet_type, channel, packet, size);
19773deb3ec6SMatthias Ringwald             break;
19783deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1979f4000eebSMatthias Ringwald             hfp_handle_hci_event(packet_type, packet, size);
1980aa4dd815SMatthias Ringwald             break;
19813deb3ec6SMatthias Ringwald         default:
19823deb3ec6SMatthias Ringwald             break;
19833deb3ec6SMatthias Ringwald     }
19843deb3ec6SMatthias Ringwald 
19853deb3ec6SMatthias Ringwald     hfp_run();
19863deb3ec6SMatthias Ringwald }
19873deb3ec6SMatthias Ringwald 
19881e35c04dSMatthias Ringwald 
1989a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
19903deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
19913deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
19923deb3ec6SMatthias Ringwald         return;
19933deb3ec6SMatthias Ringwald     }
19943deb3ec6SMatthias Ringwald     int i;
1995a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
19963deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
19973deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
19983deb3ec6SMatthias Ringwald     }
1999a0ffb263SMatthias Ringwald }
20003deb3ec6SMatthias Ringwald 
2001a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2002a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2003a0ffb263SMatthias Ringwald }
20043deb3ec6SMatthias Ringwald 
2005a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2006a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
2007a0ffb263SMatthias Ringwald     memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2008a0ffb263SMatthias Ringwald }
20093deb3ec6SMatthias Ringwald 
2010a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
2011a0ffb263SMatthias Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return;
2012a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
2013a0ffb263SMatthias Ringwald     memcpy(hfp_generic_status_indicators, hf_indicators, hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2014a0ffb263SMatthias Ringwald }
2015a0ffb263SMatthias Ringwald 
2016a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
20173deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
20183deb3ec6SMatthias Ringwald     memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *));
2019a0ffb263SMatthias Ringwald }
2020a0ffb263SMatthias Ringwald 
2021a0ffb263SMatthias Ringwald 
2022a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
2023d63c37a1SMatthias Ringwald     // register for HCI events
2024d63c37a1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
2025d63c37a1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
2026d63c37a1SMatthias Ringwald 
2027a0ffb263SMatthias Ringwald     l2cap_init();
2028d63c37a1SMatthias Ringwald 
2029d63c37a1SMatthias Ringwald     rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff);
2030aa4dd815SMatthias Ringwald 
2031d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2032d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2033d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2034d210d9c4SMatthias Ringwald 
2035d210d9c4SMatthias Ringwald     hfp_gsm_init();
20363deb3ec6SMatthias Ringwald }
20373deb3ec6SMatthias Ringwald 
20383deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
20393deb3ec6SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, SDP_Handsfree);
20403deb3ec6SMatthias Ringwald }
20413deb3ec6SMatthias Ringwald 
20423deb3ec6SMatthias Ringwald void hfp_ag_release_service_level_connection(bd_addr_t bd_addr){
2043a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2044a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2045a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20463deb3ec6SMatthias Ringwald }
20473deb3ec6SMatthias Ringwald 
20483deb3ec6SMatthias Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, hfp_cme_error_t error){
2049a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2050a0ffb263SMatthias Ringwald     if (!hfp_connection){
2051a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
20523deb3ec6SMatthias Ringwald         return;
20533deb3ec6SMatthias Ringwald     }
2054a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2055a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
20563deb3ec6SMatthias Ringwald         return;
20573deb3ec6SMatthias Ringwald     }
2058a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2059a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20603deb3ec6SMatthias Ringwald }
20613deb3ec6SMatthias Ringwald 
2062a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
2063a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2064a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
20653deb3ec6SMatthias Ringwald 
2066a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2067aa4dd815SMatthias Ringwald 
2068a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2069aa4dd815SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
2070a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
20713deb3ec6SMatthias Ringwald     }
2072aa4dd815SMatthias Ringwald 
2073a0ffb263SMatthias Ringwald     switch (hfp_connection->codecs_state){
2074aa4dd815SMatthias Ringwald         case HFP_CODECS_IDLE:
2075aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_LIST:
2076aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
2077aa4dd815SMatthias Ringwald         case HFP_CODECS_ERROR:
2078a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
2079aa4dd815SMatthias Ringwald             break;
2080aa4dd815SMatthias Ringwald         default:
2081aa4dd815SMatthias Ringwald             break;
2082aa4dd815SMatthias Ringwald     }
2083aa4dd815SMatthias Ringwald }
2084aa4dd815SMatthias Ringwald 
2085aa4dd815SMatthias Ringwald void hfp_ag_establish_audio_connection(bd_addr_t bd_addr){
2086aa4dd815SMatthias Ringwald     hfp_ag_establish_service_level_connection(bd_addr);
2087a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2088aa4dd815SMatthias Ringwald 
2089a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2090a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2091a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20923deb3ec6SMatthias Ringwald }
20933deb3ec6SMatthias Ringwald 
20943deb3ec6SMatthias Ringwald void hfp_ag_release_audio_connection(bd_addr_t bd_addr){
2095a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2096a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2097a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20983deb3ec6SMatthias Ringwald }
2099aa4dd815SMatthias Ringwald 
2100aa4dd815SMatthias Ringwald /**
2101aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2102aa4dd815SMatthias Ringwald  */
2103aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2104aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2105aa4dd815SMatthias Ringwald         return;
2106aa4dd815SMatthias Ringwald     }
2107aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2108aa4dd815SMatthias Ringwald 
2109665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2110665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2111665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2112a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2113a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2114a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2115aa4dd815SMatthias Ringwald     }
2116aa4dd815SMatthias Ringwald }
2117aa4dd815SMatthias Ringwald 
2118aa4dd815SMatthias Ringwald /**
2119aa4dd815SMatthias Ringwald  * @brief Called from GSM
2120aa4dd815SMatthias Ringwald  */
2121aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2122aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2123aa4dd815SMatthias Ringwald }
2124aa4dd815SMatthias Ringwald 
2125aa4dd815SMatthias Ringwald /**
2126aa4dd815SMatthias Ringwald  * @brief number is stored.
2127aa4dd815SMatthias Ringwald  */
2128aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2129d0c20769SMatthias Ringwald     hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number);
2130aa4dd815SMatthias Ringwald }
2131aa4dd815SMatthias Ringwald 
2132aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2133aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2134aa4dd815SMatthias Ringwald }
2135aa4dd815SMatthias Ringwald 
2136aa4dd815SMatthias Ringwald // call from AG UI
2137aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2138aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2139aa4dd815SMatthias Ringwald }
2140aa4dd815SMatthias Ringwald 
2141ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2142ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2143ce263fc8SMatthias Ringwald }
2144ce263fc8SMatthias Ringwald 
2145aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2146aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2147aa4dd815SMatthias Ringwald }
2148aa4dd815SMatthias Ringwald 
2149aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2150aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2151aa4dd815SMatthias Ringwald }
2152aa4dd815SMatthias Ringwald 
2153aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2154aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2155aa4dd815SMatthias Ringwald }
2156aa4dd815SMatthias Ringwald 
2157aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2158aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2159aa4dd815SMatthias Ringwald }
2160aa4dd815SMatthias Ringwald 
2161aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2162aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2163aa4dd815SMatthias Ringwald }
2164ce263fc8SMatthias Ringwald 
2165ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2166ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2167ce263fc8SMatthias Ringwald }
2168ce263fc8SMatthias Ringwald 
2169ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2170ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2171ce263fc8SMatthias Ringwald }
2172ce263fc8SMatthias Ringwald 
2173ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2174ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2175ce263fc8SMatthias Ringwald }
2176ce263fc8SMatthias Ringwald 
2177aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2178aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2179aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2180aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2181aa4dd815SMatthias Ringwald 
2182ce263fc8SMatthias Ringwald 
2183665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2184665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2185665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2186a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2187a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2188ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2189ce263fc8SMatthias Ringwald             continue;
2190ce263fc8SMatthias Ringwald         }
2191ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2192a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2193a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2194aa4dd815SMatthias Ringwald     }
2195aa4dd815SMatthias Ringwald }
2196aa4dd815SMatthias Ringwald 
2197aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2198aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2199aa4dd815SMatthias Ringwald }
2200aa4dd815SMatthias Ringwald 
2201aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2202aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2203aa4dd815SMatthias Ringwald }
2204aa4dd815SMatthias Ringwald 
2205aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2206aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2207aa4dd815SMatthias Ringwald }
2208aa4dd815SMatthias Ringwald 
2209aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2210aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2211aa4dd815SMatthias Ringwald }
2212aa4dd815SMatthias Ringwald 
2213aa4dd815SMatthias Ringwald void hfp_ag_activate_voice_recognition(bd_addr_t bd_addr, int activate){
2214aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
2215a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2216aa4dd815SMatthias Ringwald 
2217a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
2218aa4dd815SMatthias Ringwald         printf("AG cannot acivate voice recognition - not supported by HF\n");
2219aa4dd815SMatthias Ringwald         return;
2220aa4dd815SMatthias Ringwald     }
2221aa4dd815SMatthias Ringwald 
2222aa4dd815SMatthias Ringwald     if (activate){
2223aa4dd815SMatthias Ringwald         hfp_ag_establish_audio_connection(bd_addr);
2224aa4dd815SMatthias Ringwald     }
2225aa4dd815SMatthias Ringwald 
2226a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2227a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2228a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2229aa4dd815SMatthias Ringwald }
2230aa4dd815SMatthias Ringwald 
2231aa4dd815SMatthias Ringwald void hfp_ag_set_microphone_gain(bd_addr_t bd_addr, int gain){
2232a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2233a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2234a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2235a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2236a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2237aa4dd815SMatthias Ringwald     }
2238a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2239aa4dd815SMatthias Ringwald }
2240aa4dd815SMatthias Ringwald 
2241aa4dd815SMatthias Ringwald void hfp_ag_set_speaker_gain(bd_addr_t bd_addr, int gain){
2242a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2243a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2244a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2245a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2246aa4dd815SMatthias Ringwald     }
2247a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2248aa4dd815SMatthias Ringwald }
2249aa4dd815SMatthias Ringwald 
2250aa4dd815SMatthias Ringwald void hfp_ag_send_phone_number_for_voice_tag(bd_addr_t bd_addr, const char * number){
2251a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2252aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2253a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2254aa4dd815SMatthias Ringwald }
2255aa4dd815SMatthias Ringwald 
2256aa4dd815SMatthias Ringwald void hfp_ag_reject_phone_number_for_voice_tag(bd_addr_t bd_addr){
2257a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2258a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2259aa4dd815SMatthias Ringwald }
2260aa4dd815SMatthias Ringwald 
2261c1797c7dSMatthias Ringwald void hfp_ag_send_dtmf_code_done(bd_addr_t bd_addr){
2262a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2263a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2264c1797c7dSMatthias Ringwald }
2265aa4dd815SMatthias Ringwald 
2266ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2267ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2268ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2269ce263fc8SMatthias Ringwald }
2270ce263fc8SMatthias Ringwald 
22719cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
22729cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2273ce263fc8SMatthias Ringwald }
2274ce263fc8SMatthias Ringwald 
2275a0ffb263SMatthias Ringwald void hfp_ag_notify_incoming_call_waiting(bd_addr_t bd_addr){
2276a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2277a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2278a0ffb263SMatthias Ringwald 
2279a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2280a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2281a0ffb263SMatthias Ringwald }
2282ce263fc8SMatthias Ringwald 
2283