xref: /btstack/src/classic/hfp_ag.c (revision c5753b03751b5a2bda966a9e68a10b892e192109)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
37ab2c6ae4SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hfp_ag.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
4266a048abSMatthias Ringwald // HFP Audio Gateway (AG) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493deb3ec6SMatthias Ringwald #include <string.h>
503deb3ec6SMatthias Ringwald 
5156042629SMatthias Ringwald #include "hci_cmd.h"
5282636622SMatthias Ringwald #include "btstack_run_loop.h"
533deb3ec6SMatthias Ringwald 
54235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
553deb3ec6SMatthias Ringwald #include "hci.h"
563deb3ec6SMatthias Ringwald #include "btstack_memory.h"
573deb3ec6SMatthias Ringwald #include "hci_dump.h"
583deb3ec6SMatthias Ringwald #include "l2cap.h"
5916ece135SMatthias Ringwald #include "btstack_debug.h"
60e30a6a47SMatthias Ringwald #include "btstack_event.h"
6159c6af15SMatthias Ringwald #include "classic/core.h"
623edc84c5SMatthias Ringwald #include "classic/hfp.h"
633edc84c5SMatthias Ringwald #include "classic/hfp_ag.h"
64023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h"
65efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
66023f2764SMatthias Ringwald #include "classic/sdp_server.h"
67023f2764SMatthias Ringwald #include "classic/sdp_util.h"
683deb3ec6SMatthias Ringwald 
69c5b64319SMatthias Ringwald // private prototypes
70f0aeb307SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection);
71adaba9f3SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
72adaba9f3SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
73c5b64319SMatthias Ringwald 
74c5b64319SMatthias Ringwald // public prototypes
7535833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void);
7635833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void);
77c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr);
78c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr);
79c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context);
80c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context);
81c5b64319SMatthias Ringwald 
8227950165SMatthias Ringwald 
8320b2edb6SMatthias Ringwald // const
843deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway";
85aa4dd815SMatthias Ringwald 
8620b2edb6SMatthias Ringwald // globals
8720b2edb6SMatthias Ringwald static btstack_packet_callback_registration_t hfp_ag_hci_event_callback_registration;
88aa4dd815SMatthias Ringwald 
8920b2edb6SMatthias Ringwald static uint16_t hfp_supported_features;
9020b2edb6SMatthias Ringwald 
9120b2edb6SMatthias Ringwald static uint8_t hfp_codecs_nr;
923deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
933deb3ec6SMatthias Ringwald 
9420b2edb6SMatthias Ringwald static int  hfp_ag_indicators_nr;
9525789943SMilanka Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_INDICATORS];
963deb3ec6SMatthias Ringwald 
9720b2edb6SMatthias Ringwald static int hfp_generic_status_indicators_nr;
9825789943SMilanka Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_INDICATORS];
99a0ffb263SMatthias Ringwald 
10020b2edb6SMatthias Ringwald static int  hfp_ag_call_hold_services_nr;
1013deb3ec6SMatthias Ringwald static char *hfp_ag_call_hold_services[6];
102ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_ag_callback;
1033deb3ec6SMatthias Ringwald 
104ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
10520b2edb6SMatthias Ringwald static int hfp_ag_response_and_hold_active;
106aa4dd815SMatthias Ringwald 
10720b2edb6SMatthias Ringwald // Subscriber information entries
10820b2edb6SMatthias Ringwald static hfp_phone_number_t * subscriber_numbers;
10920b2edb6SMatthias Ringwald static int subscriber_numbers_count;
110ce263fc8SMatthias Ringwald 
11120b2edb6SMatthias Ringwald // code
112a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
113a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
114a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1156535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1166535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1173deb3ec6SMatthias Ringwald     }
118a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
119a0ffb263SMatthias Ringwald }
120a0ffb263SMatthias Ringwald 
121a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
122a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
123a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
124a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
1256535961aSMatthias Ringwald         (void)memcpy(hfp_connection->ag_indicators, hfp_ag_indicators,
1266535961aSMatthias Ringwald                      hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
127a0ffb263SMatthias Ringwald     }
128a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1293deb3ec6SMatthias Ringwald }
1303deb3ec6SMatthias Ringwald 
131aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
132aa4dd815SMatthias Ringwald     int i;
133aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
134aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
135aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
136aa4dd815SMatthias Ringwald         }
137aa4dd815SMatthias Ringwald     }
138aa4dd815SMatthias Ringwald     return NULL;
139aa4dd815SMatthias Ringwald }
140aa4dd815SMatthias Ringwald 
141aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
142aa4dd815SMatthias Ringwald     int i;
143aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
144aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
145aa4dd815SMatthias Ringwald             return i;
146aa4dd815SMatthias Ringwald         }
147aa4dd815SMatthias Ringwald     }
148aa4dd815SMatthias Ringwald     return -1;
149aa4dd815SMatthias Ringwald }
150aa4dd815SMatthias Ringwald 
1519c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_ag_connection_context_for_acl_handle(uint16_t handle){
1529c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1539c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1549c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1559c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1569c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
1579c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1589c9c64c1SMatthias Ringwald         return hfp_connection;
1599c9c64c1SMatthias Ringwald     }
1609c9c64c1SMatthias Ringwald     return NULL;
1619c9c64c1SMatthias Ringwald }
1623deb3ec6SMatthias Ringwald 
1630cb5b971SMatthias Ringwald static int use_in_band_tone(void){
164aa4dd815SMatthias Ringwald     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1653deb3ec6SMatthias Ringwald }
1663deb3ec6SMatthias Ringwald 
167a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
168a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1693deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1703deb3ec6SMatthias Ringwald     return hf && ag;
1713deb3ec6SMatthias Ringwald }
1723deb3ec6SMatthias Ringwald 
173a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
174a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1753deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1763deb3ec6SMatthias Ringwald     return hf && ag;
1773deb3ec6SMatthias Ringwald }
1783deb3ec6SMatthias Ringwald 
179a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
180a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
1813deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
1823deb3ec6SMatthias Ringwald     return hf && ag;
1833deb3ec6SMatthias Ringwald }
1843deb3ec6SMatthias Ringwald 
18576cc1527SMatthias Ringwald /* unsolicited responses */
186aa4dd815SMatthias Ringwald 
187485ac19eSMilanka Ringwald static int hfp_ag_send_change_in_band_ring_tone_setting_cmd(uint16_t cid){
188aa4dd815SMatthias Ringwald     char buffer[20];
189ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n",
190ff7d6aeaSMatthias Ringwald              HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
191ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
192aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1933deb3ec6SMatthias Ringwald }
1943deb3ec6SMatthias Ringwald 
1953deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
1963deb3ec6SMatthias Ringwald     char buffer[40];
197ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n\r\nOK\r\n",
198ff7d6aeaSMatthias Ringwald              HFP_SUPPORTED_FEATURES, hfp_supported_features);
199ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
2003deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2013deb3ec6SMatthias Ringwald }
2023deb3ec6SMatthias Ringwald 
203485ac19eSMilanka Ringwald static int hfp_ag_send_ok(uint16_t cid){
2043deb3ec6SMatthias Ringwald     char buffer[10];
205ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\nOK\r\n");
206ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
2073deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2083deb3ec6SMatthias Ringwald }
2093deb3ec6SMatthias Ringwald 
210485ac19eSMilanka Ringwald static int hfp_ag_send_ring(uint16_t cid){
211aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
212aa4dd815SMatthias Ringwald }
213aa4dd815SMatthias Ringwald 
214245852b7SMilanka Ringwald static int hfp_ag_send_no_carrier(uint16_t cid){
215245852b7SMilanka Ringwald     char buffer[15];
216245852b7SMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\nNO CARRIER\r\n");
217245852b7SMilanka Ringwald     buffer[sizeof(buffer) - 1] = 0;
218245852b7SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
219245852b7SMilanka Ringwald }
220245852b7SMilanka Ringwald 
221aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
222aa4dd815SMatthias Ringwald     char buffer[50];
223ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP,
224ff7d6aeaSMatthias Ringwald              hfp_gsm_clip_number(), hfp_gsm_clip_type());
225ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
226aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
227aa4dd815SMatthias Ringwald }
228aa4dd815SMatthias Ringwald 
229ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
230ce263fc8SMatthias Ringwald     char buffer[50];
231ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: ,\"%s\",%u, , \r\n",
232ff7d6aeaSMatthias Ringwald              HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
233ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
234ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
235ce263fc8SMatthias Ringwald }
236ce263fc8SMatthias Ringwald 
237c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
238aa4dd815SMatthias Ringwald     char buffer[50];
239ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: %s\r\n",
240ff7d6aeaSMatthias Ringwald              HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
241ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
242aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
243aa4dd815SMatthias Ringwald }
244aa4dd815SMatthias Ringwald 
245aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
246aa4dd815SMatthias Ringwald     char buffer[50];
247ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: \"%s\",%u\r\n",
248ff7d6aeaSMatthias Ringwald              HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(),
249ff7d6aeaSMatthias Ringwald              hfp_gsm_clip_type());
250ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
251aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
252aa4dd815SMatthias Ringwald }
253aa4dd815SMatthias Ringwald 
254485ac19eSMilanka Ringwald static int hfp_ag_send_error(uint16_t cid){
2553deb3ec6SMatthias Ringwald     char buffer[10];
256ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\nERROR\r\n");
257ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
2583deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2593deb3ec6SMatthias Ringwald }
2603deb3ec6SMatthias Ringwald 
261485ac19eSMilanka Ringwald static int hfp_ag_send_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2623deb3ec6SMatthias Ringwald     char buffer[20];
263ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s=%d\r\n",
264ff7d6aeaSMatthias Ringwald              HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
265ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
2663deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2673deb3ec6SMatthias Ringwald }
2683deb3ec6SMatthias Ringwald 
269ad902e3dSMatthias Ringwald // get size for indicator string
270a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
271ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
27245718b6fSMatthias Ringwald     return 8 + (int) strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
273a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
274a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
275ad902e3dSMatthias Ringwald }
276ad902e3dSMatthias Ringwald 
277ad902e3dSMatthias Ringwald // store indicator
2781167ff83SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer, uint16_t buffer_size){
2791167ff83SMatthias Ringwald     snprintf((char *)buffer, buffer_size, "(\"%s\",(%d,%d)),",
280a0ffb263SMatthias Ringwald              hfp_ag_get_ag_indicators(hfp_connection)[i].name,
281a0ffb263SMatthias Ringwald              hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
282a0ffb263SMatthias Ringwald              hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2831167ff83SMatthias Ringwald     ((char *)buffer)[buffer_size - 1] = 0;
2843deb3ec6SMatthias Ringwald }
285ad902e3dSMatthias Ringwald 
286ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
287a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
288a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
289ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
290c1ab6cc1SMatthias Ringwald     return 3 + ((num_indicators-1) * 2);
2913deb3ec6SMatthias Ringwald }
292ad902e3dSMatthias Ringwald 
293ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
294a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
295ad902e3dSMatthias Ringwald     if (index == 0) {
296ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
297ad902e3dSMatthias Ringwald     }
298ad902e3dSMatthias Ringwald     index--;
299a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
300ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
301ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
302a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
303ad902e3dSMatthias Ringwald     }
304c1ab6cc1SMatthias Ringwald     if (indicator_index == (num_indicators - 1)){
305ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
306ad902e3dSMatthias Ringwald     }
307ad902e3dSMatthias Ringwald     return 1; // comma
308ad902e3dSMatthias Ringwald }
309ad902e3dSMatthias Ringwald 
3101167ff83SMatthias Ringwald static void hfp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer, uint16_t buffer_size){
311ad902e3dSMatthias Ringwald     if (index == 0){
312ad902e3dSMatthias Ringwald         *buffer++ = '\r';
31374386ee0SMatthias Ringwald         *buffer++ = '\n';
314ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
3156535961aSMatthias Ringwald         (void)memcpy(buffer, HFP_INDICATOR, len);
316ad902e3dSMatthias Ringwald         buffer += len;
317ad902e3dSMatthias Ringwald         *buffer++ = ':';
318ad902e3dSMatthias Ringwald         return;
319ad902e3dSMatthias Ringwald     }
320ad902e3dSMatthias Ringwald     index--;
321a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
322ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
323ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
3241167ff83SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer, buffer_size);
325ad902e3dSMatthias Ringwald         return;
326ad902e3dSMatthias Ringwald     }
327c1ab6cc1SMatthias Ringwald     if (indicator_index == (num_indicators-1)){
3286535961aSMatthias Ringwald         (void)memcpy(buffer, "\r\n\r\nOK\r\n", 8);
329ad902e3dSMatthias Ringwald         return;
330ad902e3dSMatthias Ringwald     }
331ad902e3dSMatthias Ringwald     *buffer = ',';
3323deb3ec6SMatthias Ringwald }
3333deb3ec6SMatthias Ringwald 
3343deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
335c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3363deb3ec6SMatthias Ringwald     int i;
3373deb3ec6SMatthias Ringwald     int offset = 0;
338c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_generic_status_indicators_nr-1); i++) {
339a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3403deb3ec6SMatthias Ringwald     }
341a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
342ae46d666SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_generic_status_indicators[i].uuid);
3433deb3ec6SMatthias Ringwald     }
3443deb3ec6SMatthias Ringwald     return offset;
3453deb3ec6SMatthias Ringwald }
3463deb3ec6SMatthias Ringwald 
3473deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
348c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_generic_status_indicators_nr * 3)) return 0;
3493deb3ec6SMatthias Ringwald     int i;
3503deb3ec6SMatthias Ringwald     int offset = 0;
351a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
352a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "\r\n%s:%d,%d\r\n", HFP_GENERIC_STATUS_INDICATOR, hfp_generic_status_indicators[i].uuid, hfp_generic_status_indicators[i].state);
3533deb3ec6SMatthias Ringwald     }
3543deb3ec6SMatthias Ringwald     return offset;
3553deb3ec6SMatthias Ringwald }
3563deb3ec6SMatthias Ringwald 
3573deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
358c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_indicators_nr * 3)) return 0;
3593deb3ec6SMatthias Ringwald     int i;
3603deb3ec6SMatthias Ringwald     int offset = 0;
361c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_ag_indicators_nr-1); i++) {
3623deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3633deb3ec6SMatthias Ringwald     }
3643deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3653deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3663deb3ec6SMatthias Ringwald     }
3673deb3ec6SMatthias Ringwald     return offset;
3683deb3ec6SMatthias Ringwald }
3693deb3ec6SMatthias Ringwald 
3703deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
371c1ab6cc1SMatthias Ringwald     if (buffer_size < (hfp_ag_call_hold_services_nr * 3)) return 0;
3723deb3ec6SMatthias Ringwald     int i;
3733deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
374c1ab6cc1SMatthias Ringwald     for (i = 0; i < (hfp_ag_call_hold_services_nr-1); i++) {
3753deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3763deb3ec6SMatthias Ringwald     }
3773deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3783deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3793deb3ec6SMatthias Ringwald     }
3803deb3ec6SMatthias Ringwald     return offset;
3813deb3ec6SMatthias Ringwald }
3823deb3ec6SMatthias Ringwald 
383485ac19eSMilanka Ringwald static int hfp_ag_send_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
384ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
385a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
3861167ff83SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer, uint16_t buffer_size)){
3873deb3ec6SMatthias Ringwald 
388ad902e3dSMatthias Ringwald     // assumes: can send now == true
389ad902e3dSMatthias Ringwald     // assumes: num segments > 0
390aba39421SMatthias Ringwald     // assumes: individual segments are smaller than MTU
391ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
392ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
393ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
394ad902e3dSMatthias Ringwald     int offset = 0;
395ad902e3dSMatthias Ringwald     int segment = start_segment;
396ad902e3dSMatthias Ringwald     while (segment < num_segments){
397a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
3981167ff83SMatthias Ringwald         if ((offset + segment_len + 1) > mtu) break;
3991167ff83SMatthias Ringwald         // append segment. As it appends a '\0', we provide a buffer one byte larger
4001167ff83SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset, segment_len + 1);
401ad902e3dSMatthias Ringwald         offset += segment_len;
402ad902e3dSMatthias Ringwald         segment++;
403ad902e3dSMatthias Ringwald     }
404ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
405f20dd2aeSBjoern Hartmann #ifdef ENABLE_HFP_AT_MESSAGES
406f20dd2aeSBjoern Hartmann     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_SENT, (char *) data);
407f20dd2aeSBjoern Hartmann #endif
408ad902e3dSMatthias Ringwald     return segment;
409ad902e3dSMatthias Ringwald }
4103deb3ec6SMatthias Ringwald 
411ad902e3dSMatthias Ringwald // returns next segment to store
412485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
413a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
414485ac19eSMilanka Ringwald     return hfp_ag_send_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
415894d499cSMatthias Ringwald                                          hfp_ag_indicators_cmd_generator_get_segment_len,
416894d499cSMatthias Ringwald                                          hfp_ag_indicators_cmd_generator_store_segment);
4173deb3ec6SMatthias Ringwald }
4183deb3ec6SMatthias Ringwald 
419485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_indicators_status_cmd(uint16_t cid){
4203deb3ec6SMatthias Ringwald     char buffer[40];
42189425bfcSMilanka Ringwald     const int size = sizeof(buffer);
42289425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_INDICATOR);
42389425bfcSMilanka Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, size-offset-9);
42489425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4253deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4263deb3ec6SMatthias Ringwald }
4273deb3ec6SMatthias Ringwald 
428485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_can_hold_call_cmd(uint16_t cid){
429ad902e3dSMatthias Ringwald     char buffer[40];
43089425bfcSMilanka Ringwald     const int size = sizeof(buffer);
43189425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
43289425bfcSMilanka Ringwald     offset += hfp_ag_call_services_join(buffer+offset, size-offset-9);
43389425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n\r\nOK\r\n");
4343deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4353deb3ec6SMatthias Ringwald }
4363deb3ec6SMatthias Ringwald 
4373deb3ec6SMatthias Ringwald 
438485ac19eSMilanka Ringwald static int hfp_ag_send_list_supported_generic_status_indicators_cmd(uint16_t cid){
439485ac19eSMilanka Ringwald     return hfp_ag_send_ok(cid);
4403deb3ec6SMatthias Ringwald }
4413deb3ec6SMatthias Ringwald 
442485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4433deb3ec6SMatthias Ringwald     char buffer[40];
44489425bfcSMilanka Ringwald     const int size = sizeof(buffer);
44589425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
44689425bfcSMilanka Ringwald     offset += hfp_hf_indicators_join(buffer+offset, size-offset-10);
44789425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, ")\r\n\r\nOK\r\n");
4483deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4493deb3ec6SMatthias Ringwald }
4503deb3ec6SMatthias Ringwald 
451485ac19eSMilanka Ringwald static int hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4523deb3ec6SMatthias Ringwald     char buffer[40];
4531cc1d9e9SMilanka Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer) - 7);
4541cc1d9e9SMilanka Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4553deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4563deb3ec6SMatthias Ringwald }
4573deb3ec6SMatthias Ringwald 
458485ac19eSMilanka Ringwald static int hfp_ag_send_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4593deb3ec6SMatthias Ringwald     char buffer[20];
460ff7d6aeaSMatthias Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d,%d\r\n",
461ff7d6aeaSMatthias Ringwald              HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index,
462ff7d6aeaSMatthias Ringwald              indicator->status);
463ff7d6aeaSMatthias Ringwald     buffer[sizeof(buffer) - 1] = 0;
4643deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4653deb3ec6SMatthias Ringwald }
4663deb3ec6SMatthias Ringwald 
467485ac19eSMilanka Ringwald static int hfp_ag_send_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4685e71456cSMilanka Ringwald     char buffer[41];
4693deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
47089425bfcSMilanka Ringwald         snprintf(buffer, sizeof(buffer), "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4713deb3ec6SMatthias Ringwald     } else {
47289425bfcSMilanka Ringwald         int offset = snprintf(buffer,  sizeof(buffer), "\r\n%s:%d,%d,", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format);
4735e71456cSMilanka Ringwald         offset += snprintf(buffer+offset, 16, "%s", op.name);
47489425bfcSMilanka Ringwald         snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4753deb3ec6SMatthias Ringwald     }
4763deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4773deb3ec6SMatthias Ringwald }
4783deb3ec6SMatthias Ringwald 
479a04aecb1SMilanka Ringwald static inline int hfp_ag_send_cmd_with_space_and_int(uint16_t cid, const char * cmd, uint8_t value){
480a04aecb1SMilanka Ringwald     char buffer[30];
481a04aecb1SMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s: %d\r\n", cmd, value);
482a04aecb1SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
483a04aecb1SMilanka Ringwald }
484a04aecb1SMilanka Ringwald 
485a04aecb1SMilanka Ringwald 
4861cc1d9e9SMilanka Ringwald static inline int hfp_ag_send_cmd_with_int(uint16_t cid, const char * cmd, uint8_t value){
4871cc1d9e9SMilanka Ringwald     char buffer[30];
48889425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "\r\n%s:%d\r\n", cmd, value);
4891cc1d9e9SMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
4901cc1d9e9SMilanka Ringwald }
4913deb3ec6SMatthias Ringwald 
492485ac19eSMilanka Ringwald static int hfp_ag_send_suggest_codec_cmd(uint16_t cid, uint8_t codec){
4931cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
4943deb3ec6SMatthias Ringwald }
4953deb3ec6SMatthias Ringwald 
496485ac19eSMilanka Ringwald static int hfp_ag_send_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
497a04aecb1SMilanka Ringwald     return hfp_ag_send_cmd_with_space_and_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
498aa4dd815SMatthias Ringwald }
499aa4dd815SMatthias Ringwald 
500485ac19eSMilanka Ringwald static int hfp_ag_send_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
5011cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
502aa4dd815SMatthias Ringwald }
503aa4dd815SMatthias Ringwald 
504485ac19eSMilanka Ringwald static int hfp_ag_send_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
5051cc1d9e9SMilanka Ringwald     return hfp_ag_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
506aa4dd815SMatthias Ringwald }
507aa4dd815SMatthias Ringwald 
508485ac19eSMilanka Ringwald static int hfp_ag_send_set_response_and_hold(uint16_t cid, int state){
50966191aa0SMilanka Ringwald     return hfp_ag_send_cmd_with_space_and_int(cid, HFP_RESPONSE_AND_HOLD, state);
510ce263fc8SMatthias Ringwald }
511ce263fc8SMatthias Ringwald 
512a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
513bc1b1537SMilanka Ringwald     if (hfp_connection->sco_for_msbc_failed) return HFP_CODEC_CVSD;
514bc1b1537SMilanka Ringwald 
5156a7f44bdSMilanka Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){
5166a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
517df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
5183deb3ec6SMatthias Ringwald         }
5193deb3ec6SMatthias Ringwald     }
520df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
5213deb3ec6SMatthias Ringwald }
5223deb3ec6SMatthias Ringwald 
52376cc1527SMatthias Ringwald /* state machines */
52476cc1527SMatthias Ringwald 
525afac2a04SMilanka Ringwald static uint8_t hfp_ag_esco_s4_supported(hfp_connection_t * hfp_connection){
526afac2a04SMilanka Ringwald     return (hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4)) &&  (hfp_supported_features  & (1<<HFP_AGSF_ESCO_S4));
5277522e673SMatthias Ringwald }
5287522e673SMatthias Ringwald 
529a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
530aa4dd815SMatthias Ringwald     /* events ( == commands):
531aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
532aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
533aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
534aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
535aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
536aa4dd815SMatthias Ringwald     */
537a0ffb263SMatthias Ringwald     switch (hfp_connection->codecs_state){
5383e3b9207SMilanka Ringwald         case HFP_CODECS_EXCHANGED:
5393e3b9207SMilanka Ringwald             if (hfp_connection->command == HFP_CMD_AVAILABLE_CODECS){
5403e3b9207SMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
5413e3b9207SMilanka Ringwald                 return 1;
5423e3b9207SMilanka Ringwald             }
5433e3b9207SMilanka Ringwald             break;
5443e3b9207SMilanka Ringwald 
545aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
546a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
547aa4dd815SMatthias Ringwald             break;
548aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
549a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
550aa4dd815SMatthias Ringwald             break;
551aa4dd815SMatthias Ringwald         default:
552aa4dd815SMatthias Ringwald             break;
553aa4dd815SMatthias Ringwald     }
554aa4dd815SMatthias Ringwald 
555a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
556aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
557a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
558a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
559485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
560aa4dd815SMatthias Ringwald                 return 1;
561aa4dd815SMatthias Ringwald             }
562aa4dd815SMatthias Ringwald 
563a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
564aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
565a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
566aa4dd815SMatthias Ringwald                     break;
567aa4dd815SMatthias Ringwald                 default:
568aa4dd815SMatthias Ringwald                     break;
569aa4dd815SMatthias Ringwald             }
570485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
571aa4dd815SMatthias Ringwald             return 1;
572aa4dd815SMatthias Ringwald 
573aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
574a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
57540e8d6c5SMatthias Ringwald             hfp_connection->establish_audio_connection = 1;
576485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
577aa4dd815SMatthias Ringwald             return 1;
578aa4dd815SMatthias Ringwald 
579aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
580a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
581a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
582485ac19eSMilanka Ringwald             hfp_ag_send_suggest_codec_cmd(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
583aa4dd815SMatthias Ringwald             return 1;
584aa4dd815SMatthias Ringwald 
585aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
586a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
587a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
588485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
589aa4dd815SMatthias Ringwald                 return 1;
590aa4dd815SMatthias Ringwald             }
591a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
592a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
593c1ab6cc1SMatthias Ringwald             log_info("hfp: codec confirmed: %s", (hfp_connection->negotiated_codec == HFP_CODEC_MSBC) ? "mSBC" : "CVSD");
594485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
5957522e673SMatthias Ringwald             // now, pick link settings
596afac2a04SMilanka Ringwald             hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
5973721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
5983721a235SMatthias Ringwald             hfp_cc256x_prepare_for_sco(hfp_connection);
5993721a235SMatthias Ringwald #endif
600aa4dd815SMatthias Ringwald             return 1;
601aa4dd815SMatthias Ringwald         default:
602aa4dd815SMatthias Ringwald             break;
603aa4dd815SMatthias Ringwald     }
604aa4dd815SMatthias Ringwald     return 0;
605aa4dd815SMatthias Ringwald }
606aa4dd815SMatthias Ringwald 
607a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
608a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
609ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
610aa4dd815SMatthias Ringwald 
611a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
612d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
613a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
614aa4dd815SMatthias Ringwald     }
615ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
616c1ab6cc1SMatthias Ringwald     if ((hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS) &&
617505f1c30SMatthias Ringwald         (hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS)){
618a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
619ce263fc8SMatthias Ringwald     }
620aa4dd815SMatthias Ringwald }
6213deb3ec6SMatthias Ringwald 
622a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
623c79b4cabSMatthias Ringwald     // log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
624a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
625c04cf7ffSMilanka Ringwald     int sent = 0;
626a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
6273deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
628a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
6293deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
630aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
631d715cf51SMatthias Ringwald                     hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
632a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
633a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
634aa4dd815SMatthias Ringwald                     } else {
635a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
636aa4dd815SMatthias Ringwald                     }
637a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
638aa4dd815SMatthias Ringwald                     return 1;
6393deb3ec6SMatthias Ringwald                 default:
6403deb3ec6SMatthias Ringwald                     break;
6413deb3ec6SMatthias Ringwald             }
6423deb3ec6SMatthias Ringwald             break;
6433deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
644c04cf7ffSMilanka Ringwald             sent = codecs_exchange_state_machine(hfp_connection);
645a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
646a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6473deb3ec6SMatthias Ringwald             }
648c04cf7ffSMilanka Ringwald             return sent;
649aa4dd815SMatthias Ringwald 
650aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
651a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
652473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
65384a0c24eSMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
654473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
655473ac565SMatthias Ringwald                 // continue below in state switch
656ad902e3dSMatthias Ringwald             }
657ad902e3dSMatthias Ringwald             break;
658aa4dd815SMatthias Ringwald 
659aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
660a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
661a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
662485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
663aa4dd815SMatthias Ringwald             return 1;
664aa4dd815SMatthias Ringwald 
6653deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
666a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
667a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
668a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
669a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
670a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
671aa4dd815SMatthias Ringwald             } else {
672a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
6733deb3ec6SMatthias Ringwald             }
6748a46ec40SMatthias Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
675aa4dd815SMatthias Ringwald             return 1;
6763deb3ec6SMatthias Ringwald 
677aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
678a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
679a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
680a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
681aa4dd815SMatthias Ringwald             }
682485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
683a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
684a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
685ce263fc8SMatthias Ringwald             }
686aa4dd815SMatthias Ringwald             return 1;
687aa4dd815SMatthias Ringwald 
688aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
689a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
690a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
691485ac19eSMilanka Ringwald             hfp_ag_send_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
692aa4dd815SMatthias Ringwald             return 1;
693aa4dd815SMatthias Ringwald 
694aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
695a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
696a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
697485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
698aa4dd815SMatthias Ringwald             return 1;
699aa4dd815SMatthias Ringwald 
700aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
701a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
702a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
703485ac19eSMilanka Ringwald             hfp_ag_send_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
704aa4dd815SMatthias Ringwald             return 1;
7053deb3ec6SMatthias Ringwald         default:
7063deb3ec6SMatthias Ringwald             break;
7073deb3ec6SMatthias Ringwald     }
708473ac565SMatthias Ringwald 
709473ac565SMatthias Ringwald     switch (hfp_connection->state){
710473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
711485ac19eSMilanka Ringwald             int next_segment = hfp_ag_send_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
712473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
713473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
714473ac565SMatthias Ringwald             if (next_segment < num_segments){
715473ac565SMatthias Ringwald                 // prepare sending of next segment
716473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
717473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
718473ac565SMatthias Ringwald             } else {
719473ac565SMatthias Ringwald                 // done, go to next state
720473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
721473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
722473ac565SMatthias Ringwald             }
723473ac565SMatthias Ringwald             return 1;
724473ac565SMatthias Ringwald         }
725473ac565SMatthias Ringwald         default:
726473ac565SMatthias Ringwald             break;
727473ac565SMatthias Ringwald     }
728c04cf7ffSMilanka Ringwald     return 0;
7293deb3ec6SMatthias Ringwald }
7303deb3ec6SMatthias Ringwald 
731a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
732c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
733c04cf7ffSMilanka Ringwald     if (sent) return 1;
734aa4dd815SMatthias Ringwald 
735a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
736aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
737485ac19eSMilanka Ringwald             hfp_ag_send_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
73805ce4bb6SMilanka Ringwald             if (hfp_connection->ag_activate_voice_recognition > 0){
73905ce4bb6SMilanka Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
74005ce4bb6SMilanka Ringwald             } else {
74105ce4bb6SMilanka Ringwald                 hfp_release_audio_connection(hfp_connection);
74205ce4bb6SMilanka Ringwald             }
743aa4dd815SMatthias Ringwald             return 1;
744aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
745aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
746485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
747aa4dd815SMatthias Ringwald             } else {
748485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
749aa4dd815SMatthias Ringwald             }
750aa4dd815SMatthias Ringwald             return 1;
751aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
752485ac19eSMilanka Ringwald             hfp_ag_send_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
753aa4dd815SMatthias Ringwald             return 1;
754aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
755485ac19eSMilanka Ringwald             hfp_ag_send_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
756aa4dd815SMatthias Ringwald             return 1;
757aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
758a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
759485ac19eSMilanka Ringwald                 hfp_ag_send_error(hfp_connection->rfcomm_cid);
760aa4dd815SMatthias Ringwald             } else {
761485ac19eSMilanka Ringwald                 hfp_ag_send_ok(hfp_connection->rfcomm_cid);
7623deb3ec6SMatthias Ringwald             }
763aa4dd815SMatthias Ringwald             return 1;
764aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
765485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
766aa4dd815SMatthias Ringwald             return 1;
7673deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
768a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
769a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
770485ac19eSMilanka Ringwald                 hfp_ag_send_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
771aa4dd815SMatthias Ringwald                 return 1;
7723deb3ec6SMatthias Ringwald             }
773202c8a4cSMatthias Ringwald             break;
7743deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
775485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
776ce263fc8SMatthias Ringwald             return 1;
7773deb3ec6SMatthias Ringwald         default:
7783deb3ec6SMatthias Ringwald             break;
7793deb3ec6SMatthias Ringwald     }
780aa4dd815SMatthias Ringwald     return 0;
7813deb3ec6SMatthias Ringwald }
7823deb3ec6SMatthias Ringwald 
783a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
784505f1c30SMatthias Ringwald     if ((hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) ||
785505f1c30SMatthias Ringwald         (hfp_connection->state > HFP_W2_DISCONNECT_SCO)) return 0;
7863deb3ec6SMatthias Ringwald 
787a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
788aa4dd815SMatthias Ringwald 
78929cddf58SMatthias Ringwald     // accept incoming audio connection (codec negotiation is not used)
79029cddf58SMatthias Ringwald     if (hfp_connection->accept_sco){
79129cddf58SMatthias Ringwald         // notify about codec selection if not done already
79229cddf58SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
79329cddf58SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
79429cddf58SMatthias Ringwald         }
79529cddf58SMatthias Ringwald         //
79629cddf58SMatthias Ringwald         bool incoming_eSCO = hfp_connection->accept_sco == 2;
79729cddf58SMatthias Ringwald         hfp_connection->accept_sco = 0;
79829cddf58SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
79929cddf58SMatthias Ringwald         hfp_accept_synchronous_connection(hfp_connection, incoming_eSCO);
80029cddf58SMatthias Ringwald         return 1;
80129cddf58SMatthias Ringwald     }
80229cddf58SMatthias Ringwald 
803aa4dd815SMatthias Ringwald     // run codecs exchange
804c04cf7ffSMilanka Ringwald     int sent = codecs_exchange_state_machine(hfp_connection);
805c04cf7ffSMilanka Ringwald     if (sent) return 1;
806aa4dd815SMatthias Ringwald 
807c04cf7ffSMilanka Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return 0;
808a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
809a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
810a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
811eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
812aa4dd815SMatthias Ringwald         return 1;
813aa4dd815SMatthias Ringwald     }
814aa4dd815SMatthias Ringwald     return 0;
815aa4dd815SMatthias Ringwald }
816aa4dd815SMatthias Ringwald 
817ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
818665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
819665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
820aa4dd815SMatthias Ringwald 
821665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
822a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
823a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
824a0ffb263SMatthias Ringwald             return hfp_connection;
825aa4dd815SMatthias Ringwald         }
826aa4dd815SMatthias Ringwald     }
827aa4dd815SMatthias Ringwald     return NULL;
828aa4dd815SMatthias Ringwald }
829aa4dd815SMatthias Ringwald 
830ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
831d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
832d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
833aa4dd815SMatthias Ringwald 
834d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
835a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
836a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
837aa4dd815SMatthias Ringwald 
838a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
839a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
840aa4dd815SMatthias Ringwald 
841f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
842aa4dd815SMatthias Ringwald }
843aa4dd815SMatthias Ringwald 
844a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
845a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
846a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
847a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
848a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
849aa4dd815SMatthias Ringwald }
850aa4dd815SMatthias Ringwald 
851a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
852a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
853a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
854aa4dd815SMatthias Ringwald }
855aa4dd815SMatthias Ringwald 
856056efd45SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
857056efd45SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
858056efd45SMatthias Ringwald     if (!indicator){
859056efd45SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
860056efd45SMatthias Ringwald         return;
861056efd45SMatthias Ringwald     };
862056efd45SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
863056efd45SMatthias Ringwald }
864056efd45SMatthias Ringwald 
865056efd45SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
866056efd45SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
867056efd45SMatthias Ringwald     if (!indicator){
868056efd45SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
869056efd45SMatthias Ringwald         return;
870056efd45SMatthias Ringwald     };
871056efd45SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
872056efd45SMatthias Ringwald }
873056efd45SMatthias Ringwald 
874aa4dd815SMatthias Ringwald //
875aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
876aa4dd815SMatthias Ringwald //
877aa4dd815SMatthias Ringwald 
878a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
879aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
880a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
881d97d752dSMilanka Ringwald         hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
882aa4dd815SMatthias Ringwald     } else {
883a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
884a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
885a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
886a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
887ca59be51SMatthias Ringwald         hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
888aa4dd815SMatthias Ringwald     }
889aa4dd815SMatthias Ringwald }
890aa4dd815SMatthias Ringwald 
891a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
892a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
893a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
894a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
895ca59be51SMatthias Ringwald     hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_STOP_RINGINIG);
896aa4dd815SMatthias Ringwald }
897aa4dd815SMatthias Ringwald 
898aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
899aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
900aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
901aa4dd815SMatthias Ringwald 
902665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
903665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
904665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
905a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
90666c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
907a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
908a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
909a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
910d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
911aa4dd815SMatthias Ringwald         }
912a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
913a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
914aa4dd815SMatthias Ringwald         }
915f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
916aa4dd815SMatthias Ringwald     }
917aa4dd815SMatthias Ringwald }
918aa4dd815SMatthias Ringwald 
919fe899794SMatthias Ringwald static void hfp_ag_trigger_outgoing_call(void){
920fe899794SMatthias Ringwald     btstack_linked_list_iterator_t it;
921fe899794SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
922fe899794SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
923fe899794SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
924fe899794SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
925fe899794SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
926fe899794SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
927fe899794SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
928fe899794SMatthias Ringwald         }
929fe899794SMatthias Ringwald     }
930fe899794SMatthias Ringwald }
9315a7033e2SMatthias Ringwald 
9325a7033e2SMatthias Ringwald static void hfp_ag_handle_outgoing_call_accepted(hfp_connection_t * hfp_connection){
9335a7033e2SMatthias Ringwald     hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
9345a7033e2SMatthias Ringwald 
9355a7033e2SMatthias Ringwald     // trigger callsetup to be
9365a7033e2SMatthias Ringwald     int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
9375a7033e2SMatthias Ringwald     hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ACCEPTED, 0, 0, NULL);
9385a7033e2SMatthias Ringwald 
9395a7033e2SMatthias Ringwald     int indicator_index ;
9405a7033e2SMatthias Ringwald 
9415a7033e2SMatthias Ringwald     hfp_ag_set_callsetup_indicator();
9425a7033e2SMatthias Ringwald     indicator_index = get_ag_indicator_index_for_name("callsetup");
9435a7033e2SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
9445a7033e2SMatthias Ringwald 
9455a7033e2SMatthias Ringwald     // put current call on hold if active
9465a7033e2SMatthias Ringwald     if (put_call_on_hold){
9475a7033e2SMatthias Ringwald         log_info("AG putting current call on hold for new outgoing call");
9485a7033e2SMatthias Ringwald         hfp_ag_set_callheld_indicator();
9495a7033e2SMatthias Ringwald         indicator_index = get_ag_indicator_index_for_name("callheld");
9505a7033e2SMatthias Ringwald         hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
9515a7033e2SMatthias Ringwald     }
9525a7033e2SMatthias Ringwald 
9535a7033e2SMatthias Ringwald     // start audio if needed
9545a7033e2SMatthias Ringwald     hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
9555a7033e2SMatthias Ringwald }
956fe899794SMatthias Ringwald 
9574a2e058fSMatthias Ringwald static void hfp_ag_transfer_indicator(const char * name){
9584a2e058fSMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
959aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
960aa4dd815SMatthias Ringwald 
961665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
962665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
963665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
964a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
96566c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
966a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
967a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
968f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
969aa4dd815SMatthias Ringwald     }
970aa4dd815SMatthias Ringwald }
971aa4dd815SMatthias Ringwald 
9724a2e058fSMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
9734a2e058fSMatthias Ringwald     hfp_ag_transfer_indicator("callsetup");
9744a2e058fSMatthias Ringwald }
9754a2e058fSMatthias Ringwald 
976aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
9774a2e058fSMatthias Ringwald     hfp_ag_transfer_indicator("call");
978aa4dd815SMatthias Ringwald }
979aa4dd815SMatthias Ringwald 
980aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
9814a2e058fSMatthias Ringwald     hfp_ag_transfer_indicator("callheld");
982aa4dd815SMatthias Ringwald }
983aa4dd815SMatthias Ringwald 
984aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
985aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
986aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
987aa4dd815SMatthias Ringwald 
988665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
989665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
990665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
991a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
99266c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
993505f1c30SMatthias Ringwald         if ((hfp_connection->call_state != HFP_CALL_RINGING) &&
994505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
995aa4dd815SMatthias Ringwald 
996a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
997a0ffb263SMatthias Ringwald         if (hfp_connection == source){
998aa4dd815SMatthias Ringwald 
999aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
1000a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
1001aa4dd815SMatthias Ringwald             } else {
1002a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
1003d97d752dSMilanka Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->acl_handle);
1004aa4dd815SMatthias Ringwald             }
1005aa4dd815SMatthias Ringwald 
1006a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1007a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1008aa4dd815SMatthias Ringwald 
1009aa4dd815SMatthias Ringwald         } else {
1010a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1011aa4dd815SMatthias Ringwald         }
1012f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1013aa4dd815SMatthias Ringwald     }
1014aa4dd815SMatthias Ringwald }
1015aa4dd815SMatthias Ringwald 
1016aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
1017aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1018aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1019aa4dd815SMatthias Ringwald 
1020665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1021665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1022665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1023a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
102466c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1025a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
1026aa4dd815SMatthias Ringwald 
1027a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1028a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
1029aa4dd815SMatthias Ringwald 
1030a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1031a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1032aa4dd815SMatthias Ringwald 
1033f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1034aa4dd815SMatthias Ringwald         break;  // only single
1035aa4dd815SMatthias Ringwald     }
1036aa4dd815SMatthias Ringwald }
1037aa4dd815SMatthias Ringwald 
1038aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
1039aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1040665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1041665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1042665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1043665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
104466c5995fSMatthias Ringwald         if (connection->local_role != HFP_ROLE_AG) continue;
1045505f1c30SMatthias Ringwald         if ((connection->call_state != HFP_CALL_RINGING) &&
1046505f1c30SMatthias Ringwald             (connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1047aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
1048aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1049aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
1050f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(connection);
1051aa4dd815SMatthias Ringwald     }
1052aa4dd815SMatthias Ringwald }
1053aa4dd815SMatthias Ringwald 
1054aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1055aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1056aa4dd815SMatthias Ringwald 
1057665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1058665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1059665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1060a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
106166c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1062d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1063a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1064a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1065a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
10666d78145cSMatthias Ringwald         if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED){
1067a0ffb263SMatthias Ringwald             hfp_connection->release_audio_connection = 1;
10686d78145cSMatthias Ringwald         }
1069f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
1070aa4dd815SMatthias Ringwald     }
10717d81706fSMatthias Ringwald     hfp_emit_simple_event(NULL, HFP_SUBEVENT_CALL_TERMINATED);
1072aa4dd815SMatthias Ringwald }
1073aa4dd815SMatthias Ringwald 
10740cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1075aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1076aa4dd815SMatthias Ringwald     if (!indicator){
1077aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
107845718b6fSMatthias Ringwald         return;
1079aa4dd815SMatthias Ringwald     };
1080d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1081aa4dd815SMatthias Ringwald }
1082aa4dd815SMatthias Ringwald 
1083aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1084665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1085665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1086665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1087a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
108866c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1089505f1c30SMatthias Ringwald         if ((hfp_connection->call_state != HFP_CALL_RINGING) &&
1090505f1c30SMatthias Ringwald             (hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING)) continue;
1091a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1092aa4dd815SMatthias Ringwald     }
1093aa4dd815SMatthias Ringwald }
1094aa4dd815SMatthias Ringwald 
1095aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1096665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1097665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1098665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1099a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
110066c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1101a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1102aa4dd815SMatthias Ringwald     }
1103aa4dd815SMatthias Ringwald     return NULL;
1104aa4dd815SMatthias Ringwald }
1105aa4dd815SMatthias Ringwald 
1106a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1107665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1108665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1109665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1110a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
111166c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
1112a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1113ce263fc8SMatthias Ringwald     }
1114ce263fc8SMatthias Ringwald }
1115ce263fc8SMatthias Ringwald 
1116a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1117aa4dd815SMatthias Ringwald     int indicator_index;
1118a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1119aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1120a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1121a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1122a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1123a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1124a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1125a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1126ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_START_RINGINIG);
1127aa4dd815SMatthias Ringwald             break;
1128aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1129a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1130a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1131a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1132aa4dd815SMatthias Ringwald             break;
1133aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1134a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1135a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1136aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1137a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1138aa4dd815SMatthias Ringwald             break;
1139aa4dd815SMatthias Ringwald         default:
1140aa4dd815SMatthias Ringwald             break;
1141aa4dd815SMatthias Ringwald     }
1142aa4dd815SMatthias Ringwald     return 0;
1143aa4dd815SMatthias Ringwald }
114439426febSMatthias Ringwald 
114539426febSMatthias Ringwald // functions extracted from hfp_ag_call_sm below
11469f9621cfSMatthias Ringwald static void hfp_ag_handle_reject_outgoing_call(void){
114739426febSMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
114839426febSMatthias Ringwald     if (!hfp_connection){
114939426febSMatthias Ringwald         log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
115039426febSMatthias Ringwald         return;
115139426febSMatthias Ringwald     }
115239426febSMatthias Ringwald 
1153f8737b81SMatthias Ringwald     hfp_gsm_handler(HFP_AG_OUTGOING_CALL_REJECTED, 0, 0, NULL);
115439426febSMatthias Ringwald     hfp_connection->call_state = HFP_CALL_IDLE;
115539426febSMatthias Ringwald     hfp_connection->send_error = 1;
115639426febSMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
115739426febSMatthias Ringwald }
115839426febSMatthias Ringwald 
1159a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1160a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1161d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1162d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1163d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
116474386ee0SMatthias Ringwald 
1165aa4dd815SMatthias Ringwald     switch (event){
1166aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1167d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1168aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1169d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
117012cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1171f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL);
1172d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1173aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
117460ebb071SMilanka Ringwald                             log_info("AG rings");
1175aa4dd815SMatthias Ringwald                             break;
1176aa4dd815SMatthias Ringwald                         default:
11773deb3ec6SMatthias Ringwald                             break;
11783deb3ec6SMatthias Ringwald                     }
11793deb3ec6SMatthias Ringwald                     break;
1180aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1181d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
118212cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1183f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL, 0, 0, NULL);
1184d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1185aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
118660ebb071SMilanka Ringwald                             log_info("AG call waiting");
1187aa4dd815SMatthias Ringwald                             break;
1188aa4dd815SMatthias Ringwald                         default:
11893deb3ec6SMatthias Ringwald                             break;
11903deb3ec6SMatthias Ringwald                     }
11913deb3ec6SMatthias Ringwald                     break;
11927bbeb3adSMilanka Ringwald                 default:
11937bbeb3adSMilanka Ringwald                     break;
1194aa4dd815SMatthias Ringwald             }
1195aa4dd815SMatthias Ringwald             break;
1196aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1197d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1198aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1199d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
120012cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1201f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL);
1202d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1203d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1204aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
120560ebb071SMilanka Ringwald                             log_info("AG answers call, accept call by GSM");
1206aa4dd815SMatthias Ringwald                             break;
1207aa4dd815SMatthias Ringwald                         default:
12083deb3ec6SMatthias Ringwald                             break;
12093deb3ec6SMatthias Ringwald                     }
1210aa4dd815SMatthias Ringwald                     break;
1211aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1212d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1213aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
121460ebb071SMilanka Ringwald                             log_info("AG: current call is placed on hold, incoming call gets active");
1215f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, 0, 0, NULL);
1216d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1217d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1218ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1219ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1220aa4dd815SMatthias Ringwald                             break;
1221aa4dd815SMatthias Ringwald                         default:
1222aa4dd815SMatthias Ringwald                             break;
1223aa4dd815SMatthias Ringwald                     }
1224aa4dd815SMatthias Ringwald                     break;
12257bbeb3adSMilanka Ringwald                 default:
12267bbeb3adSMilanka Ringwald                     break;
1227aa4dd815SMatthias Ringwald             }
1228aa4dd815SMatthias Ringwald             break;
1229ce263fc8SMatthias Ringwald 
1230ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1231d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1232ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1233d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1234ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
123560ebb071SMilanka Ringwald                             log_info("AG: joining held call with active call");
1236f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_HELD_CALL_JOINED_BY_AG, 0, 0, NULL);
1237d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1238ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1239ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1240ce263fc8SMatthias Ringwald                             break;
1241ce263fc8SMatthias Ringwald                         default:
1242ce263fc8SMatthias Ringwald                             break;
1243ce263fc8SMatthias Ringwald                     }
1244ce263fc8SMatthias Ringwald                     break;
1245ce263fc8SMatthias Ringwald                 default:
1246ce263fc8SMatthias Ringwald                     break;
1247ce263fc8SMatthias Ringwald             }
1248ce263fc8SMatthias Ringwald             break;
1249ce263fc8SMatthias Ringwald 
1250aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1251d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1252aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1253d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
125412cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1255f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, 0, 0, NULL);
1256d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1257d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1258a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
12596323f788SMatthias Ringwald                             hfp_connection->ok_pending = 1;
126060ebb071SMilanka Ringwald                             log_info("HF answers call, accept call by GSM");
1261ca59be51SMatthias Ringwald                             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_ANSWERED);
1262aa4dd815SMatthias Ringwald                             break;
1263aa4dd815SMatthias Ringwald                         default:
1264aa4dd815SMatthias Ringwald                             break;
1265aa4dd815SMatthias Ringwald                     }
12663deb3ec6SMatthias Ringwald                     break;
12673deb3ec6SMatthias Ringwald                 default:
12683deb3ec6SMatthias Ringwald                     break;
12693deb3ec6SMatthias Ringwald             }
12703deb3ec6SMatthias Ringwald             break;
12713deb3ec6SMatthias Ringwald 
1272ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1273d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1274ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1275d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
127612cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1277f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, 0, 0, NULL);
1278ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1279ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1280a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1281ce263fc8SMatthias Ringwald                             // as with regualr call
1282d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1283d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1284ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
128560ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by AG");
1286ce263fc8SMatthias Ringwald                             break;
1287ce263fc8SMatthias Ringwald                         default:
1288ce263fc8SMatthias Ringwald                             break;
1289ce263fc8SMatthias Ringwald                     }
1290ce263fc8SMatthias Ringwald                     break;
1291ce263fc8SMatthias Ringwald                 default:
1292ce263fc8SMatthias Ringwald                     break;
1293ce263fc8SMatthias Ringwald             }
1294ce263fc8SMatthias Ringwald             break;
1295ce263fc8SMatthias Ringwald 
1296ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1297d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1298ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1299d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
130012cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1301f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, 0, 0, NULL);
1302ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1303ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1304a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1305ce263fc8SMatthias Ringwald                             // as with regualr call
1306d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1307d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1308a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
130960ebb071SMilanka Ringwald                             log_info("AG response and hold - hold by HF");
1310ce263fc8SMatthias Ringwald                             break;
1311ce263fc8SMatthias Ringwald                         default:
1312ce263fc8SMatthias Ringwald                             break;
1313ce263fc8SMatthias Ringwald                     }
1314ce263fc8SMatthias Ringwald                     break;
1315ce263fc8SMatthias Ringwald                 default:
1316ce263fc8SMatthias Ringwald                     break;
1317ce263fc8SMatthias Ringwald             }
1318ce263fc8SMatthias Ringwald             break;
1319ce263fc8SMatthias Ringwald 
1320ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1321ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1322ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1323ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1324f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, 0, 0, NULL);
1325a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1326ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1327a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
132860ebb071SMilanka Ringwald             log_info("Held Call accepted and active");
1329ce263fc8SMatthias Ringwald             break;
1330ce263fc8SMatthias Ringwald 
1331ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1332ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1333ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1334ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1335f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, 0, 0, NULL);
1336a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1337ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1338a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1339ce263fc8SMatthias Ringwald             // from terminate by ag
1340d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1341ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1342ce263fc8SMatthias Ringwald             break;
1343ce263fc8SMatthias Ringwald 
1344aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1345d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1346aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1347d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
134812cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1349f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
1350d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1351aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1352aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
135360ebb071SMilanka Ringwald                             log_info("HF Rejected Incoming call, AG terminate call");
1354aa4dd815SMatthias Ringwald                             break;
1355aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
135612cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1357f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
1358d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1359aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
136060ebb071SMilanka Ringwald                             log_info("AG terminate outgoing call process");
1361202c8a4cSMatthias Ringwald                             break;
1362aa4dd815SMatthias Ringwald                         default:
1363aa4dd815SMatthias Ringwald                             break;
1364aa4dd815SMatthias Ringwald                     }
1365aa4dd815SMatthias Ringwald                     break;
136612cbbeeeSMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1367f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_HF, 0, 0, NULL);
13681ec112deSMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1369d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
13701ec112deSMatthias Ringwald                     hfp_ag_trigger_terminate_call();
137160ebb071SMilanka Ringwald                     log_info("AG terminate call");
1372aa4dd815SMatthias Ringwald                     break;
13737bbeb3adSMilanka Ringwald                 default:
13747bbeb3adSMilanka Ringwald                     break;
1375aa4dd815SMatthias Ringwald             }
13760c1b8529SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CALL_TERMINATED);
1377aa4dd815SMatthias Ringwald             break;
13783deb3ec6SMatthias Ringwald 
1379aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1380d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1381aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1382d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
138312cbbeeeSMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1384f8737b81SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL);
1385d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1386aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
138760ebb071SMilanka Ringwald                             log_info("AG Rejected Incoming call, AG terminate call");
1388aa4dd815SMatthias Ringwald                             break;
1389aa4dd815SMatthias Ringwald                         default:
1390aa4dd815SMatthias Ringwald                             break;
13913deb3ec6SMatthias Ringwald                     }
1392202c8a4cSMatthias Ringwald                     break;
139312cbbeeeSMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1394f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_TERMINATE_CALL_BY_AG, 0, 0, NULL);
1395d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1396d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1397aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
139860ebb071SMilanka Ringwald                     log_info("AG terminate call");
1399aa4dd815SMatthias Ringwald                     break;
1400aa4dd815SMatthias Ringwald                 default:
14013deb3ec6SMatthias Ringwald                     break;
14023deb3ec6SMatthias Ringwald             }
14033deb3ec6SMatthias Ringwald             break;
1404aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1405d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1406aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1407d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1408aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1409aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
141060ebb071SMilanka Ringwald                             log_info("Incoming call interrupted");
1411aa4dd815SMatthias Ringwald                             break;
1412aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1413aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
141460ebb071SMilanka Ringwald                             log_info("Outgoing call interrupted\n");
141560ebb071SMilanka Ringwald                             log_info("AG notify call dropped\n");
1416aa4dd815SMatthias Ringwald                             break;
1417aa4dd815SMatthias Ringwald                         default:
14183deb3ec6SMatthias Ringwald                             break;
14193deb3ec6SMatthias Ringwald                     }
1420f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1421d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1422aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
142310c236bfSMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1424aa4dd815SMatthias Ringwald                     break;
1425aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1426ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1427f8737b81SMatthias Ringwald                         hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1428ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1429a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1430ce263fc8SMatthias Ringwald                     }
1431f8737b81SMatthias Ringwald                     hfp_gsm_handler(HFP_AG_CALL_DROPPED, 0, 0, NULL);
1432d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1433d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1434aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
143560ebb071SMilanka Ringwald                     log_info("AG notify call dropped");
1436aa4dd815SMatthias Ringwald                     break;
1437aa4dd815SMatthias Ringwald                 default:
1438aa4dd815SMatthias Ringwald                     break;
1439aa4dd815SMatthias Ringwald             }
1440aa4dd815SMatthias Ringwald             break;
1441aa4dd815SMatthias Ringwald 
14429ff73f41SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED_BY_HF:
1443d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1444d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1445a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1446f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1447d210d9c4SMatthias Ringwald                 break;
1448d210d9c4SMatthias Ringwald             }
1449fe899794SMatthias Ringwald 
1450fe899794SMatthias Ringwald             // note: number not used currently
14519ff73f41SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, 0, 0, (const char *) &hfp_connection->line_buffer[3]);
14529cae807eSMatthias Ringwald 
1453a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
145474386ee0SMatthias Ringwald 
1455ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1456aa4dd815SMatthias Ringwald             break;
1457aa4dd815SMatthias Ringwald 
1458fe899794SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED_BY_AG:
1459fe899794SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1460fe899794SMatthias Ringwald             if (!hfp_gsm_call_possible()) {
1461fe899794SMatthias Ringwald                 // TODO: no error for user
1462fe899794SMatthias Ringwald                 break;
1463fe899794SMatthias Ringwald             }
1464fe899794SMatthias Ringwald             switch (hfp_gsm_call_status()) {
1465fe899794SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1466fe899794SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()) {
1467fe899794SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1468fe899794SMatthias Ringwald                             // note: number not used currently
1469fe899794SMatthias Ringwald                             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, 0, 0, "1234567");
1470fe899794SMatthias Ringwald                             // init call state for all connections
1471fe899794SMatthias Ringwald                             hfp_ag_trigger_outgoing_call();
1472fe899794SMatthias Ringwald                             // get first one and accept call
1473fe899794SMatthias Ringwald                             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1474fe899794SMatthias Ringwald                             if (hfp_connection) {
1475fe899794SMatthias Ringwald                                 hfp_ag_handle_outgoing_call_accepted(hfp_connection);
1476fe899794SMatthias Ringwald                             }
1477fe899794SMatthias Ringwald                             break;
1478fe899794SMatthias Ringwald                         default:
1479fe899794SMatthias Ringwald                             // TODO: no error for user
1480fe899794SMatthias Ringwald                             break;
1481fe899794SMatthias Ringwald                     }
1482fe899794SMatthias Ringwald                     break;
1483fe899794SMatthias Ringwald                 default:
1484fe899794SMatthias Ringwald                     // TODO: no error for user
1485fe899794SMatthias Ringwald                     break;
1486fe899794SMatthias Ringwald             }
1487fe899794SMatthias Ringwald             break;
14889cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1489d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1490d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1491a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1492f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(hfp_connection);
1493d210d9c4SMatthias Ringwald                 break;
1494d210d9c4SMatthias Ringwald             }
1495d210d9c4SMatthias Ringwald 
1496f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_REDIAL_INITIATED, 0, 0, NULL);
1497a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
149874386ee0SMatthias Ringwald 
149960ebb071SMilanka Ringwald             log_info("Redial last number");
15009cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1501aa4dd815SMatthias Ringwald 
15029cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
150360ebb071SMilanka Ringwald                 log_info("Last number exists: accept call");
1504ca59be51SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
15059cae807eSMatthias Ringwald             } else {
150660ebb071SMilanka Ringwald                 log_info("log_infoLast number missing: reject call");
15079f9621cfSMatthias Ringwald                 hfp_ag_handle_reject_outgoing_call();
15089cae807eSMatthias Ringwald             }
15099cae807eSMatthias Ringwald             break;
15109cae807eSMatthias Ringwald         }
1511aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
15129f9621cfSMatthias Ringwald             hfp_ag_handle_reject_outgoing_call();
1513aa4dd815SMatthias Ringwald             break;
1514aa4dd815SMatthias Ringwald 
1515d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1516a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1517a0ffb263SMatthias Ringwald             if (!hfp_connection){
1518a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1519aa4dd815SMatthias Ringwald                 break;
1520aa4dd815SMatthias Ringwald             }
1521aa4dd815SMatthias Ringwald 
1522a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
15235a7033e2SMatthias Ringwald             hfp_ag_handle_outgoing_call_accepted(hfp_connection);
1524aa4dd815SMatthias Ringwald             break;
1525d210d9c4SMatthias Ringwald         }
1526aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1527a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1528a0ffb263SMatthias Ringwald             if (!hfp_connection){
1529a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1530aa4dd815SMatthias Ringwald                 break;
1531aa4dd815SMatthias Ringwald             }
1532d210d9c4SMatthias Ringwald 
1533f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_RINGING, 0, 0, NULL);
1534a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1535d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1536aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1537aa4dd815SMatthias Ringwald             break;
1538aa4dd815SMatthias Ringwald 
1539d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1540aa4dd815SMatthias Ringwald             // get outgoing call
1541a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1542a0ffb263SMatthias Ringwald             if (!hfp_connection){
1543a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1544aa4dd815SMatthias Ringwald             }
1545a0ffb263SMatthias Ringwald             if (!hfp_connection){
1546a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1547aa4dd815SMatthias Ringwald                 break;
1548aa4dd815SMatthias Ringwald             }
1549d210d9c4SMatthias Ringwald 
1550d0c20769SMatthias Ringwald             int CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS = hfp_gsm_callheld_status() == HFP_CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS;
1551f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_OUTGOING_CALL_ESTABLISHED, 0, 0, NULL);
1552a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1553d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1554d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1555aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1556aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1557d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1558d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1559aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
15603deb3ec6SMatthias Ringwald             }
15613deb3ec6SMatthias Ringwald             break;
1562d210d9c4SMatthias Ringwald         }
1563d210d9c4SMatthias Ringwald 
156412cbbeeeSMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1565f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_USER_BUSY, 0, 0, NULL);
1566d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1567a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1568a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
156960ebb071SMilanka Ringwald             log_info("AG: Call Waiting, User Busy");
1570d210d9c4SMatthias Ringwald             break;
1571d210d9c4SMatthias Ringwald 
1572d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1573d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1574d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1575d210d9c4SMatthias Ringwald 
1576d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1577d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1578f8737b81SMatthias Ringwald                 hfp_gsm_handler(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index,
1579f8737b81SMatthias Ringwald                                 0, NULL);
1580d0c20769SMatthias Ringwald 
1581d0c20769SMatthias Ringwald             }
1582d0c20769SMatthias Ringwald 
1583d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
158460ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Accept new call");
1585d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1586a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1587d210d9c4SMatthias Ringwald             } else {
158860ebb071SMilanka Ringwald                 log_info("AG: Call Dropped, Resume held call");
1589d210d9c4SMatthias Ringwald             }
1590d210d9c4SMatthias Ringwald             if (call_held){
1591d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1592a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1593d210d9c4SMatthias Ringwald             }
1594d0c20769SMatthias Ringwald 
1595a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1596d210d9c4SMatthias Ringwald             break;
1597d210d9c4SMatthias Ringwald         }
1598d0c20769SMatthias Ringwald 
1599d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1600d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1601d210d9c4SMatthias Ringwald             // only update if callsetup changed
1602d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1603f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index, 0,
1604f8737b81SMatthias Ringwald                             NULL);
1605d0c20769SMatthias Ringwald 
1606d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
160760ebb071SMilanka Ringwald                 log_info("AG: Call on Hold, Accept new call");
1608d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1609a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1610d210d9c4SMatthias Ringwald             } else {
161160ebb071SMilanka Ringwald                 log_info("AG: Swap calls");
1612d210d9c4SMatthias Ringwald             }
1613d0c20769SMatthias Ringwald 
1614d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1615d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1616a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1617a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1618d210d9c4SMatthias Ringwald             break;
1619d210d9c4SMatthias Ringwald         }
1620d0c20769SMatthias Ringwald 
1621d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1622d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1623d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
162460ebb071SMilanka Ringwald                 log_info("AG: Join 3-way-call");
1625f8737b81SMatthias Ringwald                 hfp_gsm_handler(HFP_AG_CALL_HOLD_ADD_HELD_CALL, 0, 0, NULL);
1626d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1627a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1628ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_CONFERENCE_CALL);
1629d210d9c4SMatthias Ringwald             }
1630a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1631d210d9c4SMatthias Ringwald             break;
1632d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1633d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1634f8737b81SMatthias Ringwald             hfp_gsm_handler(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, 0, 0, NULL);
163560ebb071SMilanka Ringwald             log_info("AG: Transfer call -> Connect two calls and disconnect");
1636d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1637d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1638a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1639a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1640a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1641d210d9c4SMatthias Ringwald             break;
1642ce263fc8SMatthias Ringwald 
16433deb3ec6SMatthias Ringwald         default:
16443deb3ec6SMatthias Ringwald             break;
16453deb3ec6SMatthias Ringwald     }
1646d210d9c4SMatthias Ringwald 
1647d0c20769SMatthias Ringwald 
16483deb3ec6SMatthias Ringwald }
16493deb3ec6SMatthias Ringwald 
16509cae807eSMatthias Ringwald 
1651a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
16529cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
16539cae807eSMatthias Ringwald     if (!active_call) return;
16549cae807eSMatthias Ringwald 
16559cae807eSMatthias Ringwald     int idx = active_call->index;
16569cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
16579cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
16589cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
16599cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
16609cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
16619cae807eSMatthias Ringwald     char * number = active_call->clip_number;
16629cae807eSMatthias Ringwald 
16639cae807eSMatthias Ringwald     char buffer[100];
16649cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
16659cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
16669cae807eSMatthias Ringwald     if (number){
16671cc1d9e9SMilanka Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset-3, ", \"%s\",%u", number, type);
16689cae807eSMatthias Ringwald     }
16699cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
167060ebb071SMilanka 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,
16719cae807eSMatthias Ringwald        mode, mpty, type, number);
1672a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
16739cae807eSMatthias Ringwald }
16749cae807eSMatthias Ringwald 
1675473ac565SMatthias Ringwald 
1676febc14f5SMatthias Ringwald // sends pending command, returns if command was sent
1677febc14f5SMatthias Ringwald static int hfp_ag_send_commands(hfp_connection_t *hfp_connection){
167822387625SMatthias Ringwald 
1679a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1680a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1681a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1682a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1683a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
1684febc14f5SMatthias Ringwald             return 1;
16859cae807eSMatthias Ringwald         } else {
1686febc14f5SMatthias 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
1687a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1688a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1689a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16909cae807eSMatthias Ringwald         }
1691ce263fc8SMatthias Ringwald     }
1692ce263fc8SMatthias Ringwald 
1693a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1694a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1695a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1696febc14f5SMatthias Ringwald         return 1;
1697aa4dd815SMatthias Ringwald     }
1698aa4dd815SMatthias Ringwald 
1699a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1700a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1701a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1702a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1703485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1704febc14f5SMatthias Ringwald         return 1;
1705a0ffb263SMatthias Ringwald     }
1706a0ffb263SMatthias Ringwald 
1707a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1708a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1709a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1710485ac19eSMilanka Ringwald         hfp_ag_send_error(hfp_connection->rfcomm_cid);
1711febc14f5SMatthias Ringwald         return 1;
1712aa4dd815SMatthias Ringwald     }
1713aa4dd815SMatthias Ringwald 
1714ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1715a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1716a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1717a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1718485ac19eSMilanka Ringwald         hfp_ag_send_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1719febc14f5SMatthias Ringwald         return 1;
1720ce263fc8SMatthias Ringwald     }
1721ce263fc8SMatthias Ringwald 
1722a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1723a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1724a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1725485ac19eSMilanka Ringwald         hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1726febc14f5SMatthias Ringwald         return 1;
1727ce263fc8SMatthias Ringwald     }
1728ce263fc8SMatthias Ringwald 
1729aa4dd815SMatthias Ringwald     // update AG indicators
1730a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1731aa4dd815SMatthias Ringwald         int i;
1732a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1733a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1734a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1735a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1736ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1737ce263fc8SMatthias Ringwald                     break;
1738ce263fc8SMatthias Ringwald                 }
1739485ac19eSMilanka Ringwald                 hfp_ag_send_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1740febc14f5SMatthias Ringwald                 return 1;
1741aa4dd815SMatthias Ringwald             }
1742aa4dd815SMatthias Ringwald         }
1743aa4dd815SMatthias Ringwald     }
1744aa4dd815SMatthias Ringwald 
1745245852b7SMilanka Ringwald     if (hfp_connection->ag_send_no_carrier){
1746245852b7SMilanka Ringwald         hfp_connection->ag_send_no_carrier = false;
1747245852b7SMilanka Ringwald         hfp_ag_send_no_carrier(hfp_connection->rfcomm_cid);
1748245852b7SMilanka Ringwald         return 1;
1749245852b7SMilanka Ringwald     }
1750245852b7SMilanka Ringwald 
1751a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1752a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1753a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1754485ac19eSMilanka Ringwald         hfp_ag_send_ring(hfp_connection->rfcomm_cid);
1755febc14f5SMatthias Ringwald         return 1;
1756aa4dd815SMatthias Ringwald     }
1757aa4dd815SMatthias Ringwald 
1758a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1759a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1760a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1761a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1762febc14f5SMatthias Ringwald         return 1;
1763aa4dd815SMatthias Ringwald     }
1764aa4dd815SMatthias Ringwald 
1765a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1766a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1767a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1768a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1769a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1770febc14f5SMatthias Ringwald         return 1;
1771aa4dd815SMatthias Ringwald     }
1772aa4dd815SMatthias Ringwald 
1773a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1774a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1775a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1776a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1777ce263fc8SMatthias Ringwald         } else {
1778a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1779a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1780485ac19eSMilanka Ringwald             hfp_ag_send_ok(hfp_connection->rfcomm_cid);
1781ce263fc8SMatthias Ringwald         }
1782a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1783febc14f5SMatthias Ringwald         return 1;
1784ce263fc8SMatthias Ringwald     }
1785ce263fc8SMatthias Ringwald 
1786a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1787a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1788a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1789485ac19eSMilanka Ringwald         hfp_ag_send_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1790febc14f5SMatthias Ringwald         return 1;
1791aa4dd815SMatthias Ringwald     }
1792aa4dd815SMatthias Ringwald 
1793a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1794a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1795a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1796485ac19eSMilanka Ringwald         hfp_ag_send_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
1797febc14f5SMatthias Ringwald         return 1;
17983deb3ec6SMatthias Ringwald     }
17993deb3ec6SMatthias Ringwald 
1800a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1801a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1802485ac19eSMilanka Ringwald         hfp_ag_send_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1803febc14f5SMatthias Ringwald         return 1;
1804febc14f5SMatthias Ringwald     }
1805febc14f5SMatthias Ringwald 
1806febc14f5SMatthias Ringwald     return 0;
1807febc14f5SMatthias Ringwald }
1808febc14f5SMatthias Ringwald 
1809febc14f5SMatthias Ringwald static void hfp_ag_run_for_context(hfp_connection_t *hfp_connection){
1810febc14f5SMatthias Ringwald 
181176cc1527SMatthias Ringwald 	btstack_assert(hfp_connection != NULL);
181276cc1527SMatthias Ringwald 	btstack_assert(hfp_connection->local_role == HFP_ROLE_AG);
1813febc14f5SMatthias Ringwald 
181476cc1527SMatthias Ringwald 	// during SDP query, RFCOMM CID is not set
181576cc1527SMatthias Ringwald 	if (hfp_connection->rfcomm_cid == 0) return;
1816ce263fc8SMatthias Ringwald 
18173721a235SMatthias Ringwald     // assert command could be sent
18183721a235SMatthias Ringwald     if (hci_can_send_command_packet_now() == 0) return;
18193721a235SMatthias Ringwald 
1820f9f3ba28SMilanka Ringwald     if ((hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) && hfp_connection->release_audio_connection){
1821f9f3ba28SMilanka Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
1822f9f3ba28SMilanka Ringwald         hfp_connection->release_audio_connection = 0;
1823f9f3ba28SMilanka Ringwald         gap_disconnect(hfp_connection->sco_handle);
1824f9f3ba28SMilanka Ringwald         return;
1825f9f3ba28SMilanka Ringwald     }
1826f9f3ba28SMilanka Ringwald 
18273721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
18283721a235SMatthias Ringwald     // WBS Disassociate
18293721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_disassociate){
18303721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_disassociate = false;
18313721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_disassociate);
18323721a235SMatthias Ringwald         return;
18333721a235SMatthias Ringwald     }
18343721a235SMatthias Ringwald     // Write Codec Config
18353721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_write_codec_config){
18363721a235SMatthias Ringwald         hfp_connection->cc256x_send_write_codec_config = false;
18373721a235SMatthias Ringwald         hfp_cc256x_write_codec_config(hfp_connection);
18383721a235SMatthias Ringwald         return;
18393721a235SMatthias Ringwald     }
18403721a235SMatthias Ringwald     // WBS Associate
18413721a235SMatthias Ringwald     if (hfp_connection->cc256x_send_wbs_associate){
18423721a235SMatthias Ringwald         hfp_connection->cc256x_send_wbs_associate = false;
18433721a235SMatthias Ringwald         hci_send_cmd(&hci_ti_wbs_associate, hfp_connection->acl_handle);
18443721a235SMatthias Ringwald         return;
18453721a235SMatthias Ringwald     }
18463721a235SMatthias Ringwald #endif
1847689d4323SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS
1848689d4323SMatthias Ringwald     // Enable WBS
1849689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_enable_wbs){
1850689d4323SMatthias Ringwald         hfp_connection->bcm_send_enable_wbs = false;
1851689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 1, 2);
1852689d4323SMatthias Ringwald         return;
1853689d4323SMatthias Ringwald     }
1854689d4323SMatthias Ringwald     // Write I2S/PCM params
1855689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_write_i2spcm_interface_param){
1856689d4323SMatthias Ringwald         hfp_connection->bcm_send_write_i2spcm_interface_param = false;
1857689d4323SMatthias Ringwald         hfp_bcm_write_i2spcm_interface_param(hfp_connection);
1858689d4323SMatthias Ringwald         return;
1859689d4323SMatthias Ringwald     }
1860689d4323SMatthias Ringwald     // Disable WBS
1861689d4323SMatthias Ringwald     if (hfp_connection->bcm_send_disable_wbs){
1862689d4323SMatthias Ringwald         hfp_connection->bcm_send_disable_wbs = false;
1863689d4323SMatthias Ringwald         hci_send_cmd(&hci_bcm_enable_wbs, 0, 2);
1864689d4323SMatthias Ringwald         return;
1865689d4323SMatthias Ringwald     }
1866689d4323SMatthias Ringwald #endif
186748e6eeeeSMatthias Ringwald #if defined (ENABLE_CC256X_ASSISTED_HFP) || defined (ENABLE_BCM_PCM_WBS)
186848e6eeeeSMatthias Ringwald     if (hfp_connection->state == HFP_W4_WBS_SHUTDOWN){
186948e6eeeeSMatthias Ringwald         hfp_finalize_connection_context(hfp_connection);
187048e6eeeeSMatthias Ringwald         return;
187148e6eeeeSMatthias Ringwald     }
187248e6eeeeSMatthias Ringwald #endif
18733721a235SMatthias Ringwald 
1874febc14f5SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1875febc14f5SMatthias Ringwald         log_info("hfp_ag_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1876febc14f5SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1877febc14f5SMatthias Ringwald         return;
1878febc14f5SMatthias Ringwald     }
1879febc14f5SMatthias Ringwald 
1880febc14f5SMatthias Ringwald     int cmd_sent = hfp_ag_send_commands(hfp_connection);
1881febc14f5SMatthias Ringwald 
1882febc14f5SMatthias Ringwald     if (!cmd_sent){
1883febc14f5SMatthias Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1884febc14f5SMatthias Ringwald     }
1885febc14f5SMatthias Ringwald 
1886c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1887c04cf7ffSMilanka Ringwald         cmd_sent = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
18883deb3ec6SMatthias Ringwald     }
18893deb3ec6SMatthias Ringwald 
1890c04cf7ffSMilanka Ringwald     if (!cmd_sent){
1891c04cf7ffSMilanka Ringwald         cmd_sent = call_setup_state_machine(hfp_connection);
1892aa4dd815SMatthias Ringwald     }
1893aa4dd815SMatthias Ringwald 
1894b956fff3SMatthias Ringwald     // trigger codec exchange (must be before hfp_ag_run_for_audio_connection)
1895d0a0eceeSMatthias Ringwald     if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && hfp_connection->trigger_codec_exchange){
1896d0a0eceeSMatthias Ringwald         switch (hfp_connection->codecs_state){
1897d0a0eceeSMatthias Ringwald             case HFP_CODECS_IDLE:
1898d0a0eceeSMatthias Ringwald             case HFP_CODECS_RECEIVED_LIST:
1899d0a0eceeSMatthias Ringwald             case HFP_CODECS_AG_RESEND_COMMON_CODEC:
1900d0a0eceeSMatthias Ringwald             case HFP_CODECS_ERROR:
1901d0a0eceeSMatthias Ringwald                 hfp_connection->trigger_codec_exchange = 0;
1902d0a0eceeSMatthias Ringwald                 hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
19033deb3ec6SMatthias Ringwald                 break;
19043deb3ec6SMatthias Ringwald             default:
19053deb3ec6SMatthias Ringwald                 break;
19063deb3ec6SMatthias Ringwald         }
19073deb3ec6SMatthias Ringwald     }
1908c04cf7ffSMilanka Ringwald 
1909b956fff3SMatthias Ringwald     if (!cmd_sent){
1910b956fff3SMatthias Ringwald         cmd_sent = hfp_ag_run_for_audio_connection(hfp_connection);
1911b956fff3SMatthias Ringwald     }
1912b956fff3SMatthias Ringwald 
1913b956fff3SMatthias Ringwald 
1914d0a0eceeSMatthias Ringwald     // disconnect
1915d0a0eceeSMatthias Ringwald     if (!cmd_sent && (hfp_connection->command == HFP_CMD_NONE) && (hfp_connection->state == HFP_W2_DISCONNECT_RFCOMM)){
1916d0a0eceeSMatthias Ringwald         hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1917d0a0eceeSMatthias Ringwald         rfcomm_disconnect(hfp_connection->rfcomm_cid);
1918d0a0eceeSMatthias Ringwald     }
1919d0a0eceeSMatthias Ringwald 
1920c04cf7ffSMilanka Ringwald     if (cmd_sent){
1921a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1922473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1923473ac565SMatthias Ringwald     }
19243deb3ec6SMatthias Ringwald }
1925d68dcce1SMatthias Ringwald 
1926e0d09929SMatthias Ringwald static int hfp_parser_is_end_of_line(uint8_t byte){
1927c1ab6cc1SMatthias Ringwald     return (byte == '\n') || (byte == '\r');
1928e0d09929SMatthias Ringwald }
1929e0d09929SMatthias Ringwald 
1930e9c22d4eSMatthias Ringwald static void hfp_ag_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
19318a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
19328a46ec40SMatthias Ringwald 
19338a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
19348a46ec40SMatthias Ringwald     if (size < 1) return;
19359ec2630cSMatthias Ringwald 
1936a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1937a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
19381e35c04dSMatthias Ringwald 
1939186dd3d2SMatthias Ringwald     hfp_log_rfcomm_message("HFP_AG_RX", packet, size);
1940e43d1938SMatthias Ringwald #ifdef ENABLE_HFP_AT_MESSAGES
1941e43d1938SMatthias Ringwald     hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_AT_MESSAGE_RECEIVED, (char *) packet);
1942e43d1938SMatthias Ringwald #endif
19431e35c04dSMatthias Ringwald 
19448a46ec40SMatthias Ringwald     // process messages byte-wise
19453deb3ec6SMatthias Ringwald     int pos;
19463deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1947a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1948186dd3d2SMatthias Ringwald 
1949e0d09929SMatthias Ringwald         // parse until end of line
1950e0d09929SMatthias Ringwald         if (!hfp_parser_is_end_of_line(packet[pos])) continue;
1951e0d09929SMatthias Ringwald 
1952186dd3d2SMatthias Ringwald         hfp_generic_status_indicator_t * indicator;
1953a0ffb263SMatthias Ringwald         switch(hfp_connection->command){
1954ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1955ce263fc8SMatthias Ringwald                 if (hfp_ag_response_and_hold_active){
1956a0ffb263SMatthias Ringwald                     hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1957ce263fc8SMatthias Ringwald                 }
1958a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1959ce263fc8SMatthias Ringwald                 break;
1960ce263fc8SMatthias Ringwald             case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
19610222a807SMatthias Ringwald                 switch(hfp_connection->ag_response_and_hold_action){
1962ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1963a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1964ce263fc8SMatthias Ringwald                         break;
1965ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1966a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1967ce263fc8SMatthias Ringwald                         break;
1968ce263fc8SMatthias Ringwald                     case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1969a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1970ce263fc8SMatthias Ringwald                         break;
1971ce263fc8SMatthias Ringwald                     default:
1972ce263fc8SMatthias Ringwald                         break;
1973ce263fc8SMatthias Ringwald                 }
1974a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1975ce263fc8SMatthias Ringwald                 break;
1976ce263fc8SMatthias Ringwald             case HFP_CMD_HF_INDICATOR_STATUS:
1977a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
19780b6ea433SMilanka Ringwald                 if (hfp_connection->parser_indicator_index < hfp_generic_status_indicators_nr){
19790b6ea433SMilanka Ringwald                     indicator = &hfp_generic_status_indicators[hfp_connection->parser_indicator_index];
19800b6ea433SMilanka Ringwald                 } else {
1981a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
1982ce263fc8SMatthias Ringwald                     break;
1983ce263fc8SMatthias Ringwald                 }
1984ce263fc8SMatthias Ringwald                 switch (indicator->uuid){
1985ce263fc8SMatthias Ringwald                     case 1: // enhanced security
19860222a807SMatthias Ringwald                         if (hfp_connection->parser_indicator_value > 1) {
1987a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1988ce263fc8SMatthias Ringwald                             return;
1989ce263fc8SMatthias Ringwald                         }
1990e883851fSMatthias Ringwald                         log_info("HF Indicator 'enhanced security' set to %u", (unsigned int) hfp_connection->parser_indicator_value);
1991ce263fc8SMatthias Ringwald                         break;
1992ce263fc8SMatthias Ringwald                     case 2: // battery level
19930222a807SMatthias Ringwald                         if (hfp_connection->parser_indicator_value > 100){
1994a0ffb263SMatthias Ringwald                             hfp_connection->send_error = 1;
1995ce263fc8SMatthias Ringwald                             return;
1996ce263fc8SMatthias Ringwald                         }
1997e883851fSMatthias Ringwald                         log_info("HF Indicator 'battery' set to %u", (unsigned int) hfp_connection->parser_indicator_value);
1998ce263fc8SMatthias Ringwald                         break;
1999ce263fc8SMatthias Ringwald                     default:
2000e883851fSMatthias Ringwald                         log_info("HF Indicator unknown set to %u", (unsigned int) hfp_connection->parser_indicator_value);
2001ce263fc8SMatthias Ringwald                         break;
2002ce263fc8SMatthias Ringwald                 }
2003a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2004ce263fc8SMatthias Ringwald                 break;
2005ce263fc8SMatthias Ringwald             case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
2006ce263fc8SMatthias Ringwald                 // expected by SLC state machine
2007a0ffb263SMatthias Ringwald                 if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
2008a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
2009a0ffb263SMatthias Ringwald                 hfp_connection->send_ag_status_indicators = 1;
2010ce263fc8SMatthias Ringwald                 break;
2011ce263fc8SMatthias Ringwald             case HFP_CMD_LIST_CURRENT_CALLS:
2012a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2013a0ffb263SMatthias Ringwald                 hfp_connection->next_call_index = 0;
2014a0ffb263SMatthias Ringwald                 hfp_connection->send_status_of_current_calls = 1;
2015ce263fc8SMatthias Ringwald                 break;
2016ce263fc8SMatthias Ringwald             case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
2017ce263fc8SMatthias Ringwald                 if (subscriber_numbers_count == 0){
2018485ac19eSMilanka Ringwald                     hfp_ag_send_ok(hfp_connection->rfcomm_cid);
2019ce263fc8SMatthias Ringwald                     break;
2020ce263fc8SMatthias Ringwald                 }
2021a0ffb263SMatthias Ringwald                 hfp_connection->next_subscriber_number_to_send = 0;
2022a0ffb263SMatthias Ringwald                 hfp_connection->send_subscriber_number = 1;
2023ce263fc8SMatthias Ringwald                 break;
2024c1797c7dSMatthias Ringwald             case HFP_CMD_TRANSMIT_DTMF_CODES:
20250222a807SMatthias Ringwald             {
2026a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
20270222a807SMatthias Ringwald                 char buffer[2];
20280222a807SMatthias Ringwald                 buffer[0] = (char) hfp_connection->ag_dtmf_code;
20290222a807SMatthias Ringwald                 buffer[1] = 0;
20300222a807SMatthias Ringwald                 hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, buffer);
2031c1797c7dSMatthias Ringwald                 break;
20320222a807SMatthias Ringwald             }
2033aa4dd815SMatthias Ringwald             case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
2034a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2035ca59be51SMatthias Ringwald                 hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
2036aa4dd815SMatthias Ringwald                 break;
2037aa4dd815SMatthias Ringwald             case HFP_CMD_TURN_OFF_EC_AND_NR:
2038a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2039aa4dd815SMatthias Ringwald                 if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
2040a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
2041a0ffb263SMatthias Ringwald                     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
204260ebb071SMilanka Ringwald                     log_info("AG: EC/NR = %u", hfp_connection->ag_echo_and_noise_reduction);
2043aa4dd815SMatthias Ringwald                 } else {
2044a0ffb263SMatthias Ringwald                     hfp_connection->send_error = 1;
2045aa4dd815SMatthias Ringwald                 }
2046aa4dd815SMatthias Ringwald                 break;
2047aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_ANSWERED:
2048a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
204960ebb071SMilanka Ringwald                 log_info("HFP: ATA");
2050a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
2051aa4dd815SMatthias Ringwald                 break;
2052aa4dd815SMatthias Ringwald             case HFP_CMD_HANG_UP_CALL:
2053a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2054a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2055a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
2056aa4dd815SMatthias Ringwald                 break;
2057aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_HOLD: {
2058a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2059a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2060d0c20769SMatthias Ringwald 
20610222a807SMatthias Ringwald                 switch (hfp_connection->ag_call_hold_action){
20620222a807SMatthias Ringwald                     case 0:
2063d0c20769SMatthias Ringwald                         // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
2064a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
2065aa4dd815SMatthias Ringwald                         break;
20660222a807SMatthias Ringwald                     case 1:
2067d0c20769SMatthias Ringwald                         // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
2068d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
2069d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
2070a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
2071aa4dd815SMatthias Ringwald                         break;
20720222a807SMatthias Ringwald                     case 2:
2073d0c20769SMatthias Ringwald                         // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
2074d0c20769SMatthias Ringwald                         // Where both a held and a waiting call exist, the above procedures shall apply to the
2075d0c20769SMatthias Ringwald                         // waiting call (i.e., not to the held call) in conflicting situation.
2076a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
2077aa4dd815SMatthias Ringwald                         break;
20780222a807SMatthias Ringwald                     case 3:
2079d0c20769SMatthias Ringwald                         // Adds a held call to the conversation.
2080a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
2081aa4dd815SMatthias Ringwald                         break;
20820222a807SMatthias Ringwald                     case 4:
2083d0c20769SMatthias Ringwald                         // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
2084a0ffb263SMatthias Ringwald                         hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
2085aa4dd815SMatthias Ringwald                         break;
2086aa4dd815SMatthias Ringwald                     default:
2087aa4dd815SMatthias Ringwald                         break;
2088aa4dd815SMatthias Ringwald                 }
20890222a807SMatthias Ringwald 				hfp_connection->call_index = 0;
209035e92150SMatthias Ringwald                 break;
2091aa4dd815SMatthias Ringwald             }
2092aa4dd815SMatthias Ringwald             case HFP_CMD_CALL_PHONE_NUMBER:
2093a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
20949ff73f41SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_HF, hfp_connection);
2095aa4dd815SMatthias Ringwald                 break;
2096aa4dd815SMatthias Ringwald             case HFP_CMD_REDIAL_LAST_NUMBER:
2097a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2098a0ffb263SMatthias Ringwald                 hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
2099aa4dd815SMatthias Ringwald                 break;
2100aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CLIP:
2101a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2102a0ffb263SMatthias Ringwald                 log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
2103a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2104aa4dd815SMatthias Ringwald                 break;
2105aa4dd815SMatthias Ringwald             case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
2106a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2107a0ffb263SMatthias Ringwald                 log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
2108a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
2109aa4dd815SMatthias Ringwald                 break;
2110aa4dd815SMatthias Ringwald             case HFP_CMD_SET_SPEAKER_GAIN:
2111a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2112a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
211360ebb071SMilanka Ringwald                 log_info("HF speaker gain = %u", hfp_connection->speaker_gain);
21143db60f78SBjoern Hartmann                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_connection->speaker_gain);
2115aa4dd815SMatthias Ringwald                 break;
2116aa4dd815SMatthias Ringwald             case HFP_CMD_SET_MICROPHONE_GAIN:
2117a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;
2118a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
211960ebb071SMilanka Ringwald                 log_info("HF microphone gain = %u", hfp_connection->microphone_gain);
21202abbd98dSMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_connection->microphone_gain);
2121aa4dd815SMatthias Ringwald                 break;
2122aa4dd815SMatthias Ringwald             default:
2123aa4dd815SMatthias Ringwald                 break;
21243deb3ec6SMatthias Ringwald         }
21253deb3ec6SMatthias Ringwald     }
21260cef86faSMatthias Ringwald }
21273deb3ec6SMatthias Ringwald 
21281c6a0fc0SMatthias Ringwald static void hfp_ag_run(void){
2129d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
2130d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2131d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2132d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
213322387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2134f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
21353deb3ec6SMatthias Ringwald     }
21363deb3ec6SMatthias Ringwald }
21373deb3ec6SMatthias Ringwald 
21381c6a0fc0SMatthias Ringwald static void hfp_ag_rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
21393deb3ec6SMatthias Ringwald     switch (packet_type){
21403deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
2141e9c22d4eSMatthias Ringwald             hfp_ag_handle_rfcomm_data(packet_type, channel, packet, size);
21423deb3ec6SMatthias Ringwald             break;
21433deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2144e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
2145e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
2146f0aeb307SMatthias Ringwald                 hfp_ag_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2147e30a6a47SMatthias Ringwald                 return;
2148e30a6a47SMatthias Ringwald             }
214927950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_AG);
2150aa4dd815SMatthias Ringwald             break;
21513deb3ec6SMatthias Ringwald         default:
21523deb3ec6SMatthias Ringwald             break;
21533deb3ec6SMatthias Ringwald     }
21543deb3ec6SMatthias Ringwald 
21551c6a0fc0SMatthias Ringwald     hfp_ag_run();
21563deb3ec6SMatthias Ringwald }
21573deb3ec6SMatthias Ringwald 
21581c6a0fc0SMatthias Ringwald static void hfp_ag_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2159405014fbSMatthias Ringwald     hfp_handle_hci_event(packet_type, channel, packet, size, HFP_ROLE_AG);
21601c6a0fc0SMatthias Ringwald     hfp_ag_run();
2161405014fbSMatthias Ringwald }
2162405014fbSMatthias Ringwald 
21637ca89cabSMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, const uint8_t * codecs){
21643deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
21653deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
21663deb3ec6SMatthias Ringwald         return;
21673deb3ec6SMatthias Ringwald     }
21683deb3ec6SMatthias Ringwald     int i;
2169a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
21703deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
21713deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
21723deb3ec6SMatthias Ringwald     }
2173a0ffb263SMatthias Ringwald }
21743deb3ec6SMatthias Ringwald 
2175a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2176a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2177a0ffb263SMatthias Ringwald }
21783deb3ec6SMatthias Ringwald 
21797ca89cabSMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, const hfp_ag_indicator_t * ag_indicators){
2180a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
21816535961aSMatthias Ringwald     (void)memcpy(hfp_ag_indicators, ag_indicators,
21826535961aSMatthias Ringwald                  ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2183a0ffb263SMatthias Ringwald }
21843deb3ec6SMatthias Ringwald 
21857ca89cabSMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, const hfp_generic_status_indicator_t * hf_indicators){
218625789943SMilanka Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_INDICATORS) return;
2187a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
21886535961aSMatthias Ringwald     (void)memcpy(hfp_generic_status_indicators, hf_indicators,
21896535961aSMatthias Ringwald                  hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2190a0ffb263SMatthias Ringwald }
2191a0ffb263SMatthias Ringwald 
2192a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
21933deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
21946535961aSMatthias Ringwald     (void)memcpy(hfp_ag_call_hold_services, call_hold_services,
21956535961aSMatthias Ringwald                  call_hold_services_nr * sizeof(char *));
2196a0ffb263SMatthias Ringwald }
2197a0ffb263SMatthias Ringwald 
2198a0ffb263SMatthias Ringwald 
2199a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
220027950165SMatthias Ringwald 
2201520c92d5SMatthias Ringwald     hfp_init();
220220b2edb6SMatthias Ringwald     hfp_ag_call_hold_services_nr = 0;
220320b2edb6SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
220420b2edb6SMatthias Ringwald     hfp_ag_indicators_nr = 0;
220520b2edb6SMatthias Ringwald     hfp_codecs_nr = 0;
220620b2edb6SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
220720b2edb6SMatthias Ringwald     subscriber_numbers = NULL;
220820b2edb6SMatthias Ringwald     subscriber_numbers_count = 0;
2209d63c37a1SMatthias Ringwald 
22101c6a0fc0SMatthias Ringwald     hfp_ag_hci_event_callback_registration.callback = &hfp_ag_hci_event_packet_handler;
22111c6a0fc0SMatthias Ringwald     hci_add_event_handler(&hfp_ag_hci_event_callback_registration);
221227950165SMatthias Ringwald 
22131c6a0fc0SMatthias Ringwald     rfcomm_register_service(&hfp_ag_rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
221427950165SMatthias Ringwald 
221527950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
22161c6a0fc0SMatthias Ringwald     hfp_set_ag_rfcomm_packet_handler(&hfp_ag_rfcomm_packet_handler);
2217aa4dd815SMatthias Ringwald 
2218d210d9c4SMatthias Ringwald     hfp_gsm_init();
22193deb3ec6SMatthias Ringwald }
22203deb3ec6SMatthias Ringwald 
222120b2edb6SMatthias Ringwald void hfp_ag_deinit(void){
222220b2edb6SMatthias Ringwald     hfp_deinit();
222320b2edb6SMatthias Ringwald     hfp_gsm_deinit();
222420b2edb6SMatthias Ringwald     (void) memset(&hfp_ag_hci_event_callback_registration, 0, sizeof(btstack_packet_callback_registration_t));
222520b2edb6SMatthias Ringwald     (void) memset(&hfp_ag_callback, 0, sizeof(btstack_packet_handler_t));
222620b2edb6SMatthias Ringwald     (void) memset(&hfp_ag_call_hold_services, 0, sizeof(hfp_ag_call_hold_services));
222720b2edb6SMatthias Ringwald     (void) memset(&hfp_ag_response_and_hold_state, 0, sizeof(hfp_response_and_hold_state_t));
222820b2edb6SMatthias Ringwald }
222920b2edb6SMatthias Ringwald 
22303deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
2231323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE, HFP_ROLE_AG);
22323deb3ec6SMatthias Ringwald }
22333deb3ec6SMatthias Ringwald 
2234d97d752dSMilanka Ringwald void hfp_ag_release_service_level_connection(hci_con_handle_t acl_handle){
22359c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2236a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2237a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2238a33eb0c4SMilanka Ringwald         return;
2239a33eb0c4SMilanka Ringwald     }
2240a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2241f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
22423deb3ec6SMatthias Ringwald }
22433deb3ec6SMatthias Ringwald 
2244d97d752dSMilanka Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, hfp_cme_error_t error){
22459c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2246a0ffb263SMatthias Ringwald     if (!hfp_connection){
2247a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
22483deb3ec6SMatthias Ringwald         return;
22493deb3ec6SMatthias Ringwald     }
2250a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2251a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
22523deb3ec6SMatthias Ringwald         return;
22533deb3ec6SMatthias Ringwald     }
2254a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2255f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
22563deb3ec6SMatthias Ringwald }
22573deb3ec6SMatthias Ringwald 
2258a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
22592ee45eb2SMilanka Ringwald     log_info("hfp_ag_setup_audio_connection state %u", hfp_connection->state);
2260a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2261a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
22623deb3ec6SMatthias Ringwald 
2263a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2264a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2265d6ff09e1SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using CVSD");
2266d6ff09e1SMatthias Ringwald         hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
2267a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
22687522e673SMatthias Ringwald         // now, pick link settings
2269afac2a04SMilanka Ringwald         hfp_init_link_settings(hfp_connection, hfp_ag_esco_s4_supported(hfp_connection));
22703721a235SMatthias Ringwald #ifdef ENABLE_CC256X_ASSISTED_HFP
22713721a235SMatthias Ringwald         hfp_cc256x_prepare_for_sco(hfp_connection);
22723721a235SMatthias Ringwald #endif
22732ee45eb2SMilanka Ringwald         return;
22743deb3ec6SMatthias Ringwald     }
2275aa4dd815SMatthias Ringwald 
2276411a9776SMilanka Ringwald     uint8_t i;
2277411a9776SMilanka Ringwald     bool codec_was_in_use = false;
2278411a9776SMilanka Ringwald     bool better_codec_can_be_used = false;
2279411a9776SMilanka Ringwald 
2280411a9776SMilanka Ringwald     for (i = 0; i<hfp_connection->remote_codecs_nr; i++){
2281411a9776SMilanka Ringwald         if (hfp_connection->negotiated_codec == hfp_connection->remote_codecs[i]){
2282411a9776SMilanka Ringwald             codec_was_in_use = true;
2283411a9776SMilanka Ringwald         } else if (hfp_connection->negotiated_codec < hfp_connection->remote_codecs[i]){
2284411a9776SMilanka Ringwald             better_codec_can_be_used = true;
2285411a9776SMilanka Ringwald         }
2286411a9776SMilanka Ringwald     }
2287411a9776SMilanka Ringwald 
2288411a9776SMilanka Ringwald     if (!codec_was_in_use || better_codec_can_be_used){
22892ee45eb2SMilanka Ringwald         hfp_connection->trigger_codec_exchange = 1;
2290411a9776SMilanka Ringwald         hfp_connection->codecs_state = HFP_CODECS_IDLE;
2291411a9776SMilanka Ringwald     }
2292aa4dd815SMatthias Ringwald }
2293aa4dd815SMatthias Ringwald 
2294d97d752dSMilanka Ringwald void hfp_ag_establish_audio_connection(hci_con_handle_t acl_handle){
22959c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2296a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2297a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2298a33eb0c4SMilanka Ringwald         return;
2299a33eb0c4SMilanka Ringwald     }
2300411a9776SMilanka Ringwald 
2301a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2302a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2303f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
23043deb3ec6SMatthias Ringwald }
23053deb3ec6SMatthias Ringwald 
2306d97d752dSMilanka Ringwald void hfp_ag_release_audio_connection(hci_con_handle_t acl_handle){
23079c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2308a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2309a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2310a33eb0c4SMilanka Ringwald         return;
2311a33eb0c4SMilanka Ringwald     }
2312a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2313f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
23143deb3ec6SMatthias Ringwald }
2315aa4dd815SMatthias Ringwald 
2316aa4dd815SMatthias Ringwald /**
2317aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2318aa4dd815SMatthias Ringwald  */
2319aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2320aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2321aa4dd815SMatthias Ringwald         return;
2322aa4dd815SMatthias Ringwald     }
2323aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2324aa4dd815SMatthias Ringwald 
2325665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2326665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2327665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2328a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
232966c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2330a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2331f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2332aa4dd815SMatthias Ringwald     }
2333aa4dd815SMatthias Ringwald }
2334aa4dd815SMatthias Ringwald 
2335aa4dd815SMatthias Ringwald /**
2336aa4dd815SMatthias Ringwald  * @brief Called from GSM
2337aa4dd815SMatthias Ringwald  */
2338aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2339aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2340aa4dd815SMatthias Ringwald }
2341aa4dd815SMatthias Ringwald 
2342fe899794SMatthias Ringwald void hfp_ag_outgoing_call_initiated(const char * number) {
2343*c5753b03SMatthias Ringwald     UNUSED(number);
2344fe899794SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED_BY_AG, NULL);
2345fe899794SMatthias Ringwald }
2346fe899794SMatthias Ringwald 
2347aa4dd815SMatthias Ringwald /**
2348aa4dd815SMatthias Ringwald  * @brief number is stored.
2349aa4dd815SMatthias Ringwald  */
2350aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2351f8737b81SMatthias Ringwald     hfp_gsm_handler(HFP_AG_SET_CLIP, 0, type, number);
2352aa4dd815SMatthias Ringwald }
2353aa4dd815SMatthias Ringwald 
2354aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2355aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2356aa4dd815SMatthias Ringwald }
2357aa4dd815SMatthias Ringwald 
2358aa4dd815SMatthias Ringwald // call from AG UI
2359aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2360aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2361aa4dd815SMatthias Ringwald }
2362aa4dd815SMatthias Ringwald 
2363ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2364ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2365ce263fc8SMatthias Ringwald }
2366ce263fc8SMatthias Ringwald 
2367aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2368aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2369aa4dd815SMatthias Ringwald }
2370aa4dd815SMatthias Ringwald 
2371aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2372aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2373aa4dd815SMatthias Ringwald }
2374aa4dd815SMatthias Ringwald 
2375aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2376aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2377aa4dd815SMatthias Ringwald }
2378aa4dd815SMatthias Ringwald 
2379aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2380aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2381aa4dd815SMatthias Ringwald }
2382aa4dd815SMatthias Ringwald 
2383aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2384aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2385aa4dd815SMatthias Ringwald }
2386ce263fc8SMatthias Ringwald 
2387ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2388ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2389ce263fc8SMatthias Ringwald }
2390ce263fc8SMatthias Ringwald 
2391ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2392ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2393ce263fc8SMatthias Ringwald }
2394ce263fc8SMatthias Ringwald 
2395ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2396ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2397ce263fc8SMatthias Ringwald }
2398ce263fc8SMatthias Ringwald 
2399aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2400aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2401aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2402aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2403aa4dd815SMatthias Ringwald 
2404665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2405665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2406665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2407a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
240866c5995fSMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_AG) continue;
2409a0ffb263SMatthias Ringwald         if (!hfp_connection->ag_indicators[indicator_index].enabled) {
2410245852b7SMilanka Ringwald             log_info("Requested AG indicator '%s' update to %u, but it is not enabled", hfp_ag_indicators[indicator_index].name, value);
2411ce263fc8SMatthias Ringwald             continue;
2412ce263fc8SMatthias Ringwald         }
2413245852b7SMilanka Ringwald         log_info("AG indicator '%s' changed to %u, request transfer status", hfp_ag_indicators[indicator_index].name, value);
2414a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2415f0aeb307SMatthias Ringwald         hfp_ag_run_for_context(hfp_connection);
2416aa4dd815SMatthias Ringwald     }
2417aa4dd815SMatthias Ringwald }
2418aa4dd815SMatthias Ringwald 
2419aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2420245852b7SMilanka Ringwald     if ( (status == 0) && (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT)){
2421245852b7SMilanka Ringwald 
2422245852b7SMilanka Ringwald         // if network goes away wihle a call is active:
2423245852b7SMilanka Ringwald         // - the  call gets dropped
2424245852b7SMilanka Ringwald         // - we send NO CARRIER
2425245852b7SMilanka Ringwald         // NOTE: the CALL=0 has to be sent before NO CARRIER
2426245852b7SMilanka Ringwald 
2427245852b7SMilanka Ringwald         hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2428245852b7SMilanka Ringwald 
2429245852b7SMilanka Ringwald         btstack_linked_list_iterator_t it;
2430245852b7SMilanka Ringwald         btstack_linked_list_iterator_init(&it, hfp_get_connections());
2431245852b7SMilanka Ringwald         while (btstack_linked_list_iterator_has_next(&it)){
2432245852b7SMilanka Ringwald             hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2433245852b7SMilanka Ringwald             hfp_connection->ag_send_no_carrier = true;
2434245852b7SMilanka Ringwald         }
2435245852b7SMilanka Ringwald     }
2436aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2437aa4dd815SMatthias Ringwald }
2438aa4dd815SMatthias Ringwald 
2439aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2440aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2441aa4dd815SMatthias Ringwald }
2442aa4dd815SMatthias Ringwald 
2443aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2444aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2445aa4dd815SMatthias Ringwald }
2446aa4dd815SMatthias Ringwald 
2447aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2448aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2449aa4dd815SMatthias Ringwald }
2450aa4dd815SMatthias Ringwald 
2451d97d752dSMilanka Ringwald void hfp_ag_activate_voice_recognition(hci_con_handle_t acl_handle, int activate){
2452aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
24539c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2454a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2455a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2456a33eb0c4SMilanka Ringwald         return;
2457a33eb0c4SMilanka Ringwald     }
2458aa4dd815SMatthias Ringwald 
2459a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
246060ebb071SMilanka Ringwald         log_info("AG cannot acivate voice recognition - not supported by HF");
2461aa4dd815SMatthias Ringwald         return;
2462aa4dd815SMatthias Ringwald     }
2463aa4dd815SMatthias Ringwald 
2464a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2465a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2466f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2467aa4dd815SMatthias Ringwald }
2468aa4dd815SMatthias Ringwald 
2469d97d752dSMilanka Ringwald void hfp_ag_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
24709c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2471a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2472a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2473a33eb0c4SMilanka Ringwald         return;
2474a33eb0c4SMilanka Ringwald     }
2475a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2476a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2477a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2478a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2479aa4dd815SMatthias Ringwald     }
2480f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2481aa4dd815SMatthias Ringwald }
2482aa4dd815SMatthias Ringwald 
2483d97d752dSMilanka Ringwald void hfp_ag_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
24849c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2485a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2486a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2487a33eb0c4SMilanka Ringwald         return;
2488a33eb0c4SMilanka Ringwald     }
2489a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2490a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2491a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2492aa4dd815SMatthias Ringwald     }
2493f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2494aa4dd815SMatthias Ringwald }
2495aa4dd815SMatthias Ringwald 
2496d97d752dSMilanka Ringwald void hfp_ag_send_phone_number_for_voice_tag(hci_con_handle_t acl_handle, const char * number){
24979c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2498a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2499a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2500a33eb0c4SMilanka Ringwald         return;
2501a33eb0c4SMilanka Ringwald     }
2502aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2503a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2504aa4dd815SMatthias Ringwald }
2505aa4dd815SMatthias Ringwald 
2506d97d752dSMilanka Ringwald void hfp_ag_reject_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
25079c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2508a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2509a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2510a33eb0c4SMilanka Ringwald         return;
2511a33eb0c4SMilanka Ringwald     }
2512a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2513aa4dd815SMatthias Ringwald }
2514aa4dd815SMatthias Ringwald 
2515d97d752dSMilanka Ringwald void hfp_ag_send_dtmf_code_done(hci_con_handle_t acl_handle){
25169c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2517a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2518a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2519a33eb0c4SMilanka Ringwald         return;
2520a33eb0c4SMilanka Ringwald     }
2521a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2522c1797c7dSMatthias Ringwald }
2523aa4dd815SMatthias Ringwald 
2524ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2525ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2526ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2527ce263fc8SMatthias Ringwald }
2528ce263fc8SMatthias Ringwald 
25299cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
25309cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2531ce263fc8SMatthias Ringwald }
2532ce263fc8SMatthias Ringwald 
25339de679b7SMilanka Ringwald void hfp_ag_set_last_dialed_number(const char * number){
25349de679b7SMilanka Ringwald     hfp_gsm_set_last_dialed_number(number);
25359de679b7SMilanka Ringwald }
25369de679b7SMilanka Ringwald 
2537d97d752dSMilanka Ringwald void hfp_ag_notify_incoming_call_waiting(hci_con_handle_t acl_handle){
25389c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_ag_connection_context_for_acl_handle(acl_handle);
2539a33eb0c4SMilanka Ringwald     if (!hfp_connection){
2540a33eb0c4SMilanka Ringwald         log_error("HFP AG: ACL connection 0x%2x is not found.", acl_handle);
2541a33eb0c4SMilanka Ringwald         return;
2542a33eb0c4SMilanka Ringwald     }
2543a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2544a0ffb263SMatthias Ringwald 
2545a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2546f0aeb307SMatthias Ringwald     hfp_ag_run_for_context(hfp_connection);
2547a0ffb263SMatthias Ringwald }
2548ce263fc8SMatthias Ringwald 
254976cc1527SMatthias Ringwald void hfp_ag_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint8_t ability_to_reject_call, uint16_t supported_features, int wide_band_speech){
255076cc1527SMatthias Ringwald 	if (!name){
255176cc1527SMatthias Ringwald 		name = default_hfp_ag_service_name;
255276cc1527SMatthias Ringwald 	}
255376cc1527SMatthias Ringwald 	hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, rfcomm_channel_nr, name);
255476cc1527SMatthias Ringwald 
255576cc1527SMatthias Ringwald 	/*
255676cc1527SMatthias Ringwald 	 * 0x01 – Ability to reject a call
255776cc1527SMatthias Ringwald 	 * 0x00 – No ability to reject a call
255876cc1527SMatthias Ringwald 	 */
255976cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
256076cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
256176cc1527SMatthias Ringwald 
256276cc1527SMatthias Ringwald 	// Construct SupportedFeatures for SDP bitmap:
256376cc1527SMatthias Ringwald 	//
256476cc1527SMatthias Ringwald 	// "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
256576cc1527SMatthias Ringwald 	//  of the Bits 0 to 4 of the unsolicited result code +BRSF"
256676cc1527SMatthias Ringwald 	//
256776cc1527SMatthias Ringwald 	// Wide band speech (bit 5) requires Codec negotiation
256876cc1527SMatthias Ringwald 	//
256976cc1527SMatthias Ringwald 	uint16_t sdp_features = supported_features & 0x1f;
257076cc1527SMatthias Ringwald 	if (wide_band_speech && (supported_features & (1 << HFP_AGSF_CODEC_NEGOTIATION))){
257176cc1527SMatthias Ringwald 		sdp_features |= 1 << 5;
257276cc1527SMatthias Ringwald 	}
257376cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
257476cc1527SMatthias Ringwald 	de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
257576cc1527SMatthias Ringwald }
257676cc1527SMatthias Ringwald 
257776cc1527SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
257876cc1527SMatthias Ringwald 	if (callback == NULL){
257976cc1527SMatthias Ringwald 		log_error("hfp_ag_register_packet_handler called with NULL callback");
258076cc1527SMatthias Ringwald 		return;
258176cc1527SMatthias Ringwald 	}
258276cc1527SMatthias Ringwald 	hfp_ag_callback = callback;
258376cc1527SMatthias Ringwald 	hfp_set_ag_callback(callback);
258476cc1527SMatthias Ringwald }
2585