xref: /btstack/src/classic/hfp_hf.c (revision 86da9d7414edc38c74d4336c8c6075b6f37ddb4e)
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  */
37ab2c6ae4SMatthias Ringwald 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "hfp_hf.c"
393deb3ec6SMatthias Ringwald 
403deb3ec6SMatthias Ringwald // *****************************************************************************
413deb3ec6SMatthias Ringwald //
42fffdd288SMatthias Ringwald // HFP Hands-Free (HF) unit
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald // *****************************************************************************
453deb3ec6SMatthias Ringwald 
467907f069SMatthias Ringwald #include "btstack_config.h"
473deb3ec6SMatthias Ringwald 
483deb3ec6SMatthias Ringwald #include <stdint.h>
493deb3ec6SMatthias Ringwald #include <stdio.h>
503deb3ec6SMatthias Ringwald #include <stdlib.h>
513deb3ec6SMatthias Ringwald #include <string.h>
523deb3ec6SMatthias Ringwald 
53235946f1SMatthias Ringwald #include "bluetooth_sdp.h"
5459c6af15SMatthias Ringwald #include "btstack_debug.h"
55d4dd47ffSMatthias Ringwald #include "btstack_event.h"
563deb3ec6SMatthias Ringwald #include "btstack_memory.h"
5759c6af15SMatthias Ringwald #include "btstack_run_loop.h"
5859c6af15SMatthias Ringwald #include "classic/core.h"
5959c6af15SMatthias Ringwald #include "classic/hfp.h"
6059c6af15SMatthias Ringwald #include "classic/hfp_hf.h"
61efda0b48SMatthias Ringwald #include "classic/sdp_client_rfcomm.h"
62746ccb7eSMatthias Ringwald #include "classic/sdp_server.h"
63023f2764SMatthias Ringwald #include "classic/sdp_util.h"
6459c6af15SMatthias Ringwald #include "hci.h"
6559c6af15SMatthias Ringwald #include "hci_cmd.h"
6659c6af15SMatthias Ringwald #include "hci_dump.h"
6759c6af15SMatthias Ringwald #include "l2cap.h"
683deb3ec6SMatthias Ringwald 
6927950165SMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
7027950165SMatthias Ringwald 
713deb3ec6SMatthias Ringwald static const char default_hfp_hf_service_name[] = "Hands-Free unit";
723deb3ec6SMatthias Ringwald static uint16_t hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
733deb3ec6SMatthias Ringwald static uint8_t hfp_codecs_nr = 0;
743deb3ec6SMatthias Ringwald static uint8_t hfp_codecs[HFP_MAX_NUM_CODECS];
753deb3ec6SMatthias Ringwald 
763deb3ec6SMatthias Ringwald static uint8_t hfp_indicators_nr = 0;
773deb3ec6SMatthias Ringwald static uint8_t hfp_indicators[HFP_MAX_NUM_HF_INDICATORS];
78667ec068SMatthias Ringwald static uint32_t hfp_indicators_value[HFP_MAX_NUM_HF_INDICATORS];
79667ec068SMatthias Ringwald 
80667ec068SMatthias Ringwald static uint8_t hfp_hf_speaker_gain = 9;
81667ec068SMatthias Ringwald static uint8_t hfp_hf_microphone_gain = 9;
823deb3ec6SMatthias Ringwald 
83ca59be51SMatthias Ringwald static btstack_packet_handler_t hfp_hf_callback;
843deb3ec6SMatthias Ringwald 
85ce263fc8SMatthias Ringwald static hfp_call_status_t hfp_call_status;
86ce263fc8SMatthias Ringwald static hfp_callsetup_status_t hfp_callsetup_status;
87ce263fc8SMatthias Ringwald static hfp_callheld_status_t hfp_callheld_status;
88ce263fc8SMatthias Ringwald 
89ce263fc8SMatthias Ringwald static char phone_number[25];
90ce263fc8SMatthias Ringwald 
919c9c64c1SMatthias Ringwald static hfp_connection_t * get_hfp_hf_connection_context_for_acl_handle(uint16_t handle){
929c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_t it;
939c9c64c1SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
949c9c64c1SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
959c9c64c1SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
969c9c64c1SMatthias Ringwald         if (hfp_connection->acl_handle != handle)      continue;
979c9c64c1SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
989c9c64c1SMatthias Ringwald         return hfp_connection;
999c9c64c1SMatthias Ringwald     }
1009c9c64c1SMatthias Ringwald     return NULL;
1019c9c64c1SMatthias Ringwald }
1029c9c64c1SMatthias Ringwald 
10313839019SMatthias Ringwald void hfp_hf_register_packet_handler(btstack_packet_handler_t callback){
1043deb3ec6SMatthias Ringwald     if (callback == NULL){
1053deb3ec6SMatthias Ringwald         log_error("hfp_hf_register_packet_handler called with NULL callback");
1063deb3ec6SMatthias Ringwald         return;
1073deb3ec6SMatthias Ringwald     }
108ca59be51SMatthias Ringwald     hfp_hf_callback = callback;
109ca59be51SMatthias Ringwald     hfp_set_hf_callback(callback);
1103deb3ec6SMatthias Ringwald }
1113deb3ec6SMatthias Ringwald 
11213839019SMatthias Ringwald static void hfp_hf_emit_subscriber_information(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t status, uint8_t bnip_type, const char * bnip_number){
113a0ffb263SMatthias Ringwald     if (!callback) return;
114a0ffb263SMatthias Ringwald     uint8_t event[31];
115a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
116a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
117a0ffb263SMatthias Ringwald     event[2] = event_subtype;
118a0ffb263SMatthias Ringwald     event[3] = status;
119a0ffb263SMatthias Ringwald     event[4] = bnip_type;
120adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 6) ? (int) strlen(bnip_number) : (int) sizeof(event) - 6;
121a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], bnip_number, size);
122a0ffb263SMatthias Ringwald     event[5 + size] = 0;
12313839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
124a0ffb263SMatthias Ringwald }
125a0ffb263SMatthias Ringwald 
12613839019SMatthias Ringwald static void hfp_hf_emit_type_and_number(btstack_packet_handler_t callback, uint8_t event_subtype, uint8_t bnip_type, const char * bnip_number){
127a0ffb263SMatthias Ringwald     if (!callback) return;
128a0ffb263SMatthias Ringwald     uint8_t event[30];
129a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
130a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
131a0ffb263SMatthias Ringwald     event[2] = event_subtype;
132a0ffb263SMatthias Ringwald     event[3] = bnip_type;
133adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 5) ? (int) strlen(bnip_number) : (int) sizeof(event) - 5;
134a0ffb263SMatthias Ringwald     strncpy((char*)&event[4], bnip_number, size);
135a0ffb263SMatthias Ringwald     event[4 + size] = 0;
13613839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
137a0ffb263SMatthias Ringwald }
138a0ffb263SMatthias Ringwald 
13913839019SMatthias Ringwald static void hfp_hf_emit_enhanced_call_status(btstack_packet_handler_t callback, uint8_t clcc_idx, uint8_t clcc_dir,
140a0ffb263SMatthias Ringwald                 uint8_t clcc_status, uint8_t clcc_mpty, uint8_t bnip_type, const char * bnip_number){
141a0ffb263SMatthias Ringwald     if (!callback) return;
142a0ffb263SMatthias Ringwald     uint8_t event[35];
143a0ffb263SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
144a0ffb263SMatthias Ringwald     event[1] = sizeof(event) - 2;
145a0ffb263SMatthias Ringwald     event[2] = HFP_SUBEVENT_ENHANCED_CALL_STATUS;
146a0ffb263SMatthias Ringwald     event[3] = clcc_idx;
147a0ffb263SMatthias Ringwald     event[4] = clcc_dir;
148a0ffb263SMatthias Ringwald     event[6] = clcc_status;
149a0ffb263SMatthias Ringwald     event[7] = clcc_mpty;
150a0ffb263SMatthias Ringwald     event[8] = bnip_type;
151adaba9f3SMatthias Ringwald     int size = (strlen(bnip_number) < sizeof(event) - 10) ? (int) strlen(bnip_number) : (int) sizeof(event) - 10;
152a0ffb263SMatthias Ringwald     strncpy((char*)&event[9], bnip_number, size);
153a0ffb263SMatthias Ringwald     event[9 + size] = 0;
15413839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
155a0ffb263SMatthias Ringwald }
156a0ffb263SMatthias Ringwald 
157a0ffb263SMatthias Ringwald static int has_codec_negotiation_feature(hfp_connection_t * hfp_connection){
1583deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_CODEC_NEGOTIATION);
159a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_CODEC_NEGOTIATION);
1603deb3ec6SMatthias Ringwald     return hf && ag;
1613deb3ec6SMatthias Ringwald }
1623deb3ec6SMatthias Ringwald 
163a0ffb263SMatthias Ringwald static int has_call_waiting_and_3way_calling_feature(hfp_connection_t * hfp_connection){
1643deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_THREE_WAY_CALLING);
165a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_THREE_WAY_CALLING);
1663deb3ec6SMatthias Ringwald     return hf && ag;
1673deb3ec6SMatthias Ringwald }
1683deb3ec6SMatthias Ringwald 
1693deb3ec6SMatthias Ringwald 
170a0ffb263SMatthias Ringwald static int has_hf_indicators_feature(hfp_connection_t * hfp_connection){
1713deb3ec6SMatthias Ringwald     int hf = get_bit(hfp_supported_features, HFP_HFSF_HF_INDICATORS);
172a0ffb263SMatthias Ringwald     int ag = get_bit(hfp_connection->remote_supported_features, HFP_AGSF_HF_INDICATORS);
1733deb3ec6SMatthias Ringwald     return hf && ag;
1743deb3ec6SMatthias Ringwald }
1753deb3ec6SMatthias Ringwald 
1764f84bf36SMatthias Ringwald void hfp_hf_create_sdp_record(uint8_t * service, uint32_t service_record_handle, int rfcomm_channel_nr, const char * name, uint16_t supported_features, int wide_band_speech){
1773deb3ec6SMatthias Ringwald     if (!name){
1783deb3ec6SMatthias Ringwald         name = default_hfp_hf_service_name;
1793deb3ec6SMatthias Ringwald     }
180235946f1SMatthias Ringwald     hfp_create_sdp_record(service, service_record_handle, BLUETOOTH_SERVICE_CLASS_HANDSFREE, rfcomm_channel_nr, name);
1813deb3ec6SMatthias Ringwald 
1824f84bf36SMatthias Ringwald     // Construct SupportedFeatures for SDP bitmap:
1834f84bf36SMatthias Ringwald     //
1844f84bf36SMatthias Ringwald     // "The values of the “SupportedFeatures” bitmap given in Table 5.4 shall be the same as the values
1854f84bf36SMatthias Ringwald     //  of the Bits 0 to 4 of the unsolicited result code +BRSF"
1864f84bf36SMatthias Ringwald     //
1874f84bf36SMatthias Ringwald     uint16_t sdp_features = supported_features & 0x1f;
1884f84bf36SMatthias Ringwald     if (supported_features & wide_band_speech){
1894f84bf36SMatthias Ringwald         sdp_features |= 1 << 5; // Wide band speech bit
1904f84bf36SMatthias Ringwald     }
191aa4dd815SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, 0x0311);    // Hands-Free Profile - SupportedFeatures
1924f84bf36SMatthias Ringwald     de_add_number(service, DE_UINT, DE_SIZE_16, sdp_features);
193aa4dd815SMatthias Ringwald }
1943deb3ec6SMatthias Ringwald 
19589425bfcSMilanka Ringwald 
19689425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd(uint16_t cid, const char * cmd){
1973deb3ec6SMatthias Ringwald     char buffer[20];
19889425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s\r\n", cmd);
19989425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
20089425bfcSMilanka Ringwald }
20189425bfcSMilanka Ringwald 
20289425bfcSMilanka Ringwald static inline int hfp_hf_send_cmd_with_mark(uint16_t cid, const char * cmd, const char * mark){
20389425bfcSMilanka Ringwald     char buffer[20];
20489425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s%s\r\n", cmd, mark);
20589425bfcSMilanka Ringwald     return send_str_over_rfcomm(cid, buffer);
20689425bfcSMilanka Ringwald }
20789425bfcSMilanka Ringwald 
208*86da9d74SMatthias Ringwald static inline int hfp_hf_send_cmd_with_int(uint16_t cid, const char * cmd, uint16_t value){
20989425bfcSMilanka Ringwald     char buffer[40];
21089425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%d\r\n", cmd, value);
2113deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2123deb3ec6SMatthias Ringwald }
2133deb3ec6SMatthias Ringwald 
2143deb3ec6SMatthias Ringwald static int hfp_hf_cmd_notify_on_codecs(uint16_t cid){
2153deb3ec6SMatthias Ringwald     char buffer[30];
21689425bfcSMilanka Ringwald     const int size = sizeof(buffer);
21789425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_AVAILABLE_CODECS);
21889425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_codecs, hfp_codecs_nr);
21989425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2203deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2213deb3ec6SMatthias Ringwald }
2223deb3ec6SMatthias Ringwald 
2233deb3ec6SMatthias Ringwald static int hfp_hf_cmd_activate_status_update_for_ag_indicator(uint16_t cid, uint32_t indicators_status, int indicators_nr){
2243deb3ec6SMatthias Ringwald     char buffer[50];
22589425bfcSMilanka Ringwald     const int size = sizeof(buffer);
22689425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_UPDATE_ENABLE_STATUS_FOR_INDIVIDUAL_AG_INDICATORS);
22789425bfcSMilanka Ringwald     offset += join_bitmap(buffer+offset, size-offset, indicators_status, indicators_nr);
22889425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2293deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2303deb3ec6SMatthias Ringwald }
2313deb3ec6SMatthias Ringwald 
2323deb3ec6SMatthias Ringwald static int hfp_hf_cmd_list_supported_generic_status_indicators(uint16_t cid){
2333deb3ec6SMatthias Ringwald     char buffer[30];
23489425bfcSMilanka Ringwald     const int size = sizeof(buffer);
23589425bfcSMilanka Ringwald     int offset = snprintf(buffer, size, "AT%s=", HFP_GENERIC_STATUS_INDICATOR);
23689425bfcSMilanka Ringwald     offset += join(buffer+offset, size-offset, hfp_indicators, hfp_indicators_nr);
23789425bfcSMilanka Ringwald     offset += snprintf(buffer+offset, size-offset, "\r\n");
2383deb3ec6SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
2393deb3ec6SMatthias Ringwald }
2403deb3ec6SMatthias Ringwald 
24189425bfcSMilanka Ringwald static int hfp_hf_cmd_activate_status_update_for_all_ag_indicators(uint16_t cid, uint8_t activate){
2423deb3ec6SMatthias Ringwald     char buffer[20];
24389425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=3,0,0,%d\r\n", HFP_ENABLE_STATUS_UPDATE_FOR_AG_INDICATORS, activate);
244ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
245ce263fc8SMatthias Ringwald }
246ce263fc8SMatthias Ringwald 
247ce263fc8SMatthias Ringwald static int hfp_hf_initiate_outgoing_call_cmd(uint16_t cid){
248ce263fc8SMatthias Ringwald     char buffer[40];
24989425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s%s;\r\n", HFP_CALL_PHONE_NUMBER, phone_number);
250ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
251ce263fc8SMatthias Ringwald }
252ce263fc8SMatthias Ringwald 
253a0ffb263SMatthias Ringwald static int hfp_hf_send_memory_dial_cmd(uint16_t cid, int memory_id){
254ce263fc8SMatthias Ringwald     char buffer[40];
25589425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "%s>%d;\r\n", HFP_CALL_PHONE_NUMBER, memory_id);
256ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
257ce263fc8SMatthias Ringwald }
258ce263fc8SMatthias Ringwald 
259f04a0c31SMatthias Ringwald static int hfp_hf_send_chld(uint16_t cid, unsigned int number){
26089425bfcSMilanka Ringwald     char buffer[40];
26189425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%u\r\n", HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, number);
262ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
263ce263fc8SMatthias Ringwald }
264ce263fc8SMatthias Ringwald 
265ce263fc8SMatthias Ringwald static int hfp_hf_send_dtmf(uint16_t cid, char code){
266ce263fc8SMatthias Ringwald     char buffer[20];
26789425bfcSMilanka Ringwald     snprintf(buffer, sizeof(buffer), "AT%s=%c\r\n", HFP_TRANSMIT_DTMF_CODES, code);
268ce263fc8SMatthias Ringwald     return send_str_over_rfcomm(cid, buffer);
269ce263fc8SMatthias Ringwald }
270ce263fc8SMatthias Ringwald 
27197d2cadbSMatthias Ringwald static int hfp_hf_cmd_ata(uint16_t cid){
27297d2cadbSMatthias Ringwald     return send_str_over_rfcomm(cid, "ATA\r\n");
27397d2cadbSMatthias Ringwald }
27497d2cadbSMatthias Ringwald 
27589425bfcSMilanka Ringwald static int hfp_hf_cmd_exchange_supported_features(uint16_t cid){
27689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SUPPORTED_FEATURES, hfp_supported_features);
27789425bfcSMilanka Ringwald }
27889425bfcSMilanka Ringwald 
27989425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators(uint16_t cid){
28089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "=?");
28189425bfcSMilanka Ringwald }
28289425bfcSMilanka Ringwald 
28389425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_indicators_status(uint16_t cid){
28489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_INDICATOR, "?");
28589425bfcSMilanka Ringwald }
28689425bfcSMilanka Ringwald 
28789425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_can_hold_call(uint16_t cid){
28889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_SUPPORT_CALL_HOLD_AND_MULTIPARTY_SERVICES, "=?");
28989425bfcSMilanka Ringwald }
29089425bfcSMilanka Ringwald 
29189425bfcSMilanka Ringwald static int hfp_hf_cmd_retrieve_supported_generic_status_indicators(uint16_t cid){
29289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "=?");
29389425bfcSMilanka Ringwald }
29489425bfcSMilanka Ringwald 
29589425bfcSMilanka Ringwald static int hfp_hf_cmd_list_initital_supported_generic_status_indicators(uint16_t cid){
29689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_GENERIC_STATUS_INDICATOR, "?");
29789425bfcSMilanka Ringwald }
29889425bfcSMilanka Ringwald 
29989425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name_format(uint16_t cid){
30089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "=3,0");
30189425bfcSMilanka Ringwald }
30289425bfcSMilanka Ringwald 
30389425bfcSMilanka Ringwald static int hfp_hf_cmd_query_operator_name(uint16_t cid){
30489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_QUERY_OPERATOR_SELECTION, "?");
30589425bfcSMilanka Ringwald }
30689425bfcSMilanka Ringwald 
30789425bfcSMilanka Ringwald static int hfp_hf_cmd_trigger_codec_connection_setup(uint16_t cid){
30889425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_TRIGGER_CODEC_CONNECTION_SETUP);
30989425bfcSMilanka Ringwald }
31089425bfcSMilanka Ringwald 
31189425bfcSMilanka Ringwald static int hfp_hf_set_microphone_gain_cmd(uint16_t cid, int gain){
31289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_MICROPHONE_GAIN, gain);
31389425bfcSMilanka Ringwald }
31489425bfcSMilanka Ringwald 
31589425bfcSMilanka Ringwald static int hfp_hf_set_speaker_gain_cmd(uint16_t cid, int gain){
31689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_SET_SPEAKER_GAIN, gain);
31789425bfcSMilanka Ringwald }
31889425bfcSMilanka Ringwald 
31989425bfcSMilanka Ringwald static int hfp_hf_set_calling_line_notification_cmd(uint16_t cid, uint8_t activate){
32089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CLIP, activate);
32189425bfcSMilanka Ringwald }
32289425bfcSMilanka Ringwald 
32389425bfcSMilanka Ringwald static int hfp_hf_set_echo_canceling_and_noise_reduction_cmd(uint16_t cid, uint8_t activate){
32489425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_TURN_OFF_EC_AND_NR, activate);
32589425bfcSMilanka Ringwald }
32689425bfcSMilanka Ringwald 
32789425bfcSMilanka Ringwald static int hfp_hf_set_voice_recognition_notification_cmd(uint16_t cid, uint8_t activate){
32889425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ACTIVATE_VOICE_RECOGNITION, activate);
32989425bfcSMilanka Ringwald }
33089425bfcSMilanka Ringwald 
33189425bfcSMilanka Ringwald static int hfp_hf_set_call_waiting_notification_cmd(uint16_t cid, uint8_t activate){
33289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_CALL_WAITING_NOTIFICATION, activate);
33389425bfcSMilanka Ringwald }
33489425bfcSMilanka Ringwald 
33589425bfcSMilanka Ringwald static int hfp_hf_cmd_confirm_codec(uint16_t cid, uint8_t codec){
33689425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_CONFIRM_COMMON_CODEC, codec);
33789425bfcSMilanka Ringwald }
33889425bfcSMilanka Ringwald 
33989425bfcSMilanka Ringwald static int hfp_hf_cmd_enable_extended_audio_gateway_error_report(uint16_t cid, uint8_t enable){
34089425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_int(cid, HFP_ENABLE_EXTENDED_AUDIO_GATEWAY_ERROR, enable);
34189425bfcSMilanka Ringwald }
34289425bfcSMilanka Ringwald 
34389425bfcSMilanka Ringwald static int hfp_hf_send_redial_last_number_cmd(uint16_t cid){
34489425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_REDIAL_LAST_NUMBER);
34589425bfcSMilanka Ringwald }
34689425bfcSMilanka Ringwald 
34789425bfcSMilanka Ringwald static int hfp_hf_send_chup(uint16_t cid){
34889425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_HANG_UP_CALL);
34989425bfcSMilanka Ringwald }
35089425bfcSMilanka Ringwald 
351ce263fc8SMatthias Ringwald static int hfp_hf_send_binp(uint16_t cid){
35289425bfcSMilanka Ringwald     return hfp_hf_send_cmd_with_mark(cid, HFP_PHONE_NUMBER_FOR_VOICE_TAG, "=1");
353ce263fc8SMatthias Ringwald }
354ce263fc8SMatthias Ringwald 
355667ec068SMatthias Ringwald static int hfp_hf_send_clcc(uint16_t cid){
35689425bfcSMilanka Ringwald     return hfp_hf_send_cmd(cid, HFP_LIST_CURRENT_CALLS);
357667ec068SMatthias Ringwald }
358667ec068SMatthias Ringwald 
35913839019SMatthias Ringwald static void hfp_emit_ag_indicator_event(btstack_packet_handler_t callback, hfp_ag_indicator_t indicator){
3603deb3ec6SMatthias Ringwald     if (!callback) return;
361a0ffb263SMatthias Ringwald     uint8_t event[5+HFP_MAX_INDICATOR_DESC_SIZE+1];
3623deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3633deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3643deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_AG_INDICATOR_STATUS_CHANGED;
365a0ffb263SMatthias Ringwald     event[3] = indicator.index;
366a0ffb263SMatthias Ringwald     event[4] = indicator.status;
367a0ffb263SMatthias Ringwald     strncpy((char*)&event[5], indicator.name, HFP_MAX_INDICATOR_DESC_SIZE);
368a0ffb263SMatthias Ringwald     event[5+HFP_MAX_INDICATOR_DESC_SIZE] = 0;
36913839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3703deb3ec6SMatthias Ringwald }
3713deb3ec6SMatthias Ringwald 
37213839019SMatthias Ringwald static void hfp_emit_network_operator_event(btstack_packet_handler_t callback, hfp_network_opearator_t network_operator){
3733deb3ec6SMatthias Ringwald     if (!callback) return;
37489425bfcSMilanka Ringwald     uint8_t event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE+1];
3753deb3ec6SMatthias Ringwald     event[0] = HCI_EVENT_HFP_META;
3763deb3ec6SMatthias Ringwald     event[1] = sizeof(event) - 2;
3773deb3ec6SMatthias Ringwald     event[2] = HFP_SUBEVENT_NETWORK_OPERATOR_CHANGED;
378a0ffb263SMatthias Ringwald     event[3] = network_operator.mode;
379a0ffb263SMatthias Ringwald     event[4] = network_operator.format;
38089425bfcSMilanka Ringwald     strncpy((char*)&event[5], network_operator.name, HFP_MAX_NETWORK_OPERATOR_NAME_SIZE);
38189425bfcSMilanka Ringwald     event[5+HFP_MAX_NETWORK_OPERATOR_NAME_SIZE] = 0;
38213839019SMatthias Ringwald     (*callback)(HCI_EVENT_PACKET, 0, event, sizeof(event));
3833deb3ec6SMatthias Ringwald }
3843deb3ec6SMatthias Ringwald 
385a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection(hfp_connection_t * hfp_connection){
386a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
387a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
388aa4dd815SMatthias Ringwald     int done = 1;
3893deb3ec6SMatthias Ringwald 
390a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
3913deb3ec6SMatthias Ringwald         case HFP_EXCHANGE_SUPPORTED_FEATURES:
392d715cf51SMatthias Ringwald             hfp_hf_drop_mSBC_if_eSCO_not_supported(hfp_codecs, &hfp_codecs_nr);
393a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_EXCHANGE_SUPPORTED_FEATURES;
394a0ffb263SMatthias Ringwald             hfp_hf_cmd_exchange_supported_features(hfp_connection->rfcomm_cid);
3953deb3ec6SMatthias Ringwald             break;
3963deb3ec6SMatthias Ringwald         case HFP_NOTIFY_ON_CODECS:
397a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_NOTIFY_ON_CODECS;
398a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
3993deb3ec6SMatthias Ringwald             break;
4003deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS:
401a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS;
402a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators(hfp_connection->rfcomm_cid);
4033deb3ec6SMatthias Ringwald             break;
4043deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INDICATORS_STATUS:
405a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INDICATORS_STATUS;
406a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_indicators_status(hfp_connection->rfcomm_cid);
4073deb3ec6SMatthias Ringwald             break;
4083deb3ec6SMatthias Ringwald         case HFP_ENABLE_INDICATORS_STATUS_UPDATE:
409a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE;
410a0ffb263SMatthias Ringwald             hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, 1);
4113deb3ec6SMatthias Ringwald             break;
4123deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_CAN_HOLD_CALL:
413a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_CAN_HOLD_CALL;
414a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_can_hold_call(hfp_connection->rfcomm_cid);
4153deb3ec6SMatthias Ringwald             break;
4163deb3ec6SMatthias Ringwald         case HFP_LIST_GENERIC_STATUS_INDICATORS:
417a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_LIST_GENERIC_STATUS_INDICATORS;
418a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4193deb3ec6SMatthias Ringwald             break;
4203deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_GENERIC_STATUS_INDICATORS:
421a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS;
422a0ffb263SMatthias Ringwald             hfp_hf_cmd_retrieve_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4233deb3ec6SMatthias Ringwald             break;
4243deb3ec6SMatthias Ringwald         case HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
425a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
426a0ffb263SMatthias Ringwald             hfp_hf_cmd_list_initital_supported_generic_status_indicators(hfp_connection->rfcomm_cid);
4273deb3ec6SMatthias Ringwald             break;
4283deb3ec6SMatthias Ringwald         default:
429aa4dd815SMatthias Ringwald             done = 0;
4303deb3ec6SMatthias Ringwald             break;
4313deb3ec6SMatthias Ringwald     }
4323deb3ec6SMatthias Ringwald     return done;
4333deb3ec6SMatthias Ringwald }
4343deb3ec6SMatthias Ringwald 
435ce263fc8SMatthias Ringwald 
436a0ffb263SMatthias Ringwald static int hfp_hf_run_for_context_service_level_connection_queries(hfp_connection_t * hfp_connection){
437a0ffb263SMatthias Ringwald     if (hfp_connection->state != HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED) return 0;
438a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
439ce263fc8SMatthias Ringwald 
440ce263fc8SMatthias Ringwald     int done = 0;
441a0ffb263SMatthias Ringwald     if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
442a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
443ce263fc8SMatthias Ringwald         done = 1;
444a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_all_ag_indicators(hfp_connection->rfcomm_cid, hfp_connection->enable_status_update_for_ag_indicators);
445ce263fc8SMatthias Ringwald         return done;
446ce263fc8SMatthias Ringwald     };
447a0ffb263SMatthias Ringwald     if (hfp_connection->change_status_update_for_individual_ag_indicators){
448a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
449ce263fc8SMatthias Ringwald         done = 1;
450a0ffb263SMatthias Ringwald         hfp_hf_cmd_activate_status_update_for_ag_indicator(hfp_connection->rfcomm_cid,
451a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_status_update_bitmap,
452a0ffb263SMatthias Ringwald                 hfp_connection->ag_indicators_nr);
453ce263fc8SMatthias Ringwald         return done;
454ce263fc8SMatthias Ringwald     }
455ce263fc8SMatthias Ringwald 
456a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
457ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SET_FORMAT:
458a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK;
459a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
460a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name_format(hfp_connection->rfcomm_cid);
461ce263fc8SMatthias Ringwald             return 1;
462ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_SEND_QUERY:
463a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HPF_HF_QUERY_OPERATOR_W4_RESULT;
464a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
465a0ffb263SMatthias Ringwald             hfp_hf_cmd_query_operator_name(hfp_connection->rfcomm_cid);
466ce263fc8SMatthias Ringwald             return 1;
467ce263fc8SMatthias Ringwald         default:
468ce263fc8SMatthias Ringwald             break;
469ce263fc8SMatthias Ringwald     }
470ce263fc8SMatthias Ringwald 
471a0ffb263SMatthias Ringwald     if (hfp_connection->enable_extended_audio_gateway_error_report){
472a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
473ce263fc8SMatthias Ringwald         done = 1;
474a0ffb263SMatthias Ringwald         hfp_hf_cmd_enable_extended_audio_gateway_error_report(hfp_connection->rfcomm_cid, hfp_connection->enable_extended_audio_gateway_error_report);
475ce263fc8SMatthias Ringwald         return done;
476ce263fc8SMatthias Ringwald     }
477ce263fc8SMatthias Ringwald 
478ce263fc8SMatthias Ringwald     return done;
479ce263fc8SMatthias Ringwald }
480ce263fc8SMatthias Ringwald 
481a0ffb263SMatthias Ringwald static int codecs_exchange_state_machine(hfp_connection_t * hfp_connection){
482ce263fc8SMatthias Ringwald     /* events ( == commands):
483ce263fc8SMatthias Ringwald         HFP_CMD_AVAILABLE_CODECS == received AT+BAC with list of codecs
484ce263fc8SMatthias Ringwald         HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
485ce263fc8SMatthias Ringwald             hf_trigger_codec_connection_setup == received BCC
486ce263fc8SMatthias Ringwald             ag_trigger_codec_connection_setup == received from AG to send BCS
487ce263fc8SMatthias Ringwald         HFP_CMD_HF_CONFIRMED_CODEC == received AT+BCS
488ce263fc8SMatthias Ringwald     */
489ce263fc8SMatthias Ringwald 
490a0ffb263SMatthias Ringwald     if (hfp_connection->ok_pending) return 0;
491ce263fc8SMatthias Ringwald 
492a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
493ce263fc8SMatthias Ringwald         case HFP_CMD_AVAILABLE_CODECS:
494a0ffb263SMatthias Ringwald             if (hfp_connection->codecs_state == HFP_CODECS_W4_AG_COMMON_CODEC) return 0;
495ce263fc8SMatthias Ringwald 
496a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
497a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
498a0ffb263SMatthias Ringwald             hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
499ce263fc8SMatthias Ringwald             return 1;
500ce263fc8SMatthias Ringwald         case HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP:
501a0ffb263SMatthias Ringwald             hfp_connection->codec_confirmed = 0;
502a0ffb263SMatthias Ringwald             hfp_connection->suggested_codec = 0;
503a0ffb263SMatthias Ringwald             hfp_connection->negotiated_codec = 0;
504ce263fc8SMatthias Ringwald 
505a0ffb263SMatthias Ringwald             hfp_connection->codecs_state = HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE;
506a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 1;
507a0ffb263SMatthias Ringwald             hfp_hf_cmd_trigger_codec_connection_setup(hfp_connection->rfcomm_cid);
508ce263fc8SMatthias Ringwald             break;
509ce263fc8SMatthias Ringwald 
5106a7f44bdSMilanka Ringwald          case HFP_CMD_AG_SUGGESTED_CODEC:{
5116a7f44bdSMilanka Ringwald             if (hfp_supports_codec(hfp_connection->suggested_codec, hfp_codecs_nr, hfp_codecs)){
512a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = hfp_connection->suggested_codec;
513a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
514a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_HF_CONFIRMED_CODEC;
5150e0be203SMatthias Ringwald                 hfp_connection->negotiated_codec = hfp_connection->suggested_codec;
5160e0be203SMatthias Ringwald                 log_info("hfp: codec confirmed: %s", hfp_connection->negotiated_codec == HFP_CODEC_MSBC ? "mSBC" : "CVSD");
517fcb08cdbSMilanka Ringwald                 hfp_hf_cmd_confirm_codec(hfp_connection->rfcomm_cid, hfp_connection->codec_confirmed);
518ce263fc8SMatthias Ringwald             } else {
519a0ffb263SMatthias Ringwald                 hfp_connection->codec_confirmed = 0;
520a0ffb263SMatthias Ringwald                 hfp_connection->suggested_codec = 0;
521a0ffb263SMatthias Ringwald                 hfp_connection->negotiated_codec = 0;
522a0ffb263SMatthias Ringwald                 hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
523a0ffb263SMatthias Ringwald                 hfp_connection->ok_pending = 1;
524a0ffb263SMatthias Ringwald                 hfp_hf_cmd_notify_on_codecs(hfp_connection->rfcomm_cid);
525ce263fc8SMatthias Ringwald 
526ce263fc8SMatthias Ringwald             }
527ce263fc8SMatthias Ringwald             break;
5286a7f44bdSMilanka Ringwald         }
529ce263fc8SMatthias Ringwald         default:
530ce263fc8SMatthias Ringwald             break;
531ce263fc8SMatthias Ringwald     }
532ce263fc8SMatthias Ringwald     return 0;
533ce263fc8SMatthias Ringwald }
534ce263fc8SMatthias Ringwald 
535a0ffb263SMatthias Ringwald static int hfp_hf_run_for_audio_connection(hfp_connection_t * hfp_connection){
536a0ffb263SMatthias Ringwald     if (hfp_connection->state < HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED ||
537a0ffb263SMatthias Ringwald         hfp_connection->state > HFP_W2_DISCONNECT_SCO) return 0;
538ce263fc8SMatthias Ringwald 
539ce263fc8SMatthias Ringwald 
540a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED && hfp_connection->release_audio_connection){
541a0ffb263SMatthias Ringwald         hfp_connection->state = HFP_W4_SCO_DISCONNECTED;
542a0ffb263SMatthias Ringwald         hfp_connection->release_audio_connection = 0;
543a0ffb263SMatthias Ringwald         gap_disconnect(hfp_connection->sco_handle);
544ce263fc8SMatthias Ringwald         return 1;
545ce263fc8SMatthias Ringwald     }
546ce263fc8SMatthias Ringwald 
547a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return 0;
548ce263fc8SMatthias Ringwald 
549ce263fc8SMatthias Ringwald     // run codecs exchange
550a0ffb263SMatthias Ringwald     int done = codecs_exchange_state_machine(hfp_connection);
551ce263fc8SMatthias Ringwald     if (done) return 1;
552ce263fc8SMatthias Ringwald 
553ce263fc8SMatthias Ringwald     return 0;
554ce263fc8SMatthias Ringwald }
555ce263fc8SMatthias Ringwald 
556a0ffb263SMatthias Ringwald static int call_setup_state_machine(hfp_connection_t * hfp_connection){
557a0ffb263SMatthias Ringwald     if (hfp_connection->hf_answer_incoming_call){
558a0ffb263SMatthias Ringwald         hfp_hf_cmd_ata(hfp_connection->rfcomm_cid);
559a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 0;
560ce263fc8SMatthias Ringwald         return 1;
561ce263fc8SMatthias Ringwald     }
562ce263fc8SMatthias Ringwald     return 0;
563ce263fc8SMatthias Ringwald }
564ce263fc8SMatthias Ringwald 
565a0ffb263SMatthias Ringwald static void hfp_run_for_context(hfp_connection_t * hfp_connection){
566a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
567724f400dSMatthias Ringwald     if (!hfp_connection->rfcomm_cid) return;
5687522e673SMatthias Ringwald 
56922387625SMatthias Ringwald     if (hfp_connection->local_role != HFP_ROLE_HF) {
57022387625SMatthias Ringwald         log_info("HFP HF%p, wrong role %u", hfp_connection, hfp_connection->local_role);
57122387625SMatthias Ringwald         return;
57222387625SMatthias Ringwald     }
57322387625SMatthias Ringwald 
574b72c4a9eSMatthias Ringwald     if (hfp_connection->hf_accept_sco && hci_can_send_command_packet_now()){
575b72c4a9eSMatthias Ringwald 
576b72c4a9eSMatthias Ringwald         hfp_connection->hf_accept_sco = 0;
5777522e673SMatthias Ringwald 
5787522e673SMatthias Ringwald         // notify about codec selection if not done already
5797522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == 0){
5807522e673SMatthias Ringwald             hfp_connection->negotiated_codec = HFP_CODEC_CVSD;
5817522e673SMatthias Ringwald         }
5827522e673SMatthias Ringwald 
5837522e673SMatthias Ringwald         // remote supported feature eSCO is set if link type is eSCO
5847522e673SMatthias Ringwald         // eSCO: S4 - max latency == transmission interval = 0x000c == 12 ms,
5857522e673SMatthias Ringwald         uint16_t max_latency;
5867522e673SMatthias Ringwald         uint8_t  retransmission_effort;
5877522e673SMatthias Ringwald         uint16_t packet_types;
5887522e673SMatthias Ringwald 
589d9f77559SMatthias Ringwald         if (hci_extended_sco_link_supported() && hci_remote_esco_supported(hfp_connection->acl_handle)){
5907522e673SMatthias Ringwald             max_latency = 0x000c;
5917522e673SMatthias Ringwald             retransmission_effort = 0x02;
5927522e673SMatthias Ringwald             packet_types = 0x388;
5937522e673SMatthias Ringwald         } else {
5947522e673SMatthias Ringwald             max_latency = 0xffff;
5957522e673SMatthias Ringwald             retransmission_effort = 0xff;
5967522e673SMatthias Ringwald             packet_types = 0x003f;
5977522e673SMatthias Ringwald         }
5987522e673SMatthias Ringwald 
5997522e673SMatthias Ringwald         uint16_t sco_voice_setting = hci_get_sco_voice_setting();
6007522e673SMatthias Ringwald         if (hfp_connection->negotiated_codec == HFP_CODEC_MSBC){
6017522e673SMatthias Ringwald             sco_voice_setting = 0x0043; // Transparent data
6027522e673SMatthias Ringwald         }
6037522e673SMatthias Ringwald 
604b72c4a9eSMatthias Ringwald         log_info("HFP: sending hci_accept_connection_request, sco_voice_setting 0x%02x", sco_voice_setting);
6057522e673SMatthias Ringwald         hci_send_cmd(&hci_accept_synchronous_connection, hfp_connection->remote_addr, 8000, 8000, max_latency,
6067522e673SMatthias Ringwald                         sco_voice_setting, retransmission_effort, packet_types);
6077522e673SMatthias Ringwald         return;
6087522e673SMatthias Ringwald     }
6097522e673SMatthias Ringwald 
610d4dd47ffSMatthias Ringwald     if (!rfcomm_can_send_packet_now(hfp_connection->rfcomm_cid)) {
611d4dd47ffSMatthias Ringwald         rfcomm_request_can_send_now_event(hfp_connection->rfcomm_cid);
612d4dd47ffSMatthias Ringwald         return;
613d4dd47ffSMatthias Ringwald     }
614a0ffb263SMatthias Ringwald     int done = hfp_hf_run_for_context_service_level_connection(hfp_connection);
615ce263fc8SMatthias Ringwald     if (!done){
616a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_context_service_level_connection_queries(hfp_connection);
617ce263fc8SMatthias Ringwald     }
618ce263fc8SMatthias Ringwald     if (!done){
619a0ffb263SMatthias Ringwald         done = hfp_hf_run_for_audio_connection(hfp_connection);
620ce263fc8SMatthias Ringwald     }
621ce263fc8SMatthias Ringwald     if (!done){
622a0ffb263SMatthias Ringwald         done = call_setup_state_machine(hfp_connection);
623ce263fc8SMatthias Ringwald     }
624ce263fc8SMatthias Ringwald 
6251016a228SMatthias Ringwald     // don't send a new command while ok still pending
6261016a228SMatthias Ringwald     if (hfp_connection->ok_pending) return;
6271016a228SMatthias Ringwald 
628a0ffb263SMatthias Ringwald     if (hfp_connection->send_microphone_gain){
629a0ffb263SMatthias Ringwald         hfp_connection->send_microphone_gain = 0;
630a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
631a0ffb263SMatthias Ringwald         hfp_hf_set_microphone_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->microphone_gain);
632ce263fc8SMatthias Ringwald         return;
633ce263fc8SMatthias Ringwald     }
634ce263fc8SMatthias Ringwald 
635a0ffb263SMatthias Ringwald     if (hfp_connection->send_speaker_gain){
636a0ffb263SMatthias Ringwald         hfp_connection->send_speaker_gain = 0;
637a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
638a0ffb263SMatthias Ringwald         hfp_hf_set_speaker_gain_cmd(hfp_connection->rfcomm_cid, hfp_connection->speaker_gain);
639ce263fc8SMatthias Ringwald         return;
640ce263fc8SMatthias Ringwald     }
641ce263fc8SMatthias Ringwald 
642a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_calling_line_notification){
643a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_calling_line_notification = 0;
644a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
645a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 0);
646ce263fc8SMatthias Ringwald         return;
647ce263fc8SMatthias Ringwald     }
648ce263fc8SMatthias Ringwald 
649a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_calling_line_notification){
650a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_calling_line_notification = 0;
651a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
652a0ffb263SMatthias Ringwald         hfp_hf_set_calling_line_notification_cmd(hfp_connection->rfcomm_cid, 1);
653ce263fc8SMatthias Ringwald         return;
654ce263fc8SMatthias Ringwald     }
655ce263fc8SMatthias Ringwald 
656a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction){
657a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 0;
658a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
659a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 0);
660ce263fc8SMatthias Ringwald         return;
661ce263fc8SMatthias Ringwald     }
662ce263fc8SMatthias Ringwald 
663a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_echo_canceling_and_noise_reduction){
664a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 0;
665a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
666a0ffb263SMatthias Ringwald         hfp_hf_set_echo_canceling_and_noise_reduction_cmd(hfp_connection->rfcomm_cid, 1);
667ce263fc8SMatthias Ringwald         return;
668ce263fc8SMatthias Ringwald     }
669ce263fc8SMatthias Ringwald 
670a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_voice_recognition_notification){
671a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_voice_recognition_notification = 0;
672a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
673a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 0);
674ce263fc8SMatthias Ringwald         return;
675ce263fc8SMatthias Ringwald     }
676ce263fc8SMatthias Ringwald 
677a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_voice_recognition_notification){
678a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_voice_recognition_notification = 0;
679a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
680a0ffb263SMatthias Ringwald         hfp_hf_set_voice_recognition_notification_cmd(hfp_connection->rfcomm_cid, 1);
681ce263fc8SMatthias Ringwald         return;
682ce263fc8SMatthias Ringwald     }
683ce263fc8SMatthias Ringwald 
684ce263fc8SMatthias Ringwald 
685a0ffb263SMatthias Ringwald     if (hfp_connection->hf_deactivate_call_waiting_notification){
686a0ffb263SMatthias Ringwald         hfp_connection->hf_deactivate_call_waiting_notification = 0;
687a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
688a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 0);
689ce263fc8SMatthias Ringwald         return;
690ce263fc8SMatthias Ringwald     }
691ce263fc8SMatthias Ringwald 
692a0ffb263SMatthias Ringwald     if (hfp_connection->hf_activate_call_waiting_notification){
693a0ffb263SMatthias Ringwald         hfp_connection->hf_activate_call_waiting_notification = 0;
694a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
695a0ffb263SMatthias Ringwald         hfp_hf_set_call_waiting_notification_cmd(hfp_connection->rfcomm_cid, 1);
696ce263fc8SMatthias Ringwald         return;
697ce263fc8SMatthias Ringwald     }
698ce263fc8SMatthias Ringwald 
699a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_outgoing_call){
700a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_outgoing_call = 0;
701a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
702a0ffb263SMatthias Ringwald         hfp_hf_initiate_outgoing_call_cmd(hfp_connection->rfcomm_cid);
703ce263fc8SMatthias Ringwald         return;
704ce263fc8SMatthias Ringwald     }
705ce263fc8SMatthias Ringwald 
706a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_memory_dialing){
707a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_memory_dialing = 0;
708a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
709a0ffb263SMatthias Ringwald         hfp_hf_send_memory_dial_cmd(hfp_connection->rfcomm_cid, hfp_connection->memory_id);
710ce263fc8SMatthias Ringwald         return;
711ce263fc8SMatthias Ringwald     }
712ce263fc8SMatthias Ringwald 
713a0ffb263SMatthias Ringwald     if (hfp_connection->hf_initiate_redial_last_number){
714a0ffb263SMatthias Ringwald         hfp_connection->hf_initiate_redial_last_number = 0;
715a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
716a0ffb263SMatthias Ringwald         hfp_hf_send_redial_last_number_cmd(hfp_connection->rfcomm_cid);
717ce263fc8SMatthias Ringwald         return;
718ce263fc8SMatthias Ringwald     }
719ce263fc8SMatthias Ringwald 
720a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chup){
721a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 0;
722a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
723a0ffb263SMatthias Ringwald         hfp_hf_send_chup(hfp_connection->rfcomm_cid);
724ce263fc8SMatthias Ringwald         return;
725ce263fc8SMatthias Ringwald     }
726ce263fc8SMatthias Ringwald 
727a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_0){
728a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 0;
729a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
730a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 0);
731ce263fc8SMatthias Ringwald         return;
732ce263fc8SMatthias Ringwald     }
733ce263fc8SMatthias Ringwald 
734a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_1){
735a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 0;
736a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
737a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 1);
738ce263fc8SMatthias Ringwald         return;
739ce263fc8SMatthias Ringwald     }
740ce263fc8SMatthias Ringwald 
741a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_2){
742a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 0;
743a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
744a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 2);
745ce263fc8SMatthias Ringwald         return;
746ce263fc8SMatthias Ringwald     }
747ce263fc8SMatthias Ringwald 
748a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_3){
749a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 0;
750a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
751a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 3);
752ce263fc8SMatthias Ringwald         return;
753ce263fc8SMatthias Ringwald     }
754ce263fc8SMatthias Ringwald 
755a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_4){
756a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 0;
757a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
758a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, 4);
759ce263fc8SMatthias Ringwald         return;
760ce263fc8SMatthias Ringwald     }
761ce263fc8SMatthias Ringwald 
762a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_chld_x){
763a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 0;
764a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
765a0ffb263SMatthias Ringwald         hfp_hf_send_chld(hfp_connection->rfcomm_cid, hfp_connection->hf_send_chld_x_index);
766667ec068SMatthias Ringwald         return;
767667ec068SMatthias Ringwald     }
768667ec068SMatthias Ringwald 
769a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_dtmf_code){
770a0ffb263SMatthias Ringwald         char code = hfp_connection->hf_send_dtmf_code;
771a0ffb263SMatthias Ringwald         hfp_connection->hf_send_dtmf_code = 0;
772a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
773a0ffb263SMatthias Ringwald         hfp_hf_send_dtmf(hfp_connection->rfcomm_cid, code);
774ce263fc8SMatthias Ringwald         return;
775ce263fc8SMatthias Ringwald     }
776ce263fc8SMatthias Ringwald 
777a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_binp){
778a0ffb263SMatthias Ringwald         hfp_connection->hf_send_binp = 0;
779a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
780a0ffb263SMatthias Ringwald         hfp_hf_send_binp(hfp_connection->rfcomm_cid);
781ce263fc8SMatthias Ringwald         return;
782ce263fc8SMatthias Ringwald     }
783ce263fc8SMatthias Ringwald 
784a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_clcc){
785a0ffb263SMatthias Ringwald         hfp_connection->hf_send_clcc = 0;
786a0ffb263SMatthias Ringwald         hfp_connection->ok_pending = 1;
787a0ffb263SMatthias Ringwald         hfp_hf_send_clcc(hfp_connection->rfcomm_cid);
788667ec068SMatthias Ringwald         return;
789667ec068SMatthias Ringwald     }
790667ec068SMatthias Ringwald 
791a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_rrh){
792a0ffb263SMatthias Ringwald         hfp_connection->hf_send_rrh = 0;
793667ec068SMatthias Ringwald         char buffer[20];
794a0ffb263SMatthias Ringwald         switch (hfp_connection->hf_send_rrh_command){
795667ec068SMatthias Ringwald             case '?':
796667ec068SMatthias Ringwald                 sprintf(buffer, "AT%s?\r\n", HFP_RESPONSE_AND_HOLD);
797a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
798667ec068SMatthias Ringwald                 return;
799667ec068SMatthias Ringwald             case '0':
800667ec068SMatthias Ringwald             case '1':
801667ec068SMatthias Ringwald             case '2':
802a0ffb263SMatthias Ringwald                 sprintf(buffer, "AT%s=%c\r\n", HFP_RESPONSE_AND_HOLD, hfp_connection->hf_send_rrh_command);
803a0ffb263SMatthias Ringwald                 send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
804667ec068SMatthias Ringwald                 return;
805667ec068SMatthias Ringwald             default:
806667ec068SMatthias Ringwald                 break;
807667ec068SMatthias Ringwald         }
808667ec068SMatthias Ringwald         return;
809667ec068SMatthias Ringwald     }
810667ec068SMatthias Ringwald 
811a0ffb263SMatthias Ringwald     if (hfp_connection->hf_send_cnum){
812a0ffb263SMatthias Ringwald         hfp_connection->hf_send_cnum = 0;
813667ec068SMatthias Ringwald         char buffer[20];
814667ec068SMatthias Ringwald         sprintf(buffer, "AT%s\r\n", HFP_SUBSCRIBER_NUMBER_INFORMATION);
815a0ffb263SMatthias Ringwald         send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
816667ec068SMatthias Ringwald         return;
817667ec068SMatthias Ringwald     }
818667ec068SMatthias Ringwald 
819667ec068SMatthias Ringwald     // update HF indicators
820a0ffb263SMatthias Ringwald     if (hfp_connection->generic_status_update_bitmap){
821667ec068SMatthias Ringwald         int i;
822667ec068SMatthias Ringwald         for (i=0;i<hfp_indicators_nr;i++){
823a0ffb263SMatthias Ringwald             if (get_bit(hfp_connection->generic_status_update_bitmap, i)){
824a0ffb263SMatthias Ringwald                 if (hfp_connection->generic_status_indicators[i].state){
825a0ffb263SMatthias Ringwald                     hfp_connection->ok_pending = 1;
826a0ffb263SMatthias Ringwald                     hfp_connection->generic_status_update_bitmap = store_bit(hfp_connection->generic_status_update_bitmap, i, 0);
827667ec068SMatthias Ringwald                     char buffer[30];
828ecb7d461SMatthias Ringwald                     sprintf(buffer, "AT%s=%u,%u\r\n", HFP_TRANSFER_HF_INDICATOR_STATUS, hfp_indicators[i], (unsigned int) hfp_indicators_value[i]);
829a0ffb263SMatthias Ringwald                     send_str_over_rfcomm(hfp_connection->rfcomm_cid, buffer);
830667ec068SMatthias Ringwald                 } else {
83160ebb071SMilanka Ringwald                     log_info("Not sending HF indicator %u as it is disabled", hfp_indicators[i]);
832667ec068SMatthias Ringwald                 }
833667ec068SMatthias Ringwald                 return;
834667ec068SMatthias Ringwald             }
835667ec068SMatthias Ringwald         }
836667ec068SMatthias Ringwald     }
837667ec068SMatthias Ringwald 
838ce263fc8SMatthias Ringwald     if (done) return;
839ce263fc8SMatthias Ringwald     // deal with disconnect
840a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
841ce263fc8SMatthias Ringwald         case HFP_W2_DISCONNECT_RFCOMM:
842a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_W4_RFCOMM_DISCONNECTED;
843a0ffb263SMatthias Ringwald             rfcomm_disconnect(hfp_connection->rfcomm_cid);
844ce263fc8SMatthias Ringwald             break;
845ce263fc8SMatthias Ringwald 
846ce263fc8SMatthias Ringwald         default:
847ce263fc8SMatthias Ringwald             break;
848ce263fc8SMatthias Ringwald     }
849ce263fc8SMatthias Ringwald }
850ce263fc8SMatthias Ringwald 
851a0ffb263SMatthias Ringwald static void hfp_ag_slc_established(hfp_connection_t * hfp_connection){
852a0ffb263SMatthias Ringwald     hfp_connection->state = HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED;
8536a7f44bdSMilanka Ringwald 
854ca59be51SMatthias Ringwald     hfp_emit_slc_connection_event(hfp_connection, 0, hfp_connection->acl_handle, hfp_connection->remote_addr);
8557522e673SMatthias Ringwald 
856667ec068SMatthias Ringwald     // restore volume settings
857a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = hfp_hf_speaker_gain;
858a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
859ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, hfp_hf_speaker_gain);
860a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = hfp_hf_microphone_gain;
861a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
862ca59be51SMatthias Ringwald     hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, hfp_hf_microphone_gain);
863667ec068SMatthias Ringwald     // enable all indicators
864667ec068SMatthias Ringwald     int i;
865667ec068SMatthias Ringwald     for (i=0;i<hfp_indicators_nr;i++){
866a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].uuid = hfp_indicators[i];
867a0ffb263SMatthias Ringwald         hfp_connection->generic_status_indicators[i].state = 1;
868667ec068SMatthias Ringwald     }
869ce263fc8SMatthias Ringwald }
870ce263fc8SMatthias Ringwald 
871a0ffb263SMatthias Ringwald static void hfp_hf_switch_on_ok(hfp_connection_t *hfp_connection){
872a0ffb263SMatthias Ringwald     hfp_connection->ok_pending = 0;
873a0ffb263SMatthias Ringwald     switch (hfp_connection->state){
8743deb3ec6SMatthias Ringwald         case HFP_W4_EXCHANGE_SUPPORTED_FEATURES:
875a0ffb263SMatthias Ringwald             if (has_codec_negotiation_feature(hfp_connection)){
876a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_NOTIFY_ON_CODECS;
8773deb3ec6SMatthias Ringwald                 break;
8783deb3ec6SMatthias Ringwald             }
879a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8803deb3ec6SMatthias Ringwald             break;
8813deb3ec6SMatthias Ringwald 
8823deb3ec6SMatthias Ringwald         case HFP_W4_NOTIFY_ON_CODECS:
883a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS;
8843deb3ec6SMatthias Ringwald             break;
8853deb3ec6SMatthias Ringwald 
8863deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS:
887a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INDICATORS_STATUS;
8883deb3ec6SMatthias Ringwald             break;
8893deb3ec6SMatthias Ringwald 
8903deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INDICATORS_STATUS:
891a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_ENABLE_INDICATORS_STATUS_UPDATE;
8923deb3ec6SMatthias Ringwald             break;
8933deb3ec6SMatthias Ringwald 
8943deb3ec6SMatthias Ringwald         case HFP_W4_ENABLE_INDICATORS_STATUS_UPDATE:
895a0ffb263SMatthias Ringwald             if (has_call_waiting_and_3way_calling_feature(hfp_connection)){
896a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_RETRIEVE_CAN_HOLD_CALL;
8973deb3ec6SMatthias Ringwald                 break;
8983deb3ec6SMatthias Ringwald             }
899a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
900a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9013deb3ec6SMatthias Ringwald                 break;
9023deb3ec6SMatthias Ringwald             }
903a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9043deb3ec6SMatthias Ringwald             break;
9053deb3ec6SMatthias Ringwald 
9063deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_CAN_HOLD_CALL:
907a0ffb263SMatthias Ringwald             if (has_hf_indicators_feature(hfp_connection)){
908a0ffb263SMatthias Ringwald                 hfp_connection->state = HFP_LIST_GENERIC_STATUS_INDICATORS;
9093deb3ec6SMatthias Ringwald                 break;
9103deb3ec6SMatthias Ringwald             }
911a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9123deb3ec6SMatthias Ringwald             break;
9133deb3ec6SMatthias Ringwald 
9143deb3ec6SMatthias Ringwald         case HFP_W4_LIST_GENERIC_STATUS_INDICATORS:
915a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_GENERIC_STATUS_INDICATORS;
9163deb3ec6SMatthias Ringwald             break;
9173deb3ec6SMatthias Ringwald 
9183deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_GENERIC_STATUS_INDICATORS:
919a0ffb263SMatthias Ringwald             hfp_connection->state = HFP_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS;
9203deb3ec6SMatthias Ringwald             break;
9213deb3ec6SMatthias Ringwald 
9223deb3ec6SMatthias Ringwald         case HFP_W4_RETRIEVE_INITITAL_STATE_GENERIC_STATUS_INDICATORS:
923a0ffb263SMatthias Ringwald             hfp_ag_slc_established(hfp_connection);
9243deb3ec6SMatthias Ringwald             break;
925ce263fc8SMatthias Ringwald         case HFP_SERVICE_LEVEL_CONNECTION_ESTABLISHED:
926a0ffb263SMatthias Ringwald             if (hfp_connection->enable_status_update_for_ag_indicators != 0xFF){
927a0ffb263SMatthias Ringwald                 hfp_connection->enable_status_update_for_ag_indicators = 0xFF;
928ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
929ce263fc8SMatthias Ringwald                 break;
930ce263fc8SMatthias Ringwald             }
9313deb3ec6SMatthias Ringwald 
932a0ffb263SMatthias Ringwald             if (hfp_connection->change_status_update_for_individual_ag_indicators == 1){
933a0ffb263SMatthias Ringwald                 hfp_connection->change_status_update_for_individual_ag_indicators = 0;
934ca59be51SMatthias Ringwald                 hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 0);
935ce263fc8SMatthias Ringwald                 break;
9363deb3ec6SMatthias Ringwald             }
9373deb3ec6SMatthias Ringwald 
938a0ffb263SMatthias Ringwald             switch (hfp_connection->hf_query_operator_state){
939ce263fc8SMatthias Ringwald                 case HFP_HF_QUERY_OPERATOR_W4_SET_FORMAT_OK:
94060ebb071SMilanka Ringwald                     // printf("Format set, querying name\n");
941a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
942ce263fc8SMatthias Ringwald                     break;
943ce263fc8SMatthias Ringwald                 case HPF_HF_QUERY_OPERATOR_W4_RESULT:
944a0ffb263SMatthias Ringwald                     hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_FORMAT_SET;
945ca59be51SMatthias Ringwald                     hfp_emit_network_operator_event(hfp_hf_callback, hfp_connection->network_operator);
946ce263fc8SMatthias Ringwald                     break;
947ce263fc8SMatthias Ringwald                 default:
948ce263fc8SMatthias Ringwald                     break;
9493deb3ec6SMatthias Ringwald             }
950ce263fc8SMatthias Ringwald 
951a0ffb263SMatthias Ringwald             if (hfp_connection->enable_extended_audio_gateway_error_report){
952a0ffb263SMatthias Ringwald                 hfp_connection->enable_extended_audio_gateway_error_report = 0;
953ce263fc8SMatthias Ringwald                 break;
9543deb3ec6SMatthias Ringwald             }
9553deb3ec6SMatthias Ringwald 
956a0ffb263SMatthias Ringwald             switch (hfp_connection->codecs_state){
957aa4dd815SMatthias Ringwald                 case HFP_CODECS_RECEIVED_TRIGGER_CODEC_EXCHANGE:
958a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_W4_AG_COMMON_CODEC;
9593deb3ec6SMatthias Ringwald                     break;
960ce263fc8SMatthias Ringwald                 case HFP_CODECS_HF_CONFIRMED_CODEC:
961a0ffb263SMatthias Ringwald                     hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
962ce263fc8SMatthias Ringwald                     break;
9633deb3ec6SMatthias Ringwald                 default:
9643deb3ec6SMatthias Ringwald                     break;
9653deb3ec6SMatthias Ringwald             }
9663deb3ec6SMatthias Ringwald             break;
9673deb3ec6SMatthias Ringwald         default:
9683deb3ec6SMatthias Ringwald             break;
9693deb3ec6SMatthias Ringwald     }
9703deb3ec6SMatthias Ringwald 
9713deb3ec6SMatthias Ringwald     // done
972a0ffb263SMatthias Ringwald     hfp_connection->command = HFP_CMD_NONE;
9733deb3ec6SMatthias Ringwald }
9743deb3ec6SMatthias Ringwald 
975e9c22d4eSMatthias Ringwald static void hfp_hf_handle_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
9768a46ec40SMatthias Ringwald     UNUSED(packet_type);    // ok: only called with RFCOMM_DATA_PACKET
9778a46ec40SMatthias Ringwald 
9788a46ec40SMatthias Ringwald     // assertion: size >= 1 as rfcomm.c does not deliver empty packets
9798a46ec40SMatthias Ringwald     if (size < 1) return;
9809ec2630cSMatthias Ringwald 
981a0ffb263SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_connection_context_for_rfcomm_cid(channel);
982a0ffb263SMatthias Ringwald     if (!hfp_connection) return;
9833deb3ec6SMatthias Ringwald 
984186dd3d2SMatthias Ringwald     hfp_log_rfcomm_message("HFP_HF_RX", packet, size);
9851e35c04dSMatthias Ringwald 
9868a46ec40SMatthias Ringwald     // process messages byte-wise
987186dd3d2SMatthias Ringwald     int pos;
9883deb3ec6SMatthias Ringwald     for (pos = 0; pos < size ; pos++){
989a0ffb263SMatthias Ringwald         hfp_parse(hfp_connection, packet[pos], 1);
990ce263fc8SMatthias Ringwald     }
9913deb3ec6SMatthias Ringwald 
992186dd3d2SMatthias Ringwald     int value;
993186dd3d2SMatthias Ringwald     int i;
994a0ffb263SMatthias Ringwald     switch (hfp_connection->command){
995667ec068SMatthias Ringwald         case HFP_CMD_GET_SUBSCRIBER_NUMBER_INFORMATION:
996a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
997a0ffb263SMatthias Ringwald             // printf("Subscriber Number: number %s, type %u\n", hfp_connection->bnip_number, hfp_connection->bnip_type);
998ca59be51SMatthias Ringwald             hfp_hf_emit_subscriber_information(hfp_hf_callback, HFP_SUBEVENT_SUBSCRIBER_NUMBER_INFORMATION, 0, hfp_connection->bnip_type, hfp_connection->bnip_number);
999667ec068SMatthias Ringwald             break;
1000667ec068SMatthias Ringwald         case HFP_CMD_RESPONSE_AND_HOLD_STATUS:
1001a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1002a0ffb263SMatthias Ringwald             // printf("Response and Hold status: %s\n", hfp_connection->line_buffer);
1003ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_RESPONSE_AND_HOLD_STATUS, btstack_atoi((char *)&hfp_connection->line_buffer[0]));
1004667ec068SMatthias Ringwald             break;
1005667ec068SMatthias Ringwald         case HFP_CMD_LIST_CURRENT_CALLS:
1006a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1007a0ffb263SMatthias Ringwald             // printf("Enhanced Call Status: idx %u, dir %u, status %u, mpty %u, number %s, type %u\n",
1008a0ffb263SMatthias Ringwald             //      hfp_connection->clcc_idx, hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
1009a0ffb263SMatthias Ringwald             //      hfp_connection->bnip_number, hfp_connection->bnip_type);
1010ca59be51SMatthias Ringwald             hfp_hf_emit_enhanced_call_status(hfp_hf_callback, hfp_connection->clcc_idx,
1011a0ffb263SMatthias Ringwald                 hfp_connection->clcc_dir, hfp_connection->clcc_status, hfp_connection->clcc_mpty,
1012a0ffb263SMatthias Ringwald                 hfp_connection->bnip_type, hfp_connection->bnip_number);
1013667ec068SMatthias Ringwald             break;
1014ce263fc8SMatthias Ringwald         case HFP_CMD_SET_SPEAKER_GAIN:
1015a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10162308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1017667ec068SMatthias Ringwald             hfp_hf_speaker_gain = value;
1018ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_SPEAKER_VOLUME, value);
1019ce263fc8SMatthias Ringwald             break;
1020ce263fc8SMatthias Ringwald         case HFP_CMD_SET_MICROPHONE_GAIN:
1021a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
10222308e108SMilanka Ringwald             value = btstack_atoi((char*)hfp_connection->line_buffer);
1023667ec068SMatthias Ringwald             hfp_hf_microphone_gain = value;
1024ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_MICROPHONE_VOLUME, value);
1025ce263fc8SMatthias Ringwald             break;
1026ce263fc8SMatthias Ringwald         case HFP_CMD_AG_SENT_PHONE_NUMBER:
1027a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1028ca59be51SMatthias Ringwald             hfp_emit_string_event(hfp_connection, HFP_SUBEVENT_NUMBER_FOR_VOICE_TAG, hfp_connection->bnip_number);
1029a0ffb263SMatthias Ringwald             break;
1030a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CALL_WAITING_NOTIFICATION_UPDATE:
1031a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1032ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALL_WAITING_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1033a0ffb263SMatthias Ringwald             break;
1034a0ffb263SMatthias Ringwald         case HFP_CMD_AG_SENT_CLIP_INFORMATION:
1035a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1036ca59be51SMatthias Ringwald             hfp_hf_emit_type_and_number(hfp_hf_callback, HFP_SUBEVENT_CALLING_LINE_IDENTIFICATION_NOTIFICATION, hfp_connection->bnip_type, hfp_connection->bnip_number);
1037ce263fc8SMatthias Ringwald             break;
1038ce263fc8SMatthias Ringwald         case HFP_CMD_EXTENDED_AUDIO_GATEWAY_ERROR:
1039a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1040a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1041a0ffb263SMatthias Ringwald             hfp_connection->extended_audio_gateway_error = 0;
1042ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_EXTENDED_AUDIO_GATEWAY_ERROR, hfp_connection->extended_audio_gateway_error_value);
1043ce263fc8SMatthias Ringwald             break;
1044ce263fc8SMatthias Ringwald         case HFP_CMD_ERROR:
1045a0ffb263SMatthias Ringwald             hfp_connection->ok_pending = 0;
1046a0ffb263SMatthias Ringwald             hfp_reset_context_flags(hfp_connection);
1047a0ffb263SMatthias Ringwald             hfp_connection->command = HFP_CMD_NONE;
1048ca59be51SMatthias Ringwald             hfp_emit_event(hfp_connection, HFP_SUBEVENT_COMPLETE, 1);
1049ce263fc8SMatthias Ringwald             break;
1050ce263fc8SMatthias Ringwald         case HFP_CMD_OK:
1051a0ffb263SMatthias Ringwald             hfp_hf_switch_on_ok(hfp_connection);
1052ce263fc8SMatthias Ringwald             break;
1053ce263fc8SMatthias Ringwald         case HFP_CMD_RING:
1054ca59be51SMatthias Ringwald             hfp_emit_simple_event(hfp_connection, HFP_SUBEVENT_RING);
1055ce263fc8SMatthias Ringwald             break;
1056ce263fc8SMatthias Ringwald         case HFP_CMD_TRANSFER_AG_INDICATOR_STATUS:
1057a0ffb263SMatthias Ringwald             for (i = 0; i < hfp_connection->ag_indicators_nr; i++){
1058a0ffb263SMatthias Ringwald                 if (hfp_connection->ag_indicators[i].status_changed) {
1059a0ffb263SMatthias Ringwald                     if (strcmp(hfp_connection->ag_indicators[i].name, "callsetup") == 0){
1060a0ffb263SMatthias Ringwald                         hfp_callsetup_status = (hfp_callsetup_status_t) hfp_connection->ag_indicators[i].status;
1061a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "callheld") == 0){
1062a0ffb263SMatthias Ringwald                         hfp_callheld_status = (hfp_callheld_status_t) hfp_connection->ag_indicators[i].status;
1063d5ff3b26SMatthias Ringwald                         // avoid set but not used warning
1064d5ff3b26SMatthias Ringwald                         (void) hfp_callheld_status;
1065a0ffb263SMatthias Ringwald                     } else if (strcmp(hfp_connection->ag_indicators[i].name, "call") == 0){
1066a0ffb263SMatthias Ringwald                         hfp_call_status = (hfp_call_status_t) hfp_connection->ag_indicators[i].status;
1067ce263fc8SMatthias Ringwald                     }
1068a0ffb263SMatthias Ringwald                     hfp_connection->ag_indicators[i].status_changed = 0;
1069ca59be51SMatthias Ringwald                     hfp_emit_ag_indicator_event(hfp_hf_callback, hfp_connection->ag_indicators[i]);
10703deb3ec6SMatthias Ringwald                     break;
10713deb3ec6SMatthias Ringwald                 }
10723deb3ec6SMatthias Ringwald             }
1073ce263fc8SMatthias Ringwald             break;
1074ce263fc8SMatthias Ringwald         default:
1075ce263fc8SMatthias Ringwald             break;
10763deb3ec6SMatthias Ringwald     }
1077a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
10783deb3ec6SMatthias Ringwald }
10793deb3ec6SMatthias Ringwald 
10800cb5b971SMatthias Ringwald static void hfp_run(void){
1081665d90f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
1082665d90f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, hfp_get_connections());
1083665d90f2SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1084a0ffb263SMatthias Ringwald         hfp_connection_t * hfp_connection = (hfp_connection_t *)btstack_linked_list_iterator_next(&it);
108522387625SMatthias Ringwald         if (hfp_connection->local_role != HFP_ROLE_HF) continue;
1086a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
10873deb3ec6SMatthias Ringwald     }
10883deb3ec6SMatthias Ringwald }
10893deb3ec6SMatthias Ringwald 
1090e9c22d4eSMatthias Ringwald static void rfcomm_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
10913deb3ec6SMatthias Ringwald     switch (packet_type){
10923deb3ec6SMatthias Ringwald         case RFCOMM_DATA_PACKET:
1093e9c22d4eSMatthias Ringwald             hfp_hf_handle_rfcomm_event(packet_type, channel, packet, size);
10943deb3ec6SMatthias Ringwald             break;
10953deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1096d4dd47ffSMatthias Ringwald             if (packet[0] == RFCOMM_EVENT_CAN_SEND_NOW){
1097d4dd47ffSMatthias Ringwald                 uint16_t rfcomm_cid = rfcomm_event_can_send_now_get_rfcomm_cid(packet);
1098d4dd47ffSMatthias Ringwald                 hfp_run_for_context(get_hfp_connection_context_for_rfcomm_cid(rfcomm_cid));
1099d4dd47ffSMatthias Ringwald                 return;
1100d4dd47ffSMatthias Ringwald             }
110127950165SMatthias Ringwald             hfp_handle_rfcomm_event(packet_type, channel, packet, size, HFP_ROLE_HF);
1102202c8a4cSMatthias Ringwald             break;
11033deb3ec6SMatthias Ringwald         default:
11043deb3ec6SMatthias Ringwald             break;
11053deb3ec6SMatthias Ringwald     }
11063deb3ec6SMatthias Ringwald     hfp_run();
11073deb3ec6SMatthias Ringwald }
11083deb3ec6SMatthias Ringwald 
1109a0ffb263SMatthias Ringwald void hfp_hf_init(uint16_t rfcomm_channel_nr){
1110520c92d5SMatthias Ringwald     hfp_init();
1111d63c37a1SMatthias Ringwald 
111227950165SMatthias Ringwald     hci_event_callback_registration.callback = &hfp_handle_hci_event;
111327950165SMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
111427950165SMatthias Ringwald 
1115e9c22d4eSMatthias Ringwald     rfcomm_register_service(rfcomm_packet_handler, rfcomm_channel_nr, 0xffff);
111627950165SMatthias Ringwald 
111727950165SMatthias Ringwald     // used to set packet handler for outgoing rfcomm connections - could be handled by emitting an event to us
1118e9c22d4eSMatthias Ringwald     hfp_set_hf_rfcomm_packet_handler(&rfcomm_packet_handler);
111927950165SMatthias Ringwald 
11201b005648SMatthias Ringwald     hfp_set_hf_run_for_context(hfp_run_for_context);
1121d68dcce1SMatthias Ringwald 
1122a0ffb263SMatthias Ringwald     hfp_supported_features = HFP_DEFAULT_HF_SUPPORTED_FEATURES;
1123a0ffb263SMatthias Ringwald     hfp_codecs_nr = 0;
1124a0ffb263SMatthias Ringwald     hfp_indicators_nr = 0;
1125a0ffb263SMatthias Ringwald     hfp_hf_speaker_gain = 9;
1126a0ffb263SMatthias Ringwald     hfp_hf_microphone_gain = 9;
1127a0ffb263SMatthias Ringwald }
1128a0ffb263SMatthias Ringwald 
1129a0ffb263SMatthias Ringwald void hfp_hf_init_codecs(int codecs_nr, uint8_t * codecs){
11303deb3ec6SMatthias Ringwald     if (codecs_nr > HFP_MAX_NUM_CODECS){
1131a0ffb263SMatthias Ringwald         log_error("hfp_hf_init_codecs: codecs_nr (%d) > HFP_MAX_NUM_CODECS (%d)", codecs_nr, HFP_MAX_NUM_CODECS);
11323deb3ec6SMatthias Ringwald         return;
11333deb3ec6SMatthias Ringwald     }
11343deb3ec6SMatthias Ringwald 
11353deb3ec6SMatthias Ringwald     hfp_codecs_nr = codecs_nr;
11363deb3ec6SMatthias Ringwald     int i;
11373deb3ec6SMatthias Ringwald     for (i=0; i<codecs_nr; i++){
11383deb3ec6SMatthias Ringwald         hfp_codecs[i] = codecs[i];
11393deb3ec6SMatthias Ringwald     }
11403deb3ec6SMatthias Ringwald }
11413deb3ec6SMatthias Ringwald 
1142a0ffb263SMatthias Ringwald void hfp_hf_init_supported_features(uint32_t supported_features){
11433deb3ec6SMatthias Ringwald     hfp_supported_features = supported_features;
1144a0ffb263SMatthias Ringwald }
11453deb3ec6SMatthias Ringwald 
1146a0ffb263SMatthias Ringwald void hfp_hf_init_hf_indicators(int indicators_nr, uint16_t * indicators){
11473deb3ec6SMatthias Ringwald     hfp_indicators_nr = indicators_nr;
11483deb3ec6SMatthias Ringwald     int i;
1149a0ffb263SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
11503deb3ec6SMatthias Ringwald         hfp_indicators[i] = indicators[i];
11513deb3ec6SMatthias Ringwald     }
11523deb3ec6SMatthias Ringwald }
11533deb3ec6SMatthias Ringwald 
11543deb3ec6SMatthias Ringwald void hfp_hf_establish_service_level_connection(bd_addr_t bd_addr){
1155323d3000SMatthias Ringwald     hfp_establish_service_level_connection(bd_addr, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY, HFP_ROLE_HF);
11563deb3ec6SMatthias Ringwald }
11573deb3ec6SMatthias Ringwald 
1158c8626498SMilanka Ringwald void hfp_hf_release_service_level_connection(hci_con_handle_t acl_handle){
11599c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1160a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1161a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1162a33eb0c4SMilanka Ringwald         return;
1163a33eb0c4SMilanka Ringwald     }
1164a0ffb263SMatthias Ringwald     hfp_release_service_level_connection(hfp_connection);
1165a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11663deb3ec6SMatthias Ringwald }
11673deb3ec6SMatthias Ringwald 
1168c8626498SMilanka Ringwald static void hfp_hf_set_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle, uint8_t enable){
11699c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1170a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1171a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11723deb3ec6SMatthias Ringwald         return;
11733deb3ec6SMatthias Ringwald     }
1174a0ffb263SMatthias Ringwald     hfp_connection->enable_status_update_for_ag_indicators = enable;
1175a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11763deb3ec6SMatthias Ringwald }
11773deb3ec6SMatthias Ringwald 
1178c8626498SMilanka Ringwald void hfp_hf_enable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1179c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 1);
1180ce263fc8SMatthias Ringwald }
1181ce263fc8SMatthias Ringwald 
1182c8626498SMilanka Ringwald void hfp_hf_disable_status_update_for_all_ag_indicators(hci_con_handle_t acl_handle){
1183c8626498SMilanka Ringwald     hfp_hf_set_status_update_for_all_ag_indicators(acl_handle, 0);
1184ce263fc8SMatthias Ringwald }
1185ce263fc8SMatthias Ringwald 
11863deb3ec6SMatthias Ringwald // TODO: returned ERROR - wrong format
1187c8626498SMilanka Ringwald void hfp_hf_set_status_update_for_individual_ag_indicators(hci_con_handle_t acl_handle, uint32_t indicators_status_bitmap){
11889c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1189a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1190a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
11913deb3ec6SMatthias Ringwald         return;
11923deb3ec6SMatthias Ringwald     }
1193a0ffb263SMatthias Ringwald     hfp_connection->change_status_update_for_individual_ag_indicators = 1;
1194a0ffb263SMatthias Ringwald     hfp_connection->ag_indicators_status_update_bitmap = indicators_status_bitmap;
1195a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
11963deb3ec6SMatthias Ringwald }
11973deb3ec6SMatthias Ringwald 
1198c8626498SMilanka Ringwald void hfp_hf_query_operator_selection(hci_con_handle_t acl_handle){
11999c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1200a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1201a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12023deb3ec6SMatthias Ringwald         return;
12033deb3ec6SMatthias Ringwald     }
1204a0ffb263SMatthias Ringwald     switch (hfp_connection->hf_query_operator_state){
1205ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_NOT_SET:
1206a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SET_FORMAT;
1207ce263fc8SMatthias Ringwald             break;
1208ce263fc8SMatthias Ringwald         case HFP_HF_QUERY_OPERATOR_FORMAT_SET:
1209a0ffb263SMatthias Ringwald             hfp_connection->hf_query_operator_state = HFP_HF_QUERY_OPERATOR_SEND_QUERY;
1210ce263fc8SMatthias Ringwald             break;
1211ce263fc8SMatthias Ringwald         default:
1212ce263fc8SMatthias Ringwald             break;
1213ce263fc8SMatthias Ringwald     }
1214a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12153deb3ec6SMatthias Ringwald }
12163deb3ec6SMatthias Ringwald 
1217c8626498SMilanka Ringwald static void hfp_hf_set_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle, uint8_t enable){
12189c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1219a0ffb263SMatthias Ringwald     if (!hfp_connection) {
1220a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
12213deb3ec6SMatthias Ringwald         return;
12223deb3ec6SMatthias Ringwald     }
1223a0ffb263SMatthias Ringwald     hfp_connection->enable_extended_audio_gateway_error_report = enable;
1224a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12253deb3ec6SMatthias Ringwald }
12263deb3ec6SMatthias Ringwald 
1227ce263fc8SMatthias Ringwald 
1228c8626498SMilanka Ringwald void hfp_hf_enable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1229c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 1);
1230ce263fc8SMatthias Ringwald }
1231ce263fc8SMatthias Ringwald 
1232c8626498SMilanka Ringwald void hfp_hf_disable_report_extended_audio_gateway_error_result_code(hci_con_handle_t acl_handle){
1233c8626498SMilanka Ringwald     hfp_hf_set_report_extended_audio_gateway_error_result_code(acl_handle, 0);
1234ce263fc8SMatthias Ringwald }
1235ce263fc8SMatthias Ringwald 
1236ce263fc8SMatthias Ringwald 
1237c8626498SMilanka Ringwald void hfp_hf_establish_audio_connection(hci_con_handle_t acl_handle){
12389c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1239a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1240a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1241a33eb0c4SMilanka Ringwald         return;
1242a33eb0c4SMilanka Ringwald     }
1243a0ffb263SMatthias Ringwald     hfp_connection->establish_audio_connection = 0;
1244ce263fc8SMatthias Ringwald 
1245a0ffb263SMatthias Ringwald     if (hfp_connection->state == HFP_AUDIO_CONNECTION_ESTABLISHED) return;
1246a0ffb263SMatthias Ringwald     if (hfp_connection->state >= HFP_W2_DISCONNECT_SCO) return;
12473deb3ec6SMatthias Ringwald 
1248a0ffb263SMatthias Ringwald     if (!has_codec_negotiation_feature(hfp_connection)){
1249ce263fc8SMatthias Ringwald         log_info("hfp_ag_establish_audio_connection - no codec negotiation feature, using defaults");
1250a0ffb263SMatthias Ringwald         hfp_connection->codecs_state = HFP_CODECS_EXCHANGED;
1251a0ffb263SMatthias Ringwald         hfp_connection->establish_audio_connection = 1;
1252ce263fc8SMatthias Ringwald     } else {
1253a0ffb263SMatthias Ringwald         switch (hfp_connection->codecs_state){
1254aa4dd815SMatthias Ringwald             case HFP_CODECS_W4_AG_COMMON_CODEC:
1255aa4dd815SMatthias Ringwald                 break;
1256aa4dd815SMatthias Ringwald             default:
1257a0ffb263SMatthias Ringwald                 hfp_connection->command = HFP_CMD_TRIGGER_CODEC_CONNECTION_SETUP;
1258aa4dd815SMatthias Ringwald                 break;
12593deb3ec6SMatthias Ringwald         }
1260ce263fc8SMatthias Ringwald     }
1261ce263fc8SMatthias Ringwald 
1262a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12633deb3ec6SMatthias Ringwald }
12643deb3ec6SMatthias Ringwald 
1265c8626498SMilanka Ringwald void hfp_hf_release_audio_connection(hci_con_handle_t acl_handle){
12669c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1267a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1268a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1269a33eb0c4SMilanka Ringwald         return;
1270a33eb0c4SMilanka Ringwald     }
1271a0ffb263SMatthias Ringwald     hfp_release_audio_connection(hfp_connection);
1272a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
12733deb3ec6SMatthias Ringwald }
12743deb3ec6SMatthias Ringwald 
1275c8626498SMilanka Ringwald void hfp_hf_answer_incoming_call(hci_con_handle_t acl_handle){
12769c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1277a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1278a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1279a33eb0c4SMilanka Ringwald         return;
1280a33eb0c4SMilanka Ringwald     }
1281ce263fc8SMatthias Ringwald 
1282ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1283a0ffb263SMatthias Ringwald         hfp_connection->hf_answer_incoming_call = 1;
1284a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1285ce263fc8SMatthias Ringwald     } else {
1286ce263fc8SMatthias Ringwald         log_error("HFP HF: answering incoming call with wrong callsetup status %u", hfp_callsetup_status);
1287ce263fc8SMatthias Ringwald     }
1288ce263fc8SMatthias Ringwald }
1289ce263fc8SMatthias Ringwald 
1290c8626498SMilanka Ringwald void hfp_hf_terminate_call(hci_con_handle_t acl_handle){
12919c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1292a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1293a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1294a33eb0c4SMilanka Ringwald         return;
1295a33eb0c4SMilanka Ringwald     }
1296a0ffb263SMatthias Ringwald     hfp_connection->hf_send_chup = 1;
1297a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1298ce263fc8SMatthias Ringwald }
1299ce263fc8SMatthias Ringwald 
1300c8626498SMilanka Ringwald void hfp_hf_reject_incoming_call(hci_con_handle_t acl_handle){
13019c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1302a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1303a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1304a33eb0c4SMilanka Ringwald         return;
1305a33eb0c4SMilanka Ringwald     }
1306ce263fc8SMatthias Ringwald 
1307ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1308a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chup = 1;
1309a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1310ce263fc8SMatthias Ringwald     }
1311ce263fc8SMatthias Ringwald }
1312ce263fc8SMatthias Ringwald 
1313c8626498SMilanka Ringwald void hfp_hf_user_busy(hci_con_handle_t acl_handle){
13149c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1315a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1316a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1317a33eb0c4SMilanka Ringwald         return;
1318a33eb0c4SMilanka Ringwald     }
1319ce263fc8SMatthias Ringwald 
1320ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS){
1321a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_0 = 1;
1322a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1323ce263fc8SMatthias Ringwald     }
1324ce263fc8SMatthias Ringwald }
1325ce263fc8SMatthias Ringwald 
1326c8626498SMilanka Ringwald void hfp_hf_end_active_and_accept_other(hci_con_handle_t acl_handle){
13279c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1328a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1329a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1330a33eb0c4SMilanka Ringwald         return;
1331a33eb0c4SMilanka Ringwald     }
1332ce263fc8SMatthias Ringwald 
1333ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1334ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1335a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_1 = 1;
1336a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1337ce263fc8SMatthias Ringwald     }
1338ce263fc8SMatthias Ringwald }
1339ce263fc8SMatthias Ringwald 
1340c8626498SMilanka Ringwald void hfp_hf_swap_calls(hci_con_handle_t acl_handle){
13419c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1342a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1343a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1344a33eb0c4SMilanka Ringwald         return;
1345a33eb0c4SMilanka Ringwald     }
1346ce263fc8SMatthias Ringwald 
1347ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1348ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1349a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_2 = 1;
1350a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1351ce263fc8SMatthias Ringwald     }
1352ce263fc8SMatthias Ringwald }
1353ce263fc8SMatthias Ringwald 
1354c8626498SMilanka Ringwald void hfp_hf_join_held_call(hci_con_handle_t acl_handle){
13559c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1356a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1357a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1358a33eb0c4SMilanka Ringwald         return;
1359a33eb0c4SMilanka Ringwald     }
1360ce263fc8SMatthias Ringwald 
1361ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1362ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1363a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_3 = 1;
1364a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1365ce263fc8SMatthias Ringwald     }
1366ce263fc8SMatthias Ringwald }
1367ce263fc8SMatthias Ringwald 
1368c8626498SMilanka Ringwald void hfp_hf_connect_calls(hci_con_handle_t acl_handle){
13699c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1370a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1371a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1372a33eb0c4SMilanka Ringwald         return;
1373a33eb0c4SMilanka Ringwald     }
1374ce263fc8SMatthias Ringwald 
1375ce263fc8SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1376ce263fc8SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1377a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_4 = 1;
1378a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1379ce263fc8SMatthias Ringwald     }
1380ce263fc8SMatthias Ringwald }
1381ce263fc8SMatthias Ringwald 
1382c8626498SMilanka Ringwald void hfp_hf_release_call_with_index(hci_con_handle_t acl_handle, int index){
13839c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1384a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1385a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1386a33eb0c4SMilanka Ringwald         return;
1387a33eb0c4SMilanka Ringwald     }
1388667ec068SMatthias Ringwald 
1389667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1390667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1391a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1392a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 10 + index;
1393a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1394667ec068SMatthias Ringwald     }
1395667ec068SMatthias Ringwald }
1396667ec068SMatthias Ringwald 
1397c8626498SMilanka Ringwald void hfp_hf_private_consultation_with_call(hci_con_handle_t acl_handle, int index){
13989c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1399a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1400a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1401a33eb0c4SMilanka Ringwald         return;
1402a33eb0c4SMilanka Ringwald     }
1403667ec068SMatthias Ringwald 
1404667ec068SMatthias Ringwald     if (hfp_callsetup_status == HFP_CALLSETUP_STATUS_INCOMING_CALL_SETUP_IN_PROGRESS ||
1405667ec068SMatthias Ringwald         hfp_call_status == HFP_CALL_STATUS_ACTIVE_OR_HELD_CALL_IS_PRESENT){
1406a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x = 1;
1407a0ffb263SMatthias Ringwald         hfp_connection->hf_send_chld_x_index = 20 + index;
1408a0ffb263SMatthias Ringwald         hfp_run_for_context(hfp_connection);
1409667ec068SMatthias Ringwald     }
1410667ec068SMatthias Ringwald }
1411ce263fc8SMatthias Ringwald 
1412c8626498SMilanka Ringwald void hfp_hf_dial_number(hci_con_handle_t acl_handle, char * number){
14139c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1414a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1415a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1416a33eb0c4SMilanka Ringwald         return;
1417a33eb0c4SMilanka Ringwald     }
1418ce263fc8SMatthias Ringwald 
1419a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_outgoing_call = 1;
1420ce263fc8SMatthias Ringwald     snprintf(phone_number, sizeof(phone_number), "%s", number);
1421a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1422ce263fc8SMatthias Ringwald }
1423ce263fc8SMatthias Ringwald 
1424c8626498SMilanka Ringwald void hfp_hf_dial_memory(hci_con_handle_t acl_handle, int memory_id){
14259c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1426a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1427a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1428a33eb0c4SMilanka Ringwald         return;
1429a33eb0c4SMilanka Ringwald     }
1430ce263fc8SMatthias Ringwald 
1431a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_memory_dialing = 1;
1432a0ffb263SMatthias Ringwald     hfp_connection->memory_id = memory_id;
1433a0ffb263SMatthias Ringwald 
1434a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1435ce263fc8SMatthias Ringwald }
1436ce263fc8SMatthias Ringwald 
1437c8626498SMilanka Ringwald void hfp_hf_redial_last_number(hci_con_handle_t acl_handle){
14389c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1439a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1440a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1441a33eb0c4SMilanka Ringwald         return;
1442a33eb0c4SMilanka Ringwald     }
1443ce263fc8SMatthias Ringwald 
1444a0ffb263SMatthias Ringwald     hfp_connection->hf_initiate_redial_last_number = 1;
1445a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1446ce263fc8SMatthias Ringwald }
1447ce263fc8SMatthias Ringwald 
1448c8626498SMilanka Ringwald void hfp_hf_activate_call_waiting_notification(hci_con_handle_t acl_handle){
14499c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1450a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1451a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1452a33eb0c4SMilanka Ringwald         return;
1453a33eb0c4SMilanka Ringwald     }
1454ce263fc8SMatthias Ringwald 
1455a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_call_waiting_notification = 1;
1456a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1457ce263fc8SMatthias Ringwald }
1458ce263fc8SMatthias Ringwald 
1459ce263fc8SMatthias Ringwald 
1460c8626498SMilanka Ringwald void hfp_hf_deactivate_call_waiting_notification(hci_con_handle_t acl_handle){
14619c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1462a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1463a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1464a33eb0c4SMilanka Ringwald         return;
1465a33eb0c4SMilanka Ringwald     }
1466ce263fc8SMatthias Ringwald 
1467a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_call_waiting_notification = 1;
1468a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1469ce263fc8SMatthias Ringwald }
1470ce263fc8SMatthias Ringwald 
1471ce263fc8SMatthias Ringwald 
1472c8626498SMilanka Ringwald void hfp_hf_activate_calling_line_notification(hci_con_handle_t acl_handle){
14739c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1474a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1475a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1476a33eb0c4SMilanka Ringwald         return;
1477a33eb0c4SMilanka Ringwald     }
1478ce263fc8SMatthias Ringwald 
1479a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_calling_line_notification = 1;
1480a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1481ce263fc8SMatthias Ringwald }
1482ce263fc8SMatthias Ringwald 
1483c8626498SMilanka Ringwald void hfp_hf_deactivate_calling_line_notification(hci_con_handle_t acl_handle){
14849c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1485a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1486a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1487a33eb0c4SMilanka Ringwald         return;
1488a33eb0c4SMilanka Ringwald     }
1489ce263fc8SMatthias Ringwald 
1490a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_calling_line_notification = 1;
1491a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1492ce263fc8SMatthias Ringwald }
1493ce263fc8SMatthias Ringwald 
1494ce263fc8SMatthias Ringwald 
1495c8626498SMilanka Ringwald void hfp_hf_activate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
14969c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1497a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1498a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1499a33eb0c4SMilanka Ringwald         return;
1500a33eb0c4SMilanka Ringwald     }
1501ce263fc8SMatthias Ringwald 
1502a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_echo_canceling_and_noise_reduction = 1;
1503a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1504ce263fc8SMatthias Ringwald }
1505ce263fc8SMatthias Ringwald 
1506c8626498SMilanka Ringwald void hfp_hf_deactivate_echo_canceling_and_noise_reduction(hci_con_handle_t acl_handle){
15079c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1508a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1509a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1510a33eb0c4SMilanka Ringwald         return;
1511a33eb0c4SMilanka Ringwald     }
1512ce263fc8SMatthias Ringwald 
1513a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_echo_canceling_and_noise_reduction = 1;
1514a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1515ce263fc8SMatthias Ringwald }
1516ce263fc8SMatthias Ringwald 
1517c8626498SMilanka Ringwald void hfp_hf_activate_voice_recognition_notification(hci_con_handle_t acl_handle){
15189c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1519a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1520a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1521a33eb0c4SMilanka Ringwald         return;
1522a33eb0c4SMilanka Ringwald     }
1523ce263fc8SMatthias Ringwald 
1524a0ffb263SMatthias Ringwald     hfp_connection->hf_activate_voice_recognition_notification = 1;
1525a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1526ce263fc8SMatthias Ringwald }
1527ce263fc8SMatthias Ringwald 
1528c8626498SMilanka Ringwald void hfp_hf_deactivate_voice_recognition_notification(hci_con_handle_t acl_handle){
15299c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1530a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1531a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1532a33eb0c4SMilanka Ringwald         return;
1533a33eb0c4SMilanka Ringwald     }
1534ce263fc8SMatthias Ringwald 
1535a0ffb263SMatthias Ringwald     hfp_connection->hf_deactivate_voice_recognition_notification = 1;
1536a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1537ce263fc8SMatthias Ringwald }
1538ce263fc8SMatthias Ringwald 
1539c8626498SMilanka Ringwald void hfp_hf_set_microphone_gain(hci_con_handle_t acl_handle, int gain){
15409c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1541a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1542a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1543a33eb0c4SMilanka Ringwald         return;
1544a33eb0c4SMilanka Ringwald     }
1545c8626498SMilanka Ringwald 
1546a0ffb263SMatthias Ringwald     if (hfp_connection->microphone_gain == gain) return;
1547a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1548a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1549a0ffb263SMatthias Ringwald         return;
1550a0ffb263SMatthias Ringwald     }
1551a0ffb263SMatthias Ringwald     hfp_connection->microphone_gain = gain;
1552a0ffb263SMatthias Ringwald     hfp_connection->send_microphone_gain = 1;
1553a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1554ce263fc8SMatthias Ringwald }
1555ce263fc8SMatthias Ringwald 
1556c8626498SMilanka Ringwald void hfp_hf_set_speaker_gain(hci_con_handle_t acl_handle, int gain){
15579c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1558a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1559a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1560a33eb0c4SMilanka Ringwald         return;
1561a33eb0c4SMilanka Ringwald     }
1562c8626498SMilanka Ringwald 
1563a0ffb263SMatthias Ringwald     if (hfp_connection->speaker_gain == gain) return;
1564a0ffb263SMatthias Ringwald     if (gain < 0 || gain > 15){
1565a0ffb263SMatthias Ringwald         log_info("Valid range for a gain is [0..15]. Currently sent: %d", gain);
1566a0ffb263SMatthias Ringwald         return;
1567a0ffb263SMatthias Ringwald     }
1568a0ffb263SMatthias Ringwald     hfp_connection->speaker_gain = gain;
1569a0ffb263SMatthias Ringwald     hfp_connection->send_speaker_gain = 1;
1570a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1571ce263fc8SMatthias Ringwald }
1572ce263fc8SMatthias Ringwald 
1573c8626498SMilanka Ringwald void hfp_hf_send_dtmf_code(hci_con_handle_t acl_handle, char code){
15749c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1575a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1576a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1577a33eb0c4SMilanka Ringwald         return;
1578a33eb0c4SMilanka Ringwald     }
1579a33eb0c4SMilanka Ringwald 
1580a0ffb263SMatthias Ringwald     hfp_connection->hf_send_dtmf_code = code;
1581a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1582ce263fc8SMatthias Ringwald }
1583ce263fc8SMatthias Ringwald 
1584c8626498SMilanka Ringwald void hfp_hf_request_phone_number_for_voice_tag(hci_con_handle_t acl_handle){
15859c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1586a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1587a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1588a33eb0c4SMilanka Ringwald         return;
1589a33eb0c4SMilanka Ringwald     }
1590a0ffb263SMatthias Ringwald     hfp_connection->hf_send_binp = 1;
1591a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1592ce263fc8SMatthias Ringwald }
15933deb3ec6SMatthias Ringwald 
1594c8626498SMilanka Ringwald void hfp_hf_query_current_call_status(hci_con_handle_t acl_handle){
15959c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1596a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1597a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1598a33eb0c4SMilanka Ringwald         return;
1599a33eb0c4SMilanka Ringwald     }
1600a0ffb263SMatthias Ringwald     hfp_connection->hf_send_clcc = 1;
1601a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1602667ec068SMatthias Ringwald }
1603667ec068SMatthias Ringwald 
1604667ec068SMatthias Ringwald 
1605c8626498SMilanka Ringwald void hfp_hf_rrh_query_status(hci_con_handle_t acl_handle){
16069c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1607a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1608a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1609a33eb0c4SMilanka Ringwald         return;
1610a33eb0c4SMilanka Ringwald     }
1611a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1612a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '?';
1613a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1614667ec068SMatthias Ringwald }
1615667ec068SMatthias Ringwald 
1616c8626498SMilanka Ringwald void hfp_hf_rrh_hold_call(hci_con_handle_t acl_handle){
16179c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1618a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1619a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1620a33eb0c4SMilanka Ringwald         return;
1621a33eb0c4SMilanka Ringwald     }
1622a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1623a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '0';
1624a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1625667ec068SMatthias Ringwald }
1626667ec068SMatthias Ringwald 
1627c8626498SMilanka Ringwald void hfp_hf_rrh_accept_held_call(hci_con_handle_t acl_handle){
16289c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1629a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1630a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1631a33eb0c4SMilanka Ringwald         return;
1632a33eb0c4SMilanka Ringwald     }
1633a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1634a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '1';
1635a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1636667ec068SMatthias Ringwald }
1637667ec068SMatthias Ringwald 
1638c8626498SMilanka Ringwald void hfp_hf_rrh_reject_held_call(hci_con_handle_t acl_handle){
16399c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1640a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1641a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1642a33eb0c4SMilanka Ringwald         return;
1643a33eb0c4SMilanka Ringwald     }
1644a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh = 1;
1645a0ffb263SMatthias Ringwald     hfp_connection->hf_send_rrh_command = '2';
1646a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1647667ec068SMatthias Ringwald }
1648667ec068SMatthias Ringwald 
1649c8626498SMilanka Ringwald void hfp_hf_query_subscriber_number(hci_con_handle_t acl_handle){
16509c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1651a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1652a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1653a33eb0c4SMilanka Ringwald         return;
1654a33eb0c4SMilanka Ringwald     }
1655a0ffb263SMatthias Ringwald     hfp_connection->hf_send_cnum = 1;
1656a0ffb263SMatthias Ringwald     hfp_run_for_context(hfp_connection);
1657667ec068SMatthias Ringwald }
1658667ec068SMatthias Ringwald 
1659c8626498SMilanka Ringwald void hfp_hf_set_hf_indicator(hci_con_handle_t acl_handle, int assigned_number, int value){
16609c9c64c1SMatthias Ringwald     hfp_connection_t * hfp_connection = get_hfp_hf_connection_context_for_acl_handle(acl_handle);
1661a33eb0c4SMilanka Ringwald     if (!hfp_connection) {
1662a33eb0c4SMilanka Ringwald         log_error("HFP HF: ACL handle 0x%2x is not found.", acl_handle);
1663a33eb0c4SMilanka Ringwald         return;
1664a33eb0c4SMilanka Ringwald     }
1665667ec068SMatthias Ringwald     // find index for assigned number
1666667ec068SMatthias Ringwald     int i;
1667667ec068SMatthias Ringwald     for (i = 0; i < hfp_indicators_nr ; i++){
1668667ec068SMatthias Ringwald         if (hfp_indicators[i] == assigned_number){
1669667ec068SMatthias Ringwald             // set value
1670667ec068SMatthias Ringwald             hfp_indicators_value[i] = value;
1671667ec068SMatthias Ringwald             // mark for update
1672a0ffb263SMatthias Ringwald             if (hfp_connection->state > HFP_LIST_GENERIC_STATUS_INDICATORS){
1673a0ffb263SMatthias Ringwald                 hfp_connection->generic_status_update_bitmap |= (1<<i);
1674667ec068SMatthias Ringwald                 // send update
1675a0ffb263SMatthias Ringwald                 hfp_run_for_context(hfp_connection);
1676a0ffb263SMatthias Ringwald             }
1677667ec068SMatthias Ringwald             return;
1678667ec068SMatthias Ringwald         }
1679667ec068SMatthias Ringwald     }
1680667ec068SMatthias Ringwald }
1681667ec068SMatthias Ringwald 
1682