xref: /btstack/src/classic/hfp_ag.c (revision 3561686e3c4a7a3314bd18099c58bb0791ac40e6)
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>
493cfa4086SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <string.h>
513deb3ec6SMatthias Ringwald 
5256042629SMatthias Ringwald #include "hci_cmd.h"
5382636622SMatthias Ringwald #include "btstack_run_loop.h"
543deb3ec6SMatthias Ringwald 
55235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
563deb3ec6SMatthias Ringwald #include "hci.h"
573deb3ec6SMatthias Ringwald #include "btstack_memory.h"
583deb3ec6SMatthias Ringwald #include "hci_dump.h"
593deb3ec6SMatthias Ringwald #include "l2cap.h"
6016ece135SMatthias Ringwald #include "btstack_debug.h"
61e30a6a47SMatthias Ringwald #include "btstack_event.h"
6259c6af15SMatthias Ringwald #include "classic/core.h"
633edc84c5SMatthias Ringwald #include "classic/hfp.h"
643edc84c5SMatthias Ringwald #include "classic/hfp_ag.h"
65023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h"
66efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
67023f2764SMatthias Ringwald #include "classic/sdp_server.h"
68023f2764SMatthias Ringwald #include "classic/sdp_util.h"
693deb3ec6SMatthias Ringwald 
70c5b64319SMatthias Ringwald // private prototypes
71f0aeb307SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection);
722ff1d7d7SMilanka Ringwald static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection);
73fcd59412SMilanka Ringwald static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection);
74c5fa3c94SMilanka Ringwald static uint8_t 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 
8499af1e28SMilanka Ringwald #define HFP_SUBEVENT_INVALID 0xFFFF
8527950165SMatthias Ringwald 
8620b2edb6SMatthias Ringwald // const
87aeb0f0feSMatthias Ringwald static const char hfp_ag_default_service_name[] = "Voice gateway";
88aa4dd815SMatthias Ringwald 
8920b2edb6SMatthias Ringwald // globals
90aeb0f0feSMatthias Ringwald 
91aeb0f0feSMatthias Ringwald // higher layer callbacks
92aeb0f0feSMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback;
93aeb0f0feSMatthias Ringwald 
9420b2edb6SMatthias Ringwald static btstack_packet_callback_registration_t hfp_ag_hci_event_callback_registration;
95aa4dd815SMatthias Ringwald 
96aeb0f0feSMatthias Ringwald static uint16_t hfp_ag_supported_features;
9720b2edb6SMatthias Ringwald 
98aeb0f0feSMatthias Ringwald // codecs
99aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs_nr;
100aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs[HFP_MAX_NUM_CODECS];
1013deb3ec6SMatthias Ringwald 
102aeb0f0feSMatthias Ringwald // AG indicators
10320b2edb6SMatthias Ringwald static int                hfp_ag_indicators_nr;
10425789943SMilanka Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS];
1053deb3ec6SMatthias Ringwald 
106aeb0f0feSMatthias Ringwald // generic status indicators
107aeb0f0feSMatthias Ringwald static int                            hfp_ag_generic_status_indicators_nr;
108aeb0f0feSMatthias Ringwald static hfp_generic_status_indicator_t hfp_ag_generic_status_indicators[HFP_MAX_NUM_INDICATORS];
109a0ffb263SMatthias Ringwald 
11020b2edb6SMatthias Ringwald static int    hfp_ag_call_hold_services_nr;
1113deb3ec6SMatthias Ringwald static char * hfp_ag_call_hold_services[6];
1123deb3ec6SMatthias Ringwald 
113ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
11420b2edb6SMatthias Ringwald static int hfp_ag_response_and_hold_active;
115aa4dd815SMatthias Ringwald 
11620b2edb6SMatthias Ringwald // Subscriber information entries
117aeb0f0feSMatthias Ringwald static hfp_phone_number_t * hfp_ag_subscriber_numbers;
118aeb0f0feSMatthias Ringwald static int                  hfp_ag_subscriber_numbers_count;
119ce263fc8SMatthias Ringwald 
120a4f85bd2SMilanka Ringwald // call state
121a4f85bd2SMilanka Ringwald static btstack_timer_source_t hfp_ag_ring_timeout;
122a4f85bd2SMilanka Ringwald 
12320b2edb6SMatthias Ringwald // code
124a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
125a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
126a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1276535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1286535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1293deb3ec6SMatthias Ringwald     }
130a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
131a0ffb263SMatthias Ringwald }
132a0ffb263SMatthias Ringwald 
133a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
134a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
135a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
136a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1376535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1386535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
139a0ffb263SMatthias Ringwald     }
140a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1413deb3ec6SMatthias Ringwald }
1423deb3ec6SMatthias Ringwald 
143aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_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 &hfp_ag_indicators[i];
148aa4dd815SMatthias Ringwald         }
149aa4dd815SMatthias Ringwald     }
150aa4dd815SMatthias Ringwald     return NULL;
151aa4dd815SMatthias Ringwald }
152aa4dd815SMatthias Ringwald 
153aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
154aa4dd815SMatthias Ringwald     int i;
155aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
156aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
157aa4dd815SMatthias Ringwald             return i;
158aa4dd815SMatthias Ringwald         }
159aa4dd815SMatthias Ringwald     }
160aa4dd815SMatthias Ringwald     return -1;
161aa4dd815SMatthias Ringwald }
162aa4dd815SMatthias Ringwald 
1639c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){
1649c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1659c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1669c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1679c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1689c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1699c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1709c9c64c1SMatthias Ringwald         return hfp_connection;
1719c9c64c1SMatthias Ringwald     }
1729c9c64c1SMatthias Ringwald     return NULL;
1739c9c64c1SMatthias Ringwald }
1743deb3ec6SMatthias Ringwald 
1750cb5b971SMatthias Ringwald static int use_in_band_tone(void){
176aeb0f0feSMatthias Ringwald     return get_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1773deb3ec6SMatthias Ringwald }
1783deb3ec6SMatthias Ringwald 
179a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
180a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
181aeb0f0feSMatthias Ringwald     int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1823deb3ec6SMatthias Ringwald     return hf && ag;
1833deb3ec6SMatthias Ringwald }
1843deb3ec6SMatthias Ringwald 
185a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
186a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
187aeb0f0feSMatthias Ringwald     int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1883deb3ec6SMatthias Ringwald     return hf && ag;
1893deb3ec6SMatthias Ringwald }
1903deb3ec6SMatthias Ringwald 
191a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
192a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
193aeb0f0feSMatthias Ringwald     int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_HF_INDICATORS);
1943deb3ec6SMatthias Ringwald     return hf && ag;
1953deb3ec6SMatthias Ringwald }
1963deb3ec6SMatthias Ringwald 
19776cc1527SMatthias Ringwald /* unsolicited responses */
198aa4dd815SMatthias Ringwald 
199485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){
200aa4dd815SMatthias Ringwald     char buffer[20];
201ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n",
202ff7d6aeaSMatthias Ringwald              HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
203ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
204aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2053deb3ec6SMatthias Ringwald }
2063deb3ec6SMatthias Ringwald 
2073deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2083deb3ec6SMatthias Ringwald     char buffer[40];
209ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n\r\nOK\r\n",
210aeb0f0feSMatthias Ringwald              HFP_SUPPORTED_FEATURES, hfp_ag_supported_features);
211ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
2123deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2133deb3ec6SMatthias Ringwald }
2143deb3ec6SMatthias Ringwald 
215485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){
2163deb3ec6SMatthias Ringwald     char buffer[10];
217ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\nOK\r\n");
218ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
2193deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2203deb3ec6SMatthias Ringwald }
2213deb3ec6SMatthias Ringwald 
222485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){
223aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
224aa4dd815SMatthias Ringwald }
225aa4dd815SMatthias Ringwald 
226245852b7SMilanka Ringwald static int hfp_ag_send_no_carrier(uint16_t cid){
227245852b7SMilanka Ringwald     char buffer[15];
228245852b7SMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\nNO CARRIER\r\n");
229245852b7SMilanka Ringwald     buffer[sizeof(buffer) - 1] = 0;
230245852b7SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
231245852b7SMilanka Ringwald }
232245852b7SMilanka Ringwald 
233aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
234aa4dd815SMatthias Ringwald     char buffer[50];
235ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP,
236ff7d6aeaSMatthias Ringwald              hfp_gsm_clip_number(), hfp_gsm_clip_type());
237ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
238aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
239aa4dd815SMatthias Ringwald }
240aa4dd815SMatthias Ringwald 
241ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
242ce263fc8SMatthias Ringwald     char buffer[50];
243ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: ,\"%s\",%u, , \r\n",
244ff7d6aeaSMatthias Ringwald              HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
245ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
246ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
247ce263fc8SMatthias Ringwald }
248ce263fc8SMatthias Ringwald 
249c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
250aa4dd815SMatthias Ringwald     char buffer[50];
251ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: %s\r\n",
252ff7d6aeaSMatthias Ringwald              HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
253ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
254aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
255aa4dd815SMatthias Ringwald }
256aa4dd815SMatthias Ringwald 
257aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
258aa4dd815SMatthias Ringwald     char buffer[50];
259ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n",
260ff7d6aeaSMatthias Ringwald              HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(),
261ff7d6aeaSMatthias Ringwald              hfp_gsm_clip_type());
262ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
263aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
264aa4dd815SMatthias Ringwald }
265aa4dd815SMatthias Ringwald 
266485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){
2673deb3ec6SMatthias Ringwald     char buffer[10];
268ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\nERROR\r\n");
269ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
2703deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2713deb3ec6SMatthias Ringwald }
2723deb3ec6SMatthias Ringwald 
273485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2743deb3ec6SMatthias Ringwald     char buffer[20];
275ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s=%d\r\n",
276ff7d6aeaSMatthias Ringwald              HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
277ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
2783deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2793deb3ec6SMatthias Ringwald }
2803deb3ec6SMatthias Ringwald 
281ad902e3dSMatthias Ringwald // get size for indicator string
282a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
283ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
28445718b6fSMatthias Ringwald     return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
285a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
286a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
287ad902e3dSMatthias Ringwald }
288ad902e3dSMatthias Ringwald 
289ad902e3dSMatthias Ringwald // store indicator
2901167ff83SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer, uint16_t buffer_size){
2911167ff83SMatthias Ringwald     snprintf((char *)buffer, buffer_size, "(\"%s\",(%d,%d)),",
292a0ffb263SMatthias Ringwald              hfp_ag_get_ag_indicators(hfp_connection)[i].name,
293a0ffb263SMatthias Ringwald              hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
294a0ffb263SMatthias Ringwald              hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2951167ff83SMatthias Ringwald     ((char *)buffer)[buffer_size - 1] = 0;
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 
3221167ff83SMatthias Ringwald static void hfp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer, uint16_t buffer_size){
323ad902e3dSMatthias Ringwald     if (index == 0){
324ad902e3dSMatthias Ringwald         *buffer++ = '\r';
32574386ee0SMatthias Ringwald         *buffer++ = '\n';
326ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
3276535961aSMatthias Ringwald         (void)memcpy(buffer, HFP_INDICATOR, len);
328ad902e3dSMatthias Ringwald         buffer += len;
329ad902e3dSMatthias Ringwald         *buffer++ = ':';
330ad902e3dSMatthias Ringwald         return;
331ad902e3dSMatthias Ringwald     }
332ad902e3dSMatthias Ringwald     index--;
333a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
334ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
335ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
3361167ff83SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer, buffer_size);
337ad902e3dSMatthias Ringwald         return;
338ad902e3dSMatthias Ringwald     }
339c1ab6cc1SMatthias Ringwald     if (indicator_index == (num_indicators-1)){
3406535961aSMatthias Ringwald         (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8);
341ad902e3dSMatthias Ringwald         return;
342ad902e3dSMatthias Ringwald     }
343ad902e3dSMatthias Ringwald     *buffer = ',';
3443deb3ec6SMatthias Ringwald }
3453deb3ec6SMatthias Ringwald 
346aeb0f0feSMatthias Ringwald static int hfp_ag_generic_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;
350aeb0f0feSMatthias Ringwald     for (i = 0; i < (hfp_ag_generic_status_indicators_nr - 1); i++) {
351aeb0f0feSMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_generic_status_indicators[i].uuid);
3523deb3ec6SMatthias Ringwald     }
353aeb0f0feSMatthias Ringwald     if (i < hfp_ag_generic_status_indicators_nr){
354aeb0f0feSMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_generic_status_indicators[i].uuid);
3553deb3ec6SMatthias Ringwald     }
3563deb3ec6SMatthias Ringwald     return offset;
3573deb3ec6SMatthias Ringwald }
3583deb3ec6SMatthias Ringwald 
359aeb0f0feSMatthias Ringwald static int hfp_ag_generic_indicators_initial_status_join(char * buffer, int buffer_size){
360aeb0f0feSMatthias Ringwald     if (buffer_size < (hfp_ag_generic_status_indicators_nr * 3)) return 0;
3613deb3ec6SMatthias Ringwald     int i;
3623deb3ec6SMatthias Ringwald     int offset = 0;
363aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_ag_generic_status_indicators_nr; i++) {
364aeb0f0feSMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "\r\n%s:%d,%d\r\n", HFP_GENERIC_STATUS_INDICATOR, hfp_ag_generic_status_indicators[i].uuid, hfp_ag_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),
3981167ff83SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer, uint16_t buffer_size)){
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);
4101167ff83SMatthias Ringwald         if ((offset + segment_len + 1) > mtu) break;
4111167ff83SMatthias Ringwald         // append segment. As it appends a '\0', we provide a buffer one byte larger
4121167ff83SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset, segment_len + 1);
413ad902e3dSMatthias Ringwald         offset += segment_len;
414ad902e3dSMatthias Ringwald         segment++;
415ad902e3dSMatthias Ringwald     }
416ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
417f20dd2aeSBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES
418f20dd2aeSBjoern Hartmann     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, (char *) data);
419f20dd2aeSBjoern Hartmann #endif
420ad902e3dSMatthias Ringwald     return segment;
421ad902e3dSMatthias Ringwald }
4223deb3ec6SMatthias Ringwald 
423ad902e3dSMatthias Ringwald // returns next segment to store
424485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
425a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
426485ac19eSMilanka Ringwald     return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
427894d499cSMatthias Ringwald                                          hfp_ag_indicators_cmd_generator_get_segment_len,
428894d499cSMatthias Ringwald                                          hfp_ag_indicators_cmd_generator_store_segment);
4293deb3ec6SMatthias Ringwald }
4303deb3ec6SMatthias Ringwald 
431485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){
4323deb3ec6SMatthias Ringwald     char buffer[40];
43389425bfcSMilanka Ringwald     const int size = sizeof(buffer);
43489425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR);
43589425bfcSMilanka Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9);
43689425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4373deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4383deb3ec6SMatthias Ringwald }
4393deb3ec6SMatthias Ringwald 
440485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){
441ad902e3dSMatthias Ringwald     char buffer[40];
44289425bfcSMilanka Ringwald     const int size = sizeof(buffer);
44389425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
44489425bfcSMilanka Ringwald     offset += hfp_ag_call_services_join(buffer+offset, size-offset-9);
44589425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4463deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4473deb3ec6SMatthias Ringwald }
4483deb3ec6SMatthias Ringwald 
4493deb3ec6SMatthias Ringwald 
450485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){
451485ac19eSMilanka Ringwald     return hfp_ag_send_ok(cid);
4523deb3ec6SMatthias Ringwald }
4533deb3ec6SMatthias Ringwald 
454485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4553deb3ec6SMatthias Ringwald     char buffer[40];
45689425bfcSMilanka Ringwald     const int size = sizeof(buffer);
45789425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
458aeb0f0feSMatthias Ringwald     offset += hfp_ag_generic_indicators_join(buffer + offset, size - offset - 10);
45989425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n");
4603deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4613deb3ec6SMatthias Ringwald }
4623deb3ec6SMatthias Ringwald 
463485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4643deb3ec6SMatthias Ringwald     char buffer[40];
465aeb0f0feSMatthias Ringwald     int offset = hfp_ag_generic_indicators_initial_status_join(buffer, sizeof(buffer) - 7);
4661cc1d9e9SMilanka Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4673deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4683deb3ec6SMatthias Ringwald }
4693deb3ec6SMatthias Ringwald 
470485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4713deb3ec6SMatthias Ringwald     char buffer[20];
472ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d\r\n",
473ff7d6aeaSMatthias Ringwald              HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index,
474ff7d6aeaSMatthias Ringwald              indicator->status);
475ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
4763deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4773deb3ec6SMatthias Ringwald }
4783deb3ec6SMatthias Ringwald 
479485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4805e71456cSMilanka Ringwald     char buffer[41];
4813deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
48289425bfcSMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4833deb3ec6SMatthias Ringwald     } else {
48489425bfcSMilanka Ringwald         int offset = snprintf(buffer,  sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format);
4855e71456cSMilanka Ringwald         offset += snprintf(buffer+offset, 16, "%s", op.name);
48689425bfcSMilanka Ringwald         snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4873deb3ec6SMatthias Ringwald     }
4883deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4893deb3ec6SMatthias Ringwald }
4903deb3ec6SMatthias Ringwald 
491a04aecb1SMilanka Ringwald static inline int hfp_ag_send_cmd_with_space_and_int(uint16_t cid, const char * cmd, uint8_t value){
492a04aecb1SMilanka Ringwald     char buffer[30];
493a04aecb1SMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", cmd, value);
494a04aecb1SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
495a04aecb1SMilanka Ringwald }
496a04aecb1SMilanka Ringwald 
497a04aecb1SMilanka Ringwald 
4981cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
4991cc1d9e9SMilanka Ringwald     char buffer[30];
50089425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value);
5011cc1d9e9SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
5021cc1d9e9SMilanka Ringwald }
5033deb3ec6SMatthias Ringwald 
504485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){
5051cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
5063deb3ec6SMatthias Ringwald }
5073deb3ec6SMatthias Ringwald 
508485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
5091cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
510aa4dd815SMatthias Ringwald }
511aa4dd815SMatthias Ringwald 
512485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
5131cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
514aa4dd815SMatthias Ringwald }
515aa4dd815SMatthias Ringwald 
516485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){
51766191aa0SMilanka Ringwald     return hfp_ag_send_cmd_with_space_and_int(cid, HFP_RESPONSE_AND_HOLD, state);
518ce263fc8SMatthias Ringwald }
519ce263fc8SMatthias Ringwald 
520cf75be85SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection_t * hfp_connection){
521cf75be85SMilanka Ringwald     char buffer[30];
522cf75be85SMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, hfp_connection->enhanced_voice_recognition_enabled, hfp_connection->ag_vra_state);
523cf75be85SMilanka Ringwald     return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
524cf75be85SMilanka Ringwald }
525cf75be85SMilanka Ringwald 
526d1b50446SMilanka Ringwald static int hfp_ag_send_voice_recognition_cmd(hfp_connection_t * hfp_connection, uint8_t activate_voice_recognition){
52745796ff1SMilanka Ringwald     char buffer[30];
528d1b50446SMilanka Ringwald     if (hfp_connection->enhanced_voice_recognition_enabled){
529d1b50446SMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition, hfp_connection->ag_vra_state);
530d1b50446SMilanka Ringwald     } else {
531d1b50446SMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
532d1b50446SMilanka Ringwald     }
533d1b50446SMilanka Ringwald     return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
53445796ff1SMilanka Ringwald }
53545796ff1SMilanka Ringwald 
53645796ff1SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection_t * hfp_connection){
53745796ff1SMilanka Ringwald     char buffer[100];
53845796ff1SMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: 1,%d,%X,%d,%d,\"%s\"\r\n", HFP_ACTIVATE_VOICE_RECOGNITION,
53945796ff1SMilanka Ringwald         hfp_connection->ag_vra_state,
54045796ff1SMilanka Ringwald         hfp_connection->ag_msg.text_id,
54145796ff1SMilanka Ringwald         hfp_connection->ag_msg.text_type,
54245796ff1SMilanka Ringwald         hfp_connection->ag_msg.text_operation,
54345796ff1SMilanka Ringwald         hfp_connection->ag_msg.text);
54445796ff1SMilanka Ringwald     return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
54545796ff1SMilanka Ringwald }
54645796ff1SMilanka Ringwald 
547a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
548bc1b1537SMilanka Ringwald     if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
549bc1b1537SMilanka Ringwald 
550aeb0f0feSMatthias Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_ag_codecs_nr, hfp_ag_codecs)){
5516a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
552df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
5533deb3ec6SMatthias Ringwald         }
5543deb3ec6SMatthias Ringwald     }
555df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
5563deb3ec6SMatthias Ringwald }
5573deb3ec6SMatthias Ringwald 
55876cc1527SMatthias Ringwald /* state machines */
55976cc1527SMatthias Ringwald 
560afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){
561aeb0f0feSMatthias Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) &&  (hfp_ag_supported_features & (1 << HFP_AGSF_ESCO_S4));
5627522e673SMatthias Ringwald }
5637522e673SMatthias Ringwald 
564a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
565aa4dd815SMatthias Ringwald     /* events ( == commands):
566aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
567aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
568aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
569aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
570aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
571aa4dd815SMatthias Ringwald     */
572a0ffb263SMatthias Ringwald     switch (hfp_connection->codecs_state){
5733e3b9207SMilanka Ringwald         case HFP_CODECS_EXCHANGED:
5743e3b9207SMilanka Ringwald             if (hfp_connection->command == HFP_CMD_AVAILABLE_CODECS){
5753e3b9207SMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
5763e3b9207SMilanka Ringwald                 return 1;
5773e3b9207SMilanka Ringwald             }
5783e3b9207SMilanka Ringwald             break;
5793e3b9207SMilanka Ringwald 
580aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
581a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
582aa4dd815SMatthias Ringwald             break;
583aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
584a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
585aa4dd815SMatthias Ringwald             break;
586aa4dd815SMatthias Ringwald         default:
587aa4dd815SMatthias Ringwald             break;
588aa4dd815SMatthias Ringwald     }
589aa4dd815SMatthias Ringwald 
590a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
591aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
592a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
593a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
594485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
595aa4dd815SMatthias Ringwald                 return 1;
596aa4dd815SMatthias Ringwald             }
597aa4dd815SMatthias Ringwald 
598a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
599aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
600a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
601aa4dd815SMatthias Ringwald                     break;
602aa4dd815SMatthias Ringwald                 default:
603aa4dd815SMatthias Ringwald                     break;
604aa4dd815SMatthias Ringwald             }
605485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
606aa4dd815SMatthias Ringwald             return 1;
607aa4dd815SMatthias Ringwald 
608aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
609a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
61040e8d6c5SMatthias Ringwald             hfp_connection->establish_audio_connection = 1;
611485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
612aa4dd815SMatthias Ringwald             return 1;
613aa4dd815SMatthias Ringwald 
614aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
615a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
616a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
617485ac19eSMilanka Ringwald             hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
618aa4dd815SMatthias Ringwald             return 1;
619aa4dd815SMatthias Ringwald 
620aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
621a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
622a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
623485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
624aa4dd815SMatthias Ringwald                 return 1;
625aa4dd815SMatthias Ringwald             }
626a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
627a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
628c1ab6cc1SMatthias Ringwald             log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
629485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
6307522e673SMatthias Ringwald             // now, pick link settings
631afac2a04SMilanka Ringwald             hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
6323721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
6333721a235SMatthias Ringwald             hfp_cc256x_prepare_for_sco(hfp_connection);
6343721a235SMatthias Ringwald #endif
635aa4dd815SMatthias Ringwald             return 1;
636aa4dd815SMatthias Ringwald         default:
637aa4dd815SMatthias Ringwald             break;
638aa4dd815SMatthias Ringwald     }
639aa4dd815SMatthias Ringwald     return 0;
640aa4dd815SMatthias Ringwald }
641aa4dd815SMatthias Ringwald 
642a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
643a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
644ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
645aa4dd815SMatthias Ringwald 
646a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
647d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
648a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
649aa4dd815SMatthias Ringwald     }
650ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
651c4f8fc1cSMilanka Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){
652c4f8fc1cSMilanka Ringwald         switch (hfp_gsm_callsetup_status()){
653c4f8fc1cSMilanka Ringwald             case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
6542ff1d7d7SMilanka Ringwald                 hfp_ag_hf_start_ringing_incoming(hfp_connection);
655c4f8fc1cSMilanka Ringwald                 break;
656fcd59412SMilanka Ringwald             case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
657fcd59412SMilanka Ringwald                 hfp_ag_hf_start_ringing_outgoing(hfp_connection);
658fcd59412SMilanka Ringwald                 break;
659c4f8fc1cSMilanka Ringwald             default:
660c4f8fc1cSMilanka Ringwald                 break;
661c4f8fc1cSMilanka Ringwald         }
662ce263fc8SMatthias Ringwald     }
663aa4dd815SMatthias Ringwald }
6643deb3ec6SMatthias Ringwald 
665a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
666c79b4cabSMatthias Ringwald     // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
667a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
668c04cf7ffSMilanka Ringwald     int sent = 0;
669a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
6703deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
671a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
6723deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
673aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
674aeb0f0feSMatthias Ringwald                     hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_ag_codecs, &hfp_ag_codecs_nr);
675a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
676a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
677aa4dd815SMatthias Ringwald                     } else {
678a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
679aa4dd815SMatthias Ringwald                     }
680a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
681aa4dd815SMatthias Ringwald                     return 1;
6823deb3ec6SMatthias Ringwald                 default:
6833deb3ec6SMatthias Ringwald                     break;
6843deb3ec6SMatthias Ringwald             }
6853deb3ec6SMatthias Ringwald             break;
6863deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
687c04cf7ffSMilanka Ringwald             sent = codecs_exchange_state_machine(hfp_connection);
688a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
689a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6903deb3ec6SMatthias Ringwald             }
691c04cf7ffSMilanka Ringwald             return sent;
692aa4dd815SMatthias Ringwald 
693aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
694a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
695473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
69684a0c24eSMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
697473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
698473ac565SMatthias Ringwald                 // continue below in state switch
699ad902e3dSMatthias Ringwald             }
700ad902e3dSMatthias Ringwald             break;
701aa4dd815SMatthias Ringwald 
702aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
703a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
704a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
705485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
706aa4dd815SMatthias Ringwald             return 1;
707aa4dd815SMatthias Ringwald 
7083deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
709a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
710a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
711a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
712a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
713a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
714aa4dd815SMatthias Ringwald             } else {
715a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
7163deb3ec6SMatthias Ringwald             }
7178a46ec40SMatthias Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
718aa4dd815SMatthias Ringwald             return 1;
7193deb3ec6SMatthias Ringwald 
720aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
721a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
722a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
723a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
724aa4dd815SMatthias Ringwald             }
725485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
726a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
727a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
728ce263fc8SMatthias Ringwald             }
729aa4dd815SMatthias Ringwald             return 1;
730aa4dd815SMatthias Ringwald 
731aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
732a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
733a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
734485ac19eSMilanka Ringwald             hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
735aa4dd815SMatthias Ringwald             return 1;
736aa4dd815SMatthias Ringwald 
737aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
738a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
739a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
740485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
741aa4dd815SMatthias Ringwald             return 1;
742aa4dd815SMatthias Ringwald 
743aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
744a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
745a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
746485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
747aa4dd815SMatthias Ringwald             return 1;
7483deb3ec6SMatthias Ringwald         default:
7493deb3ec6SMatthias Ringwald             break;
7503deb3ec6SMatthias Ringwald     }
751473ac565SMatthias Ringwald 
752473ac565SMatthias Ringwald     switch (hfp_connection->state){
753473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
754485ac19eSMilanka Ringwald             int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
755473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
756473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
757473ac565SMatthias Ringwald             if (next_segment < num_segments){
758473ac565SMatthias Ringwald                 // prepare sending of next segment
759473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
760473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
761473ac565SMatthias Ringwald             } else {
762473ac565SMatthias Ringwald                 // done, go to next state
763473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
764473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
765473ac565SMatthias Ringwald             }
766473ac565SMatthias Ringwald             return 1;
767473ac565SMatthias Ringwald         }
768473ac565SMatthias Ringwald         default:
769473ac565SMatthias Ringwald             break;
770473ac565SMatthias Ringwald     }
771c04cf7ffSMilanka Ringwald     return 0;
7723deb3ec6SMatthias Ringwald }
7733deb3ec6SMatthias Ringwald 
774013cc750SMilanka Ringwald static bool hfp_ag_voice_recognition_supported(hfp_connection_t * hfp_connection, bool enhanced){
775013cc750SMilanka Ringwald     uint8_t ag_vra_flag = HFP_AGSF_VOICE_RECOGNITION_FUNCTION;
776013cc750SMilanka Ringwald     uint8_t hf_vra_flag = HFP_HFSF_VOICE_RECOGNITION_FUNCTION;
777013cc750SMilanka Ringwald 
778013cc750SMilanka Ringwald     if (enhanced){
779013cc750SMilanka Ringwald         ag_vra_flag = HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS;
780013cc750SMilanka Ringwald         hf_vra_flag = HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS;
781013cc750SMilanka Ringwald     }
782013cc750SMilanka Ringwald 
783aeb0f0feSMatthias Ringwald     int ag = get_bit(hfp_ag_supported_features, ag_vra_flag);
784013cc750SMilanka Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, hf_vra_flag);
785d84719b1SMilanka Ringwald     return hf && ag;
786d84719b1SMilanka Ringwald }
787d84719b1SMilanka Ringwald 
788a0c831ebSMilanka Ringwald static uint8_t hfp_ag_can_activate_voice_recognition(hfp_connection_t * hfp_connection, bool enhanced){
78908a0b01cSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
79008a0b01cSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
79108a0b01cSMilanka Ringwald     }
792013cc750SMilanka Ringwald     if (!hfp_ag_voice_recognition_supported(hfp_connection, enhanced)){
793d84719b1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
794d84719b1SMilanka Ringwald     }
7950c738fd4SMilanka Ringwald     if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){
796d84719b1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
797d84719b1SMilanka Ringwald     }
798d84719b1SMilanka Ringwald     return ERROR_CODE_SUCCESS;
799d84719b1SMilanka Ringwald }
800d84719b1SMilanka Ringwald 
801a0c831ebSMilanka Ringwald static uint8_t hfp_ag_voice_recognition_session_active(hfp_connection_t * hfp_connection, bool enhanced){
80208a0b01cSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
80308a0b01cSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
80408a0b01cSMilanka Ringwald     }
805013cc750SMilanka Ringwald     if (!hfp_ag_voice_recognition_supported(hfp_connection, enhanced)){
806d84719b1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
807d84719b1SMilanka Ringwald     }
8080c738fd4SMilanka Ringwald     if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
809de9e0ea7SMilanka Ringwald         if (hfp_connection->vra_state == HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){
810de9e0ea7SMilanka Ringwald             return ERROR_CODE_SUCCESS;
811de9e0ea7SMilanka Ringwald         }
812d84719b1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
813d84719b1SMilanka Ringwald     }
814d84719b1SMilanka Ringwald     return ERROR_CODE_SUCCESS;
815d84719b1SMilanka Ringwald }
816754910caSMilanka Ringwald 
817f48848deSMilanka Ringwald static bool hfp_ag_can_send_enahnced_voice_recognition_message(hfp_connection_t * hfp_connection, bool enhanced){
818f48848deSMilanka Ringwald     if (hfp_ag_voice_recognition_session_active(hfp_connection, enhanced) == ERROR_CODE_SUCCESS){
819aeb0f0feSMatthias Ringwald         int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_TEXT);
820f48848deSMilanka Ringwald         int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_TEXT);
821f48848deSMilanka Ringwald         return hf && ag;
822f48848deSMilanka Ringwald     }
823f48848deSMilanka Ringwald     return false;
824f48848deSMilanka Ringwald }
825adb00feeSMilanka Ringwald 
8260b4debbfSMilanka Ringwald static bool hfp_ag_is_audio_connection_active(hfp_connection_t * hfp_connection){
8270b4debbfSMilanka Ringwald     switch (hfp_connection->state){
8280b4debbfSMilanka Ringwald         case HFP_W2_CONNECT_SCO:
8290b4debbfSMilanka Ringwald         case HFP_W4_SCO_CONNECTED:
8300b4debbfSMilanka Ringwald         case HFP_AUDIO_CONNECTION_ESTABLISHED:
8310b4debbfSMilanka Ringwald             return true;
832754910caSMilanka Ringwald         default:
8330b4debbfSMilanka Ringwald             return false;
834754910caSMilanka Ringwald     }
835754910caSMilanka Ringwald }
836754910caSMilanka Ringwald 
837b95cac54SMilanka Ringwald static void hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection_t * hfp_connection, uint8_t status){
838b95cac54SMilanka Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
839b95cac54SMilanka Ringwald 
840b95cac54SMilanka Ringwald     uint8_t event[6];
841b95cac54SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
842b95cac54SMilanka Ringwald     event[1] = sizeof(event) - 2;
843c95b5b3cSMilanka Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE_SENT;
844b95cac54SMilanka Ringwald     little_endian_store_16(event, 3, acl_handle);
845b95cac54SMilanka Ringwald     event[5] = status;
846b95cac54SMilanka Ringwald     (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
847b95cac54SMilanka Ringwald }
848b95cac54SMilanka Ringwald 
849347d46c8SMilanka Ringwald 
850585db78dSMilanka Ringwald static void hfp_ag_emit_hf_indicator_value(hfp_connection_t * hfp_connection, uint16_t uuid, uint8_t value){
851347d46c8SMilanka Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
852347d46c8SMilanka Ringwald 
853347d46c8SMilanka Ringwald     uint8_t event[8];
854347d46c8SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
855347d46c8SMilanka Ringwald     event[1] = sizeof(event) - 2;
856347d46c8SMilanka Ringwald     event[2] = HFP_SUBEVENT_HF_INDICATOR;
857347d46c8SMilanka Ringwald     little_endian_store_16(event, 3, acl_handle);
858347d46c8SMilanka Ringwald     little_endian_store_16(event, 5, uuid);
859347d46c8SMilanka Ringwald     event[7] = value;
860347d46c8SMilanka Ringwald     (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
861347d46c8SMilanka Ringwald }
862347d46c8SMilanka Ringwald 
863a4f85bd2SMilanka Ringwald static void hfp_ag_emit_general_simple_event(uint8_t event_subtype){
864a4f85bd2SMilanka Ringwald     if (!hfp_ag_callback) return;
865a4f85bd2SMilanka Ringwald 
866a4f85bd2SMilanka Ringwald     uint8_t event[5];
867a4f85bd2SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
868a4f85bd2SMilanka Ringwald     event[1] = sizeof(event) - 2;
869a4f85bd2SMilanka Ringwald     event[2] = event_subtype;
870a4f85bd2SMilanka Ringwald     little_endian_store_16(event, 3, HCI_CON_HANDLE_INVALID);
871a4f85bd2SMilanka Ringwald     (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
872a4f85bd2SMilanka Ringwald }
873a4f85bd2SMilanka Ringwald 
874754910caSMilanka Ringwald static int hfp_ag_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
875754910caSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
876754910caSMilanka Ringwald         return 0;
877754910caSMilanka Ringwald     }
878754910caSMilanka Ringwald     int done = 0;
879754910caSMilanka Ringwald     uint8_t status;
880754910caSMilanka Ringwald 
881fd4151d1SMilanka Ringwald     switch (hfp_connection->command){
882fd4151d1SMilanka Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
883cf75be85SMilanka Ringwald             switch (hfp_connection->vra_state_requested){
884b95cac54SMilanka Ringwald                 case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG:
885b95cac54SMilanka Ringwald                     done = hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection);
886b95cac54SMilanka Ringwald                     if (done == 0){
887b95cac54SMilanka Ringwald                         hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
888b95cac54SMilanka Ringwald                     } else {
889b95cac54SMilanka Ringwald                         hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_SUCCESS);
890b95cac54SMilanka Ringwald                     }
891b95cac54SMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
892b95cac54SMilanka Ringwald                     return done;
893b95cac54SMilanka Ringwald 
894cf75be85SMilanka Ringwald                 case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS:
895cf75be85SMilanka Ringwald                     done = hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection);
896cf75be85SMilanka Ringwald                     if (done == 0){
897cf75be85SMilanka Ringwald                         hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
898cf75be85SMilanka Ringwald                     } else {
899cf75be85SMilanka Ringwald                         hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
900cf75be85SMilanka Ringwald                     }
901cf75be85SMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
902b95cac54SMilanka Ringwald                     return done;
903b95cac54SMilanka Ringwald 
904cf75be85SMilanka Ringwald                 default:
905d1b50446SMilanka Ringwald                     done = hfp_ag_send_voice_recognition_cmd(hfp_connection, hfp_connection->ag_activate_voice_recognition_value);
9060c738fd4SMilanka Ringwald                     if (done == 0){
9070c738fd4SMilanka Ringwald                         hfp_connection->vra_state_requested = hfp_connection->vra_state;
9081a26de69SMilanka Ringwald                         hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_COMMAND_DISALLOWED);
909fd4151d1SMilanka Ringwald                         return 0;
910fd4151d1SMilanka Ringwald                     }
911fd4151d1SMilanka Ringwald                     break;
912cf75be85SMilanka Ringwald             }
913971388feSMatthias Ringwald             break;
914fd4151d1SMilanka Ringwald 
915fd4151d1SMilanka Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
916734ef2bdSMilanka Ringwald             // HF initiatied voice recognition, parser extracted activation value
917de9e0ea7SMilanka Ringwald             switch (hfp_connection->ag_activate_voice_recognition_value){
918de9e0ea7SMilanka Ringwald                 case 0:
919a0c831ebSMilanka Ringwald                     if (hfp_ag_voice_recognition_session_active(hfp_connection, hfp_connection->enhanced_voice_recognition_enabled) == ERROR_CODE_SUCCESS){
920fd4151d1SMilanka Ringwald                         hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
921fd4151d1SMilanka Ringwald                         done = hfp_ag_send_ok(hfp_connection->rfcomm_cid);
922fd4151d1SMilanka Ringwald                     }
923de9e0ea7SMilanka Ringwald                     break;
924de9e0ea7SMilanka Ringwald 
925de9e0ea7SMilanka Ringwald                 case 1:
926a0c831ebSMilanka Ringwald                     if (hfp_ag_can_activate_voice_recognition(hfp_connection, hfp_connection->enhanced_voice_recognition_enabled) == ERROR_CODE_SUCCESS){
927fd4151d1SMilanka Ringwald                         hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
928fd4151d1SMilanka Ringwald                         done = hfp_ag_send_ok(hfp_connection->rfcomm_cid);
929fd4151d1SMilanka Ringwald                     }
930de9e0ea7SMilanka Ringwald                     break;
931de9e0ea7SMilanka Ringwald 
932de9e0ea7SMilanka Ringwald                 case 2:
933de9e0ea7SMilanka Ringwald                     if (hfp_ag_voice_recognition_session_active(hfp_connection, hfp_connection->enhanced_voice_recognition_enabled) == ERROR_CODE_SUCCESS){
934de9e0ea7SMilanka Ringwald                         hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
935de9e0ea7SMilanka Ringwald                         done = hfp_ag_send_ok(hfp_connection->rfcomm_cid);
936de9e0ea7SMilanka Ringwald                     }
937de9e0ea7SMilanka Ringwald                     break;
938de9e0ea7SMilanka Ringwald                 default:
939de9e0ea7SMilanka Ringwald                     break;
940fd4151d1SMilanka Ringwald             }
941fd4151d1SMilanka Ringwald             if (done == 0){
942fd4151d1SMilanka Ringwald                 hfp_connection->vra_state_requested = hfp_connection->vra_state;
943fd4151d1SMilanka Ringwald                 done = hfp_ag_send_error(hfp_connection->rfcomm_cid);
944fd4151d1SMilanka Ringwald                 return 1;
945fd4151d1SMilanka Ringwald             }
946fd4151d1SMilanka Ringwald             break;
947fd4151d1SMilanka Ringwald 
948fd4151d1SMilanka Ringwald         default:
949754910caSMilanka Ringwald             return 0;
950754910caSMilanka Ringwald     }
951754910caSMilanka Ringwald 
952fd4151d1SMilanka Ringwald     switch (hfp_connection->vra_state_requested){
953de9e0ea7SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
954de9e0ea7SMilanka Ringwald             if (!hfp_ag_is_audio_connection_active(hfp_connection)){
955de9e0ea7SMilanka Ringwald                 status = hfp_ag_setup_audio_connection(hfp_connection);
956de9e0ea7SMilanka Ringwald                 if (status != ERROR_CODE_SUCCESS){
957de9e0ea7SMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
958de9e0ea7SMilanka Ringwald                     hfp_emit_voice_recognition_state_event(hfp_connection, status);
959de9e0ea7SMilanka Ringwald                     return 0;
960de9e0ea7SMilanka Ringwald                 }
961de9e0ea7SMilanka Ringwald             }
962de9e0ea7SMilanka Ringwald             hfp_connection->enhanced_voice_recognition_enabled = true;
963de9e0ea7SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
964de9e0ea7SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
965de9e0ea7SMilanka Ringwald             hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
966de9e0ea7SMilanka Ringwald             break;
967de9e0ea7SMilanka Ringwald 
968fd4151d1SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
9690c738fd4SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
9700c738fd4SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
9710c738fd4SMilanka Ringwald 
9721a26de69SMilanka Ringwald             // release audio connection only if it was opened after audio VR activated
9731a26de69SMilanka Ringwald             if (hfp_ag_is_audio_connection_active(hfp_connection) && !hfp_connection->ag_audio_connection_opened_before_vra){
974754910caSMilanka Ringwald                 hfp_trigger_release_audio_connection(hfp_connection);
9750b4debbfSMilanka Ringwald             }
9760b4debbfSMilanka Ringwald 
9771a26de69SMilanka Ringwald             hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
978754910caSMilanka Ringwald             break;
979754910caSMilanka Ringwald 
980fd4151d1SMilanka Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
9810b4debbfSMilanka Ringwald             // open only if audio connection does not already exist
9820b4debbfSMilanka Ringwald             if (!hfp_ag_is_audio_connection_active(hfp_connection)){
983754910caSMilanka Ringwald                 status = hfp_ag_setup_audio_connection(hfp_connection);
984754910caSMilanka Ringwald                 if (status != ERROR_CODE_SUCCESS){
9850c738fd4SMilanka Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
9861a26de69SMilanka Ringwald                     hfp_emit_voice_recognition_state_event(hfp_connection, status);
9870c738fd4SMilanka Ringwald                     return 0;
988754910caSMilanka Ringwald                 }
9890b4debbfSMilanka Ringwald             }
9900c738fd4SMilanka Ringwald 
9910c738fd4SMilanka Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
9920c738fd4SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
9931a26de69SMilanka Ringwald             hfp_emit_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
994754910caSMilanka Ringwald             break;
995754910caSMilanka Ringwald 
996754910caSMilanka Ringwald         default:
997754910caSMilanka Ringwald             break;
998754910caSMilanka Ringwald     }
999754910caSMilanka Ringwald     return done;
1000754910caSMilanka Ringwald }
1001754910caSMilanka Ringwald 
1002a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
1003c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
1004c04cf7ffSMilanka Ringwald     if (sent) return 1;
1005aa4dd815SMatthias Ringwald 
1006a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
100745796ff1SMilanka Ringwald 
1008aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
1009485ac19eSMilanka Ringwald             hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
1010aa4dd815SMatthias Ringwald             return 1;
1011aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1012485ac19eSMilanka Ringwald             hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
1013aa4dd815SMatthias Ringwald             return 1;
1014aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1015a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
1016485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
1017aa4dd815SMatthias Ringwald             } else {
1018485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
10193deb3ec6SMatthias Ringwald             }
1020aa4dd815SMatthias Ringwald             return 1;
1021aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
1022485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1023aa4dd815SMatthias Ringwald             return 1;
10243deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
1025a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
1026a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
1027485ac19eSMilanka Ringwald                 hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
1028aa4dd815SMatthias Ringwald                 return 1;
10293deb3ec6SMatthias Ringwald             }
1030202c8a4cSMatthias Ringwald             break;
10313deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
1032485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1033ce263fc8SMatthias Ringwald             return 1;
10343deb3ec6SMatthias Ringwald         default:
10353deb3ec6SMatthias Ringwald             break;
10363deb3ec6SMatthias Ringwald     }
1037aa4dd815SMatthias Ringwald     return 0;
10383deb3ec6SMatthias Ringwald }
10393deb3ec6SMatthias Ringwald 
1040a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
1041505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
1042505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
10433deb3ec6SMatthias Ringwald 
1044a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
104552cbdd6dSMilanka Ringwald 
104652cbdd6dSMilanka Ringwald     if (hfp_connection->release_audio_connection){
104752cbdd6dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
104852cbdd6dSMilanka Ringwald         hfp_connection->release_audio_connection = 0;
104952cbdd6dSMilanka Ringwald         gap_disconnect(hfp_connection->sco_handle);
105052cbdd6dSMilanka Ringwald         return 1;
105152cbdd6dSMilanka Ringwald     }
1052aa4dd815SMatthias Ringwald 
105329cddf58SMatthias Ringwald     // accept incoming audio connection (codec negotiation is not used)
105429cddf58SMatthias Ringwald     if (hfp_connection->accept_sco){
105529cddf58SMatthias Ringwald         // notify about codec selection if not done already
105629cddf58SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
105729cddf58SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
105829cddf58SMatthias Ringwald         }
105929cddf58SMatthias Ringwald         //
106029cddf58SMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
106129cddf58SMatthias Ringwald         hfp_connection->accept_sco = 0;
106229cddf58SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
106329cddf58SMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
106429cddf58SMatthias Ringwald         return 1;
106529cddf58SMatthias Ringwald     }
106629cddf58SMatthias Ringwald 
1067aa4dd815SMatthias Ringwald     // run codecs exchange
1068c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
1069c04cf7ffSMilanka Ringwald     if (sent) return 1;
1070aa4dd815SMatthias Ringwald 
1071c04cf7ffSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
1072a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
1073a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1074a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
1075eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
1076aa4dd815SMatthias Ringwald         return 1;
1077aa4dd815SMatthias Ringwald     }
1078aa4dd815SMatthias Ringwald     return 0;
1079aa4dd815SMatthias Ringwald }
1080aa4dd815SMatthias Ringwald 
1081056efd45SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
1082056efd45SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1083056efd45SMatthias Ringwald     if (!indicator){
1084056efd45SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
1085056efd45SMatthias Ringwald         return;
1086056efd45SMatthias Ringwald     };
1087056efd45SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1088056efd45SMatthias Ringwald }
1089056efd45SMatthias Ringwald 
1090056efd45SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1091056efd45SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1092056efd45SMatthias Ringwald     if (!indicator){
1093056efd45SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
1094056efd45SMatthias Ringwald         return;
1095056efd45SMatthias Ringwald     };
1096056efd45SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1097056efd45SMatthias Ringwald }
1098056efd45SMatthias Ringwald 
1099aa4dd815SMatthias Ringwald //
1100fcd59412SMilanka Ringwald // transition implementations for hfp_ag_call_state_machine
1101aa4dd815SMatthias Ringwald //
1102aa4dd815SMatthias Ringwald 
11032ff1d7d7SMilanka Ringwald static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection){
1104aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
1105a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
1106d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1107aa4dd815SMatthias Ringwald     } else {
11089493406bSMilanka Ringwald         hfp_connection->call_state = HFP_CALL_INCOMING_RINGING;
1109aa4dd815SMatthias Ringwald     }
1110aa4dd815SMatthias Ringwald }
1111aa4dd815SMatthias Ringwald 
1112fcd59412SMilanka Ringwald static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection){
1113fcd59412SMilanka Ringwald     hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1114fcd59412SMilanka Ringwald }
1115fcd59412SMilanka Ringwald 
1116a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
1117a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
1118a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
1119aa4dd815SMatthias Ringwald }
1120aa4dd815SMatthias Ringwald 
1121a4f85bd2SMilanka Ringwald 
112231f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_during_active_one(void){
112331f55b91SMilanka Ringwald     btstack_linked_list_iterator_t it;
112431f55b91SMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
112531f55b91SMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
112631f55b91SMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
112731f55b91SMilanka Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
112831f55b91SMilanka Ringwald         if (hfp_connection->call_state != HFP_CALL_ACTIVE) continue;
112931f55b91SMilanka Ringwald 
113031f55b91SMilanka Ringwald         hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
113131f55b91SMilanka Ringwald         hfp_ag_run_for_context(hfp_connection);
113231f55b91SMilanka Ringwald     }
113331f55b91SMilanka Ringwald }
113431f55b91SMilanka Ringwald 
1135*3561686eSMatthias Ringwald // trigger RING and CLIP messages on connections that are ringing
1136a4f85bd2SMilanka Ringwald static void hfp_ag_ring_timeout_handler(btstack_timer_source_t * timer){
1137a4f85bd2SMilanka Ringwald     btstack_linked_list_iterator_t it;
1138a4f85bd2SMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1139a4f85bd2SMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1140a4f85bd2SMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1141a4f85bd2SMilanka Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1142a4f85bd2SMilanka Ringwald         if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING)
1143a4f85bd2SMilanka Ringwald             && (hfp_connection->call_state != HFP_CALL_OUTGOING_RINGING)) continue;
1144*3561686eSMatthias Ringwald 
1145*3561686eSMatthias Ringwald         // Queue RING on this connection
1146a4f85bd2SMilanka Ringwald         hfp_connection->ag_ring = 1;
1147*3561686eSMatthias Ringwald 
1148*3561686eSMatthias Ringwald         // HFP v1.8, 4.23 Calling Line Identification (CLI) Notification
1149*3561686eSMatthias Ringwald         // "If the calling subscriber number information is available from the network, the AG shall issue the
1150*3561686eSMatthias Ringwald         // +CLIP unsolicited result code just after every RING indication when the HF is alerted in an incoming call."
1151*3561686eSMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1152*3561686eSMatthias Ringwald 
1153*3561686eSMatthias Ringwald         // trigger next message
1154a4f85bd2SMilanka Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1155a4f85bd2SMilanka Ringwald     }
1156a4f85bd2SMilanka Ringwald 
1157*3561686eSMatthias Ringwald     btstack_run_loop_set_timer(timer, 2000); // 2 seconds timeout
1158*3561686eSMatthias Ringwald     btstack_run_loop_add_timer(timer);
1159a4f85bd2SMilanka Ringwald }
1160a4f85bd2SMilanka Ringwald 
1161a4f85bd2SMilanka Ringwald static void hfp_ag_ring_timeout_start(void){
1162a4f85bd2SMilanka Ringwald     btstack_run_loop_remove_timer(&hfp_ag_ring_timeout);
1163a4f85bd2SMilanka Ringwald     btstack_run_loop_set_timer_handler(&hfp_ag_ring_timeout, hfp_ag_ring_timeout_handler);
1164a4f85bd2SMilanka Ringwald     hfp_ag_ring_timeout_handler(&hfp_ag_ring_timeout);
1165a4f85bd2SMilanka Ringwald }
1166a4f85bd2SMilanka Ringwald 
1167a4f85bd2SMilanka Ringwald static void hfp_ag_ring_timeout_stop(void){
1168a4f85bd2SMilanka Ringwald     btstack_run_loop_remove_timer(&hfp_ag_ring_timeout);
1169a4f85bd2SMilanka Ringwald }
1170a4f85bd2SMilanka Ringwald 
1171a4f85bd2SMilanka Ringwald static void hfp_ag_start_ringing(void){
1172a4f85bd2SMilanka Ringwald     hfp_ag_ring_timeout_start();
1173a4f85bd2SMilanka Ringwald     hfp_ag_emit_general_simple_event(HFP_SUBEVENT_START_RINGING);
1174a4f85bd2SMilanka Ringwald }
1175a4f85bd2SMilanka Ringwald 
1176a4f85bd2SMilanka Ringwald static void hfp_ag_stop_ringing(void){
1177a4f85bd2SMilanka Ringwald     hfp_ag_ring_timeout_stop();
1178a4f85bd2SMilanka Ringwald     hfp_ag_emit_general_simple_event(HFP_SUBEVENT_STOP_RINGING);
1179a4f85bd2SMilanka Ringwald 
1180a4f85bd2SMilanka Ringwald     btstack_linked_list_iterator_t it;
1181a4f85bd2SMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1182a4f85bd2SMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1183a4f85bd2SMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1184a4f85bd2SMilanka Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1185a4f85bd2SMilanka Ringwald         if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) &&
1186a4f85bd2SMilanka Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1187a4f85bd2SMilanka Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1188a4f85bd2SMilanka Ringwald     }
1189a4f85bd2SMilanka Ringwald }
1190a4f85bd2SMilanka Ringwald 
119131f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_idle(void){
1192aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
1193aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
1194aa4dd815SMatthias Ringwald 
1195*3561686eSMatthias Ringwald     // update callsetup state in connections
1196665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1197665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1198665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1199a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
120066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
120131f55b91SMilanka Ringwald         if (hfp_connection->call_state != HFP_CALL_IDLE) continue;
120231f55b91SMilanka Ringwald 
1203a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1204a4f85bd2SMilanka Ringwald 
1205a4f85bd2SMilanka Ringwald         hfp_ag_hf_start_ringing_incoming(hfp_connection);
1206a4f85bd2SMilanka Ringwald 
1207f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1208aa4dd815SMatthias Ringwald     }
1209*3561686eSMatthias Ringwald 
1210*3561686eSMatthias Ringwald     // start local ringing - started after connection state has been updated
1211*3561686eSMatthias Ringwald     hfp_ag_start_ringing();
1212aa4dd815SMatthias Ringwald }
1213aa4dd815SMatthias Ringwald 
1214fe899794SMatthias Ringwald static void hfp_ag_trigger_outgoing_call(void){
1215fe899794SMatthias Ringwald     btstack_linked_list_iterator_t it;
1216fe899794SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1217fe899794SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1218fe899794SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1219fe899794SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1220fe899794SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1221fe899794SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
1222fe899794SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
1223fe899794SMatthias Ringwald         }
1224fe899794SMatthias Ringwald     }
1225fe899794SMatthias Ringwald }
12265a7033e2SMatthias Ringwald 
12275a7033e2SMatthias Ringwald static void hfp_ag_handle_outgoing_call_accepted(hfp_connection_t * hfp_connection){
12285a7033e2SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
12295a7033e2SMatthias Ringwald 
12305a7033e2SMatthias Ringwald     // trigger callsetup to be
12315a7033e2SMatthias Ringwald     int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
12325a7033e2SMatthias Ringwald     hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL);
12335a7033e2SMatthias Ringwald 
12345a7033e2SMatthias Ringwald     int indicator_index ;
12355a7033e2SMatthias Ringwald 
12365a7033e2SMatthias Ringwald     hfp_ag_set_callsetup_indicator();
12375a7033e2SMatthias Ringwald     indicator_index = get_ag_indicator_index_for_name("callsetup");
12385a7033e2SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
12395a7033e2SMatthias Ringwald 
12405a7033e2SMatthias Ringwald     // put current call on hold if active
12415a7033e2SMatthias Ringwald     if (put_call_on_hold){
12425a7033e2SMatthias Ringwald         log_info("AG putting current call on hold for new outgoing call");
12435a7033e2SMatthias Ringwald         hfp_ag_set_callheld_indicator();
12445a7033e2SMatthias Ringwald         indicator_index = get_ag_indicator_index_for_name("callheld");
12455a7033e2SMatthias Ringwald         hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
12465a7033e2SMatthias Ringwald     }
12475a7033e2SMatthias Ringwald 
12485a7033e2SMatthias Ringwald     // start audio if needed
12495a7033e2SMatthias Ringwald     hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
12505a7033e2SMatthias Ringwald }
1251fe899794SMatthias Ringwald 
12524a2e058fSMatthias Ringwald static void hfp_ag_transfer_indicator(const char * name){
12534a2e058fSMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
1254aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
1255aa4dd815SMatthias Ringwald 
1256665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1257665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1258665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1259a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
126066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1261a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1262a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1263f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1264aa4dd815SMatthias Ringwald     }
1265aa4dd815SMatthias Ringwald }
1266aa4dd815SMatthias Ringwald 
12674a2e058fSMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
12684a2e058fSMatthias Ringwald     hfp_ag_transfer_indicator("callsetup");
12694a2e058fSMatthias Ringwald }
12704a2e058fSMatthias Ringwald 
1271aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
12724a2e058fSMatthias Ringwald     hfp_ag_transfer_indicator("call");
1273aa4dd815SMatthias Ringwald }
1274aa4dd815SMatthias Ringwald 
1275aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
12764a2e058fSMatthias Ringwald     hfp_ag_transfer_indicator("callheld");
1277aa4dd815SMatthias Ringwald }
1278aa4dd815SMatthias Ringwald 
1279aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
1280aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1281aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1282aa4dd815SMatthias Ringwald 
1283a4f85bd2SMilanka Ringwald     hfp_ag_stop_ringing();
1284a4f85bd2SMilanka Ringwald 
1285665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1286665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1287665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1288a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
128966c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
12909493406bSMilanka Ringwald         if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) &&
1291505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1292aa4dd815SMatthias Ringwald 
1293a0ffb263SMatthias Ringwald         if (hfp_connection == source){
1294aa4dd815SMatthias Ringwald 
1295aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
1296a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
1297aa4dd815SMatthias Ringwald             } else {
1298a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
1299d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1300aa4dd815SMatthias Ringwald             }
1301aa4dd815SMatthias Ringwald 
1302a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1303a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1304aa4dd815SMatthias Ringwald 
1305aa4dd815SMatthias Ringwald         } else {
1306a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1307aa4dd815SMatthias Ringwald         }
1308f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1309aa4dd815SMatthias Ringwald     }
1310aa4dd815SMatthias Ringwald }
1311aa4dd815SMatthias Ringwald 
1312aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
1313aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1314aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1315aa4dd815SMatthias Ringwald 
1316a4f85bd2SMilanka Ringwald     hfp_ag_stop_ringing();
1317a4f85bd2SMilanka Ringwald 
1318665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1319665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1320665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1321a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
132266c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
13239493406bSMilanka Ringwald         if (hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) continue;
1324aa4dd815SMatthias Ringwald 
1325a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
1326aa4dd815SMatthias Ringwald 
1327a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1328a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1329aa4dd815SMatthias Ringwald 
1330f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1331aa4dd815SMatthias Ringwald         break;  // only single
1332aa4dd815SMatthias Ringwald     }
1333aa4dd815SMatthias Ringwald }
1334aa4dd815SMatthias Ringwald 
1335aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
1336aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1337a4f85bd2SMilanka Ringwald 
1338a4f85bd2SMilanka Ringwald     hfp_ag_stop_ringing();
1339a4f85bd2SMilanka Ringwald 
1340665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1341665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1342665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1343665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
134466c5995fSMatthias Ringwald         if (connection->local_role != HFP_ROLE_AG) continue;
13459493406bSMilanka Ringwald         if ((connection->call_state != HFP_CALL_INCOMING_RINGING) &&
1346505f1c30SMatthias Ringwald             (connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1347095a9f2bSMilanka Ringwald 
1348aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
1349095a9f2bSMilanka Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1350095a9f2bSMilanka Ringwald 
1351095a9f2bSMilanka Ringwald         if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
1352095a9f2bSMilanka Ringwald             connection->release_audio_connection = 1;
1353095a9f2bSMilanka Ringwald         }
1354095a9f2bSMilanka Ringwald         hfp_emit_simple_event(connection, HFP_SUBEVENT_CALL_TERMINATED);
1355f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(connection);
1356095a9f2bSMilanka Ringwald         rfcomm_request_can_send_now_event(connection->rfcomm_cid);
1357aa4dd815SMatthias Ringwald     }
1358aa4dd815SMatthias Ringwald }
1359aa4dd815SMatthias Ringwald 
1360aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1361aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1362aa4dd815SMatthias Ringwald 
1363a4f85bd2SMilanka Ringwald     // no ringing during call
1364a4f85bd2SMilanka Ringwald 
1365665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1366665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1367665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1368a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
136966c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1370a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1371fb75b483SMilanka Ringwald 
1372a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1373a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1374095a9f2bSMilanka Ringwald 
13756d78145cSMatthias Ringwald         if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
1376a0ffb263SMatthias Ringwald             hfp_connection->release_audio_connection = 1;
13776d78145cSMatthias Ringwald         }
1378095a9f2bSMilanka Ringwald 
1379fb75b483SMilanka Ringwald         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1380f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1381a13ff7d1SMilanka Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1382aa4dd815SMatthias Ringwald     }
1383aa4dd815SMatthias Ringwald }
1384aa4dd815SMatthias Ringwald 
13850cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1386aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1387aa4dd815SMatthias Ringwald     if (!indicator){
1388aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
138945718b6fSMatthias Ringwald         return;
1390aa4dd815SMatthias Ringwald     };
1391d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1392aa4dd815SMatthias Ringwald }
1393aa4dd815SMatthias Ringwald 
1394aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1395665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1396665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1397665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1398a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
139966c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1400a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1401aa4dd815SMatthias Ringwald     }
1402aa4dd815SMatthias Ringwald     return NULL;
1403aa4dd815SMatthias Ringwald }
1404aa4dd815SMatthias Ringwald 
1405a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1406665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1407665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1408665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1409a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
141066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1411a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1412ce263fc8SMatthias Ringwald     }
1413ce263fc8SMatthias Ringwald }
1414ce263fc8SMatthias Ringwald 
1415a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1416aa4dd815SMatthias Ringwald     int indicator_index;
1417a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1418aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1419a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1420a4f85bd2SMilanka Ringwald 
1421a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
14229493406bSMilanka Ringwald             hfp_connection->call_state = HFP_CALL_INCOMING_RINGING;
1423aa4dd815SMatthias Ringwald             break;
1424aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1425a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1426a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1427a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1428aa4dd815SMatthias Ringwald             break;
1429aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1430a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1431a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1432aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1433a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1434aa4dd815SMatthias Ringwald             break;
1435aa4dd815SMatthias Ringwald         default:
1436aa4dd815SMatthias Ringwald             break;
1437aa4dd815SMatthias Ringwald     }
1438aa4dd815SMatthias Ringwald     return 0;
1439aa4dd815SMatthias Ringwald }
144039426febSMatthias Ringwald 
144139426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below
14429f9621cfSMatthias Ringwald static void hfp_ag_handle_reject_outgoing_call(void){
144339426febSMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
144439426febSMatthias Ringwald     if (!hfp_connection){
144539426febSMatthias Ringwald         log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
144639426febSMatthias Ringwald         return;
144739426febSMatthias Ringwald     }
144839426febSMatthias Ringwald 
1449f8737b81SMatthias Ringwald     hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL);
145039426febSMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
145139426febSMatthias Ringwald     hfp_connection->send_error = 1;
145239426febSMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
145339426febSMatthias Ringwald }
145439426febSMatthias Ringwald 
1455a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1456a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1457d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1458d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1459d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
146074386ee0SMatthias Ringwald 
1461aa4dd815SMatthias Ringwald     switch (event){
1462aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1463d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1464aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1465d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
146612cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1467f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL);
1468d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
146931f55b91SMilanka Ringwald                             hfp_ag_trigger_incoming_call_idle();
147060ebb071SMilanka Ringwald                             log_info("AG rings");
1471aa4dd815SMatthias Ringwald                             break;
1472aa4dd815SMatthias Ringwald                         default:
14733deb3ec6SMatthias Ringwald                             break;
14743deb3ec6SMatthias Ringwald                     }
14753deb3ec6SMatthias Ringwald                     break;
1476aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1477d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
147812cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1479f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL);
1480d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
148131f55b91SMilanka Ringwald                             hfp_ag_trigger_incoming_call_during_active_one();
148260ebb071SMilanka Ringwald                             log_info("AG call waiting");
1483aa4dd815SMatthias Ringwald                             break;
1484aa4dd815SMatthias Ringwald                         default:
14853deb3ec6SMatthias Ringwald                             break;
14863deb3ec6SMatthias Ringwald                     }
14873deb3ec6SMatthias Ringwald                     break;
14887bbeb3adSMilanka Ringwald                 default:
14897bbeb3adSMilanka Ringwald                     break;
1490aa4dd815SMatthias Ringwald             }
1491aa4dd815SMatthias Ringwald             break;
1492aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1493d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1494aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1495d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
149612cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1497f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL);
1498d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1499d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1500aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
150160ebb071SMilanka Ringwald                             log_info("AG answers call, accept call by GSM");
1502aa4dd815SMatthias Ringwald                             break;
1503aa4dd815SMatthias Ringwald                         default:
15043deb3ec6SMatthias Ringwald                             break;
15053deb3ec6SMatthias Ringwald                     }
1506aa4dd815SMatthias Ringwald                     break;
1507aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1508d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1509aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
151060ebb071SMilanka Ringwald                             log_info("AG: current call is placed on hold, incoming call gets active");
1511f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL);
1512d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1513d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1514ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1515ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1516aa4dd815SMatthias Ringwald                             break;
1517aa4dd815SMatthias Ringwald                         default:
1518aa4dd815SMatthias Ringwald                             break;
1519aa4dd815SMatthias Ringwald                     }
1520aa4dd815SMatthias Ringwald                     break;
15217bbeb3adSMilanka Ringwald                 default:
15227bbeb3adSMilanka Ringwald                     break;
1523aa4dd815SMatthias Ringwald             }
1524aa4dd815SMatthias Ringwald             break;
1525ce263fc8SMatthias Ringwald 
1526ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1527d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1528ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1529d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1530ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
153160ebb071SMilanka Ringwald                             log_info("AG: joining held call with active call");
1532f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL);
1533d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1534ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1535ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1536ce263fc8SMatthias Ringwald                             break;
1537ce263fc8SMatthias Ringwald                         default:
1538ce263fc8SMatthias Ringwald                             break;
1539ce263fc8SMatthias Ringwald                     }
1540ce263fc8SMatthias Ringwald                     break;
1541ce263fc8SMatthias Ringwald                 default:
1542ce263fc8SMatthias Ringwald                     break;
1543ce263fc8SMatthias Ringwald             }
1544ce263fc8SMatthias Ringwald             break;
1545ce263fc8SMatthias Ringwald 
1546aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1547d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1548aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1549d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
155012cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1551f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL);
1552d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1553d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1554a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
15556323f788SMatthias Ringwald                             hfp_connection->ok_pending = 1;
155660ebb071SMilanka Ringwald                             log_info("HF answers call, accept call by GSM");
1557ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1558aa4dd815SMatthias Ringwald                             break;
1559aa4dd815SMatthias Ringwald                         default:
1560aa4dd815SMatthias Ringwald                             break;
1561aa4dd815SMatthias Ringwald                     }
15623deb3ec6SMatthias Ringwald                     break;
15633deb3ec6SMatthias Ringwald                 default:
15643deb3ec6SMatthias Ringwald                     break;
15653deb3ec6SMatthias Ringwald             }
15663deb3ec6SMatthias Ringwald             break;
15673deb3ec6SMatthias Ringwald 
1568ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1569d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1570ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1571d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
157212cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1573f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL);
1574ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1575ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1576a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1577ac6f828eSMilanka Ringwald                             // as with regular call
1578d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1579d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1580ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
158160ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by AG");
1582ce263fc8SMatthias Ringwald                             break;
1583ce263fc8SMatthias Ringwald                         default:
1584ce263fc8SMatthias Ringwald                             break;
1585ce263fc8SMatthias Ringwald                     }
1586ce263fc8SMatthias Ringwald                     break;
1587ce263fc8SMatthias Ringwald                 default:
1588ce263fc8SMatthias Ringwald                     break;
1589ce263fc8SMatthias Ringwald             }
1590ce263fc8SMatthias Ringwald             break;
1591ce263fc8SMatthias Ringwald 
1592ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1593d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1594ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1595d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
159612cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1597f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL);
1598ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1599ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1600a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1601ac6f828eSMilanka Ringwald                             // as with regular call
1602d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1603d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1604a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
160560ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by HF");
1606ce263fc8SMatthias Ringwald                             break;
1607ce263fc8SMatthias Ringwald                         default:
1608ce263fc8SMatthias Ringwald                             break;
1609ce263fc8SMatthias Ringwald                     }
1610ce263fc8SMatthias Ringwald                     break;
1611ce263fc8SMatthias Ringwald                 default:
1612ce263fc8SMatthias Ringwald                     break;
1613ce263fc8SMatthias Ringwald             }
1614ce263fc8SMatthias Ringwald             break;
1615ce263fc8SMatthias Ringwald 
1616ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1617ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1618ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1619ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1620f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL);
1621a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1622ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1623a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
162460ebb071SMilanka Ringwald             log_info("Held Call accepted and active");
1625ce263fc8SMatthias Ringwald             break;
1626ce263fc8SMatthias Ringwald 
1627ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1628ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1629ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1630ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1631f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL);
1632a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1633ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1634a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1635ce263fc8SMatthias Ringwald             // from terminate by ag
1636d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1637ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1638ce263fc8SMatthias Ringwald             break;
1639ce263fc8SMatthias Ringwald 
1640aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1641d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1642aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1643d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
164412cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1645f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
1646d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1647aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1648aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
164960ebb071SMilanka Ringwald                             log_info("HF Rejected Incoming call, AG terminate call");
1650aa4dd815SMatthias Ringwald                             break;
1651aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
165212cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1653f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
1654d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1655aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
165660ebb071SMilanka Ringwald                             log_info("AG terminate outgoing call process");
1657202c8a4cSMatthias Ringwald                             break;
1658aa4dd815SMatthias Ringwald                         default:
1659aa4dd815SMatthias Ringwald                             break;
1660aa4dd815SMatthias Ringwald                     }
1661aa4dd815SMatthias Ringwald                     break;
166212cbbeeeSMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1663f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
16641ec112deSMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1665d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
16661ec112deSMatthias Ringwald                     hfp_ag_trigger_terminate_call();
166760ebb071SMilanka Ringwald                     log_info("AG terminate call");
1668aa4dd815SMatthias Ringwald                     break;
16697bbeb3adSMilanka Ringwald                 default:
16707bbeb3adSMilanka Ringwald                     break;
1671aa4dd815SMatthias Ringwald             }
1672aa4dd815SMatthias Ringwald             break;
16733deb3ec6SMatthias Ringwald 
1674aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1675d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1676aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1677d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
167812cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1679f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL);
1680d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1681aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
168260ebb071SMilanka Ringwald                             log_info("AG Rejected Incoming call, AG terminate call");
1683aa4dd815SMatthias Ringwald                             break;
1684aa4dd815SMatthias Ringwald                         default:
1685aa4dd815SMatthias Ringwald                             break;
16863deb3ec6SMatthias Ringwald                     }
1687202c8a4cSMatthias Ringwald                     break;
168812cbbeeeSMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1689f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL);
1690d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1691d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1692aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
169360ebb071SMilanka Ringwald                     log_info("AG terminate call");
1694aa4dd815SMatthias Ringwald                     break;
1695aa4dd815SMatthias Ringwald                 default:
16963deb3ec6SMatthias Ringwald                     break;
16973deb3ec6SMatthias Ringwald             }
16983deb3ec6SMatthias Ringwald             break;
1699aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1700d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1701aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1702d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1703aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1704aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1705fb75b483SMilanka Ringwald                             hfp_ag_stop_ringing();
1706fb75b483SMilanka Ringwald                             break;
1707fb75b483SMilanka Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1708fb75b483SMilanka Ringwald                             log_info("Outgoing call interrupted\n");
1709aa4dd815SMatthias Ringwald                             break;
1710aa4dd815SMatthias Ringwald                         default:
17113deb3ec6SMatthias Ringwald                             break;
17123deb3ec6SMatthias Ringwald                     }
1713f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1714d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1715aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
171610c236bfSMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1717aa4dd815SMatthias Ringwald                     break;
1718aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1719ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1720f8737b81SMatthias Ringwald                         hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1721ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1722a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1723ce263fc8SMatthias Ringwald                     }
1724f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1725d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1726d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1727aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
172860ebb071SMilanka Ringwald                     log_info("AG notify call dropped");
1729aa4dd815SMatthias Ringwald                     break;
1730aa4dd815SMatthias Ringwald                 default:
1731aa4dd815SMatthias Ringwald                     break;
1732aa4dd815SMatthias Ringwald             }
1733aa4dd815SMatthias Ringwald             break;
1734aa4dd815SMatthias Ringwald 
17359ff73f41SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED_BY_HF:
1736d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1737d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1738a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1739f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1740d210d9c4SMatthias Ringwald                 break;
1741d210d9c4SMatthias Ringwald             }
1742fe899794SMatthias Ringwald 
1743fe899794SMatthias Ringwald             // note: number not used currently
17449ff73f41SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, 0, 0, (const char *) &hfp_connection->line_buffer[3]);
17459cae807eSMatthias Ringwald 
1746a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
174774386ee0SMatthias Ringwald 
1748ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1749aa4dd815SMatthias Ringwald             break;
1750aa4dd815SMatthias Ringwald 
1751fe899794SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED_BY_AG:
1752fe899794SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1753fe899794SMatthias Ringwald             if (!hfp_gsm_call_possible()) {
1754fe899794SMatthias Ringwald                 // TODO: no error for user
1755fe899794SMatthias Ringwald                 break;
1756fe899794SMatthias Ringwald             }
1757fe899794SMatthias Ringwald             switch (hfp_gsm_call_status()) {
1758fe899794SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1759fe899794SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()) {
1760fe899794SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1761fe899794SMatthias Ringwald                             // note: number not used currently
1762fe899794SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, 0, 0, "1234567");
1763fe899794SMatthias Ringwald                             // init call state for all connections
1764fe899794SMatthias Ringwald                             hfp_ag_trigger_outgoing_call();
1765fe899794SMatthias Ringwald                             // get first one and accept call
1766fe899794SMatthias Ringwald                             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1767fe899794SMatthias Ringwald                             if (hfp_connection) {
1768fe899794SMatthias Ringwald                                 hfp_ag_handle_outgoing_call_accepted(hfp_connection);
1769fe899794SMatthias Ringwald                             }
1770fe899794SMatthias Ringwald                             break;
1771fe899794SMatthias Ringwald                         default:
1772fe899794SMatthias Ringwald                             // TODO: no error for user
1773fe899794SMatthias Ringwald                             break;
1774fe899794SMatthias Ringwald                     }
1775fe899794SMatthias Ringwald                     break;
1776fe899794SMatthias Ringwald                 default:
1777fe899794SMatthias Ringwald                     // TODO: no error for user
1778fe899794SMatthias Ringwald                     break;
1779fe899794SMatthias Ringwald             }
1780fe899794SMatthias Ringwald             break;
17819cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1782d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1783d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1784a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1785f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1786d210d9c4SMatthias Ringwald                 break;
1787d210d9c4SMatthias Ringwald             }
1788d210d9c4SMatthias Ringwald 
1789f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL);
1790a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
179174386ee0SMatthias Ringwald 
179260ebb071SMilanka Ringwald             log_info("Redial last number");
17939cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1794aa4dd815SMatthias Ringwald 
17959cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
179660ebb071SMilanka Ringwald                 log_info("Last number exists: accept call");
1797ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
17989cae807eSMatthias Ringwald             } else {
179960ebb071SMilanka Ringwald                 log_info("log_infoLast number missing: reject call");
18009f9621cfSMatthias Ringwald                 hfp_ag_handle_reject_outgoing_call();
18019cae807eSMatthias Ringwald             }
18029cae807eSMatthias Ringwald             break;
18039cae807eSMatthias Ringwald         }
1804aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
18059f9621cfSMatthias Ringwald             hfp_ag_handle_reject_outgoing_call();
1806aa4dd815SMatthias Ringwald             break;
1807aa4dd815SMatthias Ringwald 
1808d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1809a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1810a0ffb263SMatthias Ringwald             if (!hfp_connection){
1811a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1812aa4dd815SMatthias Ringwald                 break;
1813aa4dd815SMatthias Ringwald             }
1814aa4dd815SMatthias Ringwald 
1815a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
18165a7033e2SMatthias Ringwald             hfp_ag_handle_outgoing_call_accepted(hfp_connection);
1817aa4dd815SMatthias Ringwald             break;
1818d210d9c4SMatthias Ringwald         }
1819aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1820a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1821a0ffb263SMatthias Ringwald             if (!hfp_connection){
1822a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1823aa4dd815SMatthias Ringwald                 break;
1824aa4dd815SMatthias Ringwald             }
1825d210d9c4SMatthias Ringwald 
1826f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL);
1827a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1828d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1829aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1830fcd59412SMilanka Ringwald             hfp_ag_hf_start_ringing_outgoing(hfp_connection);
1831aa4dd815SMatthias Ringwald             break;
1832aa4dd815SMatthias Ringwald 
1833d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1834aa4dd815SMatthias Ringwald             // get outgoing call
1835a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1836a0ffb263SMatthias Ringwald             if (!hfp_connection){
1837a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1838aa4dd815SMatthias Ringwald             }
1839a0ffb263SMatthias Ringwald             if (!hfp_connection){
1840a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1841aa4dd815SMatthias Ringwald                 break;
1842aa4dd815SMatthias Ringwald             }
1843d210d9c4SMatthias Ringwald 
1844addc49c4SMilanka Ringwald             bool callheld_status_call_on_hold_and_no_active_calls = hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS;
1845f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL);
1846a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1847d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1848d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1849aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1850aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1851addc49c4SMilanka Ringwald             if (callheld_status_call_on_hold_and_no_active_calls){
1852d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1853aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
18543deb3ec6SMatthias Ringwald             }
1855addc49c4SMilanka Ringwald             hfp_ag_hf_stop_ringing(hfp_connection);
1856674ebed5SMilanka Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
18573deb3ec6SMatthias Ringwald             break;
1858d210d9c4SMatthias Ringwald         }
1859d210d9c4SMatthias Ringwald 
186012cbbeeeSMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1861f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL);
1862d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1863a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1864a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
186560ebb071SMilanka Ringwald             log_info("AG: Call Waiting, User Busy");
1866d210d9c4SMatthias Ringwald             break;
1867d210d9c4SMatthias Ringwald 
1868d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1869d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1870d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1871d210d9c4SMatthias Ringwald 
1872d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1873d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1874f8737b81SMatthias Ringwald                 hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index,
1875f8737b81SMatthias Ringwald                                 0, NULL);
1876d0c20769SMatthias Ringwald 
1877d0c20769SMatthias Ringwald             }
1878d0c20769SMatthias Ringwald 
1879d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
188060ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Accept new call");
1881d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1882a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1883d210d9c4SMatthias Ringwald             } else {
188460ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Resume held call");
1885d210d9c4SMatthias Ringwald             }
1886d210d9c4SMatthias Ringwald             if (call_held){
1887d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1888a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1889d210d9c4SMatthias Ringwald             }
1890d0c20769SMatthias Ringwald 
1891a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1892d210d9c4SMatthias Ringwald             break;
1893d210d9c4SMatthias Ringwald         }
1894d0c20769SMatthias Ringwald 
1895d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1896d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1897d210d9c4SMatthias Ringwald             // only update if callsetup changed
1898d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1899f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0,
1900f8737b81SMatthias Ringwald                             NULL);
1901d0c20769SMatthias Ringwald 
1902d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
190360ebb071SMilanka Ringwald                 log_info("AG: Call on Hold, Accept new call");
1904d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1905a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1906d210d9c4SMatthias Ringwald             } else {
190760ebb071SMilanka Ringwald                 log_info("AG: Swap calls");
1908d210d9c4SMatthias Ringwald             }
1909d0c20769SMatthias Ringwald 
1910d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1911d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1912a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1913a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1914d210d9c4SMatthias Ringwald             break;
1915d210d9c4SMatthias Ringwald         }
1916d0c20769SMatthias Ringwald 
1917d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1918d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1919d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
192060ebb071SMilanka Ringwald                 log_info("AG: Join 3-way-call");
1921f8737b81SMatthias Ringwald                 hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL);
1922d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1923a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1924ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1925d210d9c4SMatthias Ringwald             }
1926a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1927d210d9c4SMatthias Ringwald             break;
1928d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1929d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1930f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL);
193160ebb071SMilanka Ringwald             log_info("AG: Transfer call -> Connect two calls and disconnect");
1932d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1933d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1934a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1935a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1936a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1937d210d9c4SMatthias Ringwald             break;
1938ce263fc8SMatthias Ringwald 
19393deb3ec6SMatthias Ringwald         default:
19403deb3ec6SMatthias Ringwald             break;
19413deb3ec6SMatthias Ringwald     }
1942d210d9c4SMatthias Ringwald 
1943d0c20769SMatthias Ringwald 
19443deb3ec6SMatthias Ringwald }
19453deb3ec6SMatthias Ringwald 
19469cae807eSMatthias Ringwald 
1947a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
19489cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
19499cae807eSMatthias Ringwald     if (!active_call) return;
19509cae807eSMatthias Ringwald 
19519cae807eSMatthias Ringwald     int idx = active_call->index;
19529cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
19539cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
19549cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
19559cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
19569cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
19579cae807eSMatthias Ringwald     char * number = active_call->clip_number;
19589cae807eSMatthias Ringwald 
19599cae807eSMatthias Ringwald     char buffer[100];
19609cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
19619cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
19629cae807eSMatthias Ringwald     if (number){
19631cc1d9e9SMilanka Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
19649cae807eSMatthias Ringwald     }
19659cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
196660ebb071SMilanka 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,
19679cae807eSMatthias Ringwald        mode, mpty, type, number);
1968a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
19699cae807eSMatthias Ringwald }
19709cae807eSMatthias Ringwald 
1971febc14f5SMatthias Ringwald // sends pending command, returns if command was sent
1972febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){
1973a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1974a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1975a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1976a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1977a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
1978febc14f5SMatthias Ringwald             return 1;
19799cae807eSMatthias Ringwald         } else {
1980febc14f5SMatthias 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
1981a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1982a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1983a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
19849cae807eSMatthias Ringwald         }
1985ce263fc8SMatthias Ringwald     }
1986ce263fc8SMatthias Ringwald 
1987a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1988a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1989a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1990febc14f5SMatthias Ringwald         return 1;
1991aa4dd815SMatthias Ringwald     }
1992aa4dd815SMatthias Ringwald 
1993a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1994a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1995a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1996a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1997485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1998febc14f5SMatthias Ringwald         return 1;
1999a0ffb263SMatthias Ringwald     }
2000a0ffb263SMatthias Ringwald 
2001a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
2002a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
2003a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2004485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
2005febc14f5SMatthias Ringwald         return 1;
2006aa4dd815SMatthias Ringwald     }
2007aa4dd815SMatthias Ringwald 
2008ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
2009a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
2010a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
2011a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
2012485ac19eSMilanka Ringwald         hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
2013febc14f5SMatthias Ringwald         return 1;
2014ce263fc8SMatthias Ringwald     }
2015ce263fc8SMatthias Ringwald 
2016a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
2017a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
2018a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2019485ac19eSMilanka Ringwald         hfp_ag_send_ok(hfp_connection->rfcomm_cid);
2020febc14f5SMatthias Ringwald         return 1;
2021ce263fc8SMatthias Ringwald     }
2022ce263fc8SMatthias Ringwald 
2023aa4dd815SMatthias Ringwald     // update AG indicators
2024a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
2025aa4dd815SMatthias Ringwald         int i;
2026a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
2027a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
2028a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
2029a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
2030ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
2031ce263fc8SMatthias Ringwald                     break;
2032ce263fc8SMatthias Ringwald                 }
2033485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
2034febc14f5SMatthias Ringwald                 return 1;
2035aa4dd815SMatthias Ringwald             }
2036aa4dd815SMatthias Ringwald         }
2037aa4dd815SMatthias Ringwald     }
2038aa4dd815SMatthias Ringwald 
2039245852b7SMilanka Ringwald     if (hfp_connection->ag_send_no_carrier){
2040245852b7SMilanka Ringwald         hfp_connection->ag_send_no_carrier = false;
2041245852b7SMilanka Ringwald         hfp_ag_send_no_carrier(hfp_connection->rfcomm_cid);
2042245852b7SMilanka Ringwald         return 1;
2043245852b7SMilanka Ringwald     }
2044245852b7SMilanka Ringwald 
2045a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
2046a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
2047a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
20480d38e7e1SMilanka Ringwald         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
2049485ac19eSMilanka Ringwald         hfp_ag_send_ring(hfp_connection->rfcomm_cid);
2050febc14f5SMatthias Ringwald         return 1;
2051aa4dd815SMatthias Ringwald     }
2052aa4dd815SMatthias Ringwald 
2053a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
2054a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
2055a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2056a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
2057febc14f5SMatthias Ringwald         return 1;
2058aa4dd815SMatthias Ringwald     }
2059aa4dd815SMatthias Ringwald 
2060a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
2061a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
2062a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2063a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
2064a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
2065febc14f5SMatthias Ringwald         return 1;
2066aa4dd815SMatthias Ringwald     }
2067aa4dd815SMatthias Ringwald 
2068a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
2069aeb0f0feSMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < hfp_ag_subscriber_numbers_count){
2070aeb0f0feSMatthias Ringwald             hfp_phone_number_t phone = hfp_ag_subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
2071a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
2072ce263fc8SMatthias Ringwald         } else {
2073a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
2074a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
2075485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
2076ce263fc8SMatthias Ringwald         }
2077a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2078febc14f5SMatthias Ringwald         return 1;
2079ce263fc8SMatthias Ringwald     }
2080ce263fc8SMatthias Ringwald 
2081a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
2082a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
2083a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2084485ac19eSMilanka Ringwald         hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
2085febc14f5SMatthias Ringwald         return 1;
2086aa4dd815SMatthias Ringwald     }
2087aa4dd815SMatthias Ringwald 
2088a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
2089a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
2090a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2091485ac19eSMilanka Ringwald         hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
2092febc14f5SMatthias Ringwald         return 1;
20933deb3ec6SMatthias Ringwald     }
20943deb3ec6SMatthias Ringwald 
2095a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
2096a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
2097485ac19eSMilanka Ringwald         hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
2098febc14f5SMatthias Ringwald         return 1;
2099febc14f5SMatthias Ringwald     }
2100febc14f5SMatthias Ringwald 
2101febc14f5SMatthias Ringwald     return 0;
2102febc14f5SMatthias Ringwald }
2103febc14f5SMatthias Ringwald 
2104febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){
2105febc14f5SMatthias Ringwald 
210676cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
210776cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_AG);
2108febc14f5SMatthias Ringwald 
210976cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
211076cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
2111ce263fc8SMatthias Ringwald 
21123721a235SMatthias Ringwald     // assert command could be sent
21133721a235SMatthias Ringwald     if (hci_can_send_command_packet_now() == 0) return;
21143721a235SMatthias Ringwald 
2115f9f3ba28SMilanka Ringwald     if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){
2116f9f3ba28SMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
2117f9f3ba28SMilanka Ringwald         hfp_connection->release_audio_connection = 0;
2118f9f3ba28SMilanka Ringwald         gap_disconnect(hfp_connection->sco_handle);
2119f9f3ba28SMilanka Ringwald         return;
2120f9f3ba28SMilanka Ringwald     }
2121f9f3ba28SMilanka Ringwald 
21223721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
21233721a235SMatthias Ringwald     // WBS Disassociate
21243721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
21253721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
21263721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
21273721a235SMatthias Ringwald         return;
21283721a235SMatthias Ringwald     }
21293721a235SMatthias Ringwald     // Write Codec Config
21303721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
21313721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
21323721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
21333721a235SMatthias Ringwald         return;
21343721a235SMatthias Ringwald     }
21353721a235SMatthias Ringwald     // WBS Associate
21363721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
21373721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
21383721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
21393721a235SMatthias Ringwald         return;
21403721a235SMatthias Ringwald     }
21413721a235SMatthias Ringwald #endif
2142689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
2143689d4323SMatthias Ringwald     // Enable WBS
2144689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
2145689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
2146689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
2147689d4323SMatthias Ringwald         return;
2148689d4323SMatthias Ringwald     }
2149689d4323SMatthias Ringwald     // Write I2S/PCM params
2150689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
2151689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
2152689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
2153689d4323SMatthias Ringwald         return;
2154689d4323SMatthias Ringwald     }
2155689d4323SMatthias Ringwald     // Disable WBS
2156689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
2157689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
2158689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
2159689d4323SMatthias Ringwald         return;
2160689d4323SMatthias Ringwald     }
2161689d4323SMatthias Ringwald #endif
216248e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
216348e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
216448e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
216548e6eeeeSMatthias Ringwald         return;
216648e6eeeeSMatthias Ringwald     }
216748e6eeeeSMatthias Ringwald #endif
21683721a235SMatthias Ringwald 
2169febc14f5SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
2170febc14f5SMatthias Ringwald         log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
2171febc14f5SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
2172febc14f5SMatthias Ringwald         return;
2173febc14f5SMatthias Ringwald     }
2174febc14f5SMatthias Ringwald 
2175febc14f5SMatthias Ringwald     int cmd_sent = hfp_ag_send_commands(hfp_connection);
2176febc14f5SMatthias Ringwald 
2177febc14f5SMatthias Ringwald     if (!cmd_sent){
2178febc14f5SMatthias Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
2179febc14f5SMatthias Ringwald     }
2180febc14f5SMatthias Ringwald 
2181c04cf7ffSMilanka Ringwald     if (!cmd_sent){
2182c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
21833deb3ec6SMatthias Ringwald     }
21843deb3ec6SMatthias Ringwald 
2185c04cf7ffSMilanka Ringwald     if (!cmd_sent){
2186c04cf7ffSMilanka Ringwald         cmd_sent = call_setup_state_machine(hfp_connection);
2187aa4dd815SMatthias Ringwald     }
2188aa4dd815SMatthias Ringwald 
2189b956fff3SMatthias Ringwald     // trigger codec exchange (must be before hfp_ag_run_for_audio_connection)
2190d0a0eceeSMatthias Ringwald     if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && hfp_connection->trigger_codec_exchange){
2191d0a0eceeSMatthias Ringwald         switch (hfp_connection->codecs_state){
2192d0a0eceeSMatthias Ringwald             case HFP_CODECS_IDLE:
2193d0a0eceeSMatthias Ringwald             case HFP_CODECS_RECEIVED_LIST:
2194d0a0eceeSMatthias Ringwald             case HFP_CODECS_AG_RESEND_COMMON_CODEC:
2195d0a0eceeSMatthias Ringwald             case HFP_CODECS_ERROR:
2196d0a0eceeSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 0;
2197d0a0eceeSMatthias Ringwald                 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
21983deb3ec6SMatthias Ringwald                 break;
21993deb3ec6SMatthias Ringwald             default:
22003deb3ec6SMatthias Ringwald                 break;
22013deb3ec6SMatthias Ringwald         }
22023deb3ec6SMatthias Ringwald     }
2203c04cf7ffSMilanka Ringwald 
2204b956fff3SMatthias Ringwald     if (!cmd_sent){
2205b956fff3SMatthias Ringwald         cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
2206b956fff3SMatthias Ringwald     }
2207b956fff3SMatthias Ringwald 
2208c95b5b3cSMilanka Ringwald     if (!cmd_sent){
2209c95b5b3cSMilanka Ringwald         cmd_sent = hfp_ag_voice_recognition_state_machine(hfp_connection);
2210c95b5b3cSMilanka Ringwald     }
2211b956fff3SMatthias Ringwald 
2212d0a0eceeSMatthias Ringwald     // disconnect
2213d0a0eceeSMatthias Ringwald     if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && (hfp_connection->state == HFP_W2_DISCONNECT_RFCOMM)){
2214d0a0eceeSMatthias Ringwald         hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
2215d0a0eceeSMatthias Ringwald         rfcomm_disconnect(hfp_connection->rfcomm_cid);
2216d0a0eceeSMatthias Ringwald     }
2217d0a0eceeSMatthias Ringwald 
2218c04cf7ffSMilanka Ringwald     if (cmd_sent){
2219a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2220473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
2221473ac565SMatthias Ringwald     }
22223deb3ec6SMatthias Ringwald }
2223d68dcce1SMatthias Ringwald 
2224e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
2225c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
2226e0d09929SMatthias Ringwald }
2227e0d09929SMatthias Ringwald 
2228fdf18f86SMilanka Ringwald static void hfp_ag_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
22298a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
2230fdf18f86SMilanka Ringwald     if (size < 1) return;
2231347d46c8SMilanka Ringwald     uint8_t status = ERROR_CODE_SUCCESS;
22321e35c04dSMatthias Ringwald 
2233186dd3d2SMatthias Ringwald     hfp_log_rfcomm_message("HFP_AG_RX", packet, size);
2234e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
2235e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
2236e43d1938SMatthias Ringwald #endif
22371e35c04dSMatthias Ringwald 
22388a46ec40SMatthias Ringwald     // process messages byte-wise
22393deb3ec6SMatthias Ringwald     int pos;
22403deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
2241a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
2242186dd3d2SMatthias Ringwald 
2243e0d09929SMatthias Ringwald         // parse until end of line
2244e0d09929SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
2245e0d09929SMatthias Ringwald 
2246186dd3d2SMatthias Ringwald         hfp_generic_status_indicator_t * indicator;
2247a0ffb263SMatthias Ringwald         switch(hfp_connection->command){
224845796ff1SMilanka Ringwald             case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
2249fd4151d1SMilanka Ringwald                 // dealt with in hfp_ag_voice_recognition_state_machine
225045796ff1SMilanka Ringwald                 break;
2251ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
2252ce263fc8SMatthias Ringwald                 if (hfp_ag_response_and_hold_active){
2253a0ffb263SMatthias Ringwald                     hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
2254ce263fc8SMatthias Ringwald                 }
2255a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2256ce263fc8SMatthias Ringwald                 break;
2257ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
22580222a807SMatthias Ringwald                 switch(hfp_connection->ag_response_and_hold_action){
2259ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
2260a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
2261ce263fc8SMatthias Ringwald                         break;
2262ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
2263a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
2264ce263fc8SMatthias Ringwald                         break;
2265ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
2266a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
2267ce263fc8SMatthias Ringwald                         break;
2268ce263fc8SMatthias Ringwald                     default:
2269ce263fc8SMatthias Ringwald                         break;
2270ce263fc8SMatthias Ringwald                 }
2271a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2272ce263fc8SMatthias Ringwald                 break;
2273ce263fc8SMatthias Ringwald             case HFP_CMD_HF_INDICATOR_STATUS:
2274a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2275347d46c8SMilanka Ringwald 
2276aeb0f0feSMatthias Ringwald                 if (hfp_connection->parser_indicator_index < hfp_ag_generic_status_indicators_nr){
2277aeb0f0feSMatthias Ringwald                     indicator = &hfp_ag_generic_status_indicators[hfp_connection->parser_indicator_index];
2278ce263fc8SMatthias Ringwald                     switch (indicator->uuid){
2279347d46c8SMilanka Ringwald                         case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY:
22800222a807SMatthias Ringwald                             if (hfp_connection->parser_indicator_value > 1) {
2281a0ffb263SMatthias Ringwald                                 hfp_connection->send_error = 1;
2282ce263fc8SMatthias Ringwald                                 break;
2283ce263fc8SMatthias Ringwald                             }
2284a0ffb263SMatthias Ringwald                             hfp_connection->ok_pending = 1;
2285585db78dSMilanka Ringwald                             hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value);
2286347d46c8SMilanka Ringwald                             break;
2287347d46c8SMilanka Ringwald                         case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL:
2288347d46c8SMilanka Ringwald                             if (hfp_connection->parser_indicator_value > 100){
2289347d46c8SMilanka Ringwald                                 hfp_connection->send_error = 1;
2290347d46c8SMilanka Ringwald                                 break;
2291347d46c8SMilanka Ringwald                             }
2292347d46c8SMilanka Ringwald                             hfp_connection->ok_pending = 1;
2293585db78dSMilanka Ringwald                             hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value);
2294347d46c8SMilanka Ringwald                             break;
2295347d46c8SMilanka Ringwald                         default:
2296585db78dSMilanka Ringwald                             hfp_connection->send_error = 1;
2297347d46c8SMilanka Ringwald                             break;
2298347d46c8SMilanka Ringwald                     }
2299347d46c8SMilanka Ringwald                 } else {
2300347d46c8SMilanka Ringwald                     hfp_connection->send_error = 1;
2301347d46c8SMilanka Ringwald                 }
2302ce263fc8SMatthias Ringwald                 break;
2303ce263fc8SMatthias Ringwald             case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
2304ce263fc8SMatthias Ringwald                 // expected by SLC state machine
2305a0ffb263SMatthias Ringwald                 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
2306a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
2307a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_status_indicators = 1;
2308ce263fc8SMatthias Ringwald                 break;
2309ce263fc8SMatthias Ringwald             case HFP_CMD_LIST_CURRENT_CALLS:
2310a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2311a0ffb263SMatthias Ringwald                 hfp_connection->next_call_index = 0;
2312a0ffb263SMatthias Ringwald                 hfp_connection->send_status_of_current_calls = 1;
2313ce263fc8SMatthias Ringwald                 break;
2314ce263fc8SMatthias Ringwald             case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
2315aeb0f0feSMatthias Ringwald                 if (hfp_ag_subscriber_numbers_count == 0){
2316485ac19eSMilanka Ringwald                     hfp_ag_send_ok(hfp_connection->rfcomm_cid);
2317ce263fc8SMatthias Ringwald                     break;
2318ce263fc8SMatthias Ringwald                 }
2319a0ffb263SMatthias Ringwald                 hfp_connection->next_subscriber_number_to_send = 0;
2320a0ffb263SMatthias Ringwald                 hfp_connection->send_subscriber_number = 1;
2321ce263fc8SMatthias Ringwald                 break;
2322c1797c7dSMatthias Ringwald             case HFP_CMD_TRANSMIT_DTMF_CODES:
23230222a807SMatthias Ringwald             {
2324a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
23250222a807SMatthias Ringwald                 char buffer[2];
23260222a807SMatthias Ringwald                 buffer[0] = (char) hfp_connection->ag_dtmf_code;
23270222a807SMatthias Ringwald                 buffer[1] = 0;
23280222a807SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer);
2329c1797c7dSMatthias Ringwald                 break;
23300222a807SMatthias Ringwald             }
2331aa4dd815SMatthias Ringwald             case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
2332a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2333ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
2334aa4dd815SMatthias Ringwald                 break;
2335aa4dd815SMatthias Ringwald             case HFP_CMD_TURN_OFF_EC_AND_NR:
2336a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2337347d46c8SMilanka Ringwald 
2338aeb0f0feSMatthias Ringwald                 if (get_bit(hfp_ag_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
2339a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
2340aeb0f0feSMatthias Ringwald                     hfp_ag_supported_features = store_bit(hfp_ag_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
2341347d46c8SMilanka Ringwald                     status = ERROR_CODE_SUCCESS;
2342aa4dd815SMatthias Ringwald                 } else {
2343a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
2344347d46c8SMilanka Ringwald                     status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
2345aa4dd815SMatthias Ringwald                 }
2346347d46c8SMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status);
2347aa4dd815SMatthias Ringwald                 break;
2348aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_ANSWERED:
2349a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
235060ebb071SMilanka Ringwald                 log_info("HFP: ATA");
2351a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
2352aa4dd815SMatthias Ringwald                 break;
2353aa4dd815SMatthias Ringwald             case HFP_CMD_HANG_UP_CALL:
2354a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2355a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2356a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
2357aa4dd815SMatthias Ringwald                 break;
2358aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_HOLD: {
2359a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2360a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2361d0c20769SMatthias Ringwald 
23620222a807SMatthias Ringwald                 switch (hfp_connection->ag_call_hold_action){
23630222a807SMatthias Ringwald                     case 0:
2364d0c20769SMatthias Ringwald                         // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
2365a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
2366aa4dd815SMatthias Ringwald                         break;
23670222a807SMatthias Ringwald                     case 1:
2368d0c20769SMatthias Ringwald                         // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
2369d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
2370d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
2371a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
2372aa4dd815SMatthias Ringwald                         break;
23730222a807SMatthias Ringwald                     case 2:
2374d0c20769SMatthias Ringwald                         // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
2375d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
2376d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
2377a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
2378aa4dd815SMatthias Ringwald                         break;
23790222a807SMatthias Ringwald                     case 3:
2380d0c20769SMatthias Ringwald                         // Adds a held call to the conversation.
2381a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
2382aa4dd815SMatthias Ringwald                         break;
23830222a807SMatthias Ringwald                     case 4:
2384d0c20769SMatthias Ringwald                         // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
2385a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
2386aa4dd815SMatthias Ringwald                         break;
2387aa4dd815SMatthias Ringwald                     default:
2388aa4dd815SMatthias Ringwald                         break;
2389aa4dd815SMatthias Ringwald                 }
23900222a807SMatthias Ringwald 				hfp_connection->call_index = 0;
239135e92150SMatthias Ringwald                 break;
2392aa4dd815SMatthias Ringwald             }
2393aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_PHONE_NUMBER:
2394a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
23959ff73f41SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, hfp_connection);
2396aa4dd815SMatthias Ringwald                 break;
2397aa4dd815SMatthias Ringwald             case HFP_CMD_REDIAL_LAST_NUMBER:
2398a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2399a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
2400aa4dd815SMatthias Ringwald                 break;
2401aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CLIP:
2402a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2403a0ffb263SMatthias Ringwald                 log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
2404a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2405aa4dd815SMatthias Ringwald                 break;
2406aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
2407a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2408a0ffb263SMatthias Ringwald                 log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
2409a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2410aa4dd815SMatthias Ringwald                 break;
2411aa4dd815SMatthias Ringwald             case HFP_CMD_SET_SPEAKER_GAIN:
2412a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2413a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
241460ebb071SMilanka Ringwald                 log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
24153db60f78SBjoern Hartmann                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_connection->speaker_gain);
2416aa4dd815SMatthias Ringwald                 break;
2417aa4dd815SMatthias Ringwald             case HFP_CMD_SET_MICROPHONE_GAIN:
2418a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2419a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
242060ebb071SMilanka Ringwald                 log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
24212abbd98dSMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_connection->microphone_gain);
2422aa4dd815SMatthias Ringwald                 break;
2423aa4dd815SMatthias Ringwald             default:
2424aa4dd815SMatthias Ringwald                 break;
24253deb3ec6SMatthias Ringwald         }
24263deb3ec6SMatthias Ringwald     }
24270cef86faSMatthias Ringwald }
24283deb3ec6SMatthias Ringwald 
24291c6a0fc0SMatthias Ringwald static void hfp_ag_run(void){
2430d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
2431d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2432d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2433d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
243422387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2435f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
24363deb3ec6SMatthias Ringwald     }
24373deb3ec6SMatthias Ringwald }
24383deb3ec6SMatthias Ringwald 
24391c6a0fc0SMatthias Ringwald static void hfp_ag_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2440fdf18f86SMilanka Ringwald     hfp_connection_t * hfp_connection = NULL;
24413deb3ec6SMatthias Ringwald     switch (packet_type){
24423deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
2443fdf18f86SMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
2444fd075f20SMilanka Ringwald             btstack_assert(hfp_connection != NULL);
2445fdf18f86SMilanka Ringwald 
2446fdf18f86SMilanka Ringwald             hfp_ag_handle_rfcomm_data(hfp_connection, packet, size);
2447fdf18f86SMilanka Ringwald             hfp_ag_run_for_context(hfp_connection);
2448fdf18f86SMilanka Ringwald             return;
24493deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2450e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2451e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2452fd075f20SMilanka Ringwald                 hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
2453fd075f20SMilanka Ringwald                 btstack_assert(hfp_connection != NULL);
2454fd075f20SMilanka Ringwald 
2455fd075f20SMilanka Ringwald                 hfp_ag_run_for_context(hfp_connection);
2456e30a6a47SMatthias Ringwald                 return;
2457e30a6a47SMatthias Ringwald             }
245827950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2459aa4dd815SMatthias Ringwald             break;
24603deb3ec6SMatthias Ringwald         default:
24613deb3ec6SMatthias Ringwald             break;
24623deb3ec6SMatthias Ringwald     }
24633deb3ec6SMatthias Ringwald 
24641c6a0fc0SMatthias Ringwald     hfp_ag_run();
24653deb3ec6SMatthias Ringwald }
24663deb3ec6SMatthias Ringwald 
24671c6a0fc0SMatthias Ringwald static void hfp_ag_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2468405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
24691c6a0fc0SMatthias Ringwald     hfp_ag_run();
2470405014fbSMatthias Ringwald }
2471405014fbSMatthias Ringwald 
24727ca89cabSMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, const uint8_t * codecs){
24733deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
24743deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
24753deb3ec6SMatthias Ringwald         return;
24763deb3ec6SMatthias Ringwald     }
24773deb3ec6SMatthias Ringwald     int i;
2478aeb0f0feSMatthias Ringwald     hfp_ag_codecs_nr = codecs_nr;
24793deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
2480aeb0f0feSMatthias Ringwald         hfp_ag_codecs[i] = codecs[i];
24813deb3ec6SMatthias Ringwald     }
2482a0ffb263SMatthias Ringwald }
24833deb3ec6SMatthias Ringwald 
2484a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2485aeb0f0feSMatthias Ringwald     hfp_ag_supported_features = supported_features;
2486a0ffb263SMatthias Ringwald }
24873deb3ec6SMatthias Ringwald 
24887ca89cabSMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, const hfp_ag_indicator_t * ag_indicators){
2489a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
24906535961aSMatthias Ringwald     (void)memcpy(hfp_ag_indicators, ag_indicators,
24916535961aSMatthias Ringwald                  ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2492a0ffb263SMatthias Ringwald }
24933deb3ec6SMatthias Ringwald 
24947ca89cabSMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, const hfp_generic_status_indicator_t * hf_indicators){
249525789943SMilanka Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return;
2496aeb0f0feSMatthias Ringwald     hfp_ag_generic_status_indicators_nr = hf_indicators_nr;
2497aeb0f0feSMatthias Ringwald     (void)memcpy(hfp_ag_generic_status_indicators, hf_indicators,
24986535961aSMatthias Ringwald                  hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2499a0ffb263SMatthias Ringwald }
2500a0ffb263SMatthias Ringwald 
2501a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
25023deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
25036535961aSMatthias Ringwald     (void)memcpy(hfp_ag_call_hold_services, call_hold_services,
25046535961aSMatthias Ringwald                  call_hold_services_nr * sizeof(char *));
2505a0ffb263SMatthias Ringwald }
2506a0ffb263SMatthias Ringwald 
2507a0ffb263SMatthias Ringwald 
2508a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
250927950165SMatthias Ringwald 
2510520c92d5SMatthias Ringwald     hfp_init();
251120b2edb6SMatthias Ringwald     hfp_ag_call_hold_services_nr = 0;
251220b2edb6SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
251320b2edb6SMatthias Ringwald     hfp_ag_indicators_nr = 0;
2514aeb0f0feSMatthias Ringwald     hfp_ag_codecs_nr = 0;
2515aeb0f0feSMatthias Ringwald     hfp_ag_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
2516aeb0f0feSMatthias Ringwald     hfp_ag_subscriber_numbers = NULL;
2517aeb0f0feSMatthias Ringwald     hfp_ag_subscriber_numbers_count = 0;
2518d63c37a1SMatthias Ringwald 
25191c6a0fc0SMatthias Ringwald     hfp_ag_hci_event_callback_registration.callback = &hfp_ag_hci_event_packet_handler;
25201c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_ag_hci_event_callback_registration);
252127950165SMatthias Ringwald 
25221c6a0fc0SMatthias Ringwald     rfcomm_register_service(&hfp_ag_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
252327950165SMatthias Ringwald 
252427950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
25251c6a0fc0SMatthias Ringwald     hfp_set_ag_rfcomm_packet_handler(&hfp_ag_rfcomm_packet_handler);
2526aa4dd815SMatthias Ringwald 
2527d210d9c4SMatthias Ringwald     hfp_gsm_init();
25283deb3ec6SMatthias Ringwald }
25293deb3ec6SMatthias Ringwald 
253020b2edb6SMatthias Ringwald void hfp_ag_deinit(void){
253120b2edb6SMatthias Ringwald     hfp_deinit();
253220b2edb6SMatthias Ringwald     hfp_gsm_deinit();
2533aeb0f0feSMatthias Ringwald 
2534aeb0f0feSMatthias Ringwald     hfp_ag_callback = NULL;
2535aeb0f0feSMatthias Ringwald     hfp_ag_supported_features = 0;
2536aeb0f0feSMatthias Ringwald     hfp_ag_codecs_nr = 0;
2537aeb0f0feSMatthias Ringwald     hfp_ag_indicators_nr = 0;
2538aeb0f0feSMatthias Ringwald     hfp_ag_call_hold_services_nr = 0;
253920b2edb6SMatthias Ringwald     (void) memset(&hfp_ag_call_hold_services, 0, sizeof(hfp_ag_call_hold_services));
2540aeb0f0feSMatthias Ringwald     hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
254120b2edb6SMatthias Ringwald     (void) memset(&hfp_ag_response_and_hold_state, 0, sizeof(hfp_response_and_hold_state_t));
2542aeb0f0feSMatthias Ringwald     hfp_ag_subscriber_numbers = NULL;
2543aeb0f0feSMatthias Ringwald     (void) memset(&hfp_ag_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
254420b2edb6SMatthias Ringwald }
254520b2edb6SMatthias Ringwald 
25464eb3f1d8SMilanka Ringwald uint8_t hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
25474eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
25483deb3ec6SMatthias Ringwald }
25493deb3ec6SMatthias Ringwald 
2550657bc59fSMilanka Ringwald uint8_t hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
25519c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2552a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2553657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2554a33eb0c4SMilanka Ringwald     }
2555c5fa3c94SMilanka Ringwald 
25561ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
2557f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2558657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
25593deb3ec6SMatthias Ringwald }
25603deb3ec6SMatthias Ringwald 
2561c5fa3c94SMilanka Ringwald uint8_t hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
25629c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2563a0ffb263SMatthias Ringwald     if (!hfp_connection){
2564c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
25653deb3ec6SMatthias Ringwald     }
25663deb3ec6SMatthias Ringwald 
2567c5fa3c94SMilanka Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2568c5fa3c94SMilanka Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
2569c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2570c5fa3c94SMilanka Ringwald     }
2571c5fa3c94SMilanka Ringwald 
2572c5fa3c94SMilanka Ringwald     hfp_connection->extended_audio_gateway_error = error;
2573c5fa3c94SMilanka Ringwald     hfp_ag_run_for_context(hfp_connection);
2574c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2575c5fa3c94SMilanka Ringwald }
2576c5fa3c94SMilanka Ringwald 
2577c5fa3c94SMilanka Ringwald static uint8_t hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
2578c5fa3c94SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
2579c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2580c5fa3c94SMilanka Ringwald     }
2581c5fa3c94SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
2582c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2583c5fa3c94SMilanka Ringwald     }
25843deb3ec6SMatthias Ringwald 
2585a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2586a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2587d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2588d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2589a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
25907522e673SMatthias Ringwald         // now, pick link settings
2591afac2a04SMilanka Ringwald         hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
25923721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
25933721a235SMatthias Ringwald         hfp_cc256x_prepare_for_sco(hfp_connection);
25943721a235SMatthias Ringwald #endif
2595c5fa3c94SMilanka Ringwald         return ERROR_CODE_SUCCESS;
25963deb3ec6SMatthias Ringwald     }
2597aa4dd815SMatthias Ringwald 
2598411a9776SMilanka Ringwald     uint8_t i;
2599411a9776SMilanka Ringwald     bool codec_was_in_use = false;
2600411a9776SMilanka Ringwald     bool better_codec_can_be_used = false;
2601411a9776SMilanka Ringwald 
2602411a9776SMilanka Ringwald     for (i = 0; i<hfp_connection->remote_codecs_nr; i++){
2603411a9776SMilanka Ringwald         if (hfp_connection->negotiated_codec == hfp_connection->remote_codecs[i]){
2604411a9776SMilanka Ringwald             codec_was_in_use = true;
2605411a9776SMilanka Ringwald         } else if (hfp_connection->negotiated_codec < hfp_connection->remote_codecs[i]){
2606411a9776SMilanka Ringwald             better_codec_can_be_used = true;
2607411a9776SMilanka Ringwald         }
2608411a9776SMilanka Ringwald     }
2609411a9776SMilanka Ringwald 
2610411a9776SMilanka Ringwald     if (!codec_was_in_use || better_codec_can_be_used){
26112ee45eb2SMilanka Ringwald         hfp_connection->trigger_codec_exchange = 1;
2612411a9776SMilanka Ringwald         hfp_connection->codecs_state = HFP_CODECS_IDLE;
2613411a9776SMilanka Ringwald     }
2614c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2615aa4dd815SMatthias Ringwald }
2616aa4dd815SMatthias Ringwald 
2617c5fa3c94SMilanka Ringwald uint8_t hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
26189c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2619a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2620c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2621a33eb0c4SMilanka Ringwald     }
2622c5fa3c94SMilanka Ringwald     uint8_t status = hfp_ag_setup_audio_connection(hfp_connection);
2623c5fa3c94SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
2624c5fa3c94SMilanka Ringwald         return status;
2625c5fa3c94SMilanka Ringwald     }
2626f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2627c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
26283deb3ec6SMatthias Ringwald }
26293deb3ec6SMatthias Ringwald 
2630c5fa3c94SMilanka Ringwald uint8_t hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
26319c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2632a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2633c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2634a33eb0c4SMilanka Ringwald     }
2635c5fa3c94SMilanka Ringwald 
26360b4debbfSMilanka Ringwald     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
26370b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
26380b4debbfSMilanka Ringwald     }
26391a26de69SMilanka Ringwald 
26400b4debbfSMilanka Ringwald     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
26410b4debbfSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
2642f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
26430b4debbfSMilanka Ringwald     }
26440b4debbfSMilanka Ringwald     return status;
26453deb3ec6SMatthias Ringwald }
2646aa4dd815SMatthias Ringwald 
2647aa4dd815SMatthias Ringwald /**
2648aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2649aa4dd815SMatthias Ringwald  */
2650aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2651aeb0f0feSMatthias Ringwald     if (get_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2652aa4dd815SMatthias Ringwald         return;
2653aa4dd815SMatthias Ringwald     }
2654c5fa3c94SMilanka Ringwald 
2655aeb0f0feSMatthias Ringwald     hfp_ag_supported_features = store_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2656aa4dd815SMatthias Ringwald 
2657665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2658665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2659665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2660a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
266166c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2662a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2663f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2664aa4dd815SMatthias Ringwald     }
2665aa4dd815SMatthias Ringwald }
2666aa4dd815SMatthias Ringwald 
2667aa4dd815SMatthias Ringwald /**
2668aa4dd815SMatthias Ringwald  * @brief Called from GSM
2669aa4dd815SMatthias Ringwald  */
2670aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2671aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2672aa4dd815SMatthias Ringwald }
2673aa4dd815SMatthias Ringwald 
2674fe899794SMatthias Ringwald void hfp_ag_outgoing_call_initiated(const char * number) {
2675c5753b03SMatthias Ringwald     UNUSED(number);
2676fe899794SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, NULL);
2677fe899794SMatthias Ringwald }
2678fe899794SMatthias Ringwald 
2679aa4dd815SMatthias Ringwald /**
2680aa4dd815SMatthias Ringwald  * @brief number is stored.
2681aa4dd815SMatthias Ringwald  */
2682aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2683f8737b81SMatthias Ringwald     hfp_gsm_handler(HFP_AG_SET_CLIP, 0, type, number);
2684aa4dd815SMatthias Ringwald }
2685aa4dd815SMatthias Ringwald 
2686aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2687aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2688aa4dd815SMatthias Ringwald }
2689aa4dd815SMatthias Ringwald 
2690aa4dd815SMatthias Ringwald // call from AG UI
2691aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2692aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2693aa4dd815SMatthias Ringwald }
2694aa4dd815SMatthias Ringwald 
2695ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2696ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2697ce263fc8SMatthias Ringwald }
2698ce263fc8SMatthias Ringwald 
2699aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2700aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2701aa4dd815SMatthias Ringwald }
2702aa4dd815SMatthias Ringwald 
2703aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2704aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2705aa4dd815SMatthias Ringwald }
2706aa4dd815SMatthias Ringwald 
2707aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2708aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2709aa4dd815SMatthias Ringwald }
2710aa4dd815SMatthias Ringwald 
2711aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2712aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2713aa4dd815SMatthias Ringwald }
2714aa4dd815SMatthias Ringwald 
2715aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2716aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2717aa4dd815SMatthias Ringwald }
2718ce263fc8SMatthias Ringwald 
2719ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2720ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2721ce263fc8SMatthias Ringwald }
2722ce263fc8SMatthias Ringwald 
2723ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2724ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2725ce263fc8SMatthias Ringwald }
2726ce263fc8SMatthias Ringwald 
2727ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2728ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2729ce263fc8SMatthias Ringwald }
2730ce263fc8SMatthias Ringwald 
2731aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2732aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2733aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2734aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2735aa4dd815SMatthias Ringwald 
2736665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2737665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2738665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2739a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
274066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2741a0ffb263SMatthias Ringwald         if (!hfp_connection->ag_indicators[indicator_index].enabled) {
2742245852b7SMilanka Ringwald             log_info("Requested AG indicator '%s' update to %u, but it is not enabled", hfp_ag_indicators[indicator_index].name, value);
2743ce263fc8SMatthias Ringwald             continue;
2744ce263fc8SMatthias Ringwald         }
2745245852b7SMilanka Ringwald         log_info("AG indicator '%s' changed to %u, request transfer status", hfp_ag_indicators[indicator_index].name, value);
2746a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2747f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2748aa4dd815SMatthias Ringwald     }
2749aa4dd815SMatthias Ringwald }
2750aa4dd815SMatthias Ringwald 
2751c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_registration_status(int registration_status){
2752c5fa3c94SMilanka Ringwald     if ((registration_status < 0) || (registration_status > 1)){
2753c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2754c5fa3c94SMilanka Ringwald     }
2755c5fa3c94SMilanka Ringwald 
2756c5fa3c94SMilanka Ringwald     if ( (registration_status == 0) && (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
2757245852b7SMilanka Ringwald 
2758245852b7SMilanka Ringwald         // if network goes away wihle a call is active:
2759245852b7SMilanka Ringwald         // - the  call gets dropped
2760245852b7SMilanka Ringwald         // - we send NO CARRIER
2761245852b7SMilanka Ringwald         // NOTE: the CALL=0 has to be sent before NO CARRIER
2762245852b7SMilanka Ringwald 
2763245852b7SMilanka Ringwald         hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2764245852b7SMilanka Ringwald 
2765245852b7SMilanka Ringwald         btstack_linked_list_iterator_t it;
2766245852b7SMilanka Ringwald         btstack_linked_list_iterator_init(&it, hfp_get_connections());
2767245852b7SMilanka Ringwald         while (btstack_linked_list_iterator_has_next(&it)){
2768245852b7SMilanka Ringwald             hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2769245852b7SMilanka Ringwald             hfp_connection->ag_send_no_carrier = true;
2770245852b7SMilanka Ringwald         }
2771245852b7SMilanka Ringwald     }
2772c5fa3c94SMilanka Ringwald     hfp_ag_set_ag_indicator("service", registration_status);
2773c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2774aa4dd815SMatthias Ringwald }
2775aa4dd815SMatthias Ringwald 
2776c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_signal_strength(int signal_strength){
2777c5fa3c94SMilanka Ringwald     if ((signal_strength < 0) || (signal_strength > 5)){
2778c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2779aa4dd815SMatthias Ringwald     }
2780aa4dd815SMatthias Ringwald 
2781c5fa3c94SMilanka Ringwald     hfp_ag_set_ag_indicator("signal", signal_strength);
2782c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2783aa4dd815SMatthias Ringwald }
2784aa4dd815SMatthias Ringwald 
2785c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_roaming_status(int roaming_status){
2786c5fa3c94SMilanka Ringwald     if ((roaming_status < 0) || (roaming_status > 1)){
2787c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2788aa4dd815SMatthias Ringwald     }
2789aa4dd815SMatthias Ringwald 
2790c5fa3c94SMilanka Ringwald     hfp_ag_set_ag_indicator("roam", roaming_status);
2791c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2792c5fa3c94SMilanka Ringwald }
2793c5fa3c94SMilanka Ringwald 
2794c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_battery_level(int battery_level){
2795c5fa3c94SMilanka Ringwald     if ((battery_level < 0) || (battery_level > 5)){
2796c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2797c5fa3c94SMilanka Ringwald     }
2798c5fa3c94SMilanka Ringwald     hfp_ag_set_ag_indicator("battchg", battery_level);
2799c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2800c5fa3c94SMilanka Ringwald }
2801c5fa3c94SMilanka Ringwald 
28026b8275b0SMilanka Ringwald static uint8_t activate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){
28039c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2804a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2805c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2806a33eb0c4SMilanka Ringwald     }
28076b8275b0SMilanka Ringwald 
28086b8275b0SMilanka Ringwald     uint8_t status = hfp_ag_can_activate_voice_recognition(hfp_connection, enhanced);
2809d84719b1SMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
2810734ef2bdSMilanka Ringwald         hfp_connection->ag_activate_voice_recognition_value = 1;
2811fd4151d1SMilanka Ringwald         hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
2812fd4151d1SMilanka Ringwald         hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
28136b8275b0SMilanka Ringwald         hfp_connection->enhanced_voice_recognition_enabled = enhanced;
28141a26de69SMilanka Ringwald         hfp_connection->ag_audio_connection_opened_before_vra = hfp_ag_is_audio_connection_active(hfp_connection);
28156b8275b0SMilanka Ringwald         hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
2816f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2817aa4dd815SMatthias Ringwald     }
2818d84719b1SMilanka Ringwald     return status;
2819d84719b1SMilanka Ringwald }
2820d84719b1SMilanka Ringwald 
28216b8275b0SMilanka Ringwald static uint8_t deactivate_voice_recognition(hci_con_handle_t acl_handle, bool enhanced){
28226b8275b0SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
28236b8275b0SMilanka Ringwald     if (!hfp_connection){
28246b8275b0SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
28256b8275b0SMilanka Ringwald     }
28266b8275b0SMilanka Ringwald     uint8_t status = hfp_ag_voice_recognition_session_active(hfp_connection, enhanced);
28276b8275b0SMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
28286b8275b0SMilanka Ringwald         hfp_connection->ag_activate_voice_recognition_value = 0;
28296b8275b0SMilanka Ringwald         hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
28306b8275b0SMilanka Ringwald         hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
28316b8275b0SMilanka Ringwald         hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
28326b8275b0SMilanka Ringwald         hfp_ag_run_for_context(hfp_connection);
28336b8275b0SMilanka Ringwald     }
28346b8275b0SMilanka Ringwald     return status;
28356b8275b0SMilanka Ringwald }
28366b8275b0SMilanka Ringwald 
28376b8275b0SMilanka Ringwald uint8_t hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle){
28386b8275b0SMilanka Ringwald     return activate_voice_recognition(acl_handle, false);
28396b8275b0SMilanka Ringwald }
28406b8275b0SMilanka Ringwald 
28416b8275b0SMilanka Ringwald uint8_t hfp_ag_activate_enhanced_voice_recognition(hci_con_handle_t acl_handle){
28426b8275b0SMilanka Ringwald     return activate_voice_recognition(acl_handle, true);
28436b8275b0SMilanka Ringwald }
28446b8275b0SMilanka Ringwald 
2845d84719b1SMilanka Ringwald uint8_t hfp_ag_deactivate_voice_recognition(hci_con_handle_t acl_handle){
28466b8275b0SMilanka Ringwald     return deactivate_voice_recognition(acl_handle, false);
284745796ff1SMilanka Ringwald }
284845796ff1SMilanka Ringwald 
2849e83c2025SMilanka Ringwald uint8_t hfp_ag_deactivate_enhanced_voice_recognition(hci_con_handle_t acl_handle){
28506b8275b0SMilanka Ringwald     return deactivate_voice_recognition(acl_handle, true);
285145796ff1SMilanka Ringwald }
285245796ff1SMilanka Ringwald 
2853cf75be85SMilanka Ringwald static uint8_t hfp_ag_enhanced_voice_recognition_send_state(hci_con_handle_t acl_handle, hfp_voice_recognition_state_t state){
285445796ff1SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
285545796ff1SMilanka Ringwald     if (!hfp_connection){
2856c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
285745796ff1SMilanka Ringwald     }
2858c5fa3c94SMilanka Ringwald 
28596a61d7dcSMilanka Ringwald     uint8_t status = hfp_ag_voice_recognition_session_active(hfp_connection, true);
28606a61d7dcSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
2861cf75be85SMilanka Ringwald         hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
286245796ff1SMilanka Ringwald         hfp_connection->ag_vra_state = state;
2863cf75be85SMilanka Ringwald         hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS;
286445796ff1SMilanka Ringwald         hfp_ag_run_for_context(hfp_connection);
28656a61d7dcSMilanka Ringwald     }
28666a61d7dcSMilanka Ringwald     return status;
286745796ff1SMilanka Ringwald }
286845796ff1SMilanka Ringwald 
2869f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_sending_audio(hci_con_handle_t acl_handle){
2870cf75be85SMilanka Ringwald     return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND);
287145796ff1SMilanka Ringwald }
2872f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
2873cf75be85SMilanka Ringwald     return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT);
287445796ff1SMilanka Ringwald }
2875cf75be85SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_processing_input(hci_con_handle_t acl_handle){
2876cf75be85SMilanka Ringwald     return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT);
287745796ff1SMilanka Ringwald }
287845796ff1SMilanka Ringwald 
2879b95cac54SMilanka Ringwald 
2880b95cac54SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_send_message(hci_con_handle_t acl_handle, hfp_voice_recognition_state_t state, hfp_voice_recognition_message_t msg){
2881b95cac54SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2882b95cac54SMilanka Ringwald     if (!hfp_connection){
2883b95cac54SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2884b95cac54SMilanka Ringwald     }
2885b95cac54SMilanka Ringwald 
2886b95cac54SMilanka Ringwald     uint8_t status = hfp_ag_can_send_enahnced_voice_recognition_message(hfp_connection, true);
2887b95cac54SMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
2888b95cac54SMilanka Ringwald         hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2889b95cac54SMilanka Ringwald         hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG;
2890b95cac54SMilanka Ringwald         hfp_connection->ag_msg = msg;
2891b95cac54SMilanka Ringwald         hfp_connection->ag_vra_state = state;
2892b95cac54SMilanka Ringwald         hfp_ag_run_for_context(hfp_connection);
2893b95cac54SMilanka Ringwald     }
2894b95cac54SMilanka Ringwald     return status;
2895b95cac54SMilanka Ringwald }
2896b95cac54SMilanka Ringwald 
2897b95cac54SMilanka Ringwald 
2898c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
28999c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2900a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2901c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2902a33eb0c4SMilanka Ringwald     }
2903c5fa3c94SMilanka Ringwald 
2904c5fa3c94SMilanka Ringwald     if ((gain < 0) || (gain > 15)){
2905c5fa3c94SMilanka Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
2906c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2907c5fa3c94SMilanka Ringwald     }
2908c5fa3c94SMilanka Ringwald 
2909a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2910a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2911a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2912a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2913aa4dd815SMatthias Ringwald     }
2914f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2915c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2916aa4dd815SMatthias Ringwald }
2917aa4dd815SMatthias Ringwald 
2918c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
29199c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2920a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2921c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2922a33eb0c4SMilanka Ringwald     }
2923c5fa3c94SMilanka Ringwald 
2924c5fa3c94SMilanka Ringwald     if ((gain < 0) || (gain > 15)){
2925c5fa3c94SMilanka Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
2926c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2927c5fa3c94SMilanka Ringwald     }
2928c5fa3c94SMilanka Ringwald 
2929a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2930a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2931a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2932aa4dd815SMatthias Ringwald     }
2933f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2934c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2935aa4dd815SMatthias Ringwald }
2936aa4dd815SMatthias Ringwald 
2937c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
29389c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2939a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2940c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2941a33eb0c4SMilanka Ringwald     }
2942c5fa3c94SMilanka Ringwald 
2943aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2944a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2945c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2946aa4dd815SMatthias Ringwald }
2947aa4dd815SMatthias Ringwald 
2948c5fa3c94SMilanka Ringwald uint8_t hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
29499c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2950a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2951c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2952a33eb0c4SMilanka Ringwald     }
2953c5fa3c94SMilanka Ringwald 
2954a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2955c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2956aa4dd815SMatthias Ringwald }
2957aa4dd815SMatthias Ringwald 
2958c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
29599c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2960a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2961c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2962a33eb0c4SMilanka Ringwald     }
2963c5fa3c94SMilanka Ringwald 
2964a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2965c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2966c1797c7dSMatthias Ringwald }
2967aa4dd815SMatthias Ringwald 
2968ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2969aeb0f0feSMatthias Ringwald     hfp_ag_subscriber_numbers = numbers;
2970aeb0f0feSMatthias Ringwald     hfp_ag_subscriber_numbers_count = numbers_count;
2971ce263fc8SMatthias Ringwald }
2972ce263fc8SMatthias Ringwald 
29739cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
29749cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2975ce263fc8SMatthias Ringwald }
2976ce263fc8SMatthias Ringwald 
29779de679b7SMilanka Ringwald void hfp_ag_set_last_dialed_number(const char * number){
29789de679b7SMilanka Ringwald     hfp_gsm_set_last_dialed_number(number);
29799de679b7SMilanka Ringwald }
29809de679b7SMilanka Ringwald 
2981c5fa3c94SMilanka Ringwald uint8_t hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
29829c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2983a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2984c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2985a33eb0c4SMilanka Ringwald     }
2986c5fa3c94SMilanka Ringwald 
2987c5fa3c94SMilanka Ringwald     if (!hfp_connection->call_waiting_notification_enabled){
2988c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2989c5fa3c94SMilanka Ringwald     }
2990a0ffb263SMatthias Ringwald 
2991a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2992f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2993c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2994a0ffb263SMatthias Ringwald }
2995ce263fc8SMatthias Ringwald 
299676cc1527SMatthias 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){
299776cc1527SMatthias Ringwald 	if (!name){
2998aeb0f0feSMatthias Ringwald 		name = hfp_ag_default_service_name;
299976cc1527SMatthias Ringwald 	}
300076cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
300176cc1527SMatthias Ringwald 
300276cc1527SMatthias Ringwald 	/*
300376cc1527SMatthias Ringwald 	 * 0x01 – Ability to reject a call
300476cc1527SMatthias Ringwald 	 * 0x00 – No ability to reject a call
300576cc1527SMatthias Ringwald 	 */
300676cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
300776cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
300876cc1527SMatthias Ringwald 
300976cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
301076cc1527SMatthias Ringwald 	//
301176cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
301276cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
301376cc1527SMatthias Ringwald 	//
301476cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
301576cc1527SMatthias Ringwald 	//
301676cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
3017ef3ae4ebSMilanka Ringwald 	if ( (wide_band_speech == 1) && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){
301876cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
301976cc1527SMatthias Ringwald 	}
3020ef3ae4ebSMilanka Ringwald 
3021ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
3022ef3ae4ebSMilanka Ringwald         sdp_features |= 1 << 6;
3023ef3ae4ebSMilanka Ringwald     }
3024ef3ae4ebSMilanka Ringwald 
3025ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_AGSF_VOICE_RECOGNITION_TEXT)){
3026ef3ae4ebSMilanka Ringwald         sdp_features |= 1 << 7;
3027ef3ae4ebSMilanka Ringwald     }
3028ef3ae4ebSMilanka Ringwald 
302976cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
303076cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
303176cc1527SMatthias Ringwald }
303276cc1527SMatthias Ringwald 
303376cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
303468466199SMilanka Ringwald 	btstack_assert(callback != NULL);
303568466199SMilanka Ringwald 
303676cc1527SMatthias Ringwald 	hfp_ag_callback = callback;
303776cc1527SMatthias Ringwald 	hfp_set_ag_callback(callback);
303876cc1527SMatthias Ringwald }
3039