xref: /btstack/src/classic/hfp_ag.c (revision e9c22d4ea120f82e0a0f4f9d59236a70a8ab6236)
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 
109c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
11099a10067SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection);
1113deb3ec6SMatthias Ringwald 
1123deb3ec6SMatthias Ringwald 
113a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
114a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
115a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
116a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1173deb3ec6SMatthias Ringwald     }
118a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
119a0ffb263SMatthias Ringwald }
120a0ffb263SMatthias Ringwald 
121a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
122a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
123a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
124a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
125a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
126a0ffb263SMatthias Ringwald     }
127a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1283deb3ec6SMatthias Ringwald }
1293deb3ec6SMatthias Ringwald 
130aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
131aa4dd815SMatthias Ringwald     int i;
132aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
133aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
134aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
135aa4dd815SMatthias Ringwald         }
136aa4dd815SMatthias Ringwald     }
137aa4dd815SMatthias Ringwald     return NULL;
138aa4dd815SMatthias Ringwald }
139aa4dd815SMatthias Ringwald 
140aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
141aa4dd815SMatthias Ringwald     int i;
142aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
143aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
144aa4dd815SMatthias Ringwald             return i;
145aa4dd815SMatthias Ringwald         }
146aa4dd815SMatthias Ringwald     }
147aa4dd815SMatthias Ringwald     return -1;
148aa4dd815SMatthias Ringwald }
149aa4dd815SMatthias Ringwald 
1503deb3ec6SMatthias Ringwald 
15113839019SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
1523deb3ec6SMatthias Ringwald     if (callback == NULL){
1533deb3ec6SMatthias Ringwald         log_error("hfp_ag_register_packet_handler called with NULL callback");
1543deb3ec6SMatthias Ringwald         return;
1553deb3ec6SMatthias Ringwald     }
156ca59be51SMatthias Ringwald     hfp_ag_callback = callback;
157ca59be51SMatthias Ringwald     hfp_set_ag_callback(callback);
1583deb3ec6SMatthias Ringwald }
1593deb3ec6SMatthias Ringwald 
1600cb5b971SMatthias Ringwald static int use_in_band_tone(void){
161aa4dd815SMatthias Ringwald     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1623deb3ec6SMatthias Ringwald }
1633deb3ec6SMatthias Ringwald 
164a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
165a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1663deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1673deb3ec6SMatthias Ringwald     return hf && ag;
1683deb3ec6SMatthias Ringwald }
1693deb3ec6SMatthias Ringwald 
170a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
171a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1723deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1733deb3ec6SMatthias Ringwald     return hf && ag;
1743deb3ec6SMatthias Ringwald }
1753deb3ec6SMatthias Ringwald 
176a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
177a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
1783deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
1793deb3ec6SMatthias Ringwald     return hf && ag;
1803deb3ec6SMatthias Ringwald }
1813deb3ec6SMatthias Ringwald 
1824f84bf36SMatthias 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){
1833deb3ec6SMatthias Ringwald     if (!name){
1843deb3ec6SMatthias Ringwald         name = default_hfp_ag_service_name;
1853deb3ec6SMatthias Ringwald     }
186235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
1873deb3ec6SMatthias Ringwald 
1883deb3ec6SMatthias Ringwald     /*
1893deb3ec6SMatthias Ringwald      * 0x01 – Ability to reject a call
1903deb3ec6SMatthias Ringwald      * 0x00 – No ability to reject a call
1913deb3ec6SMatthias Ringwald      */
192aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
193aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
194aa4dd815SMatthias Ringwald 
1954f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
1964f84bf36SMatthias Ringwald     //
1974f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1984f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
1994f84bf36SMatthias Ringwald     //
2004f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features &0x1f;
2014f84bf36SMatthias Ringwald     if (supported_features & wide_band_speech){
2024f84bf36SMatthias Ringwald         sdp_features |= 1 << 5; // Wide band speech bit
2034f84bf36SMatthias Ringwald     }
204aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
2054f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
206aa4dd815SMatthias Ringwald }
207aa4dd815SMatthias Ringwald 
208485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){
209aa4dd815SMatthias Ringwald     char buffer[20];
210aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
211aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2123deb3ec6SMatthias Ringwald }
2133deb3ec6SMatthias Ringwald 
2143deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2153deb3ec6SMatthias Ringwald     char buffer[40];
2163deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
2173deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2183deb3ec6SMatthias Ringwald }
2193deb3ec6SMatthias Ringwald 
220485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){
2213deb3ec6SMatthias Ringwald     char buffer[10];
2223deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nOK\r\n");
2233deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2243deb3ec6SMatthias Ringwald }
2253deb3ec6SMatthias Ringwald 
226485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){
227aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
228aa4dd815SMatthias Ringwald }
229aa4dd815SMatthias Ringwald 
230aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
231aa4dd815SMatthias Ringwald     char buffer[50];
232d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
233aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
234aa4dd815SMatthias Ringwald }
235aa4dd815SMatthias Ringwald 
236ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
237ce263fc8SMatthias Ringwald     char buffer[50];
238ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
239ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
240ce263fc8SMatthias Ringwald }
241ce263fc8SMatthias Ringwald 
242c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
243aa4dd815SMatthias Ringwald     char buffer[50];
244d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
245aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
246aa4dd815SMatthias Ringwald }
247aa4dd815SMatthias Ringwald 
248aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
249aa4dd815SMatthias Ringwald     char buffer[50];
250a0ffb263SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
251aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
252aa4dd815SMatthias Ringwald }
253aa4dd815SMatthias Ringwald 
254485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){
2553deb3ec6SMatthias Ringwald     char buffer[10];
2563deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nERROR\r\n");
2573deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2583deb3ec6SMatthias Ringwald }
2593deb3ec6SMatthias Ringwald 
260485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2613deb3ec6SMatthias Ringwald     char buffer[20];
2623deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2633deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2643deb3ec6SMatthias Ringwald }
2653deb3ec6SMatthias Ringwald 
266ad902e3dSMatthias Ringwald // get size for indicator string
267a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
268ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
26945718b6fSMatthias Ringwald     return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
270a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
271a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
272ad902e3dSMatthias Ringwald }
273ad902e3dSMatthias Ringwald 
274ad902e3dSMatthias Ringwald // store indicator
275a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
276ad902e3dSMatthias Ringwald     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
277a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
278a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
279a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2803deb3ec6SMatthias Ringwald }
281ad902e3dSMatthias Ringwald 
282ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
283a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
284a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
285ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
286ad902e3dSMatthias Ringwald     return 3 + (num_indicators-1) * 2;
2873deb3ec6SMatthias Ringwald }
288ad902e3dSMatthias Ringwald 
289ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
290a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
291ad902e3dSMatthias Ringwald     if (index == 0) {
292ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
293ad902e3dSMatthias Ringwald     }
294ad902e3dSMatthias Ringwald     index--;
295a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
296ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
297ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
298a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
299ad902e3dSMatthias Ringwald     }
300ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators - 1){
301ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
302ad902e3dSMatthias Ringwald     }
303ad902e3dSMatthias Ringwald     return 1; // comma
304ad902e3dSMatthias Ringwald }
305ad902e3dSMatthias Ringwald 
306a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
307ad902e3dSMatthias Ringwald     if (index == 0){
308ad902e3dSMatthias Ringwald         *buffer++ = '\r';
30974386ee0SMatthias Ringwald         *buffer++ = '\n';
310ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
311ad902e3dSMatthias Ringwald         memcpy(buffer, HFP_INDICATOR, len);
312ad902e3dSMatthias Ringwald         buffer += len;
313ad902e3dSMatthias Ringwald         *buffer++ = ':';
314ad902e3dSMatthias Ringwald         return;
315ad902e3dSMatthias Ringwald     }
316ad902e3dSMatthias Ringwald     index--;
317a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
318ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
319ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
320a0ffb263SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
321ad902e3dSMatthias Ringwald         return;
322ad902e3dSMatthias Ringwald     }
323ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators-1){
324ad902e3dSMatthias Ringwald         memcpy(buffer, "\r\n\r\nOK\r\n", 8);
325ad902e3dSMatthias Ringwald         return;
326ad902e3dSMatthias Ringwald     }
327ad902e3dSMatthias Ringwald     *buffer = ',';
3283deb3ec6SMatthias Ringwald }
3293deb3ec6SMatthias Ringwald 
3303deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
3313deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3323deb3ec6SMatthias Ringwald     int i;
3333deb3ec6SMatthias Ringwald     int offset = 0;
334a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr-1; i++) {
335a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3363deb3ec6SMatthias Ringwald     }
337a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
338a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3393deb3ec6SMatthias Ringwald     }
3403deb3ec6SMatthias Ringwald     return offset;
3413deb3ec6SMatthias Ringwald }
3423deb3ec6SMatthias Ringwald 
3433deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
344a0ffb263SMatthias Ringwald     if (buffer_size < hfp_generic_status_indicators_nr * 3) return 0;
3453deb3ec6SMatthias Ringwald     int i;
3463deb3ec6SMatthias Ringwald     int offset = 0;
347a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
348a0ffb263SMatthias 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);
3493deb3ec6SMatthias Ringwald     }
3503deb3ec6SMatthias Ringwald     return offset;
3513deb3ec6SMatthias Ringwald }
3523deb3ec6SMatthias Ringwald 
3533deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
3543deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3553deb3ec6SMatthias Ringwald     int i;
3563deb3ec6SMatthias Ringwald     int offset = 0;
3573deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr-1; i++) {
3583deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3593deb3ec6SMatthias Ringwald     }
3603deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3613deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3623deb3ec6SMatthias Ringwald     }
3633deb3ec6SMatthias Ringwald     return offset;
3643deb3ec6SMatthias Ringwald }
3653deb3ec6SMatthias Ringwald 
3663deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
3673deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0;
3683deb3ec6SMatthias Ringwald     int i;
3693deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
3703deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) {
3713deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3723deb3ec6SMatthias Ringwald     }
3733deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3743deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3753deb3ec6SMatthias Ringwald     }
3763deb3ec6SMatthias Ringwald     return offset;
3773deb3ec6SMatthias Ringwald }
3783deb3ec6SMatthias Ringwald 
379485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
380ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
381a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
382a0ffb263SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
3833deb3ec6SMatthias Ringwald 
384ad902e3dSMatthias Ringwald     // assumes: can send now == true
385ad902e3dSMatthias Ringwald     // assumes: num segments > 0
386aba39421SMatthias Ringwald     // assumes: individual segments are smaller than MTU
387ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
388ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
389ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
390ad902e3dSMatthias Ringwald     int offset = 0;
391ad902e3dSMatthias Ringwald     int segment = start_segment;
392ad902e3dSMatthias Ringwald     while (segment < num_segments){
393a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
394aba39421SMatthias Ringwald         if (offset + segment_len > mtu) break;
395aba39421SMatthias Ringwald         // append segement
396a0ffb263SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset);
397ad902e3dSMatthias Ringwald         offset += segment_len;
398ad902e3dSMatthias Ringwald         segment++;
399ad902e3dSMatthias Ringwald     }
400ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
401ad902e3dSMatthias Ringwald     return segment;
402ad902e3dSMatthias Ringwald }
4033deb3ec6SMatthias Ringwald 
404ad902e3dSMatthias Ringwald // returns next segment to store
405485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
406a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
407485ac19eSMilanka Ringwald     return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
408ad902e3dSMatthias Ringwald         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
4093deb3ec6SMatthias Ringwald }
4103deb3ec6SMatthias Ringwald 
411485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){
4123deb3ec6SMatthias Ringwald     char buffer[40];
41389425bfcSMilanka Ringwald     const int size = sizeof(buffer);
41489425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR);
41589425bfcSMilanka Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9);
41689425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4173deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4183deb3ec6SMatthias Ringwald }
4193deb3ec6SMatthias Ringwald 
420485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){
421ad902e3dSMatthias Ringwald     char buffer[40];
42289425bfcSMilanka Ringwald     const int size = sizeof(buffer);
42389425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
42489425bfcSMilanka Ringwald     offset += hfp_ag_call_services_join(buffer+offset, size-offset-9);
42589425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4263deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4273deb3ec6SMatthias Ringwald }
4283deb3ec6SMatthias Ringwald 
4293deb3ec6SMatthias Ringwald 
430485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){
431485ac19eSMilanka Ringwald     return hfp_ag_send_ok(cid);
4323deb3ec6SMatthias Ringwald }
4333deb3ec6SMatthias Ringwald 
434485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4353deb3ec6SMatthias Ringwald     char buffer[40];
43689425bfcSMilanka Ringwald     const int size = sizeof(buffer);
43789425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
43889425bfcSMilanka Ringwald     offset += hfp_hf_indicators_join(buffer+offset, size-offset-10);
43989425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n");
4403deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4413deb3ec6SMatthias Ringwald }
4423deb3ec6SMatthias Ringwald 
443485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4443deb3ec6SMatthias Ringwald     char buffer[40];
4451cc1d9e9SMilanka Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7);
4461cc1d9e9SMilanka Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4473deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4483deb3ec6SMatthias Ringwald }
4493deb3ec6SMatthias Ringwald 
450485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4513deb3ec6SMatthias Ringwald     char buffer[20];
452aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
4533deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4543deb3ec6SMatthias Ringwald }
4553deb3ec6SMatthias Ringwald 
456485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4575e71456cSMilanka Ringwald     char buffer[41];
4583deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
45989425bfcSMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4603deb3ec6SMatthias Ringwald     } else {
46189425bfcSMilanka Ringwald         int offset = snprintf(buffer,  sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format);
4625e71456cSMilanka Ringwald         offset += snprintf(buffer+offset, 16, "%s", op.name);
46389425bfcSMilanka Ringwald         snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4643deb3ec6SMatthias Ringwald     }
4653deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4663deb3ec6SMatthias Ringwald }
4673deb3ec6SMatthias Ringwald 
4681cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
4691cc1d9e9SMilanka Ringwald     char buffer[30];
47089425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value);
4711cc1d9e9SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
4721cc1d9e9SMilanka Ringwald }
4733deb3ec6SMatthias Ringwald 
474485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){
4751cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
4763deb3ec6SMatthias Ringwald }
4773deb3ec6SMatthias Ringwald 
478485ac19eSMilanka Ringwald static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
4791cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
480aa4dd815SMatthias Ringwald }
481aa4dd815SMatthias Ringwald 
482485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
4831cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
484aa4dd815SMatthias Ringwald }
485aa4dd815SMatthias Ringwald 
486485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
4871cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
488aa4dd815SMatthias Ringwald }
489aa4dd815SMatthias Ringwald 
490485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){
4911cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_RESPONSE_AND_HOLD, state);
492ce263fc8SMatthias Ringwald }
493ce263fc8SMatthias Ringwald 
494a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
495bc1b1537SMilanka Ringwald     if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
496bc1b1537SMilanka Ringwald 
4976a7f44bdSMilanka Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){
4986a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
499df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
5003deb3ec6SMatthias Ringwald         }
5013deb3ec6SMatthias Ringwald     }
502df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
5033deb3ec6SMatthias Ringwald }
5043deb3ec6SMatthias Ringwald 
5057522e673SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){
5067522e673SMatthias Ringwald     // determine highest possible link setting
5077522e673SMatthias Ringwald     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
508d9f77559SMatthias Ringwald     // anything else requires eSCO support on both sides
509d9f77559SMatthias Ringwald     if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
5107522e673SMatthias Ringwald         switch (hfp_connection->negotiated_codec){
5117522e673SMatthias Ringwald             case HFP_CODEC_CVSD:
5127522e673SMatthias Ringwald                 hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
5137522e673SMatthias Ringwald                 if ((hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4))
5147522e673SMatthias Ringwald                 &&  (hfp_supported_features                    & (1<<HFP_AGSF_ESCO_S4))){
5157522e673SMatthias Ringwald                     hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
5167522e673SMatthias Ringwald                 }
5177522e673SMatthias Ringwald                 break;
5187522e673SMatthias Ringwald             case HFP_CODEC_MSBC:
5197522e673SMatthias Ringwald                 hfp_connection->link_setting = HFP_LINK_SETTINGS_T2;
5207522e673SMatthias Ringwald                 break;
521d9f77559SMatthias Ringwald             default:
522d9f77559SMatthias Ringwald                 break;
523d9f77559SMatthias Ringwald         }
5247522e673SMatthias Ringwald     }
5257522e673SMatthias Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
5267522e673SMatthias Ringwald }
5277522e673SMatthias Ringwald 
528a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
529aa4dd815SMatthias Ringwald     /* events ( == commands):
530aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
531aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
532aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
533aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
534aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
535aa4dd815SMatthias Ringwald     */
536aa4dd815SMatthias Ringwald 
537a0ffb263SMatthias Ringwald      switch (hfp_connection->codecs_state){
538aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
539a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
540aa4dd815SMatthias Ringwald             break;
541aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
542a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
543aa4dd815SMatthias Ringwald             break;
544aa4dd815SMatthias Ringwald         default:
545aa4dd815SMatthias Ringwald             break;
546aa4dd815SMatthias Ringwald     }
547aa4dd815SMatthias Ringwald 
548aa4dd815SMatthias Ringwald     // printf(" -> State machine: CC\n");
549aa4dd815SMatthias Ringwald 
550a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
551aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
552a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
553a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
554485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
555aa4dd815SMatthias Ringwald                 return 1;
556aa4dd815SMatthias Ringwald             }
557aa4dd815SMatthias Ringwald 
558a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
559aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
560aa4dd815SMatthias Ringwald                 case HFP_CODECS_EXCHANGED:
561a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
562aa4dd815SMatthias Ringwald                     break;
563aa4dd815SMatthias Ringwald                 default:
564aa4dd815SMatthias Ringwald                     break;
565aa4dd815SMatthias Ringwald             }
566485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
567aa4dd815SMatthias Ringwald             return 1;
568aa4dd815SMatthias Ringwald 
569aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
570a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
571485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
572aa4dd815SMatthias Ringwald             return 1;
573aa4dd815SMatthias Ringwald 
574aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
575a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
576a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
577485ac19eSMilanka Ringwald             hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
578aa4dd815SMatthias Ringwald             return 1;
579aa4dd815SMatthias Ringwald 
580aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
581a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
582a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
583485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
584aa4dd815SMatthias Ringwald                 return 1;
585aa4dd815SMatthias Ringwald             }
586a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
587a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
58803645b44SMatthias Ringwald             log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
589485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
5907522e673SMatthias Ringwald             // now, pick link settings
5917522e673SMatthias Ringwald             hfp_init_link_settings(hfp_connection);
592aa4dd815SMatthias Ringwald             return 1;
593aa4dd815SMatthias Ringwald         default:
594aa4dd815SMatthias Ringwald             break;
595aa4dd815SMatthias Ringwald     }
596aa4dd815SMatthias Ringwald     return 0;
597aa4dd815SMatthias Ringwald }
598aa4dd815SMatthias Ringwald 
599a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
600a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
601ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
602aa4dd815SMatthias Ringwald 
603a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
604d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
605a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
606aa4dd815SMatthias Ringwald     }
607ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
608d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS &&
609d0c20769SMatthias Ringwald         hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
610a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
611ce263fc8SMatthias Ringwald     }
612aa4dd815SMatthias Ringwald }
6133deb3ec6SMatthias Ringwald 
614a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
615c79b4cabSMatthias Ringwald     // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
616a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
617c04cf7ffSMilanka Ringwald     int sent = 0;
618a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
6193deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
620a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
6213deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
622aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
623d715cf51SMatthias Ringwald                     hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
624a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
625a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
626aa4dd815SMatthias Ringwald                     } else {
627a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
628aa4dd815SMatthias Ringwald                     }
629a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
630aa4dd815SMatthias Ringwald                     return 1;
6313deb3ec6SMatthias Ringwald                 default:
6323deb3ec6SMatthias Ringwald                     break;
6333deb3ec6SMatthias Ringwald             }
6343deb3ec6SMatthias Ringwald             break;
6353deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
636c04cf7ffSMilanka Ringwald             sent = codecs_exchange_state_machine(hfp_connection);
637a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
638a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6393deb3ec6SMatthias Ringwald             }
640c04cf7ffSMilanka Ringwald             return sent;
641aa4dd815SMatthias Ringwald 
642aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
643a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
644473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
645473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
646473ac565SMatthias Ringwald                 // continue below in state switch
647ad902e3dSMatthias Ringwald             }
648ad902e3dSMatthias Ringwald             break;
649aa4dd815SMatthias Ringwald 
650aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
651a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
652a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
653485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
654aa4dd815SMatthias Ringwald             return 1;
655aa4dd815SMatthias Ringwald 
6563deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
657a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
658a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
659a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
660a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
661a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
662aa4dd815SMatthias Ringwald             } else {
663a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
6643deb3ec6SMatthias Ringwald             }
6658a46ec40SMatthias Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
666aa4dd815SMatthias Ringwald             return 1;
6673deb3ec6SMatthias Ringwald 
668aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
669a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
670a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
671a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
672aa4dd815SMatthias Ringwald             }
673485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
674a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
675a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
676ce263fc8SMatthias Ringwald             }
677aa4dd815SMatthias Ringwald             return 1;
678aa4dd815SMatthias Ringwald 
679aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
680a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
681a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
682485ac19eSMilanka Ringwald             hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
683aa4dd815SMatthias Ringwald             return 1;
684aa4dd815SMatthias Ringwald 
685aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
686a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
687a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
688485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
689aa4dd815SMatthias Ringwald             return 1;
690aa4dd815SMatthias Ringwald 
691aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
692a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
693a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
694485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
695aa4dd815SMatthias Ringwald             return 1;
6963deb3ec6SMatthias Ringwald         default:
6973deb3ec6SMatthias Ringwald             break;
6983deb3ec6SMatthias Ringwald     }
699473ac565SMatthias Ringwald 
700473ac565SMatthias Ringwald     switch (hfp_connection->state){
701473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
702485ac19eSMilanka Ringwald             int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
703473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
704473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
705473ac565SMatthias Ringwald             if (next_segment < num_segments){
706473ac565SMatthias Ringwald                 // prepare sending of next segment
707473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
708473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
709473ac565SMatthias Ringwald             } else {
710473ac565SMatthias Ringwald                 // done, go to next state
711473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
712473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
713473ac565SMatthias Ringwald             }
714473ac565SMatthias Ringwald             return 1;
715473ac565SMatthias Ringwald         }
716473ac565SMatthias Ringwald         default:
717473ac565SMatthias Ringwald             break;
718473ac565SMatthias Ringwald     }
719c04cf7ffSMilanka Ringwald     return 0;
7203deb3ec6SMatthias Ringwald }
7213deb3ec6SMatthias Ringwald 
722a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
723c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
724c04cf7ffSMilanka Ringwald     if (sent) return 1;
725aa4dd815SMatthias Ringwald 
726a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
727aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
728a0ffb263SMatthias Ringwald             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
729485ac19eSMilanka Ringwald             hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
730aa4dd815SMatthias Ringwald             return 1;
731aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
732aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
733a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
734485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
735a0ffb263SMatthias Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
736aa4dd815SMatthias Ringwald             } else {
737485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
738aa4dd815SMatthias Ringwald             }
739aa4dd815SMatthias Ringwald             return 1;
740aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
741485ac19eSMilanka Ringwald             hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
742aa4dd815SMatthias Ringwald             return 1;
743aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
744485ac19eSMilanka Ringwald             hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
745aa4dd815SMatthias Ringwald             return 1;
746aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
747a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
748485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
749aa4dd815SMatthias Ringwald             } else {
750485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
7513deb3ec6SMatthias Ringwald             }
752aa4dd815SMatthias Ringwald             return 1;
753aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
754485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
755aa4dd815SMatthias Ringwald             return 1;
7563deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
757a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
758a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
759485ac19eSMilanka Ringwald                 hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
760aa4dd815SMatthias Ringwald                 return 1;
7613deb3ec6SMatthias Ringwald             }
762202c8a4cSMatthias Ringwald             break;
7633deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
764485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
765ce263fc8SMatthias Ringwald             return 1;
7663deb3ec6SMatthias Ringwald         default:
7673deb3ec6SMatthias Ringwald             break;
7683deb3ec6SMatthias Ringwald     }
769aa4dd815SMatthias Ringwald     return 0;
7703deb3ec6SMatthias Ringwald }
7713deb3ec6SMatthias Ringwald 
772a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
773a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
774a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
7753deb3ec6SMatthias Ringwald 
776aa4dd815SMatthias Ringwald 
777a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
778a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
779a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
780a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
781aa4dd815SMatthias Ringwald         return 1;
782aa4dd815SMatthias Ringwald     }
783aa4dd815SMatthias Ringwald 
784a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
785aa4dd815SMatthias Ringwald 
786aa4dd815SMatthias Ringwald     // run codecs exchange
787c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
788c04cf7ffSMilanka Ringwald     if (sent) return 1;
789aa4dd815SMatthias Ringwald 
790c04cf7ffSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
791a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
792a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
793a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
794eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
795aa4dd815SMatthias Ringwald         return 1;
796aa4dd815SMatthias Ringwald     }
797aa4dd815SMatthias Ringwald     return 0;
798aa4dd815SMatthias Ringwald }
799aa4dd815SMatthias Ringwald 
800ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
801665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
802665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
803aa4dd815SMatthias Ringwald 
804665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
805a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
806a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
807a0ffb263SMatthias Ringwald             return hfp_connection;
808aa4dd815SMatthias Ringwald         }
809aa4dd815SMatthias Ringwald     }
810aa4dd815SMatthias Ringwald     return NULL;
811aa4dd815SMatthias Ringwald }
812aa4dd815SMatthias Ringwald 
813ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
814d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
815d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
816aa4dd815SMatthias Ringwald 
817d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
818a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
819a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
820aa4dd815SMatthias Ringwald 
821a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
822a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
823aa4dd815SMatthias Ringwald 
824a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
825aa4dd815SMatthias Ringwald }
826aa4dd815SMatthias Ringwald 
827a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
828a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
829a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
830a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
831a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
832aa4dd815SMatthias Ringwald }
833aa4dd815SMatthias Ringwald 
834a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
835a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
836a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
837aa4dd815SMatthias Ringwald }
838aa4dd815SMatthias Ringwald 
839aa4dd815SMatthias Ringwald //
840aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
841aa4dd815SMatthias Ringwald //
842aa4dd815SMatthias Ringwald 
843a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
844aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
845a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
846d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
847aa4dd815SMatthias Ringwald     } else {
848a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
849a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
850a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
851a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
852ca59be51SMatthias Ringwald         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
853aa4dd815SMatthias Ringwald     }
854aa4dd815SMatthias Ringwald }
855aa4dd815SMatthias Ringwald 
856a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
857a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
858a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
859a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
860ca59be51SMatthias Ringwald     hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGINIG);
861aa4dd815SMatthias Ringwald }
862aa4dd815SMatthias Ringwald 
863aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
864aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
865aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
866aa4dd815SMatthias Ringwald 
867665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
868665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
869665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
870a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
871a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
872a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
873a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
874d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
875aa4dd815SMatthias Ringwald         }
876a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
877a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
878aa4dd815SMatthias Ringwald         }
879a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
880aa4dd815SMatthias Ringwald     }
881aa4dd815SMatthias Ringwald }
882aa4dd815SMatthias Ringwald 
883aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
884aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
885aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
886aa4dd815SMatthias Ringwald 
887665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
888665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
889665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
890a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
891a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
892a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
893a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
894aa4dd815SMatthias Ringwald     }
895aa4dd815SMatthias Ringwald }
896aa4dd815SMatthias Ringwald 
897aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
898aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("call");
899aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
900aa4dd815SMatthias Ringwald 
901665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
902665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
903665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
904a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
905a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
906a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
907a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
908aa4dd815SMatthias Ringwald     }
909aa4dd815SMatthias Ringwald }
910aa4dd815SMatthias Ringwald 
911aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
912aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callheld");
913aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
914aa4dd815SMatthias Ringwald 
915665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
916665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
917665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
918a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
919a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
920a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
921a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
922aa4dd815SMatthias Ringwald     }
923aa4dd815SMatthias Ringwald }
924aa4dd815SMatthias Ringwald 
925aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
926aa4dd815SMatthias Ringwald 
927aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
928aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
929aa4dd815SMatthias Ringwald 
930665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
931665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
932665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
933a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
934a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
935a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
936aa4dd815SMatthias Ringwald 
937a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
938a0ffb263SMatthias Ringwald         if (hfp_connection == source){
939a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
940aa4dd815SMatthias Ringwald 
941aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
942a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
943aa4dd815SMatthias Ringwald             } else {
944a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
945d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
946aa4dd815SMatthias Ringwald             }
947aa4dd815SMatthias Ringwald 
948a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
949a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
950aa4dd815SMatthias Ringwald 
951aa4dd815SMatthias Ringwald         } else {
952a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
953aa4dd815SMatthias Ringwald         }
954a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
955aa4dd815SMatthias Ringwald     }
956aa4dd815SMatthias Ringwald }
957aa4dd815SMatthias Ringwald 
958aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
959aa4dd815SMatthias Ringwald 
960aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
961aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
962aa4dd815SMatthias Ringwald 
963665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
964665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
965665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
966a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
967a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
968aa4dd815SMatthias Ringwald 
969a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
970a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
971aa4dd815SMatthias Ringwald 
972a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
973a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
974aa4dd815SMatthias Ringwald 
975a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
976aa4dd815SMatthias Ringwald         break;  // only single
977aa4dd815SMatthias Ringwald     }
978aa4dd815SMatthias Ringwald }
979aa4dd815SMatthias Ringwald 
980aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
981aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
982665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
983665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
984665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
985665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
986aa4dd815SMatthias Ringwald         if (connection->call_state != HFP_CALL_RINGING &&
987aa4dd815SMatthias Ringwald             connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
988aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
989aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
990aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
991aa4dd815SMatthias Ringwald         hfp_run_for_context(connection);
992aa4dd815SMatthias Ringwald     }
993aa4dd815SMatthias Ringwald }
994aa4dd815SMatthias Ringwald 
995ca59be51SMatthias Ringwald static void hfp_ag_emit_simple_event(uint8_t event_subtype){
996ca59be51SMatthias Ringwald     uint8_t event[3];
997ca59be51SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
998ca59be51SMatthias Ringwald     event[1] = sizeof(event) - 2;
999ca59be51SMatthias Ringwald     event[2] = event_subtype;
1000ca59be51SMatthias Ringwald     if (!hfp_ag_callback) return;
1001ca59be51SMatthias Ringwald     (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1002ca59be51SMatthias Ringwald }
1003ca59be51SMatthias Ringwald 
1004aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1005aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1006aa4dd815SMatthias Ringwald 
1007665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1008665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1009665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1010a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1011d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1012a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1013a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1014a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1015a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 1;
1016a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1017aa4dd815SMatthias Ringwald     }
1018ca59be51SMatthias Ringwald     hfp_ag_emit_simple_event(HFP_SUBEVENT_CALL_TERMINATED);
1019aa4dd815SMatthias Ringwald }
1020aa4dd815SMatthias Ringwald 
10210cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
1022aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1023aa4dd815SMatthias Ringwald     if (!indicator){
1024d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
102545718b6fSMatthias Ringwald         return;
1026aa4dd815SMatthias Ringwald     };
1027d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1028aa4dd815SMatthias Ringwald }
1029aa4dd815SMatthias Ringwald 
10300cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1031d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1032d210d9c4SMatthias Ringwald     if (!indicator){
1033d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
103445718b6fSMatthias Ringwald         return;
1035d210d9c4SMatthias Ringwald     };
1036d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1037d210d9c4SMatthias Ringwald }
1038d210d9c4SMatthias Ringwald 
10390cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1040aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1041aa4dd815SMatthias Ringwald     if (!indicator){
1042aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
104345718b6fSMatthias Ringwald         return;
1044aa4dd815SMatthias Ringwald     };
1045d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1046aa4dd815SMatthias Ringwald }
1047aa4dd815SMatthias Ringwald 
1048aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1049665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1050665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1051665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1052a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1053a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
1054a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1055a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1056aa4dd815SMatthias Ringwald     }
1057aa4dd815SMatthias Ringwald }
1058aa4dd815SMatthias Ringwald 
1059aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1060665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1061665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1062665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1063a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1064a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1065aa4dd815SMatthias Ringwald     }
1066aa4dd815SMatthias Ringwald     return NULL;
1067aa4dd815SMatthias Ringwald }
1068aa4dd815SMatthias Ringwald 
1069a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1070665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1071665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1072665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1073a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1074a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1075ce263fc8SMatthias Ringwald     }
1076ce263fc8SMatthias Ringwald }
1077ce263fc8SMatthias Ringwald 
1078a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1079aa4dd815SMatthias Ringwald     int indicator_index;
1080a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1081aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1082a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1083a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1084a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1085a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1086a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1087a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1088a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1089ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
1090aa4dd815SMatthias Ringwald             break;
1091aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1092a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1093a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1094a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1095aa4dd815SMatthias Ringwald             break;
1096aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1097a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1098a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1099aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1100a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1101aa4dd815SMatthias Ringwald             break;
1102aa4dd815SMatthias Ringwald         default:
1103aa4dd815SMatthias Ringwald             break;
1104aa4dd815SMatthias Ringwald     }
1105aa4dd815SMatthias Ringwald     return 0;
1106aa4dd815SMatthias Ringwald }
1107a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1108a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1109aa4dd815SMatthias Ringwald     int indicator_index;
1110d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1111d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1112d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
111374386ee0SMatthias Ringwald 
1114d210d9c4SMatthias Ringwald     //printf("hfp_ag_call_sm event %d \n", event);
1115aa4dd815SMatthias Ringwald     switch (event){
1116aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1117d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1118aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1119d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1120aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1121d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1122d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1123aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
112460ebb071SMilanka Ringwald                             log_info("AG rings");
1125aa4dd815SMatthias Ringwald                             break;
1126aa4dd815SMatthias Ringwald                         default:
11273deb3ec6SMatthias Ringwald                             break;
11283deb3ec6SMatthias Ringwald                     }
11293deb3ec6SMatthias Ringwald                     break;
1130aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1131d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1132aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1133d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1134d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1135aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
113660ebb071SMilanka Ringwald                             log_info("AG call waiting");
1137aa4dd815SMatthias Ringwald                             break;
1138aa4dd815SMatthias Ringwald                         default:
11393deb3ec6SMatthias Ringwald                             break;
11403deb3ec6SMatthias Ringwald                     }
11413deb3ec6SMatthias Ringwald                     break;
1142aa4dd815SMatthias Ringwald             }
1143aa4dd815SMatthias Ringwald             break;
1144aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1145d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1146aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1147d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1148aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1149d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1150d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1151d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1152aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
115360ebb071SMilanka Ringwald                             log_info("AG answers call, accept call by GSM");
1154aa4dd815SMatthias Ringwald                             break;
1155aa4dd815SMatthias Ringwald                         default:
11563deb3ec6SMatthias Ringwald                             break;
11573deb3ec6SMatthias Ringwald                     }
1158aa4dd815SMatthias Ringwald                     break;
1159aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1160d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1161aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
116260ebb071SMilanka Ringwald                             log_info("AG: current call is placed on hold, incoming call gets active");
1163d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1164d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1165d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1166ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1167ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1168aa4dd815SMatthias Ringwald                             break;
1169aa4dd815SMatthias Ringwald                         default:
1170aa4dd815SMatthias Ringwald                             break;
1171aa4dd815SMatthias Ringwald                     }
1172aa4dd815SMatthias Ringwald                     break;
1173aa4dd815SMatthias Ringwald             }
1174aa4dd815SMatthias Ringwald             break;
1175ce263fc8SMatthias Ringwald 
1176ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1177d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1178ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1179d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1180ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
118160ebb071SMilanka Ringwald                             log_info("AG: joining held call with active call");
1182d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG);
1183d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1184ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1185ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1186ce263fc8SMatthias Ringwald                             break;
1187ce263fc8SMatthias Ringwald                         default:
1188ce263fc8SMatthias Ringwald                             break;
1189ce263fc8SMatthias Ringwald                     }
1190ce263fc8SMatthias Ringwald                     break;
1191ce263fc8SMatthias Ringwald                 default:
1192ce263fc8SMatthias Ringwald                     break;
1193ce263fc8SMatthias Ringwald             }
1194ce263fc8SMatthias Ringwald             break;
1195ce263fc8SMatthias Ringwald 
1196aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1197d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1198aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1199d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1200aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1201d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF);
1202d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1203d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1204a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
120560ebb071SMilanka Ringwald                             log_info("HF answers call, accept call by GSM");
1206ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1207aa4dd815SMatthias Ringwald                             break;
1208aa4dd815SMatthias Ringwald                         default:
1209aa4dd815SMatthias Ringwald                             break;
1210aa4dd815SMatthias Ringwald                     }
12113deb3ec6SMatthias Ringwald                     break;
12123deb3ec6SMatthias Ringwald                 default:
12133deb3ec6SMatthias Ringwald                     break;
12143deb3ec6SMatthias Ringwald             }
12153deb3ec6SMatthias Ringwald             break;
12163deb3ec6SMatthias Ringwald 
1217ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1218d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1219ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1220d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1221ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1222d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG);
1223ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1224ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1225a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1226ce263fc8SMatthias Ringwald                             // as with regualr call
1227d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1228d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1229ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
123060ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by AG");
1231ce263fc8SMatthias Ringwald                             break;
1232ce263fc8SMatthias Ringwald                         default:
1233ce263fc8SMatthias Ringwald                             break;
1234ce263fc8SMatthias Ringwald                     }
1235ce263fc8SMatthias Ringwald                     break;
1236ce263fc8SMatthias Ringwald                 default:
1237ce263fc8SMatthias Ringwald                     break;
1238ce263fc8SMatthias Ringwald             }
1239ce263fc8SMatthias Ringwald             break;
1240ce263fc8SMatthias Ringwald 
1241ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1242d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1243ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1244d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1245ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1246d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF);
1247ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1248ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1249a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1250ce263fc8SMatthias Ringwald                             // as with regualr call
1251d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1252d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1253a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
125460ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by HF");
1255ce263fc8SMatthias Ringwald                             break;
1256ce263fc8SMatthias Ringwald                         default:
1257ce263fc8SMatthias Ringwald                             break;
1258ce263fc8SMatthias Ringwald                     }
1259ce263fc8SMatthias Ringwald                     break;
1260ce263fc8SMatthias Ringwald                 default:
1261ce263fc8SMatthias Ringwald                     break;
1262ce263fc8SMatthias Ringwald             }
1263ce263fc8SMatthias Ringwald             break;
1264ce263fc8SMatthias Ringwald 
1265ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1266ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1267ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1268ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1269d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG);
1270a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1271ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1272a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
127360ebb071SMilanka Ringwald             log_info("Held Call accepted and active");
1274ce263fc8SMatthias Ringwald             break;
1275ce263fc8SMatthias Ringwald 
1276ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1277ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1278ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1279ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1280d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG);
1281a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1282ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1283a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1284ce263fc8SMatthias Ringwald             // from terminate by ag
1285d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1286ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1287ce263fc8SMatthias Ringwald             break;
1288ce263fc8SMatthias Ringwald 
1289aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1290d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1291aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1292d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1293aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1294d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1295d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1296aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1297aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
129860ebb071SMilanka Ringwald                             log_info("HF Rejected Incoming call, AG terminate call");
1299aa4dd815SMatthias Ringwald                             break;
1300aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1301aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1302d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1303d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1304aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
130560ebb071SMilanka Ringwald                             log_info("AG terminate outgoing call process");
1306202c8a4cSMatthias Ringwald                             break;
1307aa4dd815SMatthias Ringwald                         default:
1308aa4dd815SMatthias Ringwald                             break;
1309aa4dd815SMatthias Ringwald                     }
1310aa4dd815SMatthias Ringwald                     break;
1311aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1312d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1313d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1314aa4dd815SMatthias Ringwald                     hfp_ag_transfer_call_state();
1315a0ffb263SMatthias Ringwald                     hfp_connection->call_state = HFP_CALL_IDLE;
131660ebb071SMilanka Ringwald                     log_info("AG terminate call");
1317aa4dd815SMatthias Ringwald                     break;
1318aa4dd815SMatthias Ringwald             }
1319aa4dd815SMatthias Ringwald             break;
13203deb3ec6SMatthias Ringwald 
1321aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1322d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1323aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1324d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1325aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1326d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1327d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1328aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
132960ebb071SMilanka Ringwald                             log_info("AG Rejected Incoming call, AG terminate call");
1330aa4dd815SMatthias Ringwald                             break;
1331aa4dd815SMatthias Ringwald                         default:
1332aa4dd815SMatthias Ringwald                             break;
13333deb3ec6SMatthias Ringwald                     }
1334202c8a4cSMatthias Ringwald                     break;
1335aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1336d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1337d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1338d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1339aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
134060ebb071SMilanka Ringwald                     log_info("AG terminate call");
1341aa4dd815SMatthias Ringwald                     break;
1342aa4dd815SMatthias Ringwald                 default:
13433deb3ec6SMatthias Ringwald                     break;
13443deb3ec6SMatthias Ringwald             }
13453deb3ec6SMatthias Ringwald             break;
1346aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1347d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1348aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1349d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1350aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1351aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
135260ebb071SMilanka Ringwald                             log_info("Incoming call interrupted");
1353aa4dd815SMatthias Ringwald                             break;
1354aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1355aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
135660ebb071SMilanka Ringwald                             log_info("Outgoing call interrupted\n");
135760ebb071SMilanka Ringwald                             log_info("AG notify call dropped\n");
1358aa4dd815SMatthias Ringwald                             break;
1359aa4dd815SMatthias Ringwald                         default:
13603deb3ec6SMatthias Ringwald                             break;
13613deb3ec6SMatthias Ringwald                     }
1362d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1363d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1364aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
1365aa4dd815SMatthias Ringwald                     break;
1366aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1367ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1368d210d9c4SMatthias Ringwald                         hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1369ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1370a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1371ce263fc8SMatthias Ringwald                     }
1372d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1373d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1374d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1375aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
137660ebb071SMilanka Ringwald                     log_info("AG notify call dropped");
1377aa4dd815SMatthias Ringwald                     break;
1378aa4dd815SMatthias Ringwald                 default:
1379aa4dd815SMatthias Ringwald                     break;
1380aa4dd815SMatthias Ringwald             }
1381aa4dd815SMatthias Ringwald             break;
1382aa4dd815SMatthias Ringwald 
1383aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1384d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1385d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1386a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1387a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1388d210d9c4SMatthias Ringwald                 break;
1389d210d9c4SMatthias Ringwald             }
1390a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]);
13919cae807eSMatthias Ringwald 
1392a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
139374386ee0SMatthias Ringwald 
1394ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1395aa4dd815SMatthias Ringwald             break;
1396aa4dd815SMatthias Ringwald 
13979cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1398d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1399d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1400a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1401a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1402d210d9c4SMatthias Ringwald                 break;
1403d210d9c4SMatthias Ringwald             }
1404d210d9c4SMatthias Ringwald 
140574386ee0SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED);
1406a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
140774386ee0SMatthias Ringwald 
140860ebb071SMilanka Ringwald             log_info("Redial last number");
14099cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1410aa4dd815SMatthias Ringwald 
14119cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
141260ebb071SMilanka Ringwald                 log_info("Last number exists: accept call");
1413ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
14149cae807eSMatthias Ringwald             } else {
141560ebb071SMilanka Ringwald                 log_info("log_infoLast number missing: reject call");
14169cae807eSMatthias Ringwald                 hfp_ag_outgoing_call_rejected();
14179cae807eSMatthias Ringwald             }
14189cae807eSMatthias Ringwald             break;
14199cae807eSMatthias Ringwald         }
1420aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
1421a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1422a0ffb263SMatthias Ringwald             if (!hfp_connection){
1423a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1424aa4dd815SMatthias Ringwald                 break;
1425aa4dd815SMatthias Ringwald             }
1426d210d9c4SMatthias Ringwald 
1427d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED);
1428a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1429a0ffb263SMatthias Ringwald             hfp_connection->send_error = 1;
1430a0ffb263SMatthias Ringwald             hfp_run_for_context(hfp_connection);
1431aa4dd815SMatthias Ringwald             break;
1432aa4dd815SMatthias Ringwald 
1433d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1434a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1435a0ffb263SMatthias Ringwald             if (!hfp_connection){
1436a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1437aa4dd815SMatthias Ringwald                 break;
1438aa4dd815SMatthias Ringwald             }
1439aa4dd815SMatthias Ringwald 
1440a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1441a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1442aa4dd815SMatthias Ringwald 
1443aa4dd815SMatthias Ringwald             // trigger callsetup to be
1444d0c20769SMatthias Ringwald             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1445d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED);
1446d210d9c4SMatthias Ringwald 
1447d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1448aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1449a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1450aa4dd815SMatthias Ringwald 
1451aa4dd815SMatthias Ringwald             // put current call on hold if active
1452d210d9c4SMatthias Ringwald             if (put_call_on_hold){
145360ebb071SMilanka Ringwald                 log_info("AG putting current call on hold for new outgoing calllog_info");
1454d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1455aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1456485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1457aa4dd815SMatthias Ringwald             }
1458aa4dd815SMatthias Ringwald 
1459aa4dd815SMatthias Ringwald             // start audio if needed
1460d97d752dSMilanka Ringwald             hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1461aa4dd815SMatthias Ringwald             break;
1462d210d9c4SMatthias Ringwald         }
1463aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1464a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1465a0ffb263SMatthias Ringwald             if (!hfp_connection){
1466a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1467aa4dd815SMatthias Ringwald                 break;
1468aa4dd815SMatthias Ringwald             }
1469d210d9c4SMatthias Ringwald 
1470d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING);
1471a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1472d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1473aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1474aa4dd815SMatthias Ringwald             break;
1475aa4dd815SMatthias Ringwald 
1476d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1477aa4dd815SMatthias Ringwald             // get outgoing call
1478a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1479a0ffb263SMatthias Ringwald             if (!hfp_connection){
1480a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1481aa4dd815SMatthias Ringwald             }
1482a0ffb263SMatthias Ringwald             if (!hfp_connection){
1483a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1484aa4dd815SMatthias Ringwald                 break;
1485aa4dd815SMatthias Ringwald             }
1486d210d9c4SMatthias Ringwald 
1487d0c20769SMatthias 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;
1488d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED);
1489a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1490d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1491d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1492aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1493aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1494d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1495d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1496aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
14973deb3ec6SMatthias Ringwald             }
14983deb3ec6SMatthias Ringwald             break;
1499d210d9c4SMatthias Ringwald         }
1500d210d9c4SMatthias Ringwald 
1501d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1502d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY);
1503d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1504a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1505a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
150660ebb071SMilanka Ringwald             log_info("AG: Call Waiting, User Busy");
1507d210d9c4SMatthias Ringwald             break;
1508d210d9c4SMatthias Ringwald 
1509d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1510d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1511d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1512d210d9c4SMatthias Ringwald 
1513d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1514d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1515a0ffb263SMatthias Ringwald                 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1516d0c20769SMatthias Ringwald 
1517d0c20769SMatthias Ringwald             }
1518d0c20769SMatthias Ringwald 
1519d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
152060ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Accept new call");
1521d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1522a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1523d210d9c4SMatthias Ringwald             } else {
152460ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Resume held call");
1525d210d9c4SMatthias Ringwald             }
1526d210d9c4SMatthias Ringwald             if (call_held){
1527d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1528a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1529d210d9c4SMatthias Ringwald             }
1530d0c20769SMatthias Ringwald 
1531a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1532d210d9c4SMatthias Ringwald             break;
1533d210d9c4SMatthias Ringwald         }
1534d0c20769SMatthias Ringwald 
1535d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1536d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1537d210d9c4SMatthias Ringwald             // only update if callsetup changed
1538d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1539a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1540d0c20769SMatthias Ringwald 
1541d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
154260ebb071SMilanka Ringwald                 log_info("AG: Call on Hold, Accept new call");
1543d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1544a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1545d210d9c4SMatthias Ringwald             } else {
154660ebb071SMilanka Ringwald                 log_info("AG: Swap calls");
1547d210d9c4SMatthias Ringwald             }
1548d0c20769SMatthias Ringwald 
1549d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1550d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1551a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1552a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1553d210d9c4SMatthias Ringwald             break;
1554d210d9c4SMatthias Ringwald         }
1555d0c20769SMatthias Ringwald 
1556d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1557d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1558d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
155960ebb071SMilanka Ringwald                 log_info("AG: Join 3-way-call");
1560d210d9c4SMatthias Ringwald                 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL);
1561d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1562a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1563ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1564d210d9c4SMatthias Ringwald             }
1565a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1566d210d9c4SMatthias Ringwald             break;
1567d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1568d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1569d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS);
157060ebb071SMilanka Ringwald             log_info("AG: Transfer call -> Connect two calls and disconnect");
1571d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1572d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1573a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1574a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1575a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1576d210d9c4SMatthias Ringwald             break;
1577ce263fc8SMatthias Ringwald 
15783deb3ec6SMatthias Ringwald         default:
15793deb3ec6SMatthias Ringwald             break;
15803deb3ec6SMatthias Ringwald     }
1581d210d9c4SMatthias Ringwald 
1582d0c20769SMatthias Ringwald 
15833deb3ec6SMatthias Ringwald }
15843deb3ec6SMatthias Ringwald 
15859cae807eSMatthias Ringwald 
1586a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
15879cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
15889cae807eSMatthias Ringwald     if (!active_call) return;
15899cae807eSMatthias Ringwald 
15909cae807eSMatthias Ringwald     int idx = active_call->index;
15919cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
15929cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
15939cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
15949cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
15959cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
15969cae807eSMatthias Ringwald     char * number = active_call->clip_number;
15979cae807eSMatthias Ringwald 
15989cae807eSMatthias Ringwald     char buffer[100];
15999cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
16009cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
16019cae807eSMatthias Ringwald     if (number){
16021cc1d9e9SMilanka Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
16039cae807eSMatthias Ringwald     }
16049cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
160560ebb071SMilanka 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,
16069cae807eSMatthias Ringwald        mode, mpty, type, number);
1607a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
16089cae807eSMatthias Ringwald }
16099cae807eSMatthias Ringwald 
1610a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection){
1611a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1612724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
1613473ac565SMatthias Ringwald 
1614e30a6a47SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1615473ac565SMatthias Ringwald         log_info("hfp_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1616e30a6a47SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1617e30a6a47SMatthias Ringwald         return;
1618e30a6a47SMatthias Ringwald     }
1619a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1620a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1621a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1622a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1623a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
16249cae807eSMatthias Ringwald         } else {
1625a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1626a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1627a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16289cae807eSMatthias Ringwald         }
1629ce263fc8SMatthias Ringwald         return;
1630ce263fc8SMatthias Ringwald     }
1631ce263fc8SMatthias Ringwald 
1632a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1633a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1634a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1635aa4dd815SMatthias Ringwald         return;
1636aa4dd815SMatthias Ringwald     }
1637aa4dd815SMatthias Ringwald 
1638a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1639a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1640a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1641a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1642485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1643a0ffb263SMatthias Ringwald         return;
1644a0ffb263SMatthias Ringwald     }
1645a0ffb263SMatthias Ringwald 
1646a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1647a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1648a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1649485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1650aa4dd815SMatthias Ringwald         return;
1651aa4dd815SMatthias Ringwald     }
1652aa4dd815SMatthias Ringwald 
1653ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1654a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1655a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1656a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1657485ac19eSMilanka Ringwald         hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1658ce263fc8SMatthias Ringwald         return;
1659ce263fc8SMatthias Ringwald     }
1660ce263fc8SMatthias Ringwald 
1661a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1662a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1663a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1664485ac19eSMilanka Ringwald         hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1665ce263fc8SMatthias Ringwald         return;
1666ce263fc8SMatthias Ringwald     }
1667ce263fc8SMatthias Ringwald 
1668aa4dd815SMatthias Ringwald     // update AG indicators
1669a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1670aa4dd815SMatthias Ringwald         int i;
1671a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1672a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1673a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1674a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1675ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1676ce263fc8SMatthias Ringwald                     break;
1677ce263fc8SMatthias Ringwald                 }
1678485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1679aa4dd815SMatthias Ringwald                 return;
1680aa4dd815SMatthias Ringwald             }
1681aa4dd815SMatthias Ringwald         }
1682aa4dd815SMatthias Ringwald     }
1683aa4dd815SMatthias Ringwald 
1684a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1685a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1686a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1687485ac19eSMilanka Ringwald         hfp_ag_send_ring(hfp_connection->rfcomm_cid);
1688aa4dd815SMatthias Ringwald         return;
1689aa4dd815SMatthias Ringwald     }
1690aa4dd815SMatthias Ringwald 
1691a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1692a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1693a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1694a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1695aa4dd815SMatthias Ringwald         return;
1696aa4dd815SMatthias Ringwald     }
1697aa4dd815SMatthias Ringwald 
1698a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1699a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1700a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1701a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1702a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1703aa4dd815SMatthias Ringwald         return;
1704aa4dd815SMatthias Ringwald     }
1705aa4dd815SMatthias Ringwald 
1706a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1707a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1708a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1709a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1710ce263fc8SMatthias Ringwald         } else {
1711a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1712a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1713485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1714ce263fc8SMatthias Ringwald         }
1715a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1716485ac19eSMilanka Ringwald 
1717ce263fc8SMatthias Ringwald     }
1718ce263fc8SMatthias Ringwald 
1719a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1720a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1721a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1722485ac19eSMilanka Ringwald         hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1723aa4dd815SMatthias Ringwald         return;
1724aa4dd815SMatthias Ringwald     }
1725aa4dd815SMatthias Ringwald 
1726a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1727a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1728a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1729485ac19eSMilanka Ringwald         hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
17303deb3ec6SMatthias Ringwald         return;
17313deb3ec6SMatthias Ringwald     }
17323deb3ec6SMatthias Ringwald 
1733a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1734a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1735485ac19eSMilanka Ringwald         hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1736ce263fc8SMatthias Ringwald         return;
1737ce263fc8SMatthias Ringwald     }
1738ce263fc8SMatthias Ringwald 
17392ee45eb2SMilanka Ringwald     // trigger codec exchange if requested by app
17402ee45eb2SMilanka Ringwald     if (hfp_connection->trigger_codec_exchange){
17412ee45eb2SMilanka Ringwald         log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state);
17422ee45eb2SMilanka Ringwald     }
17432ee45eb2SMilanka Ringwald     if (hfp_connection->trigger_codec_exchange && hfp_connection->command == HFP_CMD_NONE){
17442ee45eb2SMilanka Ringwald         switch (hfp_connection->codecs_state){
17452ee45eb2SMilanka Ringwald             case HFP_CODECS_IDLE:
17462ee45eb2SMilanka Ringwald             case HFP_CODECS_RECEIVED_LIST:
17472ee45eb2SMilanka Ringwald             case HFP_CODECS_AG_RESEND_COMMON_CODEC:
17482ee45eb2SMilanka Ringwald             case HFP_CODECS_ERROR:
17492ee45eb2SMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 0;
17502ee45eb2SMilanka Ringwald                 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
17512ee45eb2SMilanka Ringwald                 break;
17522ee45eb2SMilanka Ringwald             default:
17532ee45eb2SMilanka Ringwald                 break;
17542ee45eb2SMilanka Ringwald         }
17552ee45eb2SMilanka Ringwald     }
17562ee45eb2SMilanka Ringwald 
1757c04cf7ffSMilanka Ringwald     int cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1758c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1759c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
17603deb3ec6SMatthias Ringwald     }
17613deb3ec6SMatthias Ringwald 
1762c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1763c04cf7ffSMilanka Ringwald         cmd_sent = call_setup_state_machine(hfp_connection);
1764aa4dd815SMatthias Ringwald     }
1765aa4dd815SMatthias Ringwald 
1766c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1767c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
1768aa4dd815SMatthias Ringwald     }
17693deb3ec6SMatthias Ringwald 
1770c04cf7ffSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_NONE && !cmd_sent){
1771a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1772a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
17733deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1774a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1775a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
17763deb3ec6SMatthias Ringwald                 break;
17773deb3ec6SMatthias Ringwald             default:
17783deb3ec6SMatthias Ringwald                 break;
17793deb3ec6SMatthias Ringwald         }
17803deb3ec6SMatthias Ringwald     }
1781c04cf7ffSMilanka Ringwald 
1782c04cf7ffSMilanka Ringwald     if (cmd_sent){
1783a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1784473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1785473ac565SMatthias Ringwald     }
17863deb3ec6SMatthias Ringwald }
1787d68dcce1SMatthias Ringwald 
1788ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1789ce263fc8SMatthias Ringwald     int i;
1790a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1791a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1792ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1793ce263fc8SMatthias Ringwald             return indicator;
1794ce263fc8SMatthias Ringwald         }
1795ce263fc8SMatthias Ringwald     }
1796ce263fc8SMatthias Ringwald     return NULL;
1797ce263fc8SMatthias Ringwald }
17983deb3ec6SMatthias Ringwald 
1799*e9c22d4eSMatthias Ringwald static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
18008a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
18018a46ec40SMatthias Ringwald 
18028a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
18038a46ec40SMatthias Ringwald     if (size < 1) return;
18049ec2630cSMatthias Ringwald 
1805a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1806a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
18071e35c04dSMatthias Ringwald 
18088a46ec40SMatthias Ringwald     // temp overwrite last byte (most likely \n for log_info)
18091e35c04dSMatthias Ringwald     char last_char = packet[size-1];
18101e35c04dSMatthias Ringwald     packet[size-1] = 0;
18111e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
18121e35c04dSMatthias Ringwald     packet[size-1] = last_char;
18131e35c04dSMatthias Ringwald 
18148a46ec40SMatthias Ringwald     // process messages byte-wise
18153deb3ec6SMatthias Ringwald     int pos;
18163deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1817a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1818aa4dd815SMatthias Ringwald     }
1819ce263fc8SMatthias Ringwald     hfp_generic_status_indicator_t * indicator;
1820ce263fc8SMatthias Ringwald     int value;
1821a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
1822ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1823ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_active){
1824a0ffb263SMatthias Ringwald                 hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1825ce263fc8SMatthias Ringwald             }
1826a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1827ce263fc8SMatthias Ringwald             break;
1828ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
18292308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
183060ebb071SMilanka Ringwald             log_info("HF Response and Hold: %u", value);
1831ce263fc8SMatthias Ringwald             switch(value){
1832ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1833a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1834ce263fc8SMatthias Ringwald                     break;
1835ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1836a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1837ce263fc8SMatthias Ringwald                     break;
1838ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1839a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1840ce263fc8SMatthias Ringwald                     break;
1841ce263fc8SMatthias Ringwald                 default:
1842ce263fc8SMatthias Ringwald                     break;
1843ce263fc8SMatthias Ringwald             }
1844a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1845ce263fc8SMatthias Ringwald             break;
1846ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
1847a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1848ce263fc8SMatthias Ringwald             // find indicator by assigned number
1849a0ffb263SMatthias Ringwald             indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1850ce263fc8SMatthias Ringwald             if (!indicator){
1851a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1852ce263fc8SMatthias Ringwald                 break;
1853ce263fc8SMatthias Ringwald             }
18542308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1855ce263fc8SMatthias Ringwald             switch (indicator->uuid){
1856ce263fc8SMatthias Ringwald                 case 1: // enhanced security
1857ce263fc8SMatthias Ringwald                     if (value > 1) {
1858a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1859ce263fc8SMatthias Ringwald                         return;
1860ce263fc8SMatthias Ringwald                     }
186160ebb071SMilanka Ringwald                     log_info("HF Indicator 'enhanced security' set to %u", value);
1862ce263fc8SMatthias Ringwald                     break;
1863ce263fc8SMatthias Ringwald                 case 2: // battery level
1864ce263fc8SMatthias Ringwald                     if (value > 100){
1865a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1866ce263fc8SMatthias Ringwald                         return;
1867ce263fc8SMatthias Ringwald                     }
186860ebb071SMilanka Ringwald                     log_info("HF Indicator 'battery' set to %u", value);
1869ce263fc8SMatthias Ringwald                     break;
1870ce263fc8SMatthias Ringwald                 default:
187160ebb071SMilanka Ringwald                     log_info("HF Indicator unknown set to %u", value);
1872ce263fc8SMatthias Ringwald                     break;
1873ce263fc8SMatthias Ringwald             }
1874a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1875ce263fc8SMatthias Ringwald             break;
1876ce263fc8SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1877ce263fc8SMatthias Ringwald             // expected by SLC state machine
1878a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1879a0ffb263SMatthias Ringwald             hfp_connection->send_ag_indicators_segment = 0;
1880a0ffb263SMatthias Ringwald             hfp_connection->send_ag_status_indicators = 1;
1881ce263fc8SMatthias Ringwald             break;
1882ce263fc8SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1883a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1884a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1885a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 1;
1886ce263fc8SMatthias Ringwald             break;
1887ce263fc8SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1888ce263fc8SMatthias Ringwald             if (subscriber_numbers_count == 0){
1889485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1890ce263fc8SMatthias Ringwald                 break;
1891ce263fc8SMatthias Ringwald             }
1892a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1893a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 1;
1894ce263fc8SMatthias Ringwald             break;
1895c1797c7dSMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
1896a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1897ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]);
1898c1797c7dSMatthias Ringwald             break;
1899aa4dd815SMatthias Ringwald         case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1900a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1901ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1902aa4dd815SMatthias Ringwald             break;
1903aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1904a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1905aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1906a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1907a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
190860ebb071SMilanka Ringwald                 log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction);
1909aa4dd815SMatthias Ringwald             } else {
1910a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1911aa4dd815SMatthias Ringwald             }
1912aa4dd815SMatthias Ringwald             break;
1913aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_ANSWERED:
1914a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
191560ebb071SMilanka Ringwald             log_info("HFP: ATA");
1916a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1917aa4dd815SMatthias Ringwald             break;
1918aa4dd815SMatthias Ringwald         case HFP_CMD_HANG_UP_CALL:
1919a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1920a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1921a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1922aa4dd815SMatthias Ringwald             break;
1923aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_HOLD: {
1924aa4dd815SMatthias Ringwald             // TODO: fully implement this
1925a0ffb263SMatthias Ringwald             log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]);
1926a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1927a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1928a0ffb263SMatthias Ringwald             hfp_connection->call_index = 0;
1929d0c20769SMatthias Ringwald 
1930a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
19312308e108SMilanka Ringwald                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
1932d0c20769SMatthias Ringwald             }
1933d210d9c4SMatthias Ringwald 
1934a0ffb263SMatthias Ringwald             switch (hfp_connection->line_buffer[0]){
1935aa4dd815SMatthias Ringwald                 case '0':
1936d0c20769SMatthias Ringwald                     // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1937a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1938aa4dd815SMatthias Ringwald                     break;
1939aa4dd815SMatthias Ringwald                 case '1':
1940d0c20769SMatthias Ringwald                     // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1941d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1942d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1943a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1944aa4dd815SMatthias Ringwald                     break;
1945aa4dd815SMatthias Ringwald                 case '2':
1946d0c20769SMatthias Ringwald                     // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1947d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1948d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1949a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1950aa4dd815SMatthias Ringwald                     break;
1951aa4dd815SMatthias Ringwald                 case '3':
1952d0c20769SMatthias Ringwald                     // Adds a held call to the conversation.
1953a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1954aa4dd815SMatthias Ringwald                     break;
1955aa4dd815SMatthias Ringwald                 case '4':
1956d0c20769SMatthias Ringwald                     // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1957a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1958aa4dd815SMatthias Ringwald                     break;
1959aa4dd815SMatthias Ringwald                 default:
1960aa4dd815SMatthias Ringwald                     break;
1961aa4dd815SMatthias Ringwald             }
1962aa4dd815SMatthias Ringwald             break;
1963aa4dd815SMatthias Ringwald         }
1964aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_PHONE_NUMBER:
1965a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1966a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
1967aa4dd815SMatthias Ringwald             break;
1968aa4dd815SMatthias Ringwald         case HFP_CMD_REDIAL_LAST_NUMBER:
1969a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1970a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
1971aa4dd815SMatthias Ringwald             break;
1972aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
1973a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1974a0ffb263SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0';
1975a0ffb263SMatthias Ringwald             log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
1976a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1977aa4dd815SMatthias Ringwald             break;
1978aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1979a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1980a0ffb263SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0';
1981a0ffb263SMatthias Ringwald             log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
1982a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1983aa4dd815SMatthias Ringwald             break;
1984aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1985a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1986a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
198760ebb071SMilanka Ringwald             log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
1988aa4dd815SMatthias Ringwald             break;
1989aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1990a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1991a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
199260ebb071SMilanka Ringwald             log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
1993aa4dd815SMatthias Ringwald             break;
1994aa4dd815SMatthias Ringwald         default:
1995aa4dd815SMatthias Ringwald             break;
19963deb3ec6SMatthias Ringwald     }
19973deb3ec6SMatthias Ringwald }
19983deb3ec6SMatthias Ringwald 
1999aa4dd815SMatthias Ringwald static void hfp_run(void){
2000d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
2001d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2002d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2003d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2004a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
20053deb3ec6SMatthias Ringwald     }
20063deb3ec6SMatthias Ringwald }
20073deb3ec6SMatthias Ringwald 
2008*e9c22d4eSMatthias Ringwald static void hci_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2009*e9c22d4eSMatthias Ringwald     switch (packet_type){
2010*e9c22d4eSMatthias Ringwald         case HCI_EVENT_PACKET:
2011*e9c22d4eSMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2012*e9c22d4eSMatthias Ringwald             break;
2013*e9c22d4eSMatthias Ringwald         default:
2014*e9c22d4eSMatthias Ringwald             break;
2015*e9c22d4eSMatthias Ringwald     }
2016*e9c22d4eSMatthias Ringwald 
2017*e9c22d4eSMatthias Ringwald     hfp_run();
2018*e9c22d4eSMatthias Ringwald }
2019*e9c22d4eSMatthias Ringwald 
2020*e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
20213deb3ec6SMatthias Ringwald     switch (packet_type){
20223deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
2023*e9c22d4eSMatthias Ringwald             hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size);
20243deb3ec6SMatthias Ringwald             break;
20253deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2026e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2027e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2028e30a6a47SMatthias Ringwald                 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2029e30a6a47SMatthias Ringwald                 return;
2030e30a6a47SMatthias Ringwald             }
2031323d3000SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2032aa4dd815SMatthias Ringwald             break;
20333deb3ec6SMatthias Ringwald         default:
20343deb3ec6SMatthias Ringwald             break;
20353deb3ec6SMatthias Ringwald     }
20363deb3ec6SMatthias Ringwald 
20373deb3ec6SMatthias Ringwald     hfp_run();
20383deb3ec6SMatthias Ringwald }
20393deb3ec6SMatthias Ringwald 
2040a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
20413deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
20423deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
20433deb3ec6SMatthias Ringwald         return;
20443deb3ec6SMatthias Ringwald     }
20453deb3ec6SMatthias Ringwald     int i;
2046a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
20473deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
20483deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
20493deb3ec6SMatthias Ringwald     }
2050a0ffb263SMatthias Ringwald }
20513deb3ec6SMatthias Ringwald 
2052a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2053a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2054a0ffb263SMatthias Ringwald }
20553deb3ec6SMatthias Ringwald 
2056a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2057a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
2058a0ffb263SMatthias Ringwald     memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2059a0ffb263SMatthias Ringwald }
20603deb3ec6SMatthias Ringwald 
2061a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
2062a0ffb263SMatthias Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return;
2063a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
2064a0ffb263SMatthias Ringwald     memcpy(hfp_generic_status_indicators, hf_indicators, hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2065a0ffb263SMatthias Ringwald }
2066a0ffb263SMatthias Ringwald 
2067a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
20683deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
20693deb3ec6SMatthias Ringwald     memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *));
2070a0ffb263SMatthias Ringwald }
2071a0ffb263SMatthias Ringwald 
2072a0ffb263SMatthias Ringwald 
2073a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
2074d63c37a1SMatthias Ringwald     // register for HCI events
2075*e9c22d4eSMatthias Ringwald     hci_event_callback_registration.callback = &hci_packet_handler;
2076d63c37a1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
2077d63c37a1SMatthias Ringwald 
2078*e9c22d4eSMatthias Ringwald     rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
2079*e9c22d4eSMatthias Ringwald     hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler);
2080aa4dd815SMatthias Ringwald 
2081d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2082d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2083d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2084d210d9c4SMatthias Ringwald 
2085d210d9c4SMatthias Ringwald     hfp_gsm_init();
20863deb3ec6SMatthias Ringwald }
20873deb3ec6SMatthias Ringwald 
20883deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
2089323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
20903deb3ec6SMatthias Ringwald }
20913deb3ec6SMatthias Ringwald 
2092d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
2093d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2094a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2095a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2096a33eb0c4SMilanka Ringwald         return;
2097a33eb0c4SMilanka Ringwald     }
2098a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2099a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21003deb3ec6SMatthias Ringwald }
21013deb3ec6SMatthias Ringwald 
2102d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
2103d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2104a0ffb263SMatthias Ringwald     if (!hfp_connection){
2105a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
21063deb3ec6SMatthias Ringwald         return;
21073deb3ec6SMatthias Ringwald     }
2108a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2109a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
21103deb3ec6SMatthias Ringwald         return;
21113deb3ec6SMatthias Ringwald     }
2112a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2113a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21143deb3ec6SMatthias Ringwald }
21153deb3ec6SMatthias Ringwald 
2116a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
21172ee45eb2SMilanka Ringwald     log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state);
2118a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2119a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
21203deb3ec6SMatthias Ringwald 
2121a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2122a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2123d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2124d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2125a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
21267522e673SMatthias Ringwald         // now, pick link settings
21277522e673SMatthias Ringwald         hfp_init_link_settings(hfp_connection);
21282ee45eb2SMilanka Ringwald         return;
21293deb3ec6SMatthias Ringwald     }
2130aa4dd815SMatthias Ringwald 
21312ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 1;
2132aa4dd815SMatthias Ringwald }
2133aa4dd815SMatthias Ringwald 
2134d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
2135d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2136a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2137a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2138a33eb0c4SMilanka Ringwald         return;
2139a33eb0c4SMilanka Ringwald     }
21402ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 0;
2141a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2142a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2143a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21443deb3ec6SMatthias Ringwald }
21453deb3ec6SMatthias Ringwald 
2146d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
2147d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2148a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2149a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2150a33eb0c4SMilanka Ringwald         return;
2151a33eb0c4SMilanka Ringwald     }
2152a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2153a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21543deb3ec6SMatthias Ringwald }
2155aa4dd815SMatthias Ringwald 
2156aa4dd815SMatthias Ringwald /**
2157aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2158aa4dd815SMatthias Ringwald  */
2159aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2160aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2161aa4dd815SMatthias Ringwald         return;
2162aa4dd815SMatthias Ringwald     }
2163aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2164aa4dd815SMatthias Ringwald 
2165665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2166665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2167665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2168a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2169a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2170a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2171aa4dd815SMatthias Ringwald     }
2172aa4dd815SMatthias Ringwald }
2173aa4dd815SMatthias Ringwald 
2174aa4dd815SMatthias Ringwald /**
2175aa4dd815SMatthias Ringwald  * @brief Called from GSM
2176aa4dd815SMatthias Ringwald  */
2177aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2178aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2179aa4dd815SMatthias Ringwald }
2180aa4dd815SMatthias Ringwald 
2181aa4dd815SMatthias Ringwald /**
2182aa4dd815SMatthias Ringwald  * @brief number is stored.
2183aa4dd815SMatthias Ringwald  */
2184aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2185d0c20769SMatthias Ringwald     hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number);
2186aa4dd815SMatthias Ringwald }
2187aa4dd815SMatthias Ringwald 
2188aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2189aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2190aa4dd815SMatthias Ringwald }
2191aa4dd815SMatthias Ringwald 
2192aa4dd815SMatthias Ringwald // call from AG UI
2193aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2194aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2195aa4dd815SMatthias Ringwald }
2196aa4dd815SMatthias Ringwald 
2197ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2198ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2199ce263fc8SMatthias Ringwald }
2200ce263fc8SMatthias Ringwald 
2201aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2202aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2203aa4dd815SMatthias Ringwald }
2204aa4dd815SMatthias Ringwald 
2205aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2206aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2207aa4dd815SMatthias Ringwald }
2208aa4dd815SMatthias Ringwald 
2209aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2210aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2211aa4dd815SMatthias Ringwald }
2212aa4dd815SMatthias Ringwald 
2213aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2214aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2215aa4dd815SMatthias Ringwald }
2216aa4dd815SMatthias Ringwald 
2217aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2218aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2219aa4dd815SMatthias Ringwald }
2220ce263fc8SMatthias Ringwald 
2221ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2222ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2223ce263fc8SMatthias Ringwald }
2224ce263fc8SMatthias Ringwald 
2225ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2226ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2227ce263fc8SMatthias Ringwald }
2228ce263fc8SMatthias Ringwald 
2229ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2230ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2231ce263fc8SMatthias Ringwald }
2232ce263fc8SMatthias Ringwald 
2233aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2234aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2235aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2236aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2237aa4dd815SMatthias Ringwald 
2238ce263fc8SMatthias Ringwald 
2239665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2240665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2241665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2242a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2243a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2244ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2245ce263fc8SMatthias Ringwald             continue;
2246ce263fc8SMatthias Ringwald         }
2247ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2248a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2249a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2250aa4dd815SMatthias Ringwald     }
2251aa4dd815SMatthias Ringwald }
2252aa4dd815SMatthias Ringwald 
2253aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2254aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2255aa4dd815SMatthias Ringwald }
2256aa4dd815SMatthias Ringwald 
2257aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2258aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2259aa4dd815SMatthias Ringwald }
2260aa4dd815SMatthias Ringwald 
2261aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2262aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2263aa4dd815SMatthias Ringwald }
2264aa4dd815SMatthias Ringwald 
2265aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2266aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2267aa4dd815SMatthias Ringwald }
2268aa4dd815SMatthias Ringwald 
2269d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){
2270aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
2271d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2272a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2273a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2274a33eb0c4SMilanka Ringwald         return;
2275a33eb0c4SMilanka Ringwald     }
2276aa4dd815SMatthias Ringwald 
2277a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
227860ebb071SMilanka Ringwald         log_info("AG cannot acivate voice recognition - not supported by HF");
2279aa4dd815SMatthias Ringwald         return;
2280aa4dd815SMatthias Ringwald     }
2281aa4dd815SMatthias Ringwald 
2282aa4dd815SMatthias Ringwald     if (activate){
2283d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(acl_handle);
2284aa4dd815SMatthias Ringwald     }
2285aa4dd815SMatthias Ringwald 
2286a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2287a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2288a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2289aa4dd815SMatthias Ringwald }
2290aa4dd815SMatthias Ringwald 
2291d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
2292d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2293a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2294a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2295a33eb0c4SMilanka Ringwald         return;
2296a33eb0c4SMilanka Ringwald     }
2297a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2298a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2299a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2300a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2301aa4dd815SMatthias Ringwald     }
2302a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2303aa4dd815SMatthias Ringwald }
2304aa4dd815SMatthias Ringwald 
2305d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
2306d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2307a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2308a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2309a33eb0c4SMilanka Ringwald         return;
2310a33eb0c4SMilanka Ringwald     }
2311a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2312a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2313a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2314aa4dd815SMatthias Ringwald     }
2315a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2316aa4dd815SMatthias Ringwald }
2317aa4dd815SMatthias Ringwald 
2318d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
2319d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2320a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2321a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2322a33eb0c4SMilanka Ringwald         return;
2323a33eb0c4SMilanka Ringwald     }
2324aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2325a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2326aa4dd815SMatthias Ringwald }
2327aa4dd815SMatthias Ringwald 
2328d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
2329d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2330a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2331a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2332a33eb0c4SMilanka Ringwald         return;
2333a33eb0c4SMilanka Ringwald     }
2334a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2335aa4dd815SMatthias Ringwald }
2336aa4dd815SMatthias Ringwald 
2337d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
2338d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2339a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2340a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2341a33eb0c4SMilanka Ringwald         return;
2342a33eb0c4SMilanka Ringwald     }
2343a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2344c1797c7dSMatthias Ringwald }
2345aa4dd815SMatthias Ringwald 
2346ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2347ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2348ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2349ce263fc8SMatthias Ringwald }
2350ce263fc8SMatthias Ringwald 
23519cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
23529cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2353ce263fc8SMatthias Ringwald }
2354ce263fc8SMatthias Ringwald 
2355d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
2356d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2357a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2358a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2359a33eb0c4SMilanka Ringwald         return;
2360a33eb0c4SMilanka Ringwald     }
2361a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2362a0ffb263SMatthias Ringwald 
2363a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2364a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2365a0ffb263SMatthias Ringwald }
2366ce263fc8SMatthias Ringwald 
2367