xref: /btstack/src/classic/hfp_ag.c (revision 25789943f3d4acf0e0b4cc083fabb8138a19904e)
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 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp_ag.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
4266a048abSMatthias Ringwald // HFP Audio Gateway (AG) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493deb3ec6SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <stdlib.h>
513deb3ec6SMatthias Ringwald #include <string.h>
523deb3ec6SMatthias Ringwald 
5356042629SMatthias Ringwald #include "hci_cmd.h"
5482636622SMatthias Ringwald #include "btstack_run_loop.h"
553deb3ec6SMatthias Ringwald 
56235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
573deb3ec6SMatthias Ringwald #include "hci.h"
583deb3ec6SMatthias Ringwald #include "btstack_memory.h"
593deb3ec6SMatthias Ringwald #include "hci_dump.h"
603deb3ec6SMatthias Ringwald #include "l2cap.h"
6116ece135SMatthias Ringwald #include "btstack_debug.h"
62e30a6a47SMatthias Ringwald #include "btstack_event.h"
6359c6af15SMatthias Ringwald #include "classic/core.h"
643edc84c5SMatthias Ringwald #include "classic/hfp.h"
653edc84c5SMatthias Ringwald #include "classic/hfp_ag.h"
66023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h"
67efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
68023f2764SMatthias Ringwald #include "classic/sdp_server.h"
69023f2764SMatthias Ringwald #include "classic/sdp_util.h"
703deb3ec6SMatthias Ringwald 
71c5b64319SMatthias Ringwald // private prototypes
72f0aeb307SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection);
73adaba9f3SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
74adaba9f3SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
75c5b64319SMatthias Ringwald 
76c5b64319SMatthias Ringwald // public prototypes
7735833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void);
7835833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void);
79c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr);
80c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr);
81c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context);
82c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context);
83c5b64319SMatthias Ringwald 
8427950165SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
8527950165SMatthias Ringwald 
86c5b64319SMatthias Ringwald // gobals
873deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway";
88aa4dd815SMatthias Ringwald 
893deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
90aa4dd815SMatthias Ringwald 
913deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
923deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
933deb3ec6SMatthias Ringwald 
943deb3ec6SMatthias Ringwald static int  hfp_ag_indicators_nr = 0;
95*25789943SMilanka Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS];
963deb3ec6SMatthias Ringwald 
97a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0;
98*25789943SMilanka Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_INDICATORS];
99a0ffb263SMatthias Ringwald 
1003deb3ec6SMatthias Ringwald static int  hfp_ag_call_hold_services_nr = 0;
1013deb3ec6SMatthias Ringwald static char *hfp_ag_call_hold_services[6];
102ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback;
1033deb3ec6SMatthias Ringwald 
104ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
105ce263fc8SMatthias Ringwald static int hfp_ag_response_and_hold_active = 0;
106aa4dd815SMatthias Ringwald 
107ce263fc8SMatthias Ringwald // Subcriber information entries
108ce263fc8SMatthias Ringwald static hfp_phone_number_t * subscriber_numbers = NULL;
109ce263fc8SMatthias Ringwald static int subscriber_numbers_count = 0;
110ce263fc8SMatthias Ringwald 
11199a10067SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection);
1123deb3ec6SMatthias Ringwald 
1133deb3ec6SMatthias Ringwald 
114a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
115a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
116a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1176535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1186535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1193deb3ec6SMatthias Ringwald     }
120a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
121a0ffb263SMatthias Ringwald }
122a0ffb263SMatthias Ringwald 
123a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
124a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
125a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
126a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1276535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1286535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
129a0ffb263SMatthias Ringwald     }
130a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1313deb3ec6SMatthias Ringwald }
1323deb3ec6SMatthias Ringwald 
133aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
134aa4dd815SMatthias Ringwald     int i;
135aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
136aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
137aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
138aa4dd815SMatthias Ringwald         }
139aa4dd815SMatthias Ringwald     }
140aa4dd815SMatthias Ringwald     return NULL;
141aa4dd815SMatthias Ringwald }
142aa4dd815SMatthias Ringwald 
143aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
144aa4dd815SMatthias Ringwald     int i;
145aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
146aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
147aa4dd815SMatthias Ringwald             return i;
148aa4dd815SMatthias Ringwald         }
149aa4dd815SMatthias Ringwald     }
150aa4dd815SMatthias Ringwald     return -1;
151aa4dd815SMatthias Ringwald }
152aa4dd815SMatthias Ringwald 
1539c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){
1549c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1559c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1569c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1579c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1589c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1599c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1609c9c64c1SMatthias Ringwald         return hfp_connection;
1619c9c64c1SMatthias Ringwald     }
1629c9c64c1SMatthias Ringwald     return NULL;
1639c9c64c1SMatthias Ringwald }
1643deb3ec6SMatthias Ringwald 
16513839019SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
1663deb3ec6SMatthias Ringwald     if (callback == NULL){
1673deb3ec6SMatthias Ringwald         log_error("hfp_ag_register_packet_handler called with NULL callback");
1683deb3ec6SMatthias Ringwald         return;
1693deb3ec6SMatthias Ringwald     }
170ca59be51SMatthias Ringwald     hfp_ag_callback = callback;
171ca59be51SMatthias Ringwald     hfp_set_ag_callback(callback);
1723deb3ec6SMatthias Ringwald }
1733deb3ec6SMatthias Ringwald 
1740cb5b971SMatthias Ringwald static int use_in_band_tone(void){
175aa4dd815SMatthias Ringwald     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1763deb3ec6SMatthias Ringwald }
1773deb3ec6SMatthias Ringwald 
178a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
179a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1803deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1813deb3ec6SMatthias Ringwald     return hf && ag;
1823deb3ec6SMatthias Ringwald }
1833deb3ec6SMatthias Ringwald 
184a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
185a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1863deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1873deb3ec6SMatthias Ringwald     return hf && ag;
1883deb3ec6SMatthias Ringwald }
1893deb3ec6SMatthias Ringwald 
190a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
191a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
1923deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
1933deb3ec6SMatthias Ringwald     return hf && ag;
1943deb3ec6SMatthias Ringwald }
1953deb3ec6SMatthias Ringwald 
1964f84bf36SMatthias 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){
1973deb3ec6SMatthias Ringwald     if (!name){
1983deb3ec6SMatthias Ringwald         name = default_hfp_ag_service_name;
1993deb3ec6SMatthias Ringwald     }
200235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
2013deb3ec6SMatthias Ringwald 
2023deb3ec6SMatthias Ringwald     /*
2033deb3ec6SMatthias Ringwald      * 0x01 – Ability to reject a call
2043deb3ec6SMatthias Ringwald      * 0x00 – No ability to reject a call
2053deb3ec6SMatthias Ringwald      */
206aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
207aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
208aa4dd815SMatthias Ringwald 
2094f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
2104f84bf36SMatthias Ringwald     //
2114f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
2124f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
2134f84bf36SMatthias Ringwald     //
21442bafa11SMatthias Ringwald     // Wide band speech (bit 5) requires Codec negotiation
21542bafa11SMatthias Ringwald     //
2164f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
21742bafa11SMatthias Ringwald     if (wide_band_speech && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){
21842bafa11SMatthias Ringwald         sdp_features |= 1 << 5;
2194f84bf36SMatthias Ringwald     }
220aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
2214f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
222aa4dd815SMatthias Ringwald }
223aa4dd815SMatthias Ringwald 
224485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){
225aa4dd815SMatthias Ringwald     char buffer[20];
226aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
227aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2283deb3ec6SMatthias Ringwald }
2293deb3ec6SMatthias Ringwald 
2303deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2313deb3ec6SMatthias Ringwald     char buffer[40];
2323deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
2333deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2343deb3ec6SMatthias Ringwald }
2353deb3ec6SMatthias Ringwald 
236485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){
2373deb3ec6SMatthias Ringwald     char buffer[10];
2383deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nOK\r\n");
2393deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2403deb3ec6SMatthias Ringwald }
2413deb3ec6SMatthias Ringwald 
242485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){
243aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
244aa4dd815SMatthias Ringwald }
245aa4dd815SMatthias Ringwald 
246aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
247aa4dd815SMatthias Ringwald     char buffer[50];
248d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
249aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
250aa4dd815SMatthias Ringwald }
251aa4dd815SMatthias Ringwald 
252ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
253ce263fc8SMatthias Ringwald     char buffer[50];
254ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
255ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
256ce263fc8SMatthias Ringwald }
257ce263fc8SMatthias Ringwald 
258c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
259aa4dd815SMatthias Ringwald     char buffer[50];
260d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
261aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
262aa4dd815SMatthias Ringwald }
263aa4dd815SMatthias Ringwald 
264aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
265aa4dd815SMatthias Ringwald     char buffer[50];
266a0ffb263SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
267aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
268aa4dd815SMatthias Ringwald }
269aa4dd815SMatthias Ringwald 
270485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){
2713deb3ec6SMatthias Ringwald     char buffer[10];
2723deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nERROR\r\n");
2733deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2743deb3ec6SMatthias Ringwald }
2753deb3ec6SMatthias Ringwald 
276485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2773deb3ec6SMatthias Ringwald     char buffer[20];
2783deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2793deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2803deb3ec6SMatthias Ringwald }
2813deb3ec6SMatthias Ringwald 
282ad902e3dSMatthias Ringwald // get size for indicator string
283a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
284ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
28545718b6fSMatthias Ringwald     return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
286a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
287a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
288ad902e3dSMatthias Ringwald }
289ad902e3dSMatthias Ringwald 
290ad902e3dSMatthias Ringwald // store indicator
291a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
292ad902e3dSMatthias Ringwald     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
293a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
294a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
295a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2963deb3ec6SMatthias Ringwald }
297ad902e3dSMatthias Ringwald 
298ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
299a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
300a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
301ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
302c1ab6cc1SMatthias Ringwald     return 3 + ((num_indicators-1) * 2);
3033deb3ec6SMatthias Ringwald }
304ad902e3dSMatthias Ringwald 
305ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
306a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
307ad902e3dSMatthias Ringwald     if (index == 0) {
308ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
309ad902e3dSMatthias Ringwald     }
310ad902e3dSMatthias Ringwald     index--;
311a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
312ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
313ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
314a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
315ad902e3dSMatthias Ringwald     }
316c1ab6cc1SMatthias Ringwald     if (indicator_index == (num_indicators - 1)){
317ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
318ad902e3dSMatthias Ringwald     }
319ad902e3dSMatthias Ringwald     return 1; // comma
320ad902e3dSMatthias Ringwald }
321ad902e3dSMatthias Ringwald 
322a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
323ad902e3dSMatthias Ringwald     if (index == 0){
324ad902e3dSMatthias Ringwald         *buffer++ = '\r';
32574386ee0SMatthias Ringwald         *buffer++ = '\n';
326ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
3276535961aSMatthias Ringwald         (void)memcpy(buffer, HFP_INDICATOR, len);
328ad902e3dSMatthias Ringwald         buffer += len;
329ad902e3dSMatthias Ringwald         *buffer++ = ':';
330ad902e3dSMatthias Ringwald         return;
331ad902e3dSMatthias Ringwald     }
332ad902e3dSMatthias Ringwald     index--;
333a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
334ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
335ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
336a0ffb263SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
337ad902e3dSMatthias Ringwald         return;
338ad902e3dSMatthias Ringwald     }
339c1ab6cc1SMatthias Ringwald     if (indicator_index == (num_indicators-1)){
3406535961aSMatthias Ringwald         (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8);
341ad902e3dSMatthias Ringwald         return;
342ad902e3dSMatthias Ringwald     }
343ad902e3dSMatthias Ringwald     *buffer = ',';
3443deb3ec6SMatthias Ringwald }
3453deb3ec6SMatthias Ringwald 
3463deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
347c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3483deb3ec6SMatthias Ringwald     int i;
3493deb3ec6SMatthias Ringwald     int offset = 0;
350c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_generic_status_indicators_nr-1); i++) {
351a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3523deb3ec6SMatthias Ringwald     }
353a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
354a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3553deb3ec6SMatthias Ringwald     }
3563deb3ec6SMatthias Ringwald     return offset;
3573deb3ec6SMatthias Ringwald }
3583deb3ec6SMatthias Ringwald 
3593deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
360c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_generic_status_indicators_nr * 3)) return 0;
3613deb3ec6SMatthias Ringwald     int i;
3623deb3ec6SMatthias Ringwald     int offset = 0;
363a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
364a0ffb263SMatthias 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);
3653deb3ec6SMatthias Ringwald     }
3663deb3ec6SMatthias Ringwald     return offset;
3673deb3ec6SMatthias Ringwald }
3683deb3ec6SMatthias Ringwald 
3693deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
370c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3713deb3ec6SMatthias Ringwald     int i;
3723deb3ec6SMatthias Ringwald     int offset = 0;
373c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_ag_indicators_nr-1); i++) {
3743deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3753deb3ec6SMatthias Ringwald     }
3763deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3773deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3783deb3ec6SMatthias Ringwald     }
3793deb3ec6SMatthias Ringwald     return offset;
3803deb3ec6SMatthias Ringwald }
3813deb3ec6SMatthias Ringwald 
3823deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
383c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0;
3843deb3ec6SMatthias Ringwald     int i;
3853deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
386c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) {
3873deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3883deb3ec6SMatthias Ringwald     }
3893deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3903deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3913deb3ec6SMatthias Ringwald     }
3923deb3ec6SMatthias Ringwald     return offset;
3933deb3ec6SMatthias Ringwald }
3943deb3ec6SMatthias Ringwald 
395485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
396ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
397a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
398a0ffb263SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
3993deb3ec6SMatthias Ringwald 
400ad902e3dSMatthias Ringwald     // assumes: can send now == true
401ad902e3dSMatthias Ringwald     // assumes: num segments > 0
402aba39421SMatthias Ringwald     // assumes: individual segments are smaller than MTU
403ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
404ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
405ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
406ad902e3dSMatthias Ringwald     int offset = 0;
407ad902e3dSMatthias Ringwald     int segment = start_segment;
408ad902e3dSMatthias Ringwald     while (segment < num_segments){
409a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
410c1ab6cc1SMatthias Ringwald         if ((offset + segment_len) > mtu) break;
411aba39421SMatthias Ringwald         // append segement
412a0ffb263SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset);
413ad902e3dSMatthias Ringwald         offset += segment_len;
414ad902e3dSMatthias Ringwald         segment++;
415ad902e3dSMatthias Ringwald     }
416ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
417ad902e3dSMatthias Ringwald     return segment;
418ad902e3dSMatthias Ringwald }
4193deb3ec6SMatthias Ringwald 
420ad902e3dSMatthias Ringwald // returns next segment to store
421485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
422a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
423485ac19eSMilanka Ringwald     return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
424ad902e3dSMatthias Ringwald         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
4253deb3ec6SMatthias Ringwald }
4263deb3ec6SMatthias Ringwald 
427485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){
4283deb3ec6SMatthias Ringwald     char buffer[40];
42989425bfcSMilanka Ringwald     const int size = sizeof(buffer);
43089425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR);
43189425bfcSMilanka Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9);
43289425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4333deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4343deb3ec6SMatthias Ringwald }
4353deb3ec6SMatthias Ringwald 
436485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){
437ad902e3dSMatthias Ringwald     char buffer[40];
43889425bfcSMilanka Ringwald     const int size = sizeof(buffer);
43989425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
44089425bfcSMilanka Ringwald     offset += hfp_ag_call_services_join(buffer+offset, size-offset-9);
44189425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4423deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4433deb3ec6SMatthias Ringwald }
4443deb3ec6SMatthias Ringwald 
4453deb3ec6SMatthias Ringwald 
446485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){
447485ac19eSMilanka Ringwald     return hfp_ag_send_ok(cid);
4483deb3ec6SMatthias Ringwald }
4493deb3ec6SMatthias Ringwald 
450485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4513deb3ec6SMatthias Ringwald     char buffer[40];
45289425bfcSMilanka Ringwald     const int size = sizeof(buffer);
45389425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
45489425bfcSMilanka Ringwald     offset += hfp_hf_indicators_join(buffer+offset, size-offset-10);
45589425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n");
4563deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4573deb3ec6SMatthias Ringwald }
4583deb3ec6SMatthias Ringwald 
459485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4603deb3ec6SMatthias Ringwald     char buffer[40];
4611cc1d9e9SMilanka Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7);
4621cc1d9e9SMilanka Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4633deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4643deb3ec6SMatthias Ringwald }
4653deb3ec6SMatthias Ringwald 
466485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4673deb3ec6SMatthias Ringwald     char buffer[20];
468aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
4693deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4703deb3ec6SMatthias Ringwald }
4713deb3ec6SMatthias Ringwald 
472485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4735e71456cSMilanka Ringwald     char buffer[41];
4743deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
47589425bfcSMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4763deb3ec6SMatthias Ringwald     } else {
47789425bfcSMilanka Ringwald         int offset = snprintf(buffer,  sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format);
4785e71456cSMilanka Ringwald         offset += snprintf(buffer+offset, 16, "%s", op.name);
47989425bfcSMilanka Ringwald         snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4803deb3ec6SMatthias Ringwald     }
4813deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4823deb3ec6SMatthias Ringwald }
4833deb3ec6SMatthias Ringwald 
4841cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
4851cc1d9e9SMilanka Ringwald     char buffer[30];
48689425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value);
4871cc1d9e9SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
4881cc1d9e9SMilanka Ringwald }
4893deb3ec6SMatthias Ringwald 
490485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){
4911cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
4923deb3ec6SMatthias Ringwald }
4933deb3ec6SMatthias Ringwald 
494485ac19eSMilanka Ringwald static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
4951cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
496aa4dd815SMatthias Ringwald }
497aa4dd815SMatthias Ringwald 
498485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
4991cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
500aa4dd815SMatthias Ringwald }
501aa4dd815SMatthias Ringwald 
502485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
5031cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
504aa4dd815SMatthias Ringwald }
505aa4dd815SMatthias Ringwald 
506485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){
5071cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_RESPONSE_AND_HOLD, state);
508ce263fc8SMatthias Ringwald }
509ce263fc8SMatthias Ringwald 
510a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
511bc1b1537SMilanka Ringwald     if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
512bc1b1537SMilanka Ringwald 
5136a7f44bdSMilanka Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){
5146a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
515df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
5163deb3ec6SMatthias Ringwald         }
5173deb3ec6SMatthias Ringwald     }
518df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
5193deb3ec6SMatthias Ringwald }
5203deb3ec6SMatthias Ringwald 
521afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){
522afac2a04SMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_AGSF_ESCO_S4));
5237522e673SMatthias Ringwald }
5247522e673SMatthias Ringwald 
525a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
526aa4dd815SMatthias Ringwald     /* events ( == commands):
527aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
528aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
529aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
530aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
531aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
532aa4dd815SMatthias Ringwald     */
533aa4dd815SMatthias Ringwald 
534a0ffb263SMatthias Ringwald      switch (hfp_connection->codecs_state){
535aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
536a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
537aa4dd815SMatthias Ringwald             break;
538aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
539a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
540aa4dd815SMatthias Ringwald             break;
541aa4dd815SMatthias Ringwald         default:
542aa4dd815SMatthias Ringwald             break;
543aa4dd815SMatthias Ringwald     }
544aa4dd815SMatthias Ringwald 
545aa4dd815SMatthias Ringwald     // printf(" -> State machine: CC\n");
546aa4dd815SMatthias Ringwald 
547a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
548aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
549a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
550a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
551485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
552aa4dd815SMatthias Ringwald                 return 1;
553aa4dd815SMatthias Ringwald             }
554aa4dd815SMatthias Ringwald 
555a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
556aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
557aa4dd815SMatthias Ringwald                 case HFP_CODECS_EXCHANGED:
558a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
559aa4dd815SMatthias Ringwald                     break;
560aa4dd815SMatthias Ringwald                 default:
561aa4dd815SMatthias Ringwald                     break;
562aa4dd815SMatthias Ringwald             }
563485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
564aa4dd815SMatthias Ringwald             return 1;
565aa4dd815SMatthias Ringwald 
566aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
567a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
568485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
569aa4dd815SMatthias Ringwald             return 1;
570aa4dd815SMatthias Ringwald 
571aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
572a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
573a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
574485ac19eSMilanka Ringwald             hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
575aa4dd815SMatthias Ringwald             return 1;
576aa4dd815SMatthias Ringwald 
577aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
578a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
579a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
580485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
581aa4dd815SMatthias Ringwald                 return 1;
582aa4dd815SMatthias Ringwald             }
583a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
584a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
585c1ab6cc1SMatthias Ringwald             log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
586485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
5877522e673SMatthias Ringwald             // now, pick link settings
588afac2a04SMilanka Ringwald 
589afac2a04SMilanka Ringwald             hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
590aa4dd815SMatthias Ringwald             return 1;
591aa4dd815SMatthias Ringwald         default:
592aa4dd815SMatthias Ringwald             break;
593aa4dd815SMatthias Ringwald     }
594aa4dd815SMatthias Ringwald     return 0;
595aa4dd815SMatthias Ringwald }
596aa4dd815SMatthias Ringwald 
597a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
598a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
599ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
600aa4dd815SMatthias Ringwald 
601a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
602d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
603a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
604aa4dd815SMatthias Ringwald     }
605ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
606c1ab6cc1SMatthias Ringwald     if ((hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) &&
607505f1c30SMatthias Ringwald         (hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS)){
608a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
609ce263fc8SMatthias Ringwald     }
610aa4dd815SMatthias Ringwald }
6113deb3ec6SMatthias Ringwald 
612a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
613c79b4cabSMatthias Ringwald     // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
614a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
615c04cf7ffSMilanka Ringwald     int sent = 0;
616a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
6173deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
618a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
6193deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
620aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
621d715cf51SMatthias Ringwald                     hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
622a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
623a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
624aa4dd815SMatthias Ringwald                     } else {
625a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
626aa4dd815SMatthias Ringwald                     }
627a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
628aa4dd815SMatthias Ringwald                     return 1;
6293deb3ec6SMatthias Ringwald                 default:
6303deb3ec6SMatthias Ringwald                     break;
6313deb3ec6SMatthias Ringwald             }
6323deb3ec6SMatthias Ringwald             break;
6333deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
634c04cf7ffSMilanka Ringwald             sent = codecs_exchange_state_machine(hfp_connection);
635a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
636a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6373deb3ec6SMatthias Ringwald             }
638c04cf7ffSMilanka Ringwald             return sent;
639aa4dd815SMatthias Ringwald 
640aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
641a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
642473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
64384a0c24eSMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
644473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
645473ac565SMatthias Ringwald                 // continue below in state switch
646ad902e3dSMatthias Ringwald             }
647ad902e3dSMatthias Ringwald             break;
648aa4dd815SMatthias Ringwald 
649aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
650a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
651a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
652485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
653aa4dd815SMatthias Ringwald             return 1;
654aa4dd815SMatthias Ringwald 
6553deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
656a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
657a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
658a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
659a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
660a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
661aa4dd815SMatthias Ringwald             } else {
662a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
6633deb3ec6SMatthias Ringwald             }
6648a46ec40SMatthias Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
665aa4dd815SMatthias Ringwald             return 1;
6663deb3ec6SMatthias Ringwald 
667aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
668a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
669a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
670a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
671aa4dd815SMatthias Ringwald             }
672485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
673a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
674a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
675ce263fc8SMatthias Ringwald             }
676aa4dd815SMatthias Ringwald             return 1;
677aa4dd815SMatthias Ringwald 
678aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
679a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
680a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
681485ac19eSMilanka Ringwald             hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
682aa4dd815SMatthias Ringwald             return 1;
683aa4dd815SMatthias Ringwald 
684aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
685a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
686a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
687485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
688aa4dd815SMatthias Ringwald             return 1;
689aa4dd815SMatthias Ringwald 
690aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
691a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
692a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
693485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
694aa4dd815SMatthias Ringwald             return 1;
6953deb3ec6SMatthias Ringwald         default:
6963deb3ec6SMatthias Ringwald             break;
6973deb3ec6SMatthias Ringwald     }
698473ac565SMatthias Ringwald 
699473ac565SMatthias Ringwald     switch (hfp_connection->state){
700473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
701485ac19eSMilanka Ringwald             int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
702473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
703473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
704473ac565SMatthias Ringwald             if (next_segment < num_segments){
705473ac565SMatthias Ringwald                 // prepare sending of next segment
706473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
707473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
708473ac565SMatthias Ringwald             } else {
709473ac565SMatthias Ringwald                 // done, go to next state
710473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
711473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
712473ac565SMatthias Ringwald             }
713473ac565SMatthias Ringwald             return 1;
714473ac565SMatthias Ringwald         }
715473ac565SMatthias Ringwald         default:
716473ac565SMatthias Ringwald             break;
717473ac565SMatthias Ringwald     }
718c04cf7ffSMilanka Ringwald     return 0;
7193deb3ec6SMatthias Ringwald }
7203deb3ec6SMatthias Ringwald 
721a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
722c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
723c04cf7ffSMilanka Ringwald     if (sent) return 1;
724aa4dd815SMatthias Ringwald 
725a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
726aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
727a0ffb263SMatthias Ringwald             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
728485ac19eSMilanka Ringwald             hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
729aa4dd815SMatthias Ringwald             return 1;
730aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
731aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
732a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
733485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
734a0ffb263SMatthias Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
735aa4dd815SMatthias Ringwald             } else {
736485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
737aa4dd815SMatthias Ringwald             }
738aa4dd815SMatthias Ringwald             return 1;
739aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
740485ac19eSMilanka Ringwald             hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
741aa4dd815SMatthias Ringwald             return 1;
742aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
743485ac19eSMilanka Ringwald             hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
744aa4dd815SMatthias Ringwald             return 1;
745aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
746a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
747485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
748aa4dd815SMatthias Ringwald             } else {
749485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
7503deb3ec6SMatthias Ringwald             }
751aa4dd815SMatthias Ringwald             return 1;
752aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
753485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
754aa4dd815SMatthias Ringwald             return 1;
7553deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
756a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
757a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
758485ac19eSMilanka Ringwald                 hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
759aa4dd815SMatthias Ringwald                 return 1;
7603deb3ec6SMatthias Ringwald             }
761202c8a4cSMatthias Ringwald             break;
7623deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
763485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
764ce263fc8SMatthias Ringwald             return 1;
7653deb3ec6SMatthias Ringwald         default:
7663deb3ec6SMatthias Ringwald             break;
7673deb3ec6SMatthias Ringwald     }
768aa4dd815SMatthias Ringwald     return 0;
7693deb3ec6SMatthias Ringwald }
7703deb3ec6SMatthias Ringwald 
771a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
772505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
773505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
7743deb3ec6SMatthias Ringwald 
775aa4dd815SMatthias Ringwald 
776505f1c30SMatthias Ringwald     if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){
777a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
778a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
779a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
780aa4dd815SMatthias Ringwald         return 1;
781aa4dd815SMatthias Ringwald     }
782aa4dd815SMatthias Ringwald 
783a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
784aa4dd815SMatthias Ringwald 
785aa4dd815SMatthias Ringwald     // run codecs exchange
786c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
787c04cf7ffSMilanka Ringwald     if (sent) return 1;
788aa4dd815SMatthias Ringwald 
789c04cf7ffSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
790a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
791a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
792a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
793eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
794aa4dd815SMatthias Ringwald         return 1;
795aa4dd815SMatthias Ringwald     }
796aa4dd815SMatthias Ringwald     return 0;
797aa4dd815SMatthias Ringwald }
798aa4dd815SMatthias Ringwald 
799ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
800665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
801665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
802aa4dd815SMatthias Ringwald 
803665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
804a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
805a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
806a0ffb263SMatthias Ringwald             return hfp_connection;
807aa4dd815SMatthias Ringwald         }
808aa4dd815SMatthias Ringwald     }
809aa4dd815SMatthias Ringwald     return NULL;
810aa4dd815SMatthias Ringwald }
811aa4dd815SMatthias Ringwald 
812ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
813d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
814d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
815aa4dd815SMatthias Ringwald 
816d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
817a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
818a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
819aa4dd815SMatthias Ringwald 
820a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
821a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
822aa4dd815SMatthias Ringwald 
823f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
824aa4dd815SMatthias Ringwald }
825aa4dd815SMatthias Ringwald 
826a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
827a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
828a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
829a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
830a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
831aa4dd815SMatthias Ringwald }
832aa4dd815SMatthias Ringwald 
833a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
834a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
835a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
836aa4dd815SMatthias Ringwald }
837aa4dd815SMatthias Ringwald 
838aa4dd815SMatthias Ringwald //
839aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
840aa4dd815SMatthias Ringwald //
841aa4dd815SMatthias Ringwald 
842a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
843aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
844a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
845d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
846aa4dd815SMatthias Ringwald     } else {
847a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
848a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
849a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
850a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
851ca59be51SMatthias Ringwald         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
852aa4dd815SMatthias Ringwald     }
853aa4dd815SMatthias Ringwald }
854aa4dd815SMatthias Ringwald 
855a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
856a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
857a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
858a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
859ca59be51SMatthias Ringwald     hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGINIG);
860aa4dd815SMatthias Ringwald }
861aa4dd815SMatthias Ringwald 
862aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
863aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
864aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
865aa4dd815SMatthias Ringwald 
866665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
867665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
868665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
869a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
87066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
871a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
872a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
873a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
874d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
875aa4dd815SMatthias Ringwald         }
876a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
877a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
878aa4dd815SMatthias Ringwald         }
879f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
880aa4dd815SMatthias Ringwald     }
881aa4dd815SMatthias Ringwald }
882aa4dd815SMatthias Ringwald 
883aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
884aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
885aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
886aa4dd815SMatthias Ringwald 
887665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
888665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
889665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
890a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
89166c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
892a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
893a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
894f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
895aa4dd815SMatthias Ringwald     }
896aa4dd815SMatthias Ringwald }
897aa4dd815SMatthias Ringwald 
898aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
899aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("call");
900aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
901aa4dd815SMatthias Ringwald 
902665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
903665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
904665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
905a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
90666c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
907a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
908a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
909f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
910aa4dd815SMatthias Ringwald     }
911aa4dd815SMatthias Ringwald }
912aa4dd815SMatthias Ringwald 
913aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
914aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callheld");
915aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
916aa4dd815SMatthias Ringwald 
917665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
918665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
919665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
920a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
92166c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
922a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
923a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
924f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
925aa4dd815SMatthias Ringwald     }
926aa4dd815SMatthias Ringwald }
927aa4dd815SMatthias Ringwald 
928aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
929aa4dd815SMatthias Ringwald 
930aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
931aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
932aa4dd815SMatthias Ringwald 
933665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
934665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
935665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
936a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
93766c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
938505f1c30SMatthias Ringwald         if ((hfp_connection->call_state != HFP_CALL_RINGING) &&
939505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
940aa4dd815SMatthias Ringwald 
941a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
942a0ffb263SMatthias Ringwald         if (hfp_connection == source){
943aa4dd815SMatthias Ringwald 
944aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
945a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
946aa4dd815SMatthias Ringwald             } else {
947a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
948d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
949aa4dd815SMatthias Ringwald             }
950aa4dd815SMatthias Ringwald 
951a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
952a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
953aa4dd815SMatthias Ringwald 
954aa4dd815SMatthias Ringwald         } else {
955a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
956aa4dd815SMatthias Ringwald         }
957f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
958aa4dd815SMatthias Ringwald     }
959aa4dd815SMatthias Ringwald }
960aa4dd815SMatthias Ringwald 
961aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
962aa4dd815SMatthias Ringwald 
963aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
964aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
965aa4dd815SMatthias Ringwald 
966665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
967665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
968665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
969a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
97066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
971a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
972aa4dd815SMatthias Ringwald 
973a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
974a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
975aa4dd815SMatthias Ringwald 
976a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
977a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
978aa4dd815SMatthias Ringwald 
979f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
980aa4dd815SMatthias Ringwald         break;  // only single
981aa4dd815SMatthias Ringwald     }
982aa4dd815SMatthias Ringwald }
983aa4dd815SMatthias Ringwald 
984aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
985aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
986665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
987665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
988665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
989665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
99066c5995fSMatthias Ringwald         if (connection->local_role != HFP_ROLE_AG) continue;
991505f1c30SMatthias Ringwald         if ((connection->call_state != HFP_CALL_RINGING) &&
992505f1c30SMatthias Ringwald             (connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
993aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
994aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
995aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
996f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(connection);
997aa4dd815SMatthias Ringwald     }
998aa4dd815SMatthias Ringwald }
999aa4dd815SMatthias Ringwald 
1000ca59be51SMatthias Ringwald static void hfp_ag_emit_simple_event(uint8_t event_subtype){
1001ca59be51SMatthias Ringwald     uint8_t event[3];
1002ca59be51SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
1003ca59be51SMatthias Ringwald     event[1] = sizeof(event) - 2;
1004ca59be51SMatthias Ringwald     event[2] = event_subtype;
1005ca59be51SMatthias Ringwald     if (!hfp_ag_callback) return;
1006ca59be51SMatthias Ringwald     (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1007ca59be51SMatthias Ringwald }
1008ca59be51SMatthias Ringwald 
1009aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1010aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1011aa4dd815SMatthias Ringwald 
1012665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1013665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1014665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1015a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
101666c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1017d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1018a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1019a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1020a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
10216d78145cSMatthias Ringwald         if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
1022a0ffb263SMatthias Ringwald             hfp_connection->release_audio_connection = 1;
10236d78145cSMatthias Ringwald         }
1024f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1025aa4dd815SMatthias Ringwald     }
1026ca59be51SMatthias Ringwald     hfp_ag_emit_simple_event(HFP_SUBEVENT_CALL_TERMINATED);
1027aa4dd815SMatthias Ringwald }
1028aa4dd815SMatthias Ringwald 
10290cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
1030aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1031aa4dd815SMatthias Ringwald     if (!indicator){
1032d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
103345718b6fSMatthias Ringwald         return;
1034aa4dd815SMatthias Ringwald     };
1035d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1036aa4dd815SMatthias Ringwald }
1037aa4dd815SMatthias Ringwald 
10380cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1039d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1040d210d9c4SMatthias Ringwald     if (!indicator){
1041d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
104245718b6fSMatthias Ringwald         return;
1043d210d9c4SMatthias Ringwald     };
1044d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1045d210d9c4SMatthias Ringwald }
1046d210d9c4SMatthias Ringwald 
10470cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1048aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1049aa4dd815SMatthias Ringwald     if (!indicator){
1050aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
105145718b6fSMatthias Ringwald         return;
1052aa4dd815SMatthias Ringwald     };
1053d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1054aa4dd815SMatthias Ringwald }
1055aa4dd815SMatthias Ringwald 
1056aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1057665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1058665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1059665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1060a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
106166c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1062505f1c30SMatthias Ringwald         if ((hfp_connection->call_state != HFP_CALL_RINGING) &&
1063505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1064a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1065aa4dd815SMatthias Ringwald     }
1066aa4dd815SMatthias Ringwald }
1067aa4dd815SMatthias Ringwald 
1068aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1069665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1070665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1071665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1072a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
107366c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1074a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1075aa4dd815SMatthias Ringwald     }
1076aa4dd815SMatthias Ringwald     return NULL;
1077aa4dd815SMatthias Ringwald }
1078aa4dd815SMatthias Ringwald 
1079a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1080665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1081665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1082665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1083a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
108466c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1085a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1086ce263fc8SMatthias Ringwald     }
1087ce263fc8SMatthias Ringwald }
1088ce263fc8SMatthias Ringwald 
1089a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1090aa4dd815SMatthias Ringwald     int indicator_index;
1091a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1092aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1093a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1094a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1095a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1096a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1097a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1098a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1099ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
1100aa4dd815SMatthias Ringwald             break;
1101aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1102a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1103a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1104a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1105aa4dd815SMatthias Ringwald             break;
1106aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1107a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1108a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1109aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1110a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1111aa4dd815SMatthias Ringwald             break;
1112aa4dd815SMatthias Ringwald         default:
1113aa4dd815SMatthias Ringwald             break;
1114aa4dd815SMatthias Ringwald     }
1115aa4dd815SMatthias Ringwald     return 0;
1116aa4dd815SMatthias Ringwald }
111739426febSMatthias Ringwald 
111839426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below
111939426febSMatthias Ringwald static void hfp_ag_handle_reject_incoming_call(void){
112039426febSMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
112139426febSMatthias Ringwald     if (!hfp_connection){
112239426febSMatthias Ringwald         log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
112339426febSMatthias Ringwald         return;
112439426febSMatthias Ringwald     }
112539426febSMatthias Ringwald 
112639426febSMatthias Ringwald     hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED);
112739426febSMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
112839426febSMatthias Ringwald     hfp_connection->send_error = 1;
112939426febSMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
113039426febSMatthias Ringwald }
113139426febSMatthias Ringwald 
1132a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1133a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1134aa4dd815SMatthias Ringwald     int indicator_index;
1135d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1136d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1137d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
113874386ee0SMatthias Ringwald 
1139d210d9c4SMatthias Ringwald     //printf("hfp_ag_call_sm event %d \n", event);
1140aa4dd815SMatthias Ringwald     switch (event){
1141aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1142d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1143aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1144d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1145aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1146d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1147d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1148aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
114960ebb071SMilanka Ringwald                             log_info("AG rings");
1150aa4dd815SMatthias Ringwald                             break;
1151aa4dd815SMatthias Ringwald                         default:
11523deb3ec6SMatthias Ringwald                             break;
11533deb3ec6SMatthias Ringwald                     }
11543deb3ec6SMatthias Ringwald                     break;
1155aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1156d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1157aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1158d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1159d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1160aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
116160ebb071SMilanka Ringwald                             log_info("AG call waiting");
1162aa4dd815SMatthias Ringwald                             break;
1163aa4dd815SMatthias Ringwald                         default:
11643deb3ec6SMatthias Ringwald                             break;
11653deb3ec6SMatthias Ringwald                     }
11663deb3ec6SMatthias Ringwald                     break;
1167aa4dd815SMatthias Ringwald             }
1168aa4dd815SMatthias Ringwald             break;
1169aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1170d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1171aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1172d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1173aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1174d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1175d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1176d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1177aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
117860ebb071SMilanka Ringwald                             log_info("AG answers call, accept call by GSM");
1179aa4dd815SMatthias Ringwald                             break;
1180aa4dd815SMatthias Ringwald                         default:
11813deb3ec6SMatthias Ringwald                             break;
11823deb3ec6SMatthias Ringwald                     }
1183aa4dd815SMatthias Ringwald                     break;
1184aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1185d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1186aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
118760ebb071SMilanka Ringwald                             log_info("AG: current call is placed on hold, incoming call gets active");
1188d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1189d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1190d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1191ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1192ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1193aa4dd815SMatthias Ringwald                             break;
1194aa4dd815SMatthias Ringwald                         default:
1195aa4dd815SMatthias Ringwald                             break;
1196aa4dd815SMatthias Ringwald                     }
1197aa4dd815SMatthias Ringwald                     break;
1198aa4dd815SMatthias Ringwald             }
1199aa4dd815SMatthias Ringwald             break;
1200ce263fc8SMatthias Ringwald 
1201ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1202d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1203ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1204d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1205ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
120660ebb071SMilanka Ringwald                             log_info("AG: joining held call with active call");
1207d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG);
1208d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1209ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1210ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1211ce263fc8SMatthias Ringwald                             break;
1212ce263fc8SMatthias Ringwald                         default:
1213ce263fc8SMatthias Ringwald                             break;
1214ce263fc8SMatthias Ringwald                     }
1215ce263fc8SMatthias Ringwald                     break;
1216ce263fc8SMatthias Ringwald                 default:
1217ce263fc8SMatthias Ringwald                     break;
1218ce263fc8SMatthias Ringwald             }
1219ce263fc8SMatthias Ringwald             break;
1220ce263fc8SMatthias Ringwald 
1221aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1222d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1223aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1224d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1225aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1226d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF);
1227d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1228d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1229a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
12306323f788SMatthias Ringwald                             hfp_connection->ok_pending = 1;
123160ebb071SMilanka Ringwald                             log_info("HF answers call, accept call by GSM");
1232ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1233aa4dd815SMatthias Ringwald                             break;
1234aa4dd815SMatthias Ringwald                         default:
1235aa4dd815SMatthias Ringwald                             break;
1236aa4dd815SMatthias Ringwald                     }
12373deb3ec6SMatthias Ringwald                     break;
12383deb3ec6SMatthias Ringwald                 default:
12393deb3ec6SMatthias Ringwald                     break;
12403deb3ec6SMatthias Ringwald             }
12413deb3ec6SMatthias Ringwald             break;
12423deb3ec6SMatthias Ringwald 
1243ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1244d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1245ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1246d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1247ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1248d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG);
1249ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1250ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1251a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1252ce263fc8SMatthias Ringwald                             // as with regualr call
1253d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1254d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1255ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
125660ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by AG");
1257ce263fc8SMatthias Ringwald                             break;
1258ce263fc8SMatthias Ringwald                         default:
1259ce263fc8SMatthias Ringwald                             break;
1260ce263fc8SMatthias Ringwald                     }
1261ce263fc8SMatthias Ringwald                     break;
1262ce263fc8SMatthias Ringwald                 default:
1263ce263fc8SMatthias Ringwald                     break;
1264ce263fc8SMatthias Ringwald             }
1265ce263fc8SMatthias Ringwald             break;
1266ce263fc8SMatthias Ringwald 
1267ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1268d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1269ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1270d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1271ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1272d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF);
1273ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1274ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1275a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1276ce263fc8SMatthias Ringwald                             // as with regualr call
1277d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1278d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1279a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
128060ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by HF");
1281ce263fc8SMatthias Ringwald                             break;
1282ce263fc8SMatthias Ringwald                         default:
1283ce263fc8SMatthias Ringwald                             break;
1284ce263fc8SMatthias Ringwald                     }
1285ce263fc8SMatthias Ringwald                     break;
1286ce263fc8SMatthias Ringwald                 default:
1287ce263fc8SMatthias Ringwald                     break;
1288ce263fc8SMatthias Ringwald             }
1289ce263fc8SMatthias Ringwald             break;
1290ce263fc8SMatthias Ringwald 
1291ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1292ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1293ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1294ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1295d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG);
1296a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1297ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1298a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
129960ebb071SMilanka Ringwald             log_info("Held Call accepted and active");
1300ce263fc8SMatthias Ringwald             break;
1301ce263fc8SMatthias Ringwald 
1302ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1303ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1304ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1305ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1306d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG);
1307a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1308ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1309a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1310ce263fc8SMatthias Ringwald             // from terminate by ag
1311d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1312ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1313ce263fc8SMatthias Ringwald             break;
1314ce263fc8SMatthias Ringwald 
1315aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1316d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1317aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1318d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1319aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1320d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1321d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1322aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1323aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
132460ebb071SMilanka Ringwald                             log_info("HF Rejected Incoming call, AG terminate call");
1325aa4dd815SMatthias Ringwald                             break;
1326aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1327aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1328d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1329d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1330aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
133160ebb071SMilanka Ringwald                             log_info("AG terminate outgoing call process");
1332202c8a4cSMatthias Ringwald                             break;
1333aa4dd815SMatthias Ringwald                         default:
1334aa4dd815SMatthias Ringwald                             break;
1335aa4dd815SMatthias Ringwald                     }
1336aa4dd815SMatthias Ringwald                     break;
1337aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1338d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1339d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1340aa4dd815SMatthias Ringwald                     hfp_ag_transfer_call_state();
1341a0ffb263SMatthias Ringwald                     hfp_connection->call_state = HFP_CALL_IDLE;
134260ebb071SMilanka Ringwald                     log_info("AG terminate call");
1343aa4dd815SMatthias Ringwald                     break;
1344aa4dd815SMatthias Ringwald             }
13450c1b8529SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1346aa4dd815SMatthias Ringwald             break;
13473deb3ec6SMatthias Ringwald 
1348aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1349d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1350aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1351d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1352aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1353d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1354d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1355aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
135660ebb071SMilanka Ringwald                             log_info("AG Rejected Incoming call, AG terminate call");
1357aa4dd815SMatthias Ringwald                             break;
1358aa4dd815SMatthias Ringwald                         default:
1359aa4dd815SMatthias Ringwald                             break;
13603deb3ec6SMatthias Ringwald                     }
1361202c8a4cSMatthias Ringwald                     break;
1362aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1363d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1364d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1365d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1366aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
136760ebb071SMilanka Ringwald                     log_info("AG terminate call");
1368aa4dd815SMatthias Ringwald                     break;
1369aa4dd815SMatthias Ringwald                 default:
13703deb3ec6SMatthias Ringwald                     break;
13713deb3ec6SMatthias Ringwald             }
13723deb3ec6SMatthias Ringwald             break;
1373aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1374d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1375aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1376d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1377aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1378aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
137960ebb071SMilanka Ringwald                             log_info("Incoming call interrupted");
1380aa4dd815SMatthias Ringwald                             break;
1381aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1382aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
138360ebb071SMilanka Ringwald                             log_info("Outgoing call interrupted\n");
138460ebb071SMilanka Ringwald                             log_info("AG notify call dropped\n");
1385aa4dd815SMatthias Ringwald                             break;
1386aa4dd815SMatthias Ringwald                         default:
13873deb3ec6SMatthias Ringwald                             break;
13883deb3ec6SMatthias Ringwald                     }
1389d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1390d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1391aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
139210c236bfSMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1393aa4dd815SMatthias Ringwald                     break;
1394aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1395ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1396d210d9c4SMatthias Ringwald                         hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1397ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1398a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1399ce263fc8SMatthias Ringwald                     }
1400d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1401d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1402d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1403aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
140460ebb071SMilanka Ringwald                     log_info("AG notify call dropped");
1405aa4dd815SMatthias Ringwald                     break;
1406aa4dd815SMatthias Ringwald                 default:
1407aa4dd815SMatthias Ringwald                     break;
1408aa4dd815SMatthias Ringwald             }
1409aa4dd815SMatthias Ringwald             break;
1410aa4dd815SMatthias Ringwald 
1411aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1412d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1413d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1414a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1415f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1416d210d9c4SMatthias Ringwald                 break;
1417d210d9c4SMatthias Ringwald             }
1418a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]);
14199cae807eSMatthias Ringwald 
1420a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
142174386ee0SMatthias Ringwald 
1422ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1423aa4dd815SMatthias Ringwald             break;
1424aa4dd815SMatthias Ringwald 
14259cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1426d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1427d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1428a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1429f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1430d210d9c4SMatthias Ringwald                 break;
1431d210d9c4SMatthias Ringwald             }
1432d210d9c4SMatthias Ringwald 
143374386ee0SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED);
1434a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
143574386ee0SMatthias Ringwald 
143660ebb071SMilanka Ringwald             log_info("Redial last number");
14379cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1438aa4dd815SMatthias Ringwald 
14399cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
144060ebb071SMilanka Ringwald                 log_info("Last number exists: accept call");
1441ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
14429cae807eSMatthias Ringwald             } else {
144360ebb071SMilanka Ringwald                 log_info("log_infoLast number missing: reject call");
144439426febSMatthias Ringwald                 hfp_ag_handle_reject_incoming_call();
14459cae807eSMatthias Ringwald             }
14469cae807eSMatthias Ringwald             break;
14479cae807eSMatthias Ringwald         }
1448aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
144939426febSMatthias Ringwald             hfp_ag_handle_reject_incoming_call();
1450aa4dd815SMatthias Ringwald             break;
1451aa4dd815SMatthias Ringwald 
1452d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1453a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1454a0ffb263SMatthias Ringwald             if (!hfp_connection){
1455a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1456aa4dd815SMatthias Ringwald                 break;
1457aa4dd815SMatthias Ringwald             }
1458aa4dd815SMatthias Ringwald 
1459a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1460a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1461aa4dd815SMatthias Ringwald 
1462aa4dd815SMatthias Ringwald             // trigger callsetup to be
1463d0c20769SMatthias Ringwald             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1464d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED);
1465d210d9c4SMatthias Ringwald 
1466d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1467aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1468a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1469aa4dd815SMatthias Ringwald 
1470aa4dd815SMatthias Ringwald             // put current call on hold if active
1471d210d9c4SMatthias Ringwald             if (put_call_on_hold){
147260ebb071SMilanka Ringwald                 log_info("AG putting current call on hold for new outgoing calllog_info");
1473d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1474aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1475485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1476aa4dd815SMatthias Ringwald             }
1477aa4dd815SMatthias Ringwald 
1478aa4dd815SMatthias Ringwald             // start audio if needed
1479d97d752dSMilanka Ringwald             hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1480aa4dd815SMatthias Ringwald             break;
1481d210d9c4SMatthias Ringwald         }
1482aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1483a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1484a0ffb263SMatthias Ringwald             if (!hfp_connection){
1485a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1486aa4dd815SMatthias Ringwald                 break;
1487aa4dd815SMatthias Ringwald             }
1488d210d9c4SMatthias Ringwald 
1489d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING);
1490a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1491d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1492aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1493aa4dd815SMatthias Ringwald             break;
1494aa4dd815SMatthias Ringwald 
1495d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1496aa4dd815SMatthias Ringwald             // get outgoing call
1497a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1498a0ffb263SMatthias Ringwald             if (!hfp_connection){
1499a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1500aa4dd815SMatthias Ringwald             }
1501a0ffb263SMatthias Ringwald             if (!hfp_connection){
1502a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1503aa4dd815SMatthias Ringwald                 break;
1504aa4dd815SMatthias Ringwald             }
1505d210d9c4SMatthias Ringwald 
1506d0c20769SMatthias 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;
1507d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED);
1508a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1509d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1510d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1511aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1512aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1513d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1514d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1515aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
15163deb3ec6SMatthias Ringwald             }
15173deb3ec6SMatthias Ringwald             break;
1518d210d9c4SMatthias Ringwald         }
1519d210d9c4SMatthias Ringwald 
1520d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1521d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY);
1522d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1523a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1524a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
152560ebb071SMilanka Ringwald             log_info("AG: Call Waiting, User Busy");
1526d210d9c4SMatthias Ringwald             break;
1527d210d9c4SMatthias Ringwald 
1528d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1529d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1530d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1531d210d9c4SMatthias Ringwald 
1532d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1533d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1534a0ffb263SMatthias Ringwald                 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1535d0c20769SMatthias Ringwald 
1536d0c20769SMatthias Ringwald             }
1537d0c20769SMatthias Ringwald 
1538d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
153960ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Accept new call");
1540d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1541a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1542d210d9c4SMatthias Ringwald             } else {
154360ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Resume held call");
1544d210d9c4SMatthias Ringwald             }
1545d210d9c4SMatthias Ringwald             if (call_held){
1546d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1547a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1548d210d9c4SMatthias Ringwald             }
1549d0c20769SMatthias Ringwald 
1550a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1551d210d9c4SMatthias Ringwald             break;
1552d210d9c4SMatthias Ringwald         }
1553d0c20769SMatthias Ringwald 
1554d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1555d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1556d210d9c4SMatthias Ringwald             // only update if callsetup changed
1557d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1558a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1559d0c20769SMatthias Ringwald 
1560d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
156160ebb071SMilanka Ringwald                 log_info("AG: Call on Hold, Accept new call");
1562d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1563a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1564d210d9c4SMatthias Ringwald             } else {
156560ebb071SMilanka Ringwald                 log_info("AG: Swap calls");
1566d210d9c4SMatthias Ringwald             }
1567d0c20769SMatthias Ringwald 
1568d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1569d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1570a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1571a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1572d210d9c4SMatthias Ringwald             break;
1573d210d9c4SMatthias Ringwald         }
1574d0c20769SMatthias Ringwald 
1575d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1576d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1577d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
157860ebb071SMilanka Ringwald                 log_info("AG: Join 3-way-call");
1579d210d9c4SMatthias Ringwald                 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL);
1580d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1581a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1582ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1583d210d9c4SMatthias Ringwald             }
1584a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1585d210d9c4SMatthias Ringwald             break;
1586d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1587d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1588d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS);
158960ebb071SMilanka Ringwald             log_info("AG: Transfer call -> Connect two calls and disconnect");
1590d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1591d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1592a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1593a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1594a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1595d210d9c4SMatthias Ringwald             break;
1596ce263fc8SMatthias Ringwald 
15973deb3ec6SMatthias Ringwald         default:
15983deb3ec6SMatthias Ringwald             break;
15993deb3ec6SMatthias Ringwald     }
1600d210d9c4SMatthias Ringwald 
1601d0c20769SMatthias Ringwald 
16023deb3ec6SMatthias Ringwald }
16033deb3ec6SMatthias Ringwald 
16049cae807eSMatthias Ringwald 
1605a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
16069cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
16079cae807eSMatthias Ringwald     if (!active_call) return;
16089cae807eSMatthias Ringwald 
16099cae807eSMatthias Ringwald     int idx = active_call->index;
16109cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
16119cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
16129cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
16139cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
16149cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
16159cae807eSMatthias Ringwald     char * number = active_call->clip_number;
16169cae807eSMatthias Ringwald 
16179cae807eSMatthias Ringwald     char buffer[100];
16189cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
16199cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
16209cae807eSMatthias Ringwald     if (number){
16211cc1d9e9SMilanka Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
16229cae807eSMatthias Ringwald     }
16239cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
162460ebb071SMilanka 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,
16259cae807eSMatthias Ringwald        mode, mpty, type, number);
1626a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
16279cae807eSMatthias Ringwald }
16289cae807eSMatthias Ringwald 
1629473ac565SMatthias Ringwald 
1630febc14f5SMatthias Ringwald // sends pending command, returns if command was sent
1631febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){
163222387625SMatthias Ringwald 
1633a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1634a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1635a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1636a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1637a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
1638febc14f5SMatthias Ringwald             return 1;
16399cae807eSMatthias Ringwald         } else {
1640febc14f5SMatthias Ringwald             // TODO: this code should be removed. check a) before setting send_status_of_current_calls, or b) right before hfp_ag_send_call_status above
1641a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1642a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1643a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16449cae807eSMatthias Ringwald         }
1645ce263fc8SMatthias Ringwald     }
1646ce263fc8SMatthias Ringwald 
1647a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1648a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1649a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1650febc14f5SMatthias Ringwald         return 1;
1651aa4dd815SMatthias Ringwald     }
1652aa4dd815SMatthias Ringwald 
1653a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1654a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1655a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1656a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1657485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1658febc14f5SMatthias Ringwald         return 1;
1659a0ffb263SMatthias Ringwald     }
1660a0ffb263SMatthias Ringwald 
1661a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1662a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1663a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1664485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1665febc14f5SMatthias Ringwald         return 1;
1666aa4dd815SMatthias Ringwald     }
1667aa4dd815SMatthias Ringwald 
1668ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1669a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1670a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1671a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1672485ac19eSMilanka Ringwald         hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1673febc14f5SMatthias Ringwald         return 1;
1674ce263fc8SMatthias Ringwald     }
1675ce263fc8SMatthias Ringwald 
1676a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1677a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1678a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1679485ac19eSMilanka Ringwald         hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1680febc14f5SMatthias Ringwald         return 1;
1681ce263fc8SMatthias Ringwald     }
1682ce263fc8SMatthias Ringwald 
1683aa4dd815SMatthias Ringwald     // update AG indicators
1684a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1685aa4dd815SMatthias Ringwald         int i;
1686a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1687a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1688a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1689a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1690ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1691ce263fc8SMatthias Ringwald                     break;
1692ce263fc8SMatthias Ringwald                 }
1693485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1694febc14f5SMatthias Ringwald                 return 1;
1695aa4dd815SMatthias Ringwald             }
1696aa4dd815SMatthias Ringwald         }
1697aa4dd815SMatthias Ringwald     }
1698aa4dd815SMatthias Ringwald 
1699a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1700a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1701a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1702485ac19eSMilanka Ringwald         hfp_ag_send_ring(hfp_connection->rfcomm_cid);
1703febc14f5SMatthias Ringwald         return 1;
1704aa4dd815SMatthias Ringwald     }
1705aa4dd815SMatthias Ringwald 
1706a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1707a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1708a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1709a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1710febc14f5SMatthias Ringwald         return 1;
1711aa4dd815SMatthias Ringwald     }
1712aa4dd815SMatthias Ringwald 
1713a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1714a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1715a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1716a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1717a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1718febc14f5SMatthias Ringwald         return 1;
1719aa4dd815SMatthias Ringwald     }
1720aa4dd815SMatthias Ringwald 
1721a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1722a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1723a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1724a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1725ce263fc8SMatthias Ringwald         } else {
1726a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1727a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1728485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1729ce263fc8SMatthias Ringwald         }
1730a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1731febc14f5SMatthias Ringwald         return 1;
1732ce263fc8SMatthias Ringwald     }
1733ce263fc8SMatthias Ringwald 
1734a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1735a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1736a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1737485ac19eSMilanka Ringwald         hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1738febc14f5SMatthias Ringwald         return 1;
1739aa4dd815SMatthias Ringwald     }
1740aa4dd815SMatthias Ringwald 
1741a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1742a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1743a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1744485ac19eSMilanka Ringwald         hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
1745febc14f5SMatthias Ringwald         return 1;
17463deb3ec6SMatthias Ringwald     }
17473deb3ec6SMatthias Ringwald 
1748a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1749a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1750485ac19eSMilanka Ringwald         hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1751febc14f5SMatthias Ringwald         return 1;
1752febc14f5SMatthias Ringwald     }
1753febc14f5SMatthias Ringwald 
1754febc14f5SMatthias Ringwald     return 0;
1755febc14f5SMatthias Ringwald }
1756febc14f5SMatthias Ringwald 
1757febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){
1758febc14f5SMatthias Ringwald 
1759febc14f5SMatthias Ringwald     if (!hfp_connection) return;
1760febc14f5SMatthias Ringwald 
1761febc14f5SMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
1762febc14f5SMatthias Ringwald 
1763febc14f5SMatthias Ringwald     if (hfp_connection->local_role != HFP_ROLE_AG) {
1764febc14f5SMatthias Ringwald         log_info("HFP AG%p, wrong role %u", hfp_connection, hfp_connection->local_role);
1765ce263fc8SMatthias Ringwald         return;
1766ce263fc8SMatthias Ringwald     }
1767ce263fc8SMatthias Ringwald 
1768febc14f5SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1769febc14f5SMatthias Ringwald         log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1770febc14f5SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1771febc14f5SMatthias Ringwald         return;
1772febc14f5SMatthias Ringwald     }
1773febc14f5SMatthias Ringwald 
1774febc14f5SMatthias Ringwald     int cmd_sent = hfp_ag_send_commands(hfp_connection);
1775febc14f5SMatthias Ringwald 
17762ee45eb2SMilanka Ringwald     // trigger codec exchange if requested by app
17772ee45eb2SMilanka Ringwald     if (hfp_connection->trigger_codec_exchange){
17782ee45eb2SMilanka Ringwald         log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state);
17792ee45eb2SMilanka Ringwald     }
1780febc14f5SMatthias Ringwald 
1781c1ab6cc1SMatthias Ringwald     if (hfp_connection->trigger_codec_exchange && (hfp_connection->command == HFP_CMD_NONE)){
17822ee45eb2SMilanka Ringwald         switch (hfp_connection->codecs_state){
17832ee45eb2SMilanka Ringwald             case HFP_CODECS_IDLE:
17842ee45eb2SMilanka Ringwald             case HFP_CODECS_RECEIVED_LIST:
17852ee45eb2SMilanka Ringwald             case HFP_CODECS_AG_RESEND_COMMON_CODEC:
17862ee45eb2SMilanka Ringwald             case HFP_CODECS_ERROR:
17872ee45eb2SMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 0;
17882ee45eb2SMilanka Ringwald                 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
17892ee45eb2SMilanka Ringwald                 break;
17902ee45eb2SMilanka Ringwald             default:
17912ee45eb2SMilanka Ringwald                 break;
17922ee45eb2SMilanka Ringwald         }
17932ee45eb2SMilanka Ringwald     }
17942ee45eb2SMilanka Ringwald 
1795febc14f5SMatthias Ringwald     if (!cmd_sent){
1796febc14f5SMatthias Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1797febc14f5SMatthias Ringwald     }
1798febc14f5SMatthias Ringwald 
1799c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1800c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
18013deb3ec6SMatthias Ringwald     }
18023deb3ec6SMatthias Ringwald 
1803c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1804c04cf7ffSMilanka Ringwald         cmd_sent = call_setup_state_machine(hfp_connection);
1805aa4dd815SMatthias Ringwald     }
1806aa4dd815SMatthias Ringwald 
1807c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1808c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
1809aa4dd815SMatthias Ringwald     }
18103deb3ec6SMatthias Ringwald 
1811c1ab6cc1SMatthias Ringwald     if ((hfp_connection->command == HFP_CMD_NONE) && !cmd_sent){
1812a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1813a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
18143deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1815a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1816a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
18173deb3ec6SMatthias Ringwald                 break;
18183deb3ec6SMatthias Ringwald             default:
18193deb3ec6SMatthias Ringwald                 break;
18203deb3ec6SMatthias Ringwald         }
18213deb3ec6SMatthias Ringwald     }
1822c04cf7ffSMilanka Ringwald 
1823c04cf7ffSMilanka Ringwald     if (cmd_sent){
1824a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1825473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1826473ac565SMatthias Ringwald     }
18273deb3ec6SMatthias Ringwald }
1828d68dcce1SMatthias Ringwald 
1829ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1830ce263fc8SMatthias Ringwald     int i;
1831a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1832a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1833ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1834ce263fc8SMatthias Ringwald             return indicator;
1835ce263fc8SMatthias Ringwald         }
1836ce263fc8SMatthias Ringwald     }
1837ce263fc8SMatthias Ringwald     return NULL;
1838ce263fc8SMatthias Ringwald }
18393deb3ec6SMatthias Ringwald 
1840e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1841c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
1842e0d09929SMatthias Ringwald }
1843e0d09929SMatthias Ringwald 
1844e9c22d4eSMatthias Ringwald static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
18458a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
18468a46ec40SMatthias Ringwald 
18478a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
18488a46ec40SMatthias Ringwald     if (size < 1) return;
18499ec2630cSMatthias Ringwald 
1850a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1851a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
18521e35c04dSMatthias Ringwald 
1853186dd3d2SMatthias Ringwald     hfp_log_rfcomm_message("HFP_AG_RX", packet, size);
18541e35c04dSMatthias Ringwald 
18558a46ec40SMatthias Ringwald     // process messages byte-wise
18563deb3ec6SMatthias Ringwald     int pos;
18573deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1858a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1859186dd3d2SMatthias Ringwald 
1860e0d09929SMatthias Ringwald         // parse until end of line
1861e0d09929SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1862e0d09929SMatthias Ringwald 
1863186dd3d2SMatthias Ringwald         hfp_generic_status_indicator_t * indicator;
1864a0ffb263SMatthias Ringwald         switch(hfp_connection->command){
1865ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1866ce263fc8SMatthias Ringwald                 if (hfp_ag_response_and_hold_active){
1867a0ffb263SMatthias Ringwald                     hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1868ce263fc8SMatthias Ringwald                 }
1869a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1870ce263fc8SMatthias Ringwald                 break;
1871ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
18720222a807SMatthias Ringwald                 switch(hfp_connection->ag_response_and_hold_action){
1873ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1874a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1875ce263fc8SMatthias Ringwald                         break;
1876ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1877a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1878ce263fc8SMatthias Ringwald                         break;
1879ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1880a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1881ce263fc8SMatthias Ringwald                         break;
1882ce263fc8SMatthias Ringwald                     default:
1883ce263fc8SMatthias Ringwald                         break;
1884ce263fc8SMatthias Ringwald                 }
1885a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1886ce263fc8SMatthias Ringwald                 break;
1887ce263fc8SMatthias Ringwald             case HFP_CMD_HF_INDICATOR_STATUS:
1888a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1889ce263fc8SMatthias Ringwald                 // find indicator by assigned number
1890a0ffb263SMatthias Ringwald                 indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1891ce263fc8SMatthias Ringwald                 if (!indicator){
1892a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
1893ce263fc8SMatthias Ringwald                     break;
1894ce263fc8SMatthias Ringwald                 }
1895ce263fc8SMatthias Ringwald                 switch (indicator->uuid){
1896ce263fc8SMatthias Ringwald                     case 1: // enhanced security
18970222a807SMatthias Ringwald                         if (hfp_connection->parser_indicator_value > 1) {
1898a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1899ce263fc8SMatthias Ringwald                             return;
1900ce263fc8SMatthias Ringwald                         }
19010222a807SMatthias Ringwald                         log_info("HF Indicator 'enhanced security' set to %u", hfp_connection->parser_indicator_value);
1902ce263fc8SMatthias Ringwald                         break;
1903ce263fc8SMatthias Ringwald                     case 2: // battery level
19040222a807SMatthias Ringwald                         if (hfp_connection->parser_indicator_value > 100){
1905a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1906ce263fc8SMatthias Ringwald                             return;
1907ce263fc8SMatthias Ringwald                         }
19080222a807SMatthias Ringwald                         log_info("HF Indicator 'battery' set to %u", hfp_connection->parser_indicator_value);
1909ce263fc8SMatthias Ringwald                         break;
1910ce263fc8SMatthias Ringwald                     default:
19110222a807SMatthias Ringwald                         log_info("HF Indicator unknown set to %u", hfp_connection->parser_indicator_value);
1912ce263fc8SMatthias Ringwald                         break;
1913ce263fc8SMatthias Ringwald                 }
1914a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1915ce263fc8SMatthias Ringwald                 break;
1916ce263fc8SMatthias Ringwald             case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1917ce263fc8SMatthias Ringwald                 // expected by SLC state machine
1918a0ffb263SMatthias Ringwald                 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1919a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
1920a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_status_indicators = 1;
1921ce263fc8SMatthias Ringwald                 break;
1922ce263fc8SMatthias Ringwald             case HFP_CMD_LIST_CURRENT_CALLS:
1923a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1924a0ffb263SMatthias Ringwald                 hfp_connection->next_call_index = 0;
1925a0ffb263SMatthias Ringwald                 hfp_connection->send_status_of_current_calls = 1;
1926ce263fc8SMatthias Ringwald                 break;
1927ce263fc8SMatthias Ringwald             case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1928ce263fc8SMatthias Ringwald                 if (subscriber_numbers_count == 0){
1929485ac19eSMilanka Ringwald                     hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1930ce263fc8SMatthias Ringwald                     break;
1931ce263fc8SMatthias Ringwald                 }
1932a0ffb263SMatthias Ringwald                 hfp_connection->next_subscriber_number_to_send = 0;
1933a0ffb263SMatthias Ringwald                 hfp_connection->send_subscriber_number = 1;
1934ce263fc8SMatthias Ringwald                 break;
1935c1797c7dSMatthias Ringwald             case HFP_CMD_TRANSMIT_DTMF_CODES:
19360222a807SMatthias Ringwald             {
1937a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
19380222a807SMatthias Ringwald                 char buffer[2];
19390222a807SMatthias Ringwald                 buffer[0] = (char) hfp_connection->ag_dtmf_code;
19400222a807SMatthias Ringwald                 buffer[1] = 0;
19410222a807SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer);
1942c1797c7dSMatthias Ringwald                 break;
19430222a807SMatthias Ringwald             }
1944aa4dd815SMatthias Ringwald             case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1945a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1946ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1947aa4dd815SMatthias Ringwald                 break;
1948aa4dd815SMatthias Ringwald             case HFP_CMD_TURN_OFF_EC_AND_NR:
1949a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1950aa4dd815SMatthias Ringwald                 if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1951a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
1952a0ffb263SMatthias Ringwald                     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
195360ebb071SMilanka Ringwald                     log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction);
1954aa4dd815SMatthias Ringwald                 } else {
1955a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
1956aa4dd815SMatthias Ringwald                 }
1957aa4dd815SMatthias Ringwald                 break;
1958aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_ANSWERED:
1959a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
196060ebb071SMilanka Ringwald                 log_info("HFP: ATA");
1961a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1962aa4dd815SMatthias Ringwald                 break;
1963aa4dd815SMatthias Ringwald             case HFP_CMD_HANG_UP_CALL:
1964a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1965a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1966a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1967aa4dd815SMatthias Ringwald                 break;
1968aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_HOLD: {
1969a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1970a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1971d0c20769SMatthias Ringwald 
19720222a807SMatthias Ringwald                 switch (hfp_connection->ag_call_hold_action){
19730222a807SMatthias Ringwald                     case 0:
1974d0c20769SMatthias Ringwald                         // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1975a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1976aa4dd815SMatthias Ringwald                         break;
19770222a807SMatthias Ringwald                     case 1:
1978d0c20769SMatthias Ringwald                         // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1979d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
1980d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
1981a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1982aa4dd815SMatthias Ringwald                         break;
19830222a807SMatthias Ringwald                     case 2:
1984d0c20769SMatthias Ringwald                         // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1985d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
1986d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
1987a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1988aa4dd815SMatthias Ringwald                         break;
19890222a807SMatthias Ringwald                     case 3:
1990d0c20769SMatthias Ringwald                         // Adds a held call to the conversation.
1991a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1992aa4dd815SMatthias Ringwald                         break;
19930222a807SMatthias Ringwald                     case 4:
1994d0c20769SMatthias Ringwald                         // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1995a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1996aa4dd815SMatthias Ringwald                         break;
1997aa4dd815SMatthias Ringwald                     default:
1998aa4dd815SMatthias Ringwald                         break;
1999aa4dd815SMatthias Ringwald                 }
2000aa4dd815SMatthias Ringwald                 break;
20010222a807SMatthias Ringwald                 hfp_connection->call_index = 0;
2002aa4dd815SMatthias Ringwald             }
2003aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_PHONE_NUMBER:
2004a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2005a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
2006aa4dd815SMatthias Ringwald                 break;
2007aa4dd815SMatthias Ringwald             case HFP_CMD_REDIAL_LAST_NUMBER:
2008a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2009a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
2010aa4dd815SMatthias Ringwald                 break;
2011aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CLIP:
2012a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2013a0ffb263SMatthias Ringwald                 log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
2014a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2015aa4dd815SMatthias Ringwald                 break;
2016aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
2017a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2018a0ffb263SMatthias Ringwald                 log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
2019a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2020aa4dd815SMatthias Ringwald                 break;
2021aa4dd815SMatthias Ringwald             case HFP_CMD_SET_SPEAKER_GAIN:
2022a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2023a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
202460ebb071SMilanka Ringwald                 log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
2025aa4dd815SMatthias Ringwald                 break;
2026aa4dd815SMatthias Ringwald             case HFP_CMD_SET_MICROPHONE_GAIN:
2027a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2028a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
202960ebb071SMilanka Ringwald                 log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
2030aa4dd815SMatthias Ringwald                 break;
2031aa4dd815SMatthias Ringwald             default:
2032aa4dd815SMatthias Ringwald                 break;
20333deb3ec6SMatthias Ringwald         }
20343deb3ec6SMatthias Ringwald     }
20350cef86faSMatthias Ringwald }
20363deb3ec6SMatthias Ringwald 
2037aa4dd815SMatthias Ringwald static void hfp_run(void){
2038d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
2039d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2040d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2041d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
204222387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2043f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
20443deb3ec6SMatthias Ringwald     }
20453deb3ec6SMatthias Ringwald }
20463deb3ec6SMatthias Ringwald 
2047e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
20483deb3ec6SMatthias Ringwald     switch (packet_type){
20493deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
2050e9c22d4eSMatthias Ringwald             hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size);
20513deb3ec6SMatthias Ringwald             break;
20523deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2053e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2054e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2055f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2056e30a6a47SMatthias Ringwald                 return;
2057e30a6a47SMatthias Ringwald             }
205827950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2059aa4dd815SMatthias Ringwald             break;
20603deb3ec6SMatthias Ringwald         default:
20613deb3ec6SMatthias Ringwald             break;
20623deb3ec6SMatthias Ringwald     }
20633deb3ec6SMatthias Ringwald 
20643deb3ec6SMatthias Ringwald     hfp_run();
20653deb3ec6SMatthias Ringwald }
20663deb3ec6SMatthias Ringwald 
2067405014fbSMatthias Ringwald static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2068405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
206905bed9f6SMatthias Ringwald 
207005bed9f6SMatthias Ringwald     // allow for sco established -> ring transition
207105bed9f6SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
207205bed9f6SMatthias Ringwald     if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return;
207305bed9f6SMatthias Ringwald     hfp_run();
2074405014fbSMatthias Ringwald }
2075405014fbSMatthias Ringwald 
2076a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
20773deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
20783deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
20793deb3ec6SMatthias Ringwald         return;
20803deb3ec6SMatthias Ringwald     }
20813deb3ec6SMatthias Ringwald     int i;
2082a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
20833deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
20843deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
20853deb3ec6SMatthias Ringwald     }
2086a0ffb263SMatthias Ringwald }
20873deb3ec6SMatthias Ringwald 
2088a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2089a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2090a0ffb263SMatthias Ringwald }
20913deb3ec6SMatthias Ringwald 
2092a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2093a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
20946535961aSMatthias Ringwald     (void)memcpy(hfp_ag_indicators, ag_indicators,
20956535961aSMatthias Ringwald                  ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2096a0ffb263SMatthias Ringwald }
20973deb3ec6SMatthias Ringwald 
2098a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
2099*25789943SMilanka Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return;
2100a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
21016535961aSMatthias Ringwald     (void)memcpy(hfp_generic_status_indicators, hf_indicators,
21026535961aSMatthias Ringwald                  hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2103a0ffb263SMatthias Ringwald }
2104a0ffb263SMatthias Ringwald 
2105a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
21063deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
21076535961aSMatthias Ringwald     (void)memcpy(hfp_ag_call_hold_services, call_hold_services,
21086535961aSMatthias Ringwald                  call_hold_services_nr * sizeof(char *));
2109a0ffb263SMatthias Ringwald }
2110a0ffb263SMatthias Ringwald 
2111a0ffb263SMatthias Ringwald 
2112a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
211327950165SMatthias Ringwald 
2114520c92d5SMatthias Ringwald     hfp_init();
2115d63c37a1SMatthias Ringwald 
2116405014fbSMatthias Ringwald     hci_event_callback_registration.callback = &hci_event_packet_handler;
211727950165SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
211827950165SMatthias Ringwald 
2119e9c22d4eSMatthias Ringwald     rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
212027950165SMatthias Ringwald 
212127950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
2122e9c22d4eSMatthias Ringwald     hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler);
2123aa4dd815SMatthias Ringwald 
2124d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2125d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2126d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2127d210d9c4SMatthias Ringwald 
2128d210d9c4SMatthias Ringwald     hfp_gsm_init();
21293deb3ec6SMatthias Ringwald }
21303deb3ec6SMatthias Ringwald 
21313deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
2132323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
21333deb3ec6SMatthias Ringwald }
21343deb3ec6SMatthias Ringwald 
2135d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
21369c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2137a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2138a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2139a33eb0c4SMilanka Ringwald         return;
2140a33eb0c4SMilanka Ringwald     }
2141a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2142f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21433deb3ec6SMatthias Ringwald }
21443deb3ec6SMatthias Ringwald 
2145d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
21469c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2147a0ffb263SMatthias Ringwald     if (!hfp_connection){
2148a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
21493deb3ec6SMatthias Ringwald         return;
21503deb3ec6SMatthias Ringwald     }
2151a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2152a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
21533deb3ec6SMatthias Ringwald         return;
21543deb3ec6SMatthias Ringwald     }
2155a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2156f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21573deb3ec6SMatthias Ringwald }
21583deb3ec6SMatthias Ringwald 
2159a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
21602ee45eb2SMilanka Ringwald     log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state);
2161a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2162a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
21633deb3ec6SMatthias Ringwald 
2164a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2165a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2166d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2167d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2168a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
21697522e673SMatthias Ringwald         // now, pick link settings
2170afac2a04SMilanka Ringwald         hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
21712ee45eb2SMilanka Ringwald         return;
21723deb3ec6SMatthias Ringwald     }
2173aa4dd815SMatthias Ringwald 
21742ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 1;
2175aa4dd815SMatthias Ringwald }
2176aa4dd815SMatthias Ringwald 
2177d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
21789c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2179a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2180a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2181a33eb0c4SMilanka Ringwald         return;
2182a33eb0c4SMilanka Ringwald     }
21832ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 0;
2184a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2185a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2186f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21873deb3ec6SMatthias Ringwald }
21883deb3ec6SMatthias Ringwald 
2189d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
21909c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2191a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2192a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2193a33eb0c4SMilanka Ringwald         return;
2194a33eb0c4SMilanka Ringwald     }
2195a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2196f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21973deb3ec6SMatthias Ringwald }
2198aa4dd815SMatthias Ringwald 
2199aa4dd815SMatthias Ringwald /**
2200aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2201aa4dd815SMatthias Ringwald  */
2202aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2203aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2204aa4dd815SMatthias Ringwald         return;
2205aa4dd815SMatthias Ringwald     }
2206aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2207aa4dd815SMatthias Ringwald 
2208665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2209665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2210665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2211a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
221266c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2213a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2214f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2215aa4dd815SMatthias Ringwald     }
2216aa4dd815SMatthias Ringwald }
2217aa4dd815SMatthias Ringwald 
2218aa4dd815SMatthias Ringwald /**
2219aa4dd815SMatthias Ringwald  * @brief Called from GSM
2220aa4dd815SMatthias Ringwald  */
2221aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2222aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2223aa4dd815SMatthias Ringwald }
2224aa4dd815SMatthias Ringwald 
2225aa4dd815SMatthias Ringwald /**
2226aa4dd815SMatthias Ringwald  * @brief number is stored.
2227aa4dd815SMatthias Ringwald  */
2228aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2229d0c20769SMatthias Ringwald     hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number);
2230aa4dd815SMatthias Ringwald }
2231aa4dd815SMatthias Ringwald 
2232aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2233aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2234aa4dd815SMatthias Ringwald }
2235aa4dd815SMatthias Ringwald 
2236aa4dd815SMatthias Ringwald // call from AG UI
2237aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2238aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2239aa4dd815SMatthias Ringwald }
2240aa4dd815SMatthias Ringwald 
2241ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2242ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2243ce263fc8SMatthias Ringwald }
2244ce263fc8SMatthias Ringwald 
2245aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2246aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2247aa4dd815SMatthias Ringwald }
2248aa4dd815SMatthias Ringwald 
2249aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2250aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2251aa4dd815SMatthias Ringwald }
2252aa4dd815SMatthias Ringwald 
2253aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2254aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2255aa4dd815SMatthias Ringwald }
2256aa4dd815SMatthias Ringwald 
2257aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2258aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2259aa4dd815SMatthias Ringwald }
2260aa4dd815SMatthias Ringwald 
2261aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2262aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2263aa4dd815SMatthias Ringwald }
2264ce263fc8SMatthias Ringwald 
2265ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2266ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2267ce263fc8SMatthias Ringwald }
2268ce263fc8SMatthias Ringwald 
2269ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2270ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2271ce263fc8SMatthias Ringwald }
2272ce263fc8SMatthias Ringwald 
2273ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2274ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2275ce263fc8SMatthias Ringwald }
2276ce263fc8SMatthias Ringwald 
2277aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2278aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2279aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2280aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2281aa4dd815SMatthias Ringwald 
2282ce263fc8SMatthias Ringwald 
2283665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2284665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2285665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2286a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
228766c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2288a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2289ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2290ce263fc8SMatthias Ringwald             continue;
2291ce263fc8SMatthias Ringwald         }
2292ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2293a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2294f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2295aa4dd815SMatthias Ringwald     }
2296aa4dd815SMatthias Ringwald }
2297aa4dd815SMatthias Ringwald 
2298aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2299aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2300aa4dd815SMatthias Ringwald }
2301aa4dd815SMatthias Ringwald 
2302aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2303aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2304aa4dd815SMatthias Ringwald }
2305aa4dd815SMatthias Ringwald 
2306aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2307aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2308aa4dd815SMatthias Ringwald }
2309aa4dd815SMatthias Ringwald 
2310aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2311aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2312aa4dd815SMatthias Ringwald }
2313aa4dd815SMatthias Ringwald 
2314d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){
2315aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
23169c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2317a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2318a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2319a33eb0c4SMilanka Ringwald         return;
2320a33eb0c4SMilanka Ringwald     }
2321aa4dd815SMatthias Ringwald 
2322a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
232360ebb071SMilanka Ringwald         log_info("AG cannot acivate voice recognition - not supported by HF");
2324aa4dd815SMatthias Ringwald         return;
2325aa4dd815SMatthias Ringwald     }
2326aa4dd815SMatthias Ringwald 
2327aa4dd815SMatthias Ringwald     if (activate){
2328d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(acl_handle);
2329aa4dd815SMatthias Ringwald     }
2330aa4dd815SMatthias Ringwald 
2331a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2332a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2333f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2334aa4dd815SMatthias Ringwald }
2335aa4dd815SMatthias Ringwald 
2336d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
23379c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2338a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2339a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2340a33eb0c4SMilanka Ringwald         return;
2341a33eb0c4SMilanka Ringwald     }
2342a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2343a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2344a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2345a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2346aa4dd815SMatthias Ringwald     }
2347f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2348aa4dd815SMatthias Ringwald }
2349aa4dd815SMatthias Ringwald 
2350d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
23519c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2352a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2353a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2354a33eb0c4SMilanka Ringwald         return;
2355a33eb0c4SMilanka Ringwald     }
2356a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2357a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2358a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2359aa4dd815SMatthias Ringwald     }
2360f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2361aa4dd815SMatthias Ringwald }
2362aa4dd815SMatthias Ringwald 
2363d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
23649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2365a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2366a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2367a33eb0c4SMilanka Ringwald         return;
2368a33eb0c4SMilanka Ringwald     }
2369aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2370a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2371aa4dd815SMatthias Ringwald }
2372aa4dd815SMatthias Ringwald 
2373d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
23749c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2375a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2376a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2377a33eb0c4SMilanka Ringwald         return;
2378a33eb0c4SMilanka Ringwald     }
2379a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2380aa4dd815SMatthias Ringwald }
2381aa4dd815SMatthias Ringwald 
2382d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
23839c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2384a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2385a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2386a33eb0c4SMilanka Ringwald         return;
2387a33eb0c4SMilanka Ringwald     }
2388a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2389c1797c7dSMatthias Ringwald }
2390aa4dd815SMatthias Ringwald 
2391ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2392ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2393ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2394ce263fc8SMatthias Ringwald }
2395ce263fc8SMatthias Ringwald 
23969cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
23979cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2398ce263fc8SMatthias Ringwald }
2399ce263fc8SMatthias Ringwald 
2400d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
24019c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2402a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2403a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2404a33eb0c4SMilanka Ringwald         return;
2405a33eb0c4SMilanka Ringwald     }
2406a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2407a0ffb263SMatthias Ringwald 
2408a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2409f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2410a0ffb263SMatthias Ringwald }
2411ce263fc8SMatthias Ringwald 
2412