xref: /btstack/src/classic/hfp_hf.c (revision 59c6af158dbc40c703cd6422bc90ce91f2efa168)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
373deb3ec6SMatthias Ringwald 
383deb3ec6SMatthias Ringwald // *****************************************************************************
393deb3ec6SMatthias Ringwald //
40fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit
413deb3ec6SMatthias Ringwald //
423deb3ec6SMatthias Ringwald // *****************************************************************************
433deb3ec6SMatthias Ringwald 
447907f069SMatthias Ringwald #include "btstack_config.h"
453deb3ec6SMatthias Ringwald 
463deb3ec6SMatthias Ringwald #include <stdint.h>
473deb3ec6SMatthias Ringwald #include <stdio.h>
483deb3ec6SMatthias Ringwald #include <stdlib.h>
493deb3ec6SMatthias Ringwald #include <string.h>
503deb3ec6SMatthias Ringwald 
51*59c6af15SMatthias Ringwald #include "btstack_debug.h"
523deb3ec6SMatthias Ringwald #include "btstack_memory.h"
53*59c6af15SMatthias Ringwald #include "btstack_run_loop.h"
54*59c6af15SMatthias Ringwald #include "classic/core.h"
55*59c6af15SMatthias Ringwald #include "classic/hfp.h"
56*59c6af15SMatthias Ringwald #include "classic/hfp_hf.h"
57efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
58746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
59023f2764SMatthias Ringwald #include "classic/sdp_util.h"
60*59c6af15SMatthias Ringwald #include "hci.h"
61*59c6af15SMatthias Ringwald #include "hci_cmd.h"
62*59c6af15SMatthias Ringwald #include "hci_dump.h"
63*59c6af15SMatthias Ringwald #include "l2cap.h"
643deb3ec6SMatthias Ringwald 
653deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
663deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
673deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
683deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
693deb3ec6SMatthias Ringwald 
703deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0;
713deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS];
72667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS];
73667ec068SMatthias Ringwald 
74667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9;
75667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9;
763deb3ec6SMatthias Ringwald 
773deb3ec6SMatthias Ringwald static hfp_callback_t hfp_callback;
783deb3ec6SMatthias Ringwald 
79ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
80ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
81ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
82ce263fc8SMatthias Ringwald 
83ce263fc8SMatthias Ringwald static char phone_number[25];
84ce263fc8SMatthias Ringwald 
85c5b64319SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
86c5b64319SMatthias Ringwald 
873deb3ec6SMatthias Ringwald void hfp_hf_register_packet_handler(hfp_callback_t callback){
883deb3ec6SMatthias Ringwald     hfp_callback = callback;
893deb3ec6SMatthias Ringwald     if (callback == NULL){
903deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
913deb3ec6SMatthias Ringwald         return;
923deb3ec6SMatthias Ringwald     }
933deb3ec6SMatthias Ringwald     hfp_callback = callback;
94f4000eebSMatthias Ringwald     hfp_set_callback(callback);
953deb3ec6SMatthias Ringwald }
963deb3ec6SMatthias Ringwald 
97a0ffb263SMatthias 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){
98a0ffb263SMatthias Ringwald     if (!callback) return;
99a0ffb263SMatthias Ringwald     uint8_t event[31];
100a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
101a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
102a0ffb263SMatthias Ringwald     event[2] = event_subtype;
103a0ffb263SMatthias Ringwald     event[3] = status;
104a0ffb263SMatthias Ringwald     event[4] = bnip_type;
105a0ffb263SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 6) ? strlen(bnip_number) : sizeof(event) - 6;
106a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], bnip_number, size);
107a0ffb263SMatthias Ringwald     event[5 + size] = 0;
108a0ffb263SMatthias Ringwald     (*callback)(event, sizeof(event));
109a0ffb263SMatthias Ringwald }
110a0ffb263SMatthias Ringwald 
111a0ffb263SMatthias 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){
112a0ffb263SMatthias Ringwald     if (!callback) return;
113a0ffb263SMatthias Ringwald     uint8_t event[30];
114a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
115a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
116a0ffb263SMatthias Ringwald     event[2] = event_subtype;
117a0ffb263SMatthias Ringwald     event[3] = bnip_type;
118a0ffb263SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 5) ? strlen(bnip_number) : sizeof(event) - 5;
119a0ffb263SMatthias Ringwald     strncpy((char*)&event[4], bnip_number, size);
120a0ffb263SMatthias Ringwald     event[4 + size] = 0;
121a0ffb263SMatthias Ringwald     (*callback)(event, sizeof(event));
122a0ffb263SMatthias Ringwald }
123a0ffb263SMatthias Ringwald 
124a0ffb263SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(hfp_callback_t callback, uint8_t clcc_idx, uint8_t clcc_dir,
125a0ffb263SMatthias Ringwald                 uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){
126a0ffb263SMatthias Ringwald     if (!callback) return;
127a0ffb263SMatthias Ringwald     uint8_t event[35];
128a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
129a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
130a0ffb263SMatthias Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
131a0ffb263SMatthias Ringwald     event[3] = clcc_idx;
132a0ffb263SMatthias Ringwald     event[4] = clcc_dir;
133a0ffb263SMatthias Ringwald     event[6] = clcc_status;
134a0ffb263SMatthias Ringwald     event[7] = clcc_mpty;
135a0ffb263SMatthias Ringwald     event[8] = bnip_type;
136a0ffb263SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 10) ? strlen(bnip_number) : sizeof(event) - 10;
137a0ffb263SMatthias Ringwald     strncpy((char*)&event[9], bnip_number, size);
138a0ffb263SMatthias Ringwald     event[9 + size] = 0;
139a0ffb263SMatthias Ringwald     (*callback)(event, sizeof(event));
140a0ffb263SMatthias Ringwald }
141a0ffb263SMatthias Ringwald 
1423deb3ec6SMatthias Ringwald static int hfp_hf_supports_codec(uint8_t codec){
1433deb3ec6SMatthias Ringwald     int i;
1443deb3ec6SMatthias Ringwald     for (i = 0; i < hfp_codecs_nr; i++){
1453deb3ec6SMatthias Ringwald         if (hfp_codecs[i] == codec) return 1;
1463deb3ec6SMatthias Ringwald     }
147ce263fc8SMatthias Ringwald     return HFP_CODEC_CVSD;
1483deb3ec6SMatthias Ringwald }
149a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
1503deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
151a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1523deb3ec6SMatthias Ringwald     return hf && ag;
1533deb3ec6SMatthias Ringwald }
1543deb3ec6SMatthias Ringwald 
155a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
1563deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
157a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1583deb3ec6SMatthias Ringwald     return hf && ag;
1593deb3ec6SMatthias Ringwald }
1603deb3ec6SMatthias Ringwald 
1613deb3ec6SMatthias Ringwald 
162a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
1633deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
164a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
1653deb3ec6SMatthias Ringwald     return hf && ag;
1663deb3ec6SMatthias Ringwald }
1673deb3ec6SMatthias Ringwald 
168ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
1693deb3ec6SMatthias Ringwald 
1702ef54b27SMatthias 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){
1713deb3ec6SMatthias Ringwald     if (!name){
1723deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1733deb3ec6SMatthias Ringwald     }
1742ef54b27SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, SDP_Handsfree, rfcomm_channel_nr, name);
1753deb3ec6SMatthias Ringwald 
176aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
177aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, supported_features);
178aa4dd815SMatthias Ringwald }
1793deb3ec6SMatthias Ringwald 
1803deb3ec6SMatthias Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
1813deb3ec6SMatthias Ringwald     char buffer[20];
1823deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SUPPORTED_FEATURES, hfp_supported_features);
1833deb3ec6SMatthias Ringwald     // printf("exchange_supported_features %s\n", buffer);
1843deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1853deb3ec6SMatthias Ringwald }
1863deb3ec6SMatthias Ringwald 
1873deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
1883deb3ec6SMatthias Ringwald     char buffer[30];
1893deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_AVAILABLE_CODECS);
1903deb3ec6SMatthias Ringwald     offset += join(buffer+offset, sizeof(buffer)-offset, hfp_codecs, hfp_codecs_nr);
1913deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
1923deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1933deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
1943deb3ec6SMatthias Ringwald }
1953deb3ec6SMatthias Ringwald 
1963deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
1973deb3ec6SMatthias Ringwald     char buffer[20];
1983deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_INDICATOR);
1993deb3ec6SMatthias Ringwald     // printf("retrieve_indicators %s\n", buffer);
2003deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2013deb3ec6SMatthias Ringwald }
2023deb3ec6SMatthias Ringwald 
2033deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
2043deb3ec6SMatthias Ringwald     char buffer[20];
2053deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_INDICATOR);
2063deb3ec6SMatthias Ringwald     // printf("retrieve_indicators_status %s\n", buffer);
2073deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2083deb3ec6SMatthias Ringwald }
2093deb3ec6SMatthias Ringwald 
2103deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2113deb3ec6SMatthias Ringwald     char buffer[20];
2123deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
2133deb3ec6SMatthias Ringwald     // printf("toggle_indicator_status_update %s\n", buffer);
2143deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2153deb3ec6SMatthias Ringwald }
2163deb3ec6SMatthias Ringwald 
2173deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2183deb3ec6SMatthias Ringwald     char buffer[50];
2193deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
2203deb3ec6SMatthias Ringwald     offset += join_bitmap(buffer+offset, sizeof(buffer)-offset, indicators_status, indicators_nr);
2213deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
2223deb3ec6SMatthias Ringwald     buffer[offset] = 0;
2233deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2243deb3ec6SMatthias Ringwald }
2253deb3ec6SMatthias Ringwald 
2263deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
2273deb3ec6SMatthias Ringwald     char buffer[20];
2283deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES);
2293deb3ec6SMatthias Ringwald     // printf("retrieve_can_hold_call %s\n", buffer);
2303deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2313deb3ec6SMatthias Ringwald }
2323deb3ec6SMatthias Ringwald 
2333deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2343deb3ec6SMatthias Ringwald     char buffer[30];
2353deb3ec6SMatthias Ringwald     int offset = snprintf(buffer, sizeof(buffer), "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
2363deb3ec6SMatthias Ringwald     offset += join(buffer+offset, sizeof(buffer)-offset, hfp_indicators, hfp_indicators_nr);
2373deb3ec6SMatthias Ringwald     offset += snprintf(buffer+offset, sizeof(buffer)-offset, "\r\n");
2383deb3ec6SMatthias Ringwald     buffer[offset] = 0;
2393deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2403deb3ec6SMatthias Ringwald }
2413deb3ec6SMatthias Ringwald 
2423deb3ec6SMatthias Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
2433deb3ec6SMatthias Ringwald     char buffer[20];
2443deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=?\r\n", HFP_GENERIC_STATUS_INDICATOR);
2453deb3ec6SMatthias Ringwald     // printf("retrieve_supported_generic_status_indicators %s\n", buffer);
2463deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2473deb3ec6SMatthias Ringwald }
2483deb3ec6SMatthias Ringwald 
2493deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
2503deb3ec6SMatthias Ringwald     char buffer[20];
2513deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_GENERIC_STATUS_INDICATOR);
2523deb3ec6SMatthias Ringwald     // printf("list_initital_supported_generic_status_indicators %s\n", buffer);
2533deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2543deb3ec6SMatthias Ringwald }
2553deb3ec6SMatthias Ringwald 
2563deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
2573deb3ec6SMatthias Ringwald     char buffer[20];
2583deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=3,0\r\n", HFP_QUERY_OPERATOR_SELECTION);
2593deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2603deb3ec6SMatthias Ringwald }
2613deb3ec6SMatthias Ringwald 
2623deb3ec6SMatthias Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
2633deb3ec6SMatthias Ringwald     char buffer[20];
2643deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s?\r\n", HFP_QUERY_OPERATOR_SELECTION);
2653deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2663deb3ec6SMatthias Ringwald }
2673deb3ec6SMatthias Ringwald 
2683deb3ec6SMatthias Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
2693deb3ec6SMatthias Ringwald     char buffer[20];
2703deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
2713deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2723deb3ec6SMatthias Ringwald }
2733deb3ec6SMatthias Ringwald 
2743deb3ec6SMatthias Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
2753deb3ec6SMatthias Ringwald     char buffer[20];
2763deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_TRIGGER_CODEC_CONNECTION_SETUP);
2773deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2783deb3ec6SMatthias Ringwald }
2793deb3ec6SMatthias Ringwald 
2803deb3ec6SMatthias Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
2813deb3ec6SMatthias Ringwald     char buffer[20];
2823deb3ec6SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_CONFIRM_COMMON_CODEC, codec);
2833deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2843deb3ec6SMatthias Ringwald }
2853deb3ec6SMatthias Ringwald 
286ce263fc8SMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
287ce263fc8SMatthias Ringwald     char buffer[10];
288ce263fc8SMatthias Ringwald     sprintf(buffer, "%s\r\n", HFP_CALL_ANSWERED);
289ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
290ce263fc8SMatthias Ringwald }
291ce263fc8SMatthias Ringwald 
292ce263fc8SMatthias Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
293ce263fc8SMatthias Ringwald     char buffer[40];
294ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SET_MICROPHONE_GAIN, gain);
295ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
296ce263fc8SMatthias Ringwald }
297ce263fc8SMatthias Ringwald 
298ce263fc8SMatthias Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
299ce263fc8SMatthias Ringwald     char buffer[40];
300ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_SET_SPEAKER_GAIN, gain);
301ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
302ce263fc8SMatthias Ringwald }
303ce263fc8SMatthias Ringwald 
304ce263fc8SMatthias Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
305ce263fc8SMatthias Ringwald     char buffer[40];
306ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CLIP, activate);
307ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
308ce263fc8SMatthias Ringwald }
309ce263fc8SMatthias Ringwald 
310ce263fc8SMatthias Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
311ce263fc8SMatthias Ringwald     char buffer[40];
312ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_TURN_OFF_EC_AND_NR, activate);
313ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
314ce263fc8SMatthias Ringwald }
315ce263fc8SMatthias Ringwald 
316ce263fc8SMatthias Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
317ce263fc8SMatthias Ringwald     char buffer[40];
318ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ACTIVATE_VOICE_RECOGNITION, activate);
319ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
320ce263fc8SMatthias Ringwald }
321ce263fc8SMatthias Ringwald 
322ce263fc8SMatthias Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
323ce263fc8SMatthias Ringwald     char buffer[40];
324ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%d\r\n", HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
325ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
326ce263fc8SMatthias Ringwald }
327ce263fc8SMatthias Ringwald 
328ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
329ce263fc8SMatthias Ringwald     char buffer[40];
330ce263fc8SMatthias Ringwald     sprintf(buffer, "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
331ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
332ce263fc8SMatthias Ringwald }
333ce263fc8SMatthias Ringwald 
334a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
335ce263fc8SMatthias Ringwald     char buffer[40];
336a0ffb263SMatthias Ringwald     sprintf(buffer, "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id);
337ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
338ce263fc8SMatthias Ringwald }
339ce263fc8SMatthias Ringwald 
340ce263fc8SMatthias Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
341ce263fc8SMatthias Ringwald     char buffer[20];
342ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_REDIAL_LAST_NUMBER);
343ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
344ce263fc8SMatthias Ringwald }
345ce263fc8SMatthias Ringwald 
346ce263fc8SMatthias Ringwald static int hfp_hf_send_chup(uint16_t cid){
347ce263fc8SMatthias Ringwald     char buffer[20];
348ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_HANG_UP_CALL);
349ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
350ce263fc8SMatthias Ringwald }
351ce263fc8SMatthias Ringwald 
352ce263fc8SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, int number){
353ce263fc8SMatthias Ringwald     char buffer[20];
354ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
355ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
356ce263fc8SMatthias Ringwald }
357ce263fc8SMatthias Ringwald 
358ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
359ce263fc8SMatthias Ringwald     char buffer[20];
360ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
361ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
362ce263fc8SMatthias Ringwald }
363ce263fc8SMatthias Ringwald 
364ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
365ce263fc8SMatthias Ringwald     char buffer[20];
366ce263fc8SMatthias Ringwald     sprintf(buffer, "AT%s=1\r\n", HFP_PHONE_NUMBER_FOR_VOICE_TAG);
367ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
368ce263fc8SMatthias Ringwald }
369ce263fc8SMatthias Ringwald 
370667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
371667ec068SMatthias Ringwald     char buffer[20];
372667ec068SMatthias Ringwald     sprintf(buffer, "AT%s\r\n", HFP_LIST_CURRENT_CALLS);
373667ec068SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
374667ec068SMatthias Ringwald }
375667ec068SMatthias Ringwald 
376a0ffb263SMatthias Ringwald static void hfp_emit_ag_indicator_event(hfp_callback_t callback, hfp_ag_indicator_t indicator){
3773deb3ec6SMatthias Ringwald     if (!callback) return;
378a0ffb263SMatthias Ringwald     uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1];
3793deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3803deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3813deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
382a0ffb263SMatthias Ringwald     event[3] = indicator.index;
383a0ffb263SMatthias Ringwald     event[4] = indicator.status;
384a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
385a0ffb263SMatthias Ringwald     event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0;
3863deb3ec6SMatthias Ringwald     (*callback)(event, sizeof(event));
3873deb3ec6SMatthias Ringwald }
3883deb3ec6SMatthias Ringwald 
389a0ffb263SMatthias Ringwald static void hfp_emit_network_operator_event(hfp_callback_t callback, hfp_network_opearator_t network_operator){
3903deb3ec6SMatthias Ringwald     if (!callback) return;
3913deb3ec6SMatthias Ringwald     uint8_t event[24];
3923deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3933deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3943deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
395a0ffb263SMatthias Ringwald     event[3] = network_operator.mode;
396a0ffb263SMatthias Ringwald     event[4] = network_operator.format;
397a0ffb263SMatthias Ringwald     strcpy((char*)&event[5], network_operator.name);
3983deb3ec6SMatthias Ringwald     (*callback)(event, sizeof(event));
3993deb3ec6SMatthias Ringwald }
4003deb3ec6SMatthias Ringwald 
401a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
402a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
403a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
404aa4dd815SMatthias Ringwald     int done = 1;
4053deb3ec6SMatthias Ringwald 
406a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
4073deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
408a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
409a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
4103deb3ec6SMatthias Ringwald             break;
4113deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
412a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
413a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
4143deb3ec6SMatthias Ringwald             break;
4153deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
416a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
417a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4183deb3ec6SMatthias Ringwald             break;
4193deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
420a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
421a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4223deb3ec6SMatthias Ringwald             break;
4233deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
424a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
425a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4263deb3ec6SMatthias Ringwald             break;
4273deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
428a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
429a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4303deb3ec6SMatthias Ringwald             break;
4313deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
432a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
433a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4343deb3ec6SMatthias Ringwald             break;
4353deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
436a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
437a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4383deb3ec6SMatthias Ringwald             break;
4393deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
440a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
441a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4423deb3ec6SMatthias Ringwald             break;
4433deb3ec6SMatthias Ringwald         default:
444aa4dd815SMatthias Ringwald             done = 0;
4453deb3ec6SMatthias Ringwald             break;
4463deb3ec6SMatthias Ringwald     }
4473deb3ec6SMatthias Ringwald     return done;
4483deb3ec6SMatthias Ringwald }
4493deb3ec6SMatthias Ringwald 
450ce263fc8SMatthias Ringwald 
451a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
452a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
453a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
454ce263fc8SMatthias Ringwald 
455ce263fc8SMatthias Ringwald     int done = 0;
456a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
457a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
458ce263fc8SMatthias Ringwald         done = 1;
459a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
460ce263fc8SMatthias Ringwald         return done;
461ce263fc8SMatthias Ringwald     };
462a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
463a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
464ce263fc8SMatthias Ringwald         done = 1;
465a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
466a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
467a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
468ce263fc8SMatthias Ringwald         return done;
469ce263fc8SMatthias Ringwald     }
470ce263fc8SMatthias Ringwald 
471a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
472ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
473a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
474a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
475a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
476ce263fc8SMatthias Ringwald             return 1;
477ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
478a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
479a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
480a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
481ce263fc8SMatthias Ringwald             return 1;
482ce263fc8SMatthias Ringwald         default:
483ce263fc8SMatthias Ringwald             break;
484ce263fc8SMatthias Ringwald     }
485ce263fc8SMatthias Ringwald 
486a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
487a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
488ce263fc8SMatthias Ringwald         done = 1;
489a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
490ce263fc8SMatthias Ringwald         return done;
491ce263fc8SMatthias Ringwald     }
492ce263fc8SMatthias Ringwald 
493ce263fc8SMatthias Ringwald     return done;
494ce263fc8SMatthias Ringwald }
495ce263fc8SMatthias Ringwald 
496a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
497ce263fc8SMatthias Ringwald     /* events ( == commands):
498ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
499ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
500ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
501ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
502ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
503ce263fc8SMatthias Ringwald     */
504ce263fc8SMatthias Ringwald 
505a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
506ce263fc8SMatthias Ringwald 
507a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
508ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
509a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
510ce263fc8SMatthias Ringwald 
511a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
512a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
513a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
514ce263fc8SMatthias Ringwald             return 1;
515ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
516a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = 0;
517a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = 0;
518a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = 0;
519ce263fc8SMatthias Ringwald 
520a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
521a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
522a0ffb263SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
523ce263fc8SMatthias Ringwald             break;
524ce263fc8SMatthias Ringwald 
525ce263fc8SMatthias Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:
526a0ffb263SMatthias Ringwald             if (hfp_hf_supports_codec(hfp_connection->suggested_codec)){
527a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
528a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
529a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
530a0ffb263SMatthias Ringwald                 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->suggested_codec);
531ce263fc8SMatthias Ringwald             } else {
532a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
533a0ffb263SMatthias Ringwald                 hfp_connection->suggested_codec = 0;
534a0ffb263SMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
535a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
536a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
537a0ffb263SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
538ce263fc8SMatthias Ringwald 
539ce263fc8SMatthias Ringwald             }
540ce263fc8SMatthias Ringwald             break;
541ce263fc8SMatthias Ringwald 
542ce263fc8SMatthias Ringwald         default:
543ce263fc8SMatthias Ringwald             break;
544ce263fc8SMatthias Ringwald     }
545ce263fc8SMatthias Ringwald     return 0;
546ce263fc8SMatthias Ringwald }
547ce263fc8SMatthias Ringwald 
548a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
549a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
550a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
551ce263fc8SMatthias Ringwald 
552ce263fc8SMatthias Ringwald 
553a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
554a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
555a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
556a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
557ce263fc8SMatthias Ringwald         return 1;
558ce263fc8SMatthias Ringwald     }
559ce263fc8SMatthias Ringwald 
560a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
561ce263fc8SMatthias Ringwald 
562ce263fc8SMatthias Ringwald     // run codecs exchange
563a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
564ce263fc8SMatthias Ringwald     if (done) return 1;
565ce263fc8SMatthias Ringwald 
566a0ffb263SMatthias Ringwald     if (hfp_connection->establish_audio_connection){
567a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_CONNECTED;
568a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 0;
569a0ffb263SMatthias Ringwald         hfp_setup_synchronous_connection(hfp_connection->acl_handle, hfp_connection->link_setting);
570ce263fc8SMatthias Ringwald         return 1;
571ce263fc8SMatthias Ringwald     }
572ce263fc8SMatthias Ringwald 
573ce263fc8SMatthias Ringwald     return 0;
574ce263fc8SMatthias Ringwald }
575ce263fc8SMatthias Ringwald 
576a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
577a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
578a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
579a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
580ce263fc8SMatthias Ringwald         return 1;
581ce263fc8SMatthias Ringwald     }
582ce263fc8SMatthias Ringwald     return 0;
583ce263fc8SMatthias Ringwald }
584ce263fc8SMatthias Ringwald 
585a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){
586a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
587a0ffb263SMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) return;
588ce263fc8SMatthias Ringwald 
589a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
590ce263fc8SMatthias Ringwald     if (!done){
591a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
592ce263fc8SMatthias Ringwald     }
593ce263fc8SMatthias Ringwald     if (!done){
594a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
595ce263fc8SMatthias Ringwald     }
596ce263fc8SMatthias Ringwald     if (!done){
597a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
598ce263fc8SMatthias Ringwald     }
599ce263fc8SMatthias Ringwald 
600a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
601a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
602a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
603a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
604ce263fc8SMatthias Ringwald         return;
605ce263fc8SMatthias Ringwald     }
606ce263fc8SMatthias Ringwald 
607a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
608a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
609a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
610a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
611ce263fc8SMatthias Ringwald         return;
612ce263fc8SMatthias Ringwald     }
613ce263fc8SMatthias Ringwald 
614a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
615a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
616a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
617a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
618ce263fc8SMatthias Ringwald         return;
619ce263fc8SMatthias Ringwald     }
620ce263fc8SMatthias Ringwald 
621a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
622a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
623a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
624a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
625ce263fc8SMatthias Ringwald         return;
626ce263fc8SMatthias Ringwald     }
627ce263fc8SMatthias Ringwald 
628a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
629a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
630a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
631a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
632ce263fc8SMatthias Ringwald         return;
633ce263fc8SMatthias Ringwald     }
634ce263fc8SMatthias Ringwald 
635a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
636a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
637a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
638a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
639ce263fc8SMatthias Ringwald         return;
640ce263fc8SMatthias Ringwald     }
641ce263fc8SMatthias Ringwald 
642a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
643a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
644a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
645a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
646ce263fc8SMatthias Ringwald         return;
647ce263fc8SMatthias Ringwald     }
648ce263fc8SMatthias Ringwald 
649a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
650a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
651a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
652a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
653ce263fc8SMatthias Ringwald         return;
654ce263fc8SMatthias Ringwald     }
655ce263fc8SMatthias Ringwald 
656ce263fc8SMatthias Ringwald 
657a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
658a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
659a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
660a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
661ce263fc8SMatthias Ringwald         return;
662ce263fc8SMatthias Ringwald     }
663ce263fc8SMatthias Ringwald 
664a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
665a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
666a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
667a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
668ce263fc8SMatthias Ringwald         return;
669ce263fc8SMatthias Ringwald     }
670ce263fc8SMatthias Ringwald 
671a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
672a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
673a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
674a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
675ce263fc8SMatthias Ringwald         return;
676ce263fc8SMatthias Ringwald     }
677ce263fc8SMatthias Ringwald 
678a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
679a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
680a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
681a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
682ce263fc8SMatthias Ringwald         return;
683ce263fc8SMatthias Ringwald     }
684ce263fc8SMatthias Ringwald 
685a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
686a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
687a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
688a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
689ce263fc8SMatthias Ringwald         return;
690ce263fc8SMatthias Ringwald     }
691ce263fc8SMatthias Ringwald 
692a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
693a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
694a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
695a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
696ce263fc8SMatthias Ringwald         return;
697ce263fc8SMatthias Ringwald     }
698ce263fc8SMatthias Ringwald 
699a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
700a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
701a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
702a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
703ce263fc8SMatthias Ringwald         return;
704ce263fc8SMatthias Ringwald     }
705ce263fc8SMatthias Ringwald 
706a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
707a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
708a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
709a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
710ce263fc8SMatthias Ringwald         return;
711ce263fc8SMatthias Ringwald     }
712ce263fc8SMatthias Ringwald 
713a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
714a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
715a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
716a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
717ce263fc8SMatthias Ringwald         return;
718ce263fc8SMatthias Ringwald     }
719ce263fc8SMatthias Ringwald 
720a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
721a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
722a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
723a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
724ce263fc8SMatthias Ringwald         return;
725ce263fc8SMatthias Ringwald     }
726ce263fc8SMatthias Ringwald 
727a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
728a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
729a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
730a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
731ce263fc8SMatthias Ringwald         return;
732ce263fc8SMatthias Ringwald     }
733ce263fc8SMatthias Ringwald 
734a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
735a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
736a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
737a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
738667ec068SMatthias Ringwald         return;
739667ec068SMatthias Ringwald     }
740667ec068SMatthias Ringwald 
741a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
742a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
743a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
744a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
745a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
746ce263fc8SMatthias Ringwald         return;
747ce263fc8SMatthias Ringwald     }
748ce263fc8SMatthias Ringwald 
749a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
750a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
751a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
752a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
753ce263fc8SMatthias Ringwald         return;
754ce263fc8SMatthias Ringwald     }
755ce263fc8SMatthias Ringwald 
756a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
757a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
758a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
759a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
760667ec068SMatthias Ringwald         return;
761667ec068SMatthias Ringwald     }
762667ec068SMatthias Ringwald 
763a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
764a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
765667ec068SMatthias Ringwald         char buffer[20];
766a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
767667ec068SMatthias Ringwald             case '?':
768667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
769a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
770667ec068SMatthias Ringwald                 return;
771667ec068SMatthias Ringwald             case '0':
772667ec068SMatthias Ringwald             case '1':
773667ec068SMatthias Ringwald             case '2':
774a0ffb263SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
775a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
776667ec068SMatthias Ringwald                 return;
777667ec068SMatthias Ringwald             default:
778667ec068SMatthias Ringwald                 break;
779667ec068SMatthias Ringwald         }
780667ec068SMatthias Ringwald         return;
781667ec068SMatthias Ringwald     }
782667ec068SMatthias Ringwald 
783a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
784a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
785667ec068SMatthias Ringwald         char buffer[20];
786667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
787a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
788667ec068SMatthias Ringwald         return;
789667ec068SMatthias Ringwald     }
790667ec068SMatthias Ringwald 
791667ec068SMatthias Ringwald     // update HF indicators
792a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
793667ec068SMatthias Ringwald         int i;
794667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
795a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
796a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
797a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
798a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
799667ec068SMatthias Ringwald                     char buffer[30];
800667ec068SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], hfp_indicators_value[i]);
801a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
802667ec068SMatthias Ringwald                 } else {
803667ec068SMatthias Ringwald                     printf("Not sending HF indicator %u as it is disabled\n", hfp_indicators[i]);
804667ec068SMatthias Ringwald                 }
805667ec068SMatthias Ringwald                 return;
806667ec068SMatthias Ringwald             }
807667ec068SMatthias Ringwald         }
808667ec068SMatthias Ringwald     }
809667ec068SMatthias Ringwald 
810ce263fc8SMatthias Ringwald     if (done) return;
811ce263fc8SMatthias Ringwald     // deal with disconnect
812a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
813ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
814a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
815a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
816ce263fc8SMatthias Ringwald             break;
817ce263fc8SMatthias Ringwald 
818ce263fc8SMatthias Ringwald         default:
819ce263fc8SMatthias Ringwald             break;
820ce263fc8SMatthias Ringwald     }
821ce263fc8SMatthias Ringwald }
822ce263fc8SMatthias Ringwald 
823a0ffb263SMatthias Ringwald static void hfp_init_link_settings(hfp_connection_t * hfp_connection){
824ce263fc8SMatthias Ringwald     // determine highest possible link setting
825a0ffb263SMatthias Ringwald     hfp_connection->link_setting = HFP_LINK_SETTINGS_D1;
826a0ffb263SMatthias Ringwald     if (hci_remote_esco_supported(hfp_connection->acl_handle)){
827a0ffb263SMatthias Ringwald         hfp_connection->link_setting = HFP_LINK_SETTINGS_S3;
828ce263fc8SMatthias Ringwald         if ((hfp_supported_features             & (1<<HFP_HFSF_ESCO_S4))
829a0ffb263SMatthias Ringwald         &&  (hfp_connection->remote_supported_features & (1<<HFP_AGSF_ESCO_S4))){
830a0ffb263SMatthias Ringwald             hfp_connection->link_setting = HFP_LINK_SETTINGS_S4;
831ce263fc8SMatthias Ringwald         }
832ce263fc8SMatthias Ringwald     }
833ce263fc8SMatthias Ringwald }
834ce263fc8SMatthias Ringwald 
835a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
836a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
837ce263fc8SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SERVICE_LEVEL_CONNECTION_ESTABLISHED, 0);
838a0ffb263SMatthias Ringwald     hfp_init_link_settings(hfp_connection);
839667ec068SMatthias Ringwald     // restore volume settings
840a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
841a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
842667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
843a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
844a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
845667ec068SMatthias Ringwald     hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
846667ec068SMatthias Ringwald     // enable all indicators
847667ec068SMatthias Ringwald     int i;
848667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
849a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
850a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
851667ec068SMatthias Ringwald     }
852ce263fc8SMatthias Ringwald }
853ce263fc8SMatthias Ringwald 
854a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
855a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
856a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
8573deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
858a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
859a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
8603deb3ec6SMatthias Ringwald                 break;
8613deb3ec6SMatthias Ringwald             }
862a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8633deb3ec6SMatthias Ringwald             break;
8643deb3ec6SMatthias Ringwald 
8653deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
866a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8673deb3ec6SMatthias Ringwald             break;
8683deb3ec6SMatthias Ringwald 
8693deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
870a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
8713deb3ec6SMatthias Ringwald             break;
8723deb3ec6SMatthias Ringwald 
8733deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
874a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
8753deb3ec6SMatthias Ringwald             break;
8763deb3ec6SMatthias Ringwald 
8773deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
878a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
879a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
8803deb3ec6SMatthias Ringwald                 break;
8813deb3ec6SMatthias Ringwald             }
882a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
883a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8843deb3ec6SMatthias Ringwald                 break;
8853deb3ec6SMatthias Ringwald             }
886a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8873deb3ec6SMatthias Ringwald             break;
8883deb3ec6SMatthias Ringwald 
8893deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
890a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
891a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
8923deb3ec6SMatthias Ringwald                 break;
8933deb3ec6SMatthias Ringwald             }
894a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
8953deb3ec6SMatthias Ringwald             break;
8963deb3ec6SMatthias Ringwald 
8973deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
898a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
8993deb3ec6SMatthias Ringwald             break;
9003deb3ec6SMatthias Ringwald 
9013deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
902a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9033deb3ec6SMatthias Ringwald             break;
9043deb3ec6SMatthias Ringwald 
9053deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
906a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9073deb3ec6SMatthias Ringwald             break;
908ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
909a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
910a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
9113deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
912ce263fc8SMatthias Ringwald                 break;
913ce263fc8SMatthias Ringwald             }
9143deb3ec6SMatthias Ringwald 
915a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
916a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
9173deb3ec6SMatthias Ringwald                 hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 0);
918ce263fc8SMatthias Ringwald                 break;
9193deb3ec6SMatthias Ringwald             }
9203deb3ec6SMatthias Ringwald 
921a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
922ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
923ce263fc8SMatthias Ringwald                     printf("Format set, querying name\n");
924a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
925ce263fc8SMatthias Ringwald                     break;
926ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
927a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
928a0ffb263SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_callback, hfp_connection->network_operator);
929ce263fc8SMatthias Ringwald                     break;
930ce263fc8SMatthias Ringwald                 default:
931ce263fc8SMatthias Ringwald                     break;
9323deb3ec6SMatthias Ringwald             }
933ce263fc8SMatthias Ringwald 
934a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
935a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
936ce263fc8SMatthias Ringwald                 break;
9373deb3ec6SMatthias Ringwald             }
9383deb3ec6SMatthias Ringwald 
939a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
940aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
941a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9423deb3ec6SMatthias Ringwald                     break;
943ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
944a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
945ce263fc8SMatthias Ringwald                     hfp_emit_event(hfp_callback, HFP_SUBEVENT_CODECS_CONNECTION_COMPLETE, 0);
946ce263fc8SMatthias Ringwald                     break;
9473deb3ec6SMatthias Ringwald                 default:
9483deb3ec6SMatthias Ringwald                     break;
9493deb3ec6SMatthias Ringwald             }
9503deb3ec6SMatthias Ringwald             break;
9513deb3ec6SMatthias Ringwald         default:
9523deb3ec6SMatthias Ringwald             break;
9533deb3ec6SMatthias Ringwald     }
9543deb3ec6SMatthias Ringwald 
9553deb3ec6SMatthias Ringwald     // done
956a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
9573deb3ec6SMatthias Ringwald }
9583deb3ec6SMatthias Ringwald 
9593deb3ec6SMatthias Ringwald 
9603deb3ec6SMatthias Ringwald static void hfp_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
961a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
962a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
9633deb3ec6SMatthias Ringwald 
9641e35c04dSMatthias Ringwald     char last_char = packet[size-1];
9651e35c04dSMatthias Ringwald     packet[size-1] = 0;
9661e35c04dSMatthias Ringwald     log_info("HFP_RX %s", packet);
9671e35c04dSMatthias Ringwald     packet[size-1] = last_char;
9681e35c04dSMatthias Ringwald 
969667ec068SMatthias Ringwald     int pos, i, value;
9703deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
971a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
972ce263fc8SMatthias Ringwald     }
9733deb3ec6SMatthias Ringwald 
974a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
975667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
976a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
977a0ffb263SMatthias Ringwald             // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type);
978a0ffb263SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
979667ec068SMatthias Ringwald             break;
980667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
981a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
982a0ffb263SMatthias Ringwald             // printf("Response and Hold status: %s\n", hfp_connection->line_buffer);
983a0ffb263SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, atoi((char *)&hfp_connection->line_buffer[0]));
984667ec068SMatthias Ringwald             break;
985667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
986a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
987a0ffb263SMatthias Ringwald             // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
988a0ffb263SMatthias Ringwald             //      hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
989a0ffb263SMatthias Ringwald             //      hfp_connection->bnip_number, hfp_connection->bnip_type);
990a0ffb263SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_callback, hfp_connection->clcc_idx,
991a0ffb263SMatthias Ringwald                 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
992a0ffb263SMatthias Ringwald                 hfp_connection->bnip_type, hfp_connection->bnip_number);
993667ec068SMatthias Ringwald             break;
994ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
995a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
996a0ffb263SMatthias Ringwald             value = atoi((char*)hfp_connection->line_buffer);
997667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
998667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_SPEAKER_VOLUME, value);
999ce263fc8SMatthias Ringwald             break;
1000ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1001a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1002a0ffb263SMatthias Ringwald             value = atoi((char*)hfp_connection->line_buffer);
1003667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1004667ec068SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1005ce263fc8SMatthias Ringwald             break;
1006ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1007a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1008a0ffb263SMatthias Ringwald             hfp_emit_string_event(hfp_callback, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1009a0ffb263SMatthias Ringwald             break;
1010a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1011a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1012a0ffb263SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1013a0ffb263SMatthias Ringwald             break;
1014a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1015a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1016a0ffb263SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_callback, HFP_SUBEVENT_CALLING_LINE_INDETIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1017ce263fc8SMatthias Ringwald             break;
1018ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1019a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1020a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1021a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1022a0ffb263SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1023ce263fc8SMatthias Ringwald             break;
1024ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1025a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1026a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1027a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1028ce263fc8SMatthias Ringwald             hfp_emit_event(hfp_callback, HFP_SUBEVENT_COMPLETE, 1);
1029ce263fc8SMatthias Ringwald             break;
1030ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1031a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1032ce263fc8SMatthias Ringwald             break;
1033ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
1034a0ffb263SMatthias Ringwald             hfp_emit_simple_event(hfp_callback, HFP_SUBEVENT_RING);
1035ce263fc8SMatthias Ringwald             break;
1036ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1037a0ffb263SMatthias Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1038a0ffb263SMatthias Ringwald                 if (hfp_connection->ag_indicators[i].status_changed) {
1039a0ffb263SMatthias Ringwald                     if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1040a0ffb263SMatthias Ringwald                         hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1041a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1042a0ffb263SMatthias Ringwald                         hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1043a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1044a0ffb263SMatthias Ringwald                         hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1045ce263fc8SMatthias Ringwald                     }
1046a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[i].status_changed = 0;
1047a0ffb263SMatthias Ringwald                     hfp_emit_ag_indicator_event(hfp_callback, hfp_connection->ag_indicators[i]);
10483deb3ec6SMatthias Ringwald                     break;
10493deb3ec6SMatthias Ringwald                 }
10503deb3ec6SMatthias Ringwald             }
1051ce263fc8SMatthias Ringwald             break;
1052ce263fc8SMatthias Ringwald         default:
1053ce263fc8SMatthias Ringwald             break;
10543deb3ec6SMatthias Ringwald     }
1055a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
10563deb3ec6SMatthias Ringwald }
10573deb3ec6SMatthias Ringwald 
10580cb5b971SMatthias Ringwald static void hfp_run(void){
1059665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1060665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1061665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1062a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1063a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
10643deb3ec6SMatthias Ringwald     }
10653deb3ec6SMatthias Ringwald }
10663deb3ec6SMatthias Ringwald 
1067ffbf8201SMatthias Ringwald static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10683deb3ec6SMatthias Ringwald     switch (packet_type){
10693deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
10703deb3ec6SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size);
10713deb3ec6SMatthias Ringwald             break;
10723deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1073f4000eebSMatthias Ringwald             hfp_handle_hci_event(packet_type, packet, size);
10743deb3ec6SMatthias Ringwald         default:
10753deb3ec6SMatthias Ringwald             break;
10763deb3ec6SMatthias Ringwald     }
10773deb3ec6SMatthias Ringwald     hfp_run();
10783deb3ec6SMatthias Ringwald }
10793deb3ec6SMatthias Ringwald 
1080a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1081d63c37a1SMatthias Ringwald     // register for HCI events
1082d63c37a1SMatthias Ringwald     hci_event_callback_registration.callback = &packet_handler;
1083d63c37a1SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
1084d63c37a1SMatthias Ringwald 
1085a0ffb263SMatthias Ringwald     l2cap_init();
1086d63c37a1SMatthias Ringwald 
1087d63c37a1SMatthias Ringwald     rfcomm_register_service(packet_handler, rfcomm_channel_nr, 0xffff);
1088a0ffb263SMatthias Ringwald 
1089a0ffb263SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1090a0ffb263SMatthias Ringwald     hfp_codecs_nr = 0;
1091a0ffb263SMatthias Ringwald     hfp_indicators_nr = 0;
1092a0ffb263SMatthias Ringwald     hfp_hf_speaker_gain = 9;
1093a0ffb263SMatthias Ringwald     hfp_hf_microphone_gain = 9;
1094a0ffb263SMatthias Ringwald }
1095a0ffb263SMatthias Ringwald 
1096a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
10973deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1098a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
10993deb3ec6SMatthias Ringwald         return;
11003deb3ec6SMatthias Ringwald     }
11013deb3ec6SMatthias Ringwald 
11023deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
11033deb3ec6SMatthias Ringwald     int i;
11043deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
11053deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
11063deb3ec6SMatthias Ringwald     }
11073deb3ec6SMatthias Ringwald 
11083deb3ec6SMatthias Ringwald     char buffer[30];
11093deb3ec6SMatthias Ringwald     int offset = join(buffer, sizeof(buffer), hfp_codecs, hfp_codecs_nr);
11103deb3ec6SMatthias Ringwald     buffer[offset] = 0;
1111665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1112665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1113665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1114a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
1115a0ffb263SMatthias Ringwald         if (! hfp_connection) continue;
1116a0ffb263SMatthias Ringwald         hfp_connection->command = HFP_CMD_AVAILABLE_CODECS;
1117a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
11183deb3ec6SMatthias Ringwald     }
11193deb3ec6SMatthias Ringwald }
11203deb3ec6SMatthias Ringwald 
1121a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
11223deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1123a0ffb263SMatthias Ringwald }
11243deb3ec6SMatthias Ringwald 
1125a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
11263deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
11273deb3ec6SMatthias Ringwald     int i;
1128a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
11293deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
11303deb3ec6SMatthias Ringwald     }
11313deb3ec6SMatthias Ringwald }
11323deb3ec6SMatthias Ringwald 
11333deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
11343deb3ec6SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, SDP_HandsfreeAudioGateway);
11353deb3ec6SMatthias Ringwald }
11363deb3ec6SMatthias Ringwald 
11373deb3ec6SMatthias Ringwald void hfp_hf_release_service_level_connection(bd_addr_t bd_addr){
1138a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1139a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
1140a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11413deb3ec6SMatthias Ringwald }
11423deb3ec6SMatthias Ringwald 
1143ce263fc8SMatthias Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(bd_addr_t bd_addr, uint8_t enable){
11443deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1145a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1146a0ffb263SMatthias Ringwald     if (!hfp_connection){
1147a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
11483deb3ec6SMatthias Ringwald         return;
11493deb3ec6SMatthias Ringwald     }
1150a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
1151a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11523deb3ec6SMatthias Ringwald }
11533deb3ec6SMatthias Ringwald 
1154ce263fc8SMatthias Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1155ce263fc8SMatthias Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 1);
1156ce263fc8SMatthias Ringwald }
1157ce263fc8SMatthias Ringwald 
1158ce263fc8SMatthias Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(bd_addr_t bd_addr){
1159ce263fc8SMatthias Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(bd_addr, 0);
1160ce263fc8SMatthias Ringwald }
1161ce263fc8SMatthias Ringwald 
11623deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1163ce263fc8SMatthias Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(bd_addr_t bd_addr, uint32_t indicators_status_bitmap){
11643deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1165a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1166a0ffb263SMatthias Ringwald     if (!hfp_connection){
1167a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
11683deb3ec6SMatthias Ringwald         return;
11693deb3ec6SMatthias Ringwald     }
1170a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1171a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1172a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11733deb3ec6SMatthias Ringwald }
11743deb3ec6SMatthias Ringwald 
11753deb3ec6SMatthias Ringwald void hfp_hf_query_operator_selection(bd_addr_t bd_addr){
11763deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1177a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1178a0ffb263SMatthias Ringwald     if (!hfp_connection){
1179a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
11803deb3ec6SMatthias Ringwald         return;
11813deb3ec6SMatthias Ringwald     }
1182a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1183ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1184a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1185ce263fc8SMatthias Ringwald             break;
1186ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1187a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1188ce263fc8SMatthias Ringwald             break;
1189ce263fc8SMatthias Ringwald         default:
1190ce263fc8SMatthias Ringwald             break;
1191ce263fc8SMatthias Ringwald     }
1192a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11933deb3ec6SMatthias Ringwald }
11943deb3ec6SMatthias Ringwald 
1195ce263fc8SMatthias Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr, uint8_t enable){
11963deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1197a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1198a0ffb263SMatthias Ringwald     if (!hfp_connection){
1199a0ffb263SMatthias Ringwald         log_error("HFP HF: hfp_connection doesn't exist.");
12003deb3ec6SMatthias Ringwald         return;
12013deb3ec6SMatthias Ringwald     }
1202a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1203a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12043deb3ec6SMatthias Ringwald }
12053deb3ec6SMatthias Ringwald 
1206ce263fc8SMatthias Ringwald 
1207ce263fc8SMatthias Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1208ce263fc8SMatthias Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 1);
1209ce263fc8SMatthias Ringwald }
1210ce263fc8SMatthias Ringwald 
1211ce263fc8SMatthias Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(bd_addr_t bd_addr){
1212ce263fc8SMatthias Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(bd_addr, 0);
1213ce263fc8SMatthias Ringwald }
1214ce263fc8SMatthias Ringwald 
1215ce263fc8SMatthias Ringwald 
12163deb3ec6SMatthias Ringwald void hfp_hf_establish_audio_connection(bd_addr_t bd_addr){
12173deb3ec6SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1218a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1219a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1220ce263fc8SMatthias Ringwald 
1221a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1222a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
12233deb3ec6SMatthias Ringwald 
1224a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1225ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1226a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1227a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1228ce263fc8SMatthias Ringwald     } else {
1229a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1230aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1231aa4dd815SMatthias Ringwald                 break;
1232aa4dd815SMatthias Ringwald             default:
1233a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1234aa4dd815SMatthias Ringwald                 break;
12353deb3ec6SMatthias Ringwald         }
1236ce263fc8SMatthias Ringwald     }
1237ce263fc8SMatthias Ringwald 
1238a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12393deb3ec6SMatthias Ringwald }
12403deb3ec6SMatthias Ringwald 
12413deb3ec6SMatthias Ringwald void hfp_hf_release_audio_connection(bd_addr_t bd_addr){
1242a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1243a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
1244a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12453deb3ec6SMatthias Ringwald }
12463deb3ec6SMatthias Ringwald 
1247ce263fc8SMatthias Ringwald void hfp_hf_answer_incoming_call(bd_addr_t bd_addr){
1248ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1249a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1250ce263fc8SMatthias Ringwald 
1251ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1252a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
1253a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1254ce263fc8SMatthias Ringwald     } else {
1255ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1256ce263fc8SMatthias Ringwald     }
1257ce263fc8SMatthias Ringwald }
1258ce263fc8SMatthias Ringwald 
1259ce263fc8SMatthias Ringwald void hfp_hf_terminate_call(bd_addr_t bd_addr){
1260ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1261a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1262ce263fc8SMatthias Ringwald 
1263a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
1264a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1265ce263fc8SMatthias Ringwald }
1266ce263fc8SMatthias Ringwald 
1267a0ffb263SMatthias Ringwald void hfp_hf_reject_incoming_call(bd_addr_t bd_addr){
1268ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1269a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1270ce263fc8SMatthias Ringwald 
1271ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1272a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
1273a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1274ce263fc8SMatthias Ringwald     }
1275ce263fc8SMatthias Ringwald }
1276ce263fc8SMatthias Ringwald 
1277ce263fc8SMatthias Ringwald void hfp_hf_user_busy(bd_addr_t addr){
1278ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1279a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1280ce263fc8SMatthias Ringwald 
1281ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1282a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
1283a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1284ce263fc8SMatthias Ringwald     }
1285ce263fc8SMatthias Ringwald }
1286ce263fc8SMatthias Ringwald 
1287ce263fc8SMatthias Ringwald void hfp_hf_end_active_and_accept_other(bd_addr_t addr){
1288ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1289a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1290ce263fc8SMatthias Ringwald 
1291ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1292ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1293a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
1294a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1295ce263fc8SMatthias Ringwald     }
1296ce263fc8SMatthias Ringwald }
1297ce263fc8SMatthias Ringwald 
1298ce263fc8SMatthias Ringwald void hfp_hf_swap_calls(bd_addr_t addr){
1299ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1300a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1301ce263fc8SMatthias Ringwald 
1302ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1303ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1304a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
1305a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1306ce263fc8SMatthias Ringwald     }
1307ce263fc8SMatthias Ringwald }
1308ce263fc8SMatthias Ringwald 
1309ce263fc8SMatthias Ringwald void hfp_hf_join_held_call(bd_addr_t addr){
1310ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1311a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1312ce263fc8SMatthias Ringwald 
1313ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1314ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1315a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
1316a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1317ce263fc8SMatthias Ringwald     }
1318ce263fc8SMatthias Ringwald }
1319ce263fc8SMatthias Ringwald 
1320ce263fc8SMatthias Ringwald void hfp_hf_connect_calls(bd_addr_t addr){
1321ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1322a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1323ce263fc8SMatthias Ringwald 
1324ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1325ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1326a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
1327a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1328ce263fc8SMatthias Ringwald     }
1329ce263fc8SMatthias Ringwald }
1330ce263fc8SMatthias Ringwald 
1331667ec068SMatthias Ringwald void hfp_hf_release_call_with_index(bd_addr_t addr, int index){
1332667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1333a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1334667ec068SMatthias Ringwald 
1335667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1336667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1337a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1338a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
1339a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1340667ec068SMatthias Ringwald     }
1341667ec068SMatthias Ringwald }
1342667ec068SMatthias Ringwald 
1343667ec068SMatthias Ringwald void hfp_hf_private_consultation_with_call(bd_addr_t addr, int index){
1344667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1345a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1346667ec068SMatthias Ringwald 
1347667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1348667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1349a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1350a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
1351a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1352667ec068SMatthias Ringwald     }
1353667ec068SMatthias Ringwald }
1354ce263fc8SMatthias Ringwald 
1355ce263fc8SMatthias Ringwald void hfp_hf_dial_number(bd_addr_t bd_addr, char * number){
1356ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1357a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1358ce263fc8SMatthias Ringwald 
1359a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1360ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1361a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1362ce263fc8SMatthias Ringwald }
1363ce263fc8SMatthias Ringwald 
1364a0ffb263SMatthias Ringwald void hfp_hf_dial_memory(bd_addr_t bd_addr, int memory_id){
1365ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1366a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1367ce263fc8SMatthias Ringwald 
1368a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1369a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1370a0ffb263SMatthias Ringwald 
1371a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1372ce263fc8SMatthias Ringwald }
1373ce263fc8SMatthias Ringwald 
1374ce263fc8SMatthias Ringwald void hfp_hf_redial_last_number(bd_addr_t bd_addr){
1375ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1376a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1377ce263fc8SMatthias Ringwald 
1378a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
1379a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1380ce263fc8SMatthias Ringwald }
1381ce263fc8SMatthias Ringwald 
1382ce263fc8SMatthias Ringwald void hfp_hf_activate_call_waiting_notification(bd_addr_t bd_addr){
1383ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1384a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1385ce263fc8SMatthias Ringwald 
1386a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
1387a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1388ce263fc8SMatthias Ringwald }
1389ce263fc8SMatthias Ringwald 
1390ce263fc8SMatthias Ringwald 
1391ce263fc8SMatthias Ringwald void hfp_hf_deactivate_call_waiting_notification(bd_addr_t bd_addr){
1392ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1393a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1394ce263fc8SMatthias Ringwald 
1395a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1396a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1397ce263fc8SMatthias Ringwald }
1398ce263fc8SMatthias Ringwald 
1399ce263fc8SMatthias Ringwald 
1400ce263fc8SMatthias Ringwald void hfp_hf_activate_calling_line_notification(bd_addr_t bd_addr){
1401ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1402a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1403ce263fc8SMatthias Ringwald 
1404a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
1405a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1406ce263fc8SMatthias Ringwald }
1407ce263fc8SMatthias Ringwald 
1408ce263fc8SMatthias Ringwald void hfp_hf_deactivate_calling_line_notification(bd_addr_t bd_addr){
1409ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1410a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1411ce263fc8SMatthias Ringwald 
1412a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
1413a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1414ce263fc8SMatthias Ringwald }
1415ce263fc8SMatthias Ringwald 
1416ce263fc8SMatthias Ringwald 
1417ce263fc8SMatthias Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1418ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1419a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1420ce263fc8SMatthias Ringwald 
1421a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1422a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1423ce263fc8SMatthias Ringwald }
1424ce263fc8SMatthias Ringwald 
1425ce263fc8SMatthias Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(bd_addr_t bd_addr){
1426ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1427a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1428ce263fc8SMatthias Ringwald 
1429a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1430a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1431ce263fc8SMatthias Ringwald }
1432ce263fc8SMatthias Ringwald 
1433ce263fc8SMatthias Ringwald void hfp_hf_activate_voice_recognition_notification(bd_addr_t bd_addr){
1434ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1435a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1436ce263fc8SMatthias Ringwald 
1437a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
1438a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1439ce263fc8SMatthias Ringwald }
1440ce263fc8SMatthias Ringwald 
1441ce263fc8SMatthias Ringwald void hfp_hf_deactivate_voice_recognition_notification(bd_addr_t bd_addr){
1442ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1443a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1444ce263fc8SMatthias Ringwald 
1445a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1446a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1447ce263fc8SMatthias Ringwald }
1448ce263fc8SMatthias Ringwald 
1449ce263fc8SMatthias Ringwald void hfp_hf_set_microphone_gain(bd_addr_t bd_addr, int gain){
1450ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1451a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1452a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1453a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1454a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1455a0ffb263SMatthias Ringwald         return;
1456a0ffb263SMatthias Ringwald     }
1457a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1458a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1459a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1460ce263fc8SMatthias Ringwald }
1461ce263fc8SMatthias Ringwald 
1462ce263fc8SMatthias Ringwald void hfp_hf_set_speaker_gain(bd_addr_t bd_addr, int gain){
1463ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(bd_addr);
1464a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(bd_addr);
1465a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1466a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1467a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1468a0ffb263SMatthias Ringwald         return;
1469a0ffb263SMatthias Ringwald     }
1470a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1471a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1472a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1473ce263fc8SMatthias Ringwald }
1474ce263fc8SMatthias Ringwald 
1475ce263fc8SMatthias Ringwald void hfp_hf_send_dtmf_code(bd_addr_t addr, char code){
1476ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1477a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1478a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
1479a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1480ce263fc8SMatthias Ringwald }
1481ce263fc8SMatthias Ringwald 
1482ce263fc8SMatthias Ringwald void hfp_hf_request_phone_number_for_voice_tag(bd_addr_t addr){
1483ce263fc8SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1484a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1485a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
1486a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1487ce263fc8SMatthias Ringwald }
14883deb3ec6SMatthias Ringwald 
1489667ec068SMatthias Ringwald void hfp_hf_query_current_call_status(bd_addr_t addr){
1490667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1491a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1492a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
1493a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1494667ec068SMatthias Ringwald }
1495667ec068SMatthias Ringwald 
1496667ec068SMatthias Ringwald 
1497667ec068SMatthias Ringwald void hfp_hf_rrh_query_status(bd_addr_t addr){
1498667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1499a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1500a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1501a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
1502a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1503667ec068SMatthias Ringwald }
1504667ec068SMatthias Ringwald 
1505667ec068SMatthias Ringwald void hfp_hf_rrh_hold_call(bd_addr_t addr){
1506667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1507a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1508a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1509a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
1510a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1511667ec068SMatthias Ringwald }
1512667ec068SMatthias Ringwald 
1513667ec068SMatthias Ringwald void hfp_hf_rrh_accept_held_call(bd_addr_t addr){
1514667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1515a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1516a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1517a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
1518a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1519667ec068SMatthias Ringwald }
1520667ec068SMatthias Ringwald 
1521a0ffb263SMatthias Ringwald void hfp_hf_rrh_reject_held_call(bd_addr_t addr){
1522667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1523a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1524a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1525a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
1526a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1527667ec068SMatthias Ringwald }
1528667ec068SMatthias Ringwald 
1529a0ffb263SMatthias Ringwald void hfp_hf_query_subscriber_number(bd_addr_t addr){
1530667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1531a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1532a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
1533a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1534667ec068SMatthias Ringwald }
1535667ec068SMatthias Ringwald 
1536667ec068SMatthias Ringwald void hfp_hf_set_hf_indicator(bd_addr_t addr, int assigned_number, int value){
1537667ec068SMatthias Ringwald     hfp_hf_establish_service_level_connection(addr);
1538a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_bd_addr(addr);
1539667ec068SMatthias Ringwald     // find index for assigned number
1540667ec068SMatthias Ringwald     int i;
1541667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1542667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1543667ec068SMatthias Ringwald             // set value
1544667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1545667ec068SMatthias Ringwald             // mark for update
1546a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1547a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1548667ec068SMatthias Ringwald                 // send update
1549a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1550a0ffb263SMatthias Ringwald             }
1551667ec068SMatthias Ringwald             return;
1552667ec068SMatthias Ringwald         }
1553667ec068SMatthias Ringwald     }
1554667ec068SMatthias Ringwald }
1555667ec068SMatthias Ringwald 
1556