xref: /btstack/src/classic/hfp_ag.c (revision 59c6af158dbc40c703cd6422bc90ce91f2efa168)
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"
59*59c6af15SMatthias Ringwald #include "classic/core.h"
603edc84c5SMatthias Ringwald #include "classic/hfp.h"
613edc84c5SMatthias Ringwald #include "classic/hfp_ag.h"
62023f2764SMatthias Ringwald #include "classic/hfp_gsm_model.h"
63efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
64023f2764SMatthias Ringwald #include "classic/sdp_server.h"
65023f2764SMatthias Ringwald #include "classic/sdp_util.h"
663deb3ec6SMatthias Ringwald 
67c5b64319SMatthias Ringwald // private prototypes
68c5b64319SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
69c5b64319SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *context);
70c5b64319SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * connection);
71c5b64319SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * context);
72c5b64319SMatthias Ringwald 
73c5b64319SMatthias Ringwald // public prototypes
7435833313SMatthias Ringwald hfp_generic_status_indicator_t * get_hfp_generic_status_indicators(void);
7535833313SMatthias Ringwald int get_hfp_generic_status_indicators_nr(void);
76c5b64319SMatthias Ringwald void set_hfp_generic_status_indicators(hfp_generic_status_indicator_t * indicators, int indicator_nr);
77c5b64319SMatthias Ringwald void set_hfp_ag_indicators(hfp_ag_indicator_t * indicators, int indicator_nr);
78c5b64319SMatthias Ringwald int get_hfp_ag_indicators_nr(hfp_connection_t * context);
79c5b64319SMatthias Ringwald hfp_ag_indicator_t * get_hfp_ag_indicators(hfp_connection_t * context);
80c5b64319SMatthias Ringwald 
81c5b64319SMatthias Ringwald // gobals
823deb3ec6SMatthias Ringwald static const char default_hfp_ag_service_name[] = "Voice gateway";
83aa4dd815SMatthias Ringwald 
843deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_AG_SUPPORTED_FEATURES;
85aa4dd815SMatthias Ringwald 
863deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
873deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
883deb3ec6SMatthias Ringwald 
893deb3ec6SMatthias Ringwald static int  hfp_ag_indicators_nr = 0;
903deb3ec6SMatthias Ringwald static hfp_ag_indicator_t hfp_ag_indicators[HFP_MAX_NUM_AG_INDICATORS];
913deb3ec6SMatthias Ringwald 
92a0ffb263SMatthias Ringwald static int hfp_generic_status_indicators_nr = 0;
93a0ffb263SMatthias Ringwald static hfp_generic_status_indicator_t hfp_generic_status_indicators[HFP_MAX_NUM_HF_INDICATORS];
94a0ffb263SMatthias Ringwald 
953deb3ec6SMatthias Ringwald static int  hfp_ag_call_hold_services_nr = 0;
963deb3ec6SMatthias Ringwald static char *hfp_ag_call_hold_services[6];
973deb3ec6SMatthias Ringwald static hfp_callback_t hfp_callback;
983deb3ec6SMatthias Ringwald 
99ce263fc8SMatthias Ringwald static hfp_response_and_hold_state_t hfp_ag_response_and_hold_state;
100ce263fc8SMatthias Ringwald static int hfp_ag_response_and_hold_active = 0;
101aa4dd815SMatthias Ringwald 
102ce263fc8SMatthias Ringwald // Subcriber information entries
103ce263fc8SMatthias Ringwald static hfp_phone_number_t * subscriber_numbers = NULL;
104ce263fc8SMatthias Ringwald static int subscriber_numbers_count = 0;
105ce263fc8SMatthias Ringwald 
106c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
10799a10067SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection);
10899a10067SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection);
10999a10067SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection);
11099a10067SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection);
1113deb3ec6SMatthias Ringwald 
1123deb3ec6SMatthias Ringwald 
113a0ffb263SMatthias Ringwald static int hfp_ag_get_ag_indicators_nr(hfp_connection_t * hfp_connection){
114a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
115a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
116a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
1173deb3ec6SMatthias Ringwald     }
118a0ffb263SMatthias Ringwald     return hfp_connection->ag_indicators_nr;
119a0ffb263SMatthias Ringwald }
120a0ffb263SMatthias Ringwald 
121a0ffb263SMatthias Ringwald hfp_ag_indicator_t * hfp_ag_get_ag_indicators(hfp_connection_t * hfp_connection){
122a0ffb263SMatthias Ringwald     // TODO: save only value, and value changed in the hfp_connection?
123a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_nr != hfp_ag_indicators_nr){
124a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_nr = hfp_ag_indicators_nr;
125a0ffb263SMatthias Ringwald         memcpy(hfp_connection->ag_indicators, hfp_ag_indicators, hfp_ag_indicators_nr * sizeof(hfp_ag_indicator_t));
126a0ffb263SMatthias Ringwald     }
127a0ffb263SMatthias Ringwald     return (hfp_ag_indicator_t *)&(hfp_connection->ag_indicators);
1283deb3ec6SMatthias Ringwald }
1293deb3ec6SMatthias Ringwald 
130aa4dd815SMatthias Ringwald static hfp_ag_indicator_t * get_ag_indicator_for_name(const char * name){
131aa4dd815SMatthias Ringwald     int i;
132aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
133aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
134aa4dd815SMatthias Ringwald             return &hfp_ag_indicators[i];
135aa4dd815SMatthias Ringwald         }
136aa4dd815SMatthias Ringwald     }
137aa4dd815SMatthias Ringwald     return NULL;
138aa4dd815SMatthias Ringwald }
139aa4dd815SMatthias Ringwald 
140aa4dd815SMatthias Ringwald static int get_ag_indicator_index_for_name(const char * name){
141aa4dd815SMatthias Ringwald     int i;
142aa4dd815SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr; i++){
143aa4dd815SMatthias Ringwald         if (strcmp(hfp_ag_indicators[i].name, name) == 0){
144aa4dd815SMatthias Ringwald             return i;
145aa4dd815SMatthias Ringwald         }
146aa4dd815SMatthias Ringwald     }
147aa4dd815SMatthias Ringwald     return -1;
148aa4dd815SMatthias Ringwald }
149aa4dd815SMatthias Ringwald 
1503deb3ec6SMatthias Ringwald 
1513deb3ec6SMatthias Ringwald void hfp_ag_register_packet_handler(hfp_callback_t callback){
1523deb3ec6SMatthias Ringwald     if (callback == NULL){
1533deb3ec6SMatthias Ringwald         log_error("hfp_ag_register_packet_handler called with NULL callback");
1543deb3ec6SMatthias Ringwald         return;
1553deb3ec6SMatthias Ringwald     }
1563deb3ec6SMatthias Ringwald     hfp_callback = callback;
157f4000eebSMatthias Ringwald     hfp_set_callback(callback);
1583deb3ec6SMatthias Ringwald }
1593deb3ec6SMatthias Ringwald 
1600cb5b971SMatthias Ringwald static int use_in_band_tone(void){
161aa4dd815SMatthias Ringwald     return get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE);
1623deb3ec6SMatthias Ringwald }
1633deb3ec6SMatthias Ringwald 
164a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
165a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
1663deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1673deb3ec6SMatthias Ringwald     return hf && ag;
1683deb3ec6SMatthias Ringwald }
1693deb3ec6SMatthias Ringwald 
170a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
171a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_THREE_WAY_CALLING);
1723deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1733deb3ec6SMatthias Ringwald     return hf && ag;
1743deb3ec6SMatthias Ringwald }
1753deb3ec6SMatthias Ringwald 
176a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
177a0ffb263SMatthias Ringwald     int hf = get_bit(hfp_connection->remote_supported_features, HFP_HFSF_HF_INDICATORS);
1783deb3ec6SMatthias Ringwald     int ag = get_bit(hfp_supported_features, HFP_AGSF_HF_INDICATORS);
1793deb3ec6SMatthias Ringwald     return hf && ag;
1803deb3ec6SMatthias Ringwald }
1813deb3ec6SMatthias Ringwald 
1829b1c3b4dSMatthias 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){
1833deb3ec6SMatthias Ringwald     if (!name){
1843deb3ec6SMatthias Ringwald         name = default_hfp_ag_service_name;
1853deb3ec6SMatthias Ringwald     }
1862ef54b27SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, SDP_HandsfreeAudioGateway, rfcomm_channel_nr, name);
1873deb3ec6SMatthias Ringwald 
1883deb3ec6SMatthias Ringwald     /*
1893deb3ec6SMatthias Ringwald      * 0x01 – Ability to reject a call
1903deb3ec6SMatthias Ringwald      * 0x00 – No ability to reject a call
1913deb3ec6SMatthias Ringwald      */
192aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0301);    // Hands-Free Profile - Network
193aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_8, ability_to_reject_call);
194aa4dd815SMatthias Ringwald 
195aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
196aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
197aa4dd815SMatthias Ringwald }
198aa4dd815SMatthias Ringwald 
199aa4dd815SMatthias Ringwald static int hfp_ag_change_in_band_ring_tone_setting_cmd(uint16_t cid){
200aa4dd815SMatthias Ringwald     char buffer[20];
201aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CHANGE_IN_BAND_RING_TONE_SETTING, use_in_band_tone());
202aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2033deb3ec6SMatthias Ringwald }
2043deb3ec6SMatthias Ringwald 
2053deb3ec6SMatthias Ringwald static int hfp_ag_exchange_supported_features_cmd(uint16_t cid){
2063deb3ec6SMatthias Ringwald     char buffer[40];
2073deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n\r\nOK\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
2083deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2093deb3ec6SMatthias Ringwald }
2103deb3ec6SMatthias Ringwald 
2113deb3ec6SMatthias Ringwald static int hfp_ag_ok(uint16_t cid){
2123deb3ec6SMatthias Ringwald     char buffer[10];
2133deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nOK\r\n");
2143deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2153deb3ec6SMatthias Ringwald }
2163deb3ec6SMatthias Ringwald 
217aa4dd815SMatthias Ringwald static int hfp_ag_ring(uint16_t cid){
218aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, (char *) "\r\nRING\r\n");
219aa4dd815SMatthias Ringwald }
220aa4dd815SMatthias Ringwald 
221aa4dd815SMatthias Ringwald static int hfp_ag_send_clip(uint16_t cid){
222aa4dd815SMatthias Ringwald     char buffer[50];
223d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CLIP, hfp_gsm_clip_number(), hfp_gsm_clip_type());
224aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
225aa4dd815SMatthias Ringwald }
226aa4dd815SMatthias Ringwald 
227ce263fc8SMatthias Ringwald static int hfp_send_subscriber_number_cmd(uint16_t cid, uint8_t type, const char * number){
228ce263fc8SMatthias Ringwald     char buffer[50];
229ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: ,\"%s\",%u, , \r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION, number, type);
230ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
231ce263fc8SMatthias Ringwald }
232ce263fc8SMatthias Ringwald 
233c1797c7dSMatthias Ringwald static int hfp_ag_send_phone_number_for_voice_tag_cmd(uint16_t cid){
234aa4dd815SMatthias Ringwald     char buffer[50];
235d0c20769SMatthias Ringwald     sprintf(buffer, "\r\n%s: %s\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG, hfp_gsm_clip_number());
236aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
237aa4dd815SMatthias Ringwald }
238aa4dd815SMatthias Ringwald 
239aa4dd815SMatthias Ringwald static int hfp_ag_send_call_waiting_notification(uint16_t cid){
240aa4dd815SMatthias Ringwald     char buffer[50];
241a0ffb263SMatthias Ringwald     sprintf(buffer, "\r\n%s: \"%s\",%u\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, hfp_gsm_clip_number(), hfp_gsm_clip_type());
242aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
243aa4dd815SMatthias Ringwald }
244aa4dd815SMatthias Ringwald 
2453deb3ec6SMatthias Ringwald static int hfp_ag_error(uint16_t cid){
2463deb3ec6SMatthias Ringwald     char buffer[10];
2473deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\nERROR\r\n");
2483deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2493deb3ec6SMatthias Ringwald }
2503deb3ec6SMatthias Ringwald 
2513deb3ec6SMatthias Ringwald static int hfp_ag_report_extended_audio_gateway_error(uint16_t cid, uint8_t error){
2523deb3ec6SMatthias Ringwald     char buffer[20];
2533deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s=%d\r\n", HFP_EXTENDED_AUDIO_GATEWAY_ERROR, error);
2543deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2553deb3ec6SMatthias Ringwald }
2563deb3ec6SMatthias Ringwald 
257ad902e3dSMatthias Ringwald // fast & small implementation for fixed int size
258ad902e3dSMatthias Ringwald static int string_len_for_uint32(uint32_t i){
259ad902e3dSMatthias Ringwald     if (i <         10) return 1;
260ad902e3dSMatthias Ringwald     if (i <        100) return 2;
261ad902e3dSMatthias Ringwald     if (i <       1000) return 3;
262ad902e3dSMatthias Ringwald     if (i <      10000) return 4;
26374386ee0SMatthias Ringwald     if (i <     100000) return 5;
264ad902e3dSMatthias Ringwald     if (i <    1000000) return 6;
265ad902e3dSMatthias Ringwald     if (i <   10000000) return 7;
266ad902e3dSMatthias Ringwald     if (i <  100000000) return 8;
267ad902e3dSMatthias Ringwald     if (i < 1000000000) return 9;
268ad902e3dSMatthias Ringwald     return 10;
269ad902e3dSMatthias Ringwald }
270ad902e3dSMatthias Ringwald 
271ad902e3dSMatthias Ringwald // get size for indicator string
272a0ffb263SMatthias Ringwald static int hfp_ag_indicators_string_size(hfp_connection_t * hfp_connection, int i){
273ad902e3dSMatthias Ringwald     // template: ("$NAME",($MIN,$MAX))
274a0ffb263SMatthias Ringwald     return 8 + strlen(hfp_ag_get_ag_indicators(hfp_connection)[i].name)
275a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range)
276a0ffb263SMatthias Ringwald          + string_len_for_uint32(hfp_ag_get_ag_indicators(hfp_connection)[i].min_range);
277ad902e3dSMatthias Ringwald }
278ad902e3dSMatthias Ringwald 
279ad902e3dSMatthias Ringwald // store indicator
280a0ffb263SMatthias Ringwald static void hfp_ag_indicators_string_store(hfp_connection_t * hfp_connection, int i, uint8_t * buffer){
281ad902e3dSMatthias Ringwald     sprintf((char *) buffer, "(\"%s\",(%d,%d)),",
282a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].name,
283a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].min_range,
284a0ffb263SMatthias Ringwald             hfp_ag_get_ag_indicators(hfp_connection)[i].max_range);
2853deb3ec6SMatthias Ringwald }
286ad902e3dSMatthias Ringwald 
287ad902e3dSMatthias Ringwald // structure: header [indicator [comma indicator]] footer
288a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_num_segments(hfp_connection_t * hfp_connection){
289a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
290ad902e3dSMatthias Ringwald     if (!num_indicators) return 2;
291ad902e3dSMatthias Ringwald     return 3 + (num_indicators-1) * 2;
2923deb3ec6SMatthias Ringwald }
293ad902e3dSMatthias Ringwald 
294ad902e3dSMatthias Ringwald // get size of individual segment for hfp_ag_retrieve_indicators_cmd
295a0ffb263SMatthias Ringwald static int hfp_ag_indicators_cmd_generator_get_segment_len(hfp_connection_t * hfp_connection, int index){
296ad902e3dSMatthias Ringwald     if (index == 0) {
297ad902e3dSMatthias Ringwald         return strlen(HFP_INDICATOR) + 3;   // "\n\r%s:""
298ad902e3dSMatthias Ringwald     }
299ad902e3dSMatthias Ringwald     index--;
300a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
301ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
302ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
303a0ffb263SMatthias Ringwald         return hfp_ag_indicators_string_size(hfp_connection, indicator_index);
304ad902e3dSMatthias Ringwald     }
305ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators - 1){
306ad902e3dSMatthias Ringwald         return 8; // "\r\n\r\nOK\r\n"
307ad902e3dSMatthias Ringwald     }
308ad902e3dSMatthias Ringwald     return 1; // comma
309ad902e3dSMatthias Ringwald }
310ad902e3dSMatthias Ringwald 
311a0ffb263SMatthias Ringwald static void hgp_ag_indicators_cmd_generator_store_segment(hfp_connection_t * hfp_connection, int index, uint8_t * buffer){
312ad902e3dSMatthias Ringwald     if (index == 0){
313ad902e3dSMatthias Ringwald         *buffer++ = '\r';
31474386ee0SMatthias Ringwald         *buffer++ = '\n';
315ad902e3dSMatthias Ringwald         int len = strlen(HFP_INDICATOR);
316ad902e3dSMatthias Ringwald         memcpy(buffer, HFP_INDICATOR, len);
317ad902e3dSMatthias Ringwald         buffer += len;
318ad902e3dSMatthias Ringwald         *buffer++ = ':';
319ad902e3dSMatthias Ringwald         return;
320ad902e3dSMatthias Ringwald     }
321ad902e3dSMatthias Ringwald     index--;
322a0ffb263SMatthias Ringwald     int num_indicators = hfp_ag_get_ag_indicators_nr(hfp_connection);
323ad902e3dSMatthias Ringwald     int indicator_index = index >> 1;
324ad902e3dSMatthias Ringwald     if ((index & 1) == 0){
325a0ffb263SMatthias Ringwald         hfp_ag_indicators_string_store(hfp_connection, indicator_index, buffer);
326ad902e3dSMatthias Ringwald         return;
327ad902e3dSMatthias Ringwald     }
328ad902e3dSMatthias Ringwald     if (indicator_index == num_indicators-1){
329ad902e3dSMatthias Ringwald         memcpy(buffer, "\r\n\r\nOK\r\n", 8);
330ad902e3dSMatthias Ringwald         return;
331ad902e3dSMatthias Ringwald     }
332ad902e3dSMatthias Ringwald     *buffer = ',';
3333deb3ec6SMatthias Ringwald }
3343deb3ec6SMatthias Ringwald 
3353deb3ec6SMatthias Ringwald static int hfp_hf_indicators_join(char * buffer, int buffer_size){
3363deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3373deb3ec6SMatthias Ringwald     int i;
3383deb3ec6SMatthias Ringwald     int offset = 0;
339a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr-1; i++) {
340a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3413deb3ec6SMatthias Ringwald     }
342a0ffb263SMatthias Ringwald     if (i < hfp_generic_status_indicators_nr){
343a0ffb263SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_generic_status_indicators[i].uuid);
3443deb3ec6SMatthias Ringwald     }
3453deb3ec6SMatthias Ringwald     return offset;
3463deb3ec6SMatthias Ringwald }
3473deb3ec6SMatthias Ringwald 
3483deb3ec6SMatthias Ringwald static int hfp_hf_indicators_initial_status_join(char * buffer, int buffer_size){
349a0ffb263SMatthias Ringwald     if (buffer_size < hfp_generic_status_indicators_nr * 3) return 0;
3503deb3ec6SMatthias Ringwald     int i;
3513deb3ec6SMatthias Ringwald     int offset = 0;
352a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_generic_status_indicators_nr; i++) {
353a0ffb263SMatthias 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);
3543deb3ec6SMatthias Ringwald     }
3553deb3ec6SMatthias Ringwald     return offset;
3563deb3ec6SMatthias Ringwald }
3573deb3ec6SMatthias Ringwald 
3583deb3ec6SMatthias Ringwald static int hfp_ag_indicators_status_join(char * buffer, int buffer_size){
3593deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_indicators_nr * 3) return 0;
3603deb3ec6SMatthias Ringwald     int i;
3613deb3ec6SMatthias Ringwald     int offset = 0;
3623deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_indicators_nr-1; i++) {
3633deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d,", hfp_ag_indicators[i].status);
3643deb3ec6SMatthias Ringwald     }
3653deb3ec6SMatthias Ringwald     if (i<hfp_ag_indicators_nr){
3663deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%d", hfp_ag_indicators[i].status);
3673deb3ec6SMatthias Ringwald     }
3683deb3ec6SMatthias Ringwald     return offset;
3693deb3ec6SMatthias Ringwald }
3703deb3ec6SMatthias Ringwald 
3713deb3ec6SMatthias Ringwald static int hfp_ag_call_services_join(char * buffer, int buffer_size){
3723deb3ec6SMatthias Ringwald     if (buffer_size < hfp_ag_call_hold_services_nr * 3) return 0;
3733deb3ec6SMatthias Ringwald     int i;
3743deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, buffer_size, "(");
3753deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_ag_call_hold_services_nr-1; i++) {
3763deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s,", hfp_ag_call_hold_services[i]);
3773deb3ec6SMatthias Ringwald     }
3783deb3ec6SMatthias Ringwald     if (i<hfp_ag_call_hold_services_nr){
3793deb3ec6SMatthias Ringwald         offset += snprintf(buffer+offset, buffer_size-offset, "%s)", hfp_ag_call_hold_services[i]);
3803deb3ec6SMatthias Ringwald     }
3813deb3ec6SMatthias Ringwald     return offset;
3823deb3ec6SMatthias Ringwald }
3833deb3ec6SMatthias Ringwald 
384a0ffb263SMatthias Ringwald static int hfp_ag_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection,
385ad902e3dSMatthias Ringwald     int start_segment, int num_segments,
386a0ffb263SMatthias Ringwald     int (*get_segment_len)(hfp_connection_t * hfp_connection, int segment),
387a0ffb263SMatthias Ringwald     void (*store_segment) (hfp_connection_t * hfp_connection, int segment, uint8_t * buffer)){
3883deb3ec6SMatthias Ringwald 
389ad902e3dSMatthias Ringwald     // assumes: can send now == true
390ad902e3dSMatthias Ringwald     // assumes: num segments > 0
391ad902e3dSMatthias Ringwald     rfcomm_reserve_packet_buffer();
392ad902e3dSMatthias Ringwald     int mtu = rfcomm_get_max_frame_size(cid);
393ad902e3dSMatthias Ringwald     uint8_t * data = rfcomm_get_outgoing_buffer();
394ad902e3dSMatthias Ringwald     int offset = 0;
395ad902e3dSMatthias Ringwald     int segment = start_segment;
396ad902e3dSMatthias Ringwald     while (segment < num_segments){
397a0ffb263SMatthias Ringwald         int segment_len = get_segment_len(hfp_connection, segment);
398ad902e3dSMatthias Ringwald         if (offset + segment_len <= mtu){
399a0ffb263SMatthias Ringwald             store_segment(hfp_connection, segment, data+offset);
400ad902e3dSMatthias Ringwald             offset += segment_len;
401ad902e3dSMatthias Ringwald             segment++;
402ad902e3dSMatthias Ringwald         }
403ad902e3dSMatthias Ringwald     }
404ad902e3dSMatthias Ringwald     rfcomm_send_prepared(cid, offset);
405ad902e3dSMatthias Ringwald     return segment;
406ad902e3dSMatthias Ringwald }
4073deb3ec6SMatthias Ringwald 
408ad902e3dSMatthias Ringwald // returns next segment to store
409a0ffb263SMatthias Ringwald static int hfp_ag_retrieve_indicators_cmd_via_generator(uint16_t cid, hfp_connection_t * hfp_connection, int start_segment){
410a0ffb263SMatthias Ringwald     int num_segments = hfp_ag_indicators_cmd_generator_num_segments(hfp_connection);
411a0ffb263SMatthias Ringwald     return hfp_ag_cmd_via_generator(cid, hfp_connection, start_segment, num_segments,
412ad902e3dSMatthias Ringwald         hfp_ag_indicators_cmd_generator_get_segment_len, hgp_ag_indicators_cmd_generator_store_segment);
4133deb3ec6SMatthias Ringwald }
4143deb3ec6SMatthias Ringwald 
4153deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_indicators_status_cmd(uint16_t cid){
4163deb3ec6SMatthias Ringwald     char buffer[40];
4173deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_INDICATOR);
4183deb3ec6SMatthias Ringwald     offset += hfp_ag_indicators_status_join(buffer+offset, sizeof(buffer)-offset);
4193deb3ec6SMatthias Ringwald 
4203deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4213deb3ec6SMatthias Ringwald 
4223deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4233deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4243deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4253deb3ec6SMatthias Ringwald }
4263deb3ec6SMatthias Ringwald 
4273deb3ec6SMatthias Ringwald static int hfp_ag_set_indicator_status_update_cmd(uint16_t cid, uint8_t activate){
4283deb3ec6SMatthias Ringwald     // AT\r\n%s:3,0,0,%d\r\n
4293deb3ec6SMatthias Ringwald     return hfp_ag_ok(cid);
4303deb3ec6SMatthias Ringwald }
4313deb3ec6SMatthias Ringwald 
4323deb3ec6SMatthias Ringwald 
4333deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_can_hold_call_cmd(uint16_t cid){
434ad902e3dSMatthias Ringwald     char buffer[40];
4353deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
4363deb3ec6SMatthias Ringwald     offset += hfp_ag_call_services_join(buffer+offset, sizeof(buffer)-offset);
4373deb3ec6SMatthias Ringwald 
4383deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4393deb3ec6SMatthias Ringwald 
4403deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n\r\nOK\r\n");
4413deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4423deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4433deb3ec6SMatthias Ringwald }
4443deb3ec6SMatthias Ringwald 
4453deb3ec6SMatthias Ringwald 
4463deb3ec6SMatthias Ringwald static int hfp_ag_list_supported_generic_status_indicators_cmd(uint16_t cid){
4473deb3ec6SMatthias Ringwald     return hfp_ag_ok(cid);
4483deb3ec6SMatthias Ringwald }
4493deb3ec6SMatthias Ringwald 
4503deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_supported_generic_status_indicators_cmd(uint16_t cid){
4513deb3ec6SMatthias Ringwald     char buffer[40];
452aa4dd815SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s:(", HFP_GENERIC_STATUS_INDICATOR);
4533deb3ec6SMatthias Ringwald     offset += hfp_hf_indicators_join(buffer+offset, sizeof(buffer)-offset);
4543deb3ec6SMatthias Ringwald 
4553deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4563deb3ec6SMatthias Ringwald 
457aa4dd815SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, ")\r\n\r\nOK\r\n");
4583deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4593deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4603deb3ec6SMatthias Ringwald }
4613deb3ec6SMatthias Ringwald 
4623deb3ec6SMatthias Ringwald static int hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(uint16_t cid){
4633deb3ec6SMatthias Ringwald     char buffer[40];
4643deb3ec6SMatthias Ringwald     int offset = hfp_hf_indicators_initial_status_join(buffer, sizeof(buffer));
4653deb3ec6SMatthias Ringwald 
4663deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4673deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\nOK\r\n");
4683deb3ec6SMatthias Ringwald     buffer[offset] = 0;
4693deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4703deb3ec6SMatthias Ringwald }
4713deb3ec6SMatthias Ringwald 
472aa4dd815SMatthias Ringwald static int hfp_ag_transfer_ag_indicators_status_cmd(uint16_t cid, hfp_ag_indicator_t * indicator){
4733deb3ec6SMatthias Ringwald     char buffer[20];
474aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d,%d\r\n", HFP_TRANSFER_AG_INDICATOR_STATUS, indicator->index, indicator->status);
4753deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4763deb3ec6SMatthias Ringwald }
4773deb3ec6SMatthias Ringwald 
4783deb3ec6SMatthias Ringwald static int hfp_ag_report_network_operator_name_cmd(uint16_t cid, hfp_network_opearator_t op){
4793deb3ec6SMatthias Ringwald     char buffer[40];
4803deb3ec6SMatthias Ringwald     if (strlen(op.name) == 0){
4813deb3ec6SMatthias Ringwald         sprintf(buffer, "\r\n%s:%d,,\r\n\r\nOK\r\n", HFP_QUERY_OPERATOR_SELECTION, op.mode);
4823deb3ec6SMatthias Ringwald     } else {
4833deb3ec6SMatthias 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);
4843deb3ec6SMatthias Ringwald     }
4853deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4863deb3ec6SMatthias Ringwald }
4873deb3ec6SMatthias Ringwald 
4883deb3ec6SMatthias Ringwald 
4893deb3ec6SMatthias Ringwald static int hfp_ag_cmd_suggest_codec(uint16_t cid, uint8_t codec){
4903deb3ec6SMatthias Ringwald     char buffer[30];
4913deb3ec6SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec);
4923deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
4933deb3ec6SMatthias Ringwald }
4943deb3ec6SMatthias Ringwald 
495aa4dd815SMatthias Ringwald static int hfp_ag_activate_voice_recognition_cmd(uint16_t cid, uint8_t activate_voice_recognition){
496aa4dd815SMatthias Ringwald     char buffer[30];
497aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s: %d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate_voice_recognition);
498aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
499aa4dd815SMatthias Ringwald }
500aa4dd815SMatthias Ringwald 
501aa4dd815SMatthias Ringwald static int hfp_ag_set_speaker_gain_cmd(uint16_t cid, uint8_t gain){
502aa4dd815SMatthias Ringwald     char buffer[30];
503aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_SPEAKER_GAIN, gain);
504aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
505aa4dd815SMatthias Ringwald }
506aa4dd815SMatthias Ringwald 
507aa4dd815SMatthias Ringwald static int hfp_ag_set_microphone_gain_cmd(uint16_t cid, uint8_t gain){
508aa4dd815SMatthias Ringwald     char buffer[30];
509aa4dd815SMatthias Ringwald     sprintf(buffer, "\r\n%s:%d\r\n", HFP_SET_MICROPHONE_GAIN, gain);
510aa4dd815SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
511aa4dd815SMatthias Ringwald }
512aa4dd815SMatthias Ringwald 
513ce263fc8SMatthias Ringwald static int hfp_ag_set_response_and_hold(uint16_t cid, int state){
514ce263fc8SMatthias Ringwald     char buffer[30];
515ce263fc8SMatthias Ringwald     sprintf(buffer, "\r\n%s: %d\r\n", HFP_RESPONSE_AND_HOLD, state);
516ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
517ce263fc8SMatthias Ringwald }
518ce263fc8SMatthias Ringwald 
519aa4dd815SMatthias Ringwald 
520a0ffb263SMatthias Ringwald static uint8_t hfp_ag_suggest_codec(hfp_connection_t *hfp_connection){
5213deb3ec6SMatthias Ringwald     int i,j;
522aa4dd815SMatthias Ringwald     uint8_t codec = HFP_CODEC_CVSD;
5233deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_codecs_nr; i++){
524a0ffb263SMatthias Ringwald         for (j = 0; j < hfp_connection->remote_codecs_nr; j++){
525a0ffb263SMatthias Ringwald             if (hfp_connection->remote_codecs[j] == hfp_codecs[i]){
526a0ffb263SMatthias Ringwald                 codec = hfp_connection->remote_codecs[j];
5273deb3ec6SMatthias Ringwald                 continue;
5283deb3ec6SMatthias Ringwald             }
5293deb3ec6SMatthias Ringwald         }
5303deb3ec6SMatthias Ringwald     }
5313deb3ec6SMatthias Ringwald     return codec;
5323deb3ec6SMatthias Ringwald }
5333deb3ec6SMatthias Ringwald 
534a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
535aa4dd815SMatthias Ringwald     /* events ( == commands):
536aa4dd815SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
537aa4dd815SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
538aa4dd815SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
539aa4dd815SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
540aa4dd815SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
541aa4dd815SMatthias Ringwald     */
542aa4dd815SMatthias Ringwald 
543a0ffb263SMatthias Ringwald      switch (hfp_connection->codecs_state){
544aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
545a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
546aa4dd815SMatthias Ringwald             break;
547aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
548a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
549aa4dd815SMatthias Ringwald             break;
550aa4dd815SMatthias Ringwald         default:
551aa4dd815SMatthias Ringwald             break;
552aa4dd815SMatthias Ringwald     }
553aa4dd815SMatthias Ringwald 
554aa4dd815SMatthias Ringwald     // printf(" -> State machine: CC\n");
555aa4dd815SMatthias Ringwald 
556a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
557aa4dd815SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
558aa4dd815SMatthias Ringwald             //printf("HFP_CODECS_RECEIVED_LIST \n");
559a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED){
560a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_RECEIVED_LIST;
561a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
562aa4dd815SMatthias Ringwald                 return 1;
563aa4dd815SMatthias Ringwald             }
564aa4dd815SMatthias Ringwald 
565a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
566aa4dd815SMatthias Ringwald                 case HFP_CODECS_AG_SENT_COMMON_CODEC:
567aa4dd815SMatthias Ringwald                 case HFP_CODECS_EXCHANGED:
568a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_AG_RESEND_COMMON_CODEC;
569aa4dd815SMatthias Ringwald                     break;
570aa4dd815SMatthias Ringwald                 default:
571aa4dd815SMatthias Ringwald                     break;
572aa4dd815SMatthias Ringwald             }
573a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
574aa4dd815SMatthias Ringwald             return 1;
575aa4dd815SMatthias Ringwald 
576aa4dd815SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
577aa4dd815SMatthias Ringwald             //printf(" HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP \n");
578a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
579a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
580aa4dd815SMatthias Ringwald             return 1;
581aa4dd815SMatthias Ringwald 
582aa4dd815SMatthias Ringwald         case HFP_CMD_AG_SEND_COMMON_CODEC:
583aa4dd815SMatthias Ringwald             //printf(" HFP_CMD_AG_SEND_COMMON_CODEC \n");
584a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_AG_SENT_COMMON_CODEC;
585a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = hfp_ag_suggest_codec(hfp_connection);
586a0ffb263SMatthias Ringwald             hfp_ag_cmd_suggest_codec(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
587aa4dd815SMatthias Ringwald             return 1;
588aa4dd815SMatthias Ringwald 
589aa4dd815SMatthias Ringwald         case HFP_CMD_HF_CONFIRMED_CODEC:
590aa4dd815SMatthias Ringwald             //printf("HFP_CMD_HF_CONFIRMED_CODEC \n");
591a0ffb263SMatthias Ringwald             if (hfp_connection->codec_confirmed != hfp_connection->suggested_codec){
592a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_ERROR;
593a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
594aa4dd815SMatthias Ringwald                 return 1;
595aa4dd815SMatthias Ringwald             }
596a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = hfp_connection->codec_confirmed;
597a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
598aa4dd815SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_CODECS_CONNECTION_COMPLETE, 0);
599a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
600aa4dd815SMatthias Ringwald             return 1;
601aa4dd815SMatthias Ringwald         default:
602aa4dd815SMatthias Ringwald             break;
603aa4dd815SMatthias Ringwald     }
604aa4dd815SMatthias Ringwald     return 0;
605aa4dd815SMatthias Ringwald }
606aa4dd815SMatthias Ringwald 
607a0ffb263SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){
608ce263fc8SMatthias Ringwald     // determine highest possible link setting
609a0ffb263SMatthias Ringwald     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
610a0ffb263SMatthias Ringwald     if (hci_remote_esco_supported(hfp_connection->acl_handle)){
611a0ffb263SMatthias Ringwald         hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
612a0ffb263SMatthias Ringwald         if ((hfp_connection->remote_supported_features & (1<<HFP_HFSF_ESCO_S4))
613ce263fc8SMatthias Ringwald         &&  (hfp_supported_features             & (1<<HFP_AGSF_ESCO_S4))){
614a0ffb263SMatthias Ringwald             hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
615ce263fc8SMatthias Ringwald         }
616ce263fc8SMatthias Ringwald     }
617ce263fc8SMatthias Ringwald }
618ce263fc8SMatthias Ringwald 
619a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
620a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
621aa4dd815SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0);
622aa4dd815SMatthias Ringwald 
623a0ffb263SMatthias Ringwald     hfp_init_link_settings(hfp_connection);
624ce263fc8SMatthias Ringwald 
625a0ffb263SMatthias Ringwald     // if active call exist, set per-hfp_connection state active, too (when audio is on)
626d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
627a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
628aa4dd815SMatthias Ringwald     }
629ce263fc8SMatthias Ringwald     // if AG is ringing, also start ringing on the HF
630d0c20769SMatthias Ringwald     if (hfp_gsm_call_status() == HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS &&
631d0c20769SMatthias Ringwald         hfp_gsm_callsetup_status() == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
632a0ffb263SMatthias Ringwald         hfp_ag_hf_start_ringing(hfp_connection);
633ce263fc8SMatthias Ringwald     }
634aa4dd815SMatthias Ringwald }
6353deb3ec6SMatthias Ringwald 
636a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
637a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
6383deb3ec6SMatthias Ringwald     int done = 0;
639aa4dd815SMatthias Ringwald     // printf(" -> State machine: SLC\n");
640a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
6413deb3ec6SMatthias Ringwald         case HFP_CMD_SUPPORTED_FEATURES:
642a0ffb263SMatthias Ringwald             switch(hfp_connection->state){
6433deb3ec6SMatthias Ringwald                 case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
644aa4dd815SMatthias Ringwald                 case HFP_EXCHANGE_SUPPORTED_FEATURES:
645a0ffb263SMatthias Ringwald                     if (has_codec_negotiation_feature(hfp_connection)){
646a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
647aa4dd815SMatthias Ringwald                     } else {
648a0ffb263SMatthias Ringwald                         hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
649aa4dd815SMatthias Ringwald                     }
650a0ffb263SMatthias Ringwald                     hfp_ag_exchange_supported_features_cmd(hfp_connection->rfcomm_cid);
651aa4dd815SMatthias Ringwald                     return 1;
6523deb3ec6SMatthias Ringwald                 default:
6533deb3ec6SMatthias Ringwald                     break;
6543deb3ec6SMatthias Ringwald             }
6553deb3ec6SMatthias Ringwald             break;
6563deb3ec6SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
657a0ffb263SMatthias Ringwald             done = codecs_exchange_state_machine(hfp_connection);
6583deb3ec6SMatthias Ringwald 
659a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_RECEIVED_LIST){
660a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
6613deb3ec6SMatthias Ringwald             }
662aa4dd815SMatthias Ringwald             return done;
663aa4dd815SMatthias Ringwald 
664aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS:
665a0ffb263SMatthias Ringwald             if (hfp_connection->state == HFP_W4_RETRIEVE_INDICATORS) {
666a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_NONE;  // prevent reentrance
667a0ffb263SMatthias Ringwald                 int next_segment = hfp_ag_retrieve_indicators_cmd_via_generator(hfp_connection->rfcomm_cid, hfp_connection, hfp_connection->send_ag_indicators_segment);
668a0ffb263SMatthias Ringwald                 if (next_segment < hfp_ag_indicators_cmd_generator_num_segments(hfp_connection)){
669ad902e3dSMatthias Ringwald                     // prepare sending of next segment
670a0ffb263SMatthias Ringwald                     hfp_connection->send_ag_indicators_segment = next_segment;
671a0ffb263SMatthias Ringwald                     hfp_connection->command = HFP_CMD_RETRIEVE_AG_INDICATORS;
672ad902e3dSMatthias Ringwald                 } else {
673ad902e3dSMatthias Ringwald                     // done, go to next state
674a0ffb263SMatthias Ringwald                     hfp_connection->send_ag_indicators_segment = 0;
675a0ffb263SMatthias Ringwald                     hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
676ad902e3dSMatthias Ringwald                 }
677aa4dd815SMatthias Ringwald                 return 1;
678ad902e3dSMatthias Ringwald             }
679ad902e3dSMatthias Ringwald             break;
680aa4dd815SMatthias Ringwald 
681aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
682a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INDICATORS_STATUS) break;
683a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
684a0ffb263SMatthias Ringwald             hfp_ag_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
685aa4dd815SMatthias Ringwald             return 1;
686aa4dd815SMatthias Ringwald 
6873deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
688a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE) break;
689a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
690a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
691a0ffb263SMatthias Ringwald             } else if (has_hf_indicators_feature(hfp_connection)){
692a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
693aa4dd815SMatthias Ringwald             } else {
694a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
6953deb3ec6SMatthias Ringwald             }
696a0ffb263SMatthias Ringwald             hfp_ag_set_indicator_status_update_cmd(hfp_connection->rfcomm_cid, 1);
697aa4dd815SMatthias Ringwald             return 1;
6983deb3ec6SMatthias Ringwald 
699aa4dd815SMatthias Ringwald         case HFP_CMD_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES:
700a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_CAN_HOLD_CALL) break;
701a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
702a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
703aa4dd815SMatthias Ringwald             }
704a0ffb263SMatthias Ringwald             hfp_ag_retrieve_can_hold_call_cmd(hfp_connection->rfcomm_cid);
705a0ffb263SMatthias Ringwald             if (!has_hf_indicators_feature(hfp_connection)){
706a0ffb263SMatthias Ringwald                 hfp_ag_slc_established(hfp_connection);
707ce263fc8SMatthias Ringwald             }
708aa4dd815SMatthias Ringwald             return 1;
709aa4dd815SMatthias Ringwald 
710aa4dd815SMatthias Ringwald         case HFP_CMD_LIST_GENERIC_STATUS_INDICATORS:
711a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_LIST_GENERIC_STATUS_INDICATORS) break;
712a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
713a0ffb263SMatthias Ringwald             hfp_ag_list_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
714aa4dd815SMatthias Ringwald             return 1;
715aa4dd815SMatthias Ringwald 
716aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS:
717a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS) break;
718a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
719a0ffb263SMatthias Ringwald             hfp_ag_retrieve_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
720aa4dd815SMatthias Ringwald             return 1;
721aa4dd815SMatthias Ringwald 
722aa4dd815SMatthias Ringwald         case HFP_CMD_RETRIEVE_GENERIC_STATUS_INDICATORS_STATE:
723a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS) break;
724a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
725a0ffb263SMatthias Ringwald             hfp_ag_retrieve_initital_supported_generic_status_indicators_cmd(hfp_connection->rfcomm_cid);
726aa4dd815SMatthias Ringwald             return 1;
7273deb3ec6SMatthias Ringwald         default:
7283deb3ec6SMatthias Ringwald             break;
7293deb3ec6SMatthias Ringwald     }
7303deb3ec6SMatthias Ringwald     return done;
7313deb3ec6SMatthias Ringwald }
7323deb3ec6SMatthias Ringwald 
733a0ffb263SMatthias Ringwald static int hfp_ag_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
734a0ffb263SMatthias Ringwald     // if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
7353deb3ec6SMatthias Ringwald 
736a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
737aa4dd815SMatthias Ringwald     if (done) return done;
738aa4dd815SMatthias Ringwald 
739aa4dd815SMatthias Ringwald     // printf(" -> State machine: SLC Queries\n");
740a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
741aa4dd815SMatthias Ringwald         case HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION:
742a0ffb263SMatthias Ringwald             hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
743a0ffb263SMatthias Ringwald             hfp_ag_activate_voice_recognition_cmd(hfp_connection->rfcomm_cid, hfp_connection->ag_activate_voice_recognition);
744aa4dd815SMatthias Ringwald             return 1;
745aa4dd815SMatthias Ringwald         case HFP_CMD_HF_ACTIVATE_VOICE_RECOGNITION:
746aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)){
747a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION, hfp_connection->ag_activate_voice_recognition);
748a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
749a0ffb263SMatthias Ringwald                 hfp_ag_setup_audio_connection(hfp_connection);
750aa4dd815SMatthias Ringwald             } else {
751a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
752aa4dd815SMatthias Ringwald             }
753aa4dd815SMatthias Ringwald             return 1;
754aa4dd815SMatthias Ringwald         case HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING:
755a0ffb263SMatthias Ringwald             hfp_ag_change_in_band_ring_tone_setting_cmd(hfp_connection->rfcomm_cid);
756aa4dd815SMatthias Ringwald             return 1;
757aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME:
758a0ffb263SMatthias Ringwald             hfp_ag_report_network_operator_name_cmd(hfp_connection->rfcomm_cid, hfp_connection->network_operator);
759aa4dd815SMatthias Ringwald             return 1;
760aa4dd815SMatthias Ringwald         case HFP_CMD_QUERY_OPERATOR_SELECTION_NAME_FORMAT:
761a0ffb263SMatthias Ringwald             if (hfp_connection->network_operator.format != 0){
762a0ffb263SMatthias Ringwald                 hfp_ag_error(hfp_connection->rfcomm_cid);
763aa4dd815SMatthias Ringwald             } else {
764a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
7653deb3ec6SMatthias Ringwald             }
766aa4dd815SMatthias Ringwald             return 1;
767aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_INDIVIDUAL_AG_INDICATOR_STATUS_UPDATE:
768a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
769aa4dd815SMatthias Ringwald             return 1;
7703deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR:
771a0ffb263SMatthias Ringwald             if (hfp_connection->extended_audio_gateway_error){
772a0ffb263SMatthias Ringwald                 hfp_connection->extended_audio_gateway_error = 0;
773a0ffb263SMatthias Ringwald                 hfp_ag_report_extended_audio_gateway_error(hfp_connection->rfcomm_cid, hfp_connection->extended_audio_gateway_error_value);
774aa4dd815SMatthias Ringwald                 return 1;
7753deb3ec6SMatthias Ringwald             }
7763deb3ec6SMatthias Ringwald         case HFP_CMD_ENABLE_INDICATOR_STATUS_UPDATE:
777a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
778ce263fc8SMatthias Ringwald             return 1;
7793deb3ec6SMatthias Ringwald         default:
7803deb3ec6SMatthias Ringwald             break;
7813deb3ec6SMatthias Ringwald     }
782aa4dd815SMatthias Ringwald     return 0;
7833deb3ec6SMatthias Ringwald }
7843deb3ec6SMatthias Ringwald 
785a0ffb263SMatthias Ringwald static int hfp_ag_run_for_audio_connection(hfp_connection_t * hfp_connection){
786a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
787a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
7883deb3ec6SMatthias Ringwald 
789aa4dd815SMatthias Ringwald 
790a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
791a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
792a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
793a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
794aa4dd815SMatthias Ringwald         return 1;
795aa4dd815SMatthias Ringwald     }
796aa4dd815SMatthias Ringwald 
797a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
798aa4dd815SMatthias Ringwald 
799aa4dd815SMatthias Ringwald     // run codecs exchange
800a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
801aa4dd815SMatthias Ringwald     if (done) return done;
802a0ffb263SMatthias Ringwald     // printf(" -> State machine: Audio hfp_connection\n");
803aa4dd815SMatthias Ringwald 
804a0ffb263SMatthias Ringwald     if (hfp_connection->codecs_state != HFP_CODECS_EXCHANGED) return done;
805a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
806a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
807a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
808d63c37a1SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection->acl_handle, hfp_connection->link_setting);
809aa4dd815SMatthias Ringwald         return 1;
810aa4dd815SMatthias Ringwald     }
811aa4dd815SMatthias Ringwald     return 0;
812aa4dd815SMatthias Ringwald }
813aa4dd815SMatthias Ringwald 
814ec820d77SMatthias Ringwald static hfp_connection_t * hfp_ag_context_for_timer(btstack_timer_source_t * ts){
815665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
816665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
817aa4dd815SMatthias Ringwald 
818665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
819a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
820a0ffb263SMatthias Ringwald         if ( & hfp_connection->hfp_timeout == ts) {
821a0ffb263SMatthias Ringwald             return hfp_connection;
822aa4dd815SMatthias Ringwald         }
823aa4dd815SMatthias Ringwald     }
824aa4dd815SMatthias Ringwald     return NULL;
825aa4dd815SMatthias Ringwald }
826aa4dd815SMatthias Ringwald 
827ec820d77SMatthias Ringwald static void hfp_timeout_handler(btstack_timer_source_t * timer){
828d63c37a1SMatthias Ringwald     hfp_connection_t * hfp_connection = hfp_ag_context_for_timer(timer);
829d63c37a1SMatthias Ringwald     if (!hfp_connection) return;
830aa4dd815SMatthias Ringwald 
831d63c37a1SMatthias Ringwald     log_info("HFP start ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
832a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 1;
833a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
834aa4dd815SMatthias Ringwald 
835a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
836a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
837aa4dd815SMatthias Ringwald 
838a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
839aa4dd815SMatthias Ringwald }
840aa4dd815SMatthias Ringwald 
841a0ffb263SMatthias Ringwald static void hfp_timeout_start(hfp_connection_t * hfp_connection){
842a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
843a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer_handler(& hfp_connection->hfp_timeout, hfp_timeout_handler);
844a0ffb263SMatthias Ringwald     btstack_run_loop_set_timer(& hfp_connection->hfp_timeout, 2000); // 2 seconds timeout
845a0ffb263SMatthias Ringwald     btstack_run_loop_add_timer(& hfp_connection->hfp_timeout);
846aa4dd815SMatthias Ringwald }
847aa4dd815SMatthias Ringwald 
848a0ffb263SMatthias Ringwald static void hfp_timeout_stop(hfp_connection_t * hfp_connection){
849a0ffb263SMatthias Ringwald     log_info("HFP stop ring timeout, con handle 0x%02x", hfp_connection->acl_handle);
850a0ffb263SMatthias Ringwald     btstack_run_loop_remove_timer(& hfp_connection->hfp_timeout);
851aa4dd815SMatthias Ringwald }
852aa4dd815SMatthias Ringwald 
853aa4dd815SMatthias Ringwald //
854aa4dd815SMatthias Ringwald // transitition implementations for hfp_ag_call_state_machine
855aa4dd815SMatthias Ringwald //
856aa4dd815SMatthias Ringwald 
857a0ffb263SMatthias Ringwald static void hfp_ag_hf_start_ringing(hfp_connection_t * hfp_connection){
858aa4dd815SMatthias Ringwald     if (use_in_band_tone()){
859a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING;
860a0ffb263SMatthias Ringwald         hfp_ag_establish_audio_connection(hfp_connection->remote_addr);
861aa4dd815SMatthias Ringwald     } else {
862a0ffb263SMatthias Ringwald         hfp_timeout_start(hfp_connection);
863a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 1;
864a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
865a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_RINGING;
866a0ffb263SMatthias Ringwald         hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG);
867aa4dd815SMatthias Ringwald     }
868aa4dd815SMatthias Ringwald }
869aa4dd815SMatthias Ringwald 
870a0ffb263SMatthias Ringwald static void hfp_ag_hf_stop_ringing(hfp_connection_t * hfp_connection){
871a0ffb263SMatthias Ringwald     hfp_connection->ag_ring = 0;
872a0ffb263SMatthias Ringwald     hfp_connection->ag_send_clip = 0;
873a0ffb263SMatthias Ringwald     hfp_timeout_stop(hfp_connection);
874a0ffb263SMatthias Ringwald     hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_STOP_RINGINIG);
875aa4dd815SMatthias Ringwald }
876aa4dd815SMatthias Ringwald 
877aa4dd815SMatthias Ringwald static void hfp_ag_trigger_incoming_call(void){
878aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
879aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
880aa4dd815SMatthias Ringwald 
881665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
882665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
883665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
884a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
885a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
886a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE){
887a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
888d63c37a1SMatthias Ringwald             hfp_ag_hf_start_ringing(hfp_connection);
889aa4dd815SMatthias Ringwald         }
890a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_ACTIVE){
891a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W2_SEND_CALL_WAITING;
892aa4dd815SMatthias Ringwald         }
893a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
894aa4dd815SMatthias Ringwald     }
895aa4dd815SMatthias Ringwald }
896aa4dd815SMatthias Ringwald 
897aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callsetup_state(void){
898aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callsetup");
899aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
900aa4dd815SMatthias Ringwald 
901665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
902665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
903665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
904a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
905a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
906a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
907a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
908aa4dd815SMatthias Ringwald     }
909aa4dd815SMatthias Ringwald }
910aa4dd815SMatthias Ringwald 
911aa4dd815SMatthias Ringwald static void hfp_ag_transfer_call_state(void){
912aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("call");
913aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
914aa4dd815SMatthias Ringwald 
915665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
916665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
917665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
918a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
919a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
920a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
921a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
922aa4dd815SMatthias Ringwald     }
923aa4dd815SMatthias Ringwald }
924aa4dd815SMatthias Ringwald 
925aa4dd815SMatthias Ringwald static void hfp_ag_transfer_callheld_state(void){
926aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name("callheld");
927aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
928aa4dd815SMatthias Ringwald 
929665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
930665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
931665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
932a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
933a0ffb263SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
934a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
935a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
936aa4dd815SMatthias Ringwald     }
937aa4dd815SMatthias Ringwald }
938aa4dd815SMatthias Ringwald 
939aa4dd815SMatthias Ringwald static void hfp_ag_hf_accept_call(hfp_connection_t * source){
940aa4dd815SMatthias Ringwald 
941aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
942aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
943aa4dd815SMatthias Ringwald 
944665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
945665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
946665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
947a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
948a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
949a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
950aa4dd815SMatthias Ringwald 
951a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
952a0ffb263SMatthias Ringwald         if (hfp_connection == source){
953a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
954aa4dd815SMatthias Ringwald 
955aa4dd815SMatthias Ringwald             if (use_in_band_tone()){
956a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_ACTIVE;
957aa4dd815SMatthias Ringwald             } else {
958a0ffb263SMatthias Ringwald                 hfp_connection->call_state = HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE;
959a0ffb263SMatthias Ringwald                 hfp_ag_establish_audio_connection(hfp_connection->remote_addr);
960aa4dd815SMatthias Ringwald             }
961aa4dd815SMatthias Ringwald 
962a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
963a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
964aa4dd815SMatthias Ringwald 
965aa4dd815SMatthias Ringwald         } else {
966a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
967aa4dd815SMatthias Ringwald         }
968a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
969aa4dd815SMatthias Ringwald     }
970aa4dd815SMatthias Ringwald }
971aa4dd815SMatthias Ringwald 
972aa4dd815SMatthias Ringwald static void hfp_ag_ag_accept_call(void){
973aa4dd815SMatthias Ringwald 
974aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
975aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
976aa4dd815SMatthias Ringwald 
977665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
978665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
979665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
980a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
981a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING) continue;
982aa4dd815SMatthias Ringwald 
983a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
984a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_TRIGGER_AUDIO_CONNECTION;
985aa4dd815SMatthias Ringwald 
986a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
987a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
988aa4dd815SMatthias Ringwald 
989a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
990aa4dd815SMatthias Ringwald         break;  // only single
991aa4dd815SMatthias Ringwald     }
992aa4dd815SMatthias Ringwald }
993aa4dd815SMatthias Ringwald 
994aa4dd815SMatthias Ringwald static void hfp_ag_trigger_reject_call(void){
995aa4dd815SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
996665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
997665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
998665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
999665d90f2SMatthias Ringwald         hfp_connection_t * connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1000aa4dd815SMatthias Ringwald         if (connection->call_state != HFP_CALL_RINGING &&
1001aa4dd815SMatthias Ringwald             connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1002aa4dd815SMatthias Ringwald         hfp_ag_hf_stop_ringing(connection);
1003aa4dd815SMatthias Ringwald         connection->ag_indicators_status_update_bitmap = store_bit(connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1004aa4dd815SMatthias Ringwald         connection->call_state = HFP_CALL_IDLE;
1005aa4dd815SMatthias Ringwald         hfp_run_for_context(connection);
1006aa4dd815SMatthias Ringwald     }
1007aa4dd815SMatthias Ringwald }
1008aa4dd815SMatthias Ringwald 
1009aa4dd815SMatthias Ringwald static void hfp_ag_trigger_terminate_call(void){
1010aa4dd815SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
1011aa4dd815SMatthias Ringwald 
1012665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1013665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1014665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1015a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1016d63c37a1SMatthias Ringwald         hfp_ag_establish_service_level_connection(hfp_connection->remote_addr);
1017a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == HFP_CALL_IDLE) continue;
1018a0ffb263SMatthias Ringwald         hfp_connection->call_state = HFP_CALL_IDLE;
1019a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1020a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 1;
1021a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1022aa4dd815SMatthias Ringwald     }
1023a0ffb263SMatthias Ringwald     hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_TERMINATED);
1024aa4dd815SMatthias Ringwald }
1025aa4dd815SMatthias Ringwald 
10260cb5b971SMatthias Ringwald static void hfp_ag_set_callsetup_indicator(void){
1027aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callsetup");
1028aa4dd815SMatthias Ringwald     if (!indicator){
1029d0c20769SMatthias Ringwald         log_error("hfp_ag_set_callsetup_indicator: callsetup indicator is missing");
1030aa4dd815SMatthias Ringwald     };
1031d0c20769SMatthias Ringwald     indicator->status = hfp_gsm_callsetup_status();
1032aa4dd815SMatthias Ringwald }
1033aa4dd815SMatthias Ringwald 
10340cb5b971SMatthias Ringwald static void hfp_ag_set_callheld_indicator(void){
1035d210d9c4SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("callheld");
1036d210d9c4SMatthias Ringwald     if (!indicator){
1037d210d9c4SMatthias Ringwald         log_error("hfp_ag_set_callheld_state: callheld indicator is missing");
1038d210d9c4SMatthias Ringwald     };
1039d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_callheld_status();
1040d210d9c4SMatthias Ringwald }
1041d210d9c4SMatthias Ringwald 
10420cb5b971SMatthias Ringwald static void hfp_ag_set_call_indicator(void){
1043aa4dd815SMatthias Ringwald     hfp_ag_indicator_t * indicator = get_ag_indicator_for_name("call");
1044aa4dd815SMatthias Ringwald     if (!indicator){
1045aa4dd815SMatthias Ringwald         log_error("hfp_ag_set_call_state: call indicator is missing");
1046aa4dd815SMatthias Ringwald     };
1047d210d9c4SMatthias Ringwald     indicator->status = hfp_gsm_call_status();
1048aa4dd815SMatthias Ringwald }
1049aa4dd815SMatthias Ringwald 
1050aa4dd815SMatthias Ringwald static void hfp_ag_stop_ringing(void){
1051665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1052665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1053665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1054a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1055a0ffb263SMatthias Ringwald         if (hfp_connection->call_state != HFP_CALL_RINGING &&
1056a0ffb263SMatthias Ringwald             hfp_connection->call_state != HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING) continue;
1057a0ffb263SMatthias Ringwald         hfp_ag_hf_stop_ringing(hfp_connection);
1058aa4dd815SMatthias Ringwald     }
1059aa4dd815SMatthias Ringwald }
1060aa4dd815SMatthias Ringwald 
1061aa4dd815SMatthias Ringwald static hfp_connection_t * hfp_ag_connection_for_call_state(hfp_call_state_t call_state){
1062665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1063665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1064665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1065a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1066a0ffb263SMatthias Ringwald         if (hfp_connection->call_state == call_state) return hfp_connection;
1067aa4dd815SMatthias Ringwald     }
1068aa4dd815SMatthias Ringwald     return NULL;
1069aa4dd815SMatthias Ringwald }
1070aa4dd815SMatthias Ringwald 
1071a5bdcda8SMatthias Ringwald static void hfp_ag_send_response_and_hold_state(hfp_response_and_hold_state_t state){
1072665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1073665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1074665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1075a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1076a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = state + 1;
1077ce263fc8SMatthias Ringwald     }
1078ce263fc8SMatthias Ringwald }
1079ce263fc8SMatthias Ringwald 
1080a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
1081aa4dd815SMatthias Ringwald     int indicator_index;
1082a0ffb263SMatthias Ringwald     switch (hfp_connection->call_state){
1083aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_IN_BAND_RING:
1084a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1085a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1086a0ffb263SMatthias Ringwald             hfp_timeout_start(hfp_connection);
1087a0ffb263SMatthias Ringwald             hfp_connection->ag_ring = 1;
1088a0ffb263SMatthias Ringwald             hfp_connection->ag_send_clip = hfp_gsm_clip_type() && hfp_connection->clip_enabled;
1089a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1090a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_RINGING;
1091a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_START_RINGINIG);
1092aa4dd815SMatthias Ringwald             break;
1093aa4dd815SMatthias Ringwald         case HFP_CALL_W4_AUDIO_CONNECTION_FOR_ACTIVE:
1094a0ffb263SMatthias Ringwald             if (hfp_connection->state != HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
1095a0ffb263SMatthias Ringwald             // we got event: audio hfp_connection established
1096a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1097aa4dd815SMatthias Ringwald             break;
1098aa4dd815SMatthias Ringwald         case HFP_CALL_W2_SEND_CALL_WAITING:
1099a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_W4_CHLD;
1100a0ffb263SMatthias Ringwald             hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1101aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1102a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1103aa4dd815SMatthias Ringwald             break;
1104aa4dd815SMatthias Ringwald         default:
1105aa4dd815SMatthias Ringwald             break;
1106aa4dd815SMatthias Ringwald     }
1107aa4dd815SMatthias Ringwald     return 0;
1108aa4dd815SMatthias Ringwald }
1109a0ffb263SMatthias Ringwald // hfp_connection is used to identify originating HF
1110a0ffb263SMatthias Ringwald static void hfp_ag_call_sm(hfp_ag_call_event_t event, hfp_connection_t * hfp_connection){
1111aa4dd815SMatthias Ringwald     int indicator_index;
1112d210d9c4SMatthias Ringwald     int callsetup_indicator_index = get_ag_indicator_index_for_name("callsetup");
1113d210d9c4SMatthias Ringwald     int callheld_indicator_index = get_ag_indicator_index_for_name("callheld");
1114d210d9c4SMatthias Ringwald     int call_indicator_index = get_ag_indicator_index_for_name("call");
111574386ee0SMatthias Ringwald 
1116d210d9c4SMatthias Ringwald     //printf("hfp_ag_call_sm event %d \n", event);
1117aa4dd815SMatthias Ringwald     switch (event){
1118aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL:
1119d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1120aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1121d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1122aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1123d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1124d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1125aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
1126aa4dd815SMatthias Ringwald                             printf("AG rings\n");
1127aa4dd815SMatthias Ringwald                             break;
1128aa4dd815SMatthias Ringwald                         default:
11293deb3ec6SMatthias Ringwald                             break;
11303deb3ec6SMatthias Ringwald                     }
11313deb3ec6SMatthias Ringwald                     break;
1132aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1133d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1134aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS:
1135d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL);
1136d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1137aa4dd815SMatthias Ringwald                             hfp_ag_trigger_incoming_call();
1138aa4dd815SMatthias Ringwald                             printf("AG call waiting\n");
1139aa4dd815SMatthias Ringwald                             break;
1140aa4dd815SMatthias Ringwald                         default:
11413deb3ec6SMatthias Ringwald                             break;
11423deb3ec6SMatthias Ringwald                     }
11433deb3ec6SMatthias Ringwald                     break;
1144aa4dd815SMatthias Ringwald             }
1145aa4dd815SMatthias Ringwald             break;
1146aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG:
1147d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1148aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1149d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1150aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1151d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1152d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1153d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1154aa4dd815SMatthias Ringwald                             hfp_ag_ag_accept_call();
1155aa4dd815SMatthias Ringwald                             printf("AG answers call, accept call by GSM\n");
1156aa4dd815SMatthias Ringwald                             break;
1157aa4dd815SMatthias Ringwald                         default:
11583deb3ec6SMatthias Ringwald                             break;
11593deb3ec6SMatthias Ringwald                     }
1160aa4dd815SMatthias Ringwald                     break;
1161aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1162d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1163aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1164aa4dd815SMatthias Ringwald                             printf("AG: current call is placed on hold, incoming call gets active\n");
1165d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG);
1166d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1167d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1168ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1169ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1170aa4dd815SMatthias Ringwald                             break;
1171aa4dd815SMatthias Ringwald                         default:
1172aa4dd815SMatthias Ringwald                             break;
1173aa4dd815SMatthias Ringwald                     }
1174aa4dd815SMatthias Ringwald                     break;
1175aa4dd815SMatthias Ringwald             }
1176aa4dd815SMatthias Ringwald             break;
1177ce263fc8SMatthias Ringwald 
1178ce263fc8SMatthias Ringwald         case HFP_AG_HELD_CALL_JOINED_BY_AG:
1179d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1180ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1181d0c20769SMatthias Ringwald                     switch (hfp_gsm_callheld_status()){
1182ce263fc8SMatthias Ringwald                         case HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED:
1183ce263fc8SMatthias Ringwald                             printf("AG: joining held call with active call\n");
1184d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_HELD_CALL_JOINED_BY_AG);
1185d0c20769SMatthias Ringwald                             hfp_ag_set_callheld_indicator();
1186ce263fc8SMatthias Ringwald                             hfp_ag_transfer_callheld_state();
1187a0ffb263SMatthias Ringwald                             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL);
1188ce263fc8SMatthias Ringwald                             break;
1189ce263fc8SMatthias Ringwald                         default:
1190ce263fc8SMatthias Ringwald                             break;
1191ce263fc8SMatthias Ringwald                     }
1192ce263fc8SMatthias Ringwald                     break;
1193ce263fc8SMatthias Ringwald                 default:
1194ce263fc8SMatthias Ringwald                     break;
1195ce263fc8SMatthias Ringwald             }
1196ce263fc8SMatthias Ringwald             break;
1197ce263fc8SMatthias Ringwald 
1198aa4dd815SMatthias Ringwald         case HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF:
1199d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1200aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1201d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1202aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1203d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF);
1204d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1205d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1206a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
1207aa4dd815SMatthias Ringwald                             printf("HF answers call, accept call by GSM\n");
1208a0ffb263SMatthias Ringwald                             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CALL_ANSWERED);
1209aa4dd815SMatthias Ringwald                             break;
1210aa4dd815SMatthias Ringwald                         default:
1211aa4dd815SMatthias Ringwald                             break;
1212aa4dd815SMatthias Ringwald                     }
12133deb3ec6SMatthias Ringwald                     break;
12143deb3ec6SMatthias Ringwald                 default:
12153deb3ec6SMatthias Ringwald                     break;
12163deb3ec6SMatthias Ringwald             }
12173deb3ec6SMatthias Ringwald             break;
12183deb3ec6SMatthias Ringwald 
1219ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG:
1220d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1221ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1222d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1223ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1224d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG);
1225ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1226ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1227a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1228ce263fc8SMatthias Ringwald                             // as with regualr call
1229d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1230d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1231ce263fc8SMatthias Ringwald                             hfp_ag_ag_accept_call();
1232ce263fc8SMatthias Ringwald                             printf("AG response and hold - hold by AG\n");
1233ce263fc8SMatthias Ringwald                             break;
1234ce263fc8SMatthias Ringwald                         default:
1235ce263fc8SMatthias Ringwald                             break;
1236ce263fc8SMatthias Ringwald                     }
1237ce263fc8SMatthias Ringwald                     break;
1238ce263fc8SMatthias Ringwald                 default:
1239ce263fc8SMatthias Ringwald                     break;
1240ce263fc8SMatthias Ringwald             }
1241ce263fc8SMatthias Ringwald             break;
1242ce263fc8SMatthias Ringwald 
1243ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF:
1244d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1245ce263fc8SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1246d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1247ce263fc8SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1248d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF);
1249ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_active = 1;
1250ce263fc8SMatthias Ringwald                             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD;
1251a5bdcda8SMatthias Ringwald                             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1252ce263fc8SMatthias Ringwald                             // as with regualr call
1253d210d9c4SMatthias Ringwald                             hfp_ag_set_call_indicator();
1254d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1255a0ffb263SMatthias Ringwald                             hfp_ag_hf_accept_call(hfp_connection);
1256ce263fc8SMatthias Ringwald                             printf("AG response and hold - hold by HF\n");
1257ce263fc8SMatthias Ringwald                             break;
1258ce263fc8SMatthias Ringwald                         default:
1259ce263fc8SMatthias Ringwald                             break;
1260ce263fc8SMatthias Ringwald                     }
1261ce263fc8SMatthias Ringwald                     break;
1262ce263fc8SMatthias Ringwald                 default:
1263ce263fc8SMatthias Ringwald                     break;
1264ce263fc8SMatthias Ringwald             }
1265ce263fc8SMatthias Ringwald             break;
1266ce263fc8SMatthias Ringwald 
1267ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG:
1268ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF:
1269ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1270ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1271d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG);
1272a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1273ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED;
1274a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1275ce263fc8SMatthias Ringwald             printf("Held Call accepted and active\n");
1276ce263fc8SMatthias Ringwald             break;
1277ce263fc8SMatthias Ringwald 
1278ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG:
1279ce263fc8SMatthias Ringwald         case HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF:
1280ce263fc8SMatthias Ringwald             if (!hfp_ag_response_and_hold_active) break;
1281ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_state != HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD) break;
1282d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG);
1283a5bdcda8SMatthias Ringwald             hfp_ag_response_and_hold_active = 0;
1284ce263fc8SMatthias Ringwald             hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1285a5bdcda8SMatthias Ringwald             hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1286ce263fc8SMatthias Ringwald             // from terminate by ag
1287d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1288ce263fc8SMatthias Ringwald             hfp_ag_trigger_terminate_call();
1289ce263fc8SMatthias Ringwald             break;
1290ce263fc8SMatthias Ringwald 
1291aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_HF:
1292d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1293aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1294d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1295aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1296d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1297d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1298aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1299aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
1300aa4dd815SMatthias Ringwald                             printf("HF Rejected Incoming call, AG terminate call\n");
1301aa4dd815SMatthias Ringwald                             break;
1302aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1303aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1304d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1305d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1306aa4dd815SMatthias Ringwald                             hfp_ag_transfer_callsetup_state();
1307aa4dd815SMatthias Ringwald                             printf("AG terminate outgoing call process\n");
1308aa4dd815SMatthias Ringwald                         default:
1309aa4dd815SMatthias Ringwald                             break;
1310aa4dd815SMatthias Ringwald                     }
1311aa4dd815SMatthias Ringwald                     break;
1312aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1313d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_HF);
1314d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1315aa4dd815SMatthias Ringwald                     hfp_ag_transfer_call_state();
1316a0ffb263SMatthias Ringwald                     hfp_connection->call_state = HFP_CALL_IDLE;
1317aa4dd815SMatthias Ringwald                     printf("AG terminate call\n");
1318aa4dd815SMatthias Ringwald                     break;
1319aa4dd815SMatthias Ringwald             }
1320aa4dd815SMatthias Ringwald             break;
13213deb3ec6SMatthias Ringwald 
1322aa4dd815SMatthias Ringwald         case HFP_AG_TERMINATE_CALL_BY_AG:
1323d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1324aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1325d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1326aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1327d210d9c4SMatthias Ringwald                             hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1328d0c20769SMatthias Ringwald                             hfp_ag_set_callsetup_indicator();
1329aa4dd815SMatthias Ringwald                             hfp_ag_trigger_reject_call();
1330aa4dd815SMatthias Ringwald                             printf("AG Rejected Incoming call, AG terminate call\n");
1331aa4dd815SMatthias Ringwald                             break;
1332aa4dd815SMatthias Ringwald                         default:
1333aa4dd815SMatthias Ringwald                             break;
13343deb3ec6SMatthias Ringwald                     }
1335aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1336d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_TERMINATE_CALL_BY_AG);
1337d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1338d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1339aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1340aa4dd815SMatthias Ringwald                     printf("AG terminate call\n");
1341aa4dd815SMatthias Ringwald                     break;
1342aa4dd815SMatthias Ringwald                 default:
13433deb3ec6SMatthias Ringwald                     break;
13443deb3ec6SMatthias Ringwald             }
13453deb3ec6SMatthias Ringwald             break;
1346aa4dd815SMatthias Ringwald         case HFP_AG_CALL_DROPPED:
1347d0c20769SMatthias Ringwald             switch (hfp_gsm_call_status()){
1348aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_NO_HELD_OR_ACTIVE_CALLS:
1349d0c20769SMatthias Ringwald                     switch (hfp_gsm_callsetup_status()){
1350aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS:
1351aa4dd815SMatthias Ringwald                             hfp_ag_stop_ringing();
1352aa4dd815SMatthias Ringwald                             printf("Incoming call interrupted\n");
1353aa4dd815SMatthias Ringwald                             break;
1354aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_DIALING_STATE:
1355aa4dd815SMatthias Ringwald                         case HFP_CALLSETUP_STATUS_OUTGOING_CALL_SETUP_IN_ALERTING_STATE:
1356aa4dd815SMatthias Ringwald                             printf("Outgoing call interrupted\n");
1357aa4dd815SMatthias Ringwald                             printf("AG notify call dropped\n");
1358aa4dd815SMatthias Ringwald                             break;
1359aa4dd815SMatthias Ringwald                         default:
13603deb3ec6SMatthias Ringwald                             break;
13613deb3ec6SMatthias Ringwald                     }
1362d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1363d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1364aa4dd815SMatthias Ringwald                     hfp_ag_transfer_callsetup_state();
1365aa4dd815SMatthias Ringwald                     break;
1366aa4dd815SMatthias Ringwald                 case HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT:
1367ce263fc8SMatthias Ringwald                     if (hfp_ag_response_and_hold_active) {
1368d210d9c4SMatthias Ringwald                         hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1369ce263fc8SMatthias Ringwald                         hfp_ag_response_and_hold_state = HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED;
1370a5bdcda8SMatthias Ringwald                         hfp_ag_send_response_and_hold_state(hfp_ag_response_and_hold_state);
1371ce263fc8SMatthias Ringwald                     }
1372d210d9c4SMatthias Ringwald                     hfp_gsm_handle_event(HFP_AG_CALL_DROPPED);
1373d0c20769SMatthias Ringwald                     hfp_ag_set_callsetup_indicator();
1374d210d9c4SMatthias Ringwald                     hfp_ag_set_call_indicator();
1375aa4dd815SMatthias Ringwald                     hfp_ag_trigger_terminate_call();
1376aa4dd815SMatthias Ringwald                     printf("AG notify call dropped\n");
1377aa4dd815SMatthias Ringwald                     break;
1378aa4dd815SMatthias Ringwald                 default:
1379aa4dd815SMatthias Ringwald                     break;
1380aa4dd815SMatthias Ringwald             }
1381aa4dd815SMatthias Ringwald             break;
1382aa4dd815SMatthias Ringwald 
1383aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_INITIATED:
1384d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1385d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1386a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1387a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1388d210d9c4SMatthias Ringwald                 break;
1389d210d9c4SMatthias Ringwald             }
1390a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_number(HFP_AG_OUTGOING_CALL_INITIATED, (const char *) &hfp_connection->line_buffer[3]);
13919cae807eSMatthias Ringwald 
1392a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
139374386ee0SMatthias Ringwald 
1394a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, (const char *) &hfp_connection->line_buffer[3]);
1395aa4dd815SMatthias Ringwald             break;
1396aa4dd815SMatthias Ringwald 
13979cae807eSMatthias Ringwald         case HFP_AG_OUTGOING_REDIAL_INITIATED:{
1398d210d9c4SMatthias Ringwald             // directly reject call if number of free slots is exceeded
1399d210d9c4SMatthias Ringwald             if (!hfp_gsm_call_possible()){
1400a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1401a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1402d210d9c4SMatthias Ringwald                 break;
1403d210d9c4SMatthias Ringwald             }
1404d210d9c4SMatthias Ringwald 
140574386ee0SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_REDIAL_INITIATED);
1406a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_INITIATED;
140774386ee0SMatthias Ringwald 
14089cae807eSMatthias Ringwald             printf("\nRedial last number");
14099cae807eSMatthias Ringwald             char * last_dialed_number = hfp_gsm_last_dialed_number();
1410aa4dd815SMatthias Ringwald 
14119cae807eSMatthias Ringwald             if (strlen(last_dialed_number) > 0){
14129cae807eSMatthias Ringwald                 printf("\nLast number exists: accept call");
14139cae807eSMatthias Ringwald                 hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_PLACE_CALL_WITH_NUMBER, last_dialed_number);
14149cae807eSMatthias Ringwald             } else {
14159cae807eSMatthias Ringwald                 printf("\nLast number missing: reject call");
14169cae807eSMatthias Ringwald                 hfp_ag_outgoing_call_rejected();
14179cae807eSMatthias Ringwald             }
14189cae807eSMatthias Ringwald             break;
14199cae807eSMatthias Ringwald         }
1420aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_REJECTED:
1421a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1422a0ffb263SMatthias Ringwald             if (!hfp_connection){
1423a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1424aa4dd815SMatthias Ringwald                 break;
1425aa4dd815SMatthias Ringwald             }
1426d210d9c4SMatthias Ringwald 
1427d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_REJECTED);
1428a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1429a0ffb263SMatthias Ringwald             hfp_connection->send_error = 1;
1430a0ffb263SMatthias Ringwald             hfp_run_for_context(hfp_connection);
1431aa4dd815SMatthias Ringwald             break;
1432aa4dd815SMatthias Ringwald 
1433d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ACCEPTED:{
1434a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_INITIATED);
1435a0ffb263SMatthias Ringwald             if (!hfp_connection){
1436a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in initiated state");
1437aa4dd815SMatthias Ringwald                 break;
1438aa4dd815SMatthias Ringwald             }
1439aa4dd815SMatthias Ringwald 
1440a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1441a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_DIALING;
1442aa4dd815SMatthias Ringwald 
1443aa4dd815SMatthias Ringwald             // trigger callsetup to be
1444d0c20769SMatthias Ringwald             int put_call_on_hold = hfp_gsm_call_status() == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT;
1445d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ACCEPTED);
1446d210d9c4SMatthias Ringwald 
1447d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1448aa4dd815SMatthias Ringwald             indicator_index = get_ag_indicator_index_for_name("callsetup");
1449a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
1450aa4dd815SMatthias Ringwald 
1451aa4dd815SMatthias Ringwald             // put current call on hold if active
1452d210d9c4SMatthias Ringwald             if (put_call_on_hold){
1453aa4dd815SMatthias Ringwald                 printf("AG putting current call on hold for new outgoing call\n");
1454d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1455aa4dd815SMatthias Ringwald                 indicator_index = get_ag_indicator_index_for_name("callheld");
1456a0ffb263SMatthias Ringwald                 hfp_ag_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[indicator_index]);
1457aa4dd815SMatthias Ringwald             }
1458aa4dd815SMatthias Ringwald 
1459aa4dd815SMatthias Ringwald             // start audio if needed
1460a0ffb263SMatthias Ringwald             hfp_ag_establish_audio_connection(hfp_connection->remote_addr);
1461aa4dd815SMatthias Ringwald             break;
1462d210d9c4SMatthias Ringwald         }
1463aa4dd815SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_RINGING:
1464a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1465a0ffb263SMatthias Ringwald             if (!hfp_connection){
1466a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection in dialing state");
1467aa4dd815SMatthias Ringwald                 break;
1468aa4dd815SMatthias Ringwald             }
1469d210d9c4SMatthias Ringwald 
1470d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_RINGING);
1471a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_OUTGOING_RINGING;
1472d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1473aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1474aa4dd815SMatthias Ringwald             break;
1475aa4dd815SMatthias Ringwald 
1476d210d9c4SMatthias Ringwald         case HFP_AG_OUTGOING_CALL_ESTABLISHED:{
1477aa4dd815SMatthias Ringwald             // get outgoing call
1478a0ffb263SMatthias Ringwald             hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_RINGING);
1479a0ffb263SMatthias Ringwald             if (!hfp_connection){
1480a0ffb263SMatthias Ringwald                 hfp_connection = hfp_ag_connection_for_call_state(HFP_CALL_OUTGOING_DIALING);
1481aa4dd815SMatthias Ringwald             }
1482a0ffb263SMatthias Ringwald             if (!hfp_connection){
1483a0ffb263SMatthias Ringwald                 log_info("hfp_ag_call_sm: did not find outgoing hfp_connection");
1484aa4dd815SMatthias Ringwald                 break;
1485aa4dd815SMatthias Ringwald             }
1486d210d9c4SMatthias Ringwald 
1487d0c20769SMatthias 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;
1488d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_OUTGOING_CALL_ESTABLISHED);
1489a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1490d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1491d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1492aa4dd815SMatthias Ringwald             hfp_ag_transfer_call_state();
1493aa4dd815SMatthias Ringwald             hfp_ag_transfer_callsetup_state();
1494d210d9c4SMatthias Ringwald             if (CALLHELD_STATUS_CALL_ON_HOLD_AND_NO_ACTIVE_CALLS){
1495d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1496aa4dd815SMatthias Ringwald                 hfp_ag_transfer_callheld_state();
14973deb3ec6SMatthias Ringwald             }
14983deb3ec6SMatthias Ringwald             break;
1499d210d9c4SMatthias Ringwald         }
1500d210d9c4SMatthias Ringwald 
1501d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_USER_BUSY:
1502d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_USER_BUSY);
1503d0c20769SMatthias Ringwald             hfp_ag_set_callsetup_indicator();
1504a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1505a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1506d210d9c4SMatthias Ringwald             printf("AG: Call Waiting, User Busy\n");
1507d210d9c4SMatthias Ringwald             break;
1508d210d9c4SMatthias Ringwald 
1509d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1510d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1511d0c20769SMatthias Ringwald             int call_held = hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD;
1512d210d9c4SMatthias Ringwald 
1513d210d9c4SMatthias Ringwald             // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1514d0c20769SMatthias Ringwald             if (call_held || call_setup_in_progress){
1515a0ffb263SMatthias Ringwald                 hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1516d0c20769SMatthias Ringwald 
1517d0c20769SMatthias Ringwald             }
1518d0c20769SMatthias Ringwald 
1519d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
1520d210d9c4SMatthias Ringwald                 printf("AG: Call Dropped, Accept new call\n");
1521d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1522a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1523d210d9c4SMatthias Ringwald             } else {
1524d210d9c4SMatthias Ringwald                 printf("AG: Call Dropped, Resume held call\n");
1525d210d9c4SMatthias Ringwald             }
1526d210d9c4SMatthias Ringwald             if (call_held){
1527d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1528a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1529d210d9c4SMatthias Ringwald             }
1530d0c20769SMatthias Ringwald 
1531a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1532d210d9c4SMatthias Ringwald             break;
1533d210d9c4SMatthias Ringwald         }
1534d0c20769SMatthias Ringwald 
1535d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL:{
1536d210d9c4SMatthias Ringwald             // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1537d210d9c4SMatthias Ringwald             // only update if callsetup changed
1538d0c20769SMatthias Ringwald             int call_setup_in_progress = hfp_gsm_callsetup_status() != HFP_CALLSETUP_STATUS_NO_CALL_SETUP_IN_PROGRESS;
1539a0ffb263SMatthias Ringwald             hfp_gsm_handle_event_with_call_index(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection->call_index);
1540d0c20769SMatthias Ringwald 
1541d210d9c4SMatthias Ringwald             if (call_setup_in_progress){
1542d210d9c4SMatthias Ringwald                 printf("AG: Call on Hold, Accept new call\n");
1543d0c20769SMatthias Ringwald                 hfp_ag_set_callsetup_indicator();
1544a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callsetup_indicator_index, 1);
1545d210d9c4SMatthias Ringwald             } else {
1546d210d9c4SMatthias Ringwald                 printf("AG: Swap calls\n");
1547d210d9c4SMatthias Ringwald             }
1548d0c20769SMatthias Ringwald 
1549d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1550d0c20769SMatthias Ringwald             // hfp_ag_set_callheld_state(HFP_CALLHELD_STATUS_CALL_ON_HOLD_OR_SWAPPED);
1551a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1552a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1553d210d9c4SMatthias Ringwald             break;
1554d210d9c4SMatthias Ringwald         }
1555d0c20769SMatthias Ringwald 
1556d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_ADD_HELD_CALL:
1557d210d9c4SMatthias Ringwald             // Adds a held call to the conversation.
1558d0c20769SMatthias Ringwald             if (hfp_gsm_callheld_status() != HFP_CALLHELD_STATUS_NO_CALLS_HELD){
1559d210d9c4SMatthias Ringwald                 printf("AG: Join 3-way-call\n");
1560d210d9c4SMatthias Ringwald                 hfp_gsm_handle_event(HFP_AG_CALL_HOLD_ADD_HELD_CALL);
1561d0c20769SMatthias Ringwald                 hfp_ag_set_callheld_indicator();
1562a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1563a0ffb263SMatthias Ringwald                 hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_CONFERENCE_CALL);
1564d210d9c4SMatthias Ringwald             }
1565a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_ACTIVE;
1566d210d9c4SMatthias Ringwald             break;
1567d210d9c4SMatthias Ringwald         case HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS:
1568d210d9c4SMatthias Ringwald             // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer)
1569d210d9c4SMatthias Ringwald             hfp_gsm_handle_event(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS);
1570d210d9c4SMatthias Ringwald             printf("AG: Transfer call -> Connect two calls and disconnect\n");
1571d210d9c4SMatthias Ringwald             hfp_ag_set_call_indicator();
1572d0c20769SMatthias Ringwald             hfp_ag_set_callheld_indicator();
1573a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, call_indicator_index, 1);
1574a0ffb263SMatthias Ringwald             hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, callheld_indicator_index, 1);
1575a0ffb263SMatthias Ringwald             hfp_connection->call_state = HFP_CALL_IDLE;
1576d210d9c4SMatthias Ringwald             break;
1577ce263fc8SMatthias Ringwald 
15783deb3ec6SMatthias Ringwald         default:
15793deb3ec6SMatthias Ringwald             break;
15803deb3ec6SMatthias Ringwald     }
1581d210d9c4SMatthias Ringwald 
1582d0c20769SMatthias Ringwald 
15833deb3ec6SMatthias Ringwald }
15843deb3ec6SMatthias Ringwald 
15859cae807eSMatthias Ringwald 
1586a0ffb263SMatthias Ringwald static void hfp_ag_send_call_status(hfp_connection_t * hfp_connection, int call_index){
15879cae807eSMatthias Ringwald     hfp_gsm_call_t * active_call = hfp_gsm_call(call_index);
15889cae807eSMatthias Ringwald     if (!active_call) return;
15899cae807eSMatthias Ringwald 
15909cae807eSMatthias Ringwald     int idx = active_call->index;
15919cae807eSMatthias Ringwald     hfp_enhanced_call_dir_t dir = active_call->direction;
15929cae807eSMatthias Ringwald     hfp_enhanced_call_status_t status = active_call->enhanced_status;
15939cae807eSMatthias Ringwald     hfp_enhanced_call_mode_t mode = active_call->mode;
15949cae807eSMatthias Ringwald     hfp_enhanced_call_mpty_t mpty = active_call->mpty;
15959cae807eSMatthias Ringwald     uint8_t type = active_call->clip_type;
15969cae807eSMatthias Ringwald     char * number = active_call->clip_number;
15979cae807eSMatthias Ringwald 
15989cae807eSMatthias Ringwald     char buffer[100];
15999cae807eSMatthias Ringwald     // TODO: check length of a buffer, to fit the MTU
16009cae807eSMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "\r\n%s: %d,%d,%d,%d,%d", HFP_LIST_CURRENT_CALLS, idx, dir, status, mode, mpty);
16019cae807eSMatthias Ringwald     if (number){
16029cae807eSMatthias Ringwald         offset += snprintf(buffer+offset, sizeof(buffer)-offset, ", \"%s\",%u", number, type);
16039cae807eSMatthias Ringwald     }
16049cae807eSMatthias Ringwald     snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
16059cae807eSMatthias 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,
16069cae807eSMatthias Ringwald        mode, mpty, type, number);
1607a0ffb263SMatthias Ringwald     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
16089cae807eSMatthias Ringwald }
16099cae807eSMatthias Ringwald 
1610a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t *hfp_connection){
1611a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
1612a0ffb263SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return;
16133deb3ec6SMatthias Ringwald 
1614a0ffb263SMatthias Ringwald     if (hfp_connection->send_status_of_current_calls){
1615a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1616a0ffb263SMatthias Ringwald         if (hfp_connection->next_call_index < hfp_gsm_get_number_of_calls()){
1617a0ffb263SMatthias Ringwald             hfp_connection->next_call_index++;
1618a0ffb263SMatthias Ringwald             hfp_ag_send_call_status(hfp_connection, hfp_connection->next_call_index);
16199cae807eSMatthias Ringwald         } else {
1620a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1621a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1622a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 0;
16239cae807eSMatthias Ringwald         }
1624ce263fc8SMatthias Ringwald         return;
1625ce263fc8SMatthias Ringwald     }
1626ce263fc8SMatthias Ringwald 
1627a0ffb263SMatthias Ringwald     if (hfp_connection->ag_notify_incoming_call_waiting){
1628a0ffb263SMatthias Ringwald         hfp_connection->ag_notify_incoming_call_waiting = 0;
1629a0ffb263SMatthias Ringwald         hfp_ag_send_call_waiting_notification(hfp_connection->rfcomm_cid);
1630aa4dd815SMatthias Ringwald         return;
1631aa4dd815SMatthias Ringwald     }
1632aa4dd815SMatthias Ringwald 
1633a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_UNKNOWN){
1634a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1635a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1636a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1637a0ffb263SMatthias Ringwald         hfp_ag_error(hfp_connection->rfcomm_cid);
1638a0ffb263SMatthias Ringwald         return;
1639a0ffb263SMatthias Ringwald     }
1640a0ffb263SMatthias Ringwald 
1641a0ffb263SMatthias Ringwald     if (hfp_connection->send_error){
1642a0ffb263SMatthias Ringwald         hfp_connection->send_error = 0;
1643a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1644a0ffb263SMatthias Ringwald         hfp_ag_error(hfp_connection->rfcomm_cid);
1645aa4dd815SMatthias Ringwald         return;
1646aa4dd815SMatthias Ringwald     }
1647aa4dd815SMatthias Ringwald 
1648ce263fc8SMatthias Ringwald     // note: before update AG indicators and ok_pending
1649a0ffb263SMatthias Ringwald     if (hfp_connection->send_response_and_hold_status){
1650a0ffb263SMatthias Ringwald         int status = hfp_connection->send_response_and_hold_status - 1;
1651a0ffb263SMatthias Ringwald         hfp_connection->send_response_and_hold_status = 0;
1652a0ffb263SMatthias Ringwald         hfp_ag_set_response_and_hold(hfp_connection->rfcomm_cid, status);
1653ce263fc8SMatthias Ringwald         return;
1654ce263fc8SMatthias Ringwald     }
1655ce263fc8SMatthias Ringwald 
1656a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending){
1657a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 0;
1658a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1659a0ffb263SMatthias Ringwald         hfp_ag_ok(hfp_connection->rfcomm_cid);
1660ce263fc8SMatthias Ringwald         return;
1661ce263fc8SMatthias Ringwald     }
1662ce263fc8SMatthias Ringwald 
1663aa4dd815SMatthias Ringwald     // update AG indicators
1664a0ffb263SMatthias Ringwald     if (hfp_connection->ag_indicators_status_update_bitmap){
1665aa4dd815SMatthias Ringwald         int i;
1666a0ffb263SMatthias Ringwald         for (i=0;i<hfp_connection->ag_indicators_nr;i++){
1667a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->ag_indicators_status_update_bitmap, i)){
1668a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, i, 0);
1669a0ffb263SMatthias Ringwald                 if (!hfp_connection->enable_status_update_for_ag_indicators) {
1670ce263fc8SMatthias Ringwald                     log_info("+CMER:3,0,0,0 - not sending update for '%s'", hfp_ag_indicators[i].name);
1671ce263fc8SMatthias Ringwald                     break;
1672ce263fc8SMatthias Ringwald                 }
1673a0ffb263SMatthias Ringwald                 hfp_ag_transfer_ag_indicators_status_cmd(hfp_connection->rfcomm_cid, &hfp_ag_indicators[i]);
1674aa4dd815SMatthias Ringwald                 return;
1675aa4dd815SMatthias Ringwald             }
1676aa4dd815SMatthias Ringwald         }
1677aa4dd815SMatthias Ringwald     }
1678aa4dd815SMatthias Ringwald 
1679a0ffb263SMatthias Ringwald     if (hfp_connection->ag_ring){
1680a0ffb263SMatthias Ringwald         hfp_connection->ag_ring = 0;
1681a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1682a0ffb263SMatthias Ringwald         hfp_ag_ring(hfp_connection->rfcomm_cid);
1683aa4dd815SMatthias Ringwald         return;
1684aa4dd815SMatthias Ringwald     }
1685aa4dd815SMatthias Ringwald 
1686a0ffb263SMatthias Ringwald     if (hfp_connection->ag_send_clip){
1687a0ffb263SMatthias Ringwald         hfp_connection->ag_send_clip = 0;
1688a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1689a0ffb263SMatthias Ringwald         hfp_ag_send_clip(hfp_connection->rfcomm_cid);
1690aa4dd815SMatthias Ringwald         return;
1691aa4dd815SMatthias Ringwald     }
1692aa4dd815SMatthias Ringwald 
1693a0ffb263SMatthias Ringwald     if (hfp_connection->send_phone_number_for_voice_tag){
1694a0ffb263SMatthias Ringwald         hfp_connection->send_phone_number_for_voice_tag = 0;
1695a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1696a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
1697a0ffb263SMatthias Ringwald         hfp_ag_send_phone_number_for_voice_tag_cmd(hfp_connection->rfcomm_cid);
1698aa4dd815SMatthias Ringwald         return;
1699aa4dd815SMatthias Ringwald     }
1700aa4dd815SMatthias Ringwald 
1701a0ffb263SMatthias Ringwald     if (hfp_connection->send_subscriber_number){
1702a0ffb263SMatthias Ringwald         if (hfp_connection->next_subscriber_number_to_send < subscriber_numbers_count){
1703a0ffb263SMatthias Ringwald             hfp_phone_number_t phone = subscriber_numbers[hfp_connection->next_subscriber_number_to_send++];
1704a0ffb263SMatthias Ringwald             hfp_send_subscriber_number_cmd(hfp_connection->rfcomm_cid, phone.type, phone.number);
1705ce263fc8SMatthias Ringwald         } else {
1706a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 0;
1707a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1708a0ffb263SMatthias Ringwald             hfp_ag_ok(hfp_connection->rfcomm_cid);
1709ce263fc8SMatthias Ringwald         }
1710a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1711ce263fc8SMatthias Ringwald     }
1712ce263fc8SMatthias Ringwald 
1713a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
1714a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
1715a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1716a0ffb263SMatthias Ringwald         hfp_ag_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
1717aa4dd815SMatthias Ringwald         return;
1718aa4dd815SMatthias Ringwald     }
1719aa4dd815SMatthias Ringwald 
1720a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
1721a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
1722a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
1723a0ffb263SMatthias Ringwald         hfp_ag_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
17243deb3ec6SMatthias Ringwald         return;
17253deb3ec6SMatthias Ringwald     }
17263deb3ec6SMatthias Ringwald 
1727a0ffb263SMatthias Ringwald     if (hfp_connection->send_ag_status_indicators){
1728a0ffb263SMatthias Ringwald         hfp_connection->send_ag_status_indicators = 0;
1729a0ffb263SMatthias Ringwald         hfp_ag_retrieve_indicators_status_cmd(hfp_connection->rfcomm_cid);
1730ce263fc8SMatthias Ringwald         return;
1731ce263fc8SMatthias Ringwald     }
1732ce263fc8SMatthias Ringwald 
1733a0ffb263SMatthias Ringwald     int done = hfp_ag_run_for_context_service_level_connection(hfp_connection);
1734aa4dd815SMatthias Ringwald     if (!done){
1735a0ffb263SMatthias Ringwald         done = hfp_ag_run_for_context_service_level_connection_queries(hfp_connection);
17363deb3ec6SMatthias Ringwald     }
17373deb3ec6SMatthias Ringwald 
1738aa4dd815SMatthias Ringwald     if (!done){
1739a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
1740aa4dd815SMatthias Ringwald     }
1741aa4dd815SMatthias Ringwald 
1742aa4dd815SMatthias Ringwald     if (!done){
1743a0ffb263SMatthias Ringwald         done = hfp_ag_run_for_audio_connection(hfp_connection);
1744aa4dd815SMatthias Ringwald     }
17453deb3ec6SMatthias Ringwald 
1746a0ffb263SMatthias Ringwald     if (hfp_connection->command == HFP_CMD_NONE && !done){
1747a0ffb263SMatthias Ringwald         // log_info("hfp_connection->command == HFP_CMD_NONE");
1748a0ffb263SMatthias Ringwald         switch(hfp_connection->state){
17493deb3ec6SMatthias Ringwald             case HFP_W2_DISCONNECT_RFCOMM:
1750a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
1751a0ffb263SMatthias Ringwald                 rfcomm_disconnect(hfp_connection->rfcomm_cid);
17523deb3ec6SMatthias Ringwald                 break;
17533deb3ec6SMatthias Ringwald             default:
17543deb3ec6SMatthias Ringwald                 break;
17553deb3ec6SMatthias Ringwald         }
17563deb3ec6SMatthias Ringwald     }
17573deb3ec6SMatthias Ringwald     if (done){
1758a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_NONE;
17593deb3ec6SMatthias Ringwald     }
17603deb3ec6SMatthias Ringwald }
1761ce263fc8SMatthias Ringwald static hfp_generic_status_indicator_t *get_hf_indicator_by_number(int number){
1762ce263fc8SMatthias Ringwald     int i;
1763a0ffb263SMatthias Ringwald     for (i=0;i< hfp_generic_status_indicators_nr;i++){
1764a0ffb263SMatthias Ringwald         hfp_generic_status_indicator_t * indicator = &hfp_generic_status_indicators[i];
1765ce263fc8SMatthias Ringwald         if (indicator->uuid == number){
1766ce263fc8SMatthias Ringwald             return indicator;
1767ce263fc8SMatthias Ringwald         }
1768ce263fc8SMatthias Ringwald     }
1769ce263fc8SMatthias Ringwald     return NULL;
1770ce263fc8SMatthias Ringwald }
17713deb3ec6SMatthias Ringwald 
1772aa4dd815SMatthias Ringwald static void hfp_handle_rfcomm_data(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1773a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
1774a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
17751e35c04dSMatthias Ringwald 
17761e35c04dSMatthias Ringwald     char last_char = packet[size-1];
17771e35c04dSMatthias Ringwald     packet[size-1] = 0;
17781e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
17791e35c04dSMatthias Ringwald     packet[size-1] = last_char;
17801e35c04dSMatthias Ringwald 
17813deb3ec6SMatthias Ringwald     int pos;
17823deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
1783a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 0);
1784aa4dd815SMatthias Ringwald     }
1785ce263fc8SMatthias Ringwald     hfp_generic_status_indicator_t * indicator;
1786ce263fc8SMatthias Ringwald     int value;
1787a0ffb263SMatthias Ringwald     switch(hfp_connection->command){
1788ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_QUERY:
1789ce263fc8SMatthias Ringwald             if (hfp_ag_response_and_hold_active){
1790a0ffb263SMatthias Ringwald                 hfp_connection->send_response_and_hold_status = HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD + 1;
1791ce263fc8SMatthias Ringwald             }
1792a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1793ce263fc8SMatthias Ringwald             break;
1794ce263fc8SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_COMMAND:
1795a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1796ce263fc8SMatthias Ringwald             printf("HF Response and Hold: %u\n", value);
1797ce263fc8SMatthias Ringwald             switch(value){
1798ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_INCOMING_ON_HOLD:
1799a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_HF, hfp_connection);
1800ce263fc8SMatthias Ringwald                     break;
1801ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_ACCEPTED:
1802a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_HF, hfp_connection);
1803ce263fc8SMatthias Ringwald                     break;
1804ce263fc8SMatthias Ringwald                 case HFP_RESPONSE_AND_HOLD_HELD_INCOMING_REJECTED:
1805a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_HF, hfp_connection);
1806ce263fc8SMatthias Ringwald                     break;
1807ce263fc8SMatthias Ringwald                 default:
1808ce263fc8SMatthias Ringwald                     break;
1809ce263fc8SMatthias Ringwald             }
1810a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1811ce263fc8SMatthias Ringwald             break;
1812ce263fc8SMatthias Ringwald         case HFP_CMD_HF_INDICATOR_STATUS:
1813a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1814ce263fc8SMatthias Ringwald             // find indicator by assigned number
1815a0ffb263SMatthias Ringwald             indicator = get_hf_indicator_by_number(hfp_connection->parser_indicator_index);
1816ce263fc8SMatthias Ringwald             if (!indicator){
1817a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1818ce263fc8SMatthias Ringwald                 break;
1819ce263fc8SMatthias Ringwald             }
1820a0ffb263SMatthias Ringwald             value = atoi((char *)&hfp_connection->line_buffer[0]);
1821ce263fc8SMatthias Ringwald             switch (indicator->uuid){
1822ce263fc8SMatthias Ringwald                 case 1: // enhanced security
1823ce263fc8SMatthias Ringwald                     if (value > 1) {
1824a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1825ce263fc8SMatthias Ringwald                         return;
1826ce263fc8SMatthias Ringwald                     }
1827ce263fc8SMatthias Ringwald                     printf("HF Indicator 'enhanced security' set to %u\n", value);
1828ce263fc8SMatthias Ringwald                     break;
1829ce263fc8SMatthias Ringwald                 case 2: // battery level
1830ce263fc8SMatthias Ringwald                     if (value > 100){
1831a0ffb263SMatthias Ringwald                         hfp_connection->send_error = 1;
1832ce263fc8SMatthias Ringwald                         return;
1833ce263fc8SMatthias Ringwald                     }
1834ce263fc8SMatthias Ringwald                     printf("HF Indicator 'battery' set to %u\n", value);
1835ce263fc8SMatthias Ringwald                     break;
1836ce263fc8SMatthias Ringwald                 default:
1837ce263fc8SMatthias Ringwald                     printf("HF Indicator unknown set to %u\n", value);
1838ce263fc8SMatthias Ringwald                     break;
1839ce263fc8SMatthias Ringwald             }
1840a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1841ce263fc8SMatthias Ringwald             break;
1842ce263fc8SMatthias Ringwald         case HFP_CMD_RETRIEVE_AG_INDICATORS_STATUS:
1843ce263fc8SMatthias Ringwald             // expected by SLC state machine
1844a0ffb263SMatthias Ringwald             if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) break;
1845a0ffb263SMatthias Ringwald             hfp_connection->send_ag_indicators_segment = 0;
1846a0ffb263SMatthias Ringwald             hfp_connection->send_ag_status_indicators = 1;
1847ce263fc8SMatthias Ringwald             break;
1848ce263fc8SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1849a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1850a0ffb263SMatthias Ringwald             hfp_connection->next_call_index = 0;
1851a0ffb263SMatthias Ringwald             hfp_connection->send_status_of_current_calls = 1;
1852ce263fc8SMatthias Ringwald             break;
1853ce263fc8SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
1854ce263fc8SMatthias Ringwald             if (subscriber_numbers_count == 0){
1855a0ffb263SMatthias Ringwald                 hfp_ag_ok(hfp_connection->rfcomm_cid);
1856ce263fc8SMatthias Ringwald                 break;
1857ce263fc8SMatthias Ringwald             }
1858a0ffb263SMatthias Ringwald             hfp_connection->next_subscriber_number_to_send = 0;
1859a0ffb263SMatthias Ringwald             hfp_connection->send_subscriber_number = 1;
1860ce263fc8SMatthias Ringwald             break;
1861c1797c7dSMatthias Ringwald         case HFP_CMD_TRANSMIT_DTMF_CODES:
1862a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1863a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_TRANSMIT_DTMF_CODES, (const char *) &hfp_connection->line_buffer[0]);
1864c1797c7dSMatthias Ringwald             break;
1865aa4dd815SMatthias Ringwald         case HFP_CMD_HF_REQUEST_PHONE_NUMBER:
1866a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1867a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_ATTACH_NUMBER_TO_VOICE_TAG);
1868aa4dd815SMatthias Ringwald             break;
1869aa4dd815SMatthias Ringwald         case HFP_CMD_TURN_OFF_EC_AND_NR:
1870a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1871aa4dd815SMatthias Ringwald             if (get_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION)){
1872a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
1873a0ffb263SMatthias Ringwald                 hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_EC_NR_FUNCTION, hfp_connection->ag_echo_and_noise_reduction);
1874a0ffb263SMatthias Ringwald                 printf("AG: EC/NR = %u\n", hfp_connection->ag_echo_and_noise_reduction);
1875aa4dd815SMatthias Ringwald             } else {
1876a0ffb263SMatthias Ringwald                 hfp_connection->send_error = 1;
1877aa4dd815SMatthias Ringwald             }
1878aa4dd815SMatthias Ringwald             break;
1879aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_ANSWERED:
1880a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1881aa4dd815SMatthias Ringwald             printf("HFP: ATA\n");
1882a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_HF, hfp_connection);
1883aa4dd815SMatthias Ringwald             break;
1884aa4dd815SMatthias Ringwald         case HFP_CMD_HANG_UP_CALL:
1885a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1886a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1887a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_HF, hfp_connection);
1888aa4dd815SMatthias Ringwald             break;
1889aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_HOLD: {
1890aa4dd815SMatthias Ringwald             // TODO: fully implement this
1891a0ffb263SMatthias Ringwald             log_error("HFP: unhandled call hold type %c", hfp_connection->line_buffer[0]);
1892a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1893a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1894a0ffb263SMatthias Ringwald             hfp_connection->call_index = 0;
1895d0c20769SMatthias Ringwald 
1896a0ffb263SMatthias Ringwald             if (hfp_connection->line_buffer[1] != '\0'){
1897a0ffb263SMatthias Ringwald                 hfp_connection->call_index = atoi((char *)&hfp_connection->line_buffer[1]);
1898d0c20769SMatthias Ringwald             }
1899d210d9c4SMatthias Ringwald 
1900a0ffb263SMatthias Ringwald             switch (hfp_connection->line_buffer[0]){
1901aa4dd815SMatthias Ringwald                 case '0':
1902d0c20769SMatthias Ringwald                     // Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
1903a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_USER_BUSY, hfp_connection);
1904aa4dd815SMatthias Ringwald                     break;
1905aa4dd815SMatthias Ringwald                 case '1':
1906d0c20769SMatthias Ringwald                     // Releases all active calls (if any exist) and accepts the other (held or waiting) call.
1907d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1908d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1909a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_RELEASE_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1910aa4dd815SMatthias Ringwald                     break;
1911aa4dd815SMatthias Ringwald                 case '2':
1912d0c20769SMatthias Ringwald                     // Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
1913d0c20769SMatthias Ringwald                     // Where both a held and a waiting call exist, the above procedures shall apply to the
1914d0c20769SMatthias Ringwald                     // waiting call (i.e., not to the held call) in conflicting situation.
1915a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_PARK_ACTIVE_ACCEPT_HELD_OR_WAITING_CALL, hfp_connection);
1916aa4dd815SMatthias Ringwald                     break;
1917aa4dd815SMatthias Ringwald                 case '3':
1918d0c20769SMatthias Ringwald                     // Adds a held call to the conversation.
1919a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_ADD_HELD_CALL, hfp_connection);
1920aa4dd815SMatthias Ringwald                     break;
1921aa4dd815SMatthias Ringwald                 case '4':
1922d0c20769SMatthias Ringwald                     // Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
1923a0ffb263SMatthias Ringwald                     hfp_ag_call_sm(HFP_AG_CALL_HOLD_EXIT_AND_JOIN_CALLS, hfp_connection);
1924aa4dd815SMatthias Ringwald                     break;
1925aa4dd815SMatthias Ringwald                 default:
1926aa4dd815SMatthias Ringwald                     break;
1927aa4dd815SMatthias Ringwald             }
1928aa4dd815SMatthias Ringwald             break;
1929aa4dd815SMatthias Ringwald         }
1930aa4dd815SMatthias Ringwald         case HFP_CMD_CALL_PHONE_NUMBER:
1931a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1932a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_INITIATED, hfp_connection);
1933aa4dd815SMatthias Ringwald             break;
1934aa4dd815SMatthias Ringwald         case HFP_CMD_REDIAL_LAST_NUMBER:
1935a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1936a0ffb263SMatthias Ringwald             hfp_ag_call_sm(HFP_AG_OUTGOING_REDIAL_INITIATED, hfp_connection);
1937aa4dd815SMatthias Ringwald             break;
1938aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CLIP:
1939a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1940a0ffb263SMatthias Ringwald             hfp_connection->clip_enabled = hfp_connection->line_buffer[8] != '0';
1941a0ffb263SMatthias Ringwald             log_info("hfp: clip set, now: %u", hfp_connection->clip_enabled);
1942a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1943aa4dd815SMatthias Ringwald             break;
1944aa4dd815SMatthias Ringwald         case HFP_CMD_ENABLE_CALL_WAITING_NOTIFICATION:
1945a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1946a0ffb263SMatthias Ringwald             hfp_connection->call_waiting_notification_enabled = hfp_connection->line_buffer[8] != '0';
1947a0ffb263SMatthias Ringwald             log_info("hfp: call waiting notification set, now: %u", hfp_connection->call_waiting_notification_enabled);
1948a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1949aa4dd815SMatthias Ringwald             break;
1950aa4dd815SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1951a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1952a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1953a0ffb263SMatthias Ringwald             printf("HF speaker gain = %u\n", hfp_connection->speaker_gain);
1954aa4dd815SMatthias Ringwald             break;
1955aa4dd815SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1956a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1957a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
1958a0ffb263SMatthias Ringwald             printf("HF microphone gain = %u\n", hfp_connection->microphone_gain);
1959aa4dd815SMatthias Ringwald             break;
1960aa4dd815SMatthias Ringwald         default:
1961aa4dd815SMatthias Ringwald             break;
19623deb3ec6SMatthias Ringwald     }
19633deb3ec6SMatthias Ringwald }
19643deb3ec6SMatthias Ringwald 
1965aa4dd815SMatthias Ringwald static void hfp_run(void){
1966d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_t it;
1967d63c37a1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1968d63c37a1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1969d63c37a1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1970a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
19713deb3ec6SMatthias Ringwald     }
19723deb3ec6SMatthias Ringwald }
19733deb3ec6SMatthias Ringwald 
1974ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
19753deb3ec6SMatthias Ringwald     switch (packet_type){
19763deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1977aa4dd815SMatthias Ringwald             hfp_handle_rfcomm_data(packet_type, channel, packet, size);
19783deb3ec6SMatthias Ringwald             break;
19793deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1980f4000eebSMatthias Ringwald             hfp_handle_hci_event(packet_type, packet, size);
1981aa4dd815SMatthias Ringwald             break;
19823deb3ec6SMatthias Ringwald         default:
19833deb3ec6SMatthias Ringwald             break;
19843deb3ec6SMatthias Ringwald     }
19853deb3ec6SMatthias Ringwald 
19863deb3ec6SMatthias Ringwald     hfp_run();
19873deb3ec6SMatthias Ringwald }
19883deb3ec6SMatthias Ringwald 
19891e35c04dSMatthias Ringwald 
1990a0ffb263SMatthias Ringwald void hfp_ag_init_codecs(int codecs_nr, uint8_t * codecs){
19913deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
19923deb3ec6SMatthias Ringwald         log_error("hfp_init: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
19933deb3ec6SMatthias Ringwald         return;
19943deb3ec6SMatthias Ringwald     }
19953deb3ec6SMatthias Ringwald     int i;
1996a0ffb263SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
19973deb3ec6SMatthias Ringwald     for (i=0; i < codecs_nr; i++){
19983deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
19993deb3ec6SMatthias Ringwald     }
2000a0ffb263SMatthias Ringwald }
20013deb3ec6SMatthias Ringwald 
2002a0ffb263SMatthias Ringwald void hfp_ag_init_supported_features(uint32_t supported_features){
2003a0ffb263SMatthias Ringwald     hfp_supported_features = supported_features;
2004a0ffb263SMatthias Ringwald }
20053deb3ec6SMatthias Ringwald 
2006a0ffb263SMatthias Ringwald void hfp_ag_init_ag_indicators(int ag_indicators_nr, hfp_ag_indicator_t * ag_indicators){
2007a0ffb263SMatthias Ringwald     hfp_ag_indicators_nr = ag_indicators_nr;
2008a0ffb263SMatthias Ringwald     memcpy(hfp_ag_indicators, ag_indicators, ag_indicators_nr * sizeof(hfp_ag_indicator_t));
2009a0ffb263SMatthias Ringwald }
20103deb3ec6SMatthias Ringwald 
2011a0ffb263SMatthias Ringwald void hfp_ag_init_hf_indicators(int hf_indicators_nr, hfp_generic_status_indicator_t * hf_indicators){
2012a0ffb263SMatthias Ringwald     if (hf_indicators_nr > HFP_MAX_NUM_HF_INDICATORS) return;
2013a0ffb263SMatthias Ringwald     hfp_generic_status_indicators_nr = hf_indicators_nr;
2014a0ffb263SMatthias Ringwald     memcpy(hfp_generic_status_indicators, hf_indicators, hf_indicators_nr * sizeof(hfp_generic_status_indicator_t));
2015a0ffb263SMatthias Ringwald }
2016a0ffb263SMatthias Ringwald 
2017a0ffb263SMatthias Ringwald void hfp_ag_init_call_hold_services(int call_hold_services_nr, const char * call_hold_services[]){
20183deb3ec6SMatthias Ringwald     hfp_ag_call_hold_services_nr = call_hold_services_nr;
20193deb3ec6SMatthias Ringwald     memcpy(hfp_ag_call_hold_services, call_hold_services, call_hold_services_nr * sizeof(char *));
2020a0ffb263SMatthias Ringwald }
2021a0ffb263SMatthias Ringwald 
2022a0ffb263SMatthias Ringwald 
2023a0ffb263SMatthias Ringwald void hfp_ag_init(uint16_t rfcomm_channel_nr){
2024d63c37a1SMatthias Ringwald     // register for HCI events
2025d63c37a1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
2026d63c37a1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
2027d63c37a1SMatthias Ringwald 
2028a0ffb263SMatthias Ringwald     l2cap_init();
2029d63c37a1SMatthias Ringwald 
2030d63c37a1SMatthias Ringwald     rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff);
2031aa4dd815SMatthias Ringwald 
2032d210d9c4SMatthias Ringwald     hfp_ag_response_and_hold_active = 0;
2033d210d9c4SMatthias Ringwald     subscriber_numbers = NULL;
2034d210d9c4SMatthias Ringwald     subscriber_numbers_count = 0;
2035d210d9c4SMatthias Ringwald 
2036d210d9c4SMatthias Ringwald     hfp_gsm_init();
20373deb3ec6SMatthias Ringwald }
20383deb3ec6SMatthias Ringwald 
20393deb3ec6SMatthias Ringwald void hfp_ag_establish_service_level_connection(bd_addr_t bd_addr){
20403deb3ec6SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, SDP_Handsfree);
20413deb3ec6SMatthias Ringwald }
20423deb3ec6SMatthias Ringwald 
20433deb3ec6SMatthias Ringwald void hfp_ag_release_service_level_connection(bd_addr_t bd_addr){
2044a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2045a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
2046a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20473deb3ec6SMatthias Ringwald }
20483deb3ec6SMatthias Ringwald 
20493deb3ec6SMatthias Ringwald void hfp_ag_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, hfp_cme_error_t error){
2050a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2051a0ffb263SMatthias Ringwald     if (!hfp_connection){
2052a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
20533deb3ec6SMatthias Ringwald         return;
20543deb3ec6SMatthias Ringwald     }
2055a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = 0;
2056a0ffb263SMatthias Ringwald     if (!hfp_connection->enable_extended_audio_gateway_error_report){
20573deb3ec6SMatthias Ringwald         return;
20583deb3ec6SMatthias Ringwald     }
2059a0ffb263SMatthias Ringwald     hfp_connection->extended_audio_gateway_error = error;
2060a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20613deb3ec6SMatthias Ringwald }
20623deb3ec6SMatthias Ringwald 
2063a0ffb263SMatthias Ringwald static void hfp_ag_setup_audio_connection(hfp_connection_t * hfp_connection){
2064a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
2065a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
20663deb3ec6SMatthias Ringwald 
2067a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 1;
2068aa4dd815SMatthias Ringwald 
2069a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
2070aa4dd815SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
2071a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
20723deb3ec6SMatthias Ringwald     }
2073aa4dd815SMatthias Ringwald 
2074a0ffb263SMatthias Ringwald     switch (hfp_connection->codecs_state){
2075aa4dd815SMatthias Ringwald         case HFP_CODECS_IDLE:
2076aa4dd815SMatthias Ringwald         case HFP_CODECS_RECEIVED_LIST:
2077aa4dd815SMatthias Ringwald         case HFP_CODECS_AG_RESEND_COMMON_CODEC:
2078aa4dd815SMatthias Ringwald         case HFP_CODECS_ERROR:
2079a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_AG_SEND_COMMON_CODEC;
2080aa4dd815SMatthias Ringwald             break;
2081aa4dd815SMatthias Ringwald         default:
2082aa4dd815SMatthias Ringwald             break;
2083aa4dd815SMatthias Ringwald     }
2084aa4dd815SMatthias Ringwald }
2085aa4dd815SMatthias Ringwald 
2086aa4dd815SMatthias Ringwald void hfp_ag_establish_audio_connection(bd_addr_t bd_addr){
2087aa4dd815SMatthias Ringwald     hfp_ag_establish_service_level_connection(bd_addr);
2088a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2089aa4dd815SMatthias Ringwald 
2090a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
2091a0ffb263SMatthias Ringwald     hfp_ag_setup_audio_connection(hfp_connection);
2092a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20933deb3ec6SMatthias Ringwald }
20943deb3ec6SMatthias Ringwald 
20953deb3ec6SMatthias Ringwald void hfp_ag_release_audio_connection(bd_addr_t bd_addr){
2096a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2097a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
2098a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
20993deb3ec6SMatthias Ringwald }
2100aa4dd815SMatthias Ringwald 
2101aa4dd815SMatthias Ringwald /**
2102aa4dd815SMatthias Ringwald  * @brief Enable in-band ring tone
2103aa4dd815SMatthias Ringwald  */
2104aa4dd815SMatthias Ringwald void hfp_ag_set_use_in_band_ring_tone(int use_in_band_ring_tone){
2105aa4dd815SMatthias Ringwald     if (get_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE) == use_in_band_ring_tone){
2106aa4dd815SMatthias Ringwald         return;
2107aa4dd815SMatthias Ringwald     }
2108aa4dd815SMatthias Ringwald     hfp_supported_features = store_bit(hfp_supported_features, HFP_AGSF_IN_BAND_RING_TONE, use_in_band_ring_tone);
2109aa4dd815SMatthias Ringwald 
2110665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2111665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2112665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2113a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2114a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_CHANGE_IN_BAND_RING_TONE_SETTING;
2115a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2116aa4dd815SMatthias Ringwald     }
2117aa4dd815SMatthias Ringwald }
2118aa4dd815SMatthias Ringwald 
2119aa4dd815SMatthias Ringwald /**
2120aa4dd815SMatthias Ringwald  * @brief Called from GSM
2121aa4dd815SMatthias Ringwald  */
2122aa4dd815SMatthias Ringwald void hfp_ag_incoming_call(void){
2123aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL, NULL);
2124aa4dd815SMatthias Ringwald }
2125aa4dd815SMatthias Ringwald 
2126aa4dd815SMatthias Ringwald /**
2127aa4dd815SMatthias Ringwald  * @brief number is stored.
2128aa4dd815SMatthias Ringwald  */
2129aa4dd815SMatthias Ringwald void hfp_ag_set_clip(uint8_t type, const char * number){
2130d0c20769SMatthias Ringwald     hfp_gsm_handle_event_with_clip(HFP_AG_SET_CLIP, type, number);
2131aa4dd815SMatthias Ringwald }
2132aa4dd815SMatthias Ringwald 
2133aa4dd815SMatthias Ringwald void hfp_ag_call_dropped(void){
2134aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_CALL_DROPPED, NULL);
2135aa4dd815SMatthias Ringwald }
2136aa4dd815SMatthias Ringwald 
2137aa4dd815SMatthias Ringwald // call from AG UI
2138aa4dd815SMatthias Ringwald void hfp_ag_answer_incoming_call(void){
2139aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_INCOMING_CALL_ACCEPTED_BY_AG, NULL);
2140aa4dd815SMatthias Ringwald }
2141aa4dd815SMatthias Ringwald 
2142ce263fc8SMatthias Ringwald void hfp_ag_join_held_call(void){
2143ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_HELD_CALL_JOINED_BY_AG, NULL);
2144ce263fc8SMatthias Ringwald }
2145ce263fc8SMatthias Ringwald 
2146aa4dd815SMatthias Ringwald void hfp_ag_terminate_call(void){
2147aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_TERMINATE_CALL_BY_AG, NULL);
2148aa4dd815SMatthias Ringwald }
2149aa4dd815SMatthias Ringwald 
2150aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_ringing(void){
2151aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_RINGING, NULL);
2152aa4dd815SMatthias Ringwald }
2153aa4dd815SMatthias Ringwald 
2154aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_established(void){
2155aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ESTABLISHED, NULL);
2156aa4dd815SMatthias Ringwald }
2157aa4dd815SMatthias Ringwald 
2158aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_rejected(void){
2159aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_REJECTED, NULL);
2160aa4dd815SMatthias Ringwald }
2161aa4dd815SMatthias Ringwald 
2162aa4dd815SMatthias Ringwald void hfp_ag_outgoing_call_accepted(void){
2163aa4dd815SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_OUTGOING_CALL_ACCEPTED, NULL);
2164aa4dd815SMatthias Ringwald }
2165ce263fc8SMatthias Ringwald 
2166ce263fc8SMatthias Ringwald void hfp_ag_hold_incoming_call(void){
2167ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_INCOMING_CALL_BY_AG, NULL);
2168ce263fc8SMatthias Ringwald }
2169ce263fc8SMatthias Ringwald 
2170ce263fc8SMatthias Ringwald void hfp_ag_accept_held_incoming_call(void) {
2171ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_ACCEPT_HELD_CALL_BY_AG, NULL);
2172ce263fc8SMatthias Ringwald }
2173ce263fc8SMatthias Ringwald 
2174ce263fc8SMatthias Ringwald void hfp_ag_reject_held_incoming_call(void){
2175ce263fc8SMatthias Ringwald     hfp_ag_call_sm(HFP_AG_RESPONSE_AND_HOLD_REJECT_HELD_CALL_BY_AG, NULL);
2176ce263fc8SMatthias Ringwald }
2177ce263fc8SMatthias Ringwald 
2178aa4dd815SMatthias Ringwald static void hfp_ag_set_ag_indicator(const char * name, int value){
2179aa4dd815SMatthias Ringwald     int indicator_index = get_ag_indicator_index_for_name(name);
2180aa4dd815SMatthias Ringwald     if (indicator_index < 0) return;
2181aa4dd815SMatthias Ringwald     hfp_ag_indicators[indicator_index].status = value;
2182aa4dd815SMatthias Ringwald 
2183ce263fc8SMatthias Ringwald 
2184665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
2185665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
2186665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2187a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
2188a0ffb263SMatthias Ringwald         if (! hfp_connection->ag_indicators[indicator_index].enabled) {
2189ce263fc8SMatthias Ringwald             log_info("AG indicator '%s' changed to %u but not enabled", hfp_ag_indicators[indicator_index].name, value);
2190ce263fc8SMatthias Ringwald             continue;
2191ce263fc8SMatthias Ringwald         }
2192ce263fc8SMatthias Ringwald         log_info("AG indicator '%s' changed to %u, request transfer statur", hfp_ag_indicators[indicator_index].name, value);
2193a0ffb263SMatthias Ringwald         hfp_connection->ag_indicators_status_update_bitmap = store_bit(hfp_connection->ag_indicators_status_update_bitmap, indicator_index, 1);
2194a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
2195aa4dd815SMatthias Ringwald     }
2196aa4dd815SMatthias Ringwald }
2197aa4dd815SMatthias Ringwald 
2198aa4dd815SMatthias Ringwald void hfp_ag_set_registration_status(int status){
2199aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("service", status);
2200aa4dd815SMatthias Ringwald }
2201aa4dd815SMatthias Ringwald 
2202aa4dd815SMatthias Ringwald void hfp_ag_set_signal_strength(int strength){
2203aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("signal", strength);
2204aa4dd815SMatthias Ringwald }
2205aa4dd815SMatthias Ringwald 
2206aa4dd815SMatthias Ringwald void hfp_ag_set_roaming_status(int status){
2207aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("roam", status);
2208aa4dd815SMatthias Ringwald }
2209aa4dd815SMatthias Ringwald 
2210aa4dd815SMatthias Ringwald void hfp_ag_set_battery_level(int level){
2211aa4dd815SMatthias Ringwald     hfp_ag_set_ag_indicator("battchg", level);
2212aa4dd815SMatthias Ringwald }
2213aa4dd815SMatthias Ringwald 
2214aa4dd815SMatthias Ringwald void hfp_ag_activate_voice_recognition(bd_addr_t bd_addr, int activate){
2215aa4dd815SMatthias Ringwald     if (!get_bit(hfp_supported_features, HFP_AGSF_VOICE_RECOGNITION_FUNCTION)) return;
2216a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2217aa4dd815SMatthias Ringwald 
2218a0ffb263SMatthias Ringwald     if (!get_bit(hfp_connection->remote_supported_features, HFP_HFSF_VOICE_RECOGNITION_FUNCTION)) {
2219aa4dd815SMatthias Ringwald         printf("AG cannot acivate voice recognition - not supported by HF\n");
2220aa4dd815SMatthias Ringwald         return;
2221aa4dd815SMatthias Ringwald     }
2222aa4dd815SMatthias Ringwald 
2223aa4dd815SMatthias Ringwald     if (activate){
2224aa4dd815SMatthias Ringwald         hfp_ag_establish_audio_connection(bd_addr);
2225aa4dd815SMatthias Ringwald     }
2226aa4dd815SMatthias Ringwald 
2227a0ffb263SMatthias Ringwald     hfp_connection->ag_activate_voice_recognition = activate;
2228a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_AG_ACTIVATE_VOICE_RECOGNITION;
2229a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2230aa4dd815SMatthias Ringwald }
2231aa4dd815SMatthias Ringwald 
2232aa4dd815SMatthias Ringwald void hfp_ag_set_microphone_gain(bd_addr_t bd_addr, int gain){
2233a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2234a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain != gain){
2235a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_SET_MICROPHONE_GAIN;
2236a0ffb263SMatthias Ringwald         hfp_connection->microphone_gain = gain;
2237a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 1;
2238aa4dd815SMatthias Ringwald     }
2239a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2240aa4dd815SMatthias Ringwald }
2241aa4dd815SMatthias Ringwald 
2242aa4dd815SMatthias Ringwald void hfp_ag_set_speaker_gain(bd_addr_t bd_addr, int gain){
2243a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2244a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain != gain){
2245a0ffb263SMatthias Ringwald         hfp_connection->speaker_gain = gain;
2246a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 1;
2247aa4dd815SMatthias Ringwald     }
2248a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2249aa4dd815SMatthias Ringwald }
2250aa4dd815SMatthias Ringwald 
2251aa4dd815SMatthias Ringwald void hfp_ag_send_phone_number_for_voice_tag(bd_addr_t bd_addr, const char * number){
2252a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2253aa4dd815SMatthias Ringwald     hfp_ag_set_clip(0, number);
2254a0ffb263SMatthias Ringwald     hfp_connection->send_phone_number_for_voice_tag = 1;
2255aa4dd815SMatthias Ringwald }
2256aa4dd815SMatthias Ringwald 
2257aa4dd815SMatthias Ringwald void hfp_ag_reject_phone_number_for_voice_tag(bd_addr_t bd_addr){
2258a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2259a0ffb263SMatthias Ringwald     hfp_connection->send_error = 1;
2260aa4dd815SMatthias Ringwald }
2261aa4dd815SMatthias Ringwald 
2262c1797c7dSMatthias Ringwald void hfp_ag_send_dtmf_code_done(bd_addr_t bd_addr){
2263a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2264a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 1;
2265c1797c7dSMatthias Ringwald }
2266aa4dd815SMatthias Ringwald 
2267ce263fc8SMatthias Ringwald void hfp_ag_set_subcriber_number_information(hfp_phone_number_t * numbers, int numbers_count){
2268ce263fc8SMatthias Ringwald     subscriber_numbers = numbers;
2269ce263fc8SMatthias Ringwald     subscriber_numbers_count = numbers_count;
2270ce263fc8SMatthias Ringwald }
2271ce263fc8SMatthias Ringwald 
22729cae807eSMatthias Ringwald void hfp_ag_clear_last_dialed_number(void){
22739cae807eSMatthias Ringwald     hfp_gsm_clear_last_dialed_number();
2274ce263fc8SMatthias Ringwald }
2275ce263fc8SMatthias Ringwald 
2276a0ffb263SMatthias Ringwald void hfp_ag_notify_incoming_call_waiting(bd_addr_t bd_addr){
2277a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
2278a0ffb263SMatthias Ringwald     if (!hfp_connection->call_waiting_notification_enabled) return;
2279a0ffb263SMatthias Ringwald 
2280a0ffb263SMatthias Ringwald     hfp_connection->ag_notify_incoming_call_waiting = 1;
2281a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
2282a0ffb263SMatthias Ringwald }
2283ce263fc8SMatthias Ringwald 
2284