xref: /btstack/src/classic/hfp_ag.c (revision 323d300063a7e0193cf61fbe114062cf000a3fe5)
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
72c5b64319SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
73adaba9f3SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection);
74adaba9f3SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
75adaba9f3SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
76c5b64319SMatthias Ringwald 
77c5b64319SMatthias Ringwald // public prototypes
7835833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void);
7935833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void);
80c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr);
81c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr);
82c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context);
83c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context);
84c5b64319SMatthias Ringwald 
85c5b64319SMatthias Ringwald // gobals
863deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway";
87aa4dd815SMatthias Ringwald 
883deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
89aa4dd815SMatthias Ringwald 
903deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
913deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
923deb3ec6SMatthias Ringwald 
933deb3ec6SMatthias Ringwald static int  hfp_ag_indicators_nr = 0;
943deb3ec6SMatthias Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_AG_INDICATORS];
953deb3ec6SMatthias Ringwald 
96a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0;
97a0ffb263SMatthias Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_HF_INDICATORS];
98a0ffb263SMatthias Ringwald 
993deb3ec6SMatthias Ringwald static int  hfp_ag_call_hold_services_nr = 0;
1003deb3ec6SMatthias Ringwald static char *hfp_ag_call_hold_services[6];
10113839019SMatthias Ringwald static btstack_packet_handler_t hfp_callback;
1023deb3ec6SMatthias Ringwald 
103ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
104ce263fc8SMatthias Ringwald static int hfp_ag_response_and_hold_active = 0;
105aa4dd815SMatthias Ringwald 
106ce263fc8SMatthias Ringwald // Subcriber information entries
107ce263fc8SMatthias Ringwald static hfp_phone_number_t * subscriber_numbers = NULL;
108ce263fc8SMatthias Ringwald static int subscriber_numbers_count = 0;
109ce263fc8SMatthias Ringwald 
110c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
11199a10067SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection);
1123deb3ec6SMatthias Ringwald 
1133deb3ec6SMatthias Ringwald 
114a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
115a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
116a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
117a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1183deb3ec6SMatthias Ringwald     }
119a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
120a0ffb263SMatthias Ringwald }
121a0ffb263SMatthias Ringwald 
122a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
123a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
124a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
125a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
126a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
127a0ffb263SMatthias Ringwald     }
128a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1293deb3ec6SMatthias Ringwald }
1303deb3ec6SMatthias Ringwald 
131aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
132aa4dd815SMatthias Ringwald     int i;
133aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
134aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
135aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
136aa4dd815SMatthias Ringwald         }
137aa4dd815SMatthias Ringwald     }
138aa4dd815SMatthias Ringwald     return NULL;
139aa4dd815SMatthias Ringwald }
140aa4dd815SMatthias Ringwald 
141aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
142aa4dd815SMatthias Ringwald     int i;
143aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
144aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
145aa4dd815SMatthias Ringwald             return i;
146aa4dd815SMatthias Ringwald         }
147aa4dd815SMatthias Ringwald     }
148aa4dd815SMatthias Ringwald     return -1;
149aa4dd815SMatthias Ringwald }
150aa4dd815SMatthias Ringwald 
1513deb3ec6SMatthias Ringwald 
15213839019SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
1533deb3ec6SMatthias Ringwald     if (callback == NULL){
1543deb3ec6SMatthias Ringwald         log_error("hfp_ag_register_packet_handler called with NULL callback");
1553deb3ec6SMatthias Ringwald         return;
1563deb3ec6SMatthias Ringwald     }
1573deb3ec6SMatthias Ringwald     hfp_callback = callback;
158f4000eebSMatthias Ringwald     hfp_set_callback(callback);
1593deb3ec6SMatthias Ringwald }
1603deb3ec6SMatthias Ringwald 
1610cb5b971SMatthias Ringwald static int use_in_band_tone(void){
162aa4dd815SMatthias Ringwald     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1633deb3ec6SMatthias Ringwald }
1643deb3ec6SMatthias Ringwald 
165a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
166a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1673deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1683deb3ec6SMatthias Ringwald     return hf && ag;
1693deb3ec6SMatthias Ringwald }
1703deb3ec6SMatthias Ringwald 
171a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
172a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1733deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1743deb3ec6SMatthias Ringwald     return hf && ag;
1753deb3ec6SMatthias Ringwald }
1763deb3ec6SMatthias Ringwald 
177a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
178a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
1793deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
1803deb3ec6SMatthias Ringwald     return hf && ag;
1813deb3ec6SMatthias Ringwald }
1823deb3ec6SMatthias Ringwald 
1834f84bf36SMatthias Ringwald void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint8_t ability_to_reject_call, uint16_t supported_features, int wide_band_speech){
1843deb3ec6SMatthias Ringwald     if (!name){
1853deb3ec6SMatthias Ringwald         name = default_hfp_ag_service_name;
1863deb3ec6SMatthias Ringwald     }
187235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
1883deb3ec6SMatthias Ringwald 
1893deb3ec6SMatthias Ringwald     /*
1903deb3ec6SMatthias Ringwald      * 0x01 – Ability to reject a call
1913deb3ec6SMatthias Ringwald      * 0x00 – No ability to reject a call
1923deb3ec6SMatthias Ringwald      */
193aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
194aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
195aa4dd815SMatthias Ringwald 
1964f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
1974f84bf36SMatthias Ringwald     //
1984f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1994f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
2004f84bf36SMatthias Ringwald     //
2014f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features &0x1f;
2024f84bf36SMatthias Ringwald     if (supported_features & wide_band_speech){
2034f84bf36SMatthias Ringwald         sdp_features |= 1 << 5; // Wide band speech bit
2044f84bf36SMatthias Ringwald     }
205aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
2064f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
207aa4dd815SMatthias Ringwald }
208aa4dd815SMatthias Ringwald 
209485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){
210aa4dd815SMatthias Ringwald     char buffer[20];
211aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
212aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2133deb3ec6SMatthias Ringwald }
2143deb3ec6SMatthias Ringwald 
2153deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2163deb3ec6SMatthias Ringwald     char buffer[40];
2173deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
2183deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2193deb3ec6SMatthias Ringwald }
2203deb3ec6SMatthias Ringwald 
221485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){
2223deb3ec6SMatthias Ringwald     char buffer[10];
2233deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nOK\r\n");
2243deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2253deb3ec6SMatthias Ringwald }
2263deb3ec6SMatthias Ringwald 
227485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){
228aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
229aa4dd815SMatthias Ringwald }
230aa4dd815SMatthias Ringwald 
231aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
232aa4dd815SMatthias Ringwald     char buffer[50];
233d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
234aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
235aa4dd815SMatthias Ringwald }
236aa4dd815SMatthias Ringwald 
237ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
238ce263fc8SMatthias Ringwald     char buffer[50];
239ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
240ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
241ce263fc8SMatthias Ringwald }
242ce263fc8SMatthias Ringwald 
243c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
244aa4dd815SMatthias Ringwald     char buffer[50];
245d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
246aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
247aa4dd815SMatthias Ringwald }
248aa4dd815SMatthias Ringwald 
249aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
250aa4dd815SMatthias Ringwald     char buffer[50];
251a0ffb263SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
252aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
253aa4dd815SMatthias Ringwald }
254aa4dd815SMatthias Ringwald 
255485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){
2563deb3ec6SMatthias Ringwald     char buffer[10];
2573deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nERROR\r\n");
2583deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2593deb3ec6SMatthias Ringwald }
2603deb3ec6SMatthias Ringwald 
261485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2623deb3ec6SMatthias Ringwald     char buffer[20];
2633deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2643deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2653deb3ec6SMatthias Ringwald }
2663deb3ec6SMatthias Ringwald 
267ad902e3dSMatthias Ringwald // get size for indicator string
268a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
269ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
27045718b6fSMatthias Ringwald     return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
271a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
272a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
273ad902e3dSMatthias Ringwald }
274ad902e3dSMatthias Ringwald 
275ad902e3dSMatthias Ringwald // store indicator
276a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
277ad902e3dSMatthias Ringwald     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
278a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
279a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
280a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2813deb3ec6SMatthias Ringwald }
282ad902e3dSMatthias Ringwald 
283ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
284a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
285a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
286ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
287ad902e3dSMatthias Ringwald     return 3 + (num_indicators-1) * 2;
2883deb3ec6SMatthias Ringwald }
289ad902e3dSMatthias Ringwald 
290ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
291a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
292ad902e3dSMatthias Ringwald     if (index == 0) {
293ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
294ad902e3dSMatthias Ringwald     }
295ad902e3dSMatthias Ringwald     index--;
296a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
297ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
298ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
299a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
300ad902e3dSMatthias Ringwald     }
301ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators - 1){
302ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
303ad902e3dSMatthias Ringwald     }
304ad902e3dSMatthias Ringwald     return 1; // comma
305ad902e3dSMatthias Ringwald }
306ad902e3dSMatthias Ringwald 
307a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
308ad902e3dSMatthias Ringwald     if (index == 0){
309ad902e3dSMatthias Ringwald         *buffer++ = '\r';
31074386ee0SMatthias Ringwald         *buffer++ = '\n';
311ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
312ad902e3dSMatthias Ringwald         memcpy(buffer, HFP_INDICATOR, len);
313ad902e3dSMatthias Ringwald         buffer += len;
314ad902e3dSMatthias Ringwald         *buffer++ = ':';
315ad902e3dSMatthias Ringwald         return;
316ad902e3dSMatthias Ringwald     }
317ad902e3dSMatthias Ringwald     index--;
318a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
319ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
320ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
321a0ffb263SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
322ad902e3dSMatthias Ringwald         return;
323ad902e3dSMatthias Ringwald     }
324ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators-1){
325ad902e3dSMatthias Ringwald         memcpy(buffer, "\r\n\r\nOK\r\n", 8);
326ad902e3dSMatthias Ringwald         return;
327ad902e3dSMatthias Ringwald     }
328ad902e3dSMatthias Ringwald     *buffer = ',';
3293deb3ec6SMatthias Ringwald }
3303deb3ec6SMatthias Ringwald 
3313deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
3323deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3333deb3ec6SMatthias Ringwald     int i;
3343deb3ec6SMatthias Ringwald     int offset = 0;
335a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr-1; i++) {
336a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3373deb3ec6SMatthias Ringwald     }
338a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
339a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3403deb3ec6SMatthias Ringwald     }
3413deb3ec6SMatthias Ringwald     return offset;
3423deb3ec6SMatthias Ringwald }
3433deb3ec6SMatthias Ringwald 
3443deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
345a0ffb263SMatthias Ringwald     if (buffer_size < hfp_generic_status_indicators_nr * 3) return 0;
3463deb3ec6SMatthias Ringwald     int i;
3473deb3ec6SMatthias Ringwald     int offset = 0;
348a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
349a0ffb263SMatthias 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);
3503deb3ec6SMatthias Ringwald     }
3513deb3ec6SMatthias Ringwald     return offset;
3523deb3ec6SMatthias Ringwald }
3533deb3ec6SMatthias Ringwald 
3543deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
3553deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3563deb3ec6SMatthias Ringwald     int i;
3573deb3ec6SMatthias Ringwald     int offset = 0;
3583deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr-1; i++) {
3593deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3603deb3ec6SMatthias Ringwald     }
3613deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3623deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3633deb3ec6SMatthias Ringwald     }
3643deb3ec6SMatthias Ringwald     return offset;
3653deb3ec6SMatthias Ringwald }
3663deb3ec6SMatthias Ringwald 
3673deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
3683deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0;
3693deb3ec6SMatthias Ringwald     int i;
3703deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
3713deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) {
3723deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3733deb3ec6SMatthias Ringwald     }
3743deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3753deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3763deb3ec6SMatthias Ringwald     }
3773deb3ec6SMatthias Ringwald     return offset;
3783deb3ec6SMatthias Ringwald }
3793deb3ec6SMatthias Ringwald 
380485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
381ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
382a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
383a0ffb263SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
3843deb3ec6SMatthias Ringwald 
385ad902e3dSMatthias Ringwald     // assumes: can send now == true
386ad902e3dSMatthias Ringwald     // assumes: num segments > 0
387aba39421SMatthias Ringwald     // assumes: individual segments are smaller than MTU
388ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
389ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
390ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
391ad902e3dSMatthias Ringwald     int offset = 0;
392ad902e3dSMatthias Ringwald     int segment = start_segment;
393ad902e3dSMatthias Ringwald     while (segment < num_segments){
394a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
395aba39421SMatthias Ringwald         if (offset + segment_len > mtu) break;
396aba39421SMatthias Ringwald         // append segement
397a0ffb263SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset);
398ad902e3dSMatthias Ringwald         offset += segment_len;
399ad902e3dSMatthias Ringwald         segment++;
400ad902e3dSMatthias Ringwald     }
401ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
402ad902e3dSMatthias Ringwald     return segment;
403ad902e3dSMatthias Ringwald }
4043deb3ec6SMatthias Ringwald 
405ad902e3dSMatthias Ringwald // returns next segment to store
406485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
407a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
408485ac19eSMilanka Ringwald     return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
409ad902e3dSMatthias Ringwald         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
4103deb3ec6SMatthias Ringwald }
4113deb3ec6SMatthias Ringwald 
412485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){
4133deb3ec6SMatthias Ringwald     char buffer[40];
41489425bfcSMilanka Ringwald     const int size = sizeof(buffer);
41589425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR);
41689425bfcSMilanka Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9);
41789425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4183deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4193deb3ec6SMatthias Ringwald }
4203deb3ec6SMatthias Ringwald 
421485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){
422ad902e3dSMatthias Ringwald     char buffer[40];
42389425bfcSMilanka Ringwald     const int size = sizeof(buffer);
42489425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
42589425bfcSMilanka Ringwald     offset += hfp_ag_call_services_join(buffer+offset, size-offset-9);
42689425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4273deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4283deb3ec6SMatthias Ringwald }
4293deb3ec6SMatthias Ringwald 
4303deb3ec6SMatthias Ringwald 
431485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){
432485ac19eSMilanka Ringwald     return hfp_ag_send_ok(cid);
4333deb3ec6SMatthias Ringwald }
4343deb3ec6SMatthias Ringwald 
435485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4363deb3ec6SMatthias Ringwald     char buffer[40];
43789425bfcSMilanka Ringwald     const int size = sizeof(buffer);
43889425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
43989425bfcSMilanka Ringwald     offset += hfp_hf_indicators_join(buffer+offset, size-offset-10);
44089425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n");
4413deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4423deb3ec6SMatthias Ringwald }
4433deb3ec6SMatthias Ringwald 
444485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4453deb3ec6SMatthias Ringwald     char buffer[40];
4461cc1d9e9SMilanka Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7);
4471cc1d9e9SMilanka Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4483deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4493deb3ec6SMatthias Ringwald }
4503deb3ec6SMatthias Ringwald 
451485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4523deb3ec6SMatthias Ringwald     char buffer[20];
453aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
4543deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4553deb3ec6SMatthias Ringwald }
4563deb3ec6SMatthias Ringwald 
457485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4585e71456cSMilanka Ringwald     char buffer[41];
4593deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
46089425bfcSMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4613deb3ec6SMatthias Ringwald     } else {
46289425bfcSMilanka Ringwald         int offset = snprintf(buffer,  sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format);
4635e71456cSMilanka Ringwald         offset += snprintf(buffer+offset, 16, "%s", op.name);
46489425bfcSMilanka Ringwald         snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4653deb3ec6SMatthias Ringwald     }
4663deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4673deb3ec6SMatthias Ringwald }
4683deb3ec6SMatthias Ringwald 
4691cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
4701cc1d9e9SMilanka Ringwald     char buffer[30];
47189425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value);
4721cc1d9e9SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
4731cc1d9e9SMilanka Ringwald }
4743deb3ec6SMatthias Ringwald 
475485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){
4761cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
4773deb3ec6SMatthias Ringwald }
4783deb3ec6SMatthias Ringwald 
479485ac19eSMilanka Ringwald static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
4801cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
481aa4dd815SMatthias Ringwald }
482aa4dd815SMatthias Ringwald 
483485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
4841cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
485aa4dd815SMatthias Ringwald }
486aa4dd815SMatthias Ringwald 
487485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
4881cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
489aa4dd815SMatthias Ringwald }
490aa4dd815SMatthias Ringwald 
491485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){
4921cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_RESPONSE_AND_HOLD, state);
493ce263fc8SMatthias Ringwald }
494ce263fc8SMatthias Ringwald 
495a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
496bc1b1537SMilanka Ringwald     if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
497bc1b1537SMilanka Ringwald 
4986a7f44bdSMilanka Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){
4996a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
500df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
5013deb3ec6SMatthias Ringwald         }
5023deb3ec6SMatthias Ringwald     }
503df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
5043deb3ec6SMatthias Ringwald }
5053deb3ec6SMatthias Ringwald 
5067522e673SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){
5077522e673SMatthias Ringwald     // determine highest possible link setting
5087522e673SMatthias Ringwald     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
509d9f77559SMatthias Ringwald     // anything else requires eSCO support on both sides
510d9f77559SMatthias Ringwald     if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
5117522e673SMatthias Ringwald         switch (hfp_connection->negotiated_codec){
5127522e673SMatthias Ringwald             case HFP_CODEC_CVSD:
5137522e673SMatthias Ringwald                 hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
5147522e673SMatthias Ringwald                 if ((hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4))
5157522e673SMatthias Ringwald                 &&  (hfp_supported_features                    & (1<<HFP_AGSF_ESCO_S4))){
5167522e673SMatthias Ringwald                     hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
5177522e673SMatthias Ringwald                 }
5187522e673SMatthias Ringwald                 break;
5197522e673SMatthias Ringwald             case HFP_CODEC_MSBC:
5207522e673SMatthias Ringwald                 hfp_connection->link_setting = HFP_LINK_SETTINGS_T2;
5217522e673SMatthias Ringwald                 break;
522d9f77559SMatthias Ringwald             default:
523d9f77559SMatthias Ringwald                 break;
524d9f77559SMatthias Ringwald         }
5257522e673SMatthias Ringwald     }
5267522e673SMatthias Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
5277522e673SMatthias Ringwald }
5287522e673SMatthias Ringwald 
529a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
530aa4dd815SMatthias Ringwald     /* events ( == commands):
531aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
532aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
533aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
534aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
535aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
536aa4dd815SMatthias Ringwald     */
537aa4dd815SMatthias Ringwald 
538a0ffb263SMatthias Ringwald      switch (hfp_connection->codecs_state){
539aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
540a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
541aa4dd815SMatthias Ringwald             break;
542aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
543a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
544aa4dd815SMatthias Ringwald             break;
545aa4dd815SMatthias Ringwald         default:
546aa4dd815SMatthias Ringwald             break;
547aa4dd815SMatthias Ringwald     }
548aa4dd815SMatthias Ringwald 
549aa4dd815SMatthias Ringwald     // printf(" -> State machine: CC\n");
550aa4dd815SMatthias Ringwald 
551a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
552aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
553a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
554a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
555485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
556aa4dd815SMatthias Ringwald                 return 1;
557aa4dd815SMatthias Ringwald             }
558aa4dd815SMatthias Ringwald 
559a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
560aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
561aa4dd815SMatthias Ringwald                 case HFP_CODECS_EXCHANGED:
562a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
563aa4dd815SMatthias Ringwald                     break;
564aa4dd815SMatthias Ringwald                 default:
565aa4dd815SMatthias Ringwald                     break;
566aa4dd815SMatthias Ringwald             }
567485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
568aa4dd815SMatthias Ringwald             return 1;
569aa4dd815SMatthias Ringwald 
570aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
571a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
572485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
573aa4dd815SMatthias Ringwald             return 1;
574aa4dd815SMatthias Ringwald 
575aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
576a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
577a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
578485ac19eSMilanka Ringwald             hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
579aa4dd815SMatthias Ringwald             return 1;
580aa4dd815SMatthias Ringwald 
581aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
582a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
583a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
584485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
585aa4dd815SMatthias Ringwald                 return 1;
586aa4dd815SMatthias Ringwald             }
587a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
588a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
58903645b44SMatthias Ringwald             log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
590485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
5917522e673SMatthias Ringwald             // now, pick link settings
5927522e673SMatthias Ringwald             hfp_init_link_settings(hfp_connection);
593aa4dd815SMatthias Ringwald             return 1;
594aa4dd815SMatthias Ringwald         default:
595aa4dd815SMatthias Ringwald             break;
596aa4dd815SMatthias Ringwald     }
597aa4dd815SMatthias Ringwald     return 0;
598aa4dd815SMatthias Ringwald }
599aa4dd815SMatthias Ringwald 
600a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
601a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
602d0c4aea6SMilanka Ringwald     hfp_emit_slc_connection_event(hfp_callback, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
603aa4dd815SMatthias Ringwald 
604a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
605d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
606a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
607aa4dd815SMatthias Ringwald     }
608ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
609d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS &&
610d0c20769SMatthias Ringwald         hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
611a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
612ce263fc8SMatthias Ringwald     }
613aa4dd815SMatthias Ringwald }
6143deb3ec6SMatthias Ringwald 
615a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
616c79b4cabSMatthias Ringwald     // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
617a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
618c04cf7ffSMilanka Ringwald     int sent = 0;
619a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
6203deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
621a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
6223deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
623aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
624d715cf51SMatthias Ringwald                     hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
625a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
626a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
627aa4dd815SMatthias Ringwald                     } else {
628a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
629aa4dd815SMatthias Ringwald                     }
630a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
631aa4dd815SMatthias Ringwald                     return 1;
6323deb3ec6SMatthias Ringwald                 default:
6333deb3ec6SMatthias Ringwald                     break;
6343deb3ec6SMatthias Ringwald             }
6353deb3ec6SMatthias Ringwald             break;
6363deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
637c04cf7ffSMilanka Ringwald             sent = codecs_exchange_state_machine(hfp_connection);
638a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
639a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6403deb3ec6SMatthias Ringwald             }
641c04cf7ffSMilanka Ringwald             return sent;
642aa4dd815SMatthias Ringwald 
643aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
644a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
645473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
646473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
647473ac565SMatthias Ringwald                 // continue below in state switch
648ad902e3dSMatthias Ringwald             }
649ad902e3dSMatthias Ringwald             break;
650aa4dd815SMatthias Ringwald 
651aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
652a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
653a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
654485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
655aa4dd815SMatthias Ringwald             return 1;
656aa4dd815SMatthias Ringwald 
6573deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
658a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
659a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
660a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
661a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
662a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
663aa4dd815SMatthias Ringwald             } else {
664a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
6653deb3ec6SMatthias Ringwald             }
6668a46ec40SMatthias Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
667aa4dd815SMatthias Ringwald             return 1;
6683deb3ec6SMatthias Ringwald 
669aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
670a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
671a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
672a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
673aa4dd815SMatthias Ringwald             }
674485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
675a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
676a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
677ce263fc8SMatthias Ringwald             }
678aa4dd815SMatthias Ringwald             return 1;
679aa4dd815SMatthias Ringwald 
680aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
681a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
682a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
683485ac19eSMilanka Ringwald             hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
684aa4dd815SMatthias Ringwald             return 1;
685aa4dd815SMatthias Ringwald 
686aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
687a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
688a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
689485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
690aa4dd815SMatthias Ringwald             return 1;
691aa4dd815SMatthias Ringwald 
692aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
693a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
694a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
695485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
696aa4dd815SMatthias Ringwald             return 1;
6973deb3ec6SMatthias Ringwald         default:
6983deb3ec6SMatthias Ringwald             break;
6993deb3ec6SMatthias Ringwald     }
700473ac565SMatthias Ringwald 
701473ac565SMatthias Ringwald     switch (hfp_connection->state){
702473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
703485ac19eSMilanka Ringwald             int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
704473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
705473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
706473ac565SMatthias Ringwald             if (next_segment < num_segments){
707473ac565SMatthias Ringwald                 // prepare sending of next segment
708473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
709473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
710473ac565SMatthias Ringwald             } else {
711473ac565SMatthias Ringwald                 // done, go to next state
712473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
713473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
714473ac565SMatthias Ringwald             }
715473ac565SMatthias Ringwald             return 1;
716473ac565SMatthias Ringwald         }
717473ac565SMatthias Ringwald         default:
718473ac565SMatthias Ringwald             break;
719473ac565SMatthias Ringwald     }
720c04cf7ffSMilanka Ringwald     return 0;
7213deb3ec6SMatthias Ringwald }
7223deb3ec6SMatthias Ringwald 
723a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
724c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
725c04cf7ffSMilanka Ringwald     if (sent) return 1;
726aa4dd815SMatthias Ringwald 
727a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
728aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
729a0ffb263SMatthias Ringwald             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
730485ac19eSMilanka Ringwald             hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
731aa4dd815SMatthias Ringwald             return 1;
732aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
733aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
734a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
735485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
736a0ffb263SMatthias Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
737aa4dd815SMatthias Ringwald             } else {
738485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
739aa4dd815SMatthias Ringwald             }
740aa4dd815SMatthias Ringwald             return 1;
741aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
742485ac19eSMilanka Ringwald             hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
743aa4dd815SMatthias Ringwald             return 1;
744aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
745485ac19eSMilanka Ringwald             hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
746aa4dd815SMatthias Ringwald             return 1;
747aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
748a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
749485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
750aa4dd815SMatthias Ringwald             } else {
751485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
7523deb3ec6SMatthias Ringwald             }
753aa4dd815SMatthias Ringwald             return 1;
754aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
755485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
756aa4dd815SMatthias Ringwald             return 1;
7573deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
758a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
759a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
760485ac19eSMilanka Ringwald                 hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
761aa4dd815SMatthias Ringwald                 return 1;
7623deb3ec6SMatthias Ringwald             }
763202c8a4cSMatthias Ringwald             break;
7643deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
765485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
766ce263fc8SMatthias Ringwald             return 1;
7673deb3ec6SMatthias Ringwald         default:
7683deb3ec6SMatthias Ringwald             break;
7693deb3ec6SMatthias Ringwald     }
770aa4dd815SMatthias Ringwald     return 0;
7713deb3ec6SMatthias Ringwald }
7723deb3ec6SMatthias Ringwald 
773a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
774a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
775a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
7763deb3ec6SMatthias Ringwald 
777aa4dd815SMatthias Ringwald 
778a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
779a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
780a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
781a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
782aa4dd815SMatthias Ringwald         return 1;
783aa4dd815SMatthias Ringwald     }
784aa4dd815SMatthias Ringwald 
785a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
786aa4dd815SMatthias Ringwald 
787aa4dd815SMatthias Ringwald     // run codecs exchange
788c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
789c04cf7ffSMilanka Ringwald     if (sent) return 1;
790aa4dd815SMatthias Ringwald 
791c04cf7ffSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
792a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
793a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
794a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
795eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
796aa4dd815SMatthias Ringwald         return 1;
797aa4dd815SMatthias Ringwald     }
798aa4dd815SMatthias Ringwald     return 0;
799aa4dd815SMatthias Ringwald }
800aa4dd815SMatthias Ringwald 
801ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
802665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
803665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
804aa4dd815SMatthias Ringwald 
805665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
806a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
807a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
808a0ffb263SMatthias Ringwald             return hfp_connection;
809aa4dd815SMatthias Ringwald         }
810aa4dd815SMatthias Ringwald     }
811aa4dd815SMatthias Ringwald     return NULL;
812aa4dd815SMatthias Ringwald }
813aa4dd815SMatthias Ringwald 
814ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
815d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
816d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
817aa4dd815SMatthias Ringwald 
818d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
819a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
820a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
821aa4dd815SMatthias Ringwald 
822a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
823a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
824aa4dd815SMatthias Ringwald 
825a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
826aa4dd815SMatthias Ringwald }
827aa4dd815SMatthias Ringwald 
828a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
829a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
830a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
831a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
832a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
833aa4dd815SMatthias Ringwald }
834aa4dd815SMatthias Ringwald 
835a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
836a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
837a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
838aa4dd815SMatthias Ringwald }
839aa4dd815SMatthias Ringwald 
840aa4dd815SMatthias Ringwald //
841aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
842aa4dd815SMatthias Ringwald //
843aa4dd815SMatthias Ringwald 
844a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
845aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
846a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
847d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
848aa4dd815SMatthias Ringwald     } else {
849a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
850a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
851a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
852a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
853a0ffb263SMatthias Ringwald         hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG);
854aa4dd815SMatthias Ringwald     }
855aa4dd815SMatthias Ringwald }
856aa4dd815SMatthias Ringwald 
857a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
858a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
859a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
860a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
861a0ffb263SMatthias Ringwald     hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_STOP_RINGINIG);
862aa4dd815SMatthias Ringwald }
863aa4dd815SMatthias Ringwald 
864aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
865aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
866aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
867aa4dd815SMatthias Ringwald 
868665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
869665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
870665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
871a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
872a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
873a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
874a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
875d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
876aa4dd815SMatthias Ringwald         }
877a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
878a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
879aa4dd815SMatthias Ringwald         }
880a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
881aa4dd815SMatthias Ringwald     }
882aa4dd815SMatthias Ringwald }
883aa4dd815SMatthias Ringwald 
884aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
885aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
886aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
887aa4dd815SMatthias Ringwald 
888665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
889665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
890665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
891a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
892a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
893a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
894a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
895aa4dd815SMatthias Ringwald     }
896aa4dd815SMatthias Ringwald }
897aa4dd815SMatthias Ringwald 
898aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
899aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("call");
900aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
901aa4dd815SMatthias Ringwald 
902665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
903665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
904665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
905a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
906a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
907a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
908a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
909aa4dd815SMatthias Ringwald     }
910aa4dd815SMatthias Ringwald }
911aa4dd815SMatthias Ringwald 
912aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
913aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callheld");
914aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
915aa4dd815SMatthias Ringwald 
916665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
917665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
918665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
919a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
920a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
921a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
922a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
923aa4dd815SMatthias Ringwald     }
924aa4dd815SMatthias Ringwald }
925aa4dd815SMatthias Ringwald 
926aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
927aa4dd815SMatthias Ringwald 
928aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
929aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
930aa4dd815SMatthias Ringwald 
931665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
932665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
933665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
934a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
935a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
936a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
937aa4dd815SMatthias Ringwald 
938a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
939a0ffb263SMatthias Ringwald         if (hfp_connection == source){
940a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
941aa4dd815SMatthias Ringwald 
942aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
943a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
944aa4dd815SMatthias Ringwald             } else {
945a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
946d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
947aa4dd815SMatthias Ringwald             }
948aa4dd815SMatthias Ringwald 
949a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
950a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
951aa4dd815SMatthias Ringwald 
952aa4dd815SMatthias Ringwald         } else {
953a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
954aa4dd815SMatthias Ringwald         }
955a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
956aa4dd815SMatthias Ringwald     }
957aa4dd815SMatthias Ringwald }
958aa4dd815SMatthias Ringwald 
959aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
960aa4dd815SMatthias Ringwald 
961aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
962aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
963aa4dd815SMatthias Ringwald 
964665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
965665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
966665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
967a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
968a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
969aa4dd815SMatthias Ringwald 
970a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
971a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
972aa4dd815SMatthias Ringwald 
973a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
974a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
975aa4dd815SMatthias Ringwald 
976a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
977aa4dd815SMatthias Ringwald         break;  // only single
978aa4dd815SMatthias Ringwald     }
979aa4dd815SMatthias Ringwald }
980aa4dd815SMatthias Ringwald 
981aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
982aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
983665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
984665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
985665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
986665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
987aa4dd815SMatthias Ringwald         if (connection->call_state != HFP_CALL_RINGING &&
988aa4dd815SMatthias Ringwald             connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
989aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
990aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
991aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
992aa4dd815SMatthias Ringwald         hfp_run_for_context(connection);
993aa4dd815SMatthias Ringwald     }
994aa4dd815SMatthias Ringwald }
995aa4dd815SMatthias Ringwald 
996aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
997aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
998aa4dd815SMatthias Ringwald 
999665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1000665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1001665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1002a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1003d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1004a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1005a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1006a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1007a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 1;
1008a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1009aa4dd815SMatthias Ringwald     }
1010a0ffb263SMatthias Ringwald     hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_TERMINATED);
1011aa4dd815SMatthias Ringwald }
1012aa4dd815SMatthias Ringwald 
10130cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
1014aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1015aa4dd815SMatthias Ringwald     if (!indicator){
1016d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
101745718b6fSMatthias Ringwald         return;
1018aa4dd815SMatthias Ringwald     };
1019d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1020aa4dd815SMatthias Ringwald }
1021aa4dd815SMatthias Ringwald 
10220cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1023d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1024d210d9c4SMatthias Ringwald     if (!indicator){
1025d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
102645718b6fSMatthias Ringwald         return;
1027d210d9c4SMatthias Ringwald     };
1028d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1029d210d9c4SMatthias Ringwald }
1030d210d9c4SMatthias Ringwald 
10310cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1032aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1033aa4dd815SMatthias Ringwald     if (!indicator){
1034aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
103545718b6fSMatthias Ringwald         return;
1036aa4dd815SMatthias Ringwald     };
1037d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1038aa4dd815SMatthias Ringwald }
1039aa4dd815SMatthias Ringwald 
1040aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1041665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1042665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1043665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1044a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1045a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
1046a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1047a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1048aa4dd815SMatthias Ringwald     }
1049aa4dd815SMatthias Ringwald }
1050aa4dd815SMatthias Ringwald 
1051aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1052665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1053665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1054665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1055a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1056a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1057aa4dd815SMatthias Ringwald     }
1058aa4dd815SMatthias Ringwald     return NULL;
1059aa4dd815SMatthias Ringwald }
1060aa4dd815SMatthias Ringwald 
1061a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1062665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1063665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1064665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1065a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1066a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1067ce263fc8SMatthias Ringwald     }
1068ce263fc8SMatthias Ringwald }
1069ce263fc8SMatthias Ringwald 
1070a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1071aa4dd815SMatthias Ringwald     int indicator_index;
1072a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1073aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1074a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1075a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1076a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1077a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1078a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1079a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1080a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1081a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG);
1082aa4dd815SMatthias Ringwald             break;
1083aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1084a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1085a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1086a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1087aa4dd815SMatthias Ringwald             break;
1088aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1089a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1090a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1091aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1092a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1093aa4dd815SMatthias Ringwald             break;
1094aa4dd815SMatthias Ringwald         default:
1095aa4dd815SMatthias Ringwald             break;
1096aa4dd815SMatthias Ringwald     }
1097aa4dd815SMatthias Ringwald     return 0;
1098aa4dd815SMatthias Ringwald }
1099a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1100a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1101aa4dd815SMatthias Ringwald     int indicator_index;
1102d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1103d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1104d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
110574386ee0SMatthias Ringwald 
1106d210d9c4SMatthias Ringwald     //printf("hfp_ag_call_sm event %d \n", event);
1107aa4dd815SMatthias Ringwald     switch (event){
1108aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1109d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1110aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1111d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1112aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1113d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1114d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1115aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
111660ebb071SMilanka Ringwald                             log_info("AG rings");
1117aa4dd815SMatthias Ringwald                             break;
1118aa4dd815SMatthias Ringwald                         default:
11193deb3ec6SMatthias Ringwald                             break;
11203deb3ec6SMatthias Ringwald                     }
11213deb3ec6SMatthias Ringwald                     break;
1122aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1123d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1124aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1125d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1126d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1127aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
112860ebb071SMilanka Ringwald                             log_info("AG call waiting");
1129aa4dd815SMatthias Ringwald                             break;
1130aa4dd815SMatthias Ringwald                         default:
11313deb3ec6SMatthias Ringwald                             break;
11323deb3ec6SMatthias Ringwald                     }
11333deb3ec6SMatthias Ringwald                     break;
1134aa4dd815SMatthias Ringwald             }
1135aa4dd815SMatthias Ringwald             break;
1136aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1137d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1138aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1139d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1140aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1141d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1142d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1143d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1144aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
114560ebb071SMilanka Ringwald                             log_info("AG answers call, accept call by GSM");
1146aa4dd815SMatthias Ringwald                             break;
1147aa4dd815SMatthias Ringwald                         default:
11483deb3ec6SMatthias Ringwald                             break;
11493deb3ec6SMatthias Ringwald                     }
1150aa4dd815SMatthias Ringwald                     break;
1151aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1152d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1153aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
115460ebb071SMilanka Ringwald                             log_info("AG: current call is placed on hold, incoming call gets active");
1155d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1156d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1157d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1158ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1159ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1160aa4dd815SMatthias Ringwald                             break;
1161aa4dd815SMatthias Ringwald                         default:
1162aa4dd815SMatthias Ringwald                             break;
1163aa4dd815SMatthias Ringwald                     }
1164aa4dd815SMatthias Ringwald                     break;
1165aa4dd815SMatthias Ringwald             }
1166aa4dd815SMatthias Ringwald             break;
1167ce263fc8SMatthias Ringwald 
1168ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1169d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1170ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1171d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1172ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
117360ebb071SMilanka Ringwald                             log_info("AG: joining held call with active call");
1174d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG);
1175d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1176ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1177a0ffb263SMatthias Ringwald                             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL);
1178ce263fc8SMatthias Ringwald                             break;
1179ce263fc8SMatthias Ringwald                         default:
1180ce263fc8SMatthias Ringwald                             break;
1181ce263fc8SMatthias Ringwald                     }
1182ce263fc8SMatthias Ringwald                     break;
1183ce263fc8SMatthias Ringwald                 default:
1184ce263fc8SMatthias Ringwald                     break;
1185ce263fc8SMatthias Ringwald             }
1186ce263fc8SMatthias Ringwald             break;
1187ce263fc8SMatthias Ringwald 
1188aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1189d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1190aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1191d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1192aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1193d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF);
1194d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1195d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1196a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
119760ebb071SMilanka Ringwald                             log_info("HF answers call, accept call by GSM");
1198a0ffb263SMatthias Ringwald                             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_ANSWERED);
1199aa4dd815SMatthias Ringwald                             break;
1200aa4dd815SMatthias Ringwald                         default:
1201aa4dd815SMatthias Ringwald                             break;
1202aa4dd815SMatthias Ringwald                     }
12033deb3ec6SMatthias Ringwald                     break;
12043deb3ec6SMatthias Ringwald                 default:
12053deb3ec6SMatthias Ringwald                     break;
12063deb3ec6SMatthias Ringwald             }
12073deb3ec6SMatthias Ringwald             break;
12083deb3ec6SMatthias Ringwald 
1209ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1210d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1211ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1212d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1213ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1214d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG);
1215ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1216ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1217a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1218ce263fc8SMatthias Ringwald                             // as with regualr call
1219d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1220d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1221ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
122260ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by AG");
1223ce263fc8SMatthias Ringwald                             break;
1224ce263fc8SMatthias Ringwald                         default:
1225ce263fc8SMatthias Ringwald                             break;
1226ce263fc8SMatthias Ringwald                     }
1227ce263fc8SMatthias Ringwald                     break;
1228ce263fc8SMatthias Ringwald                 default:
1229ce263fc8SMatthias Ringwald                     break;
1230ce263fc8SMatthias Ringwald             }
1231ce263fc8SMatthias Ringwald             break;
1232ce263fc8SMatthias Ringwald 
1233ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1234d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1235ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1236d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1237ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1238d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF);
1239ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1240ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1241a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1242ce263fc8SMatthias Ringwald                             // as with regualr call
1243d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1244d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1245a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
124660ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by HF");
1247ce263fc8SMatthias Ringwald                             break;
1248ce263fc8SMatthias Ringwald                         default:
1249ce263fc8SMatthias Ringwald                             break;
1250ce263fc8SMatthias Ringwald                     }
1251ce263fc8SMatthias Ringwald                     break;
1252ce263fc8SMatthias Ringwald                 default:
1253ce263fc8SMatthias Ringwald                     break;
1254ce263fc8SMatthias Ringwald             }
1255ce263fc8SMatthias Ringwald             break;
1256ce263fc8SMatthias Ringwald 
1257ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1258ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1259ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1260ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1261d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG);
1262a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1263ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1264a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
126560ebb071SMilanka Ringwald             log_info("Held Call accepted and active");
1266ce263fc8SMatthias Ringwald             break;
1267ce263fc8SMatthias Ringwald 
1268ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1269ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1270ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1271ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1272d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG);
1273a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1274ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1275a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1276ce263fc8SMatthias Ringwald             // from terminate by ag
1277d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1278ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1279ce263fc8SMatthias Ringwald             break;
1280ce263fc8SMatthias Ringwald 
1281aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1282d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1283aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1284d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1285aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1286d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1287d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1288aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1289aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
129060ebb071SMilanka Ringwald                             log_info("HF Rejected Incoming call, AG terminate call");
1291aa4dd815SMatthias Ringwald                             break;
1292aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1293aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
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();
129760ebb071SMilanka Ringwald                             log_info("AG terminate outgoing call process");
1298202c8a4cSMatthias Ringwald                             break;
1299aa4dd815SMatthias Ringwald                         default:
1300aa4dd815SMatthias Ringwald                             break;
1301aa4dd815SMatthias Ringwald                     }
1302aa4dd815SMatthias Ringwald                     break;
1303aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1304d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1305d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1306aa4dd815SMatthias Ringwald                     hfp_ag_transfer_call_state();
1307a0ffb263SMatthias Ringwald                     hfp_connection->call_state = HFP_CALL_IDLE;
130860ebb071SMilanka Ringwald                     log_info("AG terminate call");
1309aa4dd815SMatthias Ringwald                     break;
1310aa4dd815SMatthias Ringwald             }
1311aa4dd815SMatthias Ringwald             break;
13123deb3ec6SMatthias Ringwald 
1313aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1314d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1315aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1316d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1317aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1318d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1319d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1320aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
132160ebb071SMilanka Ringwald                             log_info("AG Rejected Incoming call, AG terminate call");
1322aa4dd815SMatthias Ringwald                             break;
1323aa4dd815SMatthias Ringwald                         default:
1324aa4dd815SMatthias Ringwald                             break;
13253deb3ec6SMatthias Ringwald                     }
1326202c8a4cSMatthias Ringwald                     break;
1327aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1328d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1329d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1330d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1331aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
133260ebb071SMilanka Ringwald                     log_info("AG terminate call");
1333aa4dd815SMatthias Ringwald                     break;
1334aa4dd815SMatthias Ringwald                 default:
13353deb3ec6SMatthias Ringwald                     break;
13363deb3ec6SMatthias Ringwald             }
13373deb3ec6SMatthias Ringwald             break;
1338aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1339d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1340aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1341d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1342aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1343aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
134460ebb071SMilanka Ringwald                             log_info("Incoming call interrupted");
1345aa4dd815SMatthias Ringwald                             break;
1346aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1347aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
134860ebb071SMilanka Ringwald                             log_info("Outgoing call interrupted\n");
134960ebb071SMilanka Ringwald                             log_info("AG notify call dropped\n");
1350aa4dd815SMatthias Ringwald                             break;
1351aa4dd815SMatthias Ringwald                         default:
13523deb3ec6SMatthias Ringwald                             break;
13533deb3ec6SMatthias Ringwald                     }
1354d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1355d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1356aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
1357aa4dd815SMatthias Ringwald                     break;
1358aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1359ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1360d210d9c4SMatthias Ringwald                         hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1361ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1362a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1363ce263fc8SMatthias Ringwald                     }
1364d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1365d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1366d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1367aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
136860ebb071SMilanka Ringwald                     log_info("AG notify call dropped");
1369aa4dd815SMatthias Ringwald                     break;
1370aa4dd815SMatthias Ringwald                 default:
1371aa4dd815SMatthias Ringwald                     break;
1372aa4dd815SMatthias Ringwald             }
1373aa4dd815SMatthias Ringwald             break;
1374aa4dd815SMatthias Ringwald 
1375aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1376d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1377d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1378a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1379a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1380d210d9c4SMatthias Ringwald                 break;
1381d210d9c4SMatthias Ringwald             }
1382a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]);
13839cae807eSMatthias Ringwald 
1384a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
138574386ee0SMatthias Ringwald 
1386a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1387aa4dd815SMatthias Ringwald             break;
1388aa4dd815SMatthias Ringwald 
13899cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1390d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1391d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1392a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1393a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1394d210d9c4SMatthias Ringwald                 break;
1395d210d9c4SMatthias Ringwald             }
1396d210d9c4SMatthias Ringwald 
139774386ee0SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED);
1398a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
139974386ee0SMatthias Ringwald 
140060ebb071SMilanka Ringwald             log_info("Redial last number");
14019cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1402aa4dd815SMatthias Ringwald 
14039cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
140460ebb071SMilanka Ringwald                 log_info("Last number exists: accept call");
14059cae807eSMatthias Ringwald                 hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
14069cae807eSMatthias Ringwald             } else {
140760ebb071SMilanka Ringwald                 log_info("log_infoLast number missing: reject call");
14089cae807eSMatthias Ringwald                 hfp_ag_outgoing_call_rejected();
14099cae807eSMatthias Ringwald             }
14109cae807eSMatthias Ringwald             break;
14119cae807eSMatthias Ringwald         }
1412aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
1413a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1414a0ffb263SMatthias Ringwald             if (!hfp_connection){
1415a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1416aa4dd815SMatthias Ringwald                 break;
1417aa4dd815SMatthias Ringwald             }
1418d210d9c4SMatthias Ringwald 
1419d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED);
1420a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1421a0ffb263SMatthias Ringwald             hfp_connection->send_error = 1;
1422a0ffb263SMatthias Ringwald             hfp_run_for_context(hfp_connection);
1423aa4dd815SMatthias Ringwald             break;
1424aa4dd815SMatthias Ringwald 
1425d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1426a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1427a0ffb263SMatthias Ringwald             if (!hfp_connection){
1428a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1429aa4dd815SMatthias Ringwald                 break;
1430aa4dd815SMatthias Ringwald             }
1431aa4dd815SMatthias Ringwald 
1432a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1433a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1434aa4dd815SMatthias Ringwald 
1435aa4dd815SMatthias Ringwald             // trigger callsetup to be
1436d0c20769SMatthias Ringwald             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1437d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED);
1438d210d9c4SMatthias Ringwald 
1439d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1440aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1441a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1442aa4dd815SMatthias Ringwald 
1443aa4dd815SMatthias Ringwald             // put current call on hold if active
1444d210d9c4SMatthias Ringwald             if (put_call_on_hold){
144560ebb071SMilanka Ringwald                 log_info("AG putting current call on hold for new outgoing calllog_info");
1446d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1447aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1448485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1449aa4dd815SMatthias Ringwald             }
1450aa4dd815SMatthias Ringwald 
1451aa4dd815SMatthias Ringwald             // start audio if needed
1452d97d752dSMilanka Ringwald             hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1453aa4dd815SMatthias Ringwald             break;
1454d210d9c4SMatthias Ringwald         }
1455aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1456a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1457a0ffb263SMatthias Ringwald             if (!hfp_connection){
1458a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1459aa4dd815SMatthias Ringwald                 break;
1460aa4dd815SMatthias Ringwald             }
1461d210d9c4SMatthias Ringwald 
1462d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING);
1463a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1464d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1465aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1466aa4dd815SMatthias Ringwald             break;
1467aa4dd815SMatthias Ringwald 
1468d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1469aa4dd815SMatthias Ringwald             // get outgoing call
1470a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1471a0ffb263SMatthias Ringwald             if (!hfp_connection){
1472a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1473aa4dd815SMatthias Ringwald             }
1474a0ffb263SMatthias Ringwald             if (!hfp_connection){
1475a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1476aa4dd815SMatthias Ringwald                 break;
1477aa4dd815SMatthias Ringwald             }
1478d210d9c4SMatthias Ringwald 
1479d0c20769SMatthias 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;
1480d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED);
1481a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1482d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1483d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1484aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1485aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1486d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1487d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1488aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
14893deb3ec6SMatthias Ringwald             }
14903deb3ec6SMatthias Ringwald             break;
1491d210d9c4SMatthias Ringwald         }
1492d210d9c4SMatthias Ringwald 
1493d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1494d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY);
1495d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1496a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1497a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
149860ebb071SMilanka Ringwald             log_info("AG: Call Waiting, User Busy");
1499d210d9c4SMatthias Ringwald             break;
1500d210d9c4SMatthias Ringwald 
1501d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1502d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1503d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1504d210d9c4SMatthias Ringwald 
1505d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1506d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1507a0ffb263SMatthias Ringwald                 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1508d0c20769SMatthias Ringwald 
1509d0c20769SMatthias Ringwald             }
1510d0c20769SMatthias Ringwald 
1511d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
151260ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Accept new call");
1513d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1514a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1515d210d9c4SMatthias Ringwald             } else {
151660ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Resume held call");
1517d210d9c4SMatthias Ringwald             }
1518d210d9c4SMatthias Ringwald             if (call_held){
1519d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1520a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1521d210d9c4SMatthias Ringwald             }
1522d0c20769SMatthias Ringwald 
1523a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1524d210d9c4SMatthias Ringwald             break;
1525d210d9c4SMatthias Ringwald         }
1526d0c20769SMatthias Ringwald 
1527d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1528d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1529d210d9c4SMatthias Ringwald             // only update if callsetup changed
1530d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1531a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1532d0c20769SMatthias Ringwald 
1533d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
153460ebb071SMilanka Ringwald                 log_info("AG: Call on Hold, Accept new call");
1535d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1536a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1537d210d9c4SMatthias Ringwald             } else {
153860ebb071SMilanka Ringwald                 log_info("AG: Swap calls");
1539d210d9c4SMatthias Ringwald             }
1540d0c20769SMatthias Ringwald 
1541d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1542d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1543a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1544a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1545d210d9c4SMatthias Ringwald             break;
1546d210d9c4SMatthias Ringwald         }
1547d0c20769SMatthias Ringwald 
1548d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1549d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1550d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
155160ebb071SMilanka Ringwald                 log_info("AG: Join 3-way-call");
1552d210d9c4SMatthias Ringwald                 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL);
1553d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1554a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1555a0ffb263SMatthias Ringwald                 hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL);
1556d210d9c4SMatthias Ringwald             }
1557a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1558d210d9c4SMatthias Ringwald             break;
1559d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1560d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1561d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS);
156260ebb071SMilanka Ringwald             log_info("AG: Transfer call -> Connect two calls and disconnect");
1563d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1564d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1565a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1566a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1567a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1568d210d9c4SMatthias Ringwald             break;
1569ce263fc8SMatthias Ringwald 
15703deb3ec6SMatthias Ringwald         default:
15713deb3ec6SMatthias Ringwald             break;
15723deb3ec6SMatthias Ringwald     }
1573d210d9c4SMatthias Ringwald 
1574d0c20769SMatthias Ringwald 
15753deb3ec6SMatthias Ringwald }
15763deb3ec6SMatthias Ringwald 
15779cae807eSMatthias Ringwald 
1578a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
15799cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
15809cae807eSMatthias Ringwald     if (!active_call) return;
15819cae807eSMatthias Ringwald 
15829cae807eSMatthias Ringwald     int idx = active_call->index;
15839cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
15849cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
15859cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
15869cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
15879cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
15889cae807eSMatthias Ringwald     char * number = active_call->clip_number;
15899cae807eSMatthias Ringwald 
15909cae807eSMatthias Ringwald     char buffer[100];
15919cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
15929cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
15939cae807eSMatthias Ringwald     if (number){
15941cc1d9e9SMilanka Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
15959cae807eSMatthias Ringwald     }
15969cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
159760ebb071SMilanka 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,
15989cae807eSMatthias Ringwald        mode, mpty, type, number);
1599a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
16009cae807eSMatthias Ringwald }
16019cae807eSMatthias Ringwald 
1602a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection){
1603a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1604724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
1605473ac565SMatthias Ringwald 
1606e30a6a47SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1607473ac565SMatthias Ringwald         log_info("hfp_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1608e30a6a47SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1609e30a6a47SMatthias Ringwald         return;
1610e30a6a47SMatthias Ringwald     }
1611a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1612a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1613a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1614a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1615a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
16169cae807eSMatthias Ringwald         } else {
1617a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1618a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1619a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16209cae807eSMatthias Ringwald         }
1621ce263fc8SMatthias Ringwald         return;
1622ce263fc8SMatthias Ringwald     }
1623ce263fc8SMatthias Ringwald 
1624a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1625a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1626a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1627aa4dd815SMatthias Ringwald         return;
1628aa4dd815SMatthias Ringwald     }
1629aa4dd815SMatthias Ringwald 
1630a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1631a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1632a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1633a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1634485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1635a0ffb263SMatthias Ringwald         return;
1636a0ffb263SMatthias Ringwald     }
1637a0ffb263SMatthias Ringwald 
1638a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1639a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1640a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1641485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1642aa4dd815SMatthias Ringwald         return;
1643aa4dd815SMatthias Ringwald     }
1644aa4dd815SMatthias Ringwald 
1645ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1646a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1647a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1648a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1649485ac19eSMilanka Ringwald         hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1650ce263fc8SMatthias Ringwald         return;
1651ce263fc8SMatthias Ringwald     }
1652ce263fc8SMatthias Ringwald 
1653a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1654a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1655a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1656485ac19eSMilanka Ringwald         hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1657ce263fc8SMatthias Ringwald         return;
1658ce263fc8SMatthias Ringwald     }
1659ce263fc8SMatthias Ringwald 
1660aa4dd815SMatthias Ringwald     // update AG indicators
1661a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1662aa4dd815SMatthias Ringwald         int i;
1663a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1664a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1665a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1666a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1667ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1668ce263fc8SMatthias Ringwald                     break;
1669ce263fc8SMatthias Ringwald                 }
1670485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1671aa4dd815SMatthias Ringwald                 return;
1672aa4dd815SMatthias Ringwald             }
1673aa4dd815SMatthias Ringwald         }
1674aa4dd815SMatthias Ringwald     }
1675aa4dd815SMatthias Ringwald 
1676a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1677a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1678a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1679485ac19eSMilanka Ringwald         hfp_ag_send_ring(hfp_connection->rfcomm_cid);
1680aa4dd815SMatthias Ringwald         return;
1681aa4dd815SMatthias Ringwald     }
1682aa4dd815SMatthias Ringwald 
1683a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1684a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1685a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1686a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1687aa4dd815SMatthias Ringwald         return;
1688aa4dd815SMatthias Ringwald     }
1689aa4dd815SMatthias Ringwald 
1690a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1691a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1692a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1693a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1694a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1695aa4dd815SMatthias Ringwald         return;
1696aa4dd815SMatthias Ringwald     }
1697aa4dd815SMatthias Ringwald 
1698a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1699a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1700a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1701a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1702ce263fc8SMatthias Ringwald         } else {
1703a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1704a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1705485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1706ce263fc8SMatthias Ringwald         }
1707a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1708485ac19eSMilanka Ringwald 
1709ce263fc8SMatthias Ringwald     }
1710ce263fc8SMatthias Ringwald 
1711a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1712a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1713a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1714485ac19eSMilanka Ringwald         hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1715aa4dd815SMatthias Ringwald         return;
1716aa4dd815SMatthias Ringwald     }
1717aa4dd815SMatthias Ringwald 
1718a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1719a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1720a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1721485ac19eSMilanka Ringwald         hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
17223deb3ec6SMatthias Ringwald         return;
17233deb3ec6SMatthias Ringwald     }
17243deb3ec6SMatthias Ringwald 
1725a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1726a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1727485ac19eSMilanka Ringwald         hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1728ce263fc8SMatthias Ringwald         return;
1729ce263fc8SMatthias Ringwald     }
1730ce263fc8SMatthias Ringwald 
17312ee45eb2SMilanka Ringwald     // trigger codec exchange if requested by app
17322ee45eb2SMilanka Ringwald     if (hfp_connection->trigger_codec_exchange){
17332ee45eb2SMilanka Ringwald         log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state);
17342ee45eb2SMilanka Ringwald     }
17352ee45eb2SMilanka Ringwald     if (hfp_connection->trigger_codec_exchange && hfp_connection->command == HFP_CMD_NONE){
17362ee45eb2SMilanka Ringwald         switch (hfp_connection->codecs_state){
17372ee45eb2SMilanka Ringwald             case HFP_CODECS_IDLE:
17382ee45eb2SMilanka Ringwald             case HFP_CODECS_RECEIVED_LIST:
17392ee45eb2SMilanka Ringwald             case HFP_CODECS_AG_RESEND_COMMON_CODEC:
17402ee45eb2SMilanka Ringwald             case HFP_CODECS_ERROR:
17412ee45eb2SMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 0;
17422ee45eb2SMilanka Ringwald                 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
17432ee45eb2SMilanka Ringwald                 break;
17442ee45eb2SMilanka Ringwald             default:
17452ee45eb2SMilanka Ringwald                 break;
17462ee45eb2SMilanka Ringwald         }
17472ee45eb2SMilanka Ringwald     }
17482ee45eb2SMilanka Ringwald 
1749c04cf7ffSMilanka Ringwald     int cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1750c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1751c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
17523deb3ec6SMatthias Ringwald     }
17533deb3ec6SMatthias Ringwald 
1754c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1755c04cf7ffSMilanka Ringwald         cmd_sent = call_setup_state_machine(hfp_connection);
1756aa4dd815SMatthias Ringwald     }
1757aa4dd815SMatthias Ringwald 
1758c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1759c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
1760aa4dd815SMatthias Ringwald     }
17613deb3ec6SMatthias Ringwald 
1762c04cf7ffSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_NONE && !cmd_sent){
1763a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1764a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
17653deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1766a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1767a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
17683deb3ec6SMatthias Ringwald                 break;
17693deb3ec6SMatthias Ringwald             default:
17703deb3ec6SMatthias Ringwald                 break;
17713deb3ec6SMatthias Ringwald         }
17723deb3ec6SMatthias Ringwald     }
1773c04cf7ffSMilanka Ringwald 
1774c04cf7ffSMilanka Ringwald     if (cmd_sent){
1775a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1776473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1777473ac565SMatthias Ringwald     }
17783deb3ec6SMatthias Ringwald }
1779d68dcce1SMatthias Ringwald 
1780ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1781ce263fc8SMatthias Ringwald     int i;
1782a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1783a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1784ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1785ce263fc8SMatthias Ringwald             return indicator;
1786ce263fc8SMatthias Ringwald         }
1787ce263fc8SMatthias Ringwald     }
1788ce263fc8SMatthias Ringwald     return NULL;
1789ce263fc8SMatthias Ringwald }
17903deb3ec6SMatthias Ringwald 
1791aa4dd815SMatthias Ringwald static void hfp_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
17928a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
17938a46ec40SMatthias Ringwald 
17948a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
17958a46ec40SMatthias Ringwald     if (size < 1) return;
17969ec2630cSMatthias Ringwald 
1797a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1798a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
17991e35c04dSMatthias Ringwald 
18008a46ec40SMatthias Ringwald     // temp overwrite last byte (most likely \n for log_info)
18011e35c04dSMatthias Ringwald     char last_char = packet[size-1];
18021e35c04dSMatthias Ringwald     packet[size-1] = 0;
18031e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
18041e35c04dSMatthias Ringwald     packet[size-1] = last_char;
18051e35c04dSMatthias Ringwald 
18068a46ec40SMatthias Ringwald     // process messages byte-wise
18073deb3ec6SMatthias Ringwald     int pos;
18083deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1809a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1810aa4dd815SMatthias Ringwald     }
1811ce263fc8SMatthias Ringwald     hfp_generic_status_indicator_t * indicator;
1812ce263fc8SMatthias Ringwald     int value;
1813a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
1814ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1815ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_active){
1816a0ffb263SMatthias Ringwald                 hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1817ce263fc8SMatthias Ringwald             }
1818a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1819ce263fc8SMatthias Ringwald             break;
1820ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
18212308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
182260ebb071SMilanka Ringwald             log_info("HF Response and Hold: %u", value);
1823ce263fc8SMatthias Ringwald             switch(value){
1824ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1825a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1826ce263fc8SMatthias Ringwald                     break;
1827ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1828a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1829ce263fc8SMatthias Ringwald                     break;
1830ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1831a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1832ce263fc8SMatthias Ringwald                     break;
1833ce263fc8SMatthias Ringwald                 default:
1834ce263fc8SMatthias Ringwald                     break;
1835ce263fc8SMatthias Ringwald             }
1836a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1837ce263fc8SMatthias Ringwald             break;
1838ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
1839a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1840ce263fc8SMatthias Ringwald             // find indicator by assigned number
1841a0ffb263SMatthias Ringwald             indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1842ce263fc8SMatthias Ringwald             if (!indicator){
1843a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1844ce263fc8SMatthias Ringwald                 break;
1845ce263fc8SMatthias Ringwald             }
18462308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1847ce263fc8SMatthias Ringwald             switch (indicator->uuid){
1848ce263fc8SMatthias Ringwald                 case 1: // enhanced security
1849ce263fc8SMatthias Ringwald                     if (value > 1) {
1850a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1851ce263fc8SMatthias Ringwald                         return;
1852ce263fc8SMatthias Ringwald                     }
185360ebb071SMilanka Ringwald                     log_info("HF Indicator 'enhanced security' set to %u", value);
1854ce263fc8SMatthias Ringwald                     break;
1855ce263fc8SMatthias Ringwald                 case 2: // battery level
1856ce263fc8SMatthias Ringwald                     if (value > 100){
1857a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1858ce263fc8SMatthias Ringwald                         return;
1859ce263fc8SMatthias Ringwald                     }
186060ebb071SMilanka Ringwald                     log_info("HF Indicator 'battery' set to %u", value);
1861ce263fc8SMatthias Ringwald                     break;
1862ce263fc8SMatthias Ringwald                 default:
186360ebb071SMilanka Ringwald                     log_info("HF Indicator unknown set to %u", value);
1864ce263fc8SMatthias Ringwald                     break;
1865ce263fc8SMatthias Ringwald             }
1866a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1867ce263fc8SMatthias Ringwald             break;
1868ce263fc8SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1869ce263fc8SMatthias Ringwald             // expected by SLC state machine
1870a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1871a0ffb263SMatthias Ringwald             hfp_connection->send_ag_indicators_segment = 0;
1872a0ffb263SMatthias Ringwald             hfp_connection->send_ag_status_indicators = 1;
1873ce263fc8SMatthias Ringwald             break;
1874ce263fc8SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1875a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1876a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1877a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 1;
1878ce263fc8SMatthias Ringwald             break;
1879ce263fc8SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1880ce263fc8SMatthias Ringwald             if (subscriber_numbers_count == 0){
1881485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1882ce263fc8SMatthias Ringwald                 break;
1883ce263fc8SMatthias Ringwald             }
1884a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1885a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 1;
1886ce263fc8SMatthias Ringwald             break;
1887c1797c7dSMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
1888a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1889a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]);
1890c1797c7dSMatthias Ringwald             break;
1891aa4dd815SMatthias Ringwald         case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1892a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1893a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1894aa4dd815SMatthias Ringwald             break;
1895aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1896a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1897aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1898a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1899a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
190060ebb071SMilanka Ringwald                 log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction);
1901aa4dd815SMatthias Ringwald             } else {
1902a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1903aa4dd815SMatthias Ringwald             }
1904aa4dd815SMatthias Ringwald             break;
1905aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_ANSWERED:
1906a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
190760ebb071SMilanka Ringwald             log_info("HFP: ATA");
1908a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1909aa4dd815SMatthias Ringwald             break;
1910aa4dd815SMatthias Ringwald         case HFP_CMD_HANG_UP_CALL:
1911a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1912a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1913a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1914aa4dd815SMatthias Ringwald             break;
1915aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_HOLD: {
1916aa4dd815SMatthias Ringwald             // TODO: fully implement this
1917a0ffb263SMatthias Ringwald             log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]);
1918a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1919a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1920a0ffb263SMatthias Ringwald             hfp_connection->call_index = 0;
1921d0c20769SMatthias Ringwald 
1922a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
19232308e108SMilanka Ringwald                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
1924d0c20769SMatthias Ringwald             }
1925d210d9c4SMatthias Ringwald 
1926a0ffb263SMatthias Ringwald             switch (hfp_connection->line_buffer[0]){
1927aa4dd815SMatthias Ringwald                 case '0':
1928d0c20769SMatthias Ringwald                     // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1929a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1930aa4dd815SMatthias Ringwald                     break;
1931aa4dd815SMatthias Ringwald                 case '1':
1932d0c20769SMatthias Ringwald                     // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1933d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1934d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1935a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1936aa4dd815SMatthias Ringwald                     break;
1937aa4dd815SMatthias Ringwald                 case '2':
1938d0c20769SMatthias Ringwald                     // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1939d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1940d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1941a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1942aa4dd815SMatthias Ringwald                     break;
1943aa4dd815SMatthias Ringwald                 case '3':
1944d0c20769SMatthias Ringwald                     // Adds a held call to the conversation.
1945a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1946aa4dd815SMatthias Ringwald                     break;
1947aa4dd815SMatthias Ringwald                 case '4':
1948d0c20769SMatthias Ringwald                     // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1949a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1950aa4dd815SMatthias Ringwald                     break;
1951aa4dd815SMatthias Ringwald                 default:
1952aa4dd815SMatthias Ringwald                     break;
1953aa4dd815SMatthias Ringwald             }
1954aa4dd815SMatthias Ringwald             break;
1955aa4dd815SMatthias Ringwald         }
1956aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_PHONE_NUMBER:
1957a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1958a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
1959aa4dd815SMatthias Ringwald             break;
1960aa4dd815SMatthias Ringwald         case HFP_CMD_REDIAL_LAST_NUMBER:
1961a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1962a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
1963aa4dd815SMatthias Ringwald             break;
1964aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
1965a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1966a0ffb263SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0';
1967a0ffb263SMatthias Ringwald             log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
1968a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1969aa4dd815SMatthias Ringwald             break;
1970aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1971a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1972a0ffb263SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0';
1973a0ffb263SMatthias Ringwald             log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
1974a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1975aa4dd815SMatthias Ringwald             break;
1976aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1977a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1978a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
197960ebb071SMilanka Ringwald             log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
1980aa4dd815SMatthias Ringwald             break;
1981aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1982a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1983a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
198460ebb071SMilanka Ringwald             log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
1985aa4dd815SMatthias Ringwald             break;
1986aa4dd815SMatthias Ringwald         default:
1987aa4dd815SMatthias Ringwald             break;
19883deb3ec6SMatthias Ringwald     }
19893deb3ec6SMatthias Ringwald }
19903deb3ec6SMatthias Ringwald 
1991aa4dd815SMatthias Ringwald static void hfp_run(void){
1992d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1993d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1994d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1995d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1996a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
19973deb3ec6SMatthias Ringwald     }
19983deb3ec6SMatthias Ringwald }
19993deb3ec6SMatthias Ringwald 
2000ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
20013deb3ec6SMatthias Ringwald     switch (packet_type){
20023deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
2003aa4dd815SMatthias Ringwald             hfp_handle_rfcomm_data(packet_type, channel, packet, size);
20043deb3ec6SMatthias Ringwald             break;
20053deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2006e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2007e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2008e30a6a47SMatthias Ringwald                 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2009e30a6a47SMatthias Ringwald                 return;
2010e30a6a47SMatthias Ringwald             }
2011*323d3000SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2012aa4dd815SMatthias Ringwald             break;
20133deb3ec6SMatthias Ringwald         default:
20143deb3ec6SMatthias Ringwald             break;
20153deb3ec6SMatthias Ringwald     }
20163deb3ec6SMatthias Ringwald 
20173deb3ec6SMatthias Ringwald     hfp_run();
20183deb3ec6SMatthias Ringwald }
20193deb3ec6SMatthias Ringwald 
20201e35c04dSMatthias Ringwald 
2021a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
20223deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
20233deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
20243deb3ec6SMatthias Ringwald         return;
20253deb3ec6SMatthias Ringwald     }
20263deb3ec6SMatthias Ringwald     int i;
2027a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
20283deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
20293deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
20303deb3ec6SMatthias Ringwald     }
2031a0ffb263SMatthias Ringwald }
20323deb3ec6SMatthias Ringwald 
2033a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2034a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2035a0ffb263SMatthias Ringwald }
20363deb3ec6SMatthias Ringwald 
2037a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2038a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
2039a0ffb263SMatthias Ringwald     memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2040a0ffb263SMatthias Ringwald }
20413deb3ec6SMatthias Ringwald 
2042a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
2043a0ffb263SMatthias Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return;
2044a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
2045a0ffb263SMatthias Ringwald     memcpy(hfp_generic_status_indicators, hf_indicators, hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2046a0ffb263SMatthias Ringwald }
2047a0ffb263SMatthias Ringwald 
2048a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
20493deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
20503deb3ec6SMatthias Ringwald     memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *));
2051a0ffb263SMatthias Ringwald }
2052a0ffb263SMatthias Ringwald 
2053a0ffb263SMatthias Ringwald 
2054a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
2055d63c37a1SMatthias Ringwald     // register for HCI events
2056d63c37a1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
2057d63c37a1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
2058d63c37a1SMatthias Ringwald 
2059d68dcce1SMatthias Ringwald     rfcomm_register_service(&packet_handler, rfcomm_channel_nr, 0xffff);
2060aa4dd815SMatthias Ringwald 
2061d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2062d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2063d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2064d210d9c4SMatthias Ringwald 
2065d68dcce1SMatthias Ringwald     hfp_set_packet_handler_for_rfcomm_connections(&packet_handler);
2066d68dcce1SMatthias Ringwald 
2067d210d9c4SMatthias Ringwald     hfp_gsm_init();
20683deb3ec6SMatthias Ringwald }
20693deb3ec6SMatthias Ringwald 
20703deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
2071*323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
20723deb3ec6SMatthias Ringwald }
20733deb3ec6SMatthias Ringwald 
2074d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
2075d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2076a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2077a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2078a33eb0c4SMilanka Ringwald         return;
2079a33eb0c4SMilanka Ringwald     }
2080a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2081a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20823deb3ec6SMatthias Ringwald }
20833deb3ec6SMatthias Ringwald 
2084d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
2085d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2086a0ffb263SMatthias Ringwald     if (!hfp_connection){
2087a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
20883deb3ec6SMatthias Ringwald         return;
20893deb3ec6SMatthias Ringwald     }
2090a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2091a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
20923deb3ec6SMatthias Ringwald         return;
20933deb3ec6SMatthias Ringwald     }
2094a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2095a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20963deb3ec6SMatthias Ringwald }
20973deb3ec6SMatthias Ringwald 
2098a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
20992ee45eb2SMilanka Ringwald     log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state);
2100a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2101a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
21023deb3ec6SMatthias Ringwald 
2103a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2104a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2105d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2106d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2107a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
21087522e673SMatthias Ringwald         // now, pick link settings
21097522e673SMatthias Ringwald         hfp_init_link_settings(hfp_connection);
21102ee45eb2SMilanka Ringwald         return;
21113deb3ec6SMatthias Ringwald     }
2112aa4dd815SMatthias Ringwald 
21132ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 1;
2114aa4dd815SMatthias Ringwald }
2115aa4dd815SMatthias Ringwald 
2116d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
2117d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2118a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2119a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2120a33eb0c4SMilanka Ringwald         return;
2121a33eb0c4SMilanka Ringwald     }
21222ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 0;
2123a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2124a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2125a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21263deb3ec6SMatthias Ringwald }
21273deb3ec6SMatthias Ringwald 
2128d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
2129d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2130a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2131a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2132a33eb0c4SMilanka Ringwald         return;
2133a33eb0c4SMilanka Ringwald     }
2134a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2135a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21363deb3ec6SMatthias Ringwald }
2137aa4dd815SMatthias Ringwald 
2138aa4dd815SMatthias Ringwald /**
2139aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2140aa4dd815SMatthias Ringwald  */
2141aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2142aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2143aa4dd815SMatthias Ringwald         return;
2144aa4dd815SMatthias Ringwald     }
2145aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2146aa4dd815SMatthias Ringwald 
2147665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2148665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2149665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2150a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2151a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2152a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2153aa4dd815SMatthias Ringwald     }
2154aa4dd815SMatthias Ringwald }
2155aa4dd815SMatthias Ringwald 
2156aa4dd815SMatthias Ringwald /**
2157aa4dd815SMatthias Ringwald  * @brief Called from GSM
2158aa4dd815SMatthias Ringwald  */
2159aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2160aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2161aa4dd815SMatthias Ringwald }
2162aa4dd815SMatthias Ringwald 
2163aa4dd815SMatthias Ringwald /**
2164aa4dd815SMatthias Ringwald  * @brief number is stored.
2165aa4dd815SMatthias Ringwald  */
2166aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2167d0c20769SMatthias Ringwald     hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number);
2168aa4dd815SMatthias Ringwald }
2169aa4dd815SMatthias Ringwald 
2170aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2171aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2172aa4dd815SMatthias Ringwald }
2173aa4dd815SMatthias Ringwald 
2174aa4dd815SMatthias Ringwald // call from AG UI
2175aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2176aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2177aa4dd815SMatthias Ringwald }
2178aa4dd815SMatthias Ringwald 
2179ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2180ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2181ce263fc8SMatthias Ringwald }
2182ce263fc8SMatthias Ringwald 
2183aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2184aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2185aa4dd815SMatthias Ringwald }
2186aa4dd815SMatthias Ringwald 
2187aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2188aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2189aa4dd815SMatthias Ringwald }
2190aa4dd815SMatthias Ringwald 
2191aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2192aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2193aa4dd815SMatthias Ringwald }
2194aa4dd815SMatthias Ringwald 
2195aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2196aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2197aa4dd815SMatthias Ringwald }
2198aa4dd815SMatthias Ringwald 
2199aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2200aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2201aa4dd815SMatthias Ringwald }
2202ce263fc8SMatthias Ringwald 
2203ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2204ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2205ce263fc8SMatthias Ringwald }
2206ce263fc8SMatthias Ringwald 
2207ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2208ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2209ce263fc8SMatthias Ringwald }
2210ce263fc8SMatthias Ringwald 
2211ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2212ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2213ce263fc8SMatthias Ringwald }
2214ce263fc8SMatthias Ringwald 
2215aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2216aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2217aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2218aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2219aa4dd815SMatthias Ringwald 
2220ce263fc8SMatthias Ringwald 
2221665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2222665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2223665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2224a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2225a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2226ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2227ce263fc8SMatthias Ringwald             continue;
2228ce263fc8SMatthias Ringwald         }
2229ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2230a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2231a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2232aa4dd815SMatthias Ringwald     }
2233aa4dd815SMatthias Ringwald }
2234aa4dd815SMatthias Ringwald 
2235aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2236aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2237aa4dd815SMatthias Ringwald }
2238aa4dd815SMatthias Ringwald 
2239aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2240aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2241aa4dd815SMatthias Ringwald }
2242aa4dd815SMatthias Ringwald 
2243aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2244aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2245aa4dd815SMatthias Ringwald }
2246aa4dd815SMatthias Ringwald 
2247aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2248aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2249aa4dd815SMatthias Ringwald }
2250aa4dd815SMatthias Ringwald 
2251d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){
2252aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
2253d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2254a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2255a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2256a33eb0c4SMilanka Ringwald         return;
2257a33eb0c4SMilanka Ringwald     }
2258aa4dd815SMatthias Ringwald 
2259a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
226060ebb071SMilanka Ringwald         log_info("AG cannot acivate voice recognition - not supported by HF");
2261aa4dd815SMatthias Ringwald         return;
2262aa4dd815SMatthias Ringwald     }
2263aa4dd815SMatthias Ringwald 
2264aa4dd815SMatthias Ringwald     if (activate){
2265d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(acl_handle);
2266aa4dd815SMatthias Ringwald     }
2267aa4dd815SMatthias Ringwald 
2268a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2269a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2270a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2271aa4dd815SMatthias Ringwald }
2272aa4dd815SMatthias Ringwald 
2273d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
2274d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2275a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2276a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2277a33eb0c4SMilanka Ringwald         return;
2278a33eb0c4SMilanka Ringwald     }
2279a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2280a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2281a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2282a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2283aa4dd815SMatthias Ringwald     }
2284a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2285aa4dd815SMatthias Ringwald }
2286aa4dd815SMatthias Ringwald 
2287d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
2288d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2289a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2290a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2291a33eb0c4SMilanka Ringwald         return;
2292a33eb0c4SMilanka Ringwald     }
2293a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2294a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2295a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2296aa4dd815SMatthias Ringwald     }
2297a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2298aa4dd815SMatthias Ringwald }
2299aa4dd815SMatthias Ringwald 
2300d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
2301d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2302a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2303a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2304a33eb0c4SMilanka Ringwald         return;
2305a33eb0c4SMilanka Ringwald     }
2306aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2307a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2308aa4dd815SMatthias Ringwald }
2309aa4dd815SMatthias Ringwald 
2310d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
2311d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2312a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2313a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2314a33eb0c4SMilanka Ringwald         return;
2315a33eb0c4SMilanka Ringwald     }
2316a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2317aa4dd815SMatthias Ringwald }
2318aa4dd815SMatthias Ringwald 
2319d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
2320d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2321a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2322a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2323a33eb0c4SMilanka Ringwald         return;
2324a33eb0c4SMilanka Ringwald     }
2325a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2326c1797c7dSMatthias Ringwald }
2327aa4dd815SMatthias Ringwald 
2328ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2329ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2330ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2331ce263fc8SMatthias Ringwald }
2332ce263fc8SMatthias Ringwald 
23339cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
23349cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2335ce263fc8SMatthias Ringwald }
2336ce263fc8SMatthias Ringwald 
2337d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(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     if (!hfp_connection->call_waiting_notification_enabled) return;
2344a0ffb263SMatthias Ringwald 
2345a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2346a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2347a0ffb263SMatthias Ringwald }
2348ce263fc8SMatthias Ringwald 
2349