xref: /btstack/src/classic/hfp_ag.c (revision 235946f152c66eb4ba735043adb492f7a538d15c)
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 
56*235946f1SMatthias 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);
73c5b64319SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *context);
74c5b64319SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * connection);
75c5b64319SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * context);
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 static void hfp_run_for_context(hfp_connection_t *hfp_connection);
11299a10067SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
11399a10067SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
11499a10067SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection);
1153deb3ec6SMatthias Ringwald 
1163deb3ec6SMatthias Ringwald 
117a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
118a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
119a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
120a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1213deb3ec6SMatthias Ringwald     }
122a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
123a0ffb263SMatthias Ringwald }
124a0ffb263SMatthias Ringwald 
125a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
126a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
127a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
128a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
129a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
130a0ffb263SMatthias Ringwald     }
131a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1323deb3ec6SMatthias Ringwald }
1333deb3ec6SMatthias Ringwald 
134aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
135aa4dd815SMatthias Ringwald     int i;
136aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
137aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
138aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
139aa4dd815SMatthias Ringwald         }
140aa4dd815SMatthias Ringwald     }
141aa4dd815SMatthias Ringwald     return NULL;
142aa4dd815SMatthias Ringwald }
143aa4dd815SMatthias Ringwald 
144aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
145aa4dd815SMatthias Ringwald     int i;
146aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
147aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
148aa4dd815SMatthias Ringwald             return i;
149aa4dd815SMatthias Ringwald         }
150aa4dd815SMatthias Ringwald     }
151aa4dd815SMatthias Ringwald     return -1;
152aa4dd815SMatthias Ringwald }
153aa4dd815SMatthias Ringwald 
1543deb3ec6SMatthias Ringwald 
15513839019SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
1563deb3ec6SMatthias Ringwald     if (callback == NULL){
1573deb3ec6SMatthias Ringwald         log_error("hfp_ag_register_packet_handler called with NULL callback");
1583deb3ec6SMatthias Ringwald         return;
1593deb3ec6SMatthias Ringwald     }
1603deb3ec6SMatthias Ringwald     hfp_callback = callback;
161f4000eebSMatthias Ringwald     hfp_set_callback(callback);
1623deb3ec6SMatthias Ringwald }
1633deb3ec6SMatthias Ringwald 
1640cb5b971SMatthias Ringwald static int use_in_band_tone(void){
165aa4dd815SMatthias Ringwald     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1663deb3ec6SMatthias Ringwald }
1673deb3ec6SMatthias Ringwald 
168a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
169a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1703deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1713deb3ec6SMatthias Ringwald     return hf && ag;
1723deb3ec6SMatthias Ringwald }
1733deb3ec6SMatthias Ringwald 
174a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
175a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1763deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1773deb3ec6SMatthias Ringwald     return hf && ag;
1783deb3ec6SMatthias Ringwald }
1793deb3ec6SMatthias Ringwald 
180a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
181a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
1823deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
1833deb3ec6SMatthias Ringwald     return hf && ag;
1843deb3ec6SMatthias Ringwald }
1853deb3ec6SMatthias Ringwald 
1864f84bf36SMatthias 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){
1873deb3ec6SMatthias Ringwald     if (!name){
1883deb3ec6SMatthias Ringwald         name = default_hfp_ag_service_name;
1893deb3ec6SMatthias Ringwald     }
190*235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
1913deb3ec6SMatthias Ringwald 
1923deb3ec6SMatthias Ringwald     /*
1933deb3ec6SMatthias Ringwald      * 0x01 – Ability to reject a call
1943deb3ec6SMatthias Ringwald      * 0x00 – No ability to reject a call
1953deb3ec6SMatthias Ringwald      */
196aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
197aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
198aa4dd815SMatthias Ringwald 
1994f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
2004f84bf36SMatthias Ringwald     //
2014f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
2024f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
2034f84bf36SMatthias Ringwald     //
2044f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features &0x1f;
2054f84bf36SMatthias Ringwald     if (supported_features & wide_band_speech){
2064f84bf36SMatthias Ringwald         sdp_features |= 1 << 5; // Wide band speech bit
2074f84bf36SMatthias Ringwald     }
208aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
2094f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
210aa4dd815SMatthias Ringwald }
211aa4dd815SMatthias Ringwald 
212aa4dd815SMatthias Ringwald static int hfp_ag_change_in_band_ring_tone_setting_cmd(uint16_t cid){
213aa4dd815SMatthias Ringwald     char buffer[20];
214aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
215aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2163deb3ec6SMatthias Ringwald }
2173deb3ec6SMatthias Ringwald 
2183deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2193deb3ec6SMatthias Ringwald     char buffer[40];
2203deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
2213deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2223deb3ec6SMatthias Ringwald }
2233deb3ec6SMatthias Ringwald 
2243deb3ec6SMatthias Ringwald static int hfp_ag_ok(uint16_t cid){
2253deb3ec6SMatthias Ringwald     char buffer[10];
2263deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nOK\r\n");
2273deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2283deb3ec6SMatthias Ringwald }
2293deb3ec6SMatthias Ringwald 
230aa4dd815SMatthias Ringwald static int hfp_ag_ring(uint16_t cid){
231aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
232aa4dd815SMatthias Ringwald }
233aa4dd815SMatthias Ringwald 
234aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
235aa4dd815SMatthias Ringwald     char buffer[50];
236d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
237aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
238aa4dd815SMatthias Ringwald }
239aa4dd815SMatthias Ringwald 
240ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
241ce263fc8SMatthias Ringwald     char buffer[50];
242ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
243ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
244ce263fc8SMatthias Ringwald }
245ce263fc8SMatthias Ringwald 
246c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
247aa4dd815SMatthias Ringwald     char buffer[50];
248d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
249aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
250aa4dd815SMatthias Ringwald }
251aa4dd815SMatthias Ringwald 
252aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
253aa4dd815SMatthias Ringwald     char buffer[50];
254a0ffb263SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
255aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
256aa4dd815SMatthias Ringwald }
257aa4dd815SMatthias Ringwald 
2583deb3ec6SMatthias Ringwald static int hfp_ag_error(uint16_t cid){
2593deb3ec6SMatthias Ringwald     char buffer[10];
2603deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nERROR\r\n");
2613deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2623deb3ec6SMatthias Ringwald }
2633deb3ec6SMatthias Ringwald 
2643deb3ec6SMatthias Ringwald static int hfp_ag_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2653deb3ec6SMatthias Ringwald     char buffer[20];
2663deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2673deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2683deb3ec6SMatthias Ringwald }
2693deb3ec6SMatthias Ringwald 
270ad902e3dSMatthias Ringwald // fast & small implementation for fixed int size
271ad902e3dSMatthias Ringwald static int string_len_for_uint32(uint32_t i){
272ad902e3dSMatthias Ringwald     if (i <         10) return 1;
273ad902e3dSMatthias Ringwald     if (i <        100) return 2;
274ad902e3dSMatthias Ringwald     if (i <       1000) return 3;
275ad902e3dSMatthias Ringwald     if (i <      10000) return 4;
27674386ee0SMatthias Ringwald     if (i <     100000) return 5;
277ad902e3dSMatthias Ringwald     if (i <    1000000) return 6;
278ad902e3dSMatthias Ringwald     if (i <   10000000) return 7;
279ad902e3dSMatthias Ringwald     if (i <  100000000) return 8;
280ad902e3dSMatthias Ringwald     if (i < 1000000000) return 9;
281ad902e3dSMatthias Ringwald     return 10;
282ad902e3dSMatthias Ringwald }
283ad902e3dSMatthias Ringwald 
284ad902e3dSMatthias Ringwald // get size for indicator string
285a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
286ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
28745718b6fSMatthias Ringwald     return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
288a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
289a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
290ad902e3dSMatthias Ringwald }
291ad902e3dSMatthias Ringwald 
292ad902e3dSMatthias Ringwald // store indicator
293a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
294ad902e3dSMatthias Ringwald     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
295a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
296a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
297a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2983deb3ec6SMatthias Ringwald }
299ad902e3dSMatthias Ringwald 
300ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
301a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
302a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
303ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
304ad902e3dSMatthias Ringwald     return 3 + (num_indicators-1) * 2;
3053deb3ec6SMatthias Ringwald }
306ad902e3dSMatthias Ringwald 
307ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
308a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
309ad902e3dSMatthias Ringwald     if (index == 0) {
310ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
311ad902e3dSMatthias Ringwald     }
312ad902e3dSMatthias Ringwald     index--;
313a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
314ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
315ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
316a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
317ad902e3dSMatthias Ringwald     }
318ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators - 1){
319ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
320ad902e3dSMatthias Ringwald     }
321ad902e3dSMatthias Ringwald     return 1; // comma
322ad902e3dSMatthias Ringwald }
323ad902e3dSMatthias Ringwald 
324a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
325ad902e3dSMatthias Ringwald     if (index == 0){
326ad902e3dSMatthias Ringwald         *buffer++ = '\r';
32774386ee0SMatthias Ringwald         *buffer++ = '\n';
328ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
329ad902e3dSMatthias Ringwald         memcpy(buffer, HFP_INDICATOR, len);
330ad902e3dSMatthias Ringwald         buffer += len;
331ad902e3dSMatthias Ringwald         *buffer++ = ':';
332ad902e3dSMatthias Ringwald         return;
333ad902e3dSMatthias Ringwald     }
334ad902e3dSMatthias Ringwald     index--;
335a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
336ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
337ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
338a0ffb263SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
339ad902e3dSMatthias Ringwald         return;
340ad902e3dSMatthias Ringwald     }
341ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators-1){
342ad902e3dSMatthias Ringwald         memcpy(buffer, "\r\n\r\nOK\r\n", 8);
343ad902e3dSMatthias Ringwald         return;
344ad902e3dSMatthias Ringwald     }
345ad902e3dSMatthias Ringwald     *buffer = ',';
3463deb3ec6SMatthias Ringwald }
3473deb3ec6SMatthias Ringwald 
3483deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
3493deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3503deb3ec6SMatthias Ringwald     int i;
3513deb3ec6SMatthias Ringwald     int offset = 0;
352a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr-1; i++) {
353a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3543deb3ec6SMatthias Ringwald     }
355a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
356a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3573deb3ec6SMatthias Ringwald     }
3583deb3ec6SMatthias Ringwald     return offset;
3593deb3ec6SMatthias Ringwald }
3603deb3ec6SMatthias Ringwald 
3613deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
362a0ffb263SMatthias Ringwald     if (buffer_size < hfp_generic_status_indicators_nr * 3) return 0;
3633deb3ec6SMatthias Ringwald     int i;
3643deb3ec6SMatthias Ringwald     int offset = 0;
365a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
366a0ffb263SMatthias 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);
3673deb3ec6SMatthias Ringwald     }
3683deb3ec6SMatthias Ringwald     return offset;
3693deb3ec6SMatthias Ringwald }
3703deb3ec6SMatthias Ringwald 
3713deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
3723deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3733deb3ec6SMatthias Ringwald     int i;
3743deb3ec6SMatthias Ringwald     int offset = 0;
3753deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr-1; i++) {
3763deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3773deb3ec6SMatthias Ringwald     }
3783deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3793deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3803deb3ec6SMatthias Ringwald     }
3813deb3ec6SMatthias Ringwald     return offset;
3823deb3ec6SMatthias Ringwald }
3833deb3ec6SMatthias Ringwald 
3843deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
3853deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0;
3863deb3ec6SMatthias Ringwald     int i;
3873deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
3883deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) {
3893deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3903deb3ec6SMatthias Ringwald     }
3913deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3923deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3933deb3ec6SMatthias Ringwald     }
3943deb3ec6SMatthias Ringwald     return offset;
3953deb3ec6SMatthias Ringwald }
3963deb3ec6SMatthias Ringwald 
397a0ffb263SMatthias Ringwald static int hfp_ag_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
398ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
399a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
400a0ffb263SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
4013deb3ec6SMatthias Ringwald 
402ad902e3dSMatthias Ringwald     // assumes: can send now == true
403ad902e3dSMatthias Ringwald     // assumes: num segments > 0
404aba39421SMatthias Ringwald     // assumes: individual segments are smaller than MTU
405ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
406ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
407ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
408ad902e3dSMatthias Ringwald     int offset = 0;
409ad902e3dSMatthias Ringwald     int segment = start_segment;
410ad902e3dSMatthias Ringwald     while (segment < num_segments){
411a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
412aba39421SMatthias Ringwald         if (offset + segment_len > mtu) break;
413aba39421SMatthias Ringwald         // append segement
414a0ffb263SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset);
415ad902e3dSMatthias Ringwald         offset += segment_len;
416ad902e3dSMatthias Ringwald         segment++;
417ad902e3dSMatthias Ringwald     }
418ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
419ad902e3dSMatthias Ringwald     return segment;
420ad902e3dSMatthias Ringwald }
4213deb3ec6SMatthias Ringwald 
422ad902e3dSMatthias Ringwald // returns next segment to store
423a0ffb263SMatthias Ringwald static int hfp_ag_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
424a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
425a0ffb263SMatthias Ringwald     return hfp_ag_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
426ad902e3dSMatthias Ringwald         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
4273deb3ec6SMatthias Ringwald }
4283deb3ec6SMatthias Ringwald 
4293deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_indicators_status_cmd(uint16_t cid){
4303deb3ec6SMatthias Ringwald     char buffer[40];
4313deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_INDICATOR);
4323deb3ec6SMatthias Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, sizeof(buffer)-offset);
4333deb3ec6SMatthias Ringwald 
4343deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4353deb3ec6SMatthias Ringwald 
4363deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4373deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4383deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4393deb3ec6SMatthias Ringwald }
4403deb3ec6SMatthias Ringwald 
4413deb3ec6SMatthias Ringwald static int hfp_ag_set_indicator_status_update_cmd(uint16_t cid, uint8_t activate){
4429ec2630cSMatthias Ringwald     UNUSED(activate);
4439ec2630cSMatthias Ringwald 
4443deb3ec6SMatthias Ringwald     // AT\r\n%s:3,0,0,%d\r\n
4453deb3ec6SMatthias Ringwald     return hfp_ag_ok(cid);
4463deb3ec6SMatthias Ringwald }
4473deb3ec6SMatthias Ringwald 
4483deb3ec6SMatthias Ringwald 
4493deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_can_hold_call_cmd(uint16_t cid){
450ad902e3dSMatthias Ringwald     char buffer[40];
4513deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
4523deb3ec6SMatthias Ringwald     offset += hfp_ag_call_services_join(buffer+offset, sizeof(buffer)-offset);
4533deb3ec6SMatthias Ringwald 
4543deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4553deb3ec6SMatthias Ringwald 
4563deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4573deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4583deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4593deb3ec6SMatthias Ringwald }
4603deb3ec6SMatthias Ringwald 
4613deb3ec6SMatthias Ringwald 
4623deb3ec6SMatthias Ringwald static int hfp_ag_list_supported_generic_status_indicators_cmd(uint16_t cid){
4633deb3ec6SMatthias Ringwald     return hfp_ag_ok(cid);
4643deb3ec6SMatthias Ringwald }
4653deb3ec6SMatthias Ringwald 
4663deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4673deb3ec6SMatthias Ringwald     char buffer[40];
468aa4dd815SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
4693deb3ec6SMatthias Ringwald     offset += hfp_hf_indicators_join(buffer+offset, sizeof(buffer)-offset);
4703deb3ec6SMatthias Ringwald 
4713deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4723deb3ec6SMatthias Ringwald 
473aa4dd815SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, ")\r\n\r\nOK\r\n");
4743deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4753deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4763deb3ec6SMatthias Ringwald }
4773deb3ec6SMatthias Ringwald 
4783deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4793deb3ec6SMatthias Ringwald     char buffer[40];
4803deb3ec6SMatthias Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer));
4813deb3ec6SMatthias Ringwald 
4823deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4833deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4843deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4853deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4863deb3ec6SMatthias Ringwald }
4873deb3ec6SMatthias Ringwald 
488aa4dd815SMatthias Ringwald static int hfp_ag_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4893deb3ec6SMatthias Ringwald     char buffer[20];
490aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
4913deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4923deb3ec6SMatthias Ringwald }
4933deb3ec6SMatthias Ringwald 
4943deb3ec6SMatthias Ringwald static int hfp_ag_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4953deb3ec6SMatthias Ringwald     char buffer[40];
4963deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
4973deb3ec6SMatthias Ringwald         sprintf(buffer, "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4983deb3ec6SMatthias Ringwald     } else {
4993deb3ec6SMatthias Ringwald         sprintf(buffer, "\r\n%s:%d,%d,%s\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format, op.name);
5003deb3ec6SMatthias Ringwald     }
5013deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
5023deb3ec6SMatthias Ringwald }
5033deb3ec6SMatthias Ringwald 
5043deb3ec6SMatthias Ringwald 
5053deb3ec6SMatthias Ringwald static int hfp_ag_cmd_suggest_codec(uint16_t cid, uint8_t codec){
5063deb3ec6SMatthias Ringwald     char buffer[30];
5073deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec);
5083deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
5093deb3ec6SMatthias Ringwald }
5103deb3ec6SMatthias Ringwald 
511aa4dd815SMatthias Ringwald static int hfp_ag_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
512aa4dd815SMatthias Ringwald     char buffer[30];
513aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
514aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
515aa4dd815SMatthias Ringwald }
516aa4dd815SMatthias Ringwald 
517aa4dd815SMatthias Ringwald static int hfp_ag_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
518aa4dd815SMatthias Ringwald     char buffer[30];
519aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_SPEAKER_GAIN, gain);
520aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
521aa4dd815SMatthias Ringwald }
522aa4dd815SMatthias Ringwald 
523aa4dd815SMatthias Ringwald static int hfp_ag_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
524aa4dd815SMatthias Ringwald     char buffer[30];
525aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_MICROPHONE_GAIN, gain);
526aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
527aa4dd815SMatthias Ringwald }
528aa4dd815SMatthias Ringwald 
529ce263fc8SMatthias Ringwald static int hfp_ag_set_response_and_hold(uint16_t cid, int state){
530ce263fc8SMatthias Ringwald     char buffer[30];
531ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: %d\r\n", HFP_RESPONSE_AND_HOLD, state);
532ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
533ce263fc8SMatthias Ringwald }
534ce263fc8SMatthias Ringwald 
535a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
536bc1b1537SMilanka Ringwald     if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
537bc1b1537SMilanka Ringwald 
5386a7f44bdSMilanka Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){
5396a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
540df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
5413deb3ec6SMatthias Ringwald         }
5423deb3ec6SMatthias Ringwald     }
543df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
5443deb3ec6SMatthias Ringwald }
5453deb3ec6SMatthias Ringwald 
5467522e673SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){
5477522e673SMatthias Ringwald     // determine highest possible link setting
5487522e673SMatthias Ringwald     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
549d9f77559SMatthias Ringwald     // anything else requires eSCO support on both sides
550d9f77559SMatthias Ringwald     if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
5517522e673SMatthias Ringwald         switch (hfp_connection->negotiated_codec){
5527522e673SMatthias Ringwald             case HFP_CODEC_CVSD:
5537522e673SMatthias Ringwald                 hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
5547522e673SMatthias Ringwald                 if ((hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4))
5557522e673SMatthias Ringwald                 &&  (hfp_supported_features                    & (1<<HFP_AGSF_ESCO_S4))){
5567522e673SMatthias Ringwald                     hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
5577522e673SMatthias Ringwald                 }
5587522e673SMatthias Ringwald                 break;
5597522e673SMatthias Ringwald             case HFP_CODEC_MSBC:
5607522e673SMatthias Ringwald                 hfp_connection->link_setting = HFP_LINK_SETTINGS_T2;
5617522e673SMatthias Ringwald                 break;
562d9f77559SMatthias Ringwald             default:
563d9f77559SMatthias Ringwald                 break;
564d9f77559SMatthias Ringwald         }
5657522e673SMatthias Ringwald     }
5667522e673SMatthias Ringwald     log_info("hfp_init_link_settings: %u", hfp_connection->link_setting);
5677522e673SMatthias Ringwald }
5687522e673SMatthias Ringwald 
569a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
570aa4dd815SMatthias Ringwald     /* events ( == commands):
571aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
572aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
573aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
574aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
575aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
576aa4dd815SMatthias Ringwald     */
577aa4dd815SMatthias Ringwald 
578a0ffb263SMatthias Ringwald      switch (hfp_connection->codecs_state){
579aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
580a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
581aa4dd815SMatthias Ringwald             break;
582aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
583a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
584aa4dd815SMatthias Ringwald             break;
585aa4dd815SMatthias Ringwald         default:
586aa4dd815SMatthias Ringwald             break;
587aa4dd815SMatthias Ringwald     }
588aa4dd815SMatthias Ringwald 
589aa4dd815SMatthias Ringwald     // printf(" -> State machine: CC\n");
590aa4dd815SMatthias Ringwald 
591a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
592aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
593a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
594a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
595a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
596aa4dd815SMatthias Ringwald                 return 1;
597aa4dd815SMatthias Ringwald             }
598aa4dd815SMatthias Ringwald 
599a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
600aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
601aa4dd815SMatthias Ringwald                 case HFP_CODECS_EXCHANGED:
602a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
603aa4dd815SMatthias Ringwald                     break;
604aa4dd815SMatthias Ringwald                 default:
605aa4dd815SMatthias Ringwald                     break;
606aa4dd815SMatthias Ringwald             }
607a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
608aa4dd815SMatthias Ringwald             return 1;
609aa4dd815SMatthias Ringwald 
610aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
611a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
612a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
613aa4dd815SMatthias Ringwald             return 1;
614aa4dd815SMatthias Ringwald 
615aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
616a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
617a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
618a0ffb263SMatthias Ringwald             hfp_ag_cmd_suggest_codec(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
619aa4dd815SMatthias Ringwald             return 1;
620aa4dd815SMatthias Ringwald 
621aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
622a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
623a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
624a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
625aa4dd815SMatthias Ringwald                 return 1;
626aa4dd815SMatthias Ringwald             }
627a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
628a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
62903645b44SMatthias Ringwald             log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
630a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
6317522e673SMatthias Ringwald             // now, pick link settings
6327522e673SMatthias Ringwald             hfp_init_link_settings(hfp_connection);
633aa4dd815SMatthias Ringwald             return 1;
634aa4dd815SMatthias Ringwald         default:
635aa4dd815SMatthias Ringwald             break;
636aa4dd815SMatthias Ringwald     }
637aa4dd815SMatthias Ringwald     return 0;
638aa4dd815SMatthias Ringwald }
639aa4dd815SMatthias Ringwald 
640a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
641a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
642d0c4aea6SMilanka Ringwald     hfp_emit_slc_connection_event(hfp_callback, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
643aa4dd815SMatthias Ringwald 
644a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
645d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
646a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
647aa4dd815SMatthias Ringwald     }
648ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
649d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS &&
650d0c20769SMatthias Ringwald         hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
651a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
652ce263fc8SMatthias Ringwald     }
653aa4dd815SMatthias Ringwald }
6543deb3ec6SMatthias Ringwald 
655a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
656c79b4cabSMatthias Ringwald     // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
657a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
6583deb3ec6SMatthias Ringwald     int done = 0;
659a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
6603deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
661a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
6623deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
663aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
664d715cf51SMatthias Ringwald                     hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
665a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
666a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
667aa4dd815SMatthias Ringwald                     } else {
668a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
669aa4dd815SMatthias Ringwald                     }
670a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
671aa4dd815SMatthias Ringwald                     return 1;
6723deb3ec6SMatthias Ringwald                 default:
6733deb3ec6SMatthias Ringwald                     break;
6743deb3ec6SMatthias Ringwald             }
6753deb3ec6SMatthias Ringwald             break;
6763deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
677a0ffb263SMatthias Ringwald             done = codecs_exchange_state_machine(hfp_connection);
6783deb3ec6SMatthias Ringwald 
679a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
680a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6813deb3ec6SMatthias Ringwald             }
682aa4dd815SMatthias Ringwald             return done;
683aa4dd815SMatthias Ringwald 
684aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
685a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
686473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
687473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
688473ac565SMatthias Ringwald                 // continue below in state switch
689ad902e3dSMatthias Ringwald             }
690ad902e3dSMatthias Ringwald             break;
691aa4dd815SMatthias Ringwald 
692aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
693a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
694a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
695a0ffb263SMatthias Ringwald             hfp_ag_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
696aa4dd815SMatthias Ringwald             return 1;
697aa4dd815SMatthias Ringwald 
6983deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
699a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
700a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
701a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
702a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
703a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
704aa4dd815SMatthias Ringwald             } else {
705a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
7063deb3ec6SMatthias Ringwald             }
707a0ffb263SMatthias Ringwald             hfp_ag_set_indicator_status_update_cmd(hfp_connection->rfcomm_cid, 1);
708aa4dd815SMatthias Ringwald             return 1;
7093deb3ec6SMatthias Ringwald 
710aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
711a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
712a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
713a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
714aa4dd815SMatthias Ringwald             }
715a0ffb263SMatthias Ringwald             hfp_ag_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
716a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
717a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
718ce263fc8SMatthias Ringwald             }
719aa4dd815SMatthias Ringwald             return 1;
720aa4dd815SMatthias Ringwald 
721aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
722a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
723a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
724a0ffb263SMatthias Ringwald             hfp_ag_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
725aa4dd815SMatthias Ringwald             return 1;
726aa4dd815SMatthias Ringwald 
727aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
728a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
729a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
730a0ffb263SMatthias Ringwald             hfp_ag_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
731aa4dd815SMatthias Ringwald             return 1;
732aa4dd815SMatthias Ringwald 
733aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
734a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
735a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
736a0ffb263SMatthias Ringwald             hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
737aa4dd815SMatthias Ringwald             return 1;
7383deb3ec6SMatthias Ringwald         default:
7393deb3ec6SMatthias Ringwald             break;
7403deb3ec6SMatthias Ringwald     }
741473ac565SMatthias Ringwald 
742473ac565SMatthias Ringwald     switch (hfp_connection->state){
743473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
744473ac565SMatthias Ringwald             int next_segment = hfp_ag_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
745473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
746473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
747473ac565SMatthias Ringwald             if (next_segment < num_segments){
748473ac565SMatthias Ringwald                 // prepare sending of next segment
749473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
750473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
751473ac565SMatthias Ringwald             } else {
752473ac565SMatthias Ringwald                 // done, go to next state
753473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
754473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
755473ac565SMatthias Ringwald             }
756473ac565SMatthias Ringwald             return 1;
757473ac565SMatthias Ringwald         }
758473ac565SMatthias Ringwald         default:
759473ac565SMatthias Ringwald             break;
760473ac565SMatthias Ringwald     }
761473ac565SMatthias Ringwald 
7623deb3ec6SMatthias Ringwald     return done;
7633deb3ec6SMatthias Ringwald }
7643deb3ec6SMatthias Ringwald 
765a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
7663deb3ec6SMatthias Ringwald 
767a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
768aa4dd815SMatthias Ringwald     if (done) return done;
769aa4dd815SMatthias Ringwald 
770a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
771aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
772a0ffb263SMatthias Ringwald             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
773a0ffb263SMatthias Ringwald             hfp_ag_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
774aa4dd815SMatthias Ringwald             return 1;
775aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
776aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
777a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
778a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
779a0ffb263SMatthias Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
780aa4dd815SMatthias Ringwald             } else {
781a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
782aa4dd815SMatthias Ringwald             }
783aa4dd815SMatthias Ringwald             return 1;
784aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
785a0ffb263SMatthias Ringwald             hfp_ag_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
786aa4dd815SMatthias Ringwald             return 1;
787aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
788a0ffb263SMatthias Ringwald             hfp_ag_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
789aa4dd815SMatthias Ringwald             return 1;
790aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
791a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
792a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
793aa4dd815SMatthias Ringwald             } else {
794a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
7953deb3ec6SMatthias Ringwald             }
796aa4dd815SMatthias Ringwald             return 1;
797aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
798a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
799aa4dd815SMatthias Ringwald             return 1;
8003deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
801a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
802a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
803a0ffb263SMatthias Ringwald                 hfp_ag_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
804aa4dd815SMatthias Ringwald                 return 1;
8053deb3ec6SMatthias Ringwald             }
806202c8a4cSMatthias Ringwald             break;
8073deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
808a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
809ce263fc8SMatthias Ringwald             return 1;
8103deb3ec6SMatthias Ringwald         default:
8113deb3ec6SMatthias Ringwald             break;
8123deb3ec6SMatthias Ringwald     }
813aa4dd815SMatthias Ringwald     return 0;
8143deb3ec6SMatthias Ringwald }
8153deb3ec6SMatthias Ringwald 
816a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
817a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
818a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
8193deb3ec6SMatthias Ringwald 
820aa4dd815SMatthias Ringwald 
821a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
822a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
823a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
824a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
825aa4dd815SMatthias Ringwald         return 1;
826aa4dd815SMatthias Ringwald     }
827aa4dd815SMatthias Ringwald 
828a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
829aa4dd815SMatthias Ringwald 
830aa4dd815SMatthias Ringwald     // run codecs exchange
831a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
832aa4dd815SMatthias Ringwald     if (done) return done;
833aa4dd815SMatthias Ringwald 
834a0ffb263SMatthias Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return done;
835a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
836a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
837a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
838eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
839aa4dd815SMatthias Ringwald         return 1;
840aa4dd815SMatthias Ringwald     }
841aa4dd815SMatthias Ringwald     return 0;
842aa4dd815SMatthias Ringwald }
843aa4dd815SMatthias Ringwald 
844ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
845665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
846665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
847aa4dd815SMatthias Ringwald 
848665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
849a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
850a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
851a0ffb263SMatthias Ringwald             return hfp_connection;
852aa4dd815SMatthias Ringwald         }
853aa4dd815SMatthias Ringwald     }
854aa4dd815SMatthias Ringwald     return NULL;
855aa4dd815SMatthias Ringwald }
856aa4dd815SMatthias Ringwald 
857ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
858d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
859d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
860aa4dd815SMatthias Ringwald 
861d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
862a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
863a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
864aa4dd815SMatthias Ringwald 
865a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
866a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
867aa4dd815SMatthias Ringwald 
868a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
869aa4dd815SMatthias Ringwald }
870aa4dd815SMatthias Ringwald 
871a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
872a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
873a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
874a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
875a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
876aa4dd815SMatthias Ringwald }
877aa4dd815SMatthias Ringwald 
878a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
879a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
880a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
881aa4dd815SMatthias Ringwald }
882aa4dd815SMatthias Ringwald 
883aa4dd815SMatthias Ringwald //
884aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
885aa4dd815SMatthias Ringwald //
886aa4dd815SMatthias Ringwald 
887a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
888aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
889a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
890d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
891aa4dd815SMatthias Ringwald     } else {
892a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
893a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
894a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
895a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
896a0ffb263SMatthias Ringwald         hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG);
897aa4dd815SMatthias Ringwald     }
898aa4dd815SMatthias Ringwald }
899aa4dd815SMatthias Ringwald 
900a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
901a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
902a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
903a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
904a0ffb263SMatthias Ringwald     hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_STOP_RINGINIG);
905aa4dd815SMatthias Ringwald }
906aa4dd815SMatthias Ringwald 
907aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
908aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
909aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
910aa4dd815SMatthias Ringwald 
911665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
912665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
913665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
914a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
915a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
916a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
917a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
918d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
919aa4dd815SMatthias Ringwald         }
920a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
921a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
922aa4dd815SMatthias Ringwald         }
923a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
924aa4dd815SMatthias Ringwald     }
925aa4dd815SMatthias Ringwald }
926aa4dd815SMatthias Ringwald 
927aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
928aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
929aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
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         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
936a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
937a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
938aa4dd815SMatthias Ringwald     }
939aa4dd815SMatthias Ringwald }
940aa4dd815SMatthias Ringwald 
941aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
942aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("call");
943aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
944aa4dd815SMatthias Ringwald 
945665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
946665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
947665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
948a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
949a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
950a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
951a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
952aa4dd815SMatthias Ringwald     }
953aa4dd815SMatthias Ringwald }
954aa4dd815SMatthias Ringwald 
955aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
956aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callheld");
957aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
958aa4dd815SMatthias Ringwald 
959665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
960665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
961665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
962a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
963a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
964a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
965a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
966aa4dd815SMatthias Ringwald     }
967aa4dd815SMatthias Ringwald }
968aa4dd815SMatthias Ringwald 
969aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
970aa4dd815SMatthias Ringwald 
971aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
972aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
973aa4dd815SMatthias Ringwald 
974665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
975665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
976665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
977a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
978a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
979a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
980aa4dd815SMatthias Ringwald 
981a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
982a0ffb263SMatthias Ringwald         if (hfp_connection == source){
983a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
984aa4dd815SMatthias Ringwald 
985aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
986a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
987aa4dd815SMatthias Ringwald             } else {
988a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
989d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
990aa4dd815SMatthias Ringwald             }
991aa4dd815SMatthias Ringwald 
992a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
993a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
994aa4dd815SMatthias Ringwald 
995aa4dd815SMatthias Ringwald         } else {
996a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
997aa4dd815SMatthias Ringwald         }
998a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
999aa4dd815SMatthias Ringwald     }
1000aa4dd815SMatthias Ringwald }
1001aa4dd815SMatthias Ringwald 
1002aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
1003aa4dd815SMatthias Ringwald 
1004aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1005aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1006aa4dd815SMatthias Ringwald 
1007665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1008665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1009665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1010a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1011a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
1012aa4dd815SMatthias Ringwald 
1013a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1014a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
1015aa4dd815SMatthias Ringwald 
1016a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1017a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1018aa4dd815SMatthias Ringwald 
1019a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1020aa4dd815SMatthias Ringwald         break;  // only single
1021aa4dd815SMatthias Ringwald     }
1022aa4dd815SMatthias Ringwald }
1023aa4dd815SMatthias Ringwald 
1024aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
1025aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1026665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1027665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1028665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1029665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1030aa4dd815SMatthias Ringwald         if (connection->call_state != HFP_CALL_RINGING &&
1031aa4dd815SMatthias Ringwald             connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1032aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
1033aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1034aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
1035aa4dd815SMatthias Ringwald         hfp_run_for_context(connection);
1036aa4dd815SMatthias Ringwald     }
1037aa4dd815SMatthias Ringwald }
1038aa4dd815SMatthias Ringwald 
1039aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1040aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1041aa4dd815SMatthias Ringwald 
1042665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1043665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1044665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1045a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1046d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1047a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1048a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1049a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1050a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 1;
1051a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1052aa4dd815SMatthias Ringwald     }
1053a0ffb263SMatthias Ringwald     hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_TERMINATED);
1054aa4dd815SMatthias Ringwald }
1055aa4dd815SMatthias Ringwald 
10560cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
1057aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1058aa4dd815SMatthias Ringwald     if (!indicator){
1059d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
106045718b6fSMatthias Ringwald         return;
1061aa4dd815SMatthias Ringwald     };
1062d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1063aa4dd815SMatthias Ringwald }
1064aa4dd815SMatthias Ringwald 
10650cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1066d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1067d210d9c4SMatthias Ringwald     if (!indicator){
1068d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
106945718b6fSMatthias Ringwald         return;
1070d210d9c4SMatthias Ringwald     };
1071d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1072d210d9c4SMatthias Ringwald }
1073d210d9c4SMatthias Ringwald 
10740cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1075aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1076aa4dd815SMatthias Ringwald     if (!indicator){
1077aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
107845718b6fSMatthias Ringwald         return;
1079aa4dd815SMatthias Ringwald     };
1080d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1081aa4dd815SMatthias Ringwald }
1082aa4dd815SMatthias Ringwald 
1083aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1084665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1085665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1086665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1087a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1088a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
1089a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1090a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1091aa4dd815SMatthias Ringwald     }
1092aa4dd815SMatthias Ringwald }
1093aa4dd815SMatthias Ringwald 
1094aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1095665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1096665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1097665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1098a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1099a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1100aa4dd815SMatthias Ringwald     }
1101aa4dd815SMatthias Ringwald     return NULL;
1102aa4dd815SMatthias Ringwald }
1103aa4dd815SMatthias Ringwald 
1104a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1105665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1106665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1107665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1108a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1109a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1110ce263fc8SMatthias Ringwald     }
1111ce263fc8SMatthias Ringwald }
1112ce263fc8SMatthias Ringwald 
1113a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1114aa4dd815SMatthias Ringwald     int indicator_index;
1115a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1116aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1117a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1118a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1119a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1120a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1121a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1122a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1123a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1124a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG);
1125aa4dd815SMatthias Ringwald             break;
1126aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1127a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1128a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1129a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1130aa4dd815SMatthias Ringwald             break;
1131aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1132a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1133a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1134aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1135a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1136aa4dd815SMatthias Ringwald             break;
1137aa4dd815SMatthias Ringwald         default:
1138aa4dd815SMatthias Ringwald             break;
1139aa4dd815SMatthias Ringwald     }
1140aa4dd815SMatthias Ringwald     return 0;
1141aa4dd815SMatthias Ringwald }
1142a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1143a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1144aa4dd815SMatthias Ringwald     int indicator_index;
1145d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1146d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1147d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
114874386ee0SMatthias Ringwald 
1149d210d9c4SMatthias Ringwald     //printf("hfp_ag_call_sm event %d \n", event);
1150aa4dd815SMatthias Ringwald     switch (event){
1151aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1152d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1153aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1154d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1155aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1156d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1157d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1158aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
115960ebb071SMilanka Ringwald                             log_info("AG rings");
1160aa4dd815SMatthias Ringwald                             break;
1161aa4dd815SMatthias Ringwald                         default:
11623deb3ec6SMatthias Ringwald                             break;
11633deb3ec6SMatthias Ringwald                     }
11643deb3ec6SMatthias Ringwald                     break;
1165aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1166d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1167aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1168d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1169d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1170aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
117160ebb071SMilanka Ringwald                             log_info("AG call waiting");
1172aa4dd815SMatthias Ringwald                             break;
1173aa4dd815SMatthias Ringwald                         default:
11743deb3ec6SMatthias Ringwald                             break;
11753deb3ec6SMatthias Ringwald                     }
11763deb3ec6SMatthias Ringwald                     break;
1177aa4dd815SMatthias Ringwald             }
1178aa4dd815SMatthias Ringwald             break;
1179aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1180d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1181aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1182d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1183aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1184d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1185d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1186d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1187aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
118860ebb071SMilanka Ringwald                             log_info("AG answers call, accept call by GSM");
1189aa4dd815SMatthias Ringwald                             break;
1190aa4dd815SMatthias Ringwald                         default:
11913deb3ec6SMatthias Ringwald                             break;
11923deb3ec6SMatthias Ringwald                     }
1193aa4dd815SMatthias Ringwald                     break;
1194aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1195d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1196aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
119760ebb071SMilanka Ringwald                             log_info("AG: current call is placed on hold, incoming call gets active");
1198d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1199d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1200d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1201ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1202ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1203aa4dd815SMatthias Ringwald                             break;
1204aa4dd815SMatthias Ringwald                         default:
1205aa4dd815SMatthias Ringwald                             break;
1206aa4dd815SMatthias Ringwald                     }
1207aa4dd815SMatthias Ringwald                     break;
1208aa4dd815SMatthias Ringwald             }
1209aa4dd815SMatthias Ringwald             break;
1210ce263fc8SMatthias Ringwald 
1211ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1212d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1213ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1214d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1215ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
121660ebb071SMilanka Ringwald                             log_info("AG: joining held call with active call");
1217d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG);
1218d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1219ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1220a0ffb263SMatthias Ringwald                             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL);
1221ce263fc8SMatthias Ringwald                             break;
1222ce263fc8SMatthias Ringwald                         default:
1223ce263fc8SMatthias Ringwald                             break;
1224ce263fc8SMatthias Ringwald                     }
1225ce263fc8SMatthias Ringwald                     break;
1226ce263fc8SMatthias Ringwald                 default:
1227ce263fc8SMatthias Ringwald                     break;
1228ce263fc8SMatthias Ringwald             }
1229ce263fc8SMatthias Ringwald             break;
1230ce263fc8SMatthias Ringwald 
1231aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1232d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1233aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1234d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1235aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1236d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF);
1237d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1238d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1239a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
124060ebb071SMilanka Ringwald                             log_info("HF answers call, accept call by GSM");
1241a0ffb263SMatthias Ringwald                             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_ANSWERED);
1242aa4dd815SMatthias Ringwald                             break;
1243aa4dd815SMatthias Ringwald                         default:
1244aa4dd815SMatthias Ringwald                             break;
1245aa4dd815SMatthias Ringwald                     }
12463deb3ec6SMatthias Ringwald                     break;
12473deb3ec6SMatthias Ringwald                 default:
12483deb3ec6SMatthias Ringwald                     break;
12493deb3ec6SMatthias Ringwald             }
12503deb3ec6SMatthias Ringwald             break;
12513deb3ec6SMatthias Ringwald 
1252ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1253d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1254ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1255d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1256ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1257d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG);
1258ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1259ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1260a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1261ce263fc8SMatthias Ringwald                             // as with regualr call
1262d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1263d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1264ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
126560ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by AG");
1266ce263fc8SMatthias Ringwald                             break;
1267ce263fc8SMatthias Ringwald                         default:
1268ce263fc8SMatthias Ringwald                             break;
1269ce263fc8SMatthias Ringwald                     }
1270ce263fc8SMatthias Ringwald                     break;
1271ce263fc8SMatthias Ringwald                 default:
1272ce263fc8SMatthias Ringwald                     break;
1273ce263fc8SMatthias Ringwald             }
1274ce263fc8SMatthias Ringwald             break;
1275ce263fc8SMatthias Ringwald 
1276ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1277d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1278ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1279d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1280ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1281d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF);
1282ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1283ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1284a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1285ce263fc8SMatthias Ringwald                             // as with regualr call
1286d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1287d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1288a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
128960ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by HF");
1290ce263fc8SMatthias Ringwald                             break;
1291ce263fc8SMatthias Ringwald                         default:
1292ce263fc8SMatthias Ringwald                             break;
1293ce263fc8SMatthias Ringwald                     }
1294ce263fc8SMatthias Ringwald                     break;
1295ce263fc8SMatthias Ringwald                 default:
1296ce263fc8SMatthias Ringwald                     break;
1297ce263fc8SMatthias Ringwald             }
1298ce263fc8SMatthias Ringwald             break;
1299ce263fc8SMatthias Ringwald 
1300ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1301ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1302ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1303ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1304d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG);
1305a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1306ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1307a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
130860ebb071SMilanka Ringwald             log_info("Held Call accepted and active");
1309ce263fc8SMatthias Ringwald             break;
1310ce263fc8SMatthias Ringwald 
1311ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1312ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1313ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1314ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1315d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG);
1316a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1317ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1318a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1319ce263fc8SMatthias Ringwald             // from terminate by ag
1320d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1321ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1322ce263fc8SMatthias Ringwald             break;
1323ce263fc8SMatthias Ringwald 
1324aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1325d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1326aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1327d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1328aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1329d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1330d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1331aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1332aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
133360ebb071SMilanka Ringwald                             log_info("HF Rejected Incoming call, AG terminate call");
1334aa4dd815SMatthias Ringwald                             break;
1335aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1336aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1337d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1338d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1339aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
134060ebb071SMilanka Ringwald                             log_info("AG terminate outgoing call process");
1341202c8a4cSMatthias Ringwald                             break;
1342aa4dd815SMatthias Ringwald                         default:
1343aa4dd815SMatthias Ringwald                             break;
1344aa4dd815SMatthias Ringwald                     }
1345aa4dd815SMatthias Ringwald                     break;
1346aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1347d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1348d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1349aa4dd815SMatthias Ringwald                     hfp_ag_transfer_call_state();
1350a0ffb263SMatthias Ringwald                     hfp_connection->call_state = HFP_CALL_IDLE;
135160ebb071SMilanka Ringwald                     log_info("AG terminate call");
1352aa4dd815SMatthias Ringwald                     break;
1353aa4dd815SMatthias Ringwald             }
1354aa4dd815SMatthias Ringwald             break;
13553deb3ec6SMatthias Ringwald 
1356aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1357d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1358aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1359d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1360aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1361d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1362d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1363aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
136460ebb071SMilanka Ringwald                             log_info("AG Rejected Incoming call, AG terminate call");
1365aa4dd815SMatthias Ringwald                             break;
1366aa4dd815SMatthias Ringwald                         default:
1367aa4dd815SMatthias Ringwald                             break;
13683deb3ec6SMatthias Ringwald                     }
1369202c8a4cSMatthias Ringwald                     break;
1370aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1371d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1372d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1373d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1374aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
137560ebb071SMilanka Ringwald                     log_info("AG terminate call");
1376aa4dd815SMatthias Ringwald                     break;
1377aa4dd815SMatthias Ringwald                 default:
13783deb3ec6SMatthias Ringwald                     break;
13793deb3ec6SMatthias Ringwald             }
13803deb3ec6SMatthias Ringwald             break;
1381aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1382d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1383aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1384d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1385aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1386aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
138760ebb071SMilanka Ringwald                             log_info("Incoming call interrupted");
1388aa4dd815SMatthias Ringwald                             break;
1389aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1390aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
139160ebb071SMilanka Ringwald                             log_info("Outgoing call interrupted\n");
139260ebb071SMilanka Ringwald                             log_info("AG notify call dropped\n");
1393aa4dd815SMatthias Ringwald                             break;
1394aa4dd815SMatthias Ringwald                         default:
13953deb3ec6SMatthias Ringwald                             break;
13963deb3ec6SMatthias Ringwald                     }
1397d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1398d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1399aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
1400aa4dd815SMatthias Ringwald                     break;
1401aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1402ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1403d210d9c4SMatthias Ringwald                         hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1404ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1405a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1406ce263fc8SMatthias Ringwald                     }
1407d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1408d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1409d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1410aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
141160ebb071SMilanka Ringwald                     log_info("AG notify call dropped");
1412aa4dd815SMatthias Ringwald                     break;
1413aa4dd815SMatthias Ringwald                 default:
1414aa4dd815SMatthias Ringwald                     break;
1415aa4dd815SMatthias Ringwald             }
1416aa4dd815SMatthias Ringwald             break;
1417aa4dd815SMatthias Ringwald 
1418aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1419d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1420d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1421a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1422a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1423d210d9c4SMatthias Ringwald                 break;
1424d210d9c4SMatthias Ringwald             }
1425a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]);
14269cae807eSMatthias Ringwald 
1427a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
142874386ee0SMatthias Ringwald 
1429a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1430aa4dd815SMatthias Ringwald             break;
1431aa4dd815SMatthias Ringwald 
14329cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1433d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1434d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1435a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1436a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1437d210d9c4SMatthias Ringwald                 break;
1438d210d9c4SMatthias Ringwald             }
1439d210d9c4SMatthias Ringwald 
144074386ee0SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED);
1441a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
144274386ee0SMatthias Ringwald 
144360ebb071SMilanka Ringwald             log_info("Redial last number");
14449cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1445aa4dd815SMatthias Ringwald 
14469cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
144760ebb071SMilanka Ringwald                 log_info("Last number exists: accept call");
14489cae807eSMatthias Ringwald                 hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
14499cae807eSMatthias Ringwald             } else {
145060ebb071SMilanka Ringwald                 log_info("log_infoLast number missing: reject call");
14519cae807eSMatthias Ringwald                 hfp_ag_outgoing_call_rejected();
14529cae807eSMatthias Ringwald             }
14539cae807eSMatthias Ringwald             break;
14549cae807eSMatthias Ringwald         }
1455aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
1456a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1457a0ffb263SMatthias Ringwald             if (!hfp_connection){
1458a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1459aa4dd815SMatthias Ringwald                 break;
1460aa4dd815SMatthias Ringwald             }
1461d210d9c4SMatthias Ringwald 
1462d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED);
1463a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1464a0ffb263SMatthias Ringwald             hfp_connection->send_error = 1;
1465a0ffb263SMatthias Ringwald             hfp_run_for_context(hfp_connection);
1466aa4dd815SMatthias Ringwald             break;
1467aa4dd815SMatthias Ringwald 
1468d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1469a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1470a0ffb263SMatthias Ringwald             if (!hfp_connection){
1471a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1472aa4dd815SMatthias Ringwald                 break;
1473aa4dd815SMatthias Ringwald             }
1474aa4dd815SMatthias Ringwald 
1475a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1476a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1477aa4dd815SMatthias Ringwald 
1478aa4dd815SMatthias Ringwald             // trigger callsetup to be
1479d0c20769SMatthias Ringwald             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1480d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED);
1481d210d9c4SMatthias Ringwald 
1482d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1483aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1484a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1485aa4dd815SMatthias Ringwald 
1486aa4dd815SMatthias Ringwald             // put current call on hold if active
1487d210d9c4SMatthias Ringwald             if (put_call_on_hold){
148860ebb071SMilanka Ringwald                 log_info("AG putting current call on hold for new outgoing calllog_info");
1489d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1490aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1491a0ffb263SMatthias Ringwald                 hfp_ag_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1492aa4dd815SMatthias Ringwald             }
1493aa4dd815SMatthias Ringwald 
1494aa4dd815SMatthias Ringwald             // start audio if needed
1495d97d752dSMilanka Ringwald             hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1496aa4dd815SMatthias Ringwald             break;
1497d210d9c4SMatthias Ringwald         }
1498aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1499a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1500a0ffb263SMatthias Ringwald             if (!hfp_connection){
1501a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1502aa4dd815SMatthias Ringwald                 break;
1503aa4dd815SMatthias Ringwald             }
1504d210d9c4SMatthias Ringwald 
1505d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING);
1506a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1507d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1508aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1509aa4dd815SMatthias Ringwald             break;
1510aa4dd815SMatthias Ringwald 
1511d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1512aa4dd815SMatthias Ringwald             // get outgoing call
1513a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1514a0ffb263SMatthias Ringwald             if (!hfp_connection){
1515a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1516aa4dd815SMatthias Ringwald             }
1517a0ffb263SMatthias Ringwald             if (!hfp_connection){
1518a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1519aa4dd815SMatthias Ringwald                 break;
1520aa4dd815SMatthias Ringwald             }
1521d210d9c4SMatthias Ringwald 
1522d0c20769SMatthias 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;
1523d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED);
1524a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1525d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1526d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1527aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1528aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1529d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1530d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1531aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
15323deb3ec6SMatthias Ringwald             }
15333deb3ec6SMatthias Ringwald             break;
1534d210d9c4SMatthias Ringwald         }
1535d210d9c4SMatthias Ringwald 
1536d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1537d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY);
1538d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1539a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1540a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
154160ebb071SMilanka Ringwald             log_info("AG: Call Waiting, User Busy");
1542d210d9c4SMatthias Ringwald             break;
1543d210d9c4SMatthias Ringwald 
1544d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1545d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1546d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1547d210d9c4SMatthias Ringwald 
1548d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1549d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1550a0ffb263SMatthias Ringwald                 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1551d0c20769SMatthias Ringwald 
1552d0c20769SMatthias Ringwald             }
1553d0c20769SMatthias Ringwald 
1554d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
155560ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Accept new call");
1556d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1557a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1558d210d9c4SMatthias Ringwald             } else {
155960ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Resume held call");
1560d210d9c4SMatthias Ringwald             }
1561d210d9c4SMatthias Ringwald             if (call_held){
1562d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1563a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1564d210d9c4SMatthias Ringwald             }
1565d0c20769SMatthias Ringwald 
1566a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1567d210d9c4SMatthias Ringwald             break;
1568d210d9c4SMatthias Ringwald         }
1569d0c20769SMatthias Ringwald 
1570d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1571d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1572d210d9c4SMatthias Ringwald             // only update if callsetup changed
1573d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1574a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1575d0c20769SMatthias Ringwald 
1576d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
157760ebb071SMilanka Ringwald                 log_info("AG: Call on Hold, Accept new call");
1578d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1579a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1580d210d9c4SMatthias Ringwald             } else {
158160ebb071SMilanka Ringwald                 log_info("AG: Swap calls");
1582d210d9c4SMatthias Ringwald             }
1583d0c20769SMatthias Ringwald 
1584d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1585d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1586a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1587a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1588d210d9c4SMatthias Ringwald             break;
1589d210d9c4SMatthias Ringwald         }
1590d0c20769SMatthias Ringwald 
1591d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1592d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1593d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
159460ebb071SMilanka Ringwald                 log_info("AG: Join 3-way-call");
1595d210d9c4SMatthias Ringwald                 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL);
1596d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1597a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1598a0ffb263SMatthias Ringwald                 hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL);
1599d210d9c4SMatthias Ringwald             }
1600a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1601d210d9c4SMatthias Ringwald             break;
1602d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1603d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1604d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS);
160560ebb071SMilanka Ringwald             log_info("AG: Transfer call -> Connect two calls and disconnect");
1606d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1607d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1608a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1609a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1610a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1611d210d9c4SMatthias Ringwald             break;
1612ce263fc8SMatthias Ringwald 
16133deb3ec6SMatthias Ringwald         default:
16143deb3ec6SMatthias Ringwald             break;
16153deb3ec6SMatthias Ringwald     }
1616d210d9c4SMatthias Ringwald 
1617d0c20769SMatthias Ringwald 
16183deb3ec6SMatthias Ringwald }
16193deb3ec6SMatthias Ringwald 
16209cae807eSMatthias Ringwald 
1621a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
16229cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
16239cae807eSMatthias Ringwald     if (!active_call) return;
16249cae807eSMatthias Ringwald 
16259cae807eSMatthias Ringwald     int idx = active_call->index;
16269cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
16279cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
16289cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
16299cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
16309cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
16319cae807eSMatthias Ringwald     char * number = active_call->clip_number;
16329cae807eSMatthias Ringwald 
16339cae807eSMatthias Ringwald     char buffer[100];
16349cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
16359cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
16369cae807eSMatthias Ringwald     if (number){
16379cae807eSMatthias Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset, ", \"%s\",%u", number, type);
16389cae807eSMatthias Ringwald     }
16399cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
164060ebb071SMilanka 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,
16419cae807eSMatthias Ringwald        mode, mpty, type, number);
1642a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
16439cae807eSMatthias Ringwald }
16449cae807eSMatthias Ringwald 
1645a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection){
1646473ac565SMatthias Ringwald 
1647c79b4cabSMatthias Ringwald     // log_info("hfp_run_for_context %p", hfp_connection);
1648473ac565SMatthias Ringwald 
1649a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1650473ac565SMatthias Ringwald 
1651724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
1652473ac565SMatthias Ringwald 
1653e30a6a47SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1654473ac565SMatthias Ringwald         log_info("hfp_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1655e30a6a47SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1656e30a6a47SMatthias Ringwald         return;
1657e30a6a47SMatthias Ringwald     }
16583deb3ec6SMatthias Ringwald 
1659a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1660a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1661a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1662a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1663a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
16649cae807eSMatthias Ringwald         } else {
1665a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1666a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1667a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16689cae807eSMatthias Ringwald         }
1669ce263fc8SMatthias Ringwald         return;
1670ce263fc8SMatthias Ringwald     }
1671ce263fc8SMatthias Ringwald 
1672a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1673a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1674a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1675aa4dd815SMatthias Ringwald         return;
1676aa4dd815SMatthias Ringwald     }
1677aa4dd815SMatthias Ringwald 
1678a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1679a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1680a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1681a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1682a0ffb263SMatthias Ringwald         hfp_ag_error(hfp_connection->rfcomm_cid);
1683a0ffb263SMatthias Ringwald         return;
1684a0ffb263SMatthias Ringwald     }
1685a0ffb263SMatthias Ringwald 
1686a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1687a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1688a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1689a0ffb263SMatthias Ringwald         hfp_ag_error(hfp_connection->rfcomm_cid);
1690aa4dd815SMatthias Ringwald         return;
1691aa4dd815SMatthias Ringwald     }
1692aa4dd815SMatthias Ringwald 
1693ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1694a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1695a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1696a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1697a0ffb263SMatthias Ringwald         hfp_ag_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1698ce263fc8SMatthias Ringwald         return;
1699ce263fc8SMatthias Ringwald     }
1700ce263fc8SMatthias Ringwald 
1701a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1702a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1703a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1704a0ffb263SMatthias Ringwald         hfp_ag_ok(hfp_connection->rfcomm_cid);
1705ce263fc8SMatthias Ringwald         return;
1706ce263fc8SMatthias Ringwald     }
1707ce263fc8SMatthias Ringwald 
1708aa4dd815SMatthias Ringwald     // update AG indicators
1709a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1710aa4dd815SMatthias Ringwald         int i;
1711a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1712a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1713a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1714a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1715ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1716ce263fc8SMatthias Ringwald                     break;
1717ce263fc8SMatthias Ringwald                 }
1718a0ffb263SMatthias Ringwald                 hfp_ag_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1719aa4dd815SMatthias Ringwald                 return;
1720aa4dd815SMatthias Ringwald             }
1721aa4dd815SMatthias Ringwald         }
1722aa4dd815SMatthias Ringwald     }
1723aa4dd815SMatthias Ringwald 
1724a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1725a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1726a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1727a0ffb263SMatthias Ringwald         hfp_ag_ring(hfp_connection->rfcomm_cid);
1728aa4dd815SMatthias Ringwald         return;
1729aa4dd815SMatthias Ringwald     }
1730aa4dd815SMatthias Ringwald 
1731a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1732a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1733a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1734a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1735aa4dd815SMatthias Ringwald         return;
1736aa4dd815SMatthias Ringwald     }
1737aa4dd815SMatthias Ringwald 
1738a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1739a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1740a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1741a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1742a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1743aa4dd815SMatthias Ringwald         return;
1744aa4dd815SMatthias Ringwald     }
1745aa4dd815SMatthias Ringwald 
1746a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1747a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1748a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1749a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1750ce263fc8SMatthias Ringwald         } else {
1751a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1752a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1753a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
1754ce263fc8SMatthias Ringwald         }
1755a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1756ce263fc8SMatthias Ringwald     }
1757ce263fc8SMatthias Ringwald 
1758a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1759a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1760a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1761a0ffb263SMatthias Ringwald         hfp_ag_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1762aa4dd815SMatthias Ringwald         return;
1763aa4dd815SMatthias Ringwald     }
1764aa4dd815SMatthias Ringwald 
1765a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1766a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1767a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1768a0ffb263SMatthias Ringwald         hfp_ag_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
17693deb3ec6SMatthias Ringwald         return;
17703deb3ec6SMatthias Ringwald     }
17713deb3ec6SMatthias Ringwald 
1772a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1773a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1774a0ffb263SMatthias Ringwald         hfp_ag_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1775ce263fc8SMatthias Ringwald         return;
1776ce263fc8SMatthias Ringwald     }
1777ce263fc8SMatthias Ringwald 
1778a0ffb263SMatthias Ringwald     int done = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1779aa4dd815SMatthias Ringwald     if (!done){
1780a0ffb263SMatthias Ringwald         done = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
17813deb3ec6SMatthias Ringwald     }
17823deb3ec6SMatthias Ringwald 
1783aa4dd815SMatthias Ringwald     if (!done){
1784a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
1785aa4dd815SMatthias Ringwald     }
1786aa4dd815SMatthias Ringwald 
1787aa4dd815SMatthias Ringwald     if (!done){
1788a0ffb263SMatthias Ringwald         done = hfp_ag_run_for_audio_connection(hfp_connection);
1789aa4dd815SMatthias Ringwald     }
17903deb3ec6SMatthias Ringwald 
1791a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_NONE && !done){
1792a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1793a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
17943deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1795a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1796a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
17973deb3ec6SMatthias Ringwald                 break;
17983deb3ec6SMatthias Ringwald             default:
17993deb3ec6SMatthias Ringwald                 break;
18003deb3ec6SMatthias Ringwald         }
18013deb3ec6SMatthias Ringwald     }
18023deb3ec6SMatthias Ringwald     if (done){
1803a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
18043deb3ec6SMatthias Ringwald     }
1805473ac565SMatthias Ringwald     //
1806473ac565SMatthias Ringwald     if (done) {
1807473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1808473ac565SMatthias Ringwald     }
18093deb3ec6SMatthias Ringwald }
1810d68dcce1SMatthias Ringwald 
1811ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1812ce263fc8SMatthias Ringwald     int i;
1813a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1814a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1815ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1816ce263fc8SMatthias Ringwald             return indicator;
1817ce263fc8SMatthias Ringwald         }
1818ce263fc8SMatthias Ringwald     }
1819ce263fc8SMatthias Ringwald     return NULL;
1820ce263fc8SMatthias Ringwald }
18213deb3ec6SMatthias Ringwald 
1822aa4dd815SMatthias Ringwald static void hfp_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
18239ec2630cSMatthias Ringwald     UNUSED(packet_type);
18249ec2630cSMatthias Ringwald 
1825a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1826a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
18271e35c04dSMatthias Ringwald 
18281e35c04dSMatthias Ringwald     char last_char = packet[size-1];
18291e35c04dSMatthias Ringwald     packet[size-1] = 0;
18301e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
18311e35c04dSMatthias Ringwald     packet[size-1] = last_char;
18321e35c04dSMatthias Ringwald 
18333deb3ec6SMatthias Ringwald     int pos;
18343deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1835a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1836aa4dd815SMatthias Ringwald     }
1837ce263fc8SMatthias Ringwald     hfp_generic_status_indicator_t * indicator;
1838ce263fc8SMatthias Ringwald     int value;
1839a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
1840ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1841ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_active){
1842a0ffb263SMatthias Ringwald                 hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1843ce263fc8SMatthias Ringwald             }
1844a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1845ce263fc8SMatthias Ringwald             break;
1846ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
18472308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
184860ebb071SMilanka Ringwald             log_info("HF Response and Hold: %u", value);
1849ce263fc8SMatthias Ringwald             switch(value){
1850ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1851a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1852ce263fc8SMatthias Ringwald                     break;
1853ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1854a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1855ce263fc8SMatthias Ringwald                     break;
1856ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1857a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1858ce263fc8SMatthias Ringwald                     break;
1859ce263fc8SMatthias Ringwald                 default:
1860ce263fc8SMatthias Ringwald                     break;
1861ce263fc8SMatthias Ringwald             }
1862a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1863ce263fc8SMatthias Ringwald             break;
1864ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
1865a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1866ce263fc8SMatthias Ringwald             // find indicator by assigned number
1867a0ffb263SMatthias Ringwald             indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1868ce263fc8SMatthias Ringwald             if (!indicator){
1869a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1870ce263fc8SMatthias Ringwald                 break;
1871ce263fc8SMatthias Ringwald             }
18722308e108SMilanka Ringwald             value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1873ce263fc8SMatthias Ringwald             switch (indicator->uuid){
1874ce263fc8SMatthias Ringwald                 case 1: // enhanced security
1875ce263fc8SMatthias Ringwald                     if (value > 1) {
1876a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1877ce263fc8SMatthias Ringwald                         return;
1878ce263fc8SMatthias Ringwald                     }
187960ebb071SMilanka Ringwald                     log_info("HF Indicator 'enhanced security' set to %u", value);
1880ce263fc8SMatthias Ringwald                     break;
1881ce263fc8SMatthias Ringwald                 case 2: // battery level
1882ce263fc8SMatthias Ringwald                     if (value > 100){
1883a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1884ce263fc8SMatthias Ringwald                         return;
1885ce263fc8SMatthias Ringwald                     }
188660ebb071SMilanka Ringwald                     log_info("HF Indicator 'battery' set to %u", value);
1887ce263fc8SMatthias Ringwald                     break;
1888ce263fc8SMatthias Ringwald                 default:
188960ebb071SMilanka Ringwald                     log_info("HF Indicator unknown set to %u", value);
1890ce263fc8SMatthias Ringwald                     break;
1891ce263fc8SMatthias Ringwald             }
1892a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1893ce263fc8SMatthias Ringwald             break;
1894ce263fc8SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1895ce263fc8SMatthias Ringwald             // expected by SLC state machine
1896a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1897a0ffb263SMatthias Ringwald             hfp_connection->send_ag_indicators_segment = 0;
1898a0ffb263SMatthias Ringwald             hfp_connection->send_ag_status_indicators = 1;
1899ce263fc8SMatthias Ringwald             break;
1900ce263fc8SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1901a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1902a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1903a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 1;
1904ce263fc8SMatthias Ringwald             break;
1905ce263fc8SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1906ce263fc8SMatthias Ringwald             if (subscriber_numbers_count == 0){
1907a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
1908ce263fc8SMatthias Ringwald                 break;
1909ce263fc8SMatthias Ringwald             }
1910a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1911a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 1;
1912ce263fc8SMatthias Ringwald             break;
1913c1797c7dSMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
1914a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1915a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]);
1916c1797c7dSMatthias Ringwald             break;
1917aa4dd815SMatthias Ringwald         case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1918a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1919a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1920aa4dd815SMatthias Ringwald             break;
1921aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1922a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1923aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1924a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1925a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
192660ebb071SMilanka Ringwald                 log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction);
1927aa4dd815SMatthias Ringwald             } else {
1928a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1929aa4dd815SMatthias Ringwald             }
1930aa4dd815SMatthias Ringwald             break;
1931aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_ANSWERED:
1932a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
193360ebb071SMilanka Ringwald             log_info("HFP: ATA");
1934a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1935aa4dd815SMatthias Ringwald             break;
1936aa4dd815SMatthias Ringwald         case HFP_CMD_HANG_UP_CALL:
1937a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1938a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1939a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1940aa4dd815SMatthias Ringwald             break;
1941aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_HOLD: {
1942aa4dd815SMatthias Ringwald             // TODO: fully implement this
1943a0ffb263SMatthias Ringwald             log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]);
1944a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1945a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1946a0ffb263SMatthias Ringwald             hfp_connection->call_index = 0;
1947d0c20769SMatthias Ringwald 
1948a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
19492308e108SMilanka Ringwald                 hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
1950d0c20769SMatthias Ringwald             }
1951d210d9c4SMatthias Ringwald 
1952a0ffb263SMatthias Ringwald             switch (hfp_connection->line_buffer[0]){
1953aa4dd815SMatthias Ringwald                 case '0':
1954d0c20769SMatthias Ringwald                     // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1955a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1956aa4dd815SMatthias Ringwald                     break;
1957aa4dd815SMatthias Ringwald                 case '1':
1958d0c20769SMatthias Ringwald                     // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1959d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1960d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1961a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1962aa4dd815SMatthias Ringwald                     break;
1963aa4dd815SMatthias Ringwald                 case '2':
1964d0c20769SMatthias Ringwald                     // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1965d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1966d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1967a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1968aa4dd815SMatthias Ringwald                     break;
1969aa4dd815SMatthias Ringwald                 case '3':
1970d0c20769SMatthias Ringwald                     // Adds a held call to the conversation.
1971a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1972aa4dd815SMatthias Ringwald                     break;
1973aa4dd815SMatthias Ringwald                 case '4':
1974d0c20769SMatthias Ringwald                     // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1975a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1976aa4dd815SMatthias Ringwald                     break;
1977aa4dd815SMatthias Ringwald                 default:
1978aa4dd815SMatthias Ringwald                     break;
1979aa4dd815SMatthias Ringwald             }
1980aa4dd815SMatthias Ringwald             break;
1981aa4dd815SMatthias Ringwald         }
1982aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_PHONE_NUMBER:
1983a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1984a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
1985aa4dd815SMatthias Ringwald             break;
1986aa4dd815SMatthias Ringwald         case HFP_CMD_REDIAL_LAST_NUMBER:
1987a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1988a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
1989aa4dd815SMatthias Ringwald             break;
1990aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
1991a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1992a0ffb263SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0';
1993a0ffb263SMatthias Ringwald             log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
1994a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1995aa4dd815SMatthias Ringwald             break;
1996aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1997a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1998a0ffb263SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0';
1999a0ffb263SMatthias Ringwald             log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
2000a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
2001aa4dd815SMatthias Ringwald             break;
2002aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
2003a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
2004a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
200560ebb071SMilanka Ringwald             log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
2006aa4dd815SMatthias Ringwald             break;
2007aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
2008a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
2009a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
201060ebb071SMilanka Ringwald             log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
2011aa4dd815SMatthias Ringwald             break;
2012aa4dd815SMatthias Ringwald         default:
2013aa4dd815SMatthias Ringwald             break;
20143deb3ec6SMatthias Ringwald     }
20153deb3ec6SMatthias Ringwald }
20163deb3ec6SMatthias Ringwald 
2017aa4dd815SMatthias Ringwald static void hfp_run(void){
2018d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
2019d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2020d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2021d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2022a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
20233deb3ec6SMatthias Ringwald     }
20243deb3ec6SMatthias Ringwald }
20253deb3ec6SMatthias Ringwald 
2026ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
20273deb3ec6SMatthias Ringwald     switch (packet_type){
20283deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
2029aa4dd815SMatthias Ringwald             hfp_handle_rfcomm_data(packet_type, channel, packet, size);
20303deb3ec6SMatthias Ringwald             break;
20313deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2032e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2033e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2034e30a6a47SMatthias Ringwald                 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2035e30a6a47SMatthias Ringwald                 return;
2036e30a6a47SMatthias Ringwald             }
2037e30a6a47SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size);
2038aa4dd815SMatthias Ringwald             break;
20393deb3ec6SMatthias Ringwald         default:
20403deb3ec6SMatthias Ringwald             break;
20413deb3ec6SMatthias Ringwald     }
20423deb3ec6SMatthias Ringwald 
20433deb3ec6SMatthias Ringwald     hfp_run();
20443deb3ec6SMatthias Ringwald }
20453deb3ec6SMatthias Ringwald 
20461e35c04dSMatthias Ringwald 
2047a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
20483deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
20493deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
20503deb3ec6SMatthias Ringwald         return;
20513deb3ec6SMatthias Ringwald     }
20523deb3ec6SMatthias Ringwald     int i;
2053a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
20543deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
20553deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
20563deb3ec6SMatthias Ringwald     }
2057a0ffb263SMatthias Ringwald }
20583deb3ec6SMatthias Ringwald 
2059a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2060a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2061a0ffb263SMatthias Ringwald }
20623deb3ec6SMatthias Ringwald 
2063a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2064a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
2065a0ffb263SMatthias Ringwald     memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2066a0ffb263SMatthias Ringwald }
20673deb3ec6SMatthias Ringwald 
2068a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
2069a0ffb263SMatthias Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return;
2070a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
2071a0ffb263SMatthias Ringwald     memcpy(hfp_generic_status_indicators, hf_indicators, hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2072a0ffb263SMatthias Ringwald }
2073a0ffb263SMatthias Ringwald 
2074a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
20753deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
20763deb3ec6SMatthias Ringwald     memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *));
2077a0ffb263SMatthias Ringwald }
2078a0ffb263SMatthias Ringwald 
2079a0ffb263SMatthias Ringwald 
2080a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
2081d63c37a1SMatthias Ringwald     // register for HCI events
2082d63c37a1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
2083d63c37a1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
2084d63c37a1SMatthias Ringwald 
2085d68dcce1SMatthias Ringwald     rfcomm_register_service(&packet_handler, rfcomm_channel_nr, 0xffff);
2086aa4dd815SMatthias Ringwald 
2087d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2088d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2089d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2090d210d9c4SMatthias Ringwald 
2091d68dcce1SMatthias Ringwald     hfp_set_packet_handler_for_rfcomm_connections(&packet_handler);
2092d68dcce1SMatthias Ringwald 
2093d210d9c4SMatthias Ringwald     hfp_gsm_init();
20943deb3ec6SMatthias Ringwald }
20953deb3ec6SMatthias Ringwald 
20963deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
2097*235946f1SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
20983deb3ec6SMatthias Ringwald }
20993deb3ec6SMatthias Ringwald 
2100d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
2101d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2102a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2103a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2104a33eb0c4SMilanka Ringwald         return;
2105a33eb0c4SMilanka Ringwald     }
2106a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2107a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21083deb3ec6SMatthias Ringwald }
21093deb3ec6SMatthias Ringwald 
2110d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
2111d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2112a0ffb263SMatthias Ringwald     if (!hfp_connection){
2113a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
21143deb3ec6SMatthias Ringwald         return;
21153deb3ec6SMatthias Ringwald     }
2116a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2117a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
21183deb3ec6SMatthias Ringwald         return;
21193deb3ec6SMatthias Ringwald     }
2120a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2121a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21223deb3ec6SMatthias Ringwald }
21233deb3ec6SMatthias Ringwald 
2124a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
2125a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2126a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
21273deb3ec6SMatthias Ringwald 
2128a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2129aa4dd815SMatthias Ringwald 
2130a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2131d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2132d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2133a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
21347522e673SMatthias Ringwald         // now, pick link settings
21357522e673SMatthias Ringwald         hfp_init_link_settings(hfp_connection);
21363deb3ec6SMatthias Ringwald     }
2137aa4dd815SMatthias Ringwald 
2138a0ffb263SMatthias Ringwald     switch (hfp_connection->codecs_state){
2139aa4dd815SMatthias Ringwald         case HFP_CODECS_IDLE:
2140aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_LIST:
2141aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
2142aa4dd815SMatthias Ringwald         case HFP_CODECS_ERROR:
2143a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
2144aa4dd815SMatthias Ringwald             break;
2145aa4dd815SMatthias Ringwald         default:
2146aa4dd815SMatthias Ringwald             break;
2147aa4dd815SMatthias Ringwald     }
2148aa4dd815SMatthias Ringwald }
2149aa4dd815SMatthias Ringwald 
2150d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
2151d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2152a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2153a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2154a33eb0c4SMilanka Ringwald         return;
2155a33eb0c4SMilanka Ringwald     }
2156a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2157a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2158a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21593deb3ec6SMatthias Ringwald }
21603deb3ec6SMatthias Ringwald 
2161d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
2162d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2163a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2164a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2165a33eb0c4SMilanka Ringwald         return;
2166a33eb0c4SMilanka Ringwald     }
2167a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2168a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21693deb3ec6SMatthias Ringwald }
2170aa4dd815SMatthias Ringwald 
2171aa4dd815SMatthias Ringwald /**
2172aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2173aa4dd815SMatthias Ringwald  */
2174aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2175aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2176aa4dd815SMatthias Ringwald         return;
2177aa4dd815SMatthias Ringwald     }
2178aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2179aa4dd815SMatthias Ringwald 
2180665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2181665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2182665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2183a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2184a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2185a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2186aa4dd815SMatthias Ringwald     }
2187aa4dd815SMatthias Ringwald }
2188aa4dd815SMatthias Ringwald 
2189aa4dd815SMatthias Ringwald /**
2190aa4dd815SMatthias Ringwald  * @brief Called from GSM
2191aa4dd815SMatthias Ringwald  */
2192aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2193aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2194aa4dd815SMatthias Ringwald }
2195aa4dd815SMatthias Ringwald 
2196aa4dd815SMatthias Ringwald /**
2197aa4dd815SMatthias Ringwald  * @brief number is stored.
2198aa4dd815SMatthias Ringwald  */
2199aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2200d0c20769SMatthias Ringwald     hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number);
2201aa4dd815SMatthias Ringwald }
2202aa4dd815SMatthias Ringwald 
2203aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2204aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2205aa4dd815SMatthias Ringwald }
2206aa4dd815SMatthias Ringwald 
2207aa4dd815SMatthias Ringwald // call from AG UI
2208aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2209aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2210aa4dd815SMatthias Ringwald }
2211aa4dd815SMatthias Ringwald 
2212ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2213ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2214ce263fc8SMatthias Ringwald }
2215ce263fc8SMatthias Ringwald 
2216aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2217aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2218aa4dd815SMatthias Ringwald }
2219aa4dd815SMatthias Ringwald 
2220aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2221aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2222aa4dd815SMatthias Ringwald }
2223aa4dd815SMatthias Ringwald 
2224aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2225aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2226aa4dd815SMatthias Ringwald }
2227aa4dd815SMatthias Ringwald 
2228aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2229aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2230aa4dd815SMatthias Ringwald }
2231aa4dd815SMatthias Ringwald 
2232aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2233aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2234aa4dd815SMatthias Ringwald }
2235ce263fc8SMatthias Ringwald 
2236ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2237ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2238ce263fc8SMatthias Ringwald }
2239ce263fc8SMatthias Ringwald 
2240ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2241ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2242ce263fc8SMatthias Ringwald }
2243ce263fc8SMatthias Ringwald 
2244ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2245ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2246ce263fc8SMatthias Ringwald }
2247ce263fc8SMatthias Ringwald 
2248aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2249aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2250aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2251aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2252aa4dd815SMatthias Ringwald 
2253ce263fc8SMatthias Ringwald 
2254665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2255665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2256665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2257a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2258a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2259ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2260ce263fc8SMatthias Ringwald             continue;
2261ce263fc8SMatthias Ringwald         }
2262ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2263a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2264a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2265aa4dd815SMatthias Ringwald     }
2266aa4dd815SMatthias Ringwald }
2267aa4dd815SMatthias Ringwald 
2268aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2269aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2270aa4dd815SMatthias Ringwald }
2271aa4dd815SMatthias Ringwald 
2272aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2273aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2274aa4dd815SMatthias Ringwald }
2275aa4dd815SMatthias Ringwald 
2276aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2277aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2278aa4dd815SMatthias Ringwald }
2279aa4dd815SMatthias Ringwald 
2280aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2281aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2282aa4dd815SMatthias Ringwald }
2283aa4dd815SMatthias Ringwald 
2284d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){
2285aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
2286d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2287a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2288a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2289a33eb0c4SMilanka Ringwald         return;
2290a33eb0c4SMilanka Ringwald     }
2291aa4dd815SMatthias Ringwald 
2292a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
229360ebb071SMilanka Ringwald         log_info("AG cannot acivate voice recognition - not supported by HF");
2294aa4dd815SMatthias Ringwald         return;
2295aa4dd815SMatthias Ringwald     }
2296aa4dd815SMatthias Ringwald 
2297aa4dd815SMatthias Ringwald     if (activate){
2298d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(acl_handle);
2299aa4dd815SMatthias Ringwald     }
2300aa4dd815SMatthias Ringwald 
2301a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2302a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2303a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2304aa4dd815SMatthias Ringwald }
2305aa4dd815SMatthias Ringwald 
2306d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
2307d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2308a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2309a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2310a33eb0c4SMilanka Ringwald         return;
2311a33eb0c4SMilanka Ringwald     }
2312a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2313a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2314a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2315a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2316aa4dd815SMatthias Ringwald     }
2317a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2318aa4dd815SMatthias Ringwald }
2319aa4dd815SMatthias Ringwald 
2320d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
2321d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2322a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2323a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2324a33eb0c4SMilanka Ringwald         return;
2325a33eb0c4SMilanka Ringwald     }
2326a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2327a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2328a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2329aa4dd815SMatthias Ringwald     }
2330a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2331aa4dd815SMatthias Ringwald }
2332aa4dd815SMatthias Ringwald 
2333d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
2334d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2335a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2336a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2337a33eb0c4SMilanka Ringwald         return;
2338a33eb0c4SMilanka Ringwald     }
2339aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2340a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2341aa4dd815SMatthias Ringwald }
2342aa4dd815SMatthias Ringwald 
2343d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
2344d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2345a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2346a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2347a33eb0c4SMilanka Ringwald         return;
2348a33eb0c4SMilanka Ringwald     }
2349a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2350aa4dd815SMatthias Ringwald }
2351aa4dd815SMatthias Ringwald 
2352d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
2353d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2354a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2355a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2356a33eb0c4SMilanka Ringwald         return;
2357a33eb0c4SMilanka Ringwald     }
2358a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2359c1797c7dSMatthias Ringwald }
2360aa4dd815SMatthias Ringwald 
2361ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2362ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2363ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2364ce263fc8SMatthias Ringwald }
2365ce263fc8SMatthias Ringwald 
23669cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
23679cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2368ce263fc8SMatthias Ringwald }
2369ce263fc8SMatthias Ringwald 
2370d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
2371d97d752dSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_acl_handle(acl_handle);
2372a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2373a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2374a33eb0c4SMilanka Ringwald         return;
2375a33eb0c4SMilanka Ringwald     }
2376a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2377a0ffb263SMatthias Ringwald 
2378a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2379a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2380a0ffb263SMatthias Ringwald }
2381ce263fc8SMatthias Ringwald 
2382