xref: /btstack/src/classic/hfp_ag.c (revision 6535961a1f105e850ecdf294c128b9fc982ee05a)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp_ag.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
4266a048abSMatthias Ringwald // HFP Audio Gateway (AG) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493deb3ec6SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <stdlib.h>
513deb3ec6SMatthias Ringwald #include <string.h>
523deb3ec6SMatthias Ringwald 
5356042629SMatthias Ringwald #include "hci_cmd.h"
5482636622SMatthias Ringwald #include "btstack_run_loop.h"
553deb3ec6SMatthias Ringwald 
56235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
573deb3ec6SMatthias Ringwald #include "hci.h"
583deb3ec6SMatthias Ringwald #include "btstack_memory.h"
593deb3ec6SMatthias Ringwald #include "hci_dump.h"
603deb3ec6SMatthias Ringwald #include "l2cap.h"
6116ece135SMatthias Ringwald #include "btstack_debug.h"
62e30a6a47SMatthias Ringwald #include "btstack_event.h"
6359c6af15SMatthias Ringwald #include "classic/core.h"
643edc84c5SMatthias Ringwald #include "classic/hfp.h"
653edc84c5SMatthias Ringwald #include "classic/hfp_ag.h"
66023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h"
67efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
68023f2764SMatthias Ringwald #include "classic/sdp_server.h"
69023f2764SMatthias Ringwald #include "classic/sdp_util.h"
703deb3ec6SMatthias Ringwald 
71c5b64319SMatthias Ringwald // private prototypes
72f0aeb307SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection);
73adaba9f3SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
74adaba9f3SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
75c5b64319SMatthias Ringwald 
76c5b64319SMatthias Ringwald // public prototypes
7735833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void);
7835833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void);
79c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr);
80c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr);
81c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context);
82c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context);
83c5b64319SMatthias Ringwald 
8427950165SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
8527950165SMatthias Ringwald 
86c5b64319SMatthias Ringwald // gobals
873deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway";
88aa4dd815SMatthias Ringwald 
893deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
90aa4dd815SMatthias Ringwald 
913deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
923deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
933deb3ec6SMatthias Ringwald 
943deb3ec6SMatthias Ringwald static int  hfp_ag_indicators_nr = 0;
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;
117*6535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
118*6535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1193deb3ec6SMatthias Ringwald     }
120a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
121a0ffb263SMatthias Ringwald }
122a0ffb263SMatthias Ringwald 
123a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
124a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
125a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
126a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
127*6535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
128*6535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
129a0ffb263SMatthias Ringwald     }
130a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1313deb3ec6SMatthias Ringwald }
1323deb3ec6SMatthias Ringwald 
133aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
134aa4dd815SMatthias Ringwald     int i;
135aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
136aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
137aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
138aa4dd815SMatthias Ringwald         }
139aa4dd815SMatthias Ringwald     }
140aa4dd815SMatthias Ringwald     return NULL;
141aa4dd815SMatthias Ringwald }
142aa4dd815SMatthias Ringwald 
143aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
144aa4dd815SMatthias Ringwald     int i;
145aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
146aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
147aa4dd815SMatthias Ringwald             return i;
148aa4dd815SMatthias Ringwald         }
149aa4dd815SMatthias Ringwald     }
150aa4dd815SMatthias Ringwald     return -1;
151aa4dd815SMatthias Ringwald }
152aa4dd815SMatthias Ringwald 
1539c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){
1549c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1559c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1569c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1579c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1589c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1599c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1609c9c64c1SMatthias Ringwald         return hfp_connection;
1619c9c64c1SMatthias Ringwald     }
1629c9c64c1SMatthias Ringwald     return NULL;
1639c9c64c1SMatthias Ringwald }
1643deb3ec6SMatthias Ringwald 
16513839019SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
1663deb3ec6SMatthias Ringwald     if (callback == NULL){
1673deb3ec6SMatthias Ringwald         log_error("hfp_ag_register_packet_handler called with NULL callback");
1683deb3ec6SMatthias Ringwald         return;
1693deb3ec6SMatthias Ringwald     }
170ca59be51SMatthias Ringwald     hfp_ag_callback = callback;
171ca59be51SMatthias Ringwald     hfp_set_ag_callback(callback);
1723deb3ec6SMatthias Ringwald }
1733deb3ec6SMatthias Ringwald 
1740cb5b971SMatthias Ringwald static int use_in_band_tone(void){
175aa4dd815SMatthias Ringwald     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1763deb3ec6SMatthias Ringwald }
1773deb3ec6SMatthias Ringwald 
178a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
179a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1803deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1813deb3ec6SMatthias Ringwald     return hf && ag;
1823deb3ec6SMatthias Ringwald }
1833deb3ec6SMatthias Ringwald 
184a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
185a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1863deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1873deb3ec6SMatthias Ringwald     return hf && ag;
1883deb3ec6SMatthias Ringwald }
1893deb3ec6SMatthias Ringwald 
190a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
191a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
1923deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
1933deb3ec6SMatthias Ringwald     return hf && ag;
1943deb3ec6SMatthias Ringwald }
1953deb3ec6SMatthias Ringwald 
1964f84bf36SMatthias Ringwald void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint8_t ability_to_reject_call, uint16_t supported_features, int wide_band_speech){
1973deb3ec6SMatthias Ringwald     if (!name){
1983deb3ec6SMatthias Ringwald         name = default_hfp_ag_service_name;
1993deb3ec6SMatthias Ringwald     }
200235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
2013deb3ec6SMatthias Ringwald 
2023deb3ec6SMatthias Ringwald     /*
2033deb3ec6SMatthias Ringwald      * 0x01 – Ability to reject a call
2043deb3ec6SMatthias Ringwald      * 0x00 – No ability to reject a call
2053deb3ec6SMatthias Ringwald      */
206aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
207aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
208aa4dd815SMatthias Ringwald 
2094f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
2104f84bf36SMatthias Ringwald     //
2114f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
2124f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
2134f84bf36SMatthias Ringwald     //
21442bafa11SMatthias Ringwald     // Wide band speech (bit 5) requires Codec negotiation
21542bafa11SMatthias Ringwald     //
2164f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
21742bafa11SMatthias Ringwald     if (wide_band_speech && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){
21842bafa11SMatthias Ringwald         sdp_features |= 1 << 5;
2194f84bf36SMatthias Ringwald     }
220aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
2214f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
222aa4dd815SMatthias Ringwald }
223aa4dd815SMatthias Ringwald 
224485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){
225aa4dd815SMatthias Ringwald     char buffer[20];
226aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
227aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2283deb3ec6SMatthias Ringwald }
2293deb3ec6SMatthias Ringwald 
2303deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2313deb3ec6SMatthias Ringwald     char buffer[40];
2323deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
2333deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2343deb3ec6SMatthias Ringwald }
2353deb3ec6SMatthias Ringwald 
236485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){
2373deb3ec6SMatthias Ringwald     char buffer[10];
2383deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nOK\r\n");
2393deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2403deb3ec6SMatthias Ringwald }
2413deb3ec6SMatthias Ringwald 
242485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){
243aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
244aa4dd815SMatthias Ringwald }
245aa4dd815SMatthias Ringwald 
246aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
247aa4dd815SMatthias Ringwald     char buffer[50];
248d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
249aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
250aa4dd815SMatthias Ringwald }
251aa4dd815SMatthias Ringwald 
252ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
253ce263fc8SMatthias Ringwald     char buffer[50];
254ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
255ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
256ce263fc8SMatthias Ringwald }
257ce263fc8SMatthias Ringwald 
258c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
259aa4dd815SMatthias Ringwald     char buffer[50];
260d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
261aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
262aa4dd815SMatthias Ringwald }
263aa4dd815SMatthias Ringwald 
264aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
265aa4dd815SMatthias Ringwald     char buffer[50];
266a0ffb263SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
267aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
268aa4dd815SMatthias Ringwald }
269aa4dd815SMatthias Ringwald 
270485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){
2713deb3ec6SMatthias Ringwald     char buffer[10];
2723deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nERROR\r\n");
2733deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2743deb3ec6SMatthias Ringwald }
2753deb3ec6SMatthias Ringwald 
276485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2773deb3ec6SMatthias Ringwald     char buffer[20];
2783deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2793deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2803deb3ec6SMatthias Ringwald }
2813deb3ec6SMatthias Ringwald 
282ad902e3dSMatthias Ringwald // get size for indicator string
283a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
284ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
28545718b6fSMatthias Ringwald     return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
286a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
287a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
288ad902e3dSMatthias Ringwald }
289ad902e3dSMatthias Ringwald 
290ad902e3dSMatthias Ringwald // store indicator
291a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
292ad902e3dSMatthias Ringwald     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
293a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
294a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
295a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2963deb3ec6SMatthias Ringwald }
297ad902e3dSMatthias Ringwald 
298ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
299a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
300a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
301ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
302c1ab6cc1SMatthias Ringwald     return 3 + ((num_indicators-1) * 2);
3033deb3ec6SMatthias Ringwald }
304ad902e3dSMatthias Ringwald 
305ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
306a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
307ad902e3dSMatthias Ringwald     if (index == 0) {
308ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
309ad902e3dSMatthias Ringwald     }
310ad902e3dSMatthias Ringwald     index--;
311a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
312ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
313ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
314a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
315ad902e3dSMatthias Ringwald     }
316c1ab6cc1SMatthias Ringwald     if (indicator_index == (num_indicators - 1)){
317ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
318ad902e3dSMatthias Ringwald     }
319ad902e3dSMatthias Ringwald     return 1; // comma
320ad902e3dSMatthias Ringwald }
321ad902e3dSMatthias Ringwald 
322a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
323ad902e3dSMatthias Ringwald     if (index == 0){
324ad902e3dSMatthias Ringwald         *buffer++ = '\r';
32574386ee0SMatthias Ringwald         *buffer++ = '\n';
326ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
327*6535961aSMatthias Ringwald         (void)memcpy(buffer, HFP_INDICATOR, len);
328ad902e3dSMatthias Ringwald         buffer += len;
329ad902e3dSMatthias Ringwald         *buffer++ = ':';
330ad902e3dSMatthias Ringwald         return;
331ad902e3dSMatthias Ringwald     }
332ad902e3dSMatthias Ringwald     index--;
333a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
334ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
335ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
336a0ffb263SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
337ad902e3dSMatthias Ringwald         return;
338ad902e3dSMatthias Ringwald     }
339c1ab6cc1SMatthias Ringwald     if (indicator_index == (num_indicators-1)){
340*6535961aSMatthias Ringwald         (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8);
341ad902e3dSMatthias Ringwald         return;
342ad902e3dSMatthias Ringwald     }
343ad902e3dSMatthias Ringwald     *buffer = ',';
3443deb3ec6SMatthias Ringwald }
3453deb3ec6SMatthias Ringwald 
3463deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
347c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3483deb3ec6SMatthias Ringwald     int i;
3493deb3ec6SMatthias Ringwald     int offset = 0;
350c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_generic_status_indicators_nr-1); i++) {
351a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3523deb3ec6SMatthias Ringwald     }
353a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
354a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3553deb3ec6SMatthias Ringwald     }
3563deb3ec6SMatthias Ringwald     return offset;
3573deb3ec6SMatthias Ringwald }
3583deb3ec6SMatthias Ringwald 
3593deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
360c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_generic_status_indicators_nr * 3)) return 0;
3613deb3ec6SMatthias Ringwald     int i;
3623deb3ec6SMatthias Ringwald     int offset = 0;
363a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
364a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "\r\n%s:%d,%d\r\n", HFP_GENERIC_STATUS_INDICATOR, hfp_generic_status_indicators[i].uuid, hfp_generic_status_indicators[i].state);
3653deb3ec6SMatthias Ringwald     }
3663deb3ec6SMatthias Ringwald     return offset;
3673deb3ec6SMatthias Ringwald }
3683deb3ec6SMatthias Ringwald 
3693deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
370c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3713deb3ec6SMatthias Ringwald     int i;
3723deb3ec6SMatthias Ringwald     int offset = 0;
373c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_ag_indicators_nr-1); i++) {
3743deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3753deb3ec6SMatthias Ringwald     }
3763deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3773deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3783deb3ec6SMatthias Ringwald     }
3793deb3ec6SMatthias Ringwald     return offset;
3803deb3ec6SMatthias Ringwald }
3813deb3ec6SMatthias Ringwald 
3823deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
383c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0;
3843deb3ec6SMatthias Ringwald     int i;
3853deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
386c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) {
3873deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3883deb3ec6SMatthias Ringwald     }
3893deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3903deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3913deb3ec6SMatthias Ringwald     }
3923deb3ec6SMatthias Ringwald     return offset;
3933deb3ec6SMatthias Ringwald }
3943deb3ec6SMatthias Ringwald 
395485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
396ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
397a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
398a0ffb263SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
3993deb3ec6SMatthias Ringwald 
400ad902e3dSMatthias Ringwald     // assumes: can send now == true
401ad902e3dSMatthias Ringwald     // assumes: num segments > 0
402aba39421SMatthias Ringwald     // assumes: individual segments are smaller than MTU
403ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
404ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
405ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
406ad902e3dSMatthias Ringwald     int offset = 0;
407ad902e3dSMatthias Ringwald     int segment = start_segment;
408ad902e3dSMatthias Ringwald     while (segment < num_segments){
409a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
410c1ab6cc1SMatthias Ringwald         if ((offset + segment_len) > mtu) break;
411aba39421SMatthias Ringwald         // append segement
412a0ffb263SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset);
413ad902e3dSMatthias Ringwald         offset += segment_len;
414ad902e3dSMatthias Ringwald         segment++;
415ad902e3dSMatthias Ringwald     }
416ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
417ad902e3dSMatthias Ringwald     return segment;
418ad902e3dSMatthias Ringwald }
4193deb3ec6SMatthias Ringwald 
420ad902e3dSMatthias Ringwald // returns next segment to store
421485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
422a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
423485ac19eSMilanka Ringwald     return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
424ad902e3dSMatthias Ringwald         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
4253deb3ec6SMatthias Ringwald }
4263deb3ec6SMatthias Ringwald 
427485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){
4283deb3ec6SMatthias Ringwald     char buffer[40];
42989425bfcSMilanka Ringwald     const int size = sizeof(buffer);
43089425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR);
43189425bfcSMilanka Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9);
43289425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4333deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4343deb3ec6SMatthias Ringwald }
4353deb3ec6SMatthias Ringwald 
436485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){
437ad902e3dSMatthias Ringwald     char buffer[40];
43889425bfcSMilanka Ringwald     const int size = sizeof(buffer);
43989425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
44089425bfcSMilanka Ringwald     offset += hfp_ag_call_services_join(buffer+offset, size-offset-9);
44189425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4423deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4433deb3ec6SMatthias Ringwald }
4443deb3ec6SMatthias Ringwald 
4453deb3ec6SMatthias Ringwald 
446485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){
447485ac19eSMilanka Ringwald     return hfp_ag_send_ok(cid);
4483deb3ec6SMatthias Ringwald }
4493deb3ec6SMatthias Ringwald 
450485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4513deb3ec6SMatthias Ringwald     char buffer[40];
45289425bfcSMilanka Ringwald     const int size = sizeof(buffer);
45389425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
45489425bfcSMilanka Ringwald     offset += hfp_hf_indicators_join(buffer+offset, size-offset-10);
45589425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n");
4563deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4573deb3ec6SMatthias Ringwald }
4583deb3ec6SMatthias Ringwald 
459485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4603deb3ec6SMatthias Ringwald     char buffer[40];
4611cc1d9e9SMilanka Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7);
4621cc1d9e9SMilanka Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4633deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4643deb3ec6SMatthias Ringwald }
4653deb3ec6SMatthias Ringwald 
466485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4673deb3ec6SMatthias Ringwald     char buffer[20];
468aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
4693deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4703deb3ec6SMatthias Ringwald }
4713deb3ec6SMatthias Ringwald 
472485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4735e71456cSMilanka Ringwald     char buffer[41];
4743deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
47589425bfcSMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4763deb3ec6SMatthias Ringwald     } else {
47789425bfcSMilanka Ringwald         int offset = snprintf(buffer,  sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format);
4785e71456cSMilanka Ringwald         offset += snprintf(buffer+offset, 16, "%s", op.name);
47989425bfcSMilanka Ringwald         snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4803deb3ec6SMatthias Ringwald     }
4813deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4823deb3ec6SMatthias Ringwald }
4833deb3ec6SMatthias Ringwald 
4841cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
4851cc1d9e9SMilanka Ringwald     char buffer[30];
48689425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value);
4871cc1d9e9SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
4881cc1d9e9SMilanka Ringwald }
4893deb3ec6SMatthias Ringwald 
490485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){
4911cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
4923deb3ec6SMatthias Ringwald }
4933deb3ec6SMatthias Ringwald 
494485ac19eSMilanka Ringwald static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
4951cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
496aa4dd815SMatthias Ringwald }
497aa4dd815SMatthias Ringwald 
498485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
4991cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
500aa4dd815SMatthias Ringwald }
501aa4dd815SMatthias Ringwald 
502485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
5031cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
504aa4dd815SMatthias Ringwald }
505aa4dd815SMatthias Ringwald 
506485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){
5071cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_RESPONSE_AND_HOLD, state);
508ce263fc8SMatthias Ringwald }
509ce263fc8SMatthias Ringwald 
510a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
511bc1b1537SMilanka Ringwald     if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
512bc1b1537SMilanka Ringwald 
5136a7f44bdSMilanka Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){
5146a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
515df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
5163deb3ec6SMatthias Ringwald         }
5173deb3ec6SMatthias Ringwald     }
518df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
5193deb3ec6SMatthias Ringwald }
5203deb3ec6SMatthias Ringwald 
521afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){
522afac2a04SMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_AGSF_ESCO_S4));
5237522e673SMatthias Ringwald }
5247522e673SMatthias Ringwald 
525a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
526aa4dd815SMatthias Ringwald     /* events ( == commands):
527aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
528aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
529aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
530aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
531aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
532aa4dd815SMatthias Ringwald     */
533aa4dd815SMatthias Ringwald 
534a0ffb263SMatthias Ringwald      switch (hfp_connection->codecs_state){
535aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
536a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
537aa4dd815SMatthias Ringwald             break;
538aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
539a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
540aa4dd815SMatthias Ringwald             break;
541aa4dd815SMatthias Ringwald         default:
542aa4dd815SMatthias Ringwald             break;
543aa4dd815SMatthias Ringwald     }
544aa4dd815SMatthias Ringwald 
545aa4dd815SMatthias Ringwald     // printf(" -> State machine: CC\n");
546aa4dd815SMatthias Ringwald 
547a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
548aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
549a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
550a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
551485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
552aa4dd815SMatthias Ringwald                 return 1;
553aa4dd815SMatthias Ringwald             }
554aa4dd815SMatthias Ringwald 
555a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
556aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
557aa4dd815SMatthias Ringwald                 case HFP_CODECS_EXCHANGED:
558a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
559aa4dd815SMatthias Ringwald                     break;
560aa4dd815SMatthias Ringwald                 default:
561aa4dd815SMatthias Ringwald                     break;
562aa4dd815SMatthias Ringwald             }
563485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
564aa4dd815SMatthias Ringwald             return 1;
565aa4dd815SMatthias Ringwald 
566aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
567a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
568485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
569aa4dd815SMatthias Ringwald             return 1;
570aa4dd815SMatthias Ringwald 
571aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
572a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
573a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
574485ac19eSMilanka Ringwald             hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
575aa4dd815SMatthias Ringwald             return 1;
576aa4dd815SMatthias Ringwald 
577aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
578a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
579a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
580485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
581aa4dd815SMatthias Ringwald                 return 1;
582aa4dd815SMatthias Ringwald             }
583a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
584a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
585c1ab6cc1SMatthias Ringwald             log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
586485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
5877522e673SMatthias Ringwald             // now, pick link settings
588afac2a04SMilanka Ringwald 
589afac2a04SMilanka Ringwald             hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
590aa4dd815SMatthias Ringwald             return 1;
591aa4dd815SMatthias Ringwald         default:
592aa4dd815SMatthias Ringwald             break;
593aa4dd815SMatthias Ringwald     }
594aa4dd815SMatthias Ringwald     return 0;
595aa4dd815SMatthias Ringwald }
596aa4dd815SMatthias Ringwald 
597a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
598a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
599ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
600aa4dd815SMatthias Ringwald 
601a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
602d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
603a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
604aa4dd815SMatthias Ringwald     }
605ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
606c1ab6cc1SMatthias Ringwald     if ((hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) &&
607505f1c30SMatthias Ringwald         (hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS)){
608a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
609ce263fc8SMatthias Ringwald     }
610aa4dd815SMatthias Ringwald }
6113deb3ec6SMatthias Ringwald 
612a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
613c79b4cabSMatthias Ringwald     // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
614a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
615c04cf7ffSMilanka Ringwald     int sent = 0;
616a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
6173deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
618a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
6193deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
620aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
621d715cf51SMatthias Ringwald                     hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
622a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
623a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
624aa4dd815SMatthias Ringwald                     } else {
625a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
626aa4dd815SMatthias Ringwald                     }
627a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
628aa4dd815SMatthias Ringwald                     return 1;
6293deb3ec6SMatthias Ringwald                 default:
6303deb3ec6SMatthias Ringwald                     break;
6313deb3ec6SMatthias Ringwald             }
6323deb3ec6SMatthias Ringwald             break;
6333deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
634c04cf7ffSMilanka Ringwald             sent = codecs_exchange_state_machine(hfp_connection);
635a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
636a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6373deb3ec6SMatthias Ringwald             }
638c04cf7ffSMilanka Ringwald             return sent;
639aa4dd815SMatthias Ringwald 
640aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
641a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
642473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
643473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
644473ac565SMatthias Ringwald                 // continue below in state switch
645ad902e3dSMatthias Ringwald             }
646ad902e3dSMatthias Ringwald             break;
647aa4dd815SMatthias Ringwald 
648aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
649a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
650a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
651485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
652aa4dd815SMatthias Ringwald             return 1;
653aa4dd815SMatthias Ringwald 
6543deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
655a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
656a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
657a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
658a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
659a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
660aa4dd815SMatthias Ringwald             } else {
661a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
6623deb3ec6SMatthias Ringwald             }
6638a46ec40SMatthias Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
664aa4dd815SMatthias Ringwald             return 1;
6653deb3ec6SMatthias Ringwald 
666aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
667a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
668a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
669a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
670aa4dd815SMatthias Ringwald             }
671485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
672a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
673a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
674ce263fc8SMatthias Ringwald             }
675aa4dd815SMatthias Ringwald             return 1;
676aa4dd815SMatthias Ringwald 
677aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
678a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
679a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
680485ac19eSMilanka Ringwald             hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
681aa4dd815SMatthias Ringwald             return 1;
682aa4dd815SMatthias Ringwald 
683aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
684a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
685a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
686485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
687aa4dd815SMatthias Ringwald             return 1;
688aa4dd815SMatthias Ringwald 
689aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
690a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
691a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
692485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
693aa4dd815SMatthias Ringwald             return 1;
6943deb3ec6SMatthias Ringwald         default:
6953deb3ec6SMatthias Ringwald             break;
6963deb3ec6SMatthias Ringwald     }
697473ac565SMatthias Ringwald 
698473ac565SMatthias Ringwald     switch (hfp_connection->state){
699473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
700485ac19eSMilanka Ringwald             int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
701473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
702473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
703473ac565SMatthias Ringwald             if (next_segment < num_segments){
704473ac565SMatthias Ringwald                 // prepare sending of next segment
705473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
706473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
707473ac565SMatthias Ringwald             } else {
708473ac565SMatthias Ringwald                 // done, go to next state
709473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
710473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
711473ac565SMatthias Ringwald             }
712473ac565SMatthias Ringwald             return 1;
713473ac565SMatthias Ringwald         }
714473ac565SMatthias Ringwald         default:
715473ac565SMatthias Ringwald             break;
716473ac565SMatthias Ringwald     }
717c04cf7ffSMilanka Ringwald     return 0;
7183deb3ec6SMatthias Ringwald }
7193deb3ec6SMatthias Ringwald 
720a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
721c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
722c04cf7ffSMilanka Ringwald     if (sent) return 1;
723aa4dd815SMatthias Ringwald 
724a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
725aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
726a0ffb263SMatthias Ringwald             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
727485ac19eSMilanka Ringwald             hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
728aa4dd815SMatthias Ringwald             return 1;
729aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
730aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
731a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
732485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
733a0ffb263SMatthias Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
734aa4dd815SMatthias Ringwald             } else {
735485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
736aa4dd815SMatthias Ringwald             }
737aa4dd815SMatthias Ringwald             return 1;
738aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
739485ac19eSMilanka Ringwald             hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
740aa4dd815SMatthias Ringwald             return 1;
741aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
742485ac19eSMilanka Ringwald             hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
743aa4dd815SMatthias Ringwald             return 1;
744aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
745a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
746485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
747aa4dd815SMatthias Ringwald             } else {
748485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
7493deb3ec6SMatthias Ringwald             }
750aa4dd815SMatthias Ringwald             return 1;
751aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
752485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
753aa4dd815SMatthias Ringwald             return 1;
7543deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
755a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
756a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
757485ac19eSMilanka Ringwald                 hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
758aa4dd815SMatthias Ringwald                 return 1;
7593deb3ec6SMatthias Ringwald             }
760202c8a4cSMatthias Ringwald             break;
7613deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
762485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
763ce263fc8SMatthias Ringwald             return 1;
7643deb3ec6SMatthias Ringwald         default:
7653deb3ec6SMatthias Ringwald             break;
7663deb3ec6SMatthias Ringwald     }
767aa4dd815SMatthias Ringwald     return 0;
7683deb3ec6SMatthias Ringwald }
7693deb3ec6SMatthias Ringwald 
770a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
771505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
772505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
7733deb3ec6SMatthias Ringwald 
774aa4dd815SMatthias Ringwald 
775505f1c30SMatthias Ringwald     if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){
776a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
777a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
778a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
779aa4dd815SMatthias Ringwald         return 1;
780aa4dd815SMatthias Ringwald     }
781aa4dd815SMatthias Ringwald 
782a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
783aa4dd815SMatthias Ringwald 
784aa4dd815SMatthias Ringwald     // run codecs exchange
785c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
786c04cf7ffSMilanka Ringwald     if (sent) return 1;
787aa4dd815SMatthias Ringwald 
788c04cf7ffSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
789a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
790a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
791a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
792eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
793aa4dd815SMatthias Ringwald         return 1;
794aa4dd815SMatthias Ringwald     }
795aa4dd815SMatthias Ringwald     return 0;
796aa4dd815SMatthias Ringwald }
797aa4dd815SMatthias Ringwald 
798ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
799665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
800665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
801aa4dd815SMatthias Ringwald 
802665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
803a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
804a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
805a0ffb263SMatthias Ringwald             return hfp_connection;
806aa4dd815SMatthias Ringwald         }
807aa4dd815SMatthias Ringwald     }
808aa4dd815SMatthias Ringwald     return NULL;
809aa4dd815SMatthias Ringwald }
810aa4dd815SMatthias Ringwald 
811ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
812d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
813d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
814aa4dd815SMatthias Ringwald 
815d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
816a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
817a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
818aa4dd815SMatthias Ringwald 
819a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
820a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
821aa4dd815SMatthias Ringwald 
822f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
823aa4dd815SMatthias Ringwald }
824aa4dd815SMatthias Ringwald 
825a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
826a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
827a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
828a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
829a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
830aa4dd815SMatthias Ringwald }
831aa4dd815SMatthias Ringwald 
832a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
833a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
834a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
835aa4dd815SMatthias Ringwald }
836aa4dd815SMatthias Ringwald 
837aa4dd815SMatthias Ringwald //
838aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
839aa4dd815SMatthias Ringwald //
840aa4dd815SMatthias Ringwald 
841a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
842aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
843a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
844d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
845aa4dd815SMatthias Ringwald     } else {
846a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
847a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
848a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
849a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
850ca59be51SMatthias Ringwald         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
851aa4dd815SMatthias Ringwald     }
852aa4dd815SMatthias Ringwald }
853aa4dd815SMatthias Ringwald 
854a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
855a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
856a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
857a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
858ca59be51SMatthias Ringwald     hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGINIG);
859aa4dd815SMatthias Ringwald }
860aa4dd815SMatthias Ringwald 
861aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
862aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
863aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
864aa4dd815SMatthias Ringwald 
865665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
866665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
867665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
868a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
86966c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
870a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
871a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
872a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
873d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
874aa4dd815SMatthias Ringwald         }
875a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
876a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
877aa4dd815SMatthias Ringwald         }
878f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
879aa4dd815SMatthias Ringwald     }
880aa4dd815SMatthias Ringwald }
881aa4dd815SMatthias Ringwald 
882aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
883aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
884aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
885aa4dd815SMatthias Ringwald 
886665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
887665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
888665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
889a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
89066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
891a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
892a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
893f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
894aa4dd815SMatthias Ringwald     }
895aa4dd815SMatthias Ringwald }
896aa4dd815SMatthias Ringwald 
897aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
898aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("call");
899aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
900aa4dd815SMatthias Ringwald 
901665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
902665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
903665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
904a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
90566c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
906a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
907a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
908f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
909aa4dd815SMatthias Ringwald     }
910aa4dd815SMatthias Ringwald }
911aa4dd815SMatthias Ringwald 
912aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
913aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callheld");
914aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
915aa4dd815SMatthias Ringwald 
916665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
917665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
918665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
919a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
92066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
921a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
922a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
923f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
924aa4dd815SMatthias Ringwald     }
925aa4dd815SMatthias Ringwald }
926aa4dd815SMatthias Ringwald 
927aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
928aa4dd815SMatthias Ringwald 
929aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
930aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
931aa4dd815SMatthias Ringwald 
932665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
933665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
934665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
935a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
93666c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
937505f1c30SMatthias Ringwald         if ((hfp_connection->call_state != HFP_CALL_RINGING) &&
938505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
939aa4dd815SMatthias Ringwald 
940a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
941a0ffb263SMatthias Ringwald         if (hfp_connection == source){
942aa4dd815SMatthias Ringwald 
943aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
944a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
945aa4dd815SMatthias Ringwald             } else {
946a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
947d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
948aa4dd815SMatthias Ringwald             }
949aa4dd815SMatthias Ringwald 
950a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
951a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
952aa4dd815SMatthias Ringwald 
953aa4dd815SMatthias Ringwald         } else {
954a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
955aa4dd815SMatthias Ringwald         }
956f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
957aa4dd815SMatthias Ringwald     }
958aa4dd815SMatthias Ringwald }
959aa4dd815SMatthias Ringwald 
960aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
961aa4dd815SMatthias Ringwald 
962aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
963aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
964aa4dd815SMatthias Ringwald 
965665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
966665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
967665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
968a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
96966c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
970a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
971aa4dd815SMatthias Ringwald 
972a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
973a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
974aa4dd815SMatthias Ringwald 
975a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
976a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
977aa4dd815SMatthias Ringwald 
978f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
979aa4dd815SMatthias Ringwald         break;  // only single
980aa4dd815SMatthias Ringwald     }
981aa4dd815SMatthias Ringwald }
982aa4dd815SMatthias Ringwald 
983aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
984aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
985665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
986665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
987665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
988665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
98966c5995fSMatthias Ringwald         if (connection->local_role != HFP_ROLE_AG) continue;
990505f1c30SMatthias Ringwald         if ((connection->call_state != HFP_CALL_RINGING) &&
991505f1c30SMatthias Ringwald             (connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
992aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
993aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
994aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
995f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(connection);
996aa4dd815SMatthias Ringwald     }
997aa4dd815SMatthias Ringwald }
998aa4dd815SMatthias Ringwald 
999ca59be51SMatthias Ringwald static void hfp_ag_emit_simple_event(uint8_t event_subtype){
1000ca59be51SMatthias Ringwald     uint8_t event[3];
1001ca59be51SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
1002ca59be51SMatthias Ringwald     event[1] = sizeof(event) - 2;
1003ca59be51SMatthias Ringwald     event[2] = event_subtype;
1004ca59be51SMatthias Ringwald     if (!hfp_ag_callback) return;
1005ca59be51SMatthias Ringwald     (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
1006ca59be51SMatthias Ringwald }
1007ca59be51SMatthias Ringwald 
1008aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1009aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1010aa4dd815SMatthias Ringwald 
1011665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1012665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1013665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1014a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
101566c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1016d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1017a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1018a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1019a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
10206d78145cSMatthias Ringwald         if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
1021a0ffb263SMatthias Ringwald             hfp_connection->release_audio_connection = 1;
10226d78145cSMatthias Ringwald         }
1023f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1024aa4dd815SMatthias Ringwald     }
1025ca59be51SMatthias Ringwald     hfp_ag_emit_simple_event(HFP_SUBEVENT_CALL_TERMINATED);
1026aa4dd815SMatthias Ringwald }
1027aa4dd815SMatthias Ringwald 
10280cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
1029aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1030aa4dd815SMatthias Ringwald     if (!indicator){
1031d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
103245718b6fSMatthias Ringwald         return;
1033aa4dd815SMatthias Ringwald     };
1034d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1035aa4dd815SMatthias Ringwald }
1036aa4dd815SMatthias Ringwald 
10370cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1038d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1039d210d9c4SMatthias Ringwald     if (!indicator){
1040d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
104145718b6fSMatthias Ringwald         return;
1042d210d9c4SMatthias Ringwald     };
1043d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1044d210d9c4SMatthias Ringwald }
1045d210d9c4SMatthias Ringwald 
10460cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1047aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1048aa4dd815SMatthias Ringwald     if (!indicator){
1049aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
105045718b6fSMatthias Ringwald         return;
1051aa4dd815SMatthias Ringwald     };
1052d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1053aa4dd815SMatthias Ringwald }
1054aa4dd815SMatthias Ringwald 
1055aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1056665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1057665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1058665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1059a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
106066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1061505f1c30SMatthias Ringwald         if ((hfp_connection->call_state != HFP_CALL_RINGING) &&
1062505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1063a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1064aa4dd815SMatthias Ringwald     }
1065aa4dd815SMatthias Ringwald }
1066aa4dd815SMatthias Ringwald 
1067aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1068665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1069665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1070665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1071a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
107266c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1073a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1074aa4dd815SMatthias Ringwald     }
1075aa4dd815SMatthias Ringwald     return NULL;
1076aa4dd815SMatthias Ringwald }
1077aa4dd815SMatthias Ringwald 
1078a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1079665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1080665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1081665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1082a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
108366c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1084a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1085ce263fc8SMatthias Ringwald     }
1086ce263fc8SMatthias Ringwald }
1087ce263fc8SMatthias Ringwald 
1088a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1089aa4dd815SMatthias Ringwald     int indicator_index;
1090a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1091aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1092a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1093a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1094a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1095a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1096a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1097a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1098ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
1099aa4dd815SMatthias Ringwald             break;
1100aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1101a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1102a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1103a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1104aa4dd815SMatthias Ringwald             break;
1105aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1106a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1107a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1108aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1109a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1110aa4dd815SMatthias Ringwald             break;
1111aa4dd815SMatthias Ringwald         default:
1112aa4dd815SMatthias Ringwald             break;
1113aa4dd815SMatthias Ringwald     }
1114aa4dd815SMatthias Ringwald     return 0;
1115aa4dd815SMatthias Ringwald }
111639426febSMatthias Ringwald 
111739426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below
111839426febSMatthias Ringwald static void hfp_ag_handle_reject_incoming_call(void){
111939426febSMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
112039426febSMatthias Ringwald     if (!hfp_connection){
112139426febSMatthias Ringwald         log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
112239426febSMatthias Ringwald         return;
112339426febSMatthias Ringwald     }
112439426febSMatthias Ringwald 
112539426febSMatthias Ringwald     hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED);
112639426febSMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
112739426febSMatthias Ringwald     hfp_connection->send_error = 1;
112839426febSMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
112939426febSMatthias Ringwald }
113039426febSMatthias Ringwald 
1131a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1132a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1133aa4dd815SMatthias Ringwald     int indicator_index;
1134d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1135d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1136d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
113774386ee0SMatthias Ringwald 
1138d210d9c4SMatthias Ringwald     //printf("hfp_ag_call_sm event %d \n", event);
1139aa4dd815SMatthias Ringwald     switch (event){
1140aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1141d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1142aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1143d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1144aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1145d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1146d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1147aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
114860ebb071SMilanka Ringwald                             log_info("AG rings");
1149aa4dd815SMatthias Ringwald                             break;
1150aa4dd815SMatthias Ringwald                         default:
11513deb3ec6SMatthias Ringwald                             break;
11523deb3ec6SMatthias Ringwald                     }
11533deb3ec6SMatthias Ringwald                     break;
1154aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1155d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1156aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1157d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1158d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1159aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
116060ebb071SMilanka Ringwald                             log_info("AG call waiting");
1161aa4dd815SMatthias Ringwald                             break;
1162aa4dd815SMatthias Ringwald                         default:
11633deb3ec6SMatthias Ringwald                             break;
11643deb3ec6SMatthias Ringwald                     }
11653deb3ec6SMatthias Ringwald                     break;
1166aa4dd815SMatthias Ringwald             }
1167aa4dd815SMatthias Ringwald             break;
1168aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1169d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1170aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1171d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1172aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1173d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1174d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1175d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1176aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
117760ebb071SMilanka Ringwald                             log_info("AG answers call, accept call by GSM");
1178aa4dd815SMatthias Ringwald                             break;
1179aa4dd815SMatthias Ringwald                         default:
11803deb3ec6SMatthias Ringwald                             break;
11813deb3ec6SMatthias Ringwald                     }
1182aa4dd815SMatthias Ringwald                     break;
1183aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1184d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1185aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
118660ebb071SMilanka Ringwald                             log_info("AG: current call is placed on hold, incoming call gets active");
1187d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1188d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1189d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1190ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1191ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1192aa4dd815SMatthias Ringwald                             break;
1193aa4dd815SMatthias Ringwald                         default:
1194aa4dd815SMatthias Ringwald                             break;
1195aa4dd815SMatthias Ringwald                     }
1196aa4dd815SMatthias Ringwald                     break;
1197aa4dd815SMatthias Ringwald             }
1198aa4dd815SMatthias Ringwald             break;
1199ce263fc8SMatthias Ringwald 
1200ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1201d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1202ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1203d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1204ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
120560ebb071SMilanka Ringwald                             log_info("AG: joining held call with active call");
1206d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG);
1207d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1208ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1209ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1210ce263fc8SMatthias Ringwald                             break;
1211ce263fc8SMatthias Ringwald                         default:
1212ce263fc8SMatthias Ringwald                             break;
1213ce263fc8SMatthias Ringwald                     }
1214ce263fc8SMatthias Ringwald                     break;
1215ce263fc8SMatthias Ringwald                 default:
1216ce263fc8SMatthias Ringwald                     break;
1217ce263fc8SMatthias Ringwald             }
1218ce263fc8SMatthias Ringwald             break;
1219ce263fc8SMatthias Ringwald 
1220aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1221d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1222aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1223d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1224aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1225d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF);
1226d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1227d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1228a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
12296323f788SMatthias Ringwald                             hfp_connection->ok_pending = 1;
123060ebb071SMilanka Ringwald                             log_info("HF answers call, accept call by GSM");
1231ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1232aa4dd815SMatthias Ringwald                             break;
1233aa4dd815SMatthias Ringwald                         default:
1234aa4dd815SMatthias Ringwald                             break;
1235aa4dd815SMatthias Ringwald                     }
12363deb3ec6SMatthias Ringwald                     break;
12373deb3ec6SMatthias Ringwald                 default:
12383deb3ec6SMatthias Ringwald                     break;
12393deb3ec6SMatthias Ringwald             }
12403deb3ec6SMatthias Ringwald             break;
12413deb3ec6SMatthias Ringwald 
1242ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1243d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1244ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1245d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1246ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1247d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG);
1248ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1249ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1250a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1251ce263fc8SMatthias Ringwald                             // as with regualr call
1252d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1253d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1254ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
125560ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by AG");
1256ce263fc8SMatthias Ringwald                             break;
1257ce263fc8SMatthias Ringwald                         default:
1258ce263fc8SMatthias Ringwald                             break;
1259ce263fc8SMatthias Ringwald                     }
1260ce263fc8SMatthias Ringwald                     break;
1261ce263fc8SMatthias Ringwald                 default:
1262ce263fc8SMatthias Ringwald                     break;
1263ce263fc8SMatthias Ringwald             }
1264ce263fc8SMatthias Ringwald             break;
1265ce263fc8SMatthias Ringwald 
1266ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1267d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1268ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1269d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1270ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1271d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF);
1272ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1273ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1274a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1275ce263fc8SMatthias Ringwald                             // as with regualr call
1276d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1277d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1278a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
127960ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by HF");
1280ce263fc8SMatthias Ringwald                             break;
1281ce263fc8SMatthias Ringwald                         default:
1282ce263fc8SMatthias Ringwald                             break;
1283ce263fc8SMatthias Ringwald                     }
1284ce263fc8SMatthias Ringwald                     break;
1285ce263fc8SMatthias Ringwald                 default:
1286ce263fc8SMatthias Ringwald                     break;
1287ce263fc8SMatthias Ringwald             }
1288ce263fc8SMatthias Ringwald             break;
1289ce263fc8SMatthias Ringwald 
1290ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1291ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1292ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1293ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1294d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG);
1295a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1296ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1297a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
129860ebb071SMilanka Ringwald             log_info("Held Call accepted and active");
1299ce263fc8SMatthias Ringwald             break;
1300ce263fc8SMatthias Ringwald 
1301ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1302ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1303ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1304ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1305d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG);
1306a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1307ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1308a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1309ce263fc8SMatthias Ringwald             // from terminate by ag
1310d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1311ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1312ce263fc8SMatthias Ringwald             break;
1313ce263fc8SMatthias Ringwald 
1314aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1315d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1316aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1317d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1318aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1319d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1320d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1321aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1322aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
132360ebb071SMilanka Ringwald                             log_info("HF Rejected Incoming call, AG terminate call");
1324aa4dd815SMatthias Ringwald                             break;
1325aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1326aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1327d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1328d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1329aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
133060ebb071SMilanka Ringwald                             log_info("AG terminate outgoing call process");
1331202c8a4cSMatthias Ringwald                             break;
1332aa4dd815SMatthias Ringwald                         default:
1333aa4dd815SMatthias Ringwald                             break;
1334aa4dd815SMatthias Ringwald                     }
1335aa4dd815SMatthias Ringwald                     break;
1336aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1337d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1338d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1339aa4dd815SMatthias Ringwald                     hfp_ag_transfer_call_state();
1340a0ffb263SMatthias Ringwald                     hfp_connection->call_state = HFP_CALL_IDLE;
134160ebb071SMilanka Ringwald                     log_info("AG terminate call");
1342aa4dd815SMatthias Ringwald                     break;
1343aa4dd815SMatthias Ringwald             }
13440c1b8529SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1345aa4dd815SMatthias Ringwald             break;
13463deb3ec6SMatthias Ringwald 
1347aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1348d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1349aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1350d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1351aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1352d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1353d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1354aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
135560ebb071SMilanka Ringwald                             log_info("AG Rejected Incoming call, AG terminate call");
1356aa4dd815SMatthias Ringwald                             break;
1357aa4dd815SMatthias Ringwald                         default:
1358aa4dd815SMatthias Ringwald                             break;
13593deb3ec6SMatthias Ringwald                     }
1360202c8a4cSMatthias Ringwald                     break;
1361aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1362d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1363d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1364d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1365aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
136660ebb071SMilanka Ringwald                     log_info("AG terminate call");
1367aa4dd815SMatthias Ringwald                     break;
1368aa4dd815SMatthias Ringwald                 default:
13693deb3ec6SMatthias Ringwald                     break;
13703deb3ec6SMatthias Ringwald             }
13713deb3ec6SMatthias Ringwald             break;
1372aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1373d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1374aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1375d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1376aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1377aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
137860ebb071SMilanka Ringwald                             log_info("Incoming call interrupted");
1379aa4dd815SMatthias Ringwald                             break;
1380aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1381aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
138260ebb071SMilanka Ringwald                             log_info("Outgoing call interrupted\n");
138360ebb071SMilanka Ringwald                             log_info("AG notify call dropped\n");
1384aa4dd815SMatthias Ringwald                             break;
1385aa4dd815SMatthias Ringwald                         default:
13863deb3ec6SMatthias Ringwald                             break;
13873deb3ec6SMatthias Ringwald                     }
1388d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1389d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1390aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
139110c236bfSMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1392aa4dd815SMatthias Ringwald                     break;
1393aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1394ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1395d210d9c4SMatthias Ringwald                         hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1396ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1397a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1398ce263fc8SMatthias Ringwald                     }
1399d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1400d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1401d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1402aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
140360ebb071SMilanka Ringwald                     log_info("AG notify call dropped");
1404aa4dd815SMatthias Ringwald                     break;
1405aa4dd815SMatthias Ringwald                 default:
1406aa4dd815SMatthias Ringwald                     break;
1407aa4dd815SMatthias Ringwald             }
1408aa4dd815SMatthias Ringwald             break;
1409aa4dd815SMatthias Ringwald 
1410aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1411d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1412d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1413a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1414f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1415d210d9c4SMatthias Ringwald                 break;
1416d210d9c4SMatthias Ringwald             }
1417a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]);
14189cae807eSMatthias Ringwald 
1419a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
142074386ee0SMatthias Ringwald 
1421ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1422aa4dd815SMatthias Ringwald             break;
1423aa4dd815SMatthias Ringwald 
14249cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1425d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1426d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1427a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1428f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1429d210d9c4SMatthias Ringwald                 break;
1430d210d9c4SMatthias Ringwald             }
1431d210d9c4SMatthias Ringwald 
143274386ee0SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED);
1433a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
143474386ee0SMatthias Ringwald 
143560ebb071SMilanka Ringwald             log_info("Redial last number");
14369cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1437aa4dd815SMatthias Ringwald 
14389cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
143960ebb071SMilanka Ringwald                 log_info("Last number exists: accept call");
1440ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
14419cae807eSMatthias Ringwald             } else {
144260ebb071SMilanka Ringwald                 log_info("log_infoLast number missing: reject call");
144339426febSMatthias Ringwald                 hfp_ag_handle_reject_incoming_call();
14449cae807eSMatthias Ringwald             }
14459cae807eSMatthias Ringwald             break;
14469cae807eSMatthias Ringwald         }
1447aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
144839426febSMatthias Ringwald             hfp_ag_handle_reject_incoming_call();
1449aa4dd815SMatthias Ringwald             break;
1450aa4dd815SMatthias Ringwald 
1451d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1452a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1453a0ffb263SMatthias Ringwald             if (!hfp_connection){
1454a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1455aa4dd815SMatthias Ringwald                 break;
1456aa4dd815SMatthias Ringwald             }
1457aa4dd815SMatthias Ringwald 
1458a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1459a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1460aa4dd815SMatthias Ringwald 
1461aa4dd815SMatthias Ringwald             // trigger callsetup to be
1462d0c20769SMatthias Ringwald             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1463d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED);
1464d210d9c4SMatthias Ringwald 
1465d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1466aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1467a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1468aa4dd815SMatthias Ringwald 
1469aa4dd815SMatthias Ringwald             // put current call on hold if active
1470d210d9c4SMatthias Ringwald             if (put_call_on_hold){
147160ebb071SMilanka Ringwald                 log_info("AG putting current call on hold for new outgoing calllog_info");
1472d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1473aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1474485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1475aa4dd815SMatthias Ringwald             }
1476aa4dd815SMatthias Ringwald 
1477aa4dd815SMatthias Ringwald             // start audio if needed
1478d97d752dSMilanka Ringwald             hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1479aa4dd815SMatthias Ringwald             break;
1480d210d9c4SMatthias Ringwald         }
1481aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1482a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1483a0ffb263SMatthias Ringwald             if (!hfp_connection){
1484a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1485aa4dd815SMatthias Ringwald                 break;
1486aa4dd815SMatthias Ringwald             }
1487d210d9c4SMatthias Ringwald 
1488d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING);
1489a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1490d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1491aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1492aa4dd815SMatthias Ringwald             break;
1493aa4dd815SMatthias Ringwald 
1494d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1495aa4dd815SMatthias Ringwald             // get outgoing call
1496a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1497a0ffb263SMatthias Ringwald             if (!hfp_connection){
1498a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1499aa4dd815SMatthias Ringwald             }
1500a0ffb263SMatthias Ringwald             if (!hfp_connection){
1501a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1502aa4dd815SMatthias Ringwald                 break;
1503aa4dd815SMatthias Ringwald             }
1504d210d9c4SMatthias Ringwald 
1505d0c20769SMatthias 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;
1506d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED);
1507a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1508d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1509d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1510aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1511aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1512d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1513d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1514aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
15153deb3ec6SMatthias Ringwald             }
15163deb3ec6SMatthias Ringwald             break;
1517d210d9c4SMatthias Ringwald         }
1518d210d9c4SMatthias Ringwald 
1519d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1520d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY);
1521d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1522a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1523a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
152460ebb071SMilanka Ringwald             log_info("AG: Call Waiting, User Busy");
1525d210d9c4SMatthias Ringwald             break;
1526d210d9c4SMatthias Ringwald 
1527d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1528d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1529d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1530d210d9c4SMatthias Ringwald 
1531d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1532d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1533a0ffb263SMatthias Ringwald                 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1534d0c20769SMatthias Ringwald 
1535d0c20769SMatthias Ringwald             }
1536d0c20769SMatthias Ringwald 
1537d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
153860ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Accept new call");
1539d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1540a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1541d210d9c4SMatthias Ringwald             } else {
154260ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Resume held call");
1543d210d9c4SMatthias Ringwald             }
1544d210d9c4SMatthias Ringwald             if (call_held){
1545d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1546a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1547d210d9c4SMatthias Ringwald             }
1548d0c20769SMatthias Ringwald 
1549a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1550d210d9c4SMatthias Ringwald             break;
1551d210d9c4SMatthias Ringwald         }
1552d0c20769SMatthias Ringwald 
1553d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1554d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1555d210d9c4SMatthias Ringwald             // only update if callsetup changed
1556d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1557a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1558d0c20769SMatthias Ringwald 
1559d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
156060ebb071SMilanka Ringwald                 log_info("AG: Call on Hold, Accept new call");
1561d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1562a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1563d210d9c4SMatthias Ringwald             } else {
156460ebb071SMilanka Ringwald                 log_info("AG: Swap calls");
1565d210d9c4SMatthias Ringwald             }
1566d0c20769SMatthias Ringwald 
1567d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1568d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1569a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1570a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1571d210d9c4SMatthias Ringwald             break;
1572d210d9c4SMatthias Ringwald         }
1573d0c20769SMatthias Ringwald 
1574d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1575d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1576d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
157760ebb071SMilanka Ringwald                 log_info("AG: Join 3-way-call");
1578d210d9c4SMatthias Ringwald                 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL);
1579d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1580a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1581ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1582d210d9c4SMatthias Ringwald             }
1583a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1584d210d9c4SMatthias Ringwald             break;
1585d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1586d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1587d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS);
158860ebb071SMilanka Ringwald             log_info("AG: Transfer call -> Connect two calls and disconnect");
1589d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1590d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1591a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1592a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1593a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1594d210d9c4SMatthias Ringwald             break;
1595ce263fc8SMatthias Ringwald 
15963deb3ec6SMatthias Ringwald         default:
15973deb3ec6SMatthias Ringwald             break;
15983deb3ec6SMatthias Ringwald     }
1599d210d9c4SMatthias Ringwald 
1600d0c20769SMatthias Ringwald 
16013deb3ec6SMatthias Ringwald }
16023deb3ec6SMatthias Ringwald 
16039cae807eSMatthias Ringwald 
1604a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
16059cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
16069cae807eSMatthias Ringwald     if (!active_call) return;
16079cae807eSMatthias Ringwald 
16089cae807eSMatthias Ringwald     int idx = active_call->index;
16099cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
16109cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
16119cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
16129cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
16139cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
16149cae807eSMatthias Ringwald     char * number = active_call->clip_number;
16159cae807eSMatthias Ringwald 
16169cae807eSMatthias Ringwald     char buffer[100];
16179cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
16189cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
16199cae807eSMatthias Ringwald     if (number){
16201cc1d9e9SMilanka Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
16219cae807eSMatthias Ringwald     }
16229cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
162360ebb071SMilanka 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,
16249cae807eSMatthias Ringwald        mode, mpty, type, number);
1625a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
16269cae807eSMatthias Ringwald }
16279cae807eSMatthias Ringwald 
1628473ac565SMatthias Ringwald 
1629febc14f5SMatthias Ringwald // sends pending command, returns if command was sent
1630febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){
163122387625SMatthias Ringwald 
1632a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1633a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1634a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1635a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1636a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
1637febc14f5SMatthias Ringwald             return 1;
16389cae807eSMatthias Ringwald         } else {
1639febc14f5SMatthias 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
1640a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1641a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1642a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16439cae807eSMatthias Ringwald         }
1644ce263fc8SMatthias Ringwald     }
1645ce263fc8SMatthias Ringwald 
1646a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1647a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1648a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1649febc14f5SMatthias Ringwald         return 1;
1650aa4dd815SMatthias Ringwald     }
1651aa4dd815SMatthias Ringwald 
1652a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1653a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1654a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1655a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1656485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1657febc14f5SMatthias Ringwald         return 1;
1658a0ffb263SMatthias Ringwald     }
1659a0ffb263SMatthias Ringwald 
1660a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1661a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1662a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1663485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1664febc14f5SMatthias Ringwald         return 1;
1665aa4dd815SMatthias Ringwald     }
1666aa4dd815SMatthias Ringwald 
1667ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1668a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1669a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1670a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1671485ac19eSMilanka Ringwald         hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1672febc14f5SMatthias Ringwald         return 1;
1673ce263fc8SMatthias Ringwald     }
1674ce263fc8SMatthias Ringwald 
1675a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1676a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1677a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1678485ac19eSMilanka Ringwald         hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1679febc14f5SMatthias Ringwald         return 1;
1680ce263fc8SMatthias Ringwald     }
1681ce263fc8SMatthias Ringwald 
1682aa4dd815SMatthias Ringwald     // update AG indicators
1683a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1684aa4dd815SMatthias Ringwald         int i;
1685a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1686a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1687a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1688a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1689ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1690ce263fc8SMatthias Ringwald                     break;
1691ce263fc8SMatthias Ringwald                 }
1692485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1693febc14f5SMatthias Ringwald                 return 1;
1694aa4dd815SMatthias Ringwald             }
1695aa4dd815SMatthias Ringwald         }
1696aa4dd815SMatthias Ringwald     }
1697aa4dd815SMatthias Ringwald 
1698a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1699a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1700a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1701485ac19eSMilanka Ringwald         hfp_ag_send_ring(hfp_connection->rfcomm_cid);
1702febc14f5SMatthias Ringwald         return 1;
1703aa4dd815SMatthias Ringwald     }
1704aa4dd815SMatthias Ringwald 
1705a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1706a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1707a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1708a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1709febc14f5SMatthias Ringwald         return 1;
1710aa4dd815SMatthias Ringwald     }
1711aa4dd815SMatthias Ringwald 
1712a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1713a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1714a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1715a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1716a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1717febc14f5SMatthias Ringwald         return 1;
1718aa4dd815SMatthias Ringwald     }
1719aa4dd815SMatthias Ringwald 
1720a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1721a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1722a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1723a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1724ce263fc8SMatthias Ringwald         } else {
1725a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1726a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1727485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1728ce263fc8SMatthias Ringwald         }
1729a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1730febc14f5SMatthias Ringwald         return 1;
1731ce263fc8SMatthias Ringwald     }
1732ce263fc8SMatthias Ringwald 
1733a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1734a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1735a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1736485ac19eSMilanka Ringwald         hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1737febc14f5SMatthias Ringwald         return 1;
1738aa4dd815SMatthias Ringwald     }
1739aa4dd815SMatthias Ringwald 
1740a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1741a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1742a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1743485ac19eSMilanka Ringwald         hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
1744febc14f5SMatthias Ringwald         return 1;
17453deb3ec6SMatthias Ringwald     }
17463deb3ec6SMatthias Ringwald 
1747a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1748a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1749485ac19eSMilanka Ringwald         hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1750febc14f5SMatthias Ringwald         return 1;
1751febc14f5SMatthias Ringwald     }
1752febc14f5SMatthias Ringwald 
1753febc14f5SMatthias Ringwald     return 0;
1754febc14f5SMatthias Ringwald }
1755febc14f5SMatthias Ringwald 
1756febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){
1757febc14f5SMatthias Ringwald 
1758febc14f5SMatthias Ringwald     if (!hfp_connection) return;
1759febc14f5SMatthias Ringwald 
1760febc14f5SMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
1761febc14f5SMatthias Ringwald 
1762febc14f5SMatthias Ringwald     if (hfp_connection->local_role != HFP_ROLE_AG) {
1763febc14f5SMatthias Ringwald         log_info("HFP AG%p, wrong role %u", hfp_connection, hfp_connection->local_role);
1764ce263fc8SMatthias Ringwald         return;
1765ce263fc8SMatthias Ringwald     }
1766ce263fc8SMatthias Ringwald 
1767febc14f5SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1768febc14f5SMatthias Ringwald         log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1769febc14f5SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1770febc14f5SMatthias Ringwald         return;
1771febc14f5SMatthias Ringwald     }
1772febc14f5SMatthias Ringwald 
1773febc14f5SMatthias Ringwald     int cmd_sent = hfp_ag_send_commands(hfp_connection);
1774febc14f5SMatthias Ringwald 
17752ee45eb2SMilanka Ringwald     // trigger codec exchange if requested by app
17762ee45eb2SMilanka Ringwald     if (hfp_connection->trigger_codec_exchange){
17772ee45eb2SMilanka Ringwald         log_info("trigger codec, command %u, codec state %u", hfp_connection->command, hfp_connection->codecs_state);
17782ee45eb2SMilanka Ringwald     }
1779febc14f5SMatthias Ringwald 
1780c1ab6cc1SMatthias Ringwald     if (hfp_connection->trigger_codec_exchange && (hfp_connection->command == HFP_CMD_NONE)){
17812ee45eb2SMilanka Ringwald         switch (hfp_connection->codecs_state){
17822ee45eb2SMilanka Ringwald             case HFP_CODECS_IDLE:
17832ee45eb2SMilanka Ringwald             case HFP_CODECS_RECEIVED_LIST:
17842ee45eb2SMilanka Ringwald             case HFP_CODECS_AG_RESEND_COMMON_CODEC:
17852ee45eb2SMilanka Ringwald             case HFP_CODECS_ERROR:
17862ee45eb2SMilanka Ringwald                 hfp_connection->trigger_codec_exchange = 0;
17872ee45eb2SMilanka Ringwald                 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
17882ee45eb2SMilanka Ringwald                 break;
17892ee45eb2SMilanka Ringwald             default:
17902ee45eb2SMilanka Ringwald                 break;
17912ee45eb2SMilanka Ringwald         }
17922ee45eb2SMilanka Ringwald     }
17932ee45eb2SMilanka Ringwald 
1794febc14f5SMatthias Ringwald     if (!cmd_sent){
1795febc14f5SMatthias Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1796febc14f5SMatthias Ringwald     }
1797febc14f5SMatthias Ringwald 
1798c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1799c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
18003deb3ec6SMatthias Ringwald     }
18013deb3ec6SMatthias Ringwald 
1802c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1803c04cf7ffSMilanka Ringwald         cmd_sent = call_setup_state_machine(hfp_connection);
1804aa4dd815SMatthias Ringwald     }
1805aa4dd815SMatthias Ringwald 
1806c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1807c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
1808aa4dd815SMatthias Ringwald     }
18093deb3ec6SMatthias Ringwald 
1810c1ab6cc1SMatthias Ringwald     if ((hfp_connection->command == HFP_CMD_NONE) && !cmd_sent){
1811a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1812a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
18133deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1814a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1815a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
18163deb3ec6SMatthias Ringwald                 break;
18173deb3ec6SMatthias Ringwald             default:
18183deb3ec6SMatthias Ringwald                 break;
18193deb3ec6SMatthias Ringwald         }
18203deb3ec6SMatthias Ringwald     }
1821c04cf7ffSMilanka Ringwald 
1822c04cf7ffSMilanka Ringwald     if (cmd_sent){
1823a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1824473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1825473ac565SMatthias Ringwald     }
18263deb3ec6SMatthias Ringwald }
1827d68dcce1SMatthias Ringwald 
1828ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1829ce263fc8SMatthias Ringwald     int i;
1830a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1831a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1832ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1833ce263fc8SMatthias Ringwald             return indicator;
1834ce263fc8SMatthias Ringwald         }
1835ce263fc8SMatthias Ringwald     }
1836ce263fc8SMatthias Ringwald     return NULL;
1837ce263fc8SMatthias Ringwald }
18383deb3ec6SMatthias Ringwald 
1839e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1840c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
1841e0d09929SMatthias Ringwald }
1842e0d09929SMatthias Ringwald 
1843e9c22d4eSMatthias Ringwald static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
18448a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
18458a46ec40SMatthias Ringwald 
18468a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
18478a46ec40SMatthias Ringwald     if (size < 1) return;
18489ec2630cSMatthias Ringwald 
1849a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1850a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
18511e35c04dSMatthias Ringwald 
1852186dd3d2SMatthias Ringwald     hfp_log_rfcomm_message("HFP_AG_RX", packet, size);
18531e35c04dSMatthias Ringwald 
18548a46ec40SMatthias Ringwald     // process messages byte-wise
18553deb3ec6SMatthias Ringwald     int pos;
18563deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1857a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1858186dd3d2SMatthias Ringwald 
1859e0d09929SMatthias Ringwald         // parse until end of line
1860e0d09929SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1861e0d09929SMatthias Ringwald 
1862ce263fc8SMatthias Ringwald         int value;
1863186dd3d2SMatthias Ringwald         hfp_generic_status_indicator_t * indicator;
1864a0ffb263SMatthias Ringwald         switch(hfp_connection->command){
1865ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1866ce263fc8SMatthias Ringwald                 if (hfp_ag_response_and_hold_active){
1867a0ffb263SMatthias Ringwald                     hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1868ce263fc8SMatthias Ringwald                 }
1869a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1870ce263fc8SMatthias Ringwald                 break;
1871ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
18722308e108SMilanka Ringwald                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
187360ebb071SMilanka Ringwald                 log_info("HF Response and Hold: %u", value);
1874ce263fc8SMatthias Ringwald                 switch(value){
1875ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1876a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1877ce263fc8SMatthias Ringwald                         break;
1878ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1879a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1880ce263fc8SMatthias Ringwald                         break;
1881ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1882a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1883ce263fc8SMatthias Ringwald                         break;
1884ce263fc8SMatthias Ringwald                     default:
1885ce263fc8SMatthias Ringwald                         break;
1886ce263fc8SMatthias Ringwald                 }
1887a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1888ce263fc8SMatthias Ringwald                 break;
1889ce263fc8SMatthias Ringwald             case HFP_CMD_HF_INDICATOR_STATUS:
1890a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1891ce263fc8SMatthias Ringwald                 // find indicator by assigned number
1892a0ffb263SMatthias Ringwald                 indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1893ce263fc8SMatthias Ringwald                 if (!indicator){
1894a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
1895ce263fc8SMatthias Ringwald                     break;
1896ce263fc8SMatthias Ringwald                 }
18972308e108SMilanka Ringwald                 value = btstack_atoi((char *)&hfp_connection->line_buffer[0]);
1898ce263fc8SMatthias Ringwald                 switch (indicator->uuid){
1899ce263fc8SMatthias Ringwald                     case 1: // enhanced security
1900ce263fc8SMatthias Ringwald                         if (value > 1) {
1901a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1902ce263fc8SMatthias Ringwald                             return;
1903ce263fc8SMatthias Ringwald                         }
190460ebb071SMilanka Ringwald                         log_info("HF Indicator 'enhanced security' set to %u", value);
1905ce263fc8SMatthias Ringwald                         break;
1906ce263fc8SMatthias Ringwald                     case 2: // battery level
1907ce263fc8SMatthias Ringwald                         if (value > 100){
1908a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1909ce263fc8SMatthias Ringwald                             return;
1910ce263fc8SMatthias Ringwald                         }
191160ebb071SMilanka Ringwald                         log_info("HF Indicator 'battery' set to %u", value);
1912ce263fc8SMatthias Ringwald                         break;
1913ce263fc8SMatthias Ringwald                     default:
191460ebb071SMilanka Ringwald                         log_info("HF Indicator unknown set to %u", value);
1915ce263fc8SMatthias Ringwald                         break;
1916ce263fc8SMatthias Ringwald                 }
1917a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1918ce263fc8SMatthias Ringwald                 break;
1919ce263fc8SMatthias Ringwald             case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1920ce263fc8SMatthias Ringwald                 // expected by SLC state machine
1921a0ffb263SMatthias Ringwald                 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1922a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
1923a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_status_indicators = 1;
1924ce263fc8SMatthias Ringwald                 break;
1925ce263fc8SMatthias Ringwald             case HFP_CMD_LIST_CURRENT_CALLS:
1926a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1927a0ffb263SMatthias Ringwald                 hfp_connection->next_call_index = 0;
1928a0ffb263SMatthias Ringwald                 hfp_connection->send_status_of_current_calls = 1;
1929ce263fc8SMatthias Ringwald                 break;
1930ce263fc8SMatthias Ringwald             case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1931ce263fc8SMatthias Ringwald                 if (subscriber_numbers_count == 0){
1932485ac19eSMilanka Ringwald                     hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1933ce263fc8SMatthias Ringwald                     break;
1934ce263fc8SMatthias Ringwald                 }
1935a0ffb263SMatthias Ringwald                 hfp_connection->next_subscriber_number_to_send = 0;
1936a0ffb263SMatthias Ringwald                 hfp_connection->send_subscriber_number = 1;
1937ce263fc8SMatthias Ringwald                 break;
1938c1797c7dSMatthias Ringwald             case HFP_CMD_TRANSMIT_DTMF_CODES:
1939a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1940ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]);
1941c1797c7dSMatthias Ringwald                 break;
1942aa4dd815SMatthias Ringwald             case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1943a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1944ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1945aa4dd815SMatthias Ringwald                 break;
1946aa4dd815SMatthias Ringwald             case HFP_CMD_TURN_OFF_EC_AND_NR:
1947a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1948aa4dd815SMatthias Ringwald                 if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1949a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
1950a0ffb263SMatthias Ringwald                     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
195160ebb071SMilanka Ringwald                     log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction);
1952aa4dd815SMatthias Ringwald                 } else {
1953a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
1954aa4dd815SMatthias Ringwald                 }
1955aa4dd815SMatthias Ringwald                 break;
1956aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_ANSWERED:
1957a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
195860ebb071SMilanka Ringwald                 log_info("HFP: ATA");
1959a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1960aa4dd815SMatthias Ringwald                 break;
1961aa4dd815SMatthias Ringwald             case HFP_CMD_HANG_UP_CALL:
1962a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1963a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1964a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1965aa4dd815SMatthias Ringwald                 break;
1966aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_HOLD: {
1967aa4dd815SMatthias Ringwald                 // TODO: fully implement this
1968a0ffb263SMatthias Ringwald                 log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]);
1969a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
1970a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1971a0ffb263SMatthias Ringwald                 hfp_connection->call_index = 0;
1972d0c20769SMatthias Ringwald 
1973a0ffb263SMatthias Ringwald                 if (hfp_connection->line_buffer[1] != '\0'){
19742308e108SMilanka Ringwald                     hfp_connection->call_index = btstack_atoi((char *)&hfp_connection->line_buffer[1]);
1975d0c20769SMatthias Ringwald                 }
1976d210d9c4SMatthias Ringwald 
1977a0ffb263SMatthias Ringwald                 switch (hfp_connection->line_buffer[0]){
1978aa4dd815SMatthias Ringwald                     case '0':
1979d0c20769SMatthias Ringwald                         // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1980a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1981aa4dd815SMatthias Ringwald                         break;
1982aa4dd815SMatthias Ringwald                     case '1':
1983d0c20769SMatthias Ringwald                         // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1984d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
1985d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
1986a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1987aa4dd815SMatthias Ringwald                         break;
1988aa4dd815SMatthias Ringwald                     case '2':
1989d0c20769SMatthias Ringwald                         // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1990d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
1991d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
1992a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1993aa4dd815SMatthias Ringwald                         break;
1994aa4dd815SMatthias Ringwald                     case '3':
1995d0c20769SMatthias Ringwald                         // Adds a held call to the conversation.
1996a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1997aa4dd815SMatthias Ringwald                         break;
1998aa4dd815SMatthias Ringwald                     case '4':
1999d0c20769SMatthias Ringwald                         // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
2000a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
2001aa4dd815SMatthias Ringwald                         break;
2002aa4dd815SMatthias Ringwald                     default:
2003aa4dd815SMatthias Ringwald                         break;
2004aa4dd815SMatthias Ringwald                 }
2005aa4dd815SMatthias Ringwald                 break;
2006aa4dd815SMatthias Ringwald             }
2007aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_PHONE_NUMBER:
2008a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2009a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
2010aa4dd815SMatthias Ringwald                 break;
2011aa4dd815SMatthias Ringwald             case HFP_CMD_REDIAL_LAST_NUMBER:
2012a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2013a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
2014aa4dd815SMatthias Ringwald                 break;
2015aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CLIP:
2016a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2017a0ffb263SMatthias Ringwald                 hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0';
2018a0ffb263SMatthias Ringwald                 log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
2019a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2020aa4dd815SMatthias Ringwald                 break;
2021aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
2022a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2023a0ffb263SMatthias Ringwald                 hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0';
2024a0ffb263SMatthias Ringwald                 log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
2025a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2026aa4dd815SMatthias Ringwald                 break;
2027aa4dd815SMatthias Ringwald             case HFP_CMD_SET_SPEAKER_GAIN:
2028a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2029a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
203060ebb071SMilanka Ringwald                 log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
2031aa4dd815SMatthias Ringwald                 break;
2032aa4dd815SMatthias Ringwald             case HFP_CMD_SET_MICROPHONE_GAIN:
2033a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2034a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
203560ebb071SMilanka Ringwald                 log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
2036aa4dd815SMatthias Ringwald                 break;
2037aa4dd815SMatthias Ringwald             default:
2038aa4dd815SMatthias Ringwald                 break;
20393deb3ec6SMatthias Ringwald         }
20403deb3ec6SMatthias Ringwald     }
20410cef86faSMatthias Ringwald }
20423deb3ec6SMatthias Ringwald 
2043aa4dd815SMatthias Ringwald static void hfp_run(void){
2044d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
2045d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2046d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2047d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
204822387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2049f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
20503deb3ec6SMatthias Ringwald     }
20513deb3ec6SMatthias Ringwald }
20523deb3ec6SMatthias Ringwald 
2053e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
20543deb3ec6SMatthias Ringwald     switch (packet_type){
20553deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
2056e9c22d4eSMatthias Ringwald             hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size);
20573deb3ec6SMatthias Ringwald             break;
20583deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2059e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2060e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2061f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2062e30a6a47SMatthias Ringwald                 return;
2063e30a6a47SMatthias Ringwald             }
206427950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2065aa4dd815SMatthias Ringwald             break;
20663deb3ec6SMatthias Ringwald         default:
20673deb3ec6SMatthias Ringwald             break;
20683deb3ec6SMatthias Ringwald     }
20693deb3ec6SMatthias Ringwald 
20703deb3ec6SMatthias Ringwald     hfp_run();
20713deb3ec6SMatthias Ringwald }
20723deb3ec6SMatthias Ringwald 
2073405014fbSMatthias Ringwald static void hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2074405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
207505bed9f6SMatthias Ringwald 
207605bed9f6SMatthias Ringwald     // allow for sco established -> ring transition
207705bed9f6SMatthias Ringwald     if (packet_type != HCI_EVENT_PACKET) return;
207805bed9f6SMatthias Ringwald     if (hci_event_packet_get_type(packet) != HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE) return;
207905bed9f6SMatthias Ringwald     hfp_run();
2080405014fbSMatthias Ringwald }
2081405014fbSMatthias Ringwald 
2082a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
20833deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
20843deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
20853deb3ec6SMatthias Ringwald         return;
20863deb3ec6SMatthias Ringwald     }
20873deb3ec6SMatthias Ringwald     int i;
2088a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
20893deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
20903deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
20913deb3ec6SMatthias Ringwald     }
2092a0ffb263SMatthias Ringwald }
20933deb3ec6SMatthias Ringwald 
2094a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2095a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2096a0ffb263SMatthias Ringwald }
20973deb3ec6SMatthias Ringwald 
2098a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2099a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
2100*6535961aSMatthias Ringwald     (void)memcpy(hfp_ag_indicators, ag_indicators,
2101*6535961aSMatthias Ringwald                  ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2102a0ffb263SMatthias Ringwald }
21033deb3ec6SMatthias Ringwald 
2104a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
2105a0ffb263SMatthias Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return;
2106a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
2107*6535961aSMatthias Ringwald     (void)memcpy(hfp_generic_status_indicators, hf_indicators,
2108*6535961aSMatthias Ringwald                  hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2109a0ffb263SMatthias Ringwald }
2110a0ffb263SMatthias Ringwald 
2111a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
21123deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
2113*6535961aSMatthias Ringwald     (void)memcpy(hfp_ag_call_hold_services, call_hold_services,
2114*6535961aSMatthias Ringwald                  call_hold_services_nr * sizeof(char *));
2115a0ffb263SMatthias Ringwald }
2116a0ffb263SMatthias Ringwald 
2117a0ffb263SMatthias Ringwald 
2118a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
211927950165SMatthias Ringwald 
2120520c92d5SMatthias Ringwald     hfp_init();
2121d63c37a1SMatthias Ringwald 
2122405014fbSMatthias Ringwald     hci_event_callback_registration.callback = &hci_event_packet_handler;
212327950165SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
212427950165SMatthias Ringwald 
2125e9c22d4eSMatthias Ringwald     rfcomm_register_service(&rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
212627950165SMatthias Ringwald 
212727950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
2128e9c22d4eSMatthias Ringwald     hfp_set_ag_rfcomm_packet_handler(&rfcomm_packet_handler);
2129aa4dd815SMatthias Ringwald 
2130d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2131d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2132d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2133d210d9c4SMatthias Ringwald 
2134d210d9c4SMatthias Ringwald     hfp_gsm_init();
21353deb3ec6SMatthias Ringwald }
21363deb3ec6SMatthias Ringwald 
21373deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
2138323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
21393deb3ec6SMatthias Ringwald }
21403deb3ec6SMatthias Ringwald 
2141d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
21429c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2143a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2144a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2145a33eb0c4SMilanka Ringwald         return;
2146a33eb0c4SMilanka Ringwald     }
2147a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2148f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21493deb3ec6SMatthias Ringwald }
21503deb3ec6SMatthias Ringwald 
2151d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
21529c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2153a0ffb263SMatthias Ringwald     if (!hfp_connection){
2154a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
21553deb3ec6SMatthias Ringwald         return;
21563deb3ec6SMatthias Ringwald     }
2157a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2158a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
21593deb3ec6SMatthias Ringwald         return;
21603deb3ec6SMatthias Ringwald     }
2161a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2162f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21633deb3ec6SMatthias Ringwald }
21643deb3ec6SMatthias Ringwald 
2165a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
21662ee45eb2SMilanka Ringwald     log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state);
2167a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2168a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
21693deb3ec6SMatthias Ringwald 
2170a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2171a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2172d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2173d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2174a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
21757522e673SMatthias Ringwald         // now, pick link settings
2176afac2a04SMilanka Ringwald         hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
21772ee45eb2SMilanka Ringwald         return;
21783deb3ec6SMatthias Ringwald     }
2179aa4dd815SMatthias Ringwald 
21802ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 1;
2181aa4dd815SMatthias Ringwald }
2182aa4dd815SMatthias Ringwald 
2183d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
21849c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2185a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2186a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2187a33eb0c4SMilanka Ringwald         return;
2188a33eb0c4SMilanka Ringwald     }
21892ee45eb2SMilanka Ringwald     hfp_connection->trigger_codec_exchange = 0;
2190a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2191a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2192f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
21933deb3ec6SMatthias Ringwald }
21943deb3ec6SMatthias Ringwald 
2195d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
21969c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2197a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2198a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2199a33eb0c4SMilanka Ringwald         return;
2200a33eb0c4SMilanka Ringwald     }
2201a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2202f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
22033deb3ec6SMatthias Ringwald }
2204aa4dd815SMatthias Ringwald 
2205aa4dd815SMatthias Ringwald /**
2206aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2207aa4dd815SMatthias Ringwald  */
2208aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2209aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2210aa4dd815SMatthias Ringwald         return;
2211aa4dd815SMatthias Ringwald     }
2212aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2213aa4dd815SMatthias Ringwald 
2214665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2215665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2216665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2217a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
221866c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2219a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2220f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2221aa4dd815SMatthias Ringwald     }
2222aa4dd815SMatthias Ringwald }
2223aa4dd815SMatthias Ringwald 
2224aa4dd815SMatthias Ringwald /**
2225aa4dd815SMatthias Ringwald  * @brief Called from GSM
2226aa4dd815SMatthias Ringwald  */
2227aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2228aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2229aa4dd815SMatthias Ringwald }
2230aa4dd815SMatthias Ringwald 
2231aa4dd815SMatthias Ringwald /**
2232aa4dd815SMatthias Ringwald  * @brief number is stored.
2233aa4dd815SMatthias Ringwald  */
2234aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2235d0c20769SMatthias Ringwald     hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number);
2236aa4dd815SMatthias Ringwald }
2237aa4dd815SMatthias Ringwald 
2238aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2239aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2240aa4dd815SMatthias Ringwald }
2241aa4dd815SMatthias Ringwald 
2242aa4dd815SMatthias Ringwald // call from AG UI
2243aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2244aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2245aa4dd815SMatthias Ringwald }
2246aa4dd815SMatthias Ringwald 
2247ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2248ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2249ce263fc8SMatthias Ringwald }
2250ce263fc8SMatthias Ringwald 
2251aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2252aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2253aa4dd815SMatthias Ringwald }
2254aa4dd815SMatthias Ringwald 
2255aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2256aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2257aa4dd815SMatthias Ringwald }
2258aa4dd815SMatthias Ringwald 
2259aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2260aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2261aa4dd815SMatthias Ringwald }
2262aa4dd815SMatthias Ringwald 
2263aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2264aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2265aa4dd815SMatthias Ringwald }
2266aa4dd815SMatthias Ringwald 
2267aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2268aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2269aa4dd815SMatthias Ringwald }
2270ce263fc8SMatthias Ringwald 
2271ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2272ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2273ce263fc8SMatthias Ringwald }
2274ce263fc8SMatthias Ringwald 
2275ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2276ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2277ce263fc8SMatthias Ringwald }
2278ce263fc8SMatthias Ringwald 
2279ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2280ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2281ce263fc8SMatthias Ringwald }
2282ce263fc8SMatthias Ringwald 
2283aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2284aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2285aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2286aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2287aa4dd815SMatthias Ringwald 
2288ce263fc8SMatthias Ringwald 
2289665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2290665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2291665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2292a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
229366c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2294a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2295ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2296ce263fc8SMatthias Ringwald             continue;
2297ce263fc8SMatthias Ringwald         }
2298ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2299a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2300f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2301aa4dd815SMatthias Ringwald     }
2302aa4dd815SMatthias Ringwald }
2303aa4dd815SMatthias Ringwald 
2304aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2305aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2306aa4dd815SMatthias Ringwald }
2307aa4dd815SMatthias Ringwald 
2308aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2309aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2310aa4dd815SMatthias Ringwald }
2311aa4dd815SMatthias Ringwald 
2312aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2313aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2314aa4dd815SMatthias Ringwald }
2315aa4dd815SMatthias Ringwald 
2316aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2317aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2318aa4dd815SMatthias Ringwald }
2319aa4dd815SMatthias Ringwald 
2320d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){
2321aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
23229c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2323a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2324a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2325a33eb0c4SMilanka Ringwald         return;
2326a33eb0c4SMilanka Ringwald     }
2327aa4dd815SMatthias Ringwald 
2328a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
232960ebb071SMilanka Ringwald         log_info("AG cannot acivate voice recognition - not supported by HF");
2330aa4dd815SMatthias Ringwald         return;
2331aa4dd815SMatthias Ringwald     }
2332aa4dd815SMatthias Ringwald 
2333aa4dd815SMatthias Ringwald     if (activate){
2334d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(acl_handle);
2335aa4dd815SMatthias Ringwald     }
2336aa4dd815SMatthias Ringwald 
2337a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2338a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2339f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2340aa4dd815SMatthias Ringwald }
2341aa4dd815SMatthias Ringwald 
2342d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
23439c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2344a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2345a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2346a33eb0c4SMilanka Ringwald         return;
2347a33eb0c4SMilanka Ringwald     }
2348a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2349a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2350a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2351a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2352aa4dd815SMatthias Ringwald     }
2353f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2354aa4dd815SMatthias Ringwald }
2355aa4dd815SMatthias Ringwald 
2356d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
23579c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2358a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2359a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2360a33eb0c4SMilanka Ringwald         return;
2361a33eb0c4SMilanka Ringwald     }
2362a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2363a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2364a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2365aa4dd815SMatthias Ringwald     }
2366f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2367aa4dd815SMatthias Ringwald }
2368aa4dd815SMatthias Ringwald 
2369d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
23709c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2371a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2372a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2373a33eb0c4SMilanka Ringwald         return;
2374a33eb0c4SMilanka Ringwald     }
2375aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2376a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2377aa4dd815SMatthias Ringwald }
2378aa4dd815SMatthias Ringwald 
2379d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
23809c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2381a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2382a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2383a33eb0c4SMilanka Ringwald         return;
2384a33eb0c4SMilanka Ringwald     }
2385a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2386aa4dd815SMatthias Ringwald }
2387aa4dd815SMatthias Ringwald 
2388d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
23899c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2390a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2391a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2392a33eb0c4SMilanka Ringwald         return;
2393a33eb0c4SMilanka Ringwald     }
2394a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2395c1797c7dSMatthias Ringwald }
2396aa4dd815SMatthias Ringwald 
2397ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2398ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2399ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2400ce263fc8SMatthias Ringwald }
2401ce263fc8SMatthias Ringwald 
24029cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
24039cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2404ce263fc8SMatthias Ringwald }
2405ce263fc8SMatthias Ringwald 
2406d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
24079c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2408a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2409a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2410a33eb0c4SMilanka Ringwald         return;
2411a33eb0c4SMilanka Ringwald     }
2412a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2413a0ffb263SMatthias Ringwald 
2414a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2415f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2416a0ffb263SMatthias Ringwald }
2417ce263fc8SMatthias Ringwald 
2418