xref: /btstack/src/classic/hfp_hf.c (revision a0ffb263e02c613f9155d5035f68cb0ad7b80d74)
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 //
403deb3ec6SMatthias Ringwald // Minimal setup for HFP Hands-Free (HF) unit (!! UNDER DEVELOPMENT !!)
413deb3ec6SMatthias Ringwald //
423deb3ec6SMatthias Ringwald // *****************************************************************************
433deb3ec6SMatthias Ringwald 
447907f069SMatthias Ringwald #include "btstack_config.h"
453deb3ec6SMatthias Ringwald 
463deb3ec6SMatthias Ringwald #include <stdint.h>
473deb3ec6SMatthias Ringwald #include <stdio.h>
483deb3ec6SMatthias Ringwald #include <stdlib.h>
493deb3ec6SMatthias Ringwald #include <string.h>
503deb3ec6SMatthias Ringwald 
5156042629SMatthias Ringwald #include "hci_cmd.h"
5282636622SMatthias Ringwald #include "btstack_run_loop.h"
533deb3ec6SMatthias Ringwald 
543deb3ec6SMatthias Ringwald #include "hci.h"
553deb3ec6SMatthias Ringwald #include "btstack_memory.h"
563deb3ec6SMatthias Ringwald #include "hci_dump.h"
573deb3ec6SMatthias Ringwald #include "l2cap.h"
583edc84c5SMatthias Ringwald #include "classic/sdp_query_rfcomm.h"
59746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
60023f2764SMatthias Ringwald #include "classic/sdp_util.h"
6116ece135SMatthias Ringwald #include "btstack_debug.h"
623edc84c5SMatthias Ringwald #include "classic/hfp.h"
633edc84c5SMatthias Ringwald #include "classic/hfp_hf.h"
643deb3ec6SMatthias Ringwald 
653deb3ec6SMatthias Ringwald 
663deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
673deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
683deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
693deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
703deb3ec6SMatthias Ringwald 
713deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0;
723deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS];
73667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS];
74667ec068SMatthias Ringwald 
75667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9;
76667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9;
773deb3ec6SMatthias Ringwald 
783deb3ec6SMatthias Ringwald static hfp_callback_t hfp_callback;
793deb3ec6SMatthias Ringwald 
80ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
81ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
82ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
83ce263fc8SMatthias Ringwald 
84ce263fc8SMatthias Ringwald static char phone_number[25];
85ce263fc8SMatthias Ringwald 
86c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
87c5b64319SMatthias Ringwald 
883deb3ec6SMatthias Ringwald void hfp_hf_register_packet_handler(hfp_callback_t callback){
893deb3ec6SMatthias Ringwald     hfp_callback = callback;
903deb3ec6SMatthias Ringwald     if (callback == NULL){
913deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
923deb3ec6SMatthias Ringwald         return;
933deb3ec6SMatthias Ringwald     }
943deb3ec6SMatthias Ringwald     hfp_callback = callback;
95f4000eebSMatthias Ringwald     hfp_set_callback(callback);
963deb3ec6SMatthias Ringwald }
973deb3ec6SMatthias Ringwald 
98*a0ffb263SMatthias Ringwald static void hfp_hf_emit_subscriber_information(hfp_callback_t callback, uint8_t event_subtype, uint8_t status, uint8_t bnip_type, const char * bnip_number){
99*a0ffb263SMatthias Ringwald     if (!callback) return;
100*a0ffb263SMatthias Ringwald     uint8_t event[31];
101*a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
102*a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
103*a0ffb263SMatthias Ringwald     event[2] = event_subtype;
104*a0ffb263SMatthias Ringwald     event[3] = status;
105*a0ffb263SMatthias Ringwald     event[4] = bnip_type;
106*a0ffb263SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 6) ? strlen(bnip_number) : sizeof(event) - 6;
107*a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], bnip_number, size);
108*a0ffb263SMatthias Ringwald     event[5 + size] = 0;
109*a0ffb263SMatthias Ringwald     (*callback)(event, sizeof(event));
110*a0ffb263SMatthias Ringwald }
111*a0ffb263SMatthias Ringwald 
112*a0ffb263SMatthias Ringwald static void hfp_hf_emit_type_and_number(hfp_callback_t callback, uint8_t event_subtype, uint8_t bnip_type, const char * bnip_number){
113*a0ffb263SMatthias Ringwald     if (!callback) return;
114*a0ffb263SMatthias Ringwald     uint8_t event[30];
115*a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
116*a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
117*a0ffb263SMatthias Ringwald     event[2] = event_subtype;
118*a0ffb263SMatthias Ringwald     event[3] = bnip_type;
119*a0ffb263SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 5) ? strlen(bnip_number) : sizeof(event) - 5;
120*a0ffb263SMatthias Ringwald     strncpy((char*)&event[4], bnip_number, size);
121*a0ffb263SMatthias Ringwald     event[4 + size] = 0;
122*a0ffb263SMatthias Ringwald     (*callback)(event, sizeof(event));
123*a0ffb263SMatthias Ringwald }
124*a0ffb263SMatthias Ringwald 
125*a0ffb263SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(hfp_callback_t callback, uint8_t clcc_idx, uint8_t clcc_dir,
126*a0ffb263SMatthias Ringwald                 uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){
127*a0ffb263SMatthias Ringwald     if (!callback) return;
128*a0ffb263SMatthias Ringwald     uint8_t event[35];
129*a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
130*a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
131*a0ffb263SMatthias Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
132*a0ffb263SMatthias Ringwald     event[3] = clcc_idx;
133*a0ffb263SMatthias Ringwald     event[4] = clcc_dir;
134*a0ffb263SMatthias Ringwald     event[6] = clcc_status;
135*a0ffb263SMatthias Ringwald     event[7] = clcc_mpty;
136*a0ffb263SMatthias Ringwald     event[8] = bnip_type;
137*a0ffb263SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 10) ? strlen(bnip_number) : sizeof(event) - 10;
138*a0ffb263SMatthias Ringwald     strncpy((char*)&event[9], bnip_number, size);
139*a0ffb263SMatthias Ringwald     event[9 + size] = 0;
140*a0ffb263SMatthias Ringwald     (*callback)(event, sizeof(event));
141*a0ffb263SMatthias Ringwald }
142*a0ffb263SMatthias Ringwald 
1433deb3ec6SMatthias Ringwald static int hfp_hf_supports_codec(uint8_t codec){
1443deb3ec6SMatthias Ringwald     int i;
1453deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_codecs_nr; i++){
1463deb3ec6SMatthias Ringwald         if (hfp_codecs[i] == codec) return 1;
1473deb3ec6SMatthias Ringwald     }
148ce263fc8SMatthias Ringwald     return HFP_CODEC_CVSD;
1493deb3ec6SMatthias Ringwald }
150*a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
1513deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
152*a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1533deb3ec6SMatthias Ringwald     return hf && ag;
1543deb3ec6SMatthias Ringwald }
1553deb3ec6SMatthias Ringwald 
156*a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
1573deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
158*a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1593deb3ec6SMatthias Ringwald     return hf && ag;
1603deb3ec6SMatthias Ringwald }
1613deb3ec6SMatthias Ringwald 
1623deb3ec6SMatthias Ringwald 
163*a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
1643deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
165*a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
1663deb3ec6SMatthias Ringwald     return hf && ag;
1673deb3ec6SMatthias Ringwald }
1683deb3ec6SMatthias Ringwald 
169ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
1703deb3ec6SMatthias Ringwald 
1712ef54b27SMatthias 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){
1723deb3ec6SMatthias Ringwald     if (!name){
1733deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1743deb3ec6SMatthias Ringwald     }
1752ef54b27SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, SDP_Handsfree, rfcomm_channel_nr, name);
1763deb3ec6SMatthias Ringwald 
177aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
178aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
179aa4dd815SMatthias Ringwald }
1803deb3ec6SMatthias Ringwald 
1813deb3ec6SMatthias Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
1823deb3ec6SMatthias Ringwald     char buffer[20];
1833deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
1843deb3ec6SMatthias Ringwald     // printf("exchange_supported_features %s\n", buffer);
1853deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1863deb3ec6SMatthias Ringwald }
1873deb3ec6SMatthias Ringwald 
1883deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
1893deb3ec6SMatthias Ringwald     char buffer[30];
1903deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_AVAILABLE_CODECS);
1913deb3ec6SMatthias Ringwald     offset += join(buffer+offset, sizeof(buffer)-offset, hfp_codecs, hfp_codecs_nr);
1923deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
1933deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1943deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1953deb3ec6SMatthias Ringwald }
1963deb3ec6SMatthias Ringwald 
1973deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
1983deb3ec6SMatthias Ringwald     char buffer[20];
1993deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_INDICATOR);
2003deb3ec6SMatthias Ringwald     // printf("retrieve_indicators %s\n", buffer);
2013deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2023deb3ec6SMatthias Ringwald }
2033deb3ec6SMatthias Ringwald 
2043deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
2053deb3ec6SMatthias Ringwald     char buffer[20];
2063deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_INDICATOR);
2073deb3ec6SMatthias Ringwald     // printf("retrieve_indicators_status %s\n", buffer);
2083deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2093deb3ec6SMatthias Ringwald }
2103deb3ec6SMatthias Ringwald 
2113deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2123deb3ec6SMatthias Ringwald     char buffer[20];
2133deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
2143deb3ec6SMatthias Ringwald     // printf("toggle_indicator_status_update %s\n", buffer);
2153deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2163deb3ec6SMatthias Ringwald }
2173deb3ec6SMatthias Ringwald 
2183deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2193deb3ec6SMatthias Ringwald     char buffer[50];
2203deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
2213deb3ec6SMatthias Ringwald     offset += join_bitmap(buffer+offset, sizeof(buffer)-offset, indicators_status, indicators_nr);
2223deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
2233deb3ec6SMatthias Ringwald     buffer[offset] = 0;
2243deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2253deb3ec6SMatthias Ringwald }
2263deb3ec6SMatthias Ringwald 
2273deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
2283deb3ec6SMatthias Ringwald     char buffer[20];
2293deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
2303deb3ec6SMatthias Ringwald     // printf("retrieve_can_hold_call %s\n", buffer);
2313deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2323deb3ec6SMatthias Ringwald }
2333deb3ec6SMatthias Ringwald 
2343deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2353deb3ec6SMatthias Ringwald     char buffer[30];
2363deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
2373deb3ec6SMatthias Ringwald     offset += join(buffer+offset, sizeof(buffer)-offset, hfp_indicators, hfp_indicators_nr);
2383deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
2393deb3ec6SMatthias Ringwald     buffer[offset] = 0;
2403deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2413deb3ec6SMatthias Ringwald }
2423deb3ec6SMatthias Ringwald 
2433deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
2443deb3ec6SMatthias Ringwald     char buffer[20];
2453deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_GENERIC_STATUS_INDICATOR);
2463deb3ec6SMatthias Ringwald     // printf("retrieve_supported_generic_status_indicators %s\n", buffer);
2473deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2483deb3ec6SMatthias Ringwald }
2493deb3ec6SMatthias Ringwald 
2503deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
2513deb3ec6SMatthias Ringwald     char buffer[20];
2523deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_GENERIC_STATUS_INDICATOR);
2533deb3ec6SMatthias Ringwald     // printf("list_initital_supported_generic_status_indicators %s\n", buffer);
2543deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2553deb3ec6SMatthias Ringwald }
2563deb3ec6SMatthias Ringwald 
2573deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
2583deb3ec6SMatthias Ringwald     char buffer[20];
2593deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=3,0\r\n", HFP_QUERY_OPERATOR_SELECTION);
2603deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2613deb3ec6SMatthias Ringwald }
2623deb3ec6SMatthias Ringwald 
2633deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
2643deb3ec6SMatthias Ringwald     char buffer[20];
2653deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_QUERY_OPERATOR_SELECTION);
2663deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2673deb3ec6SMatthias Ringwald }
2683deb3ec6SMatthias Ringwald 
2693deb3ec6SMatthias Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
2703deb3ec6SMatthias Ringwald     char buffer[20];
2713deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
2723deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2733deb3ec6SMatthias Ringwald }
2743deb3ec6SMatthias Ringwald 
2753deb3ec6SMatthias Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
2763deb3ec6SMatthias Ringwald     char buffer[20];
2773deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_TRIGGER_CODEC_CONNECTION_SETUP);
2783deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2793deb3ec6SMatthias Ringwald }
2803deb3ec6SMatthias Ringwald 
2813deb3ec6SMatthias Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
2823deb3ec6SMatthias Ringwald     char buffer[20];
2833deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec);
2843deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2853deb3ec6SMatthias Ringwald }
2863deb3ec6SMatthias Ringwald 
287ce263fc8SMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
288ce263fc8SMatthias Ringwald     char buffer[10];
289ce263fc8SMatthias Ringwald     sprintf(buffer, "%s\r\n", HFP_CALL_ANSWERED);
290ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
291ce263fc8SMatthias Ringwald }
292ce263fc8SMatthias Ringwald 
293ce263fc8SMatthias Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
294ce263fc8SMatthias Ringwald     char buffer[40];
295ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SET_MICROPHONE_GAIN, gain);
296ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
297ce263fc8SMatthias Ringwald }
298ce263fc8SMatthias Ringwald 
299ce263fc8SMatthias Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
300ce263fc8SMatthias Ringwald     char buffer[40];
301ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SET_SPEAKER_GAIN, gain);
302ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
303ce263fc8SMatthias Ringwald }
304ce263fc8SMatthias Ringwald 
305ce263fc8SMatthias Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
306ce263fc8SMatthias Ringwald     char buffer[40];
307ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CLIP, activate);
308ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
309ce263fc8SMatthias Ringwald }
310ce263fc8SMatthias Ringwald 
311ce263fc8SMatthias Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
312ce263fc8SMatthias Ringwald     char buffer[40];
313ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_TURN_OFF_EC_AND_NR, activate);
314ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
315ce263fc8SMatthias Ringwald }
316ce263fc8SMatthias Ringwald 
317ce263fc8SMatthias Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
318ce263fc8SMatthias Ringwald     char buffer[40];
319ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate);
320ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
321ce263fc8SMatthias Ringwald }
322ce263fc8SMatthias Ringwald 
323ce263fc8SMatthias Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
324ce263fc8SMatthias Ringwald     char buffer[40];
325ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
326ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
327ce263fc8SMatthias Ringwald }
328ce263fc8SMatthias Ringwald 
329ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
330ce263fc8SMatthias Ringwald     char buffer[40];
331ce263fc8SMatthias Ringwald     sprintf(buffer, "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
332ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
333ce263fc8SMatthias Ringwald }
334ce263fc8SMatthias Ringwald 
335*a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
336ce263fc8SMatthias Ringwald     char buffer[40];
337*a0ffb263SMatthias Ringwald     sprintf(buffer, "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id);
338ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
339ce263fc8SMatthias Ringwald }
340ce263fc8SMatthias Ringwald 
341ce263fc8SMatthias Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
342ce263fc8SMatthias Ringwald     char buffer[20];
343ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_REDIAL_LAST_NUMBER);
344ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
345ce263fc8SMatthias Ringwald }
346ce263fc8SMatthias Ringwald 
347ce263fc8SMatthias Ringwald static int hfp_hf_send_chup(uint16_t cid){
348ce263fc8SMatthias Ringwald     char buffer[20];
349ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_HANG_UP_CALL);
350ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
351ce263fc8SMatthias Ringwald }
352ce263fc8SMatthias Ringwald 
353ce263fc8SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, int number){
354ce263fc8SMatthias Ringwald     char buffer[20];
355ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
356ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
357ce263fc8SMatthias Ringwald }
358ce263fc8SMatthias Ringwald 
359ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
360ce263fc8SMatthias Ringwald     char buffer[20];
361ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
362ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
363ce263fc8SMatthias Ringwald }
364ce263fc8SMatthias Ringwald 
365ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
366ce263fc8SMatthias Ringwald     char buffer[20];
367ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=1\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG);
368ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
369ce263fc8SMatthias Ringwald }
370ce263fc8SMatthias Ringwald 
371667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
372667ec068SMatthias Ringwald     char buffer[20];
373667ec068SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_LIST_CURRENT_CALLS);
374667ec068SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
375667ec068SMatthias Ringwald }
376667ec068SMatthias Ringwald 
377*a0ffb263SMatthias Ringwald static void hfp_emit_ag_indicator_event(hfp_callback_t callback, hfp_ag_indicator_t indicator){
3783deb3ec6SMatthias Ringwald     if (!callback) return;
379*a0ffb263SMatthias Ringwald     uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1];
3803deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3813deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3823deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
383*a0ffb263SMatthias Ringwald     event[3] = indicator.index;
384*a0ffb263SMatthias Ringwald     event[4] = indicator.status;
385*a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
386*a0ffb263SMatthias Ringwald     event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0;
3873deb3ec6SMatthias Ringwald     (*callback)(event, sizeof(event));
3883deb3ec6SMatthias Ringwald }
3893deb3ec6SMatthias Ringwald 
390*a0ffb263SMatthias Ringwald static void hfp_emit_network_operator_event(hfp_callback_t callback, hfp_network_opearator_t network_operator){
3913deb3ec6SMatthias Ringwald     if (!callback) return;
3923deb3ec6SMatthias Ringwald     uint8_t event[24];
3933deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3943deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3953deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
396*a0ffb263SMatthias Ringwald     event[3] = network_operator.mode;
397*a0ffb263SMatthias Ringwald     event[4] = network_operator.format;
398*a0ffb263SMatthias Ringwald     strcpy((char*)&event[5], network_operator.name);
3993deb3ec6SMatthias Ringwald     (*callback)(event, sizeof(event));
4003deb3ec6SMatthias Ringwald }
4013deb3ec6SMatthias Ringwald 
402*a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
403*a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
404*a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
405aa4dd815SMatthias Ringwald     int done = 1;
4063deb3ec6SMatthias Ringwald 
407*a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4083deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
409*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
410*a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4113deb3ec6SMatthias Ringwald             break;
4123deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
413*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
414*a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4153deb3ec6SMatthias Ringwald             break;
4163deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
417*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
418*a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4193deb3ec6SMatthias Ringwald             break;
4203deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
421*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
422*a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4233deb3ec6SMatthias Ringwald             break;
4243deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
425*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
426*a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4273deb3ec6SMatthias Ringwald             break;
4283deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
429*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
430*a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4313deb3ec6SMatthias Ringwald             break;
4323deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
433*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
434*a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4353deb3ec6SMatthias Ringwald             break;
4363deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
437*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
438*a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4393deb3ec6SMatthias Ringwald             break;
4403deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
441*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
442*a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4433deb3ec6SMatthias Ringwald             break;
4443deb3ec6SMatthias Ringwald         default:
445aa4dd815SMatthias Ringwald             done = 0;
4463deb3ec6SMatthias Ringwald             break;
4473deb3ec6SMatthias Ringwald     }
4483deb3ec6SMatthias Ringwald     return done;
4493deb3ec6SMatthias Ringwald }
4503deb3ec6SMatthias Ringwald 
451ce263fc8SMatthias Ringwald 
452*a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
453*a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
454*a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
455ce263fc8SMatthias Ringwald 
456ce263fc8SMatthias Ringwald     int done = 0;
457*a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
458*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
459ce263fc8SMatthias Ringwald         done = 1;
460*a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
461ce263fc8SMatthias Ringwald         return done;
462ce263fc8SMatthias Ringwald     };
463*a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
464*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
465ce263fc8SMatthias Ringwald         done = 1;
466*a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
467*a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
468*a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
469ce263fc8SMatthias Ringwald         return done;
470ce263fc8SMatthias Ringwald     }
471ce263fc8SMatthias Ringwald 
472*a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
473ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
474*a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
475*a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
476*a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
477ce263fc8SMatthias Ringwald             return 1;
478ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
479*a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
480*a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
481*a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
482ce263fc8SMatthias Ringwald             return 1;
483ce263fc8SMatthias Ringwald         default:
484ce263fc8SMatthias Ringwald             break;
485ce263fc8SMatthias Ringwald     }
486ce263fc8SMatthias Ringwald 
487*a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
488*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
489ce263fc8SMatthias Ringwald         done = 1;
490*a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
491ce263fc8SMatthias Ringwald         return done;
492ce263fc8SMatthias Ringwald     }
493ce263fc8SMatthias Ringwald 
494ce263fc8SMatthias Ringwald     return done;
495ce263fc8SMatthias Ringwald }
496ce263fc8SMatthias Ringwald 
497*a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
498ce263fc8SMatthias Ringwald     /* events ( == commands):
499ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
500ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
501ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
502ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
503ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
504ce263fc8SMatthias Ringwald     */
505ce263fc8SMatthias Ringwald 
506*a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
507ce263fc8SMatthias Ringwald 
508*a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
509ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
510*a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
511ce263fc8SMatthias Ringwald 
512*a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
513*a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
514*a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
515ce263fc8SMatthias Ringwald             return 1;
516ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
517*a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = 0;
518*a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = 0;
519*a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = 0;
520ce263fc8SMatthias Ringwald 
521*a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
522*a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
523*a0ffb263SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
524ce263fc8SMatthias Ringwald             break;
525ce263fc8SMatthias Ringwald 
526ce263fc8SMatthias Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:
527*a0ffb263SMatthias Ringwald             if (hfp_hf_supports_codec(hfp_connection->suggested_codec)){
528*a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
529*a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
530*a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
531*a0ffb263SMatthias Ringwald                 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
532ce263fc8SMatthias Ringwald             } else {
533*a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
534*a0ffb263SMatthias Ringwald                 hfp_connection->suggested_codec = 0;
535*a0ffb263SMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
536*a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
537*a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
538*a0ffb263SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
539ce263fc8SMatthias Ringwald 
540ce263fc8SMatthias Ringwald             }
541ce263fc8SMatthias Ringwald             break;
542ce263fc8SMatthias Ringwald 
543ce263fc8SMatthias Ringwald         default:
544ce263fc8SMatthias Ringwald             break;
545ce263fc8SMatthias Ringwald     }
546ce263fc8SMatthias Ringwald     return 0;
547ce263fc8SMatthias Ringwald }
548ce263fc8SMatthias Ringwald 
549*a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
550*a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
551*a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
552ce263fc8SMatthias Ringwald 
553ce263fc8SMatthias Ringwald 
554*a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
555*a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
556*a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
557*a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
558ce263fc8SMatthias Ringwald         return 1;
559ce263fc8SMatthias Ringwald     }
560ce263fc8SMatthias Ringwald 
561*a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
562ce263fc8SMatthias Ringwald 
563ce263fc8SMatthias Ringwald     // run codecs exchange
564*a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
565ce263fc8SMatthias Ringwald     if (done) return 1;
566ce263fc8SMatthias Ringwald 
567*a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
568*a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
569*a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
570*a0ffb263SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection->acl_handle, hfp_connection->link_setting);
571ce263fc8SMatthias Ringwald         return 1;
572ce263fc8SMatthias Ringwald     }
573ce263fc8SMatthias Ringwald 
574ce263fc8SMatthias Ringwald     return 0;
575ce263fc8SMatthias Ringwald }
576ce263fc8SMatthias Ringwald 
577*a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
578*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
579*a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
580*a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
581ce263fc8SMatthias Ringwald         return 1;
582ce263fc8SMatthias Ringwald     }
583ce263fc8SMatthias Ringwald     return 0;
584ce263fc8SMatthias Ringwald }
585ce263fc8SMatthias Ringwald 
586*a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){
587*a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
588*a0ffb263SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return;
589ce263fc8SMatthias Ringwald 
590*a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
591ce263fc8SMatthias Ringwald     if (!done){
592*a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
593ce263fc8SMatthias Ringwald     }
594ce263fc8SMatthias Ringwald     if (!done){
595*a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
596ce263fc8SMatthias Ringwald     }
597ce263fc8SMatthias Ringwald     if (!done){
598*a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
599ce263fc8SMatthias Ringwald     }
600ce263fc8SMatthias Ringwald 
601*a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
602*a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
603*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
604*a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
605ce263fc8SMatthias Ringwald         return;
606ce263fc8SMatthias Ringwald     }
607ce263fc8SMatthias Ringwald 
608*a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
609*a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
610*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
611*a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
612ce263fc8SMatthias Ringwald         return;
613ce263fc8SMatthias Ringwald     }
614ce263fc8SMatthias Ringwald 
615*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
616*a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
617*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
618*a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
619ce263fc8SMatthias Ringwald         return;
620ce263fc8SMatthias Ringwald     }
621ce263fc8SMatthias Ringwald 
622*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
623*a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
624*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
625*a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
626ce263fc8SMatthias Ringwald         return;
627ce263fc8SMatthias Ringwald     }
628ce263fc8SMatthias Ringwald 
629*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
630*a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
631*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
632*a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
633ce263fc8SMatthias Ringwald         return;
634ce263fc8SMatthias Ringwald     }
635ce263fc8SMatthias Ringwald 
636*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
637*a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
638*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
639*a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
640ce263fc8SMatthias Ringwald         return;
641ce263fc8SMatthias Ringwald     }
642ce263fc8SMatthias Ringwald 
643*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
644*a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
645*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
646*a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
647ce263fc8SMatthias Ringwald         return;
648ce263fc8SMatthias Ringwald     }
649ce263fc8SMatthias Ringwald 
650*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
651*a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
652*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
653*a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
654ce263fc8SMatthias Ringwald         return;
655ce263fc8SMatthias Ringwald     }
656ce263fc8SMatthias Ringwald 
657ce263fc8SMatthias Ringwald 
658*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
659*a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
660*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
661*a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
662ce263fc8SMatthias Ringwald         return;
663ce263fc8SMatthias Ringwald     }
664ce263fc8SMatthias Ringwald 
665*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
666*a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
667*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
668*a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
669ce263fc8SMatthias Ringwald         return;
670ce263fc8SMatthias Ringwald     }
671ce263fc8SMatthias Ringwald 
672*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
673*a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
674*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
675*a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
676ce263fc8SMatthias Ringwald         return;
677ce263fc8SMatthias Ringwald     }
678ce263fc8SMatthias Ringwald 
679*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
680*a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
681*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
682*a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
683ce263fc8SMatthias Ringwald         return;
684ce263fc8SMatthias Ringwald     }
685ce263fc8SMatthias Ringwald 
686*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
687*a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
688*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
689*a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
690ce263fc8SMatthias Ringwald         return;
691ce263fc8SMatthias Ringwald     }
692ce263fc8SMatthias Ringwald 
693*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
694*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
695*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
696*a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
697ce263fc8SMatthias Ringwald         return;
698ce263fc8SMatthias Ringwald     }
699ce263fc8SMatthias Ringwald 
700*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
701*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
702*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
703*a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
704ce263fc8SMatthias Ringwald         return;
705ce263fc8SMatthias Ringwald     }
706ce263fc8SMatthias Ringwald 
707*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
708*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
709*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
710*a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
711ce263fc8SMatthias Ringwald         return;
712ce263fc8SMatthias Ringwald     }
713ce263fc8SMatthias Ringwald 
714*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
715*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
716*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
717*a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
718ce263fc8SMatthias Ringwald         return;
719ce263fc8SMatthias Ringwald     }
720ce263fc8SMatthias Ringwald 
721*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
722*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
723*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
724*a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
725ce263fc8SMatthias Ringwald         return;
726ce263fc8SMatthias Ringwald     }
727ce263fc8SMatthias Ringwald 
728*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
729*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
730*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
731*a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
732ce263fc8SMatthias Ringwald         return;
733ce263fc8SMatthias Ringwald     }
734ce263fc8SMatthias Ringwald 
735*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
736*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
737*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
738*a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
739667ec068SMatthias Ringwald         return;
740667ec068SMatthias Ringwald     }
741667ec068SMatthias Ringwald 
742*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
743*a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
744*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
745*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
746*a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
747ce263fc8SMatthias Ringwald         return;
748ce263fc8SMatthias Ringwald     }
749ce263fc8SMatthias Ringwald 
750*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
751*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
752*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
753*a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
754ce263fc8SMatthias Ringwald         return;
755ce263fc8SMatthias Ringwald     }
756ce263fc8SMatthias Ringwald 
757*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
758*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
759*a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
760*a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
761667ec068SMatthias Ringwald         return;
762667ec068SMatthias Ringwald     }
763667ec068SMatthias Ringwald 
764*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
765*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
766667ec068SMatthias Ringwald         char buffer[20];
767*a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
768667ec068SMatthias Ringwald             case '?':
769667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
770*a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
771667ec068SMatthias Ringwald                 return;
772667ec068SMatthias Ringwald             case '0':
773667ec068SMatthias Ringwald             case '1':
774667ec068SMatthias Ringwald             case '2':
775*a0ffb263SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
776*a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
777667ec068SMatthias Ringwald                 return;
778667ec068SMatthias Ringwald             default:
779667ec068SMatthias Ringwald                 break;
780667ec068SMatthias Ringwald         }
781667ec068SMatthias Ringwald         return;
782667ec068SMatthias Ringwald     }
783667ec068SMatthias Ringwald 
784*a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
785*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
786667ec068SMatthias Ringwald         char buffer[20];
787667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
788*a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
789667ec068SMatthias Ringwald         return;
790667ec068SMatthias Ringwald     }
791667ec068SMatthias Ringwald 
792667ec068SMatthias Ringwald     // update HF indicators
793*a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
794667ec068SMatthias Ringwald         int i;
795667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
796*a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
797*a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
798*a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
799*a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
800667ec068SMatthias Ringwald                     char buffer[30];
801667ec068SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], hfp_indicators_value[i]);
802*a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
803667ec068SMatthias Ringwald                 } else {
804667ec068SMatthias Ringwald                     printf("Not sending HF indicator %u as it is disabled\n", hfp_indicators[i]);
805667ec068SMatthias Ringwald                 }
806667ec068SMatthias Ringwald                 return;
807667ec068SMatthias Ringwald             }
808667ec068SMatthias Ringwald         }
809667ec068SMatthias Ringwald     }
810667ec068SMatthias Ringwald 
811ce263fc8SMatthias Ringwald     if (done) return;
812ce263fc8SMatthias Ringwald     // deal with disconnect
813*a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
814ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
815*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
816*a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
817ce263fc8SMatthias Ringwald             break;
818ce263fc8SMatthias Ringwald 
819ce263fc8SMatthias Ringwald         default:
820ce263fc8SMatthias Ringwald             break;
821ce263fc8SMatthias Ringwald     }
822ce263fc8SMatthias Ringwald }
823ce263fc8SMatthias Ringwald 
824*a0ffb263SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){
825ce263fc8SMatthias Ringwald     // determine highest possible link setting
826*a0ffb263SMatthias Ringwald     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
827*a0ffb263SMatthias Ringwald     if (hci_remote_esco_supported(hfp_connection->acl_handle)){
828*a0ffb263SMatthias Ringwald         hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
829ce263fc8SMatthias Ringwald         if ((hfp_supported_features             & (1<<HFP_HFSF_ESCO_S4))
830*a0ffb263SMatthias Ringwald         &&  (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4))){
831*a0ffb263SMatthias Ringwald             hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
832ce263fc8SMatthias Ringwald         }
833ce263fc8SMatthias Ringwald     }
834ce263fc8SMatthias Ringwald }
835ce263fc8SMatthias Ringwald 
836*a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
837*a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
838ce263fc8SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0);
839*a0ffb263SMatthias Ringwald     hfp_init_link_settings(hfp_connection);
840667ec068SMatthias Ringwald     // restore volume settings
841*a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
842*a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
843667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
844*a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
845*a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
846667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
847667ec068SMatthias Ringwald     // enable all indicators
848667ec068SMatthias Ringwald     int i;
849667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
850*a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
851*a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
852667ec068SMatthias Ringwald     }
853ce263fc8SMatthias Ringwald }
854ce263fc8SMatthias Ringwald 
855*a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
856*a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
857ce263fc8SMatthias Ringwald     int done = 1;
858*a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
8593deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
860*a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
861*a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
8623deb3ec6SMatthias Ringwald                 break;
8633deb3ec6SMatthias Ringwald             }
864*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8653deb3ec6SMatthias Ringwald             break;
8663deb3ec6SMatthias Ringwald 
8673deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
868*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8693deb3ec6SMatthias Ringwald             break;
8703deb3ec6SMatthias Ringwald 
8713deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
872*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
8733deb3ec6SMatthias Ringwald             break;
8743deb3ec6SMatthias Ringwald 
8753deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
876*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
8773deb3ec6SMatthias Ringwald             break;
8783deb3ec6SMatthias Ringwald 
8793deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
880*a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
881*a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
8823deb3ec6SMatthias Ringwald                 break;
8833deb3ec6SMatthias Ringwald             }
884*a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
885*a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8863deb3ec6SMatthias Ringwald                 break;
8873deb3ec6SMatthias Ringwald             }
888*a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8893deb3ec6SMatthias Ringwald             break;
8903deb3ec6SMatthias Ringwald 
8913deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
892*a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
893*a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8943deb3ec6SMatthias Ringwald                 break;
8953deb3ec6SMatthias Ringwald             }
896*a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8973deb3ec6SMatthias Ringwald             break;
8983deb3ec6SMatthias Ringwald 
8993deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
900*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
9013deb3ec6SMatthias Ringwald             break;
9023deb3ec6SMatthias Ringwald 
9033deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
904*a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9053deb3ec6SMatthias Ringwald             break;
9063deb3ec6SMatthias Ringwald 
9073deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
908*a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9093deb3ec6SMatthias Ringwald             break;
910ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
911*a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
912*a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
9133deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
914ce263fc8SMatthias Ringwald                 break;
915ce263fc8SMatthias Ringwald             }
9163deb3ec6SMatthias Ringwald 
917*a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
918*a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
9193deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
920ce263fc8SMatthias Ringwald                 break;
9213deb3ec6SMatthias Ringwald             }
9223deb3ec6SMatthias Ringwald 
923*a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
924ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
925ce263fc8SMatthias Ringwald                     printf("Format set, querying name\n");
926*a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
927ce263fc8SMatthias Ringwald                     break;
928ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
929*a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
930*a0ffb263SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_callback, hfp_connection->network_operator);
931ce263fc8SMatthias Ringwald                     break;
932ce263fc8SMatthias Ringwald                 default:
933ce263fc8SMatthias Ringwald                     break;
9343deb3ec6SMatthias Ringwald             }
935ce263fc8SMatthias Ringwald 
936*a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
937*a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
938ce263fc8SMatthias Ringwald                 break;
9393deb3ec6SMatthias Ringwald             }
9403deb3ec6SMatthias Ringwald 
941*a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
942aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
943*a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9443deb3ec6SMatthias Ringwald                     break;
945ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
946*a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
947ce263fc8SMatthias Ringwald                     hfp_emit_event(hfp_callback, HFP_SUBEVENT_CODECS_CONNECTION_COMPLETE, 0);
948ce263fc8SMatthias Ringwald                     break;
9493deb3ec6SMatthias Ringwald                 default:
950ce263fc8SMatthias Ringwald                     done = 0;
9513deb3ec6SMatthias Ringwald                     break;
9523deb3ec6SMatthias Ringwald             }
9533deb3ec6SMatthias Ringwald             break;
9543deb3ec6SMatthias Ringwald         default:
955ce263fc8SMatthias Ringwald             done = 0;
9563deb3ec6SMatthias Ringwald             break;
9573deb3ec6SMatthias Ringwald     }
9583deb3ec6SMatthias Ringwald 
9593deb3ec6SMatthias Ringwald     // done
960*a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
9613deb3ec6SMatthias Ringwald }
9623deb3ec6SMatthias Ringwald 
9633deb3ec6SMatthias Ringwald 
9643deb3ec6SMatthias Ringwald static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
965*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
966*a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
9673deb3ec6SMatthias Ringwald 
9681e35c04dSMatthias Ringwald     char last_char = packet[size-1];
9691e35c04dSMatthias Ringwald     packet[size-1] = 0;
9701e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
9711e35c04dSMatthias Ringwald     packet[size-1] = last_char;
9721e35c04dSMatthias Ringwald 
973667ec068SMatthias Ringwald     int pos, i, value;
9743deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
975*a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
976ce263fc8SMatthias Ringwald     }
9773deb3ec6SMatthias Ringwald 
978*a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
979667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
980*a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
981*a0ffb263SMatthias Ringwald             // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type);
982*a0ffb263SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
983667ec068SMatthias Ringwald             break;
984667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
985*a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
986*a0ffb263SMatthias Ringwald             // printf("Response and Hold status: %s\n", hfp_connection->line_buffer);
987*a0ffb263SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, atoi((char *)&hfp_connection->line_buffer[0]));
988667ec068SMatthias Ringwald             break;
989667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
990*a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
991*a0ffb263SMatthias Ringwald             // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
992*a0ffb263SMatthias Ringwald             //      hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
993*a0ffb263SMatthias Ringwald             //      hfp_connection->bnip_number, hfp_connection->bnip_type);
994*a0ffb263SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_callback, hfp_connection->clcc_idx,
995*a0ffb263SMatthias Ringwald                 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
996*a0ffb263SMatthias Ringwald                 hfp_connection->bnip_type, hfp_connection->bnip_number);
997667ec068SMatthias Ringwald             break;
998ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
999*a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1000*a0ffb263SMatthias Ringwald             value = atoi((char*)hfp_connection->line_buffer);
1001667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1002667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1003ce263fc8SMatthias Ringwald             break;
1004ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1005*a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1006*a0ffb263SMatthias Ringwald             value = atoi((char*)hfp_connection->line_buffer);
1007667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1008667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1009ce263fc8SMatthias Ringwald             break;
1010ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1011*a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1012*a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1013*a0ffb263SMatthias Ringwald             break;
1014*a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1015*a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1016*a0ffb263SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1017*a0ffb263SMatthias Ringwald             break;
1018*a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1019*a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1020*a0ffb263SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALLING_LINE_INDETIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1021ce263fc8SMatthias Ringwald             break;
1022ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1023*a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1024*a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1025*a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1026*a0ffb263SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1027ce263fc8SMatthias Ringwald             break;
1028ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1029*a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1030*a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1031*a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1032ce263fc8SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1);
1033ce263fc8SMatthias Ringwald             break;
1034ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1035*a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1036ce263fc8SMatthias Ringwald             break;
1037ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
1038*a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_RING);
1039ce263fc8SMatthias Ringwald             break;
1040ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1041*a0ffb263SMatthias Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1042*a0ffb263SMatthias Ringwald                 if (hfp_connection->ag_indicators[i].status_changed) {
1043*a0ffb263SMatthias Ringwald                     if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1044*a0ffb263SMatthias Ringwald                         hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1045*a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1046*a0ffb263SMatthias Ringwald                         hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1047*a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1048*a0ffb263SMatthias Ringwald                         hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1049ce263fc8SMatthias Ringwald                     }
1050*a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[i].status_changed = 0;
1051*a0ffb263SMatthias Ringwald                     hfp_emit_ag_indicator_event(hfp_callback, hfp_connection->ag_indicators[i]);
10523deb3ec6SMatthias Ringwald                     break;
10533deb3ec6SMatthias Ringwald                 }
10543deb3ec6SMatthias Ringwald             }
1055ce263fc8SMatthias Ringwald             break;
1056ce263fc8SMatthias Ringwald         default:
1057ce263fc8SMatthias Ringwald             break;
10583deb3ec6SMatthias Ringwald     }
1059*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
10603deb3ec6SMatthias Ringwald }
10613deb3ec6SMatthias Ringwald 
10623deb3ec6SMatthias Ringwald static void hfp_run(){
1063665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1064665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1065665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1066*a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1067*a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
10683deb3ec6SMatthias Ringwald     }
10693deb3ec6SMatthias Ringwald }
10703deb3ec6SMatthias Ringwald 
1071ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10723deb3ec6SMatthias Ringwald     switch (packet_type){
10733deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
10743deb3ec6SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size);
10753deb3ec6SMatthias Ringwald             break;
10763deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1077f4000eebSMatthias Ringwald             hfp_handle_hci_event(packet_type, packet, size);
10783deb3ec6SMatthias Ringwald         default:
10793deb3ec6SMatthias Ringwald             break;
10803deb3ec6SMatthias Ringwald     }
10813deb3ec6SMatthias Ringwald     hfp_run();
10823deb3ec6SMatthias Ringwald }
10833deb3ec6SMatthias Ringwald 
1084*a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1085*a0ffb263SMatthias Ringwald     l2cap_init();
1086*a0ffb263SMatthias Ringwald     l2cap_register_packet_handler(packet_handler);
1087*a0ffb263SMatthias Ringwald     rfcomm_register_packet_handler(packet_handler);
1088*a0ffb263SMatthias Ringwald     hfp_init(rfcomm_channel_nr);
1089*a0ffb263SMatthias Ringwald 
1090*a0ffb263SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1091*a0ffb263SMatthias Ringwald     hfp_codecs_nr = 0;
1092*a0ffb263SMatthias Ringwald     hfp_indicators_nr = 0;
1093*a0ffb263SMatthias Ringwald     hfp_hf_speaker_gain = 9;
1094*a0ffb263SMatthias Ringwald     hfp_hf_microphone_gain = 9;
1095*a0ffb263SMatthias Ringwald }
1096*a0ffb263SMatthias Ringwald 
1097*a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
10983deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1099*a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
11003deb3ec6SMatthias Ringwald         return;
11013deb3ec6SMatthias Ringwald     }
11023deb3ec6SMatthias Ringwald 
11033deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
11043deb3ec6SMatthias Ringwald     int i;
11053deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
11063deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
11073deb3ec6SMatthias Ringwald     }
11083deb3ec6SMatthias Ringwald 
11093deb3ec6SMatthias Ringwald     char buffer[30];
11103deb3ec6SMatthias Ringwald     int offset = join(buffer, sizeof(buffer), hfp_codecs, hfp_codecs_nr);
11113deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1112665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1113665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1114665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1115*a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1116*a0ffb263SMatthias Ringwald         if (! hfp_connection) continue;
1117*a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_AVAILABLE_CODECS;
1118*a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
11193deb3ec6SMatthias Ringwald     }
11203deb3ec6SMatthias Ringwald }
11213deb3ec6SMatthias Ringwald 
1122*a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
11233deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1124*a0ffb263SMatthias Ringwald }
11253deb3ec6SMatthias Ringwald 
1126*a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
11273deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
11283deb3ec6SMatthias Ringwald     int i;
1129*a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
11303deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
11313deb3ec6SMatthias Ringwald     }
11323deb3ec6SMatthias Ringwald }
11333deb3ec6SMatthias Ringwald 
11343deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
11353deb3ec6SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway);
11363deb3ec6SMatthias Ringwald }
11373deb3ec6SMatthias Ringwald 
11383deb3ec6SMatthias Ringwald void hfp_hf_release_service_level_connection(bd_addr_t bd_addr){
1139*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1140*a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
1141*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11423deb3ec6SMatthias Ringwald }
11433deb3ec6SMatthias Ringwald 
1144ce263fc8SMatthias Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(bd_addr_t bd_addr, uint8_t enable){
11453deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1146*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1147*a0ffb263SMatthias Ringwald     if (!hfp_connection){
1148*a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
11493deb3ec6SMatthias Ringwald         return;
11503deb3ec6SMatthias Ringwald     }
1151*a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
1152*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11533deb3ec6SMatthias Ringwald }
11543deb3ec6SMatthias Ringwald 
1155ce263fc8SMatthias Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1156ce263fc8SMatthias Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 1);
1157ce263fc8SMatthias Ringwald }
1158ce263fc8SMatthias Ringwald 
1159ce263fc8SMatthias Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1160ce263fc8SMatthias Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 0);
1161ce263fc8SMatthias Ringwald }
1162ce263fc8SMatthias Ringwald 
11633deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1164ce263fc8SMatthias Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(bd_addr_t bd_addr, uint32_t indicators_status_bitmap){
11653deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1166*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1167*a0ffb263SMatthias Ringwald     if (!hfp_connection){
1168*a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
11693deb3ec6SMatthias Ringwald         return;
11703deb3ec6SMatthias Ringwald     }
1171*a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1172*a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1173*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11743deb3ec6SMatthias Ringwald }
11753deb3ec6SMatthias Ringwald 
11763deb3ec6SMatthias Ringwald void hfp_hf_query_operator_selection(bd_addr_t bd_addr){
11773deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1178*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1179*a0ffb263SMatthias Ringwald     if (!hfp_connection){
1180*a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
11813deb3ec6SMatthias Ringwald         return;
11823deb3ec6SMatthias Ringwald     }
1183*a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1184ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1185*a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1186ce263fc8SMatthias Ringwald             break;
1187ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1188*a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1189ce263fc8SMatthias Ringwald             break;
1190ce263fc8SMatthias Ringwald         default:
1191ce263fc8SMatthias Ringwald             break;
1192ce263fc8SMatthias Ringwald     }
1193*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11943deb3ec6SMatthias Ringwald }
11953deb3ec6SMatthias Ringwald 
1196ce263fc8SMatthias Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, uint8_t enable){
11973deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1198*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1199*a0ffb263SMatthias Ringwald     if (!hfp_connection){
1200*a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
12013deb3ec6SMatthias Ringwald         return;
12023deb3ec6SMatthias Ringwald     }
1203*a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1204*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12053deb3ec6SMatthias Ringwald }
12063deb3ec6SMatthias Ringwald 
1207ce263fc8SMatthias Ringwald 
1208ce263fc8SMatthias Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1209ce263fc8SMatthias Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 1);
1210ce263fc8SMatthias Ringwald }
1211ce263fc8SMatthias Ringwald 
1212ce263fc8SMatthias Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1213ce263fc8SMatthias Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 0);
1214ce263fc8SMatthias Ringwald }
1215ce263fc8SMatthias Ringwald 
1216ce263fc8SMatthias Ringwald 
12173deb3ec6SMatthias Ringwald void hfp_hf_establish_audio_connection(bd_addr_t bd_addr){
12183deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1219*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1220*a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1221ce263fc8SMatthias Ringwald 
1222*a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1223*a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
12243deb3ec6SMatthias Ringwald 
1225*a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1226ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1227*a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1228*a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1229ce263fc8SMatthias Ringwald     } else {
1230*a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1231aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1232aa4dd815SMatthias Ringwald                 break;
1233aa4dd815SMatthias Ringwald             default:
1234*a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1235aa4dd815SMatthias Ringwald                 break;
12363deb3ec6SMatthias Ringwald         }
1237ce263fc8SMatthias Ringwald     }
1238ce263fc8SMatthias Ringwald 
1239*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12403deb3ec6SMatthias Ringwald }
12413deb3ec6SMatthias Ringwald 
12423deb3ec6SMatthias Ringwald void hfp_hf_release_audio_connection(bd_addr_t bd_addr){
1243*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1244*a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
1245*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12463deb3ec6SMatthias Ringwald }
12473deb3ec6SMatthias Ringwald 
1248ce263fc8SMatthias Ringwald void hfp_hf_answer_incoming_call(bd_addr_t bd_addr){
1249ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1250*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1251ce263fc8SMatthias Ringwald 
1252ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1253*a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
1254*a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1255ce263fc8SMatthias Ringwald     } else {
1256ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1257ce263fc8SMatthias Ringwald     }
1258ce263fc8SMatthias Ringwald }
1259ce263fc8SMatthias Ringwald 
1260ce263fc8SMatthias Ringwald void hfp_hf_terminate_call(bd_addr_t bd_addr){
1261ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1262*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1263ce263fc8SMatthias Ringwald 
1264*a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
1265*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1266ce263fc8SMatthias Ringwald }
1267ce263fc8SMatthias Ringwald 
1268*a0ffb263SMatthias Ringwald void hfp_hf_reject_incoming_call(bd_addr_t bd_addr){
1269ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1270*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1271ce263fc8SMatthias Ringwald 
1272ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1273*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
1274*a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1275ce263fc8SMatthias Ringwald     }
1276ce263fc8SMatthias Ringwald }
1277ce263fc8SMatthias Ringwald 
1278ce263fc8SMatthias Ringwald void hfp_hf_user_busy(bd_addr_t addr){
1279ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1280*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1281ce263fc8SMatthias Ringwald 
1282ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1283*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
1284*a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1285ce263fc8SMatthias Ringwald     }
1286ce263fc8SMatthias Ringwald }
1287ce263fc8SMatthias Ringwald 
1288ce263fc8SMatthias Ringwald void hfp_hf_end_active_and_accept_other(bd_addr_t addr){
1289ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1290*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1291ce263fc8SMatthias Ringwald 
1292ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1293ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1294*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
1295*a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1296ce263fc8SMatthias Ringwald     }
1297ce263fc8SMatthias Ringwald }
1298ce263fc8SMatthias Ringwald 
1299ce263fc8SMatthias Ringwald void hfp_hf_swap_calls(bd_addr_t addr){
1300ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1301*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1302ce263fc8SMatthias Ringwald 
1303ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1304ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1305*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
1306*a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1307ce263fc8SMatthias Ringwald     }
1308ce263fc8SMatthias Ringwald }
1309ce263fc8SMatthias Ringwald 
1310ce263fc8SMatthias Ringwald void hfp_hf_join_held_call(bd_addr_t addr){
1311ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1312*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1313ce263fc8SMatthias Ringwald 
1314ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1315ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1316*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
1317*a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1318ce263fc8SMatthias Ringwald     }
1319ce263fc8SMatthias Ringwald }
1320ce263fc8SMatthias Ringwald 
1321ce263fc8SMatthias Ringwald void hfp_hf_connect_calls(bd_addr_t addr){
1322ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1323*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1324ce263fc8SMatthias Ringwald 
1325ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1326ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1327*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
1328*a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1329ce263fc8SMatthias Ringwald     }
1330ce263fc8SMatthias Ringwald }
1331ce263fc8SMatthias Ringwald 
1332667ec068SMatthias Ringwald void hfp_hf_release_call_with_index(bd_addr_t addr, int index){
1333667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1334*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1335667ec068SMatthias Ringwald 
1336667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1337667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1338*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1339*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
1340*a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1341667ec068SMatthias Ringwald     }
1342667ec068SMatthias Ringwald }
1343667ec068SMatthias Ringwald 
1344667ec068SMatthias Ringwald void hfp_hf_private_consultation_with_call(bd_addr_t addr, int index){
1345667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1346*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1347667ec068SMatthias Ringwald 
1348667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1349667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1350*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1351*a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
1352*a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1353667ec068SMatthias Ringwald     }
1354667ec068SMatthias Ringwald }
1355ce263fc8SMatthias Ringwald 
1356ce263fc8SMatthias Ringwald void hfp_hf_dial_number(bd_addr_t bd_addr, char * number){
1357ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1358*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1359ce263fc8SMatthias Ringwald 
1360*a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1361ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1362*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1363ce263fc8SMatthias Ringwald }
1364ce263fc8SMatthias Ringwald 
1365*a0ffb263SMatthias Ringwald void hfp_hf_dial_memory(bd_addr_t bd_addr, int memory_id){
1366ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1367*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1368ce263fc8SMatthias Ringwald 
1369*a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1370*a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1371*a0ffb263SMatthias Ringwald 
1372*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1373ce263fc8SMatthias Ringwald }
1374ce263fc8SMatthias Ringwald 
1375ce263fc8SMatthias Ringwald void hfp_hf_redial_last_number(bd_addr_t bd_addr){
1376ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1377*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1378ce263fc8SMatthias Ringwald 
1379*a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
1380*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1381ce263fc8SMatthias Ringwald }
1382ce263fc8SMatthias Ringwald 
1383ce263fc8SMatthias Ringwald void hfp_hf_activate_call_waiting_notification(bd_addr_t bd_addr){
1384ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1385*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1386ce263fc8SMatthias Ringwald 
1387*a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
1388*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1389ce263fc8SMatthias Ringwald }
1390ce263fc8SMatthias Ringwald 
1391ce263fc8SMatthias Ringwald 
1392ce263fc8SMatthias Ringwald void hfp_hf_deactivate_call_waiting_notification(bd_addr_t bd_addr){
1393ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1394*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1395ce263fc8SMatthias Ringwald 
1396*a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1397*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1398ce263fc8SMatthias Ringwald }
1399ce263fc8SMatthias Ringwald 
1400ce263fc8SMatthias Ringwald 
1401ce263fc8SMatthias Ringwald void hfp_hf_activate_calling_line_notification(bd_addr_t bd_addr){
1402ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1403*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1404ce263fc8SMatthias Ringwald 
1405*a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
1406*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1407ce263fc8SMatthias Ringwald }
1408ce263fc8SMatthias Ringwald 
1409ce263fc8SMatthias Ringwald void hfp_hf_deactivate_calling_line_notification(bd_addr_t bd_addr){
1410ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1411*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1412ce263fc8SMatthias Ringwald 
1413*a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
1414*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1415ce263fc8SMatthias Ringwald }
1416ce263fc8SMatthias Ringwald 
1417ce263fc8SMatthias Ringwald 
1418ce263fc8SMatthias Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1419ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1420*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1421ce263fc8SMatthias Ringwald 
1422*a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1423*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1424ce263fc8SMatthias Ringwald }
1425ce263fc8SMatthias Ringwald 
1426ce263fc8SMatthias Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1427ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1428*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1429ce263fc8SMatthias Ringwald 
1430*a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1431*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1432ce263fc8SMatthias Ringwald }
1433ce263fc8SMatthias Ringwald 
1434ce263fc8SMatthias Ringwald void hfp_hf_activate_voice_recognition_notification(bd_addr_t bd_addr){
1435ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1436*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1437ce263fc8SMatthias Ringwald 
1438*a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
1439*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1440ce263fc8SMatthias Ringwald }
1441ce263fc8SMatthias Ringwald 
1442ce263fc8SMatthias Ringwald void hfp_hf_deactivate_voice_recognition_notification(bd_addr_t bd_addr){
1443ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1444*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1445ce263fc8SMatthias Ringwald 
1446*a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1447*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1448ce263fc8SMatthias Ringwald }
1449ce263fc8SMatthias Ringwald 
1450ce263fc8SMatthias Ringwald void hfp_hf_set_microphone_gain(bd_addr_t bd_addr, int gain){
1451ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1452*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1453*a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1454*a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1455*a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1456*a0ffb263SMatthias Ringwald         return;
1457*a0ffb263SMatthias Ringwald     }
1458*a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1459*a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1460*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1461ce263fc8SMatthias Ringwald }
1462ce263fc8SMatthias Ringwald 
1463ce263fc8SMatthias Ringwald void hfp_hf_set_speaker_gain(bd_addr_t bd_addr, int gain){
1464ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1465*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1466*a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1467*a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1468*a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1469*a0ffb263SMatthias Ringwald         return;
1470*a0ffb263SMatthias Ringwald     }
1471*a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1472*a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1473*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1474ce263fc8SMatthias Ringwald }
1475ce263fc8SMatthias Ringwald 
1476ce263fc8SMatthias Ringwald void hfp_hf_send_dtmf_code(bd_addr_t addr, char code){
1477ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1478*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1479*a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
1480*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1481ce263fc8SMatthias Ringwald }
1482ce263fc8SMatthias Ringwald 
1483ce263fc8SMatthias Ringwald void hfp_hf_request_phone_number_for_voice_tag(bd_addr_t addr){
1484ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1485*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1486*a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
1487*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1488ce263fc8SMatthias Ringwald }
14893deb3ec6SMatthias Ringwald 
1490667ec068SMatthias Ringwald void hfp_hf_query_current_call_status(bd_addr_t addr){
1491667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1492*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1493*a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
1494*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1495667ec068SMatthias Ringwald }
1496667ec068SMatthias Ringwald 
1497667ec068SMatthias Ringwald 
1498667ec068SMatthias Ringwald void hfp_hf_rrh_query_status(bd_addr_t addr){
1499667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1500*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1501*a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1502*a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
1503*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1504667ec068SMatthias Ringwald }
1505667ec068SMatthias Ringwald 
1506667ec068SMatthias Ringwald void hfp_hf_rrh_hold_call(bd_addr_t addr){
1507667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1508*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1509*a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1510*a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
1511*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1512667ec068SMatthias Ringwald }
1513667ec068SMatthias Ringwald 
1514667ec068SMatthias Ringwald void hfp_hf_rrh_accept_held_call(bd_addr_t addr){
1515667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1516*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1517*a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1518*a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
1519*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1520667ec068SMatthias Ringwald }
1521667ec068SMatthias Ringwald 
1522*a0ffb263SMatthias Ringwald void hfp_hf_rrh_reject_held_call(bd_addr_t addr){
1523667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1524*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1525*a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1526*a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
1527*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1528667ec068SMatthias Ringwald }
1529667ec068SMatthias Ringwald 
1530*a0ffb263SMatthias Ringwald void hfp_hf_query_subscriber_number(bd_addr_t addr){
1531667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1532*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1533*a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
1534*a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1535667ec068SMatthias Ringwald }
1536667ec068SMatthias Ringwald 
1537667ec068SMatthias Ringwald void hfp_hf_set_hf_indicator(bd_addr_t addr, int assigned_number, int value){
1538667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1539*a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1540667ec068SMatthias Ringwald     // find index for assigned number
1541667ec068SMatthias Ringwald     int i;
1542667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1543667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1544667ec068SMatthias Ringwald             // set value
1545667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1546667ec068SMatthias Ringwald             // mark for update
1547*a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1548*a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1549667ec068SMatthias Ringwald                 // send update
1550*a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1551*a0ffb263SMatthias Ringwald             }
1552667ec068SMatthias Ringwald             return;
1553667ec068SMatthias Ringwald         }
1554667ec068SMatthias Ringwald     }
1555667ec068SMatthias Ringwald }
1556667ec068SMatthias Ringwald 
1557