xref: /btstack/src/classic/hfp_ag.c (revision 0cef86faede4123def9ff0fbdc75eaebcf697dfb)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hfp_ag.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
4266a048abSMatthias Ringwald // HFP Audio Gateway (AG) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493deb3ec6SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <stdlib.h>
513deb3ec6SMatthias Ringwald #include <string.h>
523deb3ec6SMatthias Ringwald 
5356042629SMatthias Ringwald #include "hci_cmd.h"
5482636622SMatthias Ringwald #include "btstack_run_loop.h"
553deb3ec6SMatthias Ringwald 
56235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
573deb3ec6SMatthias Ringwald #include "hci.h"
583deb3ec6SMatthias Ringwald #include "btstack_memory.h"
593deb3ec6SMatthias Ringwald #include "hci_dump.h"
603deb3ec6SMatthias Ringwald #include "l2cap.h"
6116ece135SMatthias Ringwald #include "btstack_debug.h"
62e30a6a47SMatthias Ringwald #include "btstack_event.h"
6359c6af15SMatthias Ringwald #include "classic/core.h"
643edc84c5SMatthias Ringwald #include "classic/hfp.h"
653edc84c5SMatthias Ringwald #include "classic/hfp_ag.h"
66023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h"
67efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
68023f2764SMatthias Ringwald #include "classic/sdp_server.h"
69023f2764SMatthias Ringwald #include "classic/sdp_util.h"
703deb3ec6SMatthias Ringwald 
71c5b64319SMatthias Ringwald // private prototypes
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;
953deb3ec6SMatthias Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_AG_INDICATORS];
963deb3ec6SMatthias Ringwald 
97a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0;
98a0ffb263SMatthias Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_HF_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;
117a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1183deb3ec6SMatthias Ringwald     }
119a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
120a0ffb263SMatthias Ringwald }
121a0ffb263SMatthias Ringwald 
122a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
123a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
124a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
125a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
126a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
127a0ffb263SMatthias Ringwald     }
128a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1293deb3ec6SMatthias Ringwald }
1303deb3ec6SMatthias Ringwald 
131aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
132aa4dd815SMatthias Ringwald     int i;
133aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
134aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
135aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
136aa4dd815SMatthias Ringwald         }
137aa4dd815SMatthias Ringwald     }
138aa4dd815SMatthias Ringwald     return NULL;
139aa4dd815SMatthias Ringwald }
140aa4dd815SMatthias Ringwald 
141aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
142aa4dd815SMatthias Ringwald     int i;
143aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
144aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
145aa4dd815SMatthias Ringwald             return i;
146aa4dd815SMatthias Ringwald         }
147aa4dd815SMatthias Ringwald     }
148aa4dd815SMatthias Ringwald     return -1;
149aa4dd815SMatthias Ringwald }
150aa4dd815SMatthias Ringwald 
1519c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){
1529c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1539c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1549c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1559c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1569c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1579c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1589c9c64c1SMatthias Ringwald         return hfp_connection;
1599c9c64c1SMatthias Ringwald     }
1609c9c64c1SMatthias Ringwald     return NULL;
1619c9c64c1SMatthias Ringwald }
1623deb3ec6SMatthias Ringwald 
16313839019SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
1643deb3ec6SMatthias Ringwald     if (callback == NULL){
1653deb3ec6SMatthias Ringwald         log_error("hfp_ag_register_packet_handler called with NULL callback");
1663deb3ec6SMatthias Ringwald         return;
1673deb3ec6SMatthias Ringwald     }
168ca59be51SMatthias Ringwald     hfp_ag_callback = callback;
169ca59be51SMatthias Ringwald     hfp_set_ag_callback(callback);
1703deb3ec6SMatthias Ringwald }
1713deb3ec6SMatthias Ringwald 
1720cb5b971SMatthias Ringwald static int use_in_band_tone(void){
173aa4dd815SMatthias Ringwald     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1743deb3ec6SMatthias Ringwald }
1753deb3ec6SMatthias Ringwald 
176a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
177a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1783deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1793deb3ec6SMatthias Ringwald     return hf && ag;
1803deb3ec6SMatthias Ringwald }
1813deb3ec6SMatthias Ringwald 
182a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
183a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1843deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1853deb3ec6SMatthias Ringwald     return hf && ag;
1863deb3ec6SMatthias Ringwald }
1873deb3ec6SMatthias Ringwald 
188a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
189a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
1903deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
1913deb3ec6SMatthias Ringwald     return hf && ag;
1923deb3ec6SMatthias Ringwald }
1933deb3ec6SMatthias Ringwald 
1944f84bf36SMatthias 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){
1953deb3ec6SMatthias Ringwald     if (!name){
1963deb3ec6SMatthias Ringwald         name = default_hfp_ag_service_name;
1973deb3ec6SMatthias Ringwald     }
198235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
1993deb3ec6SMatthias Ringwald 
2003deb3ec6SMatthias Ringwald     /*
2013deb3ec6SMatthias Ringwald      * 0x01 – Ability to reject a call
2023deb3ec6SMatthias Ringwald      * 0x00 – No ability to reject a call
2033deb3ec6SMatthias Ringwald      */
204aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
205aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
206aa4dd815SMatthias Ringwald 
2074f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
2084f84bf36SMatthias Ringwald     //
2094f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
2104f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
2114f84bf36SMatthias Ringwald     //
21242bafa11SMatthias Ringwald     // Wide band speech (bit 5) requires Codec negotiation
21342bafa11SMatthias Ringwald     //
2144f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
21542bafa11SMatthias Ringwald     if (wide_band_speech && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){
21642bafa11SMatthias Ringwald         sdp_features |= 1 << 5;
2174f84bf36SMatthias Ringwald     }
218aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
2194f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
220aa4dd815SMatthias Ringwald }
221aa4dd815SMatthias Ringwald 
222485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){
223aa4dd815SMatthias Ringwald     char buffer[20];
224aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
225aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2263deb3ec6SMatthias Ringwald }
2273deb3ec6SMatthias Ringwald 
2283deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2293deb3ec6SMatthias Ringwald     char buffer[40];
2303deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
2313deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2323deb3ec6SMatthias Ringwald }
2333deb3ec6SMatthias Ringwald 
234485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){
2353deb3ec6SMatthias Ringwald     char buffer[10];
2363deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nOK\r\n");
2373deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2383deb3ec6SMatthias Ringwald }
2393deb3ec6SMatthias Ringwald 
240485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){
241aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
242aa4dd815SMatthias Ringwald }
243aa4dd815SMatthias Ringwald 
244aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
245aa4dd815SMatthias Ringwald     char buffer[50];
246d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
247aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
248aa4dd815SMatthias Ringwald }
249aa4dd815SMatthias Ringwald 
250ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
251ce263fc8SMatthias Ringwald     char buffer[50];
252ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
253ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
254ce263fc8SMatthias Ringwald }
255ce263fc8SMatthias Ringwald 
256c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
257aa4dd815SMatthias Ringwald     char buffer[50];
258d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
259aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
260aa4dd815SMatthias Ringwald }
261aa4dd815SMatthias Ringwald 
262aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
263aa4dd815SMatthias Ringwald     char buffer[50];
264a0ffb263SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
265aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
266aa4dd815SMatthias Ringwald }
267aa4dd815SMatthias Ringwald 
268485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){
2693deb3ec6SMatthias Ringwald     char buffer[10];
2703deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nERROR\r\n");
2713deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2723deb3ec6SMatthias Ringwald }
2733deb3ec6SMatthias Ringwald 
274485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2753deb3ec6SMatthias Ringwald     char buffer[20];
2763deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2773deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2783deb3ec6SMatthias Ringwald }
2793deb3ec6SMatthias Ringwald 
280ad902e3dSMatthias Ringwald // get size for indicator string
281a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
282ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
28345718b6fSMatthias Ringwald     return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
284a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
285a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
286ad902e3dSMatthias Ringwald }
287ad902e3dSMatthias Ringwald 
288ad902e3dSMatthias Ringwald // store indicator
289a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
290ad902e3dSMatthias Ringwald     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
291a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
292a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
293a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2943deb3ec6SMatthias Ringwald }
295ad902e3dSMatthias Ringwald 
296ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
297a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
298a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
299ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
300ad902e3dSMatthias Ringwald     return 3 + (num_indicators-1) * 2;
3013deb3ec6SMatthias Ringwald }
302ad902e3dSMatthias Ringwald 
303ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
304a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
305ad902e3dSMatthias Ringwald     if (index == 0) {
306ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
307ad902e3dSMatthias Ringwald     }
308ad902e3dSMatthias Ringwald     index--;
309a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
310ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
311ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
312a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
313ad902e3dSMatthias Ringwald     }
314ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators - 1){
315ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
316ad902e3dSMatthias Ringwald     }
317ad902e3dSMatthias Ringwald     return 1; // comma
318ad902e3dSMatthias Ringwald }
319ad902e3dSMatthias Ringwald 
320a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
321ad902e3dSMatthias Ringwald     if (index == 0){
322ad902e3dSMatthias Ringwald         *buffer++ = '\r';
32374386ee0SMatthias Ringwald         *buffer++ = '\n';
324ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
325ad902e3dSMatthias Ringwald         memcpy(buffer, HFP_INDICATOR, len);
326ad902e3dSMatthias Ringwald         buffer += len;
327ad902e3dSMatthias Ringwald         *buffer++ = ':';
328ad902e3dSMatthias Ringwald         return;
329ad902e3dSMatthias Ringwald     }
330ad902e3dSMatthias Ringwald     index--;
331a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
332ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
333ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
334a0ffb263SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
335ad902e3dSMatthias Ringwald         return;
336ad902e3dSMatthias Ringwald     }
337ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators-1){
338ad902e3dSMatthias Ringwald         memcpy(buffer, "\r\n\r\nOK\r\n", 8);
339ad902e3dSMatthias Ringwald         return;
340ad902e3dSMatthias Ringwald     }
341ad902e3dSMatthias Ringwald     *buffer = ',';
3423deb3ec6SMatthias Ringwald }
3433deb3ec6SMatthias Ringwald 
3443deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
3453deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3463deb3ec6SMatthias Ringwald     int i;
3473deb3ec6SMatthias Ringwald     int offset = 0;
348a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr-1; i++) {
349a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3503deb3ec6SMatthias Ringwald     }
351a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
352a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3533deb3ec6SMatthias Ringwald     }
3543deb3ec6SMatthias Ringwald     return offset;
3553deb3ec6SMatthias Ringwald }
3563deb3ec6SMatthias Ringwald 
3573deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
358a0ffb263SMatthias Ringwald     if (buffer_size < hfp_generic_status_indicators_nr * 3) return 0;
3593deb3ec6SMatthias Ringwald     int i;
3603deb3ec6SMatthias Ringwald     int offset = 0;
361a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
362a0ffb263SMatthias 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);
3633deb3ec6SMatthias Ringwald     }
3643deb3ec6SMatthias Ringwald     return offset;
3653deb3ec6SMatthias Ringwald }
3663deb3ec6SMatthias Ringwald 
3673deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
3683deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3693deb3ec6SMatthias Ringwald     int i;
3703deb3ec6SMatthias Ringwald     int offset = 0;
3713deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr-1; i++) {
3723deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3733deb3ec6SMatthias Ringwald     }
3743deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3753deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3763deb3ec6SMatthias Ringwald     }
3773deb3ec6SMatthias Ringwald     return offset;
3783deb3ec6SMatthias Ringwald }
3793deb3ec6SMatthias Ringwald 
3803deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
3813deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0;
3823deb3ec6SMatthias Ringwald     int i;
3833deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
3843deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) {
3853deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3863deb3ec6SMatthias Ringwald     }
3873deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3883deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3893deb3ec6SMatthias Ringwald     }
3903deb3ec6SMatthias Ringwald     return offset;
3913deb3ec6SMatthias Ringwald }
3923deb3ec6SMatthias Ringwald 
393485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
394ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
395a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
396a0ffb263SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
3973deb3ec6SMatthias Ringwald 
398ad902e3dSMatthias Ringwald     // assumes: can send now == true
399ad902e3dSMatthias Ringwald     // assumes: num segments > 0
400aba39421SMatthias Ringwald     // assumes: individual segments are smaller than MTU
401ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
402ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
403ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
404ad902e3dSMatthias Ringwald     int offset = 0;
405ad902e3dSMatthias Ringwald     int segment = start_segment;
406ad902e3dSMatthias Ringwald     while (segment < num_segments){
407a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
408aba39421SMatthias Ringwald         if (offset + segment_len > mtu) break;
409aba39421SMatthias Ringwald         // append segement
410a0ffb263SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset);
411ad902e3dSMatthias Ringwald         offset += segment_len;
412ad902e3dSMatthias Ringwald         segment++;
413ad902e3dSMatthias Ringwald     }
414ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
415ad902e3dSMatthias Ringwald     return segment;
416ad902e3dSMatthias Ringwald }
4173deb3ec6SMatthias Ringwald 
418ad902e3dSMatthias Ringwald // returns next segment to store
419485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
420a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
421485ac19eSMilanka Ringwald     return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
422ad902e3dSMatthias Ringwald         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
4233deb3ec6SMatthias Ringwald }
4243deb3ec6SMatthias Ringwald 
425485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){
4263deb3ec6SMatthias Ringwald     char buffer[40];
42789425bfcSMilanka Ringwald     const int size = sizeof(buffer);
42889425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR);
42989425bfcSMilanka Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9);
43089425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4313deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4323deb3ec6SMatthias Ringwald }
4333deb3ec6SMatthias Ringwald 
434485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){
435ad902e3dSMatthias Ringwald     char buffer[40];
43689425bfcSMilanka Ringwald     const int size = sizeof(buffer);
43789425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
43889425bfcSMilanka Ringwald     offset += hfp_ag_call_services_join(buffer+offset, size-offset-9);
43989425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4403deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4413deb3ec6SMatthias Ringwald }
4423deb3ec6SMatthias Ringwald 
4433deb3ec6SMatthias Ringwald 
444485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){
445485ac19eSMilanka Ringwald     return hfp_ag_send_ok(cid);
4463deb3ec6SMatthias Ringwald }
4473deb3ec6SMatthias Ringwald 
448485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4493deb3ec6SMatthias Ringwald     char buffer[40];
45089425bfcSMilanka Ringwald     const int size = sizeof(buffer);
45189425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
45289425bfcSMilanka Ringwald     offset += hfp_hf_indicators_join(buffer+offset, size-offset-10);
45389425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n");
4543deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4553deb3ec6SMatthias Ringwald }
4563deb3ec6SMatthias Ringwald 
457485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4583deb3ec6SMatthias Ringwald     char buffer[40];
4591cc1d9e9SMilanka Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7);
4601cc1d9e9SMilanka Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4613deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4623deb3ec6SMatthias Ringwald }
4633deb3ec6SMatthias Ringwald 
464485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4653deb3ec6SMatthias Ringwald     char buffer[20];
466aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
4673deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4683deb3ec6SMatthias Ringwald }
4693deb3ec6SMatthias Ringwald 
470485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4715e71456cSMilanka Ringwald     char buffer[41];
4723deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
47389425bfcSMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4743deb3ec6SMatthias Ringwald     } else {
47589425bfcSMilanka Ringwald         int offset = snprintf(buffer,  sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format);
4765e71456cSMilanka Ringwald         offset += snprintf(buffer+offset, 16, "%s", op.name);
47789425bfcSMilanka Ringwald         snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4783deb3ec6SMatthias Ringwald     }
4793deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4803deb3ec6SMatthias Ringwald }
4813deb3ec6SMatthias Ringwald 
4821cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
4831cc1d9e9SMilanka Ringwald     char buffer[30];
48489425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value);
4851cc1d9e9SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
4861cc1d9e9SMilanka Ringwald }
4873deb3ec6SMatthias Ringwald 
488485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){
4891cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
4903deb3ec6SMatthias Ringwald }
4913deb3ec6SMatthias Ringwald 
492485ac19eSMilanka Ringwald static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
4931cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
494aa4dd815SMatthias Ringwald }
495aa4dd815SMatthias Ringwald 
496485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
4971cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
498aa4dd815SMatthias Ringwald }
499aa4dd815SMatthias Ringwald 
500485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
5011cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
502aa4dd815SMatthias Ringwald }
503aa4dd815SMatthias Ringwald 
504485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){
5051cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_RESPONSE_AND_HOLD, state);
506ce263fc8SMatthias Ringwald }
507ce263fc8SMatthias Ringwald 
508a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
509bc1b1537SMilanka Ringwald     if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
510bc1b1537SMilanka Ringwald 
5116a7f44bdSMilanka Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){
5126a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
513df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
5143deb3ec6SMatthias Ringwald         }
5153deb3ec6SMatthias Ringwald     }
516df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
5173deb3ec6SMatthias Ringwald }
5183deb3ec6SMatthias Ringwald 
519afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){
520afac2a04SMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_AGSF_ESCO_S4));
5217522e673SMatthias Ringwald }
5227522e673SMatthias Ringwald 
523a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
524aa4dd815SMatthias Ringwald     /* events ( == commands):
525aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
526aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
527aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
528aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
529aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
530aa4dd815SMatthias Ringwald     */
531aa4dd815SMatthias Ringwald 
532a0ffb263SMatthias Ringwald      switch (hfp_connection->codecs_state){
533aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
534a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
535aa4dd815SMatthias Ringwald             break;
536aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
537a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
538aa4dd815SMatthias Ringwald             break;
539aa4dd815SMatthias Ringwald         default:
540aa4dd815SMatthias Ringwald             break;
541aa4dd815SMatthias Ringwald     }
542aa4dd815SMatthias Ringwald 
543aa4dd815SMatthias Ringwald     // printf(" -> State machine: CC\n");
544aa4dd815SMatthias Ringwald 
545a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
546aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
547a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
548a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
549485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
550aa4dd815SMatthias Ringwald                 return 1;
551aa4dd815SMatthias Ringwald             }
552aa4dd815SMatthias Ringwald 
553a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
554aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
555aa4dd815SMatthias Ringwald                 case HFP_CODECS_EXCHANGED:
556a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
557aa4dd815SMatthias Ringwald                     break;
558aa4dd815SMatthias Ringwald                 default:
559aa4dd815SMatthias Ringwald                     break;
560aa4dd815SMatthias Ringwald             }
561485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
562aa4dd815SMatthias Ringwald             return 1;
563aa4dd815SMatthias Ringwald 
564aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
565a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
566485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
567aa4dd815SMatthias Ringwald             return 1;
568aa4dd815SMatthias Ringwald 
569aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
570a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
571a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
572485ac19eSMilanka Ringwald             hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
573aa4dd815SMatthias Ringwald             return 1;
574aa4dd815SMatthias Ringwald 
575aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
576a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
577a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
578485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
579aa4dd815SMatthias Ringwald                 return 1;
580aa4dd815SMatthias Ringwald             }
581a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
582a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
58303645b44SMatthias Ringwald             log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
584485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
5857522e673SMatthias Ringwald             // now, pick link settings
586afac2a04SMilanka Ringwald 
587afac2a04SMilanka Ringwald             hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
588aa4dd815SMatthias Ringwald             return 1;
589aa4dd815SMatthias Ringwald         default:
590aa4dd815SMatthias Ringwald             break;
591aa4dd815SMatthias Ringwald     }
592aa4dd815SMatthias Ringwald     return 0;
593aa4dd815SMatthias Ringwald }
594aa4dd815SMatthias Ringwald 
595a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
596a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
597ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
598aa4dd815SMatthias Ringwald 
599a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
600d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
601a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
602aa4dd815SMatthias Ringwald     }
603ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
604d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS &&
605d0c20769SMatthias Ringwald         hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
606a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
607ce263fc8SMatthias Ringwald     }
608aa4dd815SMatthias Ringwald }
6093deb3ec6SMatthias Ringwald 
610a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
611c79b4cabSMatthias Ringwald     // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
612a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
613c04cf7ffSMilanka Ringwald     int sent = 0;
614a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
6153deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
616a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
6173deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
618aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
619d715cf51SMatthias Ringwald                     hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
620a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
621a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
622aa4dd815SMatthias Ringwald                     } else {
623a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
624aa4dd815SMatthias Ringwald                     }
625a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
626aa4dd815SMatthias Ringwald                     return 1;
6273deb3ec6SMatthias Ringwald                 default:
6283deb3ec6SMatthias Ringwald                     break;
6293deb3ec6SMatthias Ringwald             }
6303deb3ec6SMatthias Ringwald             break;
6313deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
632c04cf7ffSMilanka Ringwald             sent = codecs_exchange_state_machine(hfp_connection);
633a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
634a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6353deb3ec6SMatthias Ringwald             }
636c04cf7ffSMilanka Ringwald             return sent;
637aa4dd815SMatthias Ringwald 
638aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
639a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
640473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
641473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
642473ac565SMatthias Ringwald                 // continue below in state switch
643ad902e3dSMatthias Ringwald             }
644ad902e3dSMatthias Ringwald             break;
645aa4dd815SMatthias Ringwald 
646aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
647a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
648a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
649485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
650aa4dd815SMatthias Ringwald             return 1;
651aa4dd815SMatthias Ringwald 
6523deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
653a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
654a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
655a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
656a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
657a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
658aa4dd815SMatthias Ringwald             } else {
659a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
6603deb3ec6SMatthias Ringwald             }
6618a46ec40SMatthias Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
662aa4dd815SMatthias Ringwald             return 1;
6633deb3ec6SMatthias Ringwald 
664aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
665a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
666a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
667a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
668aa4dd815SMatthias Ringwald             }
669485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
670a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
671a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
672ce263fc8SMatthias Ringwald             }
673aa4dd815SMatthias Ringwald             return 1;
674aa4dd815SMatthias Ringwald 
675aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
676a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
677a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
678485ac19eSMilanka Ringwald             hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
679aa4dd815SMatthias Ringwald             return 1;
680aa4dd815SMatthias Ringwald 
681aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
682a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
683a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
684485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
685aa4dd815SMatthias Ringwald             return 1;
686aa4dd815SMatthias Ringwald 
687aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
688a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
689a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
690485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
691aa4dd815SMatthias Ringwald             return 1;
6923deb3ec6SMatthias Ringwald         default:
6933deb3ec6SMatthias Ringwald             break;
6943deb3ec6SMatthias Ringwald     }
695473ac565SMatthias Ringwald 
696473ac565SMatthias Ringwald     switch (hfp_connection->state){
697473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
698485ac19eSMilanka Ringwald             int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
699473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
700473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
701473ac565SMatthias Ringwald             if (next_segment < num_segments){
702473ac565SMatthias Ringwald                 // prepare sending of next segment
703473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
704473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
705473ac565SMatthias Ringwald             } else {
706473ac565SMatthias Ringwald                 // done, go to next state
707473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
708473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
709473ac565SMatthias Ringwald             }
710473ac565SMatthias Ringwald             return 1;
711473ac565SMatthias Ringwald         }
712473ac565SMatthias Ringwald         default:
713473ac565SMatthias Ringwald             break;
714473ac565SMatthias Ringwald     }
715c04cf7ffSMilanka Ringwald     return 0;
7163deb3ec6SMatthias Ringwald }
7173deb3ec6SMatthias Ringwald 
718a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
719c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
720c04cf7ffSMilanka Ringwald     if (sent) return 1;
721aa4dd815SMatthias Ringwald 
722a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
723aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
724a0ffb263SMatthias Ringwald             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
725485ac19eSMilanka Ringwald             hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
726aa4dd815SMatthias Ringwald             return 1;
727aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
728aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
729a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
730485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
731a0ffb263SMatthias Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
732aa4dd815SMatthias Ringwald             } else {
733485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
734aa4dd815SMatthias Ringwald             }
735aa4dd815SMatthias Ringwald             return 1;
736aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
737485ac19eSMilanka Ringwald             hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
738aa4dd815SMatthias Ringwald             return 1;
739aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
740485ac19eSMilanka Ringwald             hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
741aa4dd815SMatthias Ringwald             return 1;
742aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
743a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
744485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
745aa4dd815SMatthias Ringwald             } else {
746485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
7473deb3ec6SMatthias Ringwald             }
748aa4dd815SMatthias Ringwald             return 1;
749aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
750485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
751aa4dd815SMatthias Ringwald             return 1;
7523deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
753a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
754a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
755485ac19eSMilanka Ringwald                 hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
756aa4dd815SMatthias Ringwald                 return 1;
7573deb3ec6SMatthias Ringwald             }
758202c8a4cSMatthias Ringwald             break;
7593deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
760485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
761ce263fc8SMatthias Ringwald             return 1;
7623deb3ec6SMatthias Ringwald         default:
7633deb3ec6SMatthias Ringwald             break;
7643deb3ec6SMatthias Ringwald     }
765aa4dd815SMatthias Ringwald     return 0;
7663deb3ec6SMatthias Ringwald }
7673deb3ec6SMatthias Ringwald 
768a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
769a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
770a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
7713deb3ec6SMatthias Ringwald 
772aa4dd815SMatthias Ringwald 
773a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
774a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
775a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
776a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
777aa4dd815SMatthias Ringwald         return 1;
778aa4dd815SMatthias Ringwald     }
779aa4dd815SMatthias Ringwald 
780a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
781aa4dd815SMatthias Ringwald 
782aa4dd815SMatthias Ringwald     // run codecs exchange
783c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
784c04cf7ffSMilanka Ringwald     if (sent) return 1;
785aa4dd815SMatthias Ringwald 
786c04cf7ffSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
787a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
788a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
789a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
790eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
791aa4dd815SMatthias Ringwald         return 1;
792aa4dd815SMatthias Ringwald     }
793aa4dd815SMatthias Ringwald     return 0;
794aa4dd815SMatthias Ringwald }
795aa4dd815SMatthias Ringwald 
796ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
797665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
798665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
799aa4dd815SMatthias Ringwald 
800665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
801a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
802a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
803a0ffb263SMatthias Ringwald             return hfp_connection;
804aa4dd815SMatthias Ringwald         }
805aa4dd815SMatthias Ringwald     }
806aa4dd815SMatthias Ringwald     return NULL;
807aa4dd815SMatthias Ringwald }
808aa4dd815SMatthias Ringwald 
809ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
810d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
811d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
812aa4dd815SMatthias Ringwald 
813d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
814a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
815a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
816aa4dd815SMatthias Ringwald 
817a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
818a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
819aa4dd815SMatthias Ringwald 
820f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
821aa4dd815SMatthias Ringwald }
822aa4dd815SMatthias Ringwald 
823a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
824a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
825a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
826a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
827a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
828aa4dd815SMatthias Ringwald }
829aa4dd815SMatthias Ringwald 
830a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
831a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
832a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
833aa4dd815SMatthias Ringwald }
834aa4dd815SMatthias Ringwald 
835aa4dd815SMatthias Ringwald //
836aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
837aa4dd815SMatthias Ringwald //
838aa4dd815SMatthias Ringwald 
839a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
840aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
841a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
842d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
843aa4dd815SMatthias Ringwald     } else {
844a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
845a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
846a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
847a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
848ca59be51SMatthias Ringwald         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
849aa4dd815SMatthias Ringwald     }
850aa4dd815SMatthias Ringwald }
851aa4dd815SMatthias Ringwald 
852a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
853a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
854a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
855a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
856ca59be51SMatthias Ringwald     hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGINIG);
857aa4dd815SMatthias Ringwald }
858aa4dd815SMatthias Ringwald 
859aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
860aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
861aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
862aa4dd815SMatthias Ringwald 
863665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
864665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
865665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
866a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
86766c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
868a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
869a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
870a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
871d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
872aa4dd815SMatthias Ringwald         }
873a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
874a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
875aa4dd815SMatthias Ringwald         }
876f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
877aa4dd815SMatthias Ringwald     }
878aa4dd815SMatthias Ringwald }
879aa4dd815SMatthias Ringwald 
880aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
881aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
882aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
883aa4dd815SMatthias Ringwald 
884665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
885665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
886665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
887a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
88866c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
889a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
890a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
891f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
892aa4dd815SMatthias Ringwald     }
893aa4dd815SMatthias Ringwald }
894aa4dd815SMatthias Ringwald 
895aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
896aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("call");
897aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
898aa4dd815SMatthias Ringwald 
899665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
900665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
901665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
902a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
90366c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
904a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
905a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
906f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
907aa4dd815SMatthias Ringwald     }
908aa4dd815SMatthias Ringwald }
909aa4dd815SMatthias Ringwald 
910aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
911aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callheld");
912aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
913aa4dd815SMatthias Ringwald 
914665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
915665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
916665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
917a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
91866c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
919a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
920a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
921f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
922aa4dd815SMatthias Ringwald     }
923aa4dd815SMatthias Ringwald }
924aa4dd815SMatthias Ringwald 
925aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
926aa4dd815SMatthias Ringwald 
927aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
928aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
929aa4dd815SMatthias Ringwald 
930665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
931665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
932665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
933a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
93466c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
935a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
936a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
937aa4dd815SMatthias Ringwald 
938a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
939a0ffb263SMatthias Ringwald         if (hfp_connection == source){
940a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
941aa4dd815SMatthias Ringwald 
942aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
943a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
944aa4dd815SMatthias Ringwald             } else {
945a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
946d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
947aa4dd815SMatthias Ringwald             }
948aa4dd815SMatthias Ringwald 
949a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
950a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
951aa4dd815SMatthias Ringwald 
952aa4dd815SMatthias Ringwald         } else {
953a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
954aa4dd815SMatthias Ringwald         }
955f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
956aa4dd815SMatthias Ringwald     }
957aa4dd815SMatthias Ringwald }
958aa4dd815SMatthias Ringwald 
959aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
960aa4dd815SMatthias Ringwald 
961aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
962aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
963aa4dd815SMatthias Ringwald 
964665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
965665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
966665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
967a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
96866c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
969a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
970aa4dd815SMatthias Ringwald 
971a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
972a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
973aa4dd815SMatthias Ringwald 
974a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
975a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
976aa4dd815SMatthias Ringwald 
977f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
978aa4dd815SMatthias Ringwald         break;  // only single
979aa4dd815SMatthias Ringwald     }
980aa4dd815SMatthias Ringwald }
981aa4dd815SMatthias Ringwald 
982aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
983aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
984665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
985665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
986665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
987665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
98866c5995fSMatthias Ringwald         if (connection->local_role != HFP_ROLE_AG) continue;
989aa4dd815SMatthias Ringwald         if (connection->call_state != HFP_CALL_RINGING &&
990aa4dd815SMatthias Ringwald             connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
991aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
992aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
993aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
994f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(connection);
995aa4dd815SMatthias Ringwald     }
996aa4dd815SMatthias Ringwald }
997aa4dd815SMatthias Ringwald 
998ca59be51SMatthias Ringwald static void hfp_ag_emit_simple_event(uint8_t event_subtype){
999ca59be51SMatthias Ringwald     uint8_t event[3];
1000ca59be51SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
1001ca59be51SMatthias Ringwald     event[1] = sizeof(event) - 2;
1002ca59be51SMatthias Ringwald     event[2] = event_subtype;
1003ca59be51SMatthias Ringwald     if (!hfp_ag_callback) return;
1004ca59be51SMatthias Ringwald     (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1005ca59be51SMatthias Ringwald }
1006ca59be51SMatthias Ringwald 
1007aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1008aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1009aa4dd815SMatthias Ringwald 
1010665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1011665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1012665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1013a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
101466c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1015d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1016a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1017a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1018a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1019a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 1;
1020f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1021aa4dd815SMatthias Ringwald     }
1022ca59be51SMatthias Ringwald     hfp_ag_emit_simple_event(HFP_SUBEVENT_CALL_TERMINATED);
1023aa4dd815SMatthias Ringwald }
1024aa4dd815SMatthias Ringwald 
10250cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
1026aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1027aa4dd815SMatthias Ringwald     if (!indicator){
1028d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
102945718b6fSMatthias Ringwald         return;
1030aa4dd815SMatthias Ringwald     };
1031d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1032aa4dd815SMatthias Ringwald }
1033aa4dd815SMatthias Ringwald 
10340cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1035d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1036d210d9c4SMatthias Ringwald     if (!indicator){
1037d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
103845718b6fSMatthias Ringwald         return;
1039d210d9c4SMatthias Ringwald     };
1040d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1041d210d9c4SMatthias Ringwald }
1042d210d9c4SMatthias Ringwald 
10430cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1044aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1045aa4dd815SMatthias Ringwald     if (!indicator){
1046aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
104745718b6fSMatthias Ringwald         return;
1048aa4dd815SMatthias Ringwald     };
1049d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1050aa4dd815SMatthias Ringwald }
1051aa4dd815SMatthias Ringwald 
1052aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1053665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1054665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1055665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1056a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
105766c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1058a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
1059a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1060a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1061aa4dd815SMatthias Ringwald     }
1062aa4dd815SMatthias Ringwald }
1063aa4dd815SMatthias Ringwald 
1064aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1065665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1066665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1067665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1068a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
106966c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1070a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1071aa4dd815SMatthias Ringwald     }
1072aa4dd815SMatthias Ringwald     return NULL;
1073aa4dd815SMatthias Ringwald }
1074aa4dd815SMatthias Ringwald 
1075a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1076665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1077665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1078665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1079a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
108066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1081a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1082ce263fc8SMatthias Ringwald     }
1083ce263fc8SMatthias Ringwald }
1084ce263fc8SMatthias Ringwald 
1085a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1086aa4dd815SMatthias Ringwald     int indicator_index;
1087a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1088aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1089a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1090a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1091a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1092a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1093a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1094a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1095ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
1096aa4dd815SMatthias Ringwald             break;
1097aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1098a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1099a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1100a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1101aa4dd815SMatthias Ringwald             break;
1102aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1103a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1104a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1105aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1106a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1107aa4dd815SMatthias Ringwald             break;
1108aa4dd815SMatthias Ringwald         default:
1109aa4dd815SMatthias Ringwald             break;
1110aa4dd815SMatthias Ringwald     }
1111aa4dd815SMatthias Ringwald     return 0;
1112aa4dd815SMatthias Ringwald }
1113a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1114a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1115aa4dd815SMatthias Ringwald     int indicator_index;
1116d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1117d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1118d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
111974386ee0SMatthias Ringwald 
1120d210d9c4SMatthias Ringwald     //printf("hfp_ag_call_sm event %d \n", event);
1121aa4dd815SMatthias Ringwald     switch (event){
1122aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1123d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1124aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1125d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1126aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1127d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1128d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1129aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
113060ebb071SMilanka Ringwald                             log_info("AG rings");
1131aa4dd815SMatthias Ringwald                             break;
1132aa4dd815SMatthias Ringwald                         default:
11333deb3ec6SMatthias Ringwald                             break;
11343deb3ec6SMatthias Ringwald                     }
11353deb3ec6SMatthias Ringwald                     break;
1136aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1137d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1138aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1139d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1140d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1141aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
114260ebb071SMilanka Ringwald                             log_info("AG call waiting");
1143aa4dd815SMatthias Ringwald                             break;
1144aa4dd815SMatthias Ringwald                         default:
11453deb3ec6SMatthias Ringwald                             break;
11463deb3ec6SMatthias Ringwald                     }
11473deb3ec6SMatthias Ringwald                     break;
1148aa4dd815SMatthias Ringwald             }
1149aa4dd815SMatthias Ringwald             break;
1150aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1151d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1152aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1153d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1154aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1155d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1156d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1157d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1158aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
115960ebb071SMilanka Ringwald                             log_info("AG answers call, accept call by GSM");
1160aa4dd815SMatthias Ringwald                             break;
1161aa4dd815SMatthias Ringwald                         default:
11623deb3ec6SMatthias Ringwald                             break;
11633deb3ec6SMatthias Ringwald                     }
1164aa4dd815SMatthias Ringwald                     break;
1165aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1166d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1167aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
116860ebb071SMilanka Ringwald                             log_info("AG: current call is placed on hold, incoming call gets active");
1169d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1170d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1171d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1172ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1173ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1174aa4dd815SMatthias Ringwald                             break;
1175aa4dd815SMatthias Ringwald                         default:
1176aa4dd815SMatthias Ringwald                             break;
1177aa4dd815SMatthias Ringwald                     }
1178aa4dd815SMatthias Ringwald                     break;
1179aa4dd815SMatthias Ringwald             }
1180aa4dd815SMatthias Ringwald             break;
1181ce263fc8SMatthias Ringwald 
1182ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1183d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1184ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1185d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1186ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
118760ebb071SMilanka Ringwald                             log_info("AG: joining held call with active call");
1188d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG);
1189d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1190ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1191ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1192ce263fc8SMatthias Ringwald                             break;
1193ce263fc8SMatthias Ringwald                         default:
1194ce263fc8SMatthias Ringwald                             break;
1195ce263fc8SMatthias Ringwald                     }
1196ce263fc8SMatthias Ringwald                     break;
1197ce263fc8SMatthias Ringwald                 default:
1198ce263fc8SMatthias Ringwald                     break;
1199ce263fc8SMatthias Ringwald             }
1200ce263fc8SMatthias Ringwald             break;
1201ce263fc8SMatthias Ringwald 
1202aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1203d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1204aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1205d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1206aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1207d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF);
1208d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1209d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1210a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
121160ebb071SMilanka Ringwald                             log_info("HF answers call, accept call by GSM");
1212ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1213aa4dd815SMatthias Ringwald                             break;
1214aa4dd815SMatthias Ringwald                         default:
1215aa4dd815SMatthias Ringwald                             break;
1216aa4dd815SMatthias Ringwald                     }
12173deb3ec6SMatthias Ringwald                     break;
12183deb3ec6SMatthias Ringwald                 default:
12193deb3ec6SMatthias Ringwald                     break;
12203deb3ec6SMatthias Ringwald             }
12213deb3ec6SMatthias Ringwald             break;
12223deb3ec6SMatthias Ringwald 
1223ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1224d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1225ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1226d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1227ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1228d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG);
1229ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1230ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1231a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1232ce263fc8SMatthias Ringwald                             // as with regualr call
1233d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1234d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1235ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
123660ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by AG");
1237ce263fc8SMatthias Ringwald                             break;
1238ce263fc8SMatthias Ringwald                         default:
1239ce263fc8SMatthias Ringwald                             break;
1240ce263fc8SMatthias Ringwald                     }
1241ce263fc8SMatthias Ringwald                     break;
1242ce263fc8SMatthias Ringwald                 default:
1243ce263fc8SMatthias Ringwald                     break;
1244ce263fc8SMatthias Ringwald             }
1245ce263fc8SMatthias Ringwald             break;
1246ce263fc8SMatthias Ringwald 
1247ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1248d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1249ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1250d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1251ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1252d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF);
1253ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1254ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1255a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1256ce263fc8SMatthias Ringwald                             // as with regualr call
1257d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1258d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1259a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
126060ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by HF");
1261ce263fc8SMatthias Ringwald                             break;
1262ce263fc8SMatthias Ringwald                         default:
1263ce263fc8SMatthias Ringwald                             break;
1264ce263fc8SMatthias Ringwald                     }
1265ce263fc8SMatthias Ringwald                     break;
1266ce263fc8SMatthias Ringwald                 default:
1267ce263fc8SMatthias Ringwald                     break;
1268ce263fc8SMatthias Ringwald             }
1269ce263fc8SMatthias Ringwald             break;
1270ce263fc8SMatthias Ringwald 
1271ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1272ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1273ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1274ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1275d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG);
1276a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1277ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1278a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
127960ebb071SMilanka Ringwald             log_info("Held Call accepted and active");
1280ce263fc8SMatthias Ringwald             break;
1281ce263fc8SMatthias Ringwald 
1282ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1283ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1284ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1285ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1286d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG);
1287a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1288ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1289a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1290ce263fc8SMatthias Ringwald             // from terminate by ag
1291d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1292ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1293ce263fc8SMatthias Ringwald             break;
1294ce263fc8SMatthias Ringwald 
1295aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1296d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1297aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1298d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1299aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1300d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1301d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1302aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1303aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
130460ebb071SMilanka Ringwald                             log_info("HF Rejected Incoming call, AG terminate call");
1305aa4dd815SMatthias Ringwald                             break;
1306aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1307aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1308d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1309d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1310aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
131160ebb071SMilanka Ringwald                             log_info("AG terminate outgoing call process");
1312202c8a4cSMatthias Ringwald                             break;
1313aa4dd815SMatthias Ringwald                         default:
1314aa4dd815SMatthias Ringwald                             break;
1315aa4dd815SMatthias Ringwald                     }
1316aa4dd815SMatthias Ringwald                     break;
1317aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1318d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1319d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1320aa4dd815SMatthias Ringwald                     hfp_ag_transfer_call_state();
1321a0ffb263SMatthias Ringwald                     hfp_connection->call_state = HFP_CALL_IDLE;
132260ebb071SMilanka Ringwald                     log_info("AG terminate call");
1323aa4dd815SMatthias Ringwald                     break;
1324aa4dd815SMatthias Ringwald             }
13250c1b8529SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1326aa4dd815SMatthias Ringwald             break;
13273deb3ec6SMatthias Ringwald 
1328aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1329d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1330aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1331d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1332aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1333d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1334d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1335aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
133660ebb071SMilanka Ringwald                             log_info("AG Rejected Incoming call, AG terminate call");
1337aa4dd815SMatthias Ringwald                             break;
1338aa4dd815SMatthias Ringwald                         default:
1339aa4dd815SMatthias Ringwald                             break;
13403deb3ec6SMatthias Ringwald                     }
1341202c8a4cSMatthias Ringwald                     break;
1342aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1343d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1344d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1345d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1346aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
134760ebb071SMilanka Ringwald                     log_info("AG terminate call");
1348aa4dd815SMatthias Ringwald                     break;
1349aa4dd815SMatthias Ringwald                 default:
13503deb3ec6SMatthias Ringwald                     break;
13513deb3ec6SMatthias Ringwald             }
13523deb3ec6SMatthias Ringwald             break;
1353aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1354d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1355aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1356d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1357aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1358aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
135960ebb071SMilanka Ringwald                             log_info("Incoming call interrupted");
1360aa4dd815SMatthias Ringwald                             break;
1361aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1362aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
136360ebb071SMilanka Ringwald                             log_info("Outgoing call interrupted\n");
136460ebb071SMilanka Ringwald                             log_info("AG notify call dropped\n");
1365aa4dd815SMatthias Ringwald                             break;
1366aa4dd815SMatthias Ringwald                         default:
13673deb3ec6SMatthias Ringwald                             break;
13683deb3ec6SMatthias Ringwald                     }
1369d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1370d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1371aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
137210c236bfSMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1373aa4dd815SMatthias Ringwald                     break;
1374aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1375ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1376d210d9c4SMatthias Ringwald                         hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1377ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1378a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1379ce263fc8SMatthias Ringwald                     }
1380d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1381d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1382d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1383aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
138460ebb071SMilanka Ringwald                     log_info("AG notify call dropped");
1385aa4dd815SMatthias Ringwald                     break;
1386aa4dd815SMatthias Ringwald                 default:
1387aa4dd815SMatthias Ringwald                     break;
1388aa4dd815SMatthias Ringwald             }
1389aa4dd815SMatthias Ringwald             break;
1390aa4dd815SMatthias Ringwald 
1391aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1392d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1393d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1394a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1395f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1396d210d9c4SMatthias Ringwald                 break;
1397d210d9c4SMatthias Ringwald             }
1398a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]);
13999cae807eSMatthias Ringwald 
1400a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
140174386ee0SMatthias Ringwald 
1402ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1403aa4dd815SMatthias Ringwald             break;
1404aa4dd815SMatthias Ringwald 
14059cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1406d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1407d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1408a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1409f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1410d210d9c4SMatthias Ringwald                 break;
1411d210d9c4SMatthias Ringwald             }
1412d210d9c4SMatthias Ringwald 
141374386ee0SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED);
1414a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
141574386ee0SMatthias Ringwald 
141660ebb071SMilanka Ringwald             log_info("Redial last number");
14179cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1418aa4dd815SMatthias Ringwald 
14199cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
142060ebb071SMilanka Ringwald                 log_info("Last number exists: accept call");
1421ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
14229cae807eSMatthias Ringwald             } else {
142360ebb071SMilanka Ringwald                 log_info("log_infoLast number missing: reject call");
14249cae807eSMatthias Ringwald                 hfp_ag_outgoing_call_rejected();
14259cae807eSMatthias Ringwald             }
14269cae807eSMatthias Ringwald             break;
14279cae807eSMatthias Ringwald         }
1428aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
1429a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1430a0ffb263SMatthias Ringwald             if (!hfp_connection){
1431a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1432aa4dd815SMatthias Ringwald                 break;
1433aa4dd815SMatthias Ringwald             }
1434d210d9c4SMatthias Ringwald 
1435d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED);
1436a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1437a0ffb263SMatthias Ringwald             hfp_connection->send_error = 1;
1438f0aeb307SMatthias Ringwald             hfp_ag_run_for_context(hfp_connection);
1439aa4dd815SMatthias Ringwald             break;
1440aa4dd815SMatthias Ringwald 
1441d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1442a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1443a0ffb263SMatthias Ringwald             if (!hfp_connection){
1444a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1445aa4dd815SMatthias Ringwald                 break;
1446aa4dd815SMatthias Ringwald             }
1447aa4dd815SMatthias Ringwald 
1448a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1449a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1450aa4dd815SMatthias Ringwald 
1451aa4dd815SMatthias Ringwald             // trigger callsetup to be
1452d0c20769SMatthias Ringwald             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1453d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED);
1454d210d9c4SMatthias Ringwald 
1455d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1456aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1457a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1458aa4dd815SMatthias Ringwald 
1459aa4dd815SMatthias Ringwald             // put current call on hold if active
1460d210d9c4SMatthias Ringwald             if (put_call_on_hold){
146160ebb071SMilanka Ringwald                 log_info("AG putting current call on hold for new outgoing calllog_info");
1462d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1463aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1464485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1465aa4dd815SMatthias Ringwald             }
1466aa4dd815SMatthias Ringwald 
1467aa4dd815SMatthias Ringwald             // start audio if needed
1468d97d752dSMilanka Ringwald             hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1469aa4dd815SMatthias Ringwald             break;
1470d210d9c4SMatthias Ringwald         }
1471aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1472a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1473a0ffb263SMatthias Ringwald             if (!hfp_connection){
1474a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1475aa4dd815SMatthias Ringwald                 break;
1476aa4dd815SMatthias Ringwald             }
1477d210d9c4SMatthias Ringwald 
1478d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING);
1479a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1480d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1481aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1482aa4dd815SMatthias Ringwald             break;
1483aa4dd815SMatthias Ringwald 
1484d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1485aa4dd815SMatthias Ringwald             // get outgoing call
1486a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1487a0ffb263SMatthias Ringwald             if (!hfp_connection){
1488a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1489aa4dd815SMatthias Ringwald             }
1490a0ffb263SMatthias Ringwald             if (!hfp_connection){
1491a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1492aa4dd815SMatthias Ringwald                 break;
1493aa4dd815SMatthias Ringwald             }
1494d210d9c4SMatthias Ringwald 
1495d0c20769SMatthias 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;
1496d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED);
1497a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1498d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1499d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1500aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1501aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1502d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1503d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1504aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
15053deb3ec6SMatthias Ringwald             }
15063deb3ec6SMatthias Ringwald             break;
1507d210d9c4SMatthias Ringwald         }
1508d210d9c4SMatthias Ringwald 
1509d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1510d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY);
1511d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1512a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1513a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
151460ebb071SMilanka Ringwald             log_info("AG: Call Waiting, User Busy");
1515d210d9c4SMatthias Ringwald             break;
1516d210d9c4SMatthias Ringwald 
1517d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1518d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1519d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1520d210d9c4SMatthias Ringwald 
1521d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1522d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1523a0ffb263SMatthias Ringwald                 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1524d0c20769SMatthias Ringwald 
1525d0c20769SMatthias Ringwald             }
1526d0c20769SMatthias Ringwald 
1527d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
152860ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Accept new call");
1529d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1530a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1531d210d9c4SMatthias Ringwald             } else {
153260ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Resume held call");
1533d210d9c4SMatthias Ringwald             }
1534d210d9c4SMatthias Ringwald             if (call_held){
1535d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1536a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1537d210d9c4SMatthias Ringwald             }
1538d0c20769SMatthias Ringwald 
1539a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1540d210d9c4SMatthias Ringwald             break;
1541d210d9c4SMatthias Ringwald         }
1542d0c20769SMatthias Ringwald 
1543d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1544d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1545d210d9c4SMatthias Ringwald             // only update if callsetup changed
1546d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1547a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1548d0c20769SMatthias Ringwald 
1549d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
155060ebb071SMilanka Ringwald                 log_info("AG: Call on Hold, Accept new call");
1551d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1552a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1553d210d9c4SMatthias Ringwald             } else {
155460ebb071SMilanka Ringwald                 log_info("AG: Swap calls");
1555d210d9c4SMatthias Ringwald             }
1556d0c20769SMatthias Ringwald 
1557d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1558d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1559a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1560a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1561d210d9c4SMatthias Ringwald             break;
1562d210d9c4SMatthias Ringwald         }
1563d0c20769SMatthias Ringwald 
1564d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1565d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1566d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
156760ebb071SMilanka Ringwald                 log_info("AG: Join 3-way-call");
1568d210d9c4SMatthias Ringwald                 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL);
1569d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1570a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1571ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1572d210d9c4SMatthias Ringwald             }
1573a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1574d210d9c4SMatthias Ringwald             break;
1575d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1576d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1577d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS);
157860ebb071SMilanka Ringwald             log_info("AG: Transfer call -> Connect two calls and disconnect");
1579d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
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, call_indicator_index, 1);
1582a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1583a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1584d210d9c4SMatthias Ringwald             break;
1585ce263fc8SMatthias Ringwald 
15863deb3ec6SMatthias Ringwald         default:
15873deb3ec6SMatthias Ringwald             break;
15883deb3ec6SMatthias Ringwald     }
1589d210d9c4SMatthias Ringwald 
1590d0c20769SMatthias Ringwald 
15913deb3ec6SMatthias Ringwald }
15923deb3ec6SMatthias Ringwald 
15939cae807eSMatthias Ringwald 
1594a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
15959cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
15969cae807eSMatthias Ringwald     if (!active_call) return;
15979cae807eSMatthias Ringwald 
15989cae807eSMatthias Ringwald     int idx = active_call->index;
15999cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
16009cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
16019cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
16029cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
16039cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
16049cae807eSMatthias Ringwald     char * number = active_call->clip_number;
16059cae807eSMatthias Ringwald 
16069cae807eSMatthias Ringwald     char buffer[100];
16079cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
16089cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
16099cae807eSMatthias Ringwald     if (number){
16101cc1d9e9SMilanka Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
16119cae807eSMatthias Ringwald     }
16129cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
161360ebb071SMilanka 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,
16149cae807eSMatthias Ringwald        mode, mpty, type, number);
1615a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
16169cae807eSMatthias Ringwald }
16179cae807eSMatthias Ringwald 
1618473ac565SMatthias Ringwald 
1619febc14f5SMatthias Ringwald // sends pending command, returns if command was sent
1620febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){
162122387625SMatthias Ringwald 
1622a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1623a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1624a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1625a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1626a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
1627febc14f5SMatthias Ringwald             return 1;
16289cae807eSMatthias Ringwald         } else {
1629febc14f5SMatthias 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
1630a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1631a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1632a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16339cae807eSMatthias Ringwald         }
1634ce263fc8SMatthias Ringwald     }
1635ce263fc8SMatthias Ringwald 
1636a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1637a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1638a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1639febc14f5SMatthias Ringwald         return 1;
1640aa4dd815SMatthias Ringwald     }
1641aa4dd815SMatthias Ringwald 
1642a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1643a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1644a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1645a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1646485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1647febc14f5SMatthias Ringwald         return 1;
1648a0ffb263SMatthias Ringwald     }
1649a0ffb263SMatthias Ringwald 
1650a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1651a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1652a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1653485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1654febc14f5SMatthias Ringwald         return 1;
1655aa4dd815SMatthias Ringwald     }
1656aa4dd815SMatthias Ringwald 
1657ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1658a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1659a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1660a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1661485ac19eSMilanka Ringwald         hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1662febc14f5SMatthias Ringwald         return 1;
1663ce263fc8SMatthias Ringwald     }
1664ce263fc8SMatthias Ringwald 
1665a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1666a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1667a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1668485ac19eSMilanka Ringwald         hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1669febc14f5SMatthias Ringwald         return 1;
1670ce263fc8SMatthias Ringwald     }
1671ce263fc8SMatthias Ringwald 
1672aa4dd815SMatthias Ringwald     // update AG indicators
1673a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1674aa4dd815SMatthias Ringwald         int i;
1675a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1676a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1677a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1678a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1679ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1680ce263fc8SMatthias Ringwald                     break;
1681ce263fc8SMatthias Ringwald                 }
1682485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1683febc14f5SMatthias Ringwald                 return 1;
1684aa4dd815SMatthias Ringwald             }
1685aa4dd815SMatthias Ringwald         }
1686aa4dd815SMatthias Ringwald     }
1687aa4dd815SMatthias Ringwald 
1688a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1689a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1690a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1691485ac19eSMilanka Ringwald         hfp_ag_send_ring(hfp_connection->rfcomm_cid);
1692febc14f5SMatthias Ringwald         return 1;
1693aa4dd815SMatthias Ringwald     }
1694aa4dd815SMatthias Ringwald 
1695a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1696a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1697a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1698a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1699febc14f5SMatthias Ringwald         return 1;
1700aa4dd815SMatthias Ringwald     }
1701aa4dd815SMatthias Ringwald 
1702a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1703a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1704a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1705a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1706a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1707febc14f5SMatthias Ringwald         return 1;
1708aa4dd815SMatthias Ringwald     }
1709aa4dd815SMatthias Ringwald 
1710a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1711a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1712a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1713a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1714ce263fc8SMatthias Ringwald         } else {
1715a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1716a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1717485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1718ce263fc8SMatthias Ringwald         }
1719a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1720febc14f5SMatthias Ringwald         return 1;
1721ce263fc8SMatthias Ringwald     }
1722ce263fc8SMatthias Ringwald 
1723a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1724a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1725a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1726485ac19eSMilanka Ringwald         hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1727febc14f5SMatthias Ringwald         return 1;
1728aa4dd815SMatthias Ringwald     }
1729aa4dd815SMatthias Ringwald 
1730a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1731a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1732a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1733485ac19eSMilanka Ringwald         hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
1734febc14f5SMatthias Ringwald         return 1;
17353deb3ec6SMatthias Ringwald     }
17363deb3ec6SMatthias Ringwald 
1737a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1738a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1739485ac19eSMilanka Ringwald         hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1740febc14f5SMatthias Ringwald         return 1;
1741febc14f5SMatthias Ringwald     }
1742febc14f5SMatthias Ringwald 
1743febc14f5SMatthias Ringwald     return 0;
1744febc14f5SMatthias Ringwald }
1745febc14f5SMatthias Ringwald 
1746febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){
1747febc14f5SMatthias Ringwald 
1748febc14f5SMatthias Ringwald     if (!hfp_connection) return;
1749febc14f5SMatthias Ringwald 
1750febc14f5SMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
1751febc14f5SMatthias Ringwald 
1752febc14f5SMatthias Ringwald     if (hfp_connection->local_role != HFP_ROLE_AG) {
1753febc14f5SMatthias Ringwald         log_info("HFP AG%p, wrong role %u", hfp_connection, hfp_connection->local_role);
1754ce263fc8SMatthias Ringwald         return;
1755ce263fc8SMatthias Ringwald     }
1756ce263fc8SMatthias Ringwald 
1757febc14f5SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1758febc14f5SMatthias Ringwald         log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1759febc14f5SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1760febc14f5SMatthias Ringwald         return;
1761febc14f5SMatthias Ringwald     }
1762febc14f5SMatthias Ringwald 
1763febc14f5SMatthias Ringwald     int cmd_sent = hfp_ag_send_commands(hfp_connection);
1764febc14f5SMatthias Ringwald 
17652ee45eb2SMilanka Ringwald     // trigger codec exchange if requested by app
17662ee45eb2SMilanka Ringwald     if (hfp_connection->trigger_codec_exchange){
17672ee45eb2SMilanka Ringwald         log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state);
17682ee45eb2SMilanka Ringwald     }
1769febc14f5SMatthias Ringwald 
17702ee45eb2SMilanka Ringwald     if (hfp_connection->trigger_codec_exchange && hfp_connection->command == HFP_CMD_NONE){
17712ee45eb2SMilanka Ringwald         switch (hfp_connection->codecs_state){
17722ee45eb2SMilanka Ringwald             case HFP_CODECS_IDLE:
17732ee45eb2SMilanka Ringwald             case HFP_CODECS_RECEIVED_LIST:
17742ee45eb2SMilanka Ringwald             case HFP_CODECS_AG_RESEND_COMMON_CODEC:
17752ee45eb2SMilanka Ringwald             case HFP_CODECS_ERROR:
17762ee45eb2SMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 0;
17772ee45eb2SMilanka Ringwald                 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
17782ee45eb2SMilanka Ringwald                 break;
17792ee45eb2SMilanka Ringwald             default:
17802ee45eb2SMilanka Ringwald                 break;
17812ee45eb2SMilanka Ringwald         }
17822ee45eb2SMilanka Ringwald     }
17832ee45eb2SMilanka Ringwald 
1784febc14f5SMatthias Ringwald     if (!cmd_sent){
1785febc14f5SMatthias Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1786febc14f5SMatthias Ringwald     }
1787febc14f5SMatthias Ringwald 
1788c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1789c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
17903deb3ec6SMatthias Ringwald     }
17913deb3ec6SMatthias Ringwald 
1792c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1793c04cf7ffSMilanka Ringwald         cmd_sent = call_setup_state_machine(hfp_connection);
1794aa4dd815SMatthias Ringwald     }
1795aa4dd815SMatthias Ringwald 
1796c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1797c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
1798aa4dd815SMatthias Ringwald     }
17993deb3ec6SMatthias Ringwald 
1800c04cf7ffSMilanka Ringwald     if (hfp_connection->command == HFP_CMD_NONE && !cmd_sent){
1801a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1802a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
18033deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1804a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1805a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
18063deb3ec6SMatthias Ringwald                 break;
18073deb3ec6SMatthias Ringwald             default:
18083deb3ec6SMatthias Ringwald                 break;
18093deb3ec6SMatthias Ringwald         }
18103deb3ec6SMatthias Ringwald     }
1811c04cf7ffSMilanka Ringwald 
1812c04cf7ffSMilanka Ringwald     if (cmd_sent){
1813a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1814473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1815473ac565SMatthias Ringwald     }
18163deb3ec6SMatthias Ringwald }
1817d68dcce1SMatthias Ringwald 
1818ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1819ce263fc8SMatthias Ringwald     int i;
1820a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1821a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1822ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1823ce263fc8SMatthias Ringwald             return indicator;
1824ce263fc8SMatthias Ringwald         }
1825ce263fc8SMatthias Ringwald     }
1826ce263fc8SMatthias Ringwald     return NULL;
1827ce263fc8SMatthias Ringwald }
18283deb3ec6SMatthias Ringwald 
1829e9c22d4eSMatthias Ringwald static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
18308a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
18318a46ec40SMatthias Ringwald 
18328a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
18338a46ec40SMatthias Ringwald     if (size < 1) return;
18349ec2630cSMatthias Ringwald 
1835a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1836a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
18371e35c04dSMatthias Ringwald 
1838186dd3d2SMatthias Ringwald     hfp_log_rfcomm_message("HFP_AG_RX", packet, size);
18391e35c04dSMatthias Ringwald 
18408a46ec40SMatthias Ringwald     // process messages byte-wise
18413deb3ec6SMatthias Ringwald     int pos;
18423deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1843a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1844186dd3d2SMatthias Ringwald 
1845ce263fc8SMatthias Ringwald         int value;
1846186dd3d2SMatthias Ringwald         hfp_generic_status_indicator_t * indicator;
1847a0ffb263SMatthias Ringwald         switch(hfp_connection->command){
1848ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1849ce263fc8SMatthias Ringwald                 if (hfp_ag_response_and_hold_active){
1850a0ffb263SMatthias Ringwald                     hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1851ce263fc8SMatthias Ringwald                 }
1852a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1853ce263fc8SMatthias Ringwald                 break;
1854ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
18552308e108SMilanka Ringwald                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
185660ebb071SMilanka Ringwald                 log_info("HF Response and Hold: %u", value);
1857ce263fc8SMatthias Ringwald                 switch(value){
1858ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1859a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1860ce263fc8SMatthias Ringwald                         break;
1861ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1862a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1863ce263fc8SMatthias Ringwald                         break;
1864ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1865a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1866ce263fc8SMatthias Ringwald                         break;
1867ce263fc8SMatthias Ringwald                     default:
1868ce263fc8SMatthias Ringwald                         break;
1869ce263fc8SMatthias Ringwald                 }
1870a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1871ce263fc8SMatthias Ringwald                 break;
1872ce263fc8SMatthias Ringwald             case HFP_CMD_HF_INDICATOR_STATUS:
1873a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1874ce263fc8SMatthias Ringwald                 // find indicator by assigned number
1875a0ffb263SMatthias Ringwald                 indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1876ce263fc8SMatthias Ringwald                 if (!indicator){
1877a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
1878ce263fc8SMatthias Ringwald                     break;
1879ce263fc8SMatthias Ringwald                 }
18802308e108SMilanka Ringwald                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1881ce263fc8SMatthias Ringwald                 switch (indicator->uuid){
1882ce263fc8SMatthias Ringwald                     case 1: // enhanced security
1883ce263fc8SMatthias Ringwald                         if (value > 1) {
1884a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1885ce263fc8SMatthias Ringwald                             return;
1886ce263fc8SMatthias Ringwald                         }
188760ebb071SMilanka Ringwald                         log_info("HF Indicator 'enhanced security' set to %u", value);
1888ce263fc8SMatthias Ringwald                         break;
1889ce263fc8SMatthias Ringwald                     case 2: // battery level
1890ce263fc8SMatthias Ringwald                         if (value > 100){
1891a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1892ce263fc8SMatthias Ringwald                             return;
1893ce263fc8SMatthias Ringwald                         }
189460ebb071SMilanka Ringwald                         log_info("HF Indicator 'battery' set to %u", value);
1895ce263fc8SMatthias Ringwald                         break;
1896ce263fc8SMatthias Ringwald                     default:
189760ebb071SMilanka Ringwald                         log_info("HF Indicator unknown set to %u", value);
1898ce263fc8SMatthias Ringwald                         break;
1899ce263fc8SMatthias Ringwald                 }
1900a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1901ce263fc8SMatthias Ringwald                 break;
1902ce263fc8SMatthias Ringwald             case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1903ce263fc8SMatthias Ringwald                 // expected by SLC state machine
1904a0ffb263SMatthias Ringwald                 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1905a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
1906a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_status_indicators = 1;
1907ce263fc8SMatthias Ringwald                 break;
1908ce263fc8SMatthias Ringwald             case HFP_CMD_LIST_CURRENT_CALLS:
1909a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1910a0ffb263SMatthias Ringwald                 hfp_connection->next_call_index = 0;
1911a0ffb263SMatthias Ringwald                 hfp_connection->send_status_of_current_calls = 1;
1912ce263fc8SMatthias Ringwald                 break;
1913ce263fc8SMatthias Ringwald             case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1914ce263fc8SMatthias Ringwald                 if (subscriber_numbers_count == 0){
1915485ac19eSMilanka Ringwald                     hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1916ce263fc8SMatthias Ringwald                     break;
1917ce263fc8SMatthias Ringwald                 }
1918a0ffb263SMatthias Ringwald                 hfp_connection->next_subscriber_number_to_send = 0;
1919a0ffb263SMatthias Ringwald                 hfp_connection->send_subscriber_number = 1;
1920ce263fc8SMatthias Ringwald                 break;
1921c1797c7dSMatthias Ringwald             case HFP_CMD_TRANSMIT_DTMF_CODES:
1922a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1923ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]);
1924c1797c7dSMatthias Ringwald                 break;
1925aa4dd815SMatthias Ringwald             case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1926a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1927ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1928aa4dd815SMatthias Ringwald                 break;
1929aa4dd815SMatthias Ringwald             case HFP_CMD_TURN_OFF_EC_AND_NR:
1930a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1931aa4dd815SMatthias Ringwald                 if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1932a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
1933a0ffb263SMatthias Ringwald                     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
193460ebb071SMilanka Ringwald                     log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction);
1935aa4dd815SMatthias Ringwald                 } else {
1936a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
1937aa4dd815SMatthias Ringwald                 }
1938aa4dd815SMatthias Ringwald                 break;
1939aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_ANSWERED:
1940a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
194160ebb071SMilanka Ringwald                 log_info("HFP: ATA");
1942a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1943aa4dd815SMatthias Ringwald                 break;
1944aa4dd815SMatthias Ringwald             case HFP_CMD_HANG_UP_CALL:
1945a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1946a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1947a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1948aa4dd815SMatthias Ringwald                 break;
1949aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_HOLD: {
1950aa4dd815SMatthias Ringwald                 // TODO: fully implement this
1951a0ffb263SMatthias Ringwald                 log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]);
1952a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1953a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1954a0ffb263SMatthias Ringwald                 hfp_connection->call_index = 0;
1955d0c20769SMatthias Ringwald 
1956a0ffb263SMatthias Ringwald                 if (hfp_connection->line_buffer[1] != '\0'){
19572308e108SMilanka Ringwald                     hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
1958d0c20769SMatthias Ringwald                 }
1959d210d9c4SMatthias Ringwald 
1960a0ffb263SMatthias Ringwald                 switch (hfp_connection->line_buffer[0]){
1961aa4dd815SMatthias Ringwald                     case '0':
1962d0c20769SMatthias Ringwald                         // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1963a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1964aa4dd815SMatthias Ringwald                         break;
1965aa4dd815SMatthias Ringwald                     case '1':
1966d0c20769SMatthias Ringwald                         // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1967d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
1968d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
1969a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1970aa4dd815SMatthias Ringwald                         break;
1971aa4dd815SMatthias Ringwald                     case '2':
1972d0c20769SMatthias Ringwald                         // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1973d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
1974d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
1975a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1976aa4dd815SMatthias Ringwald                         break;
1977aa4dd815SMatthias Ringwald                     case '3':
1978d0c20769SMatthias Ringwald                         // Adds a held call to the conversation.
1979a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1980aa4dd815SMatthias Ringwald                         break;
1981aa4dd815SMatthias Ringwald                     case '4':
1982d0c20769SMatthias Ringwald                         // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1983a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1984aa4dd815SMatthias Ringwald                         break;
1985aa4dd815SMatthias Ringwald                     default:
1986aa4dd815SMatthias Ringwald                         break;
1987aa4dd815SMatthias Ringwald                 }
1988aa4dd815SMatthias Ringwald                 break;
1989aa4dd815SMatthias Ringwald             }
1990aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_PHONE_NUMBER:
1991a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1992a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
1993aa4dd815SMatthias Ringwald                 break;
1994aa4dd815SMatthias Ringwald             case HFP_CMD_REDIAL_LAST_NUMBER:
1995a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1996a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
1997aa4dd815SMatthias Ringwald                 break;
1998aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CLIP:
1999a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2000a0ffb263SMatthias Ringwald                 hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0';
2001a0ffb263SMatthias Ringwald                 log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
2002a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2003aa4dd815SMatthias Ringwald                 break;
2004aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
2005a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2006a0ffb263SMatthias Ringwald                 hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0';
2007a0ffb263SMatthias Ringwald                 log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
2008a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2009aa4dd815SMatthias Ringwald                 break;
2010aa4dd815SMatthias Ringwald             case HFP_CMD_SET_SPEAKER_GAIN:
2011a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2012a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
201360ebb071SMilanka Ringwald                 log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
2014aa4dd815SMatthias Ringwald                 break;
2015aa4dd815SMatthias Ringwald             case HFP_CMD_SET_MICROPHONE_GAIN:
2016a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2017a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
201860ebb071SMilanka Ringwald                 log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
2019aa4dd815SMatthias Ringwald                 break;
2020aa4dd815SMatthias Ringwald             default:
2021aa4dd815SMatthias Ringwald                 break;
20223deb3ec6SMatthias Ringwald         }
20233deb3ec6SMatthias Ringwald     }
2024*0cef86faSMatthias Ringwald }
20253deb3ec6SMatthias Ringwald 
2026aa4dd815SMatthias Ringwald static void hfp_run(void){
2027d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
2028d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2029d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2030d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
203122387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2032f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
20333deb3ec6SMatthias Ringwald     }
20343deb3ec6SMatthias Ringwald }
20353deb3ec6SMatthias Ringwald 
2036e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
20373deb3ec6SMatthias Ringwald     switch (packet_type){
20383deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
2039e9c22d4eSMatthias Ringwald             hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size);
20403deb3ec6SMatthias Ringwald             break;
20413deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2042e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2043e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2044f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2045e30a6a47SMatthias Ringwald                 return;
2046e30a6a47SMatthias Ringwald             }
204727950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2048aa4dd815SMatthias Ringwald             break;
20493deb3ec6SMatthias Ringwald         default:
20503deb3ec6SMatthias Ringwald             break;
20513deb3ec6SMatthias Ringwald     }
20523deb3ec6SMatthias Ringwald 
20533deb3ec6SMatthias Ringwald     hfp_run();
20543deb3ec6SMatthias Ringwald }
20553deb3ec6SMatthias Ringwald 
2056405014fbSMatthias Ringwald static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2057405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
205805bed9f6SMatthias Ringwald 
205905bed9f6SMatthias Ringwald     // allow for sco established -> ring transition
206005bed9f6SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
206105bed9f6SMatthias Ringwald     if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return;
206205bed9f6SMatthias Ringwald     hfp_run();
2063405014fbSMatthias Ringwald }
2064405014fbSMatthias Ringwald 
2065a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
20663deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
20673deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
20683deb3ec6SMatthias Ringwald         return;
20693deb3ec6SMatthias Ringwald     }
20703deb3ec6SMatthias Ringwald     int i;
2071a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
20723deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
20733deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
20743deb3ec6SMatthias Ringwald     }
2075a0ffb263SMatthias Ringwald }
20763deb3ec6SMatthias Ringwald 
2077a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2078a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2079a0ffb263SMatthias Ringwald }
20803deb3ec6SMatthias Ringwald 
2081a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2082a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
2083a0ffb263SMatthias Ringwald     memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2084a0ffb263SMatthias Ringwald }
20853deb3ec6SMatthias Ringwald 
2086a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
2087a0ffb263SMatthias Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return;
2088a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
2089a0ffb263SMatthias Ringwald     memcpy(hfp_generic_status_indicators, hf_indicators, hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2090a0ffb263SMatthias Ringwald }
2091a0ffb263SMatthias Ringwald 
2092a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
20933deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
20943deb3ec6SMatthias Ringwald     memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *));
2095a0ffb263SMatthias Ringwald }
2096a0ffb263SMatthias Ringwald 
2097a0ffb263SMatthias Ringwald 
2098a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
209927950165SMatthias Ringwald 
2100520c92d5SMatthias Ringwald     hfp_init();
2101d63c37a1SMatthias Ringwald 
2102405014fbSMatthias Ringwald     hci_event_callback_registration.callback = &hci_event_packet_handler;
210327950165SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
210427950165SMatthias Ringwald 
2105e9c22d4eSMatthias Ringwald     rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
210627950165SMatthias Ringwald 
210727950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
2108e9c22d4eSMatthias Ringwald     hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler);
2109aa4dd815SMatthias Ringwald 
2110d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2111d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2112d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2113d210d9c4SMatthias Ringwald 
2114d210d9c4SMatthias Ringwald     hfp_gsm_init();
21153deb3ec6SMatthias Ringwald }
21163deb3ec6SMatthias Ringwald 
21173deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
2118323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
21193deb3ec6SMatthias Ringwald }
21203deb3ec6SMatthias Ringwald 
2121d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
21229c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2123a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2124a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2125a33eb0c4SMilanka Ringwald         return;
2126a33eb0c4SMilanka Ringwald     }
2127a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2128f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21293deb3ec6SMatthias Ringwald }
21303deb3ec6SMatthias Ringwald 
2131d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
21329c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2133a0ffb263SMatthias Ringwald     if (!hfp_connection){
2134a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
21353deb3ec6SMatthias Ringwald         return;
21363deb3ec6SMatthias Ringwald     }
2137a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2138a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
21393deb3ec6SMatthias Ringwald         return;
21403deb3ec6SMatthias Ringwald     }
2141a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2142f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21433deb3ec6SMatthias Ringwald }
21443deb3ec6SMatthias Ringwald 
2145a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
21462ee45eb2SMilanka Ringwald     log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state);
2147a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2148a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
21493deb3ec6SMatthias Ringwald 
2150a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2151a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2152d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2153d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2154a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
21557522e673SMatthias Ringwald         // now, pick link settings
2156afac2a04SMilanka Ringwald         hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
21572ee45eb2SMilanka Ringwald         return;
21583deb3ec6SMatthias Ringwald     }
2159aa4dd815SMatthias Ringwald 
21602ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 1;
2161aa4dd815SMatthias Ringwald }
2162aa4dd815SMatthias Ringwald 
2163d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
21649c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2165a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2166a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2167a33eb0c4SMilanka Ringwald         return;
2168a33eb0c4SMilanka Ringwald     }
21692ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 0;
2170a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2171a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2172f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21733deb3ec6SMatthias Ringwald }
21743deb3ec6SMatthias Ringwald 
2175d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
21769c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2177a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2178a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2179a33eb0c4SMilanka Ringwald         return;
2180a33eb0c4SMilanka Ringwald     }
2181a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2182f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21833deb3ec6SMatthias Ringwald }
2184aa4dd815SMatthias Ringwald 
2185aa4dd815SMatthias Ringwald /**
2186aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2187aa4dd815SMatthias Ringwald  */
2188aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2189aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2190aa4dd815SMatthias Ringwald         return;
2191aa4dd815SMatthias Ringwald     }
2192aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2193aa4dd815SMatthias Ringwald 
2194665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2195665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2196665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2197a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
219866c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2199a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2200f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2201aa4dd815SMatthias Ringwald     }
2202aa4dd815SMatthias Ringwald }
2203aa4dd815SMatthias Ringwald 
2204aa4dd815SMatthias Ringwald /**
2205aa4dd815SMatthias Ringwald  * @brief Called from GSM
2206aa4dd815SMatthias Ringwald  */
2207aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2208aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2209aa4dd815SMatthias Ringwald }
2210aa4dd815SMatthias Ringwald 
2211aa4dd815SMatthias Ringwald /**
2212aa4dd815SMatthias Ringwald  * @brief number is stored.
2213aa4dd815SMatthias Ringwald  */
2214aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2215d0c20769SMatthias Ringwald     hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number);
2216aa4dd815SMatthias Ringwald }
2217aa4dd815SMatthias Ringwald 
2218aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2219aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2220aa4dd815SMatthias Ringwald }
2221aa4dd815SMatthias Ringwald 
2222aa4dd815SMatthias Ringwald // call from AG UI
2223aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2224aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2225aa4dd815SMatthias Ringwald }
2226aa4dd815SMatthias Ringwald 
2227ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2228ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2229ce263fc8SMatthias Ringwald }
2230ce263fc8SMatthias Ringwald 
2231aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2232aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2233aa4dd815SMatthias Ringwald }
2234aa4dd815SMatthias Ringwald 
2235aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2236aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2237aa4dd815SMatthias Ringwald }
2238aa4dd815SMatthias Ringwald 
2239aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2240aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2241aa4dd815SMatthias Ringwald }
2242aa4dd815SMatthias Ringwald 
2243aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2244aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2245aa4dd815SMatthias Ringwald }
2246aa4dd815SMatthias Ringwald 
2247aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2248aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2249aa4dd815SMatthias Ringwald }
2250ce263fc8SMatthias Ringwald 
2251ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2252ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2253ce263fc8SMatthias Ringwald }
2254ce263fc8SMatthias Ringwald 
2255ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2256ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2257ce263fc8SMatthias Ringwald }
2258ce263fc8SMatthias Ringwald 
2259ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2260ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2261ce263fc8SMatthias Ringwald }
2262ce263fc8SMatthias Ringwald 
2263aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2264aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2265aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2266aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2267aa4dd815SMatthias Ringwald 
2268ce263fc8SMatthias Ringwald 
2269665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2270665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2271665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2272a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
227366c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2274a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2275ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2276ce263fc8SMatthias Ringwald             continue;
2277ce263fc8SMatthias Ringwald         }
2278ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2279a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2280f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2281aa4dd815SMatthias Ringwald     }
2282aa4dd815SMatthias Ringwald }
2283aa4dd815SMatthias Ringwald 
2284aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2285aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2286aa4dd815SMatthias Ringwald }
2287aa4dd815SMatthias Ringwald 
2288aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2289aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2290aa4dd815SMatthias Ringwald }
2291aa4dd815SMatthias Ringwald 
2292aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2293aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2294aa4dd815SMatthias Ringwald }
2295aa4dd815SMatthias Ringwald 
2296aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2297aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2298aa4dd815SMatthias Ringwald }
2299aa4dd815SMatthias Ringwald 
2300d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){
2301aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
23029c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2303a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2304a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2305a33eb0c4SMilanka Ringwald         return;
2306a33eb0c4SMilanka Ringwald     }
2307aa4dd815SMatthias Ringwald 
2308a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
230960ebb071SMilanka Ringwald         log_info("AG cannot acivate voice recognition - not supported by HF");
2310aa4dd815SMatthias Ringwald         return;
2311aa4dd815SMatthias Ringwald     }
2312aa4dd815SMatthias Ringwald 
2313aa4dd815SMatthias Ringwald     if (activate){
2314d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(acl_handle);
2315aa4dd815SMatthias Ringwald     }
2316aa4dd815SMatthias Ringwald 
2317a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2318a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2319f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2320aa4dd815SMatthias Ringwald }
2321aa4dd815SMatthias Ringwald 
2322d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
23239c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2324a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2325a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2326a33eb0c4SMilanka Ringwald         return;
2327a33eb0c4SMilanka Ringwald     }
2328a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2329a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2330a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2331a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2332aa4dd815SMatthias Ringwald     }
2333f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2334aa4dd815SMatthias Ringwald }
2335aa4dd815SMatthias Ringwald 
2336d97d752dSMilanka Ringwald void hfp_ag_set_speaker_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->speaker_gain != gain){
2343a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2344a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2345aa4dd815SMatthias Ringwald     }
2346f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2347aa4dd815SMatthias Ringwald }
2348aa4dd815SMatthias Ringwald 
2349d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
23509c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2351a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2352a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2353a33eb0c4SMilanka Ringwald         return;
2354a33eb0c4SMilanka Ringwald     }
2355aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2356a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2357aa4dd815SMatthias Ringwald }
2358aa4dd815SMatthias Ringwald 
2359d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
23609c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2361a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2362a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2363a33eb0c4SMilanka Ringwald         return;
2364a33eb0c4SMilanka Ringwald     }
2365a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2366aa4dd815SMatthias Ringwald }
2367aa4dd815SMatthias Ringwald 
2368d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
23699c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2370a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2371a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2372a33eb0c4SMilanka Ringwald         return;
2373a33eb0c4SMilanka Ringwald     }
2374a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2375c1797c7dSMatthias Ringwald }
2376aa4dd815SMatthias Ringwald 
2377ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2378ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2379ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2380ce263fc8SMatthias Ringwald }
2381ce263fc8SMatthias Ringwald 
23829cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
23839cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2384ce263fc8SMatthias Ringwald }
2385ce263fc8SMatthias Ringwald 
2386d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
23879c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2388a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2389a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2390a33eb0c4SMilanka Ringwald         return;
2391a33eb0c4SMilanka Ringwald     }
2392a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2393a0ffb263SMatthias Ringwald 
2394a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2395f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2396a0ffb263SMatthias Ringwald }
2397ce263fc8SMatthias Ringwald 
2398