xref: /btstack/src/classic/hfp_ag.c (revision 03645b44ad850537fdb28d21449bf6603d87cb2d)
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  */
373deb3ec6SMatthias Ringwald 
383deb3ec6SMatthias Ringwald // *****************************************************************************
393deb3ec6SMatthias Ringwald //
4066a048abSMatthias Ringwald // HFP Audio Gateway (AG) unit
413deb3ec6SMatthias Ringwald //
423deb3ec6SMatthias Ringwald // *****************************************************************************
433deb3ec6SMatthias Ringwald 
447907f069SMatthias Ringwald #include "btstack_config.h"
453deb3ec6SMatthias Ringwald 
463deb3ec6SMatthias Ringwald #include <stdint.h>
473deb3ec6SMatthias Ringwald #include <stdio.h>
483deb3ec6SMatthias Ringwald #include <stdlib.h>
493deb3ec6SMatthias Ringwald #include <string.h>
503deb3ec6SMatthias Ringwald 
5156042629SMatthias Ringwald #include "hci_cmd.h"
5282636622SMatthias Ringwald #include "btstack_run_loop.h"
533deb3ec6SMatthias Ringwald 
543deb3ec6SMatthias Ringwald #include "hci.h"
553deb3ec6SMatthias Ringwald #include "btstack_memory.h"
563deb3ec6SMatthias Ringwald #include "hci_dump.h"
573deb3ec6SMatthias Ringwald #include "l2cap.h"
5816ece135SMatthias Ringwald #include "btstack_debug.h"
59e30a6a47SMatthias Ringwald #include "btstack_event.h"
6059c6af15SMatthias Ringwald #include "classic/core.h"
613edc84c5SMatthias Ringwald #include "classic/hfp.h"
623edc84c5SMatthias Ringwald #include "classic/hfp_ag.h"
63023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h"
64efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
65023f2764SMatthias Ringwald #include "classic/sdp_server.h"
66023f2764SMatthias Ringwald #include "classic/sdp_util.h"
673deb3ec6SMatthias Ringwald 
68c5b64319SMatthias Ringwald // private prototypes
69c5b64319SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
70c5b64319SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *context);
71c5b64319SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * connection);
72c5b64319SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * context);
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 
82c5b64319SMatthias Ringwald // gobals
833deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway";
84aa4dd815SMatthias Ringwald 
853deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
86aa4dd815SMatthias Ringwald 
873deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
883deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
893deb3ec6SMatthias Ringwald 
903deb3ec6SMatthias Ringwald static int  hfp_ag_indicators_nr = 0;
913deb3ec6SMatthias Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_AG_INDICATORS];
923deb3ec6SMatthias Ringwald 
93a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0;
94a0ffb263SMatthias Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_HF_INDICATORS];
95a0ffb263SMatthias Ringwald 
963deb3ec6SMatthias Ringwald static int  hfp_ag_call_hold_services_nr = 0;
973deb3ec6SMatthias Ringwald static char *hfp_ag_call_hold_services[6];
9813839019SMatthias Ringwald static btstack_packet_handler_t hfp_callback;
993deb3ec6SMatthias Ringwald 
100ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
101ce263fc8SMatthias Ringwald static int hfp_ag_response_and_hold_active = 0;
102aa4dd815SMatthias Ringwald 
103ce263fc8SMatthias Ringwald // Subcriber information entries
104ce263fc8SMatthias Ringwald static hfp_phone_number_t * subscriber_numbers = NULL;
105ce263fc8SMatthias Ringwald static int subscriber_numbers_count = 0;
106ce263fc8SMatthias Ringwald 
107c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
10899a10067SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection);
10999a10067SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
11099a10067SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
11199a10067SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection);
1123deb3ec6SMatthias Ringwald 
1133deb3ec6SMatthias Ringwald 
114a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
115a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
116a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
117a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1183deb3ec6SMatthias Ringwald     }
119a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
120a0ffb263SMatthias Ringwald }
121a0ffb263SMatthias Ringwald 
122a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
123a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
124a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
125a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
126a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
127a0ffb263SMatthias Ringwald     }
128a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1293deb3ec6SMatthias Ringwald }
1303deb3ec6SMatthias Ringwald 
131aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
132aa4dd815SMatthias Ringwald     int i;
133aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
134aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
135aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
136aa4dd815SMatthias Ringwald         }
137aa4dd815SMatthias Ringwald     }
138aa4dd815SMatthias Ringwald     return NULL;
139aa4dd815SMatthias Ringwald }
140aa4dd815SMatthias Ringwald 
141aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
142aa4dd815SMatthias Ringwald     int i;
143aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
144aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
145aa4dd815SMatthias Ringwald             return i;
146aa4dd815SMatthias Ringwald         }
147aa4dd815SMatthias Ringwald     }
148aa4dd815SMatthias Ringwald     return -1;
149aa4dd815SMatthias Ringwald }
150aa4dd815SMatthias Ringwald 
1513deb3ec6SMatthias Ringwald 
15213839019SMatthias Ringwald void hfp_ag_register_packet_handler(btstack_packet_handler_t callback){
1533deb3ec6SMatthias Ringwald     if (callback == NULL){
1543deb3ec6SMatthias Ringwald         log_error("hfp_ag_register_packet_handler called with NULL callback");
1553deb3ec6SMatthias Ringwald         return;
1563deb3ec6SMatthias Ringwald     }
1573deb3ec6SMatthias Ringwald     hfp_callback = callback;
158f4000eebSMatthias Ringwald     hfp_set_callback(callback);
1593deb3ec6SMatthias Ringwald }
1603deb3ec6SMatthias Ringwald 
1610cb5b971SMatthias Ringwald static int use_in_band_tone(void){
162aa4dd815SMatthias Ringwald     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1633deb3ec6SMatthias Ringwald }
1643deb3ec6SMatthias Ringwald 
165a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
166a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1673deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1683deb3ec6SMatthias Ringwald     return hf && ag;
1693deb3ec6SMatthias Ringwald }
1703deb3ec6SMatthias Ringwald 
171a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
172a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1733deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1743deb3ec6SMatthias Ringwald     return hf && ag;
1753deb3ec6SMatthias Ringwald }
1763deb3ec6SMatthias Ringwald 
177a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
178a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
1793deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
1803deb3ec6SMatthias Ringwald     return hf && ag;
1813deb3ec6SMatthias Ringwald }
1823deb3ec6SMatthias Ringwald 
1839b1c3b4dSMatthias 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){
1843deb3ec6SMatthias Ringwald     if (!name){
1853deb3ec6SMatthias Ringwald         name = default_hfp_ag_service_name;
1863deb3ec6SMatthias Ringwald     }
1872ef54b27SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, SDP_HandsfreeAudioGateway, rfcomm_channel_nr, name);
1883deb3ec6SMatthias Ringwald 
1893deb3ec6SMatthias Ringwald     /*
1903deb3ec6SMatthias Ringwald      * 0x01 – Ability to reject a call
1913deb3ec6SMatthias Ringwald      * 0x00 – No ability to reject a call
1923deb3ec6SMatthias Ringwald      */
193aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
194aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
195aa4dd815SMatthias Ringwald 
196aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
197aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
198aa4dd815SMatthias Ringwald }
199aa4dd815SMatthias Ringwald 
200aa4dd815SMatthias Ringwald static int hfp_ag_change_in_band_ring_tone_setting_cmd(uint16_t cid){
201aa4dd815SMatthias Ringwald     char buffer[20];
202aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
203aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2043deb3ec6SMatthias Ringwald }
2053deb3ec6SMatthias Ringwald 
2063deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2073deb3ec6SMatthias Ringwald     char buffer[40];
2083deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
2093deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2103deb3ec6SMatthias Ringwald }
2113deb3ec6SMatthias Ringwald 
2123deb3ec6SMatthias Ringwald static int hfp_ag_ok(uint16_t cid){
2133deb3ec6SMatthias Ringwald     char buffer[10];
2143deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nOK\r\n");
2153deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2163deb3ec6SMatthias Ringwald }
2173deb3ec6SMatthias Ringwald 
218aa4dd815SMatthias Ringwald static int hfp_ag_ring(uint16_t cid){
219aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
220aa4dd815SMatthias Ringwald }
221aa4dd815SMatthias Ringwald 
222aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
223aa4dd815SMatthias Ringwald     char buffer[50];
224d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
225aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
226aa4dd815SMatthias Ringwald }
227aa4dd815SMatthias Ringwald 
228ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
229ce263fc8SMatthias Ringwald     char buffer[50];
230ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
231ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
232ce263fc8SMatthias Ringwald }
233ce263fc8SMatthias Ringwald 
234c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
235aa4dd815SMatthias Ringwald     char buffer[50];
236d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
237aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
238aa4dd815SMatthias Ringwald }
239aa4dd815SMatthias Ringwald 
240aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
241aa4dd815SMatthias Ringwald     char buffer[50];
242a0ffb263SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
243aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
244aa4dd815SMatthias Ringwald }
245aa4dd815SMatthias Ringwald 
2463deb3ec6SMatthias Ringwald static int hfp_ag_error(uint16_t cid){
2473deb3ec6SMatthias Ringwald     char buffer[10];
2483deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nERROR\r\n");
2493deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2503deb3ec6SMatthias Ringwald }
2513deb3ec6SMatthias Ringwald 
2523deb3ec6SMatthias Ringwald static int hfp_ag_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2533deb3ec6SMatthias Ringwald     char buffer[20];
2543deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2553deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2563deb3ec6SMatthias Ringwald }
2573deb3ec6SMatthias Ringwald 
258ad902e3dSMatthias Ringwald // fast & small implementation for fixed int size
259ad902e3dSMatthias Ringwald static int string_len_for_uint32(uint32_t i){
260ad902e3dSMatthias Ringwald     if (i <         10) return 1;
261ad902e3dSMatthias Ringwald     if (i <        100) return 2;
262ad902e3dSMatthias Ringwald     if (i <       1000) return 3;
263ad902e3dSMatthias Ringwald     if (i <      10000) return 4;
26474386ee0SMatthias Ringwald     if (i <     100000) return 5;
265ad902e3dSMatthias Ringwald     if (i <    1000000) return 6;
266ad902e3dSMatthias Ringwald     if (i <   10000000) return 7;
267ad902e3dSMatthias Ringwald     if (i <  100000000) return 8;
268ad902e3dSMatthias Ringwald     if (i < 1000000000) return 9;
269ad902e3dSMatthias Ringwald     return 10;
270ad902e3dSMatthias Ringwald }
271ad902e3dSMatthias Ringwald 
272ad902e3dSMatthias Ringwald // get size for indicator string
273a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
274ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
275a0ffb263SMatthias Ringwald     return 8 + strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
276a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
277a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
278ad902e3dSMatthias Ringwald }
279ad902e3dSMatthias Ringwald 
280ad902e3dSMatthias Ringwald // store indicator
281a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
282ad902e3dSMatthias Ringwald     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
283a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
284a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
285a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2863deb3ec6SMatthias Ringwald }
287ad902e3dSMatthias Ringwald 
288ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
289a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
290a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
291ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
292ad902e3dSMatthias Ringwald     return 3 + (num_indicators-1) * 2;
2933deb3ec6SMatthias Ringwald }
294ad902e3dSMatthias Ringwald 
295ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
296a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
297ad902e3dSMatthias Ringwald     if (index == 0) {
298ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
299ad902e3dSMatthias Ringwald     }
300ad902e3dSMatthias Ringwald     index--;
301a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
302ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
303ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
304a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
305ad902e3dSMatthias Ringwald     }
306ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators - 1){
307ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
308ad902e3dSMatthias Ringwald     }
309ad902e3dSMatthias Ringwald     return 1; // comma
310ad902e3dSMatthias Ringwald }
311ad902e3dSMatthias Ringwald 
312a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
313ad902e3dSMatthias Ringwald     if (index == 0){
314ad902e3dSMatthias Ringwald         *buffer++ = '\r';
31574386ee0SMatthias Ringwald         *buffer++ = '\n';
316ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
317ad902e3dSMatthias Ringwald         memcpy(buffer, HFP_INDICATOR, len);
318ad902e3dSMatthias Ringwald         buffer += len;
319ad902e3dSMatthias Ringwald         *buffer++ = ':';
320ad902e3dSMatthias Ringwald         return;
321ad902e3dSMatthias Ringwald     }
322ad902e3dSMatthias Ringwald     index--;
323a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
324ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
325ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
326a0ffb263SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
327ad902e3dSMatthias Ringwald         return;
328ad902e3dSMatthias Ringwald     }
329ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators-1){
330ad902e3dSMatthias Ringwald         memcpy(buffer, "\r\n\r\nOK\r\n", 8);
331ad902e3dSMatthias Ringwald         return;
332ad902e3dSMatthias Ringwald     }
333ad902e3dSMatthias Ringwald     *buffer = ',';
3343deb3ec6SMatthias Ringwald }
3353deb3ec6SMatthias Ringwald 
3363deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
3373deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3383deb3ec6SMatthias Ringwald     int i;
3393deb3ec6SMatthias Ringwald     int offset = 0;
340a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr-1; i++) {
341a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3423deb3ec6SMatthias Ringwald     }
343a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
344a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3453deb3ec6SMatthias Ringwald     }
3463deb3ec6SMatthias Ringwald     return offset;
3473deb3ec6SMatthias Ringwald }
3483deb3ec6SMatthias Ringwald 
3493deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
350a0ffb263SMatthias Ringwald     if (buffer_size < hfp_generic_status_indicators_nr * 3) return 0;
3513deb3ec6SMatthias Ringwald     int i;
3523deb3ec6SMatthias Ringwald     int offset = 0;
353a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
354a0ffb263SMatthias 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);
3553deb3ec6SMatthias Ringwald     }
3563deb3ec6SMatthias Ringwald     return offset;
3573deb3ec6SMatthias Ringwald }
3583deb3ec6SMatthias Ringwald 
3593deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
3603deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3613deb3ec6SMatthias Ringwald     int i;
3623deb3ec6SMatthias Ringwald     int offset = 0;
3633deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr-1; i++) {
3643deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3653deb3ec6SMatthias Ringwald     }
3663deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3673deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3683deb3ec6SMatthias Ringwald     }
3693deb3ec6SMatthias Ringwald     return offset;
3703deb3ec6SMatthias Ringwald }
3713deb3ec6SMatthias Ringwald 
3723deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
3733deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0;
3743deb3ec6SMatthias Ringwald     int i;
3753deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
3763deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) {
3773deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3783deb3ec6SMatthias Ringwald     }
3793deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3803deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3813deb3ec6SMatthias Ringwald     }
3823deb3ec6SMatthias Ringwald     return offset;
3833deb3ec6SMatthias Ringwald }
3843deb3ec6SMatthias Ringwald 
385a0ffb263SMatthias Ringwald static int hfp_ag_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
386ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
387a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
388a0ffb263SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
3893deb3ec6SMatthias Ringwald 
390ad902e3dSMatthias Ringwald     // assumes: can send now == true
391ad902e3dSMatthias Ringwald     // assumes: num segments > 0
392aba39421SMatthias Ringwald     // assumes: individual segments are smaller than MTU
393ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
394ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
395ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
396ad902e3dSMatthias Ringwald     int offset = 0;
397ad902e3dSMatthias Ringwald     int segment = start_segment;
398ad902e3dSMatthias Ringwald     while (segment < num_segments){
399a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
400aba39421SMatthias Ringwald         if (offset + segment_len > mtu) break;
401aba39421SMatthias Ringwald         // append segement
402a0ffb263SMatthias Ringwald         store_segment(hfp_connection, segment, data+offset);
403ad902e3dSMatthias Ringwald         offset += segment_len;
404ad902e3dSMatthias Ringwald         segment++;
405ad902e3dSMatthias Ringwald     }
406ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
407ad902e3dSMatthias Ringwald     return segment;
408ad902e3dSMatthias Ringwald }
4093deb3ec6SMatthias Ringwald 
410ad902e3dSMatthias Ringwald // returns next segment to store
411a0ffb263SMatthias Ringwald static int hfp_ag_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
412a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
413a0ffb263SMatthias Ringwald     return hfp_ag_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
414ad902e3dSMatthias Ringwald         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
4153deb3ec6SMatthias Ringwald }
4163deb3ec6SMatthias Ringwald 
4173deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_indicators_status_cmd(uint16_t cid){
4183deb3ec6SMatthias Ringwald     char buffer[40];
4193deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_INDICATOR);
4203deb3ec6SMatthias Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, sizeof(buffer)-offset);
4213deb3ec6SMatthias Ringwald 
4223deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4233deb3ec6SMatthias Ringwald 
4243deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4253deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4263deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4273deb3ec6SMatthias Ringwald }
4283deb3ec6SMatthias Ringwald 
4293deb3ec6SMatthias Ringwald static int hfp_ag_set_indicator_status_update_cmd(uint16_t cid, uint8_t activate){
4303deb3ec6SMatthias Ringwald     // AT\r\n%s:3,0,0,%d\r\n
4313deb3ec6SMatthias Ringwald     return hfp_ag_ok(cid);
4323deb3ec6SMatthias Ringwald }
4333deb3ec6SMatthias Ringwald 
4343deb3ec6SMatthias Ringwald 
4353deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_can_hold_call_cmd(uint16_t cid){
436ad902e3dSMatthias Ringwald     char buffer[40];
4373deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
4383deb3ec6SMatthias Ringwald     offset += hfp_ag_call_services_join(buffer+offset, sizeof(buffer)-offset);
4393deb3ec6SMatthias Ringwald 
4403deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4413deb3ec6SMatthias Ringwald 
4423deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4433deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4443deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4453deb3ec6SMatthias Ringwald }
4463deb3ec6SMatthias Ringwald 
4473deb3ec6SMatthias Ringwald 
4483deb3ec6SMatthias Ringwald static int hfp_ag_list_supported_generic_status_indicators_cmd(uint16_t cid){
4493deb3ec6SMatthias Ringwald     return hfp_ag_ok(cid);
4503deb3ec6SMatthias Ringwald }
4513deb3ec6SMatthias Ringwald 
4523deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4533deb3ec6SMatthias Ringwald     char buffer[40];
454aa4dd815SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
4553deb3ec6SMatthias Ringwald     offset += hfp_hf_indicators_join(buffer+offset, sizeof(buffer)-offset);
4563deb3ec6SMatthias Ringwald 
4573deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4583deb3ec6SMatthias Ringwald 
459aa4dd815SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, ")\r\n\r\nOK\r\n");
4603deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4613deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4623deb3ec6SMatthias Ringwald }
4633deb3ec6SMatthias Ringwald 
4643deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4653deb3ec6SMatthias Ringwald     char buffer[40];
4663deb3ec6SMatthias Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer));
4673deb3ec6SMatthias Ringwald 
4683deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4693deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4703deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4713deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4723deb3ec6SMatthias Ringwald }
4733deb3ec6SMatthias Ringwald 
474aa4dd815SMatthias Ringwald static int hfp_ag_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4753deb3ec6SMatthias Ringwald     char buffer[20];
476aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
4773deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4783deb3ec6SMatthias Ringwald }
4793deb3ec6SMatthias Ringwald 
4803deb3ec6SMatthias Ringwald static int hfp_ag_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4813deb3ec6SMatthias Ringwald     char buffer[40];
4823deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
4833deb3ec6SMatthias Ringwald         sprintf(buffer, "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4843deb3ec6SMatthias Ringwald     } else {
4853deb3ec6SMatthias Ringwald         sprintf(buffer, "\r\n%s:%d,%d,%s\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode, op.format, op.name);
4863deb3ec6SMatthias Ringwald     }
4873deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4883deb3ec6SMatthias Ringwald }
4893deb3ec6SMatthias Ringwald 
4903deb3ec6SMatthias Ringwald 
4913deb3ec6SMatthias Ringwald static int hfp_ag_cmd_suggest_codec(uint16_t cid, uint8_t codec){
4923deb3ec6SMatthias Ringwald     char buffer[30];
4933deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec);
4943deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4953deb3ec6SMatthias Ringwald }
4963deb3ec6SMatthias Ringwald 
497aa4dd815SMatthias Ringwald static int hfp_ag_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
498aa4dd815SMatthias Ringwald     char buffer[30];
499aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
500aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
501aa4dd815SMatthias Ringwald }
502aa4dd815SMatthias Ringwald 
503aa4dd815SMatthias Ringwald static int hfp_ag_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
504aa4dd815SMatthias Ringwald     char buffer[30];
505aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_SPEAKER_GAIN, gain);
506aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
507aa4dd815SMatthias Ringwald }
508aa4dd815SMatthias Ringwald 
509aa4dd815SMatthias Ringwald static int hfp_ag_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
510aa4dd815SMatthias Ringwald     char buffer[30];
511aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_MICROPHONE_GAIN, gain);
512aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
513aa4dd815SMatthias Ringwald }
514aa4dd815SMatthias Ringwald 
515ce263fc8SMatthias Ringwald static int hfp_ag_set_response_and_hold(uint16_t cid, int state){
516ce263fc8SMatthias Ringwald     char buffer[30];
517ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: %d\r\n", HFP_RESPONSE_AND_HOLD, state);
518ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
519ce263fc8SMatthias Ringwald }
520ce263fc8SMatthias Ringwald 
521a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
5226a7f44bdSMilanka Ringwald     if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_codecs_nr, hfp_codecs)){
5236a7f44bdSMilanka Ringwald         if (hfp_supports_codec(HFP_CODEC_MSBC, hfp_connection->remote_codecs_nr, hfp_connection->remote_codecs)){
524df327ff3SMilanka Ringwald             return HFP_CODEC_MSBC;
5253deb3ec6SMatthias Ringwald         }
5263deb3ec6SMatthias Ringwald     }
527df327ff3SMilanka Ringwald     return HFP_CODEC_CVSD;
5283deb3ec6SMatthias Ringwald }
5293deb3ec6SMatthias Ringwald 
530a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
531aa4dd815SMatthias Ringwald     /* events ( == commands):
532aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
533aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
534aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
535aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
536aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
537aa4dd815SMatthias Ringwald     */
538aa4dd815SMatthias Ringwald 
539a0ffb263SMatthias Ringwald      switch (hfp_connection->codecs_state){
540aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
541a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
542aa4dd815SMatthias Ringwald             break;
543aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
544a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
545aa4dd815SMatthias Ringwald             break;
546aa4dd815SMatthias Ringwald         default:
547aa4dd815SMatthias Ringwald             break;
548aa4dd815SMatthias Ringwald     }
549aa4dd815SMatthias Ringwald 
550aa4dd815SMatthias Ringwald     // printf(" -> State machine: CC\n");
551aa4dd815SMatthias Ringwald 
552a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
553aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
554a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
555a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
556a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
557aa4dd815SMatthias Ringwald                 return 1;
558aa4dd815SMatthias Ringwald             }
559aa4dd815SMatthias Ringwald 
560a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
561aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
562aa4dd815SMatthias Ringwald                 case HFP_CODECS_EXCHANGED:
563a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
564aa4dd815SMatthias Ringwald                     break;
565aa4dd815SMatthias Ringwald                 default:
566aa4dd815SMatthias Ringwald                     break;
567aa4dd815SMatthias Ringwald             }
568a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
569aa4dd815SMatthias Ringwald             return 1;
570aa4dd815SMatthias Ringwald 
571aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
572a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
573a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
574aa4dd815SMatthias Ringwald             return 1;
575aa4dd815SMatthias Ringwald 
576aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
577a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
578a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
579a0ffb263SMatthias Ringwald             hfp_ag_cmd_suggest_codec(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
580aa4dd815SMatthias Ringwald             return 1;
581aa4dd815SMatthias Ringwald 
582aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
583a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
584a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
585a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
586aa4dd815SMatthias Ringwald                 return 1;
587aa4dd815SMatthias Ringwald             }
588a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
589a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
590*03645b44SMatthias Ringwald             log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
5916a7f44bdSMilanka Ringwald             hfp_emit_codec_event(hfp_callback, 0, hfp_connection->negotiated_codec);
592a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
593aa4dd815SMatthias Ringwald             return 1;
594aa4dd815SMatthias Ringwald         default:
595aa4dd815SMatthias Ringwald             break;
596aa4dd815SMatthias Ringwald     }
597aa4dd815SMatthias Ringwald     return 0;
598aa4dd815SMatthias Ringwald }
599aa4dd815SMatthias Ringwald 
600a0ffb263SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){
601ce263fc8SMatthias Ringwald     // determine highest possible link setting
602a0ffb263SMatthias Ringwald     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
603a0ffb263SMatthias Ringwald     if (hci_remote_esco_supported(hfp_connection->acl_handle)){
604a0ffb263SMatthias Ringwald         hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
605a0ffb263SMatthias Ringwald         if ((hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4))
606ce263fc8SMatthias Ringwald         &&  (hfp_supported_features             & (1<<HFP_AGSF_ESCO_S4))){
607a0ffb263SMatthias Ringwald             hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
608ce263fc8SMatthias Ringwald         }
609ce263fc8SMatthias Ringwald     }
610ce263fc8SMatthias Ringwald }
611ce263fc8SMatthias Ringwald 
612a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
613a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
6146a7f44bdSMilanka Ringwald     hfp_emit_connection_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0, hfp_connection->acl_handle, hfp_connection->remote_addr, hfp_connection->negotiated_codec);
615aa4dd815SMatthias Ringwald 
616a0ffb263SMatthias Ringwald     hfp_init_link_settings(hfp_connection);
617ce263fc8SMatthias Ringwald 
618a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
619d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
620a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
621aa4dd815SMatthias Ringwald     }
622ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
623d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS &&
624d0c20769SMatthias Ringwald         hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
625a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
626ce263fc8SMatthias Ringwald     }
627aa4dd815SMatthias Ringwald }
6283deb3ec6SMatthias Ringwald 
629a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
630473ac565SMatthias Ringwald     log_info("hfp_ag_run_for_context_service_level_connection state %u, command %u", hfp_connection->state, hfp_connection->command);
631a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
6323deb3ec6SMatthias Ringwald     int done = 0;
633a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
6343deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
635a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
6363deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
637aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
638a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
639a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
640aa4dd815SMatthias Ringwald                     } else {
641a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
642aa4dd815SMatthias Ringwald                     }
643a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
644aa4dd815SMatthias Ringwald                     return 1;
6453deb3ec6SMatthias Ringwald                 default:
6463deb3ec6SMatthias Ringwald                     break;
6473deb3ec6SMatthias Ringwald             }
6483deb3ec6SMatthias Ringwald             break;
6493deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
650a0ffb263SMatthias Ringwald             done = codecs_exchange_state_machine(hfp_connection);
6513deb3ec6SMatthias Ringwald 
652a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
653a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6543deb3ec6SMatthias Ringwald             }
655aa4dd815SMatthias Ringwald             return done;
656aa4dd815SMatthias Ringwald 
657aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
658a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
659473ac565SMatthias Ringwald                 // HF requested AG Indicators and we did expect it
660473ac565SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_INDICATORS;
661473ac565SMatthias Ringwald                 // continue below in state switch
662ad902e3dSMatthias Ringwald             }
663ad902e3dSMatthias Ringwald             break;
664aa4dd815SMatthias Ringwald 
665aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
666a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
667a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
668a0ffb263SMatthias Ringwald             hfp_ag_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
669aa4dd815SMatthias Ringwald             return 1;
670aa4dd815SMatthias Ringwald 
6713deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
672a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
673a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
674a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
675a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
676a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
677aa4dd815SMatthias Ringwald             } else {
678a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
6793deb3ec6SMatthias Ringwald             }
680a0ffb263SMatthias Ringwald             hfp_ag_set_indicator_status_update_cmd(hfp_connection->rfcomm_cid, 1);
681aa4dd815SMatthias Ringwald             return 1;
6823deb3ec6SMatthias Ringwald 
683aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
684a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
685a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
686a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
687aa4dd815SMatthias Ringwald             }
688a0ffb263SMatthias Ringwald             hfp_ag_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
689a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
690a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
691ce263fc8SMatthias Ringwald             }
692aa4dd815SMatthias Ringwald             return 1;
693aa4dd815SMatthias Ringwald 
694aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
695a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
696a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
697a0ffb263SMatthias Ringwald             hfp_ag_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
698aa4dd815SMatthias Ringwald             return 1;
699aa4dd815SMatthias Ringwald 
700aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
701a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
702a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
703a0ffb263SMatthias Ringwald             hfp_ag_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
704aa4dd815SMatthias Ringwald             return 1;
705aa4dd815SMatthias Ringwald 
706aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
707a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
708a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
709a0ffb263SMatthias Ringwald             hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
710aa4dd815SMatthias Ringwald             return 1;
7113deb3ec6SMatthias Ringwald         default:
7123deb3ec6SMatthias Ringwald             break;
7133deb3ec6SMatthias Ringwald     }
714473ac565SMatthias Ringwald 
715473ac565SMatthias Ringwald     switch (hfp_connection->state){
716473ac565SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS: {
717473ac565SMatthias Ringwald             int next_segment = hfp_ag_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
718473ac565SMatthias Ringwald             int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
719473ac565SMatthias Ringwald             log_info("HFP_CMD_RETRIEVE_AG_INDICATORS next segment %u, num_segments %u", next_segment, num_segments);
720473ac565SMatthias Ringwald             if (next_segment < num_segments){
721473ac565SMatthias Ringwald                 // prepare sending of next segment
722473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = next_segment;
723473ac565SMatthias Ringwald                 log_info("HFP_CMD_RETRIEVE_AG_INDICATORS more. command %u, next seg %u", hfp_connection->command, next_segment);
724473ac565SMatthias Ringwald             } else {
725473ac565SMatthias Ringwald                 // done, go to next state
726473ac565SMatthias Ringwald                 hfp_connection->send_ag_indicators_segment = 0;
727473ac565SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
728473ac565SMatthias Ringwald             }
729473ac565SMatthias Ringwald             return 1;
730473ac565SMatthias Ringwald         }
731473ac565SMatthias Ringwald         default:
732473ac565SMatthias Ringwald             break;
733473ac565SMatthias Ringwald     }
734473ac565SMatthias Ringwald 
7353deb3ec6SMatthias Ringwald     return done;
7363deb3ec6SMatthias Ringwald }
7373deb3ec6SMatthias Ringwald 
738a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
7393deb3ec6SMatthias Ringwald 
740a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
741aa4dd815SMatthias Ringwald     if (done) return done;
742aa4dd815SMatthias Ringwald 
743a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
744aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
745a0ffb263SMatthias Ringwald             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
746a0ffb263SMatthias Ringwald             hfp_ag_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
747aa4dd815SMatthias Ringwald             return 1;
748aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
749aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
750a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
751a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
752a0ffb263SMatthias Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
753aa4dd815SMatthias Ringwald             } else {
754a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
755aa4dd815SMatthias Ringwald             }
756aa4dd815SMatthias Ringwald             return 1;
757aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
758a0ffb263SMatthias Ringwald             hfp_ag_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
759aa4dd815SMatthias Ringwald             return 1;
760aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
761a0ffb263SMatthias Ringwald             hfp_ag_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
762aa4dd815SMatthias Ringwald             return 1;
763aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
764a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
765a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
766aa4dd815SMatthias Ringwald             } else {
767a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
7683deb3ec6SMatthias Ringwald             }
769aa4dd815SMatthias Ringwald             return 1;
770aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
771a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
772aa4dd815SMatthias Ringwald             return 1;
7733deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
774a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
775a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
776a0ffb263SMatthias Ringwald                 hfp_ag_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
777aa4dd815SMatthias Ringwald                 return 1;
7783deb3ec6SMatthias Ringwald             }
7793deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
780a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
781ce263fc8SMatthias Ringwald             return 1;
7823deb3ec6SMatthias Ringwald         default:
7833deb3ec6SMatthias Ringwald             break;
7843deb3ec6SMatthias Ringwald     }
785aa4dd815SMatthias Ringwald     return 0;
7863deb3ec6SMatthias Ringwald }
7873deb3ec6SMatthias Ringwald 
788a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
789a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
790a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
7913deb3ec6SMatthias Ringwald 
792aa4dd815SMatthias Ringwald 
793a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
794a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
795a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
796a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
797aa4dd815SMatthias Ringwald         return 1;
798aa4dd815SMatthias Ringwald     }
799aa4dd815SMatthias Ringwald 
800a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
801aa4dd815SMatthias Ringwald 
802aa4dd815SMatthias Ringwald     // run codecs exchange
803a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
804aa4dd815SMatthias Ringwald     if (done) return done;
805aa4dd815SMatthias Ringwald 
806a0ffb263SMatthias Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return done;
807a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
808a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
809a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
810eddcd308SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
811aa4dd815SMatthias Ringwald         return 1;
812aa4dd815SMatthias Ringwald     }
813aa4dd815SMatthias Ringwald     return 0;
814aa4dd815SMatthias Ringwald }
815aa4dd815SMatthias Ringwald 
816ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
817665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
818665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
819aa4dd815SMatthias Ringwald 
820665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
821a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
822a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
823a0ffb263SMatthias Ringwald             return hfp_connection;
824aa4dd815SMatthias Ringwald         }
825aa4dd815SMatthias Ringwald     }
826aa4dd815SMatthias Ringwald     return NULL;
827aa4dd815SMatthias Ringwald }
828aa4dd815SMatthias Ringwald 
829ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
830d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
831d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
832aa4dd815SMatthias Ringwald 
833d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
834a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
835a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
836aa4dd815SMatthias Ringwald 
837a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
838a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
839aa4dd815SMatthias Ringwald 
840a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
841aa4dd815SMatthias Ringwald }
842aa4dd815SMatthias Ringwald 
843a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
844a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
845a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
846a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
847a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
848aa4dd815SMatthias Ringwald }
849aa4dd815SMatthias Ringwald 
850a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
851a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
852a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
853aa4dd815SMatthias Ringwald }
854aa4dd815SMatthias Ringwald 
855aa4dd815SMatthias Ringwald //
856aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
857aa4dd815SMatthias Ringwald //
858aa4dd815SMatthias Ringwald 
859a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
860aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
861a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
862a0ffb263SMatthias Ringwald         hfp_ag_establish_audio_connection(hfp_connection->remote_addr);
863aa4dd815SMatthias Ringwald     } else {
864a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
865a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
866a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
867a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
868a0ffb263SMatthias Ringwald         hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG);
869aa4dd815SMatthias Ringwald     }
870aa4dd815SMatthias Ringwald }
871aa4dd815SMatthias Ringwald 
872a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
873a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
874a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
875a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
876a0ffb263SMatthias Ringwald     hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_STOP_RINGINIG);
877aa4dd815SMatthias Ringwald }
878aa4dd815SMatthias Ringwald 
879aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
880aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
881aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
882aa4dd815SMatthias Ringwald 
883665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
884665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
885665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
886a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
887a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
888a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
889a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
890d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
891aa4dd815SMatthias Ringwald         }
892a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
893a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
894aa4dd815SMatthias Ringwald         }
895a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
896aa4dd815SMatthias Ringwald     }
897aa4dd815SMatthias Ringwald }
898aa4dd815SMatthias Ringwald 
899aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
900aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
901aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
902aa4dd815SMatthias Ringwald 
903665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
904665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
905665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
906a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
907a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
908a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
909a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
910aa4dd815SMatthias Ringwald     }
911aa4dd815SMatthias Ringwald }
912aa4dd815SMatthias Ringwald 
913aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
914aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("call");
915aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
916aa4dd815SMatthias Ringwald 
917665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
918665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
919665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
920a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
921a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
922a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
923a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
924aa4dd815SMatthias Ringwald     }
925aa4dd815SMatthias Ringwald }
926aa4dd815SMatthias Ringwald 
927aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
928aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callheld");
929aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
930aa4dd815SMatthias Ringwald 
931665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
932665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
933665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
934a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
935a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
936a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
937a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
938aa4dd815SMatthias Ringwald     }
939aa4dd815SMatthias Ringwald }
940aa4dd815SMatthias Ringwald 
941aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
942aa4dd815SMatthias Ringwald 
943aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
944aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
945aa4dd815SMatthias Ringwald 
946665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
947665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
948665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
949a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
950a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
951a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
952aa4dd815SMatthias Ringwald 
953a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
954a0ffb263SMatthias Ringwald         if (hfp_connection == source){
955a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
956aa4dd815SMatthias Ringwald 
957aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
958a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
959aa4dd815SMatthias Ringwald             } else {
960a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
961a0ffb263SMatthias Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->remote_addr);
962aa4dd815SMatthias Ringwald             }
963aa4dd815SMatthias Ringwald 
964a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
965a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
966aa4dd815SMatthias Ringwald 
967aa4dd815SMatthias Ringwald         } else {
968a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
969aa4dd815SMatthias Ringwald         }
970a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
971aa4dd815SMatthias Ringwald     }
972aa4dd815SMatthias Ringwald }
973aa4dd815SMatthias Ringwald 
974aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
975aa4dd815SMatthias Ringwald 
976aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
977aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
978aa4dd815SMatthias Ringwald 
979665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
980665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
981665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
982a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
983a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
984aa4dd815SMatthias Ringwald 
985a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
986a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
987aa4dd815SMatthias Ringwald 
988a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
989a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
990aa4dd815SMatthias Ringwald 
991a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
992aa4dd815SMatthias Ringwald         break;  // only single
993aa4dd815SMatthias Ringwald     }
994aa4dd815SMatthias Ringwald }
995aa4dd815SMatthias Ringwald 
996aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
997aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
998665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
999665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1000665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1001665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1002aa4dd815SMatthias Ringwald         if (connection->call_state != HFP_CALL_RINGING &&
1003aa4dd815SMatthias Ringwald             connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1004aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
1005aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1006aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
1007aa4dd815SMatthias Ringwald         hfp_run_for_context(connection);
1008aa4dd815SMatthias Ringwald     }
1009aa4dd815SMatthias Ringwald }
1010aa4dd815SMatthias Ringwald 
1011aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1012aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1013aa4dd815SMatthias Ringwald 
1014665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1015665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1016665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1017a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1018d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1019a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1020a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1021a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1022a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 1;
1023a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1024aa4dd815SMatthias Ringwald     }
1025a0ffb263SMatthias Ringwald     hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_TERMINATED);
1026aa4dd815SMatthias Ringwald }
1027aa4dd815SMatthias Ringwald 
10280cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
1029aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1030aa4dd815SMatthias Ringwald     if (!indicator){
1031d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
1032aa4dd815SMatthias Ringwald     };
1033d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1034aa4dd815SMatthias Ringwald }
1035aa4dd815SMatthias Ringwald 
10360cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1037d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1038d210d9c4SMatthias Ringwald     if (!indicator){
1039d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
1040d210d9c4SMatthias Ringwald     };
1041d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1042d210d9c4SMatthias Ringwald }
1043d210d9c4SMatthias Ringwald 
10440cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1045aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1046aa4dd815SMatthias Ringwald     if (!indicator){
1047aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
1048aa4dd815SMatthias Ringwald     };
1049d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1050aa4dd815SMatthias Ringwald }
1051aa4dd815SMatthias Ringwald 
1052aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1053665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1054665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1055665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1056a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1057a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
1058a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1059a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1060aa4dd815SMatthias Ringwald     }
1061aa4dd815SMatthias Ringwald }
1062aa4dd815SMatthias Ringwald 
1063aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1064665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1065665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1066665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1067a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1068a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1069aa4dd815SMatthias Ringwald     }
1070aa4dd815SMatthias Ringwald     return NULL;
1071aa4dd815SMatthias Ringwald }
1072aa4dd815SMatthias Ringwald 
1073a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1074665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1075665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1076665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1077a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1078a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1079ce263fc8SMatthias Ringwald     }
1080ce263fc8SMatthias Ringwald }
1081ce263fc8SMatthias Ringwald 
1082a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1083aa4dd815SMatthias Ringwald     int indicator_index;
1084a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1085aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1086a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1087a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1088a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1089a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1090a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1091a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1092a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1093a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG);
1094aa4dd815SMatthias Ringwald             break;
1095aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1096a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1097a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1098a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1099aa4dd815SMatthias Ringwald             break;
1100aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1101a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1102a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1103aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1104a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1105aa4dd815SMatthias Ringwald             break;
1106aa4dd815SMatthias Ringwald         default:
1107aa4dd815SMatthias Ringwald             break;
1108aa4dd815SMatthias Ringwald     }
1109aa4dd815SMatthias Ringwald     return 0;
1110aa4dd815SMatthias Ringwald }
1111a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1112a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1113aa4dd815SMatthias Ringwald     int indicator_index;
1114d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1115d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1116d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
111774386ee0SMatthias Ringwald 
1118d210d9c4SMatthias Ringwald     //printf("hfp_ag_call_sm event %d \n", event);
1119aa4dd815SMatthias Ringwald     switch (event){
1120aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1121d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1122aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1123d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1124aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1125d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1126d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1127aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
1128aa4dd815SMatthias Ringwald                             printf("AG rings\n");
1129aa4dd815SMatthias Ringwald                             break;
1130aa4dd815SMatthias Ringwald                         default:
11313deb3ec6SMatthias Ringwald                             break;
11323deb3ec6SMatthias Ringwald                     }
11333deb3ec6SMatthias Ringwald                     break;
1134aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1135d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1136aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1137d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1138d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1139aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
1140aa4dd815SMatthias Ringwald                             printf("AG call waiting\n");
1141aa4dd815SMatthias Ringwald                             break;
1142aa4dd815SMatthias Ringwald                         default:
11433deb3ec6SMatthias Ringwald                             break;
11443deb3ec6SMatthias Ringwald                     }
11453deb3ec6SMatthias Ringwald                     break;
1146aa4dd815SMatthias Ringwald             }
1147aa4dd815SMatthias Ringwald             break;
1148aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1149d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1150aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1151d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1152aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1153d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1154d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1155d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1156aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
1157aa4dd815SMatthias Ringwald                             printf("AG answers call, accept call by GSM\n");
1158aa4dd815SMatthias Ringwald                             break;
1159aa4dd815SMatthias Ringwald                         default:
11603deb3ec6SMatthias Ringwald                             break;
11613deb3ec6SMatthias Ringwald                     }
1162aa4dd815SMatthias Ringwald                     break;
1163aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1164d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1165aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1166aa4dd815SMatthias Ringwald                             printf("AG: current call is placed on hold, incoming call gets active\n");
1167d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1168d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1169d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1170ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1171ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1172aa4dd815SMatthias Ringwald                             break;
1173aa4dd815SMatthias Ringwald                         default:
1174aa4dd815SMatthias Ringwald                             break;
1175aa4dd815SMatthias Ringwald                     }
1176aa4dd815SMatthias Ringwald                     break;
1177aa4dd815SMatthias Ringwald             }
1178aa4dd815SMatthias Ringwald             break;
1179ce263fc8SMatthias Ringwald 
1180ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1181d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1182ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1183d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1184ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
1185ce263fc8SMatthias Ringwald                             printf("AG: joining held call with active call\n");
1186d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG);
1187d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1188ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1189a0ffb263SMatthias Ringwald                             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL);
1190ce263fc8SMatthias Ringwald                             break;
1191ce263fc8SMatthias Ringwald                         default:
1192ce263fc8SMatthias Ringwald                             break;
1193ce263fc8SMatthias Ringwald                     }
1194ce263fc8SMatthias Ringwald                     break;
1195ce263fc8SMatthias Ringwald                 default:
1196ce263fc8SMatthias Ringwald                     break;
1197ce263fc8SMatthias Ringwald             }
1198ce263fc8SMatthias Ringwald             break;
1199ce263fc8SMatthias Ringwald 
1200aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1201d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1202aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1203d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1204aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1205d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF);
1206d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1207d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1208a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
1209aa4dd815SMatthias Ringwald                             printf("HF answers call, accept call by GSM\n");
1210a0ffb263SMatthias Ringwald                             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_ANSWERED);
1211aa4dd815SMatthias Ringwald                             break;
1212aa4dd815SMatthias Ringwald                         default:
1213aa4dd815SMatthias Ringwald                             break;
1214aa4dd815SMatthias Ringwald                     }
12153deb3ec6SMatthias Ringwald                     break;
12163deb3ec6SMatthias Ringwald                 default:
12173deb3ec6SMatthias Ringwald                     break;
12183deb3ec6SMatthias Ringwald             }
12193deb3ec6SMatthias Ringwald             break;
12203deb3ec6SMatthias Ringwald 
1221ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1222d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1223ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1224d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1225ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1226d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG);
1227ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1228ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1229a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1230ce263fc8SMatthias Ringwald                             // as with regualr call
1231d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1232d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1233ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
1234ce263fc8SMatthias Ringwald                             printf("AG response and hold - hold by AG\n");
1235ce263fc8SMatthias Ringwald                             break;
1236ce263fc8SMatthias Ringwald                         default:
1237ce263fc8SMatthias Ringwald                             break;
1238ce263fc8SMatthias Ringwald                     }
1239ce263fc8SMatthias Ringwald                     break;
1240ce263fc8SMatthias Ringwald                 default:
1241ce263fc8SMatthias Ringwald                     break;
1242ce263fc8SMatthias Ringwald             }
1243ce263fc8SMatthias Ringwald             break;
1244ce263fc8SMatthias Ringwald 
1245ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1246d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1247ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1248d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1249ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1250d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF);
1251ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1252ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1253a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1254ce263fc8SMatthias Ringwald                             // as with regualr call
1255d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1256d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1257a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
1258ce263fc8SMatthias Ringwald                             printf("AG response and hold - hold by HF\n");
1259ce263fc8SMatthias Ringwald                             break;
1260ce263fc8SMatthias Ringwald                         default:
1261ce263fc8SMatthias Ringwald                             break;
1262ce263fc8SMatthias Ringwald                     }
1263ce263fc8SMatthias Ringwald                     break;
1264ce263fc8SMatthias Ringwald                 default:
1265ce263fc8SMatthias Ringwald                     break;
1266ce263fc8SMatthias Ringwald             }
1267ce263fc8SMatthias Ringwald             break;
1268ce263fc8SMatthias Ringwald 
1269ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1270ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1271ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1272ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1273d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG);
1274a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1275ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1276a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1277ce263fc8SMatthias Ringwald             printf("Held Call accepted and active\n");
1278ce263fc8SMatthias Ringwald             break;
1279ce263fc8SMatthias Ringwald 
1280ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1281ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1282ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1283ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1284d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG);
1285a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1286ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1287a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1288ce263fc8SMatthias Ringwald             // from terminate by ag
1289d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1290ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1291ce263fc8SMatthias Ringwald             break;
1292ce263fc8SMatthias Ringwald 
1293aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1294d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1295aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1296d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1297aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1298d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1299d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1300aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1301aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
1302aa4dd815SMatthias Ringwald                             printf("HF Rejected Incoming call, AG terminate call\n");
1303aa4dd815SMatthias Ringwald                             break;
1304aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1305aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1306d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1307d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1308aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1309aa4dd815SMatthias Ringwald                             printf("AG terminate outgoing call process\n");
1310aa4dd815SMatthias Ringwald                         default:
1311aa4dd815SMatthias Ringwald                             break;
1312aa4dd815SMatthias Ringwald                     }
1313aa4dd815SMatthias Ringwald                     break;
1314aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1315d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1316d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1317aa4dd815SMatthias Ringwald                     hfp_ag_transfer_call_state();
1318a0ffb263SMatthias Ringwald                     hfp_connection->call_state = HFP_CALL_IDLE;
1319aa4dd815SMatthias Ringwald                     printf("AG terminate call\n");
1320aa4dd815SMatthias Ringwald                     break;
1321aa4dd815SMatthias Ringwald             }
1322aa4dd815SMatthias Ringwald             break;
13233deb3ec6SMatthias Ringwald 
1324aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1325d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1326aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1327d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1328aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1329d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1330d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1331aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
1332aa4dd815SMatthias Ringwald                             printf("AG Rejected Incoming call, AG terminate call\n");
1333aa4dd815SMatthias Ringwald                             break;
1334aa4dd815SMatthias Ringwald                         default:
1335aa4dd815SMatthias Ringwald                             break;
13363deb3ec6SMatthias Ringwald                     }
1337aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1338d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1339d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1340d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1341aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1342aa4dd815SMatthias Ringwald                     printf("AG terminate call\n");
1343aa4dd815SMatthias Ringwald                     break;
1344aa4dd815SMatthias Ringwald                 default:
13453deb3ec6SMatthias Ringwald                     break;
13463deb3ec6SMatthias Ringwald             }
13473deb3ec6SMatthias Ringwald             break;
1348aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1349d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1350aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1351d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1352aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1353aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
1354aa4dd815SMatthias Ringwald                             printf("Incoming call interrupted\n");
1355aa4dd815SMatthias Ringwald                             break;
1356aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1357aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1358aa4dd815SMatthias Ringwald                             printf("Outgoing call interrupted\n");
1359aa4dd815SMatthias Ringwald                             printf("AG notify call dropped\n");
1360aa4dd815SMatthias Ringwald                             break;
1361aa4dd815SMatthias Ringwald                         default:
13623deb3ec6SMatthias Ringwald                             break;
13633deb3ec6SMatthias Ringwald                     }
1364d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1365d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1366aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
1367aa4dd815SMatthias Ringwald                     break;
1368aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1369ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1370d210d9c4SMatthias Ringwald                         hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1371ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1372a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1373ce263fc8SMatthias Ringwald                     }
1374d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1375d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1376d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1377aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1378aa4dd815SMatthias Ringwald                     printf("AG notify call dropped\n");
1379aa4dd815SMatthias Ringwald                     break;
1380aa4dd815SMatthias Ringwald                 default:
1381aa4dd815SMatthias Ringwald                     break;
1382aa4dd815SMatthias Ringwald             }
1383aa4dd815SMatthias Ringwald             break;
1384aa4dd815SMatthias Ringwald 
1385aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1386d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1387d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1388a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1389a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1390d210d9c4SMatthias Ringwald                 break;
1391d210d9c4SMatthias Ringwald             }
1392a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]);
13939cae807eSMatthias Ringwald 
1394a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
139574386ee0SMatthias Ringwald 
1396a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1397aa4dd815SMatthias Ringwald             break;
1398aa4dd815SMatthias Ringwald 
13999cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1400d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1401d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1402a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1403a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1404d210d9c4SMatthias Ringwald                 break;
1405d210d9c4SMatthias Ringwald             }
1406d210d9c4SMatthias Ringwald 
140774386ee0SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED);
1408a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
140974386ee0SMatthias Ringwald 
14109cae807eSMatthias Ringwald             printf("\nRedial last number");
14119cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1412aa4dd815SMatthias Ringwald 
14139cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
14149cae807eSMatthias Ringwald                 printf("\nLast number exists: accept call");
14159cae807eSMatthias Ringwald                 hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
14169cae807eSMatthias Ringwald             } else {
14179cae807eSMatthias Ringwald                 printf("\nLast number missing: reject call");
14189cae807eSMatthias Ringwald                 hfp_ag_outgoing_call_rejected();
14199cae807eSMatthias Ringwald             }
14209cae807eSMatthias Ringwald             break;
14219cae807eSMatthias Ringwald         }
1422aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
1423a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1424a0ffb263SMatthias Ringwald             if (!hfp_connection){
1425a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1426aa4dd815SMatthias Ringwald                 break;
1427aa4dd815SMatthias Ringwald             }
1428d210d9c4SMatthias Ringwald 
1429d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED);
1430a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1431a0ffb263SMatthias Ringwald             hfp_connection->send_error = 1;
1432a0ffb263SMatthias Ringwald             hfp_run_for_context(hfp_connection);
1433aa4dd815SMatthias Ringwald             break;
1434aa4dd815SMatthias Ringwald 
1435d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1436a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1437a0ffb263SMatthias Ringwald             if (!hfp_connection){
1438a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1439aa4dd815SMatthias Ringwald                 break;
1440aa4dd815SMatthias Ringwald             }
1441aa4dd815SMatthias Ringwald 
1442a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1443a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1444aa4dd815SMatthias Ringwald 
1445aa4dd815SMatthias Ringwald             // trigger callsetup to be
1446d0c20769SMatthias Ringwald             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1447d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED);
1448d210d9c4SMatthias Ringwald 
1449d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1450aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1451a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1452aa4dd815SMatthias Ringwald 
1453aa4dd815SMatthias Ringwald             // put current call on hold if active
1454d210d9c4SMatthias Ringwald             if (put_call_on_hold){
1455aa4dd815SMatthias Ringwald                 printf("AG putting current call on hold for new outgoing call\n");
1456d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1457aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1458a0ffb263SMatthias Ringwald                 hfp_ag_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1459aa4dd815SMatthias Ringwald             }
1460aa4dd815SMatthias Ringwald 
1461aa4dd815SMatthias Ringwald             // start audio if needed
1462a0ffb263SMatthias Ringwald             hfp_ag_establish_audio_connection(hfp_connection->remote_addr);
1463aa4dd815SMatthias Ringwald             break;
1464d210d9c4SMatthias Ringwald         }
1465aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1466a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1467a0ffb263SMatthias Ringwald             if (!hfp_connection){
1468a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1469aa4dd815SMatthias Ringwald                 break;
1470aa4dd815SMatthias Ringwald             }
1471d210d9c4SMatthias Ringwald 
1472d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING);
1473a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1474d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1475aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1476aa4dd815SMatthias Ringwald             break;
1477aa4dd815SMatthias Ringwald 
1478d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1479aa4dd815SMatthias Ringwald             // get outgoing call
1480a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1481a0ffb263SMatthias Ringwald             if (!hfp_connection){
1482a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1483aa4dd815SMatthias Ringwald             }
1484a0ffb263SMatthias Ringwald             if (!hfp_connection){
1485a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1486aa4dd815SMatthias Ringwald                 break;
1487aa4dd815SMatthias Ringwald             }
1488d210d9c4SMatthias Ringwald 
1489d0c20769SMatthias 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;
1490d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED);
1491a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1492d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1493d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1494aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1495aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1496d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1497d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1498aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
14993deb3ec6SMatthias Ringwald             }
15003deb3ec6SMatthias Ringwald             break;
1501d210d9c4SMatthias Ringwald         }
1502d210d9c4SMatthias Ringwald 
1503d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1504d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY);
1505d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1506a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1507a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1508d210d9c4SMatthias Ringwald             printf("AG: Call Waiting, User Busy\n");
1509d210d9c4SMatthias Ringwald             break;
1510d210d9c4SMatthias Ringwald 
1511d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1512d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1513d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1514d210d9c4SMatthias Ringwald 
1515d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1516d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1517a0ffb263SMatthias Ringwald                 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1518d0c20769SMatthias Ringwald 
1519d0c20769SMatthias Ringwald             }
1520d0c20769SMatthias Ringwald 
1521d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
1522d210d9c4SMatthias Ringwald                 printf("AG: Call Dropped, Accept new call\n");
1523d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1524a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1525d210d9c4SMatthias Ringwald             } else {
1526d210d9c4SMatthias Ringwald                 printf("AG: Call Dropped, Resume held call\n");
1527d210d9c4SMatthias Ringwald             }
1528d210d9c4SMatthias Ringwald             if (call_held){
1529d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1530a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1531d210d9c4SMatthias Ringwald             }
1532d0c20769SMatthias Ringwald 
1533a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1534d210d9c4SMatthias Ringwald             break;
1535d210d9c4SMatthias Ringwald         }
1536d0c20769SMatthias Ringwald 
1537d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1538d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1539d210d9c4SMatthias Ringwald             // only update if callsetup changed
1540d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1541a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1542d0c20769SMatthias Ringwald 
1543d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
1544d210d9c4SMatthias Ringwald                 printf("AG: Call on Hold, Accept new call\n");
1545d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1546a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1547d210d9c4SMatthias Ringwald             } else {
1548d210d9c4SMatthias Ringwald                 printf("AG: Swap calls\n");
1549d210d9c4SMatthias Ringwald             }
1550d0c20769SMatthias Ringwald 
1551d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1552d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1553a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1554a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1555d210d9c4SMatthias Ringwald             break;
1556d210d9c4SMatthias Ringwald         }
1557d0c20769SMatthias Ringwald 
1558d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1559d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1560d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
1561d210d9c4SMatthias Ringwald                 printf("AG: Join 3-way-call\n");
1562d210d9c4SMatthias Ringwald                 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL);
1563d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1564a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1565a0ffb263SMatthias Ringwald                 hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL);
1566d210d9c4SMatthias Ringwald             }
1567a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1568d210d9c4SMatthias Ringwald             break;
1569d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1570d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1571d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS);
1572d210d9c4SMatthias Ringwald             printf("AG: Transfer call -> Connect two calls and disconnect\n");
1573d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1574d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1575a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1576a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1577a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1578d210d9c4SMatthias Ringwald             break;
1579ce263fc8SMatthias Ringwald 
15803deb3ec6SMatthias Ringwald         default:
15813deb3ec6SMatthias Ringwald             break;
15823deb3ec6SMatthias Ringwald     }
1583d210d9c4SMatthias Ringwald 
1584d0c20769SMatthias Ringwald 
15853deb3ec6SMatthias Ringwald }
15863deb3ec6SMatthias Ringwald 
15879cae807eSMatthias Ringwald 
1588a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
15899cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
15909cae807eSMatthias Ringwald     if (!active_call) return;
15919cae807eSMatthias Ringwald 
15929cae807eSMatthias Ringwald     int idx = active_call->index;
15939cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
15949cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
15959cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
15969cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
15979cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
15989cae807eSMatthias Ringwald     char * number = active_call->clip_number;
15999cae807eSMatthias Ringwald 
16009cae807eSMatthias Ringwald     char buffer[100];
16019cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
16029cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
16039cae807eSMatthias Ringwald     if (number){
16049cae807eSMatthias Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset, ", \"%s\",%u", number, type);
16059cae807eSMatthias Ringwald     }
16069cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
16079cae807eSMatthias Ringwald     printf("hfp_ag_send_current_call_status 000 index %d, dir %d, status %d, mode %d, mpty %d, type %d, number %s\n", idx, dir, status,
16089cae807eSMatthias Ringwald        mode, mpty, type, number);
1609a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
16109cae807eSMatthias Ringwald }
16119cae807eSMatthias Ringwald 
1612a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection){
1613473ac565SMatthias Ringwald 
1614473ac565SMatthias Ringwald     log_info("hfp_run_for_context %p", hfp_connection);
1615473ac565SMatthias Ringwald 
1616a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1617473ac565SMatthias Ringwald 
1618724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
1619473ac565SMatthias Ringwald 
1620e30a6a47SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
1621473ac565SMatthias Ringwald         log_info("hfp_run_for_context: request can send for 0x%02x", hfp_connection->rfcomm_cid);
1622e30a6a47SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1623e30a6a47SMatthias Ringwald         return;
1624e30a6a47SMatthias Ringwald     }
16253deb3ec6SMatthias Ringwald 
1626a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1627a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1628a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1629a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1630a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
16319cae807eSMatthias Ringwald         } else {
1632a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1633a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1634a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16359cae807eSMatthias Ringwald         }
1636ce263fc8SMatthias Ringwald         return;
1637ce263fc8SMatthias Ringwald     }
1638ce263fc8SMatthias Ringwald 
1639a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1640a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1641a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1642aa4dd815SMatthias Ringwald         return;
1643aa4dd815SMatthias Ringwald     }
1644aa4dd815SMatthias Ringwald 
1645a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1646a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1647a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1648a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1649a0ffb263SMatthias Ringwald         hfp_ag_error(hfp_connection->rfcomm_cid);
1650a0ffb263SMatthias Ringwald         return;
1651a0ffb263SMatthias Ringwald     }
1652a0ffb263SMatthias Ringwald 
1653a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1654a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1655a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1656a0ffb263SMatthias Ringwald         hfp_ag_error(hfp_connection->rfcomm_cid);
1657aa4dd815SMatthias Ringwald         return;
1658aa4dd815SMatthias Ringwald     }
1659aa4dd815SMatthias Ringwald 
1660ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1661a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1662a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1663a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1664a0ffb263SMatthias Ringwald         hfp_ag_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1665ce263fc8SMatthias Ringwald         return;
1666ce263fc8SMatthias Ringwald     }
1667ce263fc8SMatthias Ringwald 
1668a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1669a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1670a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1671a0ffb263SMatthias Ringwald         hfp_ag_ok(hfp_connection->rfcomm_cid);
1672ce263fc8SMatthias Ringwald         return;
1673ce263fc8SMatthias Ringwald     }
1674ce263fc8SMatthias Ringwald 
1675aa4dd815SMatthias Ringwald     // update AG indicators
1676a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1677aa4dd815SMatthias Ringwald         int i;
1678a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1679a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1680a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1681a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1682ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1683ce263fc8SMatthias Ringwald                     break;
1684ce263fc8SMatthias Ringwald                 }
1685a0ffb263SMatthias Ringwald                 hfp_ag_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1686aa4dd815SMatthias Ringwald                 return;
1687aa4dd815SMatthias Ringwald             }
1688aa4dd815SMatthias Ringwald         }
1689aa4dd815SMatthias Ringwald     }
1690aa4dd815SMatthias Ringwald 
1691a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1692a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1693a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1694a0ffb263SMatthias Ringwald         hfp_ag_ring(hfp_connection->rfcomm_cid);
1695aa4dd815SMatthias Ringwald         return;
1696aa4dd815SMatthias Ringwald     }
1697aa4dd815SMatthias Ringwald 
1698a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1699a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1700a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1701a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1702aa4dd815SMatthias Ringwald         return;
1703aa4dd815SMatthias Ringwald     }
1704aa4dd815SMatthias Ringwald 
1705a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1706a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1707a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1708a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1709a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1710aa4dd815SMatthias Ringwald         return;
1711aa4dd815SMatthias Ringwald     }
1712aa4dd815SMatthias Ringwald 
1713a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1714a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1715a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1716a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1717ce263fc8SMatthias Ringwald         } else {
1718a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1719a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1720a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
1721ce263fc8SMatthias Ringwald         }
1722a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1723ce263fc8SMatthias Ringwald     }
1724ce263fc8SMatthias Ringwald 
1725a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1726a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1727a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1728a0ffb263SMatthias Ringwald         hfp_ag_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1729aa4dd815SMatthias Ringwald         return;
1730aa4dd815SMatthias Ringwald     }
1731aa4dd815SMatthias Ringwald 
1732a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1733a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1734a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1735a0ffb263SMatthias Ringwald         hfp_ag_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
17363deb3ec6SMatthias Ringwald         return;
17373deb3ec6SMatthias Ringwald     }
17383deb3ec6SMatthias Ringwald 
1739a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1740a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1741a0ffb263SMatthias Ringwald         hfp_ag_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1742ce263fc8SMatthias Ringwald         return;
1743ce263fc8SMatthias Ringwald     }
1744ce263fc8SMatthias Ringwald 
1745a0ffb263SMatthias Ringwald     int done = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1746aa4dd815SMatthias Ringwald     if (!done){
1747a0ffb263SMatthias Ringwald         done = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
17483deb3ec6SMatthias Ringwald     }
17493deb3ec6SMatthias Ringwald 
1750aa4dd815SMatthias Ringwald     if (!done){
1751a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
1752aa4dd815SMatthias Ringwald     }
1753aa4dd815SMatthias Ringwald 
1754aa4dd815SMatthias Ringwald     if (!done){
1755a0ffb263SMatthias Ringwald         done = hfp_ag_run_for_audio_connection(hfp_connection);
1756aa4dd815SMatthias Ringwald     }
17573deb3ec6SMatthias Ringwald 
1758a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_NONE && !done){
1759a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1760a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
17613deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1762a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1763a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
17643deb3ec6SMatthias Ringwald                 break;
17653deb3ec6SMatthias Ringwald             default:
17663deb3ec6SMatthias Ringwald                 break;
17673deb3ec6SMatthias Ringwald         }
17683deb3ec6SMatthias Ringwald     }
17693deb3ec6SMatthias Ringwald     if (done){
1770a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
17713deb3ec6SMatthias Ringwald     }
1772473ac565SMatthias Ringwald     //
1773473ac565SMatthias Ringwald     if (done) {
1774473ac565SMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
1775473ac565SMatthias Ringwald     }
17763deb3ec6SMatthias Ringwald }
1777d68dcce1SMatthias Ringwald 
1778ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1779ce263fc8SMatthias Ringwald     int i;
1780a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1781a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1782ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1783ce263fc8SMatthias Ringwald             return indicator;
1784ce263fc8SMatthias Ringwald         }
1785ce263fc8SMatthias Ringwald     }
1786ce263fc8SMatthias Ringwald     return NULL;
1787ce263fc8SMatthias Ringwald }
17883deb3ec6SMatthias Ringwald 
1789aa4dd815SMatthias Ringwald static void hfp_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1790a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1791a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
17921e35c04dSMatthias Ringwald 
17931e35c04dSMatthias Ringwald     char last_char = packet[size-1];
17941e35c04dSMatthias Ringwald     packet[size-1] = 0;
17951e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
17961e35c04dSMatthias Ringwald     packet[size-1] = last_char;
17971e35c04dSMatthias Ringwald 
17983deb3ec6SMatthias Ringwald     int pos;
17993deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1800a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1801aa4dd815SMatthias Ringwald     }
1802ce263fc8SMatthias Ringwald     hfp_generic_status_indicator_t * indicator;
1803ce263fc8SMatthias Ringwald     int value;
1804a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
1805ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1806ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_active){
1807a0ffb263SMatthias Ringwald                 hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1808ce263fc8SMatthias Ringwald             }
1809a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1810ce263fc8SMatthias Ringwald             break;
1811ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
1812a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1813ce263fc8SMatthias Ringwald             printf("HF Response and Hold: %u\n", value);
1814ce263fc8SMatthias Ringwald             switch(value){
1815ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1816a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1817ce263fc8SMatthias Ringwald                     break;
1818ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1819a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1820ce263fc8SMatthias Ringwald                     break;
1821ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1822a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1823ce263fc8SMatthias Ringwald                     break;
1824ce263fc8SMatthias Ringwald                 default:
1825ce263fc8SMatthias Ringwald                     break;
1826ce263fc8SMatthias Ringwald             }
1827a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1828ce263fc8SMatthias Ringwald             break;
1829ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
1830a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1831ce263fc8SMatthias Ringwald             // find indicator by assigned number
1832a0ffb263SMatthias Ringwald             indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1833ce263fc8SMatthias Ringwald             if (!indicator){
1834a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1835ce263fc8SMatthias Ringwald                 break;
1836ce263fc8SMatthias Ringwald             }
1837a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1838ce263fc8SMatthias Ringwald             switch (indicator->uuid){
1839ce263fc8SMatthias Ringwald                 case 1: // enhanced security
1840ce263fc8SMatthias Ringwald                     if (value > 1) {
1841a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1842ce263fc8SMatthias Ringwald                         return;
1843ce263fc8SMatthias Ringwald                     }
1844ce263fc8SMatthias Ringwald                     printf("HF Indicator 'enhanced security' set to %u\n", value);
1845ce263fc8SMatthias Ringwald                     break;
1846ce263fc8SMatthias Ringwald                 case 2: // battery level
1847ce263fc8SMatthias Ringwald                     if (value > 100){
1848a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1849ce263fc8SMatthias Ringwald                         return;
1850ce263fc8SMatthias Ringwald                     }
1851ce263fc8SMatthias Ringwald                     printf("HF Indicator 'battery' set to %u\n", value);
1852ce263fc8SMatthias Ringwald                     break;
1853ce263fc8SMatthias Ringwald                 default:
1854ce263fc8SMatthias Ringwald                     printf("HF Indicator unknown set to %u\n", value);
1855ce263fc8SMatthias Ringwald                     break;
1856ce263fc8SMatthias Ringwald             }
1857a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1858ce263fc8SMatthias Ringwald             break;
1859ce263fc8SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1860ce263fc8SMatthias Ringwald             // expected by SLC state machine
1861a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1862a0ffb263SMatthias Ringwald             hfp_connection->send_ag_indicators_segment = 0;
1863a0ffb263SMatthias Ringwald             hfp_connection->send_ag_status_indicators = 1;
1864ce263fc8SMatthias Ringwald             break;
1865ce263fc8SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1866a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1867a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1868a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 1;
1869ce263fc8SMatthias Ringwald             break;
1870ce263fc8SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1871ce263fc8SMatthias Ringwald             if (subscriber_numbers_count == 0){
1872a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
1873ce263fc8SMatthias Ringwald                 break;
1874ce263fc8SMatthias Ringwald             }
1875a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1876a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 1;
1877ce263fc8SMatthias Ringwald             break;
1878c1797c7dSMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
1879a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1880a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]);
1881c1797c7dSMatthias Ringwald             break;
1882aa4dd815SMatthias Ringwald         case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1883a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1884a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1885aa4dd815SMatthias Ringwald             break;
1886aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1887a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1888aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1889a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1890a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
1891a0ffb263SMatthias Ringwald                 printf("AG: EC/NR = %u\n", hfp_connection->ag_echo_and_noise_reduction);
1892aa4dd815SMatthias Ringwald             } else {
1893a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1894aa4dd815SMatthias Ringwald             }
1895aa4dd815SMatthias Ringwald             break;
1896aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_ANSWERED:
1897a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1898aa4dd815SMatthias Ringwald             printf("HFP: ATA\n");
1899a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1900aa4dd815SMatthias Ringwald             break;
1901aa4dd815SMatthias Ringwald         case HFP_CMD_HANG_UP_CALL:
1902a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1903a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1904a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1905aa4dd815SMatthias Ringwald             break;
1906aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_HOLD: {
1907aa4dd815SMatthias Ringwald             // TODO: fully implement this
1908a0ffb263SMatthias Ringwald             log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]);
1909a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1910a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1911a0ffb263SMatthias Ringwald             hfp_connection->call_index = 0;
1912d0c20769SMatthias Ringwald 
1913a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
1914a0ffb263SMatthias Ringwald                 hfp_connection->call_index = atoi((char *)&hfp_connection->line_buffer[1]);
1915d0c20769SMatthias Ringwald             }
1916d210d9c4SMatthias Ringwald 
1917a0ffb263SMatthias Ringwald             switch (hfp_connection->line_buffer[0]){
1918aa4dd815SMatthias Ringwald                 case '0':
1919d0c20769SMatthias Ringwald                     // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1920a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1921aa4dd815SMatthias Ringwald                     break;
1922aa4dd815SMatthias Ringwald                 case '1':
1923d0c20769SMatthias Ringwald                     // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1924d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1925d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1926a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1927aa4dd815SMatthias Ringwald                     break;
1928aa4dd815SMatthias Ringwald                 case '2':
1929d0c20769SMatthias Ringwald                     // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1930d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1931d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1932a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1933aa4dd815SMatthias Ringwald                     break;
1934aa4dd815SMatthias Ringwald                 case '3':
1935d0c20769SMatthias Ringwald                     // Adds a held call to the conversation.
1936a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1937aa4dd815SMatthias Ringwald                     break;
1938aa4dd815SMatthias Ringwald                 case '4':
1939d0c20769SMatthias Ringwald                     // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1940a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1941aa4dd815SMatthias Ringwald                     break;
1942aa4dd815SMatthias Ringwald                 default:
1943aa4dd815SMatthias Ringwald                     break;
1944aa4dd815SMatthias Ringwald             }
1945aa4dd815SMatthias Ringwald             break;
1946aa4dd815SMatthias Ringwald         }
1947aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_PHONE_NUMBER:
1948a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1949a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
1950aa4dd815SMatthias Ringwald             break;
1951aa4dd815SMatthias Ringwald         case HFP_CMD_REDIAL_LAST_NUMBER:
1952a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1953a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
1954aa4dd815SMatthias Ringwald             break;
1955aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
1956a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1957a0ffb263SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0';
1958a0ffb263SMatthias Ringwald             log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
1959a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1960aa4dd815SMatthias Ringwald             break;
1961aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1962a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1963a0ffb263SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0';
1964a0ffb263SMatthias Ringwald             log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
1965a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1966aa4dd815SMatthias Ringwald             break;
1967aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1968a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1969a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1970a0ffb263SMatthias Ringwald             printf("HF speaker gain = %u\n", hfp_connection->speaker_gain);
1971aa4dd815SMatthias Ringwald             break;
1972aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1973a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1974a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1975a0ffb263SMatthias Ringwald             printf("HF microphone gain = %u\n", hfp_connection->microphone_gain);
1976aa4dd815SMatthias Ringwald             break;
1977aa4dd815SMatthias Ringwald         default:
1978aa4dd815SMatthias Ringwald             break;
19793deb3ec6SMatthias Ringwald     }
19803deb3ec6SMatthias Ringwald }
19813deb3ec6SMatthias Ringwald 
1982aa4dd815SMatthias Ringwald static void hfp_run(void){
1983d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1984d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1985d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1986d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1987a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
19883deb3ec6SMatthias Ringwald     }
19893deb3ec6SMatthias Ringwald }
19903deb3ec6SMatthias Ringwald 
1991ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
19923deb3ec6SMatthias Ringwald     switch (packet_type){
19933deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1994aa4dd815SMatthias Ringwald             hfp_handle_rfcomm_data(packet_type, channel, packet, size);
19953deb3ec6SMatthias Ringwald             break;
19963deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1997e30a6a47SMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1998e30a6a47SMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
1999e30a6a47SMatthias Ringwald                 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
2000e30a6a47SMatthias Ringwald                 return;
2001e30a6a47SMatthias Ringwald             }
2002e30a6a47SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size);
2003aa4dd815SMatthias Ringwald             break;
20043deb3ec6SMatthias Ringwald         default:
20053deb3ec6SMatthias Ringwald             break;
20063deb3ec6SMatthias Ringwald     }
20073deb3ec6SMatthias Ringwald 
20083deb3ec6SMatthias Ringwald     hfp_run();
20093deb3ec6SMatthias Ringwald }
20103deb3ec6SMatthias Ringwald 
20111e35c04dSMatthias Ringwald 
2012a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
20133deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
20143deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
20153deb3ec6SMatthias Ringwald         return;
20163deb3ec6SMatthias Ringwald     }
20173deb3ec6SMatthias Ringwald     int i;
2018a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
20193deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
20203deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
20213deb3ec6SMatthias Ringwald     }
2022a0ffb263SMatthias Ringwald }
20233deb3ec6SMatthias Ringwald 
2024a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2025a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2026a0ffb263SMatthias Ringwald }
20273deb3ec6SMatthias Ringwald 
2028a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2029a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
2030a0ffb263SMatthias Ringwald     memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2031a0ffb263SMatthias Ringwald }
20323deb3ec6SMatthias Ringwald 
2033a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
2034a0ffb263SMatthias Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return;
2035a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
2036a0ffb263SMatthias Ringwald     memcpy(hfp_generic_status_indicators, hf_indicators, hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2037a0ffb263SMatthias Ringwald }
2038a0ffb263SMatthias Ringwald 
2039a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
20403deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
20413deb3ec6SMatthias Ringwald     memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *));
2042a0ffb263SMatthias Ringwald }
2043a0ffb263SMatthias Ringwald 
2044a0ffb263SMatthias Ringwald 
2045a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
2046d63c37a1SMatthias Ringwald     // register for HCI events
2047d63c37a1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
2048d63c37a1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
2049d63c37a1SMatthias Ringwald 
2050d68dcce1SMatthias Ringwald     rfcomm_register_service(&packet_handler, rfcomm_channel_nr, 0xffff);
2051aa4dd815SMatthias Ringwald 
2052d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2053d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2054d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2055d210d9c4SMatthias Ringwald 
2056d68dcce1SMatthias Ringwald     hfp_set_packet_handler_for_rfcomm_connections(&packet_handler);
2057d68dcce1SMatthias Ringwald 
2058d210d9c4SMatthias Ringwald     hfp_gsm_init();
20593deb3ec6SMatthias Ringwald }
20603deb3ec6SMatthias Ringwald 
20613deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
20623deb3ec6SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, SDP_Handsfree);
20633deb3ec6SMatthias Ringwald }
20643deb3ec6SMatthias Ringwald 
20653deb3ec6SMatthias Ringwald void hfp_ag_release_service_level_connection(bd_addr_t bd_addr){
2066a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2067a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2068a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20693deb3ec6SMatthias Ringwald }
20703deb3ec6SMatthias Ringwald 
20713deb3ec6SMatthias Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, hfp_cme_error_t error){
2072a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2073a0ffb263SMatthias Ringwald     if (!hfp_connection){
2074a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
20753deb3ec6SMatthias Ringwald         return;
20763deb3ec6SMatthias Ringwald     }
2077a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2078a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
20793deb3ec6SMatthias Ringwald         return;
20803deb3ec6SMatthias Ringwald     }
2081a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2082a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20833deb3ec6SMatthias Ringwald }
20843deb3ec6SMatthias Ringwald 
2085a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
2086a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2087a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
20883deb3ec6SMatthias Ringwald 
2089a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2090aa4dd815SMatthias Ringwald 
2091a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2092aa4dd815SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
2093a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
20943deb3ec6SMatthias Ringwald     }
2095aa4dd815SMatthias Ringwald 
2096a0ffb263SMatthias Ringwald     switch (hfp_connection->codecs_state){
2097aa4dd815SMatthias Ringwald         case HFP_CODECS_IDLE:
2098aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_LIST:
2099aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
2100aa4dd815SMatthias Ringwald         case HFP_CODECS_ERROR:
2101a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
2102aa4dd815SMatthias Ringwald             break;
2103aa4dd815SMatthias Ringwald         default:
2104aa4dd815SMatthias Ringwald             break;
2105aa4dd815SMatthias Ringwald     }
2106aa4dd815SMatthias Ringwald }
2107aa4dd815SMatthias Ringwald 
2108aa4dd815SMatthias Ringwald void hfp_ag_establish_audio_connection(bd_addr_t bd_addr){
2109aa4dd815SMatthias Ringwald     hfp_ag_establish_service_level_connection(bd_addr);
2110a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2111aa4dd815SMatthias Ringwald 
2112a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2113a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2114a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21153deb3ec6SMatthias Ringwald }
21163deb3ec6SMatthias Ringwald 
21173deb3ec6SMatthias Ringwald void hfp_ag_release_audio_connection(bd_addr_t bd_addr){
2118a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2119a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2120a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
21213deb3ec6SMatthias Ringwald }
2122aa4dd815SMatthias Ringwald 
2123aa4dd815SMatthias Ringwald /**
2124aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2125aa4dd815SMatthias Ringwald  */
2126aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2127aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2128aa4dd815SMatthias Ringwald         return;
2129aa4dd815SMatthias Ringwald     }
2130aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2131aa4dd815SMatthias Ringwald 
2132665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2133665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2134665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2135a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2136a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2137a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2138aa4dd815SMatthias Ringwald     }
2139aa4dd815SMatthias Ringwald }
2140aa4dd815SMatthias Ringwald 
2141aa4dd815SMatthias Ringwald /**
2142aa4dd815SMatthias Ringwald  * @brief Called from GSM
2143aa4dd815SMatthias Ringwald  */
2144aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2145aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2146aa4dd815SMatthias Ringwald }
2147aa4dd815SMatthias Ringwald 
2148aa4dd815SMatthias Ringwald /**
2149aa4dd815SMatthias Ringwald  * @brief number is stored.
2150aa4dd815SMatthias Ringwald  */
2151aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2152d0c20769SMatthias Ringwald     hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number);
2153aa4dd815SMatthias Ringwald }
2154aa4dd815SMatthias Ringwald 
2155aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2156aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2157aa4dd815SMatthias Ringwald }
2158aa4dd815SMatthias Ringwald 
2159aa4dd815SMatthias Ringwald // call from AG UI
2160aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2161aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2162aa4dd815SMatthias Ringwald }
2163aa4dd815SMatthias Ringwald 
2164ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2165ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2166ce263fc8SMatthias Ringwald }
2167ce263fc8SMatthias Ringwald 
2168aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2169aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2170aa4dd815SMatthias Ringwald }
2171aa4dd815SMatthias Ringwald 
2172aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2173aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2174aa4dd815SMatthias Ringwald }
2175aa4dd815SMatthias Ringwald 
2176aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2177aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2178aa4dd815SMatthias Ringwald }
2179aa4dd815SMatthias Ringwald 
2180aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2181aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2182aa4dd815SMatthias Ringwald }
2183aa4dd815SMatthias Ringwald 
2184aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2185aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2186aa4dd815SMatthias Ringwald }
2187ce263fc8SMatthias Ringwald 
2188ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2189ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2190ce263fc8SMatthias Ringwald }
2191ce263fc8SMatthias Ringwald 
2192ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2193ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2194ce263fc8SMatthias Ringwald }
2195ce263fc8SMatthias Ringwald 
2196ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2197ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2198ce263fc8SMatthias Ringwald }
2199ce263fc8SMatthias Ringwald 
2200aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2201aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2202aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2203aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2204aa4dd815SMatthias Ringwald 
2205ce263fc8SMatthias Ringwald 
2206665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2207665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2208665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2209a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2210a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2211ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2212ce263fc8SMatthias Ringwald             continue;
2213ce263fc8SMatthias Ringwald         }
2214ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2215a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2216a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2217aa4dd815SMatthias Ringwald     }
2218aa4dd815SMatthias Ringwald }
2219aa4dd815SMatthias Ringwald 
2220aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2221aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2222aa4dd815SMatthias Ringwald }
2223aa4dd815SMatthias Ringwald 
2224aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2225aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2226aa4dd815SMatthias Ringwald }
2227aa4dd815SMatthias Ringwald 
2228aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2229aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2230aa4dd815SMatthias Ringwald }
2231aa4dd815SMatthias Ringwald 
2232aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2233aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2234aa4dd815SMatthias Ringwald }
2235aa4dd815SMatthias Ringwald 
2236aa4dd815SMatthias Ringwald void hfp_ag_activate_voice_recognition(bd_addr_t bd_addr, int activate){
2237aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
2238a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2239aa4dd815SMatthias Ringwald 
2240a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
2241aa4dd815SMatthias Ringwald         printf("AG cannot acivate voice recognition - not supported by HF\n");
2242aa4dd815SMatthias Ringwald         return;
2243aa4dd815SMatthias Ringwald     }
2244aa4dd815SMatthias Ringwald 
2245aa4dd815SMatthias Ringwald     if (activate){
2246aa4dd815SMatthias Ringwald         hfp_ag_establish_audio_connection(bd_addr);
2247aa4dd815SMatthias Ringwald     }
2248aa4dd815SMatthias Ringwald 
2249a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2250a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2251a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2252aa4dd815SMatthias Ringwald }
2253aa4dd815SMatthias Ringwald 
2254aa4dd815SMatthias Ringwald void hfp_ag_set_microphone_gain(bd_addr_t bd_addr, int gain){
2255a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2256a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2257a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2258a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2259a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2260aa4dd815SMatthias Ringwald     }
2261a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2262aa4dd815SMatthias Ringwald }
2263aa4dd815SMatthias Ringwald 
2264aa4dd815SMatthias Ringwald void hfp_ag_set_speaker_gain(bd_addr_t bd_addr, int gain){
2265a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2266a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2267a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2268a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2269aa4dd815SMatthias Ringwald     }
2270a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2271aa4dd815SMatthias Ringwald }
2272aa4dd815SMatthias Ringwald 
2273aa4dd815SMatthias Ringwald void hfp_ag_send_phone_number_for_voice_tag(bd_addr_t bd_addr, const char * number){
2274a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2275aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2276a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2277aa4dd815SMatthias Ringwald }
2278aa4dd815SMatthias Ringwald 
2279aa4dd815SMatthias Ringwald void hfp_ag_reject_phone_number_for_voice_tag(bd_addr_t bd_addr){
2280a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2281a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2282aa4dd815SMatthias Ringwald }
2283aa4dd815SMatthias Ringwald 
2284c1797c7dSMatthias Ringwald void hfp_ag_send_dtmf_code_done(bd_addr_t bd_addr){
2285a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2286a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2287c1797c7dSMatthias Ringwald }
2288aa4dd815SMatthias Ringwald 
2289ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2290ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2291ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2292ce263fc8SMatthias Ringwald }
2293ce263fc8SMatthias Ringwald 
22949cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
22959cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2296ce263fc8SMatthias Ringwald }
2297ce263fc8SMatthias Ringwald 
2298a0ffb263SMatthias Ringwald void hfp_ag_notify_incoming_call_waiting(bd_addr_t bd_addr){
2299a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2300a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2301a0ffb263SMatthias Ringwald 
2302a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2303a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2304a0ffb263SMatthias Ringwald }
2305ce263fc8SMatthias Ringwald 
2306