xref: /btstack/src/classic/hfp_hf.c (revision 4f84bf367b1ba6b9143c82cc375382258d4a49e8)
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 //
40fffdd288SMatthias Ringwald // HFP Hands-Free (HF) 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 
5159c6af15SMatthias Ringwald #include "btstack_debug.h"
523deb3ec6SMatthias Ringwald #include "btstack_memory.h"
5359c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5459c6af15SMatthias Ringwald #include "classic/core.h"
5559c6af15SMatthias Ringwald #include "classic/hfp.h"
5659c6af15SMatthias Ringwald #include "classic/hfp_hf.h"
57efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
58746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
59023f2764SMatthias Ringwald #include "classic/sdp_util.h"
6059c6af15SMatthias Ringwald #include "hci.h"
6159c6af15SMatthias Ringwald #include "hci_cmd.h"
6259c6af15SMatthias Ringwald #include "hci_dump.h"
6359c6af15SMatthias Ringwald #include "l2cap.h"
643deb3ec6SMatthias Ringwald 
653deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
663deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
673deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
683deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
693deb3ec6SMatthias Ringwald 
703deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0;
713deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS];
72667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS];
73667ec068SMatthias Ringwald 
74667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9;
75667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9;
763deb3ec6SMatthias Ringwald 
7713839019SMatthias Ringwald static btstack_packet_handler_t hfp_callback;
783deb3ec6SMatthias Ringwald 
79ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
80ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
81ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
82ce263fc8SMatthias Ringwald 
83ce263fc8SMatthias Ringwald static char phone_number[25];
84ce263fc8SMatthias Ringwald 
85c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
86c5b64319SMatthias Ringwald 
8713839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
883deb3ec6SMatthias Ringwald     hfp_callback = callback;
893deb3ec6SMatthias Ringwald     if (callback == NULL){
903deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
913deb3ec6SMatthias Ringwald         return;
923deb3ec6SMatthias Ringwald     }
933deb3ec6SMatthias Ringwald     hfp_callback = callback;
94f4000eebSMatthias Ringwald     hfp_set_callback(callback);
953deb3ec6SMatthias Ringwald }
963deb3ec6SMatthias Ringwald 
9713839019SMatthias Ringwald static void hfp_hf_emit_subscriber_information(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t status, uint8_t bnip_type, const char * bnip_number){
98a0ffb263SMatthias Ringwald     if (!callback) return;
99a0ffb263SMatthias Ringwald     uint8_t event[31];
100a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
101a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
102a0ffb263SMatthias Ringwald     event[2] = event_subtype;
103a0ffb263SMatthias Ringwald     event[3] = status;
104a0ffb263SMatthias Ringwald     event[4] = bnip_type;
105a0ffb263SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 6) ? strlen(bnip_number) : sizeof(event) - 6;
106a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], bnip_number, size);
107a0ffb263SMatthias Ringwald     event[5 + size] = 0;
10813839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
109a0ffb263SMatthias Ringwald }
110a0ffb263SMatthias Ringwald 
11113839019SMatthias Ringwald static void hfp_hf_emit_type_and_number(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t bnip_type, const char * bnip_number){
112a0ffb263SMatthias Ringwald     if (!callback) return;
113a0ffb263SMatthias Ringwald     uint8_t event[30];
114a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
115a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
116a0ffb263SMatthias Ringwald     event[2] = event_subtype;
117a0ffb263SMatthias Ringwald     event[3] = bnip_type;
118a0ffb263SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 5) ? strlen(bnip_number) : sizeof(event) - 5;
119a0ffb263SMatthias Ringwald     strncpy((char*)&event[4], bnip_number, size);
120a0ffb263SMatthias Ringwald     event[4 + size] = 0;
12113839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
122a0ffb263SMatthias Ringwald }
123a0ffb263SMatthias Ringwald 
12413839019SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir,
125a0ffb263SMatthias Ringwald                 uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){
126a0ffb263SMatthias Ringwald     if (!callback) return;
127a0ffb263SMatthias Ringwald     uint8_t event[35];
128a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
129a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
130a0ffb263SMatthias Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
131a0ffb263SMatthias Ringwald     event[3] = clcc_idx;
132a0ffb263SMatthias Ringwald     event[4] = clcc_dir;
133a0ffb263SMatthias Ringwald     event[6] = clcc_status;
134a0ffb263SMatthias Ringwald     event[7] = clcc_mpty;
135a0ffb263SMatthias Ringwald     event[8] = bnip_type;
136a0ffb263SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 10) ? strlen(bnip_number) : sizeof(event) - 10;
137a0ffb263SMatthias Ringwald     strncpy((char*)&event[9], bnip_number, size);
138a0ffb263SMatthias Ringwald     event[9 + size] = 0;
13913839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
140a0ffb263SMatthias Ringwald }
141a0ffb263SMatthias Ringwald 
142a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
1433deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
144a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
145fcb08cdbSMilanka Ringwald     printf("local %d, remote %d\n", hf, ag);
1463deb3ec6SMatthias Ringwald     return hf && ag;
1473deb3ec6SMatthias Ringwald }
1483deb3ec6SMatthias Ringwald 
149a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
1503deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
151a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1523deb3ec6SMatthias Ringwald     return hf && ag;
1533deb3ec6SMatthias Ringwald }
1543deb3ec6SMatthias Ringwald 
1553deb3ec6SMatthias Ringwald 
156a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
1573deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
158a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
1593deb3ec6SMatthias Ringwald     return hf && ag;
1603deb3ec6SMatthias Ringwald }
1613deb3ec6SMatthias Ringwald 
162ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
1633deb3ec6SMatthias Ringwald 
164*4f84bf36SMatthias Ringwald void hfp_hf_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint16_t supported_features, int wide_band_speech){
1653deb3ec6SMatthias Ringwald     if (!name){
1663deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1673deb3ec6SMatthias Ringwald     }
1682ef54b27SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, SDP_Handsfree, rfcomm_channel_nr, name);
1693deb3ec6SMatthias Ringwald 
170*4f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
171*4f84bf36SMatthias Ringwald     //
172*4f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
173*4f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
174*4f84bf36SMatthias Ringwald     //
175*4f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
176*4f84bf36SMatthias Ringwald     if (supported_features & wide_band_speech){
177*4f84bf36SMatthias Ringwald         sdp_features |= 1 << 5; // Wide band speech bit
178*4f84bf36SMatthias Ringwald     }
179aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
180*4f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
181aa4dd815SMatthias Ringwald }
1823deb3ec6SMatthias Ringwald 
1833deb3ec6SMatthias Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
1843deb3ec6SMatthias Ringwald     char buffer[20];
1853deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
1863deb3ec6SMatthias Ringwald     // printf("exchange_supported_features %s\n", buffer);
1873deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1883deb3ec6SMatthias Ringwald }
1893deb3ec6SMatthias Ringwald 
1903deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
1913deb3ec6SMatthias Ringwald     char buffer[30];
1923deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_AVAILABLE_CODECS);
1933deb3ec6SMatthias Ringwald     offset += join(buffer+offset, sizeof(buffer)-offset, hfp_codecs, hfp_codecs_nr);
1943deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
1953deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1963deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1973deb3ec6SMatthias Ringwald }
1983deb3ec6SMatthias Ringwald 
1993deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
2003deb3ec6SMatthias Ringwald     char buffer[20];
2013deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_INDICATOR);
2023deb3ec6SMatthias Ringwald     // printf("retrieve_indicators %s\n", buffer);
2033deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2043deb3ec6SMatthias Ringwald }
2053deb3ec6SMatthias Ringwald 
2063deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
2073deb3ec6SMatthias Ringwald     char buffer[20];
2083deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_INDICATOR);
2093deb3ec6SMatthias Ringwald     // printf("retrieve_indicators_status %s\n", buffer);
2103deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2113deb3ec6SMatthias Ringwald }
2123deb3ec6SMatthias Ringwald 
2133deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2143deb3ec6SMatthias Ringwald     char buffer[20];
2153deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
2163deb3ec6SMatthias Ringwald     // printf("toggle_indicator_status_update %s\n", buffer);
2173deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2183deb3ec6SMatthias Ringwald }
2193deb3ec6SMatthias Ringwald 
2203deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2213deb3ec6SMatthias Ringwald     char buffer[50];
2223deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
2233deb3ec6SMatthias Ringwald     offset += join_bitmap(buffer+offset, sizeof(buffer)-offset, indicators_status, indicators_nr);
2243deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
2253deb3ec6SMatthias Ringwald     buffer[offset] = 0;
2263deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2273deb3ec6SMatthias Ringwald }
2283deb3ec6SMatthias Ringwald 
2293deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
2303deb3ec6SMatthias Ringwald     char buffer[20];
2313deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
2323deb3ec6SMatthias Ringwald     // printf("retrieve_can_hold_call %s\n", buffer);
2333deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2343deb3ec6SMatthias Ringwald }
2353deb3ec6SMatthias Ringwald 
2363deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2373deb3ec6SMatthias Ringwald     char buffer[30];
2383deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
2393deb3ec6SMatthias Ringwald     offset += join(buffer+offset, sizeof(buffer)-offset, hfp_indicators, hfp_indicators_nr);
2403deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
2413deb3ec6SMatthias Ringwald     buffer[offset] = 0;
2423deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2433deb3ec6SMatthias Ringwald }
2443deb3ec6SMatthias Ringwald 
2453deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
2463deb3ec6SMatthias Ringwald     char buffer[20];
2473deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_GENERIC_STATUS_INDICATOR);
2483deb3ec6SMatthias Ringwald     // printf("retrieve_supported_generic_status_indicators %s\n", buffer);
2493deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2503deb3ec6SMatthias Ringwald }
2513deb3ec6SMatthias Ringwald 
2523deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
2533deb3ec6SMatthias Ringwald     char buffer[20];
2543deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_GENERIC_STATUS_INDICATOR);
2553deb3ec6SMatthias Ringwald     // printf("list_initital_supported_generic_status_indicators %s\n", buffer);
2563deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2573deb3ec6SMatthias Ringwald }
2583deb3ec6SMatthias Ringwald 
2593deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
2603deb3ec6SMatthias Ringwald     char buffer[20];
2613deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=3,0\r\n", HFP_QUERY_OPERATOR_SELECTION);
2623deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2633deb3ec6SMatthias Ringwald }
2643deb3ec6SMatthias Ringwald 
2653deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
2663deb3ec6SMatthias Ringwald     char buffer[20];
2673deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_QUERY_OPERATOR_SELECTION);
2683deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2693deb3ec6SMatthias Ringwald }
2703deb3ec6SMatthias Ringwald 
2713deb3ec6SMatthias Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
2723deb3ec6SMatthias Ringwald     char buffer[20];
2733deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
2743deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2753deb3ec6SMatthias Ringwald }
2763deb3ec6SMatthias Ringwald 
2773deb3ec6SMatthias Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
2783deb3ec6SMatthias Ringwald     char buffer[20];
2793deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_TRIGGER_CODEC_CONNECTION_SETUP);
2803deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2813deb3ec6SMatthias Ringwald }
2823deb3ec6SMatthias Ringwald 
2833deb3ec6SMatthias Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
2843deb3ec6SMatthias Ringwald     char buffer[20];
2853deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec);
2863deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2873deb3ec6SMatthias Ringwald }
2883deb3ec6SMatthias Ringwald 
289ce263fc8SMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
290ce263fc8SMatthias Ringwald     char buffer[10];
291ce263fc8SMatthias Ringwald     sprintf(buffer, "%s\r\n", HFP_CALL_ANSWERED);
292ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
293ce263fc8SMatthias Ringwald }
294ce263fc8SMatthias Ringwald 
295ce263fc8SMatthias Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
296ce263fc8SMatthias Ringwald     char buffer[40];
297ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SET_MICROPHONE_GAIN, gain);
298ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
299ce263fc8SMatthias Ringwald }
300ce263fc8SMatthias Ringwald 
301ce263fc8SMatthias Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
302ce263fc8SMatthias Ringwald     char buffer[40];
303ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SET_SPEAKER_GAIN, gain);
304ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
305ce263fc8SMatthias Ringwald }
306ce263fc8SMatthias Ringwald 
307ce263fc8SMatthias Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
308ce263fc8SMatthias Ringwald     char buffer[40];
309ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CLIP, activate);
310ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
311ce263fc8SMatthias Ringwald }
312ce263fc8SMatthias Ringwald 
313ce263fc8SMatthias Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
314ce263fc8SMatthias Ringwald     char buffer[40];
315ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_TURN_OFF_EC_AND_NR, activate);
316ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
317ce263fc8SMatthias Ringwald }
318ce263fc8SMatthias Ringwald 
319ce263fc8SMatthias Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
320ce263fc8SMatthias Ringwald     char buffer[40];
321ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate);
322ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
323ce263fc8SMatthias Ringwald }
324ce263fc8SMatthias Ringwald 
325ce263fc8SMatthias Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
326ce263fc8SMatthias Ringwald     char buffer[40];
327ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
328ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
329ce263fc8SMatthias Ringwald }
330ce263fc8SMatthias Ringwald 
331ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
332ce263fc8SMatthias Ringwald     char buffer[40];
333ce263fc8SMatthias Ringwald     sprintf(buffer, "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
334ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
335ce263fc8SMatthias Ringwald }
336ce263fc8SMatthias Ringwald 
337a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
338ce263fc8SMatthias Ringwald     char buffer[40];
339a0ffb263SMatthias Ringwald     sprintf(buffer, "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id);
340ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
341ce263fc8SMatthias Ringwald }
342ce263fc8SMatthias Ringwald 
343ce263fc8SMatthias Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
344ce263fc8SMatthias Ringwald     char buffer[20];
345ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_REDIAL_LAST_NUMBER);
346ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
347ce263fc8SMatthias Ringwald }
348ce263fc8SMatthias Ringwald 
349ce263fc8SMatthias Ringwald static int hfp_hf_send_chup(uint16_t cid){
350ce263fc8SMatthias Ringwald     char buffer[20];
351ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_HANG_UP_CALL);
352ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
353ce263fc8SMatthias Ringwald }
354ce263fc8SMatthias Ringwald 
355ce263fc8SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, int number){
356ce263fc8SMatthias Ringwald     char buffer[20];
357ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
358ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
359ce263fc8SMatthias Ringwald }
360ce263fc8SMatthias Ringwald 
361ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
362ce263fc8SMatthias Ringwald     char buffer[20];
363ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
364ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
365ce263fc8SMatthias Ringwald }
366ce263fc8SMatthias Ringwald 
367ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
368ce263fc8SMatthias Ringwald     char buffer[20];
369ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=1\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG);
370ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
371ce263fc8SMatthias Ringwald }
372ce263fc8SMatthias Ringwald 
373667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
374667ec068SMatthias Ringwald     char buffer[20];
375667ec068SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_LIST_CURRENT_CALLS);
376667ec068SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
377667ec068SMatthias Ringwald }
378667ec068SMatthias Ringwald 
37913839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){
3803deb3ec6SMatthias Ringwald     if (!callback) return;
381a0ffb263SMatthias Ringwald     uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1];
3823deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3833deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3843deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
385a0ffb263SMatthias Ringwald     event[3] = indicator.index;
386a0ffb263SMatthias Ringwald     event[4] = indicator.status;
387a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
388a0ffb263SMatthias Ringwald     event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0;
38913839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3903deb3ec6SMatthias Ringwald }
3913deb3ec6SMatthias Ringwald 
39213839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){
3933deb3ec6SMatthias Ringwald     if (!callback) return;
3943deb3ec6SMatthias Ringwald     uint8_t event[24];
3953deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3963deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3973deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
398a0ffb263SMatthias Ringwald     event[3] = network_operator.mode;
399a0ffb263SMatthias Ringwald     event[4] = network_operator.format;
400a0ffb263SMatthias Ringwald     strcpy((char*)&event[5], network_operator.name);
40113839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
4023deb3ec6SMatthias Ringwald }
4033deb3ec6SMatthias Ringwald 
404a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
405a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
406a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
407aa4dd815SMatthias Ringwald     int done = 1;
4083deb3ec6SMatthias Ringwald 
409a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4103deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
411a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
412a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4133deb3ec6SMatthias Ringwald             break;
4143deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
415a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
416a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4173deb3ec6SMatthias Ringwald             break;
4183deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
419a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
420a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4213deb3ec6SMatthias Ringwald             break;
4223deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
423a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
424a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4253deb3ec6SMatthias Ringwald             break;
4263deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
427a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
428a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4293deb3ec6SMatthias Ringwald             break;
4303deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
431a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
432a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4333deb3ec6SMatthias Ringwald             break;
4343deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
435a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
436a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4373deb3ec6SMatthias Ringwald             break;
4383deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
439a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
440a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4413deb3ec6SMatthias Ringwald             break;
4423deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
443a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
444a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4453deb3ec6SMatthias Ringwald             break;
4463deb3ec6SMatthias Ringwald         default:
447aa4dd815SMatthias Ringwald             done = 0;
4483deb3ec6SMatthias Ringwald             break;
4493deb3ec6SMatthias Ringwald     }
4503deb3ec6SMatthias Ringwald     return done;
4513deb3ec6SMatthias Ringwald }
4523deb3ec6SMatthias Ringwald 
453ce263fc8SMatthias Ringwald 
454a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
455a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
456a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
457ce263fc8SMatthias Ringwald 
458ce263fc8SMatthias Ringwald     int done = 0;
459a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
460a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
461ce263fc8SMatthias Ringwald         done = 1;
462a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
463ce263fc8SMatthias Ringwald         return done;
464ce263fc8SMatthias Ringwald     };
465a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
466a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
467ce263fc8SMatthias Ringwald         done = 1;
468a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
469a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
470a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
471ce263fc8SMatthias Ringwald         return done;
472ce263fc8SMatthias Ringwald     }
473ce263fc8SMatthias Ringwald 
474a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
475ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
476a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
477a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
478a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
479ce263fc8SMatthias Ringwald             return 1;
480ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
481a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
482a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
483a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
484ce263fc8SMatthias Ringwald             return 1;
485ce263fc8SMatthias Ringwald         default:
486ce263fc8SMatthias Ringwald             break;
487ce263fc8SMatthias Ringwald     }
488ce263fc8SMatthias Ringwald 
489a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
490a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
491ce263fc8SMatthias Ringwald         done = 1;
492a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
493ce263fc8SMatthias Ringwald         return done;
494ce263fc8SMatthias Ringwald     }
495ce263fc8SMatthias Ringwald 
496ce263fc8SMatthias Ringwald     return done;
497ce263fc8SMatthias Ringwald }
498ce263fc8SMatthias Ringwald 
499a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
500ce263fc8SMatthias Ringwald     /* events ( == commands):
501ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
502ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
503ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
504ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
505ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
506ce263fc8SMatthias Ringwald     */
507ce263fc8SMatthias Ringwald 
508a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
509ce263fc8SMatthias Ringwald 
510a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
511ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
512a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
513ce263fc8SMatthias Ringwald 
514a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
515a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
516a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
517ce263fc8SMatthias Ringwald             return 1;
518ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
519a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = 0;
520a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = 0;
521a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = 0;
522ce263fc8SMatthias Ringwald 
523a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
524a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
525a0ffb263SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
526ce263fc8SMatthias Ringwald             break;
527ce263fc8SMatthias Ringwald 
5286a7f44bdSMilanka Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:{
5296a7f44bdSMilanka Ringwald             if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
530a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
531a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
532a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
533fcb08cdbSMilanka Ringwald                 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
534ce263fc8SMatthias Ringwald             } else {
535a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
536a0ffb263SMatthias Ringwald                 hfp_connection->suggested_codec = 0;
537a0ffb263SMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
538a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
539a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
540a0ffb263SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
541ce263fc8SMatthias Ringwald 
542ce263fc8SMatthias Ringwald             }
543ce263fc8SMatthias Ringwald             break;
5446a7f44bdSMilanka Ringwald         }
545ce263fc8SMatthias Ringwald         default:
546ce263fc8SMatthias Ringwald             break;
547ce263fc8SMatthias Ringwald     }
548ce263fc8SMatthias Ringwald     return 0;
549ce263fc8SMatthias Ringwald }
550ce263fc8SMatthias Ringwald 
551a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
552a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
553a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
554ce263fc8SMatthias Ringwald 
555ce263fc8SMatthias Ringwald 
556a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
557a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
558a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
559a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
560ce263fc8SMatthias Ringwald         return 1;
561ce263fc8SMatthias Ringwald     }
562ce263fc8SMatthias Ringwald 
563a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
564ce263fc8SMatthias Ringwald 
565ce263fc8SMatthias Ringwald     // run codecs exchange
566a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
567ce263fc8SMatthias Ringwald     if (done) return 1;
568ce263fc8SMatthias Ringwald 
569a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
570a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
571a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
572be2a1a78SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection);
573ce263fc8SMatthias Ringwald         return 1;
574ce263fc8SMatthias Ringwald     }
575ce263fc8SMatthias Ringwald 
576ce263fc8SMatthias Ringwald     return 0;
577ce263fc8SMatthias Ringwald }
578ce263fc8SMatthias Ringwald 
579a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
580a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
581a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
582a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
583ce263fc8SMatthias Ringwald         return 1;
584ce263fc8SMatthias Ringwald     }
585ce263fc8SMatthias Ringwald     return 0;
586ce263fc8SMatthias Ringwald }
587ce263fc8SMatthias Ringwald 
588a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){
589a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
590724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
591a0ffb263SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return;
592ce263fc8SMatthias Ringwald 
593a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
594ce263fc8SMatthias Ringwald     if (!done){
595a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
596ce263fc8SMatthias Ringwald     }
597ce263fc8SMatthias Ringwald     if (!done){
598a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
599ce263fc8SMatthias Ringwald     }
600ce263fc8SMatthias Ringwald     if (!done){
601a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
602ce263fc8SMatthias Ringwald     }
603ce263fc8SMatthias Ringwald 
604a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
605a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
606a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
607a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
608ce263fc8SMatthias Ringwald         return;
609ce263fc8SMatthias Ringwald     }
610ce263fc8SMatthias Ringwald 
611a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
612a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
613a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
614a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
615ce263fc8SMatthias Ringwald         return;
616ce263fc8SMatthias Ringwald     }
617ce263fc8SMatthias Ringwald 
618a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
619a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
620a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
621a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
622ce263fc8SMatthias Ringwald         return;
623ce263fc8SMatthias Ringwald     }
624ce263fc8SMatthias Ringwald 
625a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
626a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
627a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
628a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
629ce263fc8SMatthias Ringwald         return;
630ce263fc8SMatthias Ringwald     }
631ce263fc8SMatthias Ringwald 
632a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
633a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
634a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
635a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
636ce263fc8SMatthias Ringwald         return;
637ce263fc8SMatthias Ringwald     }
638ce263fc8SMatthias Ringwald 
639a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
640a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
641a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
642a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
643ce263fc8SMatthias Ringwald         return;
644ce263fc8SMatthias Ringwald     }
645ce263fc8SMatthias Ringwald 
646a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
647a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
648a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
649a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
650ce263fc8SMatthias Ringwald         return;
651ce263fc8SMatthias Ringwald     }
652ce263fc8SMatthias Ringwald 
653a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
654a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
655a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
656a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
657ce263fc8SMatthias Ringwald         return;
658ce263fc8SMatthias Ringwald     }
659ce263fc8SMatthias Ringwald 
660ce263fc8SMatthias Ringwald 
661a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
662a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
663a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
664a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
665ce263fc8SMatthias Ringwald         return;
666ce263fc8SMatthias Ringwald     }
667ce263fc8SMatthias Ringwald 
668a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
669a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
670a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
671a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
672ce263fc8SMatthias Ringwald         return;
673ce263fc8SMatthias Ringwald     }
674ce263fc8SMatthias Ringwald 
675a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
676a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
677a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
678a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
679ce263fc8SMatthias Ringwald         return;
680ce263fc8SMatthias Ringwald     }
681ce263fc8SMatthias Ringwald 
682a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
683a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
684a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
685a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
686ce263fc8SMatthias Ringwald         return;
687ce263fc8SMatthias Ringwald     }
688ce263fc8SMatthias Ringwald 
689a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
690a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
691a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
692a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
693ce263fc8SMatthias Ringwald         return;
694ce263fc8SMatthias Ringwald     }
695ce263fc8SMatthias Ringwald 
696a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
697a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
698a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
699a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
700ce263fc8SMatthias Ringwald         return;
701ce263fc8SMatthias Ringwald     }
702ce263fc8SMatthias Ringwald 
703a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
704a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
705a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
706a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
707ce263fc8SMatthias Ringwald         return;
708ce263fc8SMatthias Ringwald     }
709ce263fc8SMatthias Ringwald 
710a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
711a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
712a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
713a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
714ce263fc8SMatthias Ringwald         return;
715ce263fc8SMatthias Ringwald     }
716ce263fc8SMatthias Ringwald 
717a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
718a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
719a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
720a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
721ce263fc8SMatthias Ringwald         return;
722ce263fc8SMatthias Ringwald     }
723ce263fc8SMatthias Ringwald 
724a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
725a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
726a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
727a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
728ce263fc8SMatthias Ringwald         return;
729ce263fc8SMatthias Ringwald     }
730ce263fc8SMatthias Ringwald 
731a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
732a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
733a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
734a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
735ce263fc8SMatthias Ringwald         return;
736ce263fc8SMatthias Ringwald     }
737ce263fc8SMatthias Ringwald 
738a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
739a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
740a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
741a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
742667ec068SMatthias Ringwald         return;
743667ec068SMatthias Ringwald     }
744667ec068SMatthias Ringwald 
745a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
746a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
747a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
748a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
749a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
750ce263fc8SMatthias Ringwald         return;
751ce263fc8SMatthias Ringwald     }
752ce263fc8SMatthias Ringwald 
753a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
754a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
755a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
756a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
757ce263fc8SMatthias Ringwald         return;
758ce263fc8SMatthias Ringwald     }
759ce263fc8SMatthias Ringwald 
760a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
761a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
762a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
763a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
764667ec068SMatthias Ringwald         return;
765667ec068SMatthias Ringwald     }
766667ec068SMatthias Ringwald 
767a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
768a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
769667ec068SMatthias Ringwald         char buffer[20];
770a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
771667ec068SMatthias Ringwald             case '?':
772667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
773a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
774667ec068SMatthias Ringwald                 return;
775667ec068SMatthias Ringwald             case '0':
776667ec068SMatthias Ringwald             case '1':
777667ec068SMatthias Ringwald             case '2':
778a0ffb263SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
779a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
780667ec068SMatthias Ringwald                 return;
781667ec068SMatthias Ringwald             default:
782667ec068SMatthias Ringwald                 break;
783667ec068SMatthias Ringwald         }
784667ec068SMatthias Ringwald         return;
785667ec068SMatthias Ringwald     }
786667ec068SMatthias Ringwald 
787a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
788a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
789667ec068SMatthias Ringwald         char buffer[20];
790667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
791a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
792667ec068SMatthias Ringwald         return;
793667ec068SMatthias Ringwald     }
794667ec068SMatthias Ringwald 
795667ec068SMatthias Ringwald     // update HF indicators
796a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
797667ec068SMatthias Ringwald         int i;
798667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
799a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
800a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
801a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
802a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
803667ec068SMatthias Ringwald                     char buffer[30];
804667ec068SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], hfp_indicators_value[i]);
805a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
806667ec068SMatthias Ringwald                 } else {
807667ec068SMatthias Ringwald                     printf("Not sending HF indicator %u as it is disabled\n", hfp_indicators[i]);
808667ec068SMatthias Ringwald                 }
809667ec068SMatthias Ringwald                 return;
810667ec068SMatthias Ringwald             }
811667ec068SMatthias Ringwald         }
812667ec068SMatthias Ringwald     }
813667ec068SMatthias Ringwald 
814ce263fc8SMatthias Ringwald     if (done) return;
815ce263fc8SMatthias Ringwald     // deal with disconnect
816a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
817ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
818a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
819a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
820ce263fc8SMatthias Ringwald             break;
821ce263fc8SMatthias Ringwald 
822ce263fc8SMatthias Ringwald         default:
823ce263fc8SMatthias Ringwald             break;
824ce263fc8SMatthias Ringwald     }
825ce263fc8SMatthias Ringwald }
826ce263fc8SMatthias Ringwald 
827a0ffb263SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){
828ce263fc8SMatthias Ringwald     // determine highest possible link setting
829a0ffb263SMatthias Ringwald     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
830a0ffb263SMatthias Ringwald     if (hci_remote_esco_supported(hfp_connection->acl_handle)){
831a0ffb263SMatthias Ringwald         hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
832ce263fc8SMatthias Ringwald         if ((hfp_supported_features             & (1<<HFP_HFSF_ESCO_S4))
833a0ffb263SMatthias Ringwald         &&  (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4))){
834a0ffb263SMatthias Ringwald             hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
835ce263fc8SMatthias Ringwald         }
836ce263fc8SMatthias Ringwald     }
837ce263fc8SMatthias Ringwald }
838ce263fc8SMatthias Ringwald 
839a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
840a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
8416a7f44bdSMilanka Ringwald 
842*4f84bf36SMatthias Ringwald     hfp_emit_connection_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
843a0ffb263SMatthias Ringwald     hfp_init_link_settings(hfp_connection);
844667ec068SMatthias Ringwald     // restore volume settings
845a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
846a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
847667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
848a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
849a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
850667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
851667ec068SMatthias Ringwald     // enable all indicators
852667ec068SMatthias Ringwald     int i;
853667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
854a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
855a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
856667ec068SMatthias Ringwald     }
857ce263fc8SMatthias Ringwald }
858ce263fc8SMatthias Ringwald 
859a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
860a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
861a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
8623deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
863a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
864a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
8653deb3ec6SMatthias Ringwald                 break;
8663deb3ec6SMatthias Ringwald             }
867a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8683deb3ec6SMatthias Ringwald             break;
8693deb3ec6SMatthias Ringwald 
8703deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
871a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8723deb3ec6SMatthias Ringwald             break;
8733deb3ec6SMatthias Ringwald 
8743deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
875a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
8763deb3ec6SMatthias Ringwald             break;
8773deb3ec6SMatthias Ringwald 
8783deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
879a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
8803deb3ec6SMatthias Ringwald             break;
8813deb3ec6SMatthias Ringwald 
8823deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
883a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
884a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
8853deb3ec6SMatthias Ringwald                 break;
8863deb3ec6SMatthias Ringwald             }
887a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
888a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8893deb3ec6SMatthias Ringwald                 break;
8903deb3ec6SMatthias Ringwald             }
891a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8923deb3ec6SMatthias Ringwald             break;
8933deb3ec6SMatthias Ringwald 
8943deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
895a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
896a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8973deb3ec6SMatthias Ringwald                 break;
8983deb3ec6SMatthias Ringwald             }
899a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9003deb3ec6SMatthias Ringwald             break;
9013deb3ec6SMatthias Ringwald 
9023deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
903a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
9043deb3ec6SMatthias Ringwald             break;
9053deb3ec6SMatthias Ringwald 
9063deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
907a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9083deb3ec6SMatthias Ringwald             break;
9093deb3ec6SMatthias Ringwald 
9103deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
911a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9123deb3ec6SMatthias Ringwald             break;
913ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
914a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
915a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
9163deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
917ce263fc8SMatthias Ringwald                 break;
918ce263fc8SMatthias Ringwald             }
9193deb3ec6SMatthias Ringwald 
920a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
921a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
9223deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
923ce263fc8SMatthias Ringwald                 break;
9243deb3ec6SMatthias Ringwald             }
9253deb3ec6SMatthias Ringwald 
926a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
927ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
928ce263fc8SMatthias Ringwald                     printf("Format set, querying name\n");
929a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
930ce263fc8SMatthias Ringwald                     break;
931ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
932a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
933a0ffb263SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_callback, hfp_connection->network_operator);
934ce263fc8SMatthias Ringwald                     break;
935ce263fc8SMatthias Ringwald                 default:
936ce263fc8SMatthias Ringwald                     break;
9373deb3ec6SMatthias Ringwald             }
938ce263fc8SMatthias Ringwald 
939a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
940a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
941ce263fc8SMatthias Ringwald                 break;
9423deb3ec6SMatthias Ringwald             }
9433deb3ec6SMatthias Ringwald 
944a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
945aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
946a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9473deb3ec6SMatthias Ringwald                     break;
948ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
949a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
9506a7f44bdSMilanka Ringwald                     hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
95103645b44SMatthias Ringwald                     log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
9526a7f44bdSMilanka Ringwald                     hfp_emit_codec_event(hfp_callback, 0, hfp_connection->negotiated_codec);
953ce263fc8SMatthias Ringwald                     break;
9543deb3ec6SMatthias Ringwald                 default:
9553deb3ec6SMatthias Ringwald                     break;
9563deb3ec6SMatthias Ringwald             }
9573deb3ec6SMatthias Ringwald             break;
9583deb3ec6SMatthias Ringwald         default:
9593deb3ec6SMatthias Ringwald             break;
9603deb3ec6SMatthias Ringwald     }
9613deb3ec6SMatthias Ringwald 
9623deb3ec6SMatthias Ringwald     // done
963a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
9643deb3ec6SMatthias Ringwald }
9653deb3ec6SMatthias Ringwald 
9663deb3ec6SMatthias Ringwald 
9673deb3ec6SMatthias Ringwald static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
968a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
969a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
9703deb3ec6SMatthias Ringwald 
9711e35c04dSMatthias Ringwald     char last_char = packet[size-1];
9721e35c04dSMatthias Ringwald     packet[size-1] = 0;
9731e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
9741e35c04dSMatthias Ringwald     packet[size-1] = last_char;
9751e35c04dSMatthias Ringwald 
976667ec068SMatthias Ringwald     int pos, i, value;
9773deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
978a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
979ce263fc8SMatthias Ringwald     }
9803deb3ec6SMatthias Ringwald 
981a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
982667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
983a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
984a0ffb263SMatthias Ringwald             // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type);
985a0ffb263SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
986667ec068SMatthias Ringwald             break;
987667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
988a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
989a0ffb263SMatthias Ringwald             // printf("Response and Hold status: %s\n", hfp_connection->line_buffer);
990a0ffb263SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, atoi((char *)&hfp_connection->line_buffer[0]));
991667ec068SMatthias Ringwald             break;
992667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
993a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
994a0ffb263SMatthias Ringwald             // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
995a0ffb263SMatthias Ringwald             //      hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
996a0ffb263SMatthias Ringwald             //      hfp_connection->bnip_number, hfp_connection->bnip_type);
997a0ffb263SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_callback, hfp_connection->clcc_idx,
998a0ffb263SMatthias Ringwald                 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
999a0ffb263SMatthias Ringwald                 hfp_connection->bnip_type, hfp_connection->bnip_number);
1000667ec068SMatthias Ringwald             break;
1001ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1002a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1003a0ffb263SMatthias Ringwald             value = atoi((char*)hfp_connection->line_buffer);
1004667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1005667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1006ce263fc8SMatthias Ringwald             break;
1007ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1008a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1009a0ffb263SMatthias Ringwald             value = atoi((char*)hfp_connection->line_buffer);
1010667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1011667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1012ce263fc8SMatthias Ringwald             break;
1013ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1014a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1015a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1016a0ffb263SMatthias Ringwald             break;
1017a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1018a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1019a0ffb263SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1020a0ffb263SMatthias Ringwald             break;
1021a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1022a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1023a0ffb263SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALLING_LINE_INDETIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1024ce263fc8SMatthias Ringwald             break;
1025ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1026a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1027a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1028a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1029a0ffb263SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1030ce263fc8SMatthias Ringwald             break;
1031ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1032a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1033a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1034a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1035ce263fc8SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1);
1036ce263fc8SMatthias Ringwald             break;
1037ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1038a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1039ce263fc8SMatthias Ringwald             break;
1040ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
1041a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_RING);
1042ce263fc8SMatthias Ringwald             break;
1043ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1044a0ffb263SMatthias Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1045a0ffb263SMatthias Ringwald                 if (hfp_connection->ag_indicators[i].status_changed) {
1046a0ffb263SMatthias Ringwald                     if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1047a0ffb263SMatthias Ringwald                         hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1048a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1049a0ffb263SMatthias Ringwald                         hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1050a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1051a0ffb263SMatthias Ringwald                         hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1052ce263fc8SMatthias Ringwald                     }
1053a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[i].status_changed = 0;
1054a0ffb263SMatthias Ringwald                     hfp_emit_ag_indicator_event(hfp_callback, hfp_connection->ag_indicators[i]);
10553deb3ec6SMatthias Ringwald                     break;
10563deb3ec6SMatthias Ringwald                 }
10573deb3ec6SMatthias Ringwald             }
1058ce263fc8SMatthias Ringwald             break;
1059ce263fc8SMatthias Ringwald         default:
1060ce263fc8SMatthias Ringwald             break;
10613deb3ec6SMatthias Ringwald     }
1062a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
10633deb3ec6SMatthias Ringwald }
10643deb3ec6SMatthias Ringwald 
10650cb5b971SMatthias Ringwald static void hfp_run(void){
1066665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1067665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1068665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1069a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1070a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
10713deb3ec6SMatthias Ringwald     }
10723deb3ec6SMatthias Ringwald }
10733deb3ec6SMatthias Ringwald 
1074ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10753deb3ec6SMatthias Ringwald     switch (packet_type){
10763deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
10773deb3ec6SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size);
10783deb3ec6SMatthias Ringwald             break;
10793deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1080e30a6a47SMatthias Ringwald             hfp_handle_hci_event(packet_type, channel, packet, size);
10813deb3ec6SMatthias Ringwald         default:
10823deb3ec6SMatthias Ringwald             break;
10833deb3ec6SMatthias Ringwald     }
10843deb3ec6SMatthias Ringwald     hfp_run();
10853deb3ec6SMatthias Ringwald }
10863deb3ec6SMatthias Ringwald 
1087a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1088d63c37a1SMatthias Ringwald     // register for HCI events
1089d63c37a1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
1090d63c37a1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
1091d63c37a1SMatthias Ringwald 
1092d63c37a1SMatthias Ringwald     rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff);
1093a0ffb263SMatthias Ringwald 
1094d68dcce1SMatthias Ringwald     hfp_set_packet_handler_for_rfcomm_connections(&packet_handler);
1095d68dcce1SMatthias Ringwald 
1096a0ffb263SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1097a0ffb263SMatthias Ringwald     hfp_codecs_nr = 0;
1098a0ffb263SMatthias Ringwald     hfp_indicators_nr = 0;
1099a0ffb263SMatthias Ringwald     hfp_hf_speaker_gain = 9;
1100a0ffb263SMatthias Ringwald     hfp_hf_microphone_gain = 9;
1101a0ffb263SMatthias Ringwald }
1102a0ffb263SMatthias Ringwald 
1103a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
11043deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1105a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
11063deb3ec6SMatthias Ringwald         return;
11073deb3ec6SMatthias Ringwald     }
11083deb3ec6SMatthias Ringwald 
11093deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
11103deb3ec6SMatthias Ringwald     int i;
11113deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
11123deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
11133deb3ec6SMatthias Ringwald     }
11143deb3ec6SMatthias Ringwald 
11153deb3ec6SMatthias Ringwald     char buffer[30];
11163deb3ec6SMatthias Ringwald     int offset = join(buffer, sizeof(buffer), hfp_codecs, hfp_codecs_nr);
11173deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1118665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1119665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1120665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1121a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1122a0ffb263SMatthias Ringwald         if (! hfp_connection) continue;
1123a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_AVAILABLE_CODECS;
1124a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
11253deb3ec6SMatthias Ringwald     }
11263deb3ec6SMatthias Ringwald }
11273deb3ec6SMatthias Ringwald 
1128a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
11293deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1130a0ffb263SMatthias Ringwald }
11313deb3ec6SMatthias Ringwald 
1132a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
11333deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
11343deb3ec6SMatthias Ringwald     int i;
1135a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
11363deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
11373deb3ec6SMatthias Ringwald     }
11383deb3ec6SMatthias Ringwald }
11393deb3ec6SMatthias Ringwald 
11403deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
11413deb3ec6SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway);
11423deb3ec6SMatthias Ringwald }
11433deb3ec6SMatthias Ringwald 
11443deb3ec6SMatthias Ringwald void hfp_hf_release_service_level_connection(bd_addr_t bd_addr){
1145a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1146a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
1147a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11483deb3ec6SMatthias Ringwald }
11493deb3ec6SMatthias Ringwald 
1150ce263fc8SMatthias Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(bd_addr_t bd_addr, uint8_t enable){
11513deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1152a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1153a0ffb263SMatthias Ringwald     if (!hfp_connection){
1154a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
11553deb3ec6SMatthias Ringwald         return;
11563deb3ec6SMatthias Ringwald     }
1157a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
1158a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11593deb3ec6SMatthias Ringwald }
11603deb3ec6SMatthias Ringwald 
1161ce263fc8SMatthias Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1162ce263fc8SMatthias Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 1);
1163ce263fc8SMatthias Ringwald }
1164ce263fc8SMatthias Ringwald 
1165ce263fc8SMatthias Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1166ce263fc8SMatthias Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 0);
1167ce263fc8SMatthias Ringwald }
1168ce263fc8SMatthias Ringwald 
11693deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1170ce263fc8SMatthias Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(bd_addr_t bd_addr, uint32_t indicators_status_bitmap){
11713deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1172a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1173a0ffb263SMatthias Ringwald     if (!hfp_connection){
1174a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
11753deb3ec6SMatthias Ringwald         return;
11763deb3ec6SMatthias Ringwald     }
1177a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1178a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1179a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11803deb3ec6SMatthias Ringwald }
11813deb3ec6SMatthias Ringwald 
11823deb3ec6SMatthias Ringwald void hfp_hf_query_operator_selection(bd_addr_t bd_addr){
11833deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1184a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1185a0ffb263SMatthias Ringwald     if (!hfp_connection){
1186a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
11873deb3ec6SMatthias Ringwald         return;
11883deb3ec6SMatthias Ringwald     }
1189a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1190ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1191a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1192ce263fc8SMatthias Ringwald             break;
1193ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1194a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1195ce263fc8SMatthias Ringwald             break;
1196ce263fc8SMatthias Ringwald         default:
1197ce263fc8SMatthias Ringwald             break;
1198ce263fc8SMatthias Ringwald     }
1199a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12003deb3ec6SMatthias Ringwald }
12013deb3ec6SMatthias Ringwald 
1202ce263fc8SMatthias Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, uint8_t enable){
12033deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1204a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1205a0ffb263SMatthias Ringwald     if (!hfp_connection){
1206a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
12073deb3ec6SMatthias Ringwald         return;
12083deb3ec6SMatthias Ringwald     }
1209a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1210a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12113deb3ec6SMatthias Ringwald }
12123deb3ec6SMatthias Ringwald 
1213ce263fc8SMatthias Ringwald 
1214ce263fc8SMatthias Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1215ce263fc8SMatthias Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 1);
1216ce263fc8SMatthias Ringwald }
1217ce263fc8SMatthias Ringwald 
1218ce263fc8SMatthias Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1219ce263fc8SMatthias Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 0);
1220ce263fc8SMatthias Ringwald }
1221ce263fc8SMatthias Ringwald 
1222ce263fc8SMatthias Ringwald 
12233deb3ec6SMatthias Ringwald void hfp_hf_establish_audio_connection(bd_addr_t bd_addr){
12243deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1225a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1226a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1227ce263fc8SMatthias Ringwald 
1228a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1229a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
12303deb3ec6SMatthias Ringwald 
1231a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1232ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1233a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1234a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1235ce263fc8SMatthias Ringwald     } else {
1236a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1237aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1238aa4dd815SMatthias Ringwald                 break;
1239aa4dd815SMatthias Ringwald             default:
1240a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1241aa4dd815SMatthias Ringwald                 break;
12423deb3ec6SMatthias Ringwald         }
1243ce263fc8SMatthias Ringwald     }
1244ce263fc8SMatthias Ringwald 
1245a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12463deb3ec6SMatthias Ringwald }
12473deb3ec6SMatthias Ringwald 
12483deb3ec6SMatthias Ringwald void hfp_hf_release_audio_connection(bd_addr_t bd_addr){
1249a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1250a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
1251a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12523deb3ec6SMatthias Ringwald }
12533deb3ec6SMatthias Ringwald 
1254ce263fc8SMatthias Ringwald void hfp_hf_answer_incoming_call(bd_addr_t bd_addr){
1255ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1256a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1257ce263fc8SMatthias Ringwald 
1258ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1259a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
1260a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1261ce263fc8SMatthias Ringwald     } else {
1262ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1263ce263fc8SMatthias Ringwald     }
1264ce263fc8SMatthias Ringwald }
1265ce263fc8SMatthias Ringwald 
1266ce263fc8SMatthias Ringwald void hfp_hf_terminate_call(bd_addr_t bd_addr){
1267ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1268a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1269ce263fc8SMatthias Ringwald 
1270a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
1271a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1272ce263fc8SMatthias Ringwald }
1273ce263fc8SMatthias Ringwald 
1274a0ffb263SMatthias Ringwald void hfp_hf_reject_incoming_call(bd_addr_t bd_addr){
1275ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1276a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1277ce263fc8SMatthias Ringwald 
1278ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1279a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
1280a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1281ce263fc8SMatthias Ringwald     }
1282ce263fc8SMatthias Ringwald }
1283ce263fc8SMatthias Ringwald 
1284ce263fc8SMatthias Ringwald void hfp_hf_user_busy(bd_addr_t addr){
1285ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1286a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1287ce263fc8SMatthias Ringwald 
1288ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1289a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
1290a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1291ce263fc8SMatthias Ringwald     }
1292ce263fc8SMatthias Ringwald }
1293ce263fc8SMatthias Ringwald 
1294ce263fc8SMatthias Ringwald void hfp_hf_end_active_and_accept_other(bd_addr_t addr){
1295ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1296a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1297ce263fc8SMatthias Ringwald 
1298ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1299ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1300a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
1301a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1302ce263fc8SMatthias Ringwald     }
1303ce263fc8SMatthias Ringwald }
1304ce263fc8SMatthias Ringwald 
1305ce263fc8SMatthias Ringwald void hfp_hf_swap_calls(bd_addr_t addr){
1306ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1307a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1308ce263fc8SMatthias Ringwald 
1309ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1310ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1311a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
1312a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1313ce263fc8SMatthias Ringwald     }
1314ce263fc8SMatthias Ringwald }
1315ce263fc8SMatthias Ringwald 
1316ce263fc8SMatthias Ringwald void hfp_hf_join_held_call(bd_addr_t addr){
1317ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1318a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1319ce263fc8SMatthias Ringwald 
1320ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1321ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1322a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
1323a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1324ce263fc8SMatthias Ringwald     }
1325ce263fc8SMatthias Ringwald }
1326ce263fc8SMatthias Ringwald 
1327ce263fc8SMatthias Ringwald void hfp_hf_connect_calls(bd_addr_t addr){
1328ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1329a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1330ce263fc8SMatthias Ringwald 
1331ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1332ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1333a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
1334a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1335ce263fc8SMatthias Ringwald     }
1336ce263fc8SMatthias Ringwald }
1337ce263fc8SMatthias Ringwald 
1338667ec068SMatthias Ringwald void hfp_hf_release_call_with_index(bd_addr_t addr, int index){
1339667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1340a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1341667ec068SMatthias Ringwald 
1342667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1343667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1344a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1345a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
1346a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1347667ec068SMatthias Ringwald     }
1348667ec068SMatthias Ringwald }
1349667ec068SMatthias Ringwald 
1350667ec068SMatthias Ringwald void hfp_hf_private_consultation_with_call(bd_addr_t addr, int index){
1351667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1352a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1353667ec068SMatthias Ringwald 
1354667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1355667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1356a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1357a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
1358a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1359667ec068SMatthias Ringwald     }
1360667ec068SMatthias Ringwald }
1361ce263fc8SMatthias Ringwald 
1362ce263fc8SMatthias Ringwald void hfp_hf_dial_number(bd_addr_t bd_addr, char * number){
1363ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1364a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1365ce263fc8SMatthias Ringwald 
1366a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1367ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1368a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1369ce263fc8SMatthias Ringwald }
1370ce263fc8SMatthias Ringwald 
1371a0ffb263SMatthias Ringwald void hfp_hf_dial_memory(bd_addr_t bd_addr, int memory_id){
1372ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1373a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1374ce263fc8SMatthias Ringwald 
1375a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1376a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1377a0ffb263SMatthias Ringwald 
1378a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1379ce263fc8SMatthias Ringwald }
1380ce263fc8SMatthias Ringwald 
1381ce263fc8SMatthias Ringwald void hfp_hf_redial_last_number(bd_addr_t bd_addr){
1382ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1383a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1384ce263fc8SMatthias Ringwald 
1385a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
1386a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1387ce263fc8SMatthias Ringwald }
1388ce263fc8SMatthias Ringwald 
1389ce263fc8SMatthias Ringwald void hfp_hf_activate_call_waiting_notification(bd_addr_t bd_addr){
1390ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1391a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1392ce263fc8SMatthias Ringwald 
1393a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
1394a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1395ce263fc8SMatthias Ringwald }
1396ce263fc8SMatthias Ringwald 
1397ce263fc8SMatthias Ringwald 
1398ce263fc8SMatthias Ringwald void hfp_hf_deactivate_call_waiting_notification(bd_addr_t bd_addr){
1399ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1400a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1401ce263fc8SMatthias Ringwald 
1402a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1403a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1404ce263fc8SMatthias Ringwald }
1405ce263fc8SMatthias Ringwald 
1406ce263fc8SMatthias Ringwald 
1407ce263fc8SMatthias Ringwald void hfp_hf_activate_calling_line_notification(bd_addr_t bd_addr){
1408ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1409a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1410ce263fc8SMatthias Ringwald 
1411a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
1412a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1413ce263fc8SMatthias Ringwald }
1414ce263fc8SMatthias Ringwald 
1415ce263fc8SMatthias Ringwald void hfp_hf_deactivate_calling_line_notification(bd_addr_t bd_addr){
1416ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1417a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1418ce263fc8SMatthias Ringwald 
1419a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
1420a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1421ce263fc8SMatthias Ringwald }
1422ce263fc8SMatthias Ringwald 
1423ce263fc8SMatthias Ringwald 
1424ce263fc8SMatthias Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1425ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1426a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1427ce263fc8SMatthias Ringwald 
1428a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1429a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1430ce263fc8SMatthias Ringwald }
1431ce263fc8SMatthias Ringwald 
1432ce263fc8SMatthias Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1433ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1434a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1435ce263fc8SMatthias Ringwald 
1436a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1437a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1438ce263fc8SMatthias Ringwald }
1439ce263fc8SMatthias Ringwald 
1440ce263fc8SMatthias Ringwald void hfp_hf_activate_voice_recognition_notification(bd_addr_t bd_addr){
1441ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1442a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1443ce263fc8SMatthias Ringwald 
1444a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
1445a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1446ce263fc8SMatthias Ringwald }
1447ce263fc8SMatthias Ringwald 
1448ce263fc8SMatthias Ringwald void hfp_hf_deactivate_voice_recognition_notification(bd_addr_t bd_addr){
1449ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1450a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1451ce263fc8SMatthias Ringwald 
1452a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1453a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1454ce263fc8SMatthias Ringwald }
1455ce263fc8SMatthias Ringwald 
1456ce263fc8SMatthias Ringwald void hfp_hf_set_microphone_gain(bd_addr_t bd_addr, int gain){
1457ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1458a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1459a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1460a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1461a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1462a0ffb263SMatthias Ringwald         return;
1463a0ffb263SMatthias Ringwald     }
1464a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1465a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1466a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1467ce263fc8SMatthias Ringwald }
1468ce263fc8SMatthias Ringwald 
1469ce263fc8SMatthias Ringwald void hfp_hf_set_speaker_gain(bd_addr_t bd_addr, int gain){
1470ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1471a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1472a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1473a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1474a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1475a0ffb263SMatthias Ringwald         return;
1476a0ffb263SMatthias Ringwald     }
1477a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1478a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1479a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1480ce263fc8SMatthias Ringwald }
1481ce263fc8SMatthias Ringwald 
1482ce263fc8SMatthias Ringwald void hfp_hf_send_dtmf_code(bd_addr_t addr, char code){
1483ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1484a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1485a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
1486a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1487ce263fc8SMatthias Ringwald }
1488ce263fc8SMatthias Ringwald 
1489ce263fc8SMatthias Ringwald void hfp_hf_request_phone_number_for_voice_tag(bd_addr_t addr){
1490ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1491a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1492a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
1493a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1494ce263fc8SMatthias Ringwald }
14953deb3ec6SMatthias Ringwald 
1496667ec068SMatthias Ringwald void hfp_hf_query_current_call_status(bd_addr_t addr){
1497667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1498a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1499a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
1500a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1501667ec068SMatthias Ringwald }
1502667ec068SMatthias Ringwald 
1503667ec068SMatthias Ringwald 
1504667ec068SMatthias Ringwald void hfp_hf_rrh_query_status(bd_addr_t addr){
1505667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1506a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1507a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1508a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
1509a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1510667ec068SMatthias Ringwald }
1511667ec068SMatthias Ringwald 
1512667ec068SMatthias Ringwald void hfp_hf_rrh_hold_call(bd_addr_t addr){
1513667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1514a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1515a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1516a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
1517a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1518667ec068SMatthias Ringwald }
1519667ec068SMatthias Ringwald 
1520667ec068SMatthias Ringwald void hfp_hf_rrh_accept_held_call(bd_addr_t addr){
1521667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1522a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1523a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1524a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
1525a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1526667ec068SMatthias Ringwald }
1527667ec068SMatthias Ringwald 
1528a0ffb263SMatthias Ringwald void hfp_hf_rrh_reject_held_call(bd_addr_t addr){
1529667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1530a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1531a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1532a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
1533a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1534667ec068SMatthias Ringwald }
1535667ec068SMatthias Ringwald 
1536a0ffb263SMatthias Ringwald void hfp_hf_query_subscriber_number(bd_addr_t addr){
1537667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1538a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1539a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
1540a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1541667ec068SMatthias Ringwald }
1542667ec068SMatthias Ringwald 
1543667ec068SMatthias Ringwald void hfp_hf_set_hf_indicator(bd_addr_t addr, int assigned_number, int value){
1544667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1545a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1546667ec068SMatthias Ringwald     // find index for assigned number
1547667ec068SMatthias Ringwald     int i;
1548667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1549667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1550667ec068SMatthias Ringwald             // set value
1551667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1552667ec068SMatthias Ringwald             // mark for update
1553a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1554a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1555667ec068SMatthias Ringwald                 // send update
1556a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1557a0ffb263SMatthias Ringwald             }
1558667ec068SMatthias Ringwald             return;
1559667ec068SMatthias Ringwald         }
1560667ec068SMatthias Ringwald     }
1561667ec068SMatthias Ringwald }
1562667ec068SMatthias Ringwald 
1563