xref: /btstack/src/classic/hfp_ag.c (revision 520c92d527d72961be25b6e424c5df3f67d1106f)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hfp_ag.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
4266a048abSMatthias Ringwald // HFP Audio Gateway (AG) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493deb3ec6SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <stdlib.h>
513deb3ec6SMatthias Ringwald #include <string.h>
523deb3ec6SMatthias Ringwald 
5356042629SMatthias Ringwald #include "hci_cmd.h"
5482636622SMatthias Ringwald #include "btstack_run_loop.h"
553deb3ec6SMatthias Ringwald 
56235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
573deb3ec6SMatthias Ringwald #include "hci.h"
583deb3ec6SMatthias Ringwald #include "btstack_memory.h"
593deb3ec6SMatthias Ringwald #include "hci_dump.h"
603deb3ec6SMatthias Ringwald #include "l2cap.h"
6116ece135SMatthias Ringwald #include "btstack_debug.h"
62e30a6a47SMatthias Ringwald #include "btstack_event.h"
6359c6af15SMatthias Ringwald #include "classic/core.h"
643edc84c5SMatthias Ringwald #include "classic/hfp.h"
653edc84c5SMatthias Ringwald #include "classic/hfp_ag.h"
66023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h"
67efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
68023f2764SMatthias Ringwald #include "classic/sdp_server.h"
69023f2764SMatthias Ringwald #include "classic/sdp_util.h"
703deb3ec6SMatthias Ringwald 
71c5b64319SMatthias Ringwald // private prototypes
72adaba9f3SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection);
73adaba9f3SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
74adaba9f3SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
75c5b64319SMatthias Ringwald 
76c5b64319SMatthias Ringwald // public prototypes
7735833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void);
7835833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void);
79c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr);
80c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr);
81c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context);
82c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context);
83c5b64319SMatthias Ringwald 
84c5b64319SMatthias Ringwald // gobals
853deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway";
86aa4dd815SMatthias Ringwald 
873deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
88aa4dd815SMatthias Ringwald 
893deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
903deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
913deb3ec6SMatthias Ringwald 
923deb3ec6SMatthias Ringwald static int  hfp_ag_indicators_nr = 0;
933deb3ec6SMatthias Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_AG_INDICATORS];
943deb3ec6SMatthias Ringwald 
95a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0;
96a0ffb263SMatthias Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_HF_INDICATORS];
97a0ffb263SMatthias Ringwald 
983deb3ec6SMatthias Ringwald static int  hfp_ag_call_hold_services_nr = 0;
993deb3ec6SMatthias Ringwald static char *hfp_ag_call_hold_services[6];
100ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback;
1013deb3ec6SMatthias Ringwald 
102ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
103ce263fc8SMatthias Ringwald static int hfp_ag_response_and_hold_active = 0;
104aa4dd815SMatthias Ringwald 
105ce263fc8SMatthias Ringwald // Subcriber information entries
106ce263fc8SMatthias Ringwald static hfp_phone_number_t * subscriber_numbers = NULL;
107ce263fc8SMatthias Ringwald static int subscriber_numbers_count = 0;
108ce263fc8SMatthias Ringwald 
10999a10067SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection);
1103deb3ec6SMatthias Ringwald 
1113deb3ec6SMatthias Ringwald 
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 
15013839019SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_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     }
155ca59be51SMatthias Ringwald     hfp_ag_callback = callback;
156ca59be51SMatthias Ringwald     hfp_set_ag_callback(callback);
1573deb3ec6SMatthias Ringwald }
1583deb3ec6SMatthias Ringwald 
1590cb5b971SMatthias Ringwald static int use_in_band_tone(void){
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 
1814f84bf36SMatthias Ringwald void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint8_t ability_to_reject_call, uint16_t supported_features, int wide_band_speech){
1823deb3ec6SMatthias Ringwald     if (!name){
1833deb3ec6SMatthias Ringwald         name = default_hfp_ag_service_name;
1843deb3ec6SMatthias Ringwald     }
185235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, 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 
1944f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
1954f84bf36SMatthias Ringwald     //
1964f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1974f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
1984f84bf36SMatthias Ringwald     //
1994f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features &0x1f;
2004f84bf36SMatthias Ringwald     if (supported_features & wide_band_speech){
2014f84bf36SMatthias Ringwald         sdp_features |= 1 << 5; // Wide band speech bit
2024f84bf36SMatthias Ringwald     }
203aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
2044f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
205aa4dd815SMatthias Ringwald }
206aa4dd815SMatthias Ringwald 
207485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){
208aa4dd815SMatthias Ringwald     char buffer[20];
209aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
210aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2113deb3ec6SMatthias Ringwald }
2123deb3ec6SMatthias Ringwald 
2133deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2143deb3ec6SMatthias Ringwald     char buffer[40];
2153deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
2163deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2173deb3ec6SMatthias Ringwald }
2183deb3ec6SMatthias Ringwald 
219485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){
2203deb3ec6SMatthias Ringwald     char buffer[10];
2213deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nOK\r\n");
2223deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2233deb3ec6SMatthias Ringwald }
2243deb3ec6SMatthias Ringwald 
225485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){
226aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
227aa4dd815SMatthias Ringwald }
228aa4dd815SMatthias Ringwald 
229aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
230aa4dd815SMatthias Ringwald     char buffer[50];
231d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
232aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
233aa4dd815SMatthias Ringwald }
234aa4dd815SMatthias Ringwald 
235ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
236ce263fc8SMatthias Ringwald     char buffer[50];
237ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
238ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
239ce263fc8SMatthias Ringwald }
240ce263fc8SMatthias Ringwald 
241c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
242aa4dd815SMatthias Ringwald     char buffer[50];
243d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
244aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
245aa4dd815SMatthias Ringwald }
246aa4dd815SMatthias Ringwald 
247aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
248aa4dd815SMatthias Ringwald     char buffer[50];
249a0ffb263SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
250aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
251aa4dd815SMatthias Ringwald }
252aa4dd815SMatthias Ringwald 
253485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){
2543deb3ec6SMatthias Ringwald     char buffer[10];
2553deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nERROR\r\n");
2563deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2573deb3ec6SMatthias Ringwald }
2583deb3ec6SMatthias Ringwald 
259485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2603deb3ec6SMatthias Ringwald     char buffer[20];
2613deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2623deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2633deb3ec6SMatthias Ringwald }
2643deb3ec6SMatthias Ringwald 
265ad902e3dSMatthias Ringwald // get size for indicator string
266a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
267ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
26845718b6fSMatthias Ringwald     return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
269a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
270a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
271ad902e3dSMatthias Ringwald }
272ad902e3dSMatthias Ringwald 
273ad902e3dSMatthias Ringwald // store indicator
274a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
275ad902e3dSMatthias Ringwald     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
276a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
277a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
278a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2793deb3ec6SMatthias Ringwald }
280ad902e3dSMatthias Ringwald 
281ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
282a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
283a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
284ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
285ad902e3dSMatthias Ringwald     return 3 + (num_indicators-1) * 2;
2863deb3ec6SMatthias Ringwald }
287ad902e3dSMatthias Ringwald 
288ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
289a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
290ad902e3dSMatthias Ringwald     if (index == 0) {
291ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
292ad902e3dSMatthias Ringwald     }
293ad902e3dSMatthias Ringwald     index--;
294a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
295ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
296ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
297a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
298ad902e3dSMatthias Ringwald     }
299ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators - 1){
300ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
301ad902e3dSMatthias Ringwald     }
302ad902e3dSMatthias Ringwald     return 1; // comma
303ad902e3dSMatthias Ringwald }
304ad902e3dSMatthias Ringwald 
305a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
306ad902e3dSMatthias Ringwald     if (index == 0){
307ad902e3dSMatthias Ringwald         *buffer++ = '\r';
30874386ee0SMatthias Ringwald         *buffer++ = '\n';
309ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
310ad902e3dSMatthias Ringwald         memcpy(buffer, HFP_INDICATOR, len);
311ad902e3dSMatthias Ringwald         buffer += len;
312ad902e3dSMatthias Ringwald         *buffer++ = ':';
313ad902e3dSMatthias Ringwald         return;
314ad902e3dSMatthias Ringwald     }
315ad902e3dSMatthias Ringwald     index--;
316a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
317ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
318ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
319a0ffb263SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
320ad902e3dSMatthias Ringwald         return;
321ad902e3dSMatthias Ringwald     }
322ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators-1){
323ad902e3dSMatthias Ringwald         memcpy(buffer, "\r\n\r\nOK\r\n", 8);
324ad902e3dSMatthias Ringwald         return;
325ad902e3dSMatthias Ringwald     }
326ad902e3dSMatthias Ringwald     *buffer = ',';
3273deb3ec6SMatthias Ringwald }
3283deb3ec6SMatthias Ringwald 
3293deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
3303deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3313deb3ec6SMatthias Ringwald     int i;
3323deb3ec6SMatthias Ringwald     int offset = 0;
333a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr-1; i++) {
334a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3353deb3ec6SMatthias Ringwald     }
336a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
337a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3383deb3ec6SMatthias Ringwald     }
3393deb3ec6SMatthias Ringwald     return offset;
3403deb3ec6SMatthias Ringwald }
3413deb3ec6SMatthias Ringwald 
3423deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
343a0ffb263SMatthias Ringwald     if (buffer_size < hfp_generic_status_indicators_nr * 3) return 0;
3443deb3ec6SMatthias Ringwald     int i;
3453deb3ec6SMatthias Ringwald     int offset = 0;
346a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
347a0ffb263SMatthias 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);
3483deb3ec6SMatthias Ringwald     }
3493deb3ec6SMatthias Ringwald     return offset;
3503deb3ec6SMatthias Ringwald }
3513deb3ec6SMatthias Ringwald 
3523deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
3533deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3543deb3ec6SMatthias Ringwald     int i;
3553deb3ec6SMatthias Ringwald     int offset = 0;
3563deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr-1; i++) {
3573deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3583deb3ec6SMatthias Ringwald     }
3593deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3603deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3613deb3ec6SMatthias Ringwald     }
3623deb3ec6SMatthias Ringwald     return offset;
3633deb3ec6SMatthias Ringwald }
3643deb3ec6SMatthias Ringwald 
3653deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
3663deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0;
3673deb3ec6SMatthias Ringwald     int i;
3683deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
3693deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) {
3703deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3713deb3ec6SMatthias Ringwald     }
3723deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3733deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3743deb3ec6SMatthias Ringwald     }
3753deb3ec6SMatthias Ringwald     return offset;
3763deb3ec6SMatthias Ringwald }
3773deb3ec6SMatthias Ringwald 
378485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
379ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
380a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
381a0ffb263SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
3823deb3ec6SMatthias Ringwald 
383ad902e3dSMatthias Ringwald     // assumes: can send now == true
384ad902e3dSMatthias Ringwald     // assumes: num segments > 0
385aba39421SMatthias Ringwald     // assumes: individual segments are smaller than MTU
386ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
387ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
388ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
389ad902e3dSMatthias Ringwald     int offset = 0;
390ad902e3dSMatthias Ringwald     int segment = start_segment;
391ad902e3dSMatthias Ringwald     while (segment < num_segments){
392a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
393aba39421SMatthias Ringwald         if (offset + segment_len > mtu) break;
394aba39421SMatthias Ringwald         // append segement
395a0ffb263SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset);
396ad902e3dSMatthias Ringwald         offset += segment_len;
397ad902e3dSMatthias Ringwald         segment++;
398ad902e3dSMatthias Ringwald     }
399ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
400ad902e3dSMatthias Ringwald     return segment;
401ad902e3dSMatthias Ringwald }
4023deb3ec6SMatthias Ringwald 
403ad902e3dSMatthias Ringwald // returns next segment to store
404485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
405a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
406485ac19eSMilanka Ringwald     return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
407ad902e3dSMatthias Ringwald         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
4083deb3ec6SMatthias Ringwald }
4093deb3ec6SMatthias Ringwald 
410485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){
4113deb3ec6SMatthias Ringwald     char buffer[40];
41289425bfcSMilanka Ringwald     const int size = sizeof(buffer);
41389425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR);
41489425bfcSMilanka Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9);
41589425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4163deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4173deb3ec6SMatthias Ringwald }
4183deb3ec6SMatthias Ringwald 
419485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){
420ad902e3dSMatthias Ringwald     char buffer[40];
42189425bfcSMilanka Ringwald     const int size = sizeof(buffer);
42289425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
42389425bfcSMilanka Ringwald     offset += hfp_ag_call_services_join(buffer+offset, size-offset-9);
42489425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4253deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4263deb3ec6SMatthias Ringwald }
4273deb3ec6SMatthias Ringwald 
4283deb3ec6SMatthias Ringwald 
429485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){
430485ac19eSMilanka Ringwald     return hfp_ag_send_ok(cid);
4313deb3ec6SMatthias Ringwald }
4323deb3ec6SMatthias Ringwald 
433485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4343deb3ec6SMatthias Ringwald     char buffer[40];
43589425bfcSMilanka Ringwald     const int size = sizeof(buffer);
43689425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
43789425bfcSMilanka Ringwald     offset += hfp_hf_indicators_join(buffer+offset, size-offset-10);
43889425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n");
4393deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4403deb3ec6SMatthias Ringwald }
4413deb3ec6SMatthias Ringwald 
442485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4433deb3ec6SMatthias Ringwald     char buffer[40];
4441cc1d9e9SMilanka Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7);
4451cc1d9e9SMilanka Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4463deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4473deb3ec6SMatthias Ringwald }
4483deb3ec6SMatthias Ringwald 
449485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4503deb3ec6SMatthias Ringwald     char buffer[20];
451aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
4523deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4533deb3ec6SMatthias Ringwald }
4543deb3ec6SMatthias Ringwald 
455485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4565e71456cSMilanka Ringwald     char buffer[41];
4573deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
45889425bfcSMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4593deb3ec6SMatthias Ringwald     } else {
46089425bfcSMilanka Ringwald         int offset = snprintf(buffer,  sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format);
4615e71456cSMilanka Ringwald         offset += snprintf(buffer+offset, 16, "%s", op.name);
46289425bfcSMilanka Ringwald         snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4633deb3ec6SMatthias Ringwald     }
4643deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4653deb3ec6SMatthias Ringwald }
4663deb3ec6SMatthias Ringwald 
4671cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
4681cc1d9e9SMilanka Ringwald     char buffer[30];
46989425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value);
4701cc1d9e9SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
4711cc1d9e9SMilanka Ringwald }
4723deb3ec6SMatthias Ringwald 
473485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){
4741cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
4753deb3ec6SMatthias Ringwald }
4763deb3ec6SMatthias Ringwald 
477485ac19eSMilanka Ringwald static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
4781cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
479aa4dd815SMatthias Ringwald }
480aa4dd815SMatthias Ringwald 
481485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
4821cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
483aa4dd815SMatthias Ringwald }
484aa4dd815SMatthias Ringwald 
485485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
4861cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
487aa4dd815SMatthias Ringwald }
488aa4dd815SMatthias Ringwald 
489485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){
4901cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_RESPONSE_AND_HOLD, state);
491ce263fc8SMatthias Ringwald }
492ce263fc8SMatthias Ringwald 
493a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
494bc1b1537SMilanka Ringwald     if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
495bc1b1537SMilanka Ringwald 
4966a7f44bdSMilanka Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){
4976a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
498df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
4993deb3ec6SMatthias Ringwald         }
5003deb3ec6SMatthias Ringwald     }
501df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
5023deb3ec6SMatthias Ringwald }
5033deb3ec6SMatthias Ringwald 
5047522e673SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){
5057522e673SMatthias Ringwald     // determine highest possible link setting
5067522e673SMatthias Ringwald     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
507d9f77559SMatthias Ringwald     // anything else requires eSCO support on both sides
508d9f77559SMatthias Ringwald     if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
5097522e673SMatthias Ringwald         switch (hfp_connection->negotiated_codec){
5107522e673SMatthias Ringwald             case HFP_CODEC_CVSD:
5117522e673SMatthias Ringwald                 hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
5127522e673SMatthias Ringwald                 if ((hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4))
5137522e673SMatthias Ringwald                 &&  (hfp_supported_features                    & (1<<HFP_AGSF_ESCO_S4))){
5147522e673SMatthias Ringwald                     hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
5157522e673SMatthias Ringwald                 }
5167522e673SMatthias Ringwald                 break;
5177522e673SMatthias Ringwald             case HFP_CODEC_MSBC:
5187522e673SMatthias Ringwald                 hfp_connection->link_setting = HFP_LINK_SETTINGS_T2;
5197522e673SMatthias Ringwald                 break;
520d9f77559SMatthias Ringwald             default:
521d9f77559SMatthias Ringwald                 break;
522d9f77559SMatthias Ringwald         }
5237522e673SMatthias Ringwald     }
5247522e673SMatthias Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
5257522e673SMatthias Ringwald }
5267522e673SMatthias Ringwald 
527a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
528aa4dd815SMatthias Ringwald     /* events ( == commands):
529aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
530aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
531aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
532aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
533aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
534aa4dd815SMatthias Ringwald     */
535aa4dd815SMatthias Ringwald 
536a0ffb263SMatthias Ringwald      switch (hfp_connection->codecs_state){
537aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
538a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
539aa4dd815SMatthias Ringwald             break;
540aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
541a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
542aa4dd815SMatthias Ringwald             break;
543aa4dd815SMatthias Ringwald         default:
544aa4dd815SMatthias Ringwald             break;
545aa4dd815SMatthias Ringwald     }
546aa4dd815SMatthias Ringwald 
547aa4dd815SMatthias Ringwald     // printf(" -> State machine: CC\n");
548aa4dd815SMatthias Ringwald 
549a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
550aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
551a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
552a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
553485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
554aa4dd815SMatthias Ringwald                 return 1;
555aa4dd815SMatthias Ringwald             }
556aa4dd815SMatthias Ringwald 
557a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
558aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
559aa4dd815SMatthias Ringwald                 case HFP_CODECS_EXCHANGED:
560a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
561aa4dd815SMatthias Ringwald                     break;
562aa4dd815SMatthias Ringwald                 default:
563aa4dd815SMatthias Ringwald                     break;
564aa4dd815SMatthias Ringwald             }
565485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
566aa4dd815SMatthias Ringwald             return 1;
567aa4dd815SMatthias Ringwald 
568aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
569a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
570485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
571aa4dd815SMatthias Ringwald             return 1;
572aa4dd815SMatthias Ringwald 
573aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
574a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
575a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
576485ac19eSMilanka Ringwald             hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
577aa4dd815SMatthias Ringwald             return 1;
578aa4dd815SMatthias Ringwald 
579aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
580a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
581a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
582485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
583aa4dd815SMatthias Ringwald                 return 1;
584aa4dd815SMatthias Ringwald             }
585a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
586a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
58703645b44SMatthias Ringwald             log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
588485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
5897522e673SMatthias Ringwald             // now, pick link settings
5907522e673SMatthias Ringwald             hfp_init_link_settings(hfp_connection);
591aa4dd815SMatthias Ringwald             return 1;
592aa4dd815SMatthias Ringwald         default:
593aa4dd815SMatthias Ringwald             break;
594aa4dd815SMatthias Ringwald     }
595aa4dd815SMatthias Ringwald     return 0;
596aa4dd815SMatthias Ringwald }
597aa4dd815SMatthias Ringwald 
598a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
599a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
600ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
601aa4dd815SMatthias Ringwald 
602a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
603d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
604a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
605aa4dd815SMatthias Ringwald     }
606ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
607d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS &&
608d0c20769SMatthias Ringwald         hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
609a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
610ce263fc8SMatthias Ringwald     }
611aa4dd815SMatthias Ringwald }
6123deb3ec6SMatthias Ringwald 
613a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
614c79b4cabSMatthias Ringwald     // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
615a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
616c04cf7ffSMilanka Ringwald     int sent = 0;
617a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
6183deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
619a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
6203deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
621aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
622d715cf51SMatthias Ringwald                     hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
623a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
624a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
625aa4dd815SMatthias Ringwald                     } else {
626a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
627aa4dd815SMatthias Ringwald                     }
628a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
629aa4dd815SMatthias Ringwald                     return 1;
6303deb3ec6SMatthias Ringwald                 default:
6313deb3ec6SMatthias Ringwald                     break;
6323deb3ec6SMatthias Ringwald             }
6333deb3ec6SMatthias Ringwald             break;
6343deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
635c04cf7ffSMilanka Ringwald             sent = codecs_exchange_state_machine(hfp_connection);
636a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
637a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6383deb3ec6SMatthias Ringwald             }
639c04cf7ffSMilanka Ringwald             return sent;
640aa4dd815SMatthias Ringwald 
641aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
642a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
643473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
644473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
645473ac565SMatthias Ringwald                 // continue below in state switch
646ad902e3dSMatthias Ringwald             }
647ad902e3dSMatthias Ringwald             break;
648aa4dd815SMatthias Ringwald 
649aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
650a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
651a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
652485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
653aa4dd815SMatthias Ringwald             return 1;
654aa4dd815SMatthias Ringwald 
6553deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
656a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
657a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
658a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
659a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
660a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
661aa4dd815SMatthias Ringwald             } else {
662a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
6633deb3ec6SMatthias Ringwald             }
6648a46ec40SMatthias Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
665aa4dd815SMatthias Ringwald             return 1;
6663deb3ec6SMatthias Ringwald 
667aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
668a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
669a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
670a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
671aa4dd815SMatthias Ringwald             }
672485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
673a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
674a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
675ce263fc8SMatthias Ringwald             }
676aa4dd815SMatthias Ringwald             return 1;
677aa4dd815SMatthias Ringwald 
678aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
679a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
680a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
681485ac19eSMilanka Ringwald             hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
682aa4dd815SMatthias Ringwald             return 1;
683aa4dd815SMatthias Ringwald 
684aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
685a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
686a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
687485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
688aa4dd815SMatthias Ringwald             return 1;
689aa4dd815SMatthias Ringwald 
690aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
691a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
692a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
693485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
694aa4dd815SMatthias Ringwald             return 1;
6953deb3ec6SMatthias Ringwald         default:
6963deb3ec6SMatthias Ringwald             break;
6973deb3ec6SMatthias Ringwald     }
698473ac565SMatthias Ringwald 
699473ac565SMatthias Ringwald     switch (hfp_connection->state){
700473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
701485ac19eSMilanka Ringwald             int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
702473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
703473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
704473ac565SMatthias Ringwald             if (next_segment < num_segments){
705473ac565SMatthias Ringwald                 // prepare sending of next segment
706473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
707473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
708473ac565SMatthias Ringwald             } else {
709473ac565SMatthias Ringwald                 // done, go to next state
710473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
711473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
712473ac565SMatthias Ringwald             }
713473ac565SMatthias Ringwald             return 1;
714473ac565SMatthias Ringwald         }
715473ac565SMatthias Ringwald         default:
716473ac565SMatthias Ringwald             break;
717473ac565SMatthias Ringwald     }
718c04cf7ffSMilanka Ringwald     return 0;
7193deb3ec6SMatthias Ringwald }
7203deb3ec6SMatthias Ringwald 
721a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
722c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
723c04cf7ffSMilanka Ringwald     if (sent) return 1;
724aa4dd815SMatthias Ringwald 
725a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
726aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
727a0ffb263SMatthias Ringwald             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
728485ac19eSMilanka Ringwald             hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
729aa4dd815SMatthias Ringwald             return 1;
730aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
731aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
732a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
733485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
734a0ffb263SMatthias Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
735aa4dd815SMatthias Ringwald             } else {
736485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
737aa4dd815SMatthias Ringwald             }
738aa4dd815SMatthias Ringwald             return 1;
739aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
740485ac19eSMilanka Ringwald             hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
741aa4dd815SMatthias Ringwald             return 1;
742aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
743485ac19eSMilanka Ringwald             hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
744aa4dd815SMatthias Ringwald             return 1;
745aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
746a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
747485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
748aa4dd815SMatthias Ringwald             } else {
749485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
7503deb3ec6SMatthias Ringwald             }
751aa4dd815SMatthias Ringwald             return 1;
752aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
753485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
754aa4dd815SMatthias Ringwald             return 1;
7553deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
756a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
757a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
758485ac19eSMilanka Ringwald                 hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
759aa4dd815SMatthias Ringwald                 return 1;
7603deb3ec6SMatthias Ringwald             }
761202c8a4cSMatthias Ringwald             break;
7623deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
763485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
764ce263fc8SMatthias Ringwald             return 1;
7653deb3ec6SMatthias Ringwald         default:
7663deb3ec6SMatthias Ringwald             break;
7673deb3ec6SMatthias Ringwald     }
768aa4dd815SMatthias Ringwald     return 0;
7693deb3ec6SMatthias Ringwald }
7703deb3ec6SMatthias Ringwald 
771a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
772a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
773a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
7743deb3ec6SMatthias Ringwald 
775aa4dd815SMatthias Ringwald 
776a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
777a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
778a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
779a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
780aa4dd815SMatthias Ringwald         return 1;
781aa4dd815SMatthias Ringwald     }
782aa4dd815SMatthias Ringwald 
783a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
784aa4dd815SMatthias Ringwald 
785aa4dd815SMatthias Ringwald     // run codecs exchange
786c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
787c04cf7ffSMilanka Ringwald     if (sent) return 1;
788aa4dd815SMatthias Ringwald 
789c04cf7ffSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
790a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
791a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
792a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
793eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
794aa4dd815SMatthias Ringwald         return 1;
795aa4dd815SMatthias Ringwald     }
796aa4dd815SMatthias Ringwald     return 0;
797aa4dd815SMatthias Ringwald }
798aa4dd815SMatthias Ringwald 
799ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
800665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
801665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
802aa4dd815SMatthias Ringwald 
803665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
804a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
805a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
806a0ffb263SMatthias Ringwald             return hfp_connection;
807aa4dd815SMatthias Ringwald         }
808aa4dd815SMatthias Ringwald     }
809aa4dd815SMatthias Ringwald     return NULL;
810aa4dd815SMatthias Ringwald }
811aa4dd815SMatthias Ringwald 
812ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
813d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
814d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
815aa4dd815SMatthias Ringwald 
816d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
817a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
818a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
819aa4dd815SMatthias Ringwald 
820a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
821a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
822aa4dd815SMatthias Ringwald 
823a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
824aa4dd815SMatthias Ringwald }
825aa4dd815SMatthias Ringwald 
826a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
827a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
828a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
829a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
830a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
831aa4dd815SMatthias Ringwald }
832aa4dd815SMatthias Ringwald 
833a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
834a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
835a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
836aa4dd815SMatthias Ringwald }
837aa4dd815SMatthias Ringwald 
838aa4dd815SMatthias Ringwald //
839aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
840aa4dd815SMatthias Ringwald //
841aa4dd815SMatthias Ringwald 
842a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
843aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
844a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
845d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
846aa4dd815SMatthias Ringwald     } else {
847a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
848a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
849a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
850a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
851ca59be51SMatthias Ringwald         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
852aa4dd815SMatthias Ringwald     }
853aa4dd815SMatthias Ringwald }
854aa4dd815SMatthias Ringwald 
855a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
856a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
857a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
858a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
859ca59be51SMatthias Ringwald     hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGINIG);
860aa4dd815SMatthias Ringwald }
861aa4dd815SMatthias Ringwald 
862aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
863aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
864aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
865aa4dd815SMatthias Ringwald 
866665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
867665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
868665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
869a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
870a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
871a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
872a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
873d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
874aa4dd815SMatthias Ringwald         }
875a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
876a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
877aa4dd815SMatthias Ringwald         }
878a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
879aa4dd815SMatthias Ringwald     }
880aa4dd815SMatthias Ringwald }
881aa4dd815SMatthias Ringwald 
882aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
883aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
884aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
885aa4dd815SMatthias Ringwald 
886665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
887665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
888665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
889a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
890a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
891a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
892a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
893aa4dd815SMatthias Ringwald     }
894aa4dd815SMatthias Ringwald }
895aa4dd815SMatthias Ringwald 
896aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
897aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("call");
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_callheld_state(void){
911aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callheld");
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_hf_accept_call(hfp_connection_t * source){
925aa4dd815SMatthias Ringwald 
926aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
927aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
928aa4dd815SMatthias Ringwald 
929665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
930665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
931665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
932a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
933a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
934a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
935aa4dd815SMatthias Ringwald 
936a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
937a0ffb263SMatthias Ringwald         if (hfp_connection == source){
938a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
939aa4dd815SMatthias Ringwald 
940aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
941a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
942aa4dd815SMatthias Ringwald             } else {
943a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
944d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
945aa4dd815SMatthias Ringwald             }
946aa4dd815SMatthias Ringwald 
947a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
948a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
949aa4dd815SMatthias Ringwald 
950aa4dd815SMatthias Ringwald         } else {
951a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
952aa4dd815SMatthias Ringwald         }
953a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
954aa4dd815SMatthias Ringwald     }
955aa4dd815SMatthias Ringwald }
956aa4dd815SMatthias Ringwald 
957aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
958aa4dd815SMatthias Ringwald 
959aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
960aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
961aa4dd815SMatthias Ringwald 
962665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
963665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
964665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
965a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
966a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
967aa4dd815SMatthias Ringwald 
968a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
969a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
970aa4dd815SMatthias Ringwald 
971a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
972a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
973aa4dd815SMatthias Ringwald 
974a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
975aa4dd815SMatthias Ringwald         break;  // only single
976aa4dd815SMatthias Ringwald     }
977aa4dd815SMatthias Ringwald }
978aa4dd815SMatthias Ringwald 
979aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
980aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
981665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
982665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
983665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
984665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
985aa4dd815SMatthias Ringwald         if (connection->call_state != HFP_CALL_RINGING &&
986aa4dd815SMatthias Ringwald             connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
987aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
988aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
989aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
990aa4dd815SMatthias Ringwald         hfp_run_for_context(connection);
991aa4dd815SMatthias Ringwald     }
992aa4dd815SMatthias Ringwald }
993aa4dd815SMatthias Ringwald 
994ca59be51SMatthias Ringwald static void hfp_ag_emit_simple_event(uint8_t event_subtype){
995ca59be51SMatthias Ringwald     uint8_t event[3];
996ca59be51SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
997ca59be51SMatthias Ringwald     event[1] = sizeof(event) - 2;
998ca59be51SMatthias Ringwald     event[2] = event_subtype;
999ca59be51SMatthias Ringwald     if (!hfp_ag_callback) return;
1000ca59be51SMatthias Ringwald     (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1001ca59be51SMatthias Ringwald }
1002ca59be51SMatthias Ringwald 
1003aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1004aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1005aa4dd815SMatthias Ringwald 
1006665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1007665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1008665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1009a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1010d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1011a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1012a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1013a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1014a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 1;
1015a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1016aa4dd815SMatthias Ringwald     }
1017ca59be51SMatthias Ringwald     hfp_ag_emit_simple_event(HFP_SUBEVENT_CALL_TERMINATED);
1018aa4dd815SMatthias Ringwald }
1019aa4dd815SMatthias Ringwald 
10200cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
1021aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1022aa4dd815SMatthias Ringwald     if (!indicator){
1023d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
102445718b6fSMatthias Ringwald         return;
1025aa4dd815SMatthias Ringwald     };
1026d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1027aa4dd815SMatthias Ringwald }
1028aa4dd815SMatthias Ringwald 
10290cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1030d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1031d210d9c4SMatthias Ringwald     if (!indicator){
1032d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
103345718b6fSMatthias Ringwald         return;
1034d210d9c4SMatthias Ringwald     };
1035d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1036d210d9c4SMatthias Ringwald }
1037d210d9c4SMatthias Ringwald 
10380cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1039aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1040aa4dd815SMatthias Ringwald     if (!indicator){
1041aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
104245718b6fSMatthias Ringwald         return;
1043aa4dd815SMatthias Ringwald     };
1044d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1045aa4dd815SMatthias Ringwald }
1046aa4dd815SMatthias Ringwald 
1047aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1048665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1049665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1050665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1051a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1052a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
1053a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1054a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1055aa4dd815SMatthias Ringwald     }
1056aa4dd815SMatthias Ringwald }
1057aa4dd815SMatthias Ringwald 
1058aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1059665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1060665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1061665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1062a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1063a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1064aa4dd815SMatthias Ringwald     }
1065aa4dd815SMatthias Ringwald     return NULL;
1066aa4dd815SMatthias Ringwald }
1067aa4dd815SMatthias Ringwald 
1068a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1069665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1070665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1071665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1072a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1073a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1074ce263fc8SMatthias Ringwald     }
1075ce263fc8SMatthias Ringwald }
1076ce263fc8SMatthias Ringwald 
1077a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1078aa4dd815SMatthias Ringwald     int indicator_index;
1079a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1080aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1081a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1082a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1083a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1084a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1085a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1086a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1087a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1088ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
1089aa4dd815SMatthias Ringwald             break;
1090aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1091a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1092a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1093a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1094aa4dd815SMatthias Ringwald             break;
1095aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1096a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1097a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1098aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1099a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1100aa4dd815SMatthias Ringwald             break;
1101aa4dd815SMatthias Ringwald         default:
1102aa4dd815SMatthias Ringwald             break;
1103aa4dd815SMatthias Ringwald     }
1104aa4dd815SMatthias Ringwald     return 0;
1105aa4dd815SMatthias Ringwald }
1106a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1107a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1108aa4dd815SMatthias Ringwald     int indicator_index;
1109d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1110d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1111d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
111274386ee0SMatthias Ringwald 
1113d210d9c4SMatthias Ringwald     //printf("hfp_ag_call_sm event %d \n", event);
1114aa4dd815SMatthias Ringwald     switch (event){
1115aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1116d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1117aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1118d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1119aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1120d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1121d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1122aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
112360ebb071SMilanka Ringwald                             log_info("AG rings");
1124aa4dd815SMatthias Ringwald                             break;
1125aa4dd815SMatthias Ringwald                         default:
11263deb3ec6SMatthias Ringwald                             break;
11273deb3ec6SMatthias Ringwald                     }
11283deb3ec6SMatthias Ringwald                     break;
1129aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1130d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1131aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1132d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1133d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1134aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
113560ebb071SMilanka Ringwald                             log_info("AG call waiting");
1136aa4dd815SMatthias Ringwald                             break;
1137aa4dd815SMatthias Ringwald                         default:
11383deb3ec6SMatthias Ringwald                             break;
11393deb3ec6SMatthias Ringwald                     }
11403deb3ec6SMatthias Ringwald                     break;
1141aa4dd815SMatthias Ringwald             }
1142aa4dd815SMatthias Ringwald             break;
1143aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1144d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1145aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1146d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1147aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1148d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1149d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1150d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1151aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
115260ebb071SMilanka Ringwald                             log_info("AG answers call, accept call by GSM");
1153aa4dd815SMatthias Ringwald                             break;
1154aa4dd815SMatthias Ringwald                         default:
11553deb3ec6SMatthias Ringwald                             break;
11563deb3ec6SMatthias Ringwald                     }
1157aa4dd815SMatthias Ringwald                     break;
1158aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1159d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1160aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
116160ebb071SMilanka Ringwald                             log_info("AG: current call is placed on hold, incoming call gets active");
1162d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1163d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1164d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1165ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1166ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1167aa4dd815SMatthias Ringwald                             break;
1168aa4dd815SMatthias Ringwald                         default:
1169aa4dd815SMatthias Ringwald                             break;
1170aa4dd815SMatthias Ringwald                     }
1171aa4dd815SMatthias Ringwald                     break;
1172aa4dd815SMatthias Ringwald             }
1173aa4dd815SMatthias Ringwald             break;
1174ce263fc8SMatthias Ringwald 
1175ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1176d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1177ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1178d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1179ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
118060ebb071SMilanka Ringwald                             log_info("AG: joining held call with active call");
1181d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG);
1182d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1183ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1184ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1185ce263fc8SMatthias Ringwald                             break;
1186ce263fc8SMatthias Ringwald                         default:
1187ce263fc8SMatthias Ringwald                             break;
1188ce263fc8SMatthias Ringwald                     }
1189ce263fc8SMatthias Ringwald                     break;
1190ce263fc8SMatthias Ringwald                 default:
1191ce263fc8SMatthias Ringwald                     break;
1192ce263fc8SMatthias Ringwald             }
1193ce263fc8SMatthias Ringwald             break;
1194ce263fc8SMatthias Ringwald 
1195aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1196d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1197aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1198d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1199aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1200d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF);
1201d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1202d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1203a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
120460ebb071SMilanka Ringwald                             log_info("HF answers call, accept call by GSM");
1205ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1206aa4dd815SMatthias Ringwald                             break;
1207aa4dd815SMatthias Ringwald                         default:
1208aa4dd815SMatthias Ringwald                             break;
1209aa4dd815SMatthias Ringwald                     }
12103deb3ec6SMatthias Ringwald                     break;
12113deb3ec6SMatthias Ringwald                 default:
12123deb3ec6SMatthias Ringwald                     break;
12133deb3ec6SMatthias Ringwald             }
12143deb3ec6SMatthias Ringwald             break;
12153deb3ec6SMatthias Ringwald 
1216ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1217d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1218ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1219d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1220ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1221d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG);
1222ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1223ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1224a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1225ce263fc8SMatthias Ringwald                             // as with regualr call
1226d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1227d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1228ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
122960ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by AG");
1230ce263fc8SMatthias Ringwald                             break;
1231ce263fc8SMatthias Ringwald                         default:
1232ce263fc8SMatthias Ringwald                             break;
1233ce263fc8SMatthias Ringwald                     }
1234ce263fc8SMatthias Ringwald                     break;
1235ce263fc8SMatthias Ringwald                 default:
1236ce263fc8SMatthias Ringwald                     break;
1237ce263fc8SMatthias Ringwald             }
1238ce263fc8SMatthias Ringwald             break;
1239ce263fc8SMatthias Ringwald 
1240ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1241d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1242ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1243d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1244ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1245d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF);
1246ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1247ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1248a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1249ce263fc8SMatthias Ringwald                             // as with regualr call
1250d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1251d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1252a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
125360ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by HF");
1254ce263fc8SMatthias Ringwald                             break;
1255ce263fc8SMatthias Ringwald                         default:
1256ce263fc8SMatthias Ringwald                             break;
1257ce263fc8SMatthias Ringwald                     }
1258ce263fc8SMatthias Ringwald                     break;
1259ce263fc8SMatthias Ringwald                 default:
1260ce263fc8SMatthias Ringwald                     break;
1261ce263fc8SMatthias Ringwald             }
1262ce263fc8SMatthias Ringwald             break;
1263ce263fc8SMatthias Ringwald 
1264ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1265ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1266ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1267ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1268d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG);
1269a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1270ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1271a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
127260ebb071SMilanka Ringwald             log_info("Held Call accepted and active");
1273ce263fc8SMatthias Ringwald             break;
1274ce263fc8SMatthias Ringwald 
1275ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1276ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1277ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1278ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1279d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG);
1280a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1281ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1282a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1283ce263fc8SMatthias Ringwald             // from terminate by ag
1284d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1285ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1286ce263fc8SMatthias Ringwald             break;
1287ce263fc8SMatthias Ringwald 
1288aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1289d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1290aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1291d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1292aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1293d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1294d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1295aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1296aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
129760ebb071SMilanka Ringwald                             log_info("HF Rejected Incoming call, AG terminate call");
1298aa4dd815SMatthias Ringwald                             break;
1299aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1300aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1301d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1302d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1303aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
130460ebb071SMilanka Ringwald                             log_info("AG terminate outgoing call process");
1305202c8a4cSMatthias Ringwald                             break;
1306aa4dd815SMatthias Ringwald                         default:
1307aa4dd815SMatthias Ringwald                             break;
1308aa4dd815SMatthias Ringwald                     }
1309aa4dd815SMatthias Ringwald                     break;
1310aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1311d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1312d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1313aa4dd815SMatthias Ringwald                     hfp_ag_transfer_call_state();
1314a0ffb263SMatthias Ringwald                     hfp_connection->call_state = HFP_CALL_IDLE;
131560ebb071SMilanka Ringwald                     log_info("AG terminate call");
1316aa4dd815SMatthias Ringwald                     break;
1317aa4dd815SMatthias Ringwald             }
1318aa4dd815SMatthias Ringwald             break;
13193deb3ec6SMatthias Ringwald 
1320aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1321d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1322aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1323d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1324aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1325d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1326d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1327aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
132860ebb071SMilanka Ringwald                             log_info("AG Rejected Incoming call, AG terminate call");
1329aa4dd815SMatthias Ringwald                             break;
1330aa4dd815SMatthias Ringwald                         default:
1331aa4dd815SMatthias Ringwald                             break;
13323deb3ec6SMatthias Ringwald                     }
1333202c8a4cSMatthias Ringwald                     break;
1334aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1335d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1336d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1337d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1338aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
133960ebb071SMilanka Ringwald                     log_info("AG terminate call");
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();
135160ebb071SMilanka Ringwald                             log_info("Incoming call interrupted");
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:
135560ebb071SMilanka Ringwald                             log_info("Outgoing call interrupted\n");
135660ebb071SMilanka Ringwald                             log_info("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();
137560ebb071SMilanka Ringwald                     log_info("AG notify call dropped");
1376aa4dd815SMatthias Ringwald                     break;
1377aa4dd815SMatthias Ringwald                 default:
1378aa4dd815SMatthias Ringwald                     break;
1379aa4dd815SMatthias Ringwald             }
1380aa4dd815SMatthias Ringwald             break;
1381aa4dd815SMatthias Ringwald 
1382aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1383d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1384d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1385a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
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 
1393ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1394aa4dd815SMatthias Ringwald             break;
1395aa4dd815SMatthias Ringwald 
13969cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1397d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1398d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1399a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
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 
140760ebb071SMilanka Ringwald             log_info("Redial last number");
14089cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1409aa4dd815SMatthias Ringwald 
14109cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
141160ebb071SMilanka Ringwald                 log_info("Last number exists: accept call");
1412ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
14139cae807eSMatthias Ringwald             } else {
141460ebb071SMilanka Ringwald                 log_info("log_infoLast number missing: reject call");
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){
145260ebb071SMilanka Ringwald                 log_info("AG putting current call on hold for new outgoing calllog_info");
1453d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1454aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1455485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1456aa4dd815SMatthias Ringwald             }
1457aa4dd815SMatthias Ringwald 
1458aa4dd815SMatthias Ringwald             // start audio if needed
1459d97d752dSMilanka Ringwald             hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
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;
150560ebb071SMilanka Ringwald             log_info("AG: Call Waiting, User Busy");
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){
151960ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Accept new call");
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 {
152360ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Resume held call");
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){
154160ebb071SMilanka Ringwald                 log_info("AG: Call on Hold, Accept new call");
1542d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1543a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1544d210d9c4SMatthias Ringwald             } else {
154560ebb071SMilanka Ringwald                 log_info("AG: Swap calls");
1546d210d9c4SMatthias Ringwald             }
1547d0c20769SMatthias Ringwald 
1548d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1549d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1550a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1551a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1552d210d9c4SMatthias Ringwald             break;
1553d210d9c4SMatthias Ringwald         }
1554d0c20769SMatthias Ringwald 
1555d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1556d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1557d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
155860ebb071SMilanka Ringwald                 log_info("AG: Join 3-way-call");
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);
1562ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1563d210d9c4SMatthias Ringwald             }
1564a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1565d210d9c4SMatthias Ringwald             break;
1566d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1567d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1568d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS);
156960ebb071SMilanka Ringwald             log_info("AG: Transfer call -> Connect two calls and disconnect");
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){
16011cc1d9e9SMilanka Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
16029cae807eSMatthias Ringwald     }
16039cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
160460ebb071SMilanka Ringwald     log_info("hfp_ag_send_current_call_status 000 index %d, dir %d, status %d, mode %d, mpty %d, type %d, number %s", idx, dir, status,
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;
1611724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
1612473ac565SMatthias Ringwald 
1613e30a6a47SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1614473ac565SMatthias Ringwald         log_info("hfp_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1615e30a6a47SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1616e30a6a47SMatthias Ringwald         return;
1617e30a6a47SMatthias Ringwald     }
1618a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1619a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1620a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1621a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1622a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
16239cae807eSMatthias Ringwald         } else {
1624a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1625a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1626a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16279cae807eSMatthias Ringwald         }
1628ce263fc8SMatthias Ringwald         return;
1629ce263fc8SMatthias Ringwald     }
1630ce263fc8SMatthias Ringwald 
1631a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1632a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1633a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1634aa4dd815SMatthias Ringwald         return;
1635aa4dd815SMatthias Ringwald     }
1636aa4dd815SMatthias Ringwald 
1637a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1638a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1639a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1640a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1641485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1642a0ffb263SMatthias Ringwald         return;
1643a0ffb263SMatthias Ringwald     }
1644a0ffb263SMatthias Ringwald 
1645a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1646a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1647a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1648485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1649aa4dd815SMatthias Ringwald         return;
1650aa4dd815SMatthias Ringwald     }
1651aa4dd815SMatthias Ringwald 
1652ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1653a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1654a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1655a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1656485ac19eSMilanka Ringwald         hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1657ce263fc8SMatthias Ringwald         return;
1658ce263fc8SMatthias Ringwald     }
1659ce263fc8SMatthias Ringwald 
1660a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1661a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1662a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1663485ac19eSMilanka Ringwald         hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1664ce263fc8SMatthias Ringwald         return;
1665ce263fc8SMatthias Ringwald     }
1666ce263fc8SMatthias Ringwald 
1667aa4dd815SMatthias Ringwald     // update AG indicators
1668a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1669aa4dd815SMatthias Ringwald         int i;
1670a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1671a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1672a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1673a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1674ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1675ce263fc8SMatthias Ringwald                     break;
1676ce263fc8SMatthias Ringwald                 }
1677485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1678aa4dd815SMatthias Ringwald                 return;
1679aa4dd815SMatthias Ringwald             }
1680aa4dd815SMatthias Ringwald         }
1681aa4dd815SMatthias Ringwald     }
1682aa4dd815SMatthias Ringwald 
1683a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1684a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1685a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1686485ac19eSMilanka Ringwald         hfp_ag_send_ring(hfp_connection->rfcomm_cid);
1687aa4dd815SMatthias Ringwald         return;
1688aa4dd815SMatthias Ringwald     }
1689aa4dd815SMatthias Ringwald 
1690a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1691a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1692a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1693a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1694aa4dd815SMatthias Ringwald         return;
1695aa4dd815SMatthias Ringwald     }
1696aa4dd815SMatthias Ringwald 
1697a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1698a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1699a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1700a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1701a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1702aa4dd815SMatthias Ringwald         return;
1703aa4dd815SMatthias Ringwald     }
1704aa4dd815SMatthias Ringwald 
1705a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1706a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1707a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1708a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1709ce263fc8SMatthias Ringwald         } else {
1710a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1711a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1712485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1713ce263fc8SMatthias Ringwald         }
1714a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1715485ac19eSMilanka Ringwald 
1716ce263fc8SMatthias Ringwald     }
1717ce263fc8SMatthias Ringwald 
1718a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1719a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1720a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1721485ac19eSMilanka Ringwald         hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1722aa4dd815SMatthias Ringwald         return;
1723aa4dd815SMatthias Ringwald     }
1724aa4dd815SMatthias Ringwald 
1725a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1726a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1727a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1728485ac19eSMilanka Ringwald         hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
17293deb3ec6SMatthias Ringwald         return;
17303deb3ec6SMatthias Ringwald     }
17313deb3ec6SMatthias Ringwald 
1732a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1733a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1734485ac19eSMilanka Ringwald         hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1735ce263fc8SMatthias Ringwald         return;
1736ce263fc8SMatthias Ringwald     }
1737ce263fc8SMatthias Ringwald 
17382ee45eb2SMilanka Ringwald     // trigger codec exchange if requested by app
17392ee45eb2SMilanka Ringwald     if (hfp_connection->trigger_codec_exchange){
17402ee45eb2SMilanka Ringwald         log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state);
17412ee45eb2SMilanka Ringwald     }
17422ee45eb2SMilanka Ringwald     if (hfp_connection->trigger_codec_exchange && hfp_connection->command == HFP_CMD_NONE){
17432ee45eb2SMilanka Ringwald         switch (hfp_connection->codecs_state){
17442ee45eb2SMilanka Ringwald             case HFP_CODECS_IDLE:
17452ee45eb2SMilanka Ringwald             case HFP_CODECS_RECEIVED_LIST:
17462ee45eb2SMilanka Ringwald             case HFP_CODECS_AG_RESEND_COMMON_CODEC:
17472ee45eb2SMilanka Ringwald             case HFP_CODECS_ERROR:
17482ee45eb2SMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 0;
17492ee45eb2SMilanka Ringwald                 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
17502ee45eb2SMilanka Ringwald                 break;
17512ee45eb2SMilanka Ringwald             default:
17522ee45eb2SMilanka Ringwald                 break;
17532ee45eb2SMilanka Ringwald         }
17542ee45eb2SMilanka Ringwald     }
17552ee45eb2SMilanka Ringwald 
1756c04cf7ffSMilanka Ringwald     int cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1757c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1758c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
17593deb3ec6SMatthias Ringwald     }
17603deb3ec6SMatthias Ringwald 
1761c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1762c04cf7ffSMilanka Ringwald         cmd_sent = call_setup_state_machine(hfp_connection);
1763aa4dd815SMatthias Ringwald     }
1764aa4dd815SMatthias Ringwald 
1765c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1766c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
1767aa4dd815SMatthias Ringwald     }
17683deb3ec6SMatthias Ringwald 
1769c04cf7ffSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_NONE && !cmd_sent){
1770a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1771a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
17723deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1773a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1774a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
17753deb3ec6SMatthias Ringwald                 break;
17763deb3ec6SMatthias Ringwald             default:
17773deb3ec6SMatthias Ringwald                 break;
17783deb3ec6SMatthias Ringwald         }
17793deb3ec6SMatthias Ringwald     }
1780c04cf7ffSMilanka Ringwald 
1781c04cf7ffSMilanka Ringwald     if (cmd_sent){
1782a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1783473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1784473ac565SMatthias Ringwald     }
17853deb3ec6SMatthias Ringwald }
1786d68dcce1SMatthias Ringwald 
1787ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1788ce263fc8SMatthias Ringwald     int i;
1789a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1790a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1791ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1792ce263fc8SMatthias Ringwald             return indicator;
1793ce263fc8SMatthias Ringwald         }
1794ce263fc8SMatthias Ringwald     }
1795ce263fc8SMatthias Ringwald     return NULL;
1796ce263fc8SMatthias Ringwald }
17973deb3ec6SMatthias Ringwald 
1798e9c22d4eSMatthias Ringwald static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
17998a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
18008a46ec40SMatthias Ringwald 
18018a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
18028a46ec40SMatthias Ringwald     if (size < 1) return;
18039ec2630cSMatthias Ringwald 
1804a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1805a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
18061e35c04dSMatthias Ringwald 
18078a46ec40SMatthias Ringwald     // temp overwrite last byte (most likely \n for log_info)
18081e35c04dSMatthias Ringwald     char last_char = packet[size-1];
18091e35c04dSMatthias Ringwald     packet[size-1] = 0;
18101e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
18111e35c04dSMatthias Ringwald     packet[size-1] = last_char;
18121e35c04dSMatthias Ringwald 
18138a46ec40SMatthias Ringwald     // process messages byte-wise
18143deb3ec6SMatthias Ringwald     int pos;
18153deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1816a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1817aa4dd815SMatthias Ringwald     }
1818ce263fc8SMatthias Ringwald     hfp_generic_status_indicator_t * indicator;
1819ce263fc8SMatthias Ringwald     int value;
1820a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
1821ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1822ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_active){
1823a0ffb263SMatthias Ringwald                 hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1824ce263fc8SMatthias Ringwald             }
1825a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1826ce263fc8SMatthias Ringwald             break;
1827ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
18282308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
182960ebb071SMilanka Ringwald             log_info("HF Response and Hold: %u", value);
1830ce263fc8SMatthias Ringwald             switch(value){
1831ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1832a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1833ce263fc8SMatthias Ringwald                     break;
1834ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1835a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1836ce263fc8SMatthias Ringwald                     break;
1837ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1838a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1839ce263fc8SMatthias Ringwald                     break;
1840ce263fc8SMatthias Ringwald                 default:
1841ce263fc8SMatthias Ringwald                     break;
1842ce263fc8SMatthias Ringwald             }
1843a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1844ce263fc8SMatthias Ringwald             break;
1845ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
1846a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1847ce263fc8SMatthias Ringwald             // find indicator by assigned number
1848a0ffb263SMatthias Ringwald             indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1849ce263fc8SMatthias Ringwald             if (!indicator){
1850a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1851ce263fc8SMatthias Ringwald                 break;
1852ce263fc8SMatthias Ringwald             }
18532308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1854ce263fc8SMatthias Ringwald             switch (indicator->uuid){
1855ce263fc8SMatthias Ringwald                 case 1: // enhanced security
1856ce263fc8SMatthias Ringwald                     if (value > 1) {
1857a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1858ce263fc8SMatthias Ringwald                         return;
1859ce263fc8SMatthias Ringwald                     }
186060ebb071SMilanka Ringwald                     log_info("HF Indicator 'enhanced security' set to %u", value);
1861ce263fc8SMatthias Ringwald                     break;
1862ce263fc8SMatthias Ringwald                 case 2: // battery level
1863ce263fc8SMatthias Ringwald                     if (value > 100){
1864a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1865ce263fc8SMatthias Ringwald                         return;
1866ce263fc8SMatthias Ringwald                     }
186760ebb071SMilanka Ringwald                     log_info("HF Indicator 'battery' set to %u", value);
1868ce263fc8SMatthias Ringwald                     break;
1869ce263fc8SMatthias Ringwald                 default:
187060ebb071SMilanka Ringwald                     log_info("HF Indicator unknown set to %u", value);
1871ce263fc8SMatthias Ringwald                     break;
1872ce263fc8SMatthias Ringwald             }
1873a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1874ce263fc8SMatthias Ringwald             break;
1875ce263fc8SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1876ce263fc8SMatthias Ringwald             // expected by SLC state machine
1877a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1878a0ffb263SMatthias Ringwald             hfp_connection->send_ag_indicators_segment = 0;
1879a0ffb263SMatthias Ringwald             hfp_connection->send_ag_status_indicators = 1;
1880ce263fc8SMatthias Ringwald             break;
1881ce263fc8SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1882a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1883a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1884a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 1;
1885ce263fc8SMatthias Ringwald             break;
1886ce263fc8SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1887ce263fc8SMatthias Ringwald             if (subscriber_numbers_count == 0){
1888485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1889ce263fc8SMatthias Ringwald                 break;
1890ce263fc8SMatthias Ringwald             }
1891a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1892a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 1;
1893ce263fc8SMatthias Ringwald             break;
1894c1797c7dSMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
1895a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1896ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]);
1897c1797c7dSMatthias Ringwald             break;
1898aa4dd815SMatthias Ringwald         case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1899a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1900ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1901aa4dd815SMatthias Ringwald             break;
1902aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1903a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1904aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1905a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1906a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
190760ebb071SMilanka Ringwald                 log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction);
1908aa4dd815SMatthias Ringwald             } else {
1909a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1910aa4dd815SMatthias Ringwald             }
1911aa4dd815SMatthias Ringwald             break;
1912aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_ANSWERED:
1913a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
191460ebb071SMilanka Ringwald             log_info("HFP: ATA");
1915a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1916aa4dd815SMatthias Ringwald             break;
1917aa4dd815SMatthias Ringwald         case HFP_CMD_HANG_UP_CALL:
1918a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1919a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1920a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1921aa4dd815SMatthias Ringwald             break;
1922aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_HOLD: {
1923aa4dd815SMatthias Ringwald             // TODO: fully implement this
1924a0ffb263SMatthias Ringwald             log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]);
1925a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1926a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1927a0ffb263SMatthias Ringwald             hfp_connection->call_index = 0;
1928d0c20769SMatthias Ringwald 
1929a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
19302308e108SMilanka Ringwald                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
1931d0c20769SMatthias Ringwald             }
1932d210d9c4SMatthias Ringwald 
1933a0ffb263SMatthias Ringwald             switch (hfp_connection->line_buffer[0]){
1934aa4dd815SMatthias Ringwald                 case '0':
1935d0c20769SMatthias Ringwald                     // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1936a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1937aa4dd815SMatthias Ringwald                     break;
1938aa4dd815SMatthias Ringwald                 case '1':
1939d0c20769SMatthias Ringwald                     // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1940d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1941d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1942a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1943aa4dd815SMatthias Ringwald                     break;
1944aa4dd815SMatthias Ringwald                 case '2':
1945d0c20769SMatthias Ringwald                     // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1946d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1947d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1948a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1949aa4dd815SMatthias Ringwald                     break;
1950aa4dd815SMatthias Ringwald                 case '3':
1951d0c20769SMatthias Ringwald                     // Adds a held call to the conversation.
1952a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1953aa4dd815SMatthias Ringwald                     break;
1954aa4dd815SMatthias Ringwald                 case '4':
1955d0c20769SMatthias Ringwald                     // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1956a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1957aa4dd815SMatthias Ringwald                     break;
1958aa4dd815SMatthias Ringwald                 default:
1959aa4dd815SMatthias Ringwald                     break;
1960aa4dd815SMatthias Ringwald             }
1961aa4dd815SMatthias Ringwald             break;
1962aa4dd815SMatthias Ringwald         }
1963aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_PHONE_NUMBER:
1964a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1965a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
1966aa4dd815SMatthias Ringwald             break;
1967aa4dd815SMatthias Ringwald         case HFP_CMD_REDIAL_LAST_NUMBER:
1968a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1969a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
1970aa4dd815SMatthias Ringwald             break;
1971aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
1972a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1973a0ffb263SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0';
1974a0ffb263SMatthias Ringwald             log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
1975a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1976aa4dd815SMatthias Ringwald             break;
1977aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1978a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1979a0ffb263SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0';
1980a0ffb263SMatthias Ringwald             log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
1981a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1982aa4dd815SMatthias Ringwald             break;
1983aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1984a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1985a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
198660ebb071SMilanka Ringwald             log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
1987aa4dd815SMatthias Ringwald             break;
1988aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1989a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1990a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
199160ebb071SMilanka Ringwald             log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
1992aa4dd815SMatthias Ringwald             break;
1993aa4dd815SMatthias Ringwald         default:
1994aa4dd815SMatthias Ringwald             break;
19953deb3ec6SMatthias Ringwald     }
19963deb3ec6SMatthias Ringwald }
19973deb3ec6SMatthias Ringwald 
1998aa4dd815SMatthias Ringwald static void hfp_run(void){
1999d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
2000d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2001d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2002d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2003a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
20043deb3ec6SMatthias Ringwald     }
20053deb3ec6SMatthias Ringwald }
20063deb3ec6SMatthias Ringwald 
2007e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
20083deb3ec6SMatthias Ringwald     switch (packet_type){
20093deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
2010e9c22d4eSMatthias Ringwald             hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size);
20113deb3ec6SMatthias Ringwald             break;
20123deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2013e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2014e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2015e30a6a47SMatthias Ringwald                 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2016e30a6a47SMatthias Ringwald                 return;
2017e30a6a47SMatthias Ringwald             }
2018323d3000SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2019aa4dd815SMatthias Ringwald             break;
20203deb3ec6SMatthias Ringwald         default:
20213deb3ec6SMatthias Ringwald             break;
20223deb3ec6SMatthias Ringwald     }
20233deb3ec6SMatthias Ringwald 
20243deb3ec6SMatthias Ringwald     hfp_run();
20253deb3ec6SMatthias Ringwald }
20263deb3ec6SMatthias Ringwald 
2027a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
20283deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
20293deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
20303deb3ec6SMatthias Ringwald         return;
20313deb3ec6SMatthias Ringwald     }
20323deb3ec6SMatthias Ringwald     int i;
2033a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
20343deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
20353deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
20363deb3ec6SMatthias Ringwald     }
2037a0ffb263SMatthias Ringwald }
20383deb3ec6SMatthias Ringwald 
2039a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2040a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2041a0ffb263SMatthias Ringwald }
20423deb3ec6SMatthias Ringwald 
2043a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2044a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
2045a0ffb263SMatthias Ringwald     memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2046a0ffb263SMatthias Ringwald }
20473deb3ec6SMatthias Ringwald 
2048a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
2049a0ffb263SMatthias Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return;
2050a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
2051a0ffb263SMatthias Ringwald     memcpy(hfp_generic_status_indicators, hf_indicators, hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2052a0ffb263SMatthias Ringwald }
2053a0ffb263SMatthias Ringwald 
2054a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
20553deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
20563deb3ec6SMatthias Ringwald     memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *));
2057a0ffb263SMatthias Ringwald }
2058a0ffb263SMatthias Ringwald 
2059a0ffb263SMatthias Ringwald 
2060a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
2061*520c92d5SMatthias Ringwald     hfp_init();
2062d63c37a1SMatthias Ringwald 
2063e9c22d4eSMatthias Ringwald     rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
2064e9c22d4eSMatthias Ringwald     hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler);
2065aa4dd815SMatthias Ringwald 
2066d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2067d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2068d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2069d210d9c4SMatthias Ringwald 
2070d210d9c4SMatthias Ringwald     hfp_gsm_init();
20713deb3ec6SMatthias Ringwald }
20723deb3ec6SMatthias Ringwald 
20733deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
2074323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
20753deb3ec6SMatthias Ringwald }
20763deb3ec6SMatthias Ringwald 
2077d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
2078d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2079a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2080a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2081a33eb0c4SMilanka Ringwald         return;
2082a33eb0c4SMilanka Ringwald     }
2083a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2084a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20853deb3ec6SMatthias Ringwald }
20863deb3ec6SMatthias Ringwald 
2087d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
2088d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2089a0ffb263SMatthias Ringwald     if (!hfp_connection){
2090a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
20913deb3ec6SMatthias Ringwald         return;
20923deb3ec6SMatthias Ringwald     }
2093a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2094a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
20953deb3ec6SMatthias Ringwald         return;
20963deb3ec6SMatthias Ringwald     }
2097a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2098a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20993deb3ec6SMatthias Ringwald }
21003deb3ec6SMatthias Ringwald 
2101a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
21022ee45eb2SMilanka Ringwald     log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state);
2103a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2104a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
21053deb3ec6SMatthias Ringwald 
2106a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2107a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2108d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2109d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2110a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
21117522e673SMatthias Ringwald         // now, pick link settings
21127522e673SMatthias Ringwald         hfp_init_link_settings(hfp_connection);
21132ee45eb2SMilanka Ringwald         return;
21143deb3ec6SMatthias Ringwald     }
2115aa4dd815SMatthias Ringwald 
21162ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 1;
2117aa4dd815SMatthias Ringwald }
2118aa4dd815SMatthias Ringwald 
2119d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
2120d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2121a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2122a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2123a33eb0c4SMilanka Ringwald         return;
2124a33eb0c4SMilanka Ringwald     }
21252ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 0;
2126a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2127a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2128a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21293deb3ec6SMatthias Ringwald }
21303deb3ec6SMatthias Ringwald 
2131d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
2132d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2133a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2134a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2135a33eb0c4SMilanka Ringwald         return;
2136a33eb0c4SMilanka Ringwald     }
2137a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2138a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21393deb3ec6SMatthias Ringwald }
2140aa4dd815SMatthias Ringwald 
2141aa4dd815SMatthias Ringwald /**
2142aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2143aa4dd815SMatthias Ringwald  */
2144aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2145aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2146aa4dd815SMatthias Ringwald         return;
2147aa4dd815SMatthias Ringwald     }
2148aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2149aa4dd815SMatthias Ringwald 
2150665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2151665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2152665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2153a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2154a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2155a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2156aa4dd815SMatthias Ringwald     }
2157aa4dd815SMatthias Ringwald }
2158aa4dd815SMatthias Ringwald 
2159aa4dd815SMatthias Ringwald /**
2160aa4dd815SMatthias Ringwald  * @brief Called from GSM
2161aa4dd815SMatthias Ringwald  */
2162aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2163aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2164aa4dd815SMatthias Ringwald }
2165aa4dd815SMatthias Ringwald 
2166aa4dd815SMatthias Ringwald /**
2167aa4dd815SMatthias Ringwald  * @brief number is stored.
2168aa4dd815SMatthias Ringwald  */
2169aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2170d0c20769SMatthias Ringwald     hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number);
2171aa4dd815SMatthias Ringwald }
2172aa4dd815SMatthias Ringwald 
2173aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2174aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2175aa4dd815SMatthias Ringwald }
2176aa4dd815SMatthias Ringwald 
2177aa4dd815SMatthias Ringwald // call from AG UI
2178aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2179aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2180aa4dd815SMatthias Ringwald }
2181aa4dd815SMatthias Ringwald 
2182ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2183ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2184ce263fc8SMatthias Ringwald }
2185ce263fc8SMatthias Ringwald 
2186aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2187aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2188aa4dd815SMatthias Ringwald }
2189aa4dd815SMatthias Ringwald 
2190aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2191aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2192aa4dd815SMatthias Ringwald }
2193aa4dd815SMatthias Ringwald 
2194aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2195aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2196aa4dd815SMatthias Ringwald }
2197aa4dd815SMatthias Ringwald 
2198aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2199aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2200aa4dd815SMatthias Ringwald }
2201aa4dd815SMatthias Ringwald 
2202aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2203aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2204aa4dd815SMatthias Ringwald }
2205ce263fc8SMatthias Ringwald 
2206ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2207ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2208ce263fc8SMatthias Ringwald }
2209ce263fc8SMatthias Ringwald 
2210ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2211ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2212ce263fc8SMatthias Ringwald }
2213ce263fc8SMatthias Ringwald 
2214ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2215ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2216ce263fc8SMatthias Ringwald }
2217ce263fc8SMatthias Ringwald 
2218aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2219aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2220aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2221aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2222aa4dd815SMatthias Ringwald 
2223ce263fc8SMatthias Ringwald 
2224665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2225665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2226665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2227a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2228a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2229ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2230ce263fc8SMatthias Ringwald             continue;
2231ce263fc8SMatthias Ringwald         }
2232ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2233a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2234a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2235aa4dd815SMatthias Ringwald     }
2236aa4dd815SMatthias Ringwald }
2237aa4dd815SMatthias Ringwald 
2238aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2239aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2240aa4dd815SMatthias Ringwald }
2241aa4dd815SMatthias Ringwald 
2242aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2243aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2244aa4dd815SMatthias Ringwald }
2245aa4dd815SMatthias Ringwald 
2246aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2247aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2248aa4dd815SMatthias Ringwald }
2249aa4dd815SMatthias Ringwald 
2250aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2251aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2252aa4dd815SMatthias Ringwald }
2253aa4dd815SMatthias Ringwald 
2254d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){
2255aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
2256d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2257a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2258a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2259a33eb0c4SMilanka Ringwald         return;
2260a33eb0c4SMilanka Ringwald     }
2261aa4dd815SMatthias Ringwald 
2262a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
226360ebb071SMilanka Ringwald         log_info("AG cannot acivate voice recognition - not supported by HF");
2264aa4dd815SMatthias Ringwald         return;
2265aa4dd815SMatthias Ringwald     }
2266aa4dd815SMatthias Ringwald 
2267aa4dd815SMatthias Ringwald     if (activate){
2268d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(acl_handle);
2269aa4dd815SMatthias Ringwald     }
2270aa4dd815SMatthias Ringwald 
2271a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2272a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2273a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2274aa4dd815SMatthias Ringwald }
2275aa4dd815SMatthias Ringwald 
2276d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
2277d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2278a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2279a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2280a33eb0c4SMilanka Ringwald         return;
2281a33eb0c4SMilanka Ringwald     }
2282a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2283a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2284a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2285a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2286aa4dd815SMatthias Ringwald     }
2287a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2288aa4dd815SMatthias Ringwald }
2289aa4dd815SMatthias Ringwald 
2290d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
2291d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2292a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2293a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2294a33eb0c4SMilanka Ringwald         return;
2295a33eb0c4SMilanka Ringwald     }
2296a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2297a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2298a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2299aa4dd815SMatthias Ringwald     }
2300a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2301aa4dd815SMatthias Ringwald }
2302aa4dd815SMatthias Ringwald 
2303d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
2304d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2305a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2306a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2307a33eb0c4SMilanka Ringwald         return;
2308a33eb0c4SMilanka Ringwald     }
2309aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2310a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2311aa4dd815SMatthias Ringwald }
2312aa4dd815SMatthias Ringwald 
2313d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
2314d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2315a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2316a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2317a33eb0c4SMilanka Ringwald         return;
2318a33eb0c4SMilanka Ringwald     }
2319a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2320aa4dd815SMatthias Ringwald }
2321aa4dd815SMatthias Ringwald 
2322d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
2323d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2324a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2325a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2326a33eb0c4SMilanka Ringwald         return;
2327a33eb0c4SMilanka Ringwald     }
2328a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2329c1797c7dSMatthias Ringwald }
2330aa4dd815SMatthias Ringwald 
2331ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2332ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2333ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2334ce263fc8SMatthias Ringwald }
2335ce263fc8SMatthias Ringwald 
23369cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
23379cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2338ce263fc8SMatthias Ringwald }
2339ce263fc8SMatthias Ringwald 
2340d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
2341d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2342a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2343a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2344a33eb0c4SMilanka Ringwald         return;
2345a33eb0c4SMilanka Ringwald     }
2346a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2347a0ffb263SMatthias Ringwald 
2348a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2349a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2350a0ffb263SMatthias Ringwald }
2351ce263fc8SMatthias Ringwald 
2352