xref: /btstack/src/classic/hfp_ag.c (revision e58f738af417a33f8b0f75a99a2d9d5c2071f2d5)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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 
9481e25d0aSMatthias Ringwald static bool (*hfp_ag_custom_call_sm_handler)(hfp_ag_call_event_t event);
9581e25d0aSMatthias Ringwald 
9620b2edb6SMatthias Ringwald static btstack_packet_callback_registration_t hfp_ag_hci_event_callback_registration;
97aa4dd815SMatthias Ringwald 
98aeb0f0feSMatthias Ringwald static uint16_t hfp_ag_supported_features;
9920b2edb6SMatthias Ringwald 
10040a8ee13SMatthias Ringwald // in-band ring tone is active on SLC if supported
10140a8ee13SMatthias Ringwald static bool hfp_ag_in_band_ring_tone_active;
10240a8ee13SMatthias Ringwald 
103aeb0f0feSMatthias Ringwald // codecs
104aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs_nr;
105aeb0f0feSMatthias Ringwald static uint8_t hfp_ag_codecs[HFP_MAX_NUM_CODECS];
1063deb3ec6SMatthias Ringwald 
107aeb0f0feSMatthias Ringwald // AG indicators
108*e58f738aSMatthias Ringwald static uint16_t           hfp_ag_indicators_nr;
10925789943SMilanka Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS];
1103deb3ec6SMatthias Ringwald 
111aeb0f0feSMatthias Ringwald // generic status indicators
112*e58f738aSMatthias Ringwald static uint16_t                       hfp_ag_generic_status_indicators_nr;
113aeb0f0feSMatthias Ringwald static hfp_generic_status_indicator_t hfp_ag_generic_status_indicators[HFP_MAX_NUM_INDICATORS];
114a0ffb263SMatthias Ringwald 
115*e58f738aSMatthias Ringwald static uint16_t hfp_ag_call_hold_services_nr;
1163deb3ec6SMatthias Ringwald static char *   hfp_ag_call_hold_services[6];
1173deb3ec6SMatthias Ringwald 
118ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
11927bb1817SMatthias Ringwald static bool hfp_ag_response_and_hold_active = false;
120aa4dd815SMatthias Ringwald 
12120b2edb6SMatthias Ringwald // Subscriber information entries
122aeb0f0feSMatthias Ringwald static hfp_phone_number_t * hfp_ag_subscriber_numbers;
123aeb0f0feSMatthias Ringwald static int                  hfp_ag_subscriber_numbers_count;
124ce263fc8SMatthias Ringwald 
125a4f85bd2SMilanka Ringwald // call state
126a4f85bd2SMilanka Ringwald static btstack_timer_source_t hfp_ag_ring_timeout;
127a4f85bd2SMilanka Ringwald 
12820b2edb6SMatthias Ringwald // code
129a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
130a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
131a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1326535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1336535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1343deb3ec6SMatthias Ringwald     }
135a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
136a0ffb263SMatthias Ringwald }
137a0ffb263SMatthias Ringwald 
138a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
139a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
140a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
141a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1426535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1436535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
144a0ffb263SMatthias Ringwald     }
145a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1463deb3ec6SMatthias Ringwald }
1473deb3ec6SMatthias Ringwald 
148aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
149aa4dd815SMatthias Ringwald     int i;
150aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
151aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
152aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
153aa4dd815SMatthias Ringwald         }
154aa4dd815SMatthias Ringwald     }
155aa4dd815SMatthias Ringwald     return NULL;
156aa4dd815SMatthias Ringwald }
157aa4dd815SMatthias Ringwald 
158aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
159aa4dd815SMatthias Ringwald     int i;
160aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
161aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
162aa4dd815SMatthias Ringwald             return i;
163aa4dd815SMatthias Ringwald         }
164aa4dd815SMatthias Ringwald     }
165aa4dd815SMatthias Ringwald     return -1;
166aa4dd815SMatthias Ringwald }
167aa4dd815SMatthias Ringwald 
1689c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){
1699c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1709c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1719c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1729c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1739c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1749c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1759c9c64c1SMatthias Ringwald         return hfp_connection;
1769c9c64c1SMatthias Ringwald     }
1779c9c64c1SMatthias Ringwald     return NULL;
1789c9c64c1SMatthias Ringwald }
1793deb3ec6SMatthias Ringwald 
18040a8ee13SMatthias Ringwald static bool has_in_band_ring_tone(void){
18140a8ee13SMatthias Ringwald     return get_bit(hfp_ag_supported_features, HFP_AGSF_IN_BAND_RING_TONE) != 0;
18240a8ee13SMatthias Ringwald }
18340a8ee13SMatthias Ringwald 
184f197e761SMatthias Ringwald static int use_in_band_tone(hfp_connection_t *hfp_connection) {
18540a8ee13SMatthias Ringwald     return hfp_connection->ag_in_band_ring_tone_active ? 1 : 0;
1863deb3ec6SMatthias Ringwald }
1873deb3ec6SMatthias Ringwald 
188a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
189a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
190aeb0f0feSMatthias Ringwald     int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1913deb3ec6SMatthias Ringwald     return hf && ag;
1923deb3ec6SMatthias Ringwald }
1933deb3ec6SMatthias Ringwald 
194a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
195a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
196aeb0f0feSMatthias Ringwald     int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1973deb3ec6SMatthias Ringwald     return hf && ag;
1983deb3ec6SMatthias Ringwald }
1993deb3ec6SMatthias Ringwald 
200a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
201a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
202aeb0f0feSMatthias Ringwald     int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_HF_INDICATORS);
2033deb3ec6SMatthias Ringwald     return hf && ag;
2043deb3ec6SMatthias Ringwald }
2053deb3ec6SMatthias Ringwald 
20676cc1527SMatthias Ringwald /* unsolicited responses */
207aa4dd815SMatthias Ringwald 
208f197e761SMatthias Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection_t * hfp_connection){
2098d860c8fSMatthias Ringwald     char buffer[40];
210ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n",
211f197e761SMatthias Ringwald              HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone(hfp_connection));
212ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
213f197e761SMatthias Ringwald     return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
2143deb3ec6SMatthias Ringwald }
2153deb3ec6SMatthias Ringwald 
2163deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2173deb3ec6SMatthias Ringwald     char buffer[40];
218ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n\r\nOK\r\n",
219aeb0f0feSMatthias Ringwald              HFP_SUPPORTED_FEATURES, hfp_ag_supported_features);
220ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
2213deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2223deb3ec6SMatthias Ringwald }
2233deb3ec6SMatthias Ringwald 
224485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){
2253deb3ec6SMatthias Ringwald     char buffer[10];
226ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\nOK\r\n");
227ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
2283deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2293deb3ec6SMatthias Ringwald }
2303deb3ec6SMatthias Ringwald 
231485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){
232aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
233aa4dd815SMatthias Ringwald }
234aa4dd815SMatthias Ringwald 
235245852b7SMilanka Ringwald static int hfp_ag_send_no_carrier(uint16_t cid){
236245852b7SMilanka Ringwald     char buffer[15];
237245852b7SMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\nNO CARRIER\r\n");
238245852b7SMilanka Ringwald     buffer[sizeof(buffer) - 1] = 0;
239245852b7SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
240245852b7SMilanka Ringwald }
241245852b7SMilanka Ringwald 
242aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
243aa4dd815SMatthias Ringwald     char buffer[50];
244ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP,
245ff7d6aeaSMatthias Ringwald              hfp_gsm_clip_number(), hfp_gsm_clip_type());
246ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
247aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
248aa4dd815SMatthias Ringwald }
249aa4dd815SMatthias Ringwald 
250ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
251ce263fc8SMatthias Ringwald     char buffer[50];
252ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: ,\"%s\",%u, , \r\n",
253ff7d6aeaSMatthias Ringwald              HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
254ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
255ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
256ce263fc8SMatthias Ringwald }
257ce263fc8SMatthias Ringwald 
258c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
259aa4dd815SMatthias Ringwald     char buffer[50];
260ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: %s\r\n",
261ff7d6aeaSMatthias Ringwald              HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
262ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
263aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
264aa4dd815SMatthias Ringwald }
265aa4dd815SMatthias Ringwald 
266aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
267aa4dd815SMatthias Ringwald     char buffer[50];
268ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n",
269ff7d6aeaSMatthias Ringwald              HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(),
270ff7d6aeaSMatthias Ringwald              hfp_gsm_clip_type());
271ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
272aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
273aa4dd815SMatthias Ringwald }
274aa4dd815SMatthias Ringwald 
275485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){
2763deb3ec6SMatthias Ringwald     char buffer[10];
277ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\nERROR\r\n");
278ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
2793deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2803deb3ec6SMatthias Ringwald }
2813deb3ec6SMatthias Ringwald 
282485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2833deb3ec6SMatthias Ringwald     char buffer[20];
284ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s=%d\r\n",
285ff7d6aeaSMatthias Ringwald              HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
286ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
2873deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2883deb3ec6SMatthias Ringwald }
2893deb3ec6SMatthias Ringwald 
290ad902e3dSMatthias Ringwald // get size for indicator string
291a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
292ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
29345718b6fSMatthias Ringwald     return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
294a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
295a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
296ad902e3dSMatthias Ringwald }
297ad902e3dSMatthias Ringwald 
298ad902e3dSMatthias Ringwald // store indicator
2991167ff83SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer, uint16_t buffer_size){
3001167ff83SMatthias Ringwald     snprintf((char *)buffer, buffer_size, "(\"%s\",(%d,%d)),",
301a0ffb263SMatthias Ringwald              hfp_ag_get_ag_indicators(hfp_connection)[i].name,
302a0ffb263SMatthias Ringwald              hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
303a0ffb263SMatthias Ringwald              hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
3041167ff83SMatthias Ringwald     ((char *)buffer)[buffer_size - 1] = 0;
3053deb3ec6SMatthias Ringwald }
306ad902e3dSMatthias Ringwald 
307ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
308a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
309a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
310ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
311c1ab6cc1SMatthias Ringwald     return 3 + ((num_indicators-1) * 2);
3123deb3ec6SMatthias Ringwald }
313ad902e3dSMatthias Ringwald 
314ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
315a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
316ad902e3dSMatthias Ringwald     if (index == 0) {
317ab2445a0SMatthias Ringwald         return (uint16_t) strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
318ad902e3dSMatthias Ringwald     }
319ad902e3dSMatthias Ringwald     index--;
320a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
321ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
322ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
323a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
324ad902e3dSMatthias Ringwald     }
325c1ab6cc1SMatthias Ringwald     if (indicator_index == (num_indicators - 1)){
326ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
327ad902e3dSMatthias Ringwald     }
328ad902e3dSMatthias Ringwald     return 1; // comma
329ad902e3dSMatthias Ringwald }
330ad902e3dSMatthias Ringwald 
3311167ff83SMatthias Ringwald static void hfp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer, uint16_t buffer_size){
332ad902e3dSMatthias Ringwald     if (index == 0){
333ad902e3dSMatthias Ringwald         *buffer++ = '\r';
33474386ee0SMatthias Ringwald         *buffer++ = '\n';
335ab2445a0SMatthias Ringwald         int len = (uint16_t) strlen(HFP_INDICATOR);
3366535961aSMatthias Ringwald         (void)memcpy(buffer, HFP_INDICATOR, len);
337ad902e3dSMatthias Ringwald         buffer += len;
338ad902e3dSMatthias Ringwald         *buffer++ = ':';
339ad902e3dSMatthias Ringwald         return;
340ad902e3dSMatthias Ringwald     }
341ad902e3dSMatthias Ringwald     index--;
342a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
343ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
344ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
3451167ff83SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer, buffer_size);
346ad902e3dSMatthias Ringwald         return;
347ad902e3dSMatthias Ringwald     }
348c1ab6cc1SMatthias Ringwald     if (indicator_index == (num_indicators-1)){
3496535961aSMatthias Ringwald         (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8);
350ad902e3dSMatthias Ringwald         return;
351ad902e3dSMatthias Ringwald     }
352ad902e3dSMatthias Ringwald     *buffer = ',';
3533deb3ec6SMatthias Ringwald }
3543deb3ec6SMatthias Ringwald 
355aeb0f0feSMatthias Ringwald static int hfp_ag_generic_indicators_join(char * buffer, int buffer_size){
356c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3573deb3ec6SMatthias Ringwald     int i;
3583deb3ec6SMatthias Ringwald     int offset = 0;
359aeb0f0feSMatthias Ringwald     for (i = 0; i < (hfp_ag_generic_status_indicators_nr - 1); i++) {
360aeb0f0feSMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_generic_status_indicators[i].uuid);
3613deb3ec6SMatthias Ringwald     }
362aeb0f0feSMatthias Ringwald     if (i < hfp_ag_generic_status_indicators_nr){
363aeb0f0feSMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_generic_status_indicators[i].uuid);
3643deb3ec6SMatthias Ringwald     }
3653deb3ec6SMatthias Ringwald     return offset;
3663deb3ec6SMatthias Ringwald }
3673deb3ec6SMatthias Ringwald 
368aeb0f0feSMatthias Ringwald static int hfp_ag_generic_indicators_initial_status_join(char * buffer, int buffer_size){
369aeb0f0feSMatthias Ringwald     if (buffer_size < (hfp_ag_generic_status_indicators_nr * 3)) return 0;
3703deb3ec6SMatthias Ringwald     int i;
3713deb3ec6SMatthias Ringwald     int offset = 0;
372aeb0f0feSMatthias Ringwald     for (i = 0; i < hfp_ag_generic_status_indicators_nr; i++) {
373aeb0f0feSMatthias 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);
3743deb3ec6SMatthias Ringwald     }
3753deb3ec6SMatthias Ringwald     return offset;
3763deb3ec6SMatthias Ringwald }
3773deb3ec6SMatthias Ringwald 
3783deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
379c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3803deb3ec6SMatthias Ringwald     int i;
3813deb3ec6SMatthias Ringwald     int offset = 0;
382c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_ag_indicators_nr-1); i++) {
3833deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3843deb3ec6SMatthias Ringwald     }
3853deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3863deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3873deb3ec6SMatthias Ringwald     }
3883deb3ec6SMatthias Ringwald     return offset;
3893deb3ec6SMatthias Ringwald }
3903deb3ec6SMatthias Ringwald 
3913deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
392c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0;
3933deb3ec6SMatthias Ringwald     int i;
3943deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
395c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) {
3963deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3973deb3ec6SMatthias Ringwald     }
3983deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3993deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
4003deb3ec6SMatthias Ringwald     }
4013deb3ec6SMatthias Ringwald     return offset;
4023deb3ec6SMatthias Ringwald }
4033deb3ec6SMatthias Ringwald 
404485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
405ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
406a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
4071167ff83SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer, uint16_t buffer_size)){
4083deb3ec6SMatthias Ringwald 
409ad902e3dSMatthias Ringwald     // assumes: can send now == true
410ad902e3dSMatthias Ringwald     // assumes: num segments > 0
411aba39421SMatthias Ringwald     // assumes: individual segments are smaller than MTU
412ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
413ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
414ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
415ad902e3dSMatthias Ringwald     int offset = 0;
416ad902e3dSMatthias Ringwald     int segment = start_segment;
417ad902e3dSMatthias Ringwald     while (segment < num_segments){
418a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
4191167ff83SMatthias Ringwald         if ((offset + segment_len + 1) > mtu) break;
4201167ff83SMatthias Ringwald         // append segment. As it appends a '\0', we provide a buffer one byte larger
4211167ff83SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset, segment_len + 1);
422ad902e3dSMatthias Ringwald         offset += segment_len;
423ad902e3dSMatthias Ringwald         segment++;
424ad902e3dSMatthias Ringwald     }
425ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
426f20dd2aeSBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES
427f20dd2aeSBjoern Hartmann     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, (char *) data);
428f20dd2aeSBjoern Hartmann #endif
429ad902e3dSMatthias Ringwald     return segment;
430ad902e3dSMatthias Ringwald }
4313deb3ec6SMatthias Ringwald 
432ad902e3dSMatthias Ringwald // returns next segment to store
433485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
434a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
435485ac19eSMilanka Ringwald     return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
436894d499cSMatthias Ringwald                                          hfp_ag_indicators_cmd_generator_get_segment_len,
437894d499cSMatthias Ringwald                                          hfp_ag_indicators_cmd_generator_store_segment);
4383deb3ec6SMatthias Ringwald }
4393deb3ec6SMatthias Ringwald 
440485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){
4413deb3ec6SMatthias Ringwald     char buffer[40];
44289425bfcSMilanka Ringwald     const int size = sizeof(buffer);
44389425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR);
44489425bfcSMilanka Ringwald     offset += hfp_ag_indicators_status_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 
449485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){
450ad902e3dSMatthias Ringwald     char buffer[40];
45189425bfcSMilanka Ringwald     const int size = sizeof(buffer);
45289425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
45389425bfcSMilanka Ringwald     offset += hfp_ag_call_services_join(buffer+offset, size-offset-9);
45489425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4553deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4563deb3ec6SMatthias Ringwald }
4573deb3ec6SMatthias Ringwald 
4583deb3ec6SMatthias Ringwald 
459485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){
460485ac19eSMilanka Ringwald     return hfp_ag_send_ok(cid);
4613deb3ec6SMatthias Ringwald }
4623deb3ec6SMatthias Ringwald 
463485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4643deb3ec6SMatthias Ringwald     char buffer[40];
46589425bfcSMilanka Ringwald     const int size = sizeof(buffer);
46689425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
467aeb0f0feSMatthias Ringwald     offset += hfp_ag_generic_indicators_join(buffer + offset, size - offset - 10);
46889425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n");
4693deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4703deb3ec6SMatthias Ringwald }
4713deb3ec6SMatthias Ringwald 
472485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4733deb3ec6SMatthias Ringwald     char buffer[40];
474aeb0f0feSMatthias Ringwald     int offset = hfp_ag_generic_indicators_initial_status_join(buffer, sizeof(buffer) - 7);
4751cc1d9e9SMilanka Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4763deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4773deb3ec6SMatthias Ringwald }
4783deb3ec6SMatthias Ringwald 
479485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4803deb3ec6SMatthias Ringwald     char buffer[20];
481ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d\r\n",
482ff7d6aeaSMatthias Ringwald              HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index,
483ff7d6aeaSMatthias Ringwald              indicator->status);
484ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
4853deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4863deb3ec6SMatthias Ringwald }
4873deb3ec6SMatthias Ringwald 
488485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4895e71456cSMilanka Ringwald     char buffer[41];
4903deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
49189425bfcSMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4923deb3ec6SMatthias Ringwald     } else {
49389425bfcSMilanka Ringwald         int offset = snprintf(buffer,  sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format);
4945e71456cSMilanka Ringwald         offset += snprintf(buffer+offset, 16, "%s", op.name);
49589425bfcSMilanka Ringwald         snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4963deb3ec6SMatthias Ringwald     }
4973deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4983deb3ec6SMatthias Ringwald }
4993deb3ec6SMatthias Ringwald 
500a04aecb1SMilanka Ringwald static inline int hfp_ag_send_cmd_with_space_and_int(uint16_t cid, const char * cmd, uint8_t value){
501a04aecb1SMilanka Ringwald     char buffer[30];
502a04aecb1SMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", cmd, value);
503a04aecb1SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
504a04aecb1SMilanka Ringwald }
505a04aecb1SMilanka Ringwald 
506a04aecb1SMilanka Ringwald 
5071cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
5081cc1d9e9SMilanka Ringwald     char buffer[30];
50989425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value);
5101cc1d9e9SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
5111cc1d9e9SMilanka Ringwald }
5123deb3ec6SMatthias Ringwald 
513485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){
5141cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
5153deb3ec6SMatthias Ringwald }
5163deb3ec6SMatthias Ringwald 
517485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
5181cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
519aa4dd815SMatthias Ringwald }
520aa4dd815SMatthias Ringwald 
521485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
5221cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
523aa4dd815SMatthias Ringwald }
524aa4dd815SMatthias Ringwald 
525485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){
52666191aa0SMilanka Ringwald     return hfp_ag_send_cmd_with_space_and_int(cid, HFP_RESPONSE_AND_HOLD, state);
527ce263fc8SMatthias Ringwald }
528ce263fc8SMatthias Ringwald 
529cf75be85SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection_t * hfp_connection){
530cf75be85SMilanka Ringwald     char buffer[30];
531fcf4ede6SMilanka Ringwald     uint8_t evra_enabled = hfp_connection->enhanced_voice_recognition_enabled ? 1 : 0;
532fcf4ede6SMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, evra_enabled, hfp_connection->ag_vra_state);
533cf75be85SMilanka Ringwald     return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
534cf75be85SMilanka Ringwald }
535cf75be85SMilanka Ringwald 
536d1b50446SMilanka Ringwald static int hfp_ag_send_voice_recognition_cmd(hfp_connection_t * hfp_connection, uint8_t activate_voice_recognition){
53745796ff1SMilanka Ringwald     char buffer[30];
538d1b50446SMilanka Ringwald     if (hfp_connection->enhanced_voice_recognition_enabled){
539d1b50446SMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition, hfp_connection->ag_vra_state);
540d1b50446SMilanka Ringwald     } else {
541d1b50446SMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
542d1b50446SMilanka Ringwald     }
543d1b50446SMilanka Ringwald     return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
54445796ff1SMilanka Ringwald }
54545796ff1SMilanka Ringwald 
54645796ff1SMilanka Ringwald static int hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection_t * hfp_connection){
547a2e701c0SMilanka Ringwald     char buffer[HFP_VR_TEXT_HEADER_SIZE + HFP_MAX_VR_TEXT_SIZE];
54845796ff1SMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: 1,%d,%X,%d,%d,\"%s\"\r\n", HFP_ACTIVATE_VOICE_RECOGNITION,
54945796ff1SMilanka Ringwald         hfp_connection->ag_vra_state,
55045796ff1SMilanka Ringwald         hfp_connection->ag_msg.text_id,
55145796ff1SMilanka Ringwald         hfp_connection->ag_msg.text_type,
55245796ff1SMilanka Ringwald         hfp_connection->ag_msg.text_operation,
55345796ff1SMilanka Ringwald         hfp_connection->ag_msg.text);
55445796ff1SMilanka Ringwald     return send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
55545796ff1SMilanka Ringwald }
55645796ff1SMilanka Ringwald 
557a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
558bc1b1537SMilanka Ringwald     if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
559bc1b1537SMilanka Ringwald 
5602085619dSMatthias Ringwald #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH
5612085619dSMatthias Ringwald     if (hfp_supports_codec(HFP_CODEC_LC3_SWB, hfp_ag_codecs_nr, hfp_ag_codecs)){
5622085619dSMatthias Ringwald         if (hfp_supports_codec(HFP_CODEC_LC3_SWB, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
5632085619dSMatthias Ringwald             return HFP_CODEC_LC3_SWB;
5642085619dSMatthias Ringwald         }
5652085619dSMatthias Ringwald     }
5662085619dSMatthias Ringwald #endif
5672085619dSMatthias Ringwald 
5682085619dSMatthias Ringwald #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
569aeb0f0feSMatthias Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_ag_codecs_nr, hfp_ag_codecs)){
5706a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
571df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
5723deb3ec6SMatthias Ringwald         }
5733deb3ec6SMatthias Ringwald     }
5742085619dSMatthias Ringwald #endif
5752085619dSMatthias Ringwald 
576df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
5773deb3ec6SMatthias Ringwald }
5783deb3ec6SMatthias Ringwald 
57976cc1527SMatthias Ringwald /* state machines */
58076cc1527SMatthias Ringwald 
581afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){
582aeb0f0feSMatthias Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) &&  (hfp_ag_supported_features & (1 << HFP_AGSF_ESCO_S4));
5837522e673SMatthias Ringwald }
5847522e673SMatthias Ringwald 
585a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
586aa4dd815SMatthias Ringwald     /* events ( == commands):
587aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
588aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
589aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
590aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
591aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
592aa4dd815SMatthias Ringwald     */
593a0ffb263SMatthias Ringwald     switch (hfp_connection->codecs_state){
5943e3b9207SMilanka Ringwald         case HFP_CODECS_EXCHANGED:
5953e3b9207SMilanka Ringwald             if (hfp_connection->command == HFP_CMD_AVAILABLE_CODECS){
5963e3b9207SMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
5973e3b9207SMilanka Ringwald                 return 1;
5983e3b9207SMilanka Ringwald             }
5993e3b9207SMilanka Ringwald             break;
6003e3b9207SMilanka Ringwald 
601aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
602aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
603a814da6aSMatthias Ringwald             hfp_connection->ag_send_common_codec = true;
604aa4dd815SMatthias Ringwald             break;
605aa4dd815SMatthias Ringwald         default:
606aa4dd815SMatthias Ringwald             break;
607aa4dd815SMatthias Ringwald     }
608aa4dd815SMatthias Ringwald 
609a814da6aSMatthias Ringwald     if (hfp_connection->ag_send_common_codec){
610a814da6aSMatthias Ringwald         hfp_connection->ag_send_common_codec = false;
611a814da6aSMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
612a814da6aSMatthias Ringwald         hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
613a814da6aSMatthias Ringwald         hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
614a814da6aSMatthias Ringwald         return 1;
615a814da6aSMatthias Ringwald     }
616a814da6aSMatthias Ringwald 
617a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
618aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
619a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
620a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
621485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
622aa4dd815SMatthias Ringwald                 return 1;
623aa4dd815SMatthias Ringwald             }
624aa4dd815SMatthias Ringwald 
625a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
626aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
627a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
628aa4dd815SMatthias Ringwald                     break;
629aa4dd815SMatthias Ringwald                 default:
630aa4dd815SMatthias Ringwald                     break;
631aa4dd815SMatthias Ringwald             }
632485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
633aa4dd815SMatthias Ringwald             return 1;
634aa4dd815SMatthias Ringwald 
635aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
636a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
63740e8d6c5SMatthias Ringwald             hfp_connection->establish_audio_connection = 1;
6382561704cSMatthias Ringwald             hfp_connection->sco_for_msbc_failed = 0;
639485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
640aa4dd815SMatthias Ringwald             return 1;
641aa4dd815SMatthias Ringwald 
642aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
643a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
644a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
645485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
646aa4dd815SMatthias Ringwald                 return 1;
647aa4dd815SMatthias Ringwald             }
648a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
649a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
650c1ab6cc1SMatthias Ringwald             log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
651485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
6527522e673SMatthias Ringwald             // now, pick link settings
653afac2a04SMilanka Ringwald             hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
6540327df7dSMatthias Ringwald             // configure SBC coded if needed
6550327df7dSMatthias Ringwald             hfp_prepare_for_sco(hfp_connection);
656aa4dd815SMatthias Ringwald             return 1;
657aa4dd815SMatthias Ringwald         default:
658aa4dd815SMatthias Ringwald             break;
659aa4dd815SMatthias Ringwald     }
660aa4dd815SMatthias Ringwald     return 0;
661aa4dd815SMatthias Ringwald }
662aa4dd815SMatthias Ringwald 
663a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
664a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
6657095467fSMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection->local_role, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
666aa4dd815SMatthias Ringwald 
66740a8ee13SMatthias Ringwald     // in-band ring tone is active if supported
66840a8ee13SMatthias Ringwald     hfp_connection->ag_in_band_ring_tone_active = has_in_band_ring_tone();
66940a8ee13SMatthias Ringwald 
67080702f97SMatthias Ringwald     // HFP 4.35: "When [...] a new Service Level Connection is established all indicators are activated by default."
67180702f97SMatthias Ringwald     uint16_t i;
67280702f97SMatthias Ringwald     for (i=0;i<hfp_connection->ag_indicators_nr;i++){
67380702f97SMatthias Ringwald         hfp_connection->ag_indicators[i].enabled = 1;
67480702f97SMatthias Ringwald     }
67580702f97SMatthias Ringwald 
676a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
677d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
678a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
679aa4dd815SMatthias Ringwald     }
680ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
681c4f8fc1cSMilanka Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS){
682c4f8fc1cSMilanka Ringwald         switch (hfp_gsm_callsetup_status()){
683c4f8fc1cSMilanka Ringwald             case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
6842ff1d7d7SMilanka Ringwald                 hfp_ag_hf_start_ringing_incoming(hfp_connection);
685c4f8fc1cSMilanka Ringwald                 break;
686fcd59412SMilanka Ringwald             case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
687fcd59412SMilanka Ringwald                 hfp_ag_hf_start_ringing_outgoing(hfp_connection);
688fcd59412SMilanka Ringwald                 break;
689c4f8fc1cSMilanka Ringwald             default:
690c4f8fc1cSMilanka Ringwald                 break;
691c4f8fc1cSMilanka Ringwald         }
692ce263fc8SMatthias Ringwald     }
693aa4dd815SMatthias Ringwald }
6943deb3ec6SMatthias Ringwald 
695a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
696c79b4cabSMatthias Ringwald     // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
697a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
698c04cf7ffSMilanka Ringwald     int sent = 0;
699a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
7003deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
701a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
7023deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
703aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
704aeb0f0feSMatthias Ringwald                     hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_ag_codecs, &hfp_ag_codecs_nr);
705a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
706a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
707aa4dd815SMatthias Ringwald                     } else {
708a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
709aa4dd815SMatthias Ringwald                     }
710a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
711aa4dd815SMatthias Ringwald                     return 1;
7123deb3ec6SMatthias Ringwald                 default:
7133deb3ec6SMatthias Ringwald                     break;
7143deb3ec6SMatthias Ringwald             }
7153deb3ec6SMatthias Ringwald             break;
7163deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
717c04cf7ffSMilanka Ringwald             sent = codecs_exchange_state_machine(hfp_connection);
718a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
719a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
7203deb3ec6SMatthias Ringwald             }
721c04cf7ffSMilanka Ringwald             return sent;
722aa4dd815SMatthias Ringwald 
723aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
724a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
725473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
72684a0c24eSMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
727473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
728473ac565SMatthias Ringwald                 // continue below in state switch
729ad902e3dSMatthias Ringwald             }
730ad902e3dSMatthias Ringwald             break;
731aa4dd815SMatthias Ringwald 
732aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
733a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
734a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
735485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
736aa4dd815SMatthias Ringwald             return 1;
737aa4dd815SMatthias Ringwald 
7383deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
739a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
740a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
741a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
742a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
743a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
744aa4dd815SMatthias Ringwald             } else {
745a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
7463deb3ec6SMatthias Ringwald             }
7478a46ec40SMatthias Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
748aa4dd815SMatthias Ringwald             return 1;
7493deb3ec6SMatthias Ringwald 
750aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
751a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
752a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
753a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
754aa4dd815SMatthias Ringwald             }
755485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
756a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
757a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
758ce263fc8SMatthias Ringwald             }
759aa4dd815SMatthias Ringwald             return 1;
760aa4dd815SMatthias Ringwald 
761aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
762a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
763a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
764485ac19eSMilanka Ringwald             hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
765aa4dd815SMatthias Ringwald             return 1;
766aa4dd815SMatthias Ringwald 
767aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
768a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
769a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
770485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
771aa4dd815SMatthias Ringwald             return 1;
772aa4dd815SMatthias Ringwald 
773aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
774a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
775485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
776a7666b02SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
777aa4dd815SMatthias Ringwald             return 1;
7783deb3ec6SMatthias Ringwald         default:
7793deb3ec6SMatthias Ringwald             break;
7803deb3ec6SMatthias Ringwald     }
781473ac565SMatthias Ringwald 
782473ac565SMatthias Ringwald     switch (hfp_connection->state){
783473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
784485ac19eSMilanka Ringwald             int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
785473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
786473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
787473ac565SMatthias Ringwald             if (next_segment < num_segments){
788473ac565SMatthias Ringwald                 // prepare sending of next segment
789473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
790473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
791473ac565SMatthias Ringwald             } else {
792473ac565SMatthias Ringwald                 // done, go to next state
793473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
794473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
795473ac565SMatthias Ringwald             }
796473ac565SMatthias Ringwald             return 1;
797473ac565SMatthias Ringwald         }
798473ac565SMatthias Ringwald         default:
799473ac565SMatthias Ringwald             break;
800473ac565SMatthias Ringwald     }
801c04cf7ffSMilanka Ringwald     return 0;
8023deb3ec6SMatthias Ringwald }
8033deb3ec6SMatthias Ringwald 
8049cd26862SMilanka Ringwald static bool hfp_ag_vra_flag_supported(hfp_connection_t * hfp_connection){
80514c148caSMilanka Ringwald     int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION);
80614c148caSMilanka Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION);
80714c148caSMilanka Ringwald     return hf && ag;
80814c148caSMilanka Ringwald }
80914c148caSMilanka Ringwald 
8109cd26862SMilanka Ringwald static bool hfp_ag_enhanced_vra_flag_supported(hfp_connection_t * hfp_connection){
81114c148caSMilanka Ringwald     int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS);
81214c148caSMilanka Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_ENHANCED_VOICE_RECOGNITION_STATUS);
81314c148caSMilanka Ringwald     return hf && ag;
81414c148caSMilanka Ringwald }
81514c148caSMilanka Ringwald 
8169cd26862SMilanka Ringwald static bool hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection_t * hfp_connection){
817f71d2aacSMilanka Ringwald     int ag = get_bit(hfp_ag_supported_features, HFP_AGSF_VOICE_RECOGNITION_TEXT);
818f71d2aacSMilanka Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_TEXT);
819f71d2aacSMilanka Ringwald     return hf && ag;
82008a0b01cSMilanka Ringwald }
821f71d2aacSMilanka Ringwald 
822135d9718SMilanka Ringwald static bool hfp_ag_can_activate_voice_recognition(hfp_connection_t * hfp_connection){
823f71d2aacSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
824135d9718SMilanka Ringwald         return false;
825d84719b1SMilanka Ringwald     }
8260c738fd4SMilanka Ringwald     if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_OFF){
827135d9718SMilanka Ringwald         return false;
828d84719b1SMilanka Ringwald     }
82908dc7cc6SMatthias Ringwald     if (hfp_connection->ag_vra_send_command){
83008dc7cc6SMatthias Ringwald         return false;
83108dc7cc6SMatthias Ringwald     }
832135d9718SMilanka Ringwald     return true;
833d84719b1SMilanka Ringwald }
834d84719b1SMilanka Ringwald 
835135d9718SMilanka Ringwald static bool hfp_ag_voice_recognition_session_active(hfp_connection_t * hfp_connection){
83608a0b01cSMilanka Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED || hfp_connection->state > HFP_AUDIO_CONNECTION_ESTABLISHED){
837135d9718SMilanka Ringwald         return false;
83808a0b01cSMilanka Ringwald     }
8390c738fd4SMilanka Ringwald     if (hfp_connection->vra_state != HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
840de9e0ea7SMilanka Ringwald         if (hfp_connection->vra_state == HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){
841135d9718SMilanka Ringwald             return true;
842de9e0ea7SMilanka Ringwald         }
843135d9718SMilanka Ringwald         return false;
844d84719b1SMilanka Ringwald     }
845135d9718SMilanka Ringwald     return true;
846d84719b1SMilanka Ringwald }
847754910caSMilanka Ringwald 
8480b4debbfSMilanka Ringwald static bool hfp_ag_is_audio_connection_active(hfp_connection_t * hfp_connection){
8490b4debbfSMilanka Ringwald     switch (hfp_connection->state){
8500b4debbfSMilanka Ringwald         case HFP_W2_CONNECT_SCO:
8510b4debbfSMilanka Ringwald         case HFP_W4_SCO_CONNECTED:
8520b4debbfSMilanka Ringwald         case HFP_AUDIO_CONNECTION_ESTABLISHED:
8530b4debbfSMilanka Ringwald             return true;
854754910caSMilanka Ringwald         default:
8550b4debbfSMilanka Ringwald             return false;
856754910caSMilanka Ringwald     }
857754910caSMilanka Ringwald }
858754910caSMilanka Ringwald 
859b95cac54SMilanka Ringwald static void hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection_t * hfp_connection, uint8_t status){
860b95cac54SMilanka Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
861b95cac54SMilanka Ringwald 
862b95cac54SMilanka Ringwald     uint8_t event[6];
863b95cac54SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
864b95cac54SMilanka Ringwald     event[1] = sizeof(event) - 2;
865c95b5b3cSMilanka Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_VOICE_RECOGNITION_AG_MESSAGE_SENT;
866b95cac54SMilanka Ringwald     little_endian_store_16(event, 3, acl_handle);
867b95cac54SMilanka Ringwald     event[5] = status;
868b95cac54SMilanka Ringwald     (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
869b95cac54SMilanka Ringwald }
870b95cac54SMilanka Ringwald 
871347d46c8SMilanka Ringwald 
872585db78dSMilanka Ringwald static void hfp_ag_emit_hf_indicator_value(hfp_connection_t * hfp_connection, uint16_t uuid, uint8_t value){
873347d46c8SMilanka Ringwald     hci_con_handle_t acl_handle = (hfp_connection != NULL) ? hfp_connection->acl_handle : HCI_CON_HANDLE_INVALID;
874347d46c8SMilanka Ringwald 
875347d46c8SMilanka Ringwald     uint8_t event[8];
876347d46c8SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
877347d46c8SMilanka Ringwald     event[1] = sizeof(event) - 2;
878347d46c8SMilanka Ringwald     event[2] = HFP_SUBEVENT_HF_INDICATOR;
879347d46c8SMilanka Ringwald     little_endian_store_16(event, 3, acl_handle);
880347d46c8SMilanka Ringwald     little_endian_store_16(event, 5, uuid);
881347d46c8SMilanka Ringwald     event[7] = value;
882347d46c8SMilanka Ringwald     (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
883347d46c8SMilanka Ringwald }
884347d46c8SMilanka Ringwald 
885a4f85bd2SMilanka Ringwald static void hfp_ag_emit_general_simple_event(uint8_t event_subtype){
886a4f85bd2SMilanka Ringwald     if (!hfp_ag_callback) return;
887a4f85bd2SMilanka Ringwald 
888a4f85bd2SMilanka Ringwald     uint8_t event[5];
889a4f85bd2SMilanka Ringwald     event[0] = HCI_EVENT_HFP_META;
890a4f85bd2SMilanka Ringwald     event[1] = sizeof(event) - 2;
891a4f85bd2SMilanka Ringwald     event[2] = event_subtype;
892a4f85bd2SMilanka Ringwald     little_endian_store_16(event, 3, HCI_CON_HANDLE_INVALID);
893a4f85bd2SMilanka Ringwald     (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
894a4f85bd2SMilanka Ringwald }
895a4f85bd2SMilanka Ringwald 
896471dea41SMatthias Ringwald static void hfp_ag_emit_custom_command_event(hfp_connection_t * hfp_connection){
897471dea41SMatthias Ringwald     btstack_assert(sizeof(hfp_connection->line_buffer) < (255-5));
898471dea41SMatthias Ringwald 
899471dea41SMatthias Ringwald     uint16_t line_len = strlen((const char*)hfp_connection->line_buffer) + 1;
900471dea41SMatthias Ringwald     uint8_t event[7 + sizeof(hfp_connection->line_buffer)];
901471dea41SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
902471dea41SMatthias Ringwald     event[1] = 5 + line_len;
903471dea41SMatthias Ringwald     event[2] = HFP_SUBEVENT_CUSTOM_AT_COMMAND;
904471dea41SMatthias Ringwald     little_endian_store_16(event, 3, hfp_connection->acl_handle);
9056d9a41f9SMatthias Ringwald     little_endian_store_16(event, 5, hfp_connection->custom_at_command_id);
906471dea41SMatthias Ringwald     memcpy(&event[7], hfp_connection->line_buffer, line_len);
907471dea41SMatthias Ringwald     (*hfp_ag_callback)(HCI_EVENT_PACKET, 0, event, 7 + line_len);
908471dea41SMatthias Ringwald }
909471dea41SMatthias Ringwald 
910774e3016SMatthias Ringwald // @return status
911774e3016SMatthias Ringwald static uint8_t hfp_ag_vra_state_machine_two(hfp_connection_t * hfp_connection){
912774e3016SMatthias Ringwald     uint8_t status = ERROR_CODE_SUCCESS;
913774e3016SMatthias Ringwald     switch (hfp_connection->vra_state_requested){
914774e3016SMatthias Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO:
915774e3016SMatthias Ringwald             if (!hfp_ag_is_audio_connection_active(hfp_connection)){
916774e3016SMatthias Ringwald                 status = hfp_ag_setup_audio_connection(hfp_connection);
917774e3016SMatthias Ringwald                 if (status != ERROR_CODE_SUCCESS){
918774e3016SMatthias Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
919774e3016SMatthias Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, status);
920774e3016SMatthias Ringwald                     break;
921774e3016SMatthias Ringwald                 }
922774e3016SMatthias Ringwald             }
923774e3016SMatthias Ringwald             hfp_connection->enhanced_voice_recognition_enabled = true;
924774e3016SMatthias Ringwald             hfp_connection->vra_state = HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
925774e3016SMatthias Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
926774e3016SMatthias Ringwald             hfp_emit_enhanced_voice_recognition_hf_ready_for_audio_event(hfp_connection, ERROR_CODE_SUCCESS);
927774e3016SMatthias Ringwald             break;
928774e3016SMatthias Ringwald 
929774e3016SMatthias Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
930774e3016SMatthias Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_OFF;
931774e3016SMatthias Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
932774e3016SMatthias Ringwald 
933774e3016SMatthias Ringwald             // release audio connection only if it was opened after audio VR activated
934774e3016SMatthias Ringwald             if (hfp_ag_is_audio_connection_active(hfp_connection) && !hfp_connection->ag_audio_connection_opened_before_vra){
935774e3016SMatthias Ringwald                 hfp_trigger_release_audio_connection(hfp_connection);
936774e3016SMatthias Ringwald             }
937774e3016SMatthias Ringwald 
938774e3016SMatthias Ringwald             hfp_emit_voice_recognition_disabled(hfp_connection, ERROR_CODE_SUCCESS);
939774e3016SMatthias Ringwald             break;
940774e3016SMatthias Ringwald 
941774e3016SMatthias Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
942774e3016SMatthias Ringwald             // open only if audio connection does not already exist
943774e3016SMatthias Ringwald             if (!hfp_ag_is_audio_connection_active(hfp_connection)){
944774e3016SMatthias Ringwald                 status = hfp_ag_setup_audio_connection(hfp_connection);
945774e3016SMatthias Ringwald                 if (status != ERROR_CODE_SUCCESS){
946774e3016SMatthias Ringwald                     hfp_connection->vra_state_requested = hfp_connection->vra_state;
947774e3016SMatthias Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, status);
948774e3016SMatthias Ringwald                     break;
949774e3016SMatthias Ringwald                 }
950774e3016SMatthias Ringwald             }
951774e3016SMatthias Ringwald 
952774e3016SMatthias Ringwald             hfp_connection->vra_state = HFP_VRA_VOICE_RECOGNITION_ACTIVATED;
953774e3016SMatthias Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
954774e3016SMatthias Ringwald             hfp_connection->enhanced_voice_recognition_enabled = hfp_ag_enhanced_vra_flag_supported(hfp_connection);
95584fb9ac1SMilanka Ringwald 
95684fb9ac1SMilanka Ringwald             if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
957774e3016SMatthias Ringwald                 hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
95884fb9ac1SMilanka Ringwald             } else {
95984fb9ac1SMilanka Ringwald                 hfp_connection->emit_vra_enabled_after_audio_established = true;
96084fb9ac1SMilanka Ringwald             }
961774e3016SMatthias Ringwald             break;
962774e3016SMatthias Ringwald 
963774e3016SMatthias Ringwald         default:
964774e3016SMatthias Ringwald             break;
965774e3016SMatthias Ringwald     }
966774e3016SMatthias Ringwald     return status;
967774e3016SMatthias Ringwald }
968774e3016SMatthias Ringwald 
96908dc7cc6SMatthias Ringwald static uint8_t hfp_ag_vra_send_command(hfp_connection_t * hfp_connection){
97008dc7cc6SMatthias Ringwald     uint8_t done = 0;
971754910caSMilanka Ringwald     uint8_t status;
972754910caSMilanka Ringwald 
973cf75be85SMilanka Ringwald     switch (hfp_connection->vra_state_requested){
974b95cac54SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG:
975b95cac54SMilanka Ringwald             done = hfp_ag_send_enhanced_voice_recognition_msg_cmd(hfp_connection);
976b95cac54SMilanka Ringwald             if (done == 0){
977b95cac54SMilanka Ringwald                 hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
978b95cac54SMilanka Ringwald             } else {
979b95cac54SMilanka Ringwald                 hfp_ag_emit_enhanced_voice_recognition_msg_sent_event(hfp_connection, ERROR_CODE_SUCCESS);
980b95cac54SMilanka Ringwald             }
981b95cac54SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
982b95cac54SMilanka Ringwald             return done;
983b95cac54SMilanka Ringwald 
984cf75be85SMilanka Ringwald         case HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS:
985cf75be85SMilanka Ringwald             done = hfp_ag_send_enhanced_voice_recognition_state_cmd(hfp_connection);
986cf75be85SMilanka Ringwald             if (done == 0){
987cf75be85SMilanka Ringwald                 hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_UNSPECIFIED_ERROR);
988cf75be85SMilanka Ringwald             } else {
989cf75be85SMilanka Ringwald                 hfp_emit_enhanced_voice_recognition_state_event(hfp_connection, ERROR_CODE_SUCCESS);
990cf75be85SMilanka Ringwald             }
991cf75be85SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
992b95cac54SMilanka Ringwald             return done;
993b95cac54SMilanka Ringwald 
9946e13e408SMatthias Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED:
9956e13e408SMatthias Ringwald         case HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF:
996d1b50446SMilanka Ringwald             done = hfp_ag_send_voice_recognition_cmd(hfp_connection, hfp_connection->ag_activate_voice_recognition_value);
9970c738fd4SMilanka Ringwald             if (done == 0){
9980c738fd4SMilanka Ringwald                 hfp_connection->vra_state_requested = hfp_connection->vra_state;
999553a4a56SMilanka Ringwald 
1000553a4a56SMilanka Ringwald                 if (hfp_connection->ag_activate_voice_recognition_value == 1){
1001553a4a56SMilanka Ringwald                     hfp_emit_voice_recognition_enabled(hfp_connection, done);
1002553a4a56SMilanka Ringwald                 } else {
1003553a4a56SMilanka Ringwald                     hfp_emit_voice_recognition_disabled(hfp_connection, done);
1004553a4a56SMilanka Ringwald                 }
1005fd4151d1SMilanka Ringwald                 return 0;
1006fd4151d1SMilanka Ringwald             }
100708dc7cc6SMatthias Ringwald             status = hfp_ag_vra_state_machine_two(hfp_connection);
100808dc7cc6SMatthias Ringwald             return (status == ERROR_CODE_SUCCESS) ? 1 : 0;
100908dc7cc6SMatthias Ringwald 
10106e13e408SMatthias Ringwald         default:
10116e13e408SMatthias Ringwald             log_error("state %u", hfp_connection->vra_state_requested);
10126e13e408SMatthias Ringwald             btstack_assert(false);
101308dc7cc6SMatthias Ringwald             return 0;
1014cf75be85SMilanka Ringwald     }
101508dc7cc6SMatthias Ringwald }
101608dc7cc6SMatthias Ringwald 
101708dc7cc6SMatthias Ringwald static int hfp_ag_voice_recognition_state_machine(hfp_connection_t * hfp_connection){
101808dc7cc6SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
101908dc7cc6SMatthias Ringwald         return 0;
102008dc7cc6SMatthias Ringwald     }
102108dc7cc6SMatthias Ringwald     int done = 0;
102208dc7cc6SMatthias Ringwald     uint8_t status;
102308dc7cc6SMatthias Ringwald 
10248f8818a4SMatthias Ringwald     // VRA action initiated by AG
10258f8818a4SMatthias Ringwald     if (hfp_connection->ag_vra_send_command){
10268f8818a4SMatthias Ringwald         hfp_connection->ag_vra_send_command = false;
10278f8818a4SMatthias Ringwald         return hfp_ag_vra_send_command(hfp_connection);
10288f8818a4SMatthias Ringwald     }
10298f8818a4SMatthias Ringwald 
1030e7c46708SMatthias Ringwald     if (hfp_connection->ag_vra_requested_by_hf){
1031e7c46708SMatthias Ringwald         hfp_connection->ag_vra_requested_by_hf = false;
1032e7c46708SMatthias Ringwald 
1033734ef2bdSMilanka Ringwald         // HF initiatied voice recognition, parser extracted activation value
1034de9e0ea7SMilanka Ringwald         switch (hfp_connection->ag_activate_voice_recognition_value){
1035de9e0ea7SMilanka Ringwald             case 0:
1036135d9718SMilanka Ringwald                 if (hfp_ag_voice_recognition_session_active(hfp_connection)){
1037fd4151d1SMilanka Ringwald                     hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
1038fd4151d1SMilanka Ringwald                     done = hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1039fd4151d1SMilanka Ringwald                 }
1040de9e0ea7SMilanka Ringwald                 break;
1041de9e0ea7SMilanka Ringwald 
1042de9e0ea7SMilanka Ringwald                 case 1:
1043135d9718SMilanka Ringwald                     if (hfp_ag_can_activate_voice_recognition(hfp_connection)){
1044fd4151d1SMilanka Ringwald                         hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
1045fd4151d1SMilanka Ringwald                         done = hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1046fd4151d1SMilanka Ringwald                     }
1047de9e0ea7SMilanka Ringwald                     break;
1048de9e0ea7SMilanka Ringwald 
1049de9e0ea7SMilanka Ringwald                     case 2:
1050135d9718SMilanka Ringwald                         if (hfp_ag_voice_recognition_session_active(hfp_connection)){
1051de9e0ea7SMilanka Ringwald                             hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO;
1052de9e0ea7SMilanka Ringwald                             done = hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1053de9e0ea7SMilanka Ringwald                         }
1054de9e0ea7SMilanka Ringwald                         break;
1055de9e0ea7SMilanka Ringwald                         default:
1056de9e0ea7SMilanka Ringwald                             break;
1057fd4151d1SMilanka Ringwald         }
1058fd4151d1SMilanka Ringwald         if (done == 0){
1059fd4151d1SMilanka Ringwald             hfp_connection->vra_state_requested = hfp_connection->vra_state;
1060fd4151d1SMilanka Ringwald             done = hfp_ag_send_error(hfp_connection->rfcomm_cid);
1061fd4151d1SMilanka Ringwald             return 1;
1062fd4151d1SMilanka Ringwald         }
1063754910caSMilanka Ringwald 
1064774e3016SMatthias Ringwald         status = hfp_ag_vra_state_machine_two(hfp_connection);
10652cc52945SMatthias Ringwald         return (status == ERROR_CODE_SUCCESS) ? 1 : 0;
1066de9e0ea7SMilanka Ringwald     }
10672cc52945SMatthias Ringwald 
10682cc52945SMatthias Ringwald     return 0;
1069754910caSMilanka Ringwald }
1070754910caSMilanka Ringwald 
1071a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
1072dc6af379SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) {
1073dc6af379SMatthias Ringwald         return 0;
1074dc6af379SMatthias Ringwald     }
1075dc6af379SMatthias Ringwald 
1076c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
1077c04cf7ffSMilanka Ringwald     if (sent) return 1;
1078aa4dd815SMatthias Ringwald 
107940a8ee13SMatthias Ringwald     if (has_in_band_ring_tone()){
108040a8ee13SMatthias Ringwald         if (hfp_connection->ag_in_band_ring_tone_active != hfp_ag_in_band_ring_tone_active) {
108140a8ee13SMatthias Ringwald             hfp_connection->ag_in_band_ring_tone_active = hfp_ag_in_band_ring_tone_active;
1082f197e761SMatthias Ringwald             hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection);
1083aa4dd815SMatthias Ringwald             return 1;
10849430c71eSMatthias Ringwald         }
108540a8ee13SMatthias Ringwald     }
10869430c71eSMatthias Ringwald 
10879430c71eSMatthias Ringwald     switch(hfp_connection->command){
1088aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
1089485ac19eSMilanka Ringwald             hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
1090aa4dd815SMatthias Ringwald             return 1;
1091aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
1092a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
1093485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
1094aa4dd815SMatthias Ringwald             } else {
1095485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
10963deb3ec6SMatthias Ringwald             }
1097aa4dd815SMatthias Ringwald             return 1;
1098aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
1099485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1100aa4dd815SMatthias Ringwald             return 1;
11013deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
1102df9853c5SMatthias Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1103df9853c5SMatthias Ringwald             return 1;
11043deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
1105485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1106ce263fc8SMatthias Ringwald             return 1;
11073deb3ec6SMatthias Ringwald         default:
11083deb3ec6SMatthias Ringwald             break;
11093deb3ec6SMatthias Ringwald     }
1110aa4dd815SMatthias Ringwald     return 0;
11113deb3ec6SMatthias Ringwald }
11123deb3ec6SMatthias Ringwald 
1113a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
1114505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
1115505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
11163deb3ec6SMatthias Ringwald 
1117a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
111852cbdd6dSMilanka Ringwald 
111952cbdd6dSMilanka Ringwald     if (hfp_connection->release_audio_connection){
112052cbdd6dSMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
112152cbdd6dSMilanka Ringwald         hfp_connection->release_audio_connection = 0;
112252cbdd6dSMilanka Ringwald         gap_disconnect(hfp_connection->sco_handle);
112352cbdd6dSMilanka Ringwald         return 1;
112452cbdd6dSMilanka Ringwald     }
1125aa4dd815SMatthias Ringwald 
112629cddf58SMatthias Ringwald     // accept incoming audio connection (codec negotiation is not used)
1127447743f7SMatthias Ringwald     if (hfp_connection->accept_sco && (hfp_sco_setup_active() == false)){
112829cddf58SMatthias Ringwald         // notify about codec selection if not done already
112929cddf58SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
113029cddf58SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
113129cddf58SMatthias Ringwald         }
113229cddf58SMatthias Ringwald         //
113329cddf58SMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
113429cddf58SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
113529cddf58SMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
113629cddf58SMatthias Ringwald         return 1;
113729cddf58SMatthias Ringwald     }
113829cddf58SMatthias Ringwald 
1139aa4dd815SMatthias Ringwald     // run codecs exchange
1140c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
1141c04cf7ffSMilanka Ringwald     if (sent) return 1;
1142aa4dd815SMatthias Ringwald 
1143c04cf7ffSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
1144447743f7SMatthias Ringwald     if (hfp_sco_setup_active()) return 0;
1145d2e34ffbSMatthias Ringwald     if (hci_can_send_command_packet_now() == false) return 0;
1146a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
1147a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
1148a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
1149eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
1150aa4dd815SMatthias Ringwald         return 1;
1151aa4dd815SMatthias Ringwald     }
1152aa4dd815SMatthias Ringwald     return 0;
1153aa4dd815SMatthias Ringwald }
1154aa4dd815SMatthias Ringwald 
1155056efd45SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
1156056efd45SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1157056efd45SMatthias Ringwald     if (!indicator){
1158056efd45SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
1159056efd45SMatthias Ringwald         return;
1160056efd45SMatthias Ringwald     };
1161056efd45SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1162056efd45SMatthias Ringwald }
1163056efd45SMatthias Ringwald 
1164056efd45SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1165056efd45SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1166056efd45SMatthias Ringwald     if (!indicator){
1167056efd45SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
1168056efd45SMatthias Ringwald         return;
1169056efd45SMatthias Ringwald     };
1170056efd45SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1171056efd45SMatthias Ringwald }
1172056efd45SMatthias Ringwald 
1173aa4dd815SMatthias Ringwald //
1174fcd59412SMilanka Ringwald // transition implementations for hfp_ag_call_state_machine
1175aa4dd815SMatthias Ringwald //
1176aa4dd815SMatthias Ringwald 
11772ff1d7d7SMilanka Ringwald static void hfp_ag_hf_start_ringing_incoming(hfp_connection_t * hfp_connection){
1178f197e761SMatthias Ringwald     if (use_in_band_tone(hfp_connection)){
1179a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
1180d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1181aa4dd815SMatthias Ringwald     } else {
11829493406bSMilanka Ringwald         hfp_connection->call_state = HFP_CALL_INCOMING_RINGING;
1183aa4dd815SMatthias Ringwald     }
1184aa4dd815SMatthias Ringwald }
1185aa4dd815SMatthias Ringwald 
1186fcd59412SMilanka Ringwald static void hfp_ag_hf_start_ringing_outgoing(hfp_connection_t * hfp_connection){
1187fcd59412SMilanka Ringwald     hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1188fcd59412SMilanka Ringwald }
1189fcd59412SMilanka Ringwald 
1190a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
1191a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
1192a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
1193aa4dd815SMatthias Ringwald }
1194aa4dd815SMatthias Ringwald 
1195a4f85bd2SMilanka Ringwald 
119631f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_during_active_one(void){
119731f55b91SMilanka Ringwald     btstack_linked_list_iterator_t it;
119831f55b91SMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
119931f55b91SMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
120031f55b91SMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
120131f55b91SMilanka Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
120231f55b91SMilanka Ringwald         if (hfp_connection->call_state != HFP_CALL_ACTIVE) continue;
120331f55b91SMilanka Ringwald 
120431f55b91SMilanka Ringwald         hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
120531f55b91SMilanka Ringwald         hfp_ag_run_for_context(hfp_connection);
120631f55b91SMilanka Ringwald     }
120731f55b91SMilanka Ringwald }
120831f55b91SMilanka Ringwald 
1209d3709f3eSMatthias Ringwald static void hfp_ag_trigger_ring_and_clip(void) {
1210d3709f3eSMatthias Ringwald     btstack_linked_list_iterator_t it;
1211d3709f3eSMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1212d3709f3eSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1213d3709f3eSMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1214d3709f3eSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1215d3709f3eSMatthias Ringwald         switch (hfp_connection->call_state){
1216d3709f3eSMatthias Ringwald             case HFP_CALL_INCOMING_RINGING:
1217d3709f3eSMatthias Ringwald             case HFP_CALL_OUTGOING_RINGING:
1218d3709f3eSMatthias Ringwald             case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
12193561686eSMatthias Ringwald                 // Queue RING on this connection
1220a4f85bd2SMilanka Ringwald                 hfp_connection->ag_ring = 1;
12213561686eSMatthias Ringwald 
12223561686eSMatthias Ringwald                 // HFP v1.8, 4.23 Calling Line Identification (CLI) Notification
12233561686eSMatthias Ringwald                 // "If the calling subscriber number information is available from the network, the AG shall issue the
12243561686eSMatthias Ringwald                 // +CLIP unsolicited result code just after every RING indication when the HF is alerted in an incoming call."
12253561686eSMatthias Ringwald                 hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
12263561686eSMatthias Ringwald 
12273561686eSMatthias Ringwald                 // trigger next message
1228a4f85bd2SMilanka Ringwald                 rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1229b7e27351SMilanka Ringwald                 break;
1230b7e27351SMilanka Ringwald             default:
1231b7e27351SMilanka Ringwald                 break;
1232b7e27351SMilanka Ringwald         }
1233a4f85bd2SMilanka Ringwald     }
1234a4f85bd2SMilanka Ringwald }
1235a4f85bd2SMilanka Ringwald 
123650b9afa4SMilanka Ringwald // trigger RING and CLIP messages on connections that are ringing
123750b9afa4SMilanka Ringwald static void hfp_ag_ring_timeout_handler(btstack_timer_source_t * timer){
123850b9afa4SMilanka Ringwald     hfp_ag_trigger_ring_and_clip();
123950b9afa4SMilanka Ringwald 
12406db648bcSMilanka Ringwald     btstack_run_loop_set_timer(timer, HFP_RING_PERIOD_MS); // 2 seconds timeout
124150b9afa4SMilanka Ringwald     btstack_run_loop_add_timer(timer);
1242a4f85bd2SMilanka Ringwald }
1243a4f85bd2SMilanka Ringwald 
1244a4f85bd2SMilanka Ringwald static void hfp_ag_ring_timeout_stop(void){
1245a4f85bd2SMilanka Ringwald     btstack_run_loop_remove_timer(&hfp_ag_ring_timeout);
1246a4f85bd2SMilanka Ringwald }
1247a4f85bd2SMilanka Ringwald 
1248a4f85bd2SMilanka Ringwald static void hfp_ag_start_ringing(void){
124950b9afa4SMilanka Ringwald     // setup ring timer
125050b9afa4SMilanka Ringwald     btstack_run_loop_remove_timer(&hfp_ag_ring_timeout);
125150b9afa4SMilanka Ringwald     btstack_run_loop_set_timer_handler(&hfp_ag_ring_timeout, hfp_ag_ring_timeout_handler);
12526db648bcSMilanka Ringwald     btstack_run_loop_set_timer(&hfp_ag_ring_timeout, HFP_RING_PERIOD_MS); // 2 seconds timeout
125350b9afa4SMilanka Ringwald     btstack_run_loop_add_timer(&hfp_ag_ring_timeout);
125450b9afa4SMilanka Ringwald 
125550b9afa4SMilanka Ringwald     // emit start ringing
1256a4f85bd2SMilanka Ringwald     hfp_ag_emit_general_simple_event(HFP_SUBEVENT_START_RINGING);
125750b9afa4SMilanka Ringwald 
125850b9afa4SMilanka Ringwald     // send initial RING + CLIP
125950b9afa4SMilanka Ringwald     hfp_ag_trigger_ring_and_clip();
1260a4f85bd2SMilanka Ringwald }
1261a4f85bd2SMilanka Ringwald 
1262a4f85bd2SMilanka Ringwald static void hfp_ag_stop_ringing(void){
1263a4f85bd2SMilanka Ringwald     hfp_ag_ring_timeout_stop();
1264a4f85bd2SMilanka Ringwald     hfp_ag_emit_general_simple_event(HFP_SUBEVENT_STOP_RINGING);
1265a4f85bd2SMilanka Ringwald 
1266a4f85bd2SMilanka Ringwald     btstack_linked_list_iterator_t it;
1267a4f85bd2SMilanka Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1268a4f85bd2SMilanka Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1269a4f85bd2SMilanka Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1270a4f85bd2SMilanka Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1271a4f85bd2SMilanka Ringwald         if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) &&
1272a4f85bd2SMilanka Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1273a4f85bd2SMilanka Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1274a4f85bd2SMilanka Ringwald     }
1275a4f85bd2SMilanka Ringwald }
1276a4f85bd2SMilanka Ringwald 
127731f55b91SMilanka Ringwald static void hfp_ag_trigger_incoming_call_idle(void){
1278aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
1279aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
1280aa4dd815SMatthias Ringwald 
12813561686eSMatthias Ringwald     // update callsetup state in connections
1282665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1283665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1284665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1285a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
128666c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
128731f55b91SMilanka Ringwald         if (hfp_connection->call_state != HFP_CALL_IDLE) continue;
128831f55b91SMilanka Ringwald 
1289a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1290a4f85bd2SMilanka Ringwald 
1291a4f85bd2SMilanka Ringwald         hfp_ag_hf_start_ringing_incoming(hfp_connection);
1292a4f85bd2SMilanka Ringwald 
1293f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1294aa4dd815SMatthias Ringwald     }
12953561686eSMatthias Ringwald 
12963561686eSMatthias Ringwald     // start local ringing - started after connection state has been updated
12973561686eSMatthias Ringwald     hfp_ag_start_ringing();
1298aa4dd815SMatthias Ringwald }
1299aa4dd815SMatthias Ringwald 
1300fe899794SMatthias Ringwald static void hfp_ag_trigger_outgoing_call(void){
1301fe899794SMatthias Ringwald     btstack_linked_list_iterator_t it;
1302fe899794SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1303fe899794SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1304fe899794SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1305fe899794SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1306fe899794SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1307fe899794SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
1308fe899794SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
1309fe899794SMatthias Ringwald         }
1310fe899794SMatthias Ringwald     }
1311fe899794SMatthias Ringwald }
13125a7033e2SMatthias Ringwald 
13135a7033e2SMatthias Ringwald static void hfp_ag_handle_outgoing_call_accepted(hfp_connection_t * hfp_connection){
13145a7033e2SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
13155a7033e2SMatthias Ringwald 
13165a7033e2SMatthias Ringwald     // trigger callsetup to be
13175a7033e2SMatthias Ringwald     int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
13185a7033e2SMatthias Ringwald     hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL);
13195a7033e2SMatthias Ringwald 
13205a7033e2SMatthias Ringwald     int indicator_index ;
13215a7033e2SMatthias Ringwald 
13225a7033e2SMatthias Ringwald     hfp_ag_set_callsetup_indicator();
13235a7033e2SMatthias Ringwald     indicator_index = get_ag_indicator_index_for_name("callsetup");
13245a7033e2SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
13255a7033e2SMatthias Ringwald 
13265a7033e2SMatthias Ringwald     // put current call on hold if active
13275a7033e2SMatthias Ringwald     if (put_call_on_hold){
13285a7033e2SMatthias Ringwald         log_info("AG putting current call on hold for new outgoing call");
13295a7033e2SMatthias Ringwald         hfp_ag_set_callheld_indicator();
13305a7033e2SMatthias Ringwald         indicator_index = get_ag_indicator_index_for_name("callheld");
13315a7033e2SMatthias Ringwald         hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
13325a7033e2SMatthias Ringwald     }
13335a7033e2SMatthias Ringwald 
13345a7033e2SMatthias Ringwald     // start audio if needed
13355a7033e2SMatthias Ringwald     hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
13365a7033e2SMatthias Ringwald }
1337fe899794SMatthias Ringwald 
13384a2e058fSMatthias Ringwald static void hfp_ag_transfer_indicator(const char * name){
13394a2e058fSMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
1340aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
1341aa4dd815SMatthias Ringwald 
1342665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1343665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1344665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1345a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
134666c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1347a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1348a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1349f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1350aa4dd815SMatthias Ringwald     }
1351aa4dd815SMatthias Ringwald }
1352aa4dd815SMatthias Ringwald 
13534a2e058fSMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
13544a2e058fSMatthias Ringwald     hfp_ag_transfer_indicator("callsetup");
13554a2e058fSMatthias Ringwald }
13564a2e058fSMatthias Ringwald 
1357aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
13584a2e058fSMatthias Ringwald     hfp_ag_transfer_indicator("call");
1359aa4dd815SMatthias Ringwald }
1360aa4dd815SMatthias Ringwald 
1361aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
13624a2e058fSMatthias Ringwald     hfp_ag_transfer_indicator("callheld");
1363aa4dd815SMatthias Ringwald }
1364aa4dd815SMatthias Ringwald 
1365aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
1366aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1367aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1368aa4dd815SMatthias Ringwald 
1369a4f85bd2SMilanka Ringwald     hfp_ag_stop_ringing();
1370a4f85bd2SMilanka Ringwald 
1371665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1372665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1373665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1374a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
137566c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
13769493406bSMilanka Ringwald         if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) &&
1377505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1378aa4dd815SMatthias Ringwald 
1379a0ffb263SMatthias Ringwald         if (hfp_connection == source){
1380aa4dd815SMatthias Ringwald 
1381f197e761SMatthias Ringwald             if (use_in_band_tone(hfp_connection)){
1382a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
1383aa4dd815SMatthias Ringwald             } else {
1384a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
1385d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1386aa4dd815SMatthias Ringwald             }
1387aa4dd815SMatthias Ringwald 
1388a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1389a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1390aa4dd815SMatthias Ringwald 
1391aa4dd815SMatthias Ringwald         } else {
1392a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1393aa4dd815SMatthias Ringwald         }
1394f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1395aa4dd815SMatthias Ringwald     }
1396aa4dd815SMatthias Ringwald }
1397aa4dd815SMatthias Ringwald 
1398aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
1399aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1400aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1401aa4dd815SMatthias Ringwald 
1402a4f85bd2SMilanka Ringwald     hfp_ag_stop_ringing();
1403a4f85bd2SMilanka Ringwald 
1404665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1405665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1406665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1407a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
140866c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1409a5764cd2SMatthias Ringwald         if ((hfp_connection->call_state != HFP_CALL_INCOMING_RINGING) &&
1410a5764cd2SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1411aa4dd815SMatthias Ringwald 
1412a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
1413aa4dd815SMatthias Ringwald 
1414a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1415a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1416aa4dd815SMatthias Ringwald 
1417f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1418aa4dd815SMatthias Ringwald         break;  // only single
1419aa4dd815SMatthias Ringwald     }
1420aa4dd815SMatthias Ringwald }
1421aa4dd815SMatthias Ringwald 
1422aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
1423aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1424a4f85bd2SMilanka Ringwald 
1425a4f85bd2SMilanka Ringwald     hfp_ag_stop_ringing();
1426a4f85bd2SMilanka Ringwald 
1427665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1428665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1429665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1430665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
143166c5995fSMatthias Ringwald         if (connection->local_role != HFP_ROLE_AG) continue;
143207a44c5eSMilanka Ringwald 
143307a44c5eSMilanka Ringwald         switch (connection->call_state){
143407a44c5eSMilanka Ringwald             case HFP_CALL_INCOMING_RINGING:
143507a44c5eSMilanka Ringwald             case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
143607a44c5eSMilanka Ringwald             case HFP_CALL_OUTGOING_RINGING:
143707a44c5eSMilanka Ringwald             case HFP_CALL_OUTGOING_INITIATED:
143807a44c5eSMilanka Ringwald             case HFP_CALL_OUTGOING_DIALING:
143907a44c5eSMilanka Ringwald                 break;
144007a44c5eSMilanka Ringwald             default:
144107a44c5eSMilanka Ringwald                 continue;
144207a44c5eSMilanka Ringwald         }
1443095a9f2bSMilanka Ringwald 
1444aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
1445095a9f2bSMilanka Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1446095a9f2bSMilanka Ringwald 
1447095a9f2bSMilanka Ringwald         if (connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
1448095a9f2bSMilanka Ringwald             connection->release_audio_connection = 1;
1449095a9f2bSMilanka Ringwald         }
1450095a9f2bSMilanka Ringwald         hfp_emit_simple_event(connection, HFP_SUBEVENT_CALL_TERMINATED);
1451f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(connection);
1452095a9f2bSMilanka Ringwald         rfcomm_request_can_send_now_event(connection->rfcomm_cid);
1453aa4dd815SMatthias Ringwald     }
1454aa4dd815SMatthias Ringwald }
1455aa4dd815SMatthias Ringwald 
1456aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1457aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1458aa4dd815SMatthias Ringwald 
1459a4f85bd2SMilanka Ringwald     // no ringing during call
1460a4f85bd2SMilanka Ringwald 
1461665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1462665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1463665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1464a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
146566c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1466a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1467fb75b483SMilanka Ringwald 
1468a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1469a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1470095a9f2bSMilanka Ringwald 
14716d78145cSMatthias Ringwald         if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
1472a0ffb263SMatthias Ringwald             hfp_connection->release_audio_connection = 1;
14736d78145cSMatthias Ringwald         }
1474095a9f2bSMilanka Ringwald 
1475fb75b483SMilanka Ringwald         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1476f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1477a13ff7d1SMilanka Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1478aa4dd815SMatthias Ringwald     }
1479aa4dd815SMatthias Ringwald }
1480aa4dd815SMatthias Ringwald 
14810cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1482aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1483aa4dd815SMatthias Ringwald     if (!indicator){
1484aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
148545718b6fSMatthias Ringwald         return;
1486aa4dd815SMatthias Ringwald     };
1487d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1488aa4dd815SMatthias Ringwald }
1489aa4dd815SMatthias Ringwald 
1490aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1491665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1492665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1493665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1494a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
149566c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1496a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1497aa4dd815SMatthias Ringwald     }
1498aa4dd815SMatthias Ringwald     return NULL;
1499aa4dd815SMatthias Ringwald }
1500aa4dd815SMatthias Ringwald 
1501a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1502665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1503665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1504665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1505a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
150666c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1507a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1508ce263fc8SMatthias Ringwald     }
1509ce263fc8SMatthias Ringwald }
1510ce263fc8SMatthias Ringwald 
1511a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1512aa4dd815SMatthias Ringwald     int indicator_index;
1513a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1514aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1515a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1516a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
15179493406bSMilanka Ringwald             hfp_connection->call_state = HFP_CALL_INCOMING_RINGING;
1518aa4dd815SMatthias Ringwald             break;
1519aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1520a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1521a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1522a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1523aa4dd815SMatthias Ringwald             break;
1524aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1525a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1526a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1527aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1528a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1529aa4dd815SMatthias Ringwald             break;
1530aa4dd815SMatthias Ringwald         default:
1531aa4dd815SMatthias Ringwald             break;
1532aa4dd815SMatthias Ringwald     }
1533aa4dd815SMatthias Ringwald     return 0;
1534aa4dd815SMatthias Ringwald }
153539426febSMatthias Ringwald 
1536e84fa067SMatthias Ringwald // queue 'error' to HF
1537e84fa067SMatthias Ringwald static void hfp_ag_queue_error(hfp_connection_t * hfp_connection){
15385e87d1ceSMatthias Ringwald     hfp_connection->send_error++;
1539e84fa067SMatthias Ringwald }
1540e84fa067SMatthias Ringwald 
1541e84fa067SMatthias Ringwald // queue 'ok' to HF
1542e84fa067SMatthias Ringwald static void hfp_ag_queue_ok(hfp_connection_t * hfp_connection){
15435e87d1ceSMatthias Ringwald     hfp_connection->ok_pending++;
1544e84fa067SMatthias Ringwald }
1545e84fa067SMatthias Ringwald 
154639426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below
15479f9621cfSMatthias Ringwald static void hfp_ag_handle_reject_outgoing_call(void){
154839426febSMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
154939426febSMatthias Ringwald     if (!hfp_connection){
155039426febSMatthias Ringwald         log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
155139426febSMatthias Ringwald         return;
155239426febSMatthias Ringwald     }
155339426febSMatthias Ringwald 
1554f8737b81SMatthias Ringwald     hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL);
155539426febSMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
1556e84fa067SMatthias Ringwald     hfp_ag_queue_error(hfp_connection);
155739426febSMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
155839426febSMatthias Ringwald }
155939426febSMatthias Ringwald 
1560a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1561a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
156281e25d0aSMatthias Ringwald 
156381e25d0aSMatthias Ringwald     // allow to intercept call statemachine events
156481e25d0aSMatthias Ringwald     if (hfp_ag_custom_call_sm_handler != NULL){
156581e25d0aSMatthias Ringwald         bool handle_event = (*hfp_ag_custom_call_sm_handler)(event);
156681e25d0aSMatthias Ringwald         if (!handle_event) return;
156781e25d0aSMatthias Ringwald     }
156881e25d0aSMatthias Ringwald 
1569d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1570d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1571d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
157274386ee0SMatthias Ringwald 
1573aa4dd815SMatthias Ringwald     switch (event){
1574aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1575d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1576aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1577d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
157812cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1579f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL);
1580d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
158131f55b91SMilanka Ringwald                             hfp_ag_trigger_incoming_call_idle();
158260ebb071SMilanka Ringwald                             log_info("AG rings");
1583aa4dd815SMatthias Ringwald                             break;
1584aa4dd815SMatthias Ringwald                         default:
15853deb3ec6SMatthias Ringwald                             break;
15863deb3ec6SMatthias Ringwald                     }
15873deb3ec6SMatthias Ringwald                     break;
1588aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1589d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
159012cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1591f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL);
1592d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
159331f55b91SMilanka Ringwald                             hfp_ag_trigger_incoming_call_during_active_one();
159460ebb071SMilanka Ringwald                             log_info("AG call waiting");
1595aa4dd815SMatthias Ringwald                             break;
1596aa4dd815SMatthias Ringwald                         default:
15973deb3ec6SMatthias Ringwald                             break;
15983deb3ec6SMatthias Ringwald                     }
15993deb3ec6SMatthias Ringwald                     break;
16007bbeb3adSMilanka Ringwald                 default:
16017bbeb3adSMilanka Ringwald                     break;
1602aa4dd815SMatthias Ringwald             }
1603aa4dd815SMatthias Ringwald             break;
1604aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1605d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1606aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1607d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
160812cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1609f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL);
1610d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1611d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1612aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
161360ebb071SMilanka Ringwald                             log_info("AG answers call, accept call by GSM");
1614aa4dd815SMatthias Ringwald                             break;
1615aa4dd815SMatthias Ringwald                         default:
16163deb3ec6SMatthias Ringwald                             break;
16173deb3ec6SMatthias Ringwald                     }
1618aa4dd815SMatthias Ringwald                     break;
1619aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1620d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1621aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
162260ebb071SMilanka Ringwald                             log_info("AG: current call is placed on hold, incoming call gets active");
1623f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL);
1624d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1625d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1626ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1627ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1628aa4dd815SMatthias Ringwald                             break;
1629aa4dd815SMatthias Ringwald                         default:
1630aa4dd815SMatthias Ringwald                             break;
1631aa4dd815SMatthias Ringwald                     }
1632aa4dd815SMatthias Ringwald                     break;
16337bbeb3adSMilanka Ringwald                 default:
16347bbeb3adSMilanka Ringwald                     break;
1635aa4dd815SMatthias Ringwald             }
1636aa4dd815SMatthias Ringwald             break;
1637ce263fc8SMatthias Ringwald 
1638ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1639d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1640ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1641d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1642ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
164360ebb071SMilanka Ringwald                             log_info("AG: joining held call with active call");
1644f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL);
1645d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1646ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1647ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1648ce263fc8SMatthias Ringwald                             break;
1649ce263fc8SMatthias Ringwald                         default:
1650ce263fc8SMatthias Ringwald                             break;
1651ce263fc8SMatthias Ringwald                     }
1652ce263fc8SMatthias Ringwald                     break;
1653ce263fc8SMatthias Ringwald                 default:
1654ce263fc8SMatthias Ringwald                     break;
1655ce263fc8SMatthias Ringwald             }
1656ce263fc8SMatthias Ringwald             break;
1657ce263fc8SMatthias Ringwald 
1658aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1659d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1660aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1661d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
166212cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1663f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL);
1664d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1665d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1666a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
1667e84fa067SMatthias Ringwald                             hfp_ag_queue_ok(hfp_connection);
166860ebb071SMilanka Ringwald                             log_info("HF answers call, accept call by GSM");
1669ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1670aa4dd815SMatthias Ringwald                             break;
1671aa4dd815SMatthias Ringwald                         default:
1672aa4dd815SMatthias Ringwald                             break;
1673aa4dd815SMatthias Ringwald                     }
16743deb3ec6SMatthias Ringwald                     break;
16753deb3ec6SMatthias Ringwald                 default:
16763deb3ec6SMatthias Ringwald                     break;
16773deb3ec6SMatthias Ringwald             }
16783deb3ec6SMatthias Ringwald             break;
16793deb3ec6SMatthias Ringwald 
1680ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1681d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1682ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1683d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
168412cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1685f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL);
168627bb1817SMatthias Ringwald                             hfp_ag_response_and_hold_active = true;
1687ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1688a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1689ac6f828eSMilanka Ringwald                             // as with regular call
1690d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1691d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1692ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
169360ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by AG");
1694ce263fc8SMatthias Ringwald                             break;
1695ce263fc8SMatthias Ringwald                         default:
1696ce263fc8SMatthias Ringwald                             break;
1697ce263fc8SMatthias Ringwald                     }
1698ce263fc8SMatthias Ringwald                     break;
1699ce263fc8SMatthias Ringwald                 default:
1700ce263fc8SMatthias Ringwald                     break;
1701ce263fc8SMatthias Ringwald             }
1702ce263fc8SMatthias Ringwald             break;
1703ce263fc8SMatthias Ringwald 
1704ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1705d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1706ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1707d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
170812cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1709f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL);
171027bb1817SMatthias Ringwald                             hfp_ag_response_and_hold_active = true;
1711ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1712a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1713ac6f828eSMilanka Ringwald                             // as with regular call
1714d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1715d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1716a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
171760ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by HF");
1718ce263fc8SMatthias Ringwald                             break;
1719ce263fc8SMatthias Ringwald                         default:
1720ce263fc8SMatthias Ringwald                             break;
1721ce263fc8SMatthias Ringwald                     }
1722ce263fc8SMatthias Ringwald                     break;
1723ce263fc8SMatthias Ringwald                 default:
1724ce263fc8SMatthias Ringwald                     break;
1725ce263fc8SMatthias Ringwald             }
1726ce263fc8SMatthias Ringwald             break;
1727ce263fc8SMatthias Ringwald 
1728ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1729ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1730ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1731ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1732f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL);
173327bb1817SMatthias Ringwald             hfp_ag_response_and_hold_active = false;
1734ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1735a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
173660ebb071SMilanka Ringwald             log_info("Held Call accepted and active");
1737ce263fc8SMatthias Ringwald             break;
1738ce263fc8SMatthias Ringwald 
1739ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1740ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1741ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1742ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1743f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL);
174427bb1817SMatthias Ringwald             hfp_ag_response_and_hold_active = false;
1745ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1746a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1747ce263fc8SMatthias Ringwald             // from terminate by ag
1748d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1749ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1750ce263fc8SMatthias Ringwald             break;
1751ce263fc8SMatthias Ringwald 
1752aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1753d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1754aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1755d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
175612cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
175707a44c5eSMilanka Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
175807a44c5eSMilanka Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1759f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
1760d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1761aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1762aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
176360ebb071SMilanka Ringwald                             log_info("HF Rejected Incoming call, AG terminate call");
1764aa4dd815SMatthias Ringwald                             break;
1765aa4dd815SMatthias Ringwald                         default:
1766aa4dd815SMatthias Ringwald                             break;
1767aa4dd815SMatthias Ringwald                     }
1768aa4dd815SMatthias Ringwald                     break;
176912cbbeeeSMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1770f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
17711ec112deSMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1772d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
17731ec112deSMatthias Ringwald                     hfp_ag_trigger_terminate_call();
177460ebb071SMilanka Ringwald                     log_info("AG terminate call");
1775aa4dd815SMatthias Ringwald                     break;
17767bbeb3adSMilanka Ringwald                 default:
17777bbeb3adSMilanka Ringwald                     break;
1778aa4dd815SMatthias Ringwald             }
1779aa4dd815SMatthias Ringwald             break;
17803deb3ec6SMatthias Ringwald 
1781aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1782d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1783aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1784d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
178507a44c5eSMilanka Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
178607a44c5eSMilanka Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
178707a44c5eSMilanka Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1788f2aa99a9SMilanka Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1789f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL);
1790d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1791aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
179260ebb071SMilanka Ringwald                             log_info("AG Rejected Incoming call, AG terminate call");
1793aa4dd815SMatthias Ringwald                             break;
1794aa4dd815SMatthias Ringwald                         default:
1795aa4dd815SMatthias Ringwald                             break;
17963deb3ec6SMatthias Ringwald                     }
1797202c8a4cSMatthias Ringwald                     break;
179812cbbeeeSMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1799f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL);
1800d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1801d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1802aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
180360ebb071SMilanka Ringwald                     log_info("AG terminate call");
1804aa4dd815SMatthias Ringwald                     break;
1805aa4dd815SMatthias Ringwald                 default:
18063deb3ec6SMatthias Ringwald                     break;
18073deb3ec6SMatthias Ringwald             }
18083deb3ec6SMatthias Ringwald             break;
1809aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1810d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1811aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1812d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1813aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1814aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1815fb75b483SMilanka Ringwald                             hfp_ag_stop_ringing();
1816fb75b483SMilanka Ringwald                             break;
1817fb75b483SMilanka Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1818fb75b483SMilanka Ringwald                             log_info("Outgoing call interrupted\n");
1819aa4dd815SMatthias Ringwald                             break;
1820aa4dd815SMatthias Ringwald                         default:
18213deb3ec6SMatthias Ringwald                             break;
18223deb3ec6SMatthias Ringwald                     }
1823f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1824d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1825aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
182610c236bfSMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1827aa4dd815SMatthias Ringwald                     break;
1828aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1829ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1830f8737b81SMatthias Ringwald                         hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1831ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1832a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1833ce263fc8SMatthias Ringwald                     }
1834f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1835d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1836d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1837aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
183860ebb071SMilanka Ringwald                     log_info("AG notify call dropped");
1839aa4dd815SMatthias Ringwald                     break;
1840aa4dd815SMatthias Ringwald                 default:
1841aa4dd815SMatthias Ringwald                     break;
1842aa4dd815SMatthias Ringwald             }
1843aa4dd815SMatthias Ringwald             break;
1844aa4dd815SMatthias Ringwald 
18459ff73f41SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED_BY_HF:
1846d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1847d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1848e84fa067SMatthias Ringwald                 hfp_ag_queue_error(hfp_connection);
1849f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1850d210d9c4SMatthias Ringwald                 break;
1851d210d9c4SMatthias Ringwald             }
1852fe899794SMatthias Ringwald 
1853fe899794SMatthias Ringwald             // note: number not used currently
18549ff73f41SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, 0, 0, (const char *) &hfp_connection->line_buffer[3]);
18559cae807eSMatthias Ringwald 
1856a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
185774386ee0SMatthias Ringwald 
1858ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1859aa4dd815SMatthias Ringwald             break;
1860aa4dd815SMatthias Ringwald 
1861fe899794SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED_BY_AG:
1862fe899794SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1863fe899794SMatthias Ringwald             if (!hfp_gsm_call_possible()) {
1864fe899794SMatthias Ringwald                 // TODO: no error for user
1865fe899794SMatthias Ringwald                 break;
1866fe899794SMatthias Ringwald             }
1867fe899794SMatthias Ringwald             switch (hfp_gsm_call_status()) {
1868fe899794SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1869fe899794SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()) {
1870fe899794SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1871fe899794SMatthias Ringwald                             // note: number not used currently
1872fe899794SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, 0, 0, "1234567");
1873fe899794SMatthias Ringwald                             // init call state for all connections
1874fe899794SMatthias Ringwald                             hfp_ag_trigger_outgoing_call();
1875fe899794SMatthias Ringwald                             // get first one and accept call
1876fe899794SMatthias Ringwald                             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1877fe899794SMatthias Ringwald                             if (hfp_connection) {
1878fe899794SMatthias Ringwald                                 hfp_ag_handle_outgoing_call_accepted(hfp_connection);
1879fe899794SMatthias Ringwald                             }
1880fe899794SMatthias Ringwald                             break;
1881fe899794SMatthias Ringwald                         default:
1882fe899794SMatthias Ringwald                             // TODO: no error for user
1883fe899794SMatthias Ringwald                             break;
1884fe899794SMatthias Ringwald                     }
1885fe899794SMatthias Ringwald                     break;
1886fe899794SMatthias Ringwald                 default:
1887fe899794SMatthias Ringwald                     // TODO: no error for user
1888fe899794SMatthias Ringwald                     break;
1889fe899794SMatthias Ringwald             }
1890fe899794SMatthias Ringwald             break;
18919cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1892d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1893d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1894e84fa067SMatthias Ringwald                 hfp_ag_queue_error(hfp_connection);
1895f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1896d210d9c4SMatthias Ringwald                 break;
1897d210d9c4SMatthias Ringwald             }
1898d210d9c4SMatthias Ringwald 
1899f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL);
1900a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
190174386ee0SMatthias Ringwald 
190260ebb071SMilanka Ringwald             log_info("Redial last number");
19039cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1904aa4dd815SMatthias Ringwald 
19059cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
190660ebb071SMilanka Ringwald                 log_info("Last number exists: accept call");
1907ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
19089cae807eSMatthias Ringwald             } else {
190960ebb071SMilanka Ringwald                 log_info("log_infoLast number missing: reject call");
19109f9621cfSMatthias Ringwald                 hfp_ag_handle_reject_outgoing_call();
19119cae807eSMatthias Ringwald             }
19129cae807eSMatthias Ringwald             break;
19139cae807eSMatthias Ringwald         }
1914aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
19159f9621cfSMatthias Ringwald             hfp_ag_handle_reject_outgoing_call();
1916aa4dd815SMatthias Ringwald             break;
1917aa4dd815SMatthias Ringwald 
1918d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1919a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1920a0ffb263SMatthias Ringwald             if (!hfp_connection){
1921a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1922aa4dd815SMatthias Ringwald                 break;
1923aa4dd815SMatthias Ringwald             }
1924aa4dd815SMatthias Ringwald 
1925e84fa067SMatthias Ringwald             hfp_ag_queue_ok(hfp_connection);
19265a7033e2SMatthias Ringwald             hfp_ag_handle_outgoing_call_accepted(hfp_connection);
1927aa4dd815SMatthias Ringwald             break;
1928d210d9c4SMatthias Ringwald         }
1929aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1930a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1931a0ffb263SMatthias Ringwald             if (!hfp_connection){
1932a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1933aa4dd815SMatthias Ringwald                 break;
1934aa4dd815SMatthias Ringwald             }
1935d210d9c4SMatthias Ringwald 
1936f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL);
1937a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1938d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1939aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1940fcd59412SMilanka Ringwald             hfp_ag_hf_start_ringing_outgoing(hfp_connection);
1941aa4dd815SMatthias Ringwald             break;
1942aa4dd815SMatthias Ringwald 
1943d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1944aa4dd815SMatthias Ringwald             // get outgoing call
1945a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1946a0ffb263SMatthias Ringwald             if (!hfp_connection){
1947a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1948aa4dd815SMatthias Ringwald             }
1949a0ffb263SMatthias Ringwald             if (!hfp_connection){
1950a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1951aa4dd815SMatthias Ringwald                 break;
1952aa4dd815SMatthias Ringwald             }
1953d210d9c4SMatthias Ringwald 
1954addc49c4SMilanka 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;
1955f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL);
1956a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1957d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1958d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1959aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1960aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1961addc49c4SMilanka Ringwald             if (callheld_status_call_on_hold_and_no_active_calls){
1962d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1963aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
19643deb3ec6SMatthias Ringwald             }
1965addc49c4SMilanka Ringwald             hfp_ag_hf_stop_ringing(hfp_connection);
1966674ebed5SMilanka Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
19673deb3ec6SMatthias Ringwald             break;
1968d210d9c4SMatthias Ringwald         }
1969d210d9c4SMatthias Ringwald 
197012cbbeeeSMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1971f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL);
1972d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1973a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1974a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
197560ebb071SMilanka Ringwald             log_info("AG: Call Waiting, User Busy");
1976d210d9c4SMatthias Ringwald             break;
1977d210d9c4SMatthias Ringwald 
1978d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1979d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1980d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1981d210d9c4SMatthias Ringwald 
1982d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1983d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1984f8737b81SMatthias Ringwald                 hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index,
1985f8737b81SMatthias Ringwald                                 0, NULL);
1986d0c20769SMatthias Ringwald 
1987d0c20769SMatthias Ringwald             }
1988d0c20769SMatthias Ringwald 
1989d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
199060ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Accept new call");
1991d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1992a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1993d210d9c4SMatthias Ringwald             } else {
199460ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Resume held call");
1995d210d9c4SMatthias Ringwald             }
1996d210d9c4SMatthias Ringwald             if (call_held){
1997d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1998a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1999d210d9c4SMatthias Ringwald             }
2000d0c20769SMatthias Ringwald 
2001a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
2002d210d9c4SMatthias Ringwald             break;
2003d210d9c4SMatthias Ringwald         }
2004d0c20769SMatthias Ringwald 
2005d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
2006d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
2007d210d9c4SMatthias Ringwald             // only update if callsetup changed
2008d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
2009f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0,
2010f8737b81SMatthias Ringwald                             NULL);
2011d0c20769SMatthias Ringwald 
2012d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
201360ebb071SMilanka Ringwald                 log_info("AG: Call on Hold, Accept new call");
2014d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
2015a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
2016d210d9c4SMatthias Ringwald             } else {
201760ebb071SMilanka Ringwald                 log_info("AG: Swap calls");
2018d210d9c4SMatthias Ringwald             }
2019d0c20769SMatthias Ringwald 
2020d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
2021d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
2022a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
2023a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
2024d210d9c4SMatthias Ringwald             break;
2025d210d9c4SMatthias Ringwald         }
2026d0c20769SMatthias Ringwald 
2027d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
2028d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
2029d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
203060ebb071SMilanka Ringwald                 log_info("AG: Join 3-way-call");
2031f8737b81SMatthias Ringwald                 hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL);
2032d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
2033a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
2034ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
2035d210d9c4SMatthias Ringwald             }
2036a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
2037d210d9c4SMatthias Ringwald             break;
2038d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
2039d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
2040f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL);
204160ebb071SMilanka Ringwald             log_info("AG: Transfer call -> Connect two calls and disconnect");
2042d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
2043d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
2044a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
2045a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
2046a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
2047d210d9c4SMatthias Ringwald             break;
2048ce263fc8SMatthias Ringwald 
20493deb3ec6SMatthias Ringwald         default:
20503deb3ec6SMatthias Ringwald             break;
20513deb3ec6SMatthias Ringwald     }
2052d210d9c4SMatthias Ringwald 
2053d0c20769SMatthias Ringwald 
20543deb3ec6SMatthias Ringwald }
20553deb3ec6SMatthias Ringwald 
20569cae807eSMatthias Ringwald 
2057a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
20589cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
20599cae807eSMatthias Ringwald     if (!active_call) return;
20609cae807eSMatthias Ringwald 
20619cae807eSMatthias Ringwald     int idx = active_call->index;
20629cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
20639cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
20649cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
20659cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
20669cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
20679cae807eSMatthias Ringwald     char * number = active_call->clip_number;
20689cae807eSMatthias Ringwald 
20699cae807eSMatthias Ringwald     char buffer[100];
20709cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
20719cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
20729cae807eSMatthias Ringwald     if (number){
20731cc1d9e9SMilanka Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
20749cae807eSMatthias Ringwald     }
20759cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
207660ebb071SMilanka 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,
20779cae807eSMatthias Ringwald        mode, mpty, type, number);
2078a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
20799cae807eSMatthias Ringwald }
20809cae807eSMatthias Ringwald 
2081febc14f5SMatthias Ringwald // sends pending command, returns if command was sent
2082febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){
2083a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
2084a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
2085a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
2086a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
2087febc14f5SMatthias Ringwald             return 1;
20889cae807eSMatthias Ringwald         } else {
2089febc14f5SMatthias 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
2090a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
2091e84fa067SMatthias Ringwald             hfp_ag_queue_ok(hfp_connection);
2092a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
20939cae807eSMatthias Ringwald         }
2094ce263fc8SMatthias Ringwald     }
2095ce263fc8SMatthias Ringwald 
2096a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
2097a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
2098a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
2099febc14f5SMatthias Ringwald         return 1;
2100aa4dd815SMatthias Ringwald     }
2101aa4dd815SMatthias Ringwald 
21025e87d1ceSMatthias Ringwald     if (hfp_connection->send_error > 0){
21035e87d1ceSMatthias Ringwald         hfp_connection->send_error--;
2104a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2105485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
2106febc14f5SMatthias Ringwald         return 1;
2107aa4dd815SMatthias Ringwald     }
2108aa4dd815SMatthias Ringwald 
2109ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
2110a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
2111a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
2112a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
2113485ac19eSMilanka Ringwald         hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
2114febc14f5SMatthias Ringwald         return 1;
2115ce263fc8SMatthias Ringwald     }
2116ce263fc8SMatthias Ringwald 
21175be6b47aSMatthias Ringwald     // note: before ok_pending and send_error to allow for unsolicited result on custom command
21185be6b47aSMatthias Ringwald     if (hfp_connection->send_custom_message != NULL){
21195be6b47aSMatthias Ringwald         const char * message = hfp_connection->send_custom_message;
21205be6b47aSMatthias Ringwald         hfp_connection->send_custom_message = NULL;
21215be6b47aSMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, message);
21227f8f1191SMatthias Ringwald         hfp_emit_event(hfp_connection, HFP_SUBEVENT_CUSTOM_AT_MESSAGE_SENT, ERROR_CODE_SUCCESS);
21235be6b47aSMatthias Ringwald         return 1;
21245be6b47aSMatthias Ringwald     }
21255be6b47aSMatthias Ringwald 
21265e87d1ceSMatthias Ringwald     if (hfp_connection->ok_pending > 0){
21275e87d1ceSMatthias Ringwald         hfp_connection->ok_pending--;
2128a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2129485ac19eSMilanka Ringwald         hfp_ag_send_ok(hfp_connection->rfcomm_cid);
2130febc14f5SMatthias Ringwald         return 1;
2131ce263fc8SMatthias Ringwald     }
2132ce263fc8SMatthias Ringwald 
213369640b04SMatthias Ringwald     // update AG indicators only if enabled by AT+CMER=3,0,0,1
213469640b04SMatthias Ringwald     if ((hfp_connection->enable_status_update_for_ag_indicators == 1) && (hfp_connection->ag_indicators_status_update_bitmap != 0)){
213569640b04SMatthias Ringwald         uint16_t i;
2136a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
2137a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
2138a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
2139485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
2140febc14f5SMatthias Ringwald                 return 1;
2141aa4dd815SMatthias Ringwald             }
2142aa4dd815SMatthias Ringwald         }
2143aa4dd815SMatthias Ringwald     }
2144aa4dd815SMatthias Ringwald 
2145245852b7SMilanka Ringwald     if (hfp_connection->ag_send_no_carrier){
2146245852b7SMilanka Ringwald         hfp_connection->ag_send_no_carrier = false;
2147245852b7SMilanka Ringwald         hfp_ag_send_no_carrier(hfp_connection->rfcomm_cid);
2148245852b7SMilanka Ringwald         return 1;
2149245852b7SMilanka Ringwald     }
2150245852b7SMilanka Ringwald 
2151a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
2152a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
2153a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2154e84fa067SMatthias Ringwald         hfp_ag_queue_ok(hfp_connection);
2155a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
2156febc14f5SMatthias Ringwald         return 1;
2157aa4dd815SMatthias Ringwald     }
2158aa4dd815SMatthias Ringwald 
2159a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
2160aeb0f0feSMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < hfp_ag_subscriber_numbers_count){
2161aeb0f0feSMatthias Ringwald             hfp_phone_number_t phone = hfp_ag_subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
2162a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
2163ce263fc8SMatthias Ringwald         } else {
2164a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
2165a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
2166485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
2167ce263fc8SMatthias Ringwald         }
2168a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2169febc14f5SMatthias Ringwald         return 1;
2170ce263fc8SMatthias Ringwald     }
2171ce263fc8SMatthias Ringwald 
2172a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
2173a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
2174a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2175485ac19eSMilanka Ringwald         hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
2176febc14f5SMatthias Ringwald         return 1;
2177aa4dd815SMatthias Ringwald     }
2178aa4dd815SMatthias Ringwald 
2179a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
2180a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
2181a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2182485ac19eSMilanka Ringwald         hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
2183febc14f5SMatthias Ringwald         return 1;
21843deb3ec6SMatthias Ringwald     }
21853deb3ec6SMatthias Ringwald 
2186a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
2187a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
2188485ac19eSMilanka Ringwald         hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
2189febc14f5SMatthias Ringwald         return 1;
2190febc14f5SMatthias Ringwald     }
2191febc14f5SMatthias Ringwald 
219210ae9dfcSMatthias Ringwald     if (hfp_connection->extended_audio_gateway_error){
219310ae9dfcSMatthias Ringwald         uint8_t extended_audio_gateway_error = hfp_connection->extended_audio_gateway_error;
219410ae9dfcSMatthias Ringwald         hfp_connection->extended_audio_gateway_error = 0;
219510ae9dfcSMatthias Ringwald         hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, extended_audio_gateway_error);
219610ae9dfcSMatthias Ringwald 
219710ae9dfcSMatthias Ringwald     }
2198febc14f5SMatthias Ringwald     return 0;
2199febc14f5SMatthias Ringwald }
2200febc14f5SMatthias Ringwald 
2201c1eef992SMatthias Ringwald // sends pending command, returns if command was sent
2202c1eef992SMatthias Ringwald static int hfp_ag_run_ring_and_clip(hfp_connection_t *hfp_connection){
2203c1eef992SMatthias Ringwald     // delay RING/CLIP until audio connection has been established for incoming calls
2204c1eef992SMatthias Ringwald     // hfp_ag_hf_trigger_ring_and_clip is called in call_setup_state_machine
2205c1eef992SMatthias Ringwald     if (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING){
2206c1eef992SMatthias Ringwald         if (hfp_connection->ag_ring){
2207c1eef992SMatthias Ringwald             hfp_connection->ag_ring = 0;
2208c1eef992SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
2209c1eef992SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
2210c1eef992SMatthias Ringwald             hfp_ag_send_ring(hfp_connection->rfcomm_cid);
2211c1eef992SMatthias Ringwald             return 1;
2212c1eef992SMatthias Ringwald         }
2213c1eef992SMatthias Ringwald 
2214c1eef992SMatthias Ringwald         if (hfp_connection->ag_send_clip){
2215c1eef992SMatthias Ringwald             hfp_connection->ag_send_clip = 0;
2216c1eef992SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
2217c1eef992SMatthias Ringwald             hfp_ag_send_clip(hfp_connection->rfcomm_cid);
2218c1eef992SMatthias Ringwald             return 1;
2219c1eef992SMatthias Ringwald         }
2220c1eef992SMatthias Ringwald     }
2221c1eef992SMatthias Ringwald     return 0;
2222c1eef992SMatthias Ringwald }
2223c1eef992SMatthias Ringwald 
2224febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){
2225febc14f5SMatthias Ringwald 
222676cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
222776cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_AG);
2228febc14f5SMatthias Ringwald 
222976cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
223076cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
2231ce263fc8SMatthias Ringwald 
223284fb9ac1SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->emit_vra_enabled_after_audio_established){
223384fb9ac1SMilanka Ringwald         hfp_connection->emit_vra_enabled_after_audio_established = false;
223484fb9ac1SMilanka Ringwald         hfp_emit_voice_recognition_enabled(hfp_connection, ERROR_CODE_SUCCESS);
223584fb9ac1SMilanka Ringwald     }
223684fb9ac1SMilanka Ringwald 
2237f9f3ba28SMilanka Ringwald     if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){
2238f9f3ba28SMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
2239f9f3ba28SMilanka Ringwald         hfp_connection->release_audio_connection = 0;
2240f9f3ba28SMilanka Ringwald         gap_disconnect(hfp_connection->sco_handle);
2241f9f3ba28SMilanka Ringwald         return;
2242f9f3ba28SMilanka Ringwald     }
2243f9f3ba28SMilanka Ringwald 
2244b7802cc8SMatthias Ringwald     // configure NBS/WBS if needed using vendor-specific HCI commands
2245b7802cc8SMatthias Ringwald     if (hci_can_send_command_packet_now()) {
22463721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
22473721a235SMatthias Ringwald         // WBS Disassociate
22483721a235SMatthias Ringwald         if (hfp_connection->cc256x_send_wbs_disassociate){
22493721a235SMatthias Ringwald             hfp_connection->cc256x_send_wbs_disassociate = false;
22503721a235SMatthias Ringwald             hci_send_cmd(&hci_ti_wbs_disassociate);
22513721a235SMatthias Ringwald             return;
22523721a235SMatthias Ringwald         }
22533721a235SMatthias Ringwald         // Write Codec Config
22543721a235SMatthias Ringwald         if (hfp_connection->cc256x_send_write_codec_config){
22553721a235SMatthias Ringwald             hfp_connection->cc256x_send_write_codec_config = false;
22563721a235SMatthias Ringwald             hfp_cc256x_write_codec_config(hfp_connection);
22573721a235SMatthias Ringwald             return;
22583721a235SMatthias Ringwald         }
22593721a235SMatthias Ringwald         // WBS Associate
22603721a235SMatthias Ringwald         if (hfp_connection->cc256x_send_wbs_associate){
22613721a235SMatthias Ringwald             hfp_connection->cc256x_send_wbs_associate = false;
22623721a235SMatthias Ringwald             hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
22633721a235SMatthias Ringwald             return;
22643721a235SMatthias Ringwald         }
22653721a235SMatthias Ringwald #endif
2266689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
2267689d4323SMatthias Ringwald         // Enable WBS
2268689d4323SMatthias Ringwald         if (hfp_connection->bcm_send_enable_wbs){
2269689d4323SMatthias Ringwald             hfp_connection->bcm_send_enable_wbs = false;
2270689d4323SMatthias Ringwald             hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
2271689d4323SMatthias Ringwald             return;
2272689d4323SMatthias Ringwald         }
2273689d4323SMatthias Ringwald         // Write I2S/PCM params
2274689d4323SMatthias Ringwald         if (hfp_connection->bcm_send_write_i2spcm_interface_param){
2275689d4323SMatthias Ringwald             hfp_connection->bcm_send_write_i2spcm_interface_param = false;
2276689d4323SMatthias Ringwald             hfp_bcm_write_i2spcm_interface_param(hfp_connection);
2277689d4323SMatthias Ringwald             return;
2278689d4323SMatthias Ringwald         }
2279689d4323SMatthias Ringwald         // Disable WBS
2280689d4323SMatthias Ringwald         if (hfp_connection->bcm_send_disable_wbs){
2281689d4323SMatthias Ringwald             hfp_connection->bcm_send_disable_wbs = false;
2282689d4323SMatthias Ringwald             hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
2283689d4323SMatthias Ringwald             return;
2284689d4323SMatthias Ringwald         }
2285689d4323SMatthias Ringwald #endif
22862b5f92fdSMatthias Ringwald #ifdef ENABLE_RTK_PCM_WBS
22872b5f92fdSMatthias Ringwald         // Configure CVSD vs. mSBC
22882b5f92fdSMatthias Ringwald         if (hfp_connection->rtk_send_sco_config){
22892b5f92fdSMatthias Ringwald             hfp_connection->rtk_send_sco_config = false;
22902b5f92fdSMatthias Ringwald             if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
22912b5f92fdSMatthias Ringwald                 log_info("RTK SCO: 16k + mSBC");
22922b5f92fdSMatthias Ringwald                 hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x00, 0x00, 0x41);
22932b5f92fdSMatthias Ringwald             } else {
22942b5f92fdSMatthias Ringwald                 log_info("RTK SCO: 16k + CVSD");
22952b5f92fdSMatthias Ringwald                 hci_send_cmd(&hci_rtk_configure_sco_routing, 0x81, 0x90, 0x00, 0x00, 0x1a, 0x0c, 0x0c, 0x00, 0x01);
22962b5f92fdSMatthias Ringwald             }
22972b5f92fdSMatthias Ringwald             return;
22982b5f92fdSMatthias Ringwald         }
22992b5f92fdSMatthias Ringwald #endif
23005fd6f360SMatthias Ringwald #ifdef ENABLE_NXP_PCM_WBS
23015fd6f360SMatthias Ringwald         if (hfp_connection->nxp_start_audio_handle != HCI_CON_HANDLE_INVALID){
23025fd6f360SMatthias Ringwald             hci_con_handle_t sco_handle = hfp_connection->nxp_start_audio_handle;
23035fd6f360SMatthias Ringwald             hfp_connection->nxp_start_audio_handle = HCI_CON_HANDLE_INVALID;
23045fd6f360SMatthias Ringwald             hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 0, 0, sco_handle, 0);
23055fd6f360SMatthias Ringwald             return;
23065fd6f360SMatthias Ringwald         }
23075fd6f360SMatthias Ringwald         if (hfp_connection->nxp_stop_audio_handle != HCI_CON_HANDLE_INVALID){
23085fd6f360SMatthias Ringwald             hci_con_handle_t sco_handle = hfp_connection->nxp_stop_audio_handle;
23095fd6f360SMatthias Ringwald             hfp_connection->nxp_stop_audio_handle = HCI_CON_HANDLE_INVALID;
23105fd6f360SMatthias Ringwald             hci_send_cmd(&hci_nxp_host_pcm_i2s_audio_config, 1, 0, sco_handle, 0);
23115fd6f360SMatthias Ringwald             return;
23125fd6f360SMatthias Ringwald         }
23135fd6f360SMatthias Ringwald #endif
2314b7802cc8SMatthias Ringwald     }
2315b7802cc8SMatthias Ringwald 
231648e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
231748e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
231848e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
231948e6eeeeSMatthias Ringwald         return;
232048e6eeeeSMatthias Ringwald     }
232148e6eeeeSMatthias Ringwald #endif
23223721a235SMatthias Ringwald 
2323febc14f5SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
2324febc14f5SMatthias Ringwald         log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
2325febc14f5SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
2326febc14f5SMatthias Ringwald         return;
2327febc14f5SMatthias Ringwald     }
2328febc14f5SMatthias Ringwald 
2329febc14f5SMatthias Ringwald     int cmd_sent = hfp_ag_send_commands(hfp_connection);
2330febc14f5SMatthias Ringwald 
2331febc14f5SMatthias Ringwald     if (!cmd_sent){
2332febc14f5SMatthias Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
2333febc14f5SMatthias Ringwald     }
2334febc14f5SMatthias Ringwald 
2335c04cf7ffSMilanka Ringwald     if (!cmd_sent){
2336c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
23373deb3ec6SMatthias Ringwald     }
23383deb3ec6SMatthias Ringwald 
2339c04cf7ffSMilanka Ringwald     if (!cmd_sent){
2340c04cf7ffSMilanka Ringwald         cmd_sent = call_setup_state_machine(hfp_connection);
2341aa4dd815SMatthias Ringwald     }
2342aa4dd815SMatthias Ringwald 
2343b956fff3SMatthias Ringwald     if (!cmd_sent){
2344b956fff3SMatthias Ringwald         cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
2345b956fff3SMatthias Ringwald     }
2346b956fff3SMatthias Ringwald 
2347c95b5b3cSMilanka Ringwald     if (!cmd_sent){
2348c1eef992SMatthias Ringwald         cmd_sent = hfp_ag_run_ring_and_clip(hfp_connection);
2349c1eef992SMatthias Ringwald     }
2350c1eef992SMatthias Ringwald 
2351c1eef992SMatthias Ringwald     if (!cmd_sent){
2352c95b5b3cSMilanka Ringwald         cmd_sent = hfp_ag_voice_recognition_state_machine(hfp_connection);
2353c95b5b3cSMilanka Ringwald     }
2354b956fff3SMatthias Ringwald 
2355d0a0eceeSMatthias Ringwald     // disconnect
2356d0a0eceeSMatthias Ringwald     if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && (hfp_connection->state == HFP_W2_DISCONNECT_RFCOMM)){
2357d0a0eceeSMatthias Ringwald         hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
2358d0a0eceeSMatthias Ringwald         rfcomm_disconnect(hfp_connection->rfcomm_cid);
2359d0a0eceeSMatthias Ringwald     }
2360d0a0eceeSMatthias Ringwald 
2361c04cf7ffSMilanka Ringwald     if (cmd_sent){
2362a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
2363473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
2364473ac565SMatthias Ringwald     }
23653deb3ec6SMatthias Ringwald }
2366d68dcce1SMatthias Ringwald 
2367e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
2368c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
2369e0d09929SMatthias Ringwald }
2370e0d09929SMatthias Ringwald 
2371fdf18f86SMilanka Ringwald static void hfp_ag_handle_rfcomm_data(hfp_connection_t * hfp_connection, uint8_t *packet, uint16_t size){
23728a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
2373fdf18f86SMilanka Ringwald     if (size < 1) return;
2374347d46c8SMilanka Ringwald     uint8_t status = ERROR_CODE_SUCCESS;
23751e35c04dSMatthias Ringwald 
2376186dd3d2SMatthias Ringwald     hfp_log_rfcomm_message("HFP_AG_RX", packet, size);
2377e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
2378e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
2379e43d1938SMatthias Ringwald #endif
23801e35c04dSMatthias Ringwald 
23818a46ec40SMatthias Ringwald     // process messages byte-wise
23823deb3ec6SMatthias Ringwald     int pos;
23833deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
2384a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
2385186dd3d2SMatthias Ringwald 
2386e0d09929SMatthias Ringwald         // parse until end of line
2387e0d09929SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
2388e0d09929SMatthias Ringwald 
2389186dd3d2SMatthias Ringwald         hfp_generic_status_indicator_t * indicator;
2390a0ffb263SMatthias Ringwald         switch(hfp_connection->command){
239145796ff1SMilanka Ringwald             case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
2392e7c46708SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2393e7c46708SMatthias Ringwald                 hfp_connection->ag_vra_requested_by_hf = true;
239445796ff1SMilanka Ringwald                 break;
2395ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
2396fd66594dSMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2397ce263fc8SMatthias Ringwald                 if (hfp_ag_response_and_hold_active){
2398a0ffb263SMatthias Ringwald                     hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
2399ce263fc8SMatthias Ringwald                 }
2400e84fa067SMatthias Ringwald                 hfp_ag_queue_ok(hfp_connection);
2401ce263fc8SMatthias Ringwald                 break;
2402ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
2403fd66594dSMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
24040222a807SMatthias Ringwald                 switch(hfp_connection->ag_response_and_hold_action){
2405ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
2406a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
2407ce263fc8SMatthias Ringwald                         break;
2408ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
2409a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
2410ce263fc8SMatthias Ringwald                         break;
2411ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
2412a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
2413ce263fc8SMatthias Ringwald                         break;
2414ce263fc8SMatthias Ringwald                     default:
2415ce263fc8SMatthias Ringwald                         break;
2416ce263fc8SMatthias Ringwald                 }
2417e84fa067SMatthias Ringwald                 hfp_ag_queue_ok(hfp_connection);
2418ce263fc8SMatthias Ringwald                 break;
2419ce263fc8SMatthias Ringwald             case HFP_CMD_HF_INDICATOR_STATUS:
2420a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2421347d46c8SMilanka Ringwald 
2422aeb0f0feSMatthias Ringwald                 if (hfp_connection->parser_indicator_index < hfp_ag_generic_status_indicators_nr){
2423aeb0f0feSMatthias Ringwald                     indicator = &hfp_ag_generic_status_indicators[hfp_connection->parser_indicator_index];
2424ce263fc8SMatthias Ringwald                     switch (indicator->uuid){
2425347d46c8SMilanka Ringwald                         case HFP_HF_INDICATOR_UUID_ENHANCED_SAFETY:
24260222a807SMatthias Ringwald                             if (hfp_connection->parser_indicator_value > 1) {
2427e84fa067SMatthias Ringwald                                 hfp_ag_queue_error(hfp_connection);
2428ce263fc8SMatthias Ringwald                                 break;
2429ce263fc8SMatthias Ringwald                             }
2430e84fa067SMatthias Ringwald                             hfp_ag_queue_ok(hfp_connection);
2431585db78dSMilanka Ringwald                             hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value);
2432347d46c8SMilanka Ringwald                             break;
2433347d46c8SMilanka Ringwald                         case HFP_HF_INDICATOR_UUID_BATTERY_LEVEL:
2434347d46c8SMilanka Ringwald                             if (hfp_connection->parser_indicator_value > 100){
2435e84fa067SMatthias Ringwald                                 hfp_ag_queue_error(hfp_connection);
2436347d46c8SMilanka Ringwald                                 break;
2437347d46c8SMilanka Ringwald                             }
2438e84fa067SMatthias Ringwald                             hfp_ag_queue_ok(hfp_connection);
2439585db78dSMilanka Ringwald                             hfp_ag_emit_hf_indicator_value(hfp_connection, indicator->uuid, hfp_connection->parser_indicator_value);
2440347d46c8SMilanka Ringwald                             break;
2441347d46c8SMilanka Ringwald                         default:
2442e84fa067SMatthias Ringwald                             hfp_ag_queue_error(hfp_connection);
2443347d46c8SMilanka Ringwald                             break;
2444347d46c8SMilanka Ringwald                     }
2445347d46c8SMilanka Ringwald                 } else {
2446e84fa067SMatthias Ringwald                     hfp_ag_queue_error(hfp_connection);
2447347d46c8SMilanka Ringwald                 }
2448ce263fc8SMatthias Ringwald                 break;
2449ce263fc8SMatthias Ringwald             case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
2450ce263fc8SMatthias Ringwald                 // expected by SLC state machine
2451a0ffb263SMatthias Ringwald                 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
2452a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
2453a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_status_indicators = 1;
2454ce263fc8SMatthias Ringwald                 break;
2455ce263fc8SMatthias Ringwald             case HFP_CMD_LIST_CURRENT_CALLS:
2456a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2457a0ffb263SMatthias Ringwald                 hfp_connection->next_call_index = 0;
2458a0ffb263SMatthias Ringwald                 hfp_connection->send_status_of_current_calls = 1;
2459ce263fc8SMatthias Ringwald                 break;
2460ce263fc8SMatthias Ringwald             case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
2461fd66594dSMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2462aeb0f0feSMatthias Ringwald                 if (hfp_ag_subscriber_numbers_count == 0){
2463485ac19eSMilanka Ringwald                     hfp_ag_send_ok(hfp_connection->rfcomm_cid);
2464ce263fc8SMatthias Ringwald                     break;
2465ce263fc8SMatthias Ringwald                 }
2466a0ffb263SMatthias Ringwald                 hfp_connection->next_subscriber_number_to_send = 0;
2467a0ffb263SMatthias Ringwald                 hfp_connection->send_subscriber_number = 1;
2468ce263fc8SMatthias Ringwald                 break;
2469c1797c7dSMatthias Ringwald             case HFP_CMD_TRANSMIT_DTMF_CODES:
24700222a807SMatthias Ringwald             {
2471a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
24720222a807SMatthias Ringwald                 char buffer[2];
24730222a807SMatthias Ringwald                 buffer[0] = (char) hfp_connection->ag_dtmf_code;
24740222a807SMatthias Ringwald                 buffer[1] = 0;
24750222a807SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer);
2476c1797c7dSMatthias Ringwald                 break;
24770222a807SMatthias Ringwald             }
2478aa4dd815SMatthias Ringwald             case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
2479a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2480ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
2481aa4dd815SMatthias Ringwald                 break;
2482aa4dd815SMatthias Ringwald             case HFP_CMD_TURN_OFF_EC_AND_NR:
2483a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2484347d46c8SMilanka Ringwald 
2485aeb0f0feSMatthias Ringwald                 if (get_bit(hfp_ag_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
2486e84fa067SMatthias Ringwald                     hfp_ag_queue_ok(hfp_connection);
2487347d46c8SMilanka Ringwald                     status = ERROR_CODE_SUCCESS;
2488aa4dd815SMatthias Ringwald                 } else {
2489e84fa067SMatthias Ringwald                     hfp_ag_queue_error(hfp_connection);
2490347d46c8SMilanka Ringwald                     status = ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
2491aa4dd815SMatthias Ringwald                 }
2492347d46c8SMilanka Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_ECHO_CANCELING_AND_NOISE_REDUCTION_DEACTIVATE, status);
2493aa4dd815SMatthias Ringwald                 break;
2494aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_ANSWERED:
2495a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
249660ebb071SMilanka Ringwald                 log_info("HFP: ATA");
2497a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
2498aa4dd815SMatthias Ringwald                 break;
2499aa4dd815SMatthias Ringwald             case HFP_CMD_HANG_UP_CALL:
2500a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2501e84fa067SMatthias Ringwald                 hfp_ag_queue_ok(hfp_connection);
2502a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
2503aa4dd815SMatthias Ringwald                 break;
2504aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_HOLD: {
2505a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2506e84fa067SMatthias Ringwald                 hfp_ag_queue_ok(hfp_connection);
2507d0c20769SMatthias Ringwald 
25080222a807SMatthias Ringwald                 switch (hfp_connection->ag_call_hold_action){
25090222a807SMatthias Ringwald                     case 0:
2510d0c20769SMatthias Ringwald                         // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
2511a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
2512aa4dd815SMatthias Ringwald                         break;
25130222a807SMatthias Ringwald                     case 1:
2514d0c20769SMatthias Ringwald                         // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
2515d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
2516d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
2517a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
2518aa4dd815SMatthias Ringwald                         break;
25190222a807SMatthias Ringwald                     case 2:
2520d0c20769SMatthias Ringwald                         // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
2521d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
2522d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
2523a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
2524aa4dd815SMatthias Ringwald                         break;
25250222a807SMatthias Ringwald                     case 3:
2526d0c20769SMatthias Ringwald                         // Adds a held call to the conversation.
2527a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
2528aa4dd815SMatthias Ringwald                         break;
25290222a807SMatthias Ringwald                     case 4:
2530d0c20769SMatthias Ringwald                         // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
2531a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
2532aa4dd815SMatthias Ringwald                         break;
2533aa4dd815SMatthias Ringwald                     default:
2534aa4dd815SMatthias Ringwald                         break;
2535aa4dd815SMatthias Ringwald                 }
25360222a807SMatthias Ringwald 				hfp_connection->call_index = 0;
253735e92150SMatthias Ringwald                 break;
2538aa4dd815SMatthias Ringwald             }
2539aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_PHONE_NUMBER:
2540a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
25419ff73f41SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, hfp_connection);
2542aa4dd815SMatthias Ringwald                 break;
2543aa4dd815SMatthias Ringwald             case HFP_CMD_REDIAL_LAST_NUMBER:
2544a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2545a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
2546aa4dd815SMatthias Ringwald                 break;
2547aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CLIP:
2548a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2549a0ffb263SMatthias Ringwald                 log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
2550e84fa067SMatthias Ringwald                 hfp_ag_queue_ok(hfp_connection);
2551aa4dd815SMatthias Ringwald                 break;
2552aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
2553a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2554a0ffb263SMatthias Ringwald                 log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
2555e84fa067SMatthias Ringwald                 hfp_ag_queue_ok(hfp_connection);
2556aa4dd815SMatthias Ringwald                 break;
2557aa4dd815SMatthias Ringwald             case HFP_CMD_SET_SPEAKER_GAIN:
2558a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2559e84fa067SMatthias Ringwald                 hfp_ag_queue_ok(hfp_connection);
256060ebb071SMilanka Ringwald                 log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
25613db60f78SBjoern Hartmann                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_connection->speaker_gain);
2562aa4dd815SMatthias Ringwald                 break;
2563aa4dd815SMatthias Ringwald             case HFP_CMD_SET_MICROPHONE_GAIN:
2564a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2565e84fa067SMatthias Ringwald                 hfp_ag_queue_ok(hfp_connection);
256660ebb071SMilanka Ringwald                 log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
25672abbd98dSMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_connection->microphone_gain);
2568aa4dd815SMatthias Ringwald                 break;
2569471dea41SMatthias Ringwald             case HFP_CMD_CUSTOM_MESSAGE:
2570471dea41SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2571471dea41SMatthias Ringwald                 hfp_parser_reset_line_buffer(hfp_connection);
25726d9a41f9SMatthias Ringwald                 log_info("Custom AT Command ID 0x%04x", hfp_connection->custom_at_command_id);
2573471dea41SMatthias Ringwald                 hfp_ag_emit_custom_command_event(hfp_connection);
2574471dea41SMatthias Ringwald                 break;
25753f38f554SMatthias Ringwald             case HFP_CMD_UNKNOWN:
25763f38f554SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2577e84fa067SMatthias Ringwald                 hfp_ag_queue_error(hfp_connection);
25783f38f554SMatthias Ringwald                 break;
2579aa4dd815SMatthias Ringwald             default:
2580aa4dd815SMatthias Ringwald                 break;
25813deb3ec6SMatthias Ringwald         }
25823deb3ec6SMatthias Ringwald     }
25830cef86faSMatthias Ringwald }
25843deb3ec6SMatthias Ringwald 
25851c6a0fc0SMatthias Ringwald static void hfp_ag_run(void){
2586d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
2587d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2588d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2589d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
259022387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2591f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
25923deb3ec6SMatthias Ringwald     }
25933deb3ec6SMatthias Ringwald }
25943deb3ec6SMatthias Ringwald 
25951c6a0fc0SMatthias Ringwald static void hfp_ag_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2596fdf18f86SMilanka Ringwald     hfp_connection_t * hfp_connection = NULL;
25973deb3ec6SMatthias Ringwald     switch (packet_type){
25983deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
2599fdf18f86SMilanka Ringwald             hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
2600fd075f20SMilanka Ringwald             btstack_assert(hfp_connection != NULL);
2601fdf18f86SMilanka Ringwald 
2602fdf18f86SMilanka Ringwald             hfp_ag_handle_rfcomm_data(hfp_connection, packet, size);
2603fdf18f86SMilanka Ringwald             hfp_ag_run_for_context(hfp_connection);
2604fdf18f86SMilanka Ringwald             return;
26053deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2606e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2607e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2608fd075f20SMilanka Ringwald                 hfp_connection = get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid);
2609fd075f20SMilanka Ringwald                 btstack_assert(hfp_connection != NULL);
2610fd075f20SMilanka Ringwald 
2611fd075f20SMilanka Ringwald                 hfp_ag_run_for_context(hfp_connection);
2612e30a6a47SMatthias Ringwald                 return;
2613e30a6a47SMatthias Ringwald             }
261427950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2615aa4dd815SMatthias Ringwald             break;
26163deb3ec6SMatthias Ringwald         default:
26173deb3ec6SMatthias Ringwald             break;
26183deb3ec6SMatthias Ringwald     }
26193deb3ec6SMatthias Ringwald 
26201c6a0fc0SMatthias Ringwald     hfp_ag_run();
26213deb3ec6SMatthias Ringwald }
26223deb3ec6SMatthias Ringwald 
26231c6a0fc0SMatthias Ringwald static void hfp_ag_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2624405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
26251c6a0fc0SMatthias Ringwald     hfp_ag_run();
2626405014fbSMatthias Ringwald }
2627405014fbSMatthias Ringwald 
2628aa10b9cbSMatthias Ringwald void hfp_ag_init_codecs(uint8_t codecs_nr, const uint8_t * codecs){
2629aa10b9cbSMatthias Ringwald     btstack_assert(codecs_nr <= HFP_MAX_NUM_CODECS);
26308cd06fb6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS) return;
2631aa10b9cbSMatthias Ringwald 
2632aeb0f0feSMatthias Ringwald     hfp_ag_codecs_nr = codecs_nr;
2633aa10b9cbSMatthias Ringwald     uint8_t i;
26343deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
2635aeb0f0feSMatthias Ringwald         hfp_ag_codecs[i] = codecs[i];
26363deb3ec6SMatthias Ringwald     }
2637a0ffb263SMatthias Ringwald }
26383deb3ec6SMatthias Ringwald 
2639a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2640aeb0f0feSMatthias Ringwald     hfp_ag_supported_features = supported_features;
264140a8ee13SMatthias Ringwald     hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone();
2642a0ffb263SMatthias Ringwald }
26433deb3ec6SMatthias Ringwald 
26447ca89cabSMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, const hfp_ag_indicator_t * ag_indicators){
26458cd06fb6SMatthias Ringwald     btstack_assert(ag_indicators_nr <= HFP_MAX_NUM_INDICATORS);
26468cd06fb6SMatthias Ringwald     if (ag_indicators_nr > HFP_MAX_NUM_CODECS) return;
26478cd06fb6SMatthias Ringwald 
2648a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
26496535961aSMatthias Ringwald     (void)memcpy(hfp_ag_indicators, ag_indicators,
26506535961aSMatthias Ringwald                  ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2651a0ffb263SMatthias Ringwald }
26523deb3ec6SMatthias Ringwald 
26537ca89cabSMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, const hfp_generic_status_indicator_t * hf_indicators){
26548cd06fb6SMatthias Ringwald     btstack_assert(hf_indicators_nr <= HFP_MAX_NUM_INDICATORS);
26558cd06fb6SMatthias Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_CODECS) return;
26568cd06fb6SMatthias Ringwald 
2657aeb0f0feSMatthias Ringwald     hfp_ag_generic_status_indicators_nr = hf_indicators_nr;
2658aeb0f0feSMatthias Ringwald     (void)memcpy(hfp_ag_generic_status_indicators, hf_indicators,
26596535961aSMatthias Ringwald                  hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2660a0ffb263SMatthias Ringwald }
2661a0ffb263SMatthias Ringwald 
2662a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
26633deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
26646535961aSMatthias Ringwald     (void)memcpy(hfp_ag_call_hold_services, call_hold_services,
26656535961aSMatthias Ringwald                  call_hold_services_nr * sizeof(char *));
2666a0ffb263SMatthias Ringwald }
2667a0ffb263SMatthias Ringwald 
2668a0ffb263SMatthias Ringwald 
2669ab2445a0SMatthias Ringwald void hfp_ag_init(uint8_t rfcomm_channel_nr){
267027950165SMatthias Ringwald 
2671520c92d5SMatthias Ringwald     hfp_init();
267220b2edb6SMatthias Ringwald     hfp_ag_call_hold_services_nr = 0;
267327bb1817SMatthias Ringwald     hfp_ag_response_and_hold_active = false;
267420b2edb6SMatthias Ringwald     hfp_ag_indicators_nr = 0;
2675aeb0f0feSMatthias Ringwald     hfp_ag_codecs_nr = 0;
2676aeb0f0feSMatthias Ringwald     hfp_ag_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
267740a8ee13SMatthias Ringwald     hfp_ag_in_band_ring_tone_active = has_in_band_ring_tone();
2678aeb0f0feSMatthias Ringwald     hfp_ag_subscriber_numbers = NULL;
2679aeb0f0feSMatthias Ringwald     hfp_ag_subscriber_numbers_count = 0;
2680d63c37a1SMatthias Ringwald 
26811c6a0fc0SMatthias Ringwald     hfp_ag_hci_event_callback_registration.callback = &hfp_ag_hci_event_packet_handler;
26821c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_ag_hci_event_callback_registration);
268327950165SMatthias Ringwald 
26841c6a0fc0SMatthias Ringwald     rfcomm_register_service(&hfp_ag_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
268527950165SMatthias Ringwald 
268627950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
26871c6a0fc0SMatthias Ringwald     hfp_set_ag_rfcomm_packet_handler(&hfp_ag_rfcomm_packet_handler);
2688aa4dd815SMatthias Ringwald 
2689d210d9c4SMatthias Ringwald     hfp_gsm_init();
26903deb3ec6SMatthias Ringwald }
26913deb3ec6SMatthias Ringwald 
269220b2edb6SMatthias Ringwald void hfp_ag_deinit(void){
269320b2edb6SMatthias Ringwald     hfp_deinit();
269420b2edb6SMatthias Ringwald     hfp_gsm_deinit();
2695aeb0f0feSMatthias Ringwald 
2696aeb0f0feSMatthias Ringwald     hfp_ag_callback = NULL;
2697aeb0f0feSMatthias Ringwald     hfp_ag_supported_features = 0;
2698aeb0f0feSMatthias Ringwald     hfp_ag_codecs_nr = 0;
2699aeb0f0feSMatthias Ringwald     hfp_ag_indicators_nr = 0;
2700aeb0f0feSMatthias Ringwald     hfp_ag_call_hold_services_nr = 0;
270120b2edb6SMatthias Ringwald     (void) memset(&hfp_ag_call_hold_services, 0, sizeof(hfp_ag_call_hold_services));
2702aeb0f0feSMatthias Ringwald     hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
270320b2edb6SMatthias Ringwald     (void) memset(&hfp_ag_response_and_hold_state, 0, sizeof(hfp_response_and_hold_state_t));
2704aeb0f0feSMatthias Ringwald     hfp_ag_subscriber_numbers = NULL;
2705aeb0f0feSMatthias Ringwald     (void) memset(&hfp_ag_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
270681e25d0aSMatthias Ringwald     hfp_ag_custom_call_sm_handler = NULL;
270720b2edb6SMatthias Ringwald }
270820b2edb6SMatthias Ringwald 
27094eb3f1d8SMilanka Ringwald uint8_t hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
27104eb3f1d8SMilanka Ringwald     return hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
27113deb3ec6SMatthias Ringwald }
27123deb3ec6SMatthias Ringwald 
2713657bc59fSMilanka Ringwald uint8_t hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
27149c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2715a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2716657bc59fSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2717a33eb0c4SMilanka Ringwald     }
2718c5fa3c94SMilanka Ringwald 
27191ffa0dd9SMilanka Ringwald     hfp_trigger_release_service_level_connection(hfp_connection);
2720f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2721657bc59fSMilanka Ringwald     return ERROR_CODE_SUCCESS;
27223deb3ec6SMatthias Ringwald }
27233deb3ec6SMatthias Ringwald 
2724c5fa3c94SMilanka Ringwald uint8_t hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
27259c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2726a0ffb263SMatthias Ringwald     if (!hfp_connection){
2727c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
27283deb3ec6SMatthias Ringwald     }
27293deb3ec6SMatthias Ringwald 
2730c5fa3c94SMilanka Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
2731c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2732c5fa3c94SMilanka Ringwald     }
2733c5fa3c94SMilanka Ringwald 
2734c5fa3c94SMilanka Ringwald     hfp_connection->extended_audio_gateway_error = error;
2735c5fa3c94SMilanka Ringwald     hfp_ag_run_for_context(hfp_connection);
2736c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2737c5fa3c94SMilanka Ringwald }
2738c5fa3c94SMilanka Ringwald 
2739c5fa3c94SMilanka Ringwald static uint8_t hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
2740c5fa3c94SMilanka Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
2741c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2742c5fa3c94SMilanka Ringwald     }
2743c5fa3c94SMilanka Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO){
2744c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2745c5fa3c94SMilanka Ringwald     }
27463deb3ec6SMatthias Ringwald 
2747a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
27482561704cSMatthias Ringwald     hfp_connection->sco_for_msbc_failed = 0;
2749a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2750d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2751d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2752a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
27537522e673SMatthias Ringwald         // now, pick link settings
2754afac2a04SMilanka Ringwald         hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
27553721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
27563721a235SMatthias Ringwald         hfp_cc256x_prepare_for_sco(hfp_connection);
27573721a235SMatthias Ringwald #endif
2758c5fa3c94SMilanka Ringwald         return ERROR_CODE_SUCCESS;
27593deb3ec6SMatthias Ringwald     }
2760aa4dd815SMatthias Ringwald 
2761411a9776SMilanka Ringwald     uint8_t i;
2762411a9776SMilanka Ringwald     bool codec_was_in_use = false;
2763411a9776SMilanka Ringwald     bool better_codec_can_be_used = false;
2764411a9776SMilanka Ringwald 
2765411a9776SMilanka Ringwald     for (i = 0; i<hfp_connection->remote_codecs_nr; i++){
2766411a9776SMilanka Ringwald         if (hfp_connection->negotiated_codec == hfp_connection->remote_codecs[i]){
2767411a9776SMilanka Ringwald             codec_was_in_use = true;
2768411a9776SMilanka Ringwald         } else if (hfp_connection->negotiated_codec < hfp_connection->remote_codecs[i]){
2769411a9776SMilanka Ringwald             better_codec_can_be_used = true;
2770411a9776SMilanka Ringwald         }
2771411a9776SMilanka Ringwald     }
2772411a9776SMilanka Ringwald 
2773411a9776SMilanka Ringwald     if (!codec_was_in_use || better_codec_can_be_used){
2774bc94e48cSMatthias Ringwald         hfp_connection->ag_send_common_codec = true;
2775411a9776SMilanka Ringwald         hfp_connection->codecs_state = HFP_CODECS_IDLE;
2776411a9776SMilanka Ringwald     }
2777c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2778aa4dd815SMatthias Ringwald }
2779aa4dd815SMatthias Ringwald 
2780c5fa3c94SMilanka Ringwald uint8_t hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
27819c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2782a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2783c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2784a33eb0c4SMilanka Ringwald     }
2785c5fa3c94SMilanka Ringwald     uint8_t status = hfp_ag_setup_audio_connection(hfp_connection);
2786c5fa3c94SMilanka Ringwald     if (status != ERROR_CODE_SUCCESS){
2787c5fa3c94SMilanka Ringwald         return status;
2788c5fa3c94SMilanka Ringwald     }
2789f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2790c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
27913deb3ec6SMatthias Ringwald }
27923deb3ec6SMatthias Ringwald 
2793c5fa3c94SMilanka Ringwald uint8_t hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
27949c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2795a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2796c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2797a33eb0c4SMilanka Ringwald     }
2798c5fa3c94SMilanka Ringwald 
27990b4debbfSMilanka Ringwald     if (hfp_connection->vra_state == HFP_VRA_VOICE_RECOGNITION_ACTIVATED){
28000b4debbfSMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
28010b4debbfSMilanka Ringwald     }
28021a26de69SMilanka Ringwald 
28030b4debbfSMilanka Ringwald     uint8_t status = hfp_trigger_release_audio_connection(hfp_connection);
28040b4debbfSMilanka Ringwald     if (status == ERROR_CODE_SUCCESS){
2805f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
28060b4debbfSMilanka Ringwald     }
28070b4debbfSMilanka Ringwald     return status;
28083deb3ec6SMatthias Ringwald }
2809aa4dd815SMatthias Ringwald 
2810aa4dd815SMatthias Ringwald /**
2811aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2812aa4dd815SMatthias Ringwald  */
2813aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
281440a8ee13SMatthias Ringwald     if (has_in_band_ring_tone() == false){
281540a8ee13SMatthias Ringwald         return;
281640a8ee13SMatthias Ringwald     }
281740a8ee13SMatthias Ringwald     if (hfp_ag_in_band_ring_tone_active == use_in_band_ring_tone){
2818aa4dd815SMatthias Ringwald         return;
2819aa4dd815SMatthias Ringwald     }
2820c5fa3c94SMilanka Ringwald 
282140a8ee13SMatthias Ringwald     hfp_ag_in_band_ring_tone_active = use_in_band_ring_tone;
2822aa4dd815SMatthias Ringwald 
2823665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2824665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2825665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2826a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
282766c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
28289430c71eSMatthias Ringwald         hfp_connection->ag_send_in_band_ring_tone_setting = true;
2829f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2830aa4dd815SMatthias Ringwald     }
2831aa4dd815SMatthias Ringwald }
2832aa4dd815SMatthias Ringwald 
2833aa4dd815SMatthias Ringwald /**
2834aa4dd815SMatthias Ringwald  * @brief Called from GSM
2835aa4dd815SMatthias Ringwald  */
2836aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2837aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2838aa4dd815SMatthias Ringwald }
2839aa4dd815SMatthias Ringwald 
28400f1da533SMatthias Ringwald void hfp_ag_outgoing_call_initiated(void) {
2841fe899794SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, NULL);
2842fe899794SMatthias Ringwald }
2843fe899794SMatthias Ringwald 
2844aa4dd815SMatthias Ringwald /**
2845aa4dd815SMatthias Ringwald  * @brief number is stored.
2846aa4dd815SMatthias Ringwald  */
2847aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2848f8737b81SMatthias Ringwald     hfp_gsm_handler(HFP_AG_SET_CLIP, 0, type, number);
2849aa4dd815SMatthias Ringwald }
2850aa4dd815SMatthias Ringwald 
2851aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2852aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2853aa4dd815SMatthias Ringwald }
2854aa4dd815SMatthias Ringwald 
2855aa4dd815SMatthias Ringwald // call from AG UI
2856aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2857aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2858aa4dd815SMatthias Ringwald }
2859aa4dd815SMatthias Ringwald 
2860ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2861ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2862ce263fc8SMatthias Ringwald }
2863ce263fc8SMatthias Ringwald 
2864aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2865aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2866aa4dd815SMatthias Ringwald }
2867aa4dd815SMatthias Ringwald 
2868aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2869aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2870aa4dd815SMatthias Ringwald }
2871aa4dd815SMatthias Ringwald 
2872aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2873aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2874aa4dd815SMatthias Ringwald }
2875aa4dd815SMatthias Ringwald 
2876aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2877aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2878aa4dd815SMatthias Ringwald }
2879aa4dd815SMatthias Ringwald 
2880aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2881aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2882aa4dd815SMatthias Ringwald }
2883ce263fc8SMatthias Ringwald 
2884ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2885ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2886ce263fc8SMatthias Ringwald }
2887ce263fc8SMatthias Ringwald 
2888ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2889ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2890ce263fc8SMatthias Ringwald }
2891ce263fc8SMatthias Ringwald 
2892ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2893ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2894ce263fc8SMatthias Ringwald }
2895ce263fc8SMatthias Ringwald 
2896aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2897aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2898aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2899aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2900aa4dd815SMatthias Ringwald 
2901665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2902665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2903665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2904a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
290566c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2906a0ffb263SMatthias Ringwald         if (!hfp_connection->ag_indicators[indicator_index].enabled) {
2907245852b7SMilanka Ringwald             log_info("Requested AG indicator '%s' update to %u, but it is not enabled", hfp_ag_indicators[indicator_index].name, value);
2908ce263fc8SMatthias Ringwald             continue;
2909ce263fc8SMatthias Ringwald         }
2910245852b7SMilanka Ringwald         log_info("AG indicator '%s' changed to %u, request transfer status", hfp_ag_indicators[indicator_index].name, value);
2911a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2912f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2913aa4dd815SMatthias Ringwald     }
2914aa4dd815SMatthias Ringwald }
2915aa4dd815SMatthias Ringwald 
2916c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_registration_status(int registration_status){
2917c5fa3c94SMilanka Ringwald     if ((registration_status < 0) || (registration_status > 1)){
2918c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2919c5fa3c94SMilanka Ringwald     }
2920c5fa3c94SMilanka Ringwald 
2921c5fa3c94SMilanka Ringwald     if ( (registration_status == 0) && (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
2922245852b7SMilanka Ringwald 
2923245852b7SMilanka Ringwald         // if network goes away wihle a call is active:
2924245852b7SMilanka Ringwald         // - the  call gets dropped
2925245852b7SMilanka Ringwald         // - we send NO CARRIER
2926245852b7SMilanka Ringwald         // NOTE: the CALL=0 has to be sent before NO CARRIER
2927245852b7SMilanka Ringwald 
2928245852b7SMilanka Ringwald         hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2929245852b7SMilanka Ringwald 
2930245852b7SMilanka Ringwald         btstack_linked_list_iterator_t it;
2931245852b7SMilanka Ringwald         btstack_linked_list_iterator_init(&it, hfp_get_connections());
2932245852b7SMilanka Ringwald         while (btstack_linked_list_iterator_has_next(&it)){
2933245852b7SMilanka Ringwald             hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2934245852b7SMilanka Ringwald             hfp_connection->ag_send_no_carrier = true;
2935245852b7SMilanka Ringwald         }
2936245852b7SMilanka Ringwald     }
2937c5fa3c94SMilanka Ringwald     hfp_ag_set_ag_indicator("service", registration_status);
2938c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2939aa4dd815SMatthias Ringwald }
2940aa4dd815SMatthias Ringwald 
2941c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_signal_strength(int signal_strength){
2942c5fa3c94SMilanka Ringwald     if ((signal_strength < 0) || (signal_strength > 5)){
2943c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2944aa4dd815SMatthias Ringwald     }
2945aa4dd815SMatthias Ringwald 
2946c5fa3c94SMilanka Ringwald     hfp_ag_set_ag_indicator("signal", signal_strength);
2947c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2948aa4dd815SMatthias Ringwald }
2949aa4dd815SMatthias Ringwald 
2950c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_roaming_status(int roaming_status){
2951c5fa3c94SMilanka Ringwald     if ((roaming_status < 0) || (roaming_status > 1)){
2952c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2953aa4dd815SMatthias Ringwald     }
2954aa4dd815SMatthias Ringwald 
2955c5fa3c94SMilanka Ringwald     hfp_ag_set_ag_indicator("roam", roaming_status);
2956c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2957c5fa3c94SMilanka Ringwald }
2958c5fa3c94SMilanka Ringwald 
2959c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_battery_level(int battery_level){
2960c5fa3c94SMilanka Ringwald     if ((battery_level < 0) || (battery_level > 5)){
2961c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2962c5fa3c94SMilanka Ringwald     }
2963c5fa3c94SMilanka Ringwald     hfp_ag_set_ag_indicator("battchg", battery_level);
2964c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
2965c5fa3c94SMilanka Ringwald }
2966c5fa3c94SMilanka Ringwald 
29676b8275b0SMilanka Ringwald uint8_t hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle){
296814c148caSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
296914c148caSMilanka Ringwald     if (!hfp_connection){
297014c148caSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
29716b8275b0SMilanka Ringwald     }
2972f71d2aacSMilanka Ringwald 
297384fb9ac1SMilanka Ringwald     if (hfp_connection->emit_vra_enabled_after_audio_established){
297484fb9ac1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
297584fb9ac1SMilanka Ringwald     }
297684fb9ac1SMilanka Ringwald 
29779cd26862SMilanka Ringwald     bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection);
29789cd26862SMilanka Ringwald     bool legacy_vra_supported   = hfp_ag_vra_flag_supported(hfp_connection);
2979f71d2aacSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
298014c148caSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
29816b8275b0SMilanka Ringwald     }
29826b8275b0SMilanka Ringwald 
2983135d9718SMilanka Ringwald     if (!hfp_ag_can_activate_voice_recognition(hfp_connection)){
2984135d9718SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
2985f71d2aacSMilanka Ringwald     }
2986f71d2aacSMilanka Ringwald 
2987f71d2aacSMilanka Ringwald     hfp_connection->ag_activate_voice_recognition_value = 1;
2988f71d2aacSMilanka Ringwald     hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_ACTIVATED;
2989fcf4ede6SMilanka Ringwald     hfp_connection->enhanced_voice_recognition_enabled = enhanced_vra_supported;
2990f71d2aacSMilanka Ringwald     hfp_connection->ag_audio_connection_opened_before_vra = hfp_ag_is_audio_connection_active(hfp_connection);
2991f71d2aacSMilanka Ringwald     hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
29928f8818a4SMatthias Ringwald     hfp_connection->ag_vra_send_command = true;
2993f71d2aacSMilanka Ringwald     hfp_ag_run_for_context(hfp_connection);
2994f71d2aacSMilanka Ringwald     return ERROR_CODE_SUCCESS;
2995f71d2aacSMilanka Ringwald }
2996f71d2aacSMilanka Ringwald 
2997d84719b1SMilanka Ringwald uint8_t hfp_ag_deactivate_voice_recognition(hci_con_handle_t acl_handle){
299814c148caSMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
299914c148caSMilanka Ringwald     if (!hfp_connection){
300014c148caSMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
300145796ff1SMilanka Ringwald     }
3002f71d2aacSMilanka Ringwald 
300384fb9ac1SMilanka Ringwald     if (hfp_connection->emit_vra_enabled_after_audio_established){
300484fb9ac1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
300584fb9ac1SMilanka Ringwald     }
300684fb9ac1SMilanka Ringwald 
30079cd26862SMilanka Ringwald     bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection);
30089cd26862SMilanka Ringwald     bool legacy_vra_supported = hfp_ag_vra_flag_supported(hfp_connection);
30099cd26862SMilanka Ringwald 
3010f71d2aacSMilanka Ringwald     if (!enhanced_vra_supported && !legacy_vra_supported){
301114c148caSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
301245796ff1SMilanka Ringwald     }
301345796ff1SMilanka Ringwald 
3014135d9718SMilanka Ringwald     if (!hfp_ag_voice_recognition_session_active(hfp_connection)){
3015135d9718SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
3016f71d2aacSMilanka Ringwald     }
3017f71d2aacSMilanka Ringwald 
3018f71d2aacSMilanka Ringwald     hfp_connection->ag_activate_voice_recognition_value = 0;
3019f71d2aacSMilanka Ringwald     hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_VOICE_RECOGNITION_OFF;
3020f71d2aacSMilanka Ringwald     hfp_connection->ag_vra_state = HFP_VOICE_RECOGNITION_STATE_AG_READY;
30218f8818a4SMatthias Ringwald     hfp_connection->ag_vra_send_command = true;
3022f71d2aacSMilanka Ringwald     hfp_ag_run_for_context(hfp_connection);
3023f71d2aacSMilanka Ringwald     return ERROR_CODE_SUCCESS;
3024f71d2aacSMilanka Ringwald }
3025f71d2aacSMilanka Ringwald 
3026cf75be85SMilanka Ringwald static uint8_t hfp_ag_enhanced_voice_recognition_send_state(hci_con_handle_t acl_handle, hfp_voice_recognition_state_t state){
302745796ff1SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
302845796ff1SMilanka Ringwald     if (!hfp_connection){
3029c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
303045796ff1SMilanka Ringwald     }
3031c5fa3c94SMilanka Ringwald 
303284fb9ac1SMilanka Ringwald     if (hfp_connection->emit_vra_enabled_after_audio_established){
303384fb9ac1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
3034f71d2aacSMilanka Ringwald     }
3035f71d2aacSMilanka Ringwald 
3036645bfa78SMilanka Ringwald     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
3037645bfa78SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
3038645bfa78SMilanka Ringwald     }
3039645bfa78SMilanka Ringwald 
3040645bfa78SMilanka Ringwald     if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){
3041135d9718SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
30429cd26862SMilanka Ringwald     }
30439cd26862SMilanka Ringwald 
304484fb9ac1SMilanka Ringwald     bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection);
304584fb9ac1SMilanka Ringwald     if (!enhanced_vra_supported ){
304684fb9ac1SMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
304784fb9ac1SMilanka Ringwald     }
304884fb9ac1SMilanka Ringwald 
304984fb9ac1SMilanka Ringwald 
305045796ff1SMilanka Ringwald     hfp_connection->ag_vra_state = state;
3051cf75be85SMilanka Ringwald     hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_STATUS;
30528f8818a4SMatthias Ringwald     hfp_connection->ag_vra_send_command = true;
305345796ff1SMilanka Ringwald     hfp_ag_run_for_context(hfp_connection);
30549cd26862SMilanka Ringwald     return ERROR_CODE_SUCCESS;
305545796ff1SMilanka Ringwald }
305645796ff1SMilanka Ringwald 
3057f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_sending_audio(hci_con_handle_t acl_handle){
3058cf75be85SMilanka Ringwald     return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_STARTING_SOUND);
305945796ff1SMilanka Ringwald }
3060f0c27a95SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_ready_for_audio(hci_con_handle_t acl_handle){
3061cf75be85SMilanka Ringwald     return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_READY_TO_ACCEPT_AUDIO_INPUT);
306245796ff1SMilanka Ringwald }
3063cf75be85SMilanka Ringwald uint8_t hfp_ag_enhanced_voice_recognition_report_processing_input(hci_con_handle_t acl_handle){
3064cf75be85SMilanka Ringwald     return hfp_ag_enhanced_voice_recognition_send_state(acl_handle, HFP_VOICE_RECOGNITION_STATE_AG_IS_PROCESSING_AUDIO_INPUT);
306545796ff1SMilanka Ringwald }
306645796ff1SMilanka Ringwald 
3067b95cac54SMilanka Ringwald 
3068b95cac54SMilanka 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){
3069b95cac54SMilanka Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3070b95cac54SMilanka Ringwald     if (!hfp_connection){
3071b95cac54SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3072b95cac54SMilanka Ringwald     }
3073b95cac54SMilanka Ringwald 
307484fb9ac1SMilanka Ringwald     if (hfp_connection->emit_vra_enabled_after_audio_established){
307584fb9ac1SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
3076f71d2aacSMilanka Ringwald     }
3077f71d2aacSMilanka Ringwald 
3078645bfa78SMilanka Ringwald     if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED){
3079645bfa78SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
3080645bfa78SMilanka Ringwald     }
3081645bfa78SMilanka Ringwald 
3082645bfa78SMilanka Ringwald     if (hfp_connection->vra_state != HFP_VRA_ENHANCED_VOICE_RECOGNITION_READY_FOR_AUDIO){
3083135d9718SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
3084f71d2aacSMilanka Ringwald     }
3085f71d2aacSMilanka Ringwald 
308684fb9ac1SMilanka Ringwald     bool enhanced_vra_supported = hfp_ag_enhanced_vra_flag_supported(hfp_connection);
308784fb9ac1SMilanka Ringwald     if (!enhanced_vra_supported ){
308884fb9ac1SMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
308984fb9ac1SMilanka Ringwald     }
309084fb9ac1SMilanka Ringwald 
309184fb9ac1SMilanka Ringwald     bool enhanced_vra_msg_supported = hfp_ag_can_send_enhanced_vra_message_flag_supported(hfp_connection);
309284fb9ac1SMilanka Ringwald     if (!enhanced_vra_msg_supported ){
309384fb9ac1SMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
309484fb9ac1SMilanka Ringwald     }
309584fb9ac1SMilanka Ringwald 
3096ab2445a0SMatthias Ringwald     uint16_t message_len = (uint16_t) strlen(msg.text);
309751aa5d5aSMilanka Ringwald 
309889e7c136SMilanka Ringwald     if (message_len > HFP_MAX_VR_TEXT_SIZE){
30996504a03aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
31006504a03aSMilanka Ringwald     }
31016504a03aSMilanka Ringwald 
31026504a03aSMilanka Ringwald     if ((HFP_VR_TEXT_HEADER_SIZE + message_len) > hfp_connection->rfcomm_mtu){
310351aa5d5aSMilanka Ringwald         return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
310451aa5d5aSMilanka Ringwald     }
310551aa5d5aSMilanka Ringwald 
3106b95cac54SMilanka Ringwald     hfp_connection->vra_state_requested = HFP_VRA_W2_SEND_ENHANCED_VOICE_RECOGNITION_MSG;
3107b95cac54SMilanka Ringwald     hfp_connection->ag_msg = msg;
3108b95cac54SMilanka Ringwald     hfp_connection->ag_vra_state = state;
31098f8818a4SMatthias Ringwald     hfp_connection->ag_vra_send_command = true;
3110b95cac54SMilanka Ringwald     hfp_ag_run_for_context(hfp_connection);
3111f71d2aacSMilanka Ringwald 
3112f71d2aacSMilanka Ringwald     return ERROR_CODE_SUCCESS;
3113b95cac54SMilanka Ringwald }
3114b95cac54SMilanka Ringwald 
3115b95cac54SMilanka Ringwald 
3116c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
31179c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3118a33eb0c4SMilanka Ringwald     if (!hfp_connection){
3119c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3120a33eb0c4SMilanka Ringwald     }
3121c5fa3c94SMilanka Ringwald 
3122c5fa3c94SMilanka Ringwald     if ((gain < 0) || (gain > 15)){
3123c5fa3c94SMilanka Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
3124c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
3125c5fa3c94SMilanka Ringwald     }
3126c5fa3c94SMilanka Ringwald 
3127a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
3128a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
3129a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
3130aa4dd815SMatthias Ringwald     }
3131f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
3132c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
3133aa4dd815SMatthias Ringwald }
3134aa4dd815SMatthias Ringwald 
3135c5fa3c94SMilanka Ringwald uint8_t hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
31369c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3137a33eb0c4SMilanka Ringwald     if (!hfp_connection){
3138c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3139a33eb0c4SMilanka Ringwald     }
3140c5fa3c94SMilanka Ringwald 
3141c5fa3c94SMilanka Ringwald     if ((gain < 0) || (gain > 15)){
3142c5fa3c94SMilanka Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
3143c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
3144c5fa3c94SMilanka Ringwald     }
3145c5fa3c94SMilanka Ringwald 
3146a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
3147a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
3148a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
3149aa4dd815SMatthias Ringwald     }
3150f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
3151c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
3152aa4dd815SMatthias Ringwald }
3153aa4dd815SMatthias Ringwald 
3154c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
31559c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3156a33eb0c4SMilanka Ringwald     if (!hfp_connection){
3157c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3158a33eb0c4SMilanka Ringwald     }
3159c5fa3c94SMilanka Ringwald 
3160aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
3161a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
3162c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
3163aa4dd815SMatthias Ringwald }
3164aa4dd815SMatthias Ringwald 
3165c5fa3c94SMilanka Ringwald uint8_t hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
31669c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3167a33eb0c4SMilanka Ringwald     if (!hfp_connection){
3168c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3169a33eb0c4SMilanka Ringwald     }
3170c5fa3c94SMilanka Ringwald 
3171e84fa067SMatthias Ringwald     hfp_ag_queue_error(hfp_connection);
3172c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
3173aa4dd815SMatthias Ringwald }
3174aa4dd815SMatthias Ringwald 
3175c5fa3c94SMilanka Ringwald uint8_t hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
31769c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3177a33eb0c4SMilanka Ringwald     if (!hfp_connection){
3178c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3179a33eb0c4SMilanka Ringwald     }
3180c5fa3c94SMilanka Ringwald 
3181e84fa067SMatthias Ringwald     hfp_ag_queue_ok(hfp_connection);
3182c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
3183c1797c7dSMatthias Ringwald }
3184aa4dd815SMatthias Ringwald 
31855be6b47aSMatthias Ringwald uint8_t hfp_ag_send_unsolicited_result_code(hci_con_handle_t acl_handle, const char * unsolicited_result_code){
31865be6b47aSMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
31875be6b47aSMatthias Ringwald     if (!hfp_connection){
31885be6b47aSMatthias Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
31895be6b47aSMatthias Ringwald     }
31905be6b47aSMatthias Ringwald     if (hfp_connection->send_custom_message != NULL){
31915be6b47aSMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
31925be6b47aSMatthias Ringwald     }
31935be6b47aSMatthias Ringwald     hfp_connection->send_custom_message = unsolicited_result_code;
31945be6b47aSMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
31955be6b47aSMatthias Ringwald     return ERROR_CODE_SUCCESS;
31965be6b47aSMatthias Ringwald }
31975be6b47aSMatthias Ringwald 
3198471dea41SMatthias Ringwald uint8_t hfp_ag_send_command_result_code(hci_con_handle_t acl_handle, bool ok){
3199471dea41SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3200471dea41SMatthias Ringwald     if (!hfp_connection){
3201471dea41SMatthias Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3202471dea41SMatthias Ringwald     }
3203471dea41SMatthias Ringwald     if (ok){
3204e84fa067SMatthias Ringwald         hfp_ag_queue_ok(hfp_connection);
3205471dea41SMatthias Ringwald     } else {
3206e84fa067SMatthias Ringwald         hfp_ag_queue_error(hfp_connection);
3207471dea41SMatthias Ringwald     }
3208471dea41SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
3209471dea41SMatthias Ringwald     return ERROR_CODE_SUCCESS;
3210471dea41SMatthias Ringwald }
3211471dea41SMatthias Ringwald 
3212ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
3213aeb0f0feSMatthias Ringwald     hfp_ag_subscriber_numbers = numbers;
3214aeb0f0feSMatthias Ringwald     hfp_ag_subscriber_numbers_count = numbers_count;
3215ce263fc8SMatthias Ringwald }
3216ce263fc8SMatthias Ringwald 
32179cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
32189cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
3219ce263fc8SMatthias Ringwald }
3220ce263fc8SMatthias Ringwald 
32219de679b7SMilanka Ringwald void hfp_ag_set_last_dialed_number(const char * number){
32229de679b7SMilanka Ringwald     hfp_gsm_set_last_dialed_number(number);
32239de679b7SMilanka Ringwald }
32249de679b7SMilanka Ringwald 
3225c5fa3c94SMilanka Ringwald uint8_t hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
32269c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
3227a33eb0c4SMilanka Ringwald     if (!hfp_connection){
3228c5fa3c94SMilanka Ringwald         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
3229a33eb0c4SMilanka Ringwald     }
3230c5fa3c94SMilanka Ringwald 
3231c5fa3c94SMilanka Ringwald     if (!hfp_connection->call_waiting_notification_enabled){
3232c5fa3c94SMilanka Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
3233c5fa3c94SMilanka Ringwald     }
3234a0ffb263SMatthias Ringwald 
3235a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
3236f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
3237c5fa3c94SMilanka Ringwald     return ERROR_CODE_SUCCESS;
3238a0ffb263SMatthias Ringwald }
3239aa10b9cbSMatthias Ringwald void hfp_ag_create_sdp_record_with_codecs(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr,
3240aa10b9cbSMatthias Ringwald                                           const char * name, uint8_t ability_to_reject_call, uint16_t supported_features,
3241aa10b9cbSMatthias Ringwald                                           uint8_t codecs_nr, const uint8_t * codecs){
324276cc1527SMatthias Ringwald     if (!name){
3243aeb0f0feSMatthias Ringwald         name = hfp_ag_default_service_name;
324476cc1527SMatthias Ringwald     }
324576cc1527SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
324676cc1527SMatthias Ringwald 
324776cc1527SMatthias Ringwald     /*
324876cc1527SMatthias Ringwald      * 0x01 – Ability to reject a call
324976cc1527SMatthias Ringwald      * 0x00 – No ability to reject a call
325076cc1527SMatthias Ringwald      */
325176cc1527SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
325276cc1527SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
325376cc1527SMatthias Ringwald 
325476cc1527SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
325576cc1527SMatthias Ringwald     //
325676cc1527SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
325776cc1527SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
325876cc1527SMatthias Ringwald     //
3259aa10b9cbSMatthias Ringwald     // Wide band speech (bit 5) and LC3-SWB (bit 8) require Codec negotiation
326076cc1527SMatthias Ringwald     //
326176cc1527SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
3262ef3ae4ebSMilanka Ringwald 
3263ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_AGSF_ENHANCED_VOICE_RECOGNITION_STATUS)){
3264ef3ae4ebSMilanka Ringwald         sdp_features |= 1 << 6;
3265ef3ae4ebSMilanka Ringwald     }
3266ef3ae4ebSMilanka Ringwald 
3267ef3ae4ebSMilanka Ringwald     if (supported_features & (1 << HFP_AGSF_VOICE_RECOGNITION_TEXT)){
3268ef3ae4ebSMilanka Ringwald         sdp_features |= 1 << 7;
3269ef3ae4ebSMilanka Ringwald     }
3270ef3ae4ebSMilanka Ringwald 
3271aa10b9cbSMatthias Ringwald     // codecs
3272aa10b9cbSMatthias Ringwald     if ((supported_features & (1 << HFP_HFSF_CODEC_NEGOTIATION)) != 0){
3273aa10b9cbSMatthias Ringwald         uint8_t i;
3274aa10b9cbSMatthias Ringwald         for (i=0;i<codecs_nr;i++){
3275aa10b9cbSMatthias Ringwald             switch (codecs[i]){
3276aa10b9cbSMatthias Ringwald                 case HFP_CODEC_MSBC:
3277aa10b9cbSMatthias Ringwald                     sdp_features |= 1 << 5;
3278aa10b9cbSMatthias Ringwald                     break;
3279aa10b9cbSMatthias Ringwald                 case HFP_CODEC_LC3_SWB:
3280aa10b9cbSMatthias Ringwald                     sdp_features |= 1 << 8;
3281aa10b9cbSMatthias Ringwald                     break;
328275389f80SMatthias Ringwald                 default:
328375389f80SMatthias Ringwald                     break;
3284aa10b9cbSMatthias Ringwald             }
3285aa10b9cbSMatthias Ringwald         }
3286aa10b9cbSMatthias Ringwald     }
3287aa10b9cbSMatthias Ringwald 
328876cc1527SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
328976cc1527SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
329076cc1527SMatthias Ringwald }
329176cc1527SMatthias Ringwald 
3292aa10b9cbSMatthias Ringwald void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name,
3293aa10b9cbSMatthias Ringwald                               uint8_t ability_to_reject_call, uint16_t supported_features, int wide_band_speech){
3294aa10b9cbSMatthias Ringwald     uint8_t codecs_nr;
3295aa10b9cbSMatthias Ringwald     const uint8_t * codecs;
3296aa10b9cbSMatthias Ringwald     const uint8_t wide_band_codecs[] = { HFP_CODEC_MSBC };
3297aa10b9cbSMatthias Ringwald     if (wide_band_speech == 0){
3298aa10b9cbSMatthias Ringwald         codecs_nr = 0;
3299aa10b9cbSMatthias Ringwald         codecs = NULL;
3300aa10b9cbSMatthias Ringwald     } else {
3301aa10b9cbSMatthias Ringwald         codecs_nr = 1;
3302aa10b9cbSMatthias Ringwald         codecs = wide_band_codecs;
3303aa10b9cbSMatthias Ringwald     }
3304aa10b9cbSMatthias Ringwald     hfp_ag_create_sdp_record_with_codecs(service, service_record_handle, rfcomm_channel_nr, name,
3305aa10b9cbSMatthias Ringwald                                          ability_to_reject_call, supported_features, codecs_nr, codecs);
3306aa10b9cbSMatthias Ringwald }
3307aa10b9cbSMatthias Ringwald 
330876cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
330968466199SMilanka Ringwald 	btstack_assert(callback != NULL);
331068466199SMilanka Ringwald 
331176cc1527SMatthias Ringwald 	hfp_ag_callback = callback;
331276cc1527SMatthias Ringwald 	hfp_set_ag_callback(callback);
331376cc1527SMatthias Ringwald }
331481e25d0aSMatthias Ringwald 
331581e25d0aSMatthias Ringwald void hfp_ag_register_custom_call_sm_handler(bool (*handler)(hfp_ag_call_event_t event)){
331681e25d0aSMatthias Ringwald     hfp_ag_custom_call_sm_handler = handler;
331781e25d0aSMatthias Ringwald }
3318471dea41SMatthias Ringwald 
3319471dea41SMatthias Ringwald void hfp_ag_register_custom_at_command(hfp_custom_at_command_t * custom_at_command){
3320471dea41SMatthias Ringwald     hfp_register_custom_ag_command(custom_at_command);
3321471dea41SMatthias Ringwald }
3322